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, NULL,
163 static struct mcomp holder = {
164 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL, NULL,
173 static struct pair pairs[] = {
175 { "From", ADDRFMT|FACEDFLT },
176 { "Sender", ADDRFMT },
177 { "Reply-To", ADDRFMT },
181 { "Resent-Date", DATEFMT },
182 { "Resent-From", ADDRFMT },
183 { "Resent-Sender", ADDRFMT },
184 { "Resent-Reply-To", ADDRFMT },
185 { "Resent-To", ADDRFMT },
186 { "Resent-cc", ADDRFMT },
187 { "Resent-Bcc", ADDRFMT },
198 static struct triple triples[] = {
199 { "nocomponent", NOCOMPONENT, 0 },
200 { "uppercase", UPPERCASE, 0 },
201 { "nouppercase", 0, UPPERCASE },
202 { "center", CENTER, 0 },
203 { "nocenter", 0, CENTER },
204 { "clearscreen", CLEARSCR, 0 },
205 { "noclearscreen", 0, CLEARSCR },
206 { "noclear", 0, CLEARSCR },
207 { "leftadjust", LEFTADJUST, 0 },
208 { "noleftadjust", 0, LEFTADJUST },
209 { "compress", COMPRESS, 0 },
210 { "nocompress", 0, COMPRESS },
211 { "split", SPLIT, 0 },
212 { "nosplit", 0, SPLIT },
213 { "addrfield", ADDRFMT, DATEFMT },
215 { "nobell", 0, BELL },
216 { "datefield", DATEFMT, ADDRFMT },
217 { "newline", 0, NONEWLINE },
218 { "nonewline", NONEWLINE, 0 },
219 { "wrap", 0, NOWRAP },
220 { "nowrap", NOWRAP, 0 },
221 { "format", FMTFILTER, 0 },
222 { "noformat", 0, FMTFILTER },
227 static int bellflg = 0;
228 static int clearflg = 0;
229 static int dashstuff = 0;
230 static int dobody = 1;
231 static int forwflg = 0;
232 static int forwall = 0;
234 static int sleepsw = NOTOK;
236 static char *digest = NULL;
237 static int volume = 0;
238 static int issue = 0;
240 static int exitstat = 0;
241 static int mhldebug = 0;
246 static int ontty = NOTTY;
249 static unsigned int column;
255 static unsigned int wid;
259 static unsigned char *onelp;
263 static int num_ignores = 0;
264 static char *ignores[MAXARGS];
267 static jmp_buf mhlenv;
269 static char delim3[] = /* from forw.c */
270 "\n----------------------------------------------------------------------\n\n";
271 static char delim4[] = "\n------------------------------\n\n";
273 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
275 static struct comp *mhlcomp[128];
278 * Redefine a couple of functions.
279 * These are undefined later in the code.
281 #define adios mhladios
287 static void mhl_format (char *, int, int);
288 static int evalvar (struct mcomp *);
289 static int ptoi (char *, int *);
290 static int ptos (char *, char **);
291 static char *parse (void);
292 static void process (char *, char *, int, int);
293 static void mhlfile (FILE *, char *, int, int);
294 static int mcomp_flags (char *);
295 static char *mcomp_add (long, char *, char *);
296 static void mcomp_format (struct mcomp *, struct mcomp *);
297 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
298 static void free_queue (struct mcomp **, struct mcomp **);
299 static void putcomp (struct mcomp *, struct mcomp *, int);
300 static char *oneline (char *, long);
301 static void putstr (char *, long);
302 static void putch (char, long);
303 static void intrser (int);
304 static void pipeser (int);
305 static void quitser (int);
306 static void face_format (struct mcomp *);
307 static int doface (struct mcomp *);
308 static void mhladios (char *, char *, ...);
309 static void mhldone (int);
310 static void m_popen (char *);
311 static void filterbody (struct mcomp *, char *, int, int, FILE *);
312 static int compileargs (struct mcomp *, char *);
313 static int checkcomp (char *, char *);
314 static void addcomp (int, char *, char *);
315 static void freecomps (void);
316 static void freecomptext (void);
320 mhl (int argc, char **argv)
322 int length = 0, nomore = 0;
323 unsigned int i, vecp = 0;;
325 char *cp, *folder = NULL, *form = NULL;
326 char buf[BUFSIZ], *files[MAXARGS];
327 char **argp, **arguments;
329 invo_name = r1bindex (argv[0], '/');
331 /* read user profile/context */
334 arguments = getarguments (invo_name, argc, argv, 1);
337 if ((cp = getenv ("MHLDEBUG")) && *cp)
340 if ((cp = getenv ("FACEPROC")))
343 while ((cp = *argp++)) {
345 switch (smatch (++cp, mhlswitches)) {
347 ambigsw (cp, mhlswitches);
350 adios (NULL, "-%s unknown\n", cp);
353 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
354 print_help (buf, mhlswitches, 1);
357 print_version(invo_name);
375 if (!(folder = *argp++) || *folder == '-')
376 adios (NULL, "missing argument to %s", argp[-2]);
379 if (!(form = *argp++) || *form == '-')
380 adios (NULL, "missing argument to %s", argp[-2]);
384 if (!(faceproc = *argp++) || *faceproc == '-')
385 adios (NULL, "missing argument to %s", argp[-2]);
391 if (!(cp = *argp++) || *cp == '-')
392 adios (NULL, "missing argument to %s", argp[-2]);
393 sleepsw = atoi (cp);/* ZERO ok! */
397 if (!(moreproc = *argp++) || *moreproc == '-')
398 adios (NULL, "missing argument to %s", argp[-2]);
405 if (!(formatproc = *argp++) || *formatproc == '-')
406 adios (NULL, "missing argument to %s", argp[-2]);
413 if (!(cp = *argp++) || *cp == '-')
414 adios (NULL, "missing argument to %s", argp[-2]);
415 if ((length = atoi (cp)) < 1)
416 adios (NULL, "bad argument %s %s", argp[-2], cp);
419 if (!(cp = *argp++) || *cp == '-')
420 adios (NULL, "missing argument to %s", argp[-2]);
421 if ((width = atoi (cp)) < 1)
422 adios (NULL, "bad argument %s %s", argp[-2], cp);
426 if (!(digest = *argp++) || *digest == '-')
427 adios (NULL, "missing argument to %s", argp[-2]);
430 if (!(cp = *argp++) || *cp == '-')
431 adios (NULL, "missing argument to %s", argp[-2]);
432 if ((issue = atoi (cp)) < 1)
433 adios (NULL, "bad argument %s %s", argp[-2], cp);
436 if (!(cp = *argp++) || *cp == '-')
437 adios (NULL, "missing argument to %s", argp[-2]);
438 if ((volume = atoi (cp)) < 1)
439 adios (NULL, "bad argument %s %s", argp[-2], cp);
443 forwall++; /* fall */
446 clearflg = -1;/* XXX */
450 dashstuff = 1; /* trinary logic */
453 dashstuff = -1; /* trinary logic */
465 folder = getenv ("mhfolder");
467 if (isatty (fileno (stdout))) {
468 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
470 SIGNAL (SIGINT, SIG_IGN);
471 SIGNAL2 (SIGQUIT, quitser);
473 SIGNAL2 (SIGPIPE, pipeser);
477 SIGNAL (SIGINT, SIG_IGN);
478 SIGNAL2 (SIGQUIT, quitser);
485 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++)
488 mhl_format (form ? form : mhlformat, length, width);
491 process (folder, NULL, 1, vecp = 1);
493 for (i = 0; i < vecp; i++)
494 process (folder, files[i], i + 1, vecp);
501 printf ("%s", delim4);
503 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
505 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
506 digest, volume, issue);
509 for (cp = buf + i; i > 1; i--)
516 printf ("\n------- End of Forwarded Message%s\n",
517 vecp > 1 ? "s" : "");
522 adios("output", "error writing");
525 if (clearflg > 0 && ontty == NOTTY)
536 mhl_format (char *file, int length, int width)
540 char *ap, buffer[BUFSIZ], name[NAMESZ];
544 static dev_t dev = 0;
545 static ino_t ino = 0;
546 static time_t mtime = 0;
549 if (stat (etcpath (file), &st) != NOTOK
550 && mtime == st.st_mtime
555 free_queue (&fmthd, &fmttl);
558 if ((fp = fopen (etcpath (file), "r")) == NULL)
559 adios (file, "unable to open format file");
561 if (fstat (fileno (fp), &st) != NOTOK) {
567 global.c_ovtxt = global.c_nfs = NULL;
571 if ((i = sc_width ()) > 5)
573 global.c_cwidth = -1;
574 if ((i = sc_length ()) > 5)
575 global.c_length = i - 1;
576 global.c_flags = BELL; /* BELL is default */
577 *(ip = ignores) = NULL;
579 while (vfgets (fp, &ap) == OK) {
584 if ((cp = strchr(bp, '\n')))
588 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
593 strncpy (name, parse(), sizeof(name));
599 * Split this list of fields to ignore, and copy
600 * it to the end of the current "ignores" list.
602 if (!mh_strcasecmp (name, "ignores")) {
603 char **tmparray, **p;
606 /* split the fields */
607 tmparray = brkstring (getcpy (++parptr), ",", NULL);
609 /* count number of fields split */
614 /* copy pointers to split fields to ignores array */
615 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
621 if (evalvar (&global))
622 adios (NULL, "format file syntax error: %s", bp);
629 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
630 while (*parptr == ':' || *parptr == ',') {
633 adios (NULL, "format file syntax error: %s", bp);
635 if (!c1->c_nfs && global.c_nfs) {
636 if (c1->c_flags & DATEFMT) {
637 if (global.c_flags & DATEFMT)
638 c1->c_nfs = getcpy (global.c_nfs);
641 if (c1->c_flags & ADDRFMT) {
642 if (global.c_flags & ADDRFMT)
643 c1->c_nfs = getcpy (global.c_nfs);
649 adios (NULL, "format file syntax error: %s", bp);
655 for (c1 = fmthd; c1; c1 = c1->c_next) {
656 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
657 c1->c_name, c1->c_text, c1->c_ovtxt);
658 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
659 (unsigned int)(unsigned long) c1->c_nfs,
660 (unsigned int)(unsigned long) c1->c_fmt);
661 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
662 c1->c_offset, c1->c_ovoff, c1->c_width,
663 c1->c_cwidth, c1->c_length);
664 fprintf (stderr, "\tflags=%s\n",
665 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
671 global.c_flags |= CLEARSCR;
674 global.c_flags &= ~CLEARSCR;
677 switch (bellflg) { /* command line may override format file */
679 global.c_flags |= BELL;
682 global.c_flags &= ~BELL;
687 global.c_length = length;
689 global.c_width = width;
690 if (global.c_length < 5)
691 global.c_length = 10000;
692 if (global.c_width < 5)
693 global.c_width = 10000;
698 evalvar (struct mcomp *c1)
700 char *cp, name[NAMESZ];
705 strncpy (name, parse(), sizeof(name));
707 if (!mh_strcasecmp (name, "component")) {
708 if (ptos (name, &c1->c_text))
710 c1->c_flags &= ~NOCOMPONENT;
714 if (!mh_strcasecmp (name, "overflowtext"))
715 return ptos (name, &c1->c_ovtxt);
717 if (!mh_strcasecmp (name, "formatfield")) {
720 if (ptos (name, &cp))
722 nfs = new_fs (NULL, NULL, cp);
723 c1->c_nfs = getcpy (nfs);
724 c1->c_flags |= FORMAT;
728 if (!mh_strcasecmp (name, "decode")) {
731 nfs = new_fs (NULL, NULL, "%(decode{text})");
732 c1->c_nfs = getcpy (nfs);
733 c1->c_flags |= FORMAT;
737 if (!mh_strcasecmp (name, "offset"))
738 return ptoi (name, &c1->c_offset);
739 if (!mh_strcasecmp (name, "overflowoffset"))
740 return ptoi (name, &c1->c_ovoff);
741 if (!mh_strcasecmp (name, "width"))
742 return ptoi (name, &c1->c_width);
743 if (!mh_strcasecmp (name, "compwidth"))
744 return ptoi (name, &c1->c_cwidth);
745 if (!mh_strcasecmp (name, "length"))
746 return ptoi (name, &c1->c_length);
747 if (!mh_strcasecmp (name, "nodashstuffing"))
748 return (dashstuff = -1);
750 for (ap = triples; ap->t_name; ap++)
751 if (!mh_strcasecmp (ap->t_name, name)) {
752 c1->c_flags |= ap->t_on;
753 c1->c_flags &= ~ap->t_off;
757 if (!mh_strcasecmp (name, "formatarg")) {
761 if (ptos (name, &cp))
763 nfs = new_fs (NULL, NULL, cp);
765 rc = compileargs(c1, nfs);
776 ptoi (char *name, int *i)
780 if (*parptr++ != '=' || !*(cp = parse ())) {
781 advise (NULL, "missing argument to variable %s", name);
791 ptos (char *name, char **s)
795 if (*parptr++ != '=') {
796 advise (NULL, "missing argument to variable %s", name);
800 if (*parptr != '"') {
802 *parptr && *parptr != ':' && *parptr != ',';
806 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
807 if (*parptr == QUOTE)
814 if ((*parptr = c) == '"')
825 static char result[NAMESZ];
827 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
846 process (char *folder, char *fname, int ofilen, int ofilec)
852 switch (m_setjmp (env)) {
855 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
857 advise (fname, "unable to open");
865 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
867 SIGNAL (SIGINT, intrser);
868 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
872 SIGNAL (SIGINT, SIG_IGN);
873 if (mhl_action == NULL && fp != stdin)
877 free (holder.c_text);
878 holder.c_text = NULL;
880 free_queue (&msghd, &msgtl);
881 for (c1 = fmthd; c1; c1 = c1->c_next)
882 c1->c_flags &= ~HDROUTPUT;
891 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
894 struct mcomp *c1, *c2, *c3;
895 char **ip, name[NAMESZ], buf[BUFSIZ];
899 printf ("%s", ofilen == 1 ? delim3 : delim4);
901 printf ("\n-------");
903 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
905 printf (" Message %d", ofilen);
913 if ((global.c_flags & CLEARSCR))
918 printf (">>> %s\n\n", mname);
923 strncpy (buf, "\n", sizeof(buf));
925 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
928 printf ("Press <return> to list \"%s\"...", mname);
932 read (fileno (stdout), buf, sizeof(buf));
934 if (strchr(buf, '\n')) {
935 if ((global.c_flags & CLEARSCR))
949 printf (">>> %s\n\n", mname);
955 for (state = FLD;;) {
956 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
959 bucket = checkcomp(name, buf);
960 for (ip = ignores; *ip; ip++)
961 if (!mh_strcasecmp (name, *ip)) {
962 while (state == FLDPLUS) {
963 state = m_getfld (state, name, buf, sizeof(buf), fp);
964 addcomp(bucket, name, buf);
971 for (c2 = fmthd; c2; c2 = c2->c_next)
972 if (!mh_strcasecmp (c2->c_name, name))
975 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
976 for (c1 = msghd; c1; c1 = c1->c_next)
977 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
979 mcomp_add (c1->c_flags, buf, c1->c_text);
983 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
984 while (state == FLDPLUS) {
985 state = m_getfld (state, name, buf, sizeof(buf), fp);
986 c1->c_text = add (buf, c1->c_text);
987 addcomp(bucket, name, buf);
990 c1->c_flags |= EXTRA;
996 for (c1 = fmthd; c1; c1 = c1->c_next) {
997 if (c1->c_flags & CLEARTEXT) {
998 putcomp (c1, c1, ONECOMP);
1001 if (!mh_strcasecmp (c1->c_name, "messagename")) {
1002 holder.c_text = concat ("(Message ", mname, ")\n",
1004 putcomp (c1, &holder, ONECOMP);
1005 free (holder.c_text);
1006 holder.c_text = NULL;
1009 if (!mh_strcasecmp (c1->c_name, "extras")) {
1010 for (c2 = msghd; c2; c2 = c2->c_next)
1011 if (c2->c_flags & EXTRA)
1012 putcomp (c1, c2, TWOCOMP);
1015 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
1016 if (c1->c_flags & FMTFILTER && state == BODY &&
1017 formatproc != NULL) {
1018 filterbody(c1, buf, sizeof(buf), state, fp);
1020 holder.c_text = mh_xmalloc (sizeof(buf));
1021 strncpy (holder.c_text, buf, sizeof(buf));
1022 while (state == BODY) {
1023 putcomp (c1, &holder, BODYCOMP);
1024 state = m_getfld (state, name, holder.c_text,
1027 free (holder.c_text);
1028 holder.c_text = NULL;
1032 for (c2 = msghd; c2; c2 = c2->c_next)
1033 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
1034 putcomp (c1, c2, ONECOMP);
1035 if (!(c1->c_flags & SPLIT))
1038 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
1039 for (c2 = msghd; c2; c2 = c2->c_next)
1040 if (c2->c_flags & FACEDFLT) {
1041 if (c2->c_face == NULL)
1043 if ((holder.c_text = c2->c_face)) {
1044 putcomp (c1, &holder, ONECOMP);
1045 holder.c_text = NULL;
1054 advise (NULL, "format error in message %s", mname);
1059 adios (NULL, "getfld() returned %d", state);
1066 mcomp_flags (char *name)
1070 for (ap = pairs; ap->p_name; ap++)
1071 if (!mh_strcasecmp (ap->p_name, name))
1072 return (ap->p_flags);
1079 mcomp_add (long flags, char *s1, char *s2)
1083 if (!(flags & ADDRFMT))
1084 return add (s1, s2);
1086 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1089 return add (s1, add (",\n", s2));
1096 struct pqpair *pq_next;
1101 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1105 char buffer[BUFSIZ], error[BUFSIZ];
1107 struct pqpair *p, *q;
1109 struct mailname *mp;
1116 dat[3] = sizeof(buffer) - 1;
1118 fmt_compile (c1->c_nfs, &c1->c_fmt);
1120 if (!(c1->c_flags & ADDRFMT)) {
1121 FINDCOMP (cptr, "text");
1124 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1128 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1129 /* Don't need to append a newline, dctime() already did */
1130 c2->c_text = getcpy (buffer);
1136 (q = &pq)->pq_next = NULL;
1137 while ((cp = getname (ap))) {
1138 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1139 adios (NULL, "unable to allocate pqpair memory");
1141 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1142 p->pq_text = getcpy (cp);
1143 p->pq_error = getcpy (error);
1145 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1147 if ((h = mp->m_host) == NULL)
1149 if ((o = OfficialName (h)))
1151 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1154 p->pq_text = getcpy (mp->m_text);
1157 q = (q->pq_next = p);
1160 for (p = pq.pq_next; p; p = q) {
1161 FINDCOMP (cptr, "text");
1163 cptr->c_text = p->pq_text;
1164 FINDCOMP (cptr, "error");
1166 cptr->c_text = p->pq_error;
1168 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1171 c2->c_text = add (",\n", c2->c_text);
1172 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1174 c2->c_text = add (buffer, c2->c_text);
1184 c2->c_text = add ("\n", c2->c_text);
1189 static struct mcomp *
1190 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1194 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1195 adios (NULL, "unable to allocate comp memory");
1197 c1->c_flags = flags & ~INIT;
1198 if ((c1->c_name = name ? getcpy (name) : NULL))
1199 c1->c_flags |= mcomp_flags (c1->c_name);
1200 c1->c_text = text ? getcpy (text) : NULL;
1203 c1->c_ovtxt = getcpy (global.c_ovtxt);
1204 c1->c_offset = global.c_offset;
1205 c1->c_ovoff = global. c_ovoff;
1206 c1->c_width = c1->c_length = 0;
1207 c1->c_cwidth = global.c_cwidth;
1208 c1->c_flags |= global.c_flags & GFLAGS;
1213 (*tail)->c_next = c1;
1221 free_queue (struct mcomp **head, struct mcomp **tail)
1223 struct mcomp *c1, *c2;
1225 for (c1 = *head; c1; c1 = c2) {
1236 free ((char *) c1->c_fmt);
1240 struct arglist *a1, *a2;
1241 for (a1 = c1->c_f_args; a1; a1 = a2) {
1253 *head = *tail = NULL;
1258 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1265 llim = c1->c_length ? c1->c_length : -1;
1266 wid = c1->c_width ? c1->c_width : global.c_width;
1267 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1269 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1271 if (wid < ovoff + strlen (ovtxt) + 5)
1272 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1273 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1276 if (c1->c_flags & CLEARTEXT) {
1277 putstr (c1->c_text, c1->c_flags);
1278 putstr ("\n", c1->c_flags);
1282 if (c1->c_flags & FACEFMT)
1283 switch (doface (c2)) {
1284 case NOTOK: /* error */
1285 case OK: /* async faceproc */
1288 default: /* sync faceproc */
1292 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1293 mcomp_format (c1, c2);
1295 if (c1->c_flags & CENTER) {
1296 count = (c1->c_width ? c1->c_width : global.c_width)
1297 - c1->c_offset - strlen (c2->c_text);
1298 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1299 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1300 lm = c1->c_offset + (count / 2);
1306 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1307 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1308 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1310 *cp = toupper (*cp);
1311 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1312 if (flag != BODYCOMP) {
1313 putstr (": ", c1->c_flags);
1314 if (!(c1->c_flags & SPLIT))
1315 c1->c_flags |= HDROUTPUT;
1318 if ((count = c1->c_cwidth -
1319 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1321 putstr (" ", c1->c_flags);
1324 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1328 && !(c2->c_flags & HDROUTPUT)
1329 && !(c2->c_flags & NOCOMPONENT)) {
1330 if (c1->c_flags & UPPERCASE)
1331 for (cp = c2->c_name; *cp; cp++)
1333 *cp = toupper (*cp);
1334 putstr (c2->c_name, c1->c_flags);
1335 putstr (": ", c1->c_flags);
1336 if (!(c1->c_flags & SPLIT))
1337 c2->c_flags |= HDROUTPUT;
1340 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1342 putstr (" ", c1->c_flags);
1344 if (c1->c_flags & UPPERCASE)
1345 for (cp = c2->c_text; *cp; cp++)
1347 *cp = toupper (*cp);
1351 if (flag == TWOCOMP)
1352 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1353 : (int) strlen (c2->c_name) + 2;
1355 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1356 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1358 count += c1->c_offset;
1360 if ((cp = oneline (c2->c_text, c1->c_flags)))
1361 putstr(cp, c1->c_flags);
1363 putstr ("\n", c1->c_flags);
1364 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1366 if (flag == BODYCOMP
1367 && !(c1->c_flags & NOCOMPONENT))
1368 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1370 putstr (cp, c1->c_flags);
1372 putstr ("\n", c1->c_flags);
1374 if (flag == BODYCOMP && term == '\n')
1375 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1380 oneline (char *stuff, long flags)
1388 return (onelp = NULL);
1392 if (flags & COMPRESS) {
1393 for (spc = 1, cp = ret; *onelp; onelp++)
1394 if (isspace (*onelp)) {
1395 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1414 while (*onelp && *onelp != '\n')
1416 if (*onelp == '\n') {
1420 if (flags & LEFTADJUST)
1421 while (*ret == ' ' || *ret == '\t')
1424 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1432 putstr (char *string, long flags)
1434 if (!column && lm > 0) {
1437 putch ('\t', flags);
1447 putch (*string++, flags);
1452 putch (char ch, long flags)
1465 if (ontty != ISTTY || row != global.c_length)
1467 if (global.c_flags & BELL)
1471 read (fileno (stdout), buf, sizeof(buf));
1472 if (strchr(buf, '\n')) {
1473 if (global.c_flags & CLEARSCR)
1478 row = global.c_length / 3;
1497 * If we are forwarding this message, and the first
1498 * column contains a dash, then add a dash and a space.
1500 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1509 if (column >= wid && (flags & NOWRAP) == 0) {
1510 putch ('\n', flags);
1513 putstr (ovtxt ? ovtxt : "", flags);
1529 longjmp (env, DONE);
1554 face_format (struct mcomp *c1)
1557 struct mailname *mp;
1559 if ((cp = c1->c_text) == NULL)
1562 if ((cp = getname (cp))) {
1563 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1565 if ((h = mp->m_host) == NULL)
1567 if ((o = OfficialName (h)))
1569 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1572 while ((cp = getname (cp)))
1579 * faceproc is two elements defining the image agent's location:
1584 #include <sys/socket.h>
1585 #include <netinet/in.h>
1587 #include <arpa/inet.h>
1590 doface (struct mcomp *c1)
1593 static int inited = OK;
1594 static struct sockaddr_storage ss;
1595 static socklen_t socklen;
1596 static int socktype;
1597 static int protocol;
1601 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1602 struct addrinfo hints, *res;
1604 if (ap[0] == NULL || ap[1] == NULL) {
1607 return (inited = NOTOK);
1610 memset(&hints, 0, sizeof(hints));
1611 #ifdef AI_ADDRCONFIG
1612 hints.ai_flags = AI_ADDRCONFIG;
1614 hints.ai_family = PF_UNSPEC;
1615 hints.ai_socktype = SOCK_DGRAM;
1617 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1620 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1621 socklen = res->ai_addrlen;
1622 socktype = res->ai_socktype;
1623 protocol = res->ai_protocol;
1628 if (inited == NOTOK)
1631 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1634 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1635 (struct sockaddr *) &ss, socklen);
1639 return (result != NOTOK ? OK : NOTOK);
1644 * This version doesn't use sockets
1649 doface (struct mcomp *c1)
1653 int result, pdi[2], pdo[2];
1655 char buffer[BUFSIZ], *vec[10];
1657 if (pipe (pdi) == NOTOK)
1659 if (pipe (pdo) == NOTOK) {
1665 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1670 /* oops... fork error */
1675 SIGNAL (SIGINT, SIG_IGN);
1676 SIGNAL (SIGQUIT, SIG_IGN);
1677 if (pdi[0] != fileno (stdin)) {
1678 dup2 (pdi[0], fileno (stdin));
1683 if (pdo[1] != fileno (stdout)) {
1684 dup2 (pdo[1], fileno (stdout));
1688 vec[vecp++] = r1bindex (faceproc, '/');
1690 if (sleepsw != NOTOK) {
1692 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1693 vec[vecp++] = buffer;
1696 execvp (faceproc, vec);
1697 fprintf (stderr, "unable to exec ");
1699 _exit (-1); /* NOTREACHED */
1702 /* parent process */
1704 i = strlen (c1->c_text);
1705 if (write (pdi[1], c1->c_text, i) != i)
1706 adios ("pipe", "error writing to");
1707 free (c1->c_text), c1->c_text = NULL;
1713 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1717 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1718 memcpy(dp + len, buffer, i);
1722 cp = mh_xmalloc ((unsigned) i);
1723 memcpy(cp, buffer, i);
1727 for (bp = buffer + i - 1; bp >= buffer; bp--)
1728 if (!isascii (*bp) || iscntrl (*bp)) {
1735 /* no waiting for child... */
1737 if (result == OK) { /* binary */
1738 if (write (1, cp, len) != len)
1739 adios ("writing", "error");
1743 if ((c1->c_text = cp) == NULL)
1750 #endif /* COMMENTED OUT */
1754 mhlsbr (int argc, char **argv, FILE *(*action)())
1756 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1760 switch (m_setjmp (mhlenv)) {
1763 sleepsw = 0; /* XXX */
1764 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1767 mhl_action = action;
1770 * If signal is at default action, then start ignoring
1771 * it, else let it set to its current action.
1773 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1774 SIGNAL (SIGINT, istat);
1775 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1776 SIGNAL (SIGQUIT, qstat);
1777 pstat = SIGNAL (SIGPIPE, pipeser);
1778 mhl (argc, argv); /* FALL THROUGH! */
1781 SIGNAL (SIGINT, istat);
1782 SIGNAL (SIGQUIT, qstat);
1783 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1786 SIGNAL (SIGPIPE, pstat);
1788 if (holder.c_text) {
1789 free (holder.c_text);
1790 holder.c_text = NULL;
1792 free_queue (&msghd, &msgtl);
1793 for (c1 = fmthd; c1; c1 = c1->c_next)
1794 c1->c_flags &= ~HDROUTPUT;
1803 mhladios (char *what, char *fmt, ...)
1808 advertise (what, NULL, fmt, ap);
1815 mhldone (int status)
1819 longjmp (mhlenv, DONE);
1825 static int m_pid = NOTOK;
1826 static int sd = NOTOK;
1829 m_popen (char *name)
1833 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1834 adios ("standard output", "unable to dup()");
1836 if (pipe (pd) == NOTOK)
1837 adios ("pipe", "unable to");
1839 switch (m_pid = vfork()) {
1841 adios ("fork", "unable to");
1844 SIGNAL (SIGINT, SIG_DFL);
1845 SIGNAL (SIGQUIT, SIG_DFL);
1848 if (pd[0] != fileno (stdin)) {
1849 dup2 (pd[0], fileno (stdin));
1852 execlp (name, r1bindex (name, '/'), NULL);
1853 fprintf (stderr, "unable to exec ");
1859 if (pd[1] != fileno (stdout)) {
1860 dup2 (pd[1], fileno (stdout));
1875 if (dup2 (sd, fileno (stdout)) == NOTOK)
1876 adios ("standard output", "unable to dup2()");
1885 pidwait (m_pid, OK);
1891 * Compile a format string and add it to the list of arguments used by
1894 * This deserves some explanation. Here's the deal:
1896 * We want to keep track of components used as arguments by formatproc,
1897 * but the hash table is reset every time fmt_compile is called. So we
1898 * iterate through the function list looking for things that use components
1899 * and save the name. And because we might get the same components used
1900 * by different arguments we need to keep track to every reference of
1901 * every component so we can add them when the message is processed. So
1902 * we compile the argument string now (to get the components we use) and
1903 * save them for later.
1907 compileargs (struct mcomp *c1, char *nfs)
1910 struct arglist *args;
1913 i = fmt_compile(nfs, &fmt);
1915 args = (struct arglist *) mh_xmalloc(sizeof(struct arglist));
1918 adios (NULL, "Unable to allocate formatproc args storage");
1921 args->a_nfs = format_string;
1922 args->a_next = NULL;
1924 format_string = NULL;
1927 c1->c_f_tail->a_next = args;
1929 c1->c_f_tail = args;
1932 c1->c_f_args = args;
1938 * If wantcomp ever changes size, we need to change the size
1939 * of mhlcomp as well
1942 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
1946 for (c = mhlcomp[i]; c->c_next != NULL; c = c->c_next)
1948 c->c_next = wantcomp[i];
1950 mhlcomp[i] = wantcomp[i];
1958 * Check to see if we are interested in a component. If we are, save
1963 checkcomp(char *name, char *buf)
1967 int bucket = CHASH(name);
1970 if ((c = mhlcomp[bucket])) {
1972 if (mh_strcasecmp(name, c->c_name) == 0) {
1975 i = strlen(c->c_text = strdup(buf)) - 1;
1976 if (c->c_text[i] == '\n')
1977 c->c_text[i] = '\0';
1979 i = strlen(cp = c->c_text) - 1;
1980 if (cp[i] == '\n') {
1981 if (c->c_type & CT_ADDR) {
1983 cp = add (",\n\t", cp);
1985 cp = add ("\t", cp);
1988 c->c_text = add (buf, cp);
1991 } while ((c = c->c_next));
1994 return found ? bucket : -1;
1998 * Add text to an existing component
2002 addcomp(int bucket, char *name, char *buf)
2007 c = mhlcomp[bucket];
2009 if (mh_strcasecmp(name, c->c_name) == 0)
2010 c->c_text = add (buf, c->c_text);
2011 } while ((c = c->c_next));
2016 * Free up saved component structures
2022 struct comp *c1, *c2;
2025 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2026 if ((c1 = mhlcomp[i]))
2027 for (; c1; c1 = c2) {
2037 * Just free up the component text.
2046 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2047 if ((c1 = mhlcomp[i]))
2048 for (; c1; c1 = c1->c_next) {
2058 * Filter the body of a message through a specified format program
2062 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
2064 struct mcomp holder;
2066 int fdinput[2], fdoutput[2], waitstat;
2068 pid_t writerpid, filterpid;
2071 * Create pipes so we can communicate with our filter process.
2074 if (pipe(fdinput) < 0) {
2075 adios(NULL, "Unable to create input pipe");
2078 if (pipe(fdoutput) < 0) {
2079 adios(NULL, "Unable to create output pipe");
2083 * Here's what we're doing to do.
2085 * - Fork ourselves and start writing data to the write side of the
2086 * input pipe (fdinput[1]).
2088 * - Fork and exec our filter program. We set the standard input of
2089 * our filter program to be the read side of our input pipe (fdinput[0]).
2090 * Standard output is set to the write side of our output pipe
2093 * - We read from the read side of the output pipe (fdoutput[0]).
2095 * We're forking because that's the simplest way to prevent any deadlocks.
2096 * (without doing something like switching to non-blocking I/O and using
2097 * select or poll, and I'm not interested in doing that).
2100 switch (writerpid = fork()) {
2103 * Our child process - just write to the filter input (fdinput[1]).
2104 * Close all other descriptors that we don't need.
2112 * Call m_getfld() until we're no longer in the BODY state
2115 while (state == BODY) {
2116 write(fdinput[1], buf, strlen(buf));
2117 state = m_getfld(state, name, buf, bufsz, fp);
2121 * We should be done; time to exit.
2126 * Make sure we call _exit(), otherwise we may flush out the stdio
2127 * buffers that we have duplicated from the parent.
2132 adios(NULL, "Unable to fork for filter writer process");
2137 * Fork and exec() our filter program, after redirecting standard in
2138 * and standard out appropriately.
2141 switch (filterpid = fork()) {
2148 * Allocate an argument array for us
2151 args = (char **) mh_xmalloc((c1->c_nargs + 2) * sizeof(char *));
2152 args[0] = formatproc;
2153 args[c1->c_nargs + 1] = NULL;
2161 * Pull out each argument and scan them.
2164 for (a = c1->c_f_args, i = 1; a != NULL; a = a->a_next, i++) {
2165 args[i] = mh_xmalloc(BUFSIZ);
2166 fmt_scan(a->a_fmt, args[i], BUFSIZ, dat);
2168 * fmt_scan likes to put a trailing newline at the end of the
2169 * format string. If we have one, get rid of it.
2171 s = strlen(args[i]);
2172 if (args[i][s - 1] == '\n')
2173 args[i][s - 1] = '\0';
2176 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
2177 adios("formatproc", "Unable to dup2() standard input");
2179 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
2180 adios("formatproc", "Unable to dup2() standard output");
2184 * Close everything (especially the old input and output
2185 * descriptors, since they've been dup'd to stdin and stdout),
2186 * and exec the formatproc.
2194 execvp(formatproc, args);
2196 adios(formatproc, "Unable to execute filter");
2201 adios(NULL, "Unable to fork format program");
2205 * Close everything except our reader (fdoutput[0]);
2213 * As we read in this data, send it to putcomp
2216 holder.c_text = buf;
2218 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
2220 putcomp(c1, &holder, BODYCOMP);
2224 adios(NULL, "reading from formatproc");
2228 * See if we got any errors along the way. I'm a little leery of calling
2229 * waitpid() without WNOHANG, but it seems to be the most correct solution.
2232 if (waitpid(filterpid, &waitstat, 0) < 0) {
2233 if (errno != ECHILD) {
2234 adios("filterproc", "Unable to determine status");
2237 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2238 pidstatus(waitstat, stderr, "filterproc");
2242 if (waitpid(writerpid, &waitstat, 0) < 0) {
2243 if (errno != ECHILD) {
2244 adios("writer process", "Unable to determine status");
2248 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2249 pidstatus(waitstat, stderr, "writer process");