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