junk.c: file_expand() may fail
[s-mailx.git] / collect.c
blobafdaa25f6a77af843cc72b342f8ecbb968441dd0
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>
53 * Read a message from standard output and return a read file to it
54 * or NULL on error.
58 * The following hokiness with global variables is so that on
59 * receipt of an interrupt signal, the partial message can be salted
60 * away on dead.letter.
63 static sighandler_type saveint; /* Previous SIGINT value */
64 static sighandler_type savehup; /* Previous SIGHUP value */
65 static sighandler_type savetstp; /* Previous SIGTSTP value */
66 static sighandler_type savettou; /* Previous SIGTTOU value */
67 static sighandler_type savettin; /* Previous SIGTTIN value */
68 static FILE *collf; /* File for saving away */
69 static int hadintr; /* Have seen one SIGINT so far */
71 static sigjmp_buf colljmp; /* To get back to work */
72 static int colljmp_p; /* whether to long jump */
73 static sigjmp_buf collabort; /* To end collection with error */
75 static sigjmp_buf pipejmp; /* On broken pipe */
77 static void onpipe(int signo);
78 static void insertcommand(FILE *fp, char *cmd);
79 static void print_collf(FILE *collf, struct header *hp);
80 static int include_file(FILE *fbuf, char *name, int *linecount,
81 int *charcount, int echo);
82 static struct attachment *read_attachment_data(struct attachment *ap,
83 unsigned number);
84 static struct attachment *append_attachments(struct attachment *attach,
85 char *names);
86 static int exwrite(char *name, FILE *fp, int f);
87 static enum okay makeheader(FILE *fp, struct header *hp);
88 static void mesedit(int c, struct header *hp);
89 static void mespipe(char *cmd);
90 static int forward(char *ms, FILE *fp, int f);
91 static void collstop(int s);
92 static void collint(int s);
93 static void collhup(int s);
94 static int putesc(const char *s, FILE *stream);
96 /*ARGSUSED*/
97 static void
98 onpipe(int signo)
100 (void)signo;
101 siglongjmp(pipejmp, 1);
105 * Execute cmd and insert its standard output into fp.
107 static void
108 insertcommand(FILE *fp, char *cmd)
110 FILE *obuf = NULL;
111 char *volatile cp;
112 int c;
114 cp = value("SHELL");
115 if (sigsetjmp(pipejmp, 1))
116 goto endpipe;
117 if (cp == NULL)
118 cp = SHELL;
119 if ((obuf = Popen(cmd, "r", cp, 0)) == NULL) {
120 perror(cmd);
121 return;
123 safe_signal(SIGPIPE, onpipe);
124 while ((c = getc(obuf)) != EOF)
125 putc(c, fp);
126 endpipe:
127 safe_signal(SIGPIPE, SIG_IGN);
128 Pclose(obuf);
129 safe_signal(SIGPIPE, dflpipe);
133 * ~p command.
135 static void
136 print_collf(FILE *collf, struct header *hp)
138 char *lbuf = NULL;
139 FILE *volatile obuf = stdout;
140 struct attachment *ap;
141 char *cp;
142 enum gfield gf;
143 size_t linecnt, maxlines, linesize = 0, linelen, count, count2;
145 (void)&obuf;
146 (void)&cp;
147 fflush(collf);
148 rewind(collf);
149 count = count2 = fsize(collf);
150 if (is_a_tty[0] && is_a_tty[1] && (cp = value("crt")) != NULL) {
151 for (linecnt = 0;
152 fgetline(&lbuf, &linesize, &count2, NULL, collf, 0);
153 linecnt++);
154 rewind(collf);
155 maxlines = (*cp == '\0' ? screensize() : atoi(cp));
156 maxlines -= 4;
157 if (hp->h_to)
158 maxlines--;
159 if (hp->h_subject)
160 maxlines--;
161 if (hp->h_cc)
162 maxlines--;
163 if (hp->h_bcc)
164 maxlines--;
165 if (hp->h_attach)
166 maxlines--;
167 maxlines -= myaddrs(hp) != NULL || hp->h_from != NULL;
168 maxlines -= value("ORGANIZATION") != NULL ||
169 hp->h_organization != NULL;
170 maxlines -= value("replyto") != NULL || hp->h_replyto != NULL;
171 maxlines -= value("sender") != NULL || hp->h_sender != NULL;
172 if ((long)maxlines < 0 || linecnt > maxlines) {
173 cp = get_pager();
174 if (sigsetjmp(pipejmp, 1))
175 goto endpipe;
176 obuf = Popen(cp, "w", NULL, 1);
177 if (obuf == NULL) {
178 perror(cp);
179 obuf = stdout;
180 } else
181 safe_signal(SIGPIPE, onpipe);
184 fprintf(obuf, catgets(catd, CATSET, 62,
185 "-------\nMessage contains:\n"));
186 gf = GIDENT|GTO|GSUBJECT|GCC|GBCC|GNL|GFILES;
187 if (value("fullnames"))
188 gf |= GCOMMA;
189 puthead(hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
190 while (fgetline(&lbuf, &linesize, &count, &linelen, collf, 1))
191 prout(lbuf, linelen, obuf);
192 if (hp->h_attach != NULL) {
193 fputs(catgets(catd, CATSET, 63, "Attachments:"), obuf);
194 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
195 if (ap->a_msgno)
196 fprintf(obuf, " message %u", ap->a_msgno);
197 else
198 fprintf(obuf, " %s", ap->a_name);
199 if (ap->a_flink)
200 putc(',', obuf);
202 putc('\n', obuf);
204 endpipe:
205 if (obuf != stdout) {
206 safe_signal(SIGPIPE, SIG_IGN);
207 Pclose(obuf);
208 safe_signal(SIGPIPE, dflpipe);
210 if (lbuf)
211 free(lbuf);
214 static int
215 include_file(FILE *fbuf, char *name, int *linecount, int *charcount, int echo)
217 char *interactive, *linebuf = NULL;
218 size_t linesize = 0, linelen, count;
220 if (fbuf == NULL)
221 fbuf = Fopen(name, "r");
222 if (fbuf == NULL) {
223 perror(name);
224 return (-1);
226 interactive = value("interactive");
227 *linecount = 0;
228 *charcount = 0;
229 fflush(fbuf);
230 rewind(fbuf);
231 count = fsize(fbuf);
232 while (fgetline(&linebuf, &linesize, &count, &linelen, fbuf, 0)
233 != NULL) {
234 if (fwrite(linebuf, sizeof *linebuf, linelen, collf)
235 != linelen) {
236 Fclose(fbuf);
237 return (-1);
239 if (interactive != NULL && echo)
240 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
241 (*linecount)++;
242 (*charcount) += linelen;
244 if (linebuf)
245 free(linebuf);
246 Fclose(fbuf);
247 if (fflush(collf))
248 return (-1);
249 return (0);
253 * Ask the user to edit file names and other data for the given
254 * attachment. NULL is returned if no file name is given.
256 static struct attachment *
257 read_attachment_data(struct attachment *ap, unsigned number)
259 char prefix[80], *cp;
261 if (ap == NULL)
262 ap = csalloc(1, sizeof *ap);
263 if (ap->a_msgno) {
264 printf("#%u\tmessage %u\n", number, ap->a_msgno);
265 return ap;
267 snprintf(prefix, sizeof prefix, catgets(catd, CATSET, 50,
268 "#%u\tfilename: "), number);
269 for (;;) {
270 char *exf;
271 if ((ap->a_name = readtty(prefix, ap->a_name)) == NULL)
272 break;
273 if ((exf = file_expand(ap->a_name)) == NULL)
274 continue;
275 ap->a_name = exf;
276 if (access(ap->a_name, R_OK) == 0)
277 break;
278 perror(ap->a_name);
280 if ((ap->a_name && (value("attachment-ask-charset"))) ||
281 ((cp = value("sendcharsets")) != NULL &&
282 strchr(cp, ',') != NULL)) {
283 snprintf(prefix, sizeof prefix, "#%u\tcharset: ", number);
284 ap->a_charset = readtty(prefix, ap->a_charset);
287 * The "attachment-ask-content-*" variables are left undocumented
288 * since they are for RFC connoisseurs only.
290 if (ap->a_name && value("attachment-ask-content-type")) {
291 if (ap->a_content_type == NULL)
292 ap->a_content_type = mime_filecontent(ap->a_name);
293 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
294 ap->a_content_type = readtty(prefix, ap->a_content_type);
296 if (ap->a_name && value("attachment-ask-content-disposition")) {
297 snprintf(prefix, sizeof prefix,
298 "#%u\tContent-Disposition: ", number);
299 ap->a_content_disposition = readtty(prefix,
300 ap->a_content_disposition);
302 if (ap->a_name && value("attachment-ask-content-id")) {
303 snprintf(prefix, sizeof prefix, "#%u\tContent-ID: ", number);
304 ap->a_content_id = readtty(prefix, ap->a_content_id);
306 if (ap->a_name && value("attachment-ask-content-description")) {
307 snprintf(prefix, sizeof prefix,
308 "#%u\tContent-Description: ", number);
309 ap->a_content_description = readtty(prefix,
310 ap->a_content_description);
312 return ap->a_name ? ap : NULL;
316 * Interactively edit the attachment list.
318 struct attachment *
319 edit_attachments(struct attachment *attach)
321 struct attachment *ap, *nap;
322 unsigned attno = 1;
324 for (ap = attach; ap; ap = ap->a_flink) {
325 if ((nap = read_attachment_data(ap, attno)) == NULL) {
326 if (ap->a_blink)
327 ap->a_blink->a_flink = ap->a_flink;
328 else
329 attach = ap->a_flink;
330 if (ap->a_flink)
331 ap->a_flink->a_blink = ap->a_blink;
332 else
333 return attach;
334 } else
335 attno++;
337 while ((nap = read_attachment_data(NULL, attno)) != NULL) {
338 for (ap = attach; ap && ap->a_flink; ap = ap->a_flink);
339 if (ap)
340 ap->a_flink = nap;
341 nap->a_blink = ap;
342 nap->a_flink = NULL;
343 if (attach == NULL)
344 attach = nap;
345 attno++;
347 return attach;
351 * Put the given file to the end of the attachment list.
353 struct attachment *
354 add_attachment(struct attachment *attach, char *file, int expand_file)
356 struct attachment *ap, *nap;
358 if (expand_file) {
359 if ((file = file_expand(file)) == NULL)
360 return (NULL);
361 } else
362 file = savestr(file);
363 if (access(file, R_OK) != 0)
364 return NULL;
365 /*LINTED*/
366 nap = csalloc(1, sizeof *nap);
367 nap->a_name = file;
368 if (attach != NULL) {
369 for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink);
370 ap->a_flink = nap;
371 nap->a_blink = ap;
372 } else {
373 nap->a_blink = NULL;
374 attach = nap;
376 return attach;
380 * Append the whitespace-separated file names to the end of
381 * the attachment list.
383 static struct attachment *
384 append_attachments(struct attachment *attach, char *names)
386 char *cp;
387 int c;
388 struct attachment *ap;
390 cp = names;
391 while (*cp != '\0' && blankchar(*cp & 0377))
392 cp++;
393 while (*cp != '\0') {
394 names = cp;
395 while (*cp != '\0' && !blankchar(*cp & 0377))
396 cp++;
397 c = *cp;
398 *cp++ = '\0';
399 if (*names != '\0') {
400 if ((ap = add_attachment(attach, names, 1)) == NULL)
401 perror(names);
402 else
403 attach = ap;
405 if (c == '\0')
406 break;
407 while (*cp != '\0' && blankchar(*cp & 0377))
408 cp++;
410 return attach;
413 FILE *
414 collect(struct header *hp, int printheaders, struct message *mp,
415 char *quotefile, int doprefix, int volatile tflag)
417 enum {
418 val_INTERACT = 1
421 FILE *fbuf;
422 struct ignoretab *quoteig;
423 int lc, cc, eofcount, val, c, t;
424 int volatile escape, getfields;
425 char *linebuf = NULL, *cp, *quote = NULL, *tempMail = NULL;
426 size_t linesize;
427 long count;
428 enum sendaction action;
429 sigset_t oset, nset;
430 sighandler_type savedtop;
432 val = 0;
433 if (value("interactive") != NULL)
434 val |= val_INTERACT;
436 collf = NULL;
438 * Start catching signals from here, but we're still die on interrupts
439 * until we're in the main loop.
441 sigemptyset(&nset);
442 sigaddset(&nset, SIGINT);
443 sigaddset(&nset, SIGHUP);
444 sigprocmask(SIG_BLOCK, &nset, &oset);
445 handlerpush(collint);
446 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
447 safe_signal(SIGINT, collint);
448 if ((savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
449 safe_signal(SIGHUP, collhup);
450 savetstp = safe_signal(SIGTSTP, collstop);
451 savettou = safe_signal(SIGTTOU, collstop);
452 savettin = safe_signal(SIGTTIN, collstop);
453 if (sigsetjmp(collabort, 1))
454 goto jerr;
455 if (sigsetjmp(colljmp, 1))
456 goto jerr;
457 sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
459 noreset++;
460 if ((collf = Ftemp(&tempMail, "Rs", "w+", 0600, 1)) == NULL) {
461 perror(tr(51, "temporary mail file"));
462 goto jerr;
464 unlink(tempMail);
465 Ftfree(&tempMail);
467 if ((cp = value("NAIL_HEAD")) != NULL && putesc(cp, collf) < 0)
468 goto jerr;
471 * If we are going to prompt for a subject,
472 * refrain from printing a newline after
473 * the headers (since some people mind).
475 getfields = 0;
476 if (! tflag) {
477 t = GTO|GSUBJECT|GCC|GNL;
478 if (value("fullnames"))
479 t |= GCOMMA;
480 if (hp->h_subject == NULL && (val & val_INTERACT) &&
481 (value("ask") != NULL || value("asksub") != NULL))
482 t &= ~GNL, getfields |= GSUBJECT;
483 if (hp->h_to == NULL && (val & val_INTERACT))
484 t &= ~GNL, getfields |= GTO;
485 if (value("bsdcompat") == NULL && value("askatend") == NULL &&
486 (val & val_INTERACT)) {
487 if (hp->h_bcc == NULL && value("askbcc"))
488 t &= ~GNL, getfields |= GBCC;
489 if (hp->h_cc == NULL && value("askcc"))
490 t &= ~GNL, getfields |= GCC;
492 if (printheaders) {
493 (void)puthead(hp, stdout, t, SEND_TODISP, CONV_NONE,
494 NULL, NULL);
495 (void)fflush(stdout);
500 * Quote an original message
502 if (mp != NULL && (doprefix || (quote = value("quote")) != NULL)) {
503 quoteig = allignore;
504 action = SEND_QUOTE;
505 if (doprefix) {
506 quoteig = fwdignore;
507 if ((cp = value("fwdheading")) == NULL)
508 cp = "-------- Original Message --------";
509 if (*cp && fprintf(collf, "%s\n", cp) < 0)
510 goto jerr;
511 } else if (strcmp(quote, "noheading") == 0) {
512 /*EMPTY*/;
513 } else if (strcmp(quote, "headers") == 0) {
514 quoteig = ignore;
515 } else if (strcmp(quote, "allheaders") == 0) {
516 quoteig = NULL;
517 action = SEND_QUOTE_ALL;
518 } else {
519 cp = hfield1("from", mp);
520 if (cp != NULL && (count = (long)strlen(cp)) > 0) {
521 if (mime_write(cp, count,
522 collf, CONV_FROMHDR, TD_NONE,
523 NULL, (size_t) 0,
524 NULL, NULL) == 0)
525 goto jerr;
526 if (fprintf(collf, tr(52, " wrote:\n\n")) < 0)
527 goto jerr;
530 if (fflush(collf))
531 goto jerr;
532 cp = value("indentprefix");
533 if (cp != NULL && *cp == '\0')
534 cp = "\t";
535 if (send(mp, collf, quoteig, (doprefix ? NULL : cp), action,
536 NULL) < 0)
537 goto jerr;
540 /* Print what we have sofar also on the terminal */
541 (void)rewind(collf);
542 while ((c = getc(collf)) != EOF)
543 (void)putc(c, stdout);
544 if (fseek(collf, 0, SEEK_END))
545 goto jerr;
547 escape = ((cp = value("escape")) != NULL) ? *cp : ESCAPE;
548 eofcount = 0;
549 hadintr = 0;
551 if (! sigsetjmp(colljmp, 1)) {
552 if (getfields)
553 grabh(hp, getfields, 1);
554 if (quotefile != NULL) {
555 if (include_file(NULL, quotefile, &lc, &cc, 1) != 0)
556 goto jerr;
558 } else {
560 * Come here for printing the after-signal message.
561 * Duplicate messages won't be printed because
562 * the write is aborted if we get a SIGTTOU.
564 jcont:
565 if (hadintr) {
566 (void)fprintf(stderr, tr(53,
567 "\n(Interrupt -- one more to kill letter)\n"));
568 } else {
569 printf(tr(54, "(continue)\n"));
570 (void)fflush(stdout);
575 * No tilde escapes, interrupts not expected. Simply copy STDIN
577 if ((val & val_INTERACT) == 0 && tildeflag <= 0 && ! tflag) {
578 linebuf = srealloc(linebuf, linesize = LINESIZE);
579 while ((count = fread(linebuf, sizeof *linebuf,
580 linesize, stdin)) > 0) {
581 if ((size_t)count != fwrite(linebuf, sizeof *linebuf,
582 count, collf))
583 goto jerr;
585 if (fflush(collf))
586 goto jerr;
587 goto jout;
591 * The interactive collect loop
593 for (;;) {
594 colljmp_p = 1;
595 count = readline(stdin, &linebuf, &linesize);
596 colljmp_p = 0;
597 if (count < 0) {
598 if ((val & val_INTERACT) &&
599 value("ignoreeof") != NULL && ++eofcount < 25) {
600 printf(tr(55,
601 "Use \".\" to terminate letter\n"));
602 continue;
604 break;
606 if (tflag && count == 0) {
607 rewind(collf);
608 if (makeheader(collf, hp) != OKAY)
609 goto jerr;
610 rewind(collf);
611 tflag = 0;
612 continue;
614 eofcount = 0;
615 hadintr = 0;
616 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
617 (val & val_INTERACT) &&
618 (value("dot") != NULL ||
619 value("ignoreeof") != NULL))
620 break;
621 if (linebuf[0] != escape ||
622 ((val & val_INTERACT) == 0 && tildeflag == 0) ||
623 tildeflag < 0) {
624 if (putline(collf, linebuf, count) < 0)
625 goto jerr;
626 continue;
629 c = linebuf[1];
630 switch (c) {
631 default:
633 * On double escape, just send the single one.
634 * Otherwise, it's an error.
636 if (c == escape) {
637 if (putline(collf, &linebuf[1], count - 1) < 0)
638 goto jerr;
639 else
640 break;
642 printf(tr(56, "Unknown tilde escape.\n"));
643 break;
644 #ifdef HAVE_ASSERTS
645 case 'C':
646 /* Dump core */
647 core(NULL);
648 break;
649 #endif
650 case '!':
651 /* Shell escape, send the balance of line to sh -c */
652 shell(&linebuf[2]);
653 break;
654 case ':':
655 case '_':
656 /* Escape to command mode, but be nice! */
657 inhook = 0;
658 execute(&linebuf[2], 1, count - 2);
659 goto jcont;
660 case '.':
661 /* Simulate end of file on input */
662 goto jout;
663 case 'x':
664 /* Same as 'q', but no dead.letter saving */
665 hadintr++;
666 collint(0);
667 exit(1);
668 /*NOTREACHED*/
669 case 'q':
671 * Force a quit of sending mail.
672 * Act like an interrupt happened.
674 hadintr++;
675 collint(SIGINT);
676 exit(1);
677 /*NOTREACHED*/
678 case 'h':
679 /* Grab a bunch of headers */
681 grabh(hp, GTO|GSUBJECT|GCC|GBCC,
682 value("bsdcompat") != NULL &&
683 value("bsdorder") != NULL);
684 while (hp->h_to == NULL);
685 goto jcont;
686 case 'H':
687 /* Grab extra headers */
689 grabh(hp, GEXTRA, 0);
690 while (check_from_and_sender(hp->h_from, hp->h_sender));
691 goto jcont;
692 case 't':
693 /* Add to the To list */
694 while ((hp->h_to = cat(hp->h_to, checkaddrs(
695 lextract(&linebuf[2], GTO|GFULL))))
696 == NULL)
698 break;
699 case 's':
700 /* Set the Subject list */
701 cp = &linebuf[2];
702 while (whitechar(*cp))
703 ++cp;
704 hp->h_subject = savestr(cp);
705 break;
706 case '@':
707 /* Edit the attachment list */
708 if (linebuf[2] != '\0')
709 hp->h_attach = append_attachments(hp->h_attach,
710 &linebuf[2]);
711 else
712 hp->h_attach = edit_attachments(hp->h_attach);
713 break;
714 case 'c':
715 /* Add to the CC list */
716 hp->h_cc = cat(hp->h_cc, checkaddrs(
717 lextract(&linebuf[2], GCC|GFULL)));
718 break;
719 case 'b':
720 /* Add stuff to blind carbon copies list */
721 hp->h_bcc = cat(hp->h_bcc, checkaddrs(
722 lextract(&linebuf[2], GBCC|GFULL)));
723 break;
724 case 'd':
725 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
726 linebuf[linesize-1]='\0';
727 /*FALLTHRU*/
728 case 'r':
729 case '<':
731 * Invoke a file:
732 * Search for the file name,
733 * then open it and copy the contents to collf.
735 cp = &linebuf[2];
736 while (whitechar(*cp))
737 cp++;
738 if (*cp == '\0') {
739 fprintf(stderr, tr(57,
740 "Interpolate what file?\n"));
741 break;
743 if (*cp == '!') {
744 insertcommand(collf, cp + 1);
745 break;
747 if ((cp = file_expand(cp)) == NULL)
748 break;
749 if (is_dir(cp)) {
750 fprintf(stderr, tr(58, "%s: Directory\n"), cp);
751 break;
753 if ((fbuf = Fopen(cp, "r")) == NULL) {
754 perror(cp);
755 break;
757 printf(tr(59, "\"%s\" "), cp);
758 fflush(stdout);
759 if (include_file(fbuf, cp, &lc, &cc, 0) != 0)
760 goto jerr;
761 printf(tr(60, "%d/%d\n"), lc, cc);
762 break;
763 case 'i':
764 /* Insert an environment variable into the file */
765 cp = &linebuf[2];
766 while (whitechar(*cp))
767 ++cp;
768 if ((cp = value(cp)) == NULL || *cp == '\0')
769 break;
770 if (putesc(cp, collf) < 0)
771 goto jerr;
772 if ((val & val_INTERACT) && putesc(cp, stdout) < 0)
773 goto jerr;
774 break;
775 case 'a':
776 case 'A':
777 /* Insert the contents of a signature variable */
778 if ((cp = value(c == 'a' ? "sign" : "Sign")) != NULL &&
779 *cp != '\0') {
780 if (putesc(cp, collf) < 0)
781 goto jerr;
782 if ((val & val_INTERACT) &&
783 putesc(cp, stdout) < 0)
784 goto jerr;
786 break;
787 case 'w':
788 /* Write the message on a file */
789 cp = &linebuf[2];
790 while (blankchar(*cp))
791 ++cp;
792 if (*cp == '\0' || (cp = file_expand(cp)) == NULL) {
793 fprintf(stderr, tr(61, "Write what file!?\n"));
794 break;
796 rewind(collf);
797 if (exwrite(cp, collf, 1) < 0)
798 goto jerr;
799 break;
800 case 'm':
801 case 'M':
802 case 'f':
803 case 'F':
805 * Interpolate the named messages, if we
806 * are in receiving mail mode. Does the
807 * standard list processing garbage.
808 * If ~f is given, we don't shift over.
810 if (forward(linebuf + 2, collf, c) < 0)
811 goto jerr;
812 goto jcont;
813 case 'p':
815 * Print out the current state of the
816 * message without altering anything.
818 print_collf(collf, hp);
819 goto jcont;
820 case '|':
822 * Pipe message through command.
823 * Collect output as new message.
825 rewind(collf);
826 mespipe(&linebuf[2]);
827 goto jcont;
828 case 'v':
829 case 'e':
831 * Edit the current message.
832 * 'e' means to use EDITOR
833 * 'v' means to use VISUAL
835 rewind(collf);
836 mesedit(c, value("editheaders") ? hp : NULL);
837 goto jcont;
838 case '?':
840 * Last the lengthy help string.
841 * (Very ugly, but take care for compiler supported
842 * string lengths :()
844 (void)puts(
845 "-------------------- ~ ESCAPES ----------------------------\n"
846 "~~ Quote a single tilde\n"
847 "~@ [file ...] Edit attachment list\n"
848 "~b users Add users to \"blind\" cc list\n"
849 "~c users Add users to cc list\n"
850 "~d Read in dead.letter\n"
851 "~e Edit the message buffer\n"
852 "~f messages Read in messages without indenting lines\n"
853 "~F messages Same as ~f, but keep all header lines\n"
854 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
855 (void)puts(
856 "~r file Read a file into the message buffer\n"
857 "~p Print the message buffer\n"
858 "~q Abort message composition and save text to dead.letter\n"
859 "~m messages Read in messages with each line indented\n"
860 "~M messages Same as ~m, but keep all header lines\n"
861 "~s subject Set subject\n"
862 "~t users Add users to to list\n"
863 "~v Invoke display editor on message\n"
864 "~w file Write message onto file\n"
865 "~x Abort message composition and discard text written so far\n");
866 (void)puts(
867 "~!command Invoke the shell\n"
868 "~:command Execute a regular command\n"
869 "-----------------------------------------------------------\n");
870 break;
874 jout:
875 if (collf != NULL) {
876 if ((cp = value("NAIL_TAIL")) != NULL) {
877 if (putesc(cp, collf) < 0)
878 goto jerr;
879 if ((val & val_INTERACT) && putesc(cp, stdout) < 0)
880 goto jerr;
882 rewind(collf);
884 handlerpop();
885 noreset--;
886 sigemptyset(&nset);
887 sigaddset(&nset, SIGINT);
888 sigaddset(&nset, SIGHUP);
889 sigprocmask(SIG_BLOCK, &nset, (sigset_t*)NULL);
890 safe_signal(SIGINT, saveint);
891 safe_signal(SIGHUP, savehup);
892 safe_signal(SIGTSTP, savetstp);
893 safe_signal(SIGTTOU, savettou);
894 safe_signal(SIGTTIN, savettin);
895 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
896 return (collf);
897 jerr:
898 if (tempMail != NULL) {
899 rm(tempMail);
900 Ftfree(&tempMail);
902 if (collf != NULL) {
903 Fclose(collf);
904 collf = NULL;
906 goto jout;
910 * Write a file, ex-like if f set.
912 static int
913 exwrite(char *name, FILE *fp, int f)
915 FILE *of;
916 int c;
917 long cc;
918 int lc;
920 if (f) {
921 printf("\"%s\" ", name);
922 fflush(stdout);
924 if ((of = Fopen(name, "a")) == NULL) {
925 perror(NULL);
926 return(-1);
928 lc = 0;
929 cc = 0;
930 while ((c = getc(fp)) != EOF) {
931 cc++;
932 if (c == '\n')
933 lc++;
934 putc(c, of);
935 if (ferror(of)) {
936 perror(name);
937 Fclose(of);
938 return(-1);
941 Fclose(of);
942 printf(catgets(catd, CATSET, 65, "%d/%ld\n"), lc, cc);
943 fflush(stdout);
944 return(0);
947 static enum okay
948 makeheader(FILE *fp, struct header *hp)
950 char *tempEdit;
951 FILE *nf;
952 int c;
954 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
955 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
956 Fclose(nf);
957 unlink(tempEdit);
958 Ftfree(&tempEdit);
959 return STOP;
961 unlink(tempEdit);
962 Ftfree(&tempEdit);
963 extract_header(fp, hp);
964 while ((c = getc(fp)) != EOF)
965 putc(c, nf);
966 if (fp != collf)
967 Fclose(collf);
968 Fclose(fp);
969 collf = nf;
970 if (check_from_and_sender(hp->h_from, hp->h_sender))
971 return STOP;
972 return OKAY;
976 * Edit the message being collected on fp.
977 * On return, make the edit file the new temp file.
979 static void
980 mesedit(int c, struct header *hp)
982 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
983 FILE *nf = run_editor(collf, (off_t)-1, c, 0, hp, NULL, SEND_MBOX,
984 sigint);
986 if (nf != NULL) {
987 if (hp) {
988 rewind(nf);
989 makeheader(nf, hp);
990 } else {
991 fseek(nf, 0L, SEEK_END);
992 Fclose(collf);
993 collf = nf;
996 safe_signal(SIGINT, sigint);
1000 * Pipe the message through the command.
1001 * Old message is on stdin of command;
1002 * New message collected from stdout.
1003 * Sh -c must return 0 to accept the new message.
1005 static void
1006 mespipe(char *cmd)
1008 FILE *nf;
1009 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
1010 char *tempEdit;
1011 char *shell;
1013 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
1014 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
1015 goto out;
1017 fflush(collf);
1018 unlink(tempEdit);
1019 Ftfree(&tempEdit);
1021 * stdin = current message.
1022 * stdout = new message.
1024 if ((shell = value("SHELL")) == NULL)
1025 shell = SHELL;
1026 if (run_command(shell,
1027 0, fileno(collf), fileno(nf), "-c", cmd, NULL) < 0) {
1028 Fclose(nf);
1029 goto out;
1031 if (fsize(nf) == 0) {
1032 fprintf(stderr, catgets(catd, CATSET, 67,
1033 "No bytes from \"%s\" !?\n"), cmd);
1034 Fclose(nf);
1035 goto out;
1038 * Take new files.
1040 fseek(nf, 0L, SEEK_END);
1041 Fclose(collf);
1042 collf = nf;
1043 out:
1044 safe_signal(SIGINT, sigint);
1048 * Interpolate the named messages into the current
1049 * message, preceding each line with a tab.
1050 * Return a count of the number of characters now in
1051 * the message, or -1 if an error is encountered writing
1052 * the message temporary. The flag argument is 'm' if we
1053 * should shift over and 'f' if not.
1055 static int
1056 forward(char *ms, FILE *fp, int f)
1058 int *msgvec;
1059 struct ignoretab *ig;
1060 char *tabst;
1061 enum sendaction action;
1063 /*LINTED*/
1064 msgvec = (int *)salloc((msgCount+1) * sizeof *msgvec);
1065 if (msgvec == NULL)
1066 return(0);
1067 if (getmsglist(ms, msgvec, 0) < 0)
1068 return(0);
1069 if (*msgvec == 0) {
1070 *msgvec = first(0, MMNORM);
1071 if (*msgvec == 0) {
1072 printf(catgets(catd, CATSET, 68,
1073 "No appropriate messages\n"));
1074 return(0);
1076 msgvec[1] = 0;
1078 if (f == 'f' || f == 'F')
1079 tabst = NULL;
1080 else if ((tabst = value("indentprefix")) == NULL)
1081 tabst = "\t";
1082 ig = upperchar(f) ? (struct ignoretab *)NULL : ignore;
1083 action = upperchar(f) ? SEND_QUOTE_ALL : SEND_QUOTE;
1084 printf(catgets(catd, CATSET, 69, "Interpolating:"));
1085 for (; *msgvec != 0; msgvec++) {
1086 struct message *mp = message + *msgvec - 1;
1088 touch(mp);
1089 printf(" %d", *msgvec);
1090 if (send(mp, fp, ig, tabst, action, NULL) < 0) {
1091 perror(catgets(catd, CATSET, 70,
1092 "temporary mail file"));
1093 return(-1);
1096 printf("\n");
1097 return(0);
1101 * Print (continue) when continued after ^Z.
1103 /*ARGSUSED*/
1104 static void
1105 collstop(int s)
1107 sighandler_type old_action = safe_signal(s, SIG_DFL);
1108 sigset_t nset;
1110 sigemptyset(&nset);
1111 sigaddset(&nset, s);
1112 sigprocmask(SIG_UNBLOCK, &nset, (sigset_t *)NULL);
1113 kill(0, s);
1114 sigprocmask(SIG_BLOCK, &nset, (sigset_t *)NULL);
1115 safe_signal(s, old_action);
1116 if (colljmp_p) {
1117 colljmp_p = 0;
1118 hadintr = 0;
1119 siglongjmp(colljmp, 1);
1124 * On interrupt, come here to save the partial message in ~/dead.letter.
1125 * Then jump out of the collection loop.
1127 /*ARGSUSED*/
1128 static void
1129 collint(int s)
1132 * the control flow is subtle, because we can be called from ~q.
1134 if (!hadintr) {
1135 if (value("ignore") != NULL) {
1136 puts("@");
1137 fflush(stdout);
1138 clearerr(stdin);
1139 return;
1141 hadintr = 1;
1142 siglongjmp(colljmp, 1);
1144 exit_status |= 04;
1145 rewind(collf);
1146 if (value("save") != NULL && s != 0)
1147 savedeadletter(collf);
1148 siglongjmp(collabort, 1);
1151 /*ARGSUSED*/
1152 static void
1153 collhup(int s)
1155 (void)s;
1156 rewind(collf);
1157 savedeadletter(collf);
1159 * Let's pretend nobody else wants to clean up,
1160 * a true statement at this time.
1162 exit(1);
1165 void
1166 savedeadletter(FILE *fp)
1168 FILE *dbuf;
1169 int c, lines = 0, bytes = 0;
1170 char *cp;
1172 if (fsize(fp) == 0)
1173 return;
1174 cp = getdeadletter();
1175 c = umask(077);
1176 dbuf = Fopen(cp, "a");
1177 umask(c);
1178 if (dbuf == NULL)
1179 return;
1180 printf("\"%s\" ", cp);
1181 while ((c = getc(fp)) != EOF) {
1182 putc(c, dbuf);
1183 bytes++;
1184 if (c == '\n')
1185 lines++;
1187 Fclose(dbuf);
1188 printf("%d/%d\n", lines, bytes);
1189 rewind(fp);
1192 static int
1193 putesc(const char *s, FILE *stream)
1195 int n = 0;
1197 while (s[0]) {
1198 if (s[0] == '\\') {
1199 if (s[1] == 't') {
1200 if (putc('\t', stream) == EOF)
1201 return (-1);
1202 n++;
1203 s += 2;
1204 continue;
1206 if (s[1] == 'n') {
1207 if (putc('\n', stream) == EOF)
1208 return (-1);
1209 n++;
1210 s += 2;
1211 continue;
1214 if (putc(s[0], stream) == EOF)
1215 return (-1);
1216 n++;
1217 s++;
1219 if (putc('\n', stream) == EOF)
1220 return (-1);
1221 return (++n);