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
)
426 struct ignoretab
*quoteig
;
427 int lc
, cc
, escape
, eofcount
;
429 char *linebuf
= NULL
, *cp
, *quote
= NULL
;
431 char *tempMail
= NULL
;
435 enum sendaction action
;
436 sighandler_type savedtop
;
440 * Start catching signals from here, but we're still die on interrupts
441 * until we're in the main loop.
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
) {
462 if (sigsetjmp(colljmp
, 1)) {
463 if (tempMail
!= NULL
) {
469 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
472 if ((collf
= Ftemp(&tempMail
, "Rs", "w+", 0600, 1)) == NULL
) {
473 perror(catgets(catd
, CATSET
, 51, "temporary mail file"));
479 if ((cp
= value("SNAIL_HEAD")) != NULL
) {
486 * If we are going to prompt for a subject,
487 * refrain from printing a newline after
488 * the headers (since some people mind).
492 t
= GTO
|GSUBJECT
|GCC
|GNL
;
493 if (value("fullnames"))
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
;
508 puthead(hp
, stdout
, t
, SEND_TODISP
, CONV_NONE
,
515 * Quote an original message
517 if (mp
!= NULL
&& (doprefix
|| (quote
= value("quote")) != NULL
)) {
522 if ((cp
= value("fwdheading")) == NULL
)
523 cp
= "-------- Original Message --------";
525 fprintf(collf
, "%s\n", cp
);
526 fprintf(stdout
, "%s\n", cp
);
528 } else if (strcmp(quote
, "noheading") == 0) {
530 } else if (strcmp(quote
, "headers") == 0) {
532 } else if (strcmp(quote
, "allheaders") == 0) {
534 action
= SEND_QUOTE_ALL
;
536 cp
= hfield("from", mp
);
538 mime_write(cp
, strlen(cp
),
539 collf
, CONV_FROMHDR
, TD_NONE
,
542 mime_write(cp
, strlen(cp
),
543 stdout
, CONV_FROMHDR
, TD_NONE
,
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')
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
)
566 if (!sigsetjmp(colljmp
, 1)) {
568 grabh(hp
, getfields
, 1);
569 if (quotefile
!= NULL
) {
570 if (include_file(NULL
, quotefile
, &lc
, &cc
, 1) != 0)
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.
582 fprintf(stderr
, catgets(catd
, CATSET
, 53,
583 "\n(Interrupt -- one more to kill letter)\n"));
585 printf(catgets(catd
, CATSET
, 54, "(continue)\n"));
589 if (value("interactive") == NULL
&& tildeflag
<= 0 && !is_a_tty
[0] &&
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
,
606 count
= readline(stdin
, &linebuf
, &linesize
);
609 if (value("interactive") != NULL
&&
610 value("ignoreeof") != NULL
&& ++eofcount
< 25) {
611 printf(catgets(catd
, CATSET
, 55,
612 "Use \".\" to terminate letter\n"));
617 if (tflag
&& count
== 0) {
619 if (makeheader(collf
, hp
) != OKAY
)
627 if (linebuf
[0] == '.' && linebuf
[1] == '\0' &&
628 value("interactive") != NULL
&&
629 (value("dot") != NULL
|| value("ignoreeof") != NULL
))
631 if (linebuf
[0] != escape
||
632 (value("interactive") == NULL
&&
635 if (putline(collf
, linebuf
, count
) < 0)
643 * On double escape, just send the single one.
644 * Otherwise, it's an error.
647 if (putline(collf
, &linebuf
[1], count
- 1) < 0)
652 printf(catgets(catd
, CATSET
, 56,
653 "Unknown tilde escape.\n"));
655 #ifdef DEBUG_COMMANDS
662 #endif /* DEBUG_COMMANDS */
665 * Shell escape, send the balance of the
673 * Escape to command mode, but be nice!
676 execute(&linebuf
[2], 1, count
- 2);
680 * Simulate end of file on input.
685 * Same as 'q', but no dead.letter saving.
693 * Force a quit of sending mail.
694 * Act like an interrupt happened.
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
);
712 * Grab extra headers.
715 grabh(hp
, GEXTRA
, 0);
716 while (check_from_and_sender(hp
->h_from
, hp
->h_sender
));
720 * Add to the To list.
722 while ((hp
->h_to
= checkaddrs(cat(hp
->h_to
,
723 sextract(&linebuf
[2], GTO
|GFULL
))))
728 * Set the Subject list.
731 while (whitechar(*cp
& 0377))
733 hp
->h_subject
= savestr(cp
);
737 * Edit the attachment list.
739 if (linebuf
[2] != '\0')
740 hp
->h_attach
= append_attachments(hp
->h_attach
,
743 hp
->h_attach
= edit_attachments(hp
->h_attach
);
747 * Add to the CC list.
749 hp
->h_cc
= checkaddrs(cat(hp
->h_cc
,
750 sextract(&linebuf
[2], GCC
|GFULL
)));
754 * Add stuff to blind carbon copies list.
756 hp
->h_bcc
= checkaddrs(cat(hp
->h_bcc
,
757 sextract(&linebuf
[2], GBCC
|GFULL
)));
760 strncpy(linebuf
+ 2, getdeadletter(), linesize
- 2);
761 linebuf
[linesize
-1]='\0';
767 * Search for the file name,
768 * then open it and copy the contents to collf.
771 while (whitechar(*cp
& 0377))
774 printf(catgets(catd
, CATSET
, 57,
775 "Interpolate what file?\n"));
779 insertcommand(collf
, cp
+ 1);
786 printf(catgets(catd
, CATSET
, 58,
787 "%s: Directory\n"), cp
);
790 if ((fbuf
= Fopen(cp
, "r")) == NULL
) {
794 printf(catgets(catd
, CATSET
, 59, "\"%s\" "), cp
);
796 if (include_file(fbuf
, cp
, &lc
, &cc
, 0) != 0)
798 printf(catgets(catd
, CATSET
, 60, "%d/%d\n"), lc
, cc
);
802 * Insert an environment variable into the file.
805 while (whitechar(*cp
& 0377))
807 if ((cp
= value(cp
)) == NULL
|| *cp
== '\0')
816 * Insert the contents of a signature variable.
818 if ((cp
= value(c
== 'a' ? "sign" : "Sign")) != NULL
&&
827 * Write the message on a file.
830 while (blankchar(*cp
& 0377))
833 fprintf(stderr
, catgets(catd
, CATSET
, 61,
834 "Write what file!?\n"));
837 if ((cp
= expand(cp
)) == NULL
)
840 exwrite(cp
, collf
, 1);
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)
857 * Print out the current state of the
858 * message without altering anything.
860 print_collf(collf
, hp
);
864 * Pipe message through command.
865 * Collect output as new message.
868 mespipe(&linebuf
[2]);
873 * Edit the current message.
874 * 'e' means to use EDITOR
875 * 'v' means to use VISUAL
878 mesedit(c
, value("editheaders") ? hp
: NULL
);
882 * Last the lengthy help string.
883 * (Very ugly, but take care for compiler supported
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");
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");
909 "~!command Invoke the shell\n"
910 "~:command Execute a regular command\n"
911 "-----------------------------------------------------------\n");
924 if ((cp
= value("SNAIL_TAIL")) != NULL
) {
935 sigaddset(&nset
, SIGINT
);
936 sigaddset(&nset
, SIGHUP
);
938 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
940 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
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
);
952 * Write a file, ex-like if f set.
955 exwrite(char *name
, FILE *fp
, int f
)
963 printf("\"%s\" ", name
);
966 if ((of
= Fopen(name
, "a")) == NULL
) {
972 while ((c
= getc(fp
)) != EOF
) {
984 printf(catgets(catd
, CATSET
, 65, "%d/%ld\n"), lc
, cc
);
990 makeheader(FILE *fp
, struct header
*hp
)
996 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
997 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1005 extract_header(fp
, hp
);
1006 while ((c
= getc(fp
)) != EOF
)
1012 if (check_from_and_sender(hp
->h_from
, hp
->h_sender
))
1018 * Edit the message being collected on fp.
1019 * On return, make the edit file the new temp file.
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
,
1033 fseek(nf
, 0L, SEEK_END
);
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.
1051 sighandler_type sigint
= safe_signal(SIGINT
, SIG_IGN
);
1055 if ((nf
= Ftemp(&tempEdit
, "Re", "w+", 0600, 1)) == NULL
) {
1056 perror(catgets(catd
, CATSET
, 66, "temporary mail edit file"));
1063 * stdin = current message.
1064 * stdout = new message.
1066 if ((shell
= value("SHELL")) == NULL
)
1068 if (run_command(shell
,
1069 0, fileno(collf
), fileno(nf
), "-c", cmd
, NULL
) < 0) {
1073 if (fsize(nf
) == 0) {
1074 fprintf(stderr
, catgets(catd
, CATSET
, 67,
1075 "No bytes from \"%s\" !?\n"), cmd
);
1082 fseek(nf
, 0L, SEEK_END
);
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.
1098 forward(char *ms
, FILE *fp
, int f
)
1101 struct ignoretab
*ig
;
1103 enum sendaction action
;
1106 msgvec
= (int *)salloc((msgCount
+1) * sizeof *msgvec
);
1109 if (getmsglist(ms
, msgvec
, 0) < 0)
1112 *msgvec
= first(0, MMNORM
);
1114 printf(catgets(catd
, CATSET
, 68,
1115 "No appropriate messages\n"));
1120 if (f
== 'f' || f
== 'F')
1122 else if ((tabst
= value("indentprefix")) == NULL
)
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;
1131 printf(" %d", *msgvec
);
1132 if (send(mp
, fp
, ig
, tabst
, action
, NULL
) < 0) {
1133 perror(catgets(catd
, CATSET
, 70,
1134 "temporary mail file"));
1143 * Print (continue) when continued after ^Z.
1149 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
1153 sigaddset(&nset
, s
);
1154 sigprocmask(SIG_UNBLOCK
, &nset
, (sigset_t
*)NULL
);
1156 sigprocmask(SIG_BLOCK
, &nset
, (sigset_t
*)NULL
);
1157 safe_signal(s
, old_action
);
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.
1174 * the control flow is subtle, because we can be called from ~q.
1177 if (value("ignore") != NULL
) {
1184 siglongjmp(colljmp
, 1);
1188 if (value("save") != NULL
&& s
!= 0)
1189 savedeadletter(collf
);
1190 siglongjmp(collabort
, 1);
1199 savedeadletter(collf
);
1201 * Let's pretend nobody else wants to clean up,
1202 * a true statement at this time.
1208 savedeadletter(FILE *fp
)
1211 int c
, lines
= 0, bytes
= 0;
1216 cp
= getdeadletter();
1218 dbuf
= Fopen(cp
, "a");
1222 printf("\"%s\" ", cp
);
1223 while ((c
= getc(fp
)) != EOF
) {
1230 printf("%d/%d\n", lines
, bytes
);
1235 putesc(const char *s
, FILE *stream
)
1254 putc(s
[0]&0377, stream
);