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