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