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.
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
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
42 static char sccsid
[] = "@(#)collect.c 2.54 (gritter) 6/16/07";
47 * Mail -- a mail program
49 * Collect input from standard input, handling
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
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
,
94 static struct attachment
*append_attachments(struct attachment
*attach
,
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
);
111 siglongjmp(pipejmp
, 1);
115 * Execute cmd and insert its standard output into fp.
118 insertcommand(FILE *fp
, char *cmd
)
125 if (sigsetjmp(pipejmp
, 1))
129 if ((obuf
= Popen(cmd
, "r", cp
, 0)) == NULL
) {
133 safe_signal(SIGPIPE
, onpipe
);
134 while ((c
= getc(obuf
)) != EOF
)
137 safe_signal(SIGPIPE
, SIG_IGN
);
139 safe_signal(SIGPIPE
, dflpipe
);
146 print_collf(FILE *collf
, struct header
*hp
)
150 struct attachment
*ap
;
153 size_t linecnt
, maxlines
, linesize
= 0, linelen
, count
, count2
;
159 count
= count2
= fsize(collf
);
160 if (is_a_tty
[0] && is_a_tty
[1] && (cp
= value("crt")) != NULL
) {
162 fgetline(&lbuf
, &linesize
, &count2
, NULL
, collf
, 0);
165 maxlines
= (*cp
== '\0' ? screensize() : atoi(cp
));
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
) {
184 if (sigsetjmp(pipejmp
, 1))
186 obuf
= Popen(cp
, "w", NULL
, 1);
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"))
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
) {
206 fprintf(obuf
, " message %u", ap
->a_msgno
);
208 fprintf(obuf
, " %s", ap
->a_name
);
215 if (obuf
!= stdout
) {
216 safe_signal(SIGPIPE
, SIG_IGN
);
218 safe_signal(SIGPIPE
, dflpipe
);
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
;
231 fbuf
= Fopen(name
, "r");
236 interactive
= value("interactive");
242 while (fgetline(&linebuf
, &linesize
, &count
, &linelen
, fbuf
, 0)
244 if (fwrite(linebuf
, sizeof *linebuf
, linelen
, collf
)
249 if (interactive
!= NULL
&& echo
)
250 fwrite(linebuf
, sizeof *linebuf
, linelen
, stdout
);
252 (*charcount
) += linelen
;
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
;
270 ap
= csalloc(1, sizeof *ap
);
272 printf("#%u\tmessage %u\n", number
, ap
->a_msgno
);
275 snprintf(prefix
, sizeof prefix
, catgets(catd
, CATSET
, 50,
276 "#%u\tfilename: "), number
);
279 if ((ap
->a_name
= readtty(prefix
, ap
->a_name
)) == NULL
)
281 if ((exf
= expand(ap
->a_name
)) != NULL
)
283 if (access(ap
->a_name
, R_OK
) == 0)
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.
326 edit_attachments(struct attachment
*attach
)
328 struct attachment
*ap
, *nap
;
331 for (ap
= attach
; ap
; ap
= ap
->a_flink
) {
332 if ((nap
= read_attachment_data(ap
, attno
)) == NULL
) {
334 ap
->a_blink
->a_flink
= ap
->a_flink
;
336 attach
= ap
->a_flink
;
338 ap
->a_flink
->a_blink
= ap
->a_blink
;
344 while ((nap
= read_attachment_data(NULL
, attno
)) != NULL
) {
345 for (ap
= attach
; ap
&& ap
->a_flink
; ap
= ap
->a_flink
);
358 * Put the given file to the end of the attachment list.
361 add_attachment(struct attachment
*attach
, char *file
, int expand_file
)
363 struct attachment
*ap
, *nap
;
370 file
= savestr(file
);
371 if (access(file
, R_OK
) != 0)
374 nap
= csalloc(1, sizeof *nap
);
376 if (attach
!= NULL
) {
377 for (ap
= attach
; ap
->a_flink
!= NULL
; ap
= ap
->a_flink
);
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
)
396 struct attachment
*ap
;
399 while (*cp
!= '\0' && blankchar(*cp
& 0377))
401 while (*cp
!= '\0') {
403 while (*cp
!= '\0' && !blankchar(*cp
& 0377))
407 if (*names
!= '\0') {
408 if ((ap
= add_attachment(attach
, names
, 1)) == NULL
)
415 while (*cp
!= '\0' && blankchar(*cp
& 0377))
422 collect(struct header
*hp
, int printheaders
, struct message
*mp
,
423 char *quotefile
, int doprefix
, int tflag
)
430 struct ignoretab
*quoteig
;
431 int lc
, cc
, escape
, eofcount
;
433 char *linebuf
= NULL
, *cp
, *quote
= NULL
;
435 char *tempMail
= NULL
;
439 enum sendaction action
;
440 sighandler_type savedtop
;
443 if (value("interactive") != NULL
)
448 * Start catching signals from here, but we're still die on interrupts
449 * until we're in the main loop.
452 sigaddset(&nset
, SIGINT
);
453 sigaddset(&nset
, SIGHUP
);
454 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
455 handlerpush(collint
);
456 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
457 safe_signal(SIGINT
, collint
);
458 if ((savehup
= safe_signal(SIGHUP
, SIG_IGN
)) != SIG_IGN
)
459 safe_signal(SIGHUP
, collhup
);
460 savetstp
= safe_signal(SIGTSTP
, collstop
);
461 savettou
= safe_signal(SIGTTOU
, collstop
);
462 savettin
= safe_signal(SIGTTIN
, collstop
);
463 if (sigsetjmp(collabort
, 1)) {
464 if (tempMail
!= NULL
) {
470 if (sigsetjmp(colljmp
, 1)) {
471 if (tempMail
!= NULL
) {
477 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
480 if ((collf
= Ftemp(&tempMail
, "Rs", "w+", 0600, 1)) == NULL
) {
481 perror(catgets(catd
, CATSET
, 51, "temporary mail file"));
487 if ((cp
= value("SNAIL_HEAD")) != NULL
) {
494 * If we are going to prompt for a subject,
495 * refrain from printing a newline after
496 * the headers (since some people mind).
500 t
= GTO
|GSUBJECT
|GCC
|GNL
;
501 if (value("fullnames"))
503 if (hp
->h_subject
== NULL
&& (val
& val_INTERACT
) &&
504 (value("ask") != NULL
|| value("asksub") != NULL
))
505 t
&= ~GNL
, getfields
|= GSUBJECT
;
506 if (hp
->h_to
== NULL
&& (val
& val_INTERACT
))
507 t
&= ~GNL
, getfields
|= GTO
;
508 if (value("bsdcompat") == NULL
&& value("askatend") == NULL
&&
509 (val
& val_INTERACT
)) {
510 if (hp
->h_bcc
== NULL
&& value("askbcc"))
511 t
&= ~GNL
, getfields
|= GBCC
;
512 if (hp
->h_cc
== NULL
&& value("askcc"))
513 t
&= ~GNL
, getfields
|= GCC
;
516 puthead(hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
,
523 * Quote an original message
525 if (mp
!= NULL
&& (doprefix
|| (quote
= value("quote")) != NULL
)) {
530 if ((cp
= value("fwdheading")) == NULL
)
531 cp
= "-------- Original Message --------";
533 fprintf(collf
, "%s\n", cp
);
534 fprintf(stdout
, "%s\n", cp
);
536 } else if (strcmp(quote
, "noheading") == 0) {
538 } else if (strcmp(quote
, "headers") == 0) {
540 } else if (strcmp(quote
, "allheaders") == 0) {
542 action
= SEND_QUOTE_ALL
;
544 cp
= hfield("from", mp
);
546 mime_write(cp
, strlen(cp
),
547 collf
, CONV_FROMHDR
, TD_NONE
,
550 mime_write(cp
, strlen(cp
),
551 stdout
, CONV_FROMHDR
, TD_NONE
,
554 fwrite(catgets(catd
, CATSET
, 52,
555 " wrote:\n\n"), sizeof(char), 9, collf
);
556 fwrite(catgets(catd
, CATSET
, 52,
557 " wrote:\n\n"), sizeof(char), 9, stdout
);
560 cp
= value("indentprefix");
561 if (cp
!= NULL
&& *cp
== '\0')
563 send(mp
, collf
, quoteig
, doprefix
? NULL
: cp
, action
, NULL
);
564 send(mp
, stdout
, quoteig
, doprefix
? NULL
: cp
, action
, NULL
);
567 if ((cp
= value("escape")) != NULL
)
574 if (!sigsetjmp(colljmp
, 1)) {
576 grabh(hp
, getfields
, 1);
577 if (quotefile
!= NULL
) {
578 if (include_file(NULL
, quotefile
, &lc
, &cc
, 1) != 0)
583 * Come here for printing the after-signal message.
584 * Duplicate messages won't be printed because
585 * the write is aborted if we get a SIGTTOU.
590 fprintf(stderr
, catgets(catd
, CATSET
, 53,
591 "\n(Interrupt -- one more to kill letter)\n"));
593 printf(catgets(catd
, CATSET
, 54, "(continue)\n"));
597 if ((val
& val_INTERACT
) == 0 && tildeflag
<= 0 &&
598 !is_a_tty
[0] && !tflag
) {
600 * No tilde escapes, interrupts not expected. Copy
601 * standard input the simple way.
603 linebuf
= srealloc(linebuf
, linesize
= BUFSIZ
);
604 while ((count
= fread(linebuf
, sizeof *linebuf
,
605 linesize
, stdin
)) > 0) {
606 if ((size_t)count
!= fwrite(linebuf
, sizeof *linebuf
,
614 count
= readline(stdin
, &linebuf
, &linesize
);
617 if ((val
& val_INTERACT
) &&
618 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
619 printf(catgets(catd
, CATSET
, 55,
620 "Use \".\" to terminate letter\n"));
625 if (tflag
&& count
== 0) {
627 if (makeheader(collf
, hp
) != OKAY
)
635 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
636 (val
& val_INTERACT
) &&
637 (value("dot") != NULL
|| value("ignoreeof") != NULL
))
639 if (linebuf
[0] != escape
||
640 ((val
& val_INTERACT
) == 0 && tildeflag
== 0) ||
642 if (putline(collf
, linebuf
, count
) < 0)
650 * On double escape, just send the single one.
651 * Otherwise, it's an error.
654 if (putline(collf
, &linebuf
[1], count
- 1) < 0)
659 printf(catgets(catd
, CATSET
, 56,
660 "Unknown tilde escape.\n"));
662 #ifdef DEBUG_COMMANDS
669 #endif /* DEBUG_COMMANDS */
672 * Shell escape, send the balance of the
680 * Escape to command mode, but be nice!
683 execute(&linebuf
[2], 1, count
- 2);
687 * Simulate end of file on input.
692 * Same as 'q', but no dead.letter saving.
700 * Force a quit of sending mail.
701 * Act like an interrupt happened.
709 * Grab a bunch of headers.
712 grabh(hp
, GTO
|GSUBJECT
|GCC
|GBCC
,
713 value("bsdcompat") != NULL
&&
714 value("bsdorder") != NULL
);
715 while (hp
->h_to
== NULL
);
719 * Grab extra headers.
722 grabh(hp
, GEXTRA
, 0);
723 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
));
727 * Add to the To list.
729 while ((hp
->h_to
= checkaddrs(cat(hp
->h_to
,
730 sextract(&linebuf
[2], GTO
|GFULL
))))
735 * Set the Subject list.
738 while (whitechar(*cp
& 0377))
740 hp
->h_subject
= savestr(cp
);
744 * Edit the attachment list.
746 if (linebuf
[2] != '\0')
747 hp
->h_attach
= append_attachments(hp
->h_attach
,
750 hp
->h_attach
= edit_attachments(hp
->h_attach
);
754 * Add to the CC list.
756 hp
->h_cc
= checkaddrs(cat(hp
->h_cc
,
757 sextract(&linebuf
[2], GCC
|GFULL
)));
761 * Add stuff to blind carbon copies list.
763 hp
->h_bcc
= checkaddrs(cat(hp
->h_bcc
,
764 sextract(&linebuf
[2], GBCC
|GFULL
)));
767 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
768 linebuf
[linesize
-1]='\0';
774 * Search for the file name,
775 * then open it and copy the contents to collf.
778 while (whitechar(*cp
& 0377))
781 printf(catgets(catd
, CATSET
, 57,
782 "Interpolate what file?\n"));
786 insertcommand(collf
, cp
+ 1);
793 printf(catgets(catd
, CATSET
, 58,
794 "%s: Directory\n"), cp
);
797 if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
801 printf(catgets(catd
, CATSET
, 59, "\"%s\" "), cp
);
803 if (include_file(fbuf
, cp
, &lc
, &cc
, 0) != 0)
805 printf(catgets(catd
, CATSET
, 60, "%d/%d\n"), lc
, cc
);
809 * Insert an environment variable into the file.
812 while (whitechar(*cp
& 0377))
814 if ((cp
= value(cp
)) == NULL
|| *cp
== '\0')
823 * Insert the contents of a signature variable.
825 if ((cp
= value(c
== 'a' ? "sign" : "Sign")) != NULL
&&
834 * Write the message on a file.
837 while (blankchar(*cp
& 0377))
840 fprintf(stderr
, catgets(catd
, CATSET
, 61,
841 "Write what file!?\n"));
844 if ((cp
= expand(cp
)) == NULL
)
847 exwrite(cp
, collf
, 1);
854 * Interpolate the named messages, if we
855 * are in receiving mail mode. Does the
856 * standard list processing garbage.
857 * If ~f is given, we don't shift over.
859 if (forward(linebuf
+ 2, collf
, c
) < 0)
864 * Print out the current state of the
865 * message without altering anything.
867 print_collf(collf
, hp
);
871 * Pipe message through command.
872 * Collect output as new message.
875 mespipe(&linebuf
[2]);
880 * Edit the current message.
881 * 'e' means to use EDITOR
882 * 'v' means to use VISUAL
885 mesedit(c
, value("editheaders") ? hp
: NULL
);
889 * Last the lengthy help string.
890 * (Very ugly, but take care for compiler supported
894 "-------------------- ~ ESCAPES ----------------------------\n"
895 "~~ Quote a single tilde\n"
896 "~@ [file ...] Edit attachment list\n"
897 "~b users Add users to \"blind\" cc list\n"
898 "~c users Add users to cc list\n"
899 "~d Read in dead.letter\n"
900 "~e Edit the message buffer\n"
901 "~f messages Read in messages without indenting lines\n"
902 "~F messages Same as ~f, but keep all header lines\n"
903 "~h Prompt for to list, subject, cc, and \"blind\" cc list\n");
905 "~r file Read a file into the message buffer\n"
906 "~p Print the message buffer\n"
907 "~q Abort message composition and save text to dead.letter\n"
908 "~m messages Read in messages with each line indented\n"
909 "~M messages Same as ~m, but keep all header lines\n"
910 "~s subject Set subject\n"
911 "~t users Add users to to list\n"
912 "~v Invoke display editor on message\n"
913 "~w file Write message onto file\n"
914 "~x Abort message composition and discard text written so far\n");
916 "~!command Invoke the shell\n"
917 "~:command Execute a regular command\n"
918 "-----------------------------------------------------------\n");
931 if ((cp
= value("SNAIL_TAIL")) != NULL
) {
942 sigaddset(&nset
, SIGINT
);
943 sigaddset(&nset
, SIGHUP
);
945 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
947 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
949 safe_signal(SIGINT
, saveint
);
950 safe_signal(SIGHUP
, savehup
);
951 safe_signal(SIGTSTP
, savetstp
);
952 safe_signal(SIGTTOU
, savettou
);
953 safe_signal(SIGTTIN
, savettin
);
954 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
959 * Write a file, ex-like if f set.
962 exwrite(char *name
, FILE *fp
, int f
)
970 printf("\"%s\" ", name
);
973 if ((of
= Fopen(name
, "a")) == NULL
) {
979 while ((c
= getc(fp
)) != EOF
) {
991 printf(catgets(catd
, CATSET
, 65, "%d/%ld\n"), lc
, cc
);
997 makeheader(FILE *fp
, struct header
*hp
)
1003 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1004 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1012 extract_header(fp
, hp
);
1013 while ((c
= getc(fp
)) != EOF
)
1019 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
))
1025 * Edit the message being collected on fp.
1026 * On return, make the edit file the new temp file.
1029 mesedit(int c
, struct header
*hp
)
1031 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1032 FILE *nf
= run_editor(collf
, (off_t
)-1, c
, 0, hp
, NULL
, SEND_MBOX
,
1040 fseek(nf
, 0L, SEEK_END
);
1045 safe_signal(SIGINT
, sigint
);
1049 * Pipe the message through the command.
1050 * Old message is on stdin of command;
1051 * New message collected from stdout.
1052 * Sh -c must return 0 to accept the new message.
1058 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1062 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1063 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1070 * stdin = current message.
1071 * stdout = new message.
1073 if ((shell
= value("SHELL")) == NULL
)
1075 if (run_command(shell
,
1076 0, fileno(collf
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
1080 if (fsize(nf
) == 0) {
1081 fprintf(stderr
, catgets(catd
, CATSET
, 67,
1082 "No bytes from \"%s\" !?\n"), cmd
);
1089 fseek(nf
, 0L, SEEK_END
);
1093 safe_signal(SIGINT
, sigint
);
1097 * Interpolate the named messages into the current
1098 * message, preceding each line with a tab.
1099 * Return a count of the number of characters now in
1100 * the message, or -1 if an error is encountered writing
1101 * the message temporary. The flag argument is 'm' if we
1102 * should shift over and 'f' if not.
1105 forward(char *ms
, FILE *fp
, int f
)
1108 struct ignoretab
*ig
;
1110 enum sendaction action
;
1113 msgvec
= (int *)salloc((msgCount
+1) * sizeof *msgvec
);
1116 if (getmsglist(ms
, msgvec
, 0) < 0)
1119 *msgvec
= first(0, MMNORM
);
1121 printf(catgets(catd
, CATSET
, 68,
1122 "No appropriate messages\n"));
1127 if (f
== 'f' || f
== 'F')
1129 else if ((tabst
= value("indentprefix")) == NULL
)
1131 ig
= upperchar(f
) ? (struct ignoretab
*)NULL
: ignore
;
1132 action
= upperchar(f
) ? SEND_QUOTE_ALL
: SEND_QUOTE
;
1133 printf(catgets(catd
, CATSET
, 69, "Interpolating:"));
1134 for (; *msgvec
!= 0; msgvec
++) {
1135 struct message
*mp
= message
+ *msgvec
- 1;
1138 printf(" %d", *msgvec
);
1139 if (send(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
1140 perror(catgets(catd
, CATSET
, 70,
1141 "temporary mail file"));
1150 * Print (continue) when continued after ^Z.
1156 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1160 sigaddset(&nset
, s
);
1161 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1163 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1164 safe_signal(s
, old_action
);
1168 siglongjmp(colljmp
, 1);
1173 * On interrupt, come here to save the partial message in ~/dead.letter.
1174 * Then jump out of the collection loop.
1181 * the control flow is subtle, because we can be called from ~q.
1184 if (value("ignore") != NULL
) {
1191 siglongjmp(colljmp
, 1);
1195 if (value("save") != NULL
&& s
!= 0)
1196 savedeadletter(collf
);
1197 siglongjmp(collabort
, 1);
1206 savedeadletter(collf
);
1208 * Let's pretend nobody else wants to clean up,
1209 * a true statement at this time.
1215 savedeadletter(FILE *fp
)
1218 int c
, lines
= 0, bytes
= 0;
1223 cp
= getdeadletter();
1225 dbuf
= Fopen(cp
, "a");
1229 printf("\"%s\" ", cp
);
1230 while ((c
= getc(fp
)) != EOF
) {
1237 printf("%d/%d\n", lines
, bytes
);
1242 putesc(const char *s
, FILE *stream
)
1261 putc(s
[0]&0377, stream
);