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