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