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