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 },
71 { "forward", -7 }, /* interface from forw */
73 { "forwall", -7 }, /* interface from forw */
75 { "digest list", -6 },
77 { "volume number", -6 },
79 { "issue number", -5 },
85 #define NOCOMPONENT 0x000001 /* don't show component name */
86 #define UPPERCASE 0x000002 /* display in all upper case */
87 #define CENTER 0x000004 /* center line */
88 #define CLEARTEXT 0x000008 /* cleartext */
89 #define EXTRA 0x000010 /* an "extra" component */
90 #define HDROUTPUT 0x000020 /* already output */
91 #define CLEARSCR 0x000040 /* clear screen */
92 #define LEFTADJUST 0x000080 /* left justify multiple lines */
93 #define COMPRESS 0x000100 /* compress text */
94 #define ADDRFMT 0x000200 /* contains addresses */
95 #define BELL 0x000400 /* sound bell at EOP */
96 #define DATEFMT 0x000800 /* contains dates */
97 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
98 #define INIT 0x002000 /* initialize component */
99 #define SPLIT 0x004000 /* split headers (don't concatenate) */
100 #define NONEWLINE 0x008000 /* don't write trailing newline */
101 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017SPLIT\020NONEWLINE"
102 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
105 char *c_name; /* component name */
106 char *c_text; /* component text */
107 char *c_ovtxt; /* text overflow indicator */
108 char *c_nfs; /* iff FORMAT */
109 struct format *c_fmt; /* .. */
110 int c_offset; /* left margin indentation */
111 int c_ovoff; /* overflow indentation */
112 int c_width; /* width of field */
113 int c_cwidth; /* width of component */
114 int c_length; /* length in lines */
116 struct mcomp *c_next;
119 static struct mcomp *msghd = NULL;
120 static struct mcomp *msgtl = NULL;
121 static struct mcomp *fmthd = NULL;
122 static struct mcomp *fmttl = NULL;
124 static struct mcomp global = {
125 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
128 static struct mcomp holder = {
129 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
137 static struct pair pairs[] = {
140 { "Sender", ADDRFMT },
141 { "Reply-To", ADDRFMT },
145 { "Resent-Date", DATEFMT },
146 { "Resent-From", ADDRFMT },
147 { "Resent-Sender", ADDRFMT },
148 { "Resent-Reply-To", ADDRFMT },
149 { "Resent-To", ADDRFMT },
150 { "Resent-Cc", ADDRFMT },
151 { "Resent-Bcc", ADDRFMT },
161 static struct triple triples[] = {
162 { "nocomponent", NOCOMPONENT, 0 },
163 { "uppercase", UPPERCASE, 0 },
164 { "nouppercase", 0, UPPERCASE },
165 { "center", CENTER, 0 },
166 { "nocenter", 0, CENTER },
167 { "clearscreen", CLEARSCR, 0 },
168 { "noclearscreen", 0, CLEARSCR },
169 { "noclear", 0, CLEARSCR },
170 { "leftadjust", LEFTADJUST, 0 },
171 { "noleftadjust", 0, LEFTADJUST },
172 { "compress", COMPRESS, 0 },
173 { "nocompress", 0, COMPRESS },
174 { "split", SPLIT, 0 },
175 { "nosplit", 0, SPLIT },
176 { "addrfield", ADDRFMT, DATEFMT },
178 { "nobell", 0, BELL },
179 { "datefield", DATEFMT, ADDRFMT },
180 { "newline", 0, NONEWLINE },
181 { "nonewline", NONEWLINE, 0 },
186 static int bellflg = 0;
187 static int clearflg = 0;
188 static int dobody = 1;
189 static int forwflg = 0;
190 static int forwall = 0;
192 static int sleepsw = NOTOK;
194 static char *digest = NULL;
195 static int volume = 0;
196 static int issue = 0;
198 static int exitstat = 0;
199 static int mhldebug = 0;
204 static int ontty = NOTTY;
217 static unsigned char *onelp;
221 static int num_ignores = 0;
222 static char *ignores[MAXARGS];
225 static jmp_buf mhlenv;
227 static char delim3[] = /* from forw.c */
228 "\n----------------------------------------------------------------------\n\n";
229 static char delim4[] = "\n------------------------------\n\n";
231 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
235 ** Redefine a couple of functions.
236 ** These are undefined later in the code.
238 #define adios mhladios
244 static void mhl_format(char *, int, int);
245 static int evalvar(struct mcomp *);
246 static int ptoi(char *, int *);
247 static int ptos(char *, char **);
248 static char *parse(void);
249 static void process(char *, char *, int, int);
250 static void mhlfile(FILE *, char *, int, int);
251 static int mcomp_flags(char *);
252 static char *mcomp_add(long, char *, char *);
253 static void mcomp_format(struct mcomp *, struct mcomp *);
254 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
255 char *, char *, int);
256 static void free_queue(struct mcomp **, struct mcomp **);
257 static void putcomp(struct mcomp *, struct mcomp *, int);
258 static char *oneline(char *, long);
259 static void putstr(char *);
260 static void putch(char);
261 static RETSIGTYPE intrser(int);
262 static RETSIGTYPE pipeser(int);
263 static RETSIGTYPE quitser(int);
264 static void mhladios(char *, char *, ...);
265 static void mhldone(int);
266 static void m_popen(char *);
268 int mhl(int, char **);
269 int mhlsbr(int, char **, FILE *(*)());
272 void clear_screen(void); /* from termsbr.c */
273 int SOprintf(char *, ...); /* from termsbr.c */
274 int sc_width(void); /* from termsbr.c */
275 int sc_length(void); /* from termsbr.c */
276 int sc_hardcopy(void); /* from termsbr.c */
280 mhl(int argc, char **argv)
282 int length = 0, nomore = 0;
283 int i, width = 0, vecp = 0;
284 char *cp, *folder = NULL, *form = NULL;
285 char buf[BUFSIZ], *files[MAXARGS];
286 char **argp, **arguments;
288 invo_name = mhbasename(argv[0]);
290 /* read user profile/context */
293 arguments = getarguments(invo_name, argc, argv, 1);
296 if ((cp = getenv("MHLDEBUG")) && *cp)
299 while ((cp = *argp++)) {
301 switch (smatch(++cp, mhlswitches)) {
303 ambigsw(cp, mhlswitches);
306 adios(NULL, "-%s unknown\n", cp);
309 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
310 print_help(buf, mhlswitches, 1);
313 print_version(invo_name);
331 if (!(folder = *argp++) || *folder == '-')
332 adios(NULL, "missing argument to %s",
336 if (!(form = *argp++) || *form == '-')
337 adios(NULL, "missing argument to %s",
342 if (!(cp = *argp++) || *cp == '-')
343 adios(NULL, "missing argument to %s",
345 sleepsw = atoi(cp); /* ZERO ok! */
349 if (!(moreproc = *argp++) || *moreproc == '-')
350 adios(NULL, "missing argument to %s",
358 if (!(cp = *argp++) || *cp == '-')
359 adios(NULL, "missing argument to %s",
361 if ((length = atoi(cp)) < 1)
362 adios(NULL, "bad argument %s %s",
366 if (!(cp = *argp++) || *cp == '-')
367 adios(NULL, "missing argument to %s",
369 if ((width = atoi(cp)) < 1)
370 adios(NULL, "bad argument %s %s",
375 if (!(digest = *argp++) ||
377 adios(NULL, "missing argument to %s",
381 if (!(cp = *argp++) || *cp == '-')
382 adios(NULL, "missing argument to %s",
384 if ((issue = atoi(cp)) < 1)
385 adios(NULL, "bad argument %s %s",
389 if (!(cp = *argp++) || *cp == '-')
390 adios(NULL, "missing argument to %s",
392 if ((volume = atoi(cp)) < 1)
393 adios(NULL, "bad argument %s %s",
398 forwall++; /* fall */
401 clearflg = -1;/* XXX */
413 folder = getenv("mhfolder");
415 if (isatty(fileno(stdout))) {
416 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
418 SIGNAL(SIGINT, SIG_IGN);
419 SIGNAL2(SIGQUIT, quitser);
421 SIGNAL2(SIGPIPE, pipeser);
425 SIGNAL(SIGINT, SIG_IGN);
426 SIGNAL2(SIGQUIT, quitser);
433 mhl_format(form ? form : mhlformat, length, width);
436 process(folder, NULL, 1, vecp = 1);
438 for (i = 0; i < vecp; i++)
439 process(folder, files[i], i + 1, vecp);
444 printf("%s", delim4);
446 snprintf(buf, sizeof(buf),
447 "End of %s Digest\n", digest);
449 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
452 for (cp = buf + i; i > 1; i--)
458 printf("\n------- End of Forwarded Message%s\n\n",
459 vecp > 1 ? "s" : "");
463 if (ferror(stdout)) {
464 adios("output", "error writing");
467 if (clearflg > 0 && ontty == NOTTY)
478 mhl_format(char *file, int length, int width)
482 char *ap, buffer[BUFSIZ], name[NAMESZ];
486 static dev_t dev = 0;
487 static ino_t ino = 0;
488 static time_t mtime = 0;
491 if (stat(etcpath(file), &st) != NOTOK
492 && mtime == st.st_mtime
497 free_queue(&fmthd, &fmttl);
500 if ((fp = fopen(etcpath(file), "r")) == NULL)
501 adios(file, "unable to open format file");
503 if (fstat(fileno(fp), &st) != NOTOK) {
509 global.c_ovtxt = global.c_nfs = NULL;
513 if ((i = sc_width()) > 5)
515 global.c_cwidth = -1;
516 if ((i = sc_length()) > 5)
517 global.c_length = i - 1;
518 global.c_flags = BELL; /* BELL is default */
519 *(ip = ignores) = NULL;
521 while (vfgets(fp, &ap) == OK) {
526 if ((cp = strchr(bp, '\n')))
530 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
536 strncpy(name, parse(), sizeof(name));
542 ** Split this list of fields to ignore, and copy
543 ** it to the end of the current "ignores" list.
545 if (!mh_strcasecmp(name, "ignores")) {
546 char **tmparray, **p;
549 /* split the fields */
550 tmparray = brkstring(getcpy(++parptr), ",",
553 /* count number of fields split */
559 ** copy pointers to split fields
562 ip = copyip(tmparray, ip,
563 MAXARGS - num_ignores);
569 if (evalvar(&global))
570 adios(NULL, "format file syntax error: %s", bp);
577 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
578 while (*parptr == ':' || *parptr == ',') {
581 adios(NULL, "format file syntax error: %s", bp);
583 if (!c1->c_nfs && global.c_nfs) {
584 if ((c1->c_flags & DATEFMT) &&
585 (global.c_flags & DATEFMT)) {
586 c1->c_nfs = getcpy(global.c_nfs);
587 } else if ((c1->c_flags & ADDRFMT) &&
588 (global.c_flags & ADDRFMT)) {
589 c1->c_nfs = getcpy(global.c_nfs);
595 adios(NULL, "format file syntax error: %s", bp);
601 for (c1 = fmthd; c1; c1 = c1->c_next) {
602 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
603 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
604 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);
605 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
611 global.c_flags |= CLEARSCR;
614 global.c_flags &= ~CLEARSCR;
617 switch (bellflg) { /* command line may override format file */
619 global.c_flags |= BELL;
622 global.c_flags &= ~BELL;
627 global.c_length = length;
629 global.c_width = width;
630 if (global.c_length < 5)
631 global.c_length = 10000;
632 if (global.c_width < 5)
633 global.c_width = 10000;
638 evalvar(struct mcomp *c1)
640 char *cp, name[NAMESZ];
645 strncpy(name, parse(), sizeof(name));
647 if (!mh_strcasecmp(name, "component")) {
648 if (ptos(name, &c1->c_text))
650 c1->c_flags &= ~NOCOMPONENT;
654 if (!mh_strcasecmp(name, "overflowtext"))
655 return ptos(name, &c1->c_ovtxt);
657 if (!mh_strcasecmp(name, "formatfield")) {
662 nfs = new_fs(NULL, NULL, cp);
663 c1->c_nfs = getcpy(nfs);
664 c1->c_flags |= FORMAT;
668 if (!mh_strcasecmp(name, "decode")) {
671 nfs = new_fs(NULL, NULL, "%(decode{text})");
672 c1->c_nfs = getcpy(nfs);
673 c1->c_flags |= FORMAT;
677 if (!mh_strcasecmp(name, "offset"))
678 return ptoi(name, &c1->c_offset);
679 if (!mh_strcasecmp(name, "overflowoffset"))
680 return ptoi(name, &c1->c_ovoff);
681 if (!mh_strcasecmp(name, "width"))
682 return ptoi(name, &c1->c_width);
683 if (!mh_strcasecmp(name, "compwidth"))
684 return ptoi(name, &c1->c_cwidth);
685 if (!mh_strcasecmp(name, "length"))
686 return ptoi(name, &c1->c_length);
688 for (ap = triples; ap->t_name; ap++)
689 if (!mh_strcasecmp(ap->t_name, name)) {
690 c1->c_flags |= ap->t_on;
691 c1->c_flags &= ~ap->t_off;
700 ptoi(char *name, int *i)
704 if (*parptr++ != '=' || !*(cp = parse())) {
705 advise(NULL, "missing argument to variable %s", name);
715 ptos(char *name, char **s)
719 if (*parptr++ != '=') {
720 advise(NULL, "missing argument to variable %s", name);
724 if (*parptr != '"') {
725 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
729 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
730 if (*parptr == QUOTE)
737 if ((*parptr = c) == '"')
748 static char result[NAMESZ];
750 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
769 process(char *folder, char *fname, int ofilen, int ofilec)
775 switch (setjmp(env)) {
778 fp = mhl_action ? (*mhl_action) (fname) :
781 advise(fname, "unable to open");
789 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
791 SIGNAL(SIGINT, intrser);
792 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
796 SIGNAL(SIGINT, SIG_IGN);
797 if (mhl_action == NULL && fp != stdin)
802 holder.c_text = NULL;
804 free_queue(&msghd, &msgtl);
805 for (c1 = fmthd; c1; c1 = c1->c_next)
806 c1->c_flags &= ~HDROUTPUT;
813 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
816 struct mcomp *c1, *c2, *c3;
817 char **ip, name[NAMESZ], buf[BUFSIZ];
821 printf("%s", ofilen == 1 ? delim3 : delim4);
825 printf(" Forwarded Message%s",
826 ofilec > 1 ? "s" : "");
828 printf(" Message %d", ofilen);
836 if ((global.c_flags & CLEARSCR))
841 printf(">>> %s\n\n", mname);
846 strncpy(buf, "\n", sizeof(buf));
848 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
851 printf("Press <return> to list \"%s\"...", mname);
855 read(fileno(stdout), buf, sizeof(buf));
857 if (strchr(buf, '\n')) {
858 if ((global.c_flags & CLEARSCR))
871 printf(">>> %s\n\n", mname);
877 for (state = FLD;;) {
878 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
881 for (ip = ignores; *ip; ip++)
882 if (!mh_strcasecmp(name, *ip)) {
883 while (state == FLDPLUS)
884 state = m_getfld(state, name, buf, sizeof(buf), fp);
890 for (c2 = fmthd; c2; c2 = c2->c_next)
891 if (!mh_strcasecmp(c2->c_name, name))
894 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
895 for (c1 = msghd; c1; c1 = c1->c_next)
896 if (!mh_strcasecmp(c1->c_name,
898 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
902 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
903 while (state == FLDPLUS) {
904 state = m_getfld(state, name, buf,
906 c1->c_text = add(buf, c1->c_text);
909 c1->c_flags |= EXTRA;
915 for (c1 = fmthd; c1; c1 = c1->c_next) {
916 if (c1->c_flags & CLEARTEXT) {
917 putcomp(c1, c1, ONECOMP);
920 if (!mh_strcasecmp(c1->c_name, "messagename")) {
921 holder.c_text = concat("(Message ",
923 putcomp(c1, &holder, ONECOMP);
925 holder.c_text = NULL;
928 if (!mh_strcasecmp(c1->c_name, "extras")) {
929 for (c2 = msghd; c2; c2 = c2->c_next)
930 if (c2->c_flags & EXTRA)
931 putcomp(c1, c2, TWOCOMP);
934 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
935 holder.c_text = mh_xmalloc(sizeof(buf));
936 strncpy(holder.c_text, buf, sizeof(buf));
937 while (state == BODY) {
938 putcomp(c1, &holder, BODYCOMP);
939 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
942 holder.c_text = NULL;
945 for (c2 = msghd; c2; c2 = c2->c_next)
946 if (!mh_strcasecmp(c2->c_name,
948 putcomp(c1, c2, ONECOMP);
949 if (!(c1->c_flags & SPLIT))
957 advise(NULL, "format error in message %s", mname);
962 adios(NULL, "getfld() returned %d", state);
969 mcomp_flags(char *name)
973 for (ap = pairs; ap->p_name; ap++)
974 if (!mh_strcasecmp(ap->p_name, name))
975 return (ap->p_flags);
982 mcomp_add(long flags, char *s1, char *s2)
986 if (!(flags & ADDRFMT))
989 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
992 return add(s1, add(",\n", s2));
999 struct pqpair *pq_next;
1004 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1008 char buffer[BUFSIZ], error[BUFSIZ];
1010 struct pqpair *p, *q;
1012 struct mailname *mp;
1019 dat[3] = sizeof(buffer) - 1;
1021 fmt_compile(c1->c_nfs, &c1->c_fmt);
1023 if (!(c1->c_flags & ADDRFMT)) {
1024 FINDCOMP(cptr, "text");
1027 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1031 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1032 /* Don't need to append a newline, dctime() already did */
1033 c2->c_text = getcpy(buffer);
1039 (q = &pq)->pq_next = NULL;
1040 while ((cp = getname(ap))) {
1041 if ((p = (struct pqpair *)
1042 calloc((size_t) 1, sizeof(*p))) == NULL)
1043 adios(NULL, "unable to allocate pqpair memory");
1045 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1046 p->pq_text = getcpy(cp);
1047 p->pq_error = getcpy(error);
1049 p->pq_text = getcpy(mp->m_text);
1052 q = (q->pq_next = p);
1055 for (p = pq.pq_next; p; p = q) {
1056 FINDCOMP(cptr, "text");
1058 cptr->c_text = p->pq_text;
1059 FINDCOMP(cptr, "error");
1061 cptr->c_text = p->pq_error;
1063 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1066 c2->c_text = add(",\n", c2->c_text);
1067 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1069 c2->c_text = add(buffer, c2->c_text);
1079 c2->c_text = add("\n", c2->c_text);
1084 static struct mcomp *
1085 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1086 char *text, int flags)
1090 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1091 adios(NULL, "unable to allocate comp memory");
1093 c1->c_flags = flags & ~INIT;
1094 if ((c1->c_name = name ? getcpy(name) : NULL))
1095 c1->c_flags |= mcomp_flags(c1->c_name);
1096 c1->c_text = text ? getcpy(text) : NULL;
1099 c1->c_ovtxt = getcpy(global.c_ovtxt);
1100 c1->c_offset = global.c_offset;
1101 c1->c_ovoff = global. c_ovoff;
1102 c1->c_width = c1->c_length = 0;
1103 c1->c_cwidth = global.c_cwidth;
1104 c1->c_flags |= global.c_flags & GFLAGS;
1109 (*tail)->c_next = c1;
1117 free_queue(struct mcomp **head, struct mcomp **tail)
1119 struct mcomp *c1, *c2;
1121 for (c1 = *head; c1; c1 = c2) {
1132 free((char *) c1->c_fmt);
1136 *head = *tail = NULL;
1141 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1148 llim = c1->c_length ? c1->c_length : -1;
1149 wid = c1->c_width ? c1->c_width : global.c_width;
1150 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1152 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1154 if (wid < ovoff + strlen(ovtxt) + 5)
1155 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1158 if (c1->c_flags & CLEARTEXT) {
1164 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1165 mcomp_format(c1, c2);
1167 if (c1->c_flags & CENTER) {
1168 count = (c1->c_width ? c1->c_width : global.c_width)
1169 - c1->c_offset - strlen(c2->c_text);
1170 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1171 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1173 lm = c1->c_offset + (count / 2);
1179 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1180 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1181 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1184 putstr(c1->c_text ? c1->c_text : c1->c_name);
1185 if (flag != BODYCOMP) {
1187 if (!(c1->c_flags & SPLIT))
1188 c1->c_flags |= HDROUTPUT;
1191 if ((count = c1->c_cwidth -
1192 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1196 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1199 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1200 && !(c2->c_flags & NOCOMPONENT)) {
1201 if (c1->c_flags & UPPERCASE)
1202 for (cp = c2->c_name; *cp; cp++)
1207 if (!(c1->c_flags & SPLIT))
1208 c2->c_flags |= HDROUTPUT;
1211 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1215 if (c1->c_flags & UPPERCASE)
1216 for (cp = c2->c_text; *cp; cp++)
1222 if (flag == TWOCOMP)
1223 count = (c1->c_cwidth >= 0) ?
1224 c1->c_cwidth : strlen(c2->c_name) + 2;
1226 count = (c1->c_cwidth >= 0) ?
1227 c1->c_cwidth : strlen(c1->c_text ?
1228 c1->c_text : c1->c_name) + 2;
1230 count += c1->c_offset;
1232 if ((cp = oneline(c2->c_text, c1->c_flags)))
1236 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1238 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1239 putstr(c1->c_text ? c1->c_text : c1->c_name);
1245 if (flag == BODYCOMP && term == '\n')
1246 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1251 oneline(char *stuff, long flags)
1259 return (onelp = NULL);
1263 if (flags & COMPRESS) {
1264 for (spc = 1, cp = ret; *onelp; onelp++)
1265 if (isspace(*onelp)) {
1266 if (*onelp == '\n' &&
1268 (flags & ADDRFMT))) {
1283 while (*onelp && *onelp != '\n')
1285 if (*onelp == '\n') {
1289 if (flags & LEFTADJUST)
1290 while (*ret == ' ' || *ret == '\t')
1293 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1301 putstr(char *string)
1303 if (!column && lm > 0) {
1333 if (ontty != ISTTY || row != global.c_length)
1335 if (global.c_flags & BELL)
1339 read(fileno(stdout), buf, sizeof(buf));
1340 if (strchr(buf, '\n')) {
1341 if (global.c_flags & CLEARSCR)
1346 row = global.c_length / 3;
1365 ** If we are forwarding this message, and the first
1366 ** column contains a dash, then add a dash and a space.
1368 if (column == 0 && forwflg && ch == '-') {
1377 if (column >= wid) {
1381 putstr(ovtxt ? ovtxt : "");
1393 #ifndef RELIABLE_SIGNALS
1394 SIGNAL(SIGINT, intrser);
1406 #ifndef RELIABLE_SIGNALS
1407 SIGNAL(SIGPIPE, pipeser);
1417 #ifndef RELIABLE_SIGNALS
1418 SIGNAL(SIGQUIT, quitser);
1428 mhlsbr(int argc, char **argv, FILE *(*action)())
1430 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1434 switch (setjmp(mhlenv)) {
1437 sleepsw = 0; /* XXX */
1438 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1441 mhl_action = action;
1444 ** If signal is at default action, then start ignoring
1445 ** it, else let it set to its current action.
1447 if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1448 SIGNAL(SIGINT, istat);
1449 if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1450 SIGNAL(SIGQUIT, qstat);
1451 pstat = SIGNAL(SIGPIPE, pipeser);
1452 mhl(argc, argv); /* FALL THROUGH! */
1455 SIGNAL(SIGINT, istat);
1456 SIGNAL(SIGQUIT, qstat);
1457 SIGNAL(SIGPIPE, SIG_IGN); /*
1458 ** should probably change to block
1463 SIGNAL(SIGPIPE, pstat);
1465 if (holder.c_text) {
1466 free(holder.c_text);
1467 holder.c_text = NULL;
1469 free_queue(&msghd, &msgtl);
1470 for (c1 = fmthd; c1; c1 = c1->c_next)
1471 c1->c_flags &= ~HDROUTPUT;
1480 mhladios(char *what, char *fmt, ...)
1485 advertise(what, NULL, fmt, ap);
1496 longjmp(mhlenv, DONE);
1502 static int m_pid = NOTOK;
1503 static int sd = NOTOK;
1510 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1511 adios("standard output", "unable to dup()");
1513 if (pipe(pd) == NOTOK)
1514 adios("pipe", "unable to");
1516 switch (m_pid = fork()) {
1518 adios("fork", "unable to");
1521 SIGNAL(SIGINT, SIG_DFL);
1522 SIGNAL(SIGQUIT, SIG_DFL);
1525 if (pd[0] != fileno(stdin)) {
1526 dup2(pd[0], fileno(stdin));
1529 execlp(name, mhbasename(name), NULL);
1530 fprintf(stderr, "unable to exec ");
1536 if (pd[1] != fileno(stdout)) {
1537 dup2(pd[1], fileno(stdout));
1552 if (dup2(sd, fileno(stdout)) == NOTOK)
1553 adios("standard output", "unable to dup2()");