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