Replace getcpy() and strdup() with mh_xstrdup()
[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         int state;
601         struct mcomp *c1, *c2, *c3;
602         char **ip, name[NAMESZ], buf[BUFSIZ];
603
604         if (forwall) {
605                 printf("\n-------");
606                 if (ofilen == 1)
607                         printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
608                 else
609                         printf(" Message %d", ofilen);
610                 printf("\n\n");
611         } else if (ofilec > 1) {
612                 if (ofilen > 1) {
613                         printf("\n\n\n");
614                 }
615                 printf(">>> %s\n\n", mname);
616         }
617
618         for (state = FLD;!eflag;) {
619                 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
620                 case FLD:
621                 case FLDPLUS:
622                         for (ip = ignores; *ip; ip++)
623                                 if (!mh_strcasecmp(name, *ip)) {
624                                         while (state == FLDPLUS)
625                                                 state = m_getfld(state, name, buf, sizeof(buf), fp);
626                                         break;
627                                 }
628                         if (*ip)
629                                 continue;
630
631                         for (c2 = fmthd; c2; c2 = c2->c_next)
632                                 if (!mh_strcasecmp(c2->c_name, name))
633                                         break;
634                         c1 = NULL;
635                         if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
636                                 for (c1 = msghd; c1; c1 = c1->c_next)
637                                         if (!mh_strcasecmp(c1->c_name,
638                                                         c3->c_name)) {
639                                                 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
640                                                 break;
641                                         }
642                         if (c1 == NULL)
643                                 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
644                         while (state == FLDPLUS) {
645                                 state = m_getfld(state, name, buf,
646                                                 sizeof(buf), fp);
647                                 c1->c_text = add(buf, c1->c_text);
648                         }
649                         if (c2 == NULL)
650                                 c1->c_flags |= EXTRA;
651                         continue;
652
653                 case BODY:
654                 case FILEEOF:
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")) {
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")) {
669                                         for (c2 = msghd; c2; c2 = c2->c_next)
670                                                 if (c2->c_flags & EXTRA)
671                                                         putcomp(c1, c2, TWOCOMP);
672                                         continue;
673                                 }
674                                 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
675                                         holder.c_text = mh_xcalloc(sizeof(buf), sizeof(char));
676                                         strncpy(holder.c_text, buf, sizeof(buf));
677                                         while (state == BODY) {
678                                                 putcomp(c1, &holder, BODYCOMP);
679                                                 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
680                                         }
681                                         mh_free0(&(holder.c_text));
682                                         continue;
683                                 }
684                                 for (c2 = msghd; c2; c2 = c2->c_next)
685                                         if (!mh_strcasecmp(c2->c_name,
686                                                         c1->c_name)) {
687                                                 putcomp(c1, c2, ONECOMP);
688                                                 if (!(c1->c_flags & SPLIT))
689                                                         break;
690                                         }
691                         }
692                         return;
693
694                 case LENERR:
695                 case FMTERR:
696                         advise(NULL, "format error in message %s", mname);
697                         exitstat++;
698                         return;
699
700                 default:
701                         adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
702                 }
703         }
704 }
705
706
707 static int
708 mcomp_flags(char *name)
709 {
710         struct pair *ap;
711
712         for (ap = pairs; ap->p_name; ap++)
713                 if (!mh_strcasecmp(ap->p_name, name))
714                         return (ap->p_flags);
715
716         return 0;
717 }
718
719
720 static char *
721 mcomp_add(long flags, char *s1, char *s2)
722 {
723         char *dp;
724
725         if (!(flags & ADDRFMT))
726                 return add(s1, s2);
727
728         if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
729                 *dp = 0;
730
731         return add(s1, add(",\n", s2));
732 }
733
734
735 struct pqpair {
736         char *pq_text;
737         char *pq_error;
738         struct pqpair *pq_next;
739 };
740
741
742 static void
743 mcomp_format(struct mcomp *c1, struct mcomp *c2)
744 {
745         int dat[5];
746         char *ap, *cp;
747         char buffer[BUFSIZ], error[BUFSIZ];
748         struct comp *cptr;
749         struct pqpair *p, *q;
750         struct pqpair pq;
751         struct mailname *mp;
752
753         ap = c2->c_text;
754         c2->c_text = NULL;
755         dat[0] = 0;
756         dat[1] = 0;
757         dat[2] = 0;
758         dat[3] = sizeof(buffer) - 1;
759         dat[4] = 0;
760         fmt_compile(c1->c_fstr, &c1->c_fmt);
761
762         if (!(c1->c_flags & ADDRFMT)) {
763                 FINDCOMP(cptr, "text");
764                 if (cptr)
765                         cptr->c_text = ap;
766                 if ((cp = strrchr(ap, '\n')))  /* drop ending newline */
767                         if (!cp[1])
768                                 *cp = 0;
769
770                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
771                 /* Don't need to append a newline, dctime() already did */
772                 c2->c_text = mh_xstrdup(buffer);
773
774                 mh_free0(&ap);
775                 return;
776         }
777
778         (q = &pq)->pq_next = NULL;
779         while ((cp = getname(ap))) {
780                 p = mh_xcalloc(1, sizeof(*p));
781
782                 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
783                         p->pq_text = mh_xstrdup(cp);
784                         p->pq_error = mh_xstrdup(error);
785                 } else {
786                         p->pq_text = mh_xstrdup(mp->m_text);
787                         mnfree(mp);
788                 }
789                 q = (q->pq_next = p);
790         }
791
792         for (p = pq.pq_next; p; p = q) {
793                 FINDCOMP(cptr, "text");
794                 if (cptr)
795                         cptr->c_text = p->pq_text;
796                 FINDCOMP(cptr, "error");
797                 if (cptr)
798                         cptr->c_text = p->pq_error;
799
800                 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
801                 if (*buffer) {
802                         if (c2->c_text)
803                                 c2->c_text = add(",\n", c2->c_text);
804                         if (*(cp = buffer + strlen(buffer) - 1) == '\n')
805                                 *cp = 0;
806                         c2->c_text = add(buffer, c2->c_text);
807                 }
808
809                 mh_free0(&(p->pq_text));
810                 if (p->pq_error)
811                         mh_free0(&(p->pq_error));
812                 q = p->pq_next;
813                 mh_free0(&p);
814         }
815
816         c2->c_text = add("\n", c2->c_text);
817         free (ap);
818 }
819
820
821 static struct mcomp *
822 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
823                 char *text, int flags)
824 {
825         struct mcomp *c1;
826
827         c1 = mh_xcalloc(1, sizeof(*c1));
828
829         c1->c_flags = flags & ~INIT;
830         if ((c1->c_name = name ? mh_xstrdup(name) : NULL))
831                 c1->c_flags |= mcomp_flags(c1->c_name);
832         c1->c_text = text ? mh_xstrdup(text) : NULL;
833         if (flags & INIT) {
834                 if (global.c_ovtxt)
835                         c1->c_ovtxt = mh_xstrdup(global.c_ovtxt);
836                 c1->c_offset = global.c_offset;
837                 c1->c_ovoff = global. c_ovoff;
838                 c1->c_width = 0;
839                 c1->c_cwidth = global.c_cwidth;
840                 c1->c_flags |= global.c_flags & GFLAGS;
841         }
842         if (*head == NULL)
843                 *head = c1;
844         if (*tail != NULL)
845                 (*tail)->c_next = c1;
846         *tail = c1;
847
848         return c1;
849 }
850
851
852 static void
853 free_queue(struct mcomp **head, struct mcomp **tail)
854 {
855         struct mcomp *c1, *c2;
856
857         for (c1 = *head; c1; c1 = c2) {
858                 c2 = c1->c_next;
859                 if (c1->c_name)
860                         mh_free0(&(c1->c_name));
861                 if (c1->c_text)
862                         mh_free0(&(c1->c_text));
863                 if (c1->c_ovtxt)
864                         mh_free0(&(c1->c_ovtxt));
865                 if (c1->c_fstr)
866                         mh_free0(&(c1->c_fstr));
867                 if (c1->c_fmt)
868                         mh_free0(&(c1->c_fmt));
869                 mh_free0(&c1);
870         }
871
872         *head = *tail = NULL;
873 }
874
875
876 static void
877 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
878 {
879         int count, cchdr;
880         unsigned char *cp;
881
882         cchdr = 0;
883         lm = 0;
884         wid = c1->c_width ? c1->c_width : global.c_width;
885         ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
886                         + c1->c_offset;
887         if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
888                 ovtxt = "";
889         if (wid < ovoff + strlen(ovtxt) + 5)
890                 adios(EX_SOFTWARE, NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
891         onelp = NULL;
892
893         if (c1->c_flags & CLEARTEXT) {
894                 putstr(c1->c_text);
895                 putstr("\n");
896                 return;
897         }
898
899         if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
900                 mcomp_format(c1, c2);
901
902         if (c1->c_flags & CENTER) {
903                 count = (c1->c_width ? c1->c_width : global.c_width)
904                                 - c1->c_offset - strlen(c2->c_text);
905                 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
906                         count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
907                                         + 2;
908                 lm = c1->c_offset + (count / 2);
909         } else {
910                 if (c1->c_offset)
911                         lm = c1->c_offset;
912         }
913
914         if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
915                 if (c1->c_flags & UPPERCASE)  /* uppercase component also */
916                         for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
917                                 if (islower(*cp))
918                                         *cp = toupper(*cp);
919                 putstr(c1->c_text ? c1->c_text : c1->c_name);
920                 if (flag != BODYCOMP) {
921                         putstr(": ");
922                         if (!(c1->c_flags & SPLIT))
923                                 c1->c_flags |= HDROUTPUT;
924
925                 cchdr++;
926                 if ((count = c1->c_cwidth -
927                         strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
928                         while (count--)
929                                 putstr(" ");
930                 } else
931                         c1->c_flags |= HDROUTPUT;  /* for BODYCOMP */
932         }
933
934         if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
935                 && !(c2->c_flags & NOCOMPONENT)) {
936                 if (c1->c_flags & UPPERCASE)
937                         for (cp = c2->c_name; *cp; cp++)
938                                 if (islower(*cp))
939                                         *cp = toupper(*cp);
940                 putstr(c2->c_name);
941                 putstr(": ");
942                 if (!(c1->c_flags & SPLIT))
943                         c2->c_flags |= HDROUTPUT;
944
945                 cchdr++;
946                 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
947                         while (count--)
948                                 putstr(" ");
949         }
950         if (c1->c_flags & UPPERCASE)
951                 for (cp = c2->c_text; *cp; cp++)
952                         if (islower(*cp))
953                                 *cp = toupper(*cp);
954
955         count = 0;
956         if (cchdr) {
957                 if (flag == TWOCOMP)
958                         count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
959                                         (int)strlen(c2->c_name) + 2;
960                 else
961                         count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
962                                         strlen(c1->c_text ?
963                                         c1->c_text : c1->c_name) + 2;
964         }
965         count += c1->c_offset;
966
967         if ((cp = oneline(c2->c_text, c1->c_flags)))
968            putstr(cp);
969         if (term == '\n')
970                 putstr("\n");
971         while ((cp = oneline(c2->c_text, c1->c_flags))) {
972                 lm = count;
973                 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
974                         putstr(c1->c_text ? c1->c_text : c1->c_name);
975                 if (*cp)
976                         putstr(cp);
977                 if (term == '\n')
978                         putstr("\n");
979         }
980         if (flag == BODYCOMP && term == '\n')
981                 c1->c_flags &= ~HDROUTPUT;  /* Buffer ended on a newline */
982 }
983
984
985 static char *
986 oneline(char *stuff, long flags)
987 {
988         int spc;
989         char *cp, *ret;
990
991         if (onelp == NULL)
992                 onelp = stuff;
993         if (*onelp == 0)
994                 return (onelp = NULL);
995
996         ret = onelp;
997         term = 0;
998         if (flags & COMPRESS) {
999                 for (spc = 1, cp = ret; *onelp; onelp++)
1000                         if (isspace(*onelp)) {
1001                                 if (*onelp == '\n' &&
1002                                                 (!onelp[1] ||
1003                                                 (flags & ADDRFMT))) {
1004                                         term = '\n';
1005                                         *onelp++ = 0;
1006                                         break;
1007                                 } else if (!spc) {
1008                                         *cp++ = ' ';
1009                                         spc++;
1010                                 }
1011                         } else {
1012                                 *cp++ = *onelp;
1013                                 spc = 0;
1014                         }
1015
1016                 *cp = 0;
1017         } else {
1018                 while (*onelp && *onelp != '\n')
1019                         onelp++;
1020                 if (*onelp == '\n') {
1021                         term = '\n';
1022                         *onelp++ = 0;
1023                 }
1024                 if (flags & LEFTADJUST)
1025                         while (*ret == ' ' || *ret == '\t')
1026                                 ret++;
1027         }
1028         if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1029                 term = 0;
1030
1031         return ret;
1032 }
1033
1034
1035 static void
1036 putstr(char *string)
1037 {
1038         if (!column && lm > 0) {
1039                 while (lm > 0)
1040                         if (lm >= 8) {
1041                                 putch('\t');
1042                                 lm -= 8;
1043                         } else {
1044                                 putch(' ');
1045                                 lm--;
1046                         }
1047         }
1048         lm = 0;
1049         while (*string)
1050                 putch(*string++);
1051 }
1052
1053
1054 static void
1055 putch(char ch)
1056 {
1057         switch (ch) {
1058         case '\t':
1059                 column |= 07;
1060                 column++;
1061                 break;
1062
1063         case '\b':
1064                 column--;
1065                 break;
1066
1067         case '\n':
1068         case '\r':
1069                 column = 0;
1070                 break;
1071
1072         default:
1073                 /*
1074                 ** If we are forwarding this message, and the first
1075                 ** column contains a dash, then add a dash and a space.
1076                 */
1077                 if (column == 0 && forwflg && ch == '-') {
1078                         putchar('-');
1079                         putchar(' ');
1080                 }
1081                 if (ch >= ' ')
1082                         column++;
1083                 break;
1084         }
1085
1086         if (column >= wid) {
1087                 putch('\n');
1088                 if (ovoff > 0)
1089                         lm = ovoff;
1090                 putstr(ovtxt ? ovtxt : "");
1091                 putch(ch);
1092                 return;
1093         }
1094
1095         putchar(ch);
1096 }
1097
1098
1099 static void
1100 intrser(int i)
1101 {
1102         eflag = 1;
1103 }