remove unnecessary casts
[mmh] / uip / repl.c
1 /*
2 ** repl.c -- reply to a message
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/utils.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
13 #include <sys/file.h>  /* L_SET */
14 #include <errno.h>
15 #include <unistd.h>
16 #include <ctype.h>
17 #include <sys/stat.h>
18 #include <locale.h>
19 #include <sysexits.h>
20
21 static struct swit switches[] = {
22 #define GROUPSW  0
23         { "group", 0 },
24 #define NGROUPSW  1
25         { "nogroup", 2 },
26 #define ANNOSW  2
27         { "annotate", 0 },
28 #define NANNOSW  3
29         { "noannotate", 2 },
30 #define CCSW  4
31         { "cc all|to|cc|me", 0 },
32 #define NCCSW  5
33         { "nocc type", 2 },
34 #define EDITRSW  6
35         { "editor editor", 0 },
36 #define FILTSW  7
37         { "filter filterfile", 0 },
38 #define NFILTSW  8
39         { "nofilter", 2 },
40 #define FORMSW  9
41         { "form formfile", 0 },
42 #define MIMESW  10
43         { "mime", 0 },
44 #define NMIMESW  11
45         { "nomime", 2 },
46 #define QURYSW  12
47         { "query", 0 },
48 #define NQURYSW  13
49         { "noquery", 2 },
50 #define WHATSW  14
51         { "whatnowproc program", 0 },
52 #define VERSIONSW  15
53         { "Version", 0 },
54 #define HELPSW  16
55         { "help", 0 },
56 #define FILESW  17
57         { "file file", 4 },  /* interface from msh */
58 #define BILDSW  18
59         { "build", 5 },  /* interface from mhe */
60         { NULL, 0 }
61 };
62
63 static struct swit ccswitches[] = {
64 #define CTOSW  0
65         { "to", 0 },
66 #define CCCSW  1
67         { "cc", 0 },
68 #define CMESW  2
69         { "me", 0 },
70 #define CALSW  3
71         { "all", 0 },
72         { NULL, 0 }
73 };
74
75 /*
76 ** Buffer size for content part of header fields.
77 ** We want this to be large enough so that we don't
78 ** do a lot of extra FLDPLUS calls on m_getfld but
79 ** small enough so that we don't snarf the entire
80 ** message body when we're not going to use any of it.
81 */
82 #define SBUFSIZ 256
83
84 static short ccto = -1;
85 static short cccc = -1;
86 static short ccme = -1;
87 static short querysw = 0;
88
89 static short groupreply = 0;  /* Is this a group reply? */
90
91 static int mime = 0;  /* include original as MIME part */
92 static char *form   = NULL;  /* form (components) file */
93 static char *filter = NULL;  /* message filter file */
94
95 static int dftype=0;
96
97 static char *badaddrs = NULL;
98 static char *dfhost = NULL;
99
100 static struct mailname mq;
101
102 static struct format *fmt;
103
104 static int ncomps = 0;  /* # of interesting components */
105 static char **compbuffers = NULL;  /* buffers for component text */
106 static struct comp **used_buf = NULL;  /* stack for comp that use buffers */
107
108 static int dat[5];  /* aux. data for format routine */
109
110 static char *addrcomps[] = {
111         "from",
112         "sender",
113         "reply-to",
114         "to",
115         "cc",
116         "bcc",
117         "resent-from",
118         "resent-sender",
119         "resent-reply-to",
120         "resent-to",
121         "resent-cc",
122         "resent-bcc",
123         NULL
124 };
125
126 /*
127 ** static prototypes
128 */
129 static void docc(char *, int);
130 static int insert(struct mailname *);
131 static void replfilter(FILE *, FILE *, char *);
132 static void replout(FILE *, char *, struct msgs *, int,
133                 char *, char *);
134
135
136 int
137 main(int argc, char **argv)
138 {
139         int anot = 0;
140         char *cp, *cwd, *maildir, *file = NULL;
141         char *folder = NULL, *msg = NULL;
142         char *ed = NULL, drft[BUFSIZ], buf[BUFSIZ];
143         char **argp, **arguments;
144         struct msgs *mp = NULL;
145         FILE *in;
146         int buildsw = 0;
147
148         setlocale(LC_ALL, "");
149         invo_name = mhbasename(argv[0]);
150
151         /* read user profile/context */
152         context_read();
153
154         filter = getcpy(etcpath(mhlreply));
155
156         arguments = getarguments(invo_name, argc, argv, 1);
157         argp = arguments;
158
159         while ((cp = *argp++)) {
160                 if (*cp == '-') {
161                         switch (smatch(++cp, switches)) {
162                         case AMBIGSW:
163                                 ambigsw(cp, switches);
164                                 exit(EX_USAGE);
165                         case UNKWNSW:
166                                 adios(EX_USAGE, NULL, "-%s unknown", cp);
167
168                         case HELPSW:
169                                 snprintf(buf, sizeof(buf), "%s: [+folder] [msg] [switches]", invo_name);
170                                 print_help(buf, switches, 1);
171                                 exit(argc == 2 ? EX_OK : EX_USAGE);
172                         case VERSIONSW:
173                                 print_version(invo_name);
174                                 exit(argc == 2 ? EX_OK : EX_USAGE);
175
176                         case GROUPSW:
177                                 groupreply++;
178                                 continue;
179                         case NGROUPSW:
180                                 groupreply = 0;
181                                 continue;
182
183                         case ANNOSW:
184                                 anot++;
185                                 continue;
186                         case NANNOSW:
187                                 anot = 0;
188                                 continue;
189
190                         case CCSW:
191                                 if (!(cp = *argp++) || *cp == '-')
192                                         adios(EX_USAGE, NULL, "missing argument to %s",
193                                                         argp[-2]);
194                                 docc(cp, 1);
195                                 continue;
196                         case NCCSW:
197                                 if (!(cp = *argp++) || *cp == '-')
198                                         adios(EX_USAGE, NULL, "missing argument to %s",
199                                                         argp[-2]);
200                                 docc(cp, 0);
201                                 continue;
202
203                         case EDITRSW:
204                                 if (!(ed = *argp++) || *ed == '-')
205                                         adios(EX_USAGE, NULL, "missing argument to %s",
206                                                         argp[-2]);
207                                 continue;
208
209                         case WHATSW:
210                                 if (!(whatnowproc = *argp++) ||
211                                                 *whatnowproc == '-')
212                                         adios(EX_USAGE, NULL, "missing argument to %s",
213                                                         argp[-2]);
214                                 continue;
215
216                         case BILDSW:
217                                 buildsw++;
218                                 continue;
219
220                         case FILESW:
221                                 if (file)
222                                         adios(EX_USAGE, NULL, "only one file at a time!");
223                                 if (!(cp = *argp++) || *cp == '-')
224                                         adios(EX_USAGE, NULL, "missing argument to %s",
225                                                         argp[-2]);
226                                 file = getcpy(expanddir(cp));
227                                 continue;
228                         case FORMSW:
229                                 if (!(form = *argp++) || *form == '-')
230                                         adios(EX_USAGE, NULL, "missing argument to %s",
231                                                         argp[-2]);
232                                 continue;
233
234                         case FILTSW:
235                                 if (!(cp = *argp++) || *cp == '-')
236                                         adios(EX_USAGE, NULL, "missing argument to %s",
237                                                         argp[-2]);
238                                 filter = getcpy(etcpath(cp));
239                                 continue;
240                         case NFILTSW:
241                                 filter = NULL;
242                                 continue;
243
244                         case MIMESW:
245                                 mime++;
246                                 continue;
247                         case NMIMESW:
248                                 mime = 0;
249                                 continue;
250
251                         case QURYSW:
252                                 querysw++;
253                                 continue;
254                         case NQURYSW:
255                                 querysw = 0;
256                                 continue;
257
258                         }
259                 }
260                 if (*cp == '+' || *cp == '@') {
261                         if (folder)
262                                 adios(EX_USAGE, NULL, "only one folder at a time!");
263                         else
264                                 folder = getcpy(expandfol(cp));
265                 } else {
266                         if (msg)
267                                 adios(EX_USAGE, NULL, "only one message at a time!");
268                         else
269                                 msg = cp;
270                 }
271         }
272
273         if (ccto == -1)
274                 ccto = groupreply;
275         if (cccc == -1)
276                 cccc = groupreply;
277         if (ccme == -1)
278                 ccme = groupreply;
279
280         cwd = getcpy(pwd());
281
282         if (file && (msg || folder))
283                 adios(EX_USAGE, NULL, "can't mix files and folders/msgs");
284
285         strncpy(drft, buildsw ? toabsdir("reply") : m_draft(seq_beyond),
286                         sizeof(drft));
287         /*
288         ** FIXME: (concerning MHE support (buildsw) only)
289         ** There's no check if the draft already exists. mmh has removed
290         ** this case by having the draft folder. I won't add code only to
291         ** handle this legacy issue for MHE. -- meillo@marmaro.de 2012-05
292         */
293
294         if (file) {
295                 /*
296                 ** We are replying to a file.
297                 */
298                 anot = 0;  /* we don't want to annotate a file */
299         } else {
300                 /*
301                 ** We are replying to a message.
302                 */
303                 if (!msg)
304                         msg = seq_cur;
305                 if (!folder)
306                         folder = getcurfol();
307                 maildir = toabsdir(folder);
308
309                 if (chdir(maildir) == NOTOK)
310                         adios(EX_OSERR, maildir, "unable to change directory to");
311
312                 /* read folder and create message structure */
313                 if (!(mp = folder_read(folder)))
314                         adios(EX_IOERR, NULL, "unable to read folder %s", folder);
315
316                 /* check for empty folder */
317                 if (mp->nummsg == 0)
318                         adios(EX_DATAERR, NULL, "no messages in %s", folder);
319
320                 /* parse the message range/sequence/name and set SELECTED */
321                 if (!m_convert(mp, msg))
322                         exit(EX_SOFTWARE);
323                 seq_setprev(mp);  /* set the previous-sequence */
324
325                 if (mp->numsel > 1)
326                         adios(EX_USAGE, NULL, "only one message at a time!");
327
328                 context_replace(curfolder, folder); /* update current folder */
329                 seq_setcur(mp, mp->lowsel);  /* update current message  */
330                 seq_save(mp);  /* synchronize sequences   */
331                 context_save();  /* save the context file   */
332         }
333
334         msg = file ? file : getcpy(m_name(mp->lowsel));
335
336         if ((in = fopen(msg, "r")) == NULL)
337                 adios(EX_IOERR, msg, "unable to open");
338
339         /* find form (components) file */
340         if (!form) {
341                 if (groupreply)
342                         form = etcpath(replgroupcomps);
343                 else
344                         form = etcpath(replcomps);
345         }
346
347         replout(in, drft, mp, mime, form, filter);
348         fclose(in);
349
350         if (buildsw)
351                 exit(EX_OK);
352         what_now(ed, NOUSE, drft, msg, 0, mp, anot ? "Replied" : NULL, cwd);
353         return EX_OSERR;
354 }
355
356 static void
357 docc(char *cp, int ccflag)
358 {
359         switch (smatch(cp, ccswitches)) {
360         case AMBIGSW:
361                 ambigsw(cp, ccswitches);
362                 exit(EX_USAGE);
363         case UNKWNSW:
364                 adios(EX_USAGE, NULL, "-%scc %s unknown", ccflag ? "" : "no", cp);
365
366         case CTOSW:
367                 ccto = ccflag;
368                 break;
369
370         case CCCSW:
371                 cccc = ccflag;
372                 break;
373
374         case CMESW:
375                 ccme = ccflag;
376                 break;
377
378         case CALSW:
379                 ccto = cccc = ccme = ccflag;
380                 break;
381         }
382 }
383
384
385
386
387 static void
388 replout(FILE *inb, char *drft, struct msgs *mp,
389         int mime, char *form, char *filter)
390 {
391         int state, i;
392         struct comp *cptr;
393         char *tmpbuf;
394         char **nxtbuf;
395         char **ap;
396         struct comp **savecomp;
397         int char_read = 0, format_len, mask;
398         char name[NAMESZ], *scanl;
399         unsigned char *cp;
400         FILE *out;
401
402         mask = umask(~m_gmprot());
403         if ((out = fopen(drft, "w")) == NULL)
404                 adios(EX_CANTCREAT, drft, "unable to create");
405
406         umask(mask);
407
408         /* get new format string */
409         cp = new_fs(form, NULL);
410         format_len = strlen(cp);
411
412         /* compile format string */
413         ncomps = fmt_compile(cp, &fmt) + 1;
414
415         nxtbuf = compbuffers = mh_xcalloc(ncomps, sizeof(char *));
416         savecomp = used_buf = mh_xcalloc(ncomps+1, sizeof(struct comp *));
417         savecomp += ncomps + 1;
418         *--savecomp = NULL;  /* point at zero'd end minus 1 */
419
420         for (i = ncomps; i--; )
421                 *nxtbuf++ = mh_xcalloc(SBUFSIZ, sizeof(char));
422
423         nxtbuf = compbuffers;  /* point at start */
424         tmpbuf = *nxtbuf++;
425
426         for (ap = addrcomps; *ap; ap++) {
427                 FINDCOMP(cptr, *ap);
428                 if (cptr)
429                         cptr->c_type |= CT_ADDR;
430         }
431
432         /*
433         ** ignore any components killed by command line switches
434         */
435         if (!ccto) {
436                 FINDCOMP(cptr, "to");
437                 if (cptr)
438                         cptr->c_name = "";
439         }
440         if (!cccc) {
441                 FINDCOMP(cptr, "cc");
442                 if (cptr)
443                         cptr->c_name = "";
444         }
445         if ((cp = getenv("USER"))) {
446                 FINDCOMP(cptr, "user");
447                 if (cptr)
448                         cptr->c_text = getcpy(cp);
449         }
450         if (!ccme)
451                 ismymbox(NULL);
452
453         /*
454         ** pick any interesting stuff out of msg "inb"
455         */
456         for (state = FLD;;) {
457                 state = m_getfld(state, name, tmpbuf, SBUFSIZ, inb);
458                 switch (state) {
459                 case FLD:
460                 case FLDPLUS:
461                         /*
462                         ** if we're interested in this component, save
463                         ** a pointer to the component text, then start
464                         ** using our next free buffer as the component
465                         ** temp buffer (buffer switching saves an extra
466                         ** copy of the component text).
467                         */
468                         if ((cptr = wantcomp[CHASH(name)]))
469                                 do {
470                                         if (!mh_strcasecmp(name, cptr->c_name)) {
471                                                 char_read += msg_count;
472                                                 if (! cptr->c_text) {
473                                                         i = strlen(cptr->c_text = tmpbuf) - 1;
474                                                         if (tmpbuf[i] == '\n')
475                                                                 tmpbuf[i] = '\0';
476                                                         *--savecomp = cptr;
477                                                         tmpbuf = *nxtbuf++;
478                                                 } else {
479                                                         i = strlen(cp = cptr->c_text) - 1;
480                                                         if (cp[i] == '\n') {
481                                                                 if (cptr->c_type & CT_ADDR) {
482                                                                         cp[i] = '\0';
483                                                                         cp = add(",\n\t", cp);
484                                                                 } else {
485                                                                         cp = add("\t", cp);
486                                                                 }
487                                                         }
488                                                         cptr->c_text = add(tmpbuf, cp);
489                                                 }
490                                                 while (state == FLDPLUS) {
491                                                         state = m_getfld(state, name, tmpbuf,
492                                                                                           SBUFSIZ, inb);
493                                                         cptr->c_text = add(tmpbuf, cptr->c_text);
494                                                         char_read += msg_count;
495                                                 }
496                                                 break;
497                                         }
498                                 } while ((cptr = cptr->c_next));
499
500                         while (state == FLDPLUS)
501                                 state = m_getfld(state, name, tmpbuf,
502                                                 SBUFSIZ, inb);
503                         break;
504
505                 case LENERR:
506                 case FMTERR:
507                 case BODY:
508                 case FILEEOF:
509                         goto finished;
510
511                 default:
512                         adios(EX_SOFTWARE, NULL, "m_getfld() returned %d", state);
513                 }
514         }
515
516         /*
517         ** format and output the header lines.
518         */
519 finished:
520
521         /*
522         ** if there's a "Subject" component, strip any "Re:"s off it
523         */
524         FINDCOMP(cptr, "subject")
525         if (cptr && (cp = cptr->c_text)) {
526                 char *sp = cp;
527
528                 for (;;) {
529                         while (isspace(*cp))
530                                 cp++;
531                         if(uprf(cp, "re:"))
532                                 cp += 3;
533                         else
534                                 break;
535                         sp = cp;
536                 }
537                 if (sp != cptr->c_text) {
538                         cp = cptr->c_text;
539                         cptr->c_text = getcpy(sp);
540                         free(cp);
541                 }
542         }
543         i = format_len + char_read + 256;
544         scanl = mh_xcalloc(i + 2, sizeof(char));
545         dat[0] = 0;
546         dat[1] = 0;
547         dat[2] = 0;
548         dat[3] = OUTPUTLINELEN;
549         dat[4] = 0;
550         fmt_scan(fmt, scanl, i, dat);
551         fputs(scanl, out);
552         if (badaddrs) {
553                 fputs("\nrepl: bad addresses:\n", out);
554                 fputs( badaddrs, out);
555         }
556
557         /* Check if we should filter the message */
558         if (filter) {
559                 fflush(out);
560                 if (ferror(out))
561                         adios(EX_IOERR, drft, "error writing");
562
563                 replfilter(inb, out, filter);
564         }
565
566         fflush(out);
567         if (ferror(out))
568                 adios(EX_IOERR, drft, "error writing");
569         fclose(out);
570
571         if (mime && mp) {
572                 /* add an attachment header */
573                 char buffer[BUFSIZ];
574
575                 snprintf(buffer, sizeof buffer, "+%s %s",
576                                 mp->foldpath, m_name(mp->lowsel));
577                 if (execprogl("anno", "anno", "-append", "-nodate",
578                                 drft, "-comp", attach_hdr, "-text", buffer,
579                                 (char *)NULL) != 0) {
580                         advise(NULL, "unable to add attachment header");
581                 }
582         }
583
584         /* return dynamically allocated buffers */
585         free(scanl);
586         for (nxtbuf = compbuffers, i = ncomps; (cptr = *savecomp++);
587                         nxtbuf++, i--)
588                 free(cptr->c_text);  /* if not nxtbuf, nxtbuf already freed */
589         while ( i-- > 0)
590                 free(*nxtbuf++);  /* free unused nxtbufs */
591         free((char *) compbuffers);
592         free((char *) used_buf);
593 }
594
595 static char *buf;  /* our current working buffer */
596 static char *bufend;  /* end of working buffer */
597 static char *last_dst;  /* buf ptr at end of last call */
598 static unsigned int bufsiz=0;  /* current size of buf */
599
600 #define BUFINCR 512  /* how much to expand buf when if fills */
601
602 #define CPY(s) { cp = (s); while ((*dst++ = *cp++)) ; --dst; }
603
604 /*
605 ** check if there's enough room in buf for str.
606 ** add more mem if needed
607 */
608 #define CHECKMEM(str) \
609         if ((len = strlen(str)) >= bufend - dst) {\
610                 int i = dst - buf;\
611                 int n = last_dst - buf;\
612                 bufsiz += ((dst + len - bufend) / BUFINCR + 1) * BUFINCR;\
613                 buf = mh_xrealloc(buf, bufsiz);\
614                 dst = buf + i;\
615                 last_dst = buf + n;\
616                 bufend = buf + bufsiz;\
617         }
618
619
620 /*
621 ** fmt_scan will call this routine if the user includes the function
622 ** "(formataddr {component})" in a format string.  "orig" is the
623 ** original contents of the string register.  "str" is the address
624 ** string to be formatted and concatenated onto orig.  This routine
625 ** returns a pointer to the concatenated address string.
626 **
627 ** We try to not do a lot of malloc/copy/free's (which is why we
628 ** don't call "getcpy") but still place no upper limit on the
629 ** length of the result string.
630 **
631 ** This routine is an override for the equally named one in sbr/fmt_addr.c.
632 ** Don't delete it!
633 */
634 char *
635 formataddr(char *orig, char *str)
636 {
637         int len;
638         char baddr[BUFSIZ], error[BUFSIZ];
639         int isgroup;
640         char *dst;
641         char *cp;
642         char *sp;
643         struct mailname *mp = NULL;
644
645         /* if we don't have a buffer yet, get one */
646         if (bufsiz == 0) {
647                 buf = mh_xcalloc(BUFINCR, sizeof(char));
648                 last_dst = buf;  /* XXX */
649                 bufsiz = BUFINCR - 6;  /* leave some slop */
650                 bufend = buf + bufsiz;
651         }
652         /*
653         ** If "orig" points to our buffer we can just pick up where we
654         ** left off.  Otherwise we have to copy orig into our buffer.
655         */
656         if (orig == buf)
657                 dst = last_dst;
658         else if (!orig || !*orig) {
659                 dst = buf;
660                 *dst = '\0';
661         } else {
662                 dst = last_dst;  /* XXX */
663                 CHECKMEM(orig);
664                 CPY(orig);
665         }
666
667         /* concatenate all the new addresses onto 'buf' */
668         for (isgroup = 0; (cp = getname(str)); ) {
669                 if ((mp = getm(cp, dfhost, dftype, AD_NAME, error)) == NULL) {
670                         snprintf(baddr, sizeof(baddr), "\t%s -- %s\n",
671                                         cp, error);
672                         badaddrs = add(baddr, badaddrs);
673                         continue;
674                 }
675                 if (isgroup && (mp->m_gname || !mp->m_ingrp)) {
676                         *dst++ = ';';
677                         isgroup = 0;
678                 }
679                 if (insert(mp)) {
680                         /* if we get here we're going to add an address */
681                         if (dst != buf) {
682                                 *dst++ = ',';
683                                 *dst++ = ' ';
684                         }
685                         if (mp->m_gname) {
686                                 CHECKMEM(mp->m_gname);
687                                 CPY(mp->m_gname);
688                                 isgroup++;
689                         }
690                         sp = adrformat(mp);
691                         CHECKMEM(sp);
692                         CPY(sp);
693                 }
694         }
695
696         if (isgroup)
697                 *dst++ = ';';
698
699         *dst = '\0';
700         last_dst = dst;
701         return (buf);
702 }
703
704
705 static int
706 insert(struct mailname *np)
707 {
708         char buffer[BUFSIZ];
709         struct mailname *mp;
710
711         if (np->m_mbox == NULL)
712                 return 0;
713
714         for (mp = &mq; mp->m_next; mp = mp->m_next) {
715                 if (!mh_strcasecmp(np->m_host, mp->m_next->m_host) &&
716                                 !mh_strcasecmp(np->m_mbox, mp->m_next->m_mbox))
717                         return 0;
718         }
719         if (!ccme && ismymbox(np))
720                 return 0;
721
722         if (querysw) {
723                 snprintf(buffer, sizeof(buffer), "Reply to %s? ",
724                                 adrformat(np));
725                 if (!gans(buffer, anoyes))
726                         return 0;
727         }
728         mp->m_next = np;
729         return 1;
730 }
731
732
733 /*
734 ** Call mhl
735 **
736 ** This function expects that argument out has been fflushed by the caller.
737 */
738 static void
739 replfilter(FILE *in, FILE *out, char *filter)
740 {
741         int pid, pid_show, n;
742         int mailpipe[2];
743         char *errstr;
744
745         if (filter == NULL)
746                 return;
747
748         if (access(filter, R_OK) == NOTOK)
749                 adios(EX_IOERR, filter, "unable to read");
750
751         rewind(in);
752         lseek(fileno(in), (off_t) 0, SEEK_SET);
753
754         if (pipe(mailpipe) == -1) {
755                 adios(EX_OSERR, "pipe", "can't create pipe");
756         }
757
758         switch (pid_show = fork()) {
759         case NOTOK:
760                 adios(EX_OSERR, "fork", "unable to");
761
762         case OK:
763                 dup2(fileno(in), fileno(stdin));
764                 dup2(mailpipe[1], fileno(stdout));
765                 for (n=3; n<OPEN_MAX; n++) {
766                         close(n);
767                 }
768
769                 execlp("show", "show", "-file", "-", NULL);
770
771                 adios(EX_OSERR, "exec", "unable to");
772         }
773
774         switch (pid = fork()) {
775         case NOTOK:
776                 adios(EX_OSERR, "fork", "unable to");
777
778         case OK:
779                 dup2(mailpipe[0], fileno(stdin));
780                 dup2(fileno(out), fileno(stdout));
781                 for (n=3; n<OPEN_MAX; n++) {
782                         close(n);
783                 }
784
785                 execlp("mhl", "mhl", "-form", filter, NULL);
786                 errstr = strerror(errno);
787                 write(2, "unable to exec mhl: ", 20);
788                 write(2, errstr, strlen(errstr));
789                 write(2, "\n", 1);
790                 _exit(EX_OSERR);
791
792         default:
793                 if (pidXwait(-1, "show | mhl"))
794                         exit(EX_SOFTWARE);
795                 fseek(out, 0L, SEEK_END);
796                 break;
797         }
798
799         close(mailpipe[0]);
800         close(mailpipe[1]);
801 }