makeconfig: small tweaks
[s-mailx.git] / collect.c
blob8914f3ba546bd47da7f20817fc96fd56873bb8c4
1 /*
2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
41 * Mail -- a mail program
43 * Collect input from standard input, handling
44 * ~ escapes.
47 #include "rcv.h"
48 #include "extern.h"
49 #include <unistd.h>
50 #include <sys/stat.h>
52 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
53 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
54 * TODO may leave child running if fdopen() fails! */
57 * Read a message from standard output and return a read file to it
58 * or NULL on error.
62 * The following hokiness with global variables is so that on
63 * receipt of an interrupt signal, the partial message can be salted
64 * away on dead.letter.
67 static sighandler_type saveint; /* Previous SIGINT value */
68 static sighandler_type savehup; /* Previous SIGHUP value */
69 static sighandler_type savetstp; /* Previous SIGTSTP value */
70 static sighandler_type savettou; /* Previous SIGTTOU value */
71 static sighandler_type savettin; /* Previous SIGTTIN value */
72 static FILE *collf; /* File for saving away */
73 static int hadintr; /* Have seen one SIGINT so far */
75 static sigjmp_buf colljmp; /* To get back to work */
76 static int colljmp_p; /* whether to long jump */
77 static sigjmp_buf collabort; /* To end collection with error */
79 static sigjmp_buf pipejmp; /* On broken pipe */
81 static void onpipe(int signo);
82 static void insertcommand(FILE *fp, char *cmd);
83 static void print_collf(FILE *collf, struct header *hp);
84 static int include_file(FILE *fbuf, char *name, int *linecount,
85 int *charcount, int echo);
86 static struct attachment *read_attachment_data(struct attachment *ap,
87 unsigned number);
88 static struct attachment *append_attachments(struct attachment *attach,
89 char *names);
90 static int exwrite(char *name, FILE *fp, int f);
91 static enum okay makeheader(FILE *fp, struct header *hp);
92 static void mesedit(int c, struct header *hp);
93 static void mespipe(char *cmd);
94 static int forward(char *ms, FILE *fp, int f);
95 static void collstop(int s);
96 static void collint(int s);
97 static void collhup(int s);
98 static int putesc(const char *s, FILE *stream);
100 /*ARGSUSED*/
101 static void
102 onpipe(int signo)
104 (void)signo;
105 siglongjmp(pipejmp, 1);
109 * Execute cmd and insert its standard output into fp.
111 static void
112 insertcommand(FILE *fp, char *cmd)
114 FILE *obuf = NULL;
115 char *volatile cp;
116 int c;
118 cp = value("SHELL");
119 if (sigsetjmp(pipejmp, 1))
120 goto endpipe;
121 if (cp == NULL)
122 cp = SHELL;
123 if ((obuf = Popen(cmd, "r", cp, 0)) == NULL) {
124 perror(cmd);
125 return;
127 safe_signal(SIGPIPE, onpipe);
128 while ((c = getc(obuf)) != EOF)
129 putc(c, fp);
130 endpipe:
131 safe_signal(SIGPIPE, SIG_IGN);
132 Pclose(obuf);
133 safe_signal(SIGPIPE, dflpipe);
137 * ~p command.
139 static void
140 print_collf(FILE *collf, struct header *hp)
142 char *lbuf = NULL;
143 FILE *volatile obuf = stdout;
144 struct attachment *ap;
145 char *cp;
146 enum gfield gf;
147 size_t linecnt, maxlines, linesize = 0, linelen, count, count2;
149 (void)&obuf;
150 (void)&cp;
151 fflush(collf);
152 rewind(collf);
153 count = count2 = fsize(collf);
154 if (is_a_tty[0] && is_a_tty[1] && (cp = value("crt")) != NULL) {
155 for (linecnt = 0;
156 fgetline(&lbuf, &linesize, &count2, NULL, collf, 0);
157 linecnt++);
158 rewind(collf);
159 maxlines = (*cp == '\0' ? screensize() : atoi(cp));
160 maxlines -= 4;
161 if (hp->h_to)
162 maxlines--;
163 if (hp->h_subject)
164 maxlines--;
165 if (hp->h_cc)
166 maxlines--;
167 if (hp->h_bcc)
168 maxlines--;
169 if (hp->h_attach)
170 maxlines--;
171 maxlines -= myaddrs(hp) != NULL || hp->h_from != NULL;
172 maxlines -= value("ORGANIZATION") != NULL ||
173 hp->h_organization != NULL;
174 maxlines -= value("replyto") != NULL || hp->h_replyto != NULL;
175 maxlines -= value("sender") != NULL || hp->h_sender != NULL;
176 if ((long)maxlines < 0 || linecnt > maxlines) {
177 cp = get_pager();
178 if (sigsetjmp(pipejmp, 1))
179 goto endpipe;
180 obuf = Popen(cp, "w", NULL, 1);
181 if (obuf == NULL) {
182 perror(cp);
183 obuf = stdout;
184 } else
185 safe_signal(SIGPIPE, onpipe);
188 fprintf(obuf, catgets(catd, CATSET, 62,
189 "-------\nMessage contains:\n"));
190 gf = GIDENT|GTO|GSUBJECT|GCC|GBCC|GNL|GFILES;
191 if (value("fullnames"))
192 gf |= GCOMMA;
193 puthead(hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
194 while (fgetline(&lbuf, &linesize, &count, &linelen, collf, 1))
195 prout(lbuf, linelen, obuf);
196 if (hp->h_attach != NULL) {
197 fputs(catgets(catd, CATSET, 63, "Attachments:"), obuf);
198 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
199 if (ap->a_msgno)
200 fprintf(obuf, " message %u", ap->a_msgno);
201 else
202 fprintf(obuf, " %s", ap->a_name);
203 if (ap->a_flink)
204 putc(',', obuf);
206 putc('\n', obuf);
208 endpipe:
209 if (obuf != stdout) {
210 safe_signal(SIGPIPE, SIG_IGN);
211 Pclose(obuf);
212 safe_signal(SIGPIPE, dflpipe);
214 if (lbuf)
215 free(lbuf);
218 static int
219 include_file(FILE *fbuf, char *name, int *linecount, int *charcount, int echo)
221 char *interactive, *linebuf = NULL;
222 size_t linesize = 0, linelen, count;
224 if (fbuf == NULL)
225 fbuf = Fopen(name, "r");
226 if (fbuf == NULL) {
227 perror(name);
228 return (-1);
230 interactive = value("interactive");
231 *linecount = 0;
232 *charcount = 0;
233 fflush(fbuf);
234 rewind(fbuf);
235 count = fsize(fbuf);
236 while (fgetline(&linebuf, &linesize, &count, &linelen, fbuf, 0)
237 != NULL) {
238 if (fwrite(linebuf, sizeof *linebuf, linelen, collf)
239 != linelen) {
240 Fclose(fbuf);
241 return (-1);
243 if (interactive != NULL && echo)
244 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
245 (*linecount)++;
246 (*charcount) += linelen;
248 if (linebuf)
249 free(linebuf);
250 Fclose(fbuf);
251 if (fflush(collf))
252 return (-1);
253 return (0);
257 * Ask the user to edit file names and other data for the given
258 * attachment. NULL is returned if no file name is given.
260 static struct attachment *
261 read_attachment_data(struct attachment *ap, unsigned number)
263 char prefix[80], *cp;
265 if (ap == NULL)
266 ap = csalloc(1, sizeof *ap);
267 if (ap->a_msgno) {
268 printf("#%u\tmessage %u\n", number, ap->a_msgno);
269 return ap;
271 snprintf(prefix, sizeof prefix, catgets(catd, CATSET, 50,
272 "#%u\tfilename: "), number);
273 for (;;) {
274 char *exf;
275 if ((ap->a_name = readtty(prefix, ap->a_name)) == NULL)
276 break;
277 if ((exf = expand(ap->a_name)) != NULL)
278 ap->a_name = exf;
279 if (access(ap->a_name, R_OK) == 0)
280 break;
281 perror(ap->a_name);
283 if ((ap->a_name && (value("attachment-ask-charset"))) ||
284 ((cp = value("sendcharsets")) != NULL &&
285 strchr(cp, ',') != NULL)) {
286 snprintf(prefix, sizeof prefix, "#%u\tcharset: ", number);
287 ap->a_charset = readtty(prefix, ap->a_charset);
290 * The "attachment-ask-content-*" variables are left undocumented
291 * since they are for RFC connoisseurs only.
293 if (ap->a_name && value("attachment-ask-content-type")) {
294 if (ap->a_content_type == NULL)
295 ap->a_content_type = mime_filecontent(ap->a_name);
296 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
297 ap->a_content_type = readtty(prefix, ap->a_content_type);
299 if (ap->a_name && value("attachment-ask-content-disposition")) {
300 snprintf(prefix, sizeof prefix,
301 "#%u\tContent-Disposition: ", number);
302 ap->a_content_disposition = readtty(prefix,
303 ap->a_content_disposition);
305 if (ap->a_name && value("attachment-ask-content-id")) {
306 snprintf(prefix, sizeof prefix, "#%u\tContent-ID: ", number);
307 ap->a_content_id = readtty(prefix, ap->a_content_id);
309 if (ap->a_name && value("attachment-ask-content-description")) {
310 snprintf(prefix, sizeof prefix,
311 "#%u\tContent-Description: ", number);
312 ap->a_content_description = readtty(prefix,
313 ap->a_content_description);
315 return ap->a_name ? ap : NULL;
319 * Interactively edit the attachment list.
321 struct attachment *
322 edit_attachments(struct attachment *attach)
324 struct attachment *ap, *nap;
325 unsigned attno = 1;
327 for (ap = attach; ap; ap = ap->a_flink) {
328 if ((nap = read_attachment_data(ap, attno)) == NULL) {
329 if (ap->a_blink)
330 ap->a_blink->a_flink = ap->a_flink;
331 else
332 attach = ap->a_flink;
333 if (ap->a_flink)
334 ap->a_flink->a_blink = ap->a_blink;
335 else
336 return attach;
337 } else
338 attno++;
340 while ((nap = read_attachment_data(NULL, attno)) != NULL) {
341 for (ap = attach; ap && ap->a_flink; ap = ap->a_flink);
342 if (ap)
343 ap->a_flink = nap;
344 nap->a_blink = ap;
345 nap->a_flink = NULL;
346 if (attach == NULL)
347 attach = nap;
348 attno++;
350 return attach;
354 * Put the given file to the end of the attachment list.
356 struct attachment *
357 add_attachment(struct attachment *attach, char *file, int expand_file)
359 struct attachment *ap, *nap;
361 if (expand_file) {
362 file = expand(file);
363 if (file == NULL)
364 return NULL;
365 } else
366 file = savestr(file);
367 if (access(file, R_OK) != 0)
368 return NULL;
369 /*LINTED*/
370 nap = csalloc(1, sizeof *nap);
371 nap->a_name = file;
372 if (attach != NULL) {
373 for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink);
374 ap->a_flink = nap;
375 nap->a_blink = ap;
376 } else {
377 nap->a_blink = NULL;
378 attach = nap;
380 return attach;
384 * Append the whitespace-separated file names to the end of
385 * the attachment list.
387 static struct attachment *
388 append_attachments(struct attachment *attach, char *names)
390 char *cp;
391 int c;
392 struct attachment *ap;
394 cp = names;
395 while (*cp != '\0' && blankchar(*cp & 0377))
396 cp++;
397 while (*cp != '\0') {
398 names = cp;
399 while (*cp != '\0' && !blankchar(*cp & 0377))
400 cp++;
401 c = *cp;
402 *cp++ = '\0';
403 if (*names != '\0') {
404 if ((ap = add_attachment(attach, names, 1)) == NULL)
405 perror(names);
406 else
407 attach = ap;
409 if (c == '\0')
410 break;
411 while (*cp != '\0' && blankchar(*cp & 0377))
412 cp++;
414 return attach;
417 FILE *
418 collect(struct header *hp, int printheaders, struct message *mp,
419 char *quotefile, int doprefix, int volatile tflag)
421 enum {
422 val_INTERACT = 1
425 FILE *fbuf;
426 struct ignoretab *quoteig;
427 int lc, cc, eofcount, val, c, t;
428 int volatile escape, getfields;
429 char *linebuf = NULL, *cp, *quote = NULL, *tempMail = NULL;
430 size_t linesize;
431 long count;
432 enum sendaction action;
433 sigset_t oset, nset;
434 sighandler_type savedtop;
436 val = 0;
437 if (value("interactive") != NULL)
438 val |= val_INTERACT;
440 collf = NULL;
442 * Start catching signals from here, but we're still die on interrupts
443 * until we're in the main loop.
445 sigemptyset(&nset);
446 sigaddset(&nset, SIGINT);
447 sigaddset(&nset, SIGHUP);
448 sigprocmask(SIG_BLOCK, &nset, &oset);
449 handlerpush(collint);
450 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
451 safe_signal(SIGINT, collint);
452 if ((savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
453 safe_signal(SIGHUP, collhup);
454 savetstp = safe_signal(SIGTSTP, collstop);
455 savettou = safe_signal(SIGTTOU, collstop);
456 savettin = safe_signal(SIGTTIN, collstop);
457 if (sigsetjmp(collabort, 1))
458 goto jerr;
459 if (sigsetjmp(colljmp, 1))
460 goto jerr;
461 sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
463 noreset++;
464 if ((collf = Ftemp(&tempMail, "Rs", "w+", 0600, 1)) == NULL) {
465 perror(tr(51, "temporary mail file"));
466 goto jerr;
468 unlink(tempMail);
469 Ftfree(&tempMail);
471 if ((cp = value("NAIL_HEAD")) != NULL && putesc(cp, collf) < 0)
472 goto jerr;
475 * If we are going to prompt for a subject,
476 * refrain from printing a newline after
477 * the headers (since some people mind).
479 getfields = 0;
480 if (! tflag) {
481 t = GTO|GSUBJECT|GCC|GNL;
482 if (value("fullnames"))
483 t |= GCOMMA;
484 if (hp->h_subject == NULL && (val & val_INTERACT) &&
485 (value("ask") != NULL || value("asksub") != NULL))
486 t &= ~GNL, getfields |= GSUBJECT;
487 if (hp->h_to == NULL && (val & val_INTERACT))
488 t &= ~GNL, getfields |= GTO;
489 if (value("bsdcompat") == NULL && value("askatend") == NULL &&
490 (val & val_INTERACT)) {
491 if (hp->h_bcc == NULL && value("askbcc"))
492 t &= ~GNL, getfields |= GBCC;
493 if (hp->h_cc == NULL && value("askcc"))
494 t &= ~GNL, getfields |= GCC;
496 if (printheaders) {
497 (void)puthead(hp, stdout, t, SEND_TODISP, CONV_NONE,
498 NULL, NULL);
499 (void)fflush(stdout);
504 * Quote an original message
506 if (mp != NULL && (doprefix || (quote = value("quote")) != NULL)) {
507 quoteig = allignore;
508 action = SEND_QUOTE;
509 if (doprefix) {
510 quoteig = fwdignore;
511 if ((cp = value("fwdheading")) == NULL)
512 cp = "-------- Original Message --------";
513 if (*cp && fprintf(collf, "%s\n", cp) < 0)
514 goto jerr;
515 } else if (strcmp(quote, "noheading") == 0) {
516 /*EMPTY*/;
517 } else if (strcmp(quote, "headers") == 0) {
518 quoteig = ignore;
519 } else if (strcmp(quote, "allheaders") == 0) {
520 quoteig = NULL;
521 action = SEND_QUOTE_ALL;
522 } else {
523 cp = hfield1("from", mp);
524 if (cp != NULL && (count = (long)strlen(cp)) > 0) {
525 if (mime_write(cp, count,
526 collf, CONV_FROMHDR, TD_NONE,
527 NULL, (size_t) 0,
528 NULL, NULL) == 0)
529 goto jerr;
530 if (fprintf(collf, tr(52, " wrote:\n\n")) < 0)
531 goto jerr;
534 if (fflush(collf))
535 goto jerr;
536 cp = value("indentprefix");
537 if (cp != NULL && *cp == '\0')
538 cp = "\t";
539 if (send(mp, collf, quoteig, (doprefix ? NULL : cp), action,
540 NULL) < 0)
541 goto jerr;
544 /* Print what we have sofar also on the terminal */
545 (void)rewind(collf);
546 while ((c = getc(collf)) != EOF)
547 (void)putc(c, stdout);
548 if (fseek(collf, 0, SEEK_END))
549 goto jerr;
551 escape = ((cp = value("escape")) != NULL) ? *cp : ESCAPE;
552 eofcount = 0;
553 hadintr = 0;
555 if (! sigsetjmp(colljmp, 1)) {
556 if (getfields)
557 grabh(hp, getfields, 1);
558 if (quotefile != NULL) {
559 if (include_file(NULL, quotefile, &lc, &cc, 1) != 0)
560 goto jerr;
562 } else {
564 * Come here for printing the after-signal message.
565 * Duplicate messages won't be printed because
566 * the write is aborted if we get a SIGTTOU.
568 jcont:
569 if (hadintr) {
570 (void)fprintf(stderr, tr(53,
571 "\n(Interrupt -- one more to kill letter)\n"));
572 } else {
573 printf(tr(54, "(continue)\n"));
574 (void)fflush(stdout);
579 * No tilde escapes, interrupts not expected. Simply copy STDIN
581 if ((val & val_INTERACT) == 0 && tildeflag <= 0 && ! tflag) {
582 linebuf = srealloc(linebuf, linesize = LINESIZE);
583 while ((count = fread(linebuf, sizeof *linebuf,
584 linesize, stdin)) > 0) {
585 if ((size_t)count != fwrite(linebuf, sizeof *linebuf,
586 count, collf))
587 goto jerr;
589 if (fflush(collf))
590 goto jerr;
591 goto jout;
595 * The interactive collect loop
597 for (;;) {
598 colljmp_p = 1;
599 count = readline(stdin, &linebuf, &linesize);
600 colljmp_p = 0;
601 if (count < 0) {
602 if ((val & val_INTERACT) &&
603 value("ignoreeof") != NULL && ++eofcount < 25) {
604 printf(tr(55,
605 "Use \".\" to terminate letter\n"));
606 continue;
608 break;
610 if (tflag && count == 0) {
611 rewind(collf);
612 if (makeheader(collf, hp) != OKAY)
613 goto jerr;
614 rewind(collf);
615 tflag = 0;
616 continue;
618 eofcount = 0;
619 hadintr = 0;
620 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
621 (val & val_INTERACT) &&
622 (value("dot") != NULL ||
623 value("ignoreeof") != NULL))
624 break;
625 if (linebuf[0] != escape ||
626 ((val & val_INTERACT) == 0 && tildeflag == 0) ||
627 tildeflag < 0) {
628 if (putline(collf, linebuf, count) < 0)
629 goto jerr;
630 continue;
633 c = linebuf[1];
634 switch (c) {
635 default:
637 * On double escape, just send the single one.
638 * Otherwise, it's an error.
640 if (c == escape) {
641 if (putline(collf, &linebuf[1], count - 1) < 0)
642 goto jerr;
643 else
644 break;
646 printf(tr(56, "Unknown tilde escape.\n"));
647 break;
648 #ifdef HAVE_ASSERTS
649 case 'C':
650 /* Dump core */
651 core(NULL);
652 break;
653 #endif
654 case '!':
655 /* Shell escape, send the balance of line to sh -c */
656 shell(&linebuf[2]);
657 break;
658 case ':':
659 case '_':
660 /* Escape to command mode, but be nice! */
661 inhook = 0;
662 execute(&linebuf[2], 1, count - 2);
663 goto jcont;
664 case '.':
665 /* Simulate end of file on input */
666 goto jout;
667 case 'x':
668 /* Same as 'q', but no dead.letter saving */
669 hadintr++;
670 collint(0);
671 exit(1);
672 /*NOTREACHED*/
673 case 'q':
675 * Force a quit of sending mail.
676 * Act like an interrupt happened.
678 hadintr++;
679 collint(SIGINT);
680 exit(1);
681 /*NOTREACHED*/
682 case 'h':
683 /* Grab a bunch of headers */
685 grabh(hp, GTO|GSUBJECT|GCC|GBCC,
686 value("bsdcompat") != NULL &&
687 value("bsdorder") != NULL);
688 while (hp->h_to == NULL);
689 goto jcont;
690 case 'H':
691 /* Grab extra headers */
693 grabh(hp, GEXTRA, 0);
694 while (check_from_and_sender(hp->h_from, hp->h_sender));
695 goto jcont;
696 case 't':
697 /* Add to the To list */
698 while ((hp->h_to = cat(hp->h_to, checkaddrs(
699 lextract(&linebuf[2], GTO|GFULL))))
700 == NULL)
702 break;
703 case 's':
704 /* Set the Subject list */
705 cp = &linebuf[2];
706 while (whitechar(*cp))
707 ++cp;
708 hp->h_subject = savestr(cp);
709 break;
710 case '@':
711 /* Edit the attachment list */
712 if (linebuf[2] != '\0')
713 hp->h_attach = append_attachments(hp->h_attach,
714 &linebuf[2]);
715 else
716 hp->h_attach = edit_attachments(hp->h_attach);
717 break;
718 case 'c':
719 /* Add to the CC list */
720 hp->h_cc = cat(hp->h_cc, checkaddrs(
721 lextract(&linebuf[2], GCC|GFULL)));
722 break;
723 case 'b':
724 /* Add stuff to blind carbon copies list */
725 hp->h_bcc = cat(hp->h_bcc, checkaddrs(
726 lextract(&linebuf[2], GBCC|GFULL)));
727 break;
728 case 'd':
729 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
730 linebuf[linesize-1]='\0';
731 /*FALLTHRU*/
732 case 'r':
733 case '<':
735 * Invoke a file:
736 * Search for the file name,
737 * then open it and copy the contents to collf.
739 cp = &linebuf[2];
740 while (whitechar(*cp))
741 cp++;
742 if (*cp == '\0') {
743 printf(tr(57, "Interpolate what file?\n"));
744 break;
746 if (*cp == '!') {
747 insertcommand(collf, cp + 1);
748 break;
750 cp = expand(cp);
751 if (cp == NULL)
752 break;
753 if (is_dir(cp)) {
754 printf(tr(58, "%s: Directory\n"), cp);
755 break;
757 if ((fbuf = Fopen(cp, "r")) == NULL) {
758 perror(cp);
759 break;
761 printf(tr(59, "\"%s\" "), cp);
762 fflush(stdout);
763 if (include_file(fbuf, cp, &lc, &cc, 0) != 0)
764 goto jerr;
765 printf(tr(60, "%d/%d\n"), lc, cc);
766 break;
767 case 'i':
768 /* Insert an environment variable into the file */
769 cp = &linebuf[2];
770 while (whitechar(*cp))
771 ++cp;
772 if ((cp = value(cp)) == NULL || *cp == '\0')
773 break;
774 if (putesc(cp, collf) < 0)
775 goto jerr;
776 if ((val & val_INTERACT) && putesc(cp, stdout) < 0)
777 goto jerr;
778 break;
779 case 'a':
780 case 'A':
781 /* Insert the contents of a signature variable */
782 if ((cp = value(c == 'a' ? "sign" : "Sign")) != NULL &&
783 *cp != '\0') {
784 if (putesc(cp, collf) < 0)
785 goto jerr;
786 if ((val & val_INTERACT) &&
787 putesc(cp, stdout) < 0)
788 goto jerr;
790 break;
791 case 'w':
792 /* Write the message on a file */
793 cp = &linebuf[2];
794 while (blankchar(*cp))
795 ++cp;
796 if (*cp == '\0' || (cp = expand(cp)) == NULL) {
797 fprintf(stderr, tr(61, "Write what file!?\n"));
798 break;
800 rewind(collf);
801 if (exwrite(cp, collf, 1) < 0)
802 goto jerr;
803 break;
804 case 'm':
805 case 'M':
806 case 'f':
807 case 'F':
809 * Interpolate the named messages, if we
810 * are in receiving mail mode. Does the
811 * standard list processing garbage.
812 * If ~f is given, we don't shift over.
814 if (forward(linebuf + 2, collf, c) < 0)
815 goto jerr;
816 goto jcont;
817 case 'p':
819 * Print out the current state of the
820 * message without altering anything.
822 print_collf(collf, hp);
823 goto jcont;
824 case '|':
826 * Pipe message through command.
827 * Collect output as new message.
829 rewind(collf);
830 mespipe(&linebuf[2]);
831 goto jcont;
832 case 'v':
833 case 'e':
835 * Edit the current message.
836 * 'e' means to use EDITOR
837 * 'v' means to use VISUAL
839 rewind(collf);
840 mesedit(c, value("editheaders") ? hp : NULL);
841 goto jcont;
842 case '?':
844 * Last the lengthy help string.
845 * (Very ugly, but take care for compiler supported
846 * string lengths :()
848 (void)puts(
849 "-------------------- ~ ESCAPES ----------------------------\n"
850 "~~ Quote a single tilde\n"
851 "~@ [file ...] Edit attachment list\n"
852 "~b users Add users to \"blind\" cc list\n"
853 "~c users Add users to cc list\n"
854 "~d Read in dead.letter\n"
855 "~e Edit the message buffer\n"
856 "~f messages Read in messages without indenting lines\n"
857 "~F messages Same as ~f, but keep all header lines\n"
858 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
859 (void)puts(
860 "~r file Read a file into the message buffer\n"
861 "~p Print the message buffer\n"
862 "~q Abort message composition and save text to dead.letter\n"
863 "~m messages Read in messages with each line indented\n"
864 "~M messages Same as ~m, but keep all header lines\n"
865 "~s subject Set subject\n"
866 "~t users Add users to to list\n"
867 "~v Invoke display editor on message\n"
868 "~w file Write message onto file\n"
869 "~x Abort message composition and discard text written so far\n");
870 (void)puts(
871 "~!command Invoke the shell\n"
872 "~:command Execute a regular command\n"
873 "-----------------------------------------------------------\n");
874 break;
878 jout:
879 if (collf != NULL) {
880 if ((cp = value("NAIL_TAIL")) != NULL) {
881 if (putesc(cp, collf) < 0)
882 goto jerr;
883 if ((val & val_INTERACT) && putesc(cp, stdout) < 0)
884 goto jerr;
886 rewind(collf);
888 handlerpop();
889 noreset--;
890 sigemptyset(&nset);
891 sigaddset(&nset, SIGINT);
892 sigaddset(&nset, SIGHUP);
893 sigprocmask(SIG_BLOCK, &nset, (sigset_t*)NULL);
894 safe_signal(SIGINT, saveint);
895 safe_signal(SIGHUP, savehup);
896 safe_signal(SIGTSTP, savetstp);
897 safe_signal(SIGTTOU, savettou);
898 safe_signal(SIGTTIN, savettin);
899 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
900 return (collf);
901 jerr:
902 if (tempMail != NULL) {
903 rm(tempMail);
904 Ftfree(&tempMail);
906 if (collf != NULL) {
907 Fclose(collf);
908 collf = NULL;
910 goto jout;
914 * Write a file, ex-like if f set.
916 static int
917 exwrite(char *name, FILE *fp, int f)
919 FILE *of;
920 int c;
921 long cc;
922 int lc;
924 if (f) {
925 printf("\"%s\" ", name);
926 fflush(stdout);
928 if ((of = Fopen(name, "a")) == NULL) {
929 perror(NULL);
930 return(-1);
932 lc = 0;
933 cc = 0;
934 while ((c = getc(fp)) != EOF) {
935 cc++;
936 if (c == '\n')
937 lc++;
938 putc(c, of);
939 if (ferror(of)) {
940 perror(name);
941 Fclose(of);
942 return(-1);
945 Fclose(of);
946 printf(catgets(catd, CATSET, 65, "%d/%ld\n"), lc, cc);
947 fflush(stdout);
948 return(0);
951 static enum okay
952 makeheader(FILE *fp, struct header *hp)
954 char *tempEdit;
955 FILE *nf;
956 int c;
958 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
959 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
960 Fclose(nf);
961 unlink(tempEdit);
962 Ftfree(&tempEdit);
963 return STOP;
965 unlink(tempEdit);
966 Ftfree(&tempEdit);
967 extract_header(fp, hp);
968 while ((c = getc(fp)) != EOF)
969 putc(c, nf);
970 if (fp != collf)
971 Fclose(collf);
972 Fclose(fp);
973 collf = nf;
974 if (check_from_and_sender(hp->h_from, hp->h_sender))
975 return STOP;
976 return OKAY;
980 * Edit the message being collected on fp.
981 * On return, make the edit file the new temp file.
983 static void
984 mesedit(int c, struct header *hp)
986 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
987 FILE *nf = run_editor(collf, (off_t)-1, c, 0, hp, NULL, SEND_MBOX,
988 sigint);
990 if (nf != NULL) {
991 if (hp) {
992 rewind(nf);
993 makeheader(nf, hp);
994 } else {
995 fseek(nf, 0L, SEEK_END);
996 Fclose(collf);
997 collf = nf;
1000 safe_signal(SIGINT, sigint);
1004 * Pipe the message through the command.
1005 * Old message is on stdin of command;
1006 * New message collected from stdout.
1007 * Sh -c must return 0 to accept the new message.
1009 static void
1010 mespipe(char *cmd)
1012 FILE *nf;
1013 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
1014 char *tempEdit;
1015 char *shell;
1017 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
1018 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
1019 goto out;
1021 fflush(collf);
1022 unlink(tempEdit);
1023 Ftfree(&tempEdit);
1025 * stdin = current message.
1026 * stdout = new message.
1028 if ((shell = value("SHELL")) == NULL)
1029 shell = SHELL;
1030 if (run_command(shell,
1031 0, fileno(collf), fileno(nf), "-c", cmd, NULL) < 0) {
1032 Fclose(nf);
1033 goto out;
1035 if (fsize(nf) == 0) {
1036 fprintf(stderr, catgets(catd, CATSET, 67,
1037 "No bytes from \"%s\" !?\n"), cmd);
1038 Fclose(nf);
1039 goto out;
1042 * Take new files.
1044 fseek(nf, 0L, SEEK_END);
1045 Fclose(collf);
1046 collf = nf;
1047 out:
1048 safe_signal(SIGINT, sigint);
1052 * Interpolate the named messages into the current
1053 * message, preceding each line with a tab.
1054 * Return a count of the number of characters now in
1055 * the message, or -1 if an error is encountered writing
1056 * the message temporary. The flag argument is 'm' if we
1057 * should shift over and 'f' if not.
1059 static int
1060 forward(char *ms, FILE *fp, int f)
1062 int *msgvec;
1063 struct ignoretab *ig;
1064 char *tabst;
1065 enum sendaction action;
1067 /*LINTED*/
1068 msgvec = (int *)salloc((msgCount+1) * sizeof *msgvec);
1069 if (msgvec == NULL)
1070 return(0);
1071 if (getmsglist(ms, msgvec, 0) < 0)
1072 return(0);
1073 if (*msgvec == 0) {
1074 *msgvec = first(0, MMNORM);
1075 if (*msgvec == 0) {
1076 printf(catgets(catd, CATSET, 68,
1077 "No appropriate messages\n"));
1078 return(0);
1080 msgvec[1] = 0;
1082 if (f == 'f' || f == 'F')
1083 tabst = NULL;
1084 else if ((tabst = value("indentprefix")) == NULL)
1085 tabst = "\t";
1086 ig = upperchar(f) ? (struct ignoretab *)NULL : ignore;
1087 action = upperchar(f) ? SEND_QUOTE_ALL : SEND_QUOTE;
1088 printf(catgets(catd, CATSET, 69, "Interpolating:"));
1089 for (; *msgvec != 0; msgvec++) {
1090 struct message *mp = message + *msgvec - 1;
1092 touch(mp);
1093 printf(" %d", *msgvec);
1094 if (send(mp, fp, ig, tabst, action, NULL) < 0) {
1095 perror(catgets(catd, CATSET, 70,
1096 "temporary mail file"));
1097 return(-1);
1100 printf("\n");
1101 return(0);
1105 * Print (continue) when continued after ^Z.
1107 /*ARGSUSED*/
1108 static void
1109 collstop(int s)
1111 sighandler_type old_action = safe_signal(s, SIG_DFL);
1112 sigset_t nset;
1114 sigemptyset(&nset);
1115 sigaddset(&nset, s);
1116 sigprocmask(SIG_UNBLOCK, &nset, (sigset_t *)NULL);
1117 kill(0, s);
1118 sigprocmask(SIG_BLOCK, &nset, (sigset_t *)NULL);
1119 safe_signal(s, old_action);
1120 if (colljmp_p) {
1121 colljmp_p = 0;
1122 hadintr = 0;
1123 siglongjmp(colljmp, 1);
1128 * On interrupt, come here to save the partial message in ~/dead.letter.
1129 * Then jump out of the collection loop.
1131 /*ARGSUSED*/
1132 static void
1133 collint(int s)
1136 * the control flow is subtle, because we can be called from ~q.
1138 if (!hadintr) {
1139 if (value("ignore") != NULL) {
1140 puts("@");
1141 fflush(stdout);
1142 clearerr(stdin);
1143 return;
1145 hadintr = 1;
1146 siglongjmp(colljmp, 1);
1148 exit_status |= 04;
1149 rewind(collf);
1150 if (value("save") != NULL && s != 0)
1151 savedeadletter(collf);
1152 siglongjmp(collabort, 1);
1155 /*ARGSUSED*/
1156 static void
1157 collhup(int s)
1159 (void)s;
1160 rewind(collf);
1161 savedeadletter(collf);
1163 * Let's pretend nobody else wants to clean up,
1164 * a true statement at this time.
1166 exit(1);
1169 void
1170 savedeadletter(FILE *fp)
1172 FILE *dbuf;
1173 int c, lines = 0, bytes = 0;
1174 char *cp;
1176 if (fsize(fp) == 0)
1177 return;
1178 cp = getdeadletter();
1179 c = umask(077);
1180 dbuf = Fopen(cp, "a");
1181 umask(c);
1182 if (dbuf == NULL)
1183 return;
1184 printf("\"%s\" ", cp);
1185 while ((c = getc(fp)) != EOF) {
1186 putc(c, dbuf);
1187 bytes++;
1188 if (c == '\n')
1189 lines++;
1191 Fclose(dbuf);
1192 printf("%d/%d\n", lines, bytes);
1193 rewind(fp);
1196 static int
1197 putesc(const char *s, FILE *stream)
1199 int n = 0;
1201 while (s[0]) {
1202 if (s[0] == '\\') {
1203 if (s[1] == 't') {
1204 if (putc('\t', stream) == EOF)
1205 return (-1);
1206 n++;
1207 s += 2;
1208 continue;
1210 if (s[1] == 'n') {
1211 if (putc('\n', stream) == EOF)
1212 return (-1);
1213 n++;
1214 s += 2;
1215 continue;
1218 if (putc(s[0], stream) == EOF)
1219 return (-1);
1220 n++;
1221 s++;
1223 if (putc('\n', stream) == EOF)
1224 return (-1);
1225 return (++n);