-s, ~s: normalize NL/CR characters (Debian #419840)
[s-mailx.git] / collect.c
blobca88f997c2533bd76848868fec25985a1cd8beda
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 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 FILE *_coll_fp; /* File for saving away */
48 static int volatile _coll_hadintr; /* Have seen one SIGINT so far */
49 static sigjmp_buf _coll_jmp; /* To get back to work */
50 static sigjmp_buf _coll_abort; /* To end collection with error */
51 static sigjmp_buf _coll_pipejmp; /* On broken pipe */
53 /* Handle `~:', `~_' and some hooks; hp may be NULL */
54 static void _execute_command(struct header *hp, char const *linebuf,
55 size_t linesize);
57 /* If *interactive* is set and *doecho* is, too, also dump to *stdout* */
58 static int _include_file(char const *name, int *linecount,
59 int *charcount, bool_t doecho, bool_t indent);
61 static void _collect_onpipe(int signo);
63 /* Execute cmd and insert its standard output into fp */
64 static void insertcommand(FILE *fp, char const *cmd);
66 /* ~p command */
67 static void print_collf(FILE *collf, struct header *hp);
69 /* Write a file, ex-like if f set */
70 static int exwrite(char const *name, FILE *fp, int f);
72 /* Parse off the message header from fp and store relevant fields in hp,
73 * replace _coll_fp with a shiny new version without any header */
74 static enum okay makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err);
76 /* Edit the message being collected on fp. On return, make the edit file the
77 * new temp file */
78 static void mesedit(int c, struct header *hp);
80 /* Pipe the message through the command. Old message is on stdin of command,
81 * new message collected from stdout. Shell must return 0 to accept new msg */
82 static void mespipe(char *cmd);
84 /* Interpolate the named messages into the current message, possibly doing
85 * indent stuff. The flag argument is one of the tilde escapes: [mMfFuU].
86 * Return a count of the number of characters now in the message, or -1 if an
87 * error is encountered writing the message temporary */
88 static int forward(char *ms, FILE *fp, int f);
90 /* On interrupt, come here to save the partial message in ~/dead.letter.
91 * Then jump out of the collection loop */
92 static void _collint(int s);
94 static void collhup(int s);
96 static int putesc(char const *s, FILE *stream); /* TODO wysh set! */
98 /* call_compose_mode_hook() setter hook */
99 static void a_coll__hook_setter(void *arg);
101 static void
102 _execute_command(struct header *hp, char const *linebuf, size_t linesize){
103 /* The problem arises if there are rfc822 message attachments and the
104 * user uses `~:' to change the current file. TODO Unfortunately we
105 * TODO cannot simply keep a pointer to, or increment a reference count
106 * TODO of the current `file' (mailbox that is) object, because the
107 * TODO codebase doesn't deal with that at all; so, until some far
108 * TODO later time, copy the name of the path, and warn the user if it
109 * TODO changed; we COULD use the AC_TMPFILE attachment type, i.e.,
110 * TODO copy the message attachments over to temporary files, but that
111 * TODO would require more changes so that the user still can recognize
112 * TODO in `~@' etc. that its a rfc822 message attachment; see below */
113 struct n_sigman sm;
114 struct attachment *ap;
115 char * volatile mnbuf;
116 NYD_ENTER;
118 n_UNUSED(linesize);
119 mnbuf = NULL;
121 n_SIGMAN_ENTER_SWITCH(&sm, n_SIGMAN_ALL){
122 case 0:
123 break;
124 default:
125 goto jleave;
128 /* If the above todo is worked, remove or outsource to attachments.c! */
129 if(hp != NULL && (ap = hp->h_attach) != NULL) do
130 if(ap->a_msgno){
131 mnbuf = sstrdup(mailname);
132 break;
134 while((ap = ap->a_flink) != NULL);
136 n_source_command(n_LEXINPUT_CTX_COMPOSE, linebuf);
138 n_sigman_cleanup_ping(&sm);
139 jleave:
140 if(mnbuf != NULL){
141 if(strcmp(mnbuf, mailname))
142 n_err(_("Mailbox changed: it is likely that existing "
143 "rfc822 attachments became invalid!\n"));
144 free(mnbuf);
146 NYD_LEAVE;
147 n_sigman_leave(&sm, n_SIGMAN_VIPSIGS_NTTYOUT);
150 static int
151 _include_file(char const *name, int *linecount, int *charcount,
152 bool_t doecho, bool_t indent)
154 FILE *fbuf;
155 char const *indb;
156 int ret = -1;
157 char *linebuf = NULL; /* TODO line pool */
158 size_t linesize = 0, indl, linelen, cnt;
159 NYD_ENTER;
161 if (name == (char*)-1)
162 fbuf = stdin;
163 else if ((fbuf = Fopen(name, "r")) == NULL) {
164 n_perr(name, 0);
165 goto jleave;
168 if (!indent)
169 indb = NULL, indl = 0;
170 else {
171 if ((indb = ok_vlook(indentprefix)) == NULL)
172 indb = INDENT_DEFAULT;
173 indl = strlen(indb);
176 *linecount = *charcount = 0;
177 cnt = fsize(fbuf);
178 while (fgetline(&linebuf, &linesize, &cnt, &linelen, fbuf, 0) != NULL) {
179 if (indl > 0 && fwrite(indb, sizeof *indb, indl, _coll_fp) != indl)
180 goto jleave;
181 if (fwrite(linebuf, sizeof *linebuf, linelen, _coll_fp) != linelen)
182 goto jleave;
183 ++(*linecount);
184 (*charcount) += linelen + indl;
185 if ((options & OPT_INTERACTIVE) && doecho) {
186 if (indl > 0)
187 fwrite(indb, sizeof *indb, indl, stdout);
188 fwrite(linebuf, sizeof *linebuf, linelen, stdout);
191 if (fflush(_coll_fp))
192 goto jleave;
193 if ((options & OPT_INTERACTIVE) && doecho)
194 fflush(stdout);
196 ret = 0;
197 jleave:
198 if (linebuf != NULL)
199 free(linebuf);
200 if (fbuf != NULL && fbuf != stdin)
201 Fclose(fbuf);
202 NYD_LEAVE;
203 return ret;
206 static void
207 _collect_onpipe(int signo)
209 NYD_X; /* Signal handler */
210 n_UNUSED(signo);
211 siglongjmp(_coll_pipejmp, 1);
214 static void
215 insertcommand(FILE *fp, char const *cmd)
217 FILE *ibuf = NULL;
218 int c;
219 NYD_ENTER;
221 if ((ibuf = Popen(cmd, "r", ok_vlook(SHELL), NULL, 0)) != NULL) {
222 while ((c = getc(ibuf)) != EOF) /* XXX bytewise, yuck! */
223 putc(c, fp);
224 Pclose(ibuf, TRU1);
225 } else
226 n_perr(cmd, 0);
227 NYD_LEAVE;
230 static void
231 print_collf(FILE *cf, struct header *hp)
233 char *lbuf = NULL; /* TODO line pool */
234 sighandler_type sigint;
235 FILE * volatile obuf = stdout;
236 struct attachment *ap;
237 char const *cp;
238 enum gfield gf;
239 size_t linesize = 0, linelen, cnt, cnt2;
240 NYD_ENTER;
242 fflush_rewind(cf);
243 cnt = cnt2 = (size_t)fsize(cf);
245 sigint = safe_signal(SIGINT, SIG_IGN);
247 if ((options & OPT_INTERACTIVE) && (cp = ok_vlook(crt)) != NULL) {
248 size_t l, m;
250 m = 4;
251 if (hp->h_to != NULL)
252 ++m;
253 if (hp->h_subject != NULL)
254 ++m;
255 if (hp->h_cc != NULL)
256 ++m;
257 if (hp->h_bcc != NULL)
258 ++m;
259 if (hp->h_attach != NULL)
260 ++m;
261 m += (hp->h_from != NULL || myaddrs(hp) != NULL);
262 m += (hp->h_sender != NULL || ok_vlook(sender) != NULL);
263 m += (hp->h_replyto != NULL || ok_vlook(replyto) != NULL);
265 l = (*cp == '\0') ? (size_t)screensize() : strtoul(cp, NULL, 0);
266 if (m > l)
267 goto jpager;
268 l -= m;
270 for (m = 0; fgetline(&lbuf, &linesize, &cnt2, NULL, cf, 0); ++m)
272 rewind(cf);
273 if (l < m) {
274 jpager:
275 if (sigsetjmp(_coll_pipejmp, 1))
276 goto jendpipe;
277 if ((obuf = n_pager_open()) == NULL)
278 obuf = stdout;
279 else
280 safe_signal(SIGPIPE, &_collect_onpipe);
284 fprintf(obuf, _("-------\nMessage contains:\n"));
285 gf = GIDENT | GTO | GSUBJECT | GCC | GBCC | GNL | GFILES | GCOMMA;
286 puthead(TRU1, hp, obuf, gf, SEND_TODISP, CONV_NONE, NULL, NULL);
287 while (fgetline(&lbuf, &linesize, &cnt, &linelen, cf, 1))
288 prout(lbuf, linelen, obuf);
289 if (hp->h_attach != NULL) {
290 fputs(_("-------\nAttachments:\n"), obuf);
291 for (ap = hp->h_attach; ap != NULL; ap = ap->a_flink) {
292 if (ap->a_msgno)
293 fprintf(obuf, " - message %u\n", ap->a_msgno);
294 else {
295 /* TODO after MIME/send layer rewrite we *know*
296 * TODO the details of the attachment here,
297 * TODO so adjust this again, then */
298 char const *cs, *csi = "-> ";
300 if ((cs = ap->a_charset) == NULL &&
301 (csi = "<- ", cs = ap->a_input_charset) == NULL)
302 cs = charset_get_lc();
303 if ((cp = ap->a_content_type) == NULL)
304 cp = "?";
305 else if (ascncasecmp(cp, "text/", 5))
306 csi = n_empty;
307 fprintf(obuf, " - [%s, %s%s] %s\n", cp, csi, cs,
308 n_shexp_quote_cp(ap->a_name, FAL0));
313 jendpipe:
314 if (obuf != stdout)
315 n_pager_close(obuf);
316 if (lbuf != NULL)
317 free(lbuf);
318 safe_signal(SIGINT, sigint);
319 NYD_LEAVE;
322 static int
323 exwrite(char const *name, FILE *fp, int f)
325 FILE *of;
326 int c, lc, rv = -1;
327 long cc;
328 NYD_ENTER;
330 if (f) {
331 printf("%s ", n_shexp_quote_cp(name, FAL0));
332 fflush(stdout);
334 if ((of = Fopen(name, "a")) == NULL) {
335 n_perr(NULL, 0);
336 goto jleave;
339 lc = 0;
340 cc = 0;
341 while ((c = getc(fp)) != EOF) {
342 ++cc;
343 if (c == '\n')
344 ++lc;
345 putc(c, of);
346 if (ferror(of)) {
347 n_perr(name, 0);
348 Fclose(of);
349 goto jleave;
352 Fclose(of);
353 printf(_("%d/%ld\n"), lc, cc);
354 fflush(stdout);
355 rv = 0;
356 jleave:
357 NYD_LEAVE;
358 return rv;
361 static enum okay
362 makeheader(FILE *fp, struct header *hp, si8_t *checkaddr_err)
364 FILE *nf;
365 int c;
366 enum okay rv = STOP;
367 NYD_ENTER;
369 if ((nf = Ftmp(NULL, "colhead", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
370 n_perr(_("temporary mail edit file"), 0);
371 goto jleave;
374 extract_header(fp, hp, checkaddr_err);
376 while ((c = getc(fp)) != EOF) /* XXX bytewise, yuck! */
377 putc(c, nf);
378 if (fp != _coll_fp)
379 Fclose(_coll_fp);
380 Fclose(fp);
381 _coll_fp = nf;
382 if (check_from_and_sender(hp->h_from, hp->h_sender) == NULL)
383 goto jleave;
384 rv = OKAY;
385 jleave:
386 NYD_LEAVE;
387 return rv;
390 static void
391 mesedit(int c, struct header *hp)
393 bool_t saved;
394 sighandler_type sigint;
395 FILE *nf;
396 NYD_ENTER;
398 if(!(saved = ok_blook(add_file_recipients)))
399 ok_bset(add_file_recipients);
401 sigint = safe_signal(SIGINT, SIG_IGN);
402 nf = run_editor(_coll_fp, (off_t)-1, c, FAL0, hp, NULL, SEND_MBOX, sigint);
403 if (nf != NULL) {
404 if (hp) {
405 rewind(nf);
406 makeheader(nf, hp, NULL);
407 } else {
408 fseek(nf, 0L, SEEK_END);
409 Fclose(_coll_fp);
410 _coll_fp = nf;
413 safe_signal(SIGINT, sigint);
415 if(!saved)
416 ok_bclear(add_file_recipients);
417 NYD_LEAVE;
420 static void
421 mespipe(char *cmd)
423 FILE *nf;
424 sighandler_type sigint;
425 NYD_ENTER;
427 sigint = safe_signal(SIGINT, SIG_IGN);
429 if ((nf = Ftmp(NULL, "colpipe", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==NULL) {
430 n_perr(_("temporary mail edit file"), 0);
431 goto jout;
434 /* stdin = current message. stdout = new message */
435 fflush(_coll_fp);
436 if (run_command(ok_vlook(SHELL), 0, fileno(_coll_fp), fileno(nf), "-c",
437 cmd, NULL, NULL) < 0) {
438 Fclose(nf);
439 goto jout;
442 if (fsize(nf) == 0) {
443 n_err(_("No bytes from %s !?\n"), n_shexp_quote_cp(cmd, FAL0));
444 Fclose(nf);
445 goto jout;
448 /* Take new files */
449 fseek(nf, 0L, SEEK_END);
450 Fclose(_coll_fp);
451 _coll_fp = nf;
452 jout:
453 safe_signal(SIGINT, sigint);
454 NYD_LEAVE;
457 static int
458 forward(char *ms, FILE *fp, int f)
460 int *msgvec, rv = 0;
461 struct n_ignore const *itp;
462 char const *tabst;
463 enum sendaction action;
464 NYD_ENTER;
466 msgvec = salloc((size_t)(msgCount + 1) * sizeof *msgvec);
467 if (getmsglist(ms, msgvec, 0) < 0)
468 goto jleave;
469 if (*msgvec == 0) {
470 *msgvec = first(0, MMNORM);
471 if (*msgvec == 0) {
472 n_err(_("No appropriate messages\n"));
473 goto jleave;
475 msgvec[1] = 0;
478 if (f == 'f' || f == 'F' || f == 'u')
479 tabst = NULL;
480 else if ((tabst = ok_vlook(indentprefix)) == NULL)
481 tabst = INDENT_DEFAULT;
482 if (f == 'u' || f == 'U')
483 itp = n_IGNORE_ALL;
484 else
485 itp = upperchar(f) ? NULL : n_IGNORE_TYPE;
486 action = (upperchar(f) && f != 'U') ? SEND_QUOTE_ALL : SEND_QUOTE;
488 printf(_("Interpolating:"));
489 for (; *msgvec != 0; ++msgvec) {
490 struct message *mp = message + *msgvec - 1;
492 touch(mp);
493 printf(" %d", *msgvec);
494 fflush(stdout);
495 if (sendmp(mp, fp, itp, tabst, action, NULL) < 0) {
496 n_perr(_("temporary mail file"), 0);
497 rv = -1;
498 break;
501 printf("\n");
502 jleave:
503 NYD_LEAVE;
504 return rv;
507 static void
508 _collint(int s)
510 NYD_X; /* Signal handler */
512 /* the control flow is subtle, because we can be called from ~q */
513 if (_coll_hadintr == 0) {
514 if (ok_blook(ignore)) {
515 puts("@");
516 fflush(stdout);
517 clearerr(stdin);
518 } else
519 _coll_hadintr = 1;
520 siglongjmp(_coll_jmp, 1);
522 exit_status |= EXIT_SEND_ERROR;
523 if (s != 0)
524 savedeadletter(_coll_fp, TRU1);
525 /* Aborting message, no need to fflush() .. */
526 siglongjmp(_coll_abort, 1);
529 static void
530 collhup(int s)
532 NYD_X; /* Signal handler */
533 n_UNUSED(s);
535 savedeadletter(_coll_fp, TRU1);
536 /* Let's pretend nobody else wants to clean up, a true statement at
537 * this time */
538 exit(EXIT_ERR);
541 static int
542 putesc(char const *s, FILE *stream)
544 int n = 0, rv = -1;
545 NYD_ENTER;
547 while (s[0] != '\0') {
548 if (s[0] == '\\') {
549 if (s[1] == 't') {
550 if (putc('\t', stream) == EOF)
551 goto jleave;
552 ++n;
553 s += 2;
554 continue;
556 if (s[1] == 'n') {
557 if (putc('\n', stream) == EOF)
558 goto jleave;
559 ++n;
560 s += 2;
561 continue;
564 if (putc(s[0], stream) == EOF)
565 goto jleave;
566 ++n;
567 ++s;
569 if (putc('\n', stream) == EOF)
570 goto jleave;
571 rv = ++n;
572 jleave:
573 NYD_LEAVE;
574 return rv;
577 static void
578 a_coll__hook_setter(void *arg){ /* TODO v15: drop */
579 struct header *hp;
580 char const *val;
581 NYD2_ENTER;
583 hp = arg;
585 if((val = detract(hp->h_from, GNAMEONLY)) == NULL)
586 val = n_empty;
587 ok_vset(compose_from, val);
588 if((val = detract(hp->h_sender, 0)) == NULL)
589 val = n_empty;
590 ok_vset(compose_sender, val);
591 if((val = detract(hp->h_to, GNAMEONLY)) == NULL)
592 val = n_empty;
593 ok_vset(compose_to, val);
594 if((val = detract(hp->h_cc, GNAMEONLY)) == NULL)
595 val = n_empty;
596 ok_vset(compose_cc, val);
597 if((val = detract(hp->h_bcc, GNAMEONLY)) == NULL)
598 val = n_empty;
599 ok_vset(compose_bcc, val);
600 if((val = hp->h_subject) == NULL)
601 val = n_empty;
602 ok_vset(compose_subject, val);
603 NYD2_LEAVE;
606 FL FILE *
607 collect(struct header *hp, int printheaders, struct message *mp,
608 char *quotefile, int doprefix, si8_t *checkaddr_err)
610 struct n_ignore const *quoteitp;
611 int lc, cc, c;
612 int volatile t;
613 int volatile escape, getfields;
614 char *linebuf;
615 char const *cp;
616 size_t i, linesize; /* TODO line pool */
617 long cnt;
618 enum sendaction action;
619 sigset_t oset, nset;
620 FILE * volatile sigfp;
621 NYD_ENTER;
623 _coll_fp = NULL;
624 sigfp = NULL;
625 linesize = 0;
626 linebuf = NULL;
628 /* Start catching signals from here, but we're still die on interrupts
629 * until we're in the main loop */
630 sigfillset(&nset);
631 sigprocmask(SIG_BLOCK, &nset, &oset);
632 /* FIXME have dropped handlerpush() and localized onintr() in lex.c! */
633 if ((_coll_saveint = safe_signal(SIGINT, SIG_IGN)) != SIG_IGN)
634 safe_signal(SIGINT, &_collint);
635 if ((_coll_savehup = safe_signal(SIGHUP, SIG_IGN)) != SIG_IGN)
636 safe_signal(SIGHUP, collhup);
637 if (sigsetjmp(_coll_abort, 1))
638 goto jerr;
639 if (sigsetjmp(_coll_jmp, 1))
640 goto jerr;
641 pstate |= PS_RECURSED;
642 sigprocmask(SIG_SETMASK, &oset, (sigset_t*)NULL);
644 if ((_coll_fp = Ftmp(NULL, "collect", OF_RDWR | OF_UNLINK | OF_REGISTER)) ==
645 NULL) {
646 n_perr(_("temporary mail file"), 0);
647 goto jerr;
650 /* If we are going to prompt for a subject, refrain from printing a newline
651 * after the headers (since some people mind) */
652 getfields = 0;
653 if (!(options & OPT_t_FLAG)) {
654 t = GTO | GSUBJECT | GCC | GNL;
655 if (ok_blook(fullnames))
656 t |= GCOMMA;
658 if (options & OPT_INTERACTIVE) {
659 if (hp->h_subject == NULL && (ok_blook(ask) || ok_blook(asksub)))
660 t &= ~GNL, getfields |= GSUBJECT;
662 if (hp->h_to == NULL)
663 t &= ~GNL, getfields |= GTO;
665 if (!ok_blook(bsdcompat) && !ok_blook(askatend)) {
666 if (hp->h_bcc == NULL && ok_blook(askbcc))
667 t &= ~GNL, getfields |= GBCC;
668 if (hp->h_cc == NULL && ok_blook(askcc))
669 t &= ~GNL, getfields |= GCC;
672 } else {
673 n_UNINIT(t, 0);
676 escape = ((cp = ok_vlook(escape)) != NULL) ? *cp : ESCAPE;
677 _coll_hadintr = 0;
679 if (!sigsetjmp(_coll_jmp, 1)) {
680 /* Ask for some headers first, as necessary */
681 if (getfields)
682 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, getfields, 1);
684 /* Execute compose-enter TODO completely v15-compat intermediate!! */
685 if((cp = ok_vlook(on_compose_enter)) != NULL){
686 setup_from_and_sender(hp);
687 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
690 if(!(options & OPT_Mm_FLAG)){
691 char const *cp_obsolete = ok_vlook(NAIL_HEAD);
692 if(cp_obsolete != NULL)
693 OBSOLETE(_("please use *message-inject-head* "
694 "instead of *NAIL_HEAD*"));
696 if(((cp = ok_vlook(message_inject_head)) != NULL ||
697 (cp = cp_obsolete) != NULL) && putesc(cp, _coll_fp) < 0)
698 goto jerr;
700 /* Quote an original message */
701 if (mp != NULL && (doprefix || (cp = ok_vlook(quote)) != NULL)) {
702 quoteitp = n_IGNORE_ALL;
703 action = SEND_QUOTE;
704 if (doprefix) {
705 quoteitp = n_IGNORE_FWD;
706 if ((cp = ok_vlook(fwdheading)) == NULL)
707 cp = "-------- Original Message --------";
708 if (*cp != '\0' && fprintf(_coll_fp, "%s\n", cp) < 0)
709 goto jerr;
710 } else if (!strcmp(cp, "noheading")) {
711 /*EMPTY*/;
712 } else if (!strcmp(cp, "headers")) {
713 quoteitp = n_IGNORE_TYPE;
714 } else if (!strcmp(cp, "allheaders")) {
715 quoteitp = NULL;
716 action = SEND_QUOTE_ALL;
717 } else {
718 cp = hfield1("from", mp);
719 if (cp != NULL && (cnt = (long)strlen(cp)) > 0) {
720 if (xmime_write(cp, cnt, _coll_fp, CONV_FROMHDR, TD_NONE) < 0)
721 goto jerr;
722 if (fprintf(_coll_fp, _(" wrote:\n\n")) < 0)
723 goto jerr;
726 if (fflush(_coll_fp))
727 goto jerr;
728 if (doprefix)
729 cp = NULL;
730 else if ((cp = ok_vlook(indentprefix)) == NULL)
731 cp = INDENT_DEFAULT;
732 if (sendmp(mp, _coll_fp, quoteitp, cp, action, NULL) < 0)
733 goto jerr;
737 if (quotefile != NULL) {
738 if (_include_file(quotefile, &lc, &cc,
739 !(options & OPT_Mm_FLAG), FAL0) != 0)
740 goto jerr;
743 if ((options & (OPT_Mm_FLAG | OPT_INTERACTIVE)) == OPT_INTERACTIVE) {
744 /* Print what we have sofar also on the terminal (if useful) */
745 if (!ok_blook(editalong)) {
746 if (printheaders)
747 puthead(TRU1, hp, stdout, t, SEND_TODISP, CONV_NONE, NULL, NULL);
749 rewind(_coll_fp);
750 while ((c = getc(_coll_fp)) != EOF) /* XXX bytewise, yuck! */
751 putc(c, stdout);
752 if (fseek(_coll_fp, 0, SEEK_END))
753 goto jerr;
755 /* Ensure this is clean xxx not really necessary? */
756 fflush(stdout);
757 } else {
758 rewind(_coll_fp);
759 mesedit('e', hp);
760 /* As mandated by the Mail Reference Manual, print "(continue)" */
761 jcont:
762 if(options & OPT_INTERACTIVE){
763 printf(_("(continue)\n"));
764 fflush(stdout);
768 } else {
769 /* Come here for printing the after-signal message. Duplicate messages
770 * won't be printed because the write is aborted if we get a SIGTTOU */
771 if (_coll_hadintr)
772 n_err(_("\n(Interrupt -- one more to kill letter)\n"));
775 /* We're done with -M or -m */
776 if(options & OPT_Mm_FLAG)
777 goto jout;
778 /* No tilde escapes, interrupts not expected. Simply copy STDIN */
779 if (!(options & (OPT_INTERACTIVE | OPT_t_FLAG | OPT_TILDE_FLAG))) {
780 linebuf = srealloc(linebuf, linesize = LINESIZE);
781 while ((i = fread(linebuf, sizeof *linebuf, linesize, stdin)) > 0) {
782 if (i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
783 goto jerr;
785 goto jout;
788 /* The interactive collect loop.
789 * All commands which come here are forbidden when sourcing! */
790 assert(_coll_hadintr || !(pstate & PS_SOURCING));
791 for(;;){
792 /* C99 */{
793 enum n_lexinput_flags lif;
795 lif = n_LEXINPUT_CTX_COMPOSE;
796 if(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)){
797 if(!(options & OPT_t_FLAG))
798 lif |= n_LEXINPUT_NL_ESC;
800 cnt = n_lex_input(lif, n_empty, &linebuf, &linesize, NULL);
803 if (cnt < 0) {
804 assert(!(pstate & PS_SOURCING));
805 if (options & OPT_t_FLAG) {
806 fflush_rewind(_coll_fp);
807 /* It is important to set PS_t_FLAG before extract_header() *and*
808 * keep OPT_t_FLAG for the first parse of the message, too! */
809 pstate |= PS_t_FLAG;
810 if (makeheader(_coll_fp, hp, checkaddr_err) != OKAY)
811 goto jerr;
812 options &= ~OPT_t_FLAG;
813 continue;
814 } else if ((options & OPT_INTERACTIVE) && ok_blook(ignoreeof)) {
815 printf(_("*ignoreeof* set, use `~.' to terminate letter\n"));
816 continue;
818 break;
821 _coll_hadintr = 0;
823 cp = linebuf;
824 if(cnt == 0)
825 goto jputnl;
826 else if(!(options & (OPT_INTERACTIVE | OPT_TILDE_FLAG)))
827 goto jputline;
828 else if(cp[0] == '.'){
829 if(cnt == 1 && (ok_blook(dot) || ok_blook(ignoreeof)))
830 break;
832 if(cp[0] != escape){
833 jputline:
834 if(fwrite(cp, sizeof *cp, cnt, _coll_fp) != (size_t)cnt)
835 goto jerr;
836 /* TODO PS_READLINE_NL is a terrible hack to ensure that _in_all_-
837 * TODO _code_paths_ a file without trailing newline isn't modified
838 * TODO to continue one; the "saw-newline" needs to be part of an
839 * TODO I/O input machinery object */
840 jputnl:
841 if(pstate & PS_READLINE_NL){
842 if(putc('\n', _coll_fp) == EOF)
843 goto jerr;
845 continue;
848 /* Cleanup the input string: like this we can perform a little bit of
849 * usage testing and also have somewhat normalized history entries */
850 for(cp = &linebuf[2]; (c = *cp) != '\0' && blankspacechar(c); ++cp)
851 continue;
852 if(c == '\0'){
853 linebuf[2] = '\0';
854 cnt = 2;
855 }else{
856 i = PTR2SIZE(cp - linebuf) - 3;
857 memmove(&linebuf[3], cp, (cnt -= i));
858 linebuf[2] = ' ';
859 linebuf[cnt] = '\0';
861 if(cnt > 0){ /* TODO v15 no more trailing WS from lex_input please */
862 cp = &linebuf[cnt];
864 for(;; --cp){
865 c = cp[-1];
866 if(!blankspacechar(c))
867 break;
869 ((char*)n_UNCONST(cp))[0] = '\0';
870 cnt = PTR2SIZE(cp - linebuf);
874 switch((c = linebuf[1])){
875 default:
876 /* On double escape, send a single one. Otherwise, it's an error */
877 if(c == escape){
878 cp = &linebuf[1];
879 --cnt;
880 goto jputline;
881 }else{
882 char buf[sizeof(n_UNIREPL)];
884 if(asciichar(c))
885 buf[0] = c, buf[1] = '\0';
886 else if(options & OPT_UNICODE)
887 memcpy(buf, n_unirepl, sizeof n_unirepl);
888 else
889 buf[0] = '?', buf[1] = '\0';
890 n_err(_("Unknown tilde escape: ~%s\n"), buf);
891 continue;
893 jearg:
894 n_err(_("Invalid tilde escape usage: %s\n"), linebuf);
895 continue;
896 case '!':
897 /* Shell escape, send the balance of line to sh -c */
898 if(cnt == 2)
899 goto jearg;
900 c_shell(&linebuf[3]);
901 goto jhistcont;
902 case ':':
903 /* FALLTHRU */
904 case '_':
905 /* Escape to command mode, but be nice! *//* TODO command expansion
906 * TODO should be handled here so that we have unique history! */
907 if(cnt == 2)
908 goto jearg;
909 _execute_command(hp, &linebuf[3], cnt -= 3);
910 break;
911 case '.':
912 /* Simulate end of file on input */
913 if(cnt != 2)
914 goto jearg;
915 goto jout;
916 case 'x':
917 /* Same as 'q', but no *DEAD* saving */
918 /* FALLTHRU */
919 case 'q':
920 /* Force a quit, act like an interrupt had happened */
921 if(cnt != 2)
922 goto jearg;
923 ++_coll_hadintr;
924 _collint((c == 'x') ? 0 : SIGINT);
925 exit(EXIT_ERR);
926 /*NOTREACHED*/
927 case 'h':
928 /* Grab a bunch of headers */
929 if(cnt != 2)
930 goto jearg;
932 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp,
933 (GTO | GSUBJECT | GCC | GBCC),
934 (ok_blook(bsdcompat) && ok_blook(bsdorder)));
935 while(hp->h_to == NULL);
936 break;
937 case 'H':
938 /* Grab extra headers */
939 if(cnt != 2)
940 goto jearg;
942 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GEXTRA, 0);
943 while(check_from_and_sender(hp->h_from, hp->h_sender) == NULL);
944 break;
945 case 't':
946 /* Add to the To: list */
947 if(cnt == 2)
948 goto jearg;
949 hp->h_to = cat(hp->h_to,
950 checkaddrs(lextract(&linebuf[3], GTO | GFULL), EACM_NORMAL,
951 NULL));
952 break;
953 case 's':
954 /* Set the Subject list */
955 if(cnt == 2)
956 goto jearg;
957 /* Subject:; take care for Debian #419840 and strip any \r and \n */
958 if(n_anyof_cp("\n\r", hp->h_subject = savestr(&linebuf[3]))){
959 char *xp;
961 n_err(_("-s: normalizing away invalid ASCII NL / CR bytes\n"));
962 for(xp = hp->h_subject; *xp != '\0'; ++xp)
963 if(*xp == '\n' || *xp == '\r')
964 *xp = ' ';
966 break;
967 case '@':
968 /* Edit the attachment list */
969 if(cnt != 2)
970 append_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach,
971 &linebuf[3]);
972 else
973 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
974 break;
975 case 'c':
976 /* Add to the CC list */
977 if(cnt == 2)
978 goto jearg;
979 hp->h_cc = cat(hp->h_cc,
980 checkaddrs(lextract(&linebuf[3], GCC | GFULL), EACM_NORMAL,
981 NULL));
982 break;
983 case 'b':
984 /* Add stuff to blind carbon copies list */
985 if(cnt == 2)
986 goto jearg;
987 hp->h_bcc = cat(hp->h_bcc,
988 checkaddrs(lextract(&linebuf[3], GBCC | GFULL), EACM_NORMAL,
989 NULL));
990 break;
991 case 'd':
992 if(cnt != 2)
993 goto jearg;
994 cp = n_getdeadletter();
995 if(0){
996 /*FALLTHRU*/
997 case 'R':
998 case 'r':
999 case '<':
1000 /* Invoke a file: Search for the file name, then open it and copy
1001 * the contents to _coll_fp */
1002 if(cnt == 2){
1003 n_err(_("Interpolate what file?\n"));
1004 break;
1006 if(*(cp = &linebuf[3]) == '!'){
1007 insertcommand(_coll_fp, ++cp);
1008 goto jhistcont;
1010 if((cp = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL)
1011 break;
1013 if(is_dir(cp)){
1014 n_err(_("%s: is a directory\n"), n_shexp_quote_cp(cp, FAL0));
1015 break;
1017 if(_include_file(cp, &lc, &cc, FAL0, (c == 'R')) != 0){
1018 if(ferror(_coll_fp))
1019 goto jerr;
1020 break;
1022 printf(_("%s %d/%d\n"), n_shexp_quote_cp(cp, FAL0), lc, cc);
1023 break;
1024 case 'i':
1025 /* Insert a variable into the file */
1026 if(cnt == 2)
1027 goto jearg;
1028 if((cp = vok_vlook(&linebuf[3])) == NULL || *cp == '\0')
1029 break;
1030 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user resp upon `set' time */
1031 goto jerr;
1032 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1033 goto jerr;
1034 break;
1035 case 'a':
1036 case 'A':
1037 /* Insert the contents of a signature variable */
1038 if(cnt != 2)
1039 goto jearg;
1040 cp = (c == 'a') ? ok_vlook(sign) : ok_vlook(Sign);
1041 if(cp != NULL && *cp != '\0'){
1042 if(putesc(cp, _coll_fp) < 0) /* TODO v15: user upon `set' time */
1043 goto jerr;
1044 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1045 goto jerr;
1047 break;
1048 case 'w':
1049 /* Write the message on a file */
1050 if(cnt == 2)
1051 goto jearg;
1052 if((cp = fexpand(&linebuf[3], FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1053 n_err(_("Write what file!?\n"));
1054 break;
1056 rewind(_coll_fp);
1057 if(exwrite(cp, _coll_fp, 1) < 0)
1058 goto jerr;
1059 break;
1060 case 'm':
1061 case 'M':
1062 case 'f':
1063 case 'F':
1064 case 'u':
1065 case 'U':
1066 /* Interpolate the named messages, if we are in receiving mail mode.
1067 * Does the standard list processing garbage. If ~f is given, we
1068 * don't shift over */
1069 if(cnt == 2)
1070 goto jearg;
1071 if(forward(&linebuf[3], _coll_fp, c) < 0)
1072 break;
1073 break;
1074 case 'p':
1075 /* Print current state of the message without altering anything */
1076 if(cnt != 2)
1077 goto jearg;
1078 print_collf(_coll_fp, hp);
1079 break;
1080 case '|':
1081 /* Pipe message through command. Collect output as new message */
1082 if(cnt == 2)
1083 goto jearg;
1084 rewind(_coll_fp);
1085 mespipe(&linebuf[3]);
1086 goto jhistcont;
1087 case 'v':
1088 case 'e':
1089 /* Edit the current message. 'e' -> use EDITOR, 'v' -> use VISUAL */
1090 if(cnt != 2)
1091 goto jearg;
1092 rewind(_coll_fp);
1093 mesedit(c, ok_blook(editheaders) ? hp : NULL);
1094 goto jhistcont;
1095 case '?':
1096 /* Last the lengthy help string. (Very ugly, but take care for
1097 * compiler supported string lengths :() */
1098 puts(_(
1099 "TILDE ESCAPES (to be placed after a newline) excerpt:\n"
1100 "~. Commit and send message\n"
1101 "~: <command> Execute a mail command\n"
1102 "~<! <command> Insert output of command\n"
1103 "~@ [<files>] Edit attachment list\n"
1104 "~A Insert *Sign* variable (`~a' inserts *sign*)\n"
1105 "~c <users> Add users to Cc: list (`~b' for Bcc:)\n"
1106 "~d Read in *DEAD* (dead.letter)\n"
1107 "~e Edit message via *EDITOR*"
1109 puts(_(
1110 "~F <msglist> Read in with headers, don't *indentprefix* lines\n"
1111 "~f <msglist> Like ~F, but honour `ignore' / `retain' configuration\n"
1112 "~H Edit From:, Reply-To: and Sender:\n"
1113 "~h Prompt for Subject:, To:, Cc: and blind Bcc:\n"
1114 "~i <variable> Insert a value and a newline\n"
1115 "~M <msglist> Read in with headers, *indentprefix* (`~m': `retain' etc.)\n"
1116 "~p Print current message compose buffer\n"
1117 "~r <file> Read in a file (`~R' *indentprefix* lines)"
1119 puts(_(
1120 "~s <subject> Set Subject:\n"
1121 "~t <users> Add users to To: list\n"
1122 "~u <msglist> Read in message(s) without headers (`~U' indents lines)\n"
1123 "~v Edit message via *VISUAL*\n"
1124 "~w <file> Write message onto file\n"
1125 "~x Abort composition, discard message (`~q' saves in *DEAD*)\n"
1126 "~| <command> Pipe message through shell filter"
1128 if(cnt != 2)
1129 goto jearg;
1130 break;
1133 /* Finally place an entry in history as applicable */
1134 if(0){
1135 jhistcont:
1136 c = '\1';
1137 }else
1138 c = '\0';
1139 if(!(options & OPT_t_FLAG))
1140 n_tty_addhist(linebuf, TRU1);
1141 if(c != '\0')
1142 goto jcont;
1145 jout:
1146 /* Execute compose-leave TODO completely v15-compat intermediate!! */
1147 if((cp = ok_vlook(on_compose_leave)) != NULL){
1148 setup_from_and_sender(hp);
1149 call_compose_mode_hook(cp, &a_coll__hook_setter, hp);
1152 /* Final change to edit headers, if not already above */
1153 if (ok_blook(bsdcompat) || ok_blook(askatend)) {
1154 if (hp->h_cc == NULL && ok_blook(askcc))
1155 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GCC, 1);
1156 if (hp->h_bcc == NULL && ok_blook(askbcc))
1157 grab_headers(n_LEXINPUT_CTX_COMPOSE, hp, GBCC, 1);
1159 if (hp->h_attach == NULL && ok_blook(askattach))
1160 edit_attachments(n_LEXINPUT_CTX_COMPOSE, &hp->h_attach);
1162 /* Add automatic receivers */
1163 if ((cp = ok_vlook(autocc)) != NULL && *cp != '\0')
1164 hp->h_cc = cat(hp->h_cc, checkaddrs(lextract(cp, GCC | GFULL),
1165 EACM_NORMAL, checkaddr_err));
1166 if ((cp = ok_vlook(autobcc)) != NULL && *cp != '\0')
1167 hp->h_bcc = cat(hp->h_bcc, checkaddrs(lextract(cp, GBCC | GFULL),
1168 EACM_NORMAL, checkaddr_err));
1169 if (*checkaddr_err != 0)
1170 goto jerr;
1172 if(options & OPT_Mm_FLAG)
1173 goto jskiptails;
1175 /* Place signature? */
1176 if((cp = ok_vlook(signature)) != NULL && *cp != '\0'){
1177 char const *cpq;
1179 if((cpq = fexpand(cp, FEXP_LOCAL | FEXP_NOPROTO)) == NULL){
1180 n_err(_("*signature* expands to invalid file: %s\n"),
1181 n_shexp_quote_cp(cp, FAL0));
1182 goto jerr;
1184 cpq = n_shexp_quote_cp(cp = cpq, FAL0);
1186 if((sigfp = Fopen(cp, "r")) == NULL){
1187 n_err(_("Can't open *signature* %s: %s\n"), cpq, strerror(errno));
1188 goto jerr;
1191 if(linebuf == NULL)
1192 linebuf = smalloc(linesize = LINESIZE);
1193 c = '\0';
1194 while((i = fread(linebuf, sizeof *linebuf, linesize, n_UNVOLATILE(sigfp)))
1195 > 0){
1196 c = linebuf[i - 1];
1197 if(i != fwrite(linebuf, sizeof *linebuf, i, _coll_fp))
1198 goto jerr;
1201 /* C99 */{
1202 FILE *x = n_UNVOLATILE(sigfp);
1203 int e = errno, ise = ferror(x);
1205 sigfp = NULL;
1206 Fclose(x);
1208 if(ise){
1209 n_err(_("Errors while reading *signature* %s: %s\n"),
1210 cpq, strerror(e));
1211 goto jerr;
1215 if(c != '\0' && c != '\n')
1216 putc('\n', _coll_fp);
1219 { char const *cp_obsolete = ok_vlook(NAIL_TAIL);
1221 if(cp_obsolete != NULL)
1222 OBSOLETE(_("please use *message-inject-tail* instead of *NAIL_TAIL*"));
1224 if((cp = ok_vlook(message_inject_tail)) != NULL ||
1225 (cp = cp_obsolete) != NULL){
1226 if(putesc(cp, _coll_fp) < 0)
1227 goto jerr;
1228 if((options & OPT_INTERACTIVE) && putesc(cp, stdout) < 0)
1229 goto jerr;
1233 jskiptails:
1234 if(fflush(_coll_fp))
1235 goto jerr;
1236 rewind(_coll_fp);
1238 jleave:
1239 if (linebuf != NULL)
1240 free(linebuf);
1241 sigfillset(&nset);
1242 sigprocmask(SIG_BLOCK, &nset, NULL);
1243 pstate &= ~PS_RECURSED;
1244 safe_signal(SIGINT, _coll_saveint);
1245 safe_signal(SIGHUP, _coll_savehup);
1246 sigprocmask(SIG_SETMASK, &oset, NULL);
1247 NYD_LEAVE;
1248 return _coll_fp;
1250 jerr:
1251 if(sigfp != NULL)
1252 Fclose(n_UNVOLATILE(sigfp));
1253 if (_coll_fp != NULL) {
1254 Fclose(_coll_fp);
1255 _coll_fp = NULL;
1257 goto jleave;
1260 /* s-it-mode */