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