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>
21 * for a component containing addresses, ADDRFMT, if COMPRESS is also
22 * set, then addresses get split wrong (not at the spaces between commas).
23 * To fix this correctly, putstr() should know about "atomic" strings that
24 * must NOT be broken across lines. That's too difficult for right now
25 * (it turns out that there are a number of degernate cases), so in
26 * oneline(), instead of
28 * (*onelp == '\n' && !onelp[1])
30 * being a terminating condition,
32 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
34 * is used instead. This cuts the line prematurely, and gives us a much
35 * better chance of getting things right.
44 static struct swit mhlswitches[] = {
54 { "faceproc program", 0 },
58 { "folder +folder", 0 },
60 { "form formfile", 0 },
62 { "moreproc program", 0 },
66 { "length lines", 0 },
68 { "width columns", 0 },
70 { "sleep seconds", 0 },
72 { "dashstuffing", -12 }, /* interface from forw */
73 #define NBITSTUFFSW 14
74 { "nodashstuffing", -14 }, /* interface from forw */
80 { "forward", -7 }, /* interface from forw */
82 { "forwall", -7 }, /* interface from forw */
84 { "digest list", -6 },
86 { "volume number", -6 },
88 { "issue number", -5 },
94 #define NOCOMPONENT 0x000001 /* don't show component name */
95 #define UPPERCASE 0x000002 /* display in all upper case */
96 #define CENTER 0x000004 /* center line */
97 #define CLEARTEXT 0x000008 /* cleartext */
98 #define EXTRA 0x000010 /* an "extra" component */
99 #define HDROUTPUT 0x000020 /* already output */
100 #define CLEARSCR 0x000040 /* clear screen */
101 #define LEFTADJUST 0x000080 /* left justify multiple lines */
102 #define COMPRESS 0x000100 /* compress text */
103 #define ADDRFMT 0x000200 /* contains addresses */
104 #define BELL 0x000400 /* sound bell at EOP */
105 #define DATEFMT 0x000800 /* contains dates */
106 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
107 #define INIT 0x002000 /* initialize component */
108 #define FACEFMT 0x004000 /* contains face */
109 #define FACEDFLT 0x008000 /* default for face */
110 #define SPLIT 0x010000 /* split headers (don't concatenate) */
111 #define NONEWLINE 0x020000 /* don't write trailing newline */
112 #define NOWRAP 0x040000 /* Don't wrap lines ever */
113 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE\023NOWRAP"
114 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
117 char *c_name; /* component name */
118 char *c_text; /* component text */
119 char *c_ovtxt; /* text overflow indicator */
120 char *c_nfs; /* iff FORMAT */
121 struct format *c_fmt; /* .. */
122 char *c_face; /* face designator */
123 int c_offset; /* left margin indentation */
124 int c_ovoff; /* overflow indentation */
125 int c_width; /* width of field */
126 int c_cwidth; /* width of component */
127 int c_length; /* length in lines */
129 struct mcomp *c_next;
132 static struct mcomp *msghd = NULL;
133 static struct mcomp *msgtl = NULL;
134 static struct mcomp *fmthd = NULL;
135 static struct mcomp *fmttl = NULL;
137 static struct mcomp global = {
138 NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL
141 static struct mcomp holder = {
142 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL
150 static struct pair pairs[] = {
152 { "From", ADDRFMT|FACEDFLT },
153 { "Sender", ADDRFMT },
154 { "Reply-To", ADDRFMT },
158 { "Resent-Date", DATEFMT },
159 { "Resent-From", ADDRFMT },
160 { "Resent-Sender", ADDRFMT },
161 { "Resent-Reply-To", ADDRFMT },
162 { "Resent-To", ADDRFMT },
163 { "Resent-cc", ADDRFMT },
164 { "Resent-Bcc", ADDRFMT },
175 static struct triple triples[] = {
176 { "nocomponent", NOCOMPONENT, 0 },
177 { "uppercase", UPPERCASE, 0 },
178 { "nouppercase", 0, UPPERCASE },
179 { "center", CENTER, 0 },
180 { "nocenter", 0, CENTER },
181 { "clearscreen", CLEARSCR, 0 },
182 { "noclearscreen", 0, CLEARSCR },
183 { "noclear", 0, CLEARSCR },
184 { "leftadjust", LEFTADJUST, 0 },
185 { "noleftadjust", 0, LEFTADJUST },
186 { "compress", COMPRESS, 0 },
187 { "nocompress", 0, COMPRESS },
188 { "split", SPLIT, 0 },
189 { "nosplit", 0, SPLIT },
190 { "addrfield", ADDRFMT, DATEFMT },
192 { "nobell", 0, BELL },
193 { "datefield", DATEFMT, ADDRFMT },
194 { "newline", 0, NONEWLINE },
195 { "nonewline", NONEWLINE, 0 },
196 { "wrap", 0, NOWRAP },
197 { "nowrap", NOWRAP, 0 },
202 static int bellflg = 0;
203 static int clearflg = 0;
204 static int dashstuff = 0;
205 static int dobody = 1;
206 static int forwflg = 0;
207 static int forwall = 0;
209 static int sleepsw = NOTOK;
211 static char *digest = NULL;
212 static int volume = 0;
213 static int issue = 0;
215 static int exitstat = 0;
216 static int mhldebug = 0;
221 static int ontty = NOTTY;
224 static unsigned int column;
230 static unsigned int wid;
234 static unsigned char *onelp;
238 static int num_ignores = 0;
239 static char *ignores[MAXARGS];
242 static jmp_buf mhlenv;
244 static char delim3[] = /* from forw.c */
245 "\n----------------------------------------------------------------------\n\n";
246 static char delim4[] = "\n------------------------------\n\n";
248 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
252 * Redefine a couple of functions.
253 * These are undefined later in the code.
255 #define adios mhladios
261 static void mhl_format (char *, int, int);
262 static int evalvar (struct mcomp *);
263 static int ptoi (char *, int *);
264 static int ptos (char *, char **);
265 static char *parse (void);
266 static void process (char *, char *, int, int);
267 static void mhlfile (FILE *, char *, int, int);
268 static int mcomp_flags (char *);
269 static char *mcomp_add (long, char *, char *);
270 static void mcomp_format (struct mcomp *, struct mcomp *);
271 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
272 static void free_queue (struct mcomp **, struct mcomp **);
273 static void putcomp (struct mcomp *, struct mcomp *, int);
274 static char *oneline (char *, long);
275 static void putstr (char *, long);
276 static void putch (char, long);
277 static void intrser (int);
278 static void pipeser (int);
279 static void quitser (int);
280 static void face_format (struct mcomp *);
281 static int doface (struct mcomp *);
282 static void mhladios (char *, char *, ...);
283 static void mhldone (int);
284 static void m_popen (char *);
286 int mhl (int, char **);
287 int mhlsbr (int, char **, FILE *(*)());
288 void m_pclose (void);
290 void clear_screen (void); /* from termsbr.c */
291 int SOprintf (char *, ...); /* from termsbr.c */
292 int sc_width (void); /* from termsbr.c */
293 int sc_length (void); /* from termsbr.c */
294 int sc_hardcopy (void); /* from termsbr.c */
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)(unsigned long) c1->c_nfs,
624 (unsigned int)(unsigned long) c1->c_fmt);
625 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
626 c1->c_offset, c1->c_ovoff, c1->c_width,
627 c1->c_cwidth, c1->c_length);
628 fprintf (stderr, "\tflags=%s\n",
629 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
635 global.c_flags |= CLEARSCR;
638 global.c_flags &= ~CLEARSCR;
641 switch (bellflg) { /* command line may override format file */
643 global.c_flags |= BELL;
646 global.c_flags &= ~BELL;
651 global.c_length = length;
653 global.c_width = width;
654 if (global.c_length < 5)
655 global.c_length = 10000;
656 if (global.c_width < 5)
657 global.c_width = 10000;
662 evalvar (struct mcomp *c1)
664 char *cp, name[NAMESZ];
669 strncpy (name, parse(), sizeof(name));
671 if (!mh_strcasecmp (name, "component")) {
672 if (ptos (name, &c1->c_text))
674 c1->c_flags &= ~NOCOMPONENT;
678 if (!mh_strcasecmp (name, "overflowtext"))
679 return ptos (name, &c1->c_ovtxt);
681 if (!mh_strcasecmp (name, "formatfield")) {
684 if (ptos (name, &cp))
686 nfs = new_fs (NULL, NULL, cp);
687 c1->c_nfs = getcpy (nfs);
688 c1->c_flags |= FORMAT;
692 if (!mh_strcasecmp (name, "decode")) {
695 nfs = new_fs (NULL, NULL, "%(decode{text})");
696 c1->c_nfs = getcpy (nfs);
697 c1->c_flags |= FORMAT;
701 if (!mh_strcasecmp (name, "offset"))
702 return ptoi (name, &c1->c_offset);
703 if (!mh_strcasecmp (name, "overflowoffset"))
704 return ptoi (name, &c1->c_ovoff);
705 if (!mh_strcasecmp (name, "width"))
706 return ptoi (name, &c1->c_width);
707 if (!mh_strcasecmp (name, "compwidth"))
708 return ptoi (name, &c1->c_cwidth);
709 if (!mh_strcasecmp (name, "length"))
710 return ptoi (name, &c1->c_length);
711 if (!mh_strcasecmp (name, "nodashstuffing"))
712 return (dashstuff = -1);
714 for (ap = triples; ap->t_name; ap++)
715 if (!mh_strcasecmp (ap->t_name, name)) {
716 c1->c_flags |= ap->t_on;
717 c1->c_flags &= ~ap->t_off;
726 ptoi (char *name, int *i)
730 if (*parptr++ != '=' || !*(cp = parse ())) {
731 advise (NULL, "missing argument to variable %s", name);
741 ptos (char *name, char **s)
745 if (*parptr++ != '=') {
746 advise (NULL, "missing argument to variable %s", name);
750 if (*parptr != '"') {
752 *parptr && *parptr != ':' && *parptr != ',';
756 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
757 if (*parptr == QUOTE)
764 if ((*parptr = c) == '"')
775 static char result[NAMESZ];
777 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
796 process (char *folder, char *fname, int ofilen, int ofilec)
802 switch (m_setjmp (env)) {
805 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
807 advise (fname, "unable to open");
815 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
817 SIGNAL (SIGINT, intrser);
818 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
822 SIGNAL (SIGINT, SIG_IGN);
823 if (mhl_action == NULL && fp != stdin)
827 free (holder.c_text);
828 holder.c_text = NULL;
830 free_queue (&msghd, &msgtl);
831 for (c1 = fmthd; c1; c1 = c1->c_next)
832 c1->c_flags &= ~HDROUTPUT;
839 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
842 struct mcomp *c1, *c2, *c3;
843 char **ip, name[NAMESZ], buf[BUFSIZ];
847 printf ("%s", ofilen == 1 ? delim3 : delim4);
849 printf ("\n-------");
851 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
853 printf (" Message %d", ofilen);
861 if ((global.c_flags & CLEARSCR))
866 printf (">>> %s\n\n", mname);
871 strncpy (buf, "\n", sizeof(buf));
873 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
876 printf ("Press <return> to list \"%s\"...", mname);
880 read (fileno (stdout), buf, sizeof(buf));
882 if (strchr(buf, '\n')) {
883 if ((global.c_flags & CLEARSCR))
897 printf (">>> %s\n\n", mname);
903 for (state = FLD;;) {
904 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
907 for (ip = ignores; *ip; ip++)
908 if (!mh_strcasecmp (name, *ip)) {
909 while (state == FLDPLUS)
910 state = m_getfld (state, name, buf, sizeof(buf), fp);
916 for (c2 = fmthd; c2; c2 = c2->c_next)
917 if (!mh_strcasecmp (c2->c_name, name))
920 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
921 for (c1 = msghd; c1; c1 = c1->c_next)
922 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
924 mcomp_add (c1->c_flags, buf, c1->c_text);
928 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
929 while (state == FLDPLUS) {
930 state = m_getfld (state, name, buf, sizeof(buf), fp);
931 c1->c_text = add (buf, c1->c_text);
934 c1->c_flags |= EXTRA;
940 for (c1 = fmthd; c1; c1 = c1->c_next) {
941 if (c1->c_flags & CLEARTEXT) {
942 putcomp (c1, c1, ONECOMP);
945 if (!mh_strcasecmp (c1->c_name, "messagename")) {
946 holder.c_text = concat ("(Message ", mname, ")\n",
948 putcomp (c1, &holder, ONECOMP);
949 free (holder.c_text);
950 holder.c_text = NULL;
953 if (!mh_strcasecmp (c1->c_name, "extras")) {
954 for (c2 = msghd; c2; c2 = c2->c_next)
955 if (c2->c_flags & EXTRA)
956 putcomp (c1, c2, TWOCOMP);
959 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
960 holder.c_text = mh_xmalloc (sizeof(buf));
961 strncpy (holder.c_text, buf, sizeof(buf));
962 while (state == BODY) {
963 putcomp (c1, &holder, BODYCOMP);
964 state = m_getfld (state, name, holder.c_text,
967 free (holder.c_text);
968 holder.c_text = NULL;
971 for (c2 = msghd; c2; c2 = c2->c_next)
972 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
973 putcomp (c1, c2, ONECOMP);
974 if (!(c1->c_flags & SPLIT))
977 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
978 for (c2 = msghd; c2; c2 = c2->c_next)
979 if (c2->c_flags & FACEDFLT) {
980 if (c2->c_face == NULL)
982 if ((holder.c_text = c2->c_face)) {
983 putcomp (c1, &holder, ONECOMP);
984 holder.c_text = NULL;
993 advise (NULL, "format error in message %s", mname);
998 adios (NULL, "getfld() returned %d", state);
1005 mcomp_flags (char *name)
1009 for (ap = pairs; ap->p_name; ap++)
1010 if (!mh_strcasecmp (ap->p_name, name))
1011 return (ap->p_flags);
1018 mcomp_add (long flags, char *s1, char *s2)
1022 if (!(flags & ADDRFMT))
1023 return add (s1, s2);
1025 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1028 return add (s1, add (",\n", s2));
1035 struct pqpair *pq_next;
1040 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1044 char buffer[BUFSIZ], error[BUFSIZ];
1046 struct pqpair *p, *q;
1048 struct mailname *mp;
1055 dat[3] = sizeof(buffer) - 1;
1057 fmt_compile (c1->c_nfs, &c1->c_fmt);
1059 if (!(c1->c_flags & ADDRFMT)) {
1060 FINDCOMP (cptr, "text");
1063 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1067 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1068 /* Don't need to append a newline, dctime() already did */
1069 c2->c_text = getcpy (buffer);
1075 (q = &pq)->pq_next = NULL;
1076 while ((cp = getname (ap))) {
1077 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1078 adios (NULL, "unable to allocate pqpair memory");
1080 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1081 p->pq_text = getcpy (cp);
1082 p->pq_error = getcpy (error);
1084 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1086 if ((h = mp->m_host) == NULL)
1088 if ((o = OfficialName (h)))
1090 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1093 p->pq_text = getcpy (mp->m_text);
1096 q = (q->pq_next = p);
1099 for (p = pq.pq_next; p; p = q) {
1100 FINDCOMP (cptr, "text");
1102 cptr->c_text = p->pq_text;
1103 FINDCOMP (cptr, "error");
1105 cptr->c_text = p->pq_error;
1107 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1110 c2->c_text = add (",\n", c2->c_text);
1111 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1113 c2->c_text = add (buffer, c2->c_text);
1123 c2->c_text = add ("\n", c2->c_text);
1128 static struct mcomp *
1129 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1133 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1134 adios (NULL, "unable to allocate comp memory");
1136 c1->c_flags = flags & ~INIT;
1137 if ((c1->c_name = name ? getcpy (name) : NULL))
1138 c1->c_flags |= mcomp_flags (c1->c_name);
1139 c1->c_text = text ? getcpy (text) : NULL;
1142 c1->c_ovtxt = getcpy (global.c_ovtxt);
1143 c1->c_offset = global.c_offset;
1144 c1->c_ovoff = global. c_ovoff;
1145 c1->c_width = c1->c_length = 0;
1146 c1->c_cwidth = global.c_cwidth;
1147 c1->c_flags |= global.c_flags & GFLAGS;
1152 (*tail)->c_next = c1;
1160 free_queue (struct mcomp **head, struct mcomp **tail)
1162 struct mcomp *c1, *c2;
1164 for (c1 = *head; c1; c1 = c2) {
1175 free ((char *) c1->c_fmt);
1181 *head = *tail = NULL;
1186 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1193 llim = c1->c_length ? c1->c_length : -1;
1194 wid = c1->c_width ? c1->c_width : global.c_width;
1195 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1197 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1199 if (wid < ovoff + strlen (ovtxt) + 5)
1200 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1201 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1204 if (c1->c_flags & CLEARTEXT) {
1205 putstr (c1->c_text, c1->c_flags);
1206 putstr ("\n", c1->c_flags);
1210 if (c1->c_flags & FACEFMT)
1211 switch (doface (c2)) {
1212 case NOTOK: /* error */
1213 case OK: /* async faceproc */
1216 default: /* sync faceproc */
1220 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1221 mcomp_format (c1, c2);
1223 if (c1->c_flags & CENTER) {
1224 count = (c1->c_width ? c1->c_width : global.c_width)
1225 - c1->c_offset - strlen (c2->c_text);
1226 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1227 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1228 lm = c1->c_offset + (count / 2);
1234 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1235 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1236 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1238 *cp = toupper (*cp);
1239 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1240 if (flag != BODYCOMP) {
1241 putstr (": ", c1->c_flags);
1242 if (!(c1->c_flags & SPLIT))
1243 c1->c_flags |= HDROUTPUT;
1246 if ((count = c1->c_cwidth -
1247 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1249 putstr (" ", c1->c_flags);
1252 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1256 && !(c2->c_flags & HDROUTPUT)
1257 && !(c2->c_flags & NOCOMPONENT)) {
1258 if (c1->c_flags & UPPERCASE)
1259 for (cp = c2->c_name; *cp; cp++)
1261 *cp = toupper (*cp);
1262 putstr (c2->c_name, c1->c_flags);
1263 putstr (": ", c1->c_flags);
1264 if (!(c1->c_flags & SPLIT))
1265 c2->c_flags |= HDROUTPUT;
1268 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1270 putstr (" ", c1->c_flags);
1272 if (c1->c_flags & UPPERCASE)
1273 for (cp = c2->c_text; *cp; cp++)
1275 *cp = toupper (*cp);
1279 if (flag == TWOCOMP)
1280 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1281 : (int) strlen (c2->c_name) + 2;
1283 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1284 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1286 count += c1->c_offset;
1288 if ((cp = oneline (c2->c_text, c1->c_flags)))
1289 putstr(cp, c1->c_flags);
1291 putstr ("\n", c1->c_flags);
1292 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1294 if (flag == BODYCOMP
1295 && !(c1->c_flags & NOCOMPONENT))
1296 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1298 putstr (cp, c1->c_flags);
1300 putstr ("\n", c1->c_flags);
1302 if (flag == BODYCOMP && term == '\n')
1303 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1308 oneline (char *stuff, long flags)
1316 return (onelp = NULL);
1320 if (flags & COMPRESS) {
1321 for (spc = 1, cp = ret; *onelp; onelp++)
1322 if (isspace (*onelp)) {
1323 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1342 while (*onelp && *onelp != '\n')
1344 if (*onelp == '\n') {
1348 if (flags & LEFTADJUST)
1349 while (*ret == ' ' || *ret == '\t')
1352 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1360 putstr (char *string, long flags)
1362 if (!column && lm > 0) {
1365 putch ('\t', flags);
1375 putch (*string++, flags);
1380 putch (char ch, long flags)
1393 if (ontty != ISTTY || row != global.c_length)
1395 if (global.c_flags & BELL)
1399 read (fileno (stdout), buf, sizeof(buf));
1400 if (strchr(buf, '\n')) {
1401 if (global.c_flags & CLEARSCR)
1406 row = global.c_length / 3;
1425 * If we are forwarding this message, and the first
1426 * column contains a dash, then add a dash and a space.
1428 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1437 if (column >= wid && (flags & NOWRAP) == 0) {
1438 putch ('\n', flags);
1441 putstr (ovtxt ? ovtxt : "", flags);
1457 longjmp (env, DONE);
1482 face_format (struct mcomp *c1)
1485 struct mailname *mp;
1487 if ((cp = c1->c_text) == NULL)
1490 if ((cp = getname (cp))) {
1491 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1493 if ((h = mp->m_host) == NULL)
1495 if ((o = OfficialName (h)))
1497 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1500 while ((cp = getname (cp)))
1507 * faceproc is two elements defining the image agent's location:
1512 #include <sys/socket.h>
1513 #include <netinet/in.h>
1515 #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);
1650 cp = mh_xmalloc ((unsigned) i);
1651 memcpy(cp, buffer, i);
1655 for (bp = buffer + i - 1; bp >= buffer; bp--)
1656 if (!isascii (*bp) || iscntrl (*bp)) {
1663 /* no waiting for child... */
1665 if (result == OK) { /* binary */
1666 if (write (1, cp, len) != len)
1667 adios ("writing", "error");
1671 if ((c1->c_text = cp) == NULL)
1678 #endif /* COMMENTED OUT */
1682 mhlsbr (int argc, char **argv, FILE *(*action)())
1684 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1688 switch (m_setjmp (mhlenv)) {
1691 sleepsw = 0; /* XXX */
1692 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1695 mhl_action = action;
1698 * If signal is at default action, then start ignoring
1699 * it, else let it set to its current action.
1701 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1702 SIGNAL (SIGINT, istat);
1703 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1704 SIGNAL (SIGQUIT, qstat);
1705 pstat = SIGNAL (SIGPIPE, pipeser);
1706 mhl (argc, argv); /* FALL THROUGH! */
1709 SIGNAL (SIGINT, istat);
1710 SIGNAL (SIGQUIT, qstat);
1711 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1714 SIGNAL (SIGPIPE, pstat);
1716 if (holder.c_text) {
1717 free (holder.c_text);
1718 holder.c_text = NULL;
1720 free_queue (&msghd, &msgtl);
1721 for (c1 = fmthd; c1; c1 = c1->c_next)
1722 c1->c_flags &= ~HDROUTPUT;
1731 mhladios (char *what, char *fmt, ...)
1736 advertise (what, NULL, fmt, ap);
1743 mhldone (int status)
1747 longjmp (mhlenv, DONE);
1753 static int m_pid = NOTOK;
1754 static int sd = NOTOK;
1757 m_popen (char *name)
1761 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1762 adios ("standard output", "unable to dup()");
1764 if (pipe (pd) == NOTOK)
1765 adios ("pipe", "unable to");
1767 switch (m_pid = vfork()) {
1769 adios ("fork", "unable to");
1772 SIGNAL (SIGINT, SIG_DFL);
1773 SIGNAL (SIGQUIT, SIG_DFL);
1776 if (pd[0] != fileno (stdin)) {
1777 dup2 (pd[0], fileno (stdin));
1780 execlp (name, r1bindex (name, '/'), NULL);
1781 fprintf (stderr, "unable to exec ");
1787 if (pd[1] != fileno (stdout)) {
1788 dup2 (pd[1], fileno (stdout));
1803 if (dup2 (sd, fileno (stdout)) == NOTOK)
1804 adios ("standard output", "unable to dup2()");
1813 pidwait (m_pid, OK);