2 * mhlsbr.c -- main routines for nmh message lister
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
20 * for a component containing addresses, ADDRFMT, if COMPRESS is also
21 * set, then addresses get split wrong (not at the spaces between commas).
22 * To fix this correctly, putstr() should know about "atomic" strings that
23 * must NOT be broken across lines. That's too difficult for right now
24 * (it turns out that there are a number of degernate cases), so in
25 * oneline(), instead of
27 * (*onelp == '\n' && !onelp[1])
29 * being a terminating condition,
31 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
33 * is used instead. This cuts the line prematurely, and gives us a much
34 * better chance of getting things right.
43 static struct swit mhlswitches[] = {
53 { "faceproc program", 0 },
57 { "folder +folder", 0 },
59 { "form formfile", 0 },
61 { "moreproc program", 0 },
65 { "length lines", 0 },
67 { "width columns", 0 },
69 { "sleep seconds", 0 },
71 { "dashstuffing", -12 }, /* interface from forw */
72 #define NBITSTUFFSW 14
73 { "nodashstuffing", -14 }, /* interface from forw */
79 { "forward", -7 }, /* interface from forw */
81 { "forwall", -7 }, /* interface from forw */
83 { "digest list", -6 },
85 { "volume number", -6 },
87 { "issue number", -5 },
93 #define NOCOMPONENT 0x000001 /* don't show component name */
94 #define UPPERCASE 0x000002 /* display in all upper case */
95 #define CENTER 0x000004 /* center line */
96 #define CLEARTEXT 0x000008 /* cleartext */
97 #define EXTRA 0x000010 /* an "extra" component */
98 #define HDROUTPUT 0x000020 /* already output */
99 #define CLEARSCR 0x000040 /* clear screen */
100 #define LEFTADJUST 0x000080 /* left justify multiple lines */
101 #define COMPRESS 0x000100 /* compress text */
102 #define ADDRFMT 0x000200 /* contains addresses */
103 #define BELL 0x000400 /* sound bell at EOP */
104 #define DATEFMT 0x000800 /* contains dates */
105 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
106 #define INIT 0x002000 /* initialize component */
107 #define FACEFMT 0x004000 /* contains face */
108 #define FACEDFLT 0x008000 /* default for face */
109 #define SPLIT 0x010000 /* split headers (don't concatenate) */
110 #define NONEWLINE 0x020000 /* don't write trailing newline */
111 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
112 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
115 char *c_name; /* component name */
116 char *c_text; /* component text */
117 char *c_ovtxt; /* text overflow indicator */
118 char *c_nfs; /* iff FORMAT */
119 struct format *c_fmt; /* .. */
120 char *c_face; /* face designator */
121 int c_offset; /* left margin indentation */
122 int c_ovoff; /* overflow indentation */
123 int c_width; /* width of field */
124 int c_cwidth; /* width of component */
125 int c_length; /* length in lines */
127 struct mcomp *c_next;
130 static struct mcomp *msghd = NULL;
131 static struct mcomp *msgtl = NULL;
132 static struct mcomp *fmthd = NULL;
133 static struct mcomp *fmttl = NULL;
135 static struct mcomp global = {
136 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
139 static struct mcomp holder = {
140 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
148 static struct pair pairs[] = {
150 { "From", ADDRFMT|FACEDFLT },
151 { "Sender", ADDRFMT },
152 { "Reply-To", ADDRFMT },
156 { "Resent-Date", DATEFMT },
157 { "Resent-From", ADDRFMT },
158 { "Resent-Sender", ADDRFMT },
159 { "Resent-Reply-To", ADDRFMT },
160 { "Resent-To", ADDRFMT },
161 { "Resent-cc", ADDRFMT },
162 { "Resent-Bcc", ADDRFMT },
173 static struct triple triples[] = {
174 { "nocomponent", NOCOMPONENT, 0 },
175 { "uppercase", UPPERCASE, 0 },
176 { "nouppercase", 0, UPPERCASE },
177 { "center", CENTER, 0 },
178 { "nocenter", 0, CENTER },
179 { "clearscreen", CLEARSCR, 0 },
180 { "noclearscreen", 0, CLEARSCR },
181 { "noclear", 0, CLEARSCR },
182 { "leftadjust", LEFTADJUST, 0 },
183 { "noleftadjust", 0, LEFTADJUST },
184 { "compress", COMPRESS, 0 },
185 { "nocompress", 0, COMPRESS },
186 { "split", SPLIT, 0 },
187 { "nosplit", 0, SPLIT },
188 { "addrfield", ADDRFMT, DATEFMT },
190 { "nobell", 0, BELL },
191 { "datefield", DATEFMT, ADDRFMT },
192 { "newline", 0, NONEWLINE },
193 { "nonewline", NONEWLINE, 0 },
198 static int bellflg = 0;
199 static int clearflg = 0;
200 static int dashstuff = 0;
201 static int dobody = 1;
202 static int forwflg = 0;
203 static int forwall = 0;
205 static int sleepsw = NOTOK;
207 static char *digest = NULL;
208 static int volume = 0;
209 static int issue = 0;
211 static int exitstat = 0;
212 static int mhldebug = 0;
217 static int ontty = NOTTY;
230 static unsigned char *onelp;
234 static int num_ignores = 0;
235 static char *ignores[MAXARGS];
238 static jmp_buf mhlenv;
240 static char delim3[] = /* from forw.c */
241 "\n----------------------------------------------------------------------\n\n";
242 static char delim4[] = "\n------------------------------\n\n";
244 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
248 * Redefine a couple of functions.
249 * These are undefined later in the code.
251 #define adios mhladios
257 static void mhl_format (char *, int, int);
258 static int evalvar (struct mcomp *);
259 static int ptoi (char *, int *);
260 static int ptos (char *, char **);
261 static char *parse (void);
262 static void process (char *, char *, int, int);
263 static void mhlfile (FILE *, char *, int, int);
264 static int mcomp_flags (char *);
265 static char *mcomp_add (long, char *, char *);
266 static void mcomp_format (struct mcomp *, struct mcomp *);
267 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
268 static void free_queue (struct mcomp **, struct mcomp **);
269 static void putcomp (struct mcomp *, struct mcomp *, int);
270 static char *oneline (char *, long);
271 static void putstr (char *);
272 static void putch (char);
273 static RETSIGTYPE intrser (int);
274 static RETSIGTYPE pipeser (int);
275 static RETSIGTYPE quitser (int);
276 static void face_format (struct mcomp *);
277 static int doface (struct mcomp *);
278 static void mhladios (char *, char *, ...);
279 static void mhldone (int);
280 static void m_popen (char *);
282 int mhl (int, char **);
283 int mhlsbr (int, char **, FILE *(*)());
284 void m_pclose (void);
286 void clear_screen (void); /* from termsbr.c */
287 int SOprintf (char *, ...); /* from termsbr.c */
288 int sc_width (void); /* from termsbr.c */
289 int sc_length (void); /* from termsbr.c */
290 int sc_hardcopy (void); /* from termsbr.c */
294 mhl (int argc, char **argv)
296 int length = 0, nomore = 0;
297 int i, width = 0, vecp = 0;
298 char *cp, *folder = NULL, *form = NULL;
299 char buf[BUFSIZ], *files[MAXARGS];
300 char **argp, **arguments;
302 invo_name = r1bindex (argv[0], '/');
304 /* read user profile/context */
307 arguments = getarguments (invo_name, argc, argv, 1);
310 if ((cp = getenv ("MHLDEBUG")) && *cp)
313 if ((cp = getenv ("FACEPROC")))
316 while ((cp = *argp++)) {
318 switch (smatch (++cp, mhlswitches)) {
320 ambigsw (cp, mhlswitches);
323 adios (NULL, "-%s unknown\n", cp);
326 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
327 print_help (buf, mhlswitches, 1);
330 print_version(invo_name);
348 if (!(folder = *argp++) || *folder == '-')
349 adios (NULL, "missing argument to %s", argp[-2]);
352 if (!(form = *argp++) || *form == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
357 if (!(faceproc = *argp++) || *faceproc == '-')
358 adios (NULL, "missing argument to %s", argp[-2]);
364 if (!(cp = *argp++) || *cp == '-')
365 adios (NULL, "missing argument to %s", argp[-2]);
366 sleepsw = atoi (cp);/* ZERO ok! */
370 if (!(moreproc = *argp++) || *moreproc == '-')
371 adios (NULL, "missing argument to %s", argp[-2]);
378 if (!(cp = *argp++) || *cp == '-')
379 adios (NULL, "missing argument to %s", argp[-2]);
380 if ((length = atoi (cp)) < 1)
381 adios (NULL, "bad argument %s %s", argp[-2], cp);
384 if (!(cp = *argp++) || *cp == '-')
385 adios (NULL, "missing argument to %s", argp[-2]);
386 if ((width = atoi (cp)) < 1)
387 adios (NULL, "bad argument %s %s", argp[-2], cp);
391 if (!(digest = *argp++) || *digest == '-')
392 adios (NULL, "missing argument to %s", argp[-2]);
395 if (!(cp = *argp++) || *cp == '-')
396 adios (NULL, "missing argument to %s", argp[-2]);
397 if ((issue = atoi (cp)) < 1)
398 adios (NULL, "bad argument %s %s", argp[-2], cp);
401 if (!(cp = *argp++) || *cp == '-')
402 adios (NULL, "missing argument to %s", argp[-2]);
403 if ((volume = atoi (cp)) < 1)
404 adios (NULL, "bad argument %s %s", argp[-2], cp);
408 forwall++; /* fall */
411 clearflg = -1;/* XXX */
415 dashstuff = 1; /* trinary logic */
418 dashstuff = -1; /* trinary logic */
430 folder = getenv ("mhfolder");
432 if (isatty (fileno (stdout))) {
433 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
435 SIGNAL (SIGINT, SIG_IGN);
436 SIGNAL2 (SIGQUIT, quitser);
438 SIGNAL2 (SIGPIPE, pipeser);
442 SIGNAL (SIGINT, SIG_IGN);
443 SIGNAL2 (SIGQUIT, quitser);
450 mhl_format (form ? form : mhlformat, length, width);
453 process (folder, NULL, 1, vecp = 1);
455 for (i = 0; i < vecp; i++)
456 process (folder, files[i], i + 1, vecp);
461 printf ("%s", delim4);
463 snprintf (buf, sizeof(buf),
464 "End of %s Digest\n", digest);
466 snprintf (buf, sizeof(buf),
467 "End of %s Digest [Volume %d Issue %d]\n",
468 digest, volume, issue);
471 for (cp = buf + i; i > 1; i--)
477 printf ("\n------- End of Forwarded Message%s\n\n",
478 vecp > 1 ? "s" : "");
482 if (ferror(stdout)) {
483 adios("output", "error writing");
486 if (clearflg > 0 && ontty == NOTTY)
497 mhl_format (char *file, int length, int width)
501 char *ap, buffer[BUFSIZ], name[NAMESZ];
505 static dev_t dev = 0;
506 static ino_t ino = 0;
507 static time_t mtime = 0;
510 if (stat (etcpath (file), &st) != NOTOK
511 && mtime == st.st_mtime
516 free_queue (&fmthd, &fmttl);
519 if ((fp = fopen (etcpath (file), "r")) == NULL)
520 adios (file, "unable to open format file");
522 if (fstat (fileno (fp), &st) != NOTOK) {
528 global.c_ovtxt = global.c_nfs = NULL;
532 if ((i = sc_width ()) > 5)
534 global.c_cwidth = -1;
535 if ((i = sc_length ()) > 5)
536 global.c_length = i - 1;
537 global.c_flags = BELL; /* BELL is default */
538 *(ip = ignores) = NULL;
540 while (vfgets (fp, &ap) == OK) {
545 if ((cp = strchr(bp, '\n')))
549 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
554 strncpy (name, parse(), sizeof(name));
560 * Split this list of fields to ignore,
561 * and copy it to the end of the current
564 if (!mh_strcasecmp (name, "ignores")) {
565 char **tmparray, **p;
568 /* split the fields */
569 tmparray = brkstring (getcpy (++parptr), ",", NULL);
571 /* count number of fields split */
576 /* copy pointers to split fields to ignores array */
577 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
583 if (evalvar (&global))
584 adios (NULL, "format file syntax error: %s", bp);
591 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
592 while (*parptr == ':' || *parptr == ',') {
595 adios (NULL, "format file syntax error: %s", bp);
597 if (!c1->c_nfs && global.c_nfs) {
598 if (c1->c_flags & DATEFMT) {
599 if (global.c_flags & DATEFMT)
600 c1->c_nfs = getcpy (global.c_nfs);
603 if (c1->c_flags & ADDRFMT) {
604 if (global.c_flags & ADDRFMT)
605 c1->c_nfs = getcpy (global.c_nfs);
611 adios (NULL, "format file syntax error: %s", bp);
617 for (c1 = fmthd; c1; c1 = c1->c_next) {
618 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
619 c1->c_name, c1->c_text, c1->c_ovtxt);
620 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
621 (unsigned int)(unsigned long) c1->c_nfs,
622 (unsigned int)(unsigned long) c1->c_fmt);
623 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
624 c1->c_offset, c1->c_ovoff, c1->c_width,
625 c1->c_cwidth, c1->c_length);
626 fprintf (stderr, "\tflags=%s\n",
627 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
633 global.c_flags |= CLEARSCR;
636 global.c_flags &= ~CLEARSCR;
639 switch (bellflg) { /* command line may override format file */
641 global.c_flags |= BELL;
644 global.c_flags &= ~BELL;
649 global.c_length = length;
651 global.c_width = width;
652 if (global.c_length < 5)
653 global.c_length = 10000;
654 if (global.c_width < 5)
655 global.c_width = 10000;
660 evalvar (struct mcomp *c1)
662 char *cp, name[NAMESZ];
667 strncpy (name, parse(), sizeof(name));
669 if (!mh_strcasecmp (name, "component")) {
670 if (ptos (name, &c1->c_text))
672 c1->c_flags &= ~NOCOMPONENT;
676 if (!mh_strcasecmp (name, "overflowtext"))
677 return ptos (name, &c1->c_ovtxt);
679 if (!mh_strcasecmp (name, "formatfield")) {
682 if (ptos (name, &cp))
684 nfs = new_fs (NULL, NULL, cp);
685 c1->c_nfs = getcpy (nfs);
686 c1->c_flags |= FORMAT;
690 if (!mh_strcasecmp (name, "decode")) {
693 nfs = new_fs (NULL, NULL, "%(decode{text})");
694 c1->c_nfs = getcpy (nfs);
695 c1->c_flags |= FORMAT;
699 if (!mh_strcasecmp (name, "offset"))
700 return ptoi (name, &c1->c_offset);
701 if (!mh_strcasecmp (name, "overflowoffset"))
702 return ptoi (name, &c1->c_ovoff);
703 if (!mh_strcasecmp (name, "width"))
704 return ptoi (name, &c1->c_width);
705 if (!mh_strcasecmp (name, "compwidth"))
706 return ptoi (name, &c1->c_cwidth);
707 if (!mh_strcasecmp (name, "length"))
708 return ptoi (name, &c1->c_length);
709 if (!mh_strcasecmp (name, "nodashstuffing"))
710 return (dashstuff = -1);
712 for (ap = triples; ap->t_name; ap++)
713 if (!mh_strcasecmp (ap->t_name, name)) {
714 c1->c_flags |= ap->t_on;
715 c1->c_flags &= ~ap->t_off;
724 ptoi (char *name, int *i)
728 if (*parptr++ != '=' || !*(cp = parse ())) {
729 advise (NULL, "missing argument to variable %s", name);
739 ptos (char *name, char **s)
743 if (*parptr++ != '=') {
744 advise (NULL, "missing argument to variable %s", name);
748 if (*parptr != '"') {
750 *parptr && *parptr != ':' && *parptr != ',';
754 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
755 if (*parptr == QUOTE)
762 if ((*parptr = c) == '"')
773 static char result[NAMESZ];
775 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
794 process (char *folder, char *fname, int ofilen, int ofilec)
800 switch (setjmp (env)) {
803 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
805 advise (fname, "unable to open");
813 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
815 SIGNAL (SIGINT, intrser);
816 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
820 SIGNAL (SIGINT, SIG_IGN);
821 if (mhl_action == NULL && fp != stdin)
825 free (holder.c_text);
826 holder.c_text = NULL;
828 free_queue (&msghd, &msgtl);
829 for (c1 = fmthd; c1; c1 = c1->c_next)
830 c1->c_flags &= ~HDROUTPUT;
837 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
840 struct mcomp *c1, *c2, *c3;
841 char **ip, name[NAMESZ], buf[BUFSIZ];
845 printf ("%s", ofilen == 1 ? delim3 : delim4);
847 printf ("\n-------");
849 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
851 printf (" Message %d", ofilen);
859 if ((global.c_flags & CLEARSCR))
864 printf (">>> %s\n\n", mname);
869 strncpy (buf, "\n", sizeof(buf));
871 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
874 printf ("Press <return> to list \"%s\"...", mname);
878 read (fileno (stdout), buf, sizeof(buf));
880 if (strchr(buf, '\n')) {
881 if ((global.c_flags & CLEARSCR))
895 printf (">>> %s\n\n", mname);
901 for (state = FLD;;) {
902 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
905 for (ip = ignores; *ip; ip++)
906 if (!mh_strcasecmp (name, *ip)) {
907 while (state == FLDPLUS)
908 state = m_getfld (state, name, buf, sizeof(buf), fp);
914 for (c2 = fmthd; c2; c2 = c2->c_next)
915 if (!mh_strcasecmp (c2->c_name, name))
918 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
919 for (c1 = msghd; c1; c1 = c1->c_next)
920 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
922 mcomp_add (c1->c_flags, buf, c1->c_text);
926 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
927 while (state == FLDPLUS) {
928 state = m_getfld (state, name, buf, sizeof(buf), fp);
929 c1->c_text = add (buf, c1->c_text);
932 c1->c_flags |= EXTRA;
938 for (c1 = fmthd; c1; c1 = c1->c_next) {
939 if (c1->c_flags & CLEARTEXT) {
940 putcomp (c1, c1, ONECOMP);
943 if (!mh_strcasecmp (c1->c_name, "messagename")) {
944 holder.c_text = concat ("(Message ", mname, ")\n",
946 putcomp (c1, &holder, ONECOMP);
947 free (holder.c_text);
948 holder.c_text = NULL;
951 if (!mh_strcasecmp (c1->c_name, "extras")) {
952 for (c2 = msghd; c2; c2 = c2->c_next)
953 if (c2->c_flags & EXTRA)
954 putcomp (c1, c2, TWOCOMP);
957 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
958 holder.c_text = mh_xmalloc (sizeof(buf));
959 strncpy (holder.c_text, buf, sizeof(buf));
960 while (state == BODY) {
961 putcomp (c1, &holder, BODYCOMP);
962 state = m_getfld (state, name, holder.c_text,
965 free (holder.c_text);
966 holder.c_text = NULL;
969 for (c2 = msghd; c2; c2 = c2->c_next)
970 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
971 putcomp (c1, c2, ONECOMP);
972 if (!(c1->c_flags & SPLIT))
975 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
976 for (c2 = msghd; c2; c2 = c2->c_next)
977 if (c2->c_flags & FACEDFLT) {
978 if (c2->c_face == NULL)
980 if ((holder.c_text = c2->c_face)) {
981 putcomp (c1, &holder, ONECOMP);
982 holder.c_text = NULL;
991 advise (NULL, "format error in message %s", mname);
996 adios (NULL, "getfld() returned %d", state);
1003 mcomp_flags (char *name)
1007 for (ap = pairs; ap->p_name; ap++)
1008 if (!mh_strcasecmp (ap->p_name, name))
1009 return (ap->p_flags);
1016 mcomp_add (long flags, char *s1, char *s2)
1020 if (!(flags & ADDRFMT))
1021 return add (s1, s2);
1023 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1026 return add (s1, add (",\n", s2));
1033 struct pqpair *pq_next;
1038 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1042 char buffer[BUFSIZ], error[BUFSIZ];
1044 struct pqpair *p, *q;
1046 struct mailname *mp;
1053 dat[3] = sizeof(buffer) - 1;
1055 fmt_compile (c1->c_nfs, &c1->c_fmt);
1057 if (!(c1->c_flags & ADDRFMT)) {
1058 FINDCOMP (cptr, "text");
1061 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1065 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1066 /* Don't need to append a newline, dctime() already did */
1067 c2->c_text = getcpy (buffer);
1073 (q = &pq)->pq_next = NULL;
1074 while ((cp = getname (ap))) {
1075 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1076 adios (NULL, "unable to allocate pqpair memory");
1078 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1079 p->pq_text = getcpy (cp);
1080 p->pq_error = getcpy (error);
1082 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1084 if ((h = mp->m_host) == NULL)
1086 if ((o = OfficialName (h)))
1088 c2->c_face = concat ("address ", h, " ",
1091 p->pq_text = getcpy (mp->m_text);
1094 q = (q->pq_next = p);
1097 for (p = pq.pq_next; p; p = q) {
1098 FINDCOMP (cptr, "text");
1100 cptr->c_text = p->pq_text;
1101 FINDCOMP (cptr, "error");
1103 cptr->c_text = p->pq_error;
1105 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1108 c2->c_text = add (",\n", c2->c_text);
1109 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1111 c2->c_text = add (buffer, c2->c_text);
1121 c2->c_text = add ("\n", c2->c_text);
1126 static struct mcomp *
1127 add_queue (struct mcomp **head, struct mcomp **tail, char *name,
1128 char *text, int flags)
1132 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1133 adios (NULL, "unable to allocate comp memory");
1135 c1->c_flags = flags & ~INIT;
1136 if ((c1->c_name = name ? getcpy (name) : NULL))
1137 c1->c_flags |= mcomp_flags (c1->c_name);
1138 c1->c_text = text ? getcpy (text) : NULL;
1141 c1->c_ovtxt = getcpy (global.c_ovtxt);
1142 c1->c_offset = global.c_offset;
1143 c1->c_ovoff = global. c_ovoff;
1144 c1->c_width = c1->c_length = 0;
1145 c1->c_cwidth = global.c_cwidth;
1146 c1->c_flags |= global.c_flags & GFLAGS;
1151 (*tail)->c_next = c1;
1159 free_queue (struct mcomp **head, struct mcomp **tail)
1161 struct mcomp *c1, *c2;
1163 for (c1 = *head; c1; c1 = c2) {
1174 free ((char *) c1->c_fmt);
1180 *head = *tail = NULL;
1185 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1192 llim = c1->c_length ? c1->c_length : -1;
1193 wid = c1->c_width ? c1->c_width : global.c_width;
1194 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1196 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1198 if (wid < ovoff + strlen (ovtxt) + 5)
1199 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1200 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1203 if (c1->c_flags & CLEARTEXT) {
1204 putstr (c1->c_text);
1209 if (c1->c_flags & FACEFMT)
1210 switch (doface (c2)) {
1211 case NOTOK: /* error */
1212 case OK: /* async faceproc */
1215 default: /* sync faceproc */
1219 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1220 mcomp_format (c1, c2);
1222 if (c1->c_flags & CENTER) {
1223 count = (c1->c_width ? c1->c_width : global.c_width)
1224 - c1->c_offset - strlen (c2->c_text);
1225 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1226 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1227 lm = c1->c_offset + (count / 2);
1233 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1234 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1235 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1237 *cp = toupper (*cp);
1238 putstr (c1->c_text ? c1->c_text : c1->c_name);
1239 if (flag != BODYCOMP) {
1241 if (!(c1->c_flags & SPLIT))
1242 c1->c_flags |= HDROUTPUT;
1245 if ((count = c1->c_cwidth -
1246 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1250 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1253 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1254 && !(c2->c_flags & NOCOMPONENT)) {
1255 if (c1->c_flags & UPPERCASE)
1256 for (cp = c2->c_name; *cp; cp++)
1258 *cp = toupper (*cp);
1259 putstr (c2->c_name);
1261 if (!(c1->c_flags & SPLIT))
1262 c2->c_flags |= HDROUTPUT;
1265 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1269 if (c1->c_flags & UPPERCASE)
1270 for (cp = c2->c_text; *cp; cp++)
1272 *cp = toupper (*cp);
1276 if (flag == TWOCOMP)
1277 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1278 : strlen (c2->c_name) + 2;
1280 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1281 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1283 count += c1->c_offset;
1285 if ((cp = oneline (c2->c_text, c1->c_flags)))
1289 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1291 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1292 putstr (c1->c_text ? c1->c_text : c1->c_name);
1298 if (flag == BODYCOMP && term == '\n')
1299 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1304 oneline (char *stuff, long flags)
1312 return (onelp = NULL);
1316 if (flags & COMPRESS) {
1317 for (spc = 1, cp = ret; *onelp; onelp++)
1318 if (isspace (*onelp)) {
1319 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1334 while (*onelp && *onelp != '\n')
1336 if (*onelp == '\n') {
1340 if (flags & LEFTADJUST)
1341 while (*ret == ' ' || *ret == '\t')
1344 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1352 putstr (char *string)
1354 if (!column && lm > 0) {
1384 if (ontty != ISTTY || row != global.c_length)
1386 if (global.c_flags & BELL)
1390 read (fileno (stdout), buf, sizeof(buf));
1391 if (strchr(buf, '\n')) {
1392 if (global.c_flags & CLEARSCR)
1397 row = global.c_length / 3;
1416 * If we are forwarding this message, and the first
1417 * column contains a dash, then add a dash and a space.
1419 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1428 if (column >= wid) {
1432 putstr (ovtxt ? ovtxt : "");
1444 #ifndef RELIABLE_SIGNALS
1445 SIGNAL (SIGINT, intrser);
1450 longjmp (env, DONE);
1457 #ifndef RELIABLE_SIGNALS
1458 SIGNAL (SIGPIPE, pipeser);
1468 #ifndef RELIABLE_SIGNALS
1469 SIGNAL (SIGQUIT, quitser);
1479 face_format (struct mcomp *c1)
1482 struct mailname *mp;
1484 if ((cp = c1->c_text) == NULL)
1487 if ((cp = getname (cp))) {
1488 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1490 if ((h = mp->m_host) == NULL)
1492 if ((o = OfficialName (h)))
1494 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1497 while ((cp = getname (cp)))
1504 * faceproc is two elements defining the image agent's location:
1509 #include <sys/socket.h>
1510 #include <netinet/in.h>
1513 #ifdef HAVE_ARPA_INET_H
1514 # include <arpa/inet.h>
1518 doface (struct mcomp *c1)
1521 static int inited = OK;
1522 static struct sockaddr_storage ss;
1523 static socklen_t socklen;
1524 static int socktype;
1525 static int protocol;
1529 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1530 struct addrinfo hints, *res;
1532 if (ap[0] == NULL || ap[1] == NULL) {
1535 return (inited = NOTOK);
1538 memset(&hints, 0, sizeof(hints));
1539 #ifdef AI_ADDRCONFIG
1540 hints.ai_flags = AI_ADDRCONFIG;
1542 hints.ai_family = PF_UNSPEC;
1543 hints.ai_socktype = SOCK_DGRAM;
1545 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1548 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1549 socklen = res->ai_addrlen;
1550 socktype = res->ai_socktype;
1551 protocol = res->ai_protocol;
1556 if (inited == NOTOK)
1559 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1562 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1563 (struct sockaddr *) &ss, socklen);
1567 return (result != NOTOK ? OK : NOTOK);
1572 * This version doesn't use sockets
1577 doface (struct mcomp *c1)
1581 int result, pdi[2], pdo[2];
1583 char buffer[BUFSIZ], *vec[10];
1585 if (pipe (pdi) == NOTOK)
1587 if (pipe (pdo) == NOTOK) {
1593 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1598 /* oops... fork error */
1603 SIGNAL (SIGINT, SIG_IGN);
1604 SIGNAL (SIGQUIT, SIG_IGN);
1605 if (pdi[0] != fileno (stdin)) {
1606 dup2 (pdi[0], fileno (stdin));
1611 if (pdo[1] != fileno (stdout)) {
1612 dup2 (pdo[1], fileno (stdout));
1616 vec[vecp++] = r1bindex (faceproc, '/');
1618 if (sleepsw != NOTOK) {
1620 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1621 vec[vecp++] = buffer;
1624 execvp (faceproc, vec);
1625 fprintf (stderr, "unable to exec ");
1627 _exit (-1); /* NOTREACHED */
1630 /* parent process */
1632 i = strlen (c1->c_text);
1633 if (write (pdi[1], c1->c_text, i) != i)
1634 adios ("pipe", "error writing to");
1635 free (c1->c_text), c1->c_text = NULL;
1641 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1645 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1646 memcpy(dp + len, buffer, i);
1649 cp = mh_xmalloc ((unsigned) i);
1650 memcpy(cp, buffer, i);
1654 for (bp = buffer + i - 1; bp >= buffer; bp--)
1655 if (!isascii (*bp) || iscntrl (*bp)) {
1662 /* no waiting for child... */
1664 if (result == OK) { /* binary */
1665 if (write (1, cp, len) != len)
1666 adios ("writing", "error");
1670 if ((c1->c_text = cp) == NULL)
1677 #endif /* COMMENTED OUT */
1681 mhlsbr (int argc, char **argv, FILE *(*action)())
1683 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1687 switch (setjmp (mhlenv)) {
1690 sleepsw = 0; /* XXX */
1691 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1694 mhl_action = action;
1697 * If signal is at default action, then start ignoring
1698 * it, else let it set to its current action.
1700 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1701 SIGNAL (SIGINT, istat);
1702 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1703 SIGNAL (SIGQUIT, qstat);
1704 pstat = SIGNAL (SIGPIPE, pipeser);
1705 mhl (argc, argv); /* FALL THROUGH! */
1708 SIGNAL (SIGINT, istat);
1709 SIGNAL (SIGQUIT, qstat);
1710 SIGNAL (SIGPIPE, SIG_IGN); /* should probably change to block instead */
1713 SIGNAL (SIGPIPE, pstat);
1715 if (holder.c_text) {
1716 free (holder.c_text);
1717 holder.c_text = NULL;
1719 free_queue (&msghd, &msgtl);
1720 for (c1 = fmthd; c1; c1 = c1->c_next)
1721 c1->c_flags &= ~HDROUTPUT;
1730 mhladios (char *what, char *fmt, ...)
1735 advertise (what, NULL, fmt, ap);
1742 mhldone (int status)
1746 longjmp (mhlenv, DONE);
1752 static int m_pid = NOTOK;
1753 static int sd = NOTOK;
1756 m_popen (char *name)
1760 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1761 adios ("standard output", "unable to dup()");
1763 if (pipe (pd) == NOTOK)
1764 adios ("pipe", "unable to");
1766 switch (m_pid = vfork ()) {
1768 adios ("fork", "unable to");
1771 SIGNAL (SIGINT, SIG_DFL);
1772 SIGNAL (SIGQUIT, SIG_DFL);
1775 if (pd[0] != fileno (stdin)) {
1776 dup2 (pd[0], fileno (stdin));
1779 execlp (name, r1bindex (name, '/'), NULL);
1780 fprintf (stderr, "unable to exec ");
1786 if (pd[1] != fileno (stdout)) {
1787 dup2 (pd[1], fileno (stdout));
1802 if (dup2 (sd, fileno (stdout)) == NOTOK)
1803 adios ("standard output", "unable to dup2()");
1812 pidwait (m_pid, OK);