send.c: fix compiler warnings..
[s-mailx.git] / collect.c
blob02731f3ed32677a97608270043e0398310524168
1 /*
2 * Heirloom mailx - 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.
40 #ifndef lint
41 #ifdef DOSCCS
42 static char sccsid[] = "@(#)collect.c 2.54 (gritter) 6/16/07";
43 #endif
44 #endif /* not lint */
47 * Mail -- a mail program
49 * Collect input from standard input, handling
50 * ~ escapes.
53 #include "rcv.h"
54 #include "extern.h"
55 #include <unistd.h>
56 #include <sys/stat.h>
58 /* TODO longjmp() globbering as in cmd1.c and cmd3.c (see there)
59 * TODO Problem: Popen doesn't encapsulate all cases of open failures,
60 * TODO may leave child running if fdopen() fails! */
63 * Read a message from standard output and return a read file to it
64 * or NULL on error.
68 * The following hokiness with global variables is so that on
69 * receipt of an interrupt signal, the partial message can be salted
70 * away on dead.letter.
73 static sighandler_type saveint; /* Previous SIGINT value */
74 static sighandler_type savehup; /* Previous SIGHUP value */
75 static sighandler_type savetstp; /* Previous SIGTSTP value */
76 static sighandler_type savettou; /* Previous SIGTTOU value */
77 static sighandler_type savettin; /* Previous SIGTTIN value */
78 static FILE *collf; /* File for saving away */
79 static int hadintr; /* Have seen one SIGINT so far */
81 static sigjmp_buf colljmp; /* To get back to work */
82 static int colljmp_p; /* whether to long jump */
83 static sigjmp_buf collabort; /* To end collection with error */
85 static sigjmp_buf pipejmp; /* On broken pipe */
87 static void onpipe(int signo);
88 static void insertcommand(FILE *fp, char *cmd);
89 static void print_collf(FILE *collf, struct header *hp);
90 static int include_file(FILE *fbuf, char *name, int *linecount,
91 int *charcount, int echo);
92 static struct attachment *read_attachment_data(struct attachment *ap,
93 unsigned number);
94 static struct attachment *append_attachments(struct attachment *attach,
95 char *names);
96 static int exwrite(char *name, FILE *fp, int f);
97 static enum okay makeheader(FILE *fp, struct header *hp);
98 static void mesedit(int c, struct header *hp);
99 static void mespipe(char *cmd);
100 static int forward(char *ms, FILE *fp, int f);
101 static void collstop(int s);
102 static void collint(int s);
103 static void collhup(int s);
104 static int putesc(const char *s, FILE *stream);
106 /*ARGSUSED*/
107 static void
108 onpipe(int signo)
110 (void)signo;
111 siglongjmp(pipejmp, 1);
115 * Execute cmd and insert its standard output into fp.
117 static void
118 insertcommand(FILE *fp, char *cmd)
120 FILE *obuf = NULL;
121 char *cp;
122 int c;
124 cp = value("SHELL");
125 if (sigsetjmp(pipejmp, 1))
126 goto endpipe;
127 if (cp == NULL)
128 cp = SHELL;
129 if ((obuf = Popen(cmd, "r", cp, 0)) == NULL) {
130 perror(cmd);
131 return;
133 safe_signal(SIGPIPE, onpipe);
134 while ((c = getc(obuf)) != EOF)
135 putc(c, fp);
136 endpipe:
137 safe_signal(SIGPIPE, SIG_IGN);
138 Pclose(obuf);
139 safe_signal(SIGPIPE, dflpipe);
143 * ~p command.
145 static void
146 print_collf(FILE *collf, struct header *hp)
148 char *lbuf = NULL;
149 FILE *obuf = stdout;
150 struct attachment *ap;
151 char *cp;
152 enum gfield gf;
153 size_t linecnt, maxlines, linesize = 0, linelen, count, count2;
155 (void)&obuf;
156 (void)&cp;
157 fflush(collf);
158 rewind(collf);
159 count = count2 = fsize(collf);
160 if (is_a_tty[0] && is_a_tty[1] && (cp = value("crt")) != NULL) {
161 for (linecnt = 0;
162 fgetline(&lbuf, &linesize, &count2, NULL, collf, 0);
163 linecnt++);
164 rewind(collf);
165 maxlines = (*cp == '\0' ? screensize() : atoi(cp));
166 maxlines -= 4;
167 if (hp->h_to)
168 maxlines--;
169 if (hp->h_subject)
170 maxlines--;
171 if (hp->h_cc)
172 maxlines--;
173 if (hp->h_bcc)
174 maxlines--;
175 if (hp->h_attach)
176 maxlines--;
177 maxlines -= myaddrs(hp) != NULL || hp->h_from != NULL;
178 maxlines -= value("ORGANIZATION") != NULL ||
179 hp->h_organization != NULL;
180 maxlines -= value("replyto") != NULL || hp->h_replyto != NULL;
181 maxlines -= value("sender") != NULL || hp->h_sender != NULL;
182 if ((long)maxlines < 0 || linecnt > maxlines) {
183 cp = get_pager();
184 if (sigsetjmp(pipejmp, 1))
185 goto endpipe;
186 obuf = Popen(cp, "w", NULL, 1);
187 if (obuf == NULL) {
188 perror(cp);
189 obuf = stdout;
190 } else
191 safe_signal(SIGPIPE, onpipe);
194 fprintf(obuf, catgets(catd, CATSET, 62,
195 "-------\nMessage contains:\n"));
196 gf = GIDENT|GTO|GSUBJECT|GCC|GBCC|GNL|GFILES;
197 if (value("fullnames"))
198 gf |= GCOMMA;
199 puthead(hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
200 while (fgetline(&lbuf, &linesize, &count, &linelen, collf, 1))
201 prout(lbuf, linelen, obuf);
202 if (hp->h_attach != NULL) {
203 fputs(catgets(catd, CATSET, 63, "Attachments:"), obuf);
204 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
205 if (ap->a_msgno)
206 fprintf(obuf, " message %u", ap->a_msgno);
207 else
208 fprintf(obuf, " %s", ap->a_name);
209 if (ap->a_flink)
210 putc(',', obuf);
212 putc('\n', obuf);
214 endpipe:
215 if (obuf != stdout) {
216 safe_signal(SIGPIPE, SIG_IGN);
217 Pclose(obuf);
218 safe_signal(SIGPIPE, dflpipe);
220 if (lbuf)
221 free(lbuf);
224 static int
225 include_file(FILE *fbuf, char *name, int *linecount, int *charcount, int echo)
227 char *interactive, *linebuf = NULL;
228 size_t linesize = 0, linelen, count;
230 if (fbuf == NULL)
231 fbuf = Fopen(name, "r");
232 if (fbuf == NULL) {
233 perror(name);
234 return -1;
236 interactive = value("interactive");
237 *linecount = 0;
238 *charcount = 0;
239 fflush(fbuf);
240 rewind(fbuf);
241 count = fsize(fbuf);
242 while (fgetline(&linebuf, &linesize, &count, &linelen, fbuf, 0)
243 != NULL) {
244 if (fwrite(linebuf, sizeof *linebuf, linelen, collf)
245 != linelen) {
246 Fclose(fbuf);
247 return -1;
249 if (interactive != NULL && echo)
250 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
251 (*linecount)++;
252 (*charcount) += linelen;
254 if (linebuf)
255 free(linebuf);
256 Fclose(fbuf);
257 return 0;
261 * Ask the user to edit file names and other data for the given
262 * attachment. NULL is returned if no file name is given.
264 static struct attachment *
265 read_attachment_data(struct attachment *ap, unsigned number)
267 char prefix[80], *cp;
269 if (ap == NULL)
270 ap = csalloc(1, sizeof *ap);
271 if (ap->a_msgno) {
272 printf("#%u\tmessage %u\n", number, ap->a_msgno);
273 return ap;
275 snprintf(prefix, sizeof prefix, catgets(catd, CATSET, 50,
276 "#%u\tfilename: "), number);
277 for (;;) {
278 char *exf;
279 if ((ap->a_name = readtty(prefix, ap->a_name)) == NULL)
280 break;
281 if ((exf = expand(ap->a_name)) != NULL)
282 ap->a_name = exf;
283 if (access(ap->a_name, R_OK) == 0)
284 break;
285 perror(ap->a_name);
287 if ((ap->a_name && (value("attachment-ask-charset"))) ||
288 ((cp = value("sendcharsets")) != NULL &&
289 strchr(cp, ',') != NULL)) {
290 snprintf(prefix, sizeof prefix, "#%u\tcharset: ", number);
291 ap->a_charset = readtty(prefix, ap->a_charset);
294 * The "attachment-ask-content-*" variables are left undocumented
295 * since they are for RFC connoisseurs only.
297 if (ap->a_name && value("attachment-ask-content-type")) {
298 if (ap->a_content_type == NULL)
299 ap->a_content_type = mime_filecontent(ap->a_name);
300 snprintf(prefix, sizeof prefix, "#%u\tContent-Type: ", number);
301 ap->a_content_type = readtty(prefix, ap->a_content_type);
303 if (ap->a_name && value("attachment-ask-content-disposition")) {
304 snprintf(prefix, sizeof prefix,
305 "#%u\tContent-Disposition: ", number);
306 ap->a_content_disposition = readtty(prefix,
307 ap->a_content_disposition);
309 if (ap->a_name && value("attachment-ask-content-id")) {
310 snprintf(prefix, sizeof prefix, "#%u\tContent-ID: ", number);
311 ap->a_content_id = readtty(prefix, ap->a_content_id);
313 if (ap->a_name && value("attachment-ask-content-description")) {
314 snprintf(prefix, sizeof prefix,
315 "#%u\tContent-Description: ", number);
316 ap->a_content_description = readtty(prefix,
317 ap->a_content_description);
319 return ap->a_name ? ap : NULL;
323 * Interactively edit the attachment list.
325 struct attachment *
326 edit_attachments(struct attachment *attach)
328 struct attachment *ap, *nap;
329 unsigned attno = 1;
331 for (ap = attach; ap; ap = ap->a_flink) {
332 if ((nap = read_attachment_data(ap, attno)) == NULL) {
333 if (ap->a_blink)
334 ap->a_blink->a_flink = ap->a_flink;
335 else
336 attach = ap->a_flink;
337 if (ap->a_flink)
338 ap->a_flink->a_blink = ap->a_blink;
339 else
340 return attach;
341 } else
342 attno++;
344 while ((nap = read_attachment_data(NULL, attno)) != NULL) {
345 for (ap = attach; ap && ap->a_flink; ap = ap->a_flink);
346 if (ap)
347 ap->a_flink = nap;
348 nap->a_blink = ap;
349 nap->a_flink = NULL;
350 if (attach == NULL)
351 attach = nap;
352 attno++;
354 return attach;
358 * Put the given file to the end of the attachment list.
360 struct attachment *
361 add_attachment(struct attachment *attach, char *file, int expand_file)
363 struct attachment *ap, *nap;
365 if (expand_file) {
366 file = expand(file);
367 if (file == NULL)
368 return NULL;
369 } else
370 file = savestr(file);
371 if (access(file, R_OK) != 0)
372 return NULL;
373 /*LINTED*/
374 nap = csalloc(1, sizeof *nap);
375 nap->a_name = file;
376 if (attach != NULL) {
377 for (ap = attach; ap->a_flink != NULL; ap = ap->a_flink);
378 ap->a_flink = nap;
379 nap->a_blink = ap;
380 } else {
381 nap->a_blink = NULL;
382 attach = nap;
384 return attach;
388 * Append the whitespace-separated file names to the end of
389 * the attachment list.
391 static struct attachment *
392 append_attachments(struct attachment *attach, char *names)
394 char *cp;
395 int c;
396 struct attachment *ap;
398 cp = names;
399 while (*cp != '\0' && blankchar(*cp & 0377))
400 cp++;
401 while (*cp != '\0') {
402 names = cp;
403 while (*cp != '\0' && !blankchar(*cp & 0377))
404 cp++;
405 c = *cp;
406 *cp++ = '\0';
407 if (*names != '\0') {
408 if ((ap = add_attachment(attach, names, 1)) == NULL)
409 perror(names);
410 else
411 attach = ap;
413 if (c == '\0')
414 break;
415 while (*cp != '\0' && blankchar(*cp & 0377))
416 cp++;
418 return attach;
421 FILE *
422 collect(struct header *hp, int printheaders, struct message *mp,
423 char *quotefile, int doprefix, int tflag)
425 FILE *fbuf;
426 struct ignoretab *quoteig;
427 int lc, cc, escape, eofcount;
428 int c, t;
429 char *linebuf = NULL, *cp, *quote = NULL;
430 size_t linesize;
431 char *tempMail = NULL;
432 int getfields;
433 sigset_t oset, nset;
434 long count;
435 enum sendaction action;
436 sighandler_type savedtop;
438 collf = NULL;
440 * Start catching signals from here, but we're still die on interrupts
441 * until we're in the main loop.
443 sigemptyset(&nset);
444 sigaddset(&nset, SIGINT);
445 sigaddset(&nset, SIGHUP);
446 sigprocmask(SIG_BLOCK, &nset, &oset);
447 handlerpush(collint);
448 if ((saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
449 safe_signal(SIGINT, collint);
450 if ((savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
451 safe_signal(SIGHUP, collhup);
452 savetstp = safe_signal(SIGTSTP, collstop);
453 savettou = safe_signal(SIGTTOU, collstop);
454 savettin = safe_signal(SIGTTIN, collstop);
455 if (sigsetjmp(collabort, 1)) {
456 if (tempMail != NULL) {
457 rm(tempMail);
458 Ftfree(&tempMail);
460 goto err;
462 if (sigsetjmp(colljmp, 1)) {
463 if (tempMail != NULL) {
464 rm(tempMail);
465 Ftfree(&tempMail);
467 goto err;
469 sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
471 noreset++;
472 if ((collf = Ftemp(&tempMail, "Rs", "w+", 0600, 1)) == NULL) {
473 perror(catgets(catd, CATSET, 51, "temporary mail file"));
474 goto err;
476 unlink(tempMail);
477 Ftfree(&tempMail);
479 if ((cp = value("SNAIL_HEAD")) != NULL) {
480 if (is_a_tty[0])
481 putesc(cp, stdout);
482 putesc(cp, collf);
486 * If we are going to prompt for a subject,
487 * refrain from printing a newline after
488 * the headers (since some people mind).
490 getfields = 0;
491 if (!tflag) {
492 t = GTO|GSUBJECT|GCC|GNL;
493 if (value("fullnames"))
494 t |= GCOMMA;
495 if (hp->h_subject == NULL && value("interactive") != NULL &&
496 (value("ask") != NULL || value("asksub") != NULL))
497 t &= ~GNL, getfields |= GSUBJECT;
498 if (hp->h_to == NULL && value("interactive") != NULL)
499 t &= ~GNL, getfields |= GTO;
500 if (value("bsdcompat") == NULL && value("askatend") == NULL &&
501 value("interactive")) {
502 if (hp->h_bcc == NULL && value("askbcc"))
503 t &= ~GNL, getfields |= GBCC;
504 if (hp->h_cc == NULL && value("askcc"))
505 t &= ~GNL, getfields |= GCC;
507 if (printheaders) {
508 puthead(hp, stdout, t, SEND_TODISP, CONV_NONE,
509 NULL, NULL);
510 fflush(stdout);
515 * Quote an original message
517 if (mp != NULL && (doprefix || (quote = value("quote")) != NULL)) {
518 quoteig = allignore;
519 action = SEND_QUOTE;
520 if (doprefix) {
521 quoteig = fwdignore;
522 if ((cp = value("fwdheading")) == NULL)
523 cp = "-------- Original Message --------";
524 if (*cp) {
525 fprintf(collf, "%s\n", cp);
526 fprintf(stdout, "%s\n", cp);
528 } else if (strcmp(quote, "noheading") == 0) {
529 /*EMPTY*/;
530 } else if (strcmp(quote, "headers") == 0) {
531 quoteig = ignore;
532 } else if (strcmp(quote, "allheaders") == 0) {
533 quoteig = NULL;
534 action = SEND_QUOTE_ALL;
535 } else {
536 cp = hfield("from", mp);
537 if (cp != NULL) {
538 mime_write(cp, strlen(cp),
539 collf, CONV_FROMHDR, TD_NONE,
540 NULL, (size_t) 0,
541 NULL, NULL);
542 mime_write(cp, strlen(cp),
543 stdout, CONV_FROMHDR, TD_NONE,
544 NULL, (size_t) 0,
545 NULL, NULL);
546 fwrite(catgets(catd, CATSET, 52,
547 " wrote:\n\n"), sizeof(char), 9, collf);
548 fwrite(catgets(catd, CATSET, 52,
549 " wrote:\n\n"), sizeof(char), 9, stdout);
552 cp = value("indentprefix");
553 if (cp != NULL && *cp == '\0')
554 cp = "\t";
555 send(mp, collf, quoteig, doprefix ? NULL : cp, action, NULL);
556 send(mp, stdout, quoteig, doprefix ? NULL : cp, action, NULL);
559 if ((cp = value("escape")) != NULL)
560 escape = *cp;
561 else
562 escape = ESCAPE;
563 eofcount = 0;
564 hadintr = 0;
566 if (!sigsetjmp(colljmp, 1)) {
567 if (getfields)
568 grabh(hp, getfields, 1);
569 if (quotefile != NULL) {
570 if (include_file(NULL, quotefile, &lc, &cc, 1) != 0)
571 goto err;
573 } else {
575 * Come here for printing the after-signal message.
576 * Duplicate messages won't be printed because
577 * the write is aborted if we get a SIGTTOU.
579 cont:
580 if (hadintr) {
581 fflush(stdout);
582 fprintf(stderr, catgets(catd, CATSET, 53,
583 "\n(Interrupt -- one more to kill letter)\n"));
584 } else {
585 printf(catgets(catd, CATSET, 54, "(continue)\n"));
586 fflush(stdout);
589 if (value("interactive") == NULL && tildeflag <= 0 && !is_a_tty[0] &&
590 !tflag) {
592 * No tilde escapes, interrupts not expected. Copy
593 * standard input the simple way.
595 linebuf = srealloc(linebuf, linesize = BUFSIZ);
596 while ((count = fread(linebuf, sizeof *linebuf,
597 linesize, stdin)) > 0) {
598 if ((size_t)count != fwrite(linebuf, sizeof *linebuf,
599 count, collf))
600 goto err;
602 goto out;
604 for (;;) {
605 colljmp_p = 1;
606 count = readline(stdin, &linebuf, &linesize);
607 colljmp_p = 0;
608 if (count < 0) {
609 if (value("interactive") != NULL &&
610 value("ignoreeof") != NULL && ++eofcount < 25) {
611 printf(catgets(catd, CATSET, 55,
612 "Use \".\" to terminate letter\n"));
613 continue;
615 break;
617 if (tflag && count == 0) {
618 rewind(collf);
619 if (makeheader(collf, hp) != OKAY)
620 goto err;
621 rewind(collf);
622 tflag = 0;
623 continue;
625 eofcount = 0;
626 hadintr = 0;
627 if (linebuf[0] == '.' && linebuf[1] == '\0' &&
628 value("interactive") != NULL &&
629 (value("dot") != NULL || value("ignoreeof") != NULL))
630 break;
631 if (linebuf[0] != escape ||
632 (value("interactive") == NULL &&
633 tildeflag == 0) ||
634 tildeflag < 0) {
635 if (putline(collf, linebuf, count) < 0)
636 goto err;
637 continue;
639 c = linebuf[1];
640 switch (c) {
641 default:
643 * On double escape, just send the single one.
644 * Otherwise, it's an error.
646 if (c == escape) {
647 if (putline(collf, &linebuf[1], count - 1) < 0)
648 goto err;
649 else
650 break;
652 printf(catgets(catd, CATSET, 56,
653 "Unknown tilde escape.\n"));
654 break;
655 #ifdef DEBUG_COMMANDS
656 case 'C':
658 * Dump core.
660 core(NULL);
661 break;
662 #endif /* DEBUG_COMMANDS */
663 case '!':
665 * Shell escape, send the balance of the
666 * line to sh -c.
668 shell(&linebuf[2]);
669 break;
670 case ':':
671 case '_':
673 * Escape to command mode, but be nice!
675 inhook = 0;
676 execute(&linebuf[2], 1, count - 2);
677 goto cont;
678 case '.':
680 * Simulate end of file on input.
682 goto out;
683 case 'x':
685 * Same as 'q', but no dead.letter saving.
687 hadintr++;
688 collint(0);
689 exit(1);
690 /*NOTREACHED*/
691 case 'q':
693 * Force a quit of sending mail.
694 * Act like an interrupt happened.
696 hadintr++;
697 collint(SIGINT);
698 exit(1);
699 /*NOTREACHED*/
700 case 'h':
702 * Grab a bunch of headers.
705 grabh(hp, GTO|GSUBJECT|GCC|GBCC,
706 value("bsdcompat") != NULL &&
707 value("bsdorder") != NULL);
708 while (hp->h_to == NULL);
709 goto cont;
710 case 'H':
712 * Grab extra headers.
715 grabh(hp, GEXTRA, 0);
716 while (check_from_and_sender(hp->h_from, hp->h_sender));
717 goto cont;
718 case 't':
720 * Add to the To list.
722 while ((hp->h_to = checkaddrs(cat(hp->h_to,
723 sextract(&linebuf[2], GTO|GFULL))))
724 == NULL);
725 break;
726 case 's':
728 * Set the Subject list.
730 cp = &linebuf[2];
731 while (whitechar(*cp & 0377))
732 cp++;
733 hp->h_subject = savestr(cp);
734 break;
735 case '@':
737 * Edit the attachment list.
739 if (linebuf[2] != '\0')
740 hp->h_attach = append_attachments(hp->h_attach,
741 &linebuf[2]);
742 else
743 hp->h_attach = edit_attachments(hp->h_attach);
744 break;
745 case 'c':
747 * Add to the CC list.
749 hp->h_cc = checkaddrs(cat(hp->h_cc,
750 sextract(&linebuf[2], GCC|GFULL)));
751 break;
752 case 'b':
754 * Add stuff to blind carbon copies list.
756 hp->h_bcc = checkaddrs(cat(hp->h_bcc,
757 sextract(&linebuf[2], GBCC|GFULL)));
758 break;
759 case 'd':
760 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
761 linebuf[linesize-1]='\0';
762 /*FALLTHRU*/
763 case 'r':
764 case '<':
766 * Invoke a file:
767 * Search for the file name,
768 * then open it and copy the contents to collf.
770 cp = &linebuf[2];
771 while (whitechar(*cp & 0377))
772 cp++;
773 if (*cp == '\0') {
774 printf(catgets(catd, CATSET, 57,
775 "Interpolate what file?\n"));
776 break;
778 if (*cp == '!') {
779 insertcommand(collf, cp + 1);
780 break;
782 cp = expand(cp);
783 if (cp == NULL)
784 break;
785 if (is_dir(cp)) {
786 printf(catgets(catd, CATSET, 58,
787 "%s: Directory\n"), cp);
788 break;
790 if ((fbuf = Fopen(cp, "r")) == NULL) {
791 perror(cp);
792 break;
794 printf(catgets(catd, CATSET, 59, "\"%s\" "), cp);
795 fflush(stdout);
796 if (include_file(fbuf, cp, &lc, &cc, 0) != 0)
797 goto err;
798 printf(catgets(catd, CATSET, 60, "%d/%d\n"), lc, cc);
799 break;
800 case 'i':
802 * Insert an environment variable into the file.
804 cp = &linebuf[2];
805 while (whitechar(*cp & 0377))
806 cp++;
807 if ((cp = value(cp)) == NULL || *cp == '\0')
808 break;
809 if (is_a_tty[0])
810 putesc(cp, stdout);
811 putesc(cp, collf);
812 break;
813 case 'a':
814 case 'A':
816 * Insert the contents of a signature variable.
818 if ((cp = value(c == 'a' ? "sign" : "Sign")) != NULL &&
819 *cp != '\0') {
820 if (is_a_tty[0])
821 putesc(cp, stdout);
822 putesc(cp, collf);
824 break;
825 case 'w':
827 * Write the message on a file.
829 cp = &linebuf[2];
830 while (blankchar(*cp & 0377))
831 cp++;
832 if (*cp == '\0') {
833 fprintf(stderr, catgets(catd, CATSET, 61,
834 "Write what file!?\n"));
835 break;
837 if ((cp = expand(cp)) == NULL)
838 break;
839 rewind(collf);
840 exwrite(cp, collf, 1);
841 break;
842 case 'm':
843 case 'M':
844 case 'f':
845 case 'F':
847 * Interpolate the named messages, if we
848 * are in receiving mail mode. Does the
849 * standard list processing garbage.
850 * If ~f is given, we don't shift over.
852 if (forward(linebuf + 2, collf, c) < 0)
853 goto err;
854 goto cont;
855 case 'p':
857 * Print out the current state of the
858 * message without altering anything.
860 print_collf(collf, hp);
861 goto cont;
862 case '|':
864 * Pipe message through command.
865 * Collect output as new message.
867 rewind(collf);
868 mespipe(&linebuf[2]);
869 goto cont;
870 case 'v':
871 case 'e':
873 * Edit the current message.
874 * 'e' means to use EDITOR
875 * 'v' means to use VISUAL
877 rewind(collf);
878 mesedit(c, value("editheaders") ? hp : NULL);
879 goto cont;
880 case '?':
882 * Last the lengthy help string.
883 * (Very ugly, but take care for compiler supported
884 * string lengths :()
886 puts(
887 "-------------------- ~ ESCAPES ----------------------------\n"
888 "~~ Quote a single tilde\n"
889 "~@ [file ...] Edit attachment list\n"
890 "~b users Add users to \"blind\" cc list\n"
891 "~c users Add users to cc list\n"
892 "~d Read in dead.letter\n"
893 "~e Edit the message buffer\n"
894 "~f messages Read in messages without indenting lines\n"
895 "~F messages Same as ~f, but keep all header lines\n"
896 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
897 puts(
898 "~r file Read a file into the message buffer\n"
899 "~p Print the message buffer\n"
900 "~q Abort message composition and save text to dead.letter\n"
901 "~m messages Read in messages with each line indented\n"
902 "~M messages Same as ~m, but keep all header lines\n"
903 "~s subject Set subject\n"
904 "~t users Add users to to list\n"
905 "~v Invoke display editor on message\n"
906 "~w file Write message onto file\n"
907 "~x Abort message composition and discard text written so far\n");
908 puts(
909 "~!command Invoke the shell\n"
910 "~:command Execute a regular command\n"
911 "-----------------------------------------------------------\n");
912 break;
916 goto out;
917 err:
918 if (collf != NULL) {
919 Fclose(collf);
920 collf = NULL;
922 out:
923 if (collf != NULL) {
924 if ((cp = value("SNAIL_TAIL")) != NULL) {
925 if (is_a_tty[0])
926 putesc(cp, stdout);
927 fflush(collf);
928 putesc(cp, collf);
930 rewind(collf);
932 handlerpop();
933 noreset--;
934 sigemptyset(&nset);
935 sigaddset(&nset, SIGINT);
936 sigaddset(&nset, SIGHUP);
937 #ifndef OLDBUG
938 sigprocmask(SIG_BLOCK, &nset, (sigset_t *)NULL);
939 #else
940 sigprocmask(SIG_BLOCK, &nset, &oset);
941 #endif
942 safe_signal(SIGINT, saveint);
943 safe_signal(SIGHUP, savehup);
944 safe_signal(SIGTSTP, savetstp);
945 safe_signal(SIGTTOU, savettou);
946 safe_signal(SIGTTIN, savettin);
947 sigprocmask(SIG_SETMASK, &oset, (sigset_t *)NULL);
948 return collf;
952 * Write a file, ex-like if f set.
954 static int
955 exwrite(char *name, FILE *fp, int f)
957 FILE *of;
958 int c;
959 long cc;
960 int lc;
962 if (f) {
963 printf("\"%s\" ", name);
964 fflush(stdout);
966 if ((of = Fopen(name, "a")) == NULL) {
967 perror(NULL);
968 return(-1);
970 lc = 0;
971 cc = 0;
972 while ((c = getc(fp)) != EOF) {
973 cc++;
974 if (c == '\n')
975 lc++;
976 putc(c, of);
977 if (ferror(of)) {
978 perror(name);
979 Fclose(of);
980 return(-1);
983 Fclose(of);
984 printf(catgets(catd, CATSET, 65, "%d/%ld\n"), lc, cc);
985 fflush(stdout);
986 return(0);
989 static enum okay
990 makeheader(FILE *fp, struct header *hp)
992 char *tempEdit;
993 FILE *nf;
994 int c;
996 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
997 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
998 Fclose(nf);
999 unlink(tempEdit);
1000 Ftfree(&tempEdit);
1001 return STOP;
1003 unlink(tempEdit);
1004 Ftfree(&tempEdit);
1005 extract_header(fp, hp);
1006 while ((c = getc(fp)) != EOF)
1007 putc(c, nf);
1008 if (fp != collf)
1009 Fclose(collf);
1010 Fclose(fp);
1011 collf = nf;
1012 if (check_from_and_sender(hp->h_from, hp->h_sender))
1013 return STOP;
1014 return OKAY;
1018 * Edit the message being collected on fp.
1019 * On return, make the edit file the new temp file.
1021 static void
1022 mesedit(int c, struct header *hp)
1024 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
1025 FILE *nf = run_editor(collf, (off_t)-1, c, 0, hp, NULL, SEND_MBOX,
1026 sigint);
1028 if (nf != NULL) {
1029 if (hp) {
1030 rewind(nf);
1031 makeheader(nf, hp);
1032 } else {
1033 fseek(nf, 0L, SEEK_END);
1034 Fclose(collf);
1035 collf = nf;
1038 safe_signal(SIGINT, sigint);
1042 * Pipe the message through the command.
1043 * Old message is on stdin of command;
1044 * New message collected from stdout.
1045 * Sh -c must return 0 to accept the new message.
1047 static void
1048 mespipe(char *cmd)
1050 FILE *nf;
1051 sighandler_type sigint = safe_signal(SIGINT, SIG_IGN);
1052 char *tempEdit;
1053 char *shell;
1055 if ((nf = Ftemp(&tempEdit, "Re", "w+", 0600, 1)) == NULL) {
1056 perror(catgets(catd, CATSET, 66, "temporary mail edit file"));
1057 goto out;
1059 fflush(collf);
1060 unlink(tempEdit);
1061 Ftfree(&tempEdit);
1063 * stdin = current message.
1064 * stdout = new message.
1066 if ((shell = value("SHELL")) == NULL)
1067 shell = SHELL;
1068 if (run_command(shell,
1069 0, fileno(collf), fileno(nf), "-c", cmd, NULL) < 0) {
1070 Fclose(nf);
1071 goto out;
1073 if (fsize(nf) == 0) {
1074 fprintf(stderr, catgets(catd, CATSET, 67,
1075 "No bytes from \"%s\" !?\n"), cmd);
1076 Fclose(nf);
1077 goto out;
1080 * Take new files.
1082 fseek(nf, 0L, SEEK_END);
1083 Fclose(collf);
1084 collf = nf;
1085 out:
1086 safe_signal(SIGINT, sigint);
1090 * Interpolate the named messages into the current
1091 * message, preceding each line with a tab.
1092 * Return a count of the number of characters now in
1093 * the message, or -1 if an error is encountered writing
1094 * the message temporary. The flag argument is 'm' if we
1095 * should shift over and 'f' if not.
1097 static int
1098 forward(char *ms, FILE *fp, int f)
1100 int *msgvec;
1101 struct ignoretab *ig;
1102 char *tabst;
1103 enum sendaction action;
1105 /*LINTED*/
1106 msgvec = (int *)salloc((msgCount+1) * sizeof *msgvec);
1107 if (msgvec == NULL)
1108 return(0);
1109 if (getmsglist(ms, msgvec, 0) < 0)
1110 return(0);
1111 if (*msgvec == 0) {
1112 *msgvec = first(0, MMNORM);
1113 if (*msgvec == 0) {
1114 printf(catgets(catd, CATSET, 68,
1115 "No appropriate messages\n"));
1116 return(0);
1118 msgvec[1] = 0;
1120 if (f == 'f' || f == 'F')
1121 tabst = NULL;
1122 else if ((tabst = value("indentprefix")) == NULL)
1123 tabst = "\t";
1124 ig = upperchar(f) ? (struct ignoretab *)NULL : ignore;
1125 action = upperchar(f) ? SEND_QUOTE_ALL : SEND_QUOTE;
1126 printf(catgets(catd, CATSET, 69, "Interpolating:"));
1127 for (; *msgvec != 0; msgvec++) {
1128 struct message *mp = message + *msgvec - 1;
1130 touch(mp);
1131 printf(" %d", *msgvec);
1132 if (send(mp, fp, ig, tabst, action, NULL) < 0) {
1133 perror(catgets(catd, CATSET, 70,
1134 "temporary mail file"));
1135 return(-1);
1138 printf("\n");
1139 return(0);
1143 * Print (continue) when continued after ^Z.
1145 /*ARGSUSED*/
1146 static void
1147 collstop(int s)
1149 sighandler_type old_action = safe_signal(s, SIG_DFL);
1150 sigset_t nset;
1152 sigemptyset(&nset);
1153 sigaddset(&nset, s);
1154 sigprocmask(SIG_UNBLOCK, &nset, (sigset_t *)NULL);
1155 kill(0, s);
1156 sigprocmask(SIG_BLOCK, &nset, (sigset_t *)NULL);
1157 safe_signal(s, old_action);
1158 if (colljmp_p) {
1159 colljmp_p = 0;
1160 hadintr = 0;
1161 siglongjmp(colljmp, 1);
1166 * On interrupt, come here to save the partial message in ~/dead.letter.
1167 * Then jump out of the collection loop.
1169 /*ARGSUSED*/
1170 static void
1171 collint(int s)
1174 * the control flow is subtle, because we can be called from ~q.
1176 if (!hadintr) {
1177 if (value("ignore") != NULL) {
1178 puts("@");
1179 fflush(stdout);
1180 clearerr(stdin);
1181 return;
1183 hadintr = 1;
1184 siglongjmp(colljmp, 1);
1186 exit_status |= 04;
1187 rewind(collf);
1188 if (value("save") != NULL && s != 0)
1189 savedeadletter(collf);
1190 siglongjmp(collabort, 1);
1193 /*ARGSUSED*/
1194 static void
1195 collhup(int s)
1197 (void)s;
1198 rewind(collf);
1199 savedeadletter(collf);
1201 * Let's pretend nobody else wants to clean up,
1202 * a true statement at this time.
1204 exit(1);
1207 void
1208 savedeadletter(FILE *fp)
1210 FILE *dbuf;
1211 int c, lines = 0, bytes = 0;
1212 char *cp;
1214 if (fsize(fp) == 0)
1215 return;
1216 cp = getdeadletter();
1217 c = umask(077);
1218 dbuf = Fopen(cp, "a");
1219 umask(c);
1220 if (dbuf == NULL)
1221 return;
1222 printf("\"%s\" ", cp);
1223 while ((c = getc(fp)) != EOF) {
1224 putc(c, dbuf);
1225 bytes++;
1226 if (c == '\n')
1227 lines++;
1229 Fclose(dbuf);
1230 printf("%d/%d\n", lines, bytes);
1231 rewind(fp);
1234 static int
1235 putesc(const char *s, FILE *stream)
1237 int n = 0;
1239 while (s[0]) {
1240 if (s[0] == '\\') {
1241 if (s[1] == 't') {
1242 putc('\t', stream);
1243 n++;
1244 s += 2;
1245 continue;
1247 if (s[1] == 'n') {
1248 putc('\n', stream);
1249 n++;
1250 s += 2;
1251 continue;
1254 putc(s[0]&0377, stream);
1255 n++;
1256 s++;
1258 putc('\n', stream);
1259 return ++n;