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
36 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
43 * Mail -- a mail program
45 * Collect input from standard input, handling
54 * Read a message from standard output and return a read file to it
59 * The following hokiness with global variables is so that on
60 * receipt of an interrupt signal, the partial message can be salted
61 * away on dead.letter.
64 static sig_t saveint; /* Previous SIGINT value */
65 static sig_t savehup; /* Previous SIGHUP value */
66 static sig_t savetstp; /* Previous SIGTSTP value */
67 static sig_t savettou; /* Previous SIGTTOU value */
68 static sig_t savettin; /* Previous SIGTTIN value */
69 static FILE *collf; /* File for saving away */
70 static int hadintr; /* Have seen one SIGINT so far */
72 static jmp_buf colljmp; /* To get back to work */
73 static int colljmp_p; /* whether to long jump */
74 static jmp_buf collabort; /* To end collection with error */
77 collect(hp, printheaders)
82 int lc, cc, escape, eofcount, fd, c, t;
83 char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
85 int longline, lastlong, rc; /* So we don't make 2 or more lines
86 out of a long input line. */
90 * Start catching signals from here, but we're still die on interrupts
91 * until we're in the main loop.
93 (void)sigemptyset(&nset);
94 (void)sigaddset(&nset, SIGINT);
95 (void)sigaddset(&nset, SIGHUP);
96 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
97 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
98 (void)signal(SIGINT, collint);
99 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
100 (void)signal(SIGHUP, collhup);
101 savetstp = signal(SIGTSTP, collstop);
102 savettou = signal(SIGTTOU, collstop);
103 savettin = signal(SIGTTIN, collstop);
104 if (setjmp(collabort) || setjmp(colljmp)) {
108 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
111 (void)snprintf(tempname, sizeof(tempname),
112 "%s/mail.RsXXXXXXXXXX", tmpdir);
113 if ((fd = mkstemp(tempname)) == -1 ||
114 (collf = Fdopen(fd, "w+")) == NULL) {
115 warn("%s", tempname);
121 * If we are going to prompt for a subject,
122 * refrain from printing a newline after
123 * the headers (since some people mind).
125 t = GTO|GSUBJECT|GCC|GNL;
127 if (hp->h_subject == NULL && value("interactive") != NULL &&
128 (value("ask") != NULL || value("asksub") != NULL))
131 puthead(hp, stdout, t);
132 (void)fflush(stdout);
134 if ((cp = value("escape")) != NULL)
143 if (!setjmp(colljmp)) {
148 * Come here for printing the after-signal message.
149 * Duplicate messages won't be printed because
150 * the write is aborted if we get a SIGTTOU.
154 (void)fflush(stdout);
156 "\n(Interrupt -- one more to kill letter)\n");
158 printf("(continue)\n");
159 (void)fflush(stdout);
164 c = readline(stdin, linebuf, LINESIZE);
167 if (value("interactive") != NULL &&
168 value("ignoreeof") != NULL && ++eofcount < 25) {
169 printf("Use \".\" to terminate letter\n");
175 longline = c == LINESIZE - 1;
178 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
179 value("interactive") != NULL && !lastlong &&
180 (value("dot") != NULL || value("ignoreeof") != NULL))
182 if (linebuf[0] != escape || value("interactive") == NULL ||
184 if (putline(collf, linebuf, !longline) < 0)
192 * On double escape, just send the single one.
193 * Otherwise, it's an error.
196 if (putline(collf, &linebuf[1], !longline) < 0)
201 printf("Unknown tilde escape.\n");
211 * Shell escape, send the balance of the
219 * Escape to command mode, but be nice!
221 execute(&linebuf[2], 1);
225 * Simulate end of file on input.
230 * Force a quit of sending mail.
231 * Act like an interrupt happened.
238 * Exit, do not save in dead.letter.
243 * Grab a bunch of headers.
245 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
249 * Add to the To list.
251 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
255 * Set the Subject line.
258 while (isspace((unsigned char)*cp))
260 hp->h_subject = savestr(cp);
264 * Set the Reply-To line.
267 while (isspace((unsigned char)*cp))
269 hp->h_replyto = savestr(cp);
273 * Add to the CC list.
275 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
279 * Add to the BCC list.
281 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
287 * Insert named variable in message.
292 while(isspace((unsigned char)*cp))
305 if(*cp != '\0' && (cp = value(cp)) != NULL) {
307 if(putline(collf, cp, 1) < 0)
314 * Read in the dead letter file.
316 if (strlcpy(linebuf + 2, getdeadletter(),
318 >= sizeof(linebuf) - 2) {
319 printf("Line buffer overflow\n");
327 * Search for the file name,
328 * then open it and copy the contents to collf.
331 while (isspace((unsigned char)*cp))
334 printf("Interpolate what file?\n");
342 * Insert stdout of command.
345 int nullfd, tempfd, rc;
346 char tempname2[PATHSIZE];
348 if ((nullfd = open("/dev/null", O_RDONLY, 0))
354 (void)snprintf(tempname2, sizeof(tempname2),
355 "%s/mail.ReXXXXXXXXXX", tmpdir);
356 if ((tempfd = mkstemp(tempname2)) == -1 ||
357 (fbuf = Fdopen(tempfd, "w+")) == NULL) {
358 warn("%s", tempname2);
361 (void)unlink(tempname2);
363 if ((sh = value("SHELL")) == NULL)
366 rc = run_command(sh, 0, nullfd, fileno(fbuf),
376 if (fsize(fbuf) == 0) {
378 "No bytes from command \"%s\"\n",
385 } else if (isdir(cp)) {
386 printf("%s: Directory\n", cp);
388 } else if ((fbuf = Fopen(cp, "r")) == NULL) {
392 printf("\"%s\" ", cp);
393 (void)fflush(stdout);
396 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
397 if (rc != LINESIZE - 1)
399 if ((t = putline(collf, linebuf,
400 rc != LINESIZE - 1)) < 0) {
407 printf("%d/%d\n", lc, cc);
411 * Write the message on a file.
414 while (*cp == ' ' || *cp == '\t')
417 fprintf(stderr, "Write what file!?\n");
420 if ((cp = expand(cp)) == NULL)
423 exwrite(cp, collf, 1);
430 * Interpolate the named messages, if we
431 * are in receiving mail mode. Does the
432 * standard list processing garbage.
433 * If ~f is given, we don't shift over.
435 if (forward(linebuf + 2, collf, tempname, c) < 0)
439 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
440 warn("%s", _PATH_TILDE);
443 while ((t = getc(fbuf)) != EOF)
449 * Print out the current state of the
450 * message without altering anything.
453 printf("-------\nMessage contains:\n");
454 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
455 while ((t = getc(collf)) != EOF)
460 * Pipe message through command.
461 * Collect output as new message.
464 mespipe(collf, &linebuf[2]);
469 * Edit the current message.
470 * 'e' means to use EDITOR
471 * 'v' means to use VISUAL
488 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
489 (void)signal(SIGINT, saveint);
490 (void)signal(SIGHUP, savehup);
491 (void)signal(SIGTSTP, savetstp);
492 (void)signal(SIGTTOU, savettou);
493 (void)signal(SIGTTIN, savettin);
494 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
499 * Write a file, ex-like if f set.
513 printf("\"%s\" ", name);
514 (void)fflush(stdout);
516 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
518 fprintf(stderr, "%s: ", name);
519 fprintf(stderr, "File exists\n");
522 if ((of = Fopen(name, "w")) == NULL) {
528 while ((c = getc(fp)) != EOF) {
540 printf("%d/%ld\n", lc, cc);
541 (void)fflush(stdout);
546 * Edit the message being collected on fp.
547 * On return, make the edit file the new temp file.
554 sig_t sigint = signal(SIGINT, SIG_IGN);
555 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
558 (void)fseeko(nf, (off_t)0, SEEK_END);
562 (void)signal(SIGINT, sigint);
566 * Pipe the message through the command.
567 * Old message is on stdin of command;
568 * New message collected from stdout.
569 * Sh -c must return 0 to accept the new message.
578 sig_t sigint = signal(SIGINT, SIG_IGN);
579 char *sh, tempname[PATHSIZE];
581 (void)snprintf(tempname, sizeof(tempname),
582 "%s/mail.ReXXXXXXXXXX", tmpdir);
583 if ((fd = mkstemp(tempname)) == -1 ||
584 (nf = Fdopen(fd, "w+")) == NULL) {
585 warn("%s", tempname);
590 * stdin = current message.
591 * stdout = new message.
593 if ((sh = value("SHELL")) == NULL)
596 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
600 if (fsize(nf) == 0) {
601 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
608 (void)fseeko(nf, (off_t)0, SEEK_END);
612 (void)signal(SIGINT, sigint);
616 * Interpolate the named messages into the current
617 * message, preceding each line with a tab.
618 * Return a count of the number of characters now in
619 * the message, or -1 if an error is encountered writing
620 * the message temporary. The flag argument is 'm' if we
621 * should shift over and 'f' if not.
624 forward(ms, fp, fn, f)
631 struct ignoretab *ig;
634 msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
637 if (getmsglist(ms, msgvec, 0) < 0)
640 *msgvec = first(0, MMNORM);
642 printf("No appropriate messages\n");
647 if (f == 'f' || f == 'F')
649 else if ((tabst = value("indentprefix")) == NULL)
651 ig = isupper((unsigned char)f) ? NULL : ignore;
652 printf("Interpolating:");
653 for (; *msgvec != 0; msgvec++) {
654 struct message *mp = message + *msgvec - 1;
657 printf(" %d", *msgvec);
658 if (sendmessage(mp, fp, ig, tabst) < 0) {
668 * Print (continue) when continued after ^Z.
675 sig_t old_action = signal(s, SIG_DFL);
678 (void)sigemptyset(&nset);
679 (void)sigaddset(&nset, s);
680 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
682 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
683 (void)signal(s, old_action);
692 * On interrupt, come here to save the partial message in ~/dead.letter.
693 * Then jump out of the collection loop.
701 * the control flow is subtle, because we can be called from ~q.
704 if (value("ignore") != NULL) {
706 (void)fflush(stdout);
714 if (value("nosave") == NULL)
715 savedeadletter(collf);
716 longjmp(collabort, 1);
725 savedeadletter(collf);
727 * Let's pretend nobody else wants to clean up,
728 * a true statement at this time.
743 cp = getdeadletter();
745 dbuf = Fopen(cp, "a");
749 while ((c = getc(fp)) != EOF)