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