2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)collect.c 8.2 (Berkeley) 4/19/94
34 * $FreeBSD: src/usr.bin/mail/collect.c,v 1.4.6.3 2003/01/06 05:46:03 mikeh Exp $
35 * $DragonFly: src/usr.bin/mail/collect.c,v 1.2 2003/06/17 04:29:28 dillon Exp $
39 * Mail -- a mail program
41 * Collect input from standard input, handling
50 * Read a message from standard output and return a read file to it
55 * The following hokiness with global variables is so that on
56 * receipt of an interrupt signal, the partial message can be salted
57 * away on dead.letter.
60 static sig_t saveint; /* Previous SIGINT value */
61 static sig_t savehup; /* Previous SIGHUP value */
62 static sig_t savetstp; /* Previous SIGTSTP value */
63 static sig_t savettou; /* Previous SIGTTOU value */
64 static sig_t savettin; /* Previous SIGTTIN value */
65 static FILE *collf; /* File for saving away */
66 static int hadintr; /* Have seen one SIGINT so far */
68 static jmp_buf colljmp; /* To get back to work */
69 static int colljmp_p; /* whether to long jump */
70 static jmp_buf collabort; /* To end collection with error */
73 collect(hp, printheaders)
78 int lc, cc, escape, eofcount, fd, c, t;
79 char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
81 int longline, lastlong, rc; /* So we don't make 2 or more lines
82 out of a long input line. */
86 * Start catching signals from here, but we're still die on interrupts
87 * until we're in the main loop.
89 (void)sigemptyset(&nset);
90 (void)sigaddset(&nset, SIGINT);
91 (void)sigaddset(&nset, SIGHUP);
92 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
93 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
94 (void)signal(SIGINT, collint);
95 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
96 (void)signal(SIGHUP, collhup);
97 savetstp = signal(SIGTSTP, collstop);
98 savettou = signal(SIGTTOU, collstop);
99 savettin = signal(SIGTTIN, collstop);
100 if (setjmp(collabort) || setjmp(colljmp)) {
104 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
107 (void)snprintf(tempname, sizeof(tempname),
108 "%s/mail.RsXXXXXXXXXX", tmpdir);
109 if ((fd = mkstemp(tempname)) == -1 ||
110 (collf = Fdopen(fd, "w+")) == NULL) {
111 warn("%s", tempname);
117 * If we are going to prompt for a subject,
118 * refrain from printing a newline after
119 * the headers (since some people mind).
121 t = GTO|GSUBJECT|GCC|GNL;
123 if (hp->h_subject == NULL && value("interactive") != NULL &&
124 (value("ask") != NULL || value("asksub") != NULL))
127 puthead(hp, stdout, t);
128 (void)fflush(stdout);
130 if ((cp = value("escape")) != NULL)
139 if (!setjmp(colljmp)) {
144 * Come here for printing the after-signal message.
145 * Duplicate messages won't be printed because
146 * the write is aborted if we get a SIGTTOU.
150 (void)fflush(stdout);
152 "\n(Interrupt -- one more to kill letter)\n");
154 printf("(continue)\n");
155 (void)fflush(stdout);
160 c = readline(stdin, linebuf, LINESIZE);
163 if (value("interactive") != NULL &&
164 value("ignoreeof") != NULL && ++eofcount < 25) {
165 printf("Use \".\" to terminate letter\n");
171 longline = c == LINESIZE - 1;
174 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
175 value("interactive") != NULL && !lastlong &&
176 (value("dot") != NULL || value("ignoreeof") != NULL))
178 if (linebuf[0] != escape || value("interactive") == NULL ||
180 if (putline(collf, linebuf, !longline) < 0)
188 * On double escape, just send the single one.
189 * Otherwise, it's an error.
192 if (putline(collf, &linebuf[1], !longline) < 0)
197 printf("Unknown tilde escape.\n");
207 * Shell escape, send the balance of the
215 * Escape to command mode, but be nice!
217 execute(&linebuf[2], 1);
221 * Simulate end of file on input.
226 * Force a quit of sending mail.
227 * Act like an interrupt happened.
234 * Exit, do not save in dead.letter.
239 * Grab a bunch of headers.
241 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
245 * Add to the To list.
247 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
251 * Set the Subject line.
254 while (isspace((unsigned char)*cp))
256 hp->h_subject = savestr(cp);
260 * Set the Reply-To line.
263 while (isspace((unsigned char)*cp))
265 hp->h_replyto = savestr(cp);
269 * Add to the CC list.
271 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
275 * Add to the BCC list.
277 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
283 * Insert named variable in message.
288 while(isspace((unsigned char)*cp))
301 if(*cp != '\0' && (cp = value(cp)) != NULL) {
303 if(putline(collf, cp, 1) < 0)
310 * Read in the dead letter file.
312 if (strlcpy(linebuf + 2, getdeadletter(),
314 >= sizeof(linebuf) - 2) {
315 printf("Line buffer overflow\n");
323 * Search for the file name,
324 * then open it and copy the contents to collf.
327 while (isspace((unsigned char)*cp))
330 printf("Interpolate what file?\n");
338 * Insert stdout of command.
341 int nullfd, tempfd, rc;
342 char tempname2[PATHSIZE];
344 if ((nullfd = open("/dev/null", O_RDONLY, 0))
350 (void)snprintf(tempname2, sizeof(tempname2),
351 "%s/mail.ReXXXXXXXXXX", tmpdir);
352 if ((tempfd = mkstemp(tempname2)) == -1 ||
353 (fbuf = Fdopen(tempfd, "w+")) == NULL) {
354 warn("%s", tempname2);
357 (void)unlink(tempname2);
359 if ((sh = value("SHELL")) == NULL)
362 rc = run_command(sh, 0, nullfd, fileno(fbuf),
372 if (fsize(fbuf) == 0) {
374 "No bytes from command \"%s\"\n",
381 } else if (isdir(cp)) {
382 printf("%s: Directory\n", cp);
384 } else if ((fbuf = Fopen(cp, "r")) == NULL) {
388 printf("\"%s\" ", cp);
389 (void)fflush(stdout);
392 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
393 if (rc != LINESIZE - 1)
395 if ((t = putline(collf, linebuf,
396 rc != LINESIZE - 1)) < 0) {
403 printf("%d/%d\n", lc, cc);
407 * Write the message on a file.
410 while (*cp == ' ' || *cp == '\t')
413 fprintf(stderr, "Write what file!?\n");
416 if ((cp = expand(cp)) == NULL)
419 exwrite(cp, collf, 1);
426 * Interpolate the named messages, if we
427 * are in receiving mail mode. Does the
428 * standard list processing garbage.
429 * If ~f is given, we don't shift over.
431 if (forward(linebuf + 2, collf, tempname, c) < 0)
435 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
436 warn("%s", _PATH_TILDE);
439 while ((t = getc(fbuf)) != EOF)
445 * Print out the current state of the
446 * message without altering anything.
449 printf("-------\nMessage contains:\n");
450 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
451 while ((t = getc(collf)) != EOF)
456 * Pipe message through command.
457 * Collect output as new message.
460 mespipe(collf, &linebuf[2]);
465 * Edit the current message.
466 * 'e' means to use EDITOR
467 * 'v' means to use VISUAL
484 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
485 (void)signal(SIGINT, saveint);
486 (void)signal(SIGHUP, savehup);
487 (void)signal(SIGTSTP, savetstp);
488 (void)signal(SIGTTOU, savettou);
489 (void)signal(SIGTTIN, savettin);
490 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
495 * Write a file, ex-like if f set.
509 printf("\"%s\" ", name);
510 (void)fflush(stdout);
512 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
514 fprintf(stderr, "%s: ", name);
515 fprintf(stderr, "File exists\n");
518 if ((of = Fopen(name, "w")) == NULL) {
524 while ((c = getc(fp)) != EOF) {
536 printf("%d/%ld\n", lc, cc);
537 (void)fflush(stdout);
542 * Edit the message being collected on fp.
543 * On return, make the edit file the new temp file.
550 sig_t sigint = signal(SIGINT, SIG_IGN);
551 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
554 (void)fseeko(nf, (off_t)0, SEEK_END);
558 (void)signal(SIGINT, sigint);
562 * Pipe the message through the command.
563 * Old message is on stdin of command;
564 * New message collected from stdout.
565 * Sh -c must return 0 to accept the new message.
574 sig_t sigint = signal(SIGINT, SIG_IGN);
575 char *sh, tempname[PATHSIZE];
577 (void)snprintf(tempname, sizeof(tempname),
578 "%s/mail.ReXXXXXXXXXX", tmpdir);
579 if ((fd = mkstemp(tempname)) == -1 ||
580 (nf = Fdopen(fd, "w+")) == NULL) {
581 warn("%s", tempname);
586 * stdin = current message.
587 * stdout = new message.
589 if ((sh = value("SHELL")) == NULL)
592 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
596 if (fsize(nf) == 0) {
597 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
604 (void)fseeko(nf, (off_t)0, SEEK_END);
608 (void)signal(SIGINT, sigint);
612 * Interpolate the named messages into the current
613 * message, preceding each line with a tab.
614 * Return a count of the number of characters now in
615 * the message, or -1 if an error is encountered writing
616 * the message temporary. The flag argument is 'm' if we
617 * should shift over and 'f' if not.
620 forward(ms, fp, fn, f)
627 struct ignoretab *ig;
630 msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
633 if (getmsglist(ms, msgvec, 0) < 0)
636 *msgvec = first(0, MMNORM);
638 printf("No appropriate messages\n");
643 if (f == 'f' || f == 'F')
645 else if ((tabst = value("indentprefix")) == NULL)
647 ig = isupper((unsigned char)f) ? NULL : ignore;
648 printf("Interpolating:");
649 for (; *msgvec != 0; msgvec++) {
650 struct message *mp = message + *msgvec - 1;
653 printf(" %d", *msgvec);
654 if (sendmessage(mp, fp, ig, tabst) < 0) {
664 * Print (continue) when continued after ^Z.
671 sig_t old_action = signal(s, SIG_DFL);
674 (void)sigemptyset(&nset);
675 (void)sigaddset(&nset, s);
676 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
678 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
679 (void)signal(s, old_action);
688 * On interrupt, come here to save the partial message in ~/dead.letter.
689 * Then jump out of the collection loop.
697 * the control flow is subtle, because we can be called from ~q.
700 if (value("ignore") != NULL) {
702 (void)fflush(stdout);
710 if (value("nosave") == NULL)
711 savedeadletter(collf);
712 longjmp(collabort, 1);
721 savedeadletter(collf);
723 * Let's pretend nobody else wants to clean up,
724 * a true statement at this time.
739 cp = getdeadletter();
741 dbuf = Fopen(cp, "a");
745 while ((c = getc(fp)) != EOF)