2 ** mhl.c -- the nmh message listing program
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 },
59 { "length lines", 0 },
61 { "width columns", 0 },
63 { "sleep seconds", 0 },
73 { "digest list", -6 },
75 { "volume number", -6 },
77 { "issue number", -5 },
83 #define NOCOMPONENT 0x000001 /* don't show component name */
84 #define UPPERCASE 0x000002 /* display in all upper case */
85 #define CENTER 0x000004 /* center line */
86 #define CLEARTEXT 0x000008 /* cleartext */
87 #define EXTRA 0x000010 /* an "extra" component */
88 #define HDROUTPUT 0x000020 /* already output */
89 #define CLEARSCR 0x000040 /* clear screen */
90 #define LEFTADJUST 0x000080 /* left justify multiple lines */
91 #define COMPRESS 0x000100 /* compress text */
92 #define ADDRFMT 0x000200 /* contains addresses */
93 #define BELL 0x000400 /* sound bell at EOP */
94 #define DATEFMT 0x000800 /* contains dates */
95 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
96 #define INIT 0x002000 /* initialize component */
97 #define SPLIT 0x004000 /* split headers (don't concatenate) */
98 #define NONEWLINE 0x008000 /* don't write trailing newline */
99 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017SPLIT\020NONEWLINE"
100 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
103 char *c_name; /* component name */
104 char *c_text; /* component text */
105 char *c_ovtxt; /* text overflow indicator */
106 char *c_nfs; /* iff FORMAT */
107 struct format *c_fmt; /* .. */
108 int c_offset; /* left margin indentation */
109 int c_ovoff; /* overflow indentation */
110 int c_width; /* width of field */
111 int c_cwidth; /* width of component */
112 int c_length; /* length in lines */
114 struct mcomp *c_next;
117 static struct mcomp *msghd = NULL;
118 static struct mcomp *msgtl = NULL;
119 static struct mcomp *fmthd = NULL;
120 static struct mcomp *fmttl = NULL;
122 static struct mcomp global = {
123 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
126 static struct mcomp holder = {
127 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
135 static struct pair pairs[] = {
138 { "Sender", ADDRFMT },
139 { "Reply-To", ADDRFMT },
143 { "Resent-Date", DATEFMT },
144 { "Resent-From", ADDRFMT },
145 { "Resent-Sender", ADDRFMT },
146 { "Resent-Reply-To", ADDRFMT },
147 { "Resent-To", ADDRFMT },
148 { "Resent-Cc", ADDRFMT },
149 { "Resent-Bcc", ADDRFMT },
159 static struct triple triples[] = {
160 { "nocomponent", NOCOMPONENT, 0 },
161 { "uppercase", UPPERCASE, 0 },
162 { "nouppercase", 0, UPPERCASE },
163 { "center", CENTER, 0 },
164 { "nocenter", 0, CENTER },
165 { "clearscreen", CLEARSCR, 0 },
166 { "noclearscreen", 0, CLEARSCR },
167 { "noclear", 0, CLEARSCR },
168 { "leftadjust", LEFTADJUST, 0 },
169 { "noleftadjust", 0, LEFTADJUST },
170 { "compress", COMPRESS, 0 },
171 { "nocompress", 0, COMPRESS },
172 { "split", SPLIT, 0 },
173 { "nosplit", 0, SPLIT },
174 { "addrfield", ADDRFMT, DATEFMT },
176 { "nobell", 0, BELL },
177 { "datefield", DATEFMT, ADDRFMT },
178 { "newline", 0, NONEWLINE },
179 { "nonewline", NONEWLINE, 0 },
184 static int bellflg = 0;
185 static int clearflg = 0;
186 static int dobody = 1;
187 static int forwflg = 0;
188 static int forwall = 0;
190 static int sleepsw = NOTOK;
192 static char *digest = NULL;
193 static int volume = 0;
194 static int issue = 0;
196 static int exitstat = 0;
197 static int mhldebug = 0;
202 static int ontty = NOTTY;
215 static unsigned char *onelp;
219 static int num_ignores = 0;
220 static char *ignores[MAXARGS];
223 static jmp_buf mhlenv;
225 static char delim3[] =
226 "\n----------------------------------------------------------------------\n\n";
227 static char delim4[] = "\n------------------------------\n\n";
229 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
233 ** Redefine a couple of functions.
234 ** These are undefined later in the code.
236 #define adios mhladios
242 static void mhl_format(char *, int, int);
243 static int evalvar(struct mcomp *);
244 static int ptoi(char *, int *);
245 static int ptos(char *, char **);
246 static char *parse(void);
247 static void process(char *, char *, int, int);
248 static void mhlfile(FILE *, char *, int, int);
249 static int mcomp_flags(char *);
250 static char *mcomp_add(long, char *, char *);
251 static void mcomp_format(struct mcomp *, struct mcomp *);
252 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
253 char *, char *, int);
254 static void free_queue(struct mcomp **, struct mcomp **);
255 static void putcomp(struct mcomp *, struct mcomp *, int);
256 static char *oneline(char *, long);
257 static void putstr(char *);
258 static void putch(char);
259 static RETSIGTYPE intrser(int);
260 static RETSIGTYPE pipeser(int);
261 static RETSIGTYPE quitser(int);
262 static void mhladios(char *, char *, ...);
263 static void mhldone(int);
264 static void m_popen(char *);
268 void clear_screen(void); /* from termsbr.c */
269 int SOprintf(char *, ...); /* from termsbr.c */
270 int sc_width(void); /* from termsbr.c */
271 int sc_length(void); /* from termsbr.c */
272 int sc_hardcopy(void); /* from termsbr.c */
276 main(int argc, char **argv)
279 int i, width = 0, vecp = 0;
280 char *cp, *folder = NULL, *form = NULL;
281 char buf[BUFSIZ], *files[MAXARGS];
282 char **argp, **arguments;
285 setlocale(LC_ALL, "");
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",
355 if (!(cp = *argp++) || *cp == '-')
356 adios(NULL, "missing argument to %s",
358 if ((length = atoi(cp)) < 1)
359 adios(NULL, "bad argument %s %s",
363 if (!(cp = *argp++) || *cp == '-')
364 adios(NULL, "missing argument to %s",
366 if ((width = atoi(cp)) < 1)
367 adios(NULL, "bad argument %s %s",
372 if (!(digest = *argp++) ||
374 adios(NULL, "missing argument to %s",
378 if (!(cp = *argp++) || *cp == '-')
379 adios(NULL, "missing argument to %s",
381 if ((issue = atoi(cp)) < 1)
382 adios(NULL, "bad argument %s %s",
386 if (!(cp = *argp++) || *cp == '-')
387 adios(NULL, "missing argument to %s",
389 if ((volume = atoi(cp)) < 1)
390 adios(NULL, "bad argument %s %s",
395 forwall++; /* fall */
398 clearflg = -1;/* XXX */
410 folder = getenv("mhfolder");
412 if (isatty(fileno(stdout))) {
413 if (!sc_hardcopy() && moreproc && *moreproc != '\0') {
415 SIGNAL(SIGINT, SIG_IGN);
416 SIGNAL2(SIGQUIT, quitser);
418 SIGNAL2(SIGPIPE, pipeser);
422 SIGNAL(SIGINT, SIG_IGN);
423 SIGNAL2(SIGQUIT, quitser);
430 mhl_format(form ? form : mhlformat, length, width);
433 process(folder, NULL, 1, vecp = 1);
435 for (i = 0; i < vecp; i++)
436 process(folder, files[i], i + 1, vecp);
441 printf("%s", delim4);
443 snprintf(buf, sizeof(buf),
444 "End of %s Digest\n", digest);
446 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
449 for (cp = buf + i; i > 1; i--)
455 printf("\n------- End of Forwarded Message%s\n\n",
456 vecp > 1 ? "s" : "");
460 if (ferror(stdout)) {
461 adios("output", "error writing");
464 if (clearflg > 0 && ontty == NOTTY)
475 mhl_format(char *file, int length, int width)
479 char *ap, buffer[BUFSIZ], name[NAMESZ];
483 static dev_t dev = 0;
484 static ino_t ino = 0;
485 static time_t mtime = 0;
488 if (stat(etcpath(file), &st) != NOTOK
489 && mtime == st.st_mtime
494 free_queue(&fmthd, &fmttl);
497 if ((fp = fopen(etcpath(file), "r")) == NULL)
498 adios(file, "unable to open format file");
500 if (fstat(fileno(fp), &st) != NOTOK) {
506 global.c_ovtxt = global.c_nfs = NULL;
510 if ((i = sc_width()) > 5)
512 global.c_cwidth = -1;
513 if ((i = sc_length()) > 5)
514 global.c_length = i - 1;
515 global.c_flags = BELL; /* BELL is default */
516 *(ip = ignores) = NULL;
518 while (vfgets(fp, &ap) == OK) {
523 if ((cp = strchr(bp, '\n')))
527 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
533 strncpy(name, parse(), sizeof(name));
539 ** Split this list of fields to ignore, and copy
540 ** it to the end of the current "ignores" list.
542 if (!mh_strcasecmp(name, "ignores")) {
543 char **tmparray, **p;
546 /* split the fields */
547 tmparray = brkstring(getcpy(++parptr), ",",
550 /* count number of fields split */
556 ** copy pointers to split fields
559 ip = copyip(tmparray, ip,
560 MAXARGS - num_ignores);
566 if (evalvar(&global))
567 adios(NULL, "format file syntax error: %s", bp);
574 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
575 while (*parptr == ':' || *parptr == ',') {
578 adios(NULL, "format file syntax error: %s", bp);
580 if (!c1->c_nfs && global.c_nfs) {
581 if ((c1->c_flags & DATEFMT) &&
582 (global.c_flags & DATEFMT)) {
583 c1->c_nfs = getcpy(global.c_nfs);
584 } else if ((c1->c_flags & ADDRFMT) &&
585 (global.c_flags & ADDRFMT)) {
586 c1->c_nfs = getcpy(global.c_nfs);
592 adios(NULL, "format file syntax error: %s", bp);
598 for (c1 = fmthd; c1; c1 = c1->c_next) {
599 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
600 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
601 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);
602 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
608 global.c_flags |= CLEARSCR;
611 global.c_flags &= ~CLEARSCR;
614 switch (bellflg) { /* command line may override format file */
616 global.c_flags |= BELL;
619 global.c_flags &= ~BELL;
624 global.c_length = length;
626 global.c_width = width;
627 if (global.c_length < 5)
628 global.c_length = 10000;
629 if (global.c_width < 5)
630 global.c_width = 10000;
635 evalvar(struct mcomp *c1)
637 char *cp, name[NAMESZ];
642 strncpy(name, parse(), sizeof(name));
644 if (!mh_strcasecmp(name, "component")) {
645 if (ptos(name, &c1->c_text))
647 c1->c_flags &= ~NOCOMPONENT;
651 if (!mh_strcasecmp(name, "overflowtext"))
652 return ptos(name, &c1->c_ovtxt);
654 if (!mh_strcasecmp(name, "formatfield")) {
659 cp = concat("=", cp, NULL);
660 nfs = new_fs(cp, NULL);
662 c1->c_nfs = getcpy(nfs);
663 c1->c_flags |= FORMAT;
667 if (!mh_strcasecmp(name, "decode")) {
670 nfs = new_fs("=%(decode{text})", NULL);
671 c1->c_nfs = getcpy(nfs);
672 c1->c_flags |= FORMAT;
676 if (!mh_strcasecmp(name, "offset"))
677 return ptoi(name, &c1->c_offset);
678 if (!mh_strcasecmp(name, "overflowoffset"))
679 return ptoi(name, &c1->c_ovoff);
680 if (!mh_strcasecmp(name, "width"))
681 return ptoi(name, &c1->c_width);
682 if (!mh_strcasecmp(name, "compwidth"))
683 return ptoi(name, &c1->c_cwidth);
684 if (!mh_strcasecmp(name, "length"))
685 return ptoi(name, &c1->c_length);
687 for (ap = triples; ap->t_name; ap++)
688 if (!mh_strcasecmp(ap->t_name, name)) {
689 c1->c_flags |= ap->t_on;
690 c1->c_flags &= ~ap->t_off;
699 ptoi(char *name, int *i)
703 if (*parptr++ != '=' || !*(cp = parse())) {
704 advise(NULL, "missing argument to variable %s", name);
714 ptos(char *name, char **s)
718 if (*parptr++ != '=') {
719 advise(NULL, "missing argument to variable %s", name);
723 if (*parptr != '"') {
724 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
728 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
729 if (*parptr == QUOTE)
736 if ((*parptr = c) == '"')
747 static char result[NAMESZ];
749 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
768 process(char *folder, char *fname, int ofilen, int ofilec)
774 switch (setjmp(env)) {
777 fp = mhl_action ? (*mhl_action) (fname) :
780 advise(fname, "unable to open");
788 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
790 SIGNAL(SIGINT, intrser);
791 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
795 SIGNAL(SIGINT, SIG_IGN);
796 if (mhl_action == NULL && fp != stdin)
801 holder.c_text = NULL;
803 free_queue(&msghd, &msgtl);
804 for (c1 = fmthd; c1; c1 = c1->c_next)
805 c1->c_flags &= ~HDROUTPUT;
812 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
815 struct mcomp *c1, *c2, *c3;
816 char **ip, name[NAMESZ], buf[BUFSIZ];
820 printf("%s", ofilen == 1 ? delim3 : delim4);
824 printf(" Forwarded Message%s",
825 ofilec > 1 ? "s" : "");
827 printf(" Message %d", ofilen);
835 if ((global.c_flags & CLEARSCR))
840 printf(">>> %s\n\n", mname);
845 strncpy(buf, "\n", sizeof(buf));
847 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
850 printf("Press <return> to list \"%s\"...", mname);
854 read(fileno(stdout), buf, sizeof(buf));
856 if (strchr(buf, '\n')) {
857 if ((global.c_flags & CLEARSCR))
870 printf(">>> %s\n\n", mname);
876 for (state = FLD;;) {
877 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
880 for (ip = ignores; *ip; ip++)
881 if (!mh_strcasecmp(name, *ip)) {
882 while (state == FLDPLUS)
883 state = m_getfld(state, name, buf, sizeof(buf), fp);
889 for (c2 = fmthd; c2; c2 = c2->c_next)
890 if (!mh_strcasecmp(c2->c_name, name))
893 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
894 for (c1 = msghd; c1; c1 = c1->c_next)
895 if (!mh_strcasecmp(c1->c_name,
897 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
901 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
902 while (state == FLDPLUS) {
903 state = m_getfld(state, name, buf,
905 c1->c_text = add(buf, c1->c_text);
908 c1->c_flags |= EXTRA;
914 for (c1 = fmthd; c1; c1 = c1->c_next) {
915 if (c1->c_flags & CLEARTEXT) {
916 putcomp(c1, c1, ONECOMP);
919 if (!mh_strcasecmp(c1->c_name, "messagename")) {
920 holder.c_text = concat("(Message ",
922 putcomp(c1, &holder, ONECOMP);
924 holder.c_text = NULL;
927 if (!mh_strcasecmp(c1->c_name, "extras")) {
928 for (c2 = msghd; c2; c2 = c2->c_next)
929 if (c2->c_flags & EXTRA)
930 putcomp(c1, c2, TWOCOMP);
933 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
934 holder.c_text = mh_xmalloc(sizeof(buf));
935 strncpy(holder.c_text, buf, sizeof(buf));
936 while (state == BODY) {
937 putcomp(c1, &holder, BODYCOMP);
938 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
941 holder.c_text = NULL;
944 for (c2 = msghd; c2; c2 = c2->c_next)
945 if (!mh_strcasecmp(c2->c_name,
947 putcomp(c1, c2, ONECOMP);
948 if (!(c1->c_flags & SPLIT))
956 advise(NULL, "format error in message %s", mname);
961 adios(NULL, "getfld() returned %d", state);
968 mcomp_flags(char *name)
972 for (ap = pairs; ap->p_name; ap++)
973 if (!mh_strcasecmp(ap->p_name, name))
974 return (ap->p_flags);
981 mcomp_add(long flags, char *s1, char *s2)
985 if (!(flags & ADDRFMT))
988 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
991 return add(s1, add(",\n", s2));
998 struct pqpair *pq_next;
1003 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1007 char buffer[BUFSIZ], error[BUFSIZ];
1009 struct pqpair *p, *q;
1011 struct mailname *mp;
1018 dat[3] = sizeof(buffer) - 1;
1020 fmt_compile(c1->c_nfs, &c1->c_fmt);
1022 if (!(c1->c_flags & ADDRFMT)) {
1023 FINDCOMP(cptr, "text");
1026 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1030 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1031 /* Don't need to append a newline, dctime() already did */
1032 c2->c_text = getcpy(buffer);
1038 (q = &pq)->pq_next = NULL;
1039 while ((cp = getname(ap))) {
1040 if ((p = (struct pqpair *)
1041 calloc((size_t) 1, sizeof(*p))) == NULL)
1042 adios(NULL, "unable to allocate pqpair memory");
1044 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1045 p->pq_text = getcpy(cp);
1046 p->pq_error = getcpy(error);
1048 p->pq_text = getcpy(mp->m_text);
1051 q = (q->pq_next = p);
1054 for (p = pq.pq_next; p; p = q) {
1055 FINDCOMP(cptr, "text");
1057 cptr->c_text = p->pq_text;
1058 FINDCOMP(cptr, "error");
1060 cptr->c_text = p->pq_error;
1062 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1065 c2->c_text = add(",\n", c2->c_text);
1066 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1068 c2->c_text = add(buffer, c2->c_text);
1078 c2->c_text = add("\n", c2->c_text);
1083 static struct mcomp *
1084 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1085 char *text, int flags)
1089 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1090 adios(NULL, "unable to allocate comp memory");
1092 c1->c_flags = flags & ~INIT;
1093 if ((c1->c_name = name ? getcpy(name) : NULL))
1094 c1->c_flags |= mcomp_flags(c1->c_name);
1095 c1->c_text = text ? getcpy(text) : NULL;
1098 c1->c_ovtxt = getcpy(global.c_ovtxt);
1099 c1->c_offset = global.c_offset;
1100 c1->c_ovoff = global. c_ovoff;
1101 c1->c_width = c1->c_length = 0;
1102 c1->c_cwidth = global.c_cwidth;
1103 c1->c_flags |= global.c_flags & GFLAGS;
1108 (*tail)->c_next = c1;
1116 free_queue(struct mcomp **head, struct mcomp **tail)
1118 struct mcomp *c1, *c2;
1120 for (c1 = *head; c1; c1 = c2) {
1131 free((char *) c1->c_fmt);
1135 *head = *tail = NULL;
1140 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1147 llim = c1->c_length ? c1->c_length : -1;
1148 wid = c1->c_width ? c1->c_width : global.c_width;
1149 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1151 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1153 if (wid < ovoff + strlen(ovtxt) + 5)
1154 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1157 if (c1->c_flags & CLEARTEXT) {
1163 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1164 mcomp_format(c1, c2);
1166 if (c1->c_flags & CENTER) {
1167 count = (c1->c_width ? c1->c_width : global.c_width)
1168 - c1->c_offset - strlen(c2->c_text);
1169 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1170 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1172 lm = c1->c_offset + (count / 2);
1178 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1179 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1180 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1183 putstr(c1->c_text ? c1->c_text : c1->c_name);
1184 if (flag != BODYCOMP) {
1186 if (!(c1->c_flags & SPLIT))
1187 c1->c_flags |= HDROUTPUT;
1190 if ((count = c1->c_cwidth -
1191 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1195 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1198 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1199 && !(c2->c_flags & NOCOMPONENT)) {
1200 if (c1->c_flags & UPPERCASE)
1201 for (cp = c2->c_name; *cp; cp++)
1206 if (!(c1->c_flags & SPLIT))
1207 c2->c_flags |= HDROUTPUT;
1210 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1214 if (c1->c_flags & UPPERCASE)
1215 for (cp = c2->c_text; *cp; cp++)
1221 if (flag == TWOCOMP)
1222 count = (c1->c_cwidth >= 0) ?
1223 c1->c_cwidth : strlen(c2->c_name) + 2;
1225 count = (c1->c_cwidth >= 0) ?
1226 c1->c_cwidth : strlen(c1->c_text ?
1227 c1->c_text : c1->c_name) + 2;
1229 count += c1->c_offset;
1231 if ((cp = oneline(c2->c_text, c1->c_flags)))
1235 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1237 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1238 putstr(c1->c_text ? c1->c_text : c1->c_name);
1244 if (flag == BODYCOMP && term == '\n')
1245 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1250 oneline(char *stuff, long flags)
1258 return (onelp = NULL);
1262 if (flags & COMPRESS) {
1263 for (spc = 1, cp = ret; *onelp; onelp++)
1264 if (isspace(*onelp)) {
1265 if (*onelp == '\n' &&
1267 (flags & ADDRFMT))) {
1282 while (*onelp && *onelp != '\n')
1284 if (*onelp == '\n') {
1288 if (flags & LEFTADJUST)
1289 while (*ret == ' ' || *ret == '\t')
1292 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1300 putstr(char *string)
1302 if (!column && lm > 0) {
1332 if (ontty != ISTTY || row != global.c_length)
1334 if (global.c_flags & BELL)
1338 read(fileno(stdout), buf, sizeof(buf));
1339 if (strchr(buf, '\n')) {
1340 if (global.c_flags & CLEARSCR)
1345 row = global.c_length / 3;
1364 ** If we are forwarding this message, and the first
1365 ** column contains a dash, then add a dash and a space.
1367 if (column == 0 && forwflg && ch == '-') {
1376 if (column >= wid) {
1380 putstr(ovtxt ? ovtxt : "");
1392 #ifndef RELIABLE_SIGNALS
1393 SIGNAL(SIGINT, intrser);
1405 #ifndef RELIABLE_SIGNALS
1406 SIGNAL(SIGPIPE, pipeser);
1416 #ifndef RELIABLE_SIGNALS
1417 SIGNAL(SIGQUIT, quitser);
1430 mhladios(char *what, char *fmt, ...)
1435 advertise(what, NULL, fmt, ap);
1446 longjmp(mhlenv, DONE);
1452 static int m_pid = NOTOK;
1453 static int sd = NOTOK;
1460 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1461 adios("standard output", "unable to dup()");
1463 if (pipe(pd) == NOTOK)
1464 adios("pipe", "unable to");
1466 switch (m_pid = fork()) {
1468 adios("fork", "unable to");
1471 SIGNAL(SIGINT, SIG_DFL);
1472 SIGNAL(SIGQUIT, SIG_DFL);
1475 if (pd[0] != fileno(stdin)) {
1476 dup2(pd[0], fileno(stdin));
1479 execlp(name, mhbasename(name), NULL);
1480 fprintf(stderr, "unable to exec ");
1486 if (pd[1] != fileno(stdout)) {
1487 dup2(pd[1], fileno(stdout));
1502 if (dup2(sd, fileno(stdout)) == NOTOK)
1503 adios("standard output", "unable to dup2()");