2 ** mhl.c -- the nmh message listing program
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.
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
22 ** for a component containing addresses, ADDRFMT, if COMPRESS is also
23 ** set, then addresses get split wrong (not at the spaces between commas).
24 ** To fix this correctly, putstr() should know about "atomic" strings that
25 ** must NOT be broken across lines. That's too difficult for right now
26 ** (it turns out that there are a number of degernate cases), so in
27 ** oneline(), instead of
29 ** (*onelp == '\n' && !onelp[1])
31 ** being a terminating condition,
33 ** (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
35 ** is used instead. This cuts the line prematurely, and gives us a much
36 ** better chance of getting things right.
45 static struct swit switches[] = {
47 { "form formfile", 0 },
49 { "width columns", 0 },
63 #define NOCOMPONENT 0x000001 /* don't show component name */
64 #define UPPERCASE 0x000002 /* display in all upper case */
65 #define CENTER 0x000004 /* center line */
66 #define CLEARTEXT 0x000008 /* cleartext */
67 #define EXTRA 0x000010 /* an "extra" component */
68 #define HDROUTPUT 0x000020 /* already output */
69 #define LEFTADJUST 0x000040 /* left justify multiple lines */
70 #define COMPRESS 0x000080 /* compress text */
71 #define ADDRFMT 0x000100 /* contains addresses */
72 #define DATEFMT 0x000200 /* contains dates */
73 #define FORMAT 0x000400 /* parse address/date/RFC-2047 field */
74 #define INIT 0x000800 /* initialize component */
75 #define SPLIT 0x001000 /* split headers (don't concatenate) */
76 #define NONEWLINE 0x002000 /* don't write trailing newline */
77 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07LEFTADJUST\010COMPRESS\011ADDRFMT\012DATEFMT\013FORMAT\014INIT\015SPLIT\016NONEWLINE"
78 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
81 char *c_name; /* component name */
82 char *c_text; /* component text */
83 char *c_ovtxt; /* text overflow indicator */
84 char *c_fstr; /* iff FORMAT */
85 struct format *c_fmt; /* .. */
86 int c_offset; /* left margin indentation */
87 int c_ovoff; /* overflow indentation */
88 int c_width; /* width of field */
89 int c_cwidth; /* width of component */
94 static struct mcomp *msghd = NULL;
95 static struct mcomp *msgtl = NULL;
96 static struct mcomp *fmthd = NULL;
97 static struct mcomp *fmttl = NULL;
99 static struct mcomp global = {
100 NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 0, NULL
103 static struct mcomp holder = {
104 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NOCOMPONENT, NULL
112 static struct pair pairs[] = {
115 { "Sender", ADDRFMT },
116 { "Reply-To", ADDRFMT },
120 { "Resent-Date", DATEFMT },
121 { "Resent-From", ADDRFMT },
122 { "Resent-Sender", ADDRFMT },
123 { "Resent-Reply-To", ADDRFMT },
124 { "Resent-To", ADDRFMT },
125 { "Resent-Cc", ADDRFMT },
126 { "Resent-Bcc", ADDRFMT },
136 static struct triple triples[] = {
137 { "nocomponent", NOCOMPONENT, 0 },
138 { "uppercase", UPPERCASE, 0 },
139 { "nouppercase", 0, UPPERCASE },
140 { "center", CENTER, 0 },
141 { "nocenter", 0, CENTER },
142 { "leftadjust", LEFTADJUST, 0 },
143 { "noleftadjust", 0, LEFTADJUST },
144 { "compress", COMPRESS, 0 },
145 { "nocompress", 0, COMPRESS },
146 { "split", SPLIT, 0 },
147 { "nosplit", 0, SPLIT },
148 { "addrfield", ADDRFMT, DATEFMT },
149 { "datefield", DATEFMT, ADDRFMT },
150 { "newline", 0, NONEWLINE },
151 { "nonewline", NONEWLINE, 0 },
156 static int dobody = 1;
157 static int forwflg = 0;
158 static int forwall = 0;
160 static int exitstat = 0;
161 static int mhldebug = 0;
163 static unsigned int column;
168 static unsigned int wid;
172 static unsigned char *onelp;
176 static int num_ignores = 0;
177 static char *ignores[MAXARGS];
179 volatile sig_atomic_t eflag = 0;
182 ** Redefine a couple of functions.
183 ** These are undefined later in the code.
189 static void mhl_format(char *, int);
190 static int evalvar(struct mcomp *);
191 static int ptoi(char *, int *);
192 static int ptos(char *, char **);
193 static char *parse(void);
194 static void process(char *, int, int);
195 static void mhlfile(FILE *, char *, int, int);
196 static int mcomp_flags(char *);
197 static char *mcomp_add(long, char *, char *);
198 static void mcomp_format(struct mcomp *, struct mcomp *);
199 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
200 char *, char *, int);
201 static void free_queue(struct mcomp **, struct mcomp **);
202 static void putcomp(struct mcomp *, struct mcomp *, int);
203 static char *oneline(char *, long);
204 static void putstr(char *);
205 static void putch(char);
206 static void intrser(int);
208 int sc_width(void); /* from termsbr.c */
212 main(int argc, char **argv)
214 int i, width = 0, vecp = 0;
215 char *cp, *form = NULL;
216 char buf[BUFSIZ], *files[MAXARGS];
217 char **argp, **arguments;
219 setlocale(LC_ALL, "");
220 invo_name = mhbasename(argv[0]);
222 /* read user profile/context */
225 arguments = getarguments(invo_name, argc, argv, 1);
228 if ((cp = getenv("MHLDEBUG")) && *cp)
231 while ((cp = *argp++)) {
233 switch (smatch(++cp, switches)) {
235 ambigsw(cp, switches);
238 adios(NULL, "-%s unknown\n", cp);
241 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
242 print_help(buf, switches, 1);
245 print_version(invo_name);
249 if (!(form = *argp++) || *form == '-')
250 adios(NULL, "missing argument to %s",
255 if (!(cp = *argp++) || *cp == '-')
256 adios(NULL, "missing argument to %s",
258 if ((width = atoi(cp)) < 1)
259 adios(NULL, "bad argument %s %s",
264 forwall++; /* fall */
277 mhl_format(form ? form : mhlformat, width);
280 process(NULL, 1, vecp = 1);
282 for (i = 0; i < vecp; i++)
283 process(files[i], i + 1, vecp);
287 printf("\n------- End of Forwarded Message%s\n\n",
288 vecp > 1 ? "s" : "");
292 if (ferror(stdout)) {
293 adios("output", "error writing");
301 mhl_format(char *file, int width)
305 char *ap, buffer[BUFSIZ], name[NAMESZ];
309 static dev_t dev = 0;
310 static ino_t ino = 0;
311 static time_t mtime = 0;
314 if (stat(etcpath(file), &st) != NOTOK
315 && mtime == st.st_mtime
320 free_queue(&fmthd, &fmttl);
323 if ((fp = fopen(etcpath(file), "r")) == NULL)
324 adios(file, "unable to open format file");
326 if (fstat(fileno(fp), &st) != NOTOK) {
332 global.c_ovtxt = global.c_fstr = NULL;
336 if ((i = sc_width()) > 5)
338 global.c_cwidth = -1;
342 while (vfgets(fp, &ap) == OK) {
347 if ((cp = strchr(bp, '\n')))
351 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
357 strncpy(name, parse(), sizeof(name));
363 ** Split this list of fields to ignore, and copy
364 ** it to the end of the current "ignores" list.
366 if (!mh_strcasecmp(name, "ignores")) {
370 /* split the fields */
371 tmparray = brkstring(getcpy(++parptr), ",",
374 ** copy pointers to split fields
377 while (tmparray[n] && num_ignores<MAXARGS-1) {
378 ignores[num_ignores++] = tmparray[n++];
380 ignores[num_ignores] = NULL;
385 if (evalvar(&global))
386 adios(NULL, "format file syntax error: %s", bp);
393 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
394 while (*parptr == ':' || *parptr == ',') {
397 adios(NULL, "format file syntax error: %s", bp);
399 if (!c1->c_fstr && global.c_fstr) {
400 if ((c1->c_flags & DATEFMT) &&
401 (global.c_flags & DATEFMT)) {
402 c1->c_fstr = getcpy(global.c_fstr);
403 } else if ((c1->c_flags & ADDRFMT) &&
404 (global.c_flags & ADDRFMT)) {
405 c1->c_fstr = getcpy(global.c_fstr);
411 adios(NULL, "format file syntax error: %s", bp);
417 for (c1 = fmthd; c1; c1 = c1->c_next) {
418 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
419 fprintf(stderr, "\tfstr=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_fstr, (unsigned int)(unsigned long) c1->c_fmt);
420 fprintf(stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d\n", c1->c_offset, c1->c_ovoff, c1->c_width, c1->c_cwidth);
421 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
427 global.c_width = width;
428 if (global.c_width < 5)
429 global.c_width = 10000;
434 evalvar(struct mcomp *c1)
436 char *cp, name[NAMESZ];
441 strncpy(name, parse(), sizeof(name));
443 if (!mh_strcasecmp(name, "component")) {
444 if (ptos(name, &c1->c_text))
446 c1->c_flags &= ~NOCOMPONENT;
450 if (!mh_strcasecmp(name, "overflowtext"))
451 return ptos(name, &c1->c_ovtxt);
453 if (!mh_strcasecmp(name, "formatfield")) {
458 cp = concat("=", cp, NULL);
459 fmtstr = new_fs(cp, NULL);
461 c1->c_fstr = getcpy(fmtstr);
462 c1->c_flags |= FORMAT;
466 if (!mh_strcasecmp(name, "decode")) {
469 fmtstr = new_fs("=%(decode{text})", NULL);
470 c1->c_fstr = getcpy(fmtstr);
471 c1->c_flags |= FORMAT;
475 if (!mh_strcasecmp(name, "offset"))
476 return ptoi(name, &c1->c_offset);
477 if (!mh_strcasecmp(name, "overflowoffset"))
478 return ptoi(name, &c1->c_ovoff);
479 if (!mh_strcasecmp(name, "width"))
480 return ptoi(name, &c1->c_width);
481 if (!mh_strcasecmp(name, "compwidth"))
482 return ptoi(name, &c1->c_cwidth);
484 for (ap = triples; ap->t_name; ap++)
485 if (!mh_strcasecmp(ap->t_name, name)) {
486 c1->c_flags |= ap->t_on;
487 c1->c_flags &= ~ap->t_off;
496 ptoi(char *name, int *i)
500 if (*parptr++ != '=' || !*(cp = parse())) {
501 advise(NULL, "missing argument to variable %s", name);
511 ptos(char *name, char **s)
515 if (*parptr++ != '=') {
516 advise(NULL, "missing argument to variable %s", name);
520 if (*parptr != '"') {
521 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
525 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
526 if (*parptr == QUOTE)
533 if ((*parptr = c) == '"')
544 static char result[NAMESZ];
546 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
565 process(char *fname, int ofilen, int ofilec)
571 fp = fopen(fname, "r");
573 advise(fname, "unable to open");
581 SIGNAL(SIGINT, intrser);
582 mhlfile(fp, fname, ofilen, ofilec);
584 SIGNAL(SIGINT, SIG_IGN);
589 holder.c_text = NULL;
591 free_queue(&msghd, &msgtl);
592 for (c1 = fmthd; c1; c1 = c1->c_next)
593 c1->c_flags &= ~HDROUTPUT;
598 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
601 struct mcomp *c1, *c2, *c3;
602 char **ip, name[NAMESZ], buf[BUFSIZ];
607 printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
609 printf(" Message %d", ofilen);
611 } else if (ofilec > 1) {
615 printf(">>> %s\n\n", mname);
618 for (state = FLD;!eflag;) {
619 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
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);
631 for (c2 = fmthd; c2; c2 = c2->c_next)
632 if (!mh_strcasecmp(c2->c_name, name))
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,
639 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
643 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
644 while (state == FLDPLUS) {
645 state = m_getfld(state, name, buf,
647 c1->c_text = add(buf, c1->c_text);
650 c1->c_flags |= EXTRA;
656 for (c1 = fmthd; c1; c1 = c1->c_next) {
657 if (c1->c_flags & CLEARTEXT) {
658 putcomp(c1, c1, ONECOMP);
661 if (!mh_strcasecmp(c1->c_name, "messagename")) {
662 holder.c_text = concat("(Message ",
664 putcomp(c1, &holder, ONECOMP);
666 holder.c_text = NULL;
669 if (!mh_strcasecmp(c1->c_name, "extras")) {
670 for (c2 = msghd; c2; c2 = c2->c_next)
671 if (c2->c_flags & EXTRA)
672 putcomp(c1, c2, TWOCOMP);
675 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
676 holder.c_text = mh_xmalloc(sizeof(buf));
677 strncpy(holder.c_text, buf, sizeof(buf));
678 while (state == BODY) {
679 putcomp(c1, &holder, BODYCOMP);
680 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
683 holder.c_text = NULL;
686 for (c2 = msghd; c2; c2 = c2->c_next)
687 if (!mh_strcasecmp(c2->c_name,
689 putcomp(c1, c2, ONECOMP);
690 if (!(c1->c_flags & SPLIT))
698 advise(NULL, "format error in message %s", mname);
703 adios(NULL, "getfld() returned %d", state);
710 mcomp_flags(char *name)
714 for (ap = pairs; ap->p_name; ap++)
715 if (!mh_strcasecmp(ap->p_name, name))
716 return (ap->p_flags);
723 mcomp_add(long flags, char *s1, char *s2)
727 if (!(flags & ADDRFMT))
730 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
733 return add(s1, add(",\n", s2));
740 struct pqpair *pq_next;
745 mcomp_format(struct mcomp *c1, struct mcomp *c2)
749 char buffer[BUFSIZ], error[BUFSIZ];
751 struct pqpair *p, *q;
760 dat[3] = sizeof(buffer) - 1;
762 fmt_compile(c1->c_fstr, &c1->c_fmt);
764 if (!(c1->c_flags & ADDRFMT)) {
765 FINDCOMP(cptr, "text");
768 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
772 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
773 /* Don't need to append a newline, dctime() already did */
774 c2->c_text = getcpy(buffer);
780 (q = &pq)->pq_next = NULL;
781 while ((cp = getname(ap))) {
782 if ((p = (struct pqpair *)
783 calloc((size_t) 1, sizeof(*p))) == NULL)
784 adios(NULL, "unable to allocate pqpair memory");
786 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
787 p->pq_text = getcpy(cp);
788 p->pq_error = getcpy(error);
790 p->pq_text = getcpy(mp->m_text);
793 q = (q->pq_next = p);
796 for (p = pq.pq_next; p; p = q) {
797 FINDCOMP(cptr, "text");
799 cptr->c_text = p->pq_text;
800 FINDCOMP(cptr, "error");
802 cptr->c_text = p->pq_error;
804 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
807 c2->c_text = add(",\n", c2->c_text);
808 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
810 c2->c_text = add(buffer, c2->c_text);
820 c2->c_text = add("\n", c2->c_text);
825 static struct mcomp *
826 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
827 char *text, int flags)
831 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
832 adios(NULL, "unable to allocate comp memory");
834 c1->c_flags = flags & ~INIT;
835 if ((c1->c_name = name ? getcpy(name) : NULL))
836 c1->c_flags |= mcomp_flags(c1->c_name);
837 c1->c_text = text ? getcpy(text) : NULL;
840 c1->c_ovtxt = getcpy(global.c_ovtxt);
841 c1->c_offset = global.c_offset;
842 c1->c_ovoff = global. c_ovoff;
844 c1->c_cwidth = global.c_cwidth;
845 c1->c_flags |= global.c_flags & GFLAGS;
850 (*tail)->c_next = c1;
858 free_queue(struct mcomp **head, struct mcomp **tail)
860 struct mcomp *c1, *c2;
862 for (c1 = *head; c1; c1 = c2) {
873 free((char *) c1->c_fmt);
877 *head = *tail = NULL;
882 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
889 wid = c1->c_width ? c1->c_width : global.c_width;
890 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
892 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
894 if (wid < ovoff + strlen(ovtxt) + 5)
895 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
898 if (c1->c_flags & CLEARTEXT) {
904 if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
905 mcomp_format(c1, c2);
907 if (c1->c_flags & CENTER) {
908 count = (c1->c_width ? c1->c_width : global.c_width)
909 - c1->c_offset - strlen(c2->c_text);
910 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
911 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
913 lm = c1->c_offset + (count / 2);
919 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
920 if (c1->c_flags & UPPERCASE) /* uppercase component also */
921 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
924 putstr(c1->c_text ? c1->c_text : c1->c_name);
925 if (flag != BODYCOMP) {
927 if (!(c1->c_flags & SPLIT))
928 c1->c_flags |= HDROUTPUT;
931 if ((count = c1->c_cwidth -
932 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
936 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
939 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
940 && !(c2->c_flags & NOCOMPONENT)) {
941 if (c1->c_flags & UPPERCASE)
942 for (cp = c2->c_name; *cp; cp++)
947 if (!(c1->c_flags & SPLIT))
948 c2->c_flags |= HDROUTPUT;
951 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
955 if (c1->c_flags & UPPERCASE)
956 for (cp = c2->c_text; *cp; cp++)
963 count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
964 (int)strlen(c2->c_name) + 2;
966 count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
968 c1->c_text : c1->c_name) + 2;
970 count += c1->c_offset;
972 if ((cp = oneline(c2->c_text, c1->c_flags)))
976 while ((cp = oneline(c2->c_text, c1->c_flags))) {
978 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
979 putstr(c1->c_text ? c1->c_text : c1->c_name);
985 if (flag == BODYCOMP && term == '\n')
986 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
991 oneline(char *stuff, long flags)
999 return (onelp = NULL);
1003 if (flags & COMPRESS) {
1004 for (spc = 1, cp = ret; *onelp; onelp++)
1005 if (isspace(*onelp)) {
1006 if (*onelp == '\n' &&
1008 (flags & ADDRFMT))) {
1023 while (*onelp && *onelp != '\n')
1025 if (*onelp == '\n') {
1029 if (flags & LEFTADJUST)
1030 while (*ret == ' ' || *ret == '\t')
1033 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1041 putstr(char *string)
1043 if (!column && lm > 0) {
1079 ** If we are forwarding this message, and the first
1080 ** column contains a dash, then add a dash and a space.
1082 if (column == 0 && forwflg && ch == '-') {
1091 if (column >= wid) {
1095 putstr(ovtxt ? ovtxt : "");