Removed ``Face'' support in mhl.
[mmh] / uip / mhlsbr.c
1 /*
2 ** mhlsbr.c -- main routines for nmh message lister
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <h/tws.h>
14 #include <h/utils.h>
15 #include <setjmp.h>
16 #include <signal.h>
17
18 /*
19 ** MAJOR BUG:
20 ** for a component containing addresses, ADDRFMT, if COMPRESS is also
21 ** set, then addresses get split wrong (not at the spaces between commas).
22 ** To fix this correctly, putstr() should know about "atomic" strings that
23 ** must NOT be broken across lines.  That's too difficult for right now
24 ** (it turns out that there are a number of degernate cases), so in
25 ** oneline(), instead of
26 **
27 **       (*onelp == '\n' && !onelp[1])
28 **
29 ** being a terminating condition,
30 **
31 **       (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
32 **
33 ** is used instead.  This cuts the line prematurely, and gives us a much
34 ** better chance of getting things right.
35 */
36
37 #define ONECOMP  0
38 #define TWOCOMP  1
39 #define BODYCOMP 2
40
41 #define QUOTE  '\\'
42
43 static struct swit mhlswitches[] = {
44 #define BELLSW  0
45         { "bell", 0 },
46 #define NBELLSW  1
47         { "nobell", 0 },
48 #define CLRSW  2
49         { "clear", 0 },
50 #define NCLRSW  3
51         { "noclear", 0 },
52 #define FOLDSW  4
53         { "folder +folder", 0 },
54 #define FORMSW  5
55         { "form formfile", 0 },
56 #define PROGSW  6
57         { "moreproc program", 0 },
58 #define NPROGSW  7
59         { "nomoreproc", 0 },
60 #define LENSW  8
61         { "length lines", 0 },
62 #define WIDTHSW  9
63         { "width columns", 0 },
64 #define SLEEPSW  10
65         { "sleep seconds",  0 },
66 #define BITSTUFFSW  11
67         { "dashstuffing", -12 },  /* interface from forw */
68 #define NBITSTUFFSW   12
69         { "nodashstuffing", -14 },  /* interface from forw */
70 #define VERSIONSW  13
71         { "version", 0 },
72 #define HELPSW  14
73         { "help", 0 },
74 #define FORW1SW  15
75         { "forward", -7 },  /* interface from forw */
76 #define FORW2SW  16
77         { "forwall", -7 },  /* interface from forw */
78 #define DGSTSW  17
79         { "digest list", -6 },
80 #define VOLUMSW  18
81         { "volume number", -6 },
82 #define ISSUESW  19
83         { "issue number", -5 },
84 #define NBODYSW  20
85         { "nobody", -6 },
86         { NULL, 0 }
87 };
88
89 #define NOCOMPONENT 0x000001  /* don't show component name   */
90 #define UPPERCASE   0x000002  /* display in all upper case   */
91 #define CENTER      0x000004  /* center line                 */
92 #define CLEARTEXT   0x000008  /* cleartext                   */
93 #define EXTRA       0x000010  /* an "extra" component        */
94 #define HDROUTPUT   0x000020  /* already output              */
95 #define CLEARSCR    0x000040  /* clear screen                */
96 #define LEFTADJUST  0x000080  /* left justify multiple lines */
97 #define COMPRESS    0x000100  /* compress text               */
98 #define ADDRFMT     0x000200  /* contains addresses          */
99 #define BELL        0x000400  /* sound bell at EOP           */
100 #define DATEFMT     0x000800  /* contains dates              */
101 #define FORMAT      0x001000  /* parse address/date/RFC-2047 field */
102 #define INIT        0x002000  /* initialize component        */
103 #define SPLIT       0x004000  /* split headers (don't concatenate) */
104 #define NONEWLINE   0x008000  /* don't write trailing newline */
105 #define LBITS       "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017SPLIT\020NONEWLINE"
106 #define GFLAGS      (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
107
108 struct mcomp {
109         char *c_name;  /* component name */
110         char *c_text;  /* component text */
111         char *c_ovtxt; /* text overflow indicator */
112         char *c_nfs;   /* iff FORMAT */
113         struct format *c_fmt;  /*  .. */
114         int c_offset;  /* left margin indentation */
115         int c_ovoff;   /* overflow indentation */
116         int c_width;   /* width of field */
117         int c_cwidth;  /* width of component */
118         int c_length;  /* length in lines */
119         long c_flags;
120         struct mcomp *c_next;
121 };
122
123 static struct mcomp *msghd = NULL;
124 static struct mcomp *msgtl = NULL;
125 static struct mcomp *fmthd = NULL;
126 static struct mcomp *fmttl = NULL;
127
128 static struct mcomp global = {
129         NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
130 };
131
132 static struct mcomp holder = {
133         NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
134 };
135
136 struct pair {
137         char *p_name;
138         long p_flags;
139 };
140
141 static struct pair pairs[] = {
142         { "Date", DATEFMT },
143         { "From", ADDRFMT },
144         { "Sender", ADDRFMT },
145         { "Reply-To", ADDRFMT },
146         { "To", ADDRFMT },
147         { "Cc", ADDRFMT },
148         { "Bcc", ADDRFMT },
149         { "Resent-Date", DATEFMT },
150         { "Resent-From", ADDRFMT },
151         { "Resent-Sender", ADDRFMT },
152         { "Resent-Reply-To", ADDRFMT },
153         { "Resent-To", ADDRFMT },
154         { "Resent-Cc", ADDRFMT },
155         { "Resent-Bcc", ADDRFMT },
156         { NULL, 0 }
157 };
158
159 struct triple {
160         char *t_name;
161         long t_on;
162         long t_off;
163 };
164
165 static struct triple triples[] = {
166         { "nocomponent",  NOCOMPONENT, 0 },
167         { "uppercase", UPPERCASE, 0 },
168         { "nouppercase", 0, UPPERCASE },
169         { "center", CENTER, 0 },
170         { "nocenter", 0, CENTER },
171         { "clearscreen", CLEARSCR, 0 },
172         { "noclearscreen", 0, CLEARSCR },
173         { "noclear", 0, CLEARSCR },
174         { "leftadjust", LEFTADJUST, 0 },
175         { "noleftadjust", 0, LEFTADJUST },
176         { "compress", COMPRESS, 0 },
177         { "nocompress", 0, COMPRESS },
178         { "split", SPLIT, 0 },
179         { "nosplit", 0, SPLIT },
180         { "addrfield", ADDRFMT, DATEFMT },
181         { "bell", BELL, 0 },
182         { "nobell", 0, BELL },
183         { "datefield", DATEFMT, ADDRFMT },
184         { "newline", 0, NONEWLINE },
185         { "nonewline", NONEWLINE, 0 },
186         { NULL, 0, 0 }
187 };
188
189
190 static int bellflg   = 0;
191 static int clearflg  = 0;
192 static int dashstuff = 0;
193 static int dobody    = 1;
194 static int forwflg   = 0;
195 static int forwall   = 0;
196
197 static int sleepsw = NOTOK;
198
199 static char *digest = NULL;
200 static int volume = 0;
201 static int issue = 0;
202
203 static int exitstat = 0;
204 static int mhldebug = 0;
205
206 #define PITTY  (-1)
207 #define NOTTY  0
208 #define ISTTY  1
209 static int ontty = NOTTY;
210
211 static int row;
212 static int column;
213
214 static int lm;
215 static int llim;
216 static int ovoff;
217 static int term;
218 static int wid;
219
220 static char *ovtxt;
221
222 static unsigned char *onelp;
223
224 static char *parptr;
225
226 static int num_ignores = 0;
227 static char *ignores[MAXARGS];
228
229 static  jmp_buf env;
230 static  jmp_buf mhlenv;
231
232 static char delim3[] = /* from forw.c */
233         "\n----------------------------------------------------------------------\n\n";
234 static char delim4[] = "\n------------------------------\n\n";
235
236 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
237
238
239 /*
240 ** Redefine a couple of functions.
241 ** These are undefined later in the code.
242 */
243 #define adios mhladios
244 #define done  mhldone
245
246 /*
247 ** prototypes
248 */
249 static void mhl_format(char *, int, int);
250 static int evalvar(struct mcomp *);
251 static int ptoi(char *, int *);
252 static int ptos(char *, char **);
253 static char *parse(void);
254 static void process(char *, char *, int, int);
255 static void mhlfile(FILE *, char *, int, int);
256 static int mcomp_flags(char *);
257 static char *mcomp_add(long, char *, char *);
258 static void mcomp_format(struct mcomp *, struct mcomp *);
259 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
260                 char *, char *, int);
261 static void free_queue(struct mcomp **, struct mcomp **);
262 static void putcomp(struct mcomp *, struct mcomp *, int);
263 static char *oneline(char *, long);
264 static void putstr(char *);
265 static void putch(char);
266 static RETSIGTYPE intrser(int);
267 static RETSIGTYPE pipeser(int);
268 static RETSIGTYPE quitser(int);
269 static void mhladios(char *, char *, ...);
270 static void mhldone(int);
271 static void m_popen(char *);
272
273 int mhl(int, char **);
274 int mhlsbr(int, char **, FILE *(*)());
275 void m_pclose(void);
276
277 void clear_screen(void);  /* from termsbr.c */
278 int SOprintf(char *, ...);  /* from termsbr.c */
279 int sc_width(void);  /* from termsbr.c */
280 int sc_length(void);  /* from termsbr.c */
281 int sc_hardcopy(void);  /* from termsbr.c */
282
283
284 int
285 mhl(int argc, char **argv)
286 {
287         int length = 0, nomore = 0;
288         int i, width = 0, vecp = 0;
289         char *cp, *folder = NULL, *form = NULL;
290         char buf[BUFSIZ], *files[MAXARGS];
291         char **argp, **arguments;
292
293         invo_name = mhbasename(argv[0]);
294
295         /* read user profile/context */
296         context_read();
297
298         arguments = getarguments(invo_name, argc, argv, 1);
299         argp = arguments;
300
301         if ((cp = getenv("MHLDEBUG")) && *cp)
302                 mhldebug++;
303
304         while ((cp = *argp++)) {
305                 if (*cp == '-') {
306                         switch (smatch(++cp, mhlswitches)) {
307                                 case AMBIGSW:
308                                         ambigsw(cp, mhlswitches);
309                                         done(1);
310                                 case UNKWNSW:
311                                         adios(NULL, "-%s unknown\n", cp);
312
313                                 case HELPSW:
314                                         snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
315                                         print_help(buf, mhlswitches, 1);
316                                         done(1);
317                                 case VERSIONSW:
318                                         print_version(invo_name);
319                                         done(1);
320
321                                 case BELLSW:
322                                         bellflg = 1;
323                                         continue;
324                                 case NBELLSW:
325                                         bellflg = -1;
326                                         continue;
327
328                                 case CLRSW:
329                                         clearflg = 1;
330                                         continue;
331                                 case NCLRSW:
332                                         clearflg = -1;
333                                         continue;
334
335                                 case FOLDSW:
336                                         if (!(folder = *argp++) ||
337                                                         *folder == '-')
338                                                 adios(NULL, "missing argument to %s", argp[-2]);
339                                         continue;
340                                 case FORMSW:
341                                         if (!(form = *argp++) || *form == '-')
342                                                 adios(NULL, "missing argument to %s", argp[-2]);
343                                         continue;
344
345                                 case SLEEPSW:
346                                         if (!(cp = *argp++) || *cp == '-')
347                                                 adios(NULL, "missing argument to %s", argp[-2]);
348                                         sleepsw = atoi(cp);  /* ZERO ok! */
349                                         continue;
350
351                                 case PROGSW:
352                                         if (!(moreproc = *argp++) || *moreproc == '-')
353                                                 adios(NULL, "missing argument to %s", argp[-2]);
354                                         continue;
355                                 case NPROGSW:
356                                         nomore++;
357                                         continue;
358
359                                 case LENSW:
360                                         if (!(cp = *argp++) || *cp == '-')
361                                                 adios(NULL, "missing argument to %s", argp[-2]);
362                                         if ((length = atoi(cp)) < 1)
363                                                 adios(NULL, "bad argument %s %s", argp[-2], cp);
364                                         continue;
365                                 case WIDTHSW:
366                                         if (!(cp = *argp++) || *cp == '-')
367                                                 adios(NULL, "missing argument to %s", argp[-2]);
368                                         if ((width = atoi(cp)) < 1)
369                                                 adios(NULL, "bad argument %s %s", argp[-2], cp);
370                                         continue;
371
372                                 case DGSTSW:
373                                         if (!(digest = *argp++) ||
374                                                         *digest == '-')
375                                                 adios(NULL, "missing argument to %s", argp[-2]);
376                                         continue;
377                                 case ISSUESW:
378                                         if (!(cp = *argp++) || *cp == '-')
379                                                 adios(NULL, "missing argument to %s", argp[-2]);
380                                         if ((issue = atoi(cp)) < 1)
381                                                 adios(NULL, "bad argument %s %s", argp[-2], cp);
382                                         continue;
383                                 case VOLUMSW:
384                                         if (!(cp = *argp++) || *cp == '-')
385                                                 adios(NULL, "missing argument to %s", argp[-2]);
386                                         if ((volume = atoi(cp)) < 1)
387                                                 adios(NULL, "bad argument %s %s", argp[-2], cp);
388                                         continue;
389
390                                 case FORW2SW:
391                                         forwall++;  /* fall */
392                                 case FORW1SW:
393                                         forwflg++;
394                                         clearflg = -1;/* XXX */
395                                         continue;
396
397                                 case BITSTUFFSW:
398                                         dashstuff = 1;  /* trinary logic */
399                                         continue;
400                                 case NBITSTUFFSW:
401                                         dashstuff = -1;  /* trinary logic */
402                                         continue;
403
404                                 case NBODYSW:
405                                         dobody = 0;
406                                         continue;
407                         }
408                 }
409                 files[vecp++] = cp;
410         }
411
412         if (!folder)
413                 folder = getenv("mhfolder");
414
415         if (isatty(fileno(stdout))) {
416                 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
417                         if (mhl_action) {
418                                 SIGNAL(SIGINT, SIG_IGN);
419                                 SIGNAL2(SIGQUIT, quitser);
420                         }
421                         SIGNAL2(SIGPIPE, pipeser);
422                         m_popen(moreproc);
423                         ontty = PITTY;
424                 } else {
425                         SIGNAL(SIGINT, SIG_IGN);
426                         SIGNAL2(SIGQUIT, quitser);
427                         ontty = ISTTY;
428                 }
429         } else {
430                 ontty = NOTTY;
431         }
432
433         mhl_format(form ? form : mhlformat, length, width);
434
435         if (vecp == 0) {
436                 process(folder, NULL, 1, vecp = 1);
437         } else {
438                 for (i = 0; i < vecp; i++)
439                         process(folder, files[i], i + 1, vecp);
440         }
441
442         if (forwall) {
443                 if (digest) {
444                         printf("%s", delim4);
445                         if (volume == 0) {
446                                 snprintf(buf, sizeof(buf),
447                                         "End of %s Digest\n", digest);
448                         } else {
449                                 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
450                         }
451                         i = strlen(buf);
452                         for (cp = buf + i; i > 1; i--)
453                                 *cp++ = '*';
454                         *cp++ = '\n';
455                         *cp = 0;
456                         printf("%s", buf);
457                 } else
458                         printf("\n------- End of Forwarded Message%s\n\n",
459                                 vecp > 1 ? "s" : "");
460         }
461
462         fflush(stdout);
463         if (ferror(stdout)) {
464                 adios("output", "error writing");
465         }
466
467         if (clearflg > 0 && ontty == NOTTY)
468                 clear_screen();
469
470         if (ontty == PITTY)
471                 m_pclose();
472
473         return exitstat;
474 }
475
476
477 static void
478 mhl_format(char *file, int length, int width)
479 {
480         int i;
481         char *bp, *cp, **ip;
482         char *ap, buffer[BUFSIZ], name[NAMESZ];
483         struct mcomp *c1;
484         struct stat st;
485         FILE *fp;
486         static dev_t dev = 0;
487         static ino_t ino = 0;
488         static time_t mtime = 0;
489
490         if (fmthd != NULL) {
491                 if (stat(etcpath(file), &st) != NOTOK
492                         && mtime == st.st_mtime
493                         && dev == st.st_dev
494                         && ino == st.st_ino)
495                         goto out;
496                 else
497                         free_queue(&fmthd, &fmttl);
498         }
499
500         if ((fp = fopen(etcpath(file), "r")) == NULL)
501                 adios(file, "unable to open format file");
502
503         if (fstat(fileno(fp), &st) != NOTOK) {
504                 mtime = st.st_mtime;
505                 dev = st.st_dev;
506                 ino = st.st_ino;
507         }
508
509         global.c_ovtxt = global.c_nfs = NULL;
510         global.c_fmt = NULL;
511         global.c_offset = 0;
512         global.c_ovoff = -1;
513         if ((i = sc_width()) > 5)
514                 global.c_width = i;
515         global.c_cwidth = -1;
516         if ((i = sc_length()) > 5)
517                 global.c_length = i - 1;
518         global.c_flags = BELL;  /* BELL is default */
519         *(ip = ignores) = NULL;
520
521         while (vfgets(fp, &ap) == OK) {
522                 bp = ap;
523                 if (*bp == ';')
524                         continue;
525
526                 if ((cp = strchr(bp, '\n')))
527                         *cp = 0;
528
529                 if (*bp == ':') {
530                         c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
531                                         CLEARTEXT);
532                         continue;
533                 }
534
535                 parptr = bp;
536                 strncpy(name, parse(), sizeof(name));
537                 switch(*parptr) {
538                         case '\0':
539                         case ',':
540                         case '=':
541                                 /*
542                                 ** Split this list of fields to ignore,
543                                 ** and copy it to the end of the current
544                                 ** "ignores" list.
545                                 */
546                                 if (!mh_strcasecmp(name, "ignores")) {
547                                         char **tmparray, **p;
548                                         int n = 0;
549
550                                         /* split the fields */
551                                         tmparray = brkstring(getcpy(++parptr),
552                                                         ",", NULL);
553
554                                         /* count number of fields split */
555                                         p = tmparray;
556                                         while (*p++)
557                                                 n++;
558
559                                         /*
560                                         ** copy pointers to split fields
561                                         ** to ignores array
562                                         */
563                                         ip = copyip(tmparray, ip,
564                                                         MAXARGS - num_ignores);
565                                         num_ignores += n;
566                                         continue;
567                                 }
568                                 parptr = bp;
569                                 while (*parptr) {
570                                         if (evalvar(&global))
571                                                 adios(NULL, "format file syntax error: %s", bp);
572                                         if (*parptr)
573                                                 parptr++;
574                                 }
575                                 continue;
576
577                         case ':':
578                                 c1 = add_queue(&fmthd, &fmttl, name, NULL,
579                                                 INIT);
580                                 while (*parptr == ':' || *parptr == ',') {
581                                         parptr++;
582                                         if (evalvar(c1))
583                                                 adios(NULL, "format file syntax error: %s", bp);
584                                 }
585                                 if (!c1->c_nfs && global.c_nfs) {
586                                         if (c1->c_flags & DATEFMT) {
587                                                 if (global.c_flags & DATEFMT)
588                                                         c1->c_nfs = getcpy(global.c_nfs);
589                                         }
590                                         else
591                                                 if (c1->c_flags & ADDRFMT) {
592                                                         if (global.c_flags & ADDRFMT)
593                                                                 c1->c_nfs = getcpy(global.c_nfs);
594                                                 }
595                                 }
596                                 continue;
597
598                         default:
599                                 adios(NULL, "format file syntax error: %s",
600                                                 bp);
601                 }
602         }
603         fclose(fp);
604
605         if (mhldebug) {
606                 for (c1 = fmthd; c1; c1 = c1->c_next) {
607                         fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
608                         fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
609                         fprintf(stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n", c1->c_offset, c1->c_ovoff, c1->c_width, c1->c_cwidth, c1->c_length);
610                         fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
611                 }
612         }
613
614 out:
615         if (clearflg == 1) {
616                 global.c_flags |= CLEARSCR;
617         } else {
618                 if (clearflg == -1)
619                         global.c_flags &= ~CLEARSCR;
620         }
621
622         switch (bellflg) {  /* command line may override format file */
623                 case 1:
624                         global.c_flags |= BELL;
625                         break;
626                 case -1:
627                         global.c_flags &= ~BELL;
628                         break;
629         }
630
631         if (length)
632                 global.c_length = length;
633         if (width)
634                 global.c_width = width;
635         if (global.c_length < 5)
636                 global.c_length = 10000;
637         if (global.c_width < 5)
638                 global.c_width = 10000;
639 }
640
641
642 static int
643 evalvar(struct mcomp *c1)
644 {
645         char *cp, name[NAMESZ];
646         struct triple *ap;
647
648         if (!*parptr)
649                 return 0;
650         strncpy(name, parse(), sizeof(name));
651
652         if (!mh_strcasecmp(name, "component")) {
653                 if (ptos(name, &c1->c_text))
654                         return 1;
655                 c1->c_flags &= ~NOCOMPONENT;
656                 return 0;
657         }
658
659         if (!mh_strcasecmp(name, "overflowtext"))
660                 return ptos(name, &c1->c_ovtxt);
661
662         if (!mh_strcasecmp(name, "formatfield")) {
663                 char *nfs;
664
665                 if (ptos(name, &cp))
666                         return 1;
667                 nfs = new_fs(NULL, NULL, cp);
668                 c1->c_nfs = getcpy(nfs);
669                 c1->c_flags |= FORMAT;
670                 return 0;
671         }
672
673         if (!mh_strcasecmp(name, "decode")) {
674                 char *nfs;
675
676                 nfs = new_fs(NULL, NULL, "%(decode{text})");
677                 c1->c_nfs = getcpy(nfs);
678                 c1->c_flags |= FORMAT;
679                 return 0;
680         }
681
682         if (!mh_strcasecmp(name, "offset"))
683                 return ptoi(name, &c1->c_offset);
684         if (!mh_strcasecmp(name, "overflowoffset"))
685                 return ptoi(name, &c1->c_ovoff);
686         if (!mh_strcasecmp(name, "width"))
687                 return ptoi(name, &c1->c_width);
688         if (!mh_strcasecmp(name, "compwidth"))
689                 return ptoi(name, &c1->c_cwidth);
690         if (!mh_strcasecmp(name, "length"))
691                 return ptoi(name, &c1->c_length);
692         if (!mh_strcasecmp(name, "nodashstuffing"))
693                 return (dashstuff = -1);
694
695         for (ap = triples; ap->t_name; ap++)
696                 if (!mh_strcasecmp(ap->t_name, name)) {
697                         c1->c_flags |= ap->t_on;
698                         c1->c_flags &= ~ap->t_off;
699                         return 0;
700                 }
701
702         return 1;
703 }
704
705
706 static int
707 ptoi(char *name, int *i)
708 {
709         char *cp;
710
711         if (*parptr++ != '=' || !*(cp = parse())) {
712                 advise(NULL, "missing argument to variable %s", name);
713                 return 1;
714         }
715
716         *i = atoi(cp);
717         return 0;
718 }
719
720
721 static int
722 ptos(char *name, char **s)
723 {
724         char c, *cp;
725
726         if (*parptr++ != '=') {
727                 advise(NULL, "missing argument to variable %s", name);
728                 return 1;
729         }
730
731         if (*parptr != '"') {
732                 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
733                                 parptr++)
734                         continue;
735         } else {
736                 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
737                         if (*parptr == QUOTE)
738                                 if (!*++parptr)
739                                         parptr--;
740         }
741         c = *parptr;
742         *parptr = 0;
743         *s = getcpy(cp);
744         if ((*parptr = c) == '"')
745                 parptr++;
746         return 0;
747 }
748
749
750 static char *
751 parse(void)
752 {
753         int c;
754         char *cp;
755         static char result[NAMESZ];
756
757         for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
758                 c = *parptr;
759                 if (isalnum(c)
760                                 || c == '.'
761                                 || c == '-'
762                                 || c == '_'
763                                 || c == '['
764                                 || c == ']')
765                         *cp++ = c;
766                 else
767                         break;
768         }
769         *cp = '\0';
770
771         return result;
772 }
773
774
775 static void
776 process(char *folder, char *fname, int ofilen, int ofilec)
777 {
778         char *cp = NULL;
779         FILE *fp = NULL;
780         struct mcomp *c1;
781
782         switch (setjmp(env)) {
783                 case OK:
784                         if (fname) {
785                                 fp = mhl_action ? (*mhl_action) (fname) :
786                                                 fopen(fname, "r");
787                                 if (fp == NULL) {
788                                         advise(fname, "unable to open");
789                                         exitstat++;
790                                         return;
791                                 }
792                         } else {
793                                 fname = "(stdin)";
794                                 fp = stdin;
795                         }
796                         cp = folder ? concat(folder, ":", fname, NULL) :
797                                         getcpy(fname);
798                         if (ontty != PITTY)
799                                 SIGNAL(SIGINT, intrser);
800                         mhlfile(fp, cp, ofilen, ofilec);  /* FALL THROUGH! */
801
802                 default:
803                         if (ontty != PITTY)
804                                 SIGNAL(SIGINT, SIG_IGN);
805                         if (mhl_action == NULL && fp != stdin)
806                                 fclose(fp);
807                         free(cp);
808                         if (holder.c_text) {
809                                 free(holder.c_text);
810                                 holder.c_text = NULL;
811                         }
812                         free_queue(&msghd, &msgtl);
813                         for (c1 = fmthd; c1; c1 = c1->c_next)
814                                 c1->c_flags &= ~HDROUTPUT;
815                         break;
816         }
817 }
818
819
820 static void
821 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
822 {
823         int state;
824         struct mcomp *c1, *c2, *c3;
825         char **ip, name[NAMESZ], buf[BUFSIZ];
826
827         if (forwall) {
828                 if (digest)
829                         printf("%s", ofilen == 1 ? delim3 : delim4);
830                 else {
831                         printf("\n-------");
832                         if (ofilen == 1)
833                                 printf(" Forwarded Message%s",
834                                                 ofilec > 1 ? "s" : "");
835                         else
836                                 printf(" Message %d", ofilen);
837                         printf("\n\n");
838                 }
839         } else {
840                 switch (ontty) {
841                         case PITTY:
842                                 if (ofilec > 1) {
843                                         if (ofilen > 1) {
844                                                 if ((global.c_flags & CLEARSCR))
845                                                         clear_screen();
846                                                 else
847                                                         printf("\n\n\n");
848                                         }
849                                         printf(">>> %s\n\n", mname);
850                                 }
851                                 break;
852
853                         case ISTTY:
854                                 strncpy(buf, "\n", sizeof(buf));
855                                 if (ofilec > 1) {
856                                         if (SOprintf("Press <return> to list \"%s\"...", mname)) {
857                                                 if (ofilen > 1)
858                                                         printf("\n\n\n");
859                                                 printf("Press <return> to list \"%s\"...", mname);
860                                         }
861                                         fflush(stdout);
862                                         buf[0] = 0;
863                                         read(fileno(stdout), buf, sizeof(buf));
864                                 }
865                                 if (strchr(buf, '\n')) {
866                                         if ((global.c_flags & CLEARSCR))
867                                                 clear_screen();
868                                 }
869                                 else
870                                         printf("\n");
871                                 break;
872
873                         default:
874                                 if (ofilec > 1) {
875                                         if (ofilen > 1) {
876                                                 printf("\n\n\n");
877                                                 if (clearflg > 0)
878                                                         clear_screen();
879                                         }
880                                         printf(">>> %s\n\n", mname);
881                                 }
882                                 break;
883                 }
884         }
885
886         for (state = FLD;;) {
887                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
888                         case FLD:
889                         case FLDPLUS:
890                                 for (ip = ignores; *ip; ip++)
891                                         if (!mh_strcasecmp(name, *ip)) {
892                                                 while (state == FLDPLUS)
893                                                         state = m_getfld(state, name, buf, sizeof(buf), fp);
894                                                 break;
895                                         }
896                                 if (*ip)
897                                         continue;
898
899                                 for (c2 = fmthd; c2; c2 = c2->c_next)
900                                         if (!mh_strcasecmp(c2->c_name, name))
901                                                 break;
902                                 c1 = NULL;
903                                 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
904                                         for (c1 = msghd; c1; c1 = c1->c_next)
905                                                 if (!mh_strcasecmp(c1->c_name, c3->c_name)) {
906                                                         c1->c_text = mcomp_add (c1->c_flags, buf, c1->c_text);
907                                                         break;
908                                                 }
909                                 if (c1 == NULL)
910                                         c1 = add_queue(&msghd, &msgtl, name, buf, 0);
911                                 while (state == FLDPLUS) {
912                                         state = m_getfld(state, name, buf, sizeof(buf), fp);
913                                         c1->c_text = add(buf, c1->c_text);
914                                 }
915                                 if (c2 == NULL)
916                                         c1->c_flags |= EXTRA;
917                                 continue;
918
919                         case BODY:
920                         case FILEEOF:
921                                 row = column = 0;
922                                 for (c1 = fmthd; c1; c1 = c1->c_next) {
923                                         if (c1->c_flags & CLEARTEXT) {
924                                                 putcomp(c1, c1, ONECOMP);
925                                                 continue;
926                                         }
927                                         if (!mh_strcasecmp(c1->c_name, "messagename")) {
928                                                 holder.c_text = concat("(Message ", mname, ")\n",
929                                                                                         NULL);
930                                                 putcomp(c1, &holder, ONECOMP);
931                                                 free(holder.c_text);
932                                                 holder.c_text = NULL;
933                                                 continue;
934                                         }
935                                         if (!mh_strcasecmp(c1->c_name, "extras")) {
936                                                 for (c2 = msghd; c2; c2 = c2->c_next)
937                                                         if (c2->c_flags & EXTRA)
938                                                                 putcomp(c1, c2, TWOCOMP);
939                                                 continue;
940                                         }
941                                         if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
942                                                 holder.c_text = mh_xmalloc(sizeof(buf));
943                                                 strncpy(holder.c_text, buf, sizeof(buf));
944                                                 while (state == BODY) {
945                                                         putcomp(c1, &holder, BODYCOMP);
946                                                         state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
947                                                 }
948                                                 free(holder.c_text);
949                                                 holder.c_text = NULL;
950                                                 continue;
951                                         }
952                                         for (c2 = msghd; c2; c2 = c2->c_next)
953                                                 if (!mh_strcasecmp(c2->c_name, c1->c_name)) {
954                                                         putcomp(c1, c2, ONECOMP);
955                                                         if (!(c1->c_flags & SPLIT))
956                                                                 break;
957                                                 }
958                                 }
959                                 return;
960
961                         case LENERR:
962                         case FMTERR:
963                                 advise(NULL, "format error in message %s",
964                                                 mname);
965                                 exitstat++;
966                                 return;
967
968                         default:
969                                 adios(NULL, "getfld() returned %d", state);
970                 }
971         }
972 }
973
974
975 static int
976 mcomp_flags(char *name)
977 {
978         struct pair *ap;
979
980         for (ap = pairs; ap->p_name; ap++)
981                 if (!mh_strcasecmp(ap->p_name, name))
982                         return (ap->p_flags);
983
984         return 0;
985 }
986
987
988 static char *
989 mcomp_add(long flags, char *s1, char *s2)
990 {
991         char *dp;
992
993         if (!(flags & ADDRFMT))
994                 return add(s1, s2);
995
996         if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
997                 *dp = 0;
998
999         return add(s1, add(",\n", s2));
1000 }
1001
1002
1003 struct pqpair {
1004         char *pq_text;
1005         char *pq_error;
1006         struct pqpair *pq_next;
1007 };
1008
1009
1010 static void
1011 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1012 {
1013         int dat[5];
1014         char *ap, *cp;
1015         char buffer[BUFSIZ], error[BUFSIZ];
1016         struct comp *cptr;
1017         struct pqpair *p, *q;
1018         struct pqpair pq;
1019         struct mailname *mp;
1020
1021         ap = c2->c_text;
1022         c2->c_text = NULL;
1023         dat[0] = 0;
1024         dat[1] = 0;
1025         dat[2] = 0;
1026         dat[3] = sizeof(buffer) - 1;
1027         dat[4] = 0;
1028         fmt_compile(c1->c_nfs, &c1->c_fmt);
1029
1030         if (!(c1->c_flags & ADDRFMT)) {
1031                 FINDCOMP(cptr, "text");
1032                 if (cptr)
1033                         cptr->c_text = ap;
1034                 if ((cp = strrchr(ap, '\n')))  /* drop ending newline */
1035                         if (!cp[1])
1036                                 *cp = 0;
1037
1038                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1039                 /* Don't need to append a newline, dctime() already did */
1040                 c2->c_text = getcpy(buffer);
1041
1042                 free(ap);
1043                 return;
1044         }
1045
1046         (q = &pq)->pq_next = NULL;
1047         while ((cp = getname(ap))) {
1048                 if ((p = (struct pqpair *)
1049                                 calloc((size_t) 1, sizeof(*p))) == NULL)
1050                         adios(NULL, "unable to allocate pqpair memory");
1051
1052                 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1053                         p->pq_text = getcpy(cp);
1054                         p->pq_error = getcpy(error);
1055                 } else {
1056                         p->pq_text = getcpy(mp->m_text);
1057                         mnfree(mp);
1058                 }
1059                 q = (q->pq_next = p);
1060         }
1061
1062         for (p = pq.pq_next; p; p = q) {
1063                 FINDCOMP(cptr, "text");
1064                 if (cptr)
1065                         cptr->c_text = p->pq_text;
1066                 FINDCOMP(cptr, "error");
1067                 if (cptr)
1068                         cptr->c_text = p->pq_error;
1069
1070                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1071                 if (*buffer) {
1072                         if (c2->c_text)
1073                                 c2->c_text = add(",\n", c2->c_text);
1074                         if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1075                                 *cp = 0;
1076                         c2->c_text = add(buffer, c2->c_text);
1077                 }
1078
1079                 free(p->pq_text);
1080                 if (p->pq_error)
1081                         free(p->pq_error);
1082                 q = p->pq_next;
1083                 free((char *) p);
1084         }
1085
1086         c2->c_text = add("\n", c2->c_text);
1087         free (ap);
1088 }
1089
1090
1091 static struct mcomp *
1092 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1093                 char *text, int flags)
1094 {
1095         struct mcomp *c1;
1096
1097         if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1098                 adios(NULL, "unable to allocate comp memory");
1099
1100         c1->c_flags = flags & ~INIT;
1101         if ((c1->c_name = name ? getcpy(name) : NULL))
1102                 c1->c_flags |= mcomp_flags(c1->c_name);
1103         c1->c_text = text ? getcpy(text) : NULL;
1104         if (flags & INIT) {
1105                 if (global.c_ovtxt)
1106                         c1->c_ovtxt = getcpy(global.c_ovtxt);
1107                 c1->c_offset = global.c_offset;
1108                 c1->c_ovoff = global. c_ovoff;
1109                 c1->c_width = c1->c_length = 0;
1110                 c1->c_cwidth = global.c_cwidth;
1111                 c1->c_flags |= global.c_flags & GFLAGS;
1112         }
1113         if (*head == NULL)
1114                 *head = c1;
1115         if (*tail != NULL)
1116                 (*tail)->c_next = c1;
1117         *tail = c1;
1118
1119         return c1;
1120 }
1121
1122
1123 static void
1124 free_queue(struct mcomp **head, struct mcomp **tail)
1125 {
1126         struct mcomp *c1, *c2;
1127
1128         for (c1 = *head; c1; c1 = c2) {
1129                 c2 = c1->c_next;
1130                 if (c1->c_name)
1131                         free(c1->c_name);
1132                 if (c1->c_text)
1133                         free(c1->c_text);
1134                 if (c1->c_ovtxt)
1135                         free(c1->c_ovtxt);
1136                 if (c1->c_nfs)
1137                         free(c1->c_nfs);
1138                 if (c1->c_fmt)
1139                         free((char *) c1->c_fmt);
1140                 free((char *) c1);
1141         }
1142
1143         *head = *tail = NULL;
1144 }
1145
1146
1147 static void
1148 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1149 {
1150         int count, cchdr;
1151         unsigned char *cp;
1152
1153         cchdr = 0;
1154         lm = 0;
1155         llim = c1->c_length ? c1->c_length : -1;
1156         wid = c1->c_width ? c1->c_width : global.c_width;
1157         ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1158                         + c1->c_offset;
1159         if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1160                 ovtxt = "";
1161         if (wid < ovoff + strlen(ovtxt) + 5)
1162                 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1163         onelp = NULL;
1164
1165         if (c1->c_flags & CLEARTEXT) {
1166                 putstr(c1->c_text);
1167                 putstr("\n");
1168                 return;
1169         }
1170
1171         if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1172                 mcomp_format(c1, c2);
1173
1174         if (c1->c_flags & CENTER) {
1175                 count = (c1->c_width ? c1->c_width : global.c_width)
1176                                 - c1->c_offset - strlen(c2->c_text);
1177                 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1178                         count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1179                                         + 2;
1180                 lm = c1->c_offset + (count / 2);
1181         } else {
1182                 if (c1->c_offset)
1183                         lm = c1->c_offset;
1184         }
1185
1186         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1187                 if (c1->c_flags & UPPERCASE)  /* uppercase component also */
1188                         for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1189                                 if (islower(*cp))
1190                                         *cp = toupper(*cp);
1191                 putstr(c1->c_text ? c1->c_text : c1->c_name);
1192                 if (flag != BODYCOMP) {
1193                         putstr(": ");
1194                         if (!(c1->c_flags & SPLIT))
1195                                 c1->c_flags |= HDROUTPUT;
1196
1197                 cchdr++;
1198                 if ((count = c1->c_cwidth -
1199                         strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1200                         while (count--)
1201                                 putstr(" ");
1202                 } else
1203                         c1->c_flags |= HDROUTPUT;  /* for BODYCOMP */
1204         }
1205
1206         if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1207                 && !(c2->c_flags & NOCOMPONENT)) {
1208                 if (c1->c_flags & UPPERCASE)
1209                         for (cp = c2->c_name; *cp; cp++)
1210                                 if (islower(*cp))
1211                                         *cp = toupper(*cp);
1212                 putstr(c2->c_name);
1213                 putstr(": ");
1214                 if (!(c1->c_flags & SPLIT))
1215                         c2->c_flags |= HDROUTPUT;
1216
1217                 cchdr++;
1218                 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1219                         while (count--)
1220                                 putstr(" ");
1221         }
1222         if (c1->c_flags & UPPERCASE)
1223                 for (cp = c2->c_text; *cp; cp++)
1224                         if (islower(*cp))
1225                                 *cp = toupper(*cp);
1226
1227         count = 0;
1228         if (cchdr) {
1229                 if (flag == TWOCOMP)
1230                         count = (c1->c_cwidth >= 0) ?
1231                                         c1->c_cwidth : strlen(c2->c_name) + 2;
1232                 else
1233                         count = (c1->c_cwidth >= 0) ?
1234                                         c1->c_cwidth : strlen(c1->c_text ?
1235                                         c1->c_text : c1->c_name) + 2;
1236         }
1237         count += c1->c_offset;
1238
1239         if ((cp = oneline(c2->c_text, c1->c_flags)))
1240            putstr(cp);
1241         if (term == '\n')
1242                 putstr("\n");
1243         while ((cp = oneline(c2->c_text, c1->c_flags))) {
1244                 lm = count;
1245                 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1246                         putstr(c1->c_text ? c1->c_text : c1->c_name);
1247                 if (*cp)
1248                         putstr(cp);
1249                 if (term == '\n')
1250                         putstr("\n");
1251         }
1252         if (flag == BODYCOMP && term == '\n')
1253                 c1->c_flags &= ~HDROUTPUT;  /* Buffer ended on a newline */
1254 }
1255
1256
1257 static char *
1258 oneline(char *stuff, long flags)
1259 {
1260         int spc;
1261         char *cp, *ret;
1262
1263         if (onelp == NULL)
1264                 onelp = stuff;
1265         if (*onelp == 0)
1266                 return (onelp = NULL);
1267
1268         ret = onelp;
1269         term = 0;
1270         if (flags & COMPRESS) {
1271                 for (spc = 1, cp = ret; *onelp; onelp++)
1272                         if (isspace(*onelp)) {
1273                                 if (*onelp == '\n' &&
1274                                                 (!onelp[1] ||
1275                                                 (flags & ADDRFMT))) {
1276                                         term = '\n';
1277                                         *onelp++ = 0;
1278                                         break;
1279                                 } else if (!spc) {
1280                                         *cp++ = ' ';
1281                                         spc++;
1282                                 }
1283                         } else {
1284                                 *cp++ = *onelp;
1285                                 spc = 0;
1286                         }
1287
1288                 *cp = 0;
1289         } else {
1290                 while (*onelp && *onelp != '\n')
1291                         onelp++;
1292                 if (*onelp == '\n') {
1293                         term = '\n';
1294                         *onelp++ = 0;
1295                 }
1296                 if (flags & LEFTADJUST)
1297                         while (*ret == ' ' || *ret == '\t')
1298                                 ret++;
1299         }
1300         if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1301                 term = 0;
1302
1303         return ret;
1304 }
1305
1306
1307 static void
1308 putstr(char *string)
1309 {
1310         if (!column && lm > 0) {
1311                 while (lm > 0)
1312                         if (lm >= 8) {
1313                                 putch('\t');
1314                                 lm -= 8;
1315                         } else {
1316                                 putch(' ');
1317                                 lm--;
1318                         }
1319         }
1320         lm = 0;
1321         while (*string)
1322                 putch(*string++);
1323 }
1324
1325
1326 static void
1327 putch(char ch)
1328 {
1329         char buf[BUFSIZ];
1330
1331         if (llim == 0)
1332                 return;
1333
1334         switch (ch) {
1335                 case '\n':
1336                         if (llim > 0)
1337                                 llim--;
1338                         column = 0;
1339                         row++;
1340                         if (ontty != ISTTY || row != global.c_length)
1341                                 break;
1342                         if (global.c_flags & BELL)
1343                                 putchar('\007');
1344                         fflush(stdout);
1345                         buf[0] = 0;
1346                         read(fileno(stdout), buf, sizeof(buf));
1347                         if (strchr(buf, '\n')) {
1348                                 if (global.c_flags & CLEARSCR)
1349                                         clear_screen();
1350                                 row = 0;
1351                         } else {
1352                                 putchar('\n');
1353                                 row = global.c_length / 3;
1354                         }
1355                         return;
1356
1357                 case '\t':
1358                         column |= 07;
1359                         column++;
1360                         break;
1361
1362                 case '\b':
1363                         column--;
1364                         break;
1365
1366                 case '\r':
1367                         column = 0;
1368                         break;
1369
1370                 default:
1371                         /*
1372                         ** If we are forwarding this message, and the first
1373                         ** column contains a dash, then add a dash and a space.
1374                         */
1375                         if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1376                                 putchar('-');
1377                                 putchar(' ');
1378                         }
1379                         if (ch >= ' ')
1380                                 column++;
1381                         break;
1382         }
1383
1384         if (column >= wid) {
1385                 putch('\n');
1386                 if (ovoff > 0)
1387                         lm = ovoff;
1388                 putstr(ovtxt ? ovtxt : "");
1389                 putch(ch);
1390                 return;
1391         }
1392
1393         putchar(ch);
1394 }
1395
1396
1397 static RETSIGTYPE
1398 intrser(int i)
1399 {
1400 #ifndef RELIABLE_SIGNALS
1401         SIGNAL(SIGINT, intrser);
1402 #endif
1403
1404         discard(stdout);
1405         putchar('\n');
1406         longjmp(env, DONE);
1407 }
1408
1409
1410 static RETSIGTYPE
1411 pipeser(int i)
1412 {
1413 #ifndef RELIABLE_SIGNALS
1414         SIGNAL(SIGPIPE, pipeser);
1415 #endif
1416
1417         done(NOTOK);
1418 }
1419
1420
1421 static RETSIGTYPE
1422 quitser(int i)
1423 {
1424 #ifndef RELIABLE_SIGNALS
1425         SIGNAL(SIGQUIT, quitser);
1426 #endif
1427
1428         putchar('\n');
1429         fflush(stdout);
1430         done(NOTOK);
1431 }
1432
1433
1434 int
1435 mhlsbr(int argc, char **argv, FILE *(*action)())
1436 {
1437         SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1438         char *cp = NULL;
1439         struct mcomp *c1;
1440
1441         switch (setjmp(mhlenv)) {
1442                 case OK:
1443                         cp = invo_name;
1444                         sleepsw = 0;  /* XXX */
1445                         bellflg = clearflg = forwflg = forwall = exitstat = 0;
1446                         digest = NULL;
1447                         ontty = NOTTY;
1448                         mhl_action = action;
1449
1450                         /*
1451                         ** If signal is at default action, then start ignoring
1452                         ** it, else let it set to its current action.
1453                         */
1454                         if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1455                                 SIGNAL(SIGINT, istat);
1456                         if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1457                                 SIGNAL(SIGQUIT, qstat);
1458                         pstat = SIGNAL(SIGPIPE, pipeser);
1459                         mhl(argc, argv);  /* FALL THROUGH! */
1460
1461                 default:
1462                         SIGNAL(SIGINT, istat);
1463                         SIGNAL(SIGQUIT, qstat);
1464                         SIGNAL(SIGPIPE, SIG_IGN);  /*
1465                                         ** should probably change to block
1466                                         ** instead
1467                                         */
1468                         if (ontty == PITTY)
1469                                 m_pclose();
1470                         SIGNAL(SIGPIPE, pstat);
1471                         invo_name = cp;
1472                         if (holder.c_text) {
1473                                 free(holder.c_text);
1474                                 holder.c_text = NULL;
1475                         }
1476                         free_queue(&msghd, &msgtl);
1477                         for (c1 = fmthd; c1; c1 = c1->c_next)
1478                                 c1->c_flags &= ~HDROUTPUT;
1479                         return exitstat;
1480         }
1481 }
1482
1483 #undef adios
1484 #undef done
1485
1486 static void
1487 mhladios(char *what, char *fmt, ...)
1488 {
1489         va_list ap;
1490
1491         va_start(ap, fmt);
1492         advertise(what, NULL, fmt, ap);
1493         va_end(ap);
1494         mhldone(1);
1495 }
1496
1497
1498 static void
1499 mhldone(int status)
1500 {
1501         exitstat = status;
1502         if (mhl_action)
1503                 longjmp(mhlenv, DONE);
1504         else
1505                 done(exitstat);
1506 }
1507
1508
1509 static int m_pid = NOTOK;
1510 static int sd = NOTOK;
1511
1512 static void
1513 m_popen(char *name)
1514 {
1515         int pd[2];
1516
1517         if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1518                 adios("standard output", "unable to dup()");
1519
1520         if (pipe(pd) == NOTOK)
1521                 adios("pipe", "unable to");
1522
1523         switch (m_pid = vfork()) {
1524                 case NOTOK:
1525                         adios("fork", "unable to");
1526
1527                 case OK:
1528                         SIGNAL(SIGINT, SIG_DFL);
1529                         SIGNAL(SIGQUIT, SIG_DFL);
1530
1531                         close(pd[1]);
1532                         if (pd[0] != fileno(stdin)) {
1533                                 dup2(pd[0], fileno(stdin));
1534                                 close(pd[0]);
1535                         }
1536                         execlp(name, mhbasename(name), NULL);
1537                         fprintf(stderr, "unable to exec ");
1538                         perror(name);
1539                         _exit(-1);
1540
1541                 default:
1542                         close(pd[0]);
1543                         if (pd[1] != fileno(stdout)) {
1544                                 dup2(pd[1], fileno(stdout));
1545                                 close(pd[1]);
1546                         }
1547         }
1548 }
1549
1550
1551 void
1552 m_pclose(void)
1553 {
1554         if (m_pid == NOTOK)
1555                 return;
1556
1557         if (sd != NOTOK) {
1558                 fflush(stdout);
1559                 if (dup2(sd, fileno(stdout)) == NOTOK)
1560                         adios("standard output", "unable to dup2()");
1561
1562                 clearerr(stdout);
1563                 close(sd);
1564                 sd = NOTOK;
1565         }
1566         else
1567                 fclose(stdout);
1568
1569         pidwait(m_pid, OK);
1570         m_pid = NOTOK;
1571 }