3 * mhlsbr.c -- main routines for nmh message lister
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
12 #include <zotnet/tws/tws.h>
18 * for a component containing addresses, ADDRFMT, if COMPRESS is also
19 * set, then addresses get split wrong (not at the spaces between commas).
20 * To fix this correctly, putstr() should know about "atomic" strings that
21 * must NOT be broken across lines. That's too difficult for right now
22 * (it turns out that there are a number of degernate cases), so in
23 * oneline(), instead of
25 * (*onelp == '\n' && !onelp[1])
27 * being a terminating condition,
29 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
31 * is used instead. This cuts the line prematurely, and gives us a much
32 * better chance of getting things right.
41 static struct swit mhlswitches[] = {
51 { "faceproc program", 0 },
55 { "folder +folder", 0 },
57 { "form formfile", 0 },
59 { "moreproc program", 0 },
63 { "length lines", 0 },
65 { "width columns", 0 },
67 { "sleep seconds", 0 },
69 { "dashstuffing", -12 }, /* interface from forw */
70 #define NBITSTUFFSW 14
71 { "nodashstuffing", -14 }, /* interface from forw */
77 { "forward", -7 }, /* interface from forw */
79 { "forwall", -7 }, /* interface from forw */
81 { "digest list", -6 },
83 { "volume number", -6 },
85 { "issue number", -5 },
91 #define NOCOMPONENT 0x000001 /* don't show component name */
92 #define UPPERCASE 0x000002 /* display in all upper case */
93 #define CENTER 0x000004 /* center line */
94 #define CLEARTEXT 0x000008 /* cleartext */
95 #define EXTRA 0x000010 /* an "extra" component */
96 #define HDROUTPUT 0x000020 /* already output */
97 #define CLEARSCR 0x000040 /* clear screen */
98 #define LEFTADJUST 0x000080 /* left justify multiple lines */
99 #define COMPRESS 0x000100 /* compress text */
100 #define ADDRFMT 0x000200 /* contains addresses */
101 #define BELL 0x000400 /* sound bell at EOP */
102 #define DATEFMT 0x000800 /* contains dates */
103 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
104 #define INIT 0x002000 /* initialize component */
105 #define FACEFMT 0x004000 /* contains face */
106 #define FACEDFLT 0x008000 /* default for face */
107 #define SPLIT 0x010000 /* split headers (don't concatenate) */
108 #define NONEWLINE 0x020000 /* don't write trailing newline */
109 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
110 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
113 char *c_name; /* component name */
114 char *c_text; /* component text */
115 char *c_ovtxt; /* text overflow indicator */
116 char *c_nfs; /* iff FORMAT */
117 struct format *c_fmt; /* .. */
118 char *c_face; /* face designator */
119 int c_offset; /* left margin indentation */
120 int c_ovoff; /* overflow indentation */
121 int c_width; /* width of field */
122 int c_cwidth; /* width of component */
123 int c_length; /* length in lines */
125 struct mcomp *c_next;
128 static struct mcomp *msghd = NULL;
129 static struct mcomp *msgtl = NULL;
130 static struct mcomp *fmthd = NULL;
131 static struct mcomp *fmttl = NULL;
133 static struct mcomp global = {
134 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
137 static struct mcomp holder = {
138 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
146 static struct pair pairs[] = {
148 { "From", ADDRFMT|FACEDFLT },
149 { "Sender", ADDRFMT },
150 { "Reply-To", ADDRFMT },
154 { "Resent-Date", DATEFMT },
155 { "Resent-From", ADDRFMT },
156 { "Resent-Sender", ADDRFMT },
157 { "Resent-Reply-To", ADDRFMT },
158 { "Resent-To", ADDRFMT },
159 { "Resent-cc", ADDRFMT },
160 { "Resent-Bcc", ADDRFMT },
171 static struct triple triples[] = {
172 { "nocomponent", NOCOMPONENT, 0 },
173 { "uppercase", UPPERCASE, 0 },
174 { "nouppercase", 0, UPPERCASE },
175 { "center", CENTER, 0 },
176 { "nocenter", 0, CENTER },
177 { "clearscreen", CLEARSCR, 0 },
178 { "noclearscreen", 0, CLEARSCR },
179 { "noclear", 0, CLEARSCR },
180 { "leftadjust", LEFTADJUST, 0 },
181 { "noleftadjust", 0, LEFTADJUST },
182 { "compress", COMPRESS, 0 },
183 { "nocompress", 0, COMPRESS },
184 { "split", SPLIT, 0 },
185 { "nosplit", 0, SPLIT },
186 { "addrfield", ADDRFMT, DATEFMT },
188 { "nobell", 0, BELL },
189 { "datefield", DATEFMT, ADDRFMT },
190 { "newline", 0, NONEWLINE },
191 { "nonewline", NONEWLINE, 0 },
196 static int bellflg = 0;
197 static int clearflg = 0;
198 static int dashstuff = 0;
199 static int dobody = 1;
200 static int forwflg = 0;
201 static int forwall = 0;
203 static int sleepsw = NOTOK;
205 static char *digest = NULL;
206 static int volume = 0;
207 static int issue = 0;
209 static int exitstat = 0;
210 static int mhldebug = 0;
215 static int ontty = NOTTY;
232 static int num_ignores = 0;
233 static char *ignores[MAXARGS];
236 static jmp_buf mhlenv;
238 static char delim3[] = /* from forw.c */
239 "\n----------------------------------------------------------------------\n\n";
240 static char delim4[] = "\n------------------------------\n\n";
242 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
246 * Redefine a couple of functions.
247 * These are undefined later in the code.
249 #define adios mhladios
255 static void mhl_format (char *, int, int);
256 static int evalvar (struct mcomp *);
257 static int ptoi (char *, int *);
258 static int ptos (char *, char **);
259 static char *parse (void);
260 static void process (char *, char *, int, int);
261 static void mhlfile (FILE *, char *, int, int);
262 static int mcomp_flags (char *);
263 static char *mcomp_add (long, char *, char *);
264 static void mcomp_format (struct mcomp *, struct mcomp *);
265 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
266 static void free_queue (struct mcomp **, struct mcomp **);
267 static void putcomp (struct mcomp *, struct mcomp *, int);
268 static char *oneline (char *, long);
269 static void putstr (char *);
270 static void putch (char);
271 static RETSIGTYPE intrser (int);
272 static RETSIGTYPE pipeser (int);
273 static RETSIGTYPE quitser (int);
274 static void face_format (struct mcomp *);
275 static int doface (struct mcomp *);
276 static void mhladios (char *, char *, ...);
277 static void mhldone (int);
278 static void m_popen (char *);
280 int mhl (int, char **);
281 int mhlsbr (int, char **, FILE *(*)());
282 void m_pclose (void);
284 void clear_screen (void); /* from termsbr.c */
285 int SOprintf (char *, ...); /* from termsbr.c */
286 int sc_width (void); /* from termsbr.c */
287 int sc_length (void); /* from termsbr.c */
288 int sc_hardcopy (void); /* from termsbr.c */
289 struct hostent *gethostbystring ();
293 mhl (int argc, char **argv)
295 int length = 0, nomore = 0;
296 int i, width = 0, vecp = 0;
297 char *cp, *folder = NULL, *form = NULL;
298 char buf[BUFSIZ], *files[MAXARGS];
299 char **argp, **arguments;
301 invo_name = r1bindex (argv[0], '/');
303 /* read user profile/context */
306 arguments = getarguments (invo_name, argc, argv, 1);
309 if ((cp = getenv ("MHLDEBUG")) && *cp)
312 if ((cp = getenv ("FACEPROC")))
315 while ((cp = *argp++)) {
317 switch (smatch (++cp, mhlswitches)) {
319 ambigsw (cp, mhlswitches);
322 adios (NULL, "-%s unknown\n", cp);
325 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
326 print_help (buf, mhlswitches, 1);
329 print_version(invo_name);
347 if (!(folder = *argp++) || *folder == '-')
348 adios (NULL, "missing argument to %s", argp[-2]);
351 if (!(form = *argp++) || *form == '-')
352 adios (NULL, "missing argument to %s", argp[-2]);
356 if (!(faceproc = *argp++) || *faceproc == '-')
357 adios (NULL, "missing argument to %s", argp[-2]);
363 if (!(cp = *argp++) || *cp == '-')
364 adios (NULL, "missing argument to %s", argp[-2]);
365 sleepsw = atoi (cp);/* ZERO ok! */
369 if (!(moreproc = *argp++) || *moreproc == '-')
370 adios (NULL, "missing argument to %s", argp[-2]);
377 if (!(cp = *argp++) || *cp == '-')
378 adios (NULL, "missing argument to %s", argp[-2]);
379 if ((length = atoi (cp)) < 1)
380 adios (NULL, "bad argument %s %s", argp[-2], cp);
383 if (!(cp = *argp++) || *cp == '-')
384 adios (NULL, "missing argument to %s", argp[-2]);
385 if ((width = atoi (cp)) < 1)
386 adios (NULL, "bad argument %s %s", argp[-2], cp);
390 if (!(digest = *argp++) || *digest == '-')
391 adios (NULL, "missing argument to %s", argp[-2]);
394 if (!(cp = *argp++) || *cp == '-')
395 adios (NULL, "missing argument to %s", argp[-2]);
396 if ((issue = atoi (cp)) < 1)
397 adios (NULL, "bad argument %s %s", argp[-2], cp);
400 if (!(cp = *argp++) || *cp == '-')
401 adios (NULL, "missing argument to %s", argp[-2]);
402 if ((volume = atoi (cp)) < 1)
403 adios (NULL, "bad argument %s %s", argp[-2], cp);
407 forwall++; /* fall */
410 clearflg = -1;/* XXX */
414 dashstuff = 1; /* trinary logic */
417 dashstuff = -1; /* trinary logic */
429 folder = getenv ("mhfolder");
431 if (isatty (fileno (stdout))) {
432 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
434 SIGNAL (SIGINT, SIG_IGN);
435 SIGNAL2 (SIGQUIT, quitser);
437 SIGNAL2 (SIGPIPE, pipeser);
441 SIGNAL (SIGINT, SIG_IGN);
442 SIGNAL2 (SIGQUIT, quitser);
449 mhl_format (form ? form : mhlformat, length, width);
452 process (folder, NULL, 1, vecp = 1);
454 for (i = 0; i < vecp; i++)
455 process (folder, files[i], i + 1, vecp);
460 printf ("%s", delim4);
462 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
464 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
465 digest, volume, issue);
468 for (cp = buf + i; i > 1; i--)
475 printf ("\n------- End of Forwarded Message%s\n\n",
476 vecp > 1 ? "s" : "");
479 if (clearflg > 0 && ontty == NOTTY)
490 mhl_format (char *file, int length, int width)
494 char *ap, buffer[BUFSIZ], name[NAMESZ];
498 static dev_t dev = 0;
499 static ino_t ino = 0;
500 static time_t mtime = 0;
503 if (stat (etcpath (file), &st) != NOTOK
504 && mtime == st.st_mtime
509 free_queue (&fmthd, &fmttl);
511 if ((fp = fopen (etcpath (file), "r")) == NULL)
512 adios (file, "unable to open format file");
514 if (fstat (fileno (fp), &st) != NOTOK) {
520 global.c_ovtxt = global.c_nfs = NULL;
524 if ((i = sc_width ()) > 5)
526 global.c_cwidth = -1;
527 if ((i = sc_length ()) > 5)
528 global.c_length = i - 1;
529 global.c_flags = BELL; /* BELL is default */
530 *(ip = ignores) = NULL;
532 while (vfgets (fp, &ap) == OK) {
537 if ((cp = strchr(bp, '\n')))
541 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
546 strncpy (name, parse(), sizeof(name));
552 * Split this list of fields to ignore, and copy
553 * it to the end of the current "ignores" list.
555 if (!strcasecmp (name, "ignores")) {
556 char **tmparray, **p;
559 /* split the fields */
560 tmparray = brkstring (getcpy (++parptr), ",", NULL);
562 /* count number of fields split */
567 /* copy pointers to split fields to ignores array */
568 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
574 if (evalvar (&global))
575 adios (NULL, "format file syntax error: %s", bp);
582 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
583 while (*parptr == ':' || *parptr == ',') {
586 adios (NULL, "format file syntax error: %s", bp);
588 if (!c1->c_nfs && global.c_nfs)
589 if (c1->c_flags & DATEFMT) {
590 if (global.c_flags & DATEFMT)
591 c1->c_nfs = getcpy (global.c_nfs);
594 if (c1->c_flags & ADDRFMT) {
595 if (global.c_flags & ADDRFMT)
596 c1->c_nfs = getcpy (global.c_nfs);
601 adios (NULL, "format file syntax error: %s", bp);
607 for (c1 = fmthd; c1; c1 = c1->c_next) {
608 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
609 c1->c_name, c1->c_text, c1->c_ovtxt);
610 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
611 (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
612 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
613 c1->c_offset, c1->c_ovoff, c1->c_width,
614 c1->c_cwidth, c1->c_length);
615 fprintf (stderr, "\tflags=%s\n",
616 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
622 global.c_flags |= CLEARSCR;
625 global.c_flags &= ~CLEARSCR;
628 switch (bellflg) { /* command line may override format file */
630 global.c_flags |= BELL;
633 global.c_flags &= ~BELL;
638 global.c_length = length;
640 global.c_width = width;
641 if (global.c_length < 5)
642 global.c_length = 10000;
643 if (global.c_width < 5)
644 global.c_width = 10000;
649 evalvar (struct mcomp *c1)
651 char *cp, name[NAMESZ];
656 strncpy (name, parse(), sizeof(name));
658 if (!strcasecmp (name, "component")) {
659 if (ptos (name, &c1->c_text))
661 c1->c_flags &= ~NOCOMPONENT;
665 if (!strcasecmp (name, "overflowtext"))
666 return ptos (name, &c1->c_ovtxt);
668 if (!strcasecmp (name, "formatfield")) {
671 if (ptos (name, &cp))
673 nfs = new_fs (NULL, NULL, cp);
674 c1->c_nfs = getcpy (nfs);
675 c1->c_flags |= FORMAT;
679 if (!strcasecmp (name, "decode")) {
682 nfs = new_fs (NULL, NULL, "%(decode{text})");
683 c1->c_nfs = getcpy (nfs);
684 c1->c_flags |= FORMAT;
688 if (!strcasecmp (name, "offset"))
689 return ptoi (name, &c1->c_offset);
690 if (!strcasecmp (name, "overflowoffset"))
691 return ptoi (name, &c1->c_ovoff);
692 if (!strcasecmp (name, "width"))
693 return ptoi (name, &c1->c_width);
694 if (!strcasecmp (name, "compwidth"))
695 return ptoi (name, &c1->c_cwidth);
696 if (!strcasecmp (name, "length"))
697 return ptoi (name, &c1->c_length);
698 if (!strcasecmp (name, "nodashstuffing"))
699 return (dashstuff = -1);
701 for (ap = triples; ap->t_name; ap++)
702 if (!strcasecmp (ap->t_name, name)) {
703 c1->c_flags |= ap->t_on;
704 c1->c_flags &= ~ap->t_off;
713 ptoi (char *name, int *i)
717 if (*parptr++ != '=' || !*(cp = parse ())) {
718 advise (NULL, "missing argument to variable %s", name);
728 ptos (char *name, char **s)
732 if (*parptr++ != '=') {
733 advise (NULL, "missing argument to variable %s", name);
737 if (*parptr != '"') {
739 *parptr && *parptr != ':' && *parptr != ',';
743 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
744 if (*parptr == QUOTE)
751 if ((*parptr = c) == '"')
762 static char result[NAMESZ];
764 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
783 process (char *folder, char *fname, int ofilen, int ofilec)
789 switch (setjmp (env)) {
792 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
794 advise (fname, "unable to open");
802 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
804 SIGNAL (SIGINT, intrser);
805 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
809 SIGNAL (SIGINT, SIG_IGN);
810 if (mhl_action == NULL && fp != stdin)
814 free (holder.c_text);
815 holder.c_text = NULL;
817 free_queue (&msghd, &msgtl);
818 for (c1 = fmthd; c1; c1 = c1->c_next)
819 c1->c_flags &= ~HDROUTPUT;
826 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
829 struct mcomp *c1, *c2, *c3;
830 char **ip, name[NAMESZ], buf[BUFSIZ];
834 printf ("%s", ofilen == 1 ? delim3 : delim4);
836 printf ("\n-------");
838 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
840 printf (" Message %d", ofilen);
848 if ((global.c_flags & CLEARSCR))
853 printf (">>> %s\n\n", mname);
858 strncpy (buf, "\n", sizeof(buf));
860 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
863 printf ("Press <return> to list \"%s\"...", mname);
867 read (fileno (stdout), buf, sizeof(buf));
869 if (strchr(buf, '\n')) {
870 if ((global.c_flags & CLEARSCR))
884 printf (">>> %s\n\n", mname);
890 for (state = FLD;;) {
891 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
894 for (ip = ignores; *ip; ip++)
895 if (!strcasecmp (name, *ip)) {
896 while (state == FLDPLUS)
897 state = m_getfld (state, name, buf, sizeof(buf), fp);
903 for (c2 = fmthd; c2; c2 = c2->c_next)
904 if (!strcasecmp (c2->c_name, name))
907 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
908 for (c1 = msghd; c1; c1 = c1->c_next)
909 if (!strcasecmp (c1->c_name, c3->c_name)) {
911 mcomp_add (c1->c_flags, buf, c1->c_text);
915 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
916 while (state == FLDPLUS) {
917 state = m_getfld (state, name, buf, sizeof(buf), fp);
918 c1->c_text = add (buf, c1->c_text);
921 c1->c_flags |= EXTRA;
927 for (c1 = fmthd; c1; c1 = c1->c_next) {
928 if (c1->c_flags & CLEARTEXT) {
929 putcomp (c1, c1, ONECOMP);
932 if (!strcasecmp (c1->c_name, "messagename")) {
933 holder.c_text = concat ("(Message ", mname, ")\n",
935 putcomp (c1, &holder, ONECOMP);
936 free (holder.c_text);
937 holder.c_text = NULL;
940 if (!strcasecmp (c1->c_name, "extras")) {
941 for (c2 = msghd; c2; c2 = c2->c_next)
942 if (c2->c_flags & EXTRA)
943 putcomp (c1, c2, TWOCOMP);
946 if (dobody && !strcasecmp (c1->c_name, "body")) {
947 if ((holder.c_text = malloc (sizeof(buf))) == NULL)
948 adios (NULL, "unable to allocate buffer memory");
949 strncpy (holder.c_text, buf, sizeof(buf));
950 while (state == BODY) {
951 putcomp (c1, &holder, BODYCOMP);
952 state = m_getfld (state, name, holder.c_text,
955 free (holder.c_text);
956 holder.c_text = NULL;
959 for (c2 = msghd; c2; c2 = c2->c_next)
960 if (!strcasecmp (c2->c_name, c1->c_name)) {
961 putcomp (c1, c2, ONECOMP);
962 if (!(c1->c_flags & SPLIT))
965 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
966 for (c2 = msghd; c2; c2 = c2->c_next)
967 if (c2->c_flags & FACEDFLT) {
968 if (c2->c_face == NULL)
970 if ((holder.c_text = c2->c_face)) {
971 putcomp (c1, &holder, ONECOMP);
972 holder.c_text = NULL;
981 advise (NULL, "format error in message %s", mname);
986 adios (NULL, "getfld() returned %d", state);
993 mcomp_flags (char *name)
997 for (ap = pairs; ap->p_name; ap++)
998 if (!strcasecmp (ap->p_name, name))
999 return (ap->p_flags);
1006 mcomp_add (long flags, char *s1, char *s2)
1010 if (!(flags & ADDRFMT))
1011 return add (s1, s2);
1013 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1016 return add (s1, add (",\n", s2));
1023 struct pqpair *pq_next;
1028 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1032 char buffer[BUFSIZ], error[BUFSIZ];
1034 struct pqpair *p, *q;
1036 struct mailname *mp;
1043 dat[3] = sizeof(buffer) - 1;
1045 fmt_compile (c1->c_nfs, &c1->c_fmt);
1047 if (!(c1->c_flags & ADDRFMT)) {
1048 FINDCOMP (cptr, "text");
1051 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1055 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1056 /* Don't need to append a newline, dctime() already did */
1057 c2->c_text = getcpy (buffer);
1063 (q = &pq)->pq_next = NULL;
1064 while ((cp = getname (ap))) {
1065 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1066 adios (NULL, "unable to allocate pqpair memory");
1068 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1069 p->pq_text = getcpy (cp);
1070 p->pq_error = getcpy (error);
1072 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1074 if ((h = mp->m_host) == NULL)
1076 if ((o = OfficialName (h)))
1078 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1081 p->pq_text = getcpy (mp->m_text);
1084 q = (q->pq_next = p);
1087 for (p = pq.pq_next; p; p = q) {
1088 FINDCOMP (cptr, "text");
1090 cptr->c_text = p->pq_text;
1091 FINDCOMP (cptr, "error");
1093 cptr->c_text = p->pq_error;
1095 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1098 c2->c_text = add (",\n", c2->c_text);
1099 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1101 c2->c_text = add (buffer, c2->c_text);
1111 c2->c_text = add ("\n", c2->c_text);
1116 static struct mcomp *
1117 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1121 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1122 adios (NULL, "unable to allocate comp memory");
1124 c1->c_flags = flags & ~INIT;
1125 if ((c1->c_name = name ? getcpy (name) : NULL))
1126 c1->c_flags |= mcomp_flags (c1->c_name);
1127 c1->c_text = text ? getcpy (text) : NULL;
1130 c1->c_ovtxt = getcpy (global.c_ovtxt);
1131 c1->c_offset = global.c_offset;
1132 c1->c_ovoff = global. c_ovoff;
1133 c1->c_width = c1->c_length = 0;
1134 c1->c_cwidth = global.c_cwidth;
1135 c1->c_flags |= global.c_flags & GFLAGS;
1140 (*tail)->c_next = c1;
1148 free_queue (struct mcomp **head, struct mcomp **tail)
1150 struct mcomp *c1, *c2;
1152 for (c1 = *head; c1; c1 = c2) {
1163 free ((char *) c1->c_fmt);
1169 *head = *tail = NULL;
1174 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1181 llim = c1->c_length ? c1->c_length : -1;
1182 wid = c1->c_width ? c1->c_width : global.c_width;
1183 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1185 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1187 if (wid < ovoff + strlen (ovtxt) + 5)
1188 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1189 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1192 if (c1->c_flags & CLEARTEXT) {
1193 putstr (c1->c_text);
1198 if (c1->c_flags & FACEFMT)
1199 switch (doface (c2)) {
1200 case NOTOK: /* error */
1201 case OK: /* async faceproc */
1204 default: /* sync faceproc */
1208 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1209 mcomp_format (c1, c2);
1211 if (c1->c_flags & CENTER) {
1212 count = (c1->c_width ? c1->c_width : global.c_width)
1213 - c1->c_offset - strlen (c2->c_text);
1214 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1215 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1216 lm = c1->c_offset + (count / 2);
1222 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1223 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1224 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1226 *cp = toupper (*cp);
1227 putstr (c1->c_text ? c1->c_text : c1->c_name);
1228 if (flag != BODYCOMP) {
1230 if (!(c1->c_flags & SPLIT))
1231 c1->c_flags |= HDROUTPUT;
1234 if ((count = c1->c_cwidth -
1235 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1240 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1244 && !(c2->c_flags & HDROUTPUT)
1245 && !(c2->c_flags & NOCOMPONENT)) {
1246 if (c1->c_flags & UPPERCASE)
1247 for (cp = c2->c_name; *cp; cp++)
1249 *cp = toupper (*cp);
1250 putstr (c2->c_name);
1252 if (!(c1->c_flags & SPLIT))
1253 c2->c_flags |= HDROUTPUT;
1256 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1260 if (c1->c_flags & UPPERCASE)
1261 for (cp = c2->c_text; *cp; cp++)
1263 *cp = toupper (*cp);
1267 if (flag == TWOCOMP)
1268 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1269 : strlen (c2->c_name) + 2;
1271 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1272 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1273 count += c1->c_offset;
1275 if ((cp = oneline (c2->c_text, c1->c_flags)))
1279 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1281 if (flag == BODYCOMP
1282 && !(c1->c_flags & NOCOMPONENT))
1283 putstr (c1->c_text ? c1->c_text : c1->c_name);
1293 oneline (char *stuff, long flags)
1301 return (onelp = NULL);
1305 if (flags & COMPRESS) {
1306 for (spc = 1, cp = ret; *onelp; onelp++)
1307 if (isspace (*onelp)) {
1308 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1327 while (*onelp && *onelp != '\n')
1329 if (*onelp == '\n') {
1333 if (flags & LEFTADJUST)
1334 while (*ret == ' ' || *ret == '\t')
1337 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1345 putstr (char *string)
1347 if (!column && lm > 0)
1377 if (ontty != ISTTY || row != global.c_length)
1379 if (global.c_flags & BELL)
1383 read (fileno (stdout), buf, sizeof(buf));
1384 if (strchr(buf, '\n')) {
1385 if (global.c_flags & CLEARSCR)
1390 row = global.c_length / 3;
1409 * If we are forwarding this message, and the first
1410 * column contains a dash, then add a dash and a space.
1412 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1421 if (column >= wid) {
1425 putstr (ovtxt ? ovtxt : "");
1437 #ifndef RELIABLE_SIGNALS
1438 SIGNAL (SIGINT, intrser);
1443 longjmp (env, DONE);
1450 #ifndef RELIABLE_SIGNALS
1451 SIGNAL (SIGPIPE, pipeser);
1461 #ifndef RELIABLE_SIGNALS
1462 SIGNAL (SIGQUIT, quitser);
1472 face_format (struct mcomp *c1)
1475 struct mailname *mp;
1477 if ((cp = c1->c_text) == NULL)
1480 if ((cp = getname (cp))) {
1481 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1483 if ((h = mp->m_host) == NULL)
1485 if ((o = OfficialName (h)))
1487 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1490 while ((cp = getname (cp)))
1497 * faceproc is two elements defining the image agent's location:
1502 #include <sys/socket.h>
1503 #include <netinet/in.h>
1506 #ifdef HAVE_ARPA_INET_H
1507 # include <arpa/inet.h>
1511 doface (struct mcomp *c1)
1514 struct sockaddr_in in_socket;
1515 struct sockaddr_in *isock = &in_socket;
1516 static int inited = OK;
1518 static struct in_addr addr;
1519 static unsigned short portno;
1523 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1526 if (ap[0] == NULL || ap[1] == NULL) {
1529 return (inited = NOTOK);
1532 if (!(hp = gethostbystring (ap[0])))
1534 memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1536 portno = htons ((unsigned short) atoi (ap[1]));
1541 if (inited == NOTOK)
1544 isock->sin_family = AF_INET;
1545 isock->sin_port = portno;
1546 memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1548 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1551 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1552 (struct sockaddr *) isock, sizeof(*isock));
1556 return (result != NOTOK ? OK : NOTOK);
1561 * This version doesn't use sockets
1566 doface (struct mcomp *c1)
1570 int result, pdi[2], pdo[2];
1572 char buffer[BUFSIZ], *vec[10];
1574 if (pipe (pdi) == NOTOK)
1576 if (pipe (pdo) == NOTOK) {
1582 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1587 /* oops... fork error */
1592 SIGNAL (SIGINT, SIG_IGN);
1593 SIGNAL (SIGQUIT, SIG_IGN);
1594 if (pdi[0] != fileno (stdin)) {
1595 dup2 (pdi[0], fileno (stdin));
1600 if (pdo[1] != fileno (stdout)) {
1601 dup2 (pdo[1], fileno (stdout));
1605 vec[vecp++] = r1bindex (faceproc, '/');
1607 if (sleepsw != NOTOK) {
1609 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1610 vec[vecp++] = buffer;
1613 execvp (faceproc, vec);
1614 fprintf (stderr, "unable to exec ");
1616 _exit (-1); /* NOTREACHED */
1619 /* parent process */
1621 i = strlen (c1->c_text);
1622 if (write (pdi[1], c1->c_text, i) != i)
1623 adios ("pipe", "error writing to");
1624 free (c1->c_text), c1->c_text = NULL;
1630 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1634 if ((dp = realloc (cp, (unsigned) (j = len + i))) == NULL)
1635 adios (NULL, "unable to allocate face storage");
1636 memcpy(dp + len, buffer, i);
1640 if ((cp = malloc ((unsigned) i)) == NULL)
1641 adios (NULL, "unable to allocate face storage");
1642 memcpy(cp, buffer, i);
1646 for (bp = buffer + i - 1; bp >= buffer; bp--)
1647 if (!isascii (*bp) || iscntrl (*bp)) {
1654 /* no waiting for child... */
1656 if (result == OK) { /* binary */
1657 if (write (1, cp, len) != len)
1658 adios ("writing", "error");
1662 if ((c1->c_text = cp) == NULL)
1669 #endif /* COMMENTED OUT */
1673 mhlsbr (int argc, char **argv, FILE *(*action)())
1675 SIGNAL_HANDLER istat, pstat, qstat;
1679 switch (setjmp (mhlenv)) {
1682 sleepsw = 0; /* XXX */
1683 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1686 mhl_action = action;
1689 * If signal is at default action, then start ignoring
1690 * it, else let it set to its current action.
1692 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1693 SIGNAL (SIGINT, istat);
1694 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1695 SIGNAL (SIGQUIT, qstat);
1696 pstat = SIGNAL (SIGPIPE, pipeser);
1697 mhl (argc, argv); /* FALL THROUGH! */
1700 SIGNAL (SIGINT, istat);
1701 SIGNAL (SIGQUIT, qstat);
1702 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1705 SIGNAL (SIGPIPE, pstat);
1707 if (holder.c_text) {
1708 free (holder.c_text);
1709 holder.c_text = NULL;
1711 free_queue (&msghd, &msgtl);
1712 for (c1 = fmthd; c1; c1 = c1->c_next)
1713 c1->c_flags &= ~HDROUTPUT;
1722 mhladios (char *what, char *fmt, ...)
1727 advertise (what, NULL, fmt, ap);
1734 mhldone (int status)
1738 longjmp (mhlenv, DONE);
1744 static int m_pid = NOTOK;
1745 static int sd = NOTOK;
1748 m_popen (char *name)
1752 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1753 adios ("standard output", "unable to dup()");
1755 if (pipe (pd) == NOTOK)
1756 adios ("pipe", "unable to");
1758 switch (m_pid = vfork ()) {
1760 adios ("fork", "unable to");
1763 SIGNAL (SIGINT, SIG_DFL);
1764 SIGNAL (SIGQUIT, SIG_DFL);
1767 if (pd[0] != fileno (stdin)) {
1768 dup2 (pd[0], fileno (stdin));
1771 execlp (name, r1bindex (name, '/'), NULL);
1772 fprintf (stderr, "unable to exec ");
1778 if (pd[1] != fileno (stdout)) {
1779 dup2 (pd[1], fileno (stdout));
1794 if (dup2 (sd, fileno (stdout)) == NOTOK)
1795 adios ("standard output", "unable to dup2()");
1804 pidwait (m_pid, OK);