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