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