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