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);
440 SIGNAL (SIGINT, SIG_IGN);
441 SIGNAL2 (SIGQUIT, quitser);
448 mhl_format (form ? form : mhlformat, length, width);
451 process (folder, NULL, 1, vecp = 1);
453 for (i = 0; i < vecp; i++)
454 process (folder, files[i], i + 1, vecp);
459 printf ("%s", delim4);
461 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
463 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
464 digest, volume, issue);
467 for (cp = buf + i; i > 1; i--)
474 printf ("\n------- End of Forwarded Message%s\n\n",
475 vecp > 1 ? "s" : "");
478 if (clearflg > 0 && ontty == NOTTY)
489 mhl_format (char *file, int length, int width)
493 char *ap, buffer[BUFSIZ], name[NAMESZ];
497 static dev_t dev = 0;
498 static ino_t ino = 0;
499 static time_t mtime = 0;
502 if (stat (etcpath (file), &st) != NOTOK
503 && mtime == st.st_mtime
508 free_queue (&fmthd, &fmttl);
510 if ((fp = fopen (etcpath (file), "r")) == NULL)
511 adios (file, "unable to open format file");
513 if (fstat (fileno (fp), &st) != NOTOK) {
519 global.c_ovtxt = global.c_nfs = NULL;
523 if ((i = sc_width ()) > 5)
525 global.c_cwidth = -1;
526 if ((i = sc_length ()) > 5)
527 global.c_length = i - 1;
528 global.c_flags = BELL; /* BELL is default */
529 *(ip = ignores) = NULL;
531 while (vfgets (fp, &ap) == OK) {
536 if ((cp = strchr(bp, '\n')))
540 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
545 strncpy (name, parse(), sizeof(name));
551 * Split this list of fields to ignore, and copy
552 * it to the end of the current "ignores" list.
554 if (!strcasecmp (name, "ignores")) {
555 char **tmparray, **p;
558 /* split the fields */
559 tmparray = brkstring (getcpy (++parptr), ",", NULL);
561 /* count number of fields split */
566 /* copy pointers to split fields to ignores array */
567 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
573 if (evalvar (&global))
574 adios (NULL, "format file syntax error: %s", bp);
581 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
582 while (*parptr == ':' || *parptr == ',') {
585 adios (NULL, "format file syntax error: %s", bp);
587 if (!c1->c_nfs && global.c_nfs)
588 if (c1->c_flags & DATEFMT) {
589 if (global.c_flags & DATEFMT)
590 c1->c_nfs = getcpy (global.c_nfs);
593 if (c1->c_flags & ADDRFMT) {
594 if (global.c_flags & ADDRFMT)
595 c1->c_nfs = getcpy (global.c_nfs);
600 adios (NULL, "format file syntax error: %s", bp);
606 for (c1 = fmthd; c1; c1 = c1->c_next) {
607 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
608 c1->c_name, c1->c_text, c1->c_ovtxt);
609 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
610 (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
611 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
612 c1->c_offset, c1->c_ovoff, c1->c_width,
613 c1->c_cwidth, c1->c_length);
614 fprintf (stderr, "\tflags=%s\n",
615 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
621 global.c_flags |= CLEARSCR;
624 global.c_flags &= ~CLEARSCR;
627 switch (bellflg) { /* command line may override format file */
629 global.c_flags |= BELL;
632 global.c_flags &= ~BELL;
637 global.c_length = length;
639 global.c_width = width;
640 if (global.c_length < 5)
641 global.c_length = 10000;
642 if (global.c_width < 5)
643 global.c_width = 10000;
648 evalvar (struct mcomp *c1)
650 char *cp, name[NAMESZ];
655 strncpy (name, parse(), sizeof(name));
657 if (!strcasecmp (name, "component")) {
658 if (ptos (name, &c1->c_text))
660 c1->c_flags &= ~NOCOMPONENT;
664 if (!strcasecmp (name, "overflowtext"))
665 return ptos (name, &c1->c_ovtxt);
667 if (!strcasecmp (name, "formatfield")) {
670 if (ptos (name, &cp))
672 nfs = new_fs (NULL, NULL, cp);
673 c1->c_nfs = getcpy (nfs);
674 c1->c_flags |= FORMAT;
678 if (!strcasecmp (name, "decode")) {
681 nfs = new_fs (NULL, NULL, "%(decode{text})");
682 c1->c_nfs = getcpy (nfs);
683 c1->c_flags |= FORMAT;
687 if (!strcasecmp (name, "offset"))
688 return ptoi (name, &c1->c_offset);
689 if (!strcasecmp (name, "overflowoffset"))
690 return ptoi (name, &c1->c_ovoff);
691 if (!strcasecmp (name, "width"))
692 return ptoi (name, &c1->c_width);
693 if (!strcasecmp (name, "compwidth"))
694 return ptoi (name, &c1->c_cwidth);
695 if (!strcasecmp (name, "length"))
696 return ptoi (name, &c1->c_length);
697 if (!strcasecmp (name, "nodashstuffing"))
698 return (dashstuff = -1);
700 for (ap = triples; ap->t_name; ap++)
701 if (!strcasecmp (ap->t_name, name)) {
702 c1->c_flags |= ap->t_on;
703 c1->c_flags &= ~ap->t_off;
712 ptoi (char *name, int *i)
716 if (*parptr++ != '=' || !*(cp = parse ())) {
717 advise (NULL, "missing argument to variable %s", name);
727 ptos (char *name, char **s)
731 if (*parptr++ != '=') {
732 advise (NULL, "missing argument to variable %s", name);
736 if (*parptr != '"') {
738 *parptr && *parptr != ':' && *parptr != ',';
742 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
743 if (*parptr == QUOTE)
750 if ((*parptr = c) == '"')
761 static char result[NAMESZ];
763 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
782 process (char *folder, char *fname, int ofilen, int ofilec)
788 switch (setjmp (env)) {
791 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
793 advise (fname, "unable to open");
801 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
803 SIGNAL (SIGINT, intrser);
804 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
808 SIGNAL (SIGINT, SIG_IGN);
809 if (mhl_action == NULL && fp != stdin)
813 free (holder.c_text);
814 holder.c_text = NULL;
816 free_queue (&msghd, &msgtl);
817 for (c1 = fmthd; c1; c1 = c1->c_next)
818 c1->c_flags &= ~HDROUTPUT;
825 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
828 struct mcomp *c1, *c2, *c3;
829 char **ip, name[NAMESZ], buf[BUFSIZ];
833 printf ("%s", ofilen == 1 ? delim3 : delim4);
835 printf ("\n-------");
837 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
839 printf (" Message %d", ofilen);
847 if ((global.c_flags & CLEARSCR))
852 printf (">>> %s\n\n", mname);
857 strncpy (buf, "\n", sizeof(buf));
859 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
862 printf ("Press <return> to list \"%s\"...", mname);
866 read (fileno (stdout), buf, sizeof(buf));
868 if (strchr(buf, '\n')) {
869 if ((global.c_flags & CLEARSCR))
883 printf (">>> %s\n\n", mname);
889 for (state = FLD;;) {
890 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
893 for (ip = ignores; *ip; ip++)
894 if (!strcasecmp (name, *ip)) {
895 while (state == FLDPLUS)
896 state = m_getfld (state, name, buf, sizeof(buf), fp);
902 for (c2 = fmthd; c2; c2 = c2->c_next)
903 if (!strcasecmp (c2->c_name, name))
906 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
907 for (c1 = msghd; c1; c1 = c1->c_next)
908 if (!strcasecmp (c1->c_name, c3->c_name)) {
910 mcomp_add (c1->c_flags, buf, c1->c_text);
914 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
915 while (state == FLDPLUS) {
916 state = m_getfld (state, name, buf, sizeof(buf), fp);
917 c1->c_text = add (buf, c1->c_text);
920 c1->c_flags |= EXTRA;
926 for (c1 = fmthd; c1; c1 = c1->c_next) {
927 if (c1->c_flags & CLEARTEXT) {
928 putcomp (c1, c1, ONECOMP);
931 if (!strcasecmp (c1->c_name, "messagename")) {
932 holder.c_text = concat ("(Message ", mname, ")\n",
934 putcomp (c1, &holder, ONECOMP);
935 free (holder.c_text);
936 holder.c_text = NULL;
939 if (!strcasecmp (c1->c_name, "extras")) {
940 for (c2 = msghd; c2; c2 = c2->c_next)
941 if (c2->c_flags & EXTRA)
942 putcomp (c1, c2, TWOCOMP);
945 if (dobody && !strcasecmp (c1->c_name, "body")) {
946 if ((holder.c_text = malloc (sizeof(buf))) == NULL)
947 adios (NULL, "unable to allocate buffer memory");
948 strncpy (holder.c_text, buf, sizeof(buf));
949 while (state == BODY) {
950 putcomp (c1, &holder, BODYCOMP);
951 state = m_getfld (state, name, holder.c_text,
954 free (holder.c_text);
955 holder.c_text = NULL;
958 for (c2 = msghd; c2; c2 = c2->c_next)
959 if (!strcasecmp (c2->c_name, c1->c_name)) {
960 putcomp (c1, c2, ONECOMP);
961 if (!(c1->c_flags & SPLIT))
964 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
965 for (c2 = msghd; c2; c2 = c2->c_next)
966 if (c2->c_flags & FACEDFLT) {
967 if (c2->c_face == NULL)
969 if ((holder.c_text = c2->c_face)) {
970 putcomp (c1, &holder, ONECOMP);
971 holder.c_text = NULL;
980 advise (NULL, "format error in message %s", mname);
985 adios (NULL, "getfld() returned %d", state);
992 mcomp_flags (char *name)
996 for (ap = pairs; ap->p_name; ap++)
997 if (!strcasecmp (ap->p_name, name))
998 return (ap->p_flags);
1005 mcomp_add (long flags, char *s1, char *s2)
1009 if (!(flags & ADDRFMT))
1010 return add (s1, s2);
1012 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1015 return add (s1, add (",\n", s2));
1022 struct pqpair *pq_next;
1027 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1031 char buffer[BUFSIZ], error[BUFSIZ];
1033 struct pqpair *p, *q;
1035 struct mailname *mp;
1042 dat[3] = sizeof(buffer) - 1;
1044 fmt_compile (c1->c_nfs, &c1->c_fmt);
1046 if (!(c1->c_flags & ADDRFMT)) {
1047 FINDCOMP (cptr, "text");
1050 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1054 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1055 /* Don't need to append a newline, dctime() already did */
1056 c2->c_text = getcpy (buffer);
1062 (q = &pq)->pq_next = NULL;
1063 while ((cp = getname (ap))) {
1064 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1065 adios (NULL, "unable to allocate pqpair memory");
1067 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1068 p->pq_text = getcpy (cp);
1069 p->pq_error = getcpy (error);
1071 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1073 if ((h = mp->m_host) == NULL)
1075 if ((o = OfficialName (h)))
1077 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1080 p->pq_text = getcpy (mp->m_text);
1083 q = (q->pq_next = p);
1086 for (p = pq.pq_next; p; p = q) {
1087 FINDCOMP (cptr, "text");
1089 cptr->c_text = p->pq_text;
1090 FINDCOMP (cptr, "error");
1092 cptr->c_text = p->pq_error;
1094 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1097 c2->c_text = add (",\n", c2->c_text);
1098 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1100 c2->c_text = add (buffer, c2->c_text);
1110 c2->c_text = add ("\n", c2->c_text);
1115 static struct mcomp *
1116 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1120 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1121 adios (NULL, "unable to allocate comp memory");
1123 c1->c_flags = flags & ~INIT;
1124 if ((c1->c_name = name ? getcpy (name) : NULL))
1125 c1->c_flags |= mcomp_flags (c1->c_name);
1126 c1->c_text = text ? getcpy (text) : NULL;
1129 c1->c_ovtxt = getcpy (global.c_ovtxt);
1130 c1->c_offset = global.c_offset;
1131 c1->c_ovoff = global. c_ovoff;
1132 c1->c_width = c1->c_length = 0;
1133 c1->c_cwidth = global.c_cwidth;
1134 c1->c_flags |= global.c_flags & GFLAGS;
1139 (*tail)->c_next = c1;
1147 free_queue (struct mcomp **head, struct mcomp **tail)
1149 struct mcomp *c1, *c2;
1151 for (c1 = *head; c1; c1 = c2) {
1162 free ((char *) c1->c_fmt);
1168 *head = *tail = NULL;
1173 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1180 llim = c1->c_length ? c1->c_length : -1;
1181 wid = c1->c_width ? c1->c_width : global.c_width;
1182 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1184 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1186 if (wid < ovoff + strlen (ovtxt) + 5)
1187 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1188 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1191 if (c1->c_flags & CLEARTEXT) {
1192 putstr (c1->c_text);
1197 if (c1->c_flags & FACEFMT)
1198 switch (doface (c2)) {
1199 case NOTOK: /* error */
1200 case OK: /* async faceproc */
1203 default: /* sync faceproc */
1207 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1208 mcomp_format (c1, c2);
1210 if (c1->c_flags & CENTER) {
1211 count = (c1->c_width ? c1->c_width : global.c_width)
1212 - c1->c_offset - strlen (c2->c_text);
1213 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1214 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1215 lm = c1->c_offset + (count / 2);
1221 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1222 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1223 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1225 *cp = toupper (*cp);
1226 putstr (c1->c_text ? c1->c_text : c1->c_name);
1227 if (flag != BODYCOMP) {
1229 if (!(c1->c_flags & SPLIT))
1230 c1->c_flags |= HDROUTPUT;
1233 if ((count = c1->c_cwidth -
1234 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1239 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1243 && !(c2->c_flags & HDROUTPUT)
1244 && !(c2->c_flags & NOCOMPONENT)) {
1245 if (c1->c_flags & UPPERCASE)
1246 for (cp = c2->c_name; *cp; cp++)
1248 *cp = toupper (*cp);
1249 putstr (c2->c_name);
1251 if (!(c1->c_flags & SPLIT))
1252 c2->c_flags |= HDROUTPUT;
1255 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1259 if (c1->c_flags & UPPERCASE)
1260 for (cp = c2->c_text; *cp; cp++)
1262 *cp = toupper (*cp);
1266 if (flag == TWOCOMP)
1267 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1268 : strlen (c2->c_name) + 2;
1270 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1271 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1272 count += c1->c_offset;
1274 if ((cp = oneline (c2->c_text, c1->c_flags)))
1278 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1280 if (flag == BODYCOMP
1281 && !(c1->c_flags & NOCOMPONENT))
1282 putstr (c1->c_text ? c1->c_text : c1->c_name);
1292 oneline (char *stuff, long flags)
1300 return (onelp = NULL);
1304 if (flags & COMPRESS) {
1305 for (spc = 1, cp = ret; *onelp; onelp++)
1306 if (isspace (*onelp)) {
1307 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1326 while (*onelp && *onelp != '\n')
1328 if (*onelp == '\n') {
1332 if (flags & LEFTADJUST)
1333 while (*ret == ' ' || *ret == '\t')
1336 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1344 putstr (char *string)
1346 if (!column && lm > 0)
1376 if (ontty != ISTTY || row != global.c_length)
1378 if (global.c_flags & BELL)
1382 read (fileno (stdout), buf, sizeof(buf));
1383 if (strchr(buf, '\n')) {
1384 if (global.c_flags & CLEARSCR)
1389 row = global.c_length / 3;
1408 * If we are forwarding this message, and the first
1409 * column contains a dash, then add a dash and a space.
1411 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1420 if (column >= wid) {
1424 putstr (ovtxt ? ovtxt : "");
1436 #ifndef RELIABLE_SIGNALS
1437 SIGNAL (SIGINT, intrser);
1442 longjmp (env, DONE);
1449 #ifndef RELIABLE_SIGNALS
1450 SIGNAL (SIGPIPE, pipeser);
1460 #ifndef RELIABLE_SIGNALS
1461 SIGNAL (SIGQUIT, quitser);
1471 face_format (struct mcomp *c1)
1474 struct mailname *mp;
1476 if ((cp = c1->c_text) == NULL)
1479 if ((cp = getname (cp))) {
1480 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1482 if ((h = mp->m_host) == NULL)
1484 if ((o = OfficialName (h)))
1486 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1489 while ((cp = getname (cp)))
1496 * faceproc is two elements defining the image agent's location:
1501 #include <sys/socket.h>
1502 #include <netinet/in.h>
1505 #ifdef HAVE_ARPA_INET_H
1506 # include <arpa/inet.h>
1510 doface (struct mcomp *c1)
1513 struct sockaddr_in in_socket;
1514 struct sockaddr_in *isock = &in_socket;
1515 static int inited = OK;
1517 static struct in_addr addr;
1518 static unsigned short portno;
1522 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1525 if (ap[0] == NULL || ap[1] == NULL) {
1528 return (inited = NOTOK);
1531 if (!(hp = gethostbystring (ap[0])))
1533 memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1535 portno = htons ((unsigned short) atoi (ap[1]));
1540 if (inited == NOTOK)
1543 isock->sin_family = AF_INET;
1544 isock->sin_port = portno;
1545 memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1547 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1550 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1551 (struct sockaddr *) isock, sizeof(*isock));
1555 return (result != NOTOK ? OK : NOTOK);
1560 * This version doesn't use sockets
1565 doface (struct mcomp *c1)
1569 int result, pdi[2], pdo[2];
1571 char buffer[BUFSIZ], *vec[10];
1573 if (pipe (pdi) == NOTOK)
1575 if (pipe (pdo) == NOTOK) {
1581 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1586 /* oops... fork error */
1591 SIGNAL (SIGINT, SIG_IGN);
1592 SIGNAL (SIGQUIT, SIG_IGN);
1593 if (pdi[0] != fileno (stdin)) {
1594 dup2 (pdi[0], fileno (stdin));
1599 if (pdo[1] != fileno (stdout)) {
1600 dup2 (pdo[1], fileno (stdout));
1604 vec[vecp++] = r1bindex (faceproc, '/');
1606 if (sleepsw != NOTOK) {
1608 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1609 vec[vecp++] = buffer;
1612 execvp (faceproc, vec);
1613 fprintf (stderr, "unable to exec ");
1615 _exit (-1); /* NOTREACHED */
1618 /* parent process */
1620 i = strlen (c1->c_text);
1621 if (write (pdi[1], c1->c_text, i) != i)
1622 adios ("pipe", "error writing to");
1623 free (c1->c_text), c1->c_text = NULL;
1629 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1633 if ((dp = realloc (cp, (unsigned) (j = len + i))) == NULL)
1634 adios (NULL, "unable to allocate face storage");
1635 memcpy(dp + len, buffer, i);
1639 if ((cp = malloc ((unsigned) i)) == NULL)
1640 adios (NULL, "unable to allocate face storage");
1641 memcpy(cp, buffer, i);
1645 for (bp = buffer + i - 1; bp >= buffer; bp--)
1646 if (!isascii (*bp) || iscntrl (*bp)) {
1653 /* no waiting for child... */
1655 if (result == OK) { /* binary */
1656 if (write (1, cp, len) != len)
1657 adios ("writing", "error");
1661 if ((c1->c_text = cp) == NULL)
1668 #endif /* COMMENTED OUT */
1672 mhlsbr (int argc, char **argv, FILE *(*action)())
1674 SIGNAL_HANDLER istat, pstat, qstat;
1678 switch (setjmp (mhlenv)) {
1681 sleepsw = 0; /* XXX */
1682 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1685 mhl_action = action;
1688 * If signal is at default action, then start ignoring
1689 * it, else let it set to its current action.
1691 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1692 SIGNAL (SIGINT, istat);
1693 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1694 SIGNAL (SIGQUIT, qstat);
1695 pstat = SIGNAL (SIGPIPE, pipeser);
1696 mhl (argc, argv); /* FALL THROUGH! */
1699 SIGNAL (SIGINT, istat);
1700 SIGNAL (SIGQUIT, qstat);
1701 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1704 SIGNAL (SIGPIPE, pstat);
1706 if (holder.c_text) {
1707 free (holder.c_text);
1708 holder.c_text = NULL;
1710 free_queue (&msghd, &msgtl);
1711 for (c1 = fmthd; c1; c1 = c1->c_next)
1712 c1->c_flags &= ~HDROUTPUT;
1721 mhladios (char *what, char *fmt, ...)
1726 advertise (what, NULL, fmt, ap);
1733 mhldone (int status)
1737 longjmp (mhlenv, DONE);
1743 static int m_pid = NOTOK;
1744 static int sd = NOTOK;
1747 m_popen (char *name)
1751 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1752 adios ("standard output", "unable to dup()");
1754 if (pipe (pd) == NOTOK)
1755 adios ("pipe", "unable to");
1757 switch (m_pid = vfork ()) {
1759 adios ("fork", "unable to");
1762 SIGNAL (SIGINT, SIG_DFL);
1763 SIGNAL (SIGQUIT, SIG_DFL);
1766 if (pd[0] != fileno (stdin)) {
1767 dup2 (pd[0], fileno (stdin));
1770 execlp (name, r1bindex (name, '/'), NULL);
1771 fprintf (stderr, "unable to exec ");
1777 if (pd[1] != fileno (stdout)) {
1778 dup2 (pd[1], fileno (stdout));
1793 if (dup2 (sd, fileno (stdout)) == NOTOK)
1794 adios ("standard output", "unable to dup2()");
1803 pidwait (m_pid, OK);