Only support sh(1)-style quoting for some new commands..
[s-mailx.git] / collect.c
blob974264130875e70b75c644f841c4728b7fd6ef7d
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Collect input from standard input, handling ~ escapes.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE collect
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 /* The following hookiness with global variables is so that on receipt of an
43 * interrupt signal, the partial message can be salted away on *DEAD* */
45 static sighandler_type _coll_saveint; /* Previous SIGINT value */
46 static sighandler_type _coll_savehup; /* Previous SIGHUP value */
47 static sighandler_type _coll_savetstp; /* Previous SIGTSTP value */
48 static sighandler_type _coll_savettou; /* Previous SIGTTOU value */
49 static sighandler_type _coll_savettin; /* Previous SIGTTIN value */
50 static FILE *_coll_fp; /* File for saving away */
51 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
52 static sigjmp_buf _coll_jmp; /* To get back to work */
53 static int _coll_jmp_p; /* whether to long jump */
54 static sigjmp_buf _coll_abort; /* To end collection with error */
55 static sigjmp_buf _coll_pipejmp; /* On broken pipe */
57 /* Handle `~:', `~_' and some hooks; hp may be NULL */
58 static void _execute_command(struct header *hp, char const *linebuf,
59 size_t linesize);
61 /* If *interactive* is set and *doecho* is, too, also dump to *stdout* */
62 static int _include_file(char const *name, int *linecount,
63 int *charcount, bool_t doecho, bool_t indent);
65 static void _collect_onpipe(int signo);
67 /* Execute cmd and insert its standard output into fp */
68 static void insertcommand(FILE *fp, char const *cmd);
70 /* ~p command */
71 static void print_collf(FILE *collf, struct header *hp);
73 /* Write a file, ex-like if f set */
74 static int exwrite(char const *name, FILE *fp, int f);
76 /* Parse off the message header from fp and store relevant fields in hp,
77 * replace _coll_fp with a shiny new version without any header */
78 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
80 /* Edit the message being collected on fp. On return, make the edit file the
81 * new temp file */
82 static void mesedit(int c, struct header *hp);
84 /* Pipe the message through the command. Old message is on stdin of command,
85 * new message collected from stdout. Shell must return 0 to accept new msg */
86 static void mespipe(char *cmd);
88 /* Interpolate the named messages into the current message, possibly doing
89 * indent stuff. The flag argument is one of the tilde escapes: [mMfFuU].
90 * Return a count of the number of characters now in the message, or -1 if an
91 * error is encountered writing the message temporary */
92 static int forward(char *ms, FILE *fp, int f);
94 /* Print (continue) when continued after ^Z */
95 static void collstop(int s);
97 /* On interrupt, come here to save the partial message in ~/dead.letter.
98 * Then jump out of the collection loop */
99 static void _collint(int s);
101 static void collhup(int s);
103 static int putesc(char const *s, FILE *stream);
105 /* call_compose_mode_hook() setter hook */
106 static void a_coll__hook_setter(void *arg);
108 static void
109 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
110 /* The problem arises if there are rfc822 message attachments and the
111 * user uses `~:' to change the current file. TODO Unfortunately we
112 * TODO cannot simply keep a pointer to, or increment a reference count
113 * TODO of the current `file' (mailbox that is) object, because the
114 * TODO codebase doesn't deal with that at all; so, until some far
115 * TODO later time, copy the name of the path, and warn the user if it
116 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
117 * TODO copy the message attachments over to temporary files, but that
118 * TODO would require more changes so that the user still can recognize
119 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
120 struct n_sigman sm;
121 struct attachment *ap;
122 char *mnbuf;
123 NYD_ENTER;
125 UNUSED(linesize);
126 mnbuf = NULL;
128 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
129 case 0:
130 break;
131 default:
132 goto jleave;
135 /* If the above todo is worked, remove or outsource to attachments.c! */
136 if(hp != NULL && (ap = hp->h_attach) != NULL) do
137 if(ap->a_msgno){
138 mnbuf = sstrdup(mailname);
139 break;
141 while((ap = ap->a_flink) != NULL);
143 n_source_command(linebuf);
145 n_sigman_cleanup_ping(&sm);
146 jleave:
147 if(mnbuf != NULL){
148 if(strcmp(mnbuf, mailname))
149 n_err(_("Mailbox changed: it is likely that existing "
150 "rfc822 attachments became invalid!\n"));
151 free(mnbuf);
153 NYD_LEAVE;
154 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
157 static int
158 _include_file(char const *name, int *linecount, int *charcount,
159 bool_t doecho, bool_t indent)
161 FILE *fbuf;
162 char const *indb;
163 int ret = -1;
164 char *linebuf = NULL; /* TODO line pool */
165 size_t linesize = 0, indl, linelen, cnt;
166 NYD_ENTER;
168 if (name == (char*)-1)
169 fbuf = stdin;
170 else if ((fbuf = Fopen(name, "r")) == NULL) {
171 n_perr(name, 0);
172 goto jleave;
175 if (!indent)
176 indb = NULL, indl = 0;
177 else {
178 if ((indb = ok_vlook(indentprefix)) == NULL)
179 indb = INDENT_DEFAULT;
180 indl = strlen(indb);
183 *linecount = *charcount = 0;
184 cnt = fsize(fbuf);
185 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
186 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
187 goto jleave;
188 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
189 goto jleave;
190 ++(*linecount);
191 (*charcount) += linelen + indl;
192 if ((options & OPT_INTERACTIVE) && doecho) {
193 if (indl > 0)
194 fwrite(indb, sizeof *indb, indl, stdout);
195 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
198 if (fflush(_coll_fp))
199 goto jleave;
200 if ((options & OPT_INTERACTIVE) && doecho)
201 fflush(stdout);
203 ret = 0;
204 jleave:
205 if (linebuf != NULL)
206 free(linebuf);
207 if (fbuf != NULL && fbuf != stdin)
208 Fclose(fbuf);
209 NYD_LEAVE;
210 return ret;
213 static void
214 _collect_onpipe(int signo)
216 NYD_X; /* Signal handler */
217 UNUSED(signo);
218 siglongjmp(_coll_pipejmp, 1);
221 static void
222 insertcommand(FILE *fp, char const *cmd)
224 FILE *ibuf = NULL;
225 int c;
226 NYD_ENTER;
228 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
229 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
230 putc(c, fp);
231 Pclose(ibuf, TRU1);
232 } else
233 n_perr(cmd, 0);
234 NYD_LEAVE;
237 static void
238 print_collf(FILE *cf, struct header *hp)
240 char *lbuf = NULL; /* TODO line pool */
241 sighandler_type sigint;
242 FILE * volatile obuf = stdout;
243 struct attachment *ap;
244 char const *cp;
245 enum gfield gf;
246 size_t linesize = 0, linelen, cnt, cnt2;
247 NYD_ENTER;
249 fflush_rewind(cf);
250 cnt = cnt2 = fsize(cf);
252 sigint = safe_signal(SIGINT, SIG_IGN);
254 if ((options & OPT_INTERACTIVE) && (cp = ok_vlook(crt)) != NULL) {
255 size_t l, m;
257 m = 4;
258 if (hp->h_to != NULL)
259 ++m;
260 if (hp->h_subject != NULL)
261 ++m;
262 if (hp->h_cc != NULL)
263 ++m;
264 if (hp->h_bcc != NULL)
265 ++m;
266 if (hp->h_attach != NULL)
267 ++m;
268 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
269 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
270 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
272 l = (*cp == '\0') ? (size_t)screensize() : strtoul(cp, NULL, 0);
273 if (m > l)
274 goto jpager;
275 l -= m;
277 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
279 rewind(cf);
280 if (l < m) {
281 jpager:
282 if (sigsetjmp(_coll_pipejmp, 1))
283 goto jendpipe;
284 if ((obuf = n_pager_open()) == NULL)
285 obuf = stdout;
286 else
287 safe_signal(SIGPIPE, &_collect_onpipe);
291 fprintf(obuf, _("-------\nMessage contains:\n"));
292 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
293 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
294 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
295 prout(lbuf, linelen, obuf);
296 if (hp->h_attach != NULL) {
297 fputs(_("-------\nAttachments:\n"), obuf);
298 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
299 if (ap->a_msgno)
300 fprintf(obuf, " - message %u\n", ap->a_msgno);
301 else {
302 /* TODO after MIME/send layer rewrite we *know*
303 * TODO the details of the attachment here,
304 * TODO so adjust this again, then */
305 char const *cs, *csi = "-> ";
307 if ((cs = ap->a_charset) == NULL &&
308 (csi = "<- ", cs = ap->a_input_charset) == NULL)
309 cs = charset_get_lc();
310 if ((cp = ap->a_content_type) == NULL)
311 cp = "?";
312 else if (ascncasecmp(cp, "text/", 5))
313 csi = "";
314 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs, ap->a_name);
319 jendpipe:
320 if (obuf != stdout)
321 n_pager_close(obuf);
322 if (lbuf != NULL)
323 free(lbuf);
324 safe_signal(SIGINT, sigint);
325 NYD_LEAVE;
328 static int
329 exwrite(char const *name, FILE *fp, int f)
331 FILE *of;
332 int c, lc, rv = -1;
333 long cc;
334 NYD_ENTER;
336 if (f) {
337 printf("\"%s\" ", name);
338 fflush(stdout);
340 if ((of = Fopen(name, "a")) == NULL) {
341 n_perr(NULL, 0);
342 goto jleave;
345 lc = 0;
346 cc = 0;
347 while ((c = getc(fp)) != EOF) {
348 ++cc;
349 if (c == '\n')
350 ++lc;
351 putc(c, of);
352 if (ferror(of)) {
353 n_perr(name, 0);
354 Fclose(of);
355 goto jleave;
358 Fclose(of);
359 printf(_("%d/%ld\n"), lc, cc);
360 fflush(stdout);
361 rv = 0;
362 jleave:
363 NYD_LEAVE;
364 return rv;
367 static enum okay
368 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
370 FILE *nf;
371 int c;
372 enum okay rv = STOP;
373 NYD_ENTER;
375 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
376 n_perr(_("temporary mail edit file"), 0);
377 goto jleave;
380 extract_header(fp, hp, checkaddr_err);
382 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
383 putc(c, nf);
384 if (fp != _coll_fp)
385 Fclose(_coll_fp);
386 Fclose(fp);
387 _coll_fp = nf;
388 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
389 goto jleave;
390 rv = OKAY;
391 jleave:
392 NYD_LEAVE;
393 return rv;
396 static void
397 mesedit(int c, struct header *hp)
399 bool_t saved;
400 sighandler_type sigint;
401 FILE *nf;
402 NYD_ENTER;
404 saved = ok_blook(add_file_recipients);
405 ok_bset(add_file_recipients, TRU1);
407 sigint = safe_signal(SIGINT, SIG_IGN);
408 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
409 if (nf != NULL) {
410 if (hp) {
411 rewind(nf);
412 makeheader(nf, hp, NULL);
413 } else {
414 fseek(nf, 0L, SEEK_END);
415 Fclose(_coll_fp);
416 _coll_fp = nf;
419 safe_signal(SIGINT, sigint);
421 ok_bset(add_file_recipients, saved);
422 NYD_LEAVE;
425 static void
426 mespipe(char *cmd)
428 FILE *nf;
429 sighandler_type sigint;
430 NYD_ENTER;
432 sigint = safe_signal(SIGINT, SIG_IGN);
434 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
435 n_perr(_("temporary mail edit file"), 0);
436 goto jout;
439 /* stdin = current message. stdout = new message */
440 fflush(_coll_fp);
441 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
442 cmd, NULL, NULL) < 0) {
443 Fclose(nf);
444 goto jout;
447 if (fsize(nf) == 0) {
448 n_err(_("No bytes from \"%s\" !?\n"), cmd);
449 Fclose(nf);
450 goto jout;
453 /* Take new files */
454 fseek(nf, 0L, SEEK_END);
455 Fclose(_coll_fp);
456 _coll_fp = nf;
457 jout:
458 safe_signal(SIGINT, sigint);
459 NYD_LEAVE;
462 static int
463 forward(char *ms, FILE *fp, int f)
465 int *msgvec, rv = 0;
466 struct ignoretab *ig;
467 char const *tabst;
468 enum sendaction action;
469 NYD_ENTER;
471 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
472 if (getmsglist(ms, msgvec, 0) < 0)
473 goto jleave;
474 if (*msgvec == 0) {
475 *msgvec = first(0, MMNORM);
476 if (*msgvec == 0) {
477 n_err(_("No appropriate messages\n"));
478 goto jleave;
480 msgvec[1] = 0;
483 if (f == 'f' || f == 'F' || f == 'u')
484 tabst = NULL;
485 else if ((tabst = ok_vlook(indentprefix)) == NULL)
486 tabst = INDENT_DEFAULT;
487 if (f == 'u' || f == 'U')
488 ig = allignore;
489 else
490 ig = upperchar(f) ? NULL : ignore;
491 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
493 printf(_("Interpolating:"));
494 for (; *msgvec != 0; ++msgvec) {
495 struct message *mp = message + *msgvec - 1;
497 touch(mp);
498 printf(" %d", *msgvec);
499 fflush(stdout);
500 if (sendmp(mp, fp, ig, tabst, action, NULL) < 0) {
501 n_perr(_("temporary mail file"), 0);
502 rv = -1;
503 break;
506 printf("\n");
507 jleave:
508 NYD_LEAVE;
509 return rv;
512 static void
513 collstop(int s)
515 sighandler_type old_action;
516 sigset_t nset;
517 NYD_X; /* Signal handler */
519 old_action = safe_signal(s, SIG_DFL);
521 sigemptyset(&nset);
522 sigaddset(&nset, s);
523 sigprocmask(SIG_UNBLOCK, &nset, NULL);
524 n_raise(s);
525 sigprocmask(SIG_BLOCK, &nset, NULL);
527 safe_signal(s, old_action);
528 if (_coll_jmp_p) {
529 _coll_jmp_p = 0;
530 _coll_hadintr = 0;
531 siglongjmp(_coll_jmp, 1);
535 static void
536 _collint(int s)
538 NYD_X; /* Signal handler */
540 /* the control flow is subtle, because we can be called from ~q */
541 if (_coll_hadintr == 0) {
542 if (ok_blook(ignore)) {
543 puts("@");
544 fflush(stdout);
545 clearerr(stdin);
546 } else
547 _coll_hadintr = 1;
548 siglongjmp(_coll_jmp, 1);
550 exit_status |= EXIT_SEND_ERROR;
551 if (s != 0)
552 savedeadletter(_coll_fp, 1);
553 /* Aborting message, no need to fflush() .. */
554 siglongjmp(_coll_abort, 1);
557 static void
558 collhup(int s)
560 NYD_X; /* Signal handler */
561 UNUSED(s);
563 savedeadletter(_coll_fp, 1);
564 /* Let's pretend nobody else wants to clean up, a true statement at
565 * this time */
566 exit(EXIT_ERR);
569 static int
570 putesc(char const *s, FILE *stream)
572 int n = 0, rv = -1;
573 NYD_ENTER;
575 while (s[0] != '\0') {
576 if (s[0] == '\\') {
577 if (s[1] == 't') {
578 if (putc('\t', stream) == EOF)
579 goto jleave;
580 ++n;
581 s += 2;
582 continue;
584 if (s[1] == 'n') {
585 if (putc('\n', stream) == EOF)
586 goto jleave;
587 ++n;
588 s += 2;
589 continue;
592 if (putc(s[0], stream) == EOF)
593 goto jleave;
594 ++n;
595 ++s;
597 if (putc('\n', stream) == EOF)
598 goto jleave;
599 rv = ++n;
600 jleave:
601 NYD_LEAVE;
602 return rv;
605 static void
606 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
607 struct header *hp;
608 char const *val;
609 NYD2_ENTER;
611 hp = arg;
613 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
614 val = "";
615 ok_vset(compose_from, val);
616 if((val = detract(hp->h_sender, 0)) == NULL)
617 val = "";
618 ok_vset(compose_sender, val);
619 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
620 val = "";
621 ok_vset(compose_to, val);
622 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
623 val = "";
624 ok_vset(compose_cc, val);
625 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
626 val = "";
627 ok_vset(compose_bcc, val);
628 if((val = hp->h_subject) == NULL)
629 val = "";
630 ok_vset(compose_subject, val);
631 NYD2_LEAVE;
634 FL FILE *
635 collect(struct header *hp, int printheaders, struct message *mp,
636 char *quotefile, int doprefix, si8_t *checkaddr_err)
638 struct ignoretab *quoteig;
639 int lc, cc, c, t;
640 int volatile escape, getfields;
641 char *linebuf, *quote;
642 char const *cp;
643 size_t i, linesize; /* TODO line pool */
644 long cnt;
645 enum sendaction action;
646 sigset_t oset, nset;
647 FILE * volatile sigfp;
648 NYD_ENTER;
650 _coll_fp = NULL;
651 sigfp = NULL;
652 linesize = 0;
653 linebuf = quote = NULL;
655 /* Start catching signals from here, but we're still die on interrupts
656 * until we're in the main loop */
657 sigfillset(&nset);
658 sigprocmask(SIG_BLOCK, &nset, &oset);
659 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
660 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
661 safe_signal(SIGINT, &_collint);
662 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
663 safe_signal(SIGHUP, collhup);
664 /* TODO We do a lot of redundant signal handling, especially
665 * TODO with the command line editor(s); try to merge this */
666 _coll_savetstp = safe_signal(SIGTSTP, collstop);
667 _coll_savettou = safe_signal(SIGTTOU, collstop);
668 _coll_savettin = safe_signal(SIGTTIN, collstop);
669 if (sigsetjmp(_coll_abort, 1))
670 goto jerr;
671 if (sigsetjmp(_coll_jmp, 1))
672 goto jerr;
673 pstate |= PS_RECURSED;
674 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
676 ++noreset;
677 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
678 NULL) {
679 n_perr(_("temporary mail file"), 0);
680 goto jerr;
683 /* If we are going to prompt for a subject, refrain from printing a newline
684 * after the headers (since some people mind) */
685 getfields = 0;
686 if (!(options & OPT_t_FLAG)) {
687 t = GTO | GSUBJECT | GCC | GNL;
688 if (ok_blook(fullnames))
689 t |= GCOMMA;
691 if (options & OPT_INTERACTIVE) {
692 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
693 t &= ~GNL, getfields |= GSUBJECT;
695 if (hp->h_to == NULL)
696 t &= ~GNL, getfields |= GTO;
698 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
699 if (hp->h_bcc == NULL && ok_blook(askbcc))
700 t &= ~GNL, getfields |= GBCC;
701 if (hp->h_cc == NULL && ok_blook(askcc))
702 t &= ~GNL, getfields |= GCC;
707 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
708 _coll_hadintr = 0;
710 if (!sigsetjmp(_coll_jmp, 1)) {
711 /* Ask for some headers first, as necessary */
712 if (getfields)
713 grab_headers(hp, getfields, 1);
715 /* Execute compose-enter TODO completely v15-compat intermediate!! */
716 if((cp = ok_vlook(on_compose_enter)) != NULL){
717 setup_from_and_sender(hp);
718 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
721 /* C99 */{
722 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
723 if(cp_obsolete != NULL)
724 OBSOLETE(_("please use *message-inject-head* "
725 "instead of *NAIL_HEAD*"));
727 if(((cp = ok_vlook(message_inject_head)) != NULL ||
728 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
729 goto jerr;
732 /* Quote an original message */
733 if (mp != NULL && (doprefix || (quote = ok_vlook(quote)) != NULL)) {
734 quoteig = allignore;
735 action = SEND_QUOTE;
736 if (doprefix) {
737 quoteig = fwdignore;
738 if ((cp = ok_vlook(fwdheading)) == NULL)
739 cp = "-------- Original Message --------";
740 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
741 goto jerr;
742 } else if (!strcmp(quote, "noheading")) {
743 /*EMPTY*/;
744 } else if (!strcmp(quote, "headers")) {
745 quoteig = ignore;
746 } else if (!strcmp(quote, "allheaders")) {
747 quoteig = NULL;
748 action = SEND_QUOTE_ALL;
749 } else {
750 cp = hfield1("from", mp);
751 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
752 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
753 goto jerr;
754 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
755 goto jerr;
758 if (fflush(_coll_fp))
759 goto jerr;
760 if (doprefix)
761 cp = NULL;
762 else if ((cp = ok_vlook(indentprefix)) == NULL)
763 cp = INDENT_DEFAULT;
764 if (sendmp(mp, _coll_fp, quoteig, cp, action, NULL) < 0)
765 goto jerr;
768 if (quotefile != NULL) {
769 if (_include_file(quotefile, &lc, &cc, TRU1, FAL0) != 0)
770 goto jerr;
773 if (options & OPT_INTERACTIVE) {
774 /* Print what we have sofar also on the terminal (if useful) */
775 if (!ok_blook(editalong)) {
776 if (printheaders)
777 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
779 rewind(_coll_fp);
780 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
781 putc(c, stdout);
782 if (fseek(_coll_fp, 0, SEEK_END))
783 goto jerr;
785 /* Ensure this is clean xxx not really necessary? */
786 fflush(stdout);
787 } else {
788 rewind(_coll_fp);
789 mesedit('e', hp);
790 goto jcont;
793 } else {
794 /* Come here for printing the after-signal message. Duplicate messages
795 * won't be printed because the write is aborted if we get a SIGTTOU */
796 if (_coll_hadintr) {
797 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
798 } else {
799 jcont:
800 printf(_("(continue)\n"));
801 fflush(stdout);
805 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
806 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))) {
807 linebuf = srealloc(linebuf, linesize = LINESIZE);
808 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
809 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
810 goto jerr;
812 goto jout;
815 /* The interactive collect loop.
816 * All commands which come here are forbidden when sourcing! */
817 assert(_coll_hadintr || !(pstate & PS_SOURCING));
818 for (;;) {
819 _coll_jmp_p = 1;
820 cnt = n_lex_input("", FAL0, &linebuf, &linesize, NULL);
821 _coll_jmp_p = 0;
823 if (cnt < 0) {
824 assert(!(pstate & PS_SOURCING));
825 if (options & OPT_t_FLAG) {
826 fflush_rewind(_coll_fp);
827 /* It is important to set PS_t_FLAG before extract_header() *and*
828 * keep OPT_t_FLAG for the first parse of the message, too! */
829 pstate |= PS_t_FLAG;
830 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
831 goto jerr;
832 options &= ~OPT_t_FLAG;
833 continue;
834 } else if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
835 printf(_("*ignoreeof* set, use \"~.\" to terminate letter\n"));
836 continue;
838 break;
841 _coll_hadintr = 0;
843 if (cnt == 0 || !(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG))) {
844 jputline:
845 /* TODO calls putline(), which *always* appends LF;
846 * TODO thus, STDIN with -t will ALWAYS end with LF,
847 * TODO even if no trailing LF and QP encoding.
848 * TODO when finally changed, update cc-test.sh */
849 if (putline(_coll_fp, linebuf, cnt) < 0)
850 goto jerr;
851 continue;
852 } else if (linebuf[0] == '.') {
853 if (linebuf[1] == '\0' && (ok_blook(dot) || ok_blook(ignoreeof)))
854 break;
856 if (linebuf[0] != escape)
857 goto jputline;
859 if (!(options & OPT_t_FLAG))
860 n_tty_addhist(linebuf, TRU1);
862 c = linebuf[1];
863 switch (c) {
864 default:
865 /* On double escape, send a single one. Otherwise, it's an error */
866 if (c == escape) {
867 if (putline(_coll_fp, linebuf + 1, cnt - 1) < 0)
868 goto jerr;
869 else
870 break;
872 n_err(_("Unknown tilde escape: ~%c\n"), asciichar(c) ? c : '?');
873 break;
874 case '!':
875 /* Shell escape, send the balance of line to sh -c */
876 c_shell(linebuf + 2);
877 break;
878 case ':':
879 /* FALLTHRU */
880 case '_':
881 /* Escape to command mode, but be nice! */
882 _execute_command(hp, linebuf + 2, cnt - 2);
883 goto jcont;
884 case '.':
885 /* Simulate end of file on input */
886 goto jout;
887 case 'x':
888 /* Same as 'q', but no *DEAD* saving */
889 /* FALLTHRU */
890 case 'q':
891 /* Force a quit, act like an interrupt had happened */
892 ++_coll_hadintr;
893 _collint((c == 'x') ? 0 : SIGINT);
894 exit(EXIT_ERR);
895 /*NOTREACHED*/
896 case 'h':
897 /* Grab a bunch of headers */
899 grab_headers(hp, GTO | GSUBJECT | GCC | GBCC,
900 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
901 while (hp->h_to == NULL);
902 goto jcont;
903 case 'H':
904 /* Grab extra headers */
906 grab_headers(hp, GEXTRA, 0);
907 while (check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
908 goto jcont;
909 case 't':
910 /* Add to the To list */
911 hp->h_to = cat(hp->h_to,
912 checkaddrs(lextract(linebuf + 2, GTO | GFULL), EACM_NORMAL,
913 NULL));
914 break;
915 case 's':
916 /* Set the Subject list */
917 cp = linebuf + 2;
918 while (whitechar(*cp))
919 ++cp;
920 hp->h_subject = savestr(cp);
921 break;
922 #ifdef HAVE_MEMORY_DEBUG
923 case 'S':
924 c_sstats(NULL);
925 break;
926 #endif
927 case '@':
928 /* Edit the attachment list */
929 if (linebuf[2] != '\0')
930 append_attachments(&hp->h_attach, linebuf + 2);
931 else
932 edit_attachments(&hp->h_attach);
933 break;
934 case 'c':
935 /* Add to the CC list */
936 hp->h_cc = cat(hp->h_cc,
937 checkaddrs(lextract(linebuf + 2, GCC | GFULL), EACM_NORMAL,
938 NULL));
939 break;
940 case 'b':
941 /* Add stuff to blind carbon copies list */
942 hp->h_bcc = cat(hp->h_bcc,
943 checkaddrs(lextract(linebuf + 2, GBCC | GFULL), EACM_NORMAL,
944 NULL));
945 break;
946 case 'd':
947 strncpy(linebuf + 2, getdeadletter(), linesize - 2);
948 linebuf[linesize - 1] = '\0';
949 /*FALLTHRU*/
950 case 'R':
951 case 'r':
952 case '<':
953 /* Invoke a file: Search for the file name, then open it and copy the
954 * contents to _coll_fp */
955 cp = linebuf + 2;
956 while (whitechar(*cp))
957 ++cp;
958 if (*cp == '\0') {
959 n_err(_("Interpolate what file?\n"));
960 break;
962 if (*cp == '!') {
963 insertcommand(_coll_fp, cp + 1);
964 break;
966 if ((cp = file_expand(cp)) == NULL)
967 break;
968 if (is_dir(cp)) {
969 n_err(_("\"%s\": Directory\n"), cp);
970 break;
972 printf(_("\"%s\" "), cp);
973 fflush(stdout);
974 if (_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0)
975 goto jerr;
976 printf(_("%d/%d\n"), lc, cc);
977 break;
978 case 'i':
979 /* Insert a variable into the file */
980 cp = linebuf + 2;
981 while (whitechar(*cp))
982 ++cp;
983 if ((cp = vok_vlook(cp)) == NULL || *cp == '\0')
984 break;
985 if (putesc(cp, _coll_fp) < 0)
986 goto jerr;
987 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
988 goto jerr;
989 break;
990 case 'a':
991 case 'A':
992 /* Insert the contents of a signature variable */
993 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
994 if (cp != NULL && *cp != '\0') {
995 if (putesc(cp, _coll_fp) < 0)
996 goto jerr;
997 if ((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
998 goto jerr;
1000 break;
1001 case 'w':
1002 /* Write the message on a file */
1003 cp = linebuf + 2;
1004 while (blankchar(*cp))
1005 ++cp;
1006 if (*cp == '\0' || (cp = file_expand(cp)) == NULL) {
1007 n_err(_("Write what file!?\n"));
1008 break;
1010 rewind(_coll_fp);
1011 if (exwrite(cp, _coll_fp, 1) < 0)
1012 goto jerr;
1013 break;
1014 case 'm':
1015 case 'M':
1016 case 'f':
1017 case 'F':
1018 case 'u':
1019 case 'U':
1020 /* Interpolate the named messages, if we are in receiving mail mode.
1021 * Does the standard list processing garbage. If ~f is given, we
1022 * don't shift over */
1023 if (forward(linebuf + 2, _coll_fp, c) < 0)
1024 goto jerr;
1025 goto jcont;
1026 case 'p':
1027 /* Print current state of the message without altering anything */
1028 print_collf(_coll_fp, hp);
1029 goto jcont;
1030 case '|':
1031 /* Pipe message through command. Collect output as new message */
1032 rewind(_coll_fp);
1033 mespipe(linebuf + 2);
1034 goto jcont;
1035 case 'v':
1036 case 'e':
1037 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1038 rewind(_coll_fp);
1039 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1040 goto jcont;
1041 case '?':
1042 /* Last the lengthy help string. (Very ugly, but take care for
1043 * compiler supported string lengths :() */
1044 puts(_(
1045 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1046 "~. Commit and send message\n"
1047 "~: <command> Execute a mail command\n"
1048 "~<! <command> Insert output of command\n"
1049 "~@ [<files>] Edit attachment list\n"
1050 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1051 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1052 "~d Read in *DEAD* (dead.letter)\n"
1053 "~e Edit message via *EDITOR*"
1055 puts(_(
1056 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1057 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1058 "~H Edit From:, Reply-To: and Sender:\n"
1059 "~h Prompt for Subject:, To:, Cc: and \"blind\" Bcc:\n"
1060 "~i <variable> Insert a value and a newline\n"
1061 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1062 "~p Print current message compose buffer\n"
1063 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1065 puts(_(
1066 "~s <subject> Set Subject:\n"
1067 "~t <users> Add users to To: list\n"
1068 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1069 "~v Edit message via *VISUAL*\n"
1070 "~w <file> Write message onto file\n"
1071 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1072 "~| <command> Pipe message through shell filter"
1074 break;
1078 jout:
1079 /* Execute compose-leave TODO completely v15-compat intermediate!! */
1080 if((cp = ok_vlook(on_compose_leave)) != NULL){
1081 setup_from_and_sender(hp);
1082 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1085 /* Final change to edit headers, if not already above */
1086 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1087 if (hp->h_cc == NULL && ok_blook(askcc))
1088 grab_headers(hp, GCC, 1);
1089 if (hp->h_bcc == NULL && ok_blook(askbcc))
1090 grab_headers(hp, GBCC, 1);
1092 if (hp->h_attach == NULL && ok_blook(askattach))
1093 edit_attachments(&hp->h_attach);
1095 /* Add automatic receivers */
1096 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1097 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1098 EACM_NORMAL, checkaddr_err));
1099 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1100 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1101 EACM_NORMAL, checkaddr_err));
1102 if (*checkaddr_err != 0)
1103 goto jerr;
1105 /* Place signature? */
1106 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1107 if((quote = file_expand(cp)) == NULL){
1108 n_err(_("*signature* expands to invalid file: \"%s\"\n"), cp);
1109 goto jerr;
1111 if((sigfp = Fopen(cp = quote, "r")) == NULL){
1112 n_err(_("Can't open *signature* \"%s\": %s\n"), cp, strerror(errno));
1113 goto jerr;
1116 if(linebuf == NULL)
1117 linebuf = smalloc(linesize = LINESIZE);
1118 c = '\0';
1119 while((i = fread(linebuf, sizeof *linebuf, linesize, UNVOLATILE(sigfp)))
1120 > 0){
1121 c = linebuf[i - 1];
1122 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1123 goto jerr;
1126 /* C99 */{
1127 FILE *x = UNVOLATILE(sigfp);
1128 int e = errno, ise = ferror(x);
1130 sigfp = NULL;
1131 Fclose(x);
1133 if(ise){
1134 n_err(_("Errors while reading *signature* \"%s\": %s\n"),
1135 cp, strerror(e));
1136 goto jerr;
1140 if(c != '\0' && c != '\n')
1141 putc('\n', _coll_fp);
1144 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1146 if(cp_obsolete != NULL)
1147 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1149 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1150 (cp = cp_obsolete) != NULL){
1151 if(putesc(cp, _coll_fp) < 0)
1152 goto jerr;
1153 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1154 goto jerr;
1158 if(fflush(_coll_fp))
1159 goto jerr;
1160 rewind(_coll_fp);
1162 jleave:
1163 if (linebuf != NULL)
1164 free(linebuf);
1165 --noreset;
1166 sigfillset(&nset);
1167 sigprocmask(SIG_BLOCK, &nset, NULL);
1168 pstate &= ~PS_RECURSED;
1169 safe_signal(SIGINT, _coll_saveint);
1170 safe_signal(SIGHUP, _coll_savehup);
1171 safe_signal(SIGTSTP, _coll_savetstp);
1172 safe_signal(SIGTTOU, _coll_savettou);
1173 safe_signal(SIGTTIN, _coll_savettin);
1174 sigprocmask(SIG_SETMASK, &oset, NULL);
1175 NYD_LEAVE;
1176 return _coll_fp;
1178 jerr:
1179 if(sigfp != NULL)
1180 Fclose(UNVOLATILE(sigfp));
1181 if (_coll_fp != NULL) {
1182 Fclose(_coll_fp);
1183 _coll_fp = NULL;
1185 goto jleave;
1188 FL void
1189 savedeadletter(FILE *fp, int fflush_rewind_first)
1191 char const *cp;
1192 int c;
1193 FILE *dbuf;
1194 ul_i lines, bytes;
1195 NYD_ENTER;
1197 if ((options & OPT_DEBUG) || !ok_blook(save))
1198 goto jleave;
1200 if (fflush_rewind_first) {
1201 fflush(fp);
1202 rewind(fp);
1204 if (fsize(fp) == 0)
1205 goto jleave;
1207 cp = getdeadletter();
1208 c = umask(077);
1209 dbuf = Fopen(cp, "a");
1210 umask(c);
1211 if (dbuf == NULL)
1212 goto jleave;
1214 really_rewind(fp);
1216 printf("\"%s\" ", cp);
1217 for (lines = bytes = 0; (c = getc(fp)) != EOF; ++bytes) {
1218 putc(c, dbuf);
1219 if (c == '\n')
1220 ++lines;
1222 printf("%lu/%lu\n", lines, bytes);
1224 Fclose(dbuf);
1225 rewind(fp);
1226 jleave:
1227 NYD_LEAVE;
1230 /* s-it-mode */