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>
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
30 ** (*onelp == '\n' && !onelp[1])
32 ** being a terminating condition,
34 ** (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
36 ** is used instead. This cuts the line prematurely, and gives us a much
37 ** better chance of getting things right.
46 static struct swit switches[] = {
48 { "form formfile", 0 },
50 { "width columns", 0 },
64 char *version=VERSION;
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)
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 */
98 static struct mcomp *msghd = NULL;
99 static struct mcomp *msgtl = NULL;
100 static struct mcomp *fmthd = NULL;
101 static struct mcomp *fmttl = NULL;
103 static struct mcomp global = {
104 NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 0, NULL
107 static struct mcomp holder = {
108 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NOCOMPONENT, NULL
116 static struct pair pairs[] = {
119 { "Sender", ADDRFMT },
120 { "Reply-To", 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 },
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 },
162 static int dobody = 1;
163 static int forwflg = 0;
164 static int forwall = 0;
166 static int exitstat = 0;
167 static int mhldebug = 0;
169 static unsigned int column;
174 static unsigned int wid;
178 static unsigned char *onelp;
182 static int num_ignores = 0;
183 static char *ignores[MAXARGS];
185 volatile sig_atomic_t eflag = 0;
188 ** Redefine a couple of functions.
189 ** These are undefined later in the code.
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);
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(EX_USAGE, NULL, "-%s unknown\n", cp);
247 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
248 print_help(buf, switches, 1);
249 exit(argc == 2 ? EX_OK : EX_USAGE);
251 print_version(invo_name);
252 exit(argc == 2 ? EX_OK : EX_USAGE);
255 if (!(form = *argp++) || *form == '-')
256 adios(EX_USAGE, NULL, "missing argument to %s",
261 if (!(cp = *argp++) || *cp == '-')
262 adios(EX_USAGE, NULL, "missing argument to %s",
264 if ((width = atoi(cp)) < 1)
265 adios(EX_USAGE, 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(EX_IOERR, "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(EX_IOERR, 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(mh_xstrdup(++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(EX_CONFIG, NULL, "format file syntax error: %s", bp);
399 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
400 while (*parptr == ':' || *parptr == ',') {
403 adios(EX_CONFIG, 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 = 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);
417 adios(EX_CONFIG, 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 = mh_xstrdup(fmtstr);
468 c1->c_flags |= FORMAT;
472 if (!mh_strcasecmp(name, "decode")) {
475 fmtstr = new_fs("=%(decode{text})", NULL);
476 c1->c_fstr = mh_xstrdup(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)
577 fp = fopen(fname, "r");
579 advise(fname, "unable to open");
587 SIGNAL(SIGINT, intrser);
588 mhlfile(fp, fname, ofilen, ofilec);
590 SIGNAL(SIGINT, SIG_IGN);
594 mh_free0(&(holder.c_text));
596 free_queue(&msghd, &msgtl);
597 for (c1 = fmthd; c1; c1 = c1->c_next)
598 c1->c_flags &= ~HDROUTPUT;
603 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
606 struct field f = {{0}};
607 struct mcomp *c1, *c2, *c3;
613 printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
615 printf(" Message %d", ofilen);
618 } else if (ofilec > 1) {
622 printf(">>> %s\n\n", mname);
625 for (state = FLD2; !eflag; ) {
626 switch (state = m_getfld2(state, &f, fp)) {
628 for (ip = ignores; *ip; ip++)
629 if (mh_strcasecmp(f.name, *ip)==0) {
636 for (c2 = fmthd; c2; c2 = c2->c_next)
637 if (mh_strcasecmp(c2->c_name, f.name)==0) {
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,
645 c1->c_text = mcomp_add(c1->c_flags, f.value, c1->c_text);
649 c1 = add_queue(&msghd, &msgtl, f.name, f.value, 0);
652 c1->c_flags |= EXTRA;
659 for (c1 = fmthd; c1; c1 = c1->c_next) {
660 if (c1->c_flags & CLEARTEXT) {
661 putcomp(c1, c1, ONECOMP);
664 if (mh_strcasecmp(c1->c_name, "messagename")==0) {
665 holder.c_text = concat("(Message ",
667 putcomp(c1, &holder, ONECOMP);
668 mh_free0(&(holder.c_text));
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);
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);
685 holder.c_text = mh_xstrdup(f.value);
687 mh_free0(&(holder.c_text));
690 for (c2 = msghd; c2; c2 = c2->c_next) {
691 if (mh_strcasecmp(c2->c_name,
693 putcomp(c1, c2, ONECOMP);
694 if (!(c1->c_flags & SPLIT)) {
705 advise(NULL, "format error in message %s", mname);
710 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
717 mcomp_flags(char *name)
721 for (ap = pairs; ap->p_name; ap++)
722 if (!mh_strcasecmp(ap->p_name, name))
723 return (ap->p_flags);
730 mcomp_add(long flags, char *s1, char *s2)
734 if (!(flags & ADDRFMT))
737 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
740 return add(s1, add(",\n", s2));
747 struct pqpair *pq_next;
752 mcomp_format(struct mcomp *c1, struct mcomp *c2)
756 char buffer[BUFSIZ], error[BUFSIZ];
758 struct pqpair *p, *q;
767 dat[3] = sizeof(buffer) - 1;
769 fmt_compile(c1->c_fstr, &c1->c_fmt);
771 if (!(c1->c_flags & ADDRFMT)) {
772 FINDCOMP(cptr, "text");
775 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
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);
787 (q = &pq)->pq_next = NULL;
788 while ((cp = getname(ap))) {
789 p = mh_xcalloc(1, sizeof(*p));
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);
795 p->pq_text = mh_xstrdup(mp->m_text);
798 q = (q->pq_next = p);
801 for (p = pq.pq_next; p; p = q) {
802 FINDCOMP(cptr, "text");
804 cptr->c_text = p->pq_text;
805 FINDCOMP(cptr, "error");
807 cptr->c_text = p->pq_error;
809 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
812 c2->c_text = add(",\n", c2->c_text);
813 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
815 c2->c_text = add(buffer, c2->c_text);
818 mh_free0(&(p->pq_text));
820 mh_free0(&(p->pq_error));
825 c2->c_text = add("\n", c2->c_text);
830 static struct mcomp *
831 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
832 char *text, int flags)
836 c1 = mh_xcalloc(1, sizeof(*c1));
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;
844 c1->c_ovtxt = mh_xstrdup(global.c_ovtxt);
845 c1->c_offset = global.c_offset;
846 c1->c_ovoff = global. c_ovoff;
848 c1->c_cwidth = global.c_cwidth;
849 c1->c_flags |= global.c_flags & GFLAGS;
854 (*tail)->c_next = c1;
862 free_queue(struct mcomp **head, struct mcomp **tail)
864 struct mcomp *c1, *c2;
866 for (c1 = *head; c1; c1 = c2) {
869 mh_free0(&(c1->c_name));
871 mh_free0(&(c1->c_text));
873 mh_free0(&(c1->c_ovtxt));
875 mh_free0(&(c1->c_fstr));
877 mh_free0(&(c1->c_fmt));
881 *head = *tail = NULL;
886 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
890 char trimmed_prefix[BUFSIZ];
892 strncpy(trimmed_prefix, c1->c_text ? c1->c_text : c1->c_name, sizeof(trimmed_prefix) - 1);
893 rtrim(trimmed_prefix);
896 wid = c1->c_width ? c1->c_width : global.c_width;
897 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
899 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
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);
905 if (c1->c_flags & CLEARTEXT) {
906 putstr((c1->c_flags & RTRIM) ? rtrim(c1->c_text) : c1->c_text);
911 if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
912 mcomp_format(c1, c2);
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)
920 lm = c1->c_offset + (count / 2);
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++)
931 if (*c2->c_text && *c2->c_text != '\n' && *c2->c_text != '\r') {
932 putstr(c1->c_text ? c1->c_text : c1->c_name);
934 putstr(trimmed_prefix);
936 if (flag != BODYCOMP) {
938 if (!(c1->c_flags & SPLIT))
939 c1->c_flags |= HDROUTPUT;
942 if ((count = c1->c_cwidth -
943 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
947 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
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++)
958 if (!(c1->c_flags & SPLIT))
959 c2->c_flags |= HDROUTPUT;
962 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
966 if (c1->c_flags & UPPERCASE)
967 for (cp = c2->c_text; *cp; cp++)
974 count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
975 (int)strlen(c2->c_name) + 2;
977 count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
979 c1->c_text : c1->c_name) + 2;
981 count += c1->c_offset;
983 if ((cp = oneline(c2->c_text, c1->c_flags))) {
984 putstr((c1->c_flags & RTRIM) ? rtrim(cp) : cp);
988 while ((cp = oneline(c2->c_text, c1->c_flags))) {
990 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT)) {
992 putstr(c1->c_text ? c1->c_text : c1->c_name);
994 putstr(trimmed_prefix);
998 putstr((c1->c_flags & RTRIM) ? rtrim(cp) : cp);
1003 if (flag == BODYCOMP && term == '\n')
1004 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1009 oneline(char *stuff, long flags)
1017 return (onelp = NULL);
1021 if (flags & COMPRESS) {
1022 for (spc = 1, cp = ret; *onelp; onelp++)
1023 if (isspace(*onelp)) {
1024 if (*onelp == '\n' &&
1026 (flags & ADDRFMT))) {
1041 while (*onelp && *onelp != '\n')
1043 if (*onelp == '\n') {
1047 if (flags & LEFTADJUST)
1048 while (*ret == ' ' || *ret == '\t')
1051 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1059 putstr(char *string)
1061 if (!column && lm > 0) {
1097 ** If we are forwarding this message, and the first
1098 ** column contains a dash, then add a dash and a space.
1100 if (column == 0 && forwflg && ch == '-') {
1109 if (column >= wid) {
1113 putstr(ovtxt ? ovtxt : "");