Removed the check for hardcopy terminals. There won't be any today.
[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
273
274 int
275 main(int argc, char **argv)
276 {
277         int length = 0;
278         int i, width = 0, vecp = 0;
279         char *cp, *folder = NULL, *form = NULL;
280         char buf[BUFSIZ], *files[MAXARGS];
281         char **argp, **arguments;
282
283 #ifdef LOCALE
284         setlocale(LC_ALL, "");
285 #endif
286
287         invo_name = mhbasename(argv[0]);
288
289         /* read user profile/context */
290         context_read();
291
292         arguments = getarguments(invo_name, argc, argv, 1);
293         argp = arguments;
294
295         if ((cp = getenv("MHLDEBUG")) && *cp)
296                 mhldebug++;
297
298         while ((cp = *argp++)) {
299                 if (*cp == '-') {
300                         switch (smatch(++cp, mhlswitches)) {
301                         case AMBIGSW:
302                                 ambigsw(cp, mhlswitches);
303                                 done(1);
304                         case UNKWNSW:
305                                 adios(NULL, "-%s unknown\n", cp);
306
307                         case HELPSW:
308                                 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
309                                 print_help(buf, mhlswitches, 1);
310                                 done(1);
311                         case VERSIONSW:
312                                 print_version(invo_name);
313                                 done(1);
314
315                         case BELLSW:
316                                 bellflg = 1;
317                                 continue;
318                         case NBELLSW:
319                                 bellflg = -1;
320                                 continue;
321
322                         case CLRSW:
323                                 clearflg = 1;
324                                 continue;
325                         case NCLRSW:
326                                 clearflg = -1;
327                                 continue;
328
329                         case FOLDSW:
330                                 if (!(folder = *argp++) || *folder == '-')
331                                         adios(NULL, "missing argument to %s",
332                                                         argp[-2]);
333                                 continue;
334                         case FORMSW:
335                                 if (!(form = *argp++) || *form == '-')
336                                         adios(NULL, "missing argument to %s",
337                                                         argp[-2]);
338                                 continue;
339
340                         case SLEEPSW:
341                                 if (!(cp = *argp++) || *cp == '-')
342                                         adios(NULL, "missing argument to %s",
343                                                         argp[-2]);
344                                 sleepsw = atoi(cp);  /* ZERO ok! */
345                                 continue;
346
347                         case PROGSW:
348                                 if (!(moreproc = *argp++) || *moreproc == '-')
349                                         adios(NULL, "missing argument to %s",
350                                                         argp[-2]);
351                                 continue;
352
353                         case LENSW:
354                                 if (!(cp = *argp++) || *cp == '-')
355                                         adios(NULL, "missing argument to %s",
356                                                         argp[-2]);
357                                 if ((length = atoi(cp)) < 1)
358                                         adios(NULL, "bad argument %s %s",
359                                                         argp[-2], cp);
360                                 continue;
361                         case WIDTHSW:
362                                 if (!(cp = *argp++) || *cp == '-')
363                                         adios(NULL, "missing argument to %s",
364                                                         argp[-2]);
365                                 if ((width = atoi(cp)) < 1)
366                                         adios(NULL, "bad argument %s %s",
367                                                         argp[-2], cp);
368                                 continue;
369
370                         case DGSTSW:
371                                 if (!(digest = *argp++) ||
372                                                 *digest == '-')
373                                         adios(NULL, "missing argument to %s",
374                                                         argp[-2]);
375                                 continue;
376                         case ISSUESW:
377                                 if (!(cp = *argp++) || *cp == '-')
378                                         adios(NULL, "missing argument to %s",
379                                                         argp[-2]);
380                                 if ((issue = atoi(cp)) < 1)
381                                         adios(NULL, "bad argument %s %s",
382                                                         argp[-2], cp);
383                                 continue;
384                         case VOLUMSW:
385                                 if (!(cp = *argp++) || *cp == '-')
386                                         adios(NULL, "missing argument to %s",
387                                                         argp[-2]);
388                                 if ((volume = atoi(cp)) < 1)
389                                         adios(NULL, "bad argument %s %s",
390                                                         argp[-2], cp);
391                                 continue;
392
393                         case FORW2SW:
394                                 forwall++;  /* fall */
395                         case FORW1SW:
396                                 forwflg++;
397                                 clearflg = -1;/* XXX */
398                                 continue;
399
400                         case NBODYSW:
401                                 dobody = 0;
402                                 continue;
403                         }
404                 }
405                 files[vecp++] = cp;
406         }
407
408         if (!folder)
409                 folder = getenv("mhfolder");
410
411         if (isatty(fileno(stdout))) {
412                 if (moreproc && *moreproc) {
413                         if (mhl_action) {
414                                 SIGNAL(SIGINT, SIG_IGN);
415                                 SIGNAL2(SIGQUIT, quitser);
416                         }
417                         SIGNAL2(SIGPIPE, pipeser);
418                         m_popen(moreproc);
419                         ontty = PITTY;
420                 } else {
421                         SIGNAL(SIGINT, SIG_IGN);
422                         SIGNAL2(SIGQUIT, quitser);
423                         ontty = ISTTY;
424                 }
425         } else {
426                 ontty = NOTTY;
427         }
428
429         mhl_format(form ? form : mhlformat, length, width);
430
431         if (vecp == 0) {
432                 process(folder, NULL, 1, vecp = 1);
433         } else {
434                 for (i = 0; i < vecp; i++)
435                         process(folder, files[i], i + 1, vecp);
436         }
437
438         if (forwall) {
439                 if (digest) {
440                         printf("%s", delim4);
441                         if (volume == 0) {
442                                 snprintf(buf, sizeof(buf),
443                                         "End of %s Digest\n", digest);
444                         } else {
445                                 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
446                         }
447                         i = strlen(buf);
448                         for (cp = buf + i; i > 1; i--)
449                                 *cp++ = '*';
450                         *cp++ = '\n';
451                         *cp = 0;
452                         printf("%s", buf);
453                 } else
454                         printf("\n------- End of Forwarded Message%s\n\n",
455                                 vecp > 1 ? "s" : "");
456         }
457
458         fflush(stdout);
459         if (ferror(stdout)) {
460                 adios("output", "error writing");
461         }
462
463         if (clearflg > 0 && ontty == NOTTY)
464                 clear_screen();
465
466         if (ontty == PITTY)
467                 m_pclose();
468
469         return exitstat;
470 }
471
472
473 static void
474 mhl_format(char *file, int length, int width)
475 {
476         int i;
477         char *bp, *cp, **ip;
478         char *ap, buffer[BUFSIZ], name[NAMESZ];
479         struct mcomp *c1;
480         struct stat st;
481         FILE *fp;
482         static dev_t dev = 0;
483         static ino_t ino = 0;
484         static time_t mtime = 0;
485
486         if (fmthd != NULL) {
487                 if (stat(etcpath(file), &st) != NOTOK
488                         && mtime == st.st_mtime
489                         && dev == st.st_dev
490                         && ino == st.st_ino)
491                         goto out;
492                 else
493                         free_queue(&fmthd, &fmttl);
494         }
495
496         if ((fp = fopen(etcpath(file), "r")) == NULL)
497                 adios(file, "unable to open format file");
498
499         if (fstat(fileno(fp), &st) != NOTOK) {
500                 mtime = st.st_mtime;
501                 dev = st.st_dev;
502                 ino = st.st_ino;
503         }
504
505         global.c_ovtxt = global.c_nfs = NULL;
506         global.c_fmt = NULL;
507         global.c_offset = 0;
508         global.c_ovoff = -1;
509         if ((i = sc_width()) > 5)
510                 global.c_width = i;
511         global.c_cwidth = -1;
512         if ((i = sc_length()) > 5)
513                 global.c_length = i - 1;
514         global.c_flags = BELL;  /* BELL is default */
515         *(ip = ignores) = NULL;
516
517         while (vfgets(fp, &ap) == OK) {
518                 bp = ap;
519                 if (*bp == ';')
520                         continue;
521
522                 if ((cp = strchr(bp, '\n')))
523                         *cp = 0;
524
525                 if (*bp == ':') {
526                         c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
527                                         CLEARTEXT);
528                         continue;
529                 }
530
531                 parptr = bp;
532                 strncpy(name, parse(), sizeof(name));
533                 switch(*parptr) {
534                 case '\0':
535                 case ',':
536                 case '=':
537                         /*
538                         ** Split this list of fields to ignore, and copy
539                         ** it to the end of the current "ignores" list.
540                         */
541                         if (!mh_strcasecmp(name, "ignores")) {
542                                 char **tmparray, **p;
543                                 int n = 0;
544
545                                 /* split the fields */
546                                 tmparray = brkstring(getcpy(++parptr), ",",
547                                                 NULL);
548
549                                 /* count number of fields split */
550                                 p = tmparray;
551                                 while (*p++)
552                                         n++;
553
554                                 /*
555                                 ** copy pointers to split fields
556                                 ** to ignores array
557                                 */
558                                 ip = copyip(tmparray, ip,
559                                                 MAXARGS - num_ignores);
560                                 num_ignores += n;
561                                 continue;
562                         }
563                         parptr = bp;
564                         while (*parptr) {
565                                 if (evalvar(&global))
566                                         adios(NULL, "format file syntax error: %s", bp);
567                                 if (*parptr)
568                                         parptr++;
569                         }
570                         continue;
571
572                 case ':':
573                         c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
574                         while (*parptr == ':' || *parptr == ',') {
575                                 parptr++;
576                                 if (evalvar(c1))
577                                         adios(NULL, "format file syntax error: %s", bp);
578                         }
579                         if (!c1->c_nfs && global.c_nfs) {
580                                 if ((c1->c_flags & DATEFMT) &&
581                                                 (global.c_flags & DATEFMT)) {
582                                         c1->c_nfs = getcpy(global.c_nfs);
583                                 } else if ((c1->c_flags & ADDRFMT) &&
584                                                 (global.c_flags & ADDRFMT)) {
585                                         c1->c_nfs = getcpy(global.c_nfs);
586                                 }
587                         }
588                         continue;
589
590                 default:
591                         adios(NULL, "format file syntax error: %s", bp);
592                 }
593         }
594         fclose(fp);
595
596         if (mhldebug) {
597                 for (c1 = fmthd; c1; c1 = c1->c_next) {
598                         fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
599                         fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
600                         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);
601                         fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
602                 }
603         }
604
605 out:
606         if (clearflg == 1) {
607                 global.c_flags |= CLEARSCR;
608         } else {
609                 if (clearflg == -1)
610                         global.c_flags &= ~CLEARSCR;
611         }
612
613         switch (bellflg) {  /* command line may override format file */
614         case 1:
615                 global.c_flags |= BELL;
616                 break;
617         case -1:
618                 global.c_flags &= ~BELL;
619                 break;
620         }
621
622         if (length)
623                 global.c_length = length;
624         if (width)
625                 global.c_width = width;
626         if (global.c_length < 5)
627                 global.c_length = 10000;
628         if (global.c_width < 5)
629                 global.c_width = 10000;
630 }
631
632
633 static int
634 evalvar(struct mcomp *c1)
635 {
636         char *cp, name[NAMESZ];
637         struct triple *ap;
638
639         if (!*parptr)
640                 return 0;
641         strncpy(name, parse(), sizeof(name));
642
643         if (!mh_strcasecmp(name, "component")) {
644                 if (ptos(name, &c1->c_text))
645                         return 1;
646                 c1->c_flags &= ~NOCOMPONENT;
647                 return 0;
648         }
649
650         if (!mh_strcasecmp(name, "overflowtext"))
651                 return ptos(name, &c1->c_ovtxt);
652
653         if (!mh_strcasecmp(name, "formatfield")) {
654                 char *nfs;
655
656                 if (ptos(name, &cp))
657                         return 1;
658                 cp = concat("=", cp, NULL);
659                 nfs = new_fs(cp, NULL);
660                 free(cp);
661                 c1->c_nfs = getcpy(nfs);
662                 c1->c_flags |= FORMAT;
663                 return 0;
664         }
665
666         if (!mh_strcasecmp(name, "decode")) {
667                 char *nfs;
668
669                 nfs = new_fs("=%(decode{text})", NULL);
670                 c1->c_nfs = getcpy(nfs);
671                 c1->c_flags |= FORMAT;
672                 return 0;
673         }
674
675         if (!mh_strcasecmp(name, "offset"))
676                 return ptoi(name, &c1->c_offset);
677         if (!mh_strcasecmp(name, "overflowoffset"))
678                 return ptoi(name, &c1->c_ovoff);
679         if (!mh_strcasecmp(name, "width"))
680                 return ptoi(name, &c1->c_width);
681         if (!mh_strcasecmp(name, "compwidth"))
682                 return ptoi(name, &c1->c_cwidth);
683         if (!mh_strcasecmp(name, "length"))
684                 return ptoi(name, &c1->c_length);
685
686         for (ap = triples; ap->t_name; ap++)
687                 if (!mh_strcasecmp(ap->t_name, name)) {
688                         c1->c_flags |= ap->t_on;
689                         c1->c_flags &= ~ap->t_off;
690                         return 0;
691                 }
692
693         return 1;
694 }
695
696
697 static int
698 ptoi(char *name, int *i)
699 {
700         char *cp;
701
702         if (*parptr++ != '=' || !*(cp = parse())) {
703                 advise(NULL, "missing argument to variable %s", name);
704                 return 1;
705         }
706
707         *i = atoi(cp);
708         return 0;
709 }
710
711
712 static int
713 ptos(char *name, char **s)
714 {
715         char c, *cp;
716
717         if (*parptr++ != '=') {
718                 advise(NULL, "missing argument to variable %s", name);
719                 return 1;
720         }
721
722         if (*parptr != '"') {
723                 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
724                                 parptr++)
725                         continue;
726         } else {
727                 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
728                         if (*parptr == QUOTE)
729                                 if (!*++parptr)
730                                         parptr--;
731         }
732         c = *parptr;
733         *parptr = 0;
734         *s = getcpy(cp);
735         if ((*parptr = c) == '"')
736                 parptr++;
737         return 0;
738 }
739
740
741 static char *
742 parse(void)
743 {
744         int c;
745         char *cp;
746         static char result[NAMESZ];
747
748         for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
749                 c = *parptr;
750                 if (isalnum(c)
751                                 || c == '.'
752                                 || c == '-'
753                                 || c == '_'
754                                 || c == '['
755                                 || c == ']')
756                         *cp++ = c;
757                 else
758                         break;
759         }
760         *cp = '\0';
761
762         return result;
763 }
764
765
766 static void
767 process(char *folder, char *fname, int ofilen, int ofilec)
768 {
769         char *cp = NULL;
770         FILE *fp = NULL;
771         struct mcomp *c1;
772
773         switch (setjmp(env)) {
774         case OK:
775                 if (fname) {
776                         fp = mhl_action ? (*mhl_action) (fname) :
777                                         fopen(fname, "r");
778                         if (fp == NULL) {
779                                 advise(fname, "unable to open");
780                                 exitstat++;
781                                 return;
782                         }
783                 } else {
784                         fname = "(stdin)";
785                         fp = stdin;
786                 }
787                 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
788                 if (ontty != PITTY)
789                         SIGNAL(SIGINT, intrser);
790                 mhlfile(fp, cp, ofilen, ofilec);  /* FALL THROUGH! */
791
792         default:
793                 if (ontty != PITTY)
794                         SIGNAL(SIGINT, SIG_IGN);
795                 if (mhl_action == NULL && fp != stdin)
796                         fclose(fp);
797                 free(cp);
798                 if (holder.c_text) {
799                         free(holder.c_text);
800                         holder.c_text = NULL;
801                 }
802                 free_queue(&msghd, &msgtl);
803                 for (c1 = fmthd; c1; c1 = c1->c_next)
804                         c1->c_flags &= ~HDROUTPUT;
805                 break;
806         }
807 }
808
809
810 static void
811 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
812 {
813         int state;
814         struct mcomp *c1, *c2, *c3;
815         char **ip, name[NAMESZ], buf[BUFSIZ];
816
817         if (forwall) {
818                 if (digest)
819                         printf("%s", ofilen == 1 ? delim3 : delim4);
820                 else {
821                         printf("\n-------");
822                         if (ofilen == 1)
823                                 printf(" Forwarded Message%s",
824                                                 ofilec > 1 ? "s" : "");
825                         else
826                                 printf(" Message %d", ofilen);
827                         printf("\n\n");
828                 }
829         } else {
830                 switch (ontty) {
831                 case PITTY:
832                         if (ofilec > 1) {
833                                 if (ofilen > 1) {
834                                         if ((global.c_flags & CLEARSCR))
835                                                 clear_screen();
836                                         else
837                                                 printf("\n\n\n");
838                                 }
839                                 printf(">>> %s\n\n", mname);
840                         }
841                         break;
842
843                 case ISTTY:
844                         strncpy(buf, "\n", sizeof(buf));
845                         if (ofilec > 1) {
846                                 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
847                                         if (ofilen > 1)
848                                                 printf("\n\n\n");
849                                         printf("Press <return> to list \"%s\"...", mname);
850                                 }
851                                 fflush(stdout);
852                                 buf[0] = 0;
853                                 read(fileno(stdout), buf, sizeof(buf));
854                         }
855                         if (strchr(buf, '\n')) {
856                                 if ((global.c_flags & CLEARSCR))
857                                         clear_screen();
858                         } else
859                                 printf("\n");
860                         break;
861
862                 default:
863                         if (ofilec > 1) {
864                                 if (ofilen > 1) {
865                                         printf("\n\n\n");
866                                         if (clearflg > 0)
867                                                 clear_screen();
868                                 }
869                                 printf(">>> %s\n\n", mname);
870                         }
871                         break;
872                 }
873         }
874
875         for (state = FLD;;) {
876                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
877                 case FLD:
878                 case FLDPLUS:
879                         for (ip = ignores; *ip; ip++)
880                                 if (!mh_strcasecmp(name, *ip)) {
881                                         while (state == FLDPLUS)
882                                                 state = m_getfld(state, name, buf, sizeof(buf), fp);
883                                         break;
884                                 }
885                         if (*ip)
886                                 continue;
887
888                         for (c2 = fmthd; c2; c2 = c2->c_next)
889                                 if (!mh_strcasecmp(c2->c_name, name))
890                                         break;
891                         c1 = NULL;
892                         if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
893                                 for (c1 = msghd; c1; c1 = c1->c_next)
894                                         if (!mh_strcasecmp(c1->c_name,
895                                                         c3->c_name)) {
896                                                 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
897                                                 break;
898                                         }
899                         if (c1 == NULL)
900                                 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
901                         while (state == FLDPLUS) {
902                                 state = m_getfld(state, name, buf,
903                                                 sizeof(buf), fp);
904                                 c1->c_text = add(buf, c1->c_text);
905                         }
906                         if (c2 == NULL)
907                                 c1->c_flags |= EXTRA;
908                         continue;
909
910                 case BODY:
911                 case FILEEOF:
912                         row = column = 0;
913                         for (c1 = fmthd; c1; c1 = c1->c_next) {
914                                 if (c1->c_flags & CLEARTEXT) {
915                                         putcomp(c1, c1, ONECOMP);
916                                         continue;
917                                 }
918                                 if (!mh_strcasecmp(c1->c_name, "messagename")) {
919                                         holder.c_text = concat("(Message ",
920                                                         mname, ")\n", NULL);
921                                         putcomp(c1, &holder, ONECOMP);
922                                         free(holder.c_text);
923                                         holder.c_text = NULL;
924                                         continue;
925                                 }
926                                 if (!mh_strcasecmp(c1->c_name, "extras")) {
927                                         for (c2 = msghd; c2; c2 = c2->c_next)
928                                                 if (c2->c_flags & EXTRA)
929                                                         putcomp(c1, c2, TWOCOMP);
930                                         continue;
931                                 }
932                                 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
933                                         holder.c_text = mh_xmalloc(sizeof(buf));
934                                         strncpy(holder.c_text, buf, sizeof(buf));
935                                         while (state == BODY) {
936                                                 putcomp(c1, &holder, BODYCOMP);
937                                                 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
938                                         }
939                                         free(holder.c_text);
940                                         holder.c_text = NULL;
941                                         continue;
942                                 }
943                                 for (c2 = msghd; c2; c2 = c2->c_next)
944                                         if (!mh_strcasecmp(c2->c_name,
945                                                         c1->c_name)) {
946                                                 putcomp(c1, c2, ONECOMP);
947                                                 if (!(c1->c_flags & SPLIT))
948                                                         break;
949                                         }
950                         }
951                         return;
952
953                 case LENERR:
954                 case FMTERR:
955                         advise(NULL, "format error in message %s", mname);
956                         exitstat++;
957                         return;
958
959                 default:
960                         adios(NULL, "getfld() returned %d", state);
961                 }
962         }
963 }
964
965
966 static int
967 mcomp_flags(char *name)
968 {
969         struct pair *ap;
970
971         for (ap = pairs; ap->p_name; ap++)
972                 if (!mh_strcasecmp(ap->p_name, name))
973                         return (ap->p_flags);
974
975         return 0;
976 }
977
978
979 static char *
980 mcomp_add(long flags, char *s1, char *s2)
981 {
982         char *dp;
983
984         if (!(flags & ADDRFMT))
985                 return add(s1, s2);
986
987         if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
988                 *dp = 0;
989
990         return add(s1, add(",\n", s2));
991 }
992
993
994 struct pqpair {
995         char *pq_text;
996         char *pq_error;
997         struct pqpair *pq_next;
998 };
999
1000
1001 static void
1002 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1003 {
1004         int dat[5];
1005         char *ap, *cp;
1006         char buffer[BUFSIZ], error[BUFSIZ];
1007         struct comp *cptr;
1008         struct pqpair *p, *q;
1009         struct pqpair pq;
1010         struct mailname *mp;
1011
1012         ap = c2->c_text;
1013         c2->c_text = NULL;
1014         dat[0] = 0;
1015         dat[1] = 0;
1016         dat[2] = 0;
1017         dat[3] = sizeof(buffer) - 1;
1018         dat[4] = 0;
1019         fmt_compile(c1->c_nfs, &c1->c_fmt);
1020
1021         if (!(c1->c_flags & ADDRFMT)) {
1022                 FINDCOMP(cptr, "text");
1023                 if (cptr)
1024                         cptr->c_text = ap;
1025                 if ((cp = strrchr(ap, '\n')))  /* drop ending newline */
1026                         if (!cp[1])
1027                                 *cp = 0;
1028
1029                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1030                 /* Don't need to append a newline, dctime() already did */
1031                 c2->c_text = getcpy(buffer);
1032
1033                 free(ap);
1034                 return;
1035         }
1036
1037         (q = &pq)->pq_next = NULL;
1038         while ((cp = getname(ap))) {
1039                 if ((p = (struct pqpair *)
1040                                 calloc((size_t) 1, sizeof(*p))) == NULL)
1041                         adios(NULL, "unable to allocate pqpair memory");
1042
1043                 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1044                         p->pq_text = getcpy(cp);
1045                         p->pq_error = getcpy(error);
1046                 } else {
1047                         p->pq_text = getcpy(mp->m_text);
1048                         mnfree(mp);
1049                 }
1050                 q = (q->pq_next = p);
1051         }
1052
1053         for (p = pq.pq_next; p; p = q) {
1054                 FINDCOMP(cptr, "text");
1055                 if (cptr)
1056                         cptr->c_text = p->pq_text;
1057                 FINDCOMP(cptr, "error");
1058                 if (cptr)
1059                         cptr->c_text = p->pq_error;
1060
1061                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1062                 if (*buffer) {
1063                         if (c2->c_text)
1064                                 c2->c_text = add(",\n", c2->c_text);
1065                         if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1066                                 *cp = 0;
1067                         c2->c_text = add(buffer, c2->c_text);
1068                 }
1069
1070                 free(p->pq_text);
1071                 if (p->pq_error)
1072                         free(p->pq_error);
1073                 q = p->pq_next;
1074                 free((char *) p);
1075         }
1076
1077         c2->c_text = add("\n", c2->c_text);
1078         free (ap);
1079 }
1080
1081
1082 static struct mcomp *
1083 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1084                 char *text, int flags)
1085 {
1086         struct mcomp *c1;
1087
1088         if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1089                 adios(NULL, "unable to allocate comp memory");
1090
1091         c1->c_flags = flags & ~INIT;
1092         if ((c1->c_name = name ? getcpy(name) : NULL))
1093                 c1->c_flags |= mcomp_flags(c1->c_name);
1094         c1->c_text = text ? getcpy(text) : NULL;
1095         if (flags & INIT) {
1096                 if (global.c_ovtxt)
1097                         c1->c_ovtxt = getcpy(global.c_ovtxt);
1098                 c1->c_offset = global.c_offset;
1099                 c1->c_ovoff = global. c_ovoff;
1100                 c1->c_width = c1->c_length = 0;
1101                 c1->c_cwidth = global.c_cwidth;
1102                 c1->c_flags |= global.c_flags & GFLAGS;
1103         }
1104         if (*head == NULL)
1105                 *head = c1;
1106         if (*tail != NULL)
1107                 (*tail)->c_next = c1;
1108         *tail = c1;
1109
1110         return c1;
1111 }
1112
1113
1114 static void
1115 free_queue(struct mcomp **head, struct mcomp **tail)
1116 {
1117         struct mcomp *c1, *c2;
1118
1119         for (c1 = *head; c1; c1 = c2) {
1120                 c2 = c1->c_next;
1121                 if (c1->c_name)
1122                         free(c1->c_name);
1123                 if (c1->c_text)
1124                         free(c1->c_text);
1125                 if (c1->c_ovtxt)
1126                         free(c1->c_ovtxt);
1127                 if (c1->c_nfs)
1128                         free(c1->c_nfs);
1129                 if (c1->c_fmt)
1130                         free((char *) c1->c_fmt);
1131                 free((char *) c1);
1132         }
1133
1134         *head = *tail = NULL;
1135 }
1136
1137
1138 static void
1139 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1140 {
1141         int count, cchdr;
1142         unsigned char *cp;
1143
1144         cchdr = 0;
1145         lm = 0;
1146         llim = c1->c_length ? c1->c_length : -1;
1147         wid = c1->c_width ? c1->c_width : global.c_width;
1148         ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1149                         + c1->c_offset;
1150         if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1151                 ovtxt = "";
1152         if (wid < ovoff + strlen(ovtxt) + 5)
1153                 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1154         onelp = NULL;
1155
1156         if (c1->c_flags & CLEARTEXT) {
1157                 putstr(c1->c_text);
1158                 putstr("\n");
1159                 return;
1160         }
1161
1162         if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1163                 mcomp_format(c1, c2);
1164
1165         if (c1->c_flags & CENTER) {
1166                 count = (c1->c_width ? c1->c_width : global.c_width)
1167                                 - c1->c_offset - strlen(c2->c_text);
1168                 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1169                         count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1170                                         + 2;
1171                 lm = c1->c_offset + (count / 2);
1172         } else {
1173                 if (c1->c_offset)
1174                         lm = c1->c_offset;
1175         }
1176
1177         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1178                 if (c1->c_flags & UPPERCASE)  /* uppercase component also */
1179                         for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1180                                 if (islower(*cp))
1181                                         *cp = toupper(*cp);
1182                 putstr(c1->c_text ? c1->c_text : c1->c_name);
1183                 if (flag != BODYCOMP) {
1184                         putstr(": ");
1185                         if (!(c1->c_flags & SPLIT))
1186                                 c1->c_flags |= HDROUTPUT;
1187
1188                 cchdr++;
1189                 if ((count = c1->c_cwidth -
1190                         strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1191                         while (count--)
1192                                 putstr(" ");
1193                 } else
1194                         c1->c_flags |= HDROUTPUT;  /* for BODYCOMP */
1195         }
1196
1197         if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1198                 && !(c2->c_flags & NOCOMPONENT)) {
1199                 if (c1->c_flags & UPPERCASE)
1200                         for (cp = c2->c_name; *cp; cp++)
1201                                 if (islower(*cp))
1202                                         *cp = toupper(*cp);
1203                 putstr(c2->c_name);
1204                 putstr(": ");
1205                 if (!(c1->c_flags & SPLIT))
1206                         c2->c_flags |= HDROUTPUT;
1207
1208                 cchdr++;
1209                 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1210                         while (count--)
1211                                 putstr(" ");
1212         }
1213         if (c1->c_flags & UPPERCASE)
1214                 for (cp = c2->c_text; *cp; cp++)
1215                         if (islower(*cp))
1216                                 *cp = toupper(*cp);
1217
1218         count = 0;
1219         if (cchdr) {
1220                 if (flag == TWOCOMP)
1221                         count = (c1->c_cwidth >= 0) ?
1222                                         c1->c_cwidth : strlen(c2->c_name) + 2;
1223                 else
1224                         count = (c1->c_cwidth >= 0) ?
1225                                         c1->c_cwidth : strlen(c1->c_text ?
1226                                         c1->c_text : c1->c_name) + 2;
1227         }
1228         count += c1->c_offset;
1229
1230         if ((cp = oneline(c2->c_text, c1->c_flags)))
1231            putstr(cp);
1232         if (term == '\n')
1233                 putstr("\n");
1234         while ((cp = oneline(c2->c_text, c1->c_flags))) {
1235                 lm = count;
1236                 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1237                         putstr(c1->c_text ? c1->c_text : c1->c_name);
1238                 if (*cp)
1239                         putstr(cp);
1240                 if (term == '\n')
1241                         putstr("\n");
1242         }
1243         if (flag == BODYCOMP && term == '\n')
1244                 c1->c_flags &= ~HDROUTPUT;  /* Buffer ended on a newline */
1245 }
1246
1247
1248 static char *
1249 oneline(char *stuff, long flags)
1250 {
1251         int spc;
1252         char *cp, *ret;
1253
1254         if (onelp == NULL)
1255                 onelp = stuff;
1256         if (*onelp == 0)
1257                 return (onelp = NULL);
1258
1259         ret = onelp;
1260         term = 0;
1261         if (flags & COMPRESS) {
1262                 for (spc = 1, cp = ret; *onelp; onelp++)
1263                         if (isspace(*onelp)) {
1264                                 if (*onelp == '\n' &&
1265                                                 (!onelp[1] ||
1266                                                 (flags & ADDRFMT))) {
1267                                         term = '\n';
1268                                         *onelp++ = 0;
1269                                         break;
1270                                 } else if (!spc) {
1271                                         *cp++ = ' ';
1272                                         spc++;
1273                                 }
1274                         } else {
1275                                 *cp++ = *onelp;
1276                                 spc = 0;
1277                         }
1278
1279                 *cp = 0;
1280         } else {
1281                 while (*onelp && *onelp != '\n')
1282                         onelp++;
1283                 if (*onelp == '\n') {
1284                         term = '\n';
1285                         *onelp++ = 0;
1286                 }
1287                 if (flags & LEFTADJUST)
1288                         while (*ret == ' ' || *ret == '\t')
1289                                 ret++;
1290         }
1291         if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1292                 term = 0;
1293
1294         return ret;
1295 }
1296
1297
1298 static void
1299 putstr(char *string)
1300 {
1301         if (!column && lm > 0) {
1302                 while (lm > 0)
1303                         if (lm >= 8) {
1304                                 putch('\t');
1305                                 lm -= 8;
1306                         } else {
1307                                 putch(' ');
1308                                 lm--;
1309                         }
1310         }
1311         lm = 0;
1312         while (*string)
1313                 putch(*string++);
1314 }
1315
1316
1317 static void
1318 putch(char ch)
1319 {
1320         char buf[BUFSIZ];
1321
1322         if (llim == 0)
1323                 return;
1324
1325         switch (ch) {
1326         case '\n':
1327                 if (llim > 0)
1328                         llim--;
1329                 column = 0;
1330                 row++;
1331                 if (ontty != ISTTY || row != global.c_length)
1332                         break;
1333                 if (global.c_flags & BELL)
1334                         putchar('\007');
1335                 fflush(stdout);
1336                 buf[0] = 0;
1337                 read(fileno(stdout), buf, sizeof(buf));
1338                 if (strchr(buf, '\n')) {
1339                         if (global.c_flags & CLEARSCR)
1340                                 clear_screen();
1341                         row = 0;
1342                 } else {
1343                         putchar('\n');
1344                         row = global.c_length / 3;
1345                 }
1346                 return;
1347
1348         case '\t':
1349                 column |= 07;
1350                 column++;
1351                 break;
1352
1353         case '\b':
1354                 column--;
1355                 break;
1356
1357         case '\r':
1358                 column = 0;
1359                 break;
1360
1361         default:
1362                 /*
1363                 ** If we are forwarding this message, and the first
1364                 ** column contains a dash, then add a dash and a space.
1365                 */
1366                 if (column == 0 && forwflg && ch == '-') {
1367                         putchar('-');
1368                         putchar(' ');
1369                 }
1370                 if (ch >= ' ')
1371                         column++;
1372                 break;
1373         }
1374
1375         if (column >= wid) {
1376                 putch('\n');
1377                 if (ovoff > 0)
1378                         lm = ovoff;
1379                 putstr(ovtxt ? ovtxt : "");
1380                 putch(ch);
1381                 return;
1382         }
1383
1384         putchar(ch);
1385 }
1386
1387
1388 static RETSIGTYPE
1389 intrser(int i)
1390 {
1391 #ifndef RELIABLE_SIGNALS
1392         SIGNAL(SIGINT, intrser);
1393 #endif
1394
1395         discard(stdout);
1396         putchar('\n');
1397         longjmp(env, DONE);
1398 }
1399
1400
1401 static RETSIGTYPE
1402 pipeser(int i)
1403 {
1404 #ifndef RELIABLE_SIGNALS
1405         SIGNAL(SIGPIPE, pipeser);
1406 #endif
1407
1408         done(NOTOK);
1409 }
1410
1411
1412 static RETSIGTYPE
1413 quitser(int i)
1414 {
1415 #ifndef RELIABLE_SIGNALS
1416         SIGNAL(SIGQUIT, quitser);
1417 #endif
1418
1419         putchar('\n');
1420         fflush(stdout);
1421         done(NOTOK);
1422 }
1423
1424
1425 #undef adios
1426 #undef done
1427
1428 static void
1429 mhladios(char *what, char *fmt, ...)
1430 {
1431         va_list ap;
1432
1433         va_start(ap, fmt);
1434         advertise(what, NULL, fmt, ap);
1435         va_end(ap);
1436         mhldone(1);
1437 }
1438
1439
1440 static void
1441 mhldone(int status)
1442 {
1443         exitstat = status;
1444         if (mhl_action)
1445                 longjmp(mhlenv, DONE);
1446         else
1447                 done(exitstat);
1448 }
1449
1450
1451 static int m_pid = NOTOK;
1452 static int sd = NOTOK;
1453
1454 static void
1455 m_popen(char *name)
1456 {
1457         int pd[2];
1458
1459         if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1460                 adios("standard output", "unable to dup()");
1461
1462         if (pipe(pd) == NOTOK)
1463                 adios("pipe", "unable to");
1464
1465         switch (m_pid = fork()) {
1466         case NOTOK:
1467                 adios("fork", "unable to");
1468
1469         case OK:
1470                 SIGNAL(SIGINT, SIG_DFL);
1471                 SIGNAL(SIGQUIT, SIG_DFL);
1472
1473                 close(pd[1]);
1474                 if (pd[0] != fileno(stdin)) {
1475                         dup2(pd[0], fileno(stdin));
1476                         close(pd[0]);
1477                 }
1478                 execlp(name, mhbasename(name), NULL);
1479                 fprintf(stderr, "unable to exec ");
1480                 perror(name);
1481                 _exit(-1);
1482
1483         default:
1484                 close(pd[0]);
1485                 if (pd[1] != fileno(stdout)) {
1486                         dup2(pd[1], fileno(stdout));
1487                         close(pd[1]);
1488                 }
1489         }
1490 }
1491
1492
1493 void
1494 m_pclose(void)
1495 {
1496         if (m_pid == NOTOK)
1497                 return;
1498
1499         if (sd != NOTOK) {
1500                 fflush(stdout);
1501                 if (dup2(sd, fileno(stdout)) == NOTOK)
1502                         adios("standard output", "unable to dup2()");
1503
1504                 clearerr(stdout);
1505                 close(sd);
1506                 sd = NOTOK;
1507         }
1508         else
1509                 fclose(stdout);
1510
1511         pidwait(m_pid, OK);
1512         m_pid = NOTOK;
1513 }