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