Removed `-format string' switches but made -form accept `=formatstring'.
[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                 cp = concat("=", cp, NULL);
663                 nfs = new_fs(cp, NULL);
664                 free(cp);
665                 c1->c_nfs = getcpy(nfs);
666                 c1->c_flags |= FORMAT;
667                 return 0;
668         }
669
670         if (!mh_strcasecmp(name, "decode")) {
671                 char *nfs;
672
673                 nfs = new_fs("=%(decode{text})", NULL);
674                 c1->c_nfs = getcpy(nfs);
675                 c1->c_flags |= FORMAT;
676                 return 0;
677         }
678
679         if (!mh_strcasecmp(name, "offset"))
680                 return ptoi(name, &c1->c_offset);
681         if (!mh_strcasecmp(name, "overflowoffset"))
682                 return ptoi(name, &c1->c_ovoff);
683         if (!mh_strcasecmp(name, "width"))
684                 return ptoi(name, &c1->c_width);
685         if (!mh_strcasecmp(name, "compwidth"))
686                 return ptoi(name, &c1->c_cwidth);
687         if (!mh_strcasecmp(name, "length"))
688                 return ptoi(name, &c1->c_length);
689
690         for (ap = triples; ap->t_name; ap++)
691                 if (!mh_strcasecmp(ap->t_name, name)) {
692                         c1->c_flags |= ap->t_on;
693                         c1->c_flags &= ~ap->t_off;
694                         return 0;
695                 }
696
697         return 1;
698 }
699
700
701 static int
702 ptoi(char *name, int *i)
703 {
704         char *cp;
705
706         if (*parptr++ != '=' || !*(cp = parse())) {
707                 advise(NULL, "missing argument to variable %s", name);
708                 return 1;
709         }
710
711         *i = atoi(cp);
712         return 0;
713 }
714
715
716 static int
717 ptos(char *name, char **s)
718 {
719         char c, *cp;
720
721         if (*parptr++ != '=') {
722                 advise(NULL, "missing argument to variable %s", name);
723                 return 1;
724         }
725
726         if (*parptr != '"') {
727                 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
728                                 parptr++)
729                         continue;
730         } else {
731                 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
732                         if (*parptr == QUOTE)
733                                 if (!*++parptr)
734                                         parptr--;
735         }
736         c = *parptr;
737         *parptr = 0;
738         *s = getcpy(cp);
739         if ((*parptr = c) == '"')
740                 parptr++;
741         return 0;
742 }
743
744
745 static char *
746 parse(void)
747 {
748         int c;
749         char *cp;
750         static char result[NAMESZ];
751
752         for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
753                 c = *parptr;
754                 if (isalnum(c)
755                                 || c == '.'
756                                 || c == '-'
757                                 || c == '_'
758                                 || c == '['
759                                 || c == ']')
760                         *cp++ = c;
761                 else
762                         break;
763         }
764         *cp = '\0';
765
766         return result;
767 }
768
769
770 static void
771 process(char *folder, char *fname, int ofilen, int ofilec)
772 {
773         char *cp = NULL;
774         FILE *fp = NULL;
775         struct mcomp *c1;
776
777         switch (setjmp(env)) {
778         case OK:
779                 if (fname) {
780                         fp = mhl_action ? (*mhl_action) (fname) :
781                                         fopen(fname, "r");
782                         if (fp == NULL) {
783                                 advise(fname, "unable to open");
784                                 exitstat++;
785                                 return;
786                         }
787                 } else {
788                         fname = "(stdin)";
789                         fp = stdin;
790                 }
791                 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
792                 if (ontty != PITTY)
793                         SIGNAL(SIGINT, intrser);
794                 mhlfile(fp, cp, ofilen, ofilec);  /* FALL THROUGH! */
795
796         default:
797                 if (ontty != PITTY)
798                         SIGNAL(SIGINT, SIG_IGN);
799                 if (mhl_action == NULL && fp != stdin)
800                         fclose(fp);
801                 free(cp);
802                 if (holder.c_text) {
803                         free(holder.c_text);
804                         holder.c_text = NULL;
805                 }
806                 free_queue(&msghd, &msgtl);
807                 for (c1 = fmthd; c1; c1 = c1->c_next)
808                         c1->c_flags &= ~HDROUTPUT;
809                 break;
810         }
811 }
812
813
814 static void
815 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
816 {
817         int state;
818         struct mcomp *c1, *c2, *c3;
819         char **ip, name[NAMESZ], buf[BUFSIZ];
820
821         if (forwall) {
822                 if (digest)
823                         printf("%s", ofilen == 1 ? delim3 : delim4);
824                 else {
825                         printf("\n-------");
826                         if (ofilen == 1)
827                                 printf(" Forwarded Message%s",
828                                                 ofilec > 1 ? "s" : "");
829                         else
830                                 printf(" Message %d", ofilen);
831                         printf("\n\n");
832                 }
833         } else {
834                 switch (ontty) {
835                 case PITTY:
836                         if (ofilec > 1) {
837                                 if (ofilen > 1) {
838                                         if ((global.c_flags & CLEARSCR))
839                                                 clear_screen();
840                                         else
841                                                 printf("\n\n\n");
842                                 }
843                                 printf(">>> %s\n\n", mname);
844                         }
845                         break;
846
847                 case ISTTY:
848                         strncpy(buf, "\n", sizeof(buf));
849                         if (ofilec > 1) {
850                                 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
851                                         if (ofilen > 1)
852                                                 printf("\n\n\n");
853                                         printf("Press <return> to list \"%s\"...", mname);
854                                 }
855                                 fflush(stdout);
856                                 buf[0] = 0;
857                                 read(fileno(stdout), buf, sizeof(buf));
858                         }
859                         if (strchr(buf, '\n')) {
860                                 if ((global.c_flags & CLEARSCR))
861                                         clear_screen();
862                         } else
863                                 printf("\n");
864                         break;
865
866                 default:
867                         if (ofilec > 1) {
868                                 if (ofilen > 1) {
869                                         printf("\n\n\n");
870                                         if (clearflg > 0)
871                                                 clear_screen();
872                                 }
873                                 printf(">>> %s\n\n", mname);
874                         }
875                         break;
876                 }
877         }
878
879         for (state = FLD;;) {
880                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
881                 case FLD:
882                 case FLDPLUS:
883                         for (ip = ignores; *ip; ip++)
884                                 if (!mh_strcasecmp(name, *ip)) {
885                                         while (state == FLDPLUS)
886                                                 state = m_getfld(state, name, buf, sizeof(buf), fp);
887                                         break;
888                                 }
889                         if (*ip)
890                                 continue;
891
892                         for (c2 = fmthd; c2; c2 = c2->c_next)
893                                 if (!mh_strcasecmp(c2->c_name, name))
894                                         break;
895                         c1 = NULL;
896                         if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
897                                 for (c1 = msghd; c1; c1 = c1->c_next)
898                                         if (!mh_strcasecmp(c1->c_name,
899                                                         c3->c_name)) {
900                                                 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
901                                                 break;
902                                         }
903                         if (c1 == NULL)
904                                 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
905                         while (state == FLDPLUS) {
906                                 state = m_getfld(state, name, buf,
907                                                 sizeof(buf), fp);
908                                 c1->c_text = add(buf, c1->c_text);
909                         }
910                         if (c2 == NULL)
911                                 c1->c_flags |= EXTRA;
912                         continue;
913
914                 case BODY:
915                 case FILEEOF:
916                         row = column = 0;
917                         for (c1 = fmthd; c1; c1 = c1->c_next) {
918                                 if (c1->c_flags & CLEARTEXT) {
919                                         putcomp(c1, c1, ONECOMP);
920                                         continue;
921                                 }
922                                 if (!mh_strcasecmp(c1->c_name, "messagename")) {
923                                         holder.c_text = concat("(Message ",
924                                                         mname, ")\n", NULL);
925                                         putcomp(c1, &holder, ONECOMP);
926                                         free(holder.c_text);
927                                         holder.c_text = NULL;
928                                         continue;
929                                 }
930                                 if (!mh_strcasecmp(c1->c_name, "extras")) {
931                                         for (c2 = msghd; c2; c2 = c2->c_next)
932                                                 if (c2->c_flags & EXTRA)
933                                                         putcomp(c1, c2, TWOCOMP);
934                                         continue;
935                                 }
936                                 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
937                                         holder.c_text = mh_xmalloc(sizeof(buf));
938                                         strncpy(holder.c_text, buf, sizeof(buf));
939                                         while (state == BODY) {
940                                                 putcomp(c1, &holder, BODYCOMP);
941                                                 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
942                                         }
943                                         free(holder.c_text);
944                                         holder.c_text = NULL;
945                                         continue;
946                                 }
947                                 for (c2 = msghd; c2; c2 = c2->c_next)
948                                         if (!mh_strcasecmp(c2->c_name,
949                                                         c1->c_name)) {
950                                                 putcomp(c1, c2, ONECOMP);
951                                                 if (!(c1->c_flags & SPLIT))
952                                                         break;
953                                         }
954                         }
955                         return;
956
957                 case LENERR:
958                 case FMTERR:
959                         advise(NULL, "format error in message %s", mname);
960                         exitstat++;
961                         return;
962
963                 default:
964                         adios(NULL, "getfld() returned %d", state);
965                 }
966         }
967 }
968
969
970 static int
971 mcomp_flags(char *name)
972 {
973         struct pair *ap;
974
975         for (ap = pairs; ap->p_name; ap++)
976                 if (!mh_strcasecmp(ap->p_name, name))
977                         return (ap->p_flags);
978
979         return 0;
980 }
981
982
983 static char *
984 mcomp_add(long flags, char *s1, char *s2)
985 {
986         char *dp;
987
988         if (!(flags & ADDRFMT))
989                 return add(s1, s2);
990
991         if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
992                 *dp = 0;
993
994         return add(s1, add(",\n", s2));
995 }
996
997
998 struct pqpair {
999         char *pq_text;
1000         char *pq_error;
1001         struct pqpair *pq_next;
1002 };
1003
1004
1005 static void
1006 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1007 {
1008         int dat[5];
1009         char *ap, *cp;
1010         char buffer[BUFSIZ], error[BUFSIZ];
1011         struct comp *cptr;
1012         struct pqpair *p, *q;
1013         struct pqpair pq;
1014         struct mailname *mp;
1015
1016         ap = c2->c_text;
1017         c2->c_text = NULL;
1018         dat[0] = 0;
1019         dat[1] = 0;
1020         dat[2] = 0;
1021         dat[3] = sizeof(buffer) - 1;
1022         dat[4] = 0;
1023         fmt_compile(c1->c_nfs, &c1->c_fmt);
1024
1025         if (!(c1->c_flags & ADDRFMT)) {
1026                 FINDCOMP(cptr, "text");
1027                 if (cptr)
1028                         cptr->c_text = ap;
1029                 if ((cp = strrchr(ap, '\n')))  /* drop ending newline */
1030                         if (!cp[1])
1031                                 *cp = 0;
1032
1033                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1034                 /* Don't need to append a newline, dctime() already did */
1035                 c2->c_text = getcpy(buffer);
1036
1037                 free(ap);
1038                 return;
1039         }
1040
1041         (q = &pq)->pq_next = NULL;
1042         while ((cp = getname(ap))) {
1043                 if ((p = (struct pqpair *)
1044                                 calloc((size_t) 1, sizeof(*p))) == NULL)
1045                         adios(NULL, "unable to allocate pqpair memory");
1046
1047                 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1048                         p->pq_text = getcpy(cp);
1049                         p->pq_error = getcpy(error);
1050                 } else {
1051                         p->pq_text = getcpy(mp->m_text);
1052                         mnfree(mp);
1053                 }
1054                 q = (q->pq_next = p);
1055         }
1056
1057         for (p = pq.pq_next; p; p = q) {
1058                 FINDCOMP(cptr, "text");
1059                 if (cptr)
1060                         cptr->c_text = p->pq_text;
1061                 FINDCOMP(cptr, "error");
1062                 if (cptr)
1063                         cptr->c_text = p->pq_error;
1064
1065                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1066                 if (*buffer) {
1067                         if (c2->c_text)
1068                                 c2->c_text = add(",\n", c2->c_text);
1069                         if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1070                                 *cp = 0;
1071                         c2->c_text = add(buffer, c2->c_text);
1072                 }
1073
1074                 free(p->pq_text);
1075                 if (p->pq_error)
1076                         free(p->pq_error);
1077                 q = p->pq_next;
1078                 free((char *) p);
1079         }
1080
1081         c2->c_text = add("\n", c2->c_text);
1082         free (ap);
1083 }
1084
1085
1086 static struct mcomp *
1087 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1088                 char *text, int flags)
1089 {
1090         struct mcomp *c1;
1091
1092         if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1093                 adios(NULL, "unable to allocate comp memory");
1094
1095         c1->c_flags = flags & ~INIT;
1096         if ((c1->c_name = name ? getcpy(name) : NULL))
1097                 c1->c_flags |= mcomp_flags(c1->c_name);
1098         c1->c_text = text ? getcpy(text) : NULL;
1099         if (flags & INIT) {
1100                 if (global.c_ovtxt)
1101                         c1->c_ovtxt = getcpy(global.c_ovtxt);
1102                 c1->c_offset = global.c_offset;
1103                 c1->c_ovoff = global. c_ovoff;
1104                 c1->c_width = c1->c_length = 0;
1105                 c1->c_cwidth = global.c_cwidth;
1106                 c1->c_flags |= global.c_flags & GFLAGS;
1107         }
1108         if (*head == NULL)
1109                 *head = c1;
1110         if (*tail != NULL)
1111                 (*tail)->c_next = c1;
1112         *tail = c1;
1113
1114         return c1;
1115 }
1116
1117
1118 static void
1119 free_queue(struct mcomp **head, struct mcomp **tail)
1120 {
1121         struct mcomp *c1, *c2;
1122
1123         for (c1 = *head; c1; c1 = c2) {
1124                 c2 = c1->c_next;
1125                 if (c1->c_name)
1126                         free(c1->c_name);
1127                 if (c1->c_text)
1128                         free(c1->c_text);
1129                 if (c1->c_ovtxt)
1130                         free(c1->c_ovtxt);
1131                 if (c1->c_nfs)
1132                         free(c1->c_nfs);
1133                 if (c1->c_fmt)
1134                         free((char *) c1->c_fmt);
1135                 free((char *) c1);
1136         }
1137
1138         *head = *tail = NULL;
1139 }
1140
1141
1142 static void
1143 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1144 {
1145         int count, cchdr;
1146         unsigned char *cp;
1147
1148         cchdr = 0;
1149         lm = 0;
1150         llim = c1->c_length ? c1->c_length : -1;
1151         wid = c1->c_width ? c1->c_width : global.c_width;
1152         ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1153                         + c1->c_offset;
1154         if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1155                 ovtxt = "";
1156         if (wid < ovoff + strlen(ovtxt) + 5)
1157                 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1158         onelp = NULL;
1159
1160         if (c1->c_flags & CLEARTEXT) {
1161                 putstr(c1->c_text);
1162                 putstr("\n");
1163                 return;
1164         }
1165
1166         if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1167                 mcomp_format(c1, c2);
1168
1169         if (c1->c_flags & CENTER) {
1170                 count = (c1->c_width ? c1->c_width : global.c_width)
1171                                 - c1->c_offset - strlen(c2->c_text);
1172                 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1173                         count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1174                                         + 2;
1175                 lm = c1->c_offset + (count / 2);
1176         } else {
1177                 if (c1->c_offset)
1178                         lm = c1->c_offset;
1179         }
1180
1181         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1182                 if (c1->c_flags & UPPERCASE)  /* uppercase component also */
1183                         for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1184                                 if (islower(*cp))
1185                                         *cp = toupper(*cp);
1186                 putstr(c1->c_text ? c1->c_text : c1->c_name);
1187                 if (flag != BODYCOMP) {
1188                         putstr(": ");
1189                         if (!(c1->c_flags & SPLIT))
1190                                 c1->c_flags |= HDROUTPUT;
1191
1192                 cchdr++;
1193                 if ((count = c1->c_cwidth -
1194                         strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1195                         while (count--)
1196                                 putstr(" ");
1197                 } else
1198                         c1->c_flags |= HDROUTPUT;  /* for BODYCOMP */
1199         }
1200
1201         if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1202                 && !(c2->c_flags & NOCOMPONENT)) {
1203                 if (c1->c_flags & UPPERCASE)
1204                         for (cp = c2->c_name; *cp; cp++)
1205                                 if (islower(*cp))
1206                                         *cp = toupper(*cp);
1207                 putstr(c2->c_name);
1208                 putstr(": ");
1209                 if (!(c1->c_flags & SPLIT))
1210                         c2->c_flags |= HDROUTPUT;
1211
1212                 cchdr++;
1213                 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1214                         while (count--)
1215                                 putstr(" ");
1216         }
1217         if (c1->c_flags & UPPERCASE)
1218                 for (cp = c2->c_text; *cp; cp++)
1219                         if (islower(*cp))
1220                                 *cp = toupper(*cp);
1221
1222         count = 0;
1223         if (cchdr) {
1224                 if (flag == TWOCOMP)
1225                         count = (c1->c_cwidth >= 0) ?
1226                                         c1->c_cwidth : strlen(c2->c_name) + 2;
1227                 else
1228                         count = (c1->c_cwidth >= 0) ?
1229                                         c1->c_cwidth : strlen(c1->c_text ?
1230                                         c1->c_text : c1->c_name) + 2;
1231         }
1232         count += c1->c_offset;
1233
1234         if ((cp = oneline(c2->c_text, c1->c_flags)))
1235            putstr(cp);
1236         if (term == '\n')
1237                 putstr("\n");
1238         while ((cp = oneline(c2->c_text, c1->c_flags))) {
1239                 lm = count;
1240                 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1241                         putstr(c1->c_text ? c1->c_text : c1->c_name);
1242                 if (*cp)
1243                         putstr(cp);
1244                 if (term == '\n')
1245                         putstr("\n");
1246         }
1247         if (flag == BODYCOMP && term == '\n')
1248                 c1->c_flags &= ~HDROUTPUT;  /* Buffer ended on a newline */
1249 }
1250
1251
1252 static char *
1253 oneline(char *stuff, long flags)
1254 {
1255         int spc;
1256         char *cp, *ret;
1257
1258         if (onelp == NULL)
1259                 onelp = stuff;
1260         if (*onelp == 0)
1261                 return (onelp = NULL);
1262
1263         ret = onelp;
1264         term = 0;
1265         if (flags & COMPRESS) {
1266                 for (spc = 1, cp = ret; *onelp; onelp++)
1267                         if (isspace(*onelp)) {
1268                                 if (*onelp == '\n' &&
1269                                                 (!onelp[1] ||
1270                                                 (flags & ADDRFMT))) {
1271                                         term = '\n';
1272                                         *onelp++ = 0;
1273                                         break;
1274                                 } else if (!spc) {
1275                                         *cp++ = ' ';
1276                                         spc++;
1277                                 }
1278                         } else {
1279                                 *cp++ = *onelp;
1280                                 spc = 0;
1281                         }
1282
1283                 *cp = 0;
1284         } else {
1285                 while (*onelp && *onelp != '\n')
1286                         onelp++;
1287                 if (*onelp == '\n') {
1288                         term = '\n';
1289                         *onelp++ = 0;
1290                 }
1291                 if (flags & LEFTADJUST)
1292                         while (*ret == ' ' || *ret == '\t')
1293                                 ret++;
1294         }
1295         if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1296                 term = 0;
1297
1298         return ret;
1299 }
1300
1301
1302 static void
1303 putstr(char *string)
1304 {
1305         if (!column && lm > 0) {
1306                 while (lm > 0)
1307                         if (lm >= 8) {
1308                                 putch('\t');
1309                                 lm -= 8;
1310                         } else {
1311                                 putch(' ');
1312                                 lm--;
1313                         }
1314         }
1315         lm = 0;
1316         while (*string)
1317                 putch(*string++);
1318 }
1319
1320
1321 static void
1322 putch(char ch)
1323 {
1324         char buf[BUFSIZ];
1325
1326         if (llim == 0)
1327                 return;
1328
1329         switch (ch) {
1330         case '\n':
1331                 if (llim > 0)
1332                         llim--;
1333                 column = 0;
1334                 row++;
1335                 if (ontty != ISTTY || row != global.c_length)
1336                         break;
1337                 if (global.c_flags & BELL)
1338                         putchar('\007');
1339                 fflush(stdout);
1340                 buf[0] = 0;
1341                 read(fileno(stdout), buf, sizeof(buf));
1342                 if (strchr(buf, '\n')) {
1343                         if (global.c_flags & CLEARSCR)
1344                                 clear_screen();
1345                         row = 0;
1346                 } else {
1347                         putchar('\n');
1348                         row = global.c_length / 3;
1349                 }
1350                 return;
1351
1352         case '\t':
1353                 column |= 07;
1354                 column++;
1355                 break;
1356
1357         case '\b':
1358                 column--;
1359                 break;
1360
1361         case '\r':
1362                 column = 0;
1363                 break;
1364
1365         default:
1366                 /*
1367                 ** If we are forwarding this message, and the first
1368                 ** column contains a dash, then add a dash and a space.
1369                 */
1370                 if (column == 0 && forwflg && ch == '-') {
1371                         putchar('-');
1372                         putchar(' ');
1373                 }
1374                 if (ch >= ' ')
1375                         column++;
1376                 break;
1377         }
1378
1379         if (column >= wid) {
1380                 putch('\n');
1381                 if (ovoff > 0)
1382                         lm = ovoff;
1383                 putstr(ovtxt ? ovtxt : "");
1384                 putch(ch);
1385                 return;
1386         }
1387
1388         putchar(ch);
1389 }
1390
1391
1392 static RETSIGTYPE
1393 intrser(int i)
1394 {
1395 #ifndef RELIABLE_SIGNALS
1396         SIGNAL(SIGINT, intrser);
1397 #endif
1398
1399         discard(stdout);
1400         putchar('\n');
1401         longjmp(env, DONE);
1402 }
1403
1404
1405 static RETSIGTYPE
1406 pipeser(int i)
1407 {
1408 #ifndef RELIABLE_SIGNALS
1409         SIGNAL(SIGPIPE, pipeser);
1410 #endif
1411
1412         done(NOTOK);
1413 }
1414
1415
1416 static RETSIGTYPE
1417 quitser(int i)
1418 {
1419 #ifndef RELIABLE_SIGNALS
1420         SIGNAL(SIGQUIT, quitser);
1421 #endif
1422
1423         putchar('\n');
1424         fflush(stdout);
1425         done(NOTOK);
1426 }
1427
1428
1429 int
1430 mhlsbr(int argc, char **argv, FILE *(*action)())
1431 {
1432         SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1433         char *cp = NULL;
1434         struct mcomp *c1;
1435
1436         switch (setjmp(mhlenv)) {
1437         case OK:
1438                 cp = invo_name;
1439                 sleepsw = 0;  /* XXX */
1440                 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1441                 digest = NULL;
1442                 ontty = NOTTY;
1443                 mhl_action = action;
1444
1445                 /*
1446                 ** If signal is at default action, then start ignoring
1447                 ** it, else let it set to its current action.
1448                 */
1449                 if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1450                         SIGNAL(SIGINT, istat);
1451                 if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1452                         SIGNAL(SIGQUIT, qstat);
1453                 pstat = SIGNAL(SIGPIPE, pipeser);
1454                 mhl(argc, argv);  /* FALL THROUGH! */
1455
1456         default:
1457                 SIGNAL(SIGINT, istat);
1458                 SIGNAL(SIGQUIT, qstat);
1459                 SIGNAL(SIGPIPE, SIG_IGN);  /*
1460                                 ** should probably change to block
1461                                 ** instead
1462                                 */
1463                 if (ontty == PITTY)
1464                         m_pclose();
1465                 SIGNAL(SIGPIPE, pstat);
1466                 invo_name = cp;
1467                 if (holder.c_text) {
1468                         free(holder.c_text);
1469                         holder.c_text = NULL;
1470                 }
1471                 free_queue(&msghd, &msgtl);
1472                 for (c1 = fmthd; c1; c1 = c1->c_next)
1473                         c1->c_flags &= ~HDROUTPUT;
1474                 return exitstat;
1475         }
1476 }
1477
1478 #undef adios
1479 #undef done
1480
1481 static void
1482 mhladios(char *what, char *fmt, ...)
1483 {
1484         va_list ap;
1485
1486         va_start(ap, fmt);
1487         advertise(what, NULL, fmt, ap);
1488         va_end(ap);
1489         mhldone(1);
1490 }
1491
1492
1493 static void
1494 mhldone(int status)
1495 {
1496         exitstat = status;
1497         if (mhl_action)
1498                 longjmp(mhlenv, DONE);
1499         else
1500                 done(exitstat);
1501 }
1502
1503
1504 static int m_pid = NOTOK;
1505 static int sd = NOTOK;
1506
1507 static void
1508 m_popen(char *name)
1509 {
1510         int pd[2];
1511
1512         if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1513                 adios("standard output", "unable to dup()");
1514
1515         if (pipe(pd) == NOTOK)
1516                 adios("pipe", "unable to");
1517
1518         switch (m_pid = fork()) {
1519         case NOTOK:
1520                 adios("fork", "unable to");
1521
1522         case OK:
1523                 SIGNAL(SIGINT, SIG_DFL);
1524                 SIGNAL(SIGQUIT, SIG_DFL);
1525
1526                 close(pd[1]);
1527                 if (pd[0] != fileno(stdin)) {
1528                         dup2(pd[0], fileno(stdin));
1529                         close(pd[0]);
1530                 }
1531                 execlp(name, mhbasename(name), NULL);
1532                 fprintf(stderr, "unable to exec ");
1533                 perror(name);
1534                 _exit(-1);
1535
1536         default:
1537                 close(pd[0]);
1538                 if (pd[1] != fileno(stdout)) {
1539                         dup2(pd[1], fileno(stdout));
1540                         close(pd[1]);
1541                 }
1542         }
1543 }
1544
1545
1546 void
1547 m_pclose(void)
1548 {
1549         if (m_pid == NOTOK)
1550                 return;
1551
1552         if (sd != NOTOK) {
1553                 fflush(stdout);
1554                 if (dup2(sd, fileno(stdout)) == NOTOK)
1555                         adios("standard output", "unable to dup2()");
1556
1557                 clearerr(stdout);
1558                 close(sd);
1559                 sd = NOTOK;
1560         }
1561         else
1562                 fclose(stdout);
1563
1564         pidwait(m_pid, OK);
1565         m_pid = NOTOK;
1566 }