]> diplodocus.org Git - nmh/blob - uip/mhlsbr.c
Added check of -fcc with no fcc in components.
[nmh] / uip / mhlsbr.c
1
2 /*
3 * mhlsbr.c -- main routines for nmh message lister
4 *
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
14 #include <h/tws.h>
15 #include <h/utils.h>
16 #include <setjmp.h>
17 #include <sys/types.h>
18
19 /*
20 * MAJOR BUG:
21 * for a component containing addresses, ADDRFMT, if COMPRESS is also
22 * set, then addresses get split wrong (not at the spaces between commas).
23 * To fix this correctly, putstr() should know about "atomic" strings that
24 * must NOT be broken across lines. That's too difficult for right now
25 * (it turns out that there are a number of degernate cases), so in
26 * oneline(), instead of
27 *
28 * (*onelp == '\n' && !onelp[1])
29 *
30 * being a terminating condition,
31 *
32 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
33 *
34 * is used instead. This cuts the line prematurely, and gives us a much
35 * better chance of getting things right.
36 */
37
38 #define ONECOMP 0
39 #define TWOCOMP 1
40 #define BODYCOMP 2
41
42 #define QUOTE '\\'
43
44 #define MHL_SWITCHES \
45 X("bell", 0, BELLSW) \
46 X("nobell", 0, NBELLSW) \
47 X("clear", 0, CLRSW) \
48 X("noclear", 0, NCLRSW) \
49 X("folder +folder", 0, FOLDSW) \
50 X("form formfile", 0, FORMSW) \
51 X("moreproc program", 0, PROGSW) \
52 X("nomoreproc", 0, NPROGSW) \
53 X("length lines", 0, LENSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("sleep seconds", 0, SLEEPSW) \
56 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
57 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("forward", -7, FORW1SW) /* interface from forw */ \
61 X("forwall", -7, FORW2SW) /* interface from forw */ \
62 X("digest list", -6, DGSTSW) \
63 X("volume number", -6, VOLUMSW) \
64 X("issue number", -5, ISSUESW) \
65 X("nobody", -6, NBODYSW) \
66 X("fmtproc program", 0, FMTPROCSW) \
67 X("nofmtproc", 0, NFMTPROCSW) \
68
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHL);
71 #undef X
72
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHL, mhlswitches);
75 #undef X
76
77 #define NOCOMPONENT 0x000001 /* don't show component name */
78 #define UPPERCASE 0x000002 /* display in all upper case */
79 #define CENTER 0x000004 /* center line */
80 #define CLEARTEXT 0x000008 /* cleartext */
81 #define EXTRA 0x000010 /* an "extra" component */
82 #define HDROUTPUT 0x000020 /* already output */
83 #define CLEARSCR 0x000040 /* clear screen */
84 #define LEFTADJUST 0x000080 /* left justify multiple lines */
85 #define COMPRESS 0x000100 /* compress text */
86 #define ADDRFMT 0x000200 /* contains addresses */
87 #define BELL 0x000400 /* sound bell at EOP */
88 #define DATEFMT 0x000800 /* contains dates */
89 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
90 #define INIT 0x002000 /* initialize component */
91 #define RTRIM 0x004000 /* trim trailing whitespace */
92 #define SPLIT 0x010000 /* split headers (don't concatenate) */
93 #define NONEWLINE 0x020000 /* don't write trailing newline */
94 #define NOWRAP 0x040000 /* Don't wrap lines ever */
95 #define FMTFILTER 0x080000 /* Filter through format filter */
96 #define INVISIBLE 0x100000 /* count byte in display columns? */
97 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
98 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
99 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
100
101 /*
102 * A format string to be used as a command-line argument to the body
103 * format filter.
104 */
105
106 struct arglist {
107 struct format *a_fmt;
108 char *a_nfs;
109 struct arglist *a_next;
110 };
111
112 /*
113 * Linked list of command line arguments for the body format filter. This
114 * USED to be in "struct mcomp", but the format API got cleaned up and even
115 * though it reduced the code we had to do, it make things more complicated
116 * for us. Specifically:
117 *
118 * - The interface to the hash table has been cleaned up, which means the
119 * rooting around in the hash table is no longer necessary (yay!). But
120 * this ALSO means that we have to make sure that we call our format
121 * compilation routines before we process the message, because the
122 * components need to be visible in the hash table so we can save them for
123 * later. So we moved them out of "mcomp" and now compile them right before
124 * header processing starts.
125 * - We also use format strings to handle other components in the mhl
126 * configuration (using "formatfield" and "decode"), but here life
127 * gets complicated: they aren't dealt with in the normal way. Instead
128 * of referring to a component like {from}, each component is processed
129 * using the special {text} component. But these format strings need to be
130 * compiled BEFORE we compile the format arguments; in the previous
131 * implementation they were compiled and scanned as the headers were
132 * read, and that would reset the hash table that we need to populate
133 * the components used by the body format filter. So we are compiling
134 * the formatfield component strings ahead of time and then scanning them
135 * later.
136 *
137 * Okay, fine ... this was broken before. But you know what? Fixing this
138 * the right way will make things easier down the road.
139 *
140 * One side-effect to this change: format strings are now compiled only once
141 * for components specified with "formatfield", but they are compiled for
142 * every message for format arguments.
143 */
144
145 static struct arglist *arglist_head;
146 static struct arglist *arglist_tail;
147 static int filter_nargs = 0;
148
149 /*
150 * Flags/options for each component
151 */
152
153 struct mcomp {
154 char *c_name; /* component name */
155 char *c_text; /* component text */
156 char *c_ovtxt; /* text overflow indicator */
157 char *c_nfs; /* iff FORMAT */
158 struct format *c_fmt; /* .. */
159 struct comp *c_c_text; /* Ref to {text} in FORMAT */
160 struct comp *c_c_error; /* Ref to {error} */
161 int c_offset; /* left margin indentation */
162 int c_ovoff; /* overflow indentation */
163 int c_width; /* width of field */
164 int c_cwidth; /* width of component */
165 int c_length; /* length in lines */
166 unsigned long c_flags;
167 struct mcomp *c_next;
168 };
169
170 static struct mcomp *msghd = NULL;
171 static struct mcomp *msgtl = NULL;
172 static struct mcomp *fmthd = NULL;
173 static struct mcomp *fmttl = NULL;
174
175 static struct mcomp global = {
176 NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL
177 };
178
179 static struct mcomp holder = {
180 NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL
181 };
182
183 struct pair {
184 char *p_name;
185 unsigned long p_flags;
186 };
187
188 static struct pair pairs[] = {
189 { "Date", DATEFMT },
190 { "From", ADDRFMT },
191 { "Sender", ADDRFMT },
192 { "Reply-To", ADDRFMT },
193 { "To", ADDRFMT },
194 { "cc", ADDRFMT },
195 { "Bcc", ADDRFMT },
196 { "Resent-Date", DATEFMT },
197 { "Resent-From", ADDRFMT },
198 { "Resent-Sender", ADDRFMT },
199 { "Resent-Reply-To", ADDRFMT },
200 { "Resent-To", ADDRFMT },
201 { "Resent-cc", ADDRFMT },
202 { "Resent-Bcc", ADDRFMT },
203 { NULL, 0 }
204 };
205
206 struct triple {
207 char *t_name;
208 unsigned long t_on;
209 unsigned long t_off;
210 };
211
212 static struct triple triples[] = {
213 { "nocomponent", NOCOMPONENT, 0 },
214 { "uppercase", UPPERCASE, 0 },
215 { "nouppercase", 0, UPPERCASE },
216 { "center", CENTER, 0 },
217 { "nocenter", 0, CENTER },
218 { "clearscreen", CLEARSCR, 0 },
219 { "noclearscreen", 0, CLEARSCR },
220 { "noclear", 0, CLEARSCR },
221 { "leftadjust", LEFTADJUST, 0 },
222 { "noleftadjust", 0, LEFTADJUST },
223 { "compress", COMPRESS, 0 },
224 { "nocompress", 0, COMPRESS },
225 { "split", SPLIT, 0 },
226 { "nosplit", 0, SPLIT },
227 { "rtrim", RTRIM, 0 },
228 { "nortrim", 0, RTRIM },
229 { "addrfield", ADDRFMT, DATEFMT },
230 { "bell", BELL, 0 },
231 { "nobell", 0, BELL },
232 { "datefield", DATEFMT, ADDRFMT },
233 { "newline", 0, NONEWLINE },
234 { "nonewline", NONEWLINE, 0 },
235 { "wrap", 0, NOWRAP },
236 { "nowrap", NOWRAP, 0 },
237 { "format", FMTFILTER, 0 },
238 { "noformat", 0, FMTFILTER },
239 { NULL, 0, 0 }
240 };
241
242 static char *addrcomps[] = {
243 "from",
244 "sender",
245 "reply-to",
246 "to",
247 "cc",
248 "bcc",
249 "resent-from",
250 "resent-sender",
251 "resent-reply-to",
252 "resent-to",
253 "resent-cc",
254 "resent-bcc",
255 NULL
256 };
257
258
259 static int bellflg = 0;
260 static int clearflg = 0;
261 static int dashstuff = 0;
262 static int dobody = 1;
263 static int forwflg = 0;
264 static int forwall = 0;
265
266 static int sleepsw = NOTOK;
267
268 static char *digest = NULL;
269 static int volume = 0;
270 static int issue = 0;
271
272 static int exitstat = 0;
273 static int mhldebug = 0;
274
275 static int filesize = 0;
276
277 #define PITTY (-1)
278 #define NOTTY 0
279 #define ISTTY 1
280 static int ontty = NOTTY;
281
282 static int row;
283 static unsigned int column;
284
285 static int lm;
286 static int llim;
287 static int ovoff;
288 static int term;
289 static unsigned int wid;
290
291 static char *ovtxt;
292
293 static char *onelp;
294
295 static char *parptr;
296
297 static int num_ignores = 0;
298 static char *ignores[MAXARGS];
299
300 static jmp_buf env;
301 static jmp_buf mhlenv;
302
303 static char delim3[] = /* from forw.c */
304 "\n----------------------------------------------------------------------\n\n";
305 static char delim4[] = "\n------------------------------\n\n";
306
307 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
308
309 /*
310 * prototypes
311 */
312 static void mhl_format (char *, int, int);
313 static int evalvar (struct mcomp *);
314 static int ptoi (char *, int *);
315 static int ptos (char *, char **);
316 static char *parse (void);
317 static void process (char *, char *, int, int);
318 static void mhlfile (FILE *, char *, int, int);
319 static int mcomp_flags (char *);
320 static char *mcomp_add (unsigned long, char *, char *);
321 static void mcomp_format (struct mcomp *, struct mcomp *);
322 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
323 static void free_queue (struct mcomp **, struct mcomp **);
324 static void putcomp (struct mcomp *, struct mcomp *, int);
325 static char *oneline (char *, unsigned long);
326 static void putstr (char *, unsigned long);
327 static void putch (char, unsigned long);
328 static void intrser (int);
329 static void pipeser (int);
330 static void quitser (int);
331 static void mhladios (char *, char *, ...);
332 static void mhldone (int);
333 static void filterbody (struct mcomp *, char *, int, int, FILE *,
334 m_getfld_state_t);
335 static void compile_formatfield(struct mcomp *);
336 static void compile_filterargs (void);
337
338
339 int
340 mhl (int argc, char **argv)
341 {
342 int length = 0, nomore = 0;
343 unsigned int i, vecp = 0;
344 int width = 0;
345 char *cp, *folder = NULL, *form = NULL;
346 char buf[BUFSIZ], *files[MAXARGS];
347 char **argp, **arguments;
348
349 /* Need this if called from main() of show(1). */
350 invo_name = r1bindex (argv[0], '/');
351
352 arguments = getarguments (invo_name, argc, argv, 1);
353 argp = arguments;
354
355 if ((cp = getenv ("MHLDEBUG")) && *cp)
356 mhldebug++;
357
358 while ((cp = *argp++)) {
359 if (*cp == '-') {
360 switch (smatch (++cp, mhlswitches)) {
361 case AMBIGSW:
362 ambigsw (cp, mhlswitches);
363 mhldone (1);
364 case UNKWNSW:
365 mhladios (NULL, "-%s unknown\n", cp);
366
367 case HELPSW:
368 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
369 print_help (buf, mhlswitches, 1);
370 mhldone (0);
371 case VERSIONSW:
372 print_version(invo_name);
373 mhldone (0);
374
375 case BELLSW:
376 bellflg = 1;
377 continue;
378 case NBELLSW:
379 bellflg = -1;
380 continue;
381
382 case CLRSW:
383 clearflg = 1;
384 continue;
385 case NCLRSW:
386 clearflg = -1;
387 continue;
388
389 case FOLDSW:
390 if (!(folder = *argp++) || *folder == '-')
391 mhladios (NULL, "missing argument to %s", argp[-2]);
392 continue;
393 case FORMSW:
394 if (!(form = *argp++) || *form == '-')
395 mhladios (NULL, "missing argument to %s", argp[-2]);
396 continue;
397
398 case SLEEPSW:
399 if (!(cp = *argp++) || *cp == '-')
400 mhladios (NULL, "missing argument to %s", argp[-2]);
401 else
402 sleepsw = atoi (cp);/* ZERO ok! */
403 continue;
404
405 case PROGSW:
406 if (!(moreproc = *argp++) || *moreproc == '-')
407 mhladios (NULL, "missing argument to %s", argp[-2]);
408 continue;
409 case NPROGSW:
410 nomore++;
411 continue;
412
413 case FMTPROCSW:
414 if (!(formatproc = *argp++) || *formatproc == '-')
415 mhladios (NULL, "missing argument to %s", argp[-2]);
416 continue;
417 case NFMTPROCSW:
418 formatproc = NULL;
419 continue;
420
421 case LENSW:
422 if (!(cp = *argp++) || *cp == '-')
423 mhladios (NULL, "missing argument to %s", argp[-2]);
424 else if ((length = atoi (cp)) < 1)
425 mhladios (NULL, "bad argument %s %s", argp[-2], cp);
426 continue;
427 case WIDTHSW:
428 if (!(cp = *argp++) || *cp == '-')
429 mhladios (NULL, "missing argument to %s", argp[-2]);
430 else if ((width = atoi (cp)) < 1)
431 mhladios (NULL, "bad argument %s %s", argp[-2], cp);
432 continue;
433
434 case DGSTSW:
435 if (!(digest = *argp++) || *digest == '-')
436 mhladios (NULL, "missing argument to %s", argp[-2]);
437 continue;
438 case ISSUESW:
439 if (!(cp = *argp++) || *cp == '-')
440 mhladios (NULL, "missing argument to %s", argp[-2]);
441 else if ((issue = atoi (cp)) < 1)
442 mhladios (NULL, "bad argument %s %s", argp[-2], cp);
443 continue;
444 case VOLUMSW:
445 if (!(cp = *argp++) || *cp == '-')
446 mhladios (NULL, "missing argument to %s", argp[-2]);
447 else if ((volume = atoi (cp)) < 1)
448 mhladios (NULL, "bad argument %s %s", argp[-2], cp);
449 continue;
450
451 case FORW2SW:
452 forwall++; /* fall */
453 case FORW1SW:
454 forwflg++;
455 clearflg = -1;/* XXX */
456 continue;
457
458 case BITSTUFFSW:
459 dashstuff = 1; /* trinary logic */
460 continue;
461 case NBITSTUFFSW:
462 dashstuff = -1; /* trinary logic */
463 continue;
464
465 case NBODYSW:
466 dobody = 0;
467 continue;
468 }
469 }
470 files[vecp++] = cp;
471 }
472
473 if (!folder)
474 folder = getenv ("mhfolder");
475
476 if (isatty (fileno (stdout))) {
477 if (!nomore && moreproc && *moreproc != '\0') {
478 if (mhl_action) {
479 SIGNAL (SIGINT, SIG_IGN);
480 SIGNAL2 (SIGQUIT, quitser);
481 }
482 SIGNAL2 (SIGPIPE, pipeser);
483 m_popen (moreproc, mhl_action != NULL);
484 ontty = PITTY;
485 } else {
486 SIGNAL (SIGINT, SIG_IGN);
487 SIGNAL2 (SIGQUIT, quitser);
488 ontty = ISTTY;
489 }
490 } else {
491 ontty = NOTTY;
492 }
493
494 mhl_format (form ? form : mhlformat, length, width);
495
496 if (vecp == 0) {
497 process (folder, NULL, 1, vecp = 1);
498 } else {
499 for (i = 0; i < vecp; i++)
500 process (folder, files[i], i + 1, vecp);
501 }
502
503 if (forwall) {
504 if (digest) {
505 fputs(delim4, stdout);
506 if (volume == 0) {
507 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
508 } else {
509 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
510 digest, volume, issue);
511 }
512 i = strlen (buf);
513 for (cp = buf + i; i > 1; i--)
514 *cp++ = '*';
515 *cp++ = '\n';
516 *cp = 0;
517 fputs(buf, stdout);
518 }
519 else
520 printf ("\n------- End of Forwarded Message%s\n",
521 vecp > 1 ? "s" : "");
522 }
523
524 fflush(stdout);
525 if(ferror(stdout)){
526 mhladios("output", "error writing");
527 }
528
529 if (clearflg > 0 && ontty == NOTTY)
530 nmh_clear_screen ();
531
532 if (ontty == PITTY)
533 m_pclose ();
534
535 return exitstat;
536 }
537
538
539 static void
540 mhl_format (char *file, int length, int width)
541 {
542 int i;
543 char *bp, **ip;
544 char *ap, name[NAMESZ];
545 struct mcomp *c1;
546 struct stat st;
547 FILE *fp;
548 static dev_t dev = 0;
549 static ino_t ino = 0;
550 static time_t mtime = 0;
551
552 if (fmthd != NULL) {
553 if (stat (etcpath (file), &st) != NOTOK
554 && mtime == st.st_mtime
555 && dev == st.st_dev
556 && ino == st.st_ino)
557 goto out;
558 free_queue (&fmthd, &fmttl);
559 }
560
561 if ((fp = fopen (etcpath (file), "r")) == NULL)
562 mhladios (file, "unable to open format file");
563
564 if (fstat (fileno (fp), &st) != NOTOK) {
565 mtime = st.st_mtime;
566 dev = st.st_dev;
567 ino = st.st_ino;
568 }
569
570 global.c_ovtxt = global.c_nfs = NULL;
571 global.c_fmt = NULL;
572 global.c_offset = 0;
573 global.c_ovoff = -1;
574 if ((i = sc_width ()) > 5)
575 global.c_width = i;
576 global.c_cwidth = -1;
577 if ((i = sc_length ()) > 5)
578 global.c_length = i - 1;
579 global.c_flags = BELL; /* BELL is default */
580 *(ip = ignores) = NULL;
581 filter_nargs = 0;
582
583 while (vfgets (fp, &ap) == OK) {
584 bp = ap;
585 if (*bp == ';')
586 continue;
587
588 trim_suffix_c(bp, '\n');
589
590 if (*bp == ':') {
591 (void) add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
592 continue;
593 }
594
595 parptr = bp;
596 strncpy (name, parse(), sizeof(name));
597 switch (*parptr) {
598 case '\0':
599 case ',':
600 case '=':
601 /*
602 * Split this list of fields to ignore, and copy
603 * it to the end of the current "ignores" list.
604 */
605 if (!strcasecmp (name, "ignores")) {
606 char **tmparray, **p;
607 int n = 0;
608
609 /* split the fields */
610 tmparray = brkstring (mh_xstrdup(++parptr), ",", NULL);
611
612 /* count number of fields split */
613 p = tmparray;
614 while (*p++)
615 n++;
616
617 /* copy pointers to split fields to ignores array */
618 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
619 num_ignores += n;
620 continue;
621 }
622 parptr = bp;
623 while (*parptr) {
624 if (evalvar (&global))
625 mhladios (NULL, "format file syntax error: %s", bp);
626 if (*parptr)
627 parptr++;
628 }
629 continue;
630
631 case ':':
632 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
633 while (*parptr == ':' || *parptr == ',') {
634 parptr++;
635 if (evalvar (c1))
636 mhladios (NULL, "format file syntax error: %s", bp);
637 }
638 if (!c1->c_nfs && global.c_nfs) {
639 if (c1->c_flags & DATEFMT) {
640 if (global.c_flags & DATEFMT) {
641 c1->c_nfs = mh_xstrdup(global.c_nfs);
642 compile_formatfield(c1);
643 }
644 }
645 else
646 if (c1->c_flags & ADDRFMT) {
647 if (global.c_flags & ADDRFMT) {
648 c1->c_nfs = mh_xstrdup(global.c_nfs);
649 compile_formatfield(c1);
650 }
651 }
652 }
653 continue;
654
655 default:
656 mhladios (NULL, "format file syntax error: %s", bp);
657 }
658 }
659 fclose (fp);
660
661 if (mhldebug) {
662 for (c1 = fmthd; c1; c1 = c1->c_next) {
663 char buffer[BUFSIZ];
664
665 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
666 c1->c_name, c1->c_text, c1->c_ovtxt);
667 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
668 (unsigned int)(unsigned long) c1->c_nfs,
669 (unsigned int)(unsigned long) c1->c_fmt);
670 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
671 c1->c_offset, c1->c_ovoff, c1->c_width,
672 c1->c_cwidth, c1->c_length);
673 fprintf (stderr, "\tflags=%s\n",
674 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
675 }
676 }
677
678 out:
679 if (clearflg == 1) {
680 global.c_flags |= CLEARSCR;
681 } else {
682 if (clearflg == -1)
683 global.c_flags &= ~CLEARSCR;
684 }
685
686 switch (bellflg) { /* command line may override format file */
687 case 1:
688 global.c_flags |= BELL;
689 break;
690 case -1:
691 global.c_flags &= ~BELL;
692 break;
693 }
694
695 if (length)
696 global.c_length = length;
697 if (width)
698 global.c_width = width;
699 if (global.c_length < 5)
700 global.c_length = 10000;
701 if (global.c_width < 5)
702 global.c_width = 10000;
703 }
704
705
706 static int
707 evalvar (struct mcomp *c1)
708 {
709 char *cp, name[NAMESZ];
710 struct triple *ap;
711
712 if (!*parptr)
713 return 0;
714 strncpy (name, parse(), sizeof(name));
715
716 if (!strcasecmp (name, "component")) {
717 if (ptos (name, &c1->c_text))
718 return 1;
719 c1->c_flags &= ~NOCOMPONENT;
720 return 0;
721 }
722
723 if (!strcasecmp (name, "overflowtext"))
724 return ptos (name, &c1->c_ovtxt);
725
726 if (!strcasecmp (name, "formatfield")) {
727 if (ptos (name, &cp))
728 return 1;
729 c1->c_nfs = getcpy (new_fs (NULL, NULL, cp));
730 compile_formatfield(c1);
731 c1->c_flags |= FORMAT;
732 return 0;
733 }
734
735 if (!strcasecmp (name, "decode")) {
736 c1->c_nfs = getcpy (new_fs (NULL, NULL, "%(decode{text})"));
737 compile_formatfield(c1);
738 c1->c_flags |= FORMAT;
739 return 0;
740 }
741
742 if (!strcasecmp (name, "offset"))
743 return ptoi (name, &c1->c_offset);
744 if (!strcasecmp (name, "overflowoffset"))
745 return ptoi (name, &c1->c_ovoff);
746 if (!strcasecmp (name, "width"))
747 return ptoi (name, &c1->c_width);
748 if (!strcasecmp (name, "compwidth"))
749 return ptoi (name, &c1->c_cwidth);
750 if (!strcasecmp (name, "length"))
751 return ptoi (name, &c1->c_length);
752 if (!strcasecmp (name, "nodashstuffing"))
753 return (dashstuff = -1);
754
755 for (ap = triples; ap->t_name; ap++)
756 if (!strcasecmp (ap->t_name, name)) {
757 c1->c_flags |= ap->t_on;
758 c1->c_flags &= ~ap->t_off;
759 return 0;
760 }
761
762 if (!strcasecmp (name, "formatarg")) {
763 struct arglist *args;
764
765 if (ptos (name, &cp))
766 return 1;
767
768 if (! c1->c_name || strcasecmp (c1->c_name, "body")) {
769 advise (NULL, "format filters are currently only supported on "
770 "the \"body\" component");
771 return 1;
772 }
773
774 NEW0(args);
775
776 if (arglist_tail)
777 arglist_tail->a_next = args;
778
779 arglist_tail = args;
780
781 if (! arglist_head)
782 arglist_head = args;
783
784 args->a_nfs = getcpy (new_fs (NULL, NULL, cp));
785 filter_nargs++;
786
787 return 0;
788 }
789
790 return 1;
791 }
792
793
794 static int
795 ptoi (char *name, int *i)
796 {
797 char *cp;
798
799 if (*parptr++ != '=' || !*(cp = parse ())) {
800 advise (NULL, "missing argument to variable %s", name);
801 return 1;
802 }
803
804 *i = atoi (cp);
805 return 0;
806 }
807
808
809 static int
810 ptos (char *name, char **s)
811 {
812 char c, *cp;
813
814 if (*parptr++ != '=') {
815 advise (NULL, "missing argument to variable %s", name);
816 return 1;
817 }
818
819 if (*parptr != '"') {
820 for (cp = parptr;
821 *parptr && *parptr != ':' && *parptr != ',';
822 parptr++)
823 continue;
824 } else {
825 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
826 if (*parptr == QUOTE)
827 if (!*++parptr)
828 parptr--;
829 }
830 c = *parptr;
831 *parptr = 0;
832 *s = mh_xstrdup(cp);
833 if ((*parptr = c) == '"')
834 parptr++;
835 return 0;
836 }
837
838
839 static char *
840 parse (void)
841 {
842 int c;
843 char *cp;
844 static char result[NAMESZ];
845
846 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
847 c = *parptr;
848 if (isalnum (c)
849 || c == '.'
850 || c == '-'
851 || c == '_'
852 || c =='['
853 || c == ']')
854 *cp++ = c;
855 else
856 break;
857 }
858 *cp = '\0';
859
860 return result;
861 }
862
863
864 /*
865 * Process one file/message
866 */
867
868 static void
869 process (char *folder, char *fname, int ofilen, int ofilec)
870 {
871 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
872 static char *cp;
873 static FILE *fp;
874 struct mcomp *c1;
875 struct stat st;
876 struct arglist *ap;
877 /* volatile to prevent "might be clobbered" warning from gcc: */
878 char *volatile fname2 = fname ? fname : "(stdin)";
879
880 cp = NULL;
881 fp = NULL;
882
883 switch (setjmp (env)) {
884 case OK:
885 if (fname) {
886 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
887 if (fp == NULL) {
888 advise (fname, "unable to open");
889 exitstat++;
890 return;
891 }
892 } else {
893 fp = stdin;
894 }
895 if (fstat(fileno(fp), &st) == 0) {
896 filesize = st.st_size;
897 } else {
898 filesize = 0;
899 }
900 cp = folder ? concat (folder, ":", fname2, NULL) : mh_xstrdup(fname2);
901 if (ontty != PITTY)
902 SIGNAL (SIGINT, intrser);
903 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
904 free (cp);
905
906 for (ap = arglist_head; ap; ap = ap->a_next) {
907 fmt_free(ap->a_fmt, 0);
908 ap->a_fmt = NULL;
909 }
910
911 if (arglist_head)
912 fmt_free(NULL, 1);
913
914 default:
915 if (ontty != PITTY)
916 SIGNAL (SIGINT, SIG_IGN);
917 if (mhl_action == NULL && fp != stdin && fp != NULL)
918 fclose (fp);
919 mh_xfree(holder.c_text);
920 holder.c_text = NULL;
921 free_queue (&msghd, &msgtl);
922 for (c1 = fmthd; c1; c1 = c1->c_next)
923 c1->c_flags &= ~HDROUTPUT;
924 break;
925 }
926
927 }
928
929
930 static void
931 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
932 {
933 int state, bucket;
934 struct mcomp *c1, *c2, *c3;
935 char **ip, name[NAMESZ], buf[BUFSIZ];
936 m_getfld_state_t gstate = 0;
937
938 compile_filterargs();
939
940 if (forwall) {
941 if (digest)
942 fputs(ofilen == 1 ? delim3 : delim4, stdout);
943 else {
944 printf ("\n-------");
945 if (ofilen == 1)
946 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
947 else
948 printf (" Message %d", ofilen);
949 printf ("\n\n");
950 }
951 } else {
952 switch (ontty) {
953 case PITTY:
954 if (ofilec > 1) {
955 if (ofilen > 1) {
956 if ((global.c_flags & CLEARSCR))
957 nmh_clear_screen ();
958 else
959 printf ("\n\n\n");
960 }
961 printf (">>> %s\n\n", mname);
962 }
963 break;
964
965 case ISTTY:
966 strncpy (buf, "\n", sizeof(buf));
967 if (ofilec > 1) {
968 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
969 if (ofilen > 1)
970 printf ("\n\n\n");
971 printf ("Press <return> to list \"%s\"...", mname);
972 }
973 fflush (stdout);
974 buf[0] = 0;
975 if (read (fileno (stdout), buf, sizeof(buf)) < 0) {
976 advise ("stdout", "read");
977 }
978 }
979 if (strchr(buf, '\n')) {
980 if ((global.c_flags & CLEARSCR))
981 nmh_clear_screen ();
982 }
983 else
984 putchar('\n');
985 break;
986
987 default:
988 if (ofilec > 1) {
989 if (ofilen > 1) {
990 printf ("\n\n\n");
991 if (clearflg > 0)
992 nmh_clear_screen ();
993 }
994 printf (">>> %s\n\n", mname);
995 }
996 break;
997 }
998 }
999
1000 for (;;) {
1001 int bufsz = sizeof buf;
1002 switch (state = m_getfld (&gstate, name, buf, &bufsz, fp)) {
1003 case FLD:
1004 case FLDPLUS:
1005 bucket = fmt_addcomptext(name, buf);
1006 for (ip = ignores; *ip; ip++)
1007 if (!strcasecmp (name, *ip)) {
1008 while (state == FLDPLUS) {
1009 bufsz = sizeof buf;
1010 state = m_getfld (&gstate, name, buf, &bufsz, fp);
1011 fmt_appendcomp(bucket, name, buf);
1012 }
1013 break;
1014 }
1015 if (*ip)
1016 continue;
1017
1018 for (c2 = fmthd; c2; c2 = c2->c_next)
1019 if (!strcasecmp (c2->c_name ? c2->c_name : "", name))
1020 break;
1021 c1 = NULL;
1022 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
1023 for (c1 = msghd; c1; c1 = c1->c_next)
1024 if (!strcasecmp (c1->c_name ? c1->c_name : "",
1025 c3->c_name ? c3->c_name : "")) {
1026 c1->c_text =
1027 mcomp_add (c1->c_flags, buf, c1->c_text);
1028 break;
1029 }
1030 if (c1 == NULL)
1031 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
1032 while (state == FLDPLUS) {
1033 bufsz = sizeof buf;
1034 state = m_getfld (&gstate, name, buf, &bufsz, fp);
1035 c1->c_text = add (buf, c1->c_text);
1036 fmt_appendcomp(bucket, name, buf);
1037 }
1038 if (c2 == NULL)
1039 c1->c_flags |= EXTRA;
1040 continue;
1041
1042 case BODY:
1043 case FILEEOF:
1044 row = column = 0;
1045 for (c1 = fmthd; c1; c1 = c1->c_next) {
1046 if (c1->c_flags & CLEARTEXT) {
1047 putcomp (c1, c1, ONECOMP);
1048 continue;
1049 }
1050 if (!c1->c_name ||
1051 !strcasecmp (c1->c_name, "messagename")) {
1052 holder.c_text = concat ("(Message ", mname, ")\n",
1053 NULL);
1054 putcomp (c1, &holder, ONECOMP);
1055 free (holder.c_text);
1056 holder.c_text = NULL;
1057 continue;
1058 }
1059 if (!c1->c_name || !strcasecmp (c1->c_name, "extras")) {
1060 for (c2 = msghd; c2; c2 = c2->c_next)
1061 if (c2->c_flags & EXTRA)
1062 putcomp (c1, c2, TWOCOMP);
1063 continue;
1064 }
1065 if (dobody && (!c1->c_name ||
1066 !strcasecmp (c1->c_name, "body"))) {
1067 if (c1->c_flags & FMTFILTER && state == BODY &&
1068 formatproc != NULL) {
1069 filterbody(c1, buf, sizeof(buf), state, fp, gstate);
1070 } else {
1071 holder.c_text = mh_xmalloc (sizeof(buf));
1072 strncpy (holder.c_text, buf, sizeof(buf));
1073 while (state == BODY) {
1074 putcomp (c1, &holder, BODYCOMP);
1075 bufsz = sizeof buf;
1076 state = m_getfld (&gstate, name, holder.c_text,
1077 &bufsz, fp);
1078 }
1079 free (holder.c_text);
1080 holder.c_text = NULL;
1081 }
1082 continue;
1083 }
1084 for (c2 = msghd; c2; c2 = c2->c_next)
1085 if (!strcasecmp (c2->c_name ? c2->c_name : "",
1086 c1->c_name ? c1->c_name : "")) {
1087 putcomp (c1, c2, ONECOMP);
1088 if (!(c1->c_flags & SPLIT))
1089 break;
1090 }
1091 }
1092 m_getfld_state_destroy (&gstate);
1093 return;
1094
1095 case LENERR:
1096 case FMTERR:
1097 advise (NULL, "format error in message %s", mname);
1098 exitstat++;
1099 m_getfld_state_destroy (&gstate);
1100 return;
1101
1102 default:
1103 mhladios (NULL, "getfld() returned %d", state);
1104 }
1105 }
1106 }
1107
1108
1109 static int
1110 mcomp_flags (char *name)
1111 {
1112 struct pair *ap;
1113
1114 for (ap = pairs; ap->p_name; ap++)
1115 if (!strcasecmp (ap->p_name, name))
1116 return (ap->p_flags);
1117
1118 return 0;
1119 }
1120
1121
1122 static char *
1123 mcomp_add (unsigned long flags, char *s1, char *s2)
1124 {
1125 char *dp;
1126
1127 if (!(flags & ADDRFMT))
1128 return add (s1, s2);
1129
1130 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1131 *dp = 0;
1132
1133 return add (s1, add (",\n", s2));
1134 }
1135
1136
1137 struct pqpair {
1138 char *pq_text;
1139 char *pq_error;
1140 struct pqpair *pq_next;
1141 };
1142
1143
1144 static void
1145 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1146 {
1147 int dat[5];
1148 char *ap, *cp;
1149 char error[BUFSIZ];
1150 struct pqpair *p, *q;
1151 struct pqpair pq;
1152 struct mailname *mp;
1153
1154 ap = c2->c_text;
1155 c2->c_text = NULL;
1156 dat[0] = 0;
1157 dat[1] = 0;
1158 dat[2] = filesize;
1159 dat[3] = BUFSIZ - 1;
1160 dat[4] = 0;
1161
1162 if (!(c1->c_flags & ADDRFMT)) {
1163 charstring_t scanl = charstring_create (BUFSIZ);
1164
1165 if (c1->c_c_text)
1166 c1->c_c_text->c_text = ap;
1167 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1168 if (!cp[1])
1169 *cp = 0;
1170
1171 fmt_scan (c1->c_fmt, scanl, BUFSIZ - 1, dat, NULL);
1172 /* Don't need to append a newline, dctime() already did */
1173 c2->c_text = charstring_buffer_copy (scanl);
1174 charstring_free (scanl);
1175
1176 /* ap is now owned by the component struct, so do NOT free it here */
1177 return;
1178 }
1179
1180 (q = &pq)->pq_next = NULL;
1181 while ((cp = getname (ap))) {
1182 NEW0(p);
1183 if ((mp = getm (cp, NULL, 0, error, sizeof(error))) == NULL) {
1184 p->pq_text = mh_xstrdup(cp);
1185 p->pq_error = mh_xstrdup(error);
1186 } else {
1187 p->pq_text = getcpy (mp->m_text);
1188 mnfree (mp);
1189 }
1190 q = (q->pq_next = p);
1191 }
1192
1193 for (p = pq.pq_next; p; p = q) {
1194 charstring_t scanl = charstring_create (BUFSIZ);
1195 char *buffer;
1196
1197 if (c1->c_c_text) {
1198 c1->c_c_text->c_text = p->pq_text;
1199 p->pq_text = NULL;
1200 }
1201 if (c1->c_c_error) {
1202 c1->c_c_error->c_text = p->pq_error;
1203 p->pq_error = NULL;
1204 }
1205
1206 fmt_scan (c1->c_fmt, scanl, BUFSIZ - 1, dat, NULL);
1207 buffer = charstring_buffer_copy (scanl);
1208 if (*buffer) {
1209 if (c2->c_text)
1210 c2->c_text = add (",\n", c2->c_text);
1211 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1212 *cp = 0;
1213 c2->c_text = add (buffer, c2->c_text);
1214 }
1215 charstring_free (scanl);
1216
1217 mh_xfree(p->pq_text);
1218 mh_xfree(p->pq_error);
1219 q = p->pq_next;
1220 free(p);
1221 }
1222
1223 c2->c_text = add ("\n", c2->c_text);
1224 free (ap);
1225 }
1226
1227
1228 static struct mcomp *
1229 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1230 {
1231 struct mcomp *c1;
1232
1233 NEW0(c1);
1234 c1->c_flags = flags & ~INIT;
1235 if ((c1->c_name = name ? mh_xstrdup(name) : NULL))
1236 c1->c_flags |= mcomp_flags (c1->c_name);
1237 c1->c_text = text ? mh_xstrdup(text) : NULL;
1238 if (flags & INIT) {
1239 if (global.c_ovtxt)
1240 c1->c_ovtxt = mh_xstrdup(global.c_ovtxt);
1241 c1->c_offset = global.c_offset;
1242 c1->c_ovoff = global. c_ovoff;
1243 c1->c_width = c1->c_length = 0;
1244 c1->c_cwidth = global.c_cwidth;
1245 c1->c_flags |= global.c_flags & GFLAGS;
1246 }
1247 if (*head == NULL)
1248 *head = c1;
1249 if (*tail != NULL)
1250 (*tail)->c_next = c1;
1251 *tail = c1;
1252
1253 return c1;
1254 }
1255
1256
1257 static void
1258 free_queue (struct mcomp **head, struct mcomp **tail)
1259 {
1260 struct mcomp *c1, *c2;
1261
1262 for (c1 = *head; c1; c1 = c2) {
1263 c2 = c1->c_next;
1264 mh_xfree(c1->c_name);
1265 mh_xfree(c1->c_text);
1266 mh_xfree(c1->c_ovtxt);
1267 mh_xfree(c1->c_nfs);
1268 if (c1->c_fmt)
1269 fmt_free (c1->c_fmt, 0);
1270 free(c1);
1271 }
1272
1273 *head = *tail = NULL;
1274 }
1275
1276
1277 static void
1278 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1279 {
1280 char *text; /* c1's text, or the name as a fallback. */
1281 char *trimmed_prefix;
1282 int count, cchdr;
1283 char *cp;
1284 const int utf8 = strcasecmp(get_charset(), "UTF-8") == 0;
1285
1286 if (! utf8 && flag != BODYCOMP) {
1287 /* Don't print 8-bit bytes in header field values if not in a
1288 UTF-8 locale, as required by RFC 6532. */
1289 c1->c_flags |= FORCE7BIT;
1290 }
1291
1292 text = c1->c_text ? c1->c_text : c1->c_name;
1293 /* Create a copy with trailing whitespace trimmed, for use with
1294 * blank lines. */
1295 trimmed_prefix = rtrim(add(text, NULL));
1296
1297 cchdr = 0;
1298 lm = 0;
1299 llim = c1->c_length ? c1->c_length : -1;
1300 wid = c1->c_width ? c1->c_width : global.c_width;
1301 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1302 + c1->c_offset;
1303 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1304 ovtxt = "";
1305 if (wid < ovoff + strlen (ovtxt) + 5)
1306 mhladios (NULL, "component: %s width(%d) too small for overflow(%d)",
1307 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1308 onelp = NULL;
1309
1310 if (c1->c_flags & CLEARTEXT) {
1311 putstr (c1->c_flags & RTRIM ? rtrim (c1->c_text) : c1->c_text,
1312 c1->c_flags);
1313 putstr ("\n", c1->c_flags);
1314 return;
1315 }
1316
1317 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1318 mcomp_format (c1, c2);
1319
1320 if (c1->c_flags & CENTER) {
1321 count = (c1->c_width ? c1->c_width : global.c_width)
1322 - c1->c_offset - strlen (c2->c_text);
1323 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1324 count -= strlen(text) + 2;
1325 lm = c1->c_offset + (count / 2);
1326 } else {
1327 if (c1->c_offset)
1328 lm = c1->c_offset;
1329 }
1330
1331 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1332 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1333 to_upper(text);
1334 putstr(text, c1->c_flags);
1335 if (flag != BODYCOMP) {
1336 putstr (": ", c1->c_flags);
1337 if (!(c1->c_flags & SPLIT))
1338 c1->c_flags |= HDROUTPUT;
1339
1340 cchdr++;
1341 if ((count = c1->c_cwidth - strlen(text) - 2) > 0)
1342 while (count--)
1343 putstr (" ", c1->c_flags);
1344 }
1345 else
1346 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1347 }
1348
1349 if (flag == TWOCOMP
1350 && !(c2->c_flags & HDROUTPUT)
1351 && !(c2->c_flags & NOCOMPONENT)) {
1352 if (c1->c_flags & UPPERCASE)
1353 to_upper(c2->c_name);
1354 putstr (c2->c_name, c1->c_flags);
1355 putstr (": ", c1->c_flags);
1356 if (!(c1->c_flags & SPLIT))
1357 c2->c_flags |= HDROUTPUT;
1358
1359 cchdr++;
1360 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1361 while (count--)
1362 putstr (" ", c1->c_flags);
1363 }
1364 if (c1->c_flags & UPPERCASE)
1365 to_upper(c2->c_text);
1366
1367 count = 0;
1368 if (cchdr) {
1369 if (flag == TWOCOMP)
1370 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1371 : (int) strlen (c2->c_name) + 2;
1372 else
1373 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1374 : strlen(text) + 2;
1375 }
1376 count += c1->c_offset;
1377
1378 if ((cp = oneline (c2->c_text, c1->c_flags)))
1379 /* Output line, trimming trailing whitespace if requested. */
1380 putstr (c1->c_flags & RTRIM ? rtrim (cp) : cp, c1->c_flags);
1381 if (term == '\n')
1382 putstr ("\n", c1->c_flags);
1383 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1384 lm = count;
1385 if (flag == BODYCOMP
1386 && !(c1->c_flags & NOCOMPONENT)) {
1387 /* Output component, trimming trailing whitespace if there
1388 is no text on the line. */
1389 if (*cp) {
1390 putstr(text, c1->c_flags);
1391 } else {
1392 putstr (trimmed_prefix, c1->c_flags);
1393 }
1394 }
1395 if (*cp) {
1396 /* Output line, trimming trailing whitespace if requested. */
1397 putstr (c1->c_flags & RTRIM ? rtrim (cp) : cp, c1->c_flags);
1398 }
1399 if (term == '\n')
1400 putstr ("\n", c1->c_flags);
1401 }
1402 if (flag == BODYCOMP && term == '\n')
1403 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1404
1405 free (trimmed_prefix);
1406 }
1407
1408
1409 static char *
1410 oneline (char *stuff, unsigned long flags)
1411 {
1412 int spc;
1413 char *cp, *ret;
1414
1415 if (onelp == NULL)
1416 onelp = stuff;
1417 if (*onelp == 0)
1418 return (onelp = NULL);
1419
1420 ret = onelp;
1421 term = 0;
1422 if (flags & COMPRESS) {
1423 for (spc = 1, cp = ret; *onelp; onelp++)
1424 if (isspace ((unsigned char) *onelp)) {
1425 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1426 term = '\n';
1427 *onelp++ = 0;
1428 break;
1429 }
1430 else
1431 if (!spc) {
1432 *cp++ = ' ';
1433 spc++;
1434 }
1435 }
1436 else {
1437 *cp++ = *onelp;
1438 spc = 0;
1439 }
1440
1441 *cp = 0;
1442 }
1443 else {
1444 while (*onelp && *onelp != '\n')
1445 onelp++;
1446 if (*onelp == '\n') {
1447 term = '\n';
1448 *onelp++ = 0;
1449 }
1450 if (flags & LEFTADJUST)
1451 while (*ret == ' ' || *ret == '\t')
1452 ret++;
1453 }
1454 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1455 term = 0;
1456
1457 return ret;
1458 }
1459
1460
1461 static void
1462 putstr (char *string, unsigned long flags)
1463 {
1464 /* To not count, for the purpose of counting columns, all of
1465 the bytes of a multibyte character. */
1466 int char_len;
1467
1468 if (!column && lm > 0) {
1469 while (lm > 0)
1470 if (lm >= 8) {
1471 putch ('\t', flags);
1472 lm -= 8;
1473 }
1474 else {
1475 putch (' ', flags);
1476 lm--;
1477 }
1478 }
1479 lm = 0;
1480
1481 #ifdef MULTIBYTE_SUPPORT
1482 if (mbtowc (NULL, NULL, 0)) {} /* reset shift state */
1483 char_len = 0;
1484 #else
1485 NMH_UNUSED (char_len);
1486 #endif
1487
1488 while (*string) {
1489 flags &= ~INVISIBLE;
1490 #ifdef MULTIBYTE_SUPPORT
1491 /* mbtowc should never return 0, because *string is non-NULL. */
1492 if (char_len <= 0) {
1493 /* Find number of bytes in next character. */
1494 if ((char_len =
1495 mbtowc (NULL, string, (size_t) MB_CUR_MAX)) == -1) {
1496 char_len = 1;
1497 }
1498 } else {
1499 /* Multibyte character, after the first byte. */
1500 flags |= INVISIBLE;
1501 }
1502
1503 --char_len;
1504 #endif
1505 putch (*string++, flags);
1506 }
1507 }
1508
1509
1510 static void
1511 putch (char ch, unsigned long flags)
1512 {
1513 char buf[BUFSIZ];
1514
1515 if (llim == 0)
1516 return;
1517
1518 switch (ch) {
1519 case '\n':
1520 if (llim > 0)
1521 llim--;
1522 column = 0;
1523 row++;
1524 if (ontty != ISTTY || row != global.c_length)
1525 break;
1526 if (global.c_flags & BELL)
1527 putchar ('\007');
1528 fflush (stdout);
1529 buf[0] = 0;
1530 if (read (fileno (stdout), buf, sizeof(buf)) < 0) {
1531 advise ("stdout", "read");
1532 }
1533 if (strchr(buf, '\n')) {
1534 if (global.c_flags & CLEARSCR)
1535 nmh_clear_screen ();
1536 row = 0;
1537 } else {
1538 putchar ('\n');
1539 row = global.c_length / 3;
1540 }
1541 return;
1542
1543 case '\t':
1544 column |= 07;
1545 column++;
1546 break;
1547
1548 case '\b':
1549 column--;
1550 break;
1551
1552 case '\r':
1553 column = 0;
1554 break;
1555
1556 default:
1557 /*
1558 * If we are forwarding this message, and the first
1559 * column contains a dash, then add a dash and a space.
1560 */
1561 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1562 putchar ('-');
1563 putchar (' ');
1564 }
1565 /*
1566 * Increment the character count, unless
1567 * 1) In UTF-8 locale, this is other than the last byte of
1568 a multibyte character, or
1569 * 2) In C locale, will print a non-printable character.
1570 */
1571 if ((flags & FORCE7BIT) == 0) {
1572 /* UTF-8 locale */
1573 if ((flags & INVISIBLE) == 0) {
1574 /* If multibyte character, its first byte only. */
1575 ++column;
1576 }
1577 } else {
1578 /* If not an ASCII character, the replace character will be
1579 displayed. Count it. */
1580 if (! isascii((unsigned char) ch) || isprint((unsigned char) ch)) {
1581 ++column;
1582 }
1583 }
1584 break;
1585 }
1586
1587 if (column >= wid && (flags & NOWRAP) == 0) {
1588 putch ('\n', flags);
1589 if (ovoff > 0)
1590 lm = ovoff;
1591 putstr (ovtxt ? ovtxt : "", flags);
1592 putch (ch, flags);
1593 return;
1594 }
1595
1596 if (flags & FORCE7BIT && ! isascii((unsigned char) ch)) {
1597 putchar ('?');
1598 } else {
1599 putchar (ch);
1600 }
1601 }
1602
1603
1604 static void
1605 intrser (int i)
1606 {
1607 NMH_UNUSED (i);
1608
1609 discard (stdout);
1610 putchar ('\n');
1611 longjmp (env, DONE);
1612 }
1613
1614
1615 static void
1616 pipeser (int i)
1617 {
1618 NMH_UNUSED (i);
1619
1620 mhldone (NOTOK);
1621 }
1622
1623
1624 static void
1625 quitser (int i)
1626 {
1627 NMH_UNUSED (i);
1628
1629 putchar ('\n');
1630 fflush (stdout);
1631 mhldone (NOTOK);
1632 }
1633
1634
1635 static void
1636 mhladios (char *what, char *fmt, ...)
1637 {
1638 va_list ap;
1639
1640 va_start(ap, fmt);
1641 advertise (what, NULL, fmt, ap);
1642 va_end(ap);
1643 mhldone (1);
1644 }
1645
1646
1647 static void
1648 mhldone (int status)
1649 {
1650 exitstat = status;
1651 if (mhl_action)
1652 longjmp (mhlenv, DONE);
1653 else
1654 done (exitstat);
1655 }
1656
1657
1658 /*
1659 * Compile a format string used by the formatfield option and save it
1660 * for later.
1661 *
1662 * We will want the {text} (and possibly {error}) components for later,
1663 * so look for them and save them if we find them.
1664 */
1665
1666 static void
1667 compile_formatfield(struct mcomp *c1)
1668 {
1669 fmt_compile(c1->c_nfs, &c1->c_fmt, 1);
1670
1671 /*
1672 * As a note to myself and any other poor bastard who is looking through
1673 * this code in the future ....
1674 *
1675 * When the format hash table is reset later on (as it almost certainly
1676 * will be), there will still be references to these components in the
1677 * compiled format instructions. Thus these component references will
1678 * be free'd when the format instructions are free'd (by fmt_free()).
1679 *
1680 * So, in other words ... don't go free'ing them yourself!
1681 */
1682
1683 c1->c_c_text = fmt_findcomp("text");
1684 c1->c_c_error = fmt_findcomp("error");
1685 }
1686
1687 /*
1688 * Compile all of the arguments for our format list.
1689 *
1690 * Iterate through the linked list of format strings and compile them.
1691 * Note that we reset the format hash table before we start, but we do NOT
1692 * reset it between calls to fmt_compile().
1693 *
1694 */
1695
1696 static void
1697 compile_filterargs (void)
1698 {
1699 struct arglist *arg = arglist_head;
1700 struct comp *cptr;
1701 char **ap;
1702
1703 fmt_free(NULL, 1);
1704
1705 while (arg) {
1706 fmt_compile(arg->a_nfs, &arg->a_fmt, 0);
1707 arg = arg->a_next;
1708 }
1709
1710 /*
1711 * Search through and mark any components that are address components
1712 */
1713
1714 for (ap = addrcomps; *ap; ap++) {
1715 cptr = fmt_findcomp (*ap);
1716 if (cptr)
1717 cptr->c_type |= CT_ADDR;
1718 }
1719 }
1720
1721 /*
1722 * Filter the body of a message through a specified format program
1723 */
1724
1725 static void
1726 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp,
1727 m_getfld_state_t gstate)
1728 {
1729 struct mcomp holder;
1730 char name[NAMESZ];
1731 int fdinput[2], fdoutput[2], waitstat;
1732 ssize_t cc;
1733 pid_t writerpid, filterpid;
1734
1735 /*
1736 * Create pipes so we can communicate with our filter process.
1737 */
1738
1739 if (pipe(fdinput) < 0) {
1740 adios(NULL, "Unable to create input pipe");
1741 }
1742
1743 if (pipe(fdoutput) < 0) {
1744 adios(NULL, "Unable to create output pipe");
1745 }
1746
1747 /*
1748 * Here's what we're doing to do.
1749 *
1750 * - Fork ourselves and start writing data to the write side of the
1751 * input pipe (fdinput[1]).
1752 *
1753 * - Fork and exec our filter program. We set the standard input of
1754 * our filter program to be the read side of our input pipe (fdinput[0]).
1755 * Standard output is set to the write side of our output pipe
1756 * (fdoutput[1]).
1757 *
1758 * - We read from the read side of the output pipe (fdoutput[0]).
1759 *
1760 * We're forking because that's the simplest way to prevent any deadlocks.
1761 * (without doing something like switching to non-blocking I/O and using
1762 * select or poll, and I'm not interested in doing that).
1763 */
1764
1765 switch (writerpid = fork()) {
1766 case 0:
1767 /*
1768 * Our child process - just write to the filter input (fdinput[1]).
1769 * Close all other descriptors that we don't need.
1770 */
1771
1772 close(fdinput[0]);
1773 close(fdoutput[0]);
1774 close(fdoutput[1]);
1775
1776 /*
1777 * Call m_getfld() until we're no longer in the BODY state
1778 */
1779
1780 while (state == BODY) {
1781 int bufsz2 = bufsz;
1782 if (write(fdinput[1], buf, strlen(buf)) < 0) {
1783 advise ("pipe output", "write");
1784 }
1785 state = m_getfld (&gstate, name, buf, &bufsz2, fp);
1786 }
1787
1788 /*
1789 * We should be done; time to exit.
1790 */
1791
1792 close(fdinput[1]);
1793 /*
1794 * Make sure we call _exit(), otherwise we may flush out the stdio
1795 * buffers that we have duplicated from the parent.
1796 */
1797 _exit(0);
1798 case -1:
1799 adios(NULL, "Unable to fork for filter writer process");
1800 break;
1801 }
1802
1803 /*
1804 * Fork and exec() our filter program, after redirecting standard in
1805 * and standard out appropriately.
1806 */
1807
1808 switch (filterpid = fork()) {
1809 char **args, *program;
1810 struct arglist *a;
1811 int i, dat[5], s, argp;
1812
1813 case 0:
1814 /*
1815 * Configure an argument array for us
1816 */
1817
1818 args = argsplit(formatproc, &program, &argp);
1819 args[argp + filter_nargs] = NULL;
1820 dat[0] = 0;
1821 dat[1] = 0;
1822 dat[2] = 0;
1823 dat[3] = BUFSIZ;
1824 dat[4] = 0;
1825
1826 /*
1827 * Pull out each argument and scan them.
1828 */
1829
1830 for (a = arglist_head, i = argp; a != NULL; a = a->a_next, i++) {
1831 charstring_t scanl = charstring_create (BUFSIZ);
1832
1833 fmt_scan(a->a_fmt, scanl, BUFSIZ, dat, NULL);
1834 args[i] = charstring_buffer_copy (scanl);
1835 charstring_free (scanl);
1836 /*
1837 * fmt_scan likes to put a trailing newline at the end of the
1838 * format string. If we have one, get rid of it.
1839 */
1840 s = strlen(args[i]);
1841 if (args[i][s - 1] == '\n')
1842 args[i][s - 1] = '\0';
1843
1844 if (mhldebug)
1845 fprintf(stderr, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1846 a->a_nfs, args[i]);
1847 }
1848
1849 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
1850 adios("formatproc", "Unable to dup2() standard input");
1851 }
1852 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
1853 adios("formatproc", "Unable to dup2() standard output");
1854 }
1855
1856 /*
1857 * Close everything (especially the old input and output
1858 * descriptors, since they've been dup'd to stdin and stdout),
1859 * and exec the formatproc.
1860 */
1861
1862 close(fdinput[0]);
1863 close(fdinput[1]);
1864 close(fdoutput[0]);
1865 close(fdoutput[1]);
1866
1867 execvp(formatproc, args);
1868
1869 adios(formatproc, "Unable to execute filter");
1870
1871 break;
1872
1873 case -1:
1874 adios(NULL, "Unable to fork format program");
1875 }
1876
1877 /*
1878 * Close everything except our reader (fdoutput[0]);
1879 */
1880
1881 close(fdinput[0]);
1882 close(fdinput[1]);
1883 close(fdoutput[1]);
1884
1885 /*
1886 * As we read in this data, send it to putcomp
1887 */
1888
1889 holder.c_text = buf;
1890
1891 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
1892 buf[cc] = '\0';
1893 putcomp(c1, &holder, BODYCOMP);
1894 }
1895
1896 if (cc < 0) {
1897 adios(NULL, "reading from formatproc");
1898 }
1899
1900 /*
1901 * See if we got any errors along the way. I'm a little leery of calling
1902 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1903 */
1904
1905 if (waitpid(filterpid, &waitstat, 0) < 0) {
1906 if (errno != ECHILD) {
1907 adios("filterproc", "Unable to determine status");
1908 }
1909 } else {
1910 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1911 pidstatus(waitstat, stderr, "filterproc");
1912 }
1913 }
1914
1915 if (waitpid(writerpid, &waitstat, 0) < 0) {
1916 if (errno != ECHILD) {
1917 adios("writer process", "Unable to determine status");
1918 done(1);
1919 }
1920 } else {
1921 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1922 pidstatus(waitstat, stderr, "writer process");
1923 done(1);
1924 }
1925 }
1926
1927 close(fdoutput[0]);
1928 }