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