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