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 },
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[] =
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 cp = concat("=", cp, NULL);
663 nfs = new_fs(cp, NULL);
665 c1->c_nfs = getcpy(nfs);
666 c1->c_flags |= FORMAT;
670 if (!mh_strcasecmp(name, "decode")) {
673 nfs = new_fs("=%(decode{text})", NULL);
674 c1->c_nfs = getcpy(nfs);
675 c1->c_flags |= FORMAT;
679 if (!mh_strcasecmp(name, "offset"))
680 return ptoi(name, &c1->c_offset);
681 if (!mh_strcasecmp(name, "overflowoffset"))
682 return ptoi(name, &c1->c_ovoff);
683 if (!mh_strcasecmp(name, "width"))
684 return ptoi(name, &c1->c_width);
685 if (!mh_strcasecmp(name, "compwidth"))
686 return ptoi(name, &c1->c_cwidth);
687 if (!mh_strcasecmp(name, "length"))
688 return ptoi(name, &c1->c_length);
690 for (ap = triples; ap->t_name; ap++)
691 if (!mh_strcasecmp(ap->t_name, name)) {
692 c1->c_flags |= ap->t_on;
693 c1->c_flags &= ~ap->t_off;
702 ptoi(char *name, int *i)
706 if (*parptr++ != '=' || !*(cp = parse())) {
707 advise(NULL, "missing argument to variable %s", name);
717 ptos(char *name, char **s)
721 if (*parptr++ != '=') {
722 advise(NULL, "missing argument to variable %s", name);
726 if (*parptr != '"') {
727 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
731 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
732 if (*parptr == QUOTE)
739 if ((*parptr = c) == '"')
750 static char result[NAMESZ];
752 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
771 process(char *folder, char *fname, int ofilen, int ofilec)
777 switch (setjmp(env)) {
780 fp = mhl_action ? (*mhl_action) (fname) :
783 advise(fname, "unable to open");
791 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
793 SIGNAL(SIGINT, intrser);
794 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
798 SIGNAL(SIGINT, SIG_IGN);
799 if (mhl_action == NULL && fp != stdin)
804 holder.c_text = NULL;
806 free_queue(&msghd, &msgtl);
807 for (c1 = fmthd; c1; c1 = c1->c_next)
808 c1->c_flags &= ~HDROUTPUT;
815 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
818 struct mcomp *c1, *c2, *c3;
819 char **ip, name[NAMESZ], buf[BUFSIZ];
823 printf("%s", ofilen == 1 ? delim3 : delim4);
827 printf(" Forwarded Message%s",
828 ofilec > 1 ? "s" : "");
830 printf(" Message %d", ofilen);
838 if ((global.c_flags & CLEARSCR))
843 printf(">>> %s\n\n", mname);
848 strncpy(buf, "\n", sizeof(buf));
850 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
853 printf("Press <return> to list \"%s\"...", mname);
857 read(fileno(stdout), buf, sizeof(buf));
859 if (strchr(buf, '\n')) {
860 if ((global.c_flags & CLEARSCR))
873 printf(">>> %s\n\n", mname);
879 for (state = FLD;;) {
880 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
883 for (ip = ignores; *ip; ip++)
884 if (!mh_strcasecmp(name, *ip)) {
885 while (state == FLDPLUS)
886 state = m_getfld(state, name, buf, sizeof(buf), fp);
892 for (c2 = fmthd; c2; c2 = c2->c_next)
893 if (!mh_strcasecmp(c2->c_name, name))
896 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
897 for (c1 = msghd; c1; c1 = c1->c_next)
898 if (!mh_strcasecmp(c1->c_name,
900 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
904 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
905 while (state == FLDPLUS) {
906 state = m_getfld(state, name, buf,
908 c1->c_text = add(buf, c1->c_text);
911 c1->c_flags |= EXTRA;
917 for (c1 = fmthd; c1; c1 = c1->c_next) {
918 if (c1->c_flags & CLEARTEXT) {
919 putcomp(c1, c1, ONECOMP);
922 if (!mh_strcasecmp(c1->c_name, "messagename")) {
923 holder.c_text = concat("(Message ",
925 putcomp(c1, &holder, ONECOMP);
927 holder.c_text = NULL;
930 if (!mh_strcasecmp(c1->c_name, "extras")) {
931 for (c2 = msghd; c2; c2 = c2->c_next)
932 if (c2->c_flags & EXTRA)
933 putcomp(c1, c2, TWOCOMP);
936 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
937 holder.c_text = mh_xmalloc(sizeof(buf));
938 strncpy(holder.c_text, buf, sizeof(buf));
939 while (state == BODY) {
940 putcomp(c1, &holder, BODYCOMP);
941 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
944 holder.c_text = NULL;
947 for (c2 = msghd; c2; c2 = c2->c_next)
948 if (!mh_strcasecmp(c2->c_name,
950 putcomp(c1, c2, ONECOMP);
951 if (!(c1->c_flags & SPLIT))
959 advise(NULL, "format error in message %s", mname);
964 adios(NULL, "getfld() returned %d", state);
971 mcomp_flags(char *name)
975 for (ap = pairs; ap->p_name; ap++)
976 if (!mh_strcasecmp(ap->p_name, name))
977 return (ap->p_flags);
984 mcomp_add(long flags, char *s1, char *s2)
988 if (!(flags & ADDRFMT))
991 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
994 return add(s1, add(",\n", s2));
1001 struct pqpair *pq_next;
1006 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1010 char buffer[BUFSIZ], error[BUFSIZ];
1012 struct pqpair *p, *q;
1014 struct mailname *mp;
1021 dat[3] = sizeof(buffer) - 1;
1023 fmt_compile(c1->c_nfs, &c1->c_fmt);
1025 if (!(c1->c_flags & ADDRFMT)) {
1026 FINDCOMP(cptr, "text");
1029 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1033 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1034 /* Don't need to append a newline, dctime() already did */
1035 c2->c_text = getcpy(buffer);
1041 (q = &pq)->pq_next = NULL;
1042 while ((cp = getname(ap))) {
1043 if ((p = (struct pqpair *)
1044 calloc((size_t) 1, sizeof(*p))) == NULL)
1045 adios(NULL, "unable to allocate pqpair memory");
1047 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1048 p->pq_text = getcpy(cp);
1049 p->pq_error = getcpy(error);
1051 p->pq_text = getcpy(mp->m_text);
1054 q = (q->pq_next = p);
1057 for (p = pq.pq_next; p; p = q) {
1058 FINDCOMP(cptr, "text");
1060 cptr->c_text = p->pq_text;
1061 FINDCOMP(cptr, "error");
1063 cptr->c_text = p->pq_error;
1065 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1068 c2->c_text = add(",\n", c2->c_text);
1069 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1071 c2->c_text = add(buffer, c2->c_text);
1081 c2->c_text = add("\n", c2->c_text);
1086 static struct mcomp *
1087 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1088 char *text, int flags)
1092 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1093 adios(NULL, "unable to allocate comp memory");
1095 c1->c_flags = flags & ~INIT;
1096 if ((c1->c_name = name ? getcpy(name) : NULL))
1097 c1->c_flags |= mcomp_flags(c1->c_name);
1098 c1->c_text = text ? getcpy(text) : NULL;
1101 c1->c_ovtxt = getcpy(global.c_ovtxt);
1102 c1->c_offset = global.c_offset;
1103 c1->c_ovoff = global. c_ovoff;
1104 c1->c_width = c1->c_length = 0;
1105 c1->c_cwidth = global.c_cwidth;
1106 c1->c_flags |= global.c_flags & GFLAGS;
1111 (*tail)->c_next = c1;
1119 free_queue(struct mcomp **head, struct mcomp **tail)
1121 struct mcomp *c1, *c2;
1123 for (c1 = *head; c1; c1 = c2) {
1134 free((char *) c1->c_fmt);
1138 *head = *tail = NULL;
1143 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1150 llim = c1->c_length ? c1->c_length : -1;
1151 wid = c1->c_width ? c1->c_width : global.c_width;
1152 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1154 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1156 if (wid < ovoff + strlen(ovtxt) + 5)
1157 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1160 if (c1->c_flags & CLEARTEXT) {
1166 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1167 mcomp_format(c1, c2);
1169 if (c1->c_flags & CENTER) {
1170 count = (c1->c_width ? c1->c_width : global.c_width)
1171 - c1->c_offset - strlen(c2->c_text);
1172 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1173 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1175 lm = c1->c_offset + (count / 2);
1181 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1182 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1183 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1186 putstr(c1->c_text ? c1->c_text : c1->c_name);
1187 if (flag != BODYCOMP) {
1189 if (!(c1->c_flags & SPLIT))
1190 c1->c_flags |= HDROUTPUT;
1193 if ((count = c1->c_cwidth -
1194 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1198 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1201 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1202 && !(c2->c_flags & NOCOMPONENT)) {
1203 if (c1->c_flags & UPPERCASE)
1204 for (cp = c2->c_name; *cp; cp++)
1209 if (!(c1->c_flags & SPLIT))
1210 c2->c_flags |= HDROUTPUT;
1213 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1217 if (c1->c_flags & UPPERCASE)
1218 for (cp = c2->c_text; *cp; cp++)
1224 if (flag == TWOCOMP)
1225 count = (c1->c_cwidth >= 0) ?
1226 c1->c_cwidth : strlen(c2->c_name) + 2;
1228 count = (c1->c_cwidth >= 0) ?
1229 c1->c_cwidth : strlen(c1->c_text ?
1230 c1->c_text : c1->c_name) + 2;
1232 count += c1->c_offset;
1234 if ((cp = oneline(c2->c_text, c1->c_flags)))
1238 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1240 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1241 putstr(c1->c_text ? c1->c_text : c1->c_name);
1247 if (flag == BODYCOMP && term == '\n')
1248 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1253 oneline(char *stuff, long flags)
1261 return (onelp = NULL);
1265 if (flags & COMPRESS) {
1266 for (spc = 1, cp = ret; *onelp; onelp++)
1267 if (isspace(*onelp)) {
1268 if (*onelp == '\n' &&
1270 (flags & ADDRFMT))) {
1285 while (*onelp && *onelp != '\n')
1287 if (*onelp == '\n') {
1291 if (flags & LEFTADJUST)
1292 while (*ret == ' ' || *ret == '\t')
1295 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1303 putstr(char *string)
1305 if (!column && lm > 0) {
1335 if (ontty != ISTTY || row != global.c_length)
1337 if (global.c_flags & BELL)
1341 read(fileno(stdout), buf, sizeof(buf));
1342 if (strchr(buf, '\n')) {
1343 if (global.c_flags & CLEARSCR)
1348 row = global.c_length / 3;
1367 ** If we are forwarding this message, and the first
1368 ** column contains a dash, then add a dash and a space.
1370 if (column == 0 && forwflg && ch == '-') {
1379 if (column >= wid) {
1383 putstr(ovtxt ? ovtxt : "");
1395 #ifndef RELIABLE_SIGNALS
1396 SIGNAL(SIGINT, intrser);
1408 #ifndef RELIABLE_SIGNALS
1409 SIGNAL(SIGPIPE, pipeser);
1419 #ifndef RELIABLE_SIGNALS
1420 SIGNAL(SIGQUIT, quitser);
1430 mhlsbr(int argc, char **argv, FILE *(*action)())
1432 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1436 switch (setjmp(mhlenv)) {
1439 sleepsw = 0; /* XXX */
1440 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1443 mhl_action = action;
1446 ** If signal is at default action, then start ignoring
1447 ** it, else let it set to its current action.
1449 if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1450 SIGNAL(SIGINT, istat);
1451 if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1452 SIGNAL(SIGQUIT, qstat);
1453 pstat = SIGNAL(SIGPIPE, pipeser);
1454 mhl(argc, argv); /* FALL THROUGH! */
1457 SIGNAL(SIGINT, istat);
1458 SIGNAL(SIGQUIT, qstat);
1459 SIGNAL(SIGPIPE, SIG_IGN); /*
1460 ** should probably change to block
1465 SIGNAL(SIGPIPE, pstat);
1467 if (holder.c_text) {
1468 free(holder.c_text);
1469 holder.c_text = NULL;
1471 free_queue(&msghd, &msgtl);
1472 for (c1 = fmthd; c1; c1 = c1->c_next)
1473 c1->c_flags &= ~HDROUTPUT;
1482 mhladios(char *what, char *fmt, ...)
1487 advertise(what, NULL, fmt, ap);
1498 longjmp(mhlenv, DONE);
1504 static int m_pid = NOTOK;
1505 static int sd = NOTOK;
1512 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1513 adios("standard output", "unable to dup()");
1515 if (pipe(pd) == NOTOK)
1516 adios("pipe", "unable to");
1518 switch (m_pid = fork()) {
1520 adios("fork", "unable to");
1523 SIGNAL(SIGINT, SIG_DFL);
1524 SIGNAL(SIGQUIT, SIG_DFL);
1527 if (pd[0] != fileno(stdin)) {
1528 dup2(pd[0], fileno(stdin));
1531 execlp(name, mhbasename(name), NULL);
1532 fprintf(stderr, "unable to exec ");
1538 if (pd[1] != fileno(stdout)) {
1539 dup2(pd[1], fileno(stdout));
1554 if (dup2(sd, fileno(stdout)) == NOTOK)
1555 adios("standard output", "unable to dup2()");