3 * mhlsbr.c -- main routines for nmh message lister
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
16 #include <h/m_setjmp.h>
20 #include <sys/types.h>
24 * for a component containing addresses, ADDRFMT, if COMPRESS is also
25 * set, then addresses get split wrong (not at the spaces between commas).
26 * To fix this correctly, putstr() should know about "atomic" strings that
27 * must NOT be broken across lines. That's too difficult for right now
28 * (it turns out that there are a number of degernate cases), so in
29 * oneline(), instead of
31 * (*onelp == '\n' && !onelp[1])
33 * being a terminating condition,
35 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
37 * is used instead. This cuts the line prematurely, and gives us a much
38 * better chance of getting things right.
47 static struct swit mhlswitches[] = {
57 { "faceproc program", 0 },
61 { "folder +folder", 0 },
63 { "form formfile", 0 },
65 { "moreproc program", 0 },
69 { "length lines", 0 },
71 { "width columns", 0 },
73 { "sleep seconds", 0 },
75 { "dashstuffing", -12 }, /* interface from forw */
76 #define NBITSTUFFSW 14
77 { "nodashstuffing", -14 }, /* interface from forw */
83 { "forward", -7 }, /* interface from forw */
85 { "forwall", -7 }, /* interface from forw */
87 { "digest list", -6 },
89 { "volume number", -6 },
91 { "issue number", -5 },
95 { "fmtproc program", 0 },
101 #define NOCOMPONENT 0x000001 /* don't show component name */
102 #define UPPERCASE 0x000002 /* display in all upper case */
103 #define CENTER 0x000004 /* center line */
104 #define CLEARTEXT 0x000008 /* cleartext */
105 #define EXTRA 0x000010 /* an "extra" component */
106 #define HDROUTPUT 0x000020 /* already output */
107 #define CLEARSCR 0x000040 /* clear screen */
108 #define LEFTADJUST 0x000080 /* left justify multiple lines */
109 #define COMPRESS 0x000100 /* compress text */
110 #define ADDRFMT 0x000200 /* contains addresses */
111 #define BELL 0x000400 /* sound bell at EOP */
112 #define DATEFMT 0x000800 /* contains dates */
113 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
114 #define INIT 0x002000 /* initialize component */
115 #define FACEFMT 0x004000 /* contains face */
116 #define FACEDFLT 0x008000 /* default for face */
117 #define SPLIT 0x010000 /* split headers (don't concatenate) */
118 #define NONEWLINE 0x020000 /* don't write trailing newline */
119 #define NOWRAP 0x040000 /* Don't wrap lines ever */
120 #define FMTFILTER 0x080000 /* Filter through format filter */
121 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
122 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
125 * A list of format arguments
129 struct format *a_fmt;
131 struct arglist *a_next;
135 char *c_name; /* component name */
136 char *c_text; /* component text */
137 char *c_ovtxt; /* text overflow indicator */
138 char *c_nfs; /* iff FORMAT */
139 struct format *c_fmt; /* .. */
140 char *c_face; /* face designator */
141 int c_offset; /* left margin indentation */
142 int c_ovoff; /* overflow indentation */
143 int c_width; /* width of field */
144 int c_cwidth; /* width of component */
145 int c_length; /* length in lines */
147 struct arglist *c_f_args; /* Argument list for filter*/
148 struct arglist *c_f_tail; /* Pointer to tail of list */
149 int c_nargs; /* Number of arguments */
150 struct mcomp *c_next;
153 static struct mcomp *msghd = NULL;
154 static struct mcomp *msgtl = NULL;
155 static struct mcomp *fmthd = NULL;
156 static struct mcomp *fmttl = NULL;
158 static struct mcomp global = {
159 NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL
162 static struct mcomp holder = {
163 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL
171 static struct pair pairs[] = {
173 { "From", ADDRFMT|FACEDFLT },
174 { "Sender", ADDRFMT },
175 { "Reply-To", ADDRFMT },
179 { "Resent-Date", DATEFMT },
180 { "Resent-From", ADDRFMT },
181 { "Resent-Sender", ADDRFMT },
182 { "Resent-Reply-To", ADDRFMT },
183 { "Resent-To", ADDRFMT },
184 { "Resent-cc", ADDRFMT },
185 { "Resent-Bcc", ADDRFMT },
196 static struct triple triples[] = {
197 { "nocomponent", NOCOMPONENT, 0 },
198 { "uppercase", UPPERCASE, 0 },
199 { "nouppercase", 0, UPPERCASE },
200 { "center", CENTER, 0 },
201 { "nocenter", 0, CENTER },
202 { "clearscreen", CLEARSCR, 0 },
203 { "noclearscreen", 0, CLEARSCR },
204 { "noclear", 0, CLEARSCR },
205 { "leftadjust", LEFTADJUST, 0 },
206 { "noleftadjust", 0, LEFTADJUST },
207 { "compress", COMPRESS, 0 },
208 { "nocompress", 0, COMPRESS },
209 { "split", SPLIT, 0 },
210 { "nosplit", 0, SPLIT },
211 { "addrfield", ADDRFMT, DATEFMT },
213 { "nobell", 0, BELL },
214 { "datefield", DATEFMT, ADDRFMT },
215 { "newline", 0, NONEWLINE },
216 { "nonewline", NONEWLINE, 0 },
217 { "wrap", 0, NOWRAP },
218 { "nowrap", NOWRAP, 0 },
219 { "format", FMTFILTER, 0 },
220 { "noformat", 0, FMTFILTER },
225 static int bellflg = 0;
226 static int clearflg = 0;
227 static int dashstuff = 0;
228 static int dobody = 1;
229 static int forwflg = 0;
230 static int forwall = 0;
232 static int sleepsw = NOTOK;
234 static char *digest = NULL;
235 static int volume = 0;
236 static int issue = 0;
238 static int exitstat = 0;
239 static int mhldebug = 0;
244 static int ontty = NOTTY;
247 static unsigned int column;
253 static unsigned int wid;
257 static unsigned char *onelp;
261 static int num_ignores = 0;
262 static char *ignores[MAXARGS];
265 static jmp_buf mhlenv;
267 static char delim3[] = /* from forw.c */
268 "\n----------------------------------------------------------------------\n\n";
269 static char delim4[] = "\n------------------------------\n\n";
271 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
273 static struct comp *mhlcomp[128];
276 * Redefine a couple of functions.
277 * These are undefined later in the code.
279 #define adios mhladios
285 static void mhl_format (char *, int, int);
286 static int evalvar (struct mcomp *);
287 static int ptoi (char *, int *);
288 static int ptos (char *, char **);
289 static char *parse (void);
290 static void process (char *, char *, int, int);
291 static void mhlfile (FILE *, char *, int, int);
292 static int mcomp_flags (char *);
293 static char *mcomp_add (long, char *, char *);
294 static void mcomp_format (struct mcomp *, struct mcomp *);
295 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
296 static void free_queue (struct mcomp **, struct mcomp **);
297 static void putcomp (struct mcomp *, struct mcomp *, int);
298 static char *oneline (char *, long);
299 static void putstr (char *, long);
300 static void putch (char, long);
301 static void intrser (int);
302 static void pipeser (int);
303 static void quitser (int);
304 static void face_format (struct mcomp *);
305 static int doface (struct mcomp *);
306 static void mhladios (char *, char *, ...);
307 static void mhldone (int);
308 static void m_popen (char *);
309 static void filterbody (struct mcomp *, char *, int, int, FILE *);
310 static int compileargs (struct mcomp *, char *);
311 static int checkcomp (char *, char *);
312 static void addcomp (int, char *, char *);
313 static void freecomps (void);
314 static void freecomptext (void);
318 mhl (int argc, char **argv)
320 int length = 0, nomore = 0;
321 int i, width = 0, vecp = 0;
322 char *cp, *folder = NULL, *form = NULL;
323 char buf[BUFSIZ], *files[MAXARGS];
324 char **argp, **arguments;
326 invo_name = r1bindex (argv[0], '/');
328 /* read user profile/context */
331 arguments = getarguments (invo_name, argc, argv, 1);
334 if ((cp = getenv ("MHLDEBUG")) && *cp)
337 if ((cp = getenv ("FACEPROC")))
340 while ((cp = *argp++)) {
342 switch (smatch (++cp, mhlswitches)) {
344 ambigsw (cp, mhlswitches);
347 adios (NULL, "-%s unknown\n", cp);
350 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
351 print_help (buf, mhlswitches, 1);
354 print_version(invo_name);
372 if (!(folder = *argp++) || *folder == '-')
373 adios (NULL, "missing argument to %s", argp[-2]);
376 if (!(form = *argp++) || *form == '-')
377 adios (NULL, "missing argument to %s", argp[-2]);
381 if (!(faceproc = *argp++) || *faceproc == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
388 if (!(cp = *argp++) || *cp == '-')
389 adios (NULL, "missing argument to %s", argp[-2]);
390 sleepsw = atoi (cp);/* ZERO ok! */
394 if (!(moreproc = *argp++) || *moreproc == '-')
395 adios (NULL, "missing argument to %s", argp[-2]);
402 if (!(formatproc = *argp++) || *formatproc == '-')
403 adios (NULL, "missing argument to %s", argp[-2]);
410 if (!(cp = *argp++) || *cp == '-')
411 adios (NULL, "missing argument to %s", argp[-2]);
412 if ((length = atoi (cp)) < 1)
413 adios (NULL, "bad argument %s %s", argp[-2], cp);
416 if (!(cp = *argp++) || *cp == '-')
417 adios (NULL, "missing argument to %s", argp[-2]);
418 if ((width = atoi (cp)) < 1)
419 adios (NULL, "bad argument %s %s", argp[-2], cp);
423 if (!(digest = *argp++) || *digest == '-')
424 adios (NULL, "missing argument to %s", argp[-2]);
427 if (!(cp = *argp++) || *cp == '-')
428 adios (NULL, "missing argument to %s", argp[-2]);
429 if ((issue = atoi (cp)) < 1)
430 adios (NULL, "bad argument %s %s", argp[-2], cp);
433 if (!(cp = *argp++) || *cp == '-')
434 adios (NULL, "missing argument to %s", argp[-2]);
435 if ((volume = atoi (cp)) < 1)
436 adios (NULL, "bad argument %s %s", argp[-2], cp);
440 forwall++; /* fall */
443 clearflg = -1;/* XXX */
447 dashstuff = 1; /* trinary logic */
450 dashstuff = -1; /* trinary logic */
462 folder = getenv ("mhfolder");
464 if (isatty (fileno (stdout))) {
465 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
467 SIGNAL (SIGINT, SIG_IGN);
468 SIGNAL2 (SIGQUIT, quitser);
470 SIGNAL2 (SIGPIPE, pipeser);
474 SIGNAL (SIGINT, SIG_IGN);
475 SIGNAL2 (SIGQUIT, quitser);
482 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++)
485 mhl_format (form ? form : mhlformat, length, width);
488 process (folder, NULL, 1, vecp = 1);
490 for (i = 0; i < vecp; i++)
491 process (folder, files[i], i + 1, vecp);
498 printf ("%s", delim4);
500 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
502 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
503 digest, volume, issue);
506 for (cp = buf + i; i > 1; i--)
513 printf ("\n------- End of Forwarded Message%s\n",
514 vecp > 1 ? "s" : "");
519 adios("output", "error writing");
522 if (clearflg > 0 && ontty == NOTTY)
533 mhl_format (char *file, int length, int width)
537 char *ap, buffer[BUFSIZ], name[NAMESZ];
541 static dev_t dev = 0;
542 static ino_t ino = 0;
543 static time_t mtime = 0;
546 if (stat (etcpath (file), &st) != NOTOK
547 && mtime == st.st_mtime
552 free_queue (&fmthd, &fmttl);
555 if ((fp = fopen (etcpath (file), "r")) == NULL)
556 adios (file, "unable to open format file");
558 if (fstat (fileno (fp), &st) != NOTOK) {
564 global.c_ovtxt = global.c_nfs = NULL;
568 if ((i = sc_width ()) > 5)
570 global.c_cwidth = -1;
571 if ((i = sc_length ()) > 5)
572 global.c_length = i - 1;
573 global.c_flags = BELL; /* BELL is default */
574 *(ip = ignores) = NULL;
576 while (vfgets (fp, &ap) == OK) {
581 if ((cp = strchr(bp, '\n')))
585 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
590 strncpy (name, parse(), sizeof(name));
596 * Split this list of fields to ignore, and copy
597 * it to the end of the current "ignores" list.
599 if (!mh_strcasecmp (name, "ignores")) {
600 char **tmparray, **p;
603 /* split the fields */
604 tmparray = brkstring (getcpy (++parptr), ",", NULL);
606 /* count number of fields split */
611 /* copy pointers to split fields to ignores array */
612 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
618 if (evalvar (&global))
619 adios (NULL, "format file syntax error: %s", bp);
626 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
627 while (*parptr == ':' || *parptr == ',') {
630 adios (NULL, "format file syntax error: %s", bp);
632 if (!c1->c_nfs && global.c_nfs) {
633 if (c1->c_flags & DATEFMT) {
634 if (global.c_flags & DATEFMT)
635 c1->c_nfs = getcpy (global.c_nfs);
638 if (c1->c_flags & ADDRFMT) {
639 if (global.c_flags & ADDRFMT)
640 c1->c_nfs = getcpy (global.c_nfs);
646 adios (NULL, "format file syntax error: %s", bp);
652 for (c1 = fmthd; c1; c1 = c1->c_next) {
653 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
654 c1->c_name, c1->c_text, c1->c_ovtxt);
655 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
656 (unsigned int)(unsigned long) c1->c_nfs,
657 (unsigned int)(unsigned long) c1->c_fmt);
658 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
659 c1->c_offset, c1->c_ovoff, c1->c_width,
660 c1->c_cwidth, c1->c_length);
661 fprintf (stderr, "\tflags=%s\n",
662 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
668 global.c_flags |= CLEARSCR;
671 global.c_flags &= ~CLEARSCR;
674 switch (bellflg) { /* command line may override format file */
676 global.c_flags |= BELL;
679 global.c_flags &= ~BELL;
684 global.c_length = length;
686 global.c_width = width;
687 if (global.c_length < 5)
688 global.c_length = 10000;
689 if (global.c_width < 5)
690 global.c_width = 10000;
695 evalvar (struct mcomp *c1)
697 char *cp, name[NAMESZ];
702 strncpy (name, parse(), sizeof(name));
704 if (!mh_strcasecmp (name, "component")) {
705 if (ptos (name, &c1->c_text))
707 c1->c_flags &= ~NOCOMPONENT;
711 if (!mh_strcasecmp (name, "overflowtext"))
712 return ptos (name, &c1->c_ovtxt);
714 if (!mh_strcasecmp (name, "formatfield")) {
717 if (ptos (name, &cp))
719 nfs = new_fs (NULL, NULL, cp);
720 c1->c_nfs = getcpy (nfs);
721 c1->c_flags |= FORMAT;
725 if (!mh_strcasecmp (name, "decode")) {
728 nfs = new_fs (NULL, NULL, "%(decode{text})");
729 c1->c_nfs = getcpy (nfs);
730 c1->c_flags |= FORMAT;
734 if (!mh_strcasecmp (name, "offset"))
735 return ptoi (name, &c1->c_offset);
736 if (!mh_strcasecmp (name, "overflowoffset"))
737 return ptoi (name, &c1->c_ovoff);
738 if (!mh_strcasecmp (name, "width"))
739 return ptoi (name, &c1->c_width);
740 if (!mh_strcasecmp (name, "compwidth"))
741 return ptoi (name, &c1->c_cwidth);
742 if (!mh_strcasecmp (name, "length"))
743 return ptoi (name, &c1->c_length);
744 if (!mh_strcasecmp (name, "nodashstuffing"))
745 return (dashstuff = -1);
747 for (ap = triples; ap->t_name; ap++)
748 if (!mh_strcasecmp (ap->t_name, name)) {
749 c1->c_flags |= ap->t_on;
750 c1->c_flags &= ~ap->t_off;
754 if (!mh_strcasecmp (name, "formatarg")) {
758 if (ptos (name, &cp))
760 nfs = new_fs (NULL, NULL, cp);
762 rc = compileargs(c1, nfs);
773 ptoi (char *name, int *i)
777 if (*parptr++ != '=' || !*(cp = parse ())) {
778 advise (NULL, "missing argument to variable %s", name);
788 ptos (char *name, char **s)
792 if (*parptr++ != '=') {
793 advise (NULL, "missing argument to variable %s", name);
797 if (*parptr != '"') {
799 *parptr && *parptr != ':' && *parptr != ',';
803 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
804 if (*parptr == QUOTE)
811 if ((*parptr = c) == '"')
822 static char result[NAMESZ];
824 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
843 process (char *folder, char *fname, int ofilen, int ofilec)
849 switch (m_setjmp (env)) {
852 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
854 advise (fname, "unable to open");
862 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
864 SIGNAL (SIGINT, intrser);
865 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
869 SIGNAL (SIGINT, SIG_IGN);
870 if (mhl_action == NULL && fp != stdin)
874 free (holder.c_text);
875 holder.c_text = NULL;
877 free_queue (&msghd, &msgtl);
878 for (c1 = fmthd; c1; c1 = c1->c_next)
879 c1->c_flags &= ~HDROUTPUT;
888 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
891 struct mcomp *c1, *c2, *c3;
892 char **ip, name[NAMESZ], buf[BUFSIZ];
896 printf ("%s", ofilen == 1 ? delim3 : delim4);
898 printf ("\n-------");
900 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
902 printf (" Message %d", ofilen);
910 if ((global.c_flags & CLEARSCR))
915 printf (">>> %s\n\n", mname);
920 strncpy (buf, "\n", sizeof(buf));
922 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
925 printf ("Press <return> to list \"%s\"...", mname);
929 read (fileno (stdout), buf, sizeof(buf));
931 if (strchr(buf, '\n')) {
932 if ((global.c_flags & CLEARSCR))
946 printf (">>> %s\n\n", mname);
952 for (state = FLD;;) {
953 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
956 bucket = checkcomp(name, buf);
957 for (ip = ignores; *ip; ip++)
958 if (!mh_strcasecmp (name, *ip)) {
959 while (state == FLDPLUS) {
960 state = m_getfld (state, name, buf, sizeof(buf), fp);
961 addcomp(bucket, name, buf);
968 for (c2 = fmthd; c2; c2 = c2->c_next)
969 if (!mh_strcasecmp (c2->c_name, name))
972 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
973 for (c1 = msghd; c1; c1 = c1->c_next)
974 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
976 mcomp_add (c1->c_flags, buf, c1->c_text);
980 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
981 while (state == FLDPLUS) {
982 state = m_getfld (state, name, buf, sizeof(buf), fp);
983 c1->c_text = add (buf, c1->c_text);
984 addcomp(bucket, name, buf);
987 c1->c_flags |= EXTRA;
993 for (c1 = fmthd; c1; c1 = c1->c_next) {
994 if (c1->c_flags & CLEARTEXT) {
995 putcomp (c1, c1, ONECOMP);
998 if (!mh_strcasecmp (c1->c_name, "messagename")) {
999 holder.c_text = concat ("(Message ", mname, ")\n",
1001 putcomp (c1, &holder, ONECOMP);
1002 free (holder.c_text);
1003 holder.c_text = NULL;
1006 if (!mh_strcasecmp (c1->c_name, "extras")) {
1007 for (c2 = msghd; c2; c2 = c2->c_next)
1008 if (c2->c_flags & EXTRA)
1009 putcomp (c1, c2, TWOCOMP);
1012 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
1013 if (c1->c_flags & FMTFILTER && state == BODY &&
1014 formatproc != NULL) {
1015 filterbody(c1, buf, sizeof(buf), state, fp);
1017 holder.c_text = mh_xmalloc (sizeof(buf));
1018 strncpy (holder.c_text, buf, sizeof(buf));
1019 while (state == BODY) {
1020 putcomp (c1, &holder, BODYCOMP);
1021 state = m_getfld (state, name, holder.c_text,
1024 free (holder.c_text);
1025 holder.c_text = NULL;
1029 for (c2 = msghd; c2; c2 = c2->c_next)
1030 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
1031 putcomp (c1, c2, ONECOMP);
1032 if (!(c1->c_flags & SPLIT))
1035 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
1036 for (c2 = msghd; c2; c2 = c2->c_next)
1037 if (c2->c_flags & FACEDFLT) {
1038 if (c2->c_face == NULL)
1040 if ((holder.c_text = c2->c_face)) {
1041 putcomp (c1, &holder, ONECOMP);
1042 holder.c_text = NULL;
1051 advise (NULL, "format error in message %s", mname);
1056 adios (NULL, "getfld() returned %d", state);
1063 mcomp_flags (char *name)
1067 for (ap = pairs; ap->p_name; ap++)
1068 if (!mh_strcasecmp (ap->p_name, name))
1069 return (ap->p_flags);
1076 mcomp_add (long flags, char *s1, char *s2)
1080 if (!(flags & ADDRFMT))
1081 return add (s1, s2);
1083 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1086 return add (s1, add (",\n", s2));
1093 struct pqpair *pq_next;
1098 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1102 char buffer[BUFSIZ], error[BUFSIZ];
1104 struct pqpair *p, *q;
1106 struct mailname *mp;
1113 dat[3] = sizeof(buffer) - 1;
1115 fmt_compile (c1->c_nfs, &c1->c_fmt);
1117 if (!(c1->c_flags & ADDRFMT)) {
1118 FINDCOMP (cptr, "text");
1121 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1125 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1126 /* Don't need to append a newline, dctime() already did */
1127 c2->c_text = getcpy (buffer);
1133 (q = &pq)->pq_next = NULL;
1134 while ((cp = getname (ap))) {
1135 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1136 adios (NULL, "unable to allocate pqpair memory");
1138 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1139 p->pq_text = getcpy (cp);
1140 p->pq_error = getcpy (error);
1142 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1144 if ((h = mp->m_host) == NULL)
1146 if ((o = OfficialName (h)))
1148 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1151 p->pq_text = getcpy (mp->m_text);
1154 q = (q->pq_next = p);
1157 for (p = pq.pq_next; p; p = q) {
1158 FINDCOMP (cptr, "text");
1160 cptr->c_text = p->pq_text;
1161 FINDCOMP (cptr, "error");
1163 cptr->c_text = p->pq_error;
1165 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1168 c2->c_text = add (",\n", c2->c_text);
1169 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1171 c2->c_text = add (buffer, c2->c_text);
1181 c2->c_text = add ("\n", c2->c_text);
1186 static struct mcomp *
1187 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1191 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1192 adios (NULL, "unable to allocate comp memory");
1194 c1->c_flags = flags & ~INIT;
1195 if ((c1->c_name = name ? getcpy (name) : NULL))
1196 c1->c_flags |= mcomp_flags (c1->c_name);
1197 c1->c_text = text ? getcpy (text) : NULL;
1200 c1->c_ovtxt = getcpy (global.c_ovtxt);
1201 c1->c_offset = global.c_offset;
1202 c1->c_ovoff = global. c_ovoff;
1203 c1->c_width = c1->c_length = 0;
1204 c1->c_cwidth = global.c_cwidth;
1205 c1->c_flags |= global.c_flags & GFLAGS;
1210 (*tail)->c_next = c1;
1218 free_queue (struct mcomp **head, struct mcomp **tail)
1220 struct mcomp *c1, *c2;
1222 for (c1 = *head; c1; c1 = c2) {
1233 free ((char *) c1->c_fmt);
1237 struct arglist *a1, *a2;
1238 for (a1 = c1->c_f_args; a1; a1 = a2) {
1250 *head = *tail = NULL;
1255 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1262 llim = c1->c_length ? c1->c_length : -1;
1263 wid = c1->c_width ? c1->c_width : global.c_width;
1264 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1266 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1268 if (wid < ovoff + strlen (ovtxt) + 5)
1269 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1270 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1273 if (c1->c_flags & CLEARTEXT) {
1274 putstr (c1->c_text, c1->c_flags);
1275 putstr ("\n", c1->c_flags);
1279 if (c1->c_flags & FACEFMT)
1280 switch (doface (c2)) {
1281 case NOTOK: /* error */
1282 case OK: /* async faceproc */
1285 default: /* sync faceproc */
1289 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1290 mcomp_format (c1, c2);
1292 if (c1->c_flags & CENTER) {
1293 count = (c1->c_width ? c1->c_width : global.c_width)
1294 - c1->c_offset - strlen (c2->c_text);
1295 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1296 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1297 lm = c1->c_offset + (count / 2);
1303 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1304 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1305 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1307 *cp = toupper (*cp);
1308 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1309 if (flag != BODYCOMP) {
1310 putstr (": ", c1->c_flags);
1311 if (!(c1->c_flags & SPLIT))
1312 c1->c_flags |= HDROUTPUT;
1315 if ((count = c1->c_cwidth -
1316 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1318 putstr (" ", c1->c_flags);
1321 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1325 && !(c2->c_flags & HDROUTPUT)
1326 && !(c2->c_flags & NOCOMPONENT)) {
1327 if (c1->c_flags & UPPERCASE)
1328 for (cp = c2->c_name; *cp; cp++)
1330 *cp = toupper (*cp);
1331 putstr (c2->c_name, c1->c_flags);
1332 putstr (": ", c1->c_flags);
1333 if (!(c1->c_flags & SPLIT))
1334 c2->c_flags |= HDROUTPUT;
1337 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1339 putstr (" ", c1->c_flags);
1341 if (c1->c_flags & UPPERCASE)
1342 for (cp = c2->c_text; *cp; cp++)
1344 *cp = toupper (*cp);
1348 if (flag == TWOCOMP)
1349 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1350 : (int) strlen (c2->c_name) + 2;
1352 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1353 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1355 count += c1->c_offset;
1357 if ((cp = oneline (c2->c_text, c1->c_flags)))
1358 putstr(cp, c1->c_flags);
1360 putstr ("\n", c1->c_flags);
1361 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1363 if (flag == BODYCOMP
1364 && !(c1->c_flags & NOCOMPONENT))
1365 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1367 putstr (cp, c1->c_flags);
1369 putstr ("\n", c1->c_flags);
1371 if (flag == BODYCOMP && term == '\n')
1372 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1377 oneline (char *stuff, long flags)
1385 return (onelp = NULL);
1389 if (flags & COMPRESS) {
1390 for (spc = 1, cp = ret; *onelp; onelp++)
1391 if (isspace (*onelp)) {
1392 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1411 while (*onelp && *onelp != '\n')
1413 if (*onelp == '\n') {
1417 if (flags & LEFTADJUST)
1418 while (*ret == ' ' || *ret == '\t')
1421 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1429 putstr (char *string, long flags)
1431 if (!column && lm > 0) {
1434 putch ('\t', flags);
1444 putch (*string++, flags);
1449 putch (char ch, long flags)
1462 if (ontty != ISTTY || row != global.c_length)
1464 if (global.c_flags & BELL)
1468 read (fileno (stdout), buf, sizeof(buf));
1469 if (strchr(buf, '\n')) {
1470 if (global.c_flags & CLEARSCR)
1475 row = global.c_length / 3;
1494 * If we are forwarding this message, and the first
1495 * column contains a dash, then add a dash and a space.
1497 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1506 if (column >= wid && (flags & NOWRAP) == 0) {
1507 putch ('\n', flags);
1510 putstr (ovtxt ? ovtxt : "", flags);
1526 longjmp (env, DONE);
1551 face_format (struct mcomp *c1)
1554 struct mailname *mp;
1556 if ((cp = c1->c_text) == NULL)
1559 if ((cp = getname (cp))) {
1560 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1562 if ((h = mp->m_host) == NULL)
1564 if ((o = OfficialName (h)))
1566 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1569 while ((cp = getname (cp)))
1576 * faceproc is two elements defining the image agent's location:
1581 #include <sys/socket.h>
1582 #include <netinet/in.h>
1584 #include <arpa/inet.h>
1587 doface (struct mcomp *c1)
1590 static int inited = OK;
1591 static struct sockaddr_storage ss;
1592 static socklen_t socklen;
1593 static int socktype;
1594 static int protocol;
1598 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1599 struct addrinfo hints, *res;
1601 if (ap[0] == NULL || ap[1] == NULL) {
1604 return (inited = NOTOK);
1607 memset(&hints, 0, sizeof(hints));
1608 #ifdef AI_ADDRCONFIG
1609 hints.ai_flags = AI_ADDRCONFIG;
1611 hints.ai_family = PF_UNSPEC;
1612 hints.ai_socktype = SOCK_DGRAM;
1614 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1617 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1618 socklen = res->ai_addrlen;
1619 socktype = res->ai_socktype;
1620 protocol = res->ai_protocol;
1625 if (inited == NOTOK)
1628 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1631 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1632 (struct sockaddr *) &ss, socklen);
1636 return (result != NOTOK ? OK : NOTOK);
1641 * This version doesn't use sockets
1646 doface (struct mcomp *c1)
1650 int result, pdi[2], pdo[2];
1652 char buffer[BUFSIZ], *vec[10];
1654 if (pipe (pdi) == NOTOK)
1656 if (pipe (pdo) == NOTOK) {
1662 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1667 /* oops... fork error */
1672 SIGNAL (SIGINT, SIG_IGN);
1673 SIGNAL (SIGQUIT, SIG_IGN);
1674 if (pdi[0] != fileno (stdin)) {
1675 dup2 (pdi[0], fileno (stdin));
1680 if (pdo[1] != fileno (stdout)) {
1681 dup2 (pdo[1], fileno (stdout));
1685 vec[vecp++] = r1bindex (faceproc, '/');
1687 if (sleepsw != NOTOK) {
1689 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1690 vec[vecp++] = buffer;
1693 execvp (faceproc, vec);
1694 fprintf (stderr, "unable to exec ");
1696 _exit (-1); /* NOTREACHED */
1699 /* parent process */
1701 i = strlen (c1->c_text);
1702 if (write (pdi[1], c1->c_text, i) != i)
1703 adios ("pipe", "error writing to");
1704 free (c1->c_text), c1->c_text = NULL;
1710 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1714 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1715 memcpy(dp + len, buffer, i);
1719 cp = mh_xmalloc ((unsigned) i);
1720 memcpy(cp, buffer, i);
1724 for (bp = buffer + i - 1; bp >= buffer; bp--)
1725 if (!isascii (*bp) || iscntrl (*bp)) {
1732 /* no waiting for child... */
1734 if (result == OK) { /* binary */
1735 if (write (1, cp, len) != len)
1736 adios ("writing", "error");
1740 if ((c1->c_text = cp) == NULL)
1747 #endif /* COMMENTED OUT */
1751 mhlsbr (int argc, char **argv, FILE *(*action)())
1753 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1757 switch (m_setjmp (mhlenv)) {
1760 sleepsw = 0; /* XXX */
1761 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1764 mhl_action = action;
1767 * If signal is at default action, then start ignoring
1768 * it, else let it set to its current action.
1770 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1771 SIGNAL (SIGINT, istat);
1772 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1773 SIGNAL (SIGQUIT, qstat);
1774 pstat = SIGNAL (SIGPIPE, pipeser);
1775 mhl (argc, argv); /* FALL THROUGH! */
1778 SIGNAL (SIGINT, istat);
1779 SIGNAL (SIGQUIT, qstat);
1780 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1783 SIGNAL (SIGPIPE, pstat);
1785 if (holder.c_text) {
1786 free (holder.c_text);
1787 holder.c_text = NULL;
1789 free_queue (&msghd, &msgtl);
1790 for (c1 = fmthd; c1; c1 = c1->c_next)
1791 c1->c_flags &= ~HDROUTPUT;
1800 mhladios (char *what, char *fmt, ...)
1805 advertise (what, NULL, fmt, ap);
1812 mhldone (int status)
1816 longjmp (mhlenv, DONE);
1822 static int m_pid = NOTOK;
1823 static int sd = NOTOK;
1826 m_popen (char *name)
1830 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1831 adios ("standard output", "unable to dup()");
1833 if (pipe (pd) == NOTOK)
1834 adios ("pipe", "unable to");
1836 switch (m_pid = vfork()) {
1838 adios ("fork", "unable to");
1841 SIGNAL (SIGINT, SIG_DFL);
1842 SIGNAL (SIGQUIT, SIG_DFL);
1845 if (pd[0] != fileno (stdin)) {
1846 dup2 (pd[0], fileno (stdin));
1849 execlp (name, r1bindex (name, '/'), NULL);
1850 fprintf (stderr, "unable to exec ");
1856 if (pd[1] != fileno (stdout)) {
1857 dup2 (pd[1], fileno (stdout));
1872 if (dup2 (sd, fileno (stdout)) == NOTOK)
1873 adios ("standard output", "unable to dup2()");
1882 pidwait (m_pid, OK);
1888 * Compile a format string and add it to the list of arguments used by
1891 * This deserves some explanation. Here's the deal:
1893 * We want to keep track of components used as arguments by formatproc,
1894 * but the hash table is reset every time fmt_compile is called. So we
1895 * iterate through the function list looking for things that use components
1896 * and save the name. And because we might get the same components used
1897 * by different arguments we need to keep track to every reference of
1898 * every component so we can add them when the message is processed. So
1899 * we compile the argument string now (to get the components we use) and
1900 * save them for later.
1904 compileargs (struct mcomp *c1, char *nfs)
1907 struct arglist *args;
1910 i = fmt_compile(nfs, &fmt);
1912 args = (struct arglist *) mh_xmalloc(sizeof(struct arglist));
1915 adios (NULL, "Unable to allocate formatproc args storage");
1918 args->a_nfs = format_string;
1919 args->a_next = NULL;
1921 format_string = NULL;
1924 c1->c_f_tail->a_next = args;
1926 c1->c_f_tail = args;
1929 c1->c_f_args = args;
1935 * If wantcomp ever changes size, we need to change the size
1936 * of mhlcomp as well
1939 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
1943 for (c = mhlcomp[i]; c->c_next != NULL; c = c->c_next)
1945 c->c_next = wantcomp[i];
1947 mhlcomp[i] = wantcomp[i];
1955 * Check to see if we are interested in a component. If we are, save
1960 checkcomp(char *name, char *buf)
1964 int bucket = CHASH(name);
1967 if ((c = mhlcomp[bucket])) {
1969 if (mh_strcasecmp(name, c->c_name) == 0) {
1972 i = strlen(c->c_text = strdup(buf)) - 1;
1973 if (c->c_text[i] == '\n')
1974 c->c_text[i] = '\0';
1976 i = strlen(cp = c->c_text) - 1;
1977 if (cp[i] == '\n') {
1978 if (c->c_type & CT_ADDR) {
1980 cp = add (",\n\t", cp);
1982 cp = add ("\t", cp);
1985 c->c_text = add (buf, cp);
1988 } while ((c = c->c_next));
1991 return found ? bucket : -1;
1995 * Add text to an existing component
1999 addcomp(int bucket, char *name, char *buf)
2004 c = mhlcomp[bucket];
2006 if (mh_strcasecmp(name, c->c_name) == 0)
2007 c->c_text = add (buf, c->c_text);
2008 } while ((c = c->c_next));
2013 * Free up saved component structures
2019 struct comp *c1, *c2;
2022 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2023 if ((c1 = mhlcomp[i]))
2024 for (; c1; c1 = c2) {
2034 * Just free up the component text.
2043 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2044 if ((c1 = mhlcomp[i]))
2045 for (; c1; c1 = c1->c_next) {
2055 * Filter the body of a message through a specified format program
2059 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
2061 struct mcomp holder;
2063 int fdinput[2], fdoutput[2], waitstat;
2065 pid_t writerpid, filterpid;
2068 * Create pipes so we can communicate with our filter process.
2071 if (pipe(fdinput) < 0) {
2072 adios(NULL, "Unable to create input pipe");
2075 if (pipe(fdoutput) < 0) {
2076 adios(NULL, "Unable to create output pipe");
2080 * Here's what we're doing to do.
2082 * - Fork ourselves and start writing data to the write side of the
2083 * input pipe (fdinput[1]).
2085 * - Fork and exec our filter program. We set the standard input of
2086 * our filter program to be the read side of our input pipe (fdinput[0]).
2087 * Standard output is set to the write side of our output pipe
2090 * - We read from the read side of the output pipe (fdoutput[0]).
2092 * We're forking because that's the simplest way to prevent any deadlocks.
2093 * (without doing something like switching to non-blocking I/O and using
2094 * select or poll, and I'm not interested in doing that).
2097 switch (writerpid = fork()) {
2100 * Our child process - just write to the filter input (fdinput[1]).
2101 * Close all other descriptors that we don't need.
2109 * Call m_getfld() until we're no longer in the BODY state
2112 while (state == BODY) {
2113 write(fdinput[1], buf, strlen(buf));
2114 state = m_getfld(state, name, buf, bufsz, fp);
2118 * We should be done; time to exit.
2125 adios(NULL, "Unable to fork for filter writer process");
2130 * Fork and exec() our filter program, after redirecting standard in
2131 * and standard out appropriately.
2134 switch (filterpid = fork()) {
2141 * Allocate an argument array for us
2144 args = (char **) mh_xmalloc((c1->c_nargs + 2) * sizeof(char *));
2145 args[0] = formatproc;
2146 args[c1->c_nargs + 1] = NULL;
2154 * Pull out each argument and scan them.
2157 for (a = c1->c_f_args, i = 1; a != NULL; a = a->a_next, i++) {
2158 args[i] = mh_xmalloc(BUFSIZ);
2159 fmt_scan(a->a_fmt, args[i], BUFSIZ, dat);
2161 * fmt_scan likes to put a trailing newline at the end of the
2162 * format string. If we have one, get rid of it.
2164 s = strlen(args[i]);
2165 if (args[i][s - 1] == '\n')
2166 args[i][s - 1] = '\0';
2169 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
2170 adios("formatproc", "Unable to dup2() standard input");
2172 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
2173 adios("formatproc", "Unable to dup2() standard output");
2177 * Close everything (especially the old input and output
2178 * descriptors, since they've been dup'd to stdin and stdout),
2179 * and exec the formatproc.
2187 execvp(formatproc, args);
2189 adios(formatproc, "Unable to execute filter");
2194 adios(NULL, "Unable to fork format program");
2198 * Close everything except our reader (fdoutput[0]);
2206 * As we read in this data, send it to putcomp
2209 holder.c_text = buf;
2211 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
2213 putcomp(c1, &holder, BODYCOMP);
2217 adios(NULL, "reading from formatproc");
2221 * See if we got any errors along the way. I'm a little leery of calling
2222 * waitpid() without WNOHANG, but it seems to be the most correct solution.
2225 if (waitpid(filterpid, &waitstat, 0) < 0) {
2226 if (errno != ECHILD) {
2227 adios("filterproc", "Unable to determine status");
2230 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2231 pidstatus(waitstat, stderr, "filterproc");
2235 if (waitpid(writerpid, &waitstat, 0) < 0) {
2236 if (errno != ECHILD) {
2237 adios("writer process", "Unable to determine status");
2241 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2242 pidstatus(waitstat, stderr, "writer process");