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);
512 if ((fp = fopen (etcpath (file), "r")) == NULL)
513 adios (file, "unable to open format file");
515 if (fstat (fileno (fp), &st) != NOTOK) {
521 global.c_ovtxt = global.c_nfs = NULL;
525 if ((i = sc_width ()) > 5)
527 global.c_cwidth = -1;
528 if ((i = sc_length ()) > 5)
529 global.c_length = i - 1;
530 global.c_flags = BELL; /* BELL is default */
531 *(ip = ignores) = NULL;
533 while (vfgets (fp, &ap) == OK) {
538 if ((cp = strchr(bp, '\n')))
542 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
547 strncpy (name, parse(), sizeof(name));
553 * Split this list of fields to ignore, and copy
554 * it to the end of the current "ignores" list.
556 if (!strcasecmp (name, "ignores")) {
557 char **tmparray, **p;
560 /* split the fields */
561 tmparray = brkstring (getcpy (++parptr), ",", NULL);
563 /* count number of fields split */
568 /* copy pointers to split fields to ignores array */
569 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
575 if (evalvar (&global))
576 adios (NULL, "format file syntax error: %s", bp);
583 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
584 while (*parptr == ':' || *parptr == ',') {
587 adios (NULL, "format file syntax error: %s", bp);
589 if (!c1->c_nfs && global.c_nfs) {
590 if (c1->c_flags & DATEFMT) {
591 if (global.c_flags & DATEFMT)
592 c1->c_nfs = getcpy (global.c_nfs);
595 if (c1->c_flags & ADDRFMT) {
596 if (global.c_flags & ADDRFMT)
597 c1->c_nfs = getcpy (global.c_nfs);
603 adios (NULL, "format file syntax error: %s", bp);
609 for (c1 = fmthd; c1; c1 = c1->c_next) {
610 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
611 c1->c_name, c1->c_text, c1->c_ovtxt);
612 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
613 (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
614 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
615 c1->c_offset, c1->c_ovoff, c1->c_width,
616 c1->c_cwidth, c1->c_length);
617 fprintf (stderr, "\tflags=%s\n",
618 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
624 global.c_flags |= CLEARSCR;
627 global.c_flags &= ~CLEARSCR;
630 switch (bellflg) { /* command line may override format file */
632 global.c_flags |= BELL;
635 global.c_flags &= ~BELL;
640 global.c_length = length;
642 global.c_width = width;
643 if (global.c_length < 5)
644 global.c_length = 10000;
645 if (global.c_width < 5)
646 global.c_width = 10000;
651 evalvar (struct mcomp *c1)
653 char *cp, name[NAMESZ];
658 strncpy (name, parse(), sizeof(name));
660 if (!strcasecmp (name, "component")) {
661 if (ptos (name, &c1->c_text))
663 c1->c_flags &= ~NOCOMPONENT;
667 if (!strcasecmp (name, "overflowtext"))
668 return ptos (name, &c1->c_ovtxt);
670 if (!strcasecmp (name, "formatfield")) {
673 if (ptos (name, &cp))
675 nfs = new_fs (NULL, NULL, cp);
676 c1->c_nfs = getcpy (nfs);
677 c1->c_flags |= FORMAT;
681 if (!strcasecmp (name, "decode")) {
684 nfs = new_fs (NULL, NULL, "%(decode{text})");
685 c1->c_nfs = getcpy (nfs);
686 c1->c_flags |= FORMAT;
690 if (!strcasecmp (name, "offset"))
691 return ptoi (name, &c1->c_offset);
692 if (!strcasecmp (name, "overflowoffset"))
693 return ptoi (name, &c1->c_ovoff);
694 if (!strcasecmp (name, "width"))
695 return ptoi (name, &c1->c_width);
696 if (!strcasecmp (name, "compwidth"))
697 return ptoi (name, &c1->c_cwidth);
698 if (!strcasecmp (name, "length"))
699 return ptoi (name, &c1->c_length);
700 if (!strcasecmp (name, "nodashstuffing"))
701 return (dashstuff = -1);
703 for (ap = triples; ap->t_name; ap++)
704 if (!strcasecmp (ap->t_name, name)) {
705 c1->c_flags |= ap->t_on;
706 c1->c_flags &= ~ap->t_off;
715 ptoi (char *name, int *i)
719 if (*parptr++ != '=' || !*(cp = parse ())) {
720 advise (NULL, "missing argument to variable %s", name);
730 ptos (char *name, char **s)
734 if (*parptr++ != '=') {
735 advise (NULL, "missing argument to variable %s", name);
739 if (*parptr != '"') {
741 *parptr && *parptr != ':' && *parptr != ',';
745 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
746 if (*parptr == QUOTE)
753 if ((*parptr = c) == '"')
764 static char result[NAMESZ];
766 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
785 process (char *folder, char *fname, int ofilen, int ofilec)
791 switch (setjmp (env)) {
794 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
796 advise (fname, "unable to open");
804 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
806 SIGNAL (SIGINT, intrser);
807 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
811 SIGNAL (SIGINT, SIG_IGN);
812 if (mhl_action == NULL && fp != stdin)
816 free (holder.c_text);
817 holder.c_text = NULL;
819 free_queue (&msghd, &msgtl);
820 for (c1 = fmthd; c1; c1 = c1->c_next)
821 c1->c_flags &= ~HDROUTPUT;
828 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
831 struct mcomp *c1, *c2, *c3;
832 char **ip, name[NAMESZ], buf[BUFSIZ];
836 printf ("%s", ofilen == 1 ? delim3 : delim4);
838 printf ("\n-------");
840 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
842 printf (" Message %d", ofilen);
850 if ((global.c_flags & CLEARSCR))
855 printf (">>> %s\n\n", mname);
860 strncpy (buf, "\n", sizeof(buf));
862 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
865 printf ("Press <return> to list \"%s\"...", mname);
869 read (fileno (stdout), buf, sizeof(buf));
871 if (strchr(buf, '\n')) {
872 if ((global.c_flags & CLEARSCR))
886 printf (">>> %s\n\n", mname);
892 for (state = FLD;;) {
893 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
896 for (ip = ignores; *ip; ip++)
897 if (!strcasecmp (name, *ip)) {
898 while (state == FLDPLUS)
899 state = m_getfld (state, name, buf, sizeof(buf), fp);
905 for (c2 = fmthd; c2; c2 = c2->c_next)
906 if (!strcasecmp (c2->c_name, name))
909 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
910 for (c1 = msghd; c1; c1 = c1->c_next)
911 if (!strcasecmp (c1->c_name, c3->c_name)) {
913 mcomp_add (c1->c_flags, buf, c1->c_text);
917 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
918 while (state == FLDPLUS) {
919 state = m_getfld (state, name, buf, sizeof(buf), fp);
920 c1->c_text = add (buf, c1->c_text);
923 c1->c_flags |= EXTRA;
929 for (c1 = fmthd; c1; c1 = c1->c_next) {
930 if (c1->c_flags & CLEARTEXT) {
931 putcomp (c1, c1, ONECOMP);
934 if (!strcasecmp (c1->c_name, "messagename")) {
935 holder.c_text = concat ("(Message ", mname, ")\n",
937 putcomp (c1, &holder, ONECOMP);
938 free (holder.c_text);
939 holder.c_text = NULL;
942 if (!strcasecmp (c1->c_name, "extras")) {
943 for (c2 = msghd; c2; c2 = c2->c_next)
944 if (c2->c_flags & EXTRA)
945 putcomp (c1, c2, TWOCOMP);
948 if (dobody && !strcasecmp (c1->c_name, "body")) {
949 if ((holder.c_text = malloc (sizeof(buf))) == NULL)
950 adios (NULL, "unable to allocate buffer memory");
951 strncpy (holder.c_text, buf, sizeof(buf));
952 while (state == BODY) {
953 putcomp (c1, &holder, BODYCOMP);
954 state = m_getfld (state, name, holder.c_text,
957 free (holder.c_text);
958 holder.c_text = NULL;
961 for (c2 = msghd; c2; c2 = c2->c_next)
962 if (!strcasecmp (c2->c_name, c1->c_name)) {
963 putcomp (c1, c2, ONECOMP);
964 if (!(c1->c_flags & SPLIT))
967 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
968 for (c2 = msghd; c2; c2 = c2->c_next)
969 if (c2->c_flags & FACEDFLT) {
970 if (c2->c_face == NULL)
972 if ((holder.c_text = c2->c_face)) {
973 putcomp (c1, &holder, ONECOMP);
974 holder.c_text = NULL;
983 advise (NULL, "format error in message %s", mname);
988 adios (NULL, "getfld() returned %d", state);
995 mcomp_flags (char *name)
999 for (ap = pairs; ap->p_name; ap++)
1000 if (!strcasecmp (ap->p_name, name))
1001 return (ap->p_flags);
1008 mcomp_add (long flags, char *s1, char *s2)
1012 if (!(flags & ADDRFMT))
1013 return add (s1, s2);
1015 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1018 return add (s1, add (",\n", s2));
1025 struct pqpair *pq_next;
1030 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1034 char buffer[BUFSIZ], error[BUFSIZ];
1036 struct pqpair *p, *q;
1038 struct mailname *mp;
1045 dat[3] = sizeof(buffer) - 1;
1047 fmt_compile (c1->c_nfs, &c1->c_fmt);
1049 if (!(c1->c_flags & ADDRFMT)) {
1050 FINDCOMP (cptr, "text");
1053 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1057 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1058 /* Don't need to append a newline, dctime() already did */
1059 c2->c_text = getcpy (buffer);
1065 (q = &pq)->pq_next = NULL;
1066 while ((cp = getname (ap))) {
1067 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1068 adios (NULL, "unable to allocate pqpair memory");
1070 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1071 p->pq_text = getcpy (cp);
1072 p->pq_error = getcpy (error);
1074 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1076 if ((h = mp->m_host) == NULL)
1078 if ((o = OfficialName (h)))
1080 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1083 p->pq_text = getcpy (mp->m_text);
1086 q = (q->pq_next = p);
1089 for (p = pq.pq_next; p; p = q) {
1090 FINDCOMP (cptr, "text");
1092 cptr->c_text = p->pq_text;
1093 FINDCOMP (cptr, "error");
1095 cptr->c_text = p->pq_error;
1097 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1100 c2->c_text = add (",\n", c2->c_text);
1101 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1103 c2->c_text = add (buffer, c2->c_text);
1113 c2->c_text = add ("\n", c2->c_text);
1118 static struct mcomp *
1119 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1123 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1124 adios (NULL, "unable to allocate comp memory");
1126 c1->c_flags = flags & ~INIT;
1127 if ((c1->c_name = name ? getcpy (name) : NULL))
1128 c1->c_flags |= mcomp_flags (c1->c_name);
1129 c1->c_text = text ? getcpy (text) : NULL;
1132 c1->c_ovtxt = getcpy (global.c_ovtxt);
1133 c1->c_offset = global.c_offset;
1134 c1->c_ovoff = global. c_ovoff;
1135 c1->c_width = c1->c_length = 0;
1136 c1->c_cwidth = global.c_cwidth;
1137 c1->c_flags |= global.c_flags & GFLAGS;
1142 (*tail)->c_next = c1;
1150 free_queue (struct mcomp **head, struct mcomp **tail)
1152 struct mcomp *c1, *c2;
1154 for (c1 = *head; c1; c1 = c2) {
1165 free ((char *) c1->c_fmt);
1171 *head = *tail = NULL;
1176 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1183 llim = c1->c_length ? c1->c_length : -1;
1184 wid = c1->c_width ? c1->c_width : global.c_width;
1185 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1187 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1189 if (wid < ovoff + strlen (ovtxt) + 5)
1190 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1191 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1194 if (c1->c_flags & CLEARTEXT) {
1195 putstr (c1->c_text);
1200 if (c1->c_flags & FACEFMT)
1201 switch (doface (c2)) {
1202 case NOTOK: /* error */
1203 case OK: /* async faceproc */
1206 default: /* sync faceproc */
1210 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1211 mcomp_format (c1, c2);
1213 if (c1->c_flags & CENTER) {
1214 count = (c1->c_width ? c1->c_width : global.c_width)
1215 - c1->c_offset - strlen (c2->c_text);
1216 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1217 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1218 lm = c1->c_offset + (count / 2);
1224 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1225 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1226 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1228 *cp = toupper (*cp);
1229 putstr (c1->c_text ? c1->c_text : c1->c_name);
1230 if (flag != BODYCOMP) {
1232 if (!(c1->c_flags & SPLIT))
1233 c1->c_flags |= HDROUTPUT;
1236 if ((count = c1->c_cwidth -
1237 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1242 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1246 && !(c2->c_flags & HDROUTPUT)
1247 && !(c2->c_flags & NOCOMPONENT)) {
1248 if (c1->c_flags & UPPERCASE)
1249 for (cp = c2->c_name; *cp; cp++)
1251 *cp = toupper (*cp);
1252 putstr (c2->c_name);
1254 if (!(c1->c_flags & SPLIT))
1255 c2->c_flags |= HDROUTPUT;
1258 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1262 if (c1->c_flags & UPPERCASE)
1263 for (cp = c2->c_text; *cp; cp++)
1265 *cp = toupper (*cp);
1269 if (flag == TWOCOMP)
1270 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1271 : strlen (c2->c_name) + 2;
1273 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1274 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1276 count += c1->c_offset;
1278 if ((cp = oneline (c2->c_text, c1->c_flags)))
1282 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1284 if (flag == BODYCOMP
1285 && !(c1->c_flags & NOCOMPONENT))
1286 putstr (c1->c_text ? c1->c_text : c1->c_name);
1296 oneline (char *stuff, long flags)
1304 return (onelp = NULL);
1308 if (flags & COMPRESS) {
1309 for (spc = 1, cp = ret; *onelp; onelp++)
1310 if (isspace (*onelp)) {
1311 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1330 while (*onelp && *onelp != '\n')
1332 if (*onelp == '\n') {
1336 if (flags & LEFTADJUST)
1337 while (*ret == ' ' || *ret == '\t')
1340 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1348 putstr (char *string)
1350 if (!column && lm > 0) {
1381 if (ontty != ISTTY || row != global.c_length)
1383 if (global.c_flags & BELL)
1387 read (fileno (stdout), buf, sizeof(buf));
1388 if (strchr(buf, '\n')) {
1389 if (global.c_flags & CLEARSCR)
1394 row = global.c_length / 3;
1413 * If we are forwarding this message, and the first
1414 * column contains a dash, then add a dash and a space.
1416 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1425 if (column >= wid) {
1429 putstr (ovtxt ? ovtxt : "");
1441 #ifndef RELIABLE_SIGNALS
1442 SIGNAL (SIGINT, intrser);
1447 longjmp (env, DONE);
1454 #ifndef RELIABLE_SIGNALS
1455 SIGNAL (SIGPIPE, pipeser);
1465 #ifndef RELIABLE_SIGNALS
1466 SIGNAL (SIGQUIT, quitser);
1476 face_format (struct mcomp *c1)
1479 struct mailname *mp;
1481 if ((cp = c1->c_text) == NULL)
1484 if ((cp = getname (cp))) {
1485 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1487 if ((h = mp->m_host) == NULL)
1489 if ((o = OfficialName (h)))
1491 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1494 while ((cp = getname (cp)))
1501 * faceproc is two elements defining the image agent's location:
1506 #include <sys/socket.h>
1507 #include <netinet/in.h>
1510 #ifdef HAVE_ARPA_INET_H
1511 # include <arpa/inet.h>
1515 doface (struct mcomp *c1)
1518 struct sockaddr_in in_socket;
1519 struct sockaddr_in *isock = &in_socket;
1520 static int inited = OK;
1522 static struct in_addr addr;
1523 static unsigned short portno;
1527 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1530 if (ap[0] == NULL || ap[1] == NULL) {
1533 return (inited = NOTOK);
1536 if (!(hp = gethostbystring (ap[0])))
1538 memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1540 portno = htons ((unsigned short) atoi (ap[1]));
1545 if (inited == NOTOK)
1548 isock->sin_family = AF_INET;
1549 isock->sin_port = portno;
1550 memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1552 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1555 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1556 (struct sockaddr *) isock, sizeof(*isock));
1560 return (result != NOTOK ? OK : NOTOK);
1565 * This version doesn't use sockets
1570 doface (struct mcomp *c1)
1574 int result, pdi[2], pdo[2];
1576 char buffer[BUFSIZ], *vec[10];
1578 if (pipe (pdi) == NOTOK)
1580 if (pipe (pdo) == NOTOK) {
1586 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1591 /* oops... fork error */
1596 SIGNAL (SIGINT, SIG_IGN);
1597 SIGNAL (SIGQUIT, SIG_IGN);
1598 if (pdi[0] != fileno (stdin)) {
1599 dup2 (pdi[0], fileno (stdin));
1604 if (pdo[1] != fileno (stdout)) {
1605 dup2 (pdo[1], fileno (stdout));
1609 vec[vecp++] = r1bindex (faceproc, '/');
1611 if (sleepsw != NOTOK) {
1613 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1614 vec[vecp++] = buffer;
1617 execvp (faceproc, vec);
1618 fprintf (stderr, "unable to exec ");
1620 _exit (-1); /* NOTREACHED */
1623 /* parent process */
1625 i = strlen (c1->c_text);
1626 if (write (pdi[1], c1->c_text, i) != i)
1627 adios ("pipe", "error writing to");
1628 free (c1->c_text), c1->c_text = NULL;
1634 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1638 if ((dp = realloc (cp, (unsigned) (j = len + i))) == NULL)
1639 adios (NULL, "unable to allocate face storage");
1640 memcpy(dp + len, buffer, i);
1644 if ((cp = malloc ((unsigned) i)) == NULL)
1645 adios (NULL, "unable to allocate face storage");
1646 memcpy(cp, buffer, i);
1650 for (bp = buffer + i - 1; bp >= buffer; bp--)
1651 if (!isascii (*bp) || iscntrl (*bp)) {
1658 /* no waiting for child... */
1660 if (result == OK) { /* binary */
1661 if (write (1, cp, len) != len)
1662 adios ("writing", "error");
1666 if ((c1->c_text = cp) == NULL)
1673 #endif /* COMMENTED OUT */
1677 mhlsbr (int argc, char **argv, FILE *(*action)())
1679 SIGNAL_HANDLER istat, pstat, qstat;
1683 switch (setjmp (mhlenv)) {
1686 sleepsw = 0; /* XXX */
1687 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1690 mhl_action = action;
1693 * If signal is at default action, then start ignoring
1694 * it, else let it set to its current action.
1696 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1697 SIGNAL (SIGINT, istat);
1698 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1699 SIGNAL (SIGQUIT, qstat);
1700 pstat = SIGNAL (SIGPIPE, pipeser);
1701 mhl (argc, argv); /* FALL THROUGH! */
1704 SIGNAL (SIGINT, istat);
1705 SIGNAL (SIGQUIT, qstat);
1706 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1709 SIGNAL (SIGPIPE, pstat);
1711 if (holder.c_text) {
1712 free (holder.c_text);
1713 holder.c_text = NULL;
1715 free_queue (&msghd, &msgtl);
1716 for (c1 = fmthd; c1; c1 = c1->c_next)
1717 c1->c_flags &= ~HDROUTPUT;
1726 mhladios (char *what, char *fmt, ...)
1731 advertise (what, NULL, fmt, ap);
1738 mhldone (int status)
1742 longjmp (mhlenv, DONE);
1748 static int m_pid = NOTOK;
1749 static int sd = NOTOK;
1752 m_popen (char *name)
1756 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1757 adios ("standard output", "unable to dup()");
1759 if (pipe (pd) == NOTOK)
1760 adios ("pipe", "unable to");
1762 switch (m_pid = vfork ()) {
1764 adios ("fork", "unable to");
1767 SIGNAL (SIGINT, SIG_DFL);
1768 SIGNAL (SIGQUIT, SIG_DFL);
1771 if (pd[0] != fileno (stdin)) {
1772 dup2 (pd[0], fileno (stdin));
1775 execlp (name, r1bindex (name, '/'), NULL);
1776 fprintf (stderr, "unable to exec ");
1782 if (pd[1] != fileno (stdout)) {
1783 dup2 (pd[1], fileno (stdout));
1798 if (dup2 (sd, fileno (stdout)) == NOTOK)
1799 adios ("standard output", "unable to dup2()");
1808 pidwait (m_pid, OK);