Fixed make_bcc_file () to use contents of From: in draft, if draft_from masquerade...
[mmh] / uip / mhlsbr.c
1
2 /*
3  * mhlsbr.c -- main routines for nmh message lister
4  *
5  * $Id$
6  *
7  * This code is Copyright (c) 2002, by the authors of nmh.  See the
8  * COPYRIGHT file in the root directory of the nmh distribution for
9  * complete copyright information.
10  */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
16 #include <h/tws.h>
17 #include <h/utils.h>
18 #include <setjmp.h>
19 #include <signal.h>
20
21 /*
22  * MAJOR BUG:
23  * for a component containing addresses, ADDRFMT, if COMPRESS is also
24  * set, then addresses get split wrong (not at the spaces between commas).
25  * To fix this correctly, putstr() should know about "atomic" strings that
26  * must NOT be broken across lines.  That's too difficult for right now
27  * (it turns out that there are a number of degernate cases), so in
28  * oneline(), instead of
29  *
30  *       (*onelp == '\n' && !onelp[1])
31  *
32  * being a terminating condition,
33  *
34  *       (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
35  *
36  * is used instead.  This cuts the line prematurely, and gives us a much
37  * better chance of getting things right.
38  */
39
40 #define ONECOMP  0
41 #define TWOCOMP  1
42 #define BODYCOMP 2
43
44 #define QUOTE   '\\'
45
46 static struct swit mhlswitches[] = {
47 #define BELLSW         0
48     { "bell", 0 },
49 #define NBELLSW        1
50     { "nobell", 0 },
51 #define CLRSW          2
52     { "clear", 0 },
53 #define NCLRSW         3
54     { "noclear", 0 },
55 #define FACESW         4
56     { "faceproc program", 0 },
57 #define NFACESW        5
58     { "nofaceproc", 0 },
59 #define FOLDSW         6
60     { "folder +folder", 0 },
61 #define FORMSW         7
62     { "form formfile", 0 },
63 #define PROGSW         8
64     { "moreproc program", 0 },
65 #define NPROGSW        9
66     { "nomoreproc", 0 },
67 #define LENSW         10
68     { "length lines", 0 },
69 #define WIDTHSW       11
70     { "width columns", 0 },
71 #define SLEEPSW       12
72     { "sleep seconds",  0 },
73 #define BITSTUFFSW    13
74     { "dashstuffing", -12 },    /* interface from forw */
75 #define NBITSTUFFSW   14
76     { "nodashstuffing", -14 },  /* interface from forw */
77 #define VERSIONSW     15
78     { "version", 0 },
79 #define HELPSW        16
80     { "help", 0 },
81 #define FORW1SW       17
82     { "forward", -7 },          /* interface from forw */
83 #define FORW2SW       18
84     { "forwall", -7 },          /* interface from forw */
85 #define DGSTSW        19
86     { "digest list", -6 },
87 #define VOLUMSW       20
88     { "volume number", -6 },
89 #define ISSUESW       21
90     { "issue number", -5 },
91 #define NBODYSW       22
92     { "nobody", -6 },
93     { NULL, 0 }
94 };
95
96 #define NOCOMPONENT 0x000001    /* don't show component name         */
97 #define UPPERCASE   0x000002    /* display in all upper case         */
98 #define CENTER      0x000004    /* center line                       */
99 #define CLEARTEXT   0x000008    /* cleartext                         */
100 #define EXTRA       0x000010    /* an "extra" component              */
101 #define HDROUTPUT   0x000020    /* already output                    */
102 #define CLEARSCR    0x000040    /* clear screen                      */
103 #define LEFTADJUST  0x000080    /* left justify multiple lines       */
104 #define COMPRESS    0x000100    /* compress text                     */
105 #define ADDRFMT     0x000200    /* contains addresses                */
106 #define BELL        0x000400    /* sound bell at EOP                 */
107 #define DATEFMT     0x000800    /* contains dates                    */
108 #define FORMAT      0x001000    /* parse address/date/RFC-2047 field */
109 #define INIT        0x002000    /* initialize component              */
110 #define FACEFMT     0x004000    /* contains face                     */
111 #define FACEDFLT    0x008000    /* default for face                  */
112 #define SPLIT       0x010000    /* split headers (don't concatenate) */
113 #define NONEWLINE   0x020000    /* don't write trailing newline      */
114 #define LBITS   "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
115 #define GFLAGS  (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
116
117 struct mcomp {
118     char *c_name;               /* component name          */
119     char *c_text;               /* component text          */
120     char *c_ovtxt;              /* text overflow indicator */
121     char *c_nfs;                /* iff FORMAT              */
122     struct format *c_fmt;       /*   ..                    */
123     char *c_face;               /* face designator         */
124     int c_offset;               /* left margin indentation */
125     int c_ovoff;                /* overflow indentation    */
126     int c_width;                /* width of field          */
127     int c_cwidth;               /* width of component      */
128     int c_length;               /* length in lines         */
129     long c_flags;
130     struct mcomp *c_next;
131 };
132
133 static struct mcomp *msghd = NULL;
134 static struct mcomp *msgtl = NULL;
135 static struct mcomp *fmthd = NULL;
136 static struct mcomp *fmttl = NULL;
137
138 static struct mcomp global = {
139     NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
140 };
141
142 static struct mcomp holder = {
143     NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
144 };
145
146 struct pair {
147     char *p_name;
148     long p_flags;
149 };
150
151 static struct pair pairs[] = {
152     { "Date",            DATEFMT },
153     { "From",            ADDRFMT|FACEDFLT },
154     { "Sender",          ADDRFMT },
155     { "Reply-To",        ADDRFMT },
156     { "To",              ADDRFMT },
157     { "cc",              ADDRFMT },
158     { "Bcc",             ADDRFMT },
159     { "Resent-Date",     DATEFMT },
160     { "Resent-From",     ADDRFMT },
161     { "Resent-Sender",   ADDRFMT },
162     { "Resent-Reply-To", ADDRFMT },
163     { "Resent-To",       ADDRFMT },
164     { "Resent-cc",       ADDRFMT },
165     { "Resent-Bcc",      ADDRFMT },
166     { "Face",            FACEFMT },
167     { NULL,              0 }
168 };
169
170 struct triple {
171     char *t_name;
172     long t_on;
173     long t_off;
174 };
175
176 static struct triple triples[] = {
177     { "nocomponent",   NOCOMPONENT, 0 },
178     { "uppercase",     UPPERCASE,   0 },
179     { "nouppercase",   0,           UPPERCASE },
180     { "center",        CENTER,      0 },
181     { "nocenter",      0,           CENTER },
182     { "clearscreen",   CLEARSCR,    0 },
183     { "noclearscreen", 0,           CLEARSCR },
184     { "noclear",       0,           CLEARSCR },
185     { "leftadjust",    LEFTADJUST,  0 },
186     { "noleftadjust",  0,           LEFTADJUST },
187     { "compress",      COMPRESS,    0 },
188     { "nocompress",    0,           COMPRESS },
189     { "split",         SPLIT,       0 },
190     { "nosplit",       0,           SPLIT },
191     { "addrfield",     ADDRFMT,     DATEFMT },
192     { "bell",          BELL,        0 },
193     { "nobell",        0,           BELL },
194     { "datefield",     DATEFMT,     ADDRFMT },
195     { "newline",       0,           NONEWLINE },
196     { "nonewline",     NONEWLINE,   0 },
197     { NULL,            0,           0 }
198 };
199
200
201 static int bellflg   = 0;
202 static int clearflg  = 0;
203 static int dashstuff = 0;
204 static int dobody    = 1;
205 static int forwflg   = 0;
206 static int forwall   = 0;
207
208 static int sleepsw = NOTOK;
209
210 static char *digest = NULL;
211 static int volume = 0;
212 static int issue = 0;
213
214 static int exitstat = 0;
215 static int mhldebug = 0;
216
217 #define PITTY   (-1)
218 #define NOTTY   0
219 #define ISTTY   1
220 static int ontty = NOTTY;
221
222 static int row;
223 static int column;
224
225 static int lm;
226 static int llim;
227 static int ovoff;
228 static int term;
229 static int wid;
230
231 static char *ovtxt;
232
233 static char *onelp;
234
235 static char *parptr;
236
237 static int num_ignores = 0;
238 static char *ignores[MAXARGS];
239
240 static  jmp_buf env;
241 static  jmp_buf mhlenv;
242
243 static char delim3[] =          /* from forw.c */
244     "\n----------------------------------------------------------------------\n\n";
245 static char delim4[] = "\n------------------------------\n\n";
246
247 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
248
249
250 /*
251  * Redefine a couple of functions.
252  * These are undefined later in the code.
253  */
254 #define adios mhladios
255 #define done  mhldone
256
257 /*
258  * prototypes
259  */
260 static void mhl_format (char *, int, int);
261 static int evalvar (struct mcomp *);
262 static int ptoi (char *, int *);
263 static int ptos (char *, char **);
264 static char *parse (void);
265 static void process (char *, char *, int, int);
266 static void mhlfile (FILE *, char *, int, int);
267 static int mcomp_flags (char *);
268 static char *mcomp_add (long, char *, char *);
269 static void mcomp_format (struct mcomp *, struct mcomp *);
270 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
271 static void free_queue (struct mcomp **, struct mcomp **);
272 static void putcomp (struct mcomp *, struct mcomp *, int);
273 static char *oneline (char *, long);
274 static void putstr (char *);
275 static void putch (char);
276 static RETSIGTYPE intrser (int);
277 static RETSIGTYPE pipeser (int);
278 static RETSIGTYPE quitser (int);
279 static void face_format (struct mcomp *);
280 static int doface (struct mcomp *);
281 static void mhladios (char *, char *, ...);
282 static void mhldone (int);
283 static void m_popen (char *);
284
285 int mhl (int, char **);
286 int mhlsbr (int, char **, FILE *(*)());
287 void m_pclose (void);
288
289 void clear_screen (void);             /* from termsbr.c */
290 int SOprintf (char *, ...);           /* from termsbr.c */
291 int sc_width (void);                  /* from termsbr.c */
292 int sc_length (void);                 /* from termsbr.c */
293 int sc_hardcopy (void);               /* from termsbr.c */
294 struct hostent *gethostbystring ();
295
296
297 int
298 mhl (int argc, char **argv)
299 {
300     int length = 0, nomore = 0;
301     int i, width = 0, vecp = 0;
302     char *cp, *folder = NULL, *form = NULL;
303     char buf[BUFSIZ], *files[MAXARGS];
304     char **argp, **arguments;
305
306     invo_name = r1bindex (argv[0], '/');
307
308     /* read user profile/context */
309     context_read();
310
311     arguments = getarguments (invo_name, argc, argv, 1);
312     argp = arguments;
313
314     if ((cp = getenv ("MHLDEBUG")) && *cp)
315         mhldebug++;
316
317     if ((cp = getenv ("FACEPROC")))
318         faceproc = cp;
319
320     while ((cp = *argp++)) {
321         if (*cp == '-') {
322             switch (smatch (++cp, mhlswitches)) {
323                 case AMBIGSW: 
324                     ambigsw (cp, mhlswitches);
325                     done (1);
326                 case UNKWNSW: 
327                     adios (NULL, "-%s unknown\n", cp);
328
329                 case HELPSW: 
330                     snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
331                     print_help (buf, mhlswitches, 1);
332                     done (1);
333                 case VERSIONSW:
334                     print_version(invo_name);
335                     done (1);
336
337                 case BELLSW: 
338                     bellflg = 1;
339                     continue;
340                 case NBELLSW: 
341                     bellflg = -1;
342                     continue;
343
344                 case CLRSW: 
345                     clearflg = 1;
346                     continue;
347                 case NCLRSW: 
348                     clearflg = -1;
349                     continue;
350
351                 case FOLDSW: 
352                     if (!(folder = *argp++) || *folder == '-')
353                         adios (NULL, "missing argument to %s", argp[-2]);
354                     continue;
355                 case FORMSW: 
356                     if (!(form = *argp++) || *form == '-')
357                         adios (NULL, "missing argument to %s", argp[-2]);
358                     continue;
359
360                 case FACESW:
361                     if (!(faceproc = *argp++) || *faceproc == '-')
362                         adios (NULL, "missing argument to %s", argp[-2]);
363                     continue;
364                 case NFACESW:
365                     faceproc = NULL;
366                     continue;
367                 case SLEEPSW:
368                     if (!(cp = *argp++) || *cp == '-')
369                         adios (NULL, "missing argument to %s", argp[-2]);
370                     sleepsw = atoi (cp);/* ZERO ok! */
371                     continue;
372
373                 case PROGSW:
374                     if (!(moreproc = *argp++) || *moreproc == '-')
375                         adios (NULL, "missing argument to %s", argp[-2]);
376                     continue;
377                 case NPROGSW:
378                     nomore++;
379                     continue;
380
381                 case LENSW: 
382                     if (!(cp = *argp++) || *cp == '-')
383                         adios (NULL, "missing argument to %s", argp[-2]);
384                     if ((length = atoi (cp)) < 1)
385                         adios (NULL, "bad argument %s %s", argp[-2], cp);
386                     continue;
387                 case WIDTHSW: 
388                     if (!(cp = *argp++) || *cp == '-')
389                         adios (NULL, "missing argument to %s", argp[-2]);
390                     if ((width = atoi (cp)) < 1)
391                         adios (NULL, "bad argument %s %s", argp[-2], cp);
392                     continue;
393
394                 case DGSTSW: 
395                     if (!(digest = *argp++) || *digest == '-')
396                         adios (NULL, "missing argument to %s", argp[-2]);
397                     continue;
398                 case ISSUESW:
399                     if (!(cp = *argp++) || *cp == '-')
400                         adios (NULL, "missing argument to %s", argp[-2]);
401                     if ((issue = atoi (cp)) < 1)
402                         adios (NULL, "bad argument %s %s", argp[-2], cp);
403                     continue;
404                 case VOLUMSW:
405                     if (!(cp = *argp++) || *cp == '-')
406                         adios (NULL, "missing argument to %s", argp[-2]);
407                     if ((volume = atoi (cp)) < 1)
408                         adios (NULL, "bad argument %s %s", argp[-2], cp);
409                     continue;
410
411                 case FORW2SW: 
412                     forwall++;  /* fall */
413                 case FORW1SW: 
414                     forwflg++;
415                     clearflg = -1;/* XXX */
416                     continue;
417
418                 case BITSTUFFSW: 
419                     dashstuff = 1;      /* trinary logic */
420                     continue;
421                 case NBITSTUFFSW: 
422                     dashstuff = -1;     /* trinary logic */
423                     continue;
424
425                 case NBODYSW: 
426                     dobody = 0;
427                     continue;
428             }
429         }
430         files[vecp++] = cp;
431     }
432
433     if (!folder)
434         folder = getenv ("mhfolder");
435
436     if (isatty (fileno (stdout))) {
437         if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
438             if (mhl_action) {
439                 SIGNAL (SIGINT, SIG_IGN);
440                 SIGNAL2 (SIGQUIT, quitser);
441             }
442             SIGNAL2 (SIGPIPE, pipeser);
443             m_popen (moreproc);
444             ontty = PITTY;
445         } else {
446             SIGNAL (SIGINT, SIG_IGN);
447             SIGNAL2 (SIGQUIT, quitser);
448             ontty = ISTTY;
449         }
450     } else {
451         ontty = NOTTY;
452     }
453
454     mhl_format (form ? form : mhlformat, length, width);
455
456     if (vecp == 0) {
457         process (folder, NULL, 1, vecp = 1);
458     } else {
459         for (i = 0; i < vecp; i++)
460             process (folder, files[i], i + 1, vecp);
461     }
462
463     if (forwall) {
464         if (digest) {
465             printf ("%s", delim4);
466             if (volume == 0) {
467                 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
468             } else {
469                 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
470                         digest, volume, issue);
471             }
472             i = strlen (buf);
473             for (cp = buf + i; i > 1; i--)
474                 *cp++ = '*';
475             *cp++ = '\n';
476             *cp = 0;
477             printf ("%s", buf);
478         }
479         else
480             printf ("\n------- End of Forwarded Message%s\n\n",
481                     vecp > 1 ? "s" : "");
482     }
483
484     fflush(stdout);
485     if(ferror(stdout)){
486             adios("output", "error writing");
487     }
488     
489     if (clearflg > 0 && ontty == NOTTY)
490         clear_screen ();
491
492     if (ontty == PITTY)
493         m_pclose ();
494
495     return exitstat;
496 }
497
498
499 static void
500 mhl_format (char *file, int length, int width)
501 {
502     int i;
503     char *bp, *cp, **ip;
504     char *ap, buffer[BUFSIZ], name[NAMESZ];
505     struct mcomp *c1;
506     struct stat st;
507     FILE *fp;
508     static dev_t dev = 0;
509     static ino_t ino = 0;
510     static time_t mtime = 0;
511
512     if (fmthd != NULL) {
513         if (stat (etcpath (file), &st) != NOTOK
514                 && mtime == st.st_mtime
515                 && dev == st.st_dev
516                 && ino == st.st_ino)
517             goto out;
518         else
519             free_queue (&fmthd, &fmttl);
520     }
521
522     if ((fp = fopen (etcpath (file), "r")) == NULL)
523         adios (file, "unable to open format file");
524
525     if (fstat (fileno (fp), &st) != NOTOK) {
526         mtime = st.st_mtime;
527         dev = st.st_dev;
528         ino = st.st_ino;
529     }
530
531     global.c_ovtxt = global.c_nfs = NULL;
532     global.c_fmt = NULL;
533     global.c_offset = 0;
534     global.c_ovoff = -1;
535     if ((i = sc_width ()) > 5)
536         global.c_width = i;
537     global.c_cwidth = -1;
538     if ((i = sc_length ()) > 5)
539         global.c_length = i - 1;
540     global.c_flags = BELL;              /* BELL is default */
541     *(ip = ignores) = NULL;
542
543     while (vfgets (fp, &ap) == OK) {
544         bp = ap;
545         if (*bp == ';')
546             continue;
547
548         if ((cp = strchr(bp, '\n')))
549             *cp = 0;
550
551         if (*bp == ':') {
552             c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
553             continue;
554         }
555
556         parptr = bp;
557         strncpy (name, parse(), sizeof(name));
558         switch (*parptr) {
559             case '\0': 
560             case ',': 
561             case '=': 
562                 /*
563                  * Split this list of fields to ignore, and copy
564                  * it to the end of the current "ignores" list.
565                  */
566                 if (!strcasecmp (name, "ignores")) {
567                     char **tmparray, **p;
568                     int n = 0;
569
570                     /* split the fields */
571                     tmparray = brkstring (getcpy (++parptr), ",", NULL);
572
573                     /* count number of fields split */
574                     p = tmparray;
575                     while (*p++)
576                         n++;
577
578                     /* copy pointers to split fields to ignores array */
579                     ip = copyip (tmparray, ip, MAXARGS - num_ignores);
580                     num_ignores += n;
581                     continue;
582                 }
583                 parptr = bp;
584                 while (*parptr) {
585                     if (evalvar (&global))
586                         adios (NULL, "format file syntax error: %s", bp);
587                     if (*parptr)
588                         parptr++;
589                 }
590                 continue;
591
592             case ':': 
593                 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
594                 while (*parptr == ':' || *parptr == ',') {
595                     parptr++;
596                     if (evalvar (c1))
597                         adios (NULL, "format file syntax error: %s", bp);
598                 }
599                 if (!c1->c_nfs && global.c_nfs) {
600                     if (c1->c_flags & DATEFMT) {
601                         if (global.c_flags & DATEFMT)
602                             c1->c_nfs = getcpy (global.c_nfs);
603                     }
604                     else
605                         if (c1->c_flags & ADDRFMT) {
606                             if (global.c_flags & ADDRFMT)
607                                 c1->c_nfs = getcpy (global.c_nfs);
608                         }
609                 }
610                 continue;
611
612             default: 
613                 adios (NULL, "format file syntax error: %s", bp);
614         }
615     }
616     fclose (fp);
617
618     if (mhldebug) {
619         for (c1 = fmthd; c1; c1 = c1->c_next) {
620             fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
621                     c1->c_name, c1->c_text, c1->c_ovtxt);
622             fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
623                     (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
624             fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
625                     c1->c_offset, c1->c_ovoff, c1->c_width,
626                     c1->c_cwidth, c1->c_length);
627             fprintf (stderr, "\tflags=%s\n",
628                     snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
629         }
630     }
631
632 out:
633     if (clearflg == 1) {
634         global.c_flags |= CLEARSCR;
635     } else {
636         if (clearflg == -1)
637             global.c_flags &= ~CLEARSCR;
638     }
639
640     switch (bellflg) {          /* command line may override format file */
641         case 1: 
642             global.c_flags |= BELL;
643             break;
644         case -1: 
645             global.c_flags &= ~BELL;
646             break;
647     }
648
649     if (length)
650         global.c_length = length;
651     if (width)
652         global.c_width = width;
653     if (global.c_length < 5)
654         global.c_length = 10000;
655     if (global.c_width < 5)
656         global.c_width = 10000;
657 }
658
659
660 static int
661 evalvar (struct mcomp *c1)
662 {
663     char *cp, name[NAMESZ];
664     struct triple *ap;
665
666     if (!*parptr)
667         return 0;
668     strncpy (name, parse(), sizeof(name));
669
670     if (!strcasecmp (name, "component")) {
671         if (ptos (name, &c1->c_text))
672             return 1;
673         c1->c_flags &= ~NOCOMPONENT;
674         return 0;
675     }
676
677     if (!strcasecmp (name, "overflowtext"))
678         return ptos (name, &c1->c_ovtxt);
679
680     if (!strcasecmp (name, "formatfield")) {
681         char *nfs;
682
683         if (ptos (name, &cp))
684             return 1;
685         nfs = new_fs (NULL, NULL, cp);
686         c1->c_nfs = getcpy (nfs);
687         c1->c_flags |= FORMAT;
688         return 0;
689     }
690
691     if (!strcasecmp (name, "decode")) {
692         char *nfs;
693
694         nfs = new_fs (NULL, NULL, "%(decode{text})");
695         c1->c_nfs = getcpy (nfs);
696         c1->c_flags |= FORMAT;
697         return 0;
698     }
699
700     if (!strcasecmp (name, "offset"))
701         return ptoi (name, &c1->c_offset);
702     if (!strcasecmp (name, "overflowoffset"))
703         return ptoi (name, &c1->c_ovoff);
704     if (!strcasecmp (name, "width"))
705         return ptoi (name, &c1->c_width);
706     if (!strcasecmp (name, "compwidth"))
707         return ptoi (name, &c1->c_cwidth);
708     if (!strcasecmp (name, "length"))
709         return ptoi (name, &c1->c_length);
710     if (!strcasecmp (name, "nodashstuffing"))
711         return (dashstuff = -1);
712
713     for (ap = triples; ap->t_name; ap++)
714         if (!strcasecmp (ap->t_name, name)) {
715             c1->c_flags |= ap->t_on;
716             c1->c_flags &= ~ap->t_off;
717             return 0;
718         }
719
720     return 1;
721 }
722
723
724 static int
725 ptoi (char *name, int *i)
726 {
727     char *cp;
728
729     if (*parptr++ != '=' || !*(cp = parse ())) {
730         advise (NULL, "missing argument to variable %s", name);
731         return 1;
732     }
733
734     *i = atoi (cp);
735     return 0;
736 }
737
738
739 static int
740 ptos (char *name, char **s)
741 {
742     char c, *cp;
743
744     if (*parptr++ != '=') {
745         advise (NULL, "missing argument to variable %s", name);
746         return 1;
747     }
748
749     if (*parptr != '"') {
750         for (cp = parptr;
751                 *parptr && *parptr != ':' && *parptr != ',';
752                 parptr++)
753             continue;
754     } else {
755         for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
756             if (*parptr == QUOTE)
757                 if (!*++parptr)
758                     parptr--;
759     }
760     c = *parptr;
761     *parptr = 0;
762     *s = getcpy (cp);
763     if ((*parptr = c) == '"')
764         parptr++;
765     return 0;
766 }
767
768
769 static char *
770 parse (void)
771 {
772     int c;
773     char *cp;
774     static char result[NAMESZ];
775
776     for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
777         c = *parptr;
778         if (isalnum (c)
779                 || c == '.'
780                 || c == '-'
781                 || c == '_'
782                 || c =='['
783                 || c == ']')
784             *cp++ = c;
785         else
786             break;
787     }
788     *cp = '\0';
789
790     return result;
791 }
792
793
794 static void
795 process (char *folder, char *fname, int ofilen, int ofilec)
796 {
797     char *cp;
798     FILE *fp;
799     struct mcomp *c1;
800
801     switch (setjmp (env)) {
802         case OK: 
803             if (fname) {
804                 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
805                 if (fp == NULL) {
806                     advise (fname, "unable to open");
807                     exitstat++;
808                     return;
809                 }
810             } else {
811                 fname = "(stdin)";
812                 fp = stdin;
813             }
814             cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
815             if (ontty != PITTY)
816                 SIGNAL (SIGINT, intrser);
817             mhlfile (fp, cp, ofilen, ofilec);  /* FALL THROUGH! */
818
819         default: 
820             if (ontty != PITTY)
821                 SIGNAL (SIGINT, SIG_IGN);
822             if (mhl_action == NULL && fp != stdin)
823                 fclose (fp);
824             free (cp);
825             if (holder.c_text) {
826                 free (holder.c_text);
827                 holder.c_text = NULL;
828             }
829             free_queue (&msghd, &msgtl);
830             for (c1 = fmthd; c1; c1 = c1->c_next)
831                 c1->c_flags &= ~HDROUTPUT;
832             break;
833     }
834 }
835
836
837 static void
838 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
839 {
840     int state;
841     struct mcomp *c1, *c2, *c3;
842     char **ip, name[NAMESZ], buf[BUFSIZ];
843
844     if (forwall) {
845         if (digest)
846             printf ("%s", ofilen == 1 ? delim3 : delim4);
847         else {
848             printf ("\n-------");
849             if (ofilen == 1)
850                 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
851             else
852                 printf (" Message %d", ofilen);
853             printf ("\n\n");
854         }
855     } else {
856         switch (ontty) {
857             case PITTY: 
858                 if (ofilec > 1) {
859                     if (ofilen > 1) {
860                         if ((global.c_flags & CLEARSCR))
861                             clear_screen ();
862                         else
863                             printf ("\n\n\n");
864                     }
865                     printf (">>> %s\n\n", mname);
866                 }
867                 break;
868
869             case ISTTY: 
870                 strncpy (buf, "\n", sizeof(buf));
871                 if (ofilec > 1) {
872                     if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
873                         if (ofilen > 1)
874                             printf ("\n\n\n");
875                         printf ("Press <return> to list \"%s\"...", mname);
876                     }
877                     fflush (stdout);
878                     buf[0] = 0;
879                     read (fileno (stdout), buf, sizeof(buf));
880                 }
881                 if (strchr(buf, '\n')) {
882                     if ((global.c_flags & CLEARSCR))
883                         clear_screen ();
884                 }
885                 else
886                     printf ("\n");
887                 break;
888
889             default: 
890                 if (ofilec > 1) {
891                     if (ofilen > 1) {
892                         printf ("\n\n\n");
893                         if (clearflg > 0)
894                             clear_screen ();
895                     }
896                     printf (">>> %s\n\n", mname);
897                 }
898                 break;
899         }
900     }
901
902     for (state = FLD;;) {
903         switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
904             case FLD: 
905             case FLDPLUS: 
906                 for (ip = ignores; *ip; ip++)
907                     if (!strcasecmp (name, *ip)) {
908                         while (state == FLDPLUS)
909                             state = m_getfld (state, name, buf, sizeof(buf), fp);
910                         break;
911                     }
912                 if (*ip)
913                     continue;
914
915                 for (c2 = fmthd; c2; c2 = c2->c_next)
916                     if (!strcasecmp (c2->c_name, name))
917                         break;
918                 c1 = NULL;
919                 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
920                     for (c1 = msghd; c1; c1 = c1->c_next)
921                         if (!strcasecmp (c1->c_name, c3->c_name)) {
922                             c1->c_text =
923                                 mcomp_add (c1->c_flags, buf, c1->c_text);
924                             break;
925                         }
926                 if (c1 == NULL)
927                     c1 = add_queue (&msghd, &msgtl, name, buf, 0);
928                 while (state == FLDPLUS) {
929                     state = m_getfld (state, name, buf, sizeof(buf), fp);
930                     c1->c_text = add (buf, c1->c_text);
931                 }
932                 if (c2 == NULL)
933                     c1->c_flags |= EXTRA;
934                 continue;
935
936             case BODY: 
937             case FILEEOF: 
938                 row = column = 0;
939                 for (c1 = fmthd; c1; c1 = c1->c_next) {
940                     if (c1->c_flags & CLEARTEXT) {
941                         putcomp (c1, c1, ONECOMP);
942                         continue;
943                     }
944                     if (!strcasecmp (c1->c_name, "messagename")) {
945                         holder.c_text = concat ("(Message ", mname, ")\n",
946                                             NULL);
947                         putcomp (c1, &holder, ONECOMP);
948                         free (holder.c_text);
949                         holder.c_text = NULL;
950                         continue;
951                     }
952                     if (!strcasecmp (c1->c_name, "extras")) {
953                         for (c2 = msghd; c2; c2 = c2->c_next)
954                             if (c2->c_flags & EXTRA)
955                                 putcomp (c1, c2, TWOCOMP);
956                         continue;
957                     }
958                     if (dobody && !strcasecmp (c1->c_name, "body")) {
959                         holder.c_text = mh_xmalloc (sizeof(buf));
960                         strncpy (holder.c_text, buf, sizeof(buf));
961                         while (state == BODY) {
962                             putcomp (c1, &holder, BODYCOMP);
963                             state = m_getfld (state, name, holder.c_text,
964                                         sizeof(buf), fp);
965                         }
966                         free (holder.c_text);
967                         holder.c_text = NULL;
968                         continue;
969                     }
970                     for (c2 = msghd; c2; c2 = c2->c_next)
971                         if (!strcasecmp (c2->c_name, c1->c_name)) {
972                             putcomp (c1, c2, ONECOMP);
973                             if (!(c1->c_flags & SPLIT))
974                                 break;
975                         }
976                     if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
977                         for (c2 = msghd; c2; c2 = c2->c_next)
978                             if (c2->c_flags & FACEDFLT) {
979                                 if (c2->c_face == NULL)
980                                     face_format (c2);
981                                 if ((holder.c_text = c2->c_face)) {
982                                     putcomp (c1, &holder, ONECOMP);
983                                     holder.c_text = NULL;
984                                 }
985                                 break;
986                             }
987                 }
988                 return;
989
990             case LENERR: 
991             case FMTERR: 
992                 advise (NULL, "format error in message %s", mname);
993                 exitstat++;
994                 return;
995
996             default: 
997                 adios (NULL, "getfld() returned %d", state);
998         }
999     }
1000 }
1001
1002
1003 static int
1004 mcomp_flags (char *name)
1005 {
1006     struct pair *ap;
1007
1008     for (ap = pairs; ap->p_name; ap++)
1009         if (!strcasecmp (ap->p_name, name))
1010             return (ap->p_flags);
1011
1012     return 0;
1013 }
1014
1015
1016 static char *
1017 mcomp_add (long flags, char *s1, char *s2)
1018 {
1019     char *dp;
1020
1021     if (!(flags & ADDRFMT))
1022         return add (s1, s2);
1023
1024     if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1025         *dp = 0;
1026
1027     return add (s1, add (",\n", s2));
1028 }
1029
1030
1031 struct pqpair {
1032     char *pq_text;
1033     char *pq_error;
1034     struct pqpair *pq_next;
1035 };
1036
1037
1038 static void
1039 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1040 {
1041     int dat[5];
1042     char *ap, *cp;
1043     char buffer[BUFSIZ], error[BUFSIZ];
1044     struct comp *cptr;
1045     struct pqpair *p, *q;
1046     struct pqpair pq;
1047     struct mailname *mp;
1048
1049     ap = c2->c_text;
1050     c2->c_text = NULL;
1051     dat[0] = 0;
1052     dat[1] = 0;
1053     dat[2] = 0;
1054     dat[3] = sizeof(buffer) - 1;
1055     dat[4] = 0;
1056     fmt_compile (c1->c_nfs, &c1->c_fmt);
1057
1058     if (!(c1->c_flags & ADDRFMT)) {
1059         FINDCOMP (cptr, "text");
1060         if (cptr)
1061             cptr->c_text = ap;
1062         if ((cp = strrchr(ap, '\n')))   /* drop ending newline */
1063             if (!cp[1])
1064                 *cp = 0;
1065
1066         fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1067         /* Don't need to append a newline, dctime() already did */
1068         c2->c_text = getcpy (buffer);
1069
1070         free (ap);
1071         return;
1072     }
1073
1074     (q = &pq)->pq_next = NULL;
1075     while ((cp = getname (ap))) {
1076         if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1077             adios (NULL, "unable to allocate pqpair memory");
1078
1079         if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1080             p->pq_text = getcpy (cp);
1081             p->pq_error = getcpy (error);
1082         } else {
1083             if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1084                 char   *h, *o;
1085                 if ((h = mp->m_host) == NULL)
1086                     h = LocalName ();
1087                 if ((o = OfficialName (h)))
1088                     h = o;
1089                 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1090                                     NULL);
1091             }
1092             p->pq_text = getcpy (mp->m_text);
1093             mnfree (mp);
1094         }
1095         q = (q->pq_next = p);
1096     }
1097
1098     for (p = pq.pq_next; p; p = q) {
1099         FINDCOMP (cptr, "text");
1100         if (cptr)
1101             cptr->c_text = p->pq_text;
1102         FINDCOMP (cptr, "error");
1103         if (cptr)
1104             cptr->c_text = p->pq_error;
1105
1106         fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1107         if (*buffer) {
1108             if (c2->c_text)
1109                 c2->c_text = add (",\n", c2->c_text);
1110             if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1111                 *cp = 0;
1112             c2->c_text = add (buffer, c2->c_text);
1113         }
1114
1115         free (p->pq_text);
1116         if (p->pq_error)
1117             free (p->pq_error);
1118         q = p->pq_next;
1119         free ((char *) p);
1120     }
1121
1122     c2->c_text = add ("\n", c2->c_text);
1123     free (ap);
1124 }
1125
1126
1127 static struct mcomp *
1128 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1129 {
1130     struct mcomp *c1;
1131
1132     if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1133         adios (NULL, "unable to allocate comp memory");
1134
1135     c1->c_flags = flags & ~INIT;
1136     if ((c1->c_name = name ? getcpy (name) : NULL))
1137         c1->c_flags |= mcomp_flags (c1->c_name);
1138     c1->c_text = text ? getcpy (text) : NULL;
1139     if (flags & INIT) {
1140         if (global.c_ovtxt)
1141             c1->c_ovtxt = getcpy (global.c_ovtxt);
1142         c1->c_offset = global.c_offset;
1143         c1->c_ovoff = global. c_ovoff;
1144         c1->c_width = c1->c_length = 0;
1145         c1->c_cwidth = global.c_cwidth;
1146         c1->c_flags |= global.c_flags & GFLAGS;
1147     }
1148     if (*head == NULL)
1149         *head = c1;
1150     if (*tail != NULL)
1151         (*tail)->c_next = c1;
1152     *tail = c1;
1153
1154     return c1;
1155 }
1156
1157
1158 static void
1159 free_queue (struct mcomp **head, struct mcomp **tail)
1160 {
1161     struct mcomp *c1, *c2;
1162
1163     for (c1 = *head; c1; c1 = c2) {
1164         c2 = c1->c_next;
1165         if (c1->c_name)
1166             free (c1->c_name);
1167         if (c1->c_text)
1168             free (c1->c_text);
1169         if (c1->c_ovtxt)
1170             free (c1->c_ovtxt);
1171         if (c1->c_nfs)
1172             free (c1->c_nfs);
1173         if (c1->c_fmt)
1174             free ((char *) c1->c_fmt);
1175         if (c1->c_face)
1176             free (c1->c_face);
1177         free ((char *) c1);
1178     }
1179
1180     *head = *tail = NULL;
1181 }
1182
1183
1184 static void
1185 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1186 {
1187     int count, cchdr;
1188     char *cp;
1189
1190     cchdr = 0;
1191     lm = 0;
1192     llim = c1->c_length ? c1->c_length : -1;
1193     wid = c1->c_width ? c1->c_width : global.c_width;
1194     ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1195         + c1->c_offset;
1196     if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1197         ovtxt = "";
1198     if (wid < ovoff + strlen (ovtxt) + 5)
1199         adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1200                 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1201     onelp = NULL;
1202
1203     if (c1->c_flags & CLEARTEXT) {
1204         putstr (c1->c_text);
1205         putstr ("\n");
1206         return;
1207     }
1208
1209     if (c1->c_flags & FACEFMT)
1210         switch (doface (c2)) {
1211             case NOTOK:         /* error */
1212             case OK:            /* async faceproc */
1213                 return;
1214
1215             default:            /* sync faceproc */
1216                 break;
1217         }
1218
1219     if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1220         mcomp_format (c1, c2);
1221
1222     if (c1->c_flags & CENTER) {
1223         count = (c1->c_width ? c1->c_width : global.c_width)
1224             - c1->c_offset - strlen (c2->c_text);
1225         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1226             count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1227         lm = c1->c_offset + (count / 2);
1228     } else {
1229         if (c1->c_offset)
1230             lm = c1->c_offset;
1231     }
1232
1233     if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1234         if (c1->c_flags & UPPERCASE)            /* uppercase component also */
1235             for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1236                 if (islower (*cp))
1237                     *cp = toupper (*cp);
1238         putstr (c1->c_text ? c1->c_text : c1->c_name);
1239         if (flag != BODYCOMP) {
1240             putstr (": ");
1241             if (!(c1->c_flags & SPLIT))
1242                 c1->c_flags |= HDROUTPUT;
1243
1244         cchdr++;
1245         if ((count = c1->c_cwidth -
1246                 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1247             while (count--)
1248                 putstr (" ");
1249         }
1250         else
1251             c1->c_flags |= HDROUTPUT;           /* for BODYCOMP */
1252     }
1253
1254     if (flag == TWOCOMP
1255             && !(c2->c_flags & HDROUTPUT)
1256             && !(c2->c_flags & NOCOMPONENT)) {
1257         if (c1->c_flags & UPPERCASE)
1258             for (cp = c2->c_name; *cp; cp++)
1259                 if (islower (*cp))
1260                     *cp = toupper (*cp);
1261         putstr (c2->c_name);
1262         putstr (": ");
1263         if (!(c1->c_flags & SPLIT))
1264             c2->c_flags |= HDROUTPUT;
1265
1266         cchdr++;
1267         if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1268             while (count--)
1269                 putstr (" ");
1270     }
1271     if (c1->c_flags & UPPERCASE)
1272         for (cp = c2->c_text; *cp; cp++)
1273             if (islower (*cp))
1274                 *cp = toupper (*cp);
1275
1276     count = 0;
1277     if (cchdr) {
1278         if (flag == TWOCOMP)
1279             count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1280                         : strlen (c2->c_name) + 2;
1281         else
1282             count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1283                         : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1284     }
1285     count += c1->c_offset;
1286
1287     if ((cp = oneline (c2->c_text, c1->c_flags)))
1288        putstr(cp);
1289     if (term == '\n')
1290         putstr ("\n");
1291     while ((cp = oneline (c2->c_text, c1->c_flags))) {
1292         lm = count;
1293         if (flag == BODYCOMP
1294                 && !(c1->c_flags & NOCOMPONENT))
1295             putstr (c1->c_text ? c1->c_text : c1->c_name);
1296         if (*cp)
1297             putstr (cp);
1298         if (term == '\n')
1299             putstr ("\n");
1300     }
1301 }
1302
1303
1304 static char *
1305 oneline (char *stuff, long flags)
1306 {
1307     int spc;
1308     char *cp, *ret;
1309
1310     if (onelp == NULL)
1311         onelp = stuff;
1312     if (*onelp == 0)
1313         return (onelp = NULL);
1314
1315     ret = onelp;
1316     term = 0;
1317     if (flags & COMPRESS) {
1318         for (spc = 1, cp = ret; *onelp; onelp++)
1319             if (isspace (*onelp)) {
1320                 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1321                     term = '\n';
1322                     *onelp++ = 0;
1323                     break;
1324                 }
1325                 else
1326                     if (!spc) {
1327                         *cp++ = ' ';
1328                         spc++;
1329                     }
1330             }
1331             else {
1332                 *cp++ = *onelp;
1333                 spc = 0;
1334             }
1335
1336         *cp = 0;
1337     }
1338     else {
1339         while (*onelp && *onelp != '\n')
1340             onelp++;
1341         if (*onelp == '\n') {
1342             term = '\n';
1343             *onelp++ = 0;
1344         }
1345         if (flags & LEFTADJUST)
1346             while (*ret == ' ' || *ret == '\t')
1347                 ret++;
1348     }
1349     if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1350         term = 0;
1351
1352     return ret;
1353 }
1354
1355
1356 static void
1357 putstr (char *string)
1358 {
1359     if (!column && lm > 0) {
1360         while (lm > 0)
1361             if (lm >= 8) {
1362                 putch ('\t');
1363                 lm -= 8;
1364             }
1365             else {
1366                 putch (' ');
1367                 lm--;
1368             }
1369     }
1370     lm = 0;
1371     while (*string)
1372         putch (*string++);
1373 }
1374
1375
1376 static void
1377 putch (char ch)
1378 {
1379     char buf[BUFSIZ];
1380
1381     if (llim == 0)
1382         return;
1383
1384     switch (ch) {
1385         case '\n': 
1386             if (llim > 0)
1387                 llim--;
1388             column = 0;
1389             row++;
1390             if (ontty != ISTTY || row != global.c_length)
1391                 break;
1392             if (global.c_flags & BELL)
1393                 putchar ('\007');
1394             fflush (stdout);
1395             buf[0] = 0;
1396             read (fileno (stdout), buf, sizeof(buf));
1397             if (strchr(buf, '\n')) {
1398                 if (global.c_flags & CLEARSCR)
1399                     clear_screen ();
1400                 row = 0;
1401             } else {
1402                 putchar ('\n');
1403                 row = global.c_length / 3;
1404             }
1405             return;
1406
1407         case '\t': 
1408             column |= 07;
1409             column++;
1410             break;
1411
1412         case '\b': 
1413             column--;
1414             break;
1415
1416         case '\r': 
1417             column = 0;
1418             break;
1419
1420         default: 
1421             /*
1422              * If we are forwarding this message, and the first
1423              * column contains a dash, then add a dash and a space.
1424              */
1425             if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1426                 putchar ('-');
1427                 putchar (' ');
1428             }
1429             if (ch >= ' ')
1430                 column++;
1431             break;
1432     }
1433
1434     if (column >= wid) {
1435         putch ('\n');
1436         if (ovoff > 0)
1437             lm = ovoff;
1438         putstr (ovtxt ? ovtxt : "");
1439         putch (ch);
1440         return;
1441     }
1442
1443     putchar (ch);
1444 }
1445
1446
1447 static RETSIGTYPE
1448 intrser (int i)
1449 {
1450 #ifndef RELIABLE_SIGNALS
1451     SIGNAL (SIGINT, intrser);
1452 #endif
1453
1454     discard (stdout);
1455     putchar ('\n');
1456     longjmp (env, DONE);
1457 }
1458
1459
1460 static RETSIGTYPE
1461 pipeser (int i)
1462 {
1463 #ifndef RELIABLE_SIGNALS
1464     SIGNAL (SIGPIPE, pipeser);
1465 #endif
1466
1467     done (NOTOK);
1468 }
1469
1470
1471 static RETSIGTYPE
1472 quitser (int i)
1473 {
1474 #ifndef RELIABLE_SIGNALS
1475     SIGNAL (SIGQUIT, quitser);
1476 #endif
1477
1478     putchar ('\n');
1479     fflush (stdout);
1480     done (NOTOK);
1481 }
1482
1483
1484 static void
1485 face_format (struct mcomp *c1)
1486 {
1487     char *cp;
1488     struct mailname *mp;
1489
1490     if ((cp = c1->c_text) == NULL)
1491         return;
1492
1493     if ((cp = getname (cp))) {
1494         if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1495             char *h, *o;
1496             if ((h = mp->m_host) == NULL)
1497                 h = LocalName ();
1498             if ((o = OfficialName (h)))
1499                 h = o;
1500             c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1501         }
1502
1503         while ((cp = getname (cp)))
1504             continue;
1505     }
1506 }
1507
1508
1509 /*
1510  * faceproc is two elements defining the image agent's location:
1511  *     Internet host
1512  *     UDP port
1513  */
1514
1515 #include <sys/socket.h>
1516 #include <netinet/in.h>
1517 #include <netdb.h>
1518
1519 #ifdef HAVE_ARPA_INET_H
1520 # include <arpa/inet.h>
1521 #endif
1522
1523 static int
1524 doface (struct mcomp *c1)
1525 {
1526     int result, sd;
1527     struct sockaddr_in in_socket;
1528     struct sockaddr_in *isock = &in_socket;
1529     static int inited = OK;
1530     static int addrlen;
1531     static struct in_addr addr;
1532     static unsigned short portno;
1533
1534     if (inited == OK) {
1535         char *cp;
1536         char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1537         struct hostent *hp;
1538
1539         if (ap[0] == NULL || ap[1] == NULL) {
1540 bad_faceproc: ;
1541             free (cp);
1542             return (inited = NOTOK);
1543         }
1544
1545         if (!(hp = gethostbystring (ap[0])))
1546             goto bad_faceproc;
1547         memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1548
1549         portno = htons ((unsigned short) atoi (ap[1]));
1550         free (cp);
1551
1552         inited = DONE;
1553     }
1554     if (inited == NOTOK)
1555         return NOTOK;
1556
1557     isock->sin_family = AF_INET;
1558     isock->sin_port = portno;
1559     memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1560
1561     if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1562         return NOTOK;
1563
1564     result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1565                 (struct sockaddr *) isock, sizeof(*isock));
1566
1567     close (sd);
1568
1569     return (result != NOTOK ? OK : NOTOK);
1570 }
1571
1572 /*
1573  * COMMENTED OUT
1574  * This version doesn't use sockets
1575  */
1576 #if 0
1577
1578 static int
1579 doface (struct mcomp *c1)
1580 {
1581     int i, len, vecp;
1582     pid_t child_id;
1583     int result, pdi[2], pdo[2];
1584     char *bp, *cp;
1585     char buffer[BUFSIZ], *vec[10];
1586
1587     if (pipe (pdi) == NOTOK)
1588         return NOTOK;
1589     if (pipe (pdo) == NOTOK) {
1590         close (pdi[0]);
1591         close (pdi[1]);
1592         return NOTOK;
1593     }
1594
1595     for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1596         sleep (5);
1597
1598     switch (child_id) {
1599         case NOTOK: 
1600             /* oops... fork error */
1601             return NOTOK;
1602
1603         case OK: 
1604             /* child process */
1605             SIGNAL (SIGINT, SIG_IGN);
1606             SIGNAL (SIGQUIT, SIG_IGN);
1607             if (pdi[0] != fileno (stdin)) {
1608                 dup2 (pdi[0], fileno (stdin));
1609                 close (pdi[0]);
1610             }
1611             close (pdi[1]);
1612             close (pdo[0]);
1613             if (pdo[1] != fileno (stdout)) {
1614                 dup2 (pdo[1], fileno (stdout));
1615                 close (pdo[1]);
1616             }
1617             vecp = 0;
1618             vec[vecp++] = r1bindex (faceproc, '/');
1619             vec[vecp++] = "-e";
1620             if (sleepsw != NOTOK) {
1621                 vec[vecp++] = "-s";
1622                 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1623                 vec[vecp++] = buffer;
1624             }
1625             vec[vecp] = NULL;
1626             execvp (faceproc, vec);
1627             fprintf (stderr, "unable to exec ");
1628             perror (faceproc);
1629             _exit (-1);         /* NOTREACHED */
1630
1631         default: 
1632             /* parent process */
1633             close (pdi[0]);
1634             i = strlen (c1->c_text);
1635             if (write (pdi[1], c1->c_text, i) != i)
1636                 adios ("pipe", "error writing to");
1637             free (c1->c_text), c1->c_text = NULL;
1638             close (pdi[1]);
1639
1640             close (pdo[1]);
1641             cp = NULL, len = 0;
1642             result = DONE;
1643             while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1644                 if (cp) {
1645                     int j;
1646                     char *dp;
1647                     dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1648                     memcpy(dp + len, buffer, i);
1649                     cp = dp, len = j;
1650                 }
1651                 else {
1652                     cp = mh_xmalloc ((unsigned) i);
1653                     memcpy(cp, buffer, i);
1654                     len = i;
1655                 }
1656                 if (result == DONE)
1657                     for (bp = buffer + i - 1; bp >= buffer; bp--)
1658                         if (!isascii (*bp) || iscntrl (*bp)) {
1659                             result = OK;
1660                             break;
1661                         }
1662             }
1663             close (pdo[0]);
1664
1665 /* no waiting for child... */
1666
1667             if (result == OK) { /* binary */
1668                 if (write (1, cp, len) != len)
1669                     adios ("writing", "error");
1670                 free (cp);
1671             }
1672             else                /* empty */
1673                 if ((c1->c_text = cp) == NULL)
1674                     result = OK;
1675             break;
1676     }
1677
1678     return result;
1679 }
1680 #endif /* COMMENTED OUT */
1681
1682
1683 int
1684 mhlsbr (int argc, char **argv, FILE *(*action)())
1685 {
1686     SIGNAL_HANDLER istat, pstat, qstat;
1687     char *cp;
1688     struct mcomp *c1;
1689
1690     switch (setjmp (mhlenv)) {
1691         case OK: 
1692             cp = invo_name;
1693             sleepsw = 0;        /* XXX */
1694             bellflg = clearflg = forwflg = forwall = exitstat = 0;
1695             digest = NULL;
1696             ontty = NOTTY;
1697             mhl_action = action;
1698
1699             /*
1700              * If signal is at default action, then start ignoring
1701              * it, else let it set to its current action.
1702              */
1703             if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1704                 SIGNAL (SIGINT, istat);
1705             if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1706                 SIGNAL (SIGQUIT, qstat);
1707             pstat = SIGNAL (SIGPIPE, pipeser);
1708             mhl (argc, argv);                  /* FALL THROUGH! */
1709
1710         default: 
1711             SIGNAL (SIGINT, istat);
1712             SIGNAL (SIGQUIT, qstat);
1713             SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1714             if (ontty == PITTY)
1715                 m_pclose ();
1716             SIGNAL (SIGPIPE, pstat);
1717             invo_name = cp;
1718             if (holder.c_text) {
1719                 free (holder.c_text);
1720                 holder.c_text = NULL;
1721             }
1722             free_queue (&msghd, &msgtl);
1723             for (c1 = fmthd; c1; c1 = c1->c_next)
1724                 c1->c_flags &= ~HDROUTPUT;
1725             return exitstat;
1726     }
1727 }
1728
1729 #undef adios
1730 #undef done
1731
1732 static void
1733 mhladios (char *what, char *fmt, ...)
1734 {
1735     va_list ap;
1736
1737     va_start(ap, fmt);
1738     advertise (what, NULL, fmt, ap);
1739     va_end(ap);
1740     mhldone (1);
1741 }
1742
1743
1744 static void
1745 mhldone (int status)
1746 {
1747     exitstat = status;
1748     if (mhl_action)
1749         longjmp (mhlenv, DONE);
1750     else
1751         done (exitstat);
1752 }
1753
1754
1755 static  int m_pid = NOTOK;
1756 static  int sd = NOTOK;
1757
1758 static void
1759 m_popen (char *name)
1760 {
1761     int pd[2];
1762
1763     if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1764         adios ("standard output", "unable to dup()");
1765
1766     if (pipe (pd) == NOTOK)
1767         adios ("pipe", "unable to");
1768
1769     switch (m_pid = vfork ()) {
1770         case NOTOK: 
1771             adios ("fork", "unable to");
1772
1773         case OK: 
1774             SIGNAL (SIGINT, SIG_DFL);
1775             SIGNAL (SIGQUIT, SIG_DFL);
1776
1777             close (pd[1]);
1778             if (pd[0] != fileno (stdin)) {
1779                 dup2 (pd[0], fileno (stdin));
1780                 close (pd[0]);
1781             }
1782             execlp (name, r1bindex (name, '/'), NULL);
1783             fprintf (stderr, "unable to exec ");
1784             perror (name);
1785             _exit (-1);
1786
1787         default: 
1788             close (pd[0]);
1789             if (pd[1] != fileno (stdout)) {
1790                 dup2 (pd[1], fileno (stdout));
1791                 close (pd[1]);
1792             }
1793     }
1794 }
1795
1796
1797 void
1798 m_pclose (void)
1799 {
1800     if (m_pid == NOTOK)
1801         return;
1802
1803     if (sd != NOTOK) {
1804         fflush (stdout);
1805         if (dup2 (sd, fileno (stdout)) == NOTOK)
1806             adios ("standard output", "unable to dup2()");
1807
1808         clearerr (stdout);
1809         close (sd);
1810         sd = NOTOK;
1811     }
1812     else
1813         fclose (stdout);
1814
1815     pidwait (m_pid, OK);
1816     m_pid = NOTOK;
1817 }