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 },
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 *);
270 void clear_screen(void); /* from termsbr.c */
271 int SOprintf(char *, ...); /* from termsbr.c */
272 int sc_width(void); /* from termsbr.c */
273 int sc_length(void); /* from termsbr.c */
274 int sc_hardcopy(void); /* from termsbr.c */
278 main(int argc, char **argv)
280 int length = 0, nomore = 0;
281 int i, width = 0, vecp = 0;
282 char *cp, *folder = NULL, *form = NULL;
283 char buf[BUFSIZ], *files[MAXARGS];
284 char **argp, **arguments;
287 setlocale(LC_ALL, "");
290 invo_name = mhbasename(argv[0]);
292 /* read user profile/context */
295 arguments = getarguments(invo_name, argc, argv, 1);
298 if ((cp = getenv("MHLDEBUG")) && *cp)
301 while ((cp = *argp++)) {
303 switch (smatch(++cp, mhlswitches)) {
305 ambigsw(cp, mhlswitches);
308 adios(NULL, "-%s unknown\n", cp);
311 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
312 print_help(buf, mhlswitches, 1);
315 print_version(invo_name);
333 if (!(folder = *argp++) || *folder == '-')
334 adios(NULL, "missing argument to %s",
338 if (!(form = *argp++) || *form == '-')
339 adios(NULL, "missing argument to %s",
344 if (!(cp = *argp++) || *cp == '-')
345 adios(NULL, "missing argument to %s",
347 sleepsw = atoi(cp); /* ZERO ok! */
351 if (!(moreproc = *argp++) || *moreproc == '-')
352 adios(NULL, "missing argument to %s",
360 if (!(cp = *argp++) || *cp == '-')
361 adios(NULL, "missing argument to %s",
363 if ((length = atoi(cp)) < 1)
364 adios(NULL, "bad argument %s %s",
368 if (!(cp = *argp++) || *cp == '-')
369 adios(NULL, "missing argument to %s",
371 if ((width = atoi(cp)) < 1)
372 adios(NULL, "bad argument %s %s",
377 if (!(digest = *argp++) ||
379 adios(NULL, "missing argument to %s",
383 if (!(cp = *argp++) || *cp == '-')
384 adios(NULL, "missing argument to %s",
386 if ((issue = atoi(cp)) < 1)
387 adios(NULL, "bad argument %s %s",
391 if (!(cp = *argp++) || *cp == '-')
392 adios(NULL, "missing argument to %s",
394 if ((volume = atoi(cp)) < 1)
395 adios(NULL, "bad argument %s %s",
400 forwall++; /* fall */
403 clearflg = -1;/* XXX */
415 folder = getenv("mhfolder");
417 if (isatty(fileno(stdout))) {
418 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
420 SIGNAL(SIGINT, SIG_IGN);
421 SIGNAL2(SIGQUIT, quitser);
423 SIGNAL2(SIGPIPE, pipeser);
427 SIGNAL(SIGINT, SIG_IGN);
428 SIGNAL2(SIGQUIT, quitser);
435 mhl_format(form ? form : mhlformat, length, width);
438 process(folder, NULL, 1, vecp = 1);
440 for (i = 0; i < vecp; i++)
441 process(folder, files[i], i + 1, vecp);
446 printf("%s", delim4);
448 snprintf(buf, sizeof(buf),
449 "End of %s Digest\n", digest);
451 snprintf(buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n", digest, volume, issue);
454 for (cp = buf + i; i > 1; i--)
460 printf("\n------- End of Forwarded Message%s\n\n",
461 vecp > 1 ? "s" : "");
465 if (ferror(stdout)) {
466 adios("output", "error writing");
469 if (clearflg > 0 && ontty == NOTTY)
480 mhl_format(char *file, int length, int width)
484 char *ap, buffer[BUFSIZ], name[NAMESZ];
488 static dev_t dev = 0;
489 static ino_t ino = 0;
490 static time_t mtime = 0;
493 if (stat(etcpath(file), &st) != NOTOK
494 && mtime == st.st_mtime
499 free_queue(&fmthd, &fmttl);
502 if ((fp = fopen(etcpath(file), "r")) == NULL)
503 adios(file, "unable to open format file");
505 if (fstat(fileno(fp), &st) != NOTOK) {
511 global.c_ovtxt = global.c_nfs = NULL;
515 if ((i = sc_width()) > 5)
517 global.c_cwidth = -1;
518 if ((i = sc_length()) > 5)
519 global.c_length = i - 1;
520 global.c_flags = BELL; /* BELL is default */
521 *(ip = ignores) = NULL;
523 while (vfgets(fp, &ap) == OK) {
528 if ((cp = strchr(bp, '\n')))
532 c1 = add_queue(&fmthd, &fmttl, NULL, bp + 1,
538 strncpy(name, parse(), sizeof(name));
544 ** Split this list of fields to ignore, and copy
545 ** it to the end of the current "ignores" list.
547 if (!mh_strcasecmp(name, "ignores")) {
548 char **tmparray, **p;
551 /* split the fields */
552 tmparray = brkstring(getcpy(++parptr), ",",
555 /* count number of fields split */
561 ** copy pointers to split fields
564 ip = copyip(tmparray, ip,
565 MAXARGS - num_ignores);
571 if (evalvar(&global))
572 adios(NULL, "format file syntax error: %s", bp);
579 c1 = add_queue(&fmthd, &fmttl, name, NULL, INIT);
580 while (*parptr == ':' || *parptr == ',') {
583 adios(NULL, "format file syntax error: %s", bp);
585 if (!c1->c_nfs && global.c_nfs) {
586 if ((c1->c_flags & DATEFMT) &&
587 (global.c_flags & DATEFMT)) {
588 c1->c_nfs = getcpy(global.c_nfs);
589 } else if ((c1->c_flags & ADDRFMT) &&
590 (global.c_flags & ADDRFMT)) {
591 c1->c_nfs = getcpy(global.c_nfs);
597 adios(NULL, "format file syntax error: %s", bp);
603 for (c1 = fmthd; c1; c1 = c1->c_next) {
604 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
605 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
606 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);
607 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
613 global.c_flags |= CLEARSCR;
616 global.c_flags &= ~CLEARSCR;
619 switch (bellflg) { /* command line may override format file */
621 global.c_flags |= BELL;
624 global.c_flags &= ~BELL;
629 global.c_length = length;
631 global.c_width = width;
632 if (global.c_length < 5)
633 global.c_length = 10000;
634 if (global.c_width < 5)
635 global.c_width = 10000;
640 evalvar(struct mcomp *c1)
642 char *cp, name[NAMESZ];
647 strncpy(name, parse(), sizeof(name));
649 if (!mh_strcasecmp(name, "component")) {
650 if (ptos(name, &c1->c_text))
652 c1->c_flags &= ~NOCOMPONENT;
656 if (!mh_strcasecmp(name, "overflowtext"))
657 return ptos(name, &c1->c_ovtxt);
659 if (!mh_strcasecmp(name, "formatfield")) {
664 cp = concat("=", cp, NULL);
665 nfs = new_fs(cp, NULL);
667 c1->c_nfs = getcpy(nfs);
668 c1->c_flags |= FORMAT;
672 if (!mh_strcasecmp(name, "decode")) {
675 nfs = new_fs("=%(decode{text})", NULL);
676 c1->c_nfs = getcpy(nfs);
677 c1->c_flags |= FORMAT;
681 if (!mh_strcasecmp(name, "offset"))
682 return ptoi(name, &c1->c_offset);
683 if (!mh_strcasecmp(name, "overflowoffset"))
684 return ptoi(name, &c1->c_ovoff);
685 if (!mh_strcasecmp(name, "width"))
686 return ptoi(name, &c1->c_width);
687 if (!mh_strcasecmp(name, "compwidth"))
688 return ptoi(name, &c1->c_cwidth);
689 if (!mh_strcasecmp(name, "length"))
690 return ptoi(name, &c1->c_length);
692 for (ap = triples; ap->t_name; ap++)
693 if (!mh_strcasecmp(ap->t_name, name)) {
694 c1->c_flags |= ap->t_on;
695 c1->c_flags &= ~ap->t_off;
704 ptoi(char *name, int *i)
708 if (*parptr++ != '=' || !*(cp = parse())) {
709 advise(NULL, "missing argument to variable %s", name);
719 ptos(char *name, char **s)
723 if (*parptr++ != '=') {
724 advise(NULL, "missing argument to variable %s", name);
728 if (*parptr != '"') {
729 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
733 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
734 if (*parptr == QUOTE)
741 if ((*parptr = c) == '"')
752 static char result[NAMESZ];
754 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
773 process(char *folder, char *fname, int ofilen, int ofilec)
779 switch (setjmp(env)) {
782 fp = mhl_action ? (*mhl_action) (fname) :
785 advise(fname, "unable to open");
793 cp = folder ? concat(folder, ":", fname, NULL) : getcpy(fname);
795 SIGNAL(SIGINT, intrser);
796 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
800 SIGNAL(SIGINT, SIG_IGN);
801 if (mhl_action == NULL && fp != stdin)
806 holder.c_text = NULL;
808 free_queue(&msghd, &msgtl);
809 for (c1 = fmthd; c1; c1 = c1->c_next)
810 c1->c_flags &= ~HDROUTPUT;
817 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
820 struct mcomp *c1, *c2, *c3;
821 char **ip, name[NAMESZ], buf[BUFSIZ];
825 printf("%s", ofilen == 1 ? delim3 : delim4);
829 printf(" Forwarded Message%s",
830 ofilec > 1 ? "s" : "");
832 printf(" Message %d", ofilen);
840 if ((global.c_flags & CLEARSCR))
845 printf(">>> %s\n\n", mname);
850 strncpy(buf, "\n", sizeof(buf));
852 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
855 printf("Press <return> to list \"%s\"...", mname);
859 read(fileno(stdout), buf, sizeof(buf));
861 if (strchr(buf, '\n')) {
862 if ((global.c_flags & CLEARSCR))
875 printf(">>> %s\n\n", mname);
881 for (state = FLD;;) {
882 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
885 for (ip = ignores; *ip; ip++)
886 if (!mh_strcasecmp(name, *ip)) {
887 while (state == FLDPLUS)
888 state = m_getfld(state, name, buf, sizeof(buf), fp);
894 for (c2 = fmthd; c2; c2 = c2->c_next)
895 if (!mh_strcasecmp(c2->c_name, name))
898 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
899 for (c1 = msghd; c1; c1 = c1->c_next)
900 if (!mh_strcasecmp(c1->c_name,
902 c1->c_text = mcomp_add(c1->c_flags, buf, c1->c_text);
906 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
907 while (state == FLDPLUS) {
908 state = m_getfld(state, name, buf,
910 c1->c_text = add(buf, c1->c_text);
913 c1->c_flags |= EXTRA;
919 for (c1 = fmthd; c1; c1 = c1->c_next) {
920 if (c1->c_flags & CLEARTEXT) {
921 putcomp(c1, c1, ONECOMP);
924 if (!mh_strcasecmp(c1->c_name, "messagename")) {
925 holder.c_text = concat("(Message ",
927 putcomp(c1, &holder, ONECOMP);
929 holder.c_text = NULL;
932 if (!mh_strcasecmp(c1->c_name, "extras")) {
933 for (c2 = msghd; c2; c2 = c2->c_next)
934 if (c2->c_flags & EXTRA)
935 putcomp(c1, c2, TWOCOMP);
938 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
939 holder.c_text = mh_xmalloc(sizeof(buf));
940 strncpy(holder.c_text, buf, sizeof(buf));
941 while (state == BODY) {
942 putcomp(c1, &holder, BODYCOMP);
943 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
946 holder.c_text = NULL;
949 for (c2 = msghd; c2; c2 = c2->c_next)
950 if (!mh_strcasecmp(c2->c_name,
952 putcomp(c1, c2, ONECOMP);
953 if (!(c1->c_flags & SPLIT))
961 advise(NULL, "format error in message %s", mname);
966 adios(NULL, "getfld() returned %d", state);
973 mcomp_flags(char *name)
977 for (ap = pairs; ap->p_name; ap++)
978 if (!mh_strcasecmp(ap->p_name, name))
979 return (ap->p_flags);
986 mcomp_add(long flags, char *s1, char *s2)
990 if (!(flags & ADDRFMT))
993 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
996 return add(s1, add(",\n", s2));
1003 struct pqpair *pq_next;
1008 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1012 char buffer[BUFSIZ], error[BUFSIZ];
1014 struct pqpair *p, *q;
1016 struct mailname *mp;
1023 dat[3] = sizeof(buffer) - 1;
1025 fmt_compile(c1->c_nfs, &c1->c_fmt);
1027 if (!(c1->c_flags & ADDRFMT)) {
1028 FINDCOMP(cptr, "text");
1031 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1035 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1036 /* Don't need to append a newline, dctime() already did */
1037 c2->c_text = getcpy(buffer);
1043 (q = &pq)->pq_next = NULL;
1044 while ((cp = getname(ap))) {
1045 if ((p = (struct pqpair *)
1046 calloc((size_t) 1, sizeof(*p))) == NULL)
1047 adios(NULL, "unable to allocate pqpair memory");
1049 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1050 p->pq_text = getcpy(cp);
1051 p->pq_error = getcpy(error);
1053 p->pq_text = getcpy(mp->m_text);
1056 q = (q->pq_next = p);
1059 for (p = pq.pq_next; p; p = q) {
1060 FINDCOMP(cptr, "text");
1062 cptr->c_text = p->pq_text;
1063 FINDCOMP(cptr, "error");
1065 cptr->c_text = p->pq_error;
1067 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1070 c2->c_text = add(",\n", c2->c_text);
1071 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1073 c2->c_text = add(buffer, c2->c_text);
1083 c2->c_text = add("\n", c2->c_text);
1088 static struct mcomp *
1089 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1090 char *text, int flags)
1094 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1095 adios(NULL, "unable to allocate comp memory");
1097 c1->c_flags = flags & ~INIT;
1098 if ((c1->c_name = name ? getcpy(name) : NULL))
1099 c1->c_flags |= mcomp_flags(c1->c_name);
1100 c1->c_text = text ? getcpy(text) : NULL;
1103 c1->c_ovtxt = getcpy(global.c_ovtxt);
1104 c1->c_offset = global.c_offset;
1105 c1->c_ovoff = global. c_ovoff;
1106 c1->c_width = c1->c_length = 0;
1107 c1->c_cwidth = global.c_cwidth;
1108 c1->c_flags |= global.c_flags & GFLAGS;
1113 (*tail)->c_next = c1;
1121 free_queue(struct mcomp **head, struct mcomp **tail)
1123 struct mcomp *c1, *c2;
1125 for (c1 = *head; c1; c1 = c2) {
1136 free((char *) c1->c_fmt);
1140 *head = *tail = NULL;
1145 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1152 llim = c1->c_length ? c1->c_length : -1;
1153 wid = c1->c_width ? c1->c_width : global.c_width;
1154 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1156 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1158 if (wid < ovoff + strlen(ovtxt) + 5)
1159 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1162 if (c1->c_flags & CLEARTEXT) {
1168 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1169 mcomp_format(c1, c2);
1171 if (c1->c_flags & CENTER) {
1172 count = (c1->c_width ? c1->c_width : global.c_width)
1173 - c1->c_offset - strlen(c2->c_text);
1174 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1175 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1177 lm = c1->c_offset + (count / 2);
1183 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1184 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1185 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1188 putstr(c1->c_text ? c1->c_text : c1->c_name);
1189 if (flag != BODYCOMP) {
1191 if (!(c1->c_flags & SPLIT))
1192 c1->c_flags |= HDROUTPUT;
1195 if ((count = c1->c_cwidth -
1196 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1200 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1203 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1204 && !(c2->c_flags & NOCOMPONENT)) {
1205 if (c1->c_flags & UPPERCASE)
1206 for (cp = c2->c_name; *cp; cp++)
1211 if (!(c1->c_flags & SPLIT))
1212 c2->c_flags |= HDROUTPUT;
1215 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1219 if (c1->c_flags & UPPERCASE)
1220 for (cp = c2->c_text; *cp; cp++)
1226 if (flag == TWOCOMP)
1227 count = (c1->c_cwidth >= 0) ?
1228 c1->c_cwidth : strlen(c2->c_name) + 2;
1230 count = (c1->c_cwidth >= 0) ?
1231 c1->c_cwidth : strlen(c1->c_text ?
1232 c1->c_text : c1->c_name) + 2;
1234 count += c1->c_offset;
1236 if ((cp = oneline(c2->c_text, c1->c_flags)))
1240 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1242 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1243 putstr(c1->c_text ? c1->c_text : c1->c_name);
1249 if (flag == BODYCOMP && term == '\n')
1250 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1255 oneline(char *stuff, long flags)
1263 return (onelp = NULL);
1267 if (flags & COMPRESS) {
1268 for (spc = 1, cp = ret; *onelp; onelp++)
1269 if (isspace(*onelp)) {
1270 if (*onelp == '\n' &&
1272 (flags & ADDRFMT))) {
1287 while (*onelp && *onelp != '\n')
1289 if (*onelp == '\n') {
1293 if (flags & LEFTADJUST)
1294 while (*ret == ' ' || *ret == '\t')
1297 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1305 putstr(char *string)
1307 if (!column && lm > 0) {
1337 if (ontty != ISTTY || row != global.c_length)
1339 if (global.c_flags & BELL)
1343 read(fileno(stdout), buf, sizeof(buf));
1344 if (strchr(buf, '\n')) {
1345 if (global.c_flags & CLEARSCR)
1350 row = global.c_length / 3;
1369 ** If we are forwarding this message, and the first
1370 ** column contains a dash, then add a dash and a space.
1372 if (column == 0 && forwflg && ch == '-') {
1381 if (column >= wid) {
1385 putstr(ovtxt ? ovtxt : "");
1397 #ifndef RELIABLE_SIGNALS
1398 SIGNAL(SIGINT, intrser);
1410 #ifndef RELIABLE_SIGNALS
1411 SIGNAL(SIGPIPE, pipeser);
1421 #ifndef RELIABLE_SIGNALS
1422 SIGNAL(SIGQUIT, quitser);
1435 mhladios(char *what, char *fmt, ...)
1440 advertise(what, NULL, fmt, ap);
1451 longjmp(mhlenv, DONE);
1457 static int m_pid = NOTOK;
1458 static int sd = NOTOK;
1465 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1466 adios("standard output", "unable to dup()");
1468 if (pipe(pd) == NOTOK)
1469 adios("pipe", "unable to");
1471 switch (m_pid = fork()) {
1473 adios("fork", "unable to");
1476 SIGNAL(SIGINT, SIG_DFL);
1477 SIGNAL(SIGQUIT, SIG_DFL);
1480 if (pd[0] != fileno(stdin)) {
1481 dup2(pd[0], fileno(stdin));
1484 execlp(name, mhbasename(name), NULL);
1485 fprintf(stderr, "unable to exec ");
1491 if (pd[1] != fileno(stdout)) {
1492 dup2(pd[1], fileno(stdout));
1507 if (dup2(sd, fileno(stdout)) == NOTOK)
1508 adios("standard output", "unable to dup2()");