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