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