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