2 ** mhlsbr.c -- main routines for nmh message lister
4 ** This code is Copyright (c) 2002, by the authors of nmh. See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
10 #include <h/signals.h>
11 #include <h/addrsbr.h>
12 #include <h/fmt_scan.h>
20 ** for a component containing addresses, ADDRFMT, if COMPRESS is also
21 ** set, then addresses get split wrong (not at the spaces between commas).
22 ** To fix this correctly, putstr() should know about "atomic" strings that
23 ** must NOT be broken across lines. That's too difficult for right now
24 ** (it turns out that there are a number of degernate cases), so in
25 ** oneline(), instead of
27 ** (*onelp == '\n' && !onelp[1])
29 ** being a terminating condition,
31 ** (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
33 ** is used instead. This cuts the line prematurely, and gives us a much
34 ** better chance of getting things right.
43 static struct swit mhlswitches[] = {
53 { "folder +folder", 0 },
55 { "form formfile", 0 },
57 { "moreproc program", 0 },
61 { "length lines", 0 },
63 { "width columns", 0 },
65 { "sleep seconds", 0 },
67 { "dashstuffing", -12 }, /* interface from forw */
68 #define NBITSTUFFSW 12
69 { "nodashstuffing", -14 }, /* interface from forw */
75 { "forward", -7 }, /* interface from forw */
77 { "forwall", -7 }, /* interface from forw */
79 { "digest list", -6 },
81 { "volume number", -6 },
83 { "issue number", -5 },
89 #define NOCOMPONENT 0x000001 /* don't show component name */
90 #define UPPERCASE 0x000002 /* display in all upper case */
91 #define CENTER 0x000004 /* center line */
92 #define CLEARTEXT 0x000008 /* cleartext */
93 #define EXTRA 0x000010 /* an "extra" component */
94 #define HDROUTPUT 0x000020 /* already output */
95 #define CLEARSCR 0x000040 /* clear screen */
96 #define LEFTADJUST 0x000080 /* left justify multiple lines */
97 #define COMPRESS 0x000100 /* compress text */
98 #define ADDRFMT 0x000200 /* contains addresses */
99 #define BELL 0x000400 /* sound bell at EOP */
100 #define DATEFMT 0x000800 /* contains dates */
101 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
102 #define INIT 0x002000 /* initialize component */
103 #define SPLIT 0x004000 /* split headers (don't concatenate) */
104 #define NONEWLINE 0x008000 /* don't write trailing newline */
105 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017SPLIT\020NONEWLINE"
106 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
109 char *c_name; /* component name */
110 char *c_text; /* component text */
111 char *c_ovtxt; /* text overflow indicator */
112 char *c_nfs; /* iff FORMAT */
113 struct format *c_fmt; /* .. */
114 int c_offset; /* left margin indentation */
115 int c_ovoff; /* overflow indentation */
116 int c_width; /* width of field */
117 int c_cwidth; /* width of component */
118 int c_length; /* length in lines */
120 struct mcomp *c_next;
123 static struct mcomp *msghd = NULL;
124 static struct mcomp *msgtl = NULL;
125 static struct mcomp *fmthd = NULL;
126 static struct mcomp *fmttl = NULL;
128 static struct mcomp global = {
129 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
132 static struct mcomp holder = {
133 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
141 static struct pair pairs[] = {
144 { "Sender", ADDRFMT },
145 { "Reply-To", ADDRFMT },
149 { "Resent-Date", DATEFMT },
150 { "Resent-From", ADDRFMT },
151 { "Resent-Sender", ADDRFMT },
152 { "Resent-Reply-To", ADDRFMT },
153 { "Resent-To", ADDRFMT },
154 { "Resent-Cc", ADDRFMT },
155 { "Resent-Bcc", ADDRFMT },
165 static struct triple triples[] = {
166 { "nocomponent", NOCOMPONENT, 0 },
167 { "uppercase", UPPERCASE, 0 },
168 { "nouppercase", 0, UPPERCASE },
169 { "center", CENTER, 0 },
170 { "nocenter", 0, CENTER },
171 { "clearscreen", CLEARSCR, 0 },
172 { "noclearscreen", 0, CLEARSCR },
173 { "noclear", 0, CLEARSCR },
174 { "leftadjust", LEFTADJUST, 0 },
175 { "noleftadjust", 0, LEFTADJUST },
176 { "compress", COMPRESS, 0 },
177 { "nocompress", 0, COMPRESS },
178 { "split", SPLIT, 0 },
179 { "nosplit", 0, SPLIT },
180 { "addrfield", ADDRFMT, DATEFMT },
182 { "nobell", 0, BELL },
183 { "datefield", DATEFMT, ADDRFMT },
184 { "newline", 0, NONEWLINE },
185 { "nonewline", NONEWLINE, 0 },
190 static int bellflg = 0;
191 static int clearflg = 0;
192 static int dashstuff = 0;
193 static int dobody = 1;
194 static int forwflg = 0;
195 static int forwall = 0;
197 static int sleepsw = NOTOK;
199 static char *digest = NULL;
200 static int volume = 0;
201 static int issue = 0;
203 static int exitstat = 0;
204 static int mhldebug = 0;
209 static int ontty = NOTTY;
222 static unsigned char *onelp;
226 static int num_ignores = 0;
227 static char *ignores[MAXARGS];
230 static jmp_buf mhlenv;
232 static char delim3[] = /* from forw.c */
233 "\n----------------------------------------------------------------------\n\n";
234 static char delim4[] = "\n------------------------------\n\n";
236 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
240 ** Redefine a couple of functions.
241 ** These are undefined later in the code.
243 #define adios mhladios
249 static void mhl_format(char *, int, int);
250 static int evalvar(struct mcomp *);
251 static int ptoi(char *, int *);
252 static int ptos(char *, char **);
253 static char *parse(void);
254 static void process(char *, char *, int, int);
255 static void mhlfile(FILE *, char *, int, int);
256 static int mcomp_flags(char *);
257 static char *mcomp_add(long, char *, char *);
258 static void mcomp_format(struct mcomp *, struct mcomp *);
259 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
260 char *, char *, int);
261 static void free_queue(struct mcomp **, struct mcomp **);
262 static void putcomp(struct mcomp *, struct mcomp *, int);
263 static char *oneline(char *, long);
264 static void putstr(char *);
265 static void putch(char);
266 static RETSIGTYPE intrser(int);
267 static RETSIGTYPE pipeser(int);
268 static RETSIGTYPE quitser(int);
269 static void mhladios(char *, char *, ...);
270 static void mhldone(int);
271 static void m_popen(char *);
273 int mhl(int, char **);
274 int mhlsbr(int, char **, FILE *(*)());
277 void clear_screen(void); /* from termsbr.c */
278 int SOprintf(char *, ...); /* from termsbr.c */
279 int sc_width(void); /* from termsbr.c */
280 int sc_length(void); /* from termsbr.c */
281 int sc_hardcopy(void); /* from termsbr.c */
285 mhl(int argc, char **argv)
287 int length = 0, nomore = 0;
288 int i, width = 0, vecp = 0;
289 char *cp, *folder = NULL, *form = NULL;
290 char buf[BUFSIZ], *files[MAXARGS];
291 char **argp, **arguments;
293 invo_name = mhbasename(argv[0]);
295 /* read user profile/context */
298 arguments = getarguments(invo_name, argc, argv, 1);
301 if ((cp = getenv("MHLDEBUG")) && *cp)
304 while ((cp = *argp++)) {
306 switch (smatch(++cp, mhlswitches)) {
308 ambigsw(cp, mhlswitches);
311 adios(NULL, "-%s unknown\n", cp);
314 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
315 print_help(buf, mhlswitches, 1);
318 print_version(invo_name);
336 if (!(folder = *argp++) || *folder == '-')
337 adios(NULL, "missing argument to %s",
341 if (!(form = *argp++) || *form == '-')
342 adios(NULL, "missing argument to %s",
347 if (!(cp = *argp++) || *cp == '-')
348 adios(NULL, "missing argument to %s",
350 sleepsw = atoi(cp); /* ZERO ok! */
354 if (!(moreproc = *argp++) || *moreproc == '-')
355 adios(NULL, "missing argument to %s",
363 if (!(cp = *argp++) || *cp == '-')
364 adios(NULL, "missing argument to %s",
366 if ((length = atoi(cp)) < 1)
367 adios(NULL, "bad argument %s %s",
371 if (!(cp = *argp++) || *cp == '-')
372 adios(NULL, "missing argument to %s",
374 if ((width = atoi(cp)) < 1)
375 adios(NULL, "bad argument %s %s",
380 if (!(digest = *argp++) ||
382 adios(NULL, "missing argument to %s",
386 if (!(cp = *argp++) || *cp == '-')
387 adios(NULL, "missing argument to %s",
389 if ((issue = atoi(cp)) < 1)
390 adios(NULL, "bad argument %s %s",
394 if (!(cp = *argp++) || *cp == '-')
395 adios(NULL, "missing argument to %s",
397 if ((volume = atoi(cp)) < 1)
398 adios(NULL, "bad argument %s %s",
403 forwall++; /* fall */
406 clearflg = -1;/* XXX */
410 dashstuff = 1; /* trinary logic */
413 dashstuff = -1; /* trinary logic */
425 folder = getenv("mhfolder");
427 if (isatty(fileno(stdout))) {
428 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
430 SIGNAL(SIGINT, SIG_IGN);
431 SIGNAL2(SIGQUIT, quitser);
433 SIGNAL2(SIGPIPE, pipeser);
437 SIGNAL(SIGINT, SIG_IGN);
438 SIGNAL2(SIGQUIT, quitser);
445 mhl_format(form ? form : mhlformat, length, width);
448 process(folder, NULL, 1, vecp = 1);
450 for (i = 0; i < vecp; i++)
451 process(folder, files[i], i + 1, vecp);
456 printf("%s", delim4);
458 snprintf(buf, sizeof(buf),
459 "End of %s Digest\n", digest);
461 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
464 for (cp = buf + i; i > 1; i--)
470 printf("\n------- End of Forwarded Message%s\n\n",
471 vecp > 1 ? "s" : "");
475 if (ferror(stdout)) {
476 adios("output", "error writing");
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,
548 strncpy(name, parse(), sizeof(name));
554 ** Split this list of fields to ignore, and copy
555 ** it to the end of the current "ignores" list.
557 if (!mh_strcasecmp(name, "ignores")) {
558 char **tmparray, **p;
561 /* split the fields */
562 tmparray = brkstring(getcpy(++parptr), ",",
565 /* count number of fields split */
571 ** copy pointers to split fields
574 ip = copyip(tmparray, ip,
575 MAXARGS - num_ignores);
581 if (evalvar(&global))
582 adios(NULL, "format file syntax error: %s", bp);
589 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
590 while (*parptr == ':' || *parptr == ',') {
593 adios(NULL, "format file syntax error: %s", bp);
595 if (!c1->c_nfs && global.c_nfs) {
596 if ((c1->c_flags & DATEFMT) &&
597 (global.c_flags & DATEFMT)) {
598 c1->c_nfs = getcpy(global.c_nfs);
599 } else if ((c1->c_flags & ADDRFMT) &&
600 (global.c_flags & ADDRFMT)) {
601 c1->c_nfs = getcpy(global.c_nfs);
607 adios(NULL, "format file syntax error: %s", bp);
613 for (c1 = fmthd; c1; c1 = c1->c_next) {
614 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
615 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
616 fprintf(stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n", c1->c_offset, c1->c_ovoff, c1->c_width, c1->c_cwidth, c1->c_length);
617 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
623 global.c_flags |= CLEARSCR;
626 global.c_flags &= ~CLEARSCR;
629 switch (bellflg) { /* command line may override format file */
631 global.c_flags |= BELL;
634 global.c_flags &= ~BELL;
639 global.c_length = length;
641 global.c_width = width;
642 if (global.c_length < 5)
643 global.c_length = 10000;
644 if (global.c_width < 5)
645 global.c_width = 10000;
650 evalvar(struct mcomp *c1)
652 char *cp, name[NAMESZ];
657 strncpy(name, parse(), sizeof(name));
659 if (!mh_strcasecmp(name, "component")) {
660 if (ptos(name, &c1->c_text))
662 c1->c_flags &= ~NOCOMPONENT;
666 if (!mh_strcasecmp(name, "overflowtext"))
667 return ptos(name, &c1->c_ovtxt);
669 if (!mh_strcasecmp(name, "formatfield")) {
674 nfs = new_fs(NULL, NULL, cp);
675 c1->c_nfs = getcpy(nfs);
676 c1->c_flags |= FORMAT;
680 if (!mh_strcasecmp(name, "decode")) {
683 nfs = new_fs(NULL, NULL, "%(decode{text})");
684 c1->c_nfs = getcpy(nfs);
685 c1->c_flags |= FORMAT;
689 if (!mh_strcasecmp(name, "offset"))
690 return ptoi(name, &c1->c_offset);
691 if (!mh_strcasecmp(name, "overflowoffset"))
692 return ptoi(name, &c1->c_ovoff);
693 if (!mh_strcasecmp(name, "width"))
694 return ptoi(name, &c1->c_width);
695 if (!mh_strcasecmp(name, "compwidth"))
696 return ptoi(name, &c1->c_cwidth);
697 if (!mh_strcasecmp(name, "length"))
698 return ptoi(name, &c1->c_length);
699 if (!mh_strcasecmp(name, "nodashstuffing"))
700 return (dashstuff = -1);
702 for (ap = triples; ap->t_name; ap++)
703 if (!mh_strcasecmp(ap->t_name, name)) {
704 c1->c_flags |= ap->t_on;
705 c1->c_flags &= ~ap->t_off;
714 ptoi(char *name, int *i)
718 if (*parptr++ != '=' || !*(cp = parse())) {
719 advise(NULL, "missing argument to variable %s", name);
729 ptos(char *name, char **s)
733 if (*parptr++ != '=') {
734 advise(NULL, "missing argument to variable %s", name);
738 if (*parptr != '"') {
739 for (cp = parptr; *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) :
795 advise(fname, "unable to open");
803 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
805 SIGNAL(SIGINT, intrser);
806 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
810 SIGNAL(SIGINT, SIG_IGN);
811 if (mhl_action == NULL && fp != stdin)
816 holder.c_text = NULL;
818 free_queue(&msghd, &msgtl);
819 for (c1 = fmthd; c1; c1 = c1->c_next)
820 c1->c_flags &= ~HDROUTPUT;
827 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
830 struct mcomp *c1, *c2, *c3;
831 char **ip, name[NAMESZ], buf[BUFSIZ];
835 printf("%s", ofilen == 1 ? delim3 : delim4);
839 printf(" Forwarded Message%s",
840 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))
885 printf(">>> %s\n\n", mname);
891 for (state = FLD;;) {
892 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
895 for (ip = ignores; *ip; ip++)
896 if (!mh_strcasecmp(name, *ip)) {
897 while (state == FLDPLUS)
898 state = m_getfld(state, name, buf, sizeof(buf), fp);
904 for (c2 = fmthd; c2; c2 = c2->c_next)
905 if (!mh_strcasecmp(c2->c_name, name))
908 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
909 for (c1 = msghd; c1; c1 = c1->c_next)
910 if (!mh_strcasecmp(c1->c_name,
912 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
916 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
917 while (state == FLDPLUS) {
918 state = m_getfld(state, name, buf,
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 (!mh_strcasecmp(c1->c_name, "messagename")) {
935 holder.c_text = concat("(Message ",
937 putcomp(c1, &holder, ONECOMP);
939 holder.c_text = NULL;
942 if (!mh_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 && !mh_strcasecmp(c1->c_name, "body")) {
949 holder.c_text = mh_xmalloc(sizeof(buf));
950 strncpy(holder.c_text, buf, sizeof(buf));
951 while (state == BODY) {
952 putcomp(c1, &holder, BODYCOMP);
953 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
956 holder.c_text = NULL;
959 for (c2 = msghd; c2; c2 = c2->c_next)
960 if (!mh_strcasecmp(c2->c_name,
962 putcomp(c1, c2, ONECOMP);
963 if (!(c1->c_flags & SPLIT))
971 advise(NULL, "format error in message %s", mname);
976 adios(NULL, "getfld() returned %d", state);
983 mcomp_flags(char *name)
987 for (ap = pairs; ap->p_name; ap++)
988 if (!mh_strcasecmp(ap->p_name, name))
989 return (ap->p_flags);
996 mcomp_add(long flags, char *s1, char *s2)
1000 if (!(flags & ADDRFMT))
1003 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
1006 return add(s1, add(",\n", s2));
1013 struct pqpair *pq_next;
1018 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1022 char buffer[BUFSIZ], error[BUFSIZ];
1024 struct pqpair *p, *q;
1026 struct mailname *mp;
1033 dat[3] = sizeof(buffer) - 1;
1035 fmt_compile(c1->c_nfs, &c1->c_fmt);
1037 if (!(c1->c_flags & ADDRFMT)) {
1038 FINDCOMP(cptr, "text");
1041 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1045 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1046 /* Don't need to append a newline, dctime() already did */
1047 c2->c_text = getcpy(buffer);
1053 (q = &pq)->pq_next = NULL;
1054 while ((cp = getname(ap))) {
1055 if ((p = (struct pqpair *)
1056 calloc((size_t) 1, sizeof(*p))) == NULL)
1057 adios(NULL, "unable to allocate pqpair memory");
1059 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1060 p->pq_text = getcpy(cp);
1061 p->pq_error = getcpy(error);
1063 p->pq_text = getcpy(mp->m_text);
1066 q = (q->pq_next = p);
1069 for (p = pq.pq_next; p; p = q) {
1070 FINDCOMP(cptr, "text");
1072 cptr->c_text = p->pq_text;
1073 FINDCOMP(cptr, "error");
1075 cptr->c_text = p->pq_error;
1077 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1080 c2->c_text = add(",\n", c2->c_text);
1081 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1083 c2->c_text = add(buffer, c2->c_text);
1093 c2->c_text = add("\n", c2->c_text);
1098 static struct mcomp *
1099 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1100 char *text, int flags)
1104 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1105 adios(NULL, "unable to allocate comp memory");
1107 c1->c_flags = flags & ~INIT;
1108 if ((c1->c_name = name ? getcpy(name) : NULL))
1109 c1->c_flags |= mcomp_flags(c1->c_name);
1110 c1->c_text = text ? getcpy(text) : NULL;
1113 c1->c_ovtxt = getcpy(global.c_ovtxt);
1114 c1->c_offset = global.c_offset;
1115 c1->c_ovoff = global. c_ovoff;
1116 c1->c_width = c1->c_length = 0;
1117 c1->c_cwidth = global.c_cwidth;
1118 c1->c_flags |= global.c_flags & GFLAGS;
1123 (*tail)->c_next = c1;
1131 free_queue(struct mcomp **head, struct mcomp **tail)
1133 struct mcomp *c1, *c2;
1135 for (c1 = *head; c1; c1 = c2) {
1146 free((char *) c1->c_fmt);
1150 *head = *tail = NULL;
1155 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1162 llim = c1->c_length ? c1->c_length : -1;
1163 wid = c1->c_width ? c1->c_width : global.c_width;
1164 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1166 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1168 if (wid < ovoff + strlen(ovtxt) + 5)
1169 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1172 if (c1->c_flags & CLEARTEXT) {
1178 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1179 mcomp_format(c1, c2);
1181 if (c1->c_flags & CENTER) {
1182 count = (c1->c_width ? c1->c_width : global.c_width)
1183 - c1->c_offset - strlen(c2->c_text);
1184 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1185 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1187 lm = c1->c_offset + (count / 2);
1193 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1194 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1195 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1198 putstr(c1->c_text ? c1->c_text : c1->c_name);
1199 if (flag != BODYCOMP) {
1201 if (!(c1->c_flags & SPLIT))
1202 c1->c_flags |= HDROUTPUT;
1205 if ((count = c1->c_cwidth -
1206 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1210 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1213 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1214 && !(c2->c_flags & NOCOMPONENT)) {
1215 if (c1->c_flags & UPPERCASE)
1216 for (cp = c2->c_name; *cp; cp++)
1221 if (!(c1->c_flags & SPLIT))
1222 c2->c_flags |= HDROUTPUT;
1225 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1229 if (c1->c_flags & UPPERCASE)
1230 for (cp = c2->c_text; *cp; cp++)
1236 if (flag == TWOCOMP)
1237 count = (c1->c_cwidth >= 0) ?
1238 c1->c_cwidth : strlen(c2->c_name) + 2;
1240 count = (c1->c_cwidth >= 0) ?
1241 c1->c_cwidth : strlen(c1->c_text ?
1242 c1->c_text : c1->c_name) + 2;
1244 count += c1->c_offset;
1246 if ((cp = oneline(c2->c_text, c1->c_flags)))
1250 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1252 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1253 putstr(c1->c_text ? c1->c_text : c1->c_name);
1259 if (flag == BODYCOMP && term == '\n')
1260 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1265 oneline(char *stuff, long flags)
1273 return (onelp = NULL);
1277 if (flags & COMPRESS) {
1278 for (spc = 1, cp = ret; *onelp; onelp++)
1279 if (isspace(*onelp)) {
1280 if (*onelp == '\n' &&
1282 (flags & ADDRFMT))) {
1297 while (*onelp && *onelp != '\n')
1299 if (*onelp == '\n') {
1303 if (flags & LEFTADJUST)
1304 while (*ret == ' ' || *ret == '\t')
1307 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1315 putstr(char *string)
1317 if (!column && lm > 0) {
1347 if (ontty != ISTTY || row != global.c_length)
1349 if (global.c_flags & BELL)
1353 read(fileno(stdout), buf, sizeof(buf));
1354 if (strchr(buf, '\n')) {
1355 if (global.c_flags & CLEARSCR)
1360 row = global.c_length / 3;
1379 ** If we are forwarding this message, and the first
1380 ** column contains a dash, then add a dash and a space.
1382 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1391 if (column >= wid) {
1395 putstr(ovtxt ? ovtxt : "");
1407 #ifndef RELIABLE_SIGNALS
1408 SIGNAL(SIGINT, intrser);
1420 #ifndef RELIABLE_SIGNALS
1421 SIGNAL(SIGPIPE, pipeser);
1431 #ifndef RELIABLE_SIGNALS
1432 SIGNAL(SIGQUIT, quitser);
1442 mhlsbr(int argc, char **argv, FILE *(*action)())
1444 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1448 switch (setjmp(mhlenv)) {
1451 sleepsw = 0; /* XXX */
1452 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1455 mhl_action = action;
1458 ** If signal is at default action, then start ignoring
1459 ** it, else let it set to its current action.
1461 if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1462 SIGNAL(SIGINT, istat);
1463 if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1464 SIGNAL(SIGQUIT, qstat);
1465 pstat = SIGNAL(SIGPIPE, pipeser);
1466 mhl(argc, argv); /* FALL THROUGH! */
1469 SIGNAL(SIGINT, istat);
1470 SIGNAL(SIGQUIT, qstat);
1471 SIGNAL(SIGPIPE, SIG_IGN); /*
1472 ** should probably change to block
1477 SIGNAL(SIGPIPE, pstat);
1479 if (holder.c_text) {
1480 free(holder.c_text);
1481 holder.c_text = NULL;
1483 free_queue(&msghd, &msgtl);
1484 for (c1 = fmthd; c1; c1 = c1->c_next)
1485 c1->c_flags &= ~HDROUTPUT;
1494 mhladios(char *what, char *fmt, ...)
1499 advertise(what, NULL, fmt, ap);
1510 longjmp(mhlenv, DONE);
1516 static int m_pid = NOTOK;
1517 static int sd = NOTOK;
1524 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1525 adios("standard output", "unable to dup()");
1527 if (pipe(pd) == NOTOK)
1528 adios("pipe", "unable to");
1530 switch (m_pid = fork()) {
1532 adios("fork", "unable to");
1535 SIGNAL(SIGINT, SIG_DFL);
1536 SIGNAL(SIGQUIT, SIG_DFL);
1539 if (pd[0] != fileno(stdin)) {
1540 dup2(pd[0], fileno(stdin));
1543 execlp(name, mhbasename(name), NULL);
1544 fprintf(stderr, "unable to exec ");
1550 if (pd[1] != fileno(stdout)) {
1551 dup2(pd[1], fileno(stdout));
1566 if (dup2(sd, fileno(stdout)) == NOTOK)
1567 adios("standard output", "unable to dup2()");