3 * mhlsbr.c -- main routines for nmh message lister
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
13 #include <h/signals.h>
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
23 * for a component containing addresses, ADDRFMT, if COMPRESS is also
24 * set, then addresses get split wrong (not at the spaces between commas).
25 * To fix this correctly, putstr() should know about "atomic" strings that
26 * must NOT be broken across lines. That's too difficult for right now
27 * (it turns out that there are a number of degernate cases), so in
28 * oneline(), instead of
30 * (*onelp == '\n' && !onelp[1])
32 * being a terminating condition,
34 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
36 * is used instead. This cuts the line prematurely, and gives us a much
37 * better chance of getting things right.
46 static struct swit mhlswitches[] = {
56 { "faceproc program", 0 },
60 { "folder +folder", 0 },
62 { "form formfile", 0 },
64 { "moreproc program", 0 },
68 { "length lines", 0 },
70 { "width columns", 0 },
72 { "sleep seconds", 0 },
74 { "dashstuffing", -12 }, /* interface from forw */
75 #define NBITSTUFFSW 14
76 { "nodashstuffing", -14 }, /* interface from forw */
82 { "forward", -7 }, /* interface from forw */
84 { "forwall", -7 }, /* interface from forw */
86 { "digest list", -6 },
88 { "volume number", -6 },
90 { "issue number", -5 },
96 #define NOCOMPONENT 0x000001 /* don't show component name */
97 #define UPPERCASE 0x000002 /* display in all upper case */
98 #define CENTER 0x000004 /* center line */
99 #define CLEARTEXT 0x000008 /* cleartext */
100 #define EXTRA 0x000010 /* an "extra" component */
101 #define HDROUTPUT 0x000020 /* already output */
102 #define CLEARSCR 0x000040 /* clear screen */
103 #define LEFTADJUST 0x000080 /* left justify multiple lines */
104 #define COMPRESS 0x000100 /* compress text */
105 #define ADDRFMT 0x000200 /* contains addresses */
106 #define BELL 0x000400 /* sound bell at EOP */
107 #define DATEFMT 0x000800 /* contains dates */
108 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
109 #define INIT 0x002000 /* initialize component */
110 #define FACEFMT 0x004000 /* contains face */
111 #define FACEDFLT 0x008000 /* default for face */
112 #define SPLIT 0x010000 /* split headers (don't concatenate) */
113 #define NONEWLINE 0x020000 /* don't write trailing newline */
114 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
115 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
118 char *c_name; /* component name */
119 char *c_text; /* component text */
120 char *c_ovtxt; /* text overflow indicator */
121 char *c_nfs; /* iff FORMAT */
122 struct format *c_fmt; /* .. */
123 char *c_face; /* face designator */
124 int c_offset; /* left margin indentation */
125 int c_ovoff; /* overflow indentation */
126 int c_width; /* width of field */
127 int c_cwidth; /* width of component */
128 int c_length; /* length in lines */
130 struct mcomp *c_next;
133 static struct mcomp *msghd = NULL;
134 static struct mcomp *msgtl = NULL;
135 static struct mcomp *fmthd = NULL;
136 static struct mcomp *fmttl = NULL;
138 static struct mcomp global = {
139 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
142 static struct mcomp holder = {
143 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
151 static struct pair pairs[] = {
153 { "From", ADDRFMT|FACEDFLT },
154 { "Sender", ADDRFMT },
155 { "Reply-To", ADDRFMT },
159 { "Resent-Date", DATEFMT },
160 { "Resent-From", ADDRFMT },
161 { "Resent-Sender", ADDRFMT },
162 { "Resent-Reply-To", ADDRFMT },
163 { "Resent-To", ADDRFMT },
164 { "Resent-cc", ADDRFMT },
165 { "Resent-Bcc", ADDRFMT },
176 static struct triple triples[] = {
177 { "nocomponent", NOCOMPONENT, 0 },
178 { "uppercase", UPPERCASE, 0 },
179 { "nouppercase", 0, UPPERCASE },
180 { "center", CENTER, 0 },
181 { "nocenter", 0, CENTER },
182 { "clearscreen", CLEARSCR, 0 },
183 { "noclearscreen", 0, CLEARSCR },
184 { "noclear", 0, CLEARSCR },
185 { "leftadjust", LEFTADJUST, 0 },
186 { "noleftadjust", 0, LEFTADJUST },
187 { "compress", COMPRESS, 0 },
188 { "nocompress", 0, COMPRESS },
189 { "split", SPLIT, 0 },
190 { "nosplit", 0, SPLIT },
191 { "addrfield", ADDRFMT, DATEFMT },
193 { "nobell", 0, BELL },
194 { "datefield", DATEFMT, ADDRFMT },
195 { "newline", 0, NONEWLINE },
196 { "nonewline", NONEWLINE, 0 },
201 static int bellflg = 0;
202 static int clearflg = 0;
203 static int dashstuff = 0;
204 static int dobody = 1;
205 static int forwflg = 0;
206 static int forwall = 0;
208 static int sleepsw = NOTOK;
210 static char *digest = NULL;
211 static int volume = 0;
212 static int issue = 0;
214 static int exitstat = 0;
215 static int mhldebug = 0;
220 static int ontty = NOTTY;
233 static unsigned char *onelp;
237 static int num_ignores = 0;
238 static char *ignores[MAXARGS];
241 static jmp_buf mhlenv;
243 static char delim3[] = /* from forw.c */
244 "\n----------------------------------------------------------------------\n\n";
245 static char delim4[] = "\n------------------------------\n\n";
247 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
251 * Redefine a couple of functions.
252 * These are undefined later in the code.
254 #define adios mhladios
260 static void mhl_format (char *, int, int);
261 static int evalvar (struct mcomp *);
262 static int ptoi (char *, int *);
263 static int ptos (char *, char **);
264 static char *parse (void);
265 static void process (char *, char *, int, int);
266 static void mhlfile (FILE *, char *, int, int);
267 static int mcomp_flags (char *);
268 static char *mcomp_add (long, char *, char *);
269 static void mcomp_format (struct mcomp *, struct mcomp *);
270 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
271 static void free_queue (struct mcomp **, struct mcomp **);
272 static void putcomp (struct mcomp *, struct mcomp *, int);
273 static char *oneline (char *, long);
274 static void putstr (char *);
275 static void putch (char);
276 static RETSIGTYPE intrser (int);
277 static RETSIGTYPE pipeser (int);
278 static RETSIGTYPE quitser (int);
279 static void face_format (struct mcomp *);
280 static int doface (struct mcomp *);
281 static void mhladios (char *, char *, ...);
282 static void mhldone (int);
283 static void m_popen (char *);
285 int mhl (int, char **);
286 int mhlsbr (int, char **, FILE *(*)());
287 void m_pclose (void);
289 void clear_screen (void); /* from termsbr.c */
290 int SOprintf (char *, ...); /* from termsbr.c */
291 int sc_width (void); /* from termsbr.c */
292 int sc_length (void); /* from termsbr.c */
293 int sc_hardcopy (void); /* from termsbr.c */
294 struct hostent *gethostbystring ();
298 mhl (int argc, char **argv)
300 int length = 0, nomore = 0;
301 int i, width = 0, vecp = 0;
302 char *cp, *folder = NULL, *form = NULL;
303 char buf[BUFSIZ], *files[MAXARGS];
304 char **argp, **arguments;
306 invo_name = r1bindex (argv[0], '/');
308 /* read user profile/context */
311 arguments = getarguments (invo_name, argc, argv, 1);
314 if ((cp = getenv ("MHLDEBUG")) && *cp)
317 if ((cp = getenv ("FACEPROC")))
320 while ((cp = *argp++)) {
322 switch (smatch (++cp, mhlswitches)) {
324 ambigsw (cp, mhlswitches);
327 adios (NULL, "-%s unknown\n", cp);
330 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
331 print_help (buf, mhlswitches, 1);
334 print_version(invo_name);
352 if (!(folder = *argp++) || *folder == '-')
353 adios (NULL, "missing argument to %s", argp[-2]);
356 if (!(form = *argp++) || *form == '-')
357 adios (NULL, "missing argument to %s", argp[-2]);
361 if (!(faceproc = *argp++) || *faceproc == '-')
362 adios (NULL, "missing argument to %s", argp[-2]);
368 if (!(cp = *argp++) || *cp == '-')
369 adios (NULL, "missing argument to %s", argp[-2]);
370 sleepsw = atoi (cp);/* ZERO ok! */
374 if (!(moreproc = *argp++) || *moreproc == '-')
375 adios (NULL, "missing argument to %s", argp[-2]);
382 if (!(cp = *argp++) || *cp == '-')
383 adios (NULL, "missing argument to %s", argp[-2]);
384 if ((length = atoi (cp)) < 1)
385 adios (NULL, "bad argument %s %s", argp[-2], cp);
388 if (!(cp = *argp++) || *cp == '-')
389 adios (NULL, "missing argument to %s", argp[-2]);
390 if ((width = atoi (cp)) < 1)
391 adios (NULL, "bad argument %s %s", argp[-2], cp);
395 if (!(digest = *argp++) || *digest == '-')
396 adios (NULL, "missing argument to %s", argp[-2]);
399 if (!(cp = *argp++) || *cp == '-')
400 adios (NULL, "missing argument to %s", argp[-2]);
401 if ((issue = atoi (cp)) < 1)
402 adios (NULL, "bad argument %s %s", argp[-2], cp);
405 if (!(cp = *argp++) || *cp == '-')
406 adios (NULL, "missing argument to %s", argp[-2]);
407 if ((volume = atoi (cp)) < 1)
408 adios (NULL, "bad argument %s %s", argp[-2], cp);
412 forwall++; /* fall */
415 clearflg = -1;/* XXX */
419 dashstuff = 1; /* trinary logic */
422 dashstuff = -1; /* trinary logic */
434 folder = getenv ("mhfolder");
436 if (isatty (fileno (stdout))) {
437 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
439 SIGNAL (SIGINT, SIG_IGN);
440 SIGNAL2 (SIGQUIT, quitser);
442 SIGNAL2 (SIGPIPE, pipeser);
446 SIGNAL (SIGINT, SIG_IGN);
447 SIGNAL2 (SIGQUIT, quitser);
454 mhl_format (form ? form : mhlformat, length, width);
457 process (folder, NULL, 1, vecp = 1);
459 for (i = 0; i < vecp; i++)
460 process (folder, files[i], i + 1, vecp);
465 printf ("%s", delim4);
467 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
469 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
470 digest, volume, issue);
473 for (cp = buf + i; i > 1; i--)
480 printf ("\n------- End of Forwarded Message%s\n\n",
481 vecp > 1 ? "s" : "");
486 adios("output", "error writing");
489 if (clearflg > 0 && ontty == NOTTY)
500 mhl_format (char *file, int length, int width)
504 char *ap, buffer[BUFSIZ], name[NAMESZ];
508 static dev_t dev = 0;
509 static ino_t ino = 0;
510 static time_t mtime = 0;
513 if (stat (etcpath (file), &st) != NOTOK
514 && mtime == st.st_mtime
519 free_queue (&fmthd, &fmttl);
522 if ((fp = fopen (etcpath (file), "r")) == NULL)
523 adios (file, "unable to open format file");
525 if (fstat (fileno (fp), &st) != NOTOK) {
531 global.c_ovtxt = global.c_nfs = NULL;
535 if ((i = sc_width ()) > 5)
537 global.c_cwidth = -1;
538 if ((i = sc_length ()) > 5)
539 global.c_length = i - 1;
540 global.c_flags = BELL; /* BELL is default */
541 *(ip = ignores) = NULL;
543 while (vfgets (fp, &ap) == OK) {
548 if ((cp = strchr(bp, '\n')))
552 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
557 strncpy (name, parse(), sizeof(name));
563 * Split this list of fields to ignore, and copy
564 * it to the end of the current "ignores" list.
566 if (!mh_strcasecmp (name, "ignores")) {
567 char **tmparray, **p;
570 /* split the fields */
571 tmparray = brkstring (getcpy (++parptr), ",", NULL);
573 /* count number of fields split */
578 /* copy pointers to split fields to ignores array */
579 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
585 if (evalvar (&global))
586 adios (NULL, "format file syntax error: %s", bp);
593 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
594 while (*parptr == ':' || *parptr == ',') {
597 adios (NULL, "format file syntax error: %s", bp);
599 if (!c1->c_nfs && global.c_nfs) {
600 if (c1->c_flags & DATEFMT) {
601 if (global.c_flags & DATEFMT)
602 c1->c_nfs = getcpy (global.c_nfs);
605 if (c1->c_flags & ADDRFMT) {
606 if (global.c_flags & ADDRFMT)
607 c1->c_nfs = getcpy (global.c_nfs);
613 adios (NULL, "format file syntax error: %s", bp);
619 for (c1 = fmthd; c1; c1 = c1->c_next) {
620 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
621 c1->c_name, c1->c_text, c1->c_ovtxt);
622 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
623 (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
624 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
625 c1->c_offset, c1->c_ovoff, c1->c_width,
626 c1->c_cwidth, c1->c_length);
627 fprintf (stderr, "\tflags=%s\n",
628 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
634 global.c_flags |= CLEARSCR;
637 global.c_flags &= ~CLEARSCR;
640 switch (bellflg) { /* command line may override format file */
642 global.c_flags |= BELL;
645 global.c_flags &= ~BELL;
650 global.c_length = length;
652 global.c_width = width;
653 if (global.c_length < 5)
654 global.c_length = 10000;
655 if (global.c_width < 5)
656 global.c_width = 10000;
661 evalvar (struct mcomp *c1)
663 char *cp, name[NAMESZ];
668 strncpy (name, parse(), sizeof(name));
670 if (!mh_strcasecmp (name, "component")) {
671 if (ptos (name, &c1->c_text))
673 c1->c_flags &= ~NOCOMPONENT;
677 if (!mh_strcasecmp (name, "overflowtext"))
678 return ptos (name, &c1->c_ovtxt);
680 if (!mh_strcasecmp (name, "formatfield")) {
683 if (ptos (name, &cp))
685 nfs = new_fs (NULL, NULL, cp);
686 c1->c_nfs = getcpy (nfs);
687 c1->c_flags |= FORMAT;
691 if (!mh_strcasecmp (name, "decode")) {
694 nfs = new_fs (NULL, NULL, "%(decode{text})");
695 c1->c_nfs = getcpy (nfs);
696 c1->c_flags |= FORMAT;
700 if (!mh_strcasecmp (name, "offset"))
701 return ptoi (name, &c1->c_offset);
702 if (!mh_strcasecmp (name, "overflowoffset"))
703 return ptoi (name, &c1->c_ovoff);
704 if (!mh_strcasecmp (name, "width"))
705 return ptoi (name, &c1->c_width);
706 if (!mh_strcasecmp (name, "compwidth"))
707 return ptoi (name, &c1->c_cwidth);
708 if (!mh_strcasecmp (name, "length"))
709 return ptoi (name, &c1->c_length);
710 if (!mh_strcasecmp (name, "nodashstuffing"))
711 return (dashstuff = -1);
713 for (ap = triples; ap->t_name; ap++)
714 if (!mh_strcasecmp (ap->t_name, name)) {
715 c1->c_flags |= ap->t_on;
716 c1->c_flags &= ~ap->t_off;
725 ptoi (char *name, int *i)
729 if (*parptr++ != '=' || !*(cp = parse ())) {
730 advise (NULL, "missing argument to variable %s", name);
740 ptos (char *name, char **s)
744 if (*parptr++ != '=') {
745 advise (NULL, "missing argument to variable %s", name);
749 if (*parptr != '"') {
751 *parptr && *parptr != ':' && *parptr != ',';
755 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
756 if (*parptr == QUOTE)
763 if ((*parptr = c) == '"')
774 static char result[NAMESZ];
776 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
795 process (char *folder, char *fname, int ofilen, int ofilec)
801 switch (setjmp (env)) {
804 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
806 advise (fname, "unable to open");
814 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
816 SIGNAL (SIGINT, intrser);
817 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
821 SIGNAL (SIGINT, SIG_IGN);
822 if (mhl_action == NULL && fp != stdin)
826 free (holder.c_text);
827 holder.c_text = NULL;
829 free_queue (&msghd, &msgtl);
830 for (c1 = fmthd; c1; c1 = c1->c_next)
831 c1->c_flags &= ~HDROUTPUT;
838 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
841 struct mcomp *c1, *c2, *c3;
842 char **ip, name[NAMESZ], buf[BUFSIZ];
846 printf ("%s", ofilen == 1 ? delim3 : delim4);
848 printf ("\n-------");
850 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
852 printf (" Message %d", ofilen);
860 if ((global.c_flags & CLEARSCR))
865 printf (">>> %s\n\n", mname);
870 strncpy (buf, "\n", sizeof(buf));
872 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
875 printf ("Press <return> to list \"%s\"...", mname);
879 read (fileno (stdout), buf, sizeof(buf));
881 if (strchr(buf, '\n')) {
882 if ((global.c_flags & CLEARSCR))
896 printf (">>> %s\n\n", mname);
902 for (state = FLD;;) {
903 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
906 for (ip = ignores; *ip; ip++)
907 if (!mh_strcasecmp (name, *ip)) {
908 while (state == FLDPLUS)
909 state = m_getfld (state, name, buf, sizeof(buf), fp);
915 for (c2 = fmthd; c2; c2 = c2->c_next)
916 if (!mh_strcasecmp (c2->c_name, name))
919 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
920 for (c1 = msghd; c1; c1 = c1->c_next)
921 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
923 mcomp_add (c1->c_flags, buf, c1->c_text);
927 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
928 while (state == FLDPLUS) {
929 state = m_getfld (state, name, buf, sizeof(buf), fp);
930 c1->c_text = add (buf, c1->c_text);
933 c1->c_flags |= EXTRA;
939 for (c1 = fmthd; c1; c1 = c1->c_next) {
940 if (c1->c_flags & CLEARTEXT) {
941 putcomp (c1, c1, ONECOMP);
944 if (!mh_strcasecmp (c1->c_name, "messagename")) {
945 holder.c_text = concat ("(Message ", mname, ")\n",
947 putcomp (c1, &holder, ONECOMP);
948 free (holder.c_text);
949 holder.c_text = NULL;
952 if (!mh_strcasecmp (c1->c_name, "extras")) {
953 for (c2 = msghd; c2; c2 = c2->c_next)
954 if (c2->c_flags & EXTRA)
955 putcomp (c1, c2, TWOCOMP);
958 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
959 holder.c_text = mh_xmalloc (sizeof(buf));
960 strncpy (holder.c_text, buf, sizeof(buf));
961 while (state == BODY) {
962 putcomp (c1, &holder, BODYCOMP);
963 state = m_getfld (state, name, holder.c_text,
966 free (holder.c_text);
967 holder.c_text = NULL;
970 for (c2 = msghd; c2; c2 = c2->c_next)
971 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
972 putcomp (c1, c2, ONECOMP);
973 if (!(c1->c_flags & SPLIT))
976 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
977 for (c2 = msghd; c2; c2 = c2->c_next)
978 if (c2->c_flags & FACEDFLT) {
979 if (c2->c_face == NULL)
981 if ((holder.c_text = c2->c_face)) {
982 putcomp (c1, &holder, ONECOMP);
983 holder.c_text = NULL;
992 advise (NULL, "format error in message %s", mname);
997 adios (NULL, "getfld() returned %d", state);
1004 mcomp_flags (char *name)
1008 for (ap = pairs; ap->p_name; ap++)
1009 if (!mh_strcasecmp (ap->p_name, name))
1010 return (ap->p_flags);
1017 mcomp_add (long flags, char *s1, char *s2)
1021 if (!(flags & ADDRFMT))
1022 return add (s1, s2);
1024 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1027 return add (s1, add (",\n", s2));
1034 struct pqpair *pq_next;
1039 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1043 char buffer[BUFSIZ], error[BUFSIZ];
1045 struct pqpair *p, *q;
1047 struct mailname *mp;
1054 dat[3] = sizeof(buffer) - 1;
1056 fmt_compile (c1->c_nfs, &c1->c_fmt);
1058 if (!(c1->c_flags & ADDRFMT)) {
1059 FINDCOMP (cptr, "text");
1062 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1066 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1067 /* Don't need to append a newline, dctime() already did */
1068 c2->c_text = getcpy (buffer);
1074 (q = &pq)->pq_next = NULL;
1075 while ((cp = getname (ap))) {
1076 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1077 adios (NULL, "unable to allocate pqpair memory");
1079 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1080 p->pq_text = getcpy (cp);
1081 p->pq_error = getcpy (error);
1083 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1085 if ((h = mp->m_host) == NULL)
1087 if ((o = OfficialName (h)))
1089 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1092 p->pq_text = getcpy (mp->m_text);
1095 q = (q->pq_next = p);
1098 for (p = pq.pq_next; p; p = q) {
1099 FINDCOMP (cptr, "text");
1101 cptr->c_text = p->pq_text;
1102 FINDCOMP (cptr, "error");
1104 cptr->c_text = p->pq_error;
1106 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1109 c2->c_text = add (",\n", c2->c_text);
1110 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1112 c2->c_text = add (buffer, c2->c_text);
1122 c2->c_text = add ("\n", c2->c_text);
1127 static struct mcomp *
1128 add_queue (struct mcomp **head, struct mcomp **tail, char *name, 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)
1251 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1255 && !(c2->c_flags & HDROUTPUT)
1256 && !(c2->c_flags & NOCOMPONENT)) {
1257 if (c1->c_flags & UPPERCASE)
1258 for (cp = c2->c_name; *cp; cp++)
1260 *cp = toupper (*cp);
1261 putstr (c2->c_name);
1263 if (!(c1->c_flags & SPLIT))
1264 c2->c_flags |= HDROUTPUT;
1267 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1271 if (c1->c_flags & UPPERCASE)
1272 for (cp = c2->c_text; *cp; cp++)
1274 *cp = toupper (*cp);
1278 if (flag == TWOCOMP)
1279 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1280 : strlen (c2->c_name) + 2;
1282 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1283 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1285 count += c1->c_offset;
1287 if ((cp = oneline (c2->c_text, c1->c_flags)))
1291 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1293 if (flag == BODYCOMP
1294 && !(c1->c_flags & NOCOMPONENT))
1295 putstr (c1->c_text ? c1->c_text : c1->c_name);
1301 if (flag == BODYCOMP && term == '\n')
1302 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1307 oneline (char *stuff, long flags)
1315 return (onelp = NULL);
1319 if (flags & COMPRESS) {
1320 for (spc = 1, cp = ret; *onelp; onelp++)
1321 if (isspace (*onelp)) {
1322 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1341 while (*onelp && *onelp != '\n')
1343 if (*onelp == '\n') {
1347 if (flags & LEFTADJUST)
1348 while (*ret == ' ' || *ret == '\t')
1351 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1359 putstr (char *string)
1361 if (!column && lm > 0) {
1392 if (ontty != ISTTY || row != global.c_length)
1394 if (global.c_flags & BELL)
1398 read (fileno (stdout), buf, sizeof(buf));
1399 if (strchr(buf, '\n')) {
1400 if (global.c_flags & CLEARSCR)
1405 row = global.c_length / 3;
1424 * If we are forwarding this message, and the first
1425 * column contains a dash, then add a dash and a space.
1427 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1436 if (column >= wid) {
1440 putstr (ovtxt ? ovtxt : "");
1452 #ifndef RELIABLE_SIGNALS
1453 SIGNAL (SIGINT, intrser);
1458 longjmp (env, DONE);
1465 #ifndef RELIABLE_SIGNALS
1466 SIGNAL (SIGPIPE, pipeser);
1476 #ifndef RELIABLE_SIGNALS
1477 SIGNAL (SIGQUIT, quitser);
1487 face_format (struct mcomp *c1)
1490 struct mailname *mp;
1492 if ((cp = c1->c_text) == NULL)
1495 if ((cp = getname (cp))) {
1496 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1498 if ((h = mp->m_host) == NULL)
1500 if ((o = OfficialName (h)))
1502 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1505 while ((cp = getname (cp)))
1512 * faceproc is two elements defining the image agent's location:
1517 #include <sys/socket.h>
1518 #include <netinet/in.h>
1521 #ifdef HAVE_ARPA_INET_H
1522 # include <arpa/inet.h>
1526 doface (struct mcomp *c1)
1529 struct sockaddr_in in_socket;
1530 struct sockaddr_in *isock = &in_socket;
1531 static int inited = OK;
1533 static struct in_addr addr;
1534 static unsigned short portno;
1538 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1541 if (ap[0] == NULL || ap[1] == NULL) {
1544 return (inited = NOTOK);
1547 if (!(hp = gethostbystring (ap[0])))
1549 memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1551 portno = htons ((unsigned short) atoi (ap[1]));
1556 if (inited == NOTOK)
1559 isock->sin_family = AF_INET;
1560 isock->sin_port = portno;
1561 memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1563 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1566 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1567 (struct sockaddr *) isock, sizeof(*isock));
1571 return (result != NOTOK ? OK : NOTOK);
1576 * This version doesn't use sockets
1581 doface (struct mcomp *c1)
1585 int result, pdi[2], pdo[2];
1587 char buffer[BUFSIZ], *vec[10];
1589 if (pipe (pdi) == NOTOK)
1591 if (pipe (pdo) == NOTOK) {
1597 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1602 /* oops... fork error */
1607 SIGNAL (SIGINT, SIG_IGN);
1608 SIGNAL (SIGQUIT, SIG_IGN);
1609 if (pdi[0] != fileno (stdin)) {
1610 dup2 (pdi[0], fileno (stdin));
1615 if (pdo[1] != fileno (stdout)) {
1616 dup2 (pdo[1], fileno (stdout));
1620 vec[vecp++] = r1bindex (faceproc, '/');
1622 if (sleepsw != NOTOK) {
1624 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1625 vec[vecp++] = buffer;
1628 execvp (faceproc, vec);
1629 fprintf (stderr, "unable to exec ");
1631 _exit (-1); /* NOTREACHED */
1634 /* parent process */
1636 i = strlen (c1->c_text);
1637 if (write (pdi[1], c1->c_text, i) != i)
1638 adios ("pipe", "error writing to");
1639 free (c1->c_text), c1->c_text = NULL;
1645 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1649 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1650 memcpy(dp + len, buffer, i);
1654 cp = mh_xmalloc ((unsigned) i);
1655 memcpy(cp, buffer, i);
1659 for (bp = buffer + i - 1; bp >= buffer; bp--)
1660 if (!isascii (*bp) || iscntrl (*bp)) {
1667 /* no waiting for child... */
1669 if (result == OK) { /* binary */
1670 if (write (1, cp, len) != len)
1671 adios ("writing", "error");
1675 if ((c1->c_text = cp) == NULL)
1682 #endif /* COMMENTED OUT */
1686 mhlsbr (int argc, char **argv, FILE *(*action)())
1688 SIGNAL_HANDLER istat, pstat, qstat;
1692 switch (setjmp (mhlenv)) {
1695 sleepsw = 0; /* XXX */
1696 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1699 mhl_action = action;
1702 * If signal is at default action, then start ignoring
1703 * it, else let it set to its current action.
1705 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1706 SIGNAL (SIGINT, istat);
1707 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1708 SIGNAL (SIGQUIT, qstat);
1709 pstat = SIGNAL (SIGPIPE, pipeser);
1710 mhl (argc, argv); /* FALL THROUGH! */
1713 SIGNAL (SIGINT, istat);
1714 SIGNAL (SIGQUIT, qstat);
1715 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1718 SIGNAL (SIGPIPE, pstat);
1720 if (holder.c_text) {
1721 free (holder.c_text);
1722 holder.c_text = NULL;
1724 free_queue (&msghd, &msgtl);
1725 for (c1 = fmthd; c1; c1 = c1->c_next)
1726 c1->c_flags &= ~HDROUTPUT;
1735 mhladios (char *what, char *fmt, ...)
1740 advertise (what, NULL, fmt, ap);
1747 mhldone (int status)
1751 longjmp (mhlenv, DONE);
1757 static int m_pid = NOTOK;
1758 static int sd = NOTOK;
1761 m_popen (char *name)
1765 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1766 adios ("standard output", "unable to dup()");
1768 if (pipe (pd) == NOTOK)
1769 adios ("pipe", "unable to");
1771 switch (m_pid = vfork ()) {
1773 adios ("fork", "unable to");
1776 SIGNAL (SIGINT, SIG_DFL);
1777 SIGNAL (SIGQUIT, SIG_DFL);
1780 if (pd[0] != fileno (stdin)) {
1781 dup2 (pd[0], fileno (stdin));
1784 execlp (name, r1bindex (name, '/'), NULL);
1785 fprintf (stderr, "unable to exec ");
1791 if (pd[1] != fileno (stdout)) {
1792 dup2 (pd[1], fileno (stdout));
1807 if (dup2 (sd, fileno (stdout)) == NOTOK)
1808 adios ("standard output", "unable to dup2()");
1817 pidwait (m_pid, OK);