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