redesign version string
[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 <signal.h>
16 #include <ctype.h>
17 #include <sys/stat.h>
18 #include <locale.h>
19 #include <sysexits.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 switches[] = {
47 #define FORMSW  0
48         { "form formfile", 0 },
49 #define WIDTHSW  1
50         { "width columns", 0 },
51 #define VERSIONSW  2
52         { "Version", 0 },
53 #define HELPSW  3
54         { "help", 0 },
55 #define FORW1SW  4
56         { "forward", -7 },
57 #define FORW2SW  5
58         { "forwall", -7 },
59 #define NBODYSW  6
60         { "nobody", -6 },
61         { NULL, 0 }
62 };
63
64 char *version=VERSION;
65
66 #define NOCOMPONENT 0x000001  /* don't show component name   */
67 #define UPPERCASE   0x000002  /* display in all upper case   */
68 #define CENTER      0x000004  /* center line                 */
69 #define CLEARTEXT   0x000008  /* cleartext                   */
70 #define EXTRA       0x000010  /* an "extra" component        */
71 #define HDROUTPUT   0x000020  /* already output              */
72 #define LEFTADJUST  0x000040  /* left justify multiple lines */
73 #define COMPRESS    0x000080  /* compress text               */
74 #define ADDRFMT     0x000100  /* contains addresses          */
75 #define DATEFMT     0x000200  /* contains dates              */
76 #define FORMAT      0x000400  /* parse address/date/RFC-2047 field */
77 #define INIT        0x000800  /* initialize component        */
78 #define SPLIT       0x001000  /* split headers (don't concatenate) */
79 #define NONEWLINE   0x002000  /* don't write trailing newline */
80 #define LBITS       "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07LEFTADJUST\010COMPRESS\011ADDRFMT\012DATEFMT\013FORMAT\014INIT\015SPLIT\016NONEWLINE"
81 #define GFLAGS      (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
82
83 struct mcomp {
84         char *c_name;  /* component name */
85         char *c_text;  /* component text */
86         char *c_ovtxt; /* text overflow indicator */
87         char *c_fstr;   /* iff FORMAT */
88         struct format *c_fmt;  /*  .. */
89         int c_offset;  /* left margin indentation */
90         int c_ovoff;   /* overflow indentation */
91         int c_width;   /* width of field */
92         int c_cwidth;  /* width of component */
93         long c_flags;
94         struct mcomp *c_next;
95 };
96
97 static struct mcomp *msghd = NULL;
98 static struct mcomp *msgtl = NULL;
99 static struct mcomp *fmthd = NULL;
100 static struct mcomp *fmttl = NULL;
101
102 static struct mcomp global = {
103         NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 0, NULL
104 };
105
106 static struct mcomp holder = {
107         NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NOCOMPONENT, NULL
108 };
109
110 struct pair {
111         char *p_name;
112         long p_flags;
113 };
114
115 static struct pair pairs[] = {
116         { "Date", DATEFMT },
117         { "From", ADDRFMT },
118         { "Sender", ADDRFMT },
119         { "Reply-To", ADDRFMT },
120         { "To", ADDRFMT },
121         { "Cc", ADDRFMT },
122         { "Bcc", ADDRFMT },
123         { "Resent-Date", DATEFMT },
124         { "Resent-From", ADDRFMT },
125         { "Resent-Sender", ADDRFMT },
126         { "Resent-Reply-To", ADDRFMT },
127         { "Resent-To", ADDRFMT },
128         { "Resent-Cc", ADDRFMT },
129         { "Resent-Bcc", ADDRFMT },
130         { NULL, 0 }
131 };
132
133 struct triple {
134         char *t_name;
135         long t_on;
136         long t_off;
137 };
138
139 static struct triple triples[] = {
140         { "nocomponent",  NOCOMPONENT, 0 },
141         { "uppercase", UPPERCASE, 0 },
142         { "nouppercase", 0, UPPERCASE },
143         { "center", CENTER, 0 },
144         { "nocenter", 0, CENTER },
145         { "leftadjust", LEFTADJUST, 0 },
146         { "noleftadjust", 0, LEFTADJUST },
147         { "compress", COMPRESS, 0 },
148         { "nocompress", 0, COMPRESS },
149         { "split", SPLIT, 0 },
150         { "nosplit", 0, SPLIT },
151         { "addrfield", ADDRFMT, DATEFMT },
152         { "datefield", DATEFMT, ADDRFMT },
153         { "newline", 0, NONEWLINE },
154         { "nonewline", NONEWLINE, 0 },
155         { NULL, 0, 0 }
156 };
157
158
159 static int dobody    = 1;
160 static int forwflg   = 0;
161 static int forwall   = 0;
162
163 static int exitstat = 0;
164 static int mhldebug = 0;
165
166 static unsigned int column;
167
168 static int lm;
169 static int ovoff;
170 static int term;
171 static unsigned int wid;
172
173 static char *ovtxt;
174
175 static unsigned char *onelp;
176
177 static char *parptr;
178
179 static int num_ignores = 0;
180 static char *ignores[MAXARGS];
181
182 volatile sig_atomic_t eflag = 0;
183
184 /*
185 ** Redefine a couple of functions.
186 ** These are undefined later in the code.
187 */
188
189 /*
190 ** prototypes
191 */
192 static void mhl_format(char *, int);
193 static int evalvar(struct mcomp *);
194 static int ptoi(char *, int *);
195 static int ptos(char *, char **);
196 static char *parse(void);
197 static void process(char *, int, int);
198 static void mhlfile(FILE *, char *, int, int);
199 static int mcomp_flags(char *);
200 static char *mcomp_add(long, char *, char *);
201 static void mcomp_format(struct mcomp *, struct mcomp *);
202 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
203                 char *, char *, int);
204 static void free_queue(struct mcomp **, struct mcomp **);
205 static void putcomp(struct mcomp *, struct mcomp *, int);
206 static char *oneline(char *, long);
207 static void putstr(char *);
208 static void putch(char);
209 static void intrser(int);
210
211 int sc_width(void);  /* from termsbr.c */
212
213
214 int
215 main(int argc, char **argv)
216 {
217         int i, width = 0, vecp = 0;
218         char *cp, *form = NULL;
219         char buf[BUFSIZ], *files[MAXARGS];
220         char **argp, **arguments;
221
222         setlocale(LC_ALL, "");
223         invo_name = mhbasename(argv[0]);
224
225         /* read user profile/context */
226         context_read();
227
228         arguments = getarguments(invo_name, argc, argv, 1);
229         argp = arguments;
230
231         if ((cp = getenv("MHLDEBUG")) && *cp)
232                 mhldebug++;
233
234         while ((cp = *argp++)) {
235                 if (*cp == '-') {
236                         switch (smatch(++cp, switches)) {
237                         case AMBIGSW:
238                                 ambigsw(cp, switches);
239                                 exit(EX_USAGE);
240                         case UNKWNSW:
241                                 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
242
243                         case HELPSW:
244                                 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
245                                 print_help(buf, switches, 1);
246                                 exit(argc == 2 ? EX_OK : EX_USAGE);
247                         case VERSIONSW:
248                                 print_version(invo_name);
249                                 exit(argc == 2 ? EX_OK : EX_USAGE);
250
251                         case FORMSW:
252                                 if (!(form = *argp++) || *form == '-')
253                                         adios(EX_USAGE, NULL, "missing argument to %s",
254                                                         argp[-2]);
255                                 continue;
256
257                         case WIDTHSW:
258                                 if (!(cp = *argp++) || *cp == '-')
259                                         adios(EX_USAGE, NULL, "missing argument to %s",
260                                                         argp[-2]);
261                                 if ((width = atoi(cp)) < 1)
262                                         adios(EX_USAGE, NULL, "bad argument %s %s",
263                                                         argp[-2], cp);
264                                 continue;
265
266                         case FORW2SW:
267                                 forwall++;  /* fall */
268                         case FORW1SW:
269                                 forwflg++;
270                                 continue;
271
272                         case NBODYSW:
273                                 dobody = 0;
274                                 continue;
275                         }
276                 }
277                 files[vecp++] = cp;
278         }
279
280         mhl_format(form ? form : mhlformat, width);
281
282         if (vecp == 0) {
283                 process(NULL, 1, vecp = 1);
284         } else {
285                 for (i = 0; i < vecp; i++)
286                         process(files[i], i + 1, vecp);
287         }
288
289         if (forwall) {
290                 printf("\n------- End of Forwarded Message%s\n\n",
291                                 vecp > 1 ? "s" : "");
292         }
293
294         fflush(stdout);
295         if (ferror(stdout)) {
296                 adios(EX_IOERR, "output", "error writing");
297         }
298
299         return exitstat;
300 }
301
302
303 static void
304 mhl_format(char *file, int width)
305 {
306         int i;
307         char *bp, *cp;
308         char *ap, buffer[BUFSIZ], name[NAMESZ];
309         struct mcomp *c1;
310         struct stat st;
311         FILE *fp;
312         static dev_t dev = 0;
313         static ino_t ino = 0;
314         static time_t mtime = 0;
315
316         if (fmthd != NULL) {
317                 if (stat(etcpath(file), &st) != NOTOK
318                         && mtime == st.st_mtime
319                         && dev == st.st_dev
320                         && ino == st.st_ino)
321                         goto out;
322                 else
323                         free_queue(&fmthd, &fmttl);
324         }
325
326         if ((fp = fopen(etcpath(file), "r")) == NULL)
327                 adios(EX_IOERR, file, "unable to open format file");
328
329         if (fstat(fileno(fp), &st) != NOTOK) {
330                 mtime = st.st_mtime;
331                 dev = st.st_dev;
332                 ino = st.st_ino;
333         }
334
335         global.c_ovtxt = global.c_fstr = NULL;
336         global.c_fmt = NULL;
337         global.c_offset = 0;
338         global.c_ovoff = -1;
339         if ((i = sc_width()) > 5)
340                 global.c_width = i;
341         global.c_cwidth = -1;
342         global.c_flags = 0;
343         *ignores = NULL;
344
345         while (vfgets(fp, &ap) == OK) {
346                 bp = ap;
347                 if (*bp == ';')
348                         continue;
349
350                 if ((cp = strchr(bp, '\n')))
351                         *cp = 0;
352
353                 if (*bp == ':') {
354                         c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
355                                         CLEARTEXT);
356                         continue;
357                 }
358
359                 parptr = bp;
360                 strncpy(name, parse(), sizeof(name));
361                 switch(*parptr) {
362                 case '\0':
363                 case ',':
364                 case '=':
365                         /*
366                         ** Split this list of fields to ignore, and copy
367                         ** it to the end of the current "ignores" list.
368                         */
369                         if (!mh_strcasecmp(name, "ignores")) {
370                                 char **tmparray;
371                                 int n = 0;
372
373                                 /* split the fields */
374                                 tmparray = brkstring(mh_xstrdup(++parptr), ",",
375                                                 NULL);
376                                 /*
377                                 ** copy pointers to split fields
378                                 ** to ignores array
379                                 */
380                                 while (tmparray[n] && num_ignores<MAXARGS-1) {
381                                         ignores[num_ignores++] = tmparray[n++];
382                                 }
383                                 ignores[num_ignores] = NULL;
384                                 continue;
385                         }
386                         parptr = bp;
387                         while (*parptr) {
388                                 if (evalvar(&global))
389                                         adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
390                                 if (*parptr)
391                                         parptr++;
392                         }
393                         continue;
394
395                 case ':':
396                         c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
397                         while (*parptr == ':' || *parptr == ',') {
398                                 parptr++;
399                                 if (evalvar(c1))
400                                         adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
401                         }
402                         if (!c1->c_fstr && global.c_fstr) {
403                                 if ((c1->c_flags & DATEFMT) &&
404                                                 (global.c_flags & DATEFMT)) {
405                                         c1->c_fstr = mh_xstrdup(global.c_fstr);
406                                 } else if ((c1->c_flags & ADDRFMT) &&
407                                                 (global.c_flags & ADDRFMT)) {
408                                         c1->c_fstr = mh_xstrdup(global.c_fstr);
409                                 }
410                         }
411                         continue;
412
413                 default:
414                         adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
415                 }
416         }
417         fclose(fp);
418
419         if (mhldebug) {
420                 for (c1 = fmthd; c1; c1 = c1->c_next) {
421                         fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
422                         fprintf(stderr, "\tfstr=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_fstr, (unsigned int)(unsigned long) c1->c_fmt);
423                         fprintf(stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d\n", c1->c_offset, c1->c_ovoff, c1->c_width, c1->c_cwidth);
424                         fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
425                 }
426         }
427
428 out:
429         if (width)
430                 global.c_width = width;
431         if (global.c_width < 5)
432                 global.c_width = 10000;
433 }
434
435
436 static int
437 evalvar(struct mcomp *c1)
438 {
439         char *cp, name[NAMESZ];
440         struct triple *ap;
441
442         if (!*parptr)
443                 return 0;
444         strncpy(name, parse(), sizeof(name));
445
446         if (!mh_strcasecmp(name, "component")) {
447                 if (ptos(name, &c1->c_text))
448                         return 1;
449                 c1->c_flags &= ~NOCOMPONENT;
450                 return 0;
451         }
452
453         if (!mh_strcasecmp(name, "overflowtext"))
454                 return ptos(name, &c1->c_ovtxt);
455
456         if (!mh_strcasecmp(name, "formatfield")) {
457                 char *fmtstr;
458
459                 if (ptos(name, &cp))
460                         return 1;
461                 cp = concat("=", cp, NULL);
462                 fmtstr = new_fs(cp, NULL);
463                 mh_free0(&cp);
464                 c1->c_fstr = mh_xstrdup(fmtstr);
465                 c1->c_flags |= FORMAT;
466                 return 0;
467         }
468
469         if (!mh_strcasecmp(name, "decode")) {
470                 char *fmtstr;
471
472                 fmtstr = new_fs("=%(decode{text})", NULL);
473                 c1->c_fstr = mh_xstrdup(fmtstr);
474                 c1->c_flags |= FORMAT;
475                 return 0;
476         }
477
478         if (!mh_strcasecmp(name, "offset"))
479                 return ptoi(name, &c1->c_offset);
480         if (!mh_strcasecmp(name, "overflowoffset"))
481                 return ptoi(name, &c1->c_ovoff);
482         if (!mh_strcasecmp(name, "width"))
483                 return ptoi(name, &c1->c_width);
484         if (!mh_strcasecmp(name, "compwidth"))
485                 return ptoi(name, &c1->c_cwidth);
486
487         for (ap = triples; ap->t_name; ap++)
488                 if (!mh_strcasecmp(ap->t_name, name)) {
489                         c1->c_flags |= ap->t_on;
490                         c1->c_flags &= ~ap->t_off;
491                         return 0;
492                 }
493
494         return 1;
495 }
496
497
498 static int
499 ptoi(char *name, int *i)
500 {
501         char *cp;
502
503         if (*parptr++ != '=' || !*(cp = parse())) {
504                 advise(NULL, "missing argument to variable %s", name);
505                 return 1;
506         }
507
508         *i = atoi(cp);
509         return 0;
510 }
511
512
513 static int
514 ptos(char *name, char **s)
515 {
516         char c, *cp;
517
518         if (*parptr++ != '=') {
519                 advise(NULL, "missing argument to variable %s", name);
520                 return 1;
521         }
522
523         if (*parptr != '"') {
524                 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
525                                 parptr++)
526                         continue;
527         } else {
528                 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
529                         if (*parptr == QUOTE)
530                                 if (!*++parptr)
531                                         parptr--;
532         }
533         c = *parptr;
534         *parptr = 0;
535         *s = mh_xstrdup(cp);
536         if ((*parptr = c) == '"')
537                 parptr++;
538         return 0;
539 }
540
541
542 static char *
543 parse(void)
544 {
545         int c;
546         char *cp;
547         static char result[NAMESZ];
548
549         for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
550                 c = *parptr;
551                 if (isalnum(c)
552                                 || c == '.'
553                                 || c == '-'
554                                 || c == '_'
555                                 || c == '['
556                                 || c == ']')
557                         *cp++ = c;
558                 else
559                         break;
560         }
561         *cp = '\0';
562
563         return result;
564 }
565
566
567 static void
568 process(char *fname, int ofilen, int ofilec)
569 {
570         FILE *fp = NULL;
571         struct mcomp *c1;
572
573         if (fname) {
574                 fp = fopen(fname, "r");
575                 if (fp == NULL) {
576                         advise(fname, "unable to open");
577                         exitstat++;
578                         return;
579                 }
580         } else {
581                 fname = "(stdin)";
582                 fp = stdin;
583         }
584         SIGNAL(SIGINT, intrser);
585         mhlfile(fp, fname, ofilen, ofilec);
586
587         SIGNAL(SIGINT, SIG_IGN);
588         if (fp != stdin)
589                 fclose(fp);
590         if (holder.c_text) {
591                 mh_free0(&(holder.c_text));
592         }
593         free_queue(&msghd, &msgtl);
594         for (c1 = fmthd; c1; c1 = c1->c_next)
595                 c1->c_flags &= ~HDROUTPUT;
596 }
597
598
599 static void
600 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
601 {
602         enum state state;
603         struct field f = {{0}};
604         struct mcomp *c1, *c2, *c3;
605         char **ip;
606
607         if (forwall) {
608                 printf("\n-------");
609                 if (ofilen == 1) {
610                         printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
611                 } else {
612                         printf(" Message %d", ofilen);
613                 }
614                 printf("\n\n");
615         } else if (ofilec > 1) {
616                 if (ofilen > 1) {
617                         printf("\n\n\n");
618                 }
619                 printf(">>> %s\n\n", mname);
620         }
621
622         for (state = FLD2; !eflag; ) {
623                 switch (state = m_getfld2(state, &f, fp)) {
624                 case FLD2:
625                         for (ip = ignores; *ip; ip++)
626                                 if (mh_strcasecmp(f.name, *ip)==0) {
627                                         break;
628                                 }
629                         if (*ip) {
630                                 continue;
631                         }
632
633                         for (c2 = fmthd; c2; c2 = c2->c_next)
634                                 if (mh_strcasecmp(c2->c_name, f.name)==0) {
635                                         break;
636                                 }
637                         c1 = NULL;
638                         if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
639                                 for (c1 = msghd; c1; c1 = c1->c_next)
640                                         if (mh_strcasecmp(c1->c_name,
641                                                         c3->c_name)==0) {
642                                                 c1->c_text = mcomp_add(c1->c_flags, f.value, c1->c_text);
643                                                 break;
644                                         }
645                         if (c1 == NULL) {
646                                 c1 = add_queue(&msghd, &msgtl, f.name, f.value, 0);
647                         }
648                         if (c2 == NULL) {
649                                 c1->c_flags |= EXTRA;
650                         }
651                         continue;
652
653                 case BODY2:
654                 case FILEEOF2:
655                         column = 0;
656                         for (c1 = fmthd; c1; c1 = c1->c_next) {
657                                 if (c1->c_flags & CLEARTEXT) {
658                                         putcomp(c1, c1, ONECOMP);
659                                         continue;
660                                 }
661                                 if (mh_strcasecmp(c1->c_name, "messagename")==0) {
662                                         holder.c_text = concat("(Message ",
663                                                         mname, ")\n", NULL);
664                                         putcomp(c1, &holder, ONECOMP);
665                                         mh_free0(&(holder.c_text));
666                                         continue;
667                                 }
668                                 if (mh_strcasecmp(c1->c_name, "extras")==0) {
669                                         for (c2 = msghd; c2; c2 = c2->c_next) {
670                                                 if (c2->c_flags & EXTRA) {
671                                                         putcomp(c1, c2, TWOCOMP);
672                                                 }
673                                         }
674                                         continue;
675                                 }
676                                 if (dobody && mh_strcasecmp(c1->c_name, "body")==0) {
677                                         holder.c_text = mh_xstrdup(f.value);
678                                         while (state == BODY2) {
679                                                 putcomp(c1, &holder, BODYCOMP);
680                                                 state = m_getfld2(state, &f, fp);
681                                                 free(holder.c_text);
682                                                 holder.c_text = mh_xstrdup(f.value);
683                                         }
684                                         mh_free0(&(holder.c_text));
685                                         continue;
686                                 }
687                                 for (c2 = msghd; c2; c2 = c2->c_next) {
688                                         if (mh_strcasecmp(c2->c_name,
689                                                         c1->c_name)==0) {
690                                                 putcomp(c1, c2, ONECOMP);
691                                                 if (!(c1->c_flags & SPLIT)) {
692                                                         break;
693                                                 }
694                                         }
695                                 }
696                         }
697                         return;
698
699                 case LENERR2:
700                 case FMTERR2:
701                 case IOERR2:
702                         advise(NULL, "format error in message %s", mname);
703                         exitstat++;
704                         return;
705
706                 default:
707                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
708                 }
709         }
710 }
711
712
713 static int
714 mcomp_flags(char *name)
715 {
716         struct pair *ap;
717
718         for (ap = pairs; ap->p_name; ap++)
719                 if (!mh_strcasecmp(ap->p_name, name))
720                         return (ap->p_flags);
721
722         return 0;
723 }
724
725
726 static char *
727 mcomp_add(long flags, char *s1, char *s2)
728 {
729         char *dp;
730
731         if (!(flags & ADDRFMT))
732                 return add(s1, s2);
733
734         if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
735                 *dp = 0;
736
737         return add(s1, add(",\n", s2));
738 }
739
740
741 struct pqpair {
742         char *pq_text;
743         char *pq_error;
744         struct pqpair *pq_next;
745 };
746
747
748 static void
749 mcomp_format(struct mcomp *c1, struct mcomp *c2)
750 {
751         int dat[5];
752         char *ap, *cp;
753         char buffer[BUFSIZ], error[BUFSIZ];
754         struct comp *cptr;
755         struct pqpair *p, *q;
756         struct pqpair pq;
757         struct mailname *mp;
758
759         ap = c2->c_text;
760         c2->c_text = NULL;
761         dat[0] = 0;
762         dat[1] = 0;
763         dat[2] = 0;
764         dat[3] = sizeof(buffer) - 1;
765         dat[4] = 0;
766         fmt_compile(c1->c_fstr, &c1->c_fmt);
767
768         if (!(c1->c_flags & ADDRFMT)) {
769                 FINDCOMP(cptr, "text");
770                 if (cptr)
771                         cptr->c_text = ap;
772                 if ((cp = strrchr(ap, '\n')))  /* drop ending newline */
773                         if (!cp[1])
774                                 *cp = 0;
775
776                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
777                 /* Don't need to append a newline, dctime() already did */
778                 c2->c_text = mh_xstrdup(buffer);
779
780                 mh_free0(&ap);
781                 return;
782         }
783
784         (q = &pq)->pq_next = NULL;
785         while ((cp = getname(ap))) {
786                 p = mh_xcalloc(1, sizeof(*p));
787
788                 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
789                         p->pq_text = mh_xstrdup(cp);
790                         p->pq_error = mh_xstrdup(error);
791                 } else {
792                         p->pq_text = mh_xstrdup(mp->m_text);
793                         mnfree(mp);
794                 }
795                 q = (q->pq_next = p);
796         }
797
798         for (p = pq.pq_next; p; p = q) {
799                 FINDCOMP(cptr, "text");
800                 if (cptr)
801                         cptr->c_text = p->pq_text;
802                 FINDCOMP(cptr, "error");
803                 if (cptr)
804                         cptr->c_text = p->pq_error;
805
806                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
807                 if (*buffer) {
808                         if (c2->c_text)
809                                 c2->c_text = add(",\n", c2->c_text);
810                         if (*(cp = buffer + strlen(buffer) - 1) == '\n')
811                                 *cp = 0;
812                         c2->c_text = add(buffer, c2->c_text);
813                 }
814
815                 mh_free0(&(p->pq_text));
816                 if (p->pq_error)
817                         mh_free0(&(p->pq_error));
818                 q = p->pq_next;
819                 mh_free0(&p);
820         }
821
822         c2->c_text = add("\n", c2->c_text);
823         free (ap);
824 }
825
826
827 static struct mcomp *
828 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
829                 char *text, int flags)
830 {
831         struct mcomp *c1;
832
833         c1 = mh_xcalloc(1, sizeof(*c1));
834
835         c1->c_flags = flags & ~INIT;
836         if ((c1->c_name = name ? mh_xstrdup(name) : NULL))
837                 c1->c_flags |= mcomp_flags(c1->c_name);
838         c1->c_text = text ? mh_xstrdup(text) : NULL;
839         if (flags & INIT) {
840                 if (global.c_ovtxt)
841                         c1->c_ovtxt = mh_xstrdup(global.c_ovtxt);
842                 c1->c_offset = global.c_offset;
843                 c1->c_ovoff = global. c_ovoff;
844                 c1->c_width = 0;
845                 c1->c_cwidth = global.c_cwidth;
846                 c1->c_flags |= global.c_flags & GFLAGS;
847         }
848         if (*head == NULL)
849                 *head = c1;
850         if (*tail != NULL)
851                 (*tail)->c_next = c1;
852         *tail = c1;
853
854         return c1;
855 }
856
857
858 static void
859 free_queue(struct mcomp **head, struct mcomp **tail)
860 {
861         struct mcomp *c1, *c2;
862
863         for (c1 = *head; c1; c1 = c2) {
864                 c2 = c1->c_next;
865                 if (c1->c_name)
866                         mh_free0(&(c1->c_name));
867                 if (c1->c_text)
868                         mh_free0(&(c1->c_text));
869                 if (c1->c_ovtxt)
870                         mh_free0(&(c1->c_ovtxt));
871                 if (c1->c_fstr)
872                         mh_free0(&(c1->c_fstr));
873                 if (c1->c_fmt)
874                         mh_free0(&(c1->c_fmt));
875                 mh_free0(&c1);
876         }
877
878         *head = *tail = NULL;
879 }
880
881
882 static void
883 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
884 {
885         int count, cchdr;
886         unsigned char *cp;
887
888         cchdr = 0;
889         lm = 0;
890         wid = c1->c_width ? c1->c_width : global.c_width;
891         ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
892                         + c1->c_offset;
893         if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
894                 ovtxt = "";
895         if (wid < ovoff + strlen(ovtxt) + 5)
896                 adios(EX_SOFTWARE, NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
897         onelp = NULL;
898
899         if (c1->c_flags & CLEARTEXT) {
900                 putstr(c1->c_text);
901                 putstr("\n");
902                 return;
903         }
904
905         if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
906                 mcomp_format(c1, c2);
907
908         if (c1->c_flags & CENTER) {
909                 count = (c1->c_width ? c1->c_width : global.c_width)
910                                 - c1->c_offset - strlen(c2->c_text);
911                 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
912                         count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
913                                         + 2;
914                 lm = c1->c_offset + (count / 2);
915         } else {
916                 if (c1->c_offset)
917                         lm = c1->c_offset;
918         }
919
920         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
921                 if (c1->c_flags & UPPERCASE)  /* uppercase component also */
922                         for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
923                                 if (islower(*cp))
924                                         *cp = toupper(*cp);
925                 putstr(c1->c_text ? c1->c_text : c1->c_name);
926                 if (flag != BODYCOMP) {
927                         putstr(": ");
928                         if (!(c1->c_flags & SPLIT))
929                                 c1->c_flags |= HDROUTPUT;
930
931                 cchdr++;
932                 if ((count = c1->c_cwidth -
933                         strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
934                         while (count--)
935                                 putstr(" ");
936                 } else
937                         c1->c_flags |= HDROUTPUT;  /* for BODYCOMP */
938         }
939
940         if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
941                 && !(c2->c_flags & NOCOMPONENT)) {
942                 if (c1->c_flags & UPPERCASE)
943                         for (cp = c2->c_name; *cp; cp++)
944                                 if (islower(*cp))
945                                         *cp = toupper(*cp);
946                 putstr(c2->c_name);
947                 putstr(": ");
948                 if (!(c1->c_flags & SPLIT))
949                         c2->c_flags |= HDROUTPUT;
950
951                 cchdr++;
952                 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
953                         while (count--)
954                                 putstr(" ");
955         }
956         if (c1->c_flags & UPPERCASE)
957                 for (cp = c2->c_text; *cp; cp++)
958                         if (islower(*cp))
959                                 *cp = toupper(*cp);
960
961         count = 0;
962         if (cchdr) {
963                 if (flag == TWOCOMP)
964                         count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
965                                         (int)strlen(c2->c_name) + 2;
966                 else
967                         count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
968                                         strlen(c1->c_text ?
969                                         c1->c_text : c1->c_name) + 2;
970         }
971         count += c1->c_offset;
972
973         if ((cp = oneline(c2->c_text, c1->c_flags)))
974            putstr(cp);
975         if (term == '\n')
976                 putstr("\n");
977         while ((cp = oneline(c2->c_text, c1->c_flags))) {
978                 lm = count;
979                 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
980                         putstr(c1->c_text ? c1->c_text : c1->c_name);
981                 if (*cp)
982                         putstr(cp);
983                 if (term == '\n')
984                         putstr("\n");
985         }
986         if (flag == BODYCOMP && term == '\n')
987                 c1->c_flags &= ~HDROUTPUT;  /* Buffer ended on a newline */
988 }
989
990
991 static char *
992 oneline(char *stuff, long flags)
993 {
994         int spc;
995         char *cp, *ret;
996
997         if (onelp == NULL)
998                 onelp = stuff;
999         if (*onelp == 0)
1000                 return (onelp = NULL);
1001
1002         ret = onelp;
1003         term = 0;
1004         if (flags & COMPRESS) {
1005                 for (spc = 1, cp = ret; *onelp; onelp++)
1006                         if (isspace(*onelp)) {
1007                                 if (*onelp == '\n' &&
1008                                                 (!onelp[1] ||
1009                                                 (flags & ADDRFMT))) {
1010                                         term = '\n';
1011                                         *onelp++ = 0;
1012                                         break;
1013                                 } else if (!spc) {
1014                                         *cp++ = ' ';
1015                                         spc++;
1016                                 }
1017                         } else {
1018                                 *cp++ = *onelp;
1019                                 spc = 0;
1020                         }
1021
1022                 *cp = 0;
1023         } else {
1024                 while (*onelp && *onelp != '\n')
1025                         onelp++;
1026                 if (*onelp == '\n') {
1027                         term = '\n';
1028                         *onelp++ = 0;
1029                 }
1030                 if (flags & LEFTADJUST)
1031                         while (*ret == ' ' || *ret == '\t')
1032                                 ret++;
1033         }
1034         if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1035                 term = 0;
1036
1037         return ret;
1038 }
1039
1040
1041 static void
1042 putstr(char *string)
1043 {
1044         if (!column && lm > 0) {
1045                 while (lm > 0)
1046                         if (lm >= 8) {
1047                                 putch('\t');
1048                                 lm -= 8;
1049                         } else {
1050                                 putch(' ');
1051                                 lm--;
1052                         }
1053         }
1054         lm = 0;
1055         while (*string)
1056                 putch(*string++);
1057 }
1058
1059
1060 static void
1061 putch(char ch)
1062 {
1063         switch (ch) {
1064         case '\t':
1065                 column |= 07;
1066                 column++;
1067                 break;
1068
1069         case '\b':
1070                 column--;
1071                 break;
1072
1073         case '\n':
1074         case '\r':
1075                 column = 0;
1076                 break;
1077
1078         default:
1079                 /*
1080                 ** If we are forwarding this message, and the first
1081                 ** column contains a dash, then add a dash and a space.
1082                 */
1083                 if (column == 0 && forwflg && ch == '-') {
1084                         putchar('-');
1085                         putchar(' ');
1086                 }
1087                 if (ch >= ' ')
1088                         column++;
1089                 break;
1090         }
1091
1092         if (column >= wid) {
1093                 putch('\n');
1094                 if (ovoff > 0)
1095                         lm = ovoff;
1096                 putstr(ovtxt ? ovtxt : "");
1097                 putch(ch);
1098                 return;
1099         }
1100
1101         putchar(ch);
1102 }
1103
1104
1105 static void
1106 intrser(int i)
1107 {
1108         eflag = 1;
1109 }