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>
22 * for a component containing addresses, ADDRFMT, if COMPRESS is also
23 * set, then addresses get split wrong (not at the spaces between commas).
24 * To fix this correctly, putstr() should know about "atomic" strings that
25 * must NOT be broken across lines. That's too difficult for right now
26 * (it turns out that there are a number of degernate cases), so in
27 * oneline(), instead of
29 * (*onelp == '\n' && !onelp[1])
31 * being a terminating condition,
33 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
35 * is used instead. This cuts the line prematurely, and gives us a much
36 * better chance of getting things right.
45 static struct swit mhlswitches[] = {
55 { "faceproc program", 0 },
59 { "folder +folder", 0 },
61 { "form formfile", 0 },
63 { "moreproc program", 0 },
67 { "length lines", 0 },
69 { "width columns", 0 },
71 { "sleep seconds", 0 },
73 { "dashstuffing", -12 }, /* interface from forw */
74 #define NBITSTUFFSW 14
75 { "nodashstuffing", -14 }, /* interface from forw */
81 { "forward", -7 }, /* interface from forw */
83 { "forwall", -7 }, /* interface from forw */
85 { "digest list", -6 },
87 { "volume number", -6 },
89 { "issue number", -5 },
95 #define NOCOMPONENT 0x000001 /* don't show component name */
96 #define UPPERCASE 0x000002 /* display in all upper case */
97 #define CENTER 0x000004 /* center line */
98 #define CLEARTEXT 0x000008 /* cleartext */
99 #define EXTRA 0x000010 /* an "extra" component */
100 #define HDROUTPUT 0x000020 /* already output */
101 #define CLEARSCR 0x000040 /* clear screen */
102 #define LEFTADJUST 0x000080 /* left justify multiple lines */
103 #define COMPRESS 0x000100 /* compress text */
104 #define ADDRFMT 0x000200 /* contains addresses */
105 #define BELL 0x000400 /* sound bell at EOP */
106 #define DATEFMT 0x000800 /* contains dates */
107 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
108 #define INIT 0x002000 /* initialize component */
109 #define FACEFMT 0x004000 /* contains face */
110 #define FACEDFLT 0x008000 /* default for face */
111 #define SPLIT 0x010000 /* split headers (don't concatenate) */
112 #define NONEWLINE 0x020000 /* don't write trailing newline */
113 #define NOWRAP 0x040000 /* Don't wrap lines ever */
114 #define FMTFILTER 0x080000 /* Filter through format filter */
115 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
116 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
119 char *c_name; /* component name */
120 char *c_text; /* component text */
121 char *c_ovtxt; /* text overflow indicator */
122 char *c_nfs; /* iff FORMAT */
123 struct format *c_fmt; /* .. */
124 char *c_face; /* face designator */
125 int c_offset; /* left margin indentation */
126 int c_ovoff; /* overflow indentation */
127 int c_width; /* width of field */
128 int c_cwidth; /* width of component */
129 int c_length; /* length in lines */
131 struct mcomp *c_next;
134 static struct mcomp *msghd = NULL;
135 static struct mcomp *msgtl = NULL;
136 static struct mcomp *fmthd = NULL;
137 static struct mcomp *fmttl = NULL;
139 static struct mcomp global = {
140 NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL
143 static struct mcomp holder = {
144 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL
152 static struct pair pairs[] = {
154 { "From", ADDRFMT|FACEDFLT },
155 { "Sender", ADDRFMT },
156 { "Reply-To", ADDRFMT },
160 { "Resent-Date", DATEFMT },
161 { "Resent-From", ADDRFMT },
162 { "Resent-Sender", ADDRFMT },
163 { "Resent-Reply-To", ADDRFMT },
164 { "Resent-To", ADDRFMT },
165 { "Resent-cc", ADDRFMT },
166 { "Resent-Bcc", ADDRFMT },
177 static struct triple triples[] = {
178 { "nocomponent", NOCOMPONENT, 0 },
179 { "uppercase", UPPERCASE, 0 },
180 { "nouppercase", 0, UPPERCASE },
181 { "center", CENTER, 0 },
182 { "nocenter", 0, CENTER },
183 { "clearscreen", CLEARSCR, 0 },
184 { "noclearscreen", 0, CLEARSCR },
185 { "noclear", 0, CLEARSCR },
186 { "leftadjust", LEFTADJUST, 0 },
187 { "noleftadjust", 0, LEFTADJUST },
188 { "compress", COMPRESS, 0 },
189 { "nocompress", 0, COMPRESS },
190 { "split", SPLIT, 0 },
191 { "nosplit", 0, SPLIT },
192 { "addrfield", ADDRFMT, DATEFMT },
194 { "nobell", 0, BELL },
195 { "datefield", DATEFMT, ADDRFMT },
196 { "newline", 0, NONEWLINE },
197 { "nonewline", NONEWLINE, 0 },
198 { "wrap", 0, NOWRAP },
199 { "nowrap", NOWRAP, 0 },
200 { "format", FMTFILTER, 0 },
201 { "noformat", 0, FMTFILTER },
206 static int bellflg = 0;
207 static int clearflg = 0;
208 static int dashstuff = 0;
209 static int dobody = 1;
210 static int forwflg = 0;
211 static int forwall = 0;
213 static int sleepsw = NOTOK;
215 static char *digest = NULL;
216 static int volume = 0;
217 static int issue = 0;
219 static int exitstat = 0;
220 static int mhldebug = 0;
225 static int ontty = NOTTY;
228 static unsigned int column;
234 static unsigned int wid;
238 static unsigned char *onelp;
242 static int num_ignores = 0;
243 static char *ignores[MAXARGS];
246 static jmp_buf mhlenv;
248 static char delim3[] = /* from forw.c */
249 "\n----------------------------------------------------------------------\n\n";
250 static char delim4[] = "\n------------------------------\n\n";
252 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
256 * Redefine a couple of functions.
257 * These are undefined later in the code.
259 #define adios mhladios
265 static void mhl_format (char *, int, int);
266 static int evalvar (struct mcomp *);
267 static int ptoi (char *, int *);
268 static int ptos (char *, char **);
269 static char *parse (void);
270 static void process (char *, char *, int, int);
271 static void mhlfile (FILE *, char *, int, int);
272 static int mcomp_flags (char *);
273 static char *mcomp_add (long, char *, char *);
274 static void mcomp_format (struct mcomp *, struct mcomp *);
275 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
276 static void free_queue (struct mcomp **, struct mcomp **);
277 static void putcomp (struct mcomp *, struct mcomp *, int);
278 static char *oneline (char *, long);
279 static void putstr (char *, long);
280 static void putch (char, long);
281 static void intrser (int);
282 static void pipeser (int);
283 static void quitser (int);
284 static void face_format (struct mcomp *);
285 static int doface (struct mcomp *);
286 static void mhladios (char *, char *, ...);
287 static void mhldone (int);
288 static void m_popen (char *);
289 static void filterbody (struct mcomp *, char *, int, int, FILE *);
291 int mhl (int, char **);
292 int mhlsbr (int, char **, FILE *(*)());
293 void m_pclose (void);
295 void clear_screen (void); /* from termsbr.c */
296 int SOprintf (char *, ...); /* from termsbr.c */
297 int sc_width (void); /* from termsbr.c */
298 int sc_length (void); /* from termsbr.c */
299 int sc_hardcopy (void); /* from termsbr.c */
303 mhl (int argc, char **argv)
305 int length = 0, nomore = 0;
306 int i, width = 0, vecp = 0;
307 char *cp, *folder = NULL, *form = NULL;
308 char buf[BUFSIZ], *files[MAXARGS];
309 char **argp, **arguments;
311 invo_name = r1bindex (argv[0], '/');
313 /* read user profile/context */
316 arguments = getarguments (invo_name, argc, argv, 1);
319 if ((cp = getenv ("MHLDEBUG")) && *cp)
322 if ((cp = getenv ("FACEPROC")))
325 while ((cp = *argp++)) {
327 switch (smatch (++cp, mhlswitches)) {
329 ambigsw (cp, mhlswitches);
332 adios (NULL, "-%s unknown\n", cp);
335 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
336 print_help (buf, mhlswitches, 1);
339 print_version(invo_name);
357 if (!(folder = *argp++) || *folder == '-')
358 adios (NULL, "missing argument to %s", argp[-2]);
361 if (!(form = *argp++) || *form == '-')
362 adios (NULL, "missing argument to %s", argp[-2]);
366 if (!(faceproc = *argp++) || *faceproc == '-')
367 adios (NULL, "missing argument to %s", argp[-2]);
373 if (!(cp = *argp++) || *cp == '-')
374 adios (NULL, "missing argument to %s", argp[-2]);
375 sleepsw = atoi (cp);/* ZERO ok! */
379 if (!(moreproc = *argp++) || *moreproc == '-')
380 adios (NULL, "missing argument to %s", argp[-2]);
387 if (!(cp = *argp++) || *cp == '-')
388 adios (NULL, "missing argument to %s", argp[-2]);
389 if ((length = atoi (cp)) < 1)
390 adios (NULL, "bad argument %s %s", argp[-2], cp);
393 if (!(cp = *argp++) || *cp == '-')
394 adios (NULL, "missing argument to %s", argp[-2]);
395 if ((width = atoi (cp)) < 1)
396 adios (NULL, "bad argument %s %s", argp[-2], cp);
400 if (!(digest = *argp++) || *digest == '-')
401 adios (NULL, "missing argument to %s", argp[-2]);
404 if (!(cp = *argp++) || *cp == '-')
405 adios (NULL, "missing argument to %s", argp[-2]);
406 if ((issue = atoi (cp)) < 1)
407 adios (NULL, "bad argument %s %s", argp[-2], cp);
410 if (!(cp = *argp++) || *cp == '-')
411 adios (NULL, "missing argument to %s", argp[-2]);
412 if ((volume = atoi (cp)) < 1)
413 adios (NULL, "bad argument %s %s", argp[-2], cp);
417 forwall++; /* fall */
420 clearflg = -1;/* XXX */
424 dashstuff = 1; /* trinary logic */
427 dashstuff = -1; /* trinary logic */
439 folder = getenv ("mhfolder");
441 if (isatty (fileno (stdout))) {
442 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
444 SIGNAL (SIGINT, SIG_IGN);
445 SIGNAL2 (SIGQUIT, quitser);
447 SIGNAL2 (SIGPIPE, pipeser);
451 SIGNAL (SIGINT, SIG_IGN);
452 SIGNAL2 (SIGQUIT, quitser);
459 mhl_format (form ? form : mhlformat, length, width);
462 process (folder, NULL, 1, vecp = 1);
464 for (i = 0; i < vecp; i++)
465 process (folder, files[i], i + 1, vecp);
470 printf ("%s", delim4);
472 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
474 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
475 digest, volume, issue);
478 for (cp = buf + i; i > 1; i--)
485 printf ("\n------- End of Forwarded Message%s\n\n",
486 vecp > 1 ? "s" : "");
491 adios("output", "error writing");
494 if (clearflg > 0 && ontty == NOTTY)
505 mhl_format (char *file, int length, int width)
509 char *ap, buffer[BUFSIZ], name[NAMESZ];
513 static dev_t dev = 0;
514 static ino_t ino = 0;
515 static time_t mtime = 0;
518 if (stat (etcpath (file), &st) != NOTOK
519 && mtime == st.st_mtime
524 free_queue (&fmthd, &fmttl);
527 if ((fp = fopen (etcpath (file), "r")) == NULL)
528 adios (file, "unable to open format file");
530 if (fstat (fileno (fp), &st) != NOTOK) {
536 global.c_ovtxt = global.c_nfs = NULL;
540 if ((i = sc_width ()) > 5)
542 global.c_cwidth = -1;
543 if ((i = sc_length ()) > 5)
544 global.c_length = i - 1;
545 global.c_flags = BELL; /* BELL is default */
546 *(ip = ignores) = NULL;
548 while (vfgets (fp, &ap) == OK) {
553 if ((cp = strchr(bp, '\n')))
557 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
562 strncpy (name, parse(), sizeof(name));
568 * Split this list of fields to ignore, and copy
569 * it to the end of the current "ignores" list.
571 if (!mh_strcasecmp (name, "ignores")) {
572 char **tmparray, **p;
575 /* split the fields */
576 tmparray = brkstring (getcpy (++parptr), ",", NULL);
578 /* count number of fields split */
583 /* copy pointers to split fields to ignores array */
584 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
590 if (evalvar (&global))
591 adios (NULL, "format file syntax error: %s", bp);
598 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
599 while (*parptr == ':' || *parptr == ',') {
602 adios (NULL, "format file syntax error: %s", bp);
604 if (!c1->c_nfs && global.c_nfs) {
605 if (c1->c_flags & DATEFMT) {
606 if (global.c_flags & DATEFMT)
607 c1->c_nfs = getcpy (global.c_nfs);
610 if (c1->c_flags & ADDRFMT) {
611 if (global.c_flags & ADDRFMT)
612 c1->c_nfs = getcpy (global.c_nfs);
618 adios (NULL, "format file syntax error: %s", bp);
624 for (c1 = fmthd; c1; c1 = c1->c_next) {
625 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
626 c1->c_name, c1->c_text, c1->c_ovtxt);
627 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
628 (unsigned int)(unsigned long) c1->c_nfs,
629 (unsigned int)(unsigned long) c1->c_fmt);
630 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
631 c1->c_offset, c1->c_ovoff, c1->c_width,
632 c1->c_cwidth, c1->c_length);
633 fprintf (stderr, "\tflags=%s\n",
634 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
640 global.c_flags |= CLEARSCR;
643 global.c_flags &= ~CLEARSCR;
646 switch (bellflg) { /* command line may override format file */
648 global.c_flags |= BELL;
651 global.c_flags &= ~BELL;
656 global.c_length = length;
658 global.c_width = width;
659 if (global.c_length < 5)
660 global.c_length = 10000;
661 if (global.c_width < 5)
662 global.c_width = 10000;
667 evalvar (struct mcomp *c1)
669 char *cp, name[NAMESZ];
674 strncpy (name, parse(), sizeof(name));
676 if (!mh_strcasecmp (name, "component")) {
677 if (ptos (name, &c1->c_text))
679 c1->c_flags &= ~NOCOMPONENT;
683 if (!mh_strcasecmp (name, "overflowtext"))
684 return ptos (name, &c1->c_ovtxt);
686 if (!mh_strcasecmp (name, "formatfield")) {
689 if (ptos (name, &cp))
691 nfs = new_fs (NULL, NULL, cp);
692 c1->c_nfs = getcpy (nfs);
693 c1->c_flags |= FORMAT;
697 if (!mh_strcasecmp (name, "decode")) {
700 nfs = new_fs (NULL, NULL, "%(decode{text})");
701 c1->c_nfs = getcpy (nfs);
702 c1->c_flags |= FORMAT;
706 if (!mh_strcasecmp (name, "offset"))
707 return ptoi (name, &c1->c_offset);
708 if (!mh_strcasecmp (name, "overflowoffset"))
709 return ptoi (name, &c1->c_ovoff);
710 if (!mh_strcasecmp (name, "width"))
711 return ptoi (name, &c1->c_width);
712 if (!mh_strcasecmp (name, "compwidth"))
713 return ptoi (name, &c1->c_cwidth);
714 if (!mh_strcasecmp (name, "length"))
715 return ptoi (name, &c1->c_length);
716 if (!mh_strcasecmp (name, "nodashstuffing"))
717 return (dashstuff = -1);
719 for (ap = triples; ap->t_name; ap++)
720 if (!mh_strcasecmp (ap->t_name, name)) {
721 c1->c_flags |= ap->t_on;
722 c1->c_flags &= ~ap->t_off;
731 ptoi (char *name, int *i)
735 if (*parptr++ != '=' || !*(cp = parse ())) {
736 advise (NULL, "missing argument to variable %s", name);
746 ptos (char *name, char **s)
750 if (*parptr++ != '=') {
751 advise (NULL, "missing argument to variable %s", name);
755 if (*parptr != '"') {
757 *parptr && *parptr != ':' && *parptr != ',';
761 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
762 if (*parptr == QUOTE)
769 if ((*parptr = c) == '"')
780 static char result[NAMESZ];
782 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
801 process (char *folder, char *fname, int ofilen, int ofilec)
807 switch (m_setjmp (env)) {
810 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
812 advise (fname, "unable to open");
820 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
822 SIGNAL (SIGINT, intrser);
823 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
827 SIGNAL (SIGINT, SIG_IGN);
828 if (mhl_action == NULL && fp != stdin)
832 free (holder.c_text);
833 holder.c_text = NULL;
835 free_queue (&msghd, &msgtl);
836 for (c1 = fmthd; c1; c1 = c1->c_next)
837 c1->c_flags &= ~HDROUTPUT;
844 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
847 struct mcomp *c1, *c2, *c3;
848 char **ip, name[NAMESZ], buf[BUFSIZ];
852 printf ("%s", ofilen == 1 ? delim3 : delim4);
854 printf ("\n-------");
856 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
858 printf (" Message %d", ofilen);
866 if ((global.c_flags & CLEARSCR))
871 printf (">>> %s\n\n", mname);
876 strncpy (buf, "\n", sizeof(buf));
878 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
881 printf ("Press <return> to list \"%s\"...", mname);
885 read (fileno (stdout), buf, sizeof(buf));
887 if (strchr(buf, '\n')) {
888 if ((global.c_flags & CLEARSCR))
902 printf (">>> %s\n\n", mname);
908 for (state = FLD;;) {
909 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
912 for (ip = ignores; *ip; ip++)
913 if (!mh_strcasecmp (name, *ip)) {
914 while (state == FLDPLUS)
915 state = m_getfld (state, name, buf, sizeof(buf), fp);
921 for (c2 = fmthd; c2; c2 = c2->c_next)
922 if (!mh_strcasecmp (c2->c_name, name))
925 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
926 for (c1 = msghd; c1; c1 = c1->c_next)
927 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
929 mcomp_add (c1->c_flags, buf, c1->c_text);
933 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
934 while (state == FLDPLUS) {
935 state = m_getfld (state, name, buf, sizeof(buf), fp);
936 c1->c_text = add (buf, c1->c_text);
939 c1->c_flags |= EXTRA;
945 for (c1 = fmthd; c1; c1 = c1->c_next) {
946 if (c1->c_flags & CLEARTEXT) {
947 putcomp (c1, c1, ONECOMP);
950 if (!mh_strcasecmp (c1->c_name, "messagename")) {
951 holder.c_text = concat ("(Message ", mname, ")\n",
953 putcomp (c1, &holder, ONECOMP);
954 free (holder.c_text);
955 holder.c_text = NULL;
958 if (!mh_strcasecmp (c1->c_name, "extras")) {
959 for (c2 = msghd; c2; c2 = c2->c_next)
960 if (c2->c_flags & EXTRA)
961 putcomp (c1, c2, TWOCOMP);
964 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
965 if (c1->c_flags & FMTFILTER && state == BODY &&
966 formatproc != NULL) {
967 filterbody(c1, buf, sizeof(buf), state, fp);
969 holder.c_text = mh_xmalloc (sizeof(buf));
970 strncpy (holder.c_text, buf, sizeof(buf));
971 while (state == BODY) {
972 putcomp (c1, &holder, BODYCOMP);
973 state = m_getfld (state, name, holder.c_text,
976 free (holder.c_text);
977 holder.c_text = NULL;
981 for (c2 = msghd; c2; c2 = c2->c_next)
982 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
983 putcomp (c1, c2, ONECOMP);
984 if (!(c1->c_flags & SPLIT))
987 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
988 for (c2 = msghd; c2; c2 = c2->c_next)
989 if (c2->c_flags & FACEDFLT) {
990 if (c2->c_face == NULL)
992 if ((holder.c_text = c2->c_face)) {
993 putcomp (c1, &holder, ONECOMP);
994 holder.c_text = NULL;
1003 advise (NULL, "format error in message %s", mname);
1008 adios (NULL, "getfld() returned %d", state);
1015 mcomp_flags (char *name)
1019 for (ap = pairs; ap->p_name; ap++)
1020 if (!mh_strcasecmp (ap->p_name, name))
1021 return (ap->p_flags);
1028 mcomp_add (long flags, char *s1, char *s2)
1032 if (!(flags & ADDRFMT))
1033 return add (s1, s2);
1035 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1038 return add (s1, add (",\n", s2));
1045 struct pqpair *pq_next;
1050 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1054 char buffer[BUFSIZ], error[BUFSIZ];
1056 struct pqpair *p, *q;
1058 struct mailname *mp;
1065 dat[3] = sizeof(buffer) - 1;
1067 fmt_compile (c1->c_nfs, &c1->c_fmt);
1069 if (!(c1->c_flags & ADDRFMT)) {
1070 FINDCOMP (cptr, "text");
1073 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1077 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1078 /* Don't need to append a newline, dctime() already did */
1079 c2->c_text = getcpy (buffer);
1085 (q = &pq)->pq_next = NULL;
1086 while ((cp = getname (ap))) {
1087 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1088 adios (NULL, "unable to allocate pqpair memory");
1090 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1091 p->pq_text = getcpy (cp);
1092 p->pq_error = getcpy (error);
1094 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1096 if ((h = mp->m_host) == NULL)
1098 if ((o = OfficialName (h)))
1100 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1103 p->pq_text = getcpy (mp->m_text);
1106 q = (q->pq_next = p);
1109 for (p = pq.pq_next; p; p = q) {
1110 FINDCOMP (cptr, "text");
1112 cptr->c_text = p->pq_text;
1113 FINDCOMP (cptr, "error");
1115 cptr->c_text = p->pq_error;
1117 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1120 c2->c_text = add (",\n", c2->c_text);
1121 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1123 c2->c_text = add (buffer, c2->c_text);
1133 c2->c_text = add ("\n", c2->c_text);
1138 static struct mcomp *
1139 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1143 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1144 adios (NULL, "unable to allocate comp memory");
1146 c1->c_flags = flags & ~INIT;
1147 if ((c1->c_name = name ? getcpy (name) : NULL))
1148 c1->c_flags |= mcomp_flags (c1->c_name);
1149 c1->c_text = text ? getcpy (text) : NULL;
1152 c1->c_ovtxt = getcpy (global.c_ovtxt);
1153 c1->c_offset = global.c_offset;
1154 c1->c_ovoff = global. c_ovoff;
1155 c1->c_width = c1->c_length = 0;
1156 c1->c_cwidth = global.c_cwidth;
1157 c1->c_flags |= global.c_flags & GFLAGS;
1162 (*tail)->c_next = c1;
1170 free_queue (struct mcomp **head, struct mcomp **tail)
1172 struct mcomp *c1, *c2;
1174 for (c1 = *head; c1; c1 = c2) {
1185 free ((char *) c1->c_fmt);
1191 *head = *tail = NULL;
1196 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1203 llim = c1->c_length ? c1->c_length : -1;
1204 wid = c1->c_width ? c1->c_width : global.c_width;
1205 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1207 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1209 if (wid < ovoff + strlen (ovtxt) + 5)
1210 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1211 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1214 if (c1->c_flags & CLEARTEXT) {
1215 putstr (c1->c_text, c1->c_flags);
1216 putstr ("\n", c1->c_flags);
1220 if (c1->c_flags & FACEFMT)
1221 switch (doface (c2)) {
1222 case NOTOK: /* error */
1223 case OK: /* async faceproc */
1226 default: /* sync faceproc */
1230 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1231 mcomp_format (c1, c2);
1233 if (c1->c_flags & CENTER) {
1234 count = (c1->c_width ? c1->c_width : global.c_width)
1235 - c1->c_offset - strlen (c2->c_text);
1236 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1237 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1238 lm = c1->c_offset + (count / 2);
1244 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1245 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1246 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1248 *cp = toupper (*cp);
1249 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1250 if (flag != BODYCOMP) {
1251 putstr (": ", c1->c_flags);
1252 if (!(c1->c_flags & SPLIT))
1253 c1->c_flags |= HDROUTPUT;
1256 if ((count = c1->c_cwidth -
1257 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1259 putstr (" ", c1->c_flags);
1262 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1266 && !(c2->c_flags & HDROUTPUT)
1267 && !(c2->c_flags & NOCOMPONENT)) {
1268 if (c1->c_flags & UPPERCASE)
1269 for (cp = c2->c_name; *cp; cp++)
1271 *cp = toupper (*cp);
1272 putstr (c2->c_name, c1->c_flags);
1273 putstr (": ", c1->c_flags);
1274 if (!(c1->c_flags & SPLIT))
1275 c2->c_flags |= HDROUTPUT;
1278 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1280 putstr (" ", c1->c_flags);
1282 if (c1->c_flags & UPPERCASE)
1283 for (cp = c2->c_text; *cp; cp++)
1285 *cp = toupper (*cp);
1289 if (flag == TWOCOMP)
1290 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1291 : (int) strlen (c2->c_name) + 2;
1293 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1294 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1296 count += c1->c_offset;
1298 if ((cp = oneline (c2->c_text, c1->c_flags)))
1299 putstr(cp, c1->c_flags);
1301 putstr ("\n", c1->c_flags);
1302 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1304 if (flag == BODYCOMP
1305 && !(c1->c_flags & NOCOMPONENT))
1306 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1308 putstr (cp, c1->c_flags);
1310 putstr ("\n", c1->c_flags);
1312 if (flag == BODYCOMP && term == '\n')
1313 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1318 oneline (char *stuff, long flags)
1326 return (onelp = NULL);
1330 if (flags & COMPRESS) {
1331 for (spc = 1, cp = ret; *onelp; onelp++)
1332 if (isspace (*onelp)) {
1333 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1352 while (*onelp && *onelp != '\n')
1354 if (*onelp == '\n') {
1358 if (flags & LEFTADJUST)
1359 while (*ret == ' ' || *ret == '\t')
1362 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1370 putstr (char *string, long flags)
1372 if (!column && lm > 0) {
1375 putch ('\t', flags);
1385 putch (*string++, flags);
1390 putch (char ch, long flags)
1403 if (ontty != ISTTY || row != global.c_length)
1405 if (global.c_flags & BELL)
1409 read (fileno (stdout), buf, sizeof(buf));
1410 if (strchr(buf, '\n')) {
1411 if (global.c_flags & CLEARSCR)
1416 row = global.c_length / 3;
1435 * If we are forwarding this message, and the first
1436 * column contains a dash, then add a dash and a space.
1438 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1447 if (column >= wid && (flags & NOWRAP) == 0) {
1448 putch ('\n', flags);
1451 putstr (ovtxt ? ovtxt : "", flags);
1467 longjmp (env, DONE);
1492 face_format (struct mcomp *c1)
1495 struct mailname *mp;
1497 if ((cp = c1->c_text) == NULL)
1500 if ((cp = getname (cp))) {
1501 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1503 if ((h = mp->m_host) == NULL)
1505 if ((o = OfficialName (h)))
1507 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1510 while ((cp = getname (cp)))
1517 * faceproc is two elements defining the image agent's location:
1522 #include <sys/socket.h>
1523 #include <netinet/in.h>
1525 #include <arpa/inet.h>
1528 doface (struct mcomp *c1)
1531 static int inited = OK;
1532 static struct sockaddr_storage ss;
1533 static socklen_t socklen;
1534 static int socktype;
1535 static int protocol;
1539 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1540 struct addrinfo hints, *res;
1542 if (ap[0] == NULL || ap[1] == NULL) {
1545 return (inited = NOTOK);
1548 memset(&hints, 0, sizeof(hints));
1549 #ifdef AI_ADDRCONFIG
1550 hints.ai_flags = AI_ADDRCONFIG;
1552 hints.ai_family = PF_UNSPEC;
1553 hints.ai_socktype = SOCK_DGRAM;
1555 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1558 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1559 socklen = res->ai_addrlen;
1560 socktype = res->ai_socktype;
1561 protocol = res->ai_protocol;
1566 if (inited == NOTOK)
1569 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1572 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1573 (struct sockaddr *) &ss, socklen);
1577 return (result != NOTOK ? OK : NOTOK);
1582 * This version doesn't use sockets
1587 doface (struct mcomp *c1)
1591 int result, pdi[2], pdo[2];
1593 char buffer[BUFSIZ], *vec[10];
1595 if (pipe (pdi) == NOTOK)
1597 if (pipe (pdo) == NOTOK) {
1603 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1608 /* oops... fork error */
1613 SIGNAL (SIGINT, SIG_IGN);
1614 SIGNAL (SIGQUIT, SIG_IGN);
1615 if (pdi[0] != fileno (stdin)) {
1616 dup2 (pdi[0], fileno (stdin));
1621 if (pdo[1] != fileno (stdout)) {
1622 dup2 (pdo[1], fileno (stdout));
1626 vec[vecp++] = r1bindex (faceproc, '/');
1628 if (sleepsw != NOTOK) {
1630 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1631 vec[vecp++] = buffer;
1634 execvp (faceproc, vec);
1635 fprintf (stderr, "unable to exec ");
1637 _exit (-1); /* NOTREACHED */
1640 /* parent process */
1642 i = strlen (c1->c_text);
1643 if (write (pdi[1], c1->c_text, i) != i)
1644 adios ("pipe", "error writing to");
1645 free (c1->c_text), c1->c_text = NULL;
1651 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1655 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1656 memcpy(dp + len, buffer, i);
1660 cp = mh_xmalloc ((unsigned) i);
1661 memcpy(cp, buffer, i);
1665 for (bp = buffer + i - 1; bp >= buffer; bp--)
1666 if (!isascii (*bp) || iscntrl (*bp)) {
1673 /* no waiting for child... */
1675 if (result == OK) { /* binary */
1676 if (write (1, cp, len) != len)
1677 adios ("writing", "error");
1681 if ((c1->c_text = cp) == NULL)
1688 #endif /* COMMENTED OUT */
1692 mhlsbr (int argc, char **argv, FILE *(*action)())
1694 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1698 switch (m_setjmp (mhlenv)) {
1701 sleepsw = 0; /* XXX */
1702 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1705 mhl_action = action;
1708 * If signal is at default action, then start ignoring
1709 * it, else let it set to its current action.
1711 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1712 SIGNAL (SIGINT, istat);
1713 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1714 SIGNAL (SIGQUIT, qstat);
1715 pstat = SIGNAL (SIGPIPE, pipeser);
1716 mhl (argc, argv); /* FALL THROUGH! */
1719 SIGNAL (SIGINT, istat);
1720 SIGNAL (SIGQUIT, qstat);
1721 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1724 SIGNAL (SIGPIPE, pstat);
1726 if (holder.c_text) {
1727 free (holder.c_text);
1728 holder.c_text = NULL;
1730 free_queue (&msghd, &msgtl);
1731 for (c1 = fmthd; c1; c1 = c1->c_next)
1732 c1->c_flags &= ~HDROUTPUT;
1741 mhladios (char *what, char *fmt, ...)
1746 advertise (what, NULL, fmt, ap);
1753 mhldone (int status)
1757 longjmp (mhlenv, DONE);
1763 static int m_pid = NOTOK;
1764 static int sd = NOTOK;
1767 m_popen (char *name)
1771 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1772 adios ("standard output", "unable to dup()");
1774 if (pipe (pd) == NOTOK)
1775 adios ("pipe", "unable to");
1777 switch (m_pid = vfork()) {
1779 adios ("fork", "unable to");
1782 SIGNAL (SIGINT, SIG_DFL);
1783 SIGNAL (SIGQUIT, SIG_DFL);
1786 if (pd[0] != fileno (stdin)) {
1787 dup2 (pd[0], fileno (stdin));
1790 execlp (name, r1bindex (name, '/'), NULL);
1791 fprintf (stderr, "unable to exec ");
1797 if (pd[1] != fileno (stdout)) {
1798 dup2 (pd[1], fileno (stdout));
1813 if (dup2 (sd, fileno (stdout)) == NOTOK)
1814 adios ("standard output", "unable to dup2()");
1823 pidwait (m_pid, OK);
1829 * Filter the body of a message through a specified format program
1833 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
1835 struct mcomp holder;
1837 int fdinput[2], fdoutput[2], waitstat;
1839 pid_t writerpid, filterpid;
1842 * Create pipes so we can communicate with our filter process.
1845 if (pipe(fdinput) < 0) {
1846 adios(NULL, "Unable to create input pipe");
1849 if (pipe(fdoutput) < 0) {
1850 adios(NULL, "Unable to create output pipe");
1854 * Here's what we're doing to do.
1856 * - Fork ourselves and start writing data to the write side of the
1857 * input pipe (fdinput[1]).
1859 * - Fork and exec our filter program. We set the standard input of
1860 * our filter program to be the read side of our input pipe (fdinput[0]).
1861 * Standard output is set to the write side of our output pipe
1864 * - We read from the read side of the output pipe (fdoutput[0]).
1866 * We're forking because that's the simplest way to prevent any deadlocks.
1867 * (without doing something like switching to non-blocking I/O and using
1868 * select or poll, and I'm not interested in doing that).
1871 switch (writerpid = fork()) {
1874 * Our child process - just write to the filter input (fdinput[1]).
1875 * Close all other descriptors that we don't need.
1883 * Call m_getfld() until we're no longer in the BODY state
1886 while (state == BODY) {
1887 write(fdinput[1], buf, strlen(buf));
1888 state = m_getfld(state, name, buf, bufsz, fp);
1892 * We should be done; time to exit.
1899 adios(NULL, "Unable to fork for filter writer process");
1904 * Fork and exec() our filter program, after redirecting standard in
1905 * and standard out appropriately.
1908 switch (filterpid = fork()) {
1910 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
1911 adios("formatproc", "Unable to dup2() standard input");
1913 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
1914 adios("formatproc", "Unable to dup2() standard output");
1918 * Close everything (especially the old input and output
1919 * descriptors, since they've been dup'd to stdin and stdout),
1920 * and exec the formatproc.
1928 execlp(formatproc, formatproc, (char *) NULL);
1930 adios(formatproc, "Unable to execute filter");
1935 adios(NULL, "Unable to fork format program");
1939 * Close everything except our reader (fdoutput[0]);
1947 * As we read in this data, send it to putcomp
1950 holder.c_text = buf;
1952 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
1954 putcomp(c1, &holder, BODYCOMP);
1958 adios(NULL, "reading from formatproc");
1962 * See if we got any errors along the way. I'm a little leery of calling
1963 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1966 if (waitpid(filterpid, &waitstat, 0) < 0) {
1967 if (errno != ECHILD) {
1968 adios("filterproc", "Unable to determine status");
1971 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1972 pidstatus(waitstat, stderr, "filterproc");
1976 if (waitpid(writerpid, &waitstat, 0) < 0) {
1977 if (errno != ECHILD) {
1978 adios("writer process", "Unable to determine status");
1982 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1983 pidstatus(waitstat, stderr, "writer process");