]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/mail/collect.c
Merge clang trunk r321017 to contrib/llvm/tools/clang.
[FreeBSD/FreeBSD.git] / usr.bin / mail / collect.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1980, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 #if 0
34 static char sccsid[] = "@(#)collect.c   8.2 (Berkeley) 4/19/94";
35 #endif
36 #endif /* not lint */
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39
40 /*
41  * Mail -- a mail program
42  *
43  * Collect input from standard input, handling
44  * ~ escapes.
45  */
46
47 #include "rcv.h"
48 #include <fcntl.h>
49 #include "extern.h"
50
51 /*
52  * Read a message from standard input and return a read file to it
53  * or NULL on error.
54  */
55
56 /*
57  * The following hokiness with global variables is so that on
58  * receipt of an interrupt signal, the partial message can be salted
59  * away on dead.letter.
60  */
61
62 static  sig_t   saveint;                /* Previous SIGINT value */
63 static  sig_t   savehup;                /* Previous SIGHUP value */
64 static  sig_t   savetstp;               /* Previous SIGTSTP value */
65 static  sig_t   savettou;               /* Previous SIGTTOU value */
66 static  sig_t   savettin;               /* Previous SIGTTIN value */
67 static  FILE    *collf;                 /* File for saving away */
68 static  int     hadintr;                /* Have seen one SIGINT so far */
69
70 static  jmp_buf colljmp;                /* To get back to work */
71 static  int     colljmp_p;              /* whether to long jump */
72 static  jmp_buf collabort;              /* To end collection with error */
73
74 FILE *
75 collect(struct header *hp, int printheaders)
76 {
77         FILE *fbuf;
78         int lc, cc, escape, eofcount, fd, c, t;
79         char linebuf[LINESIZE], tempname[PATHSIZE], *cp, getsub;
80         sigset_t nset;
81         int longline, lastlong, rc;     /* So we don't make 2 or more lines
82                                            out of a long input line. */
83
84         collf = NULL;
85         /*
86          * Start catching signals from here, but we're still die on interrupts
87          * until we're in the main loop.
88          */
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)) {
101                 (void)rm(tempname);
102                 goto err;
103         }
104         (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
105
106         noreset++;
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);
112                 goto err;
113         }
114         (void)rm(tempname);
115
116         /*
117          * If we are going to prompt for a subject,
118          * refrain from printing a newline after
119          * the headers (since some people mind).
120          */
121         t = GTO|GSUBJECT|GCC|GNL;
122         getsub = 0;
123         if (hp->h_subject == NULL && value("interactive") != NULL &&
124             (value("ask") != NULL || value("asksub") != NULL))
125                 t &= ~GNL, getsub++;
126         if (printheaders) {
127                 puthead(hp, stdout, t);
128                 (void)fflush(stdout);
129         }
130         if ((cp = value("escape")) != NULL)
131                 escape = *cp;
132         else
133                 escape = ESCAPE;
134         eofcount = 0;
135         hadintr = 0;
136         lastlong = 0;
137         longline = 0;
138
139         if (!setjmp(colljmp)) {
140                 if (getsub)
141                         grabh(hp, GSUBJECT);
142         } else {
143                 /*
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.
147                  */
148 cont:
149                 if (hadintr) {
150                         (void)fflush(stdout);
151                         fprintf(stderr,
152                         "\n(Interrupt -- one more to kill letter)\n");
153                 } else {
154                         printf("(continue)\n");
155                         (void)fflush(stdout);
156                 }
157         }
158         for (;;) {
159                 colljmp_p = 1;
160                 c = readline(stdin, linebuf, LINESIZE);
161                 colljmp_p = 0;
162                 if (c < 0) {
163                         if (value("interactive") != NULL &&
164                             value("ignoreeof") != NULL && ++eofcount < 25) {
165                                 printf("Use \".\" to terminate letter\n");
166                                 continue;
167                         }
168                         break;
169                 }
170                 lastlong = longline;
171                 longline = c == LINESIZE - 1;
172                 eofcount = 0;
173                 hadintr = 0;
174                 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
175                     value("interactive") != NULL && !lastlong &&
176                     (value("dot") != NULL || value("ignoreeof") != NULL))
177                         break;
178                 if (linebuf[0] != escape || value("interactive") == NULL ||
179                     lastlong) {
180                         if (putline(collf, linebuf, !longline) < 0)
181                                 goto err;
182                         continue;
183                 }
184                 c = linebuf[1];
185                 switch (c) {
186                 default:
187                         /*
188                          * On double escape, just send the single one.
189                          * Otherwise, it's an error.
190                          */
191                         if (c == escape) {
192                                 if (putline(collf, &linebuf[1], !longline) < 0)
193                                         goto err;
194                                 else
195                                         break;
196                         }
197                         printf("Unknown tilde escape.\n");
198                         break;
199                 case 'C':
200                         /*
201                          * Dump core.
202                          */
203                         core();
204                         break;
205                 case '!':
206                         /*
207                          * Shell escape, send the balance of the
208                          * line to sh -c.
209                          */
210                         shell(&linebuf[2]);
211                         break;
212                 case ':':
213                 case '_':
214                         /*
215                          * Escape to command mode, but be nice!
216                          */
217                         execute(&linebuf[2], 1);
218                         goto cont;
219                 case '.':
220                         /*
221                          * Simulate end of file on input.
222                          */
223                         goto out;
224                 case 'q':
225                         /*
226                          * Force a quit of sending mail.
227                          * Act like an interrupt happened.
228                          */
229                         hadintr++;
230                         collint(SIGINT);
231                         exit(1);
232                 case 'x':
233                         /*
234                          * Exit, do not save in dead.letter.
235                          */
236                         goto err;
237                 case 'h':
238                         /*
239                          * Grab a bunch of headers.
240                          */
241                         grabh(hp, GTO|GSUBJECT|GCC|GBCC);
242                         goto cont;
243                 case 't':
244                         /*
245                          * Add to the To list.
246                          */
247                         hp->h_to = cat(hp->h_to, extract(&linebuf[2], GTO));
248                         break;
249                 case 's':
250                         /*
251                          * Set the Subject line.
252                          */
253                         cp = &linebuf[2];
254                         while (isspace((unsigned char)*cp))
255                                 cp++;
256                         hp->h_subject = savestr(cp);
257                         break;
258                 case 'R':
259                         /*
260                          * Set the Reply-To line.
261                          */
262                         cp = &linebuf[2];
263                         while (isspace((unsigned char)*cp))
264                                 cp++;
265                         hp->h_replyto = savestr(cp);
266                         break;
267                 case 'c':
268                         /*
269                          * Add to the CC list.
270                          */
271                         hp->h_cc = cat(hp->h_cc, extract(&linebuf[2], GCC));
272                         break;
273                 case 'b':
274                         /*
275                          * Add to the BCC list.
276                          */
277                         hp->h_bcc = cat(hp->h_bcc, extract(&linebuf[2], GBCC));
278                         break;
279                 case 'i':
280                 case 'A':
281                 case 'a':
282                         /*
283                          * Insert named variable in message.
284                          */
285                         switch(c) {
286                                 case 'i':
287                                         cp = &linebuf[2];
288                                         while(isspace((unsigned char)*cp))
289                                                 cp++;
290                                         break;
291                                 case 'a':
292                                         cp = "sign";
293                                         break;
294                                 case 'A':
295                                         cp = "Sign";
296                                         break;
297                                 default:
298                                         goto err;
299                         }
300
301                         if(*cp != '\0' && (cp = value(cp)) != NULL) {
302                                 printf("%s\n", cp);
303                                 if(putline(collf, cp, 1) < 0)
304                                         goto err;
305                         }
306
307                         break;
308                 case 'd':
309                         /*
310                          * Read in the dead letter file.
311                          */
312                         if (strlcpy(linebuf + 2, getdeadletter(),
313                                 sizeof(linebuf) - 2)
314                             >= sizeof(linebuf) - 2) {
315                                 printf("Line buffer overflow\n");
316                                 break;
317                         }
318                         /* FALLTHROUGH */
319                 case 'r':
320                 case '<':
321                         /*
322                          * Invoke a file:
323                          * Search for the file name,
324                          * then open it and copy the contents to collf.
325                          */
326                         cp = &linebuf[2];
327                         while (isspace((unsigned char)*cp))
328                                 cp++;
329                         if (*cp == '\0') {
330                                 printf("Interpolate what file?\n");
331                                 break;
332                         }
333                         cp = expand(cp);
334                         if (cp == NULL)
335                                 break;
336                         if (*cp == '!') {
337                                 /*
338                                  * Insert stdout of command.
339                                  */
340                                 char *sh;
341                                 int nullfd, tempfd, rc;
342                                 char tempname2[PATHSIZE];
343
344                                 if ((nullfd = open(_PATH_DEVNULL, O_RDONLY, 0))
345                                     == -1) {
346                                         warn(_PATH_DEVNULL);
347                                         break;
348                                 }
349
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);
355                                         break;
356                                 }
357                                 (void)unlink(tempname2);
358
359                                 if ((sh = value("SHELL")) == NULL)
360                                         sh = _PATH_CSHELL;
361
362                                 rc = run_command(sh, 0, nullfd, fileno(fbuf),
363                                     "-c", cp+1, NULL);
364
365                                 close(nullfd);
366
367                                 if (rc < 0) {
368                                         (void)Fclose(fbuf);
369                                         break;
370                                 }
371
372                                 if (fsize(fbuf) == 0) {
373                                         fprintf(stderr,
374                                             "No bytes from command \"%s\"\n",
375                                             cp+1);
376                                         (void)Fclose(fbuf);
377                                         break;
378                                 }
379
380                                 rewind(fbuf);
381                         } else if (isdir(cp)) {
382                                 printf("%s: Directory\n", cp);
383                                 break;
384                         } else if ((fbuf = Fopen(cp, "r")) == NULL) {
385                                 warn("%s", cp);
386                                 break;
387                         }
388                         printf("\"%s\" ", cp);
389                         (void)fflush(stdout);
390                         lc = 0;
391                         cc = 0;
392                         while ((rc = readline(fbuf, linebuf, LINESIZE)) >= 0) {
393                                 if (rc != LINESIZE - 1)
394                                         lc++;
395                                 if ((t = putline(collf, linebuf,
396                                          rc != LINESIZE - 1)) < 0) {
397                                         (void)Fclose(fbuf);
398                                         goto err;
399                                 }
400                                 cc += t;
401                         }
402                         (void)Fclose(fbuf);
403                         printf("%d/%d\n", lc, cc);
404                         break;
405                 case 'w':
406                         /*
407                          * Write the message on a file.
408                          */
409                         cp = &linebuf[2];
410                         while (*cp == ' ' || *cp == '\t')
411                                 cp++;
412                         if (*cp == '\0') {
413                                 fprintf(stderr, "Write what file!?\n");
414                                 break;
415                         }
416                         if ((cp = expand(cp)) == NULL)
417                                 break;
418                         rewind(collf);
419                         exwrite(cp, collf, 1);
420                         break;
421                 case 'm':
422                 case 'M':
423                 case 'f':
424                 case 'F':
425                         /*
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.
430                          */
431                         if (forward(linebuf + 2, collf, tempname, c) < 0)
432                                 goto err;
433                         goto cont;
434                 case '?':
435                         if ((fbuf = Fopen(_PATH_TILDE, "r")) == NULL) {
436                                 warn("%s", _PATH_TILDE);
437                                 break;
438                         }
439                         while ((t = getc(fbuf)) != EOF)
440                                 (void)putchar(t);
441                         (void)Fclose(fbuf);
442                         break;
443                 case 'p':
444                         /*
445                          * Print out the current state of the
446                          * message without altering anything.
447                          */
448                         rewind(collf);
449                         printf("-------\nMessage contains:\n");
450                         puthead(hp, stdout, GTO|GSUBJECT|GCC|GBCC|GNL);
451                         while ((t = getc(collf)) != EOF)
452                                 (void)putchar(t);
453                         goto cont;
454                 case '|':
455                         /*
456                          * Pipe message through command.
457                          * Collect output as new message.
458                          */
459                         rewind(collf);
460                         mespipe(collf, &linebuf[2]);
461                         goto cont;
462                 case 'v':
463                 case 'e':
464                         /*
465                          * Edit the current message.
466                          * 'e' means to use EDITOR
467                          * 'v' means to use VISUAL
468                          */
469                         rewind(collf);
470                         mesedit(collf, c);
471                         goto cont;
472                 }
473         }
474         goto out;
475 err:
476         if (collf != NULL) {
477                 (void)Fclose(collf);
478                 collf = NULL;
479         }
480 out:
481         if (collf != NULL)
482                 rewind(collf);
483         noreset--;
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);
491         return (collf);
492 }
493
494 /*
495  * Write a file, ex-like if f set.
496  */
497 int
498 exwrite(char name[], FILE *fp, int f)
499 {
500         FILE *of;
501         int c, lc;
502         long cc;
503         struct stat junk;
504
505         if (f) {
506                 printf("\"%s\" ", name);
507                 (void)fflush(stdout);
508         }
509         if (stat(name, &junk) >= 0 && S_ISREG(junk.st_mode)) {
510                 if (!f)
511                         fprintf(stderr, "%s: ", name);
512                 fprintf(stderr, "File exists\n");
513                 return (-1);
514         }
515         if ((of = Fopen(name, "w")) == NULL) {
516                 warn((char *)NULL);
517                 return (-1);
518         }
519         lc = 0;
520         cc = 0;
521         while ((c = getc(fp)) != EOF) {
522                 cc++;
523                 if (c == '\n')
524                         lc++;
525                 (void)putc(c, of);
526                 if (ferror(of)) {
527                         warnx("%s", name);
528                         (void)Fclose(of);
529                         return (-1);
530                 }
531         }
532         (void)Fclose(of);
533         printf("%d/%ld\n", lc, cc);
534         (void)fflush(stdout);
535         return (0);
536 }
537
538 /*
539  * Edit the message being collected on fp.
540  * On return, make the edit file the new temp file.
541  */
542 void
543 mesedit(FILE *fp, int c)
544 {
545         sig_t sigint = signal(SIGINT, SIG_IGN);
546         FILE *nf = run_editor(fp, (off_t)-1, c, 0);
547
548         if (nf != NULL) {
549                 (void)fseeko(nf, (off_t)0, SEEK_END);
550                 collf = nf;
551                 (void)Fclose(fp);
552         }
553         (void)signal(SIGINT, sigint);
554 }
555
556 /*
557  * Pipe the message through the command.
558  * Old message is on stdin of command;
559  * New message collected from stdout.
560  * Sh -c must return 0 to accept the new message.
561  */
562 void
563 mespipe(FILE *fp, char cmd[])
564 {
565         FILE *nf;
566         int fd;
567         sig_t sigint = signal(SIGINT, SIG_IGN);
568         char *sh, tempname[PATHSIZE];
569
570         (void)snprintf(tempname, sizeof(tempname),
571             "%s/mail.ReXXXXXXXXXX", tmpdir);
572         if ((fd = mkstemp(tempname)) == -1 ||
573             (nf = Fdopen(fd, "w+")) == NULL) {
574                 warn("%s", tempname);
575                 goto out;
576         }
577         (void)rm(tempname);
578         /*
579          * stdin = current message.
580          * stdout = new message.
581          */
582         if ((sh = value("SHELL")) == NULL)
583                 sh = _PATH_CSHELL;
584         if (run_command(sh,
585             0, fileno(fp), fileno(nf), "-c", cmd, NULL) < 0) {
586                 (void)Fclose(nf);
587                 goto out;
588         }
589         if (fsize(nf) == 0) {
590                 fprintf(stderr, "No bytes from \"%s\" !?\n", cmd);
591                 (void)Fclose(nf);
592                 goto out;
593         }
594         /*
595          * Take new files.
596          */
597         (void)fseeko(nf, (off_t)0, SEEK_END);
598         collf = nf;
599         (void)Fclose(fp);
600 out:
601         (void)signal(SIGINT, sigint);
602 }
603
604 /*
605  * Interpolate the named messages into the current
606  * message, preceding each line with a tab.
607  * Return a count of the number of characters now in
608  * the message, or -1 if an error is encountered writing
609  * the message temporary.  The flag argument is 'm' if we
610  * should shift over and 'f' if not.
611  */
612 int
613 forward(char ms[], FILE *fp, char *fn, int f)
614 {
615         int *msgvec;
616         struct ignoretab *ig;
617         char *tabst;
618
619         msgvec = (int *)salloc((msgCount+1) * sizeof(*msgvec));
620         if (msgvec == NULL)
621                 return (0);
622         if (getmsglist(ms, msgvec, 0) < 0)
623                 return (0);
624         if (*msgvec == 0) {
625                 *msgvec = first(0, MMNORM);
626                 if (*msgvec == 0) {
627                         printf("No appropriate messages\n");
628                         return (0);
629                 }
630                 msgvec[1] = 0;
631         }
632         if (f == 'f' || f == 'F')
633                 tabst = NULL;
634         else if ((tabst = value("indentprefix")) == NULL)
635                 tabst = "\t";
636         ig = isupper((unsigned char)f) ? NULL : ignore;
637         printf("Interpolating:");
638         for (; *msgvec != 0; msgvec++) {
639                 struct message *mp = message + *msgvec - 1;
640
641                 touch(mp);
642                 printf(" %d", *msgvec);
643                 if (sendmessage(mp, fp, ig, tabst) < 0) {
644                         warnx("%s", fn);
645                         return (-1);
646                 }
647         }
648         printf("\n");
649         return (0);
650 }
651
652 /*
653  * Print (continue) when continued after ^Z.
654  */
655 /*ARGSUSED*/
656 void
657 collstop(int s)
658 {
659         sig_t old_action = signal(s, SIG_DFL);
660         sigset_t nset;
661
662         (void)sigemptyset(&nset);
663         (void)sigaddset(&nset, s);
664         (void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
665         (void)kill(0, s);
666         (void)sigprocmask(SIG_BLOCK, &nset, NULL);
667         (void)signal(s, old_action);
668         if (colljmp_p) {
669                 colljmp_p = 0;
670                 hadintr = 0;
671                 longjmp(colljmp, 1);
672         }
673 }
674
675 /*
676  * On interrupt, come here to save the partial message in ~/dead.letter.
677  * Then jump out of the collection loop.
678  */
679 /*ARGSUSED*/
680 void
681 collint(int s __unused)
682 {
683         /*
684          * the control flow is subtle, because we can be called from ~q.
685          */
686         if (!hadintr) {
687                 if (value("ignore") != NULL) {
688                         printf("@");
689                         (void)fflush(stdout);
690                         clearerr(stdin);
691                         return;
692                 }
693                 hadintr = 1;
694                 longjmp(colljmp, 1);
695         }
696         rewind(collf);
697         if (value("nosave") == NULL)
698                 savedeadletter(collf);
699         longjmp(collabort, 1);
700 }
701
702 /*ARGSUSED*/
703 void
704 collhup(int s __unused)
705 {
706         rewind(collf);
707         savedeadletter(collf);
708         /*
709          * Let's pretend nobody else wants to clean up,
710          * a true statement at this time.
711          */
712         exit(1);
713 }
714
715 void
716 savedeadletter(FILE *fp)
717 {
718         FILE *dbuf;
719         int c;
720         char *cp;
721
722         if (fsize(fp) == 0)
723                 return;
724         cp = getdeadletter();
725         c = umask(077);
726         dbuf = Fopen(cp, "a");
727         (void)umask(c);
728         if (dbuf == NULL)
729                 return;
730         while ((c = getc(fp)) != EOF)
731                 (void)putc(c, dbuf);
732         (void)Fclose(dbuf);
733         rewind(fp);
734 }