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 },
67 { "dashstuffing", -12 }, /* interface from forw */
68 #define NBITSTUFFSW 12
69 { "nodashstuffing", -14 }, /* interface from forw */
75 { "forward", -7 }, /* interface from forw */
77 { "forwall", -7 }, /* interface from forw */
79 { "digest list", -6 },
81 { "volume number", -6 },
83 { "issue number", -5 },
89 #define NOCOMPONENT 0x000001 /* don't show component name */
90 #define UPPERCASE 0x000002 /* display in all upper case */
91 #define CENTER 0x000004 /* center line */
92 #define CLEARTEXT 0x000008 /* cleartext */
93 #define EXTRA 0x000010 /* an "extra" component */
94 #define HDROUTPUT 0x000020 /* already output */
95 #define CLEARSCR 0x000040 /* clear screen */
96 #define LEFTADJUST 0x000080 /* left justify multiple lines */
97 #define COMPRESS 0x000100 /* compress text */
98 #define ADDRFMT 0x000200 /* contains addresses */
99 #define BELL 0x000400 /* sound bell at EOP */
100 #define DATEFMT 0x000800 /* contains dates */
101 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
102 #define INIT 0x002000 /* initialize component */
103 #define SPLIT 0x004000 /* split headers (don't concatenate) */
104 #define NONEWLINE 0x008000 /* don't write trailing newline */
105 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017SPLIT\020NONEWLINE"
106 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
109 char *c_name; /* component name */
110 char *c_text; /* component text */
111 char *c_ovtxt; /* text overflow indicator */
112 char *c_nfs; /* iff FORMAT */
113 struct format *c_fmt; /* .. */
114 int c_offset; /* left margin indentation */
115 int c_ovoff; /* overflow indentation */
116 int c_width; /* width of field */
117 int c_cwidth; /* width of component */
118 int c_length; /* length in lines */
120 struct mcomp *c_next;
123 static struct mcomp *msghd = NULL;
124 static struct mcomp *msgtl = NULL;
125 static struct mcomp *fmthd = NULL;
126 static struct mcomp *fmttl = NULL;
128 static struct mcomp global = {
129 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
132 static struct mcomp holder = {
133 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
141 static struct pair pairs[] = {
144 { "Sender", ADDRFMT },
145 { "Reply-To", ADDRFMT },
149 { "Resent-Date", DATEFMT },
150 { "Resent-From", ADDRFMT },
151 { "Resent-Sender", ADDRFMT },
152 { "Resent-Reply-To", ADDRFMT },
153 { "Resent-To", ADDRFMT },
154 { "Resent-Cc", ADDRFMT },
155 { "Resent-Bcc", ADDRFMT },
165 static struct triple triples[] = {
166 { "nocomponent", NOCOMPONENT, 0 },
167 { "uppercase", UPPERCASE, 0 },
168 { "nouppercase", 0, UPPERCASE },
169 { "center", CENTER, 0 },
170 { "nocenter", 0, CENTER },
171 { "clearscreen", CLEARSCR, 0 },
172 { "noclearscreen", 0, CLEARSCR },
173 { "noclear", 0, CLEARSCR },
174 { "leftadjust", LEFTADJUST, 0 },
175 { "noleftadjust", 0, LEFTADJUST },
176 { "compress", COMPRESS, 0 },
177 { "nocompress", 0, COMPRESS },
178 { "split", SPLIT, 0 },
179 { "nosplit", 0, SPLIT },
180 { "addrfield", ADDRFMT, DATEFMT },
182 { "nobell", 0, BELL },
183 { "datefield", DATEFMT, ADDRFMT },
184 { "newline", 0, NONEWLINE },
185 { "nonewline", NONEWLINE, 0 },
190 static int bellflg = 0;
191 static int clearflg = 0;
192 static int dashstuff = 0;
193 static int dobody = 1;
194 static int forwflg = 0;
195 static int forwall = 0;
197 static int sleepsw = NOTOK;
199 static char *digest = NULL;
200 static int volume = 0;
201 static int issue = 0;
203 static int exitstat = 0;
204 static int mhldebug = 0;
209 static int ontty = NOTTY;
222 static unsigned char *onelp;
226 static int num_ignores = 0;
227 static char *ignores[MAXARGS];
230 static jmp_buf mhlenv;
232 static char delim3[] = /* from forw.c */
233 "\n----------------------------------------------------------------------\n\n";
234 static char delim4[] = "\n------------------------------\n\n";
236 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
240 ** Redefine a couple of functions.
241 ** These are undefined later in the code.
243 #define adios mhladios
249 static void mhl_format(char *, int, int);
250 static int evalvar(struct mcomp *);
251 static int ptoi(char *, int *);
252 static int ptos(char *, char **);
253 static char *parse(void);
254 static void process(char *, char *, int, int);
255 static void mhlfile(FILE *, char *, int, int);
256 static int mcomp_flags(char *);
257 static char *mcomp_add(long, char *, char *);
258 static void mcomp_format(struct mcomp *, struct mcomp *);
259 static struct mcomp *add_queue(struct mcomp **, struct mcomp **,
260 char *, char *, int);
261 static void free_queue(struct mcomp **, struct mcomp **);
262 static void putcomp(struct mcomp *, struct mcomp *, int);
263 static char *oneline(char *, long);
264 static void putstr(char *);
265 static void putch(char);
266 static RETSIGTYPE intrser(int);
267 static RETSIGTYPE pipeser(int);
268 static RETSIGTYPE quitser(int);
269 static void mhladios(char *, char *, ...);
270 static void mhldone(int);
271 static void m_popen(char *);
273 int mhl(int, char **);
274 int mhlsbr(int, char **, FILE *(*)());
277 void clear_screen(void); /* from termsbr.c */
278 int SOprintf(char *, ...); /* from termsbr.c */
279 int sc_width(void); /* from termsbr.c */
280 int sc_length(void); /* from termsbr.c */
281 int sc_hardcopy(void); /* from termsbr.c */
285 mhl(int argc, char **argv)
287 int length = 0, nomore = 0;
288 int i, width = 0, vecp = 0;
289 char *cp, *folder = NULL, *form = NULL;
290 char buf[BUFSIZ], *files[MAXARGS];
291 char **argp, **arguments;
293 invo_name = mhbasename(argv[0]);
295 /* read user profile/context */
298 arguments = getarguments(invo_name, argc, argv, 1);
301 if ((cp = getenv("MHLDEBUG")) && *cp)
304 while ((cp = *argp++)) {
306 switch (smatch(++cp, mhlswitches)) {
308 ambigsw(cp, mhlswitches);
311 adios(NULL, "-%s unknown\n", cp);
314 snprintf(buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
315 print_help(buf, mhlswitches, 1);
318 print_version(invo_name);
336 if (!(folder = *argp++) ||
338 adios(NULL, "missing argument to %s", argp[-2]);
341 if (!(form = *argp++) || *form == '-')
342 adios(NULL, "missing argument to %s", argp[-2]);
346 if (!(cp = *argp++) || *cp == '-')
347 adios(NULL, "missing argument to %s", argp[-2]);
348 sleepsw = atoi(cp); /* ZERO ok! */
352 if (!(moreproc = *argp++) || *moreproc == '-')
353 adios(NULL, "missing argument to %s", argp[-2]);
360 if (!(cp = *argp++) || *cp == '-')
361 adios(NULL, "missing argument to %s", argp[-2]);
362 if ((length = atoi(cp)) < 1)
363 adios(NULL, "bad argument %s %s", argp[-2], cp);
366 if (!(cp = *argp++) || *cp == '-')
367 adios(NULL, "missing argument to %s", argp[-2]);
368 if ((width = atoi(cp)) < 1)
369 adios(NULL, "bad argument %s %s", argp[-2], cp);
373 if (!(digest = *argp++) ||
375 adios(NULL, "missing argument to %s", argp[-2]);
378 if (!(cp = *argp++) || *cp == '-')
379 adios(NULL, "missing argument to %s", argp[-2]);
380 if ((issue = atoi(cp)) < 1)
381 adios(NULL, "bad argument %s %s", argp[-2], cp);
384 if (!(cp = *argp++) || *cp == '-')
385 adios(NULL, "missing argument to %s", argp[-2]);
386 if ((volume = atoi(cp)) < 1)
387 adios(NULL, "bad argument %s %s", argp[-2], cp);
391 forwall++; /* fall */
394 clearflg = -1;/* XXX */
398 dashstuff = 1; /* trinary logic */
401 dashstuff = -1; /* trinary logic */
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,
543 ** and copy it to the end of the current
546 if (!mh_strcasecmp(name, "ignores")) {
547 char **tmparray, **p;
550 /* split the fields */
551 tmparray = brkstring(getcpy(++parptr),
554 /* count number of fields split */
560 ** copy pointers to split fields
563 ip = copyip(tmparray, ip,
564 MAXARGS - num_ignores);
570 if (evalvar(&global))
571 adios(NULL, "format file syntax error: %s", bp);
578 c1 = add_queue(&fmthd, &fmttl, name, NULL,
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 if (global.c_flags & DATEFMT)
588 c1->c_nfs = getcpy(global.c_nfs);
591 if (c1->c_flags & ADDRFMT) {
592 if (global.c_flags & ADDRFMT)
593 c1->c_nfs = getcpy(global.c_nfs);
599 adios(NULL, "format file syntax error: %s",
606 for (c1 = fmthd; c1; c1 = c1->c_next) {
607 fprintf(stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n", c1->c_name, c1->c_text, c1->c_ovtxt);
608 fprintf(stderr, "\tnfs=0x%x fmt=0x%x\n", (unsigned int)(unsigned long) c1->c_nfs, (unsigned int)(unsigned long) c1->c_fmt);
609 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);
610 fprintf (stderr, "\tflags=%s\n", snprintb(buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
616 global.c_flags |= CLEARSCR;
619 global.c_flags &= ~CLEARSCR;
622 switch (bellflg) { /* command line may override format file */
624 global.c_flags |= BELL;
627 global.c_flags &= ~BELL;
632 global.c_length = length;
634 global.c_width = width;
635 if (global.c_length < 5)
636 global.c_length = 10000;
637 if (global.c_width < 5)
638 global.c_width = 10000;
643 evalvar(struct mcomp *c1)
645 char *cp, name[NAMESZ];
650 strncpy(name, parse(), sizeof(name));
652 if (!mh_strcasecmp(name, "component")) {
653 if (ptos(name, &c1->c_text))
655 c1->c_flags &= ~NOCOMPONENT;
659 if (!mh_strcasecmp(name, "overflowtext"))
660 return ptos(name, &c1->c_ovtxt);
662 if (!mh_strcasecmp(name, "formatfield")) {
667 nfs = new_fs(NULL, NULL, cp);
668 c1->c_nfs = getcpy(nfs);
669 c1->c_flags |= FORMAT;
673 if (!mh_strcasecmp(name, "decode")) {
676 nfs = new_fs(NULL, NULL, "%(decode{text})");
677 c1->c_nfs = getcpy(nfs);
678 c1->c_flags |= FORMAT;
682 if (!mh_strcasecmp(name, "offset"))
683 return ptoi(name, &c1->c_offset);
684 if (!mh_strcasecmp(name, "overflowoffset"))
685 return ptoi(name, &c1->c_ovoff);
686 if (!mh_strcasecmp(name, "width"))
687 return ptoi(name, &c1->c_width);
688 if (!mh_strcasecmp(name, "compwidth"))
689 return ptoi(name, &c1->c_cwidth);
690 if (!mh_strcasecmp(name, "length"))
691 return ptoi(name, &c1->c_length);
692 if (!mh_strcasecmp(name, "nodashstuffing"))
693 return (dashstuff = -1);
695 for (ap = triples; ap->t_name; ap++)
696 if (!mh_strcasecmp(ap->t_name, name)) {
697 c1->c_flags |= ap->t_on;
698 c1->c_flags &= ~ap->t_off;
707 ptoi(char *name, int *i)
711 if (*parptr++ != '=' || !*(cp = parse())) {
712 advise(NULL, "missing argument to variable %s", name);
722 ptos(char *name, char **s)
726 if (*parptr++ != '=') {
727 advise(NULL, "missing argument to variable %s", name);
731 if (*parptr != '"') {
732 for (cp = parptr; *parptr && *parptr != ':' && *parptr != ',';
736 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
737 if (*parptr == QUOTE)
744 if ((*parptr = c) == '"')
755 static char result[NAMESZ];
757 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
776 process(char *folder, char *fname, int ofilen, int ofilec)
782 switch (setjmp(env)) {
785 fp = mhl_action ? (*mhl_action) (fname) :
788 advise(fname, "unable to open");
796 cp = folder ? concat(folder, ":", fname, NULL) :
799 SIGNAL(SIGINT, intrser);
800 mhlfile(fp, cp, ofilen, ofilec); /* FALL THROUGH! */
804 SIGNAL(SIGINT, SIG_IGN);
805 if (mhl_action == NULL && fp != stdin)
810 holder.c_text = NULL;
812 free_queue(&msghd, &msgtl);
813 for (c1 = fmthd; c1; c1 = c1->c_next)
814 c1->c_flags &= ~HDROUTPUT;
821 mhlfile(FILE *fp, char *mname, int ofilen, int ofilec)
824 struct mcomp *c1, *c2, *c3;
825 char **ip, name[NAMESZ], buf[BUFSIZ];
829 printf("%s", ofilen == 1 ? delim3 : delim4);
833 printf(" Forwarded Message%s",
834 ofilec > 1 ? "s" : "");
836 printf(" Message %d", ofilen);
844 if ((global.c_flags & CLEARSCR))
849 printf(">>> %s\n\n", mname);
854 strncpy(buf, "\n", sizeof(buf));
856 if (SOprintf("Press <return> to list \"%s\"...", mname)) {
859 printf("Press <return> to list \"%s\"...", mname);
863 read(fileno(stdout), buf, sizeof(buf));
865 if (strchr(buf, '\n')) {
866 if ((global.c_flags & CLEARSCR))
880 printf(">>> %s\n\n", mname);
886 for (state = FLD;;) {
887 switch (state = m_getfld(state, name, buf, sizeof(buf), fp)) {
890 for (ip = ignores; *ip; ip++)
891 if (!mh_strcasecmp(name, *ip)) {
892 while (state == FLDPLUS)
893 state = m_getfld(state, name, buf, sizeof(buf), fp);
899 for (c2 = fmthd; c2; c2 = c2->c_next)
900 if (!mh_strcasecmp(c2->c_name, name))
903 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
904 for (c1 = msghd; c1; c1 = c1->c_next)
905 if (!mh_strcasecmp(c1->c_name, c3->c_name)) {
906 c1->c_text = mcomp_add (c1->c_flags, buf, c1->c_text);
910 c1 = add_queue(&msghd, &msgtl, name, buf, 0);
911 while (state == FLDPLUS) {
912 state = m_getfld(state, name, buf, sizeof(buf), fp);
913 c1->c_text = add(buf, c1->c_text);
916 c1->c_flags |= EXTRA;
922 for (c1 = fmthd; c1; c1 = c1->c_next) {
923 if (c1->c_flags & CLEARTEXT) {
924 putcomp(c1, c1, ONECOMP);
927 if (!mh_strcasecmp(c1->c_name, "messagename")) {
928 holder.c_text = concat("(Message ", mname, ")\n",
930 putcomp(c1, &holder, ONECOMP);
932 holder.c_text = NULL;
935 if (!mh_strcasecmp(c1->c_name, "extras")) {
936 for (c2 = msghd; c2; c2 = c2->c_next)
937 if (c2->c_flags & EXTRA)
938 putcomp(c1, c2, TWOCOMP);
941 if (dobody && !mh_strcasecmp(c1->c_name, "body")) {
942 holder.c_text = mh_xmalloc(sizeof(buf));
943 strncpy(holder.c_text, buf, sizeof(buf));
944 while (state == BODY) {
945 putcomp(c1, &holder, BODYCOMP);
946 state = m_getfld(state, name, holder.c_text, sizeof(buf), fp);
949 holder.c_text = NULL;
952 for (c2 = msghd; c2; c2 = c2->c_next)
953 if (!mh_strcasecmp(c2->c_name, c1->c_name)) {
954 putcomp(c1, c2, ONECOMP);
955 if (!(c1->c_flags & SPLIT))
963 advise(NULL, "format error in message %s",
969 adios(NULL, "getfld() returned %d", state);
976 mcomp_flags(char *name)
980 for (ap = pairs; ap->p_name; ap++)
981 if (!mh_strcasecmp(ap->p_name, name))
982 return (ap->p_flags);
989 mcomp_add(long flags, char *s1, char *s2)
993 if (!(flags & ADDRFMT))
996 if (s2 && *(dp = s2 + strlen(s2) - 1) == '\n')
999 return add(s1, add(",\n", s2));
1006 struct pqpair *pq_next;
1011 mcomp_format(struct mcomp *c1, struct mcomp *c2)
1015 char buffer[BUFSIZ], error[BUFSIZ];
1017 struct pqpair *p, *q;
1019 struct mailname *mp;
1026 dat[3] = sizeof(buffer) - 1;
1028 fmt_compile(c1->c_nfs, &c1->c_fmt);
1030 if (!(c1->c_flags & ADDRFMT)) {
1031 FINDCOMP(cptr, "text");
1034 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1038 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1039 /* Don't need to append a newline, dctime() already did */
1040 c2->c_text = getcpy(buffer);
1046 (q = &pq)->pq_next = NULL;
1047 while ((cp = getname(ap))) {
1048 if ((p = (struct pqpair *)
1049 calloc((size_t) 1, sizeof(*p))) == NULL)
1050 adios(NULL, "unable to allocate pqpair memory");
1052 if ((mp = getm(cp, NULL, 0, AD_NAME, error)) == NULL) {
1053 p->pq_text = getcpy(cp);
1054 p->pq_error = getcpy(error);
1056 p->pq_text = getcpy(mp->m_text);
1059 q = (q->pq_next = p);
1062 for (p = pq.pq_next; p; p = q) {
1063 FINDCOMP(cptr, "text");
1065 cptr->c_text = p->pq_text;
1066 FINDCOMP(cptr, "error");
1068 cptr->c_text = p->pq_error;
1070 fmt_scan(c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1073 c2->c_text = add(",\n", c2->c_text);
1074 if (*(cp = buffer + strlen(buffer) - 1) == '\n')
1076 c2->c_text = add(buffer, c2->c_text);
1086 c2->c_text = add("\n", c2->c_text);
1091 static struct mcomp *
1092 add_queue(struct mcomp **head, struct mcomp **tail, char *name,
1093 char *text, int flags)
1097 if ((c1 = (struct mcomp *) calloc((size_t) 1, sizeof(*c1))) == NULL)
1098 adios(NULL, "unable to allocate comp memory");
1100 c1->c_flags = flags & ~INIT;
1101 if ((c1->c_name = name ? getcpy(name) : NULL))
1102 c1->c_flags |= mcomp_flags(c1->c_name);
1103 c1->c_text = text ? getcpy(text) : NULL;
1106 c1->c_ovtxt = getcpy(global.c_ovtxt);
1107 c1->c_offset = global.c_offset;
1108 c1->c_ovoff = global. c_ovoff;
1109 c1->c_width = c1->c_length = 0;
1110 c1->c_cwidth = global.c_cwidth;
1111 c1->c_flags |= global.c_flags & GFLAGS;
1116 (*tail)->c_next = c1;
1124 free_queue(struct mcomp **head, struct mcomp **tail)
1126 struct mcomp *c1, *c2;
1128 for (c1 = *head; c1; c1 = c2) {
1139 free((char *) c1->c_fmt);
1143 *head = *tail = NULL;
1148 putcomp(struct mcomp *c1, struct mcomp *c2, int flag)
1155 llim = c1->c_length ? c1->c_length : -1;
1156 wid = c1->c_width ? c1->c_width : global.c_width;
1157 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1159 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1161 if (wid < ovoff + strlen(ovtxt) + 5)
1162 adios(NULL, "component: %s width(%d) too small for overflow(%d)", c1->c_name, wid, ovoff + strlen(ovtxt) + 5);
1165 if (c1->c_flags & CLEARTEXT) {
1171 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1172 mcomp_format(c1, c2);
1174 if (c1->c_flags & CENTER) {
1175 count = (c1->c_width ? c1->c_width : global.c_width)
1176 - c1->c_offset - strlen(c2->c_text);
1177 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1178 count -= strlen(c1->c_text ? c1->c_text : c1->c_name)
1180 lm = c1->c_offset + (count / 2);
1186 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1187 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1188 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1191 putstr(c1->c_text ? c1->c_text : c1->c_name);
1192 if (flag != BODYCOMP) {
1194 if (!(c1->c_flags & SPLIT))
1195 c1->c_flags |= HDROUTPUT;
1198 if ((count = c1->c_cwidth -
1199 strlen(c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1203 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1206 if (flag == TWOCOMP && !(c2->c_flags & HDROUTPUT)
1207 && !(c2->c_flags & NOCOMPONENT)) {
1208 if (c1->c_flags & UPPERCASE)
1209 for (cp = c2->c_name; *cp; cp++)
1214 if (!(c1->c_flags & SPLIT))
1215 c2->c_flags |= HDROUTPUT;
1218 if ((count = c1->c_cwidth - strlen(c2->c_name) - 2) > 0)
1222 if (c1->c_flags & UPPERCASE)
1223 for (cp = c2->c_text; *cp; cp++)
1229 if (flag == TWOCOMP)
1230 count = (c1->c_cwidth >= 0) ?
1231 c1->c_cwidth : strlen(c2->c_name) + 2;
1233 count = (c1->c_cwidth >= 0) ?
1234 c1->c_cwidth : strlen(c1->c_text ?
1235 c1->c_text : c1->c_name) + 2;
1237 count += c1->c_offset;
1239 if ((cp = oneline(c2->c_text, c1->c_flags)))
1243 while ((cp = oneline(c2->c_text, c1->c_flags))) {
1245 if (flag == BODYCOMP && !(c1->c_flags & NOCOMPONENT))
1246 putstr(c1->c_text ? c1->c_text : c1->c_name);
1252 if (flag == BODYCOMP && term == '\n')
1253 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1258 oneline(char *stuff, long flags)
1266 return (onelp = NULL);
1270 if (flags & COMPRESS) {
1271 for (spc = 1, cp = ret; *onelp; onelp++)
1272 if (isspace(*onelp)) {
1273 if (*onelp == '\n' &&
1275 (flags & ADDRFMT))) {
1290 while (*onelp && *onelp != '\n')
1292 if (*onelp == '\n') {
1296 if (flags & LEFTADJUST)
1297 while (*ret == ' ' || *ret == '\t')
1300 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1308 putstr(char *string)
1310 if (!column && lm > 0) {
1340 if (ontty != ISTTY || row != global.c_length)
1342 if (global.c_flags & BELL)
1346 read(fileno(stdout), buf, sizeof(buf));
1347 if (strchr(buf, '\n')) {
1348 if (global.c_flags & CLEARSCR)
1353 row = global.c_length / 3;
1372 ** If we are forwarding this message, and the first
1373 ** column contains a dash, then add a dash and a space.
1375 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1384 if (column >= wid) {
1388 putstr(ovtxt ? ovtxt : "");
1400 #ifndef RELIABLE_SIGNALS
1401 SIGNAL(SIGINT, intrser);
1413 #ifndef RELIABLE_SIGNALS
1414 SIGNAL(SIGPIPE, pipeser);
1424 #ifndef RELIABLE_SIGNALS
1425 SIGNAL(SIGQUIT, quitser);
1435 mhlsbr(int argc, char **argv, FILE *(*action)())
1437 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1441 switch (setjmp(mhlenv)) {
1444 sleepsw = 0; /* XXX */
1445 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1448 mhl_action = action;
1451 ** If signal is at default action, then start ignoring
1452 ** it, else let it set to its current action.
1454 if ((istat = SIGNAL(SIGINT, SIG_IGN)) != SIG_DFL)
1455 SIGNAL(SIGINT, istat);
1456 if ((qstat = SIGNAL(SIGQUIT, SIG_IGN)) != SIG_DFL)
1457 SIGNAL(SIGQUIT, qstat);
1458 pstat = SIGNAL(SIGPIPE, pipeser);
1459 mhl(argc, argv); /* FALL THROUGH! */
1462 SIGNAL(SIGINT, istat);
1463 SIGNAL(SIGQUIT, qstat);
1464 SIGNAL(SIGPIPE, SIG_IGN); /*
1465 ** should probably change to block
1470 SIGNAL(SIGPIPE, pstat);
1472 if (holder.c_text) {
1473 free(holder.c_text);
1474 holder.c_text = NULL;
1476 free_queue(&msghd, &msgtl);
1477 for (c1 = fmthd; c1; c1 = c1->c_next)
1478 c1->c_flags &= ~HDROUTPUT;
1487 mhladios(char *what, char *fmt, ...)
1492 advertise(what, NULL, fmt, ap);
1503 longjmp(mhlenv, DONE);
1509 static int m_pid = NOTOK;
1510 static int sd = NOTOK;
1517 if (mhl_action && (sd = dup(fileno(stdout))) == NOTOK)
1518 adios("standard output", "unable to dup()");
1520 if (pipe(pd) == NOTOK)
1521 adios("pipe", "unable to");
1523 switch (m_pid = vfork()) {
1525 adios("fork", "unable to");
1528 SIGNAL(SIGINT, SIG_DFL);
1529 SIGNAL(SIGQUIT, SIG_DFL);
1532 if (pd[0] != fileno(stdin)) {
1533 dup2(pd[0], fileno(stdin));
1536 execlp(name, mhbasename(name), NULL);
1537 fprintf(stderr, "unable to exec ");
1543 if (pd[1] != fileno(stdout)) {
1544 dup2(pd[1], fileno(stdout));
1559 if (dup2(sd, fileno(stdout)) == NOTOK)
1560 adios("standard output", "unable to dup2()");