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