]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
3 * mhlsbr.c -- main routines for nmh message lister
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
20 #include <sys/types.h>
24 * for a component containing addresses, ADDRFMT, if COMPRESS is also
25 * set, then addresses get split wrong (not at the spaces between commas).
26 * To fix this correctly, putstr() should know about "atomic" strings that
27 * must NOT be broken across lines. That's too difficult for right now
28 * (it turns out that there are a number of degernate cases), so in
29 * oneline(), instead of
31 * (*onelp == '\n' && !onelp[1])
33 * being a terminating condition,
35 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
37 * is used instead. This cuts the line prematurely, and gives us a much
38 * better chance of getting things right.
47 #define MHL_SWITCHES \
48 X("bell", 0, BELLSW) \
49 X("nobell", 0, NBELLSW) \
50 X("clear", 0, CLRSW) \
51 X("noclear", 0, NCLRSW) \
52 X("folder +folder", 0, FOLDSW) \
53 X("form formfile", 0, FORMSW) \
54 X("moreproc program", 0, PROGSW) \
55 X("nomoreproc", 0, NPROGSW) \
56 X("length lines", 0, LENSW) \
57 X("width columns", 0, WIDTHSW) \
58 X("sleep seconds", 0, SLEEPSW) \
59 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
60 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("forward", -7, FORW1SW) /* interface from forw */ \
64 X("forwall", -7, FORW2SW) /* interface from forw */ \
65 X("digest list", -6, DGSTSW) \
66 X("volume number", -6, VOLUMSW) \
67 X("issue number", -5, ISSUESW) \
68 X("nobody", -6, NBODYSW) \
69 X("fmtproc program", 0, FMTPROCSW) \
70 X("nofmtproc", 0, NFMTPROCSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(MHL
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
80 #define NOCOMPONENT 0x000001 /* don't show component name */
81 #define UPPERCASE 0x000002 /* display in all upper case */
82 #define CENTER 0x000004 /* center line */
83 #define CLEARTEXT 0x000008 /* cleartext */
84 #define EXTRA 0x000010 /* an "extra" component */
85 #define HDROUTPUT 0x000020 /* already output */
86 #define CLEARSCR 0x000040 /* clear screen */
87 #define LEFTADJUST 0x000080 /* left justify multiple lines */
88 #define COMPRESS 0x000100 /* compress text */
89 #define ADDRFMT 0x000200 /* contains addresses */
90 #define BELL 0x000400 /* sound bell at EOP */
91 #define DATEFMT 0x000800 /* contains dates */
92 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
93 #define INIT 0x002000 /* initialize component */
94 #define SPLIT 0x010000 /* split headers (don't concatenate) */
95 #define NONEWLINE 0x020000 /* don't write trailing newline */
96 #define NOWRAP 0x040000 /* Don't wrap lines ever */
97 #define FMTFILTER 0x080000 /* Filter through format filter */
98 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
99 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
102 * A format string to be used as a command-line argument to the body
107 struct format
*a_fmt
;
109 struct arglist
*a_next
;
113 * Linked list of command line arguments for the body format filter. This
114 * USED to be in "struct mcomp", but the format API got cleaned up and even
115 * though it reduced the code we had to do, it make things more complicated
116 * for us. Specifically:
118 * - The interface to the hash table has been cleaned up, which means the
119 * rooting around in the hash table is no longer necessary (yay!). But
120 * this ALSO means that we have to make sure that we call our format
121 * compilation routines before we process the message, because the
122 * components need to be visible in the hash table so we can save them for
123 * later. So we moved them out of "mcomp" and now compile them right before
124 * header processing starts.
125 * - We also use format strings to handle other components in the mhl
126 * configuration (using "formatfield" and "decode"), but here life
127 * gets complicated: they aren't dealt with in the normal way. Instead
128 * of referring to a component like {from}, each component is processed
129 * using the special {text} component. But these format strings need to be
130 * compiled BEFORE we compile the format arguments; in the previous
131 * implementation they were compiled and scanned as the headers were
132 * read, and that would reset the hash table that we need to populate
133 * the components used by the body format filter. So we are compiling
134 * the formatfield component strings ahead of time and then scanning them
137 * Okay, fine ... this was broken before. But you know what? Fixing this
138 * the right way will make things easier down the road.
140 * One side-effect to this change: format strings are now compiled only once
141 * for components specified with "formatfield", but they are compiled for
142 * every message for format arguments.
145 static struct arglist
*arglist_head
;
146 static struct arglist
*arglist_tail
;
147 static int filter_nargs
= 0;
150 * Flags/options for each component
154 char *c_name
; /* component name */
155 char *c_text
; /* component text */
156 char *c_ovtxt
; /* text overflow indicator */
157 char *c_nfs
; /* iff FORMAT */
158 struct format
*c_fmt
; /* .. */
159 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
160 struct comp
*c_c_error
; /* Ref to {error} */
161 int c_offset
; /* left margin indentation */
162 int c_ovoff
; /* overflow indentation */
163 int c_width
; /* width of field */
164 int c_cwidth
; /* width of component */
165 int c_length
; /* length in lines */
167 struct mcomp
*c_next
;
170 static struct mcomp
*msghd
= NULL
;
171 static struct mcomp
*msgtl
= NULL
;
172 static struct mcomp
*fmthd
= NULL
;
173 static struct mcomp
*fmttl
= NULL
;
175 static struct mcomp global
= {
176 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
179 static struct mcomp holder
= {
180 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
188 static struct pair pairs
[] = {
191 { "Sender", ADDRFMT
},
192 { "Reply-To", ADDRFMT
},
196 { "Resent-Date", DATEFMT
},
197 { "Resent-From", ADDRFMT
},
198 { "Resent-Sender", ADDRFMT
},
199 { "Resent-Reply-To", ADDRFMT
},
200 { "Resent-To", ADDRFMT
},
201 { "Resent-cc", ADDRFMT
},
202 { "Resent-Bcc", ADDRFMT
},
212 static struct triple triples
[] = {
213 { "nocomponent", NOCOMPONENT
, 0 },
214 { "uppercase", UPPERCASE
, 0 },
215 { "nouppercase", 0, UPPERCASE
},
216 { "center", CENTER
, 0 },
217 { "nocenter", 0, CENTER
},
218 { "clearscreen", CLEARSCR
, 0 },
219 { "noclearscreen", 0, CLEARSCR
},
220 { "noclear", 0, CLEARSCR
},
221 { "leftadjust", LEFTADJUST
, 0 },
222 { "noleftadjust", 0, LEFTADJUST
},
223 { "compress", COMPRESS
, 0 },
224 { "nocompress", 0, COMPRESS
},
225 { "split", SPLIT
, 0 },
226 { "nosplit", 0, SPLIT
},
227 { "addrfield", ADDRFMT
, DATEFMT
},
229 { "nobell", 0, BELL
},
230 { "datefield", DATEFMT
, ADDRFMT
},
231 { "newline", 0, NONEWLINE
},
232 { "nonewline", NONEWLINE
, 0 },
233 { "wrap", 0, NOWRAP
},
234 { "nowrap", NOWRAP
, 0 },
235 { "format", FMTFILTER
, 0 },
236 { "noformat", 0, FMTFILTER
},
240 static char *addrcomps
[] = {
257 static int bellflg
= 0;
258 static int clearflg
= 0;
259 static int dashstuff
= 0;
260 static int dobody
= 1;
261 static int forwflg
= 0;
262 static int forwall
= 0;
264 static int sleepsw
= NOTOK
;
266 static char *digest
= NULL
;
267 static int volume
= 0;
268 static int issue
= 0;
270 static int exitstat
= 0;
271 static int mhldebug
= 0;
273 static int filesize
= 0;
278 static int ontty
= NOTTY
;
281 static unsigned int column
;
287 static unsigned int wid
;
291 static unsigned char *onelp
;
295 static int num_ignores
= 0;
296 static char *ignores
[MAXARGS
];
299 static jmp_buf mhlenv
;
301 static char delim3
[] = /* from forw.c */
302 "\n----------------------------------------------------------------------\n\n";
303 static char delim4
[] = "\n------------------------------\n\n";
305 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
308 * Redefine a couple of functions.
309 * These are undefined later in the code.
311 #define adios mhladios
317 static void mhl_format (char *, int, int);
318 static int evalvar (struct mcomp
*);
319 static int ptoi (char *, int *);
320 static int ptos (char *, char **);
321 static char *parse (void);
322 static void process (char *, char *, int, int);
323 static void mhlfile (FILE *, char *, int, int);
324 static int mcomp_flags (char *);
325 static char *mcomp_add (long, char *, char *);
326 static void mcomp_format (struct mcomp
*, struct mcomp
*);
327 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
328 static void free_queue (struct mcomp
**, struct mcomp
**);
329 static void putcomp (struct mcomp
*, struct mcomp
*, int);
330 static char *oneline (char *, long);
331 static void putstr (char *, long);
332 static void putch (char, long);
333 static void intrser (int);
334 static void pipeser (int);
335 static void quitser (int);
336 static void mhladios (char *, char *, ...);
337 static void mhldone (int);
338 static void m_popen (char *);
339 static void filterbody (struct mcomp
*, char *, int, int, FILE *);
340 static void compile_formatfield(struct mcomp
*);
341 static void compile_filterargs (void);
345 mhl (int argc
, char **argv
)
347 int length
= 0, nomore
= 0;
348 unsigned int i
, vecp
= 0;
350 char *cp
, *folder
= NULL
, *form
= NULL
;
351 char buf
[BUFSIZ
], *files
[MAXARGS
];
352 char **argp
, **arguments
;
354 invo_name
= r1bindex (argv
[0], '/');
356 /* read user profile/context */
359 arguments
= getarguments (invo_name
, argc
, argv
, 1);
362 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
365 while ((cp
= *argp
++)) {
367 switch (smatch (++cp
, mhlswitches
)) {
369 ambigsw (cp
, mhlswitches
);
372 adios (NULL
, "-%s unknown\n", cp
);
375 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
376 print_help (buf
, mhlswitches
, 1);
379 print_version(invo_name
);
397 if (!(folder
= *argp
++) || *folder
== '-')
398 adios (NULL
, "missing argument to %s", argp
[-2]);
401 if (!(form
= *argp
++) || *form
== '-')
402 adios (NULL
, "missing argument to %s", argp
[-2]);
406 if (!(cp
= *argp
++) || *cp
== '-')
407 adios (NULL
, "missing argument to %s", argp
[-2]);
408 sleepsw
= atoi (cp
);/* ZERO ok! */
412 if (!(moreproc
= *argp
++) || *moreproc
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
420 if (!(formatproc
= *argp
++) || *formatproc
== '-')
421 adios (NULL
, "missing argument to %s", argp
[-2]);
428 if (!(cp
= *argp
++) || *cp
== '-')
429 adios (NULL
, "missing argument to %s", argp
[-2]);
430 if ((length
= atoi (cp
)) < 1)
431 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
434 if (!(cp
= *argp
++) || *cp
== '-')
435 adios (NULL
, "missing argument to %s", argp
[-2]);
436 if ((width
= atoi (cp
)) < 1)
437 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
441 if (!(digest
= *argp
++) || *digest
== '-')
442 adios (NULL
, "missing argument to %s", argp
[-2]);
445 if (!(cp
= *argp
++) || *cp
== '-')
446 adios (NULL
, "missing argument to %s", argp
[-2]);
447 if ((issue
= atoi (cp
)) < 1)
448 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
451 if (!(cp
= *argp
++) || *cp
== '-')
452 adios (NULL
, "missing argument to %s", argp
[-2]);
453 if ((volume
= atoi (cp
)) < 1)
454 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
458 forwall
++; /* fall */
461 clearflg
= -1;/* XXX */
465 dashstuff
= 1; /* trinary logic */
468 dashstuff
= -1; /* trinary logic */
480 folder
= getenv ("mhfolder");
482 if (isatty (fileno (stdout
))) {
483 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
485 SIGNAL (SIGINT
, SIG_IGN
);
486 SIGNAL2 (SIGQUIT
, quitser
);
488 SIGNAL2 (SIGPIPE
, pipeser
);
492 SIGNAL (SIGINT
, SIG_IGN
);
493 SIGNAL2 (SIGQUIT
, quitser
);
500 mhl_format (form
? form
: mhlformat
, length
, width
);
503 process (folder
, NULL
, 1, vecp
= 1);
505 for (i
= 0; i
< vecp
; i
++)
506 process (folder
, files
[i
], i
+ 1, vecp
);
511 printf ("%s", delim4
);
513 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
515 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
516 digest
, volume
, issue
);
519 for (cp
= buf
+ i
; i
> 1; i
--)
526 printf ("\n------- End of Forwarded Message%s\n",
527 vecp
> 1 ? "s" : "");
532 adios("output", "error writing");
535 if (clearflg
> 0 && ontty
== NOTTY
)
546 mhl_format (char *file
, int length
, int width
)
550 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
554 static dev_t dev
= 0;
555 static ino_t ino
= 0;
556 static time_t mtime
= 0;
559 if (stat (etcpath (file
), &st
) != NOTOK
560 && mtime
== st
.st_mtime
565 free_queue (&fmthd
, &fmttl
);
568 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
569 adios (file
, "unable to open format file");
571 if (fstat (fileno (fp
), &st
) != NOTOK
) {
577 global
.c_ovtxt
= global
.c_nfs
= NULL
;
581 if ((i
= sc_width ()) > 5)
583 global
.c_cwidth
= -1;
584 if ((i
= sc_length ()) > 5)
585 global
.c_length
= i
- 1;
586 global
.c_flags
= BELL
; /* BELL is default */
587 *(ip
= ignores
) = NULL
;
590 while (vfgets (fp
, &ap
) == OK
) {
595 if ((cp
= strchr(bp
, '\n')))
599 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
604 strncpy (name
, parse(), sizeof(name
));
610 * Split this list of fields to ignore, and copy
611 * it to the end of the current "ignores" list.
613 if (!mh_strcasecmp (name
, "ignores")) {
614 char **tmparray
, **p
;
617 /* split the fields */
618 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
620 /* count number of fields split */
625 /* copy pointers to split fields to ignores array */
626 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
632 if (evalvar (&global
))
633 adios (NULL
, "format file syntax error: %s", bp
);
640 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
641 while (*parptr
== ':' || *parptr
== ',') {
644 adios (NULL
, "format file syntax error: %s", bp
);
646 if (!c1
->c_nfs
&& global
.c_nfs
) {
647 if (c1
->c_flags
& DATEFMT
) {
648 if (global
.c_flags
& DATEFMT
) {
649 c1
->c_nfs
= getcpy (global
.c_nfs
);
650 compile_formatfield(c1
);
654 if (c1
->c_flags
& ADDRFMT
) {
655 if (global
.c_flags
& ADDRFMT
) {
656 c1
->c_nfs
= getcpy (global
.c_nfs
);
657 compile_formatfield(c1
);
664 adios (NULL
, "format file syntax error: %s", bp
);
670 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
671 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
672 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
673 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
674 (unsigned int)(unsigned long) c1
->c_nfs
,
675 (unsigned int)(unsigned long) c1
->c_fmt
);
676 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
677 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
678 c1
->c_cwidth
, c1
->c_length
);
679 fprintf (stderr
, "\tflags=%s\n",
680 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
686 global
.c_flags
|= CLEARSCR
;
689 global
.c_flags
&= ~CLEARSCR
;
692 switch (bellflg
) { /* command line may override format file */
694 global
.c_flags
|= BELL
;
697 global
.c_flags
&= ~BELL
;
702 global
.c_length
= length
;
704 global
.c_width
= width
;
705 if (global
.c_length
< 5)
706 global
.c_length
= 10000;
707 if (global
.c_width
< 5)
708 global
.c_width
= 10000;
713 evalvar (struct mcomp
*c1
)
715 char *cp
, name
[NAMESZ
];
720 strncpy (name
, parse(), sizeof(name
));
722 if (!mh_strcasecmp (name
, "component")) {
723 if (ptos (name
, &c1
->c_text
))
725 c1
->c_flags
&= ~NOCOMPONENT
;
729 if (!mh_strcasecmp (name
, "overflowtext"))
730 return ptos (name
, &c1
->c_ovtxt
);
732 if (!mh_strcasecmp (name
, "formatfield")) {
733 if (ptos (name
, &cp
))
735 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
736 compile_formatfield(c1
);
737 c1
->c_flags
|= FORMAT
;
741 if (!mh_strcasecmp (name
, "decode")) {
742 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
743 compile_formatfield(c1
);
744 c1
->c_flags
|= FORMAT
;
748 if (!mh_strcasecmp (name
, "offset"))
749 return ptoi (name
, &c1
->c_offset
);
750 if (!mh_strcasecmp (name
, "overflowoffset"))
751 return ptoi (name
, &c1
->c_ovoff
);
752 if (!mh_strcasecmp (name
, "width"))
753 return ptoi (name
, &c1
->c_width
);
754 if (!mh_strcasecmp (name
, "compwidth"))
755 return ptoi (name
, &c1
->c_cwidth
);
756 if (!mh_strcasecmp (name
, "length"))
757 return ptoi (name
, &c1
->c_length
);
758 if (!mh_strcasecmp (name
, "nodashstuffing"))
759 return (dashstuff
= -1);
761 for (ap
= triples
; ap
->t_name
; ap
++)
762 if (!mh_strcasecmp (ap
->t_name
, name
)) {
763 c1
->c_flags
|= ap
->t_on
;
764 c1
->c_flags
&= ~ap
->t_off
;
768 if (!mh_strcasecmp (name
, "formatarg")) {
769 struct arglist
*args
;
771 if (ptos (name
, &cp
))
774 if (mh_strcasecmp (c1
->c_name
, "body")) {
775 advise (NULL
, "format filters are currently only supported on "
776 "the \"body\" component");
780 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
783 arglist_tail
->a_next
= args
;
790 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
801 ptoi (char *name
, int *i
)
805 if (*parptr
++ != '=' || !*(cp
= parse ())) {
806 advise (NULL
, "missing argument to variable %s", name
);
816 ptos (char *name
, char **s
)
820 if (*parptr
++ != '=') {
821 advise (NULL
, "missing argument to variable %s", name
);
825 if (*parptr
!= '"') {
827 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
831 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
832 if (*parptr
== QUOTE
)
839 if ((*parptr
= c
) == '"')
850 static char result
[NAMESZ
];
852 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
871 * Process one file/message
875 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
883 switch (setjmp (env
)) {
886 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
888 advise (fname
, "unable to open");
896 if (fstat(fileno(fp
), &st
) == 0) {
897 filesize
= st
.st_size
;
901 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
903 SIGNAL (SIGINT
, intrser
);
904 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
906 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
907 fmt_free(ap
->a_fmt
, 0);
916 SIGNAL (SIGINT
, SIG_IGN
);
917 if (mhl_action
== NULL
&& fp
!= stdin
)
921 free (holder
.c_text
);
922 holder
.c_text
= NULL
;
924 free_queue (&msghd
, &msgtl
);
925 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
926 c1
->c_flags
&= ~HDROUTPUT
;
934 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
937 struct mcomp
*c1
, *c2
, *c3
;
938 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
940 compile_filterargs();
944 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
946 printf ("\n-------");
948 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
950 printf (" Message %d", ofilen
);
958 if ((global
.c_flags
& CLEARSCR
))
963 printf (">>> %s\n\n", mname
);
968 strncpy (buf
, "\n", sizeof(buf
));
970 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
973 printf ("Press <return> to list \"%s\"...", mname
);
977 read (fileno (stdout
), buf
, sizeof(buf
));
979 if (strchr(buf
, '\n')) {
980 if ((global
.c_flags
& CLEARSCR
))
994 printf (">>> %s\n\n", mname
);
1000 for (state
= FLD
;;) {
1001 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
1004 bucket
= fmt_addcomptext(name
, buf
);
1005 for (ip
= ignores
; *ip
; ip
++)
1006 if (!mh_strcasecmp (name
, *ip
)) {
1007 while (state
== FLDPLUS
) {
1008 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
1009 fmt_appendcomp(bucket
, name
, buf
);
1016 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1017 if (!mh_strcasecmp (c2
->c_name
, name
))
1020 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1021 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1022 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
1024 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1028 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1029 while (state
== FLDPLUS
) {
1030 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
1031 c1
->c_text
= add (buf
, c1
->c_text
);
1032 fmt_appendcomp(bucket
, name
, buf
);
1035 c1
->c_flags
|= EXTRA
;
1041 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1042 if (c1
->c_flags
& CLEARTEXT
) {
1043 putcomp (c1
, c1
, ONECOMP
);
1046 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
1047 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1049 putcomp (c1
, &holder
, ONECOMP
);
1050 free (holder
.c_text
);
1051 holder
.c_text
= NULL
;
1054 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
1055 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1056 if (c2
->c_flags
& EXTRA
)
1057 putcomp (c1
, c2
, TWOCOMP
);
1060 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
1061 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1062 formatproc
!= NULL
) {
1063 filterbody(c1
, buf
, sizeof(buf
), state
, fp
);
1065 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1066 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1067 while (state
== BODY
) {
1068 putcomp (c1
, &holder
, BODYCOMP
);
1069 state
= m_getfld (state
, name
, holder
.c_text
,
1072 free (holder
.c_text
);
1073 holder
.c_text
= NULL
;
1077 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1078 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
1079 putcomp (c1
, c2
, ONECOMP
);
1080 if (!(c1
->c_flags
& SPLIT
))
1088 advise (NULL
, "format error in message %s", mname
);
1093 adios (NULL
, "getfld() returned %d", state
);
1100 mcomp_flags (char *name
)
1104 for (ap
= pairs
; ap
->p_name
; ap
++)
1105 if (!mh_strcasecmp (ap
->p_name
, name
))
1106 return (ap
->p_flags
);
1113 mcomp_add (long flags
, char *s1
, char *s2
)
1117 if (!(flags
& ADDRFMT
))
1118 return add (s1
, s2
);
1120 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1123 return add (s1
, add (",\n", s2
));
1130 struct pqpair
*pq_next
;
1135 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1139 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1140 struct pqpair
*p
, *q
;
1142 struct mailname
*mp
;
1149 dat
[3] = sizeof(buffer
) - 1;
1152 if (!(c1
->c_flags
& ADDRFMT
)) {
1154 c1
->c_c_text
->c_text
= ap
;
1155 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1159 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1160 /* Don't need to append a newline, dctime() already did */
1161 c2
->c_text
= getcpy (buffer
);
1163 /* ap is now owned by the component struct, so do NOT free it here */
1167 (q
= &pq
)->pq_next
= NULL
;
1168 while ((cp
= getname (ap
))) {
1169 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1170 adios (NULL
, "unable to allocate pqpair memory");
1172 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1173 p
->pq_text
= getcpy (cp
);
1174 p
->pq_error
= getcpy (error
);
1176 p
->pq_text
= getcpy (mp
->m_text
);
1179 q
= (q
->pq_next
= p
);
1182 for (p
= pq
.pq_next
; p
; p
= q
) {
1184 c1
->c_c_text
->c_text
= p
->pq_text
;
1187 if (c1
->c_c_error
) {
1188 c1
->c_c_error
->c_text
= p
->pq_error
;
1192 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1195 c2
->c_text
= add (",\n", c2
->c_text
);
1196 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1198 c2
->c_text
= add (buffer
, c2
->c_text
);
1209 c2
->c_text
= add ("\n", c2
->c_text
);
1214 static struct mcomp
*
1215 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1219 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1220 adios (NULL
, "unable to allocate comp memory");
1222 c1
->c_flags
= flags
& ~INIT
;
1223 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1224 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1225 c1
->c_text
= text
? getcpy (text
) : NULL
;
1228 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1229 c1
->c_offset
= global
.c_offset
;
1230 c1
->c_ovoff
= global
. c_ovoff
;
1231 c1
->c_width
= c1
->c_length
= 0;
1232 c1
->c_cwidth
= global
.c_cwidth
;
1233 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1238 (*tail
)->c_next
= c1
;
1246 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1248 struct mcomp
*c1
, *c2
;
1250 for (c1
= *head
; c1
; c1
= c2
) {
1261 fmt_free (c1
->c_fmt
, 0);
1265 *head
= *tail
= NULL
;
1270 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1277 llim
= c1
->c_length
? c1
->c_length
: -1;
1278 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1279 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1281 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1283 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1284 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1285 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1288 if (c1
->c_flags
& CLEARTEXT
) {
1289 putstr (c1
->c_text
, c1
->c_flags
);
1290 putstr ("\n", c1
->c_flags
);
1294 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1295 mcomp_format (c1
, c2
);
1297 if (c1
->c_flags
& CENTER
) {
1298 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1299 - c1
->c_offset
- strlen (c2
->c_text
);
1300 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1301 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1302 lm
= c1
->c_offset
+ (count
/ 2);
1308 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1309 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1310 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1312 *cp
= toupper (*cp
);
1313 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1314 if (flag
!= BODYCOMP
) {
1315 putstr (": ", c1
->c_flags
);
1316 if (!(c1
->c_flags
& SPLIT
))
1317 c1
->c_flags
|= HDROUTPUT
;
1320 if ((count
= c1
->c_cwidth
-
1321 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1323 putstr (" ", c1
->c_flags
);
1326 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1330 && !(c2
->c_flags
& HDROUTPUT
)
1331 && !(c2
->c_flags
& NOCOMPONENT
)) {
1332 if (c1
->c_flags
& UPPERCASE
)
1333 for (cp
= c2
->c_name
; *cp
; cp
++)
1335 *cp
= toupper (*cp
);
1336 putstr (c2
->c_name
, c1
->c_flags
);
1337 putstr (": ", c1
->c_flags
);
1338 if (!(c1
->c_flags
& SPLIT
))
1339 c2
->c_flags
|= HDROUTPUT
;
1342 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1344 putstr (" ", c1
->c_flags
);
1346 if (c1
->c_flags
& UPPERCASE
)
1347 for (cp
= c2
->c_text
; *cp
; cp
++)
1349 *cp
= toupper (*cp
);
1353 if (flag
== TWOCOMP
)
1354 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1355 : (int) strlen (c2
->c_name
) + 2;
1357 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1358 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1360 count
+= c1
->c_offset
;
1362 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1363 putstr(cp
, c1
->c_flags
);
1365 putstr ("\n", c1
->c_flags
);
1366 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1368 if (flag
== BODYCOMP
1369 && !(c1
->c_flags
& NOCOMPONENT
))
1370 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1372 putstr (cp
, c1
->c_flags
);
1374 putstr ("\n", c1
->c_flags
);
1376 if (flag
== BODYCOMP
&& term
== '\n')
1377 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1382 oneline (char *stuff
, long flags
)
1390 return (onelp
= NULL
);
1394 if (flags
& COMPRESS
) {
1395 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1396 if (isspace (*onelp
)) {
1397 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1416 while (*onelp
&& *onelp
!= '\n')
1418 if (*onelp
== '\n') {
1422 if (flags
& LEFTADJUST
)
1423 while (*ret
== ' ' || *ret
== '\t')
1426 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1434 putstr (char *string
, long flags
)
1436 if (!column
&& lm
> 0) {
1439 putch ('\t', flags
);
1449 putch (*string
++, flags
);
1454 putch (char ch
, long flags
)
1467 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1469 if (global
.c_flags
& BELL
)
1473 read (fileno (stdout
), buf
, sizeof(buf
));
1474 if (strchr(buf
, '\n')) {
1475 if (global
.c_flags
& CLEARSCR
)
1480 row
= global
.c_length
/ 3;
1499 * If we are forwarding this message, and the first
1500 * column contains a dash, then add a dash and a space.
1502 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1511 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1512 putch ('\n', flags
);
1515 putstr (ovtxt
? ovtxt
: "", flags
);
1531 longjmp (env
, DONE
);
1556 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1558 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1561 struct arglist
*a
, *a2
;
1563 switch (setjmp (mhlenv
)) {
1566 sleepsw
= 0; /* XXX */
1567 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1570 mhl_action
= action
;
1573 * If signal is at default action, then start ignoring
1574 * it, else let it set to its current action.
1576 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1577 SIGNAL (SIGINT
, istat
);
1578 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1579 SIGNAL (SIGQUIT
, qstat
);
1580 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1581 mhl (argc
, argv
); /* FALL THROUGH! */
1584 SIGNAL (SIGINT
, istat
);
1585 SIGNAL (SIGQUIT
, qstat
);
1586 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1589 SIGNAL (SIGPIPE
, pstat
);
1591 if (holder
.c_text
) {
1592 free (holder
.c_text
);
1593 holder
.c_text
= NULL
;
1595 free_queue (&msghd
, &msgtl
);
1596 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1597 c1
->c_flags
&= ~HDROUTPUT
;
1615 mhladios (char *what
, char *fmt
, ...)
1620 advertise (what
, NULL
, fmt
, ap
);
1627 mhldone (int status
)
1631 longjmp (mhlenv
, DONE
);
1637 static int m_pid
= NOTOK
;
1638 static int sd
= NOTOK
;
1641 m_popen (char *name
)
1645 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1646 adios ("standard output", "unable to dup()");
1648 if (pipe (pd
) == NOTOK
)
1649 adios ("pipe", "unable to");
1651 switch (m_pid
= vfork()) {
1653 adios ("fork", "unable to");
1656 SIGNAL (SIGINT
, SIG_DFL
);
1657 SIGNAL (SIGQUIT
, SIG_DFL
);
1660 if (pd
[0] != fileno (stdin
)) {
1661 dup2 (pd
[0], fileno (stdin
));
1664 execlp (name
, r1bindex (name
, '/'), NULL
);
1665 fprintf (stderr
, "unable to exec ");
1671 if (pd
[1] != fileno (stdout
)) {
1672 dup2 (pd
[1], fileno (stdout
));
1687 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1688 adios ("standard output", "unable to dup2()");
1697 pidwait (m_pid
, OK
);
1703 * Compile a format string used by the formatfield option and save it
1706 * We will want the {text} (and possibly {error}) components for later,
1707 * so look for them and save them if we find them.
1711 compile_formatfield(struct mcomp
*c1
)
1713 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1716 * As a note to myself and any other poor bastard who is looking through
1717 * this code in the future ....
1719 * When the format hash table is reset later on (as it almost certainly
1720 * will be), there will still be references to these components in the
1721 * compiled format instructions. Thus these component references will
1722 * be free'd when the format instructions are free'd (by fmt_free()).
1724 * So, in other words ... don't go free'ing them yourself!
1727 c1
->c_c_text
= fmt_findcomp("text");
1728 c1
->c_c_error
= fmt_findcomp("error");
1732 * Compile all of the arguments for our format list.
1734 * Iterate through the linked list of format strings and compile them.
1735 * Note that we reset the format hash table before we start, but we do NOT
1736 * reset it between calls to fmt_compile().
1741 compile_filterargs (void)
1743 struct arglist
*arg
= arglist_head
;
1750 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1755 * Search through and mark any components that are address components
1758 for (ap
= addrcomps
; *ap
; ap
++) {
1759 cptr
= fmt_findcomp (*ap
);
1761 cptr
->c_type
|= CT_ADDR
;
1766 * Filter the body of a message through a specified format program
1770 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
)
1772 struct mcomp holder
;
1774 int fdinput
[2], fdoutput
[2], waitstat
;
1776 pid_t writerpid
, filterpid
;
1779 * Create pipes so we can communicate with our filter process.
1782 if (pipe(fdinput
) < 0) {
1783 adios(NULL
, "Unable to create input pipe");
1786 if (pipe(fdoutput
) < 0) {
1787 adios(NULL
, "Unable to create output pipe");
1791 * Here's what we're doing to do.
1793 * - Fork ourselves and start writing data to the write side of the
1794 * input pipe (fdinput[1]).
1796 * - Fork and exec our filter program. We set the standard input of
1797 * our filter program to be the read side of our input pipe (fdinput[0]).
1798 * Standard output is set to the write side of our output pipe
1801 * - We read from the read side of the output pipe (fdoutput[0]).
1803 * We're forking because that's the simplest way to prevent any deadlocks.
1804 * (without doing something like switching to non-blocking I/O and using
1805 * select or poll, and I'm not interested in doing that).
1808 switch (writerpid
= fork()) {
1811 * Our child process - just write to the filter input (fdinput[1]).
1812 * Close all other descriptors that we don't need.
1820 * Call m_getfld() until we're no longer in the BODY state
1823 while (state
== BODY
) {
1824 write(fdinput
[1], buf
, strlen(buf
));
1825 state
= m_getfld(state
, name
, buf
, bufsz
, fp
);
1829 * We should be done; time to exit.
1834 * Make sure we call _exit(), otherwise we may flush out the stdio
1835 * buffers that we have duplicated from the parent.
1840 adios(NULL
, "Unable to fork for filter writer process");
1845 * Fork and exec() our filter program, after redirecting standard in
1846 * and standard out appropriately.
1849 switch (filterpid
= fork()) {
1856 * Allocate an argument array for us
1859 args
= (char **) mh_xmalloc((filter_nargs
+ 2) * sizeof(char *));
1860 args
[0] = formatproc
;
1861 args
[filter_nargs
+ 1] = NULL
;
1869 * Pull out each argument and scan them.
1872 for (a
= arglist_head
, i
= 1; a
!= NULL
; a
= a
->a_next
, i
++) {
1873 args
[i
] = mh_xmalloc(BUFSIZ
);
1874 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
- 1, BUFSIZ
, dat
);
1876 * fmt_scan likes to put a trailing newline at the end of the
1877 * format string. If we have one, get rid of it.
1879 s
= strlen(args
[i
]);
1880 if (args
[i
][s
- 1] == '\n')
1881 args
[i
][s
- 1] = '\0';
1884 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1888 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1889 adios("formatproc", "Unable to dup2() standard input");
1891 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1892 adios("formatproc", "Unable to dup2() standard output");
1896 * Close everything (especially the old input and output
1897 * descriptors, since they've been dup'd to stdin and stdout),
1898 * and exec the formatproc.
1906 execvp(formatproc
, args
);
1908 adios(formatproc
, "Unable to execute filter");
1913 adios(NULL
, "Unable to fork format program");
1917 * Close everything except our reader (fdoutput[0]);
1925 * As we read in this data, send it to putcomp
1928 holder
.c_text
= buf
;
1930 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1932 putcomp(c1
, &holder
, BODYCOMP
);
1936 adios(NULL
, "reading from formatproc");
1940 * See if we got any errors along the way. I'm a little leery of calling
1941 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1944 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1945 if (errno
!= ECHILD
) {
1946 adios("filterproc", "Unable to determine status");
1949 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1950 pidstatus(waitstat
, stderr
, "filterproc");
1954 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1955 if (errno
!= ECHILD
) {
1956 adios("writer process", "Unable to determine status");
1960 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1961 pidstatus(waitstat
, stderr
, "writer process");