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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)collect.c 8.2 (Berkeley) 4/19/94";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 * Mail -- a mail program
41 * Collect input from standard input, handling
50 * Read a message from standard input 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(struct header *hp, int printheaders)
76 int lc, cc, escape, eofcount, fd, c, t;
77 char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
79 int longline, lastlong, rc; /* So we don't make 2 or more lines
80 out of a long input line. */
84 * Start catching signals from here, but we're still die on interrupts
85 * until we're in the main loop.
87 (void)sigemptyset(&nset);
88 (void)sigaddset(&nset, SIGINT);
89 (void)sigaddset(&nset, SIGHUP);
90 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
91 if ((saveint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
92 (void)signal(SIGINT, collint);
93 if ((savehup = signal(SIGHUP, SIG_IGN)) != SIG_IGN)
94 (void)signal(SIGHUP, collhup);
95 savetstp = signal(SIGTSTP, collstop);
96 savettou = signal(SIGTTOU, collstop);
97 savettin = signal(SIGTTIN, collstop);
98 if (setjmp(collabort) || setjmp(colljmp)) {
102 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
105 (void)snprintf(tempname, sizeof(tempname),
106 "%s/mail.RsXXXXXXXXXX", tmpdir);
107 if ((fd = mkstemp(tempname)) == -1 ||
108 (collf = Fdopen(fd, "w+")) == NULL) {
109 warn("%s", tempname);
115 * If we are going to prompt for a subject,
116 * refrain from printing a newline after
117 * the headers (since some people mind).
119 t = GTO|GSUBJECT|GCC|GNL;
121 if (hp->h_subject == NULL && value("interactive") != NULL &&
122 (value("ask") != NULL || value("asksub") != NULL))
125 puthead(hp, stdout, t);
126 (void)fflush(stdout);
128 if ((cp = value("escape")) != NULL)
137 if (!setjmp(colljmp)) {
142 * Come here for printing the after-signal message.
143 * Duplicate messages won't be printed because
144 * the write is aborted if we get a SIGTTOU.
148 (void)fflush(stdout);
150 "\n(Interrupt -- one more to kill letter)\n");
152 printf("(continue)\n");
153 (void)fflush(stdout);
158 c = readline(stdin, linebuf, LINESIZE);
161 if (value("interactive") != NULL &&
162 value("ignoreeof") != NULL && ++eofcount < 25) {
163 printf("Use \".\" to terminate letter\n");
169 longline = c == LINESIZE - 1;
172 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
173 value("interactive") != NULL && !lastlong &&
174 (value("dot") != NULL || value("ignoreeof") != NULL))
176 if (linebuf[0] != escape || value("interactive") == NULL ||
178 if (putline(collf, linebuf, !longline) < 0)
186 * On double escape, just send the single one.
187 * Otherwise, it's an error.
190 if (putline(collf, &linebuf[1], !longline) < 0)
195 printf("Unknown tilde escape.\n");
205 * Shell escape, send the balance of the
213 * Escape to command mode, but be nice!
215 execute(&linebuf[2], 1);
219 * Simulate end of file on input.
224 * Force a quit of sending mail.
225 * Act like an interrupt happened.
232 * Exit, do not save in dead.letter.
237 * Grab a bunch of headers.
239 grabh(hp, GTO|GSUBJECT|GCC|GBCC);
243 * Add to the To list.
245 hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
249 * Set the Subject line.
252 while (isspace((unsigned char)*cp))
254 hp->h_subject = savestr(cp);
258 * Set the Reply-To line.
261 while (isspace((unsigned char)*cp))
263 hp->h_replyto = savestr(cp);
267 * Add to the CC list.
269 hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
273 * Add to the BCC list.
275 hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
281 * Insert named variable in message.
286 while(isspace((unsigned char)*cp))
299 if(*cp != '\0' && (cp = value(cp)) != NULL) {
301 if(putline(collf, cp, 1) < 0)
308 * Read in the dead letter file.
310 if (strlcpy(linebuf + 2, getdeadletter(),
312 >= sizeof(linebuf) - 2) {
313 printf("Line buffer overflow\n");
321 * Search for the file name,
322 * then open it and copy the contents to collf.
325 while (isspace((unsigned char)*cp))
328 printf("Interpolate what file?\n");
336 * Insert stdout of command.
339 int nullfd, tempfd, rc;
340 char tempname2[PATHSIZE];
342 if ((nullfd = open(_PATH_DEVNULL, O_RDONLY, 0))
348 (void)snprintf(tempname2, sizeof(tempname2),
349 "%s/mail.ReXXXXXXXXXX", tmpdir);
350 if ((tempfd = mkstemp(tempname2)) == -1 ||
351 (fbuf = Fdopen(tempfd, "w+")) == NULL) {
352 warn("%s", tempname2);
355 (void)unlink(tempname2);
357 if ((sh = value("SHELL")) == NULL)
360 rc = run_command(sh, 0, nullfd, fileno(fbuf),
370 if (fsize(fbuf) == 0) {
372 "No bytes from command \"%s\"\n",
379 } else if (isdir(cp)) {
380 printf("%s: Directory\n", cp);
382 } else if ((fbuf = Fopen(cp, "r")) == NULL) {
386 printf("\"%s\" ", cp);
387 (void)fflush(stdout);
390 while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
391 if (rc != LINESIZE - 1)
393 if ((t = putline(collf, linebuf,
394 rc != LINESIZE - 1)) < 0) {
401 printf("%d/%d\n", lc, cc);
405 * Write the message on a file.
408 while (*cp == ' ' || *cp == '\t')
411 fprintf(stderr, "Write what file!?\n");
414 if ((cp = expand(cp)) == NULL)
417 exwrite(cp, collf, 1);
424 * Interpolate the named messages, if we
425 * are in receiving mail mode. Does the
426 * standard list processing garbage.
427 * If ~f is given, we don't shift over.
429 if (forward(linebuf + 2, collf, tempname, c) < 0)
433 if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
434 warn("%s", _PATH_TILDE);
437 while ((t = getc(fbuf)) != EOF)
443 * Print out the current state of the
444 * message without altering anything.
447 printf("-------\nMessage contains:\n");
448 puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
449 while ((t = getc(collf)) != EOF)
454 * Pipe message through command.
455 * Collect output as new message.
458 mespipe(collf, &linebuf[2]);
463 * Edit the current message.
464 * 'e' means to use EDITOR
465 * 'v' means to use VISUAL
482 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
483 (void)signal(SIGINT, saveint);
484 (void)signal(SIGHUP, savehup);
485 (void)signal(SIGTSTP, savetstp);
486 (void)signal(SIGTTOU, savettou);
487 (void)signal(SIGTTIN, savettin);
488 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
493 * Write a file, ex-like if f set.
496 exwrite(char name[], FILE *fp, int f)
504 printf("\"%s\" ", name);
505 (void)fflush(stdout);
507 if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
509 fprintf(stderr, "%s: ", name);
510 fprintf(stderr, "File exists\n");
513 if ((of = Fopen(name, "w")) == NULL) {
519 while ((c = getc(fp)) != EOF) {
531 printf("%d/%ld\n", lc, cc);
532 (void)fflush(stdout);
537 * Edit the message being collected on fp.
538 * On return, make the edit file the new temp file.
541 mesedit(FILE *fp, int c)
543 sig_t sigint = signal(SIGINT, SIG_IGN);
544 FILE *nf = run_editor(fp, (off_t)-1, c, 0);
547 (void)fseeko(nf, (off_t)0, SEEK_END);
551 (void)signal(SIGINT, sigint);
555 * Pipe the message through the command.
556 * Old message is on stdin of command;
557 * New message collected from stdout.
558 * Sh -c must return 0 to accept the new message.
561 mespipe(FILE *fp, char cmd[])
565 sig_t sigint = signal(SIGINT, SIG_IGN);
566 char *sh, tempname[PATHSIZE];
568 (void)snprintf(tempname, sizeof(tempname),
569 "%s/mail.ReXXXXXXXXXX", tmpdir);
570 if ((fd = mkstemp(tempname)) == -1 ||
571 (nf = Fdopen(fd, "w+")) == NULL) {
572 warn("%s", tempname);
577 * stdin = current message.
578 * stdout = new message.
580 if ((sh = value("SHELL")) == NULL)
583 0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
587 if (fsize(nf) == 0) {
588 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
595 (void)fseeko(nf, (off_t)0, SEEK_END);
599 (void)signal(SIGINT, sigint);
603 * Interpolate the named messages into the current
604 * message, preceding each line with a tab.
605 * Return a count of the number of characters now in
606 * the message, or -1 if an error is encountered writing
607 * the message temporary. The flag argument is 'm' if we
608 * should shift over and 'f' if not.
611 forward(char ms[], FILE *fp, char *fn, int f)
614 struct ignoretab *ig;
617 msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
620 if (getmsglist(ms, msgvec, 0) < 0)
623 *msgvec = first(0, MMNORM);
625 printf("No appropriate messages\n");
630 if (f == 'f' || f == 'F')
632 else if ((tabst = value("indentprefix")) == NULL)
634 ig = isupper((unsigned char)f) ? NULL : ignore;
635 printf("Interpolating:");
636 for (; *msgvec != 0; msgvec++) {
637 struct message *mp = message + *msgvec - 1;
640 printf(" %d", *msgvec);
641 if (sendmessage(mp, fp, ig, tabst) < 0) {
651 * Print (continue) when continued after ^Z.
657 sig_t old_action = signal(s, SIG_DFL);
660 (void)sigemptyset(&nset);
661 (void)sigaddset(&nset, s);
662 (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
664 (void)sigprocmask(SIG_BLOCK, &nset, NULL);
665 (void)signal(s, old_action);
674 * On interrupt, come here to save the partial message in ~/dead.letter.
675 * Then jump out of the collection loop.
679 collint(int s __unused)
682 * the control flow is subtle, because we can be called from ~q.
685 if (value("ignore") != NULL) {
687 (void)fflush(stdout);
695 if (value("nosave") == NULL)
696 savedeadletter(collf);
697 longjmp(collabort, 1);
702 collhup(int s __unused)
705 savedeadletter(collf);
707 * Let's pretend nobody else wants to clean up,
708 * a true statement at this time.
714 savedeadletter(FILE *fp)
722 cp = getdeadletter();
724 dbuf = Fopen(cp, "a");
728 while ((c = getc(fp)) != EOF)