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 LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07LEFTADJUST\010COMPRESS\011ADDRFMT\012DATEFMT\013FORMAT\014INIT\015SPLIT\016NONEWLINE"
81 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
84 char *c_name; /* component name */
85 char *c_text; /* component text */
86 char *c_ovtxt; /* text overflow indicator */
87 char *c_fstr; /* iff FORMAT */
88 struct format *c_fmt; /* .. */
89 int c_offset; /* left margin indentation */
90 int c_ovoff; /* overflow indentation */
91 int c_width; /* width of field */
92 int c_cwidth; /* width of component */
97 static struct mcomp *msghd = NULL;
98 static struct mcomp *msgtl = NULL;
99 static struct mcomp *fmthd = NULL;
100 static struct mcomp *fmttl = NULL;
102 static struct mcomp global = {
103 NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 0, NULL
106 static struct mcomp holder = {
107 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, NOCOMPONENT, NULL
115 static struct pair pairs[] = {
118 { "Sender", ADDRFMT },
119 { "Reply-To", ADDRFMT },
123 { "Resent-Date", DATEFMT },
124 { "Resent-From", ADDRFMT },
125 { "Resent-Sender", ADDRFMT },
126 { "Resent-Reply-To", ADDRFMT },
127 { "Resent-To", ADDRFMT },
128 { "Resent-Cc", ADDRFMT },
129 { "Resent-Bcc", ADDRFMT },
139 static struct triple triples[] = {
140 { "nocomponent", NOCOMPONENT, 0 },
141 { "uppercase", UPPERCASE, 0 },
142 { "nouppercase", 0, UPPERCASE },
143 { "center", CENTER, 0 },
144 { "nocenter", 0, CENTER },
145 { "leftadjust", LEFTADJUST, 0 },
146 { "noleftadjust", 0, LEFTADJUST },
147 { "compress", COMPRESS, 0 },
148 { "nocompress", 0, COMPRESS },
149 { "split", SPLIT, 0 },
150 { "nosplit", 0, SPLIT },
151 { "addrfield", ADDRFMT, DATEFMT },
152 { "datefield", DATEFMT, ADDRFMT },
153 { "newline", 0, NONEWLINE },
154 { "nonewline", NONEWLINE, 0 },
159 static int dobody = 1;
160 static int forwflg = 0;
161 static int forwall = 0;
163 static int exitstat = 0;
164 static int mhldebug = 0;
166 static unsigned int column;
171 static unsigned int wid;
175 static unsigned char *onelp;
179 static int num_ignores = 0;
180 static char *ignores[MAXARGS];
182 volatile sig_atomic_t eflag = 0;
185 ** Redefine a couple of functions.
186 ** These are undefined later in the code.
192 static void mhl_format(char *, int);
193 static int evalvar(struct mcomp *);
194 static int ptoi(char *, int *);
195 static int ptos(char *, char **);
196 static char *parse(void);
197 static void process(char *, int, int);
198 static void mhlfile(FILE *, char *, int, int);
199 static int mcomp_flags(char *);
200 static char *mcomp_add(long, char *, char *);
201 static void mcomp_format(struct mcomp *, struct mcomp *);
202 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
203 char *, char *, int);
204 static void free_queue(struct mcomp **, struct mcomp **);
205 static void putcomp(struct mcomp *, struct mcomp *, int);
206 static char *oneline(char *, long);
207 static void putstr(char *);
208 static void putch(char);
209 static void intrser(int);
211 int sc_width(void); /* from termsbr.c */
215 main(int argc, char **argv)
217 int i, width = 0, vecp = 0;
218 char *cp, *form = NULL;
219 char buf[BUFSIZ], *files[MAXARGS];
220 char **argp, **arguments;
222 setlocale(LC_ALL, "");
223 invo_name = mhbasename(argv[0]);
225 /* read user profile/context */
228 arguments = getarguments(invo_name, argc, argv, 1);
231 if ((cp = getenv("MHLDEBUG")) && *cp)
234 while ((cp = *argp++)) {
236 switch (smatch(++cp, switches)) {
238 ambigsw(cp, switches);
241 adios(EX_USAGE, NULL, "-%s unknown\n", cp);
244 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
245 print_help(buf, switches, 1);
246 exit(argc == 2 ? EX_OK : EX_USAGE);
248 print_version(invo_name);
249 exit(argc == 2 ? EX_OK : EX_USAGE);
252 if (!(form = *argp++) || *form == '-')
253 adios(EX_USAGE, NULL, "missing argument to %s",
258 if (!(cp = *argp++) || *cp == '-')
259 adios(EX_USAGE, NULL, "missing argument to %s",
261 if ((width = atoi(cp)) < 1)
262 adios(EX_USAGE, NULL, "bad argument %s %s",
267 forwall++; /* fall */
280 mhl_format(form ? form : mhlformat, width);
283 process(NULL, 1, vecp = 1);
285 for (i = 0; i < vecp; i++)
286 process(files[i], i + 1, vecp);
290 printf("\n------- End of Forwarded Message%s\n\n",
291 vecp > 1 ? "s" : "");
295 if (ferror(stdout)) {
296 adios(EX_IOERR, "output", "error writing");
304 mhl_format(char *file, int width)
308 char *ap, buffer[BUFSIZ], name[NAMESZ];
312 static dev_t dev = 0;
313 static ino_t ino = 0;
314 static time_t mtime = 0;
317 if (stat(etcpath(file), &st) != NOTOK
318 && mtime == st.st_mtime
323 free_queue(&fmthd, &fmttl);
326 if ((fp = fopen(etcpath(file), "r")) == NULL)
327 adios(EX_IOERR, file, "unable to open format file");
329 if (fstat(fileno(fp), &st) != NOTOK) {
335 global.c_ovtxt = global.c_fstr = NULL;
339 if ((i = sc_width()) > 5)
341 global.c_cwidth = -1;
345 while (vfgets(fp, &ap) == OK) {
350 if ((cp = strchr(bp, '\n')))
354 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
360 strncpy(name, parse(), sizeof(name));
366 ** Split this list of fields to ignore, and copy
367 ** it to the end of the current "ignores" list.
369 if (!mh_strcasecmp(name, "ignores")) {
373 /* split the fields */
374 tmparray = brkstring(mh_xstrdup(++parptr), ",",
377 ** copy pointers to split fields
380 while (tmparray[n] && num_ignores<MAXARGS-1) {
381 ignores[num_ignores++] = tmparray[n++];
383 ignores[num_ignores] = NULL;
388 if (evalvar(&global))
389 adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
396 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
397 while (*parptr == ':' || *parptr == ',') {
400 adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
402 if (!c1->c_fstr && global.c_fstr) {
403 if ((c1->c_flags & DATEFMT) &&
404 (global.c_flags & DATEFMT)) {
405 c1->c_fstr = mh_xstrdup(global.c_fstr);
406 } else if ((c1->c_flags & ADDRFMT) &&
407 (global.c_flags & ADDRFMT)) {
408 c1->c_fstr = mh_xstrdup(global.c_fstr);
414 adios(EX_CONFIG, NULL, "format file syntax error: %s", bp);
420 for (c1 = fmthd; c1; c1 = c1->c_next) {
421 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
422 fprintf(stderr, "\tfstr=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_fstr, (unsigned int)(unsigned long) c1->c_fmt);
423 fprintf(stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d\n", c1->c_offset, c1->c_ovoff, c1->c_width, c1->c_cwidth);
424 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
430 global.c_width = width;
431 if (global.c_width < 5)
432 global.c_width = 10000;
437 evalvar(struct mcomp *c1)
439 char *cp, name[NAMESZ];
444 strncpy(name, parse(), sizeof(name));
446 if (!mh_strcasecmp(name, "component")) {
447 if (ptos(name, &c1->c_text))
449 c1->c_flags &= ~NOCOMPONENT;
453 if (!mh_strcasecmp(name, "overflowtext"))
454 return ptos(name, &c1->c_ovtxt);
456 if (!mh_strcasecmp(name, "formatfield")) {
461 cp = concat("=", cp, NULL);
462 fmtstr = new_fs(cp, NULL);
464 c1->c_fstr = mh_xstrdup(fmtstr);
465 c1->c_flags |= FORMAT;
469 if (!mh_strcasecmp(name, "decode")) {
472 fmtstr = new_fs("=%(decode{text})", NULL);
473 c1->c_fstr = mh_xstrdup(fmtstr);
474 c1->c_flags |= FORMAT;
478 if (!mh_strcasecmp(name, "offset"))
479 return ptoi(name, &c1->c_offset);
480 if (!mh_strcasecmp(name, "overflowoffset"))
481 return ptoi(name, &c1->c_ovoff);
482 if (!mh_strcasecmp(name, "width"))
483 return ptoi(name, &c1->c_width);
484 if (!mh_strcasecmp(name, "compwidth"))
485 return ptoi(name, &c1->c_cwidth);
487 for (ap = triples; ap->t_name; ap++)
488 if (!mh_strcasecmp(ap->t_name, name)) {
489 c1->c_flags |= ap->t_on;
490 c1->c_flags &= ~ap->t_off;
499 ptoi(char *name, int *i)
503 if (*parptr++ != '=' || !*(cp = parse())) {
504 advise(NULL, "missing argument to variable %s", name);
514 ptos(char *name, char **s)
518 if (*parptr++ != '=') {
519 advise(NULL, "missing argument to variable %s", name);
523 if (*parptr != '"') {
524 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
528 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
529 if (*parptr == QUOTE)
536 if ((*parptr = c) == '"')
547 static char result[NAMESZ];
549 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
568 process(char *fname, int ofilen, int ofilec)
574 fp = fopen(fname, "r");
576 advise(fname, "unable to open");
584 SIGNAL(SIGINT, intrser);
585 mhlfile(fp, fname, ofilen, ofilec);
587 SIGNAL(SIGINT, SIG_IGN);
591 mh_free0(&(holder.c_text));
593 free_queue(&msghd, &msgtl);
594 for (c1 = fmthd; c1; c1 = c1->c_next)
595 c1->c_flags &= ~HDROUTPUT;
600 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
603 struct field f = {{0}};
604 struct mcomp *c1, *c2, *c3;
610 printf(" Forwarded Message%s", ofilec > 1 ? "s" : "");
612 printf(" Message %d", ofilen);
615 } else if (ofilec > 1) {
619 printf(">>> %s\n\n", mname);
622 for (state = FLD2; !eflag; ) {
623 switch (state = m_getfld2(state, &f, fp)) {
625 for (ip = ignores; *ip; ip++)
626 if (mh_strcasecmp(f.name, *ip)==0) {
633 for (c2 = fmthd; c2; c2 = c2->c_next)
634 if (mh_strcasecmp(c2->c_name, f.name)==0) {
638 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
639 for (c1 = msghd; c1; c1 = c1->c_next)
640 if (mh_strcasecmp(c1->c_name,
642 c1->c_text = mcomp_add(c1->c_flags, f.value, c1->c_text);
646 c1 = add_queue(&msghd, &msgtl, f.name, f.value, 0);
649 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")==0) {
662 holder.c_text = concat("(Message ",
664 putcomp(c1, &holder, ONECOMP);
665 mh_free0(&(holder.c_text));
668 if (mh_strcasecmp(c1->c_name, "extras")==0) {
669 for (c2 = msghd; c2; c2 = c2->c_next) {
670 if (c2->c_flags & EXTRA) {
671 putcomp(c1, c2, TWOCOMP);
676 if (dobody && mh_strcasecmp(c1->c_name, "body")==0) {
677 holder.c_text = mh_xstrdup(f.value);
678 while (state == BODY2) {
679 putcomp(c1, &holder, BODYCOMP);
680 state = m_getfld2(state, &f, fp);
682 holder.c_text = mh_xstrdup(f.value);
684 mh_free0(&(holder.c_text));
687 for (c2 = msghd; c2; c2 = c2->c_next) {
688 if (mh_strcasecmp(c2->c_name,
690 putcomp(c1, c2, ONECOMP);
691 if (!(c1->c_flags & SPLIT)) {
702 advise(NULL, "format error in message %s", mname);
707 adios(EX_SOFTWARE, NULL, "getfld() returned %d", state);
714 mcomp_flags(char *name)
718 for (ap = pairs; ap->p_name; ap++)
719 if (!mh_strcasecmp(ap->p_name, name))
720 return (ap->p_flags);
727 mcomp_add(long flags, char *s1, char *s2)
731 if (!(flags & ADDRFMT))
734 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
737 return add(s1, add(",\n", s2));
744 struct pqpair *pq_next;
749 mcomp_format(struct mcomp *c1, struct mcomp *c2)
753 char buffer[BUFSIZ], error[BUFSIZ];
755 struct pqpair *p, *q;
764 dat[3] = sizeof(buffer) - 1;
766 fmt_compile(c1->c_fstr, &c1->c_fmt);
768 if (!(c1->c_flags & ADDRFMT)) {
769 FINDCOMP(cptr, "text");
772 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
776 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
777 /* Don't need to append a newline, dctime() already did */
778 c2->c_text = mh_xstrdup(buffer);
784 (q = &pq)->pq_next = NULL;
785 while ((cp = getname(ap))) {
786 p = mh_xcalloc(1, sizeof(*p));
788 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
789 p->pq_text = mh_xstrdup(cp);
790 p->pq_error = mh_xstrdup(error);
792 p->pq_text = mh_xstrdup(mp->m_text);
795 q = (q->pq_next = p);
798 for (p = pq.pq_next; p; p = q) {
799 FINDCOMP(cptr, "text");
801 cptr->c_text = p->pq_text;
802 FINDCOMP(cptr, "error");
804 cptr->c_text = p->pq_error;
806 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
809 c2->c_text = add(",\n", c2->c_text);
810 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
812 c2->c_text = add(buffer, c2->c_text);
815 mh_free0(&(p->pq_text));
817 mh_free0(&(p->pq_error));
822 c2->c_text = add("\n", c2->c_text);
827 static struct mcomp *
828 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
829 char *text, int flags)
833 c1 = mh_xcalloc(1, sizeof(*c1));
835 c1->c_flags = flags & ~INIT;
836 if ((c1->c_name = name ? mh_xstrdup(name) : NULL))
837 c1->c_flags |= mcomp_flags(c1->c_name);
838 c1->c_text = text ? mh_xstrdup(text) : NULL;
841 c1->c_ovtxt = mh_xstrdup(global.c_ovtxt);
842 c1->c_offset = global.c_offset;
843 c1->c_ovoff = global. c_ovoff;
845 c1->c_cwidth = global.c_cwidth;
846 c1->c_flags |= global.c_flags & GFLAGS;
851 (*tail)->c_next = c1;
859 free_queue(struct mcomp **head, struct mcomp **tail)
861 struct mcomp *c1, *c2;
863 for (c1 = *head; c1; c1 = c2) {
866 mh_free0(&(c1->c_name));
868 mh_free0(&(c1->c_text));
870 mh_free0(&(c1->c_ovtxt));
872 mh_free0(&(c1->c_fstr));
874 mh_free0(&(c1->c_fmt));
878 *head = *tail = NULL;
883 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
890 wid = c1->c_width ? c1->c_width : global.c_width;
891 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
893 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
895 if (wid < ovoff + strlen(ovtxt) + 5)
896 adios(EX_SOFTWARE, NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
899 if (c1->c_flags & CLEARTEXT) {
905 if (c1->c_fstr && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
906 mcomp_format(c1, c2);
908 if (c1->c_flags & CENTER) {
909 count = (c1->c_width ? c1->c_width : global.c_width)
910 - c1->c_offset - strlen(c2->c_text);
911 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
912 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
914 lm = c1->c_offset + (count / 2);
920 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
921 if (c1->c_flags & UPPERCASE) /* uppercase component also */
922 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
925 putstr(c1->c_text ? c1->c_text : c1->c_name);
926 if (flag != BODYCOMP) {
928 if (!(c1->c_flags & SPLIT))
929 c1->c_flags |= HDROUTPUT;
932 if ((count = c1->c_cwidth -
933 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
937 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
940 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
941 && !(c2->c_flags & NOCOMPONENT)) {
942 if (c1->c_flags & UPPERCASE)
943 for (cp = c2->c_name; *cp; cp++)
948 if (!(c1->c_flags & SPLIT))
949 c2->c_flags |= HDROUTPUT;
952 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
956 if (c1->c_flags & UPPERCASE)
957 for (cp = c2->c_text; *cp; cp++)
964 count = (c1->c_cwidth >= 0) ? c1->c_cwidth :
965 (int)strlen(c2->c_name) + 2;
967 count = (c1->c_cwidth >= 0) ? (size_t)c1->c_cwidth :
969 c1->c_text : c1->c_name) + 2;
971 count += c1->c_offset;
973 if ((cp = oneline(c2->c_text, c1->c_flags)))
977 while ((cp = oneline(c2->c_text, c1->c_flags))) {
979 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
980 putstr(c1->c_text ? c1->c_text : c1->c_name);
986 if (flag == BODYCOMP && term == '\n')
987 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
992 oneline(char *stuff, long flags)
1000 return (onelp = NULL);
1004 if (flags & COMPRESS) {
1005 for (spc = 1, cp = ret; *onelp; onelp++)
1006 if (isspace(*onelp)) {
1007 if (*onelp == '\n' &&
1009 (flags & ADDRFMT))) {
1024 while (*onelp && *onelp != '\n')
1026 if (*onelp == '\n') {
1030 if (flags & LEFTADJUST)
1031 while (*ret == ' ' || *ret == '\t')
1034 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1042 putstr(char *string)
1044 if (!column && lm > 0) {
1080 ** If we are forwarding this message, and the first
1081 ** column contains a dash, then add a dash and a space.
1083 if (column == 0 && forwflg && ch == '-') {
1092 if (column >= wid) {
1096 putstr(ovtxt ? ovtxt : "");