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 */
275 main(int argc, char **argv)
278 int i, width = 0, vecp = 0;
279 char *cp, *folder = NULL, *form = NULL;
280 char buf[BUFSIZ], *files[MAXARGS];
281 char **argp, **arguments;
284 setlocale(LC_ALL, "");
287 invo_name = mhbasename(argv[0]);
289 /* read user profile/context */
292 arguments = getarguments(invo_name, argc, argv, 1);
295 if ((cp = getenv("MHLDEBUG")) && *cp)
298 while ((cp = *argp++)) {
300 switch (smatch(++cp, mhlswitches)) {
302 ambigsw(cp, mhlswitches);
305 adios(NULL, "-%s unknown\n", cp);
308 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
309 print_help(buf, mhlswitches, 1);
312 print_version(invo_name);
330 if (!(folder = *argp++) || *folder == '-')
331 adios(NULL, "missing argument to %s",
335 if (!(form = *argp++) || *form == '-')
336 adios(NULL, "missing argument to %s",
341 if (!(cp = *argp++) || *cp == '-')
342 adios(NULL, "missing argument to %s",
344 sleepsw = atoi(cp); /* ZERO ok! */
348 if (!(moreproc = *argp++) || *moreproc == '-')
349 adios(NULL, "missing argument to %s",
354 if (!(cp = *argp++) || *cp == '-')
355 adios(NULL, "missing argument to %s",
357 if ((length = atoi(cp)) < 1)
358 adios(NULL, "bad argument %s %s",
362 if (!(cp = *argp++) || *cp == '-')
363 adios(NULL, "missing argument to %s",
365 if ((width = atoi(cp)) < 1)
366 adios(NULL, "bad argument %s %s",
371 if (!(digest = *argp++) ||
373 adios(NULL, "missing argument to %s",
377 if (!(cp = *argp++) || *cp == '-')
378 adios(NULL, "missing argument to %s",
380 if ((issue = atoi(cp)) < 1)
381 adios(NULL, "bad argument %s %s",
385 if (!(cp = *argp++) || *cp == '-')
386 adios(NULL, "missing argument to %s",
388 if ((volume = atoi(cp)) < 1)
389 adios(NULL, "bad argument %s %s",
394 forwall++; /* fall */
397 clearflg = -1;/* XXX */
409 folder = getenv("mhfolder");
411 if (isatty(fileno(stdout))) {
412 if (moreproc && *moreproc) {
414 SIGNAL(SIGINT, SIG_IGN);
415 SIGNAL2(SIGQUIT, quitser);
417 SIGNAL2(SIGPIPE, pipeser);
421 SIGNAL(SIGINT, SIG_IGN);
422 SIGNAL2(SIGQUIT, quitser);
429 mhl_format(form ? form : mhlformat, length, width);
432 process(folder, NULL, 1, vecp = 1);
434 for (i = 0; i < vecp; i++)
435 process(folder, files[i], i + 1, vecp);
440 printf("%s", delim4);
442 snprintf(buf, sizeof(buf),
443 "End of %s Digest\n", digest);
445 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
448 for (cp = buf + i; i > 1; i--)
454 printf("\n------- End of Forwarded Message%s\n\n",
455 vecp > 1 ? "s" : "");
459 if (ferror(stdout)) {
460 adios("output", "error writing");
463 if (clearflg > 0 && ontty == NOTTY)
474 mhl_format(char *file, int length, int width)
478 char *ap, buffer[BUFSIZ], name[NAMESZ];
482 static dev_t dev = 0;
483 static ino_t ino = 0;
484 static time_t mtime = 0;
487 if (stat(etcpath(file), &st) != NOTOK
488 && mtime == st.st_mtime
493 free_queue(&fmthd, &fmttl);
496 if ((fp = fopen(etcpath(file), "r")) == NULL)
497 adios(file, "unable to open format file");
499 if (fstat(fileno(fp), &st) != NOTOK) {
505 global.c_ovtxt = global.c_nfs = NULL;
509 if ((i = sc_width()) > 5)
511 global.c_cwidth = -1;
512 if ((i = sc_length()) > 5)
513 global.c_length = i - 1;
514 global.c_flags = BELL; /* BELL is default */
515 *(ip = ignores) = NULL;
517 while (vfgets(fp, &ap) == OK) {
522 if ((cp = strchr(bp, '\n')))
526 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
532 strncpy(name, parse(), sizeof(name));
538 ** Split this list of fields to ignore, and copy
539 ** it to the end of the current "ignores" list.
541 if (!mh_strcasecmp(name, "ignores")) {
542 char **tmparray, **p;
545 /* split the fields */
546 tmparray = brkstring(getcpy(++parptr), ",",
549 /* count number of fields split */
555 ** copy pointers to split fields
558 ip = copyip(tmparray, ip,
559 MAXARGS - num_ignores);
565 if (evalvar(&global))
566 adios(NULL, "format file syntax error: %s", bp);
573 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
574 while (*parptr == ':' || *parptr == ',') {
577 adios(NULL, "format file syntax error: %s", bp);
579 if (!c1->c_nfs && global.c_nfs) {
580 if ((c1->c_flags & DATEFMT) &&
581 (global.c_flags & DATEFMT)) {
582 c1->c_nfs = getcpy(global.c_nfs);
583 } else if ((c1->c_flags & ADDRFMT) &&
584 (global.c_flags & ADDRFMT)) {
585 c1->c_nfs = getcpy(global.c_nfs);
591 adios(NULL, "format file syntax error: %s", bp);
597 for (c1 = fmthd; c1; c1 = c1->c_next) {
598 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
599 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
600 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);
601 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
607 global.c_flags |= CLEARSCR;
610 global.c_flags &= ~CLEARSCR;
613 switch (bellflg) { /* command line may override format file */
615 global.c_flags |= BELL;
618 global.c_flags &= ~BELL;
623 global.c_length = length;
625 global.c_width = width;
626 if (global.c_length < 5)
627 global.c_length = 10000;
628 if (global.c_width < 5)
629 global.c_width = 10000;
634 evalvar(struct mcomp *c1)
636 char *cp, name[NAMESZ];
641 strncpy(name, parse(), sizeof(name));
643 if (!mh_strcasecmp(name, "component")) {
644 if (ptos(name, &c1->c_text))
646 c1->c_flags &= ~NOCOMPONENT;
650 if (!mh_strcasecmp(name, "overflowtext"))
651 return ptos(name, &c1->c_ovtxt);
653 if (!mh_strcasecmp(name, "formatfield")) {
658 cp = concat("=", cp, NULL);
659 nfs = new_fs(cp, NULL);
661 c1->c_nfs = getcpy(nfs);
662 c1->c_flags |= FORMAT;
666 if (!mh_strcasecmp(name, "decode")) {
669 nfs = new_fs("=%(decode{text})", NULL);
670 c1->c_nfs = getcpy(nfs);
671 c1->c_flags |= FORMAT;
675 if (!mh_strcasecmp(name, "offset"))
676 return ptoi(name, &c1->c_offset);
677 if (!mh_strcasecmp(name, "overflowoffset"))
678 return ptoi(name, &c1->c_ovoff);
679 if (!mh_strcasecmp(name, "width"))
680 return ptoi(name, &c1->c_width);
681 if (!mh_strcasecmp(name, "compwidth"))
682 return ptoi(name, &c1->c_cwidth);
683 if (!mh_strcasecmp(name, "length"))
684 return ptoi(name, &c1->c_length);
686 for (ap = triples; ap->t_name; ap++)
687 if (!mh_strcasecmp(ap->t_name, name)) {
688 c1->c_flags |= ap->t_on;
689 c1->c_flags &= ~ap->t_off;
698 ptoi(char *name, int *i)
702 if (*parptr++ != '=' || !*(cp = parse())) {
703 advise(NULL, "missing argument to variable %s", name);
713 ptos(char *name, char **s)
717 if (*parptr++ != '=') {
718 advise(NULL, "missing argument to variable %s", name);
722 if (*parptr != '"') {
723 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
727 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
728 if (*parptr == QUOTE)
735 if ((*parptr = c) == '"')
746 static char result[NAMESZ];
748 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
767 process(char *folder, char *fname, int ofilen, int ofilec)
773 switch (setjmp(env)) {
776 fp = mhl_action ? (*mhl_action) (fname) :
779 advise(fname, "unable to open");
787 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
789 SIGNAL(SIGINT, intrser);
790 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
794 SIGNAL(SIGINT, SIG_IGN);
795 if (mhl_action == NULL && fp != stdin)
800 holder.c_text = NULL;
802 free_queue(&msghd, &msgtl);
803 for (c1 = fmthd; c1; c1 = c1->c_next)
804 c1->c_flags &= ~HDROUTPUT;
811 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
814 struct mcomp *c1, *c2, *c3;
815 char **ip, name[NAMESZ], buf[BUFSIZ];
819 printf("%s", ofilen == 1 ? delim3 : delim4);
823 printf(" Forwarded Message%s",
824 ofilec > 1 ? "s" : "");
826 printf(" Message %d", ofilen);
834 if ((global.c_flags & CLEARSCR))
839 printf(">>> %s\n\n", mname);
844 strncpy(buf, "\n", sizeof(buf));
846 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
849 printf("Press <return> to list \"%s\"...", mname);
853 read(fileno(stdout), buf, sizeof(buf));
855 if (strchr(buf, '\n')) {
856 if ((global.c_flags & CLEARSCR))
869 printf(">>> %s\n\n", mname);
875 for (state = FLD;;) {
876 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
879 for (ip = ignores; *ip; ip++)
880 if (!mh_strcasecmp(name, *ip)) {
881 while (state == FLDPLUS)
882 state = m_getfld(state, name, buf, sizeof(buf), fp);
888 for (c2 = fmthd; c2; c2 = c2->c_next)
889 if (!mh_strcasecmp(c2->c_name, name))
892 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
893 for (c1 = msghd; c1; c1 = c1->c_next)
894 if (!mh_strcasecmp(c1->c_name,
896 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
900 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
901 while (state == FLDPLUS) {
902 state = m_getfld(state, name, buf,
904 c1->c_text = add(buf, c1->c_text);
907 c1->c_flags |= EXTRA;
913 for (c1 = fmthd; c1; c1 = c1->c_next) {
914 if (c1->c_flags & CLEARTEXT) {
915 putcomp(c1, c1, ONECOMP);
918 if (!mh_strcasecmp(c1->c_name, "messagename")) {
919 holder.c_text = concat("(Message ",
921 putcomp(c1, &holder, ONECOMP);
923 holder.c_text = NULL;
926 if (!mh_strcasecmp(c1->c_name, "extras")) {
927 for (c2 = msghd; c2; c2 = c2->c_next)
928 if (c2->c_flags & EXTRA)
929 putcomp(c1, c2, TWOCOMP);
932 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
933 holder.c_text = mh_xmalloc(sizeof(buf));
934 strncpy(holder.c_text, buf, sizeof(buf));
935 while (state == BODY) {
936 putcomp(c1, &holder, BODYCOMP);
937 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
940 holder.c_text = NULL;
943 for (c2 = msghd; c2; c2 = c2->c_next)
944 if (!mh_strcasecmp(c2->c_name,
946 putcomp(c1, c2, ONECOMP);
947 if (!(c1->c_flags & SPLIT))
955 advise(NULL, "format error in message %s", mname);
960 adios(NULL, "getfld() returned %d", state);
967 mcomp_flags(char *name)
971 for (ap = pairs; ap->p_name; ap++)
972 if (!mh_strcasecmp(ap->p_name, name))
973 return (ap->p_flags);
980 mcomp_add(long flags, char *s1, char *s2)
984 if (!(flags & ADDRFMT))
987 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
990 return add(s1, add(",\n", s2));
997 struct pqpair *pq_next;
1002 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1006 char buffer[BUFSIZ], error[BUFSIZ];
1008 struct pqpair *p, *q;
1010 struct mailname *mp;
1017 dat[3] = sizeof(buffer) - 1;
1019 fmt_compile(c1->c_nfs, &c1->c_fmt);
1021 if (!(c1->c_flags & ADDRFMT)) {
1022 FINDCOMP(cptr, "text");
1025 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1029 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1030 /* Don't need to append a newline, dctime() already did */
1031 c2->c_text = getcpy(buffer);
1037 (q = &pq)->pq_next = NULL;
1038 while ((cp = getname(ap))) {
1039 if ((p = (struct pqpair *)
1040 calloc((size_t) 1, sizeof(*p))) == NULL)
1041 adios(NULL, "unable to allocate pqpair memory");
1043 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1044 p->pq_text = getcpy(cp);
1045 p->pq_error = getcpy(error);
1047 p->pq_text = getcpy(mp->m_text);
1050 q = (q->pq_next = p);
1053 for (p = pq.pq_next; p; p = q) {
1054 FINDCOMP(cptr, "text");
1056 cptr->c_text = p->pq_text;
1057 FINDCOMP(cptr, "error");
1059 cptr->c_text = p->pq_error;
1061 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1064 c2->c_text = add(",\n", c2->c_text);
1065 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1067 c2->c_text = add(buffer, c2->c_text);
1077 c2->c_text = add("\n", c2->c_text);
1082 static struct mcomp *
1083 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1084 char *text, int flags)
1088 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1089 adios(NULL, "unable to allocate comp memory");
1091 c1->c_flags = flags & ~INIT;
1092 if ((c1->c_name = name ? getcpy(name) : NULL))
1093 c1->c_flags |= mcomp_flags(c1->c_name);
1094 c1->c_text = text ? getcpy(text) : NULL;
1097 c1->c_ovtxt = getcpy(global.c_ovtxt);
1098 c1->c_offset = global.c_offset;
1099 c1->c_ovoff = global. c_ovoff;
1100 c1->c_width = c1->c_length = 0;
1101 c1->c_cwidth = global.c_cwidth;
1102 c1->c_flags |= global.c_flags & GFLAGS;
1107 (*tail)->c_next = c1;
1115 free_queue(struct mcomp **head, struct mcomp **tail)
1117 struct mcomp *c1, *c2;
1119 for (c1 = *head; c1; c1 = c2) {
1130 free((char *) c1->c_fmt);
1134 *head = *tail = NULL;
1139 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1146 llim = c1->c_length ? c1->c_length : -1;
1147 wid = c1->c_width ? c1->c_width : global.c_width;
1148 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1150 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1152 if (wid < ovoff + strlen(ovtxt) + 5)
1153 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1156 if (c1->c_flags & CLEARTEXT) {
1162 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1163 mcomp_format(c1, c2);
1165 if (c1->c_flags & CENTER) {
1166 count = (c1->c_width ? c1->c_width : global.c_width)
1167 - c1->c_offset - strlen(c2->c_text);
1168 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1169 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1171 lm = c1->c_offset + (count / 2);
1177 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1178 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1179 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1182 putstr(c1->c_text ? c1->c_text : c1->c_name);
1183 if (flag != BODYCOMP) {
1185 if (!(c1->c_flags & SPLIT))
1186 c1->c_flags |= HDROUTPUT;
1189 if ((count = c1->c_cwidth -
1190 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1194 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1197 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1198 && !(c2->c_flags & NOCOMPONENT)) {
1199 if (c1->c_flags & UPPERCASE)
1200 for (cp = c2->c_name; *cp; cp++)
1205 if (!(c1->c_flags & SPLIT))
1206 c2->c_flags |= HDROUTPUT;
1209 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1213 if (c1->c_flags & UPPERCASE)
1214 for (cp = c2->c_text; *cp; cp++)
1220 if (flag == TWOCOMP)
1221 count = (c1->c_cwidth >= 0) ?
1222 c1->c_cwidth : strlen(c2->c_name) + 2;
1224 count = (c1->c_cwidth >= 0) ?
1225 c1->c_cwidth : strlen(c1->c_text ?
1226 c1->c_text : c1->c_name) + 2;
1228 count += c1->c_offset;
1230 if ((cp = oneline(c2->c_text, c1->c_flags)))
1234 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1236 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1237 putstr(c1->c_text ? c1->c_text : c1->c_name);
1243 if (flag == BODYCOMP && term == '\n')
1244 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1249 oneline(char *stuff, long flags)
1257 return (onelp = NULL);
1261 if (flags & COMPRESS) {
1262 for (spc = 1, cp = ret; *onelp; onelp++)
1263 if (isspace(*onelp)) {
1264 if (*onelp == '\n' &&
1266 (flags & ADDRFMT))) {
1281 while (*onelp && *onelp != '\n')
1283 if (*onelp == '\n') {
1287 if (flags & LEFTADJUST)
1288 while (*ret == ' ' || *ret == '\t')
1291 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1299 putstr(char *string)
1301 if (!column && lm > 0) {
1331 if (ontty != ISTTY || row != global.c_length)
1333 if (global.c_flags & BELL)
1337 read(fileno(stdout), buf, sizeof(buf));
1338 if (strchr(buf, '\n')) {
1339 if (global.c_flags & CLEARSCR)
1344 row = global.c_length / 3;
1363 ** If we are forwarding this message, and the first
1364 ** column contains a dash, then add a dash and a space.
1366 if (column == 0 && forwflg && ch == '-') {
1375 if (column >= wid) {
1379 putstr(ovtxt ? ovtxt : "");
1391 #ifndef RELIABLE_SIGNALS
1392 SIGNAL(SIGINT, intrser);
1404 #ifndef RELIABLE_SIGNALS
1405 SIGNAL(SIGPIPE, pipeser);
1415 #ifndef RELIABLE_SIGNALS
1416 SIGNAL(SIGQUIT, quitser);
1429 mhladios(char *what, char *fmt, ...)
1434 advertise(what, NULL, fmt, ap);
1445 longjmp(mhlenv, DONE);
1451 static int m_pid = NOTOK;
1452 static int sd = NOTOK;
1459 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1460 adios("standard output", "unable to dup()");
1462 if (pipe(pd) == NOTOK)
1463 adios("pipe", "unable to");
1465 switch (m_pid = fork()) {
1467 adios("fork", "unable to");
1470 SIGNAL(SIGINT, SIG_DFL);
1471 SIGNAL(SIGQUIT, SIG_DFL);
1474 if (pd[0] != fileno(stdin)) {
1475 dup2(pd[0], fileno(stdin));
1478 execlp(name, mhbasename(name), NULL);
1479 fprintf(stderr, "unable to exec ");
1485 if (pd[1] != fileno(stdout)) {
1486 dup2(pd[1], fileno(stdout));
1501 if (dup2(sd, fileno(stdout)) == NOTOK)
1502 adios("standard output", "unable to dup2()");