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>
20 ** for a component containing addresses, ADDRFMT, if COMPRESS is also
21 ** set, then addresses get split wrong (not at the spaces between commas).
22 ** To fix this correctly, putstr() should know about "atomic" strings that
23 ** must NOT be broken across lines. That's too difficult for right now
24 ** (it turns out that there are a number of degernate cases), so in
25 ** oneline(), instead of
27 ** (*onelp == '\n' && !onelp[1])
29 ** being a terminating condition,
31 ** (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
33 ** is used instead. This cuts the line prematurely, and gives us a much
34 ** better chance of getting things right.
43 static struct swit switches[] = {
45 { "form formfile", 0 },
47 { "width columns", 0 },
61 #define NOCOMPONENT 0x000001 /* don't show component name */
62 #define UPPERCASE 0x000002 /* display in all upper case */
63 #define CENTER 0x000004 /* center line */
64 #define CLEARTEXT 0x000008 /* cleartext */
65 #define EXTRA 0x000010 /* an "extra" component */
66 #define HDROUTPUT 0x000020 /* already output */
67 #define LEFTADJUST 0x000040 /* left justify multiple lines */
68 #define COMPRESS 0x000080 /* compress text */
69 #define ADDRFMT 0x000100 /* contains addresses */
70 #define DATEFMT 0x000200 /* contains dates */
71 #define FORMAT 0x000400 /* parse address/date/RFC-2047 field */
72 #define INIT 0x000800 /* initialize component */
73 #define SPLIT 0x001000 /* split headers (don't concatenate) */
74 #define NONEWLINE 0x002000 /* don't write trailing newline */
75 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07LEFTADJUST\010COMPRESS\011ADDRFMT\012DATEFMT\013FORMAT\014INIT\015SPLIT\016NONEWLINE"
76 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
79 char *c_name; /* component name */
80 char *c_text; /* component text */
81 char *c_ovtxt; /* text overflow indicator */
82 char *c_fstr; /* iff FORMAT */
83 struct format *c_fmt; /* .. */
84 int c_offset; /* left margin indentation */
85 int c_ovoff; /* overflow indentation */
86 int c_width; /* width of field */
87 int c_cwidth; /* width of component */
92 static struct mcomp *msghd = NULL;
93 static struct mcomp *msgtl = NULL;
94 static struct mcomp *fmthd = NULL;
95 static struct mcomp *fmttl = NULL;
97 static struct mcomp global = {
98 NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 0, NULL
101 static struct mcomp holder = {
102 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NOCOMPONENT, NULL
110 static struct pair pairs[] = {
113 { "Sender", ADDRFMT },
114 { "Reply-To", ADDRFMT },
118 { "Resent-Date", DATEFMT },
119 { "Resent-From", ADDRFMT },
120 { "Resent-Sender", ADDRFMT },
121 { "Resent-Reply-To", ADDRFMT },
122 { "Resent-To", ADDRFMT },
123 { "Resent-Cc", ADDRFMT },
124 { "Resent-Bcc", ADDRFMT },
134 static struct triple triples[] = {
135 { "nocomponent", NOCOMPONENT, 0 },
136 { "uppercase", UPPERCASE, 0 },
137 { "nouppercase", 0, UPPERCASE },
138 { "center", CENTER, 0 },
139 { "nocenter", 0, CENTER },
140 { "leftadjust", LEFTADJUST, 0 },
141 { "noleftadjust", 0, LEFTADJUST },
142 { "compress", COMPRESS, 0 },
143 { "nocompress", 0, COMPRESS },
144 { "split", SPLIT, 0 },
145 { "nosplit", 0, SPLIT },
146 { "addrfield", ADDRFMT, DATEFMT },
147 { "datefield", DATEFMT, ADDRFMT },
148 { "newline", 0, NONEWLINE },
149 { "nonewline", NONEWLINE, 0 },
154 static int dobody = 1;
155 static int forwflg = 0;
156 static int forwall = 0;
158 static int exitstat = 0;
159 static int mhldebug = 0;
161 static unsigned int column;
166 static unsigned int wid;
170 static unsigned char *onelp;
174 static int num_ignores = 0;
175 static char *ignores[MAXARGS];
178 static jmp_buf mhlenv;
180 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
184 ** Redefine a couple of functions.
185 ** These are undefined later in the code.
187 #define adios mhladios
193 static void mhl_format(char *, int);
194 static int evalvar(struct mcomp *);
195 static int ptoi(char *, int *);
196 static int ptos(char *, char **);
197 static char *parse(void);
198 static void process(char *, int, int);
199 static void mhlfile(FILE *, char *, int, int);
200 static int mcomp_flags(char *);
201 static char *mcomp_add(long, char *, char *);
202 static void mcomp_format(struct mcomp *, struct mcomp *);
203 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
204 char *, char *, int);
205 static void free_queue(struct mcomp **, struct mcomp **);
206 static void putcomp(struct mcomp *, struct mcomp *, int);
207 static char *oneline(char *, long);
208 static void putstr(char *);
209 static void putch(char);
210 static void intrser(int);
211 static void mhladios(char *, char *, ...);
212 static void mhldone(int);
214 int sc_width(void); /* from termsbr.c */
218 main(int argc, char **argv)
220 int i, width = 0, vecp = 0;
221 char *cp, *form = NULL;
222 char buf[BUFSIZ], *files[MAXARGS];
223 char **argp, **arguments;
225 setlocale(LC_ALL, "");
226 invo_name = mhbasename(argv[0]);
228 /* read user profile/context */
231 arguments = getarguments(invo_name, argc, argv, 1);
234 if ((cp = getenv("MHLDEBUG")) && *cp)
237 while ((cp = *argp++)) {
239 switch (smatch(++cp, switches)) {
241 ambigsw(cp, switches);
244 adios(NULL, "-%s unknown\n", cp);
247 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
248 print_help(buf, switches, 1);
251 print_version(invo_name);
255 if (!(form = *argp++) || *form == '-')
256 adios(NULL, "missing argument to %s",
261 if (!(cp = *argp++) || *cp == '-')
262 adios(NULL, "missing argument to %s",
264 if ((width = atoi(cp)) < 1)
265 adios(NULL, "bad argument %s %s",
270 forwall++; /* fall */
283 mhl_format(form ? form : mhlformat, width);
286 process(NULL, 1, vecp = 1);
288 for (i = 0; i < vecp; i++)
289 process(files[i], i + 1, vecp);
293 printf("\n------- End of Forwarded Message%s\n\n",
294 vecp > 1 ? "s" : "");
298 if (ferror(stdout)) {
299 adios("output", "error writing");
307 mhl_format(char *file, int width)
311 char *ap, buffer[BUFSIZ], name[NAMESZ];
315 static dev_t dev = 0;
316 static ino_t ino = 0;
317 static time_t mtime = 0;
320 if (stat(etcpath(file), &st) != NOTOK
321 && mtime == st.st_mtime
326 free_queue(&fmthd, &fmttl);
329 if ((fp = fopen(etcpath(file), "r")) == NULL)
330 adios(file, "unable to open format file");
332 if (fstat(fileno(fp), &st) != NOTOK) {
338 global.c_ovtxt = global.c_fstr = NULL;
342 if ((i = sc_width()) > 5)
344 global.c_cwidth = -1;
348 while (vfgets(fp, &ap) == OK) {
353 if ((cp = strchr(bp, '\n')))
357 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
363 strncpy(name, parse(), sizeof(name));
369 ** Split this list of fields to ignore, and copy
370 ** it to the end of the current "ignores" list.
372 if (!mh_strcasecmp(name, "ignores")) {
376 /* split the fields */
377 tmparray = brkstring(getcpy(++parptr), ",",
380 ** copy pointers to split fields
383 while (tmparray[n] && num_ignores<MAXARGS-1) {
384 ignores[num_ignores++] = tmparray[n++];
386 ignores[num_ignores] = NULL;
391 if (evalvar(&global))
392 adios(NULL, "format file syntax error: %s", bp);
399 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
400 while (*parptr == ':' || *parptr == ',') {
403 adios(NULL, "format file syntax error: %s", bp);
405 if (!c1->c_fstr && global.c_fstr) {
406 if ((c1->c_flags & DATEFMT) &&
407 (global.c_flags & DATEFMT)) {
408 c1->c_fstr = getcpy(global.c_fstr);
409 } else if ((c1->c_flags & ADDRFMT) &&
410 (global.c_flags & ADDRFMT)) {
411 c1->c_fstr = getcpy(global.c_fstr);
417 adios(NULL, "format file syntax error: %s", bp);
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));
433 global.c_width = width;
434 if (global.c_width < 5)
435 global.c_width = 10000;
440 evalvar(struct mcomp *c1)
442 char *cp, name[NAMESZ];
447 strncpy(name, parse(), sizeof(name));
449 if (!mh_strcasecmp(name, "component")) {
450 if (ptos(name, &c1->c_text))
452 c1->c_flags &= ~NOCOMPONENT;
456 if (!mh_strcasecmp(name, "overflowtext"))
457 return ptos(name, &c1->c_ovtxt);
459 if (!mh_strcasecmp(name, "formatfield")) {
464 cp = concat("=", cp, NULL);
465 fmtstr = new_fs(cp, NULL);
467 c1->c_fstr = getcpy(fmtstr);
468 c1->c_flags |= FORMAT;
472 if (!mh_strcasecmp(name, "decode")) {
475 fmtstr = new_fs("=%(decode{text})", NULL);
476 c1->c_fstr = getcpy(fmtstr);
477 c1->c_flags |= FORMAT;
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);
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;
502 ptoi(char *name, int *i)
506 if (*parptr++ != '=' || !*(cp = parse())) {
507 advise(NULL, "missing argument to variable %s", name);
517 ptos(char *name, char **s)
521 if (*parptr++ != '=') {
522 advise(NULL, "missing argument to variable %s", name);
526 if (*parptr != '"') {
527 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
531 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
532 if (*parptr == QUOTE)
539 if ((*parptr = c) == '"')
550 static char result[NAMESZ];
552 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
571 process(char *fname, int ofilen, int ofilec)
576 if (setjmp(env) == 0) {
578 fp = mhl_action ? (*mhl_action) (fname) :
581 advise(fname, "unable to open");
589 SIGNAL(SIGINT, intrser);
590 mhlfile(fp, fname, ofilen, ofilec);
593 SIGNAL(SIGINT, SIG_IGN);
594 if (mhl_action == NULL && fp != stdin)
598 holder.c_text = NULL;
600 free_queue(&msghd, &msgtl);
601 for (c1 = fmthd; c1; c1 = c1->c_next)
602 c1->c_flags &= ~HDROUTPUT;
607 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
610 struct mcomp *c1, *c2, *c3;
611 char **ip, name[NAMESZ], buf[BUFSIZ];
616 printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
618 printf(" Message %d", ofilen);
620 } else if (ofilec > 1) {
624 printf(">>> %s\n\n", mname);
627 for (state = FLD;;) {
628 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
631 for (ip = ignores; *ip; ip++)
632 if (!mh_strcasecmp(name, *ip)) {
633 while (state == FLDPLUS)
634 state = m_getfld(state, name, buf, sizeof(buf), fp);
640 for (c2 = fmthd; c2; c2 = c2->c_next)
641 if (!mh_strcasecmp(c2->c_name, name))
644 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
645 for (c1 = msghd; c1; c1 = c1->c_next)
646 if (!mh_strcasecmp(c1->c_name,
648 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
652 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
653 while (state == FLDPLUS) {
654 state = m_getfld(state, name, buf,
656 c1->c_text = add(buf, c1->c_text);
659 c1->c_flags |= EXTRA;
665 for (c1 = fmthd; c1; c1 = c1->c_next) {
666 if (c1->c_flags & CLEARTEXT) {
667 putcomp(c1, c1, ONECOMP);
670 if (!mh_strcasecmp(c1->c_name, "messagename")) {
671 holder.c_text = concat("(Message ",
673 putcomp(c1, &holder, ONECOMP);
675 holder.c_text = NULL;
678 if (!mh_strcasecmp(c1->c_name, "extras")) {
679 for (c2 = msghd; c2; c2 = c2->c_next)
680 if (c2->c_flags & EXTRA)
681 putcomp(c1, c2, TWOCOMP);
684 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
685 holder.c_text = mh_xmalloc(sizeof(buf));
686 strncpy(holder.c_text, buf, sizeof(buf));
687 while (state == BODY) {
688 putcomp(c1, &holder, BODYCOMP);
689 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
692 holder.c_text = NULL;
695 for (c2 = msghd; c2; c2 = c2->c_next)
696 if (!mh_strcasecmp(c2->c_name,
698 putcomp(c1, c2, ONECOMP);
699 if (!(c1->c_flags & SPLIT))
707 advise(NULL, "format error in message %s", mname);
712 adios(NULL, "getfld() returned %d", state);
719 mcomp_flags(char *name)
723 for (ap = pairs; ap->p_name; ap++)
724 if (!mh_strcasecmp(ap->p_name, name))
725 return (ap->p_flags);
732 mcomp_add(long flags, char *s1, char *s2)
736 if (!(flags & ADDRFMT))
739 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
742 return add(s1, add(",\n", s2));
749 struct pqpair *pq_next;
754 mcomp_format(struct mcomp *c1, struct mcomp *c2)
758 char buffer[BUFSIZ], error[BUFSIZ];
760 struct pqpair *p, *q;
769 dat[3] = sizeof(buffer) - 1;
771 fmt_compile(c1->c_fstr, &c1->c_fmt);
773 if (!(c1->c_flags & ADDRFMT)) {
774 FINDCOMP(cptr, "text");
777 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
781 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
782 /* Don't need to append a newline, dctime() already did */
783 c2->c_text = getcpy(buffer);
789 (q = &pq)->pq_next = NULL;
790 while ((cp = getname(ap))) {
791 if ((p = (struct pqpair *)
792 calloc((size_t) 1, sizeof(*p))) == NULL)
793 adios(NULL, "unable to allocate pqpair memory");
795 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
796 p->pq_text = getcpy(cp);
797 p->pq_error = getcpy(error);
799 p->pq_text = getcpy(mp->m_text);
802 q = (q->pq_next = p);
805 for (p = pq.pq_next; p; p = q) {
806 FINDCOMP(cptr, "text");
808 cptr->c_text = p->pq_text;
809 FINDCOMP(cptr, "error");
811 cptr->c_text = p->pq_error;
813 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
816 c2->c_text = add(",\n", c2->c_text);
817 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
819 c2->c_text = add(buffer, c2->c_text);
829 c2->c_text = add("\n", c2->c_text);
834 static struct mcomp *
835 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
836 char *text, int flags)
840 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
841 adios(NULL, "unable to allocate comp memory");
843 c1->c_flags = flags & ~INIT;
844 if ((c1->c_name = name ? getcpy(name) : NULL))
845 c1->c_flags |= mcomp_flags(c1->c_name);
846 c1->c_text = text ? getcpy(text) : NULL;
849 c1->c_ovtxt = getcpy(global.c_ovtxt);
850 c1->c_offset = global.c_offset;
851 c1->c_ovoff = global. c_ovoff;
853 c1->c_cwidth = global.c_cwidth;
854 c1->c_flags |= global.c_flags & GFLAGS;
859 (*tail)->c_next = c1;
867 free_queue(struct mcomp **head, struct mcomp **tail)
869 struct mcomp *c1, *c2;
871 for (c1 = *head; c1; c1 = c2) {
882 free((char *) c1->c_fmt);
886 *head = *tail = NULL;
891 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
898 wid = c1->c_width ? c1->c_width : global.c_width;
899 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
901 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
903 if (wid < ovoff + strlen(ovtxt) + 5)
904 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
907 if (c1->c_flags & CLEARTEXT) {
913 if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
914 mcomp_format(c1, c2);
916 if (c1->c_flags & CENTER) {
917 count = (c1->c_width ? c1->c_width : global.c_width)
918 - c1->c_offset - strlen(c2->c_text);
919 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
920 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
922 lm = c1->c_offset + (count / 2);
928 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
929 if (c1->c_flags & UPPERCASE) /* uppercase component also */
930 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
933 putstr(c1->c_text ? c1->c_text : c1->c_name);
934 if (flag != BODYCOMP) {
936 if (!(c1->c_flags & SPLIT))
937 c1->c_flags |= HDROUTPUT;
940 if ((count = c1->c_cwidth -
941 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
945 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
948 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
949 && !(c2->c_flags & NOCOMPONENT)) {
950 if (c1->c_flags & UPPERCASE)
951 for (cp = c2->c_name; *cp; cp++)
956 if (!(c1->c_flags & SPLIT))
957 c2->c_flags |= HDROUTPUT;
960 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
964 if (c1->c_flags & UPPERCASE)
965 for (cp = c2->c_text; *cp; cp++)
972 count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
973 (int)strlen(c2->c_name) + 2;
975 count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
977 c1->c_text : c1->c_name) + 2;
979 count += c1->c_offset;
981 if ((cp = oneline(c2->c_text, c1->c_flags)))
985 while ((cp = oneline(c2->c_text, c1->c_flags))) {
987 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
988 putstr(c1->c_text ? c1->c_text : c1->c_name);
994 if (flag == BODYCOMP && term == '\n')
995 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1000 oneline(char *stuff, long flags)
1008 return (onelp = NULL);
1012 if (flags & COMPRESS) {
1013 for (spc = 1, cp = ret; *onelp; onelp++)
1014 if (isspace(*onelp)) {
1015 if (*onelp == '\n' &&
1017 (flags & ADDRFMT))) {
1032 while (*onelp && *onelp != '\n')
1034 if (*onelp == '\n') {
1038 if (flags & LEFTADJUST)
1039 while (*ret == ' ' || *ret == '\t')
1042 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1050 putstr(char *string)
1052 if (!column && lm > 0) {
1088 ** If we are forwarding this message, and the first
1089 ** column contains a dash, then add a dash and a space.
1091 if (column == 0 && forwflg && ch == '-') {
1100 if (column >= wid) {
1104 putstr(ovtxt ? ovtxt : "");
1126 mhladios(char *what, char *fmt, ...)
1131 advertise(what, NULL, fmt, ap);
1142 longjmp(mhlenv, DONE);