]>
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>
17 #include <sys/types.h>
21 * for a component containing addresses, ADDRFMT, if COMPRESS is also
22 * set, then addresses get split wrong (not at the spaces between commas).
23 * To fix this correctly, putstr() should know about "atomic" strings that
24 * must NOT be broken across lines. That's too difficult for right now
25 * (it turns out that there are a number of degernate cases), so in
26 * oneline(), instead of
28 * (*onelp == '\n' && !onelp[1])
30 * being a terminating condition,
32 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
34 * is used instead. This cuts the line prematurely, and gives us a much
35 * better chance of getting things right.
44 #define MHL_SWITCHES \
45 X("bell", 0, BELLSW) \
46 X("nobell", 0, NBELLSW) \
47 X("clear", 0, CLRSW) \
48 X("noclear", 0, NCLRSW) \
49 X("folder +folder", 0, FOLDSW) \
50 X("form formfile", 0, FORMSW) \
51 X("moreproc program", 0, PROGSW) \
52 X("nomoreproc", 0, NPROGSW) \
53 X("length lines", 0, LENSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("sleep seconds", 0, SLEEPSW) \
56 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
57 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("forward", -7, FORW1SW) /* interface from forw */ \
61 X("forwall", -7, FORW2SW) /* interface from forw */ \
62 X("digest list", -6, DGSTSW) \
63 X("volume number", -6, VOLUMSW) \
64 X("issue number", -5, ISSUESW) \
65 X("nobody", -6, NBODYSW) \
66 X("fmtproc program", 0, FMTPROCSW) \
67 X("nofmtproc", 0, NFMTPROCSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHL
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
77 #define NOCOMPONENT 0x000001 /* don't show component name */
78 #define UPPERCASE 0x000002 /* display in all upper case */
79 #define CENTER 0x000004 /* center line */
80 #define CLEARTEXT 0x000008 /* cleartext */
81 #define EXTRA 0x000010 /* an "extra" component */
82 #define HDROUTPUT 0x000020 /* already output */
83 #define CLEARSCR 0x000040 /* clear screen */
84 #define LEFTADJUST 0x000080 /* left justify multiple lines */
85 #define COMPRESS 0x000100 /* compress text */
86 #define ADDRFMT 0x000200 /* contains addresses */
87 #define BELL 0x000400 /* sound bell at EOP */
88 #define DATEFMT 0x000800 /* contains dates */
89 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
90 #define INIT 0x002000 /* initialize component */
91 #define SPLIT 0x010000 /* split headers (don't concatenate) */
92 #define NONEWLINE 0x020000 /* don't write trailing newline */
93 #define NOWRAP 0x040000 /* Don't wrap lines ever */
94 #define FMTFILTER 0x080000 /* Filter through format filter */
95 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
96 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
99 * A format string to be used as a command-line argument to the body
104 struct format
*a_fmt
;
106 struct arglist
*a_next
;
110 * Linked list of command line arguments for the body format filter. This
111 * USED to be in "struct mcomp", but the format API got cleaned up and even
112 * though it reduced the code we had to do, it make things more complicated
113 * for us. Specifically:
115 * - The interface to the hash table has been cleaned up, which means the
116 * rooting around in the hash table is no longer necessary (yay!). But
117 * this ALSO means that we have to make sure that we call our format
118 * compilation routines before we process the message, because the
119 * components need to be visible in the hash table so we can save them for
120 * later. So we moved them out of "mcomp" and now compile them right before
121 * header processing starts.
122 * - We also use format strings to handle other components in the mhl
123 * configuration (using "formatfield" and "decode"), but here life
124 * gets complicated: they aren't dealt with in the normal way. Instead
125 * of referring to a component like {from}, each component is processed
126 * using the special {text} component. But these format strings need to be
127 * compiled BEFORE we compile the format arguments; in the previous
128 * implementation they were compiled and scanned as the headers were
129 * read, and that would reset the hash table that we need to populate
130 * the components used by the body format filter. So we are compiling
131 * the formatfield component strings ahead of time and then scanning them
134 * Okay, fine ... this was broken before. But you know what? Fixing this
135 * the right way will make things easier down the road.
137 * One side-effect to this change: format strings are now compiled only once
138 * for components specified with "formatfield", but they are compiled for
139 * every message for format arguments.
142 static struct arglist
*arglist_head
;
143 static struct arglist
*arglist_tail
;
144 static int filter_nargs
= 0;
147 * Flags/options for each component
151 char *c_name
; /* component name */
152 char *c_text
; /* component text */
153 char *c_ovtxt
; /* text overflow indicator */
154 char *c_nfs
; /* iff FORMAT */
155 struct format
*c_fmt
; /* .. */
156 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
157 struct comp
*c_c_error
; /* Ref to {error} */
158 int c_offset
; /* left margin indentation */
159 int c_ovoff
; /* overflow indentation */
160 int c_width
; /* width of field */
161 int c_cwidth
; /* width of component */
162 int c_length
; /* length in lines */
164 struct mcomp
*c_next
;
167 static struct mcomp
*msghd
= NULL
;
168 static struct mcomp
*msgtl
= NULL
;
169 static struct mcomp
*fmthd
= NULL
;
170 static struct mcomp
*fmttl
= NULL
;
172 static struct mcomp global
= {
173 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
176 static struct mcomp holder
= {
177 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
185 static struct pair pairs
[] = {
188 { "Sender", ADDRFMT
},
189 { "Reply-To", ADDRFMT
},
193 { "Resent-Date", DATEFMT
},
194 { "Resent-From", ADDRFMT
},
195 { "Resent-Sender", ADDRFMT
},
196 { "Resent-Reply-To", ADDRFMT
},
197 { "Resent-To", ADDRFMT
},
198 { "Resent-cc", ADDRFMT
},
199 { "Resent-Bcc", ADDRFMT
},
209 static struct triple triples
[] = {
210 { "nocomponent", NOCOMPONENT
, 0 },
211 { "uppercase", UPPERCASE
, 0 },
212 { "nouppercase", 0, UPPERCASE
},
213 { "center", CENTER
, 0 },
214 { "nocenter", 0, CENTER
},
215 { "clearscreen", CLEARSCR
, 0 },
216 { "noclearscreen", 0, CLEARSCR
},
217 { "noclear", 0, CLEARSCR
},
218 { "leftadjust", LEFTADJUST
, 0 },
219 { "noleftadjust", 0, LEFTADJUST
},
220 { "compress", COMPRESS
, 0 },
221 { "nocompress", 0, COMPRESS
},
222 { "split", SPLIT
, 0 },
223 { "nosplit", 0, SPLIT
},
224 { "addrfield", ADDRFMT
, DATEFMT
},
226 { "nobell", 0, BELL
},
227 { "datefield", DATEFMT
, ADDRFMT
},
228 { "newline", 0, NONEWLINE
},
229 { "nonewline", NONEWLINE
, 0 },
230 { "wrap", 0, NOWRAP
},
231 { "nowrap", NOWRAP
, 0 },
232 { "format", FMTFILTER
, 0 },
233 { "noformat", 0, FMTFILTER
},
237 static char *addrcomps
[] = {
254 static int bellflg
= 0;
255 static int clearflg
= 0;
256 static int dashstuff
= 0;
257 static int dobody
= 1;
258 static int forwflg
= 0;
259 static int forwall
= 0;
261 static int sleepsw
= NOTOK
;
263 static char *digest
= NULL
;
264 static int volume
= 0;
265 static int issue
= 0;
267 static int exitstat
= 0;
268 static int mhldebug
= 0;
270 static int filesize
= 0;
275 static int ontty
= NOTTY
;
278 static unsigned int column
;
284 static unsigned int wid
;
292 static int num_ignores
= 0;
293 static char *ignores
[MAXARGS
];
296 static jmp_buf mhlenv
;
298 static char delim3
[] = /* from forw.c */
299 "\n----------------------------------------------------------------------\n\n";
300 static char delim4
[] = "\n------------------------------\n\n";
302 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
305 * Redefine a couple of functions.
306 * These are undefined later in the code.
308 #define adios mhladios
314 static void mhl_format (char *, int, int);
315 static int evalvar (struct mcomp
*);
316 static int ptoi (char *, int *);
317 static int ptos (char *, char **);
318 static char *parse (void);
319 static void process (char *, char *, int, int);
320 static void mhlfile (FILE *, char *, int, int);
321 static int mcomp_flags (char *);
322 static char *mcomp_add (long, char *, char *);
323 static void mcomp_format (struct mcomp
*, struct mcomp
*);
324 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
325 static void free_queue (struct mcomp
**, struct mcomp
**);
326 static void putcomp (struct mcomp
*, struct mcomp
*, int);
327 static char *oneline (char *, long);
328 static void putstr (char *, long);
329 static void putch (char, long);
330 static void intrser (int);
331 static void pipeser (int);
332 static void quitser (int);
333 static void mhladios (char *, char *, ...);
334 static void mhldone (int);
335 static void m_popen (char *);
336 static void filterbody (struct mcomp
*, char *, int, int, FILE *,
338 static void compile_formatfield(struct mcomp
*);
339 static void compile_filterargs (void);
343 mhl (int argc
, char **argv
)
345 int length
= 0, nomore
= 0;
346 unsigned int i
, vecp
= 0;
348 char *cp
, *folder
= NULL
, *form
= NULL
;
349 char buf
[BUFSIZ
], *files
[MAXARGS
];
350 char **argp
, **arguments
;
352 invo_name
= r1bindex (argv
[0], '/');
354 /* read user profile/context */
357 arguments
= getarguments (invo_name
, argc
, argv
, 1);
360 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
363 while ((cp
= *argp
++)) {
365 switch (smatch (++cp
, mhlswitches
)) {
367 ambigsw (cp
, mhlswitches
);
370 adios (NULL
, "-%s unknown\n", cp
);
373 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
374 print_help (buf
, mhlswitches
, 1);
377 print_version(invo_name
);
395 if (!(folder
= *argp
++) || *folder
== '-')
396 adios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(form
= *argp
++) || *form
== '-')
400 adios (NULL
, "missing argument to %s", argp
[-2]);
404 if (!(cp
= *argp
++) || *cp
== '-')
405 adios (NULL
, "missing argument to %s", argp
[-2]);
406 sleepsw
= atoi (cp
);/* ZERO ok! */
410 if (!(moreproc
= *argp
++) || *moreproc
== '-')
411 adios (NULL
, "missing argument to %s", argp
[-2]);
418 if (!(formatproc
= *argp
++) || *formatproc
== '-')
419 adios (NULL
, "missing argument to %s", argp
[-2]);
426 if (!(cp
= *argp
++) || *cp
== '-')
427 adios (NULL
, "missing argument to %s", argp
[-2]);
428 if ((length
= atoi (cp
)) < 1)
429 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
432 if (!(cp
= *argp
++) || *cp
== '-')
433 adios (NULL
, "missing argument to %s", argp
[-2]);
434 if ((width
= atoi (cp
)) < 1)
435 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
439 if (!(digest
= *argp
++) || *digest
== '-')
440 adios (NULL
, "missing argument to %s", argp
[-2]);
443 if (!(cp
= *argp
++) || *cp
== '-')
444 adios (NULL
, "missing argument to %s", argp
[-2]);
445 if ((issue
= atoi (cp
)) < 1)
446 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
449 if (!(cp
= *argp
++) || *cp
== '-')
450 adios (NULL
, "missing argument to %s", argp
[-2]);
451 if ((volume
= atoi (cp
)) < 1)
452 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
456 forwall
++; /* fall */
459 clearflg
= -1;/* XXX */
463 dashstuff
= 1; /* trinary logic */
466 dashstuff
= -1; /* trinary logic */
478 folder
= getenv ("mhfolder");
480 if (isatty (fileno (stdout
))) {
481 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
483 SIGNAL (SIGINT
, SIG_IGN
);
484 SIGNAL2 (SIGQUIT
, quitser
);
486 SIGNAL2 (SIGPIPE
, pipeser
);
490 SIGNAL (SIGINT
, SIG_IGN
);
491 SIGNAL2 (SIGQUIT
, quitser
);
498 mhl_format (form
? form
: mhlformat
, length
, width
);
501 process (folder
, NULL
, 1, vecp
= 1);
503 for (i
= 0; i
< vecp
; i
++)
504 process (folder
, files
[i
], i
+ 1, vecp
);
509 printf ("%s", delim4
);
511 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
513 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
514 digest
, volume
, issue
);
517 for (cp
= buf
+ i
; i
> 1; i
--)
524 printf ("\n------- End of Forwarded Message%s\n",
525 vecp
> 1 ? "s" : "");
530 adios("output", "error writing");
533 if (clearflg
> 0 && ontty
== NOTTY
)
544 mhl_format (char *file
, int length
, int width
)
548 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
552 static dev_t dev
= 0;
553 static ino_t ino
= 0;
554 static time_t mtime
= 0;
557 if (stat (etcpath (file
), &st
) != NOTOK
558 && mtime
== st
.st_mtime
563 free_queue (&fmthd
, &fmttl
);
566 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
567 adios (file
, "unable to open format file");
569 if (fstat (fileno (fp
), &st
) != NOTOK
) {
575 global
.c_ovtxt
= global
.c_nfs
= NULL
;
579 if ((i
= sc_width ()) > 5)
581 global
.c_cwidth
= -1;
582 if ((i
= sc_length ()) > 5)
583 global
.c_length
= i
- 1;
584 global
.c_flags
= BELL
; /* BELL is default */
585 *(ip
= ignores
) = NULL
;
588 while (vfgets (fp
, &ap
) == OK
) {
593 if ((cp
= strchr(bp
, '\n')))
597 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
602 strncpy (name
, parse(), sizeof(name
));
608 * Split this list of fields to ignore, and copy
609 * it to the end of the current "ignores" list.
611 if (!strcasecmp (name
, "ignores")) {
612 char **tmparray
, **p
;
615 /* split the fields */
616 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
618 /* count number of fields split */
623 /* copy pointers to split fields to ignores array */
624 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
630 if (evalvar (&global
))
631 adios (NULL
, "format file syntax error: %s", bp
);
638 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
639 while (*parptr
== ':' || *parptr
== ',') {
642 adios (NULL
, "format file syntax error: %s", bp
);
644 if (!c1
->c_nfs
&& global
.c_nfs
) {
645 if (c1
->c_flags
& DATEFMT
) {
646 if (global
.c_flags
& DATEFMT
) {
647 c1
->c_nfs
= getcpy (global
.c_nfs
);
648 compile_formatfield(c1
);
652 if (c1
->c_flags
& ADDRFMT
) {
653 if (global
.c_flags
& ADDRFMT
) {
654 c1
->c_nfs
= getcpy (global
.c_nfs
);
655 compile_formatfield(c1
);
662 adios (NULL
, "format file syntax error: %s", bp
);
668 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
669 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
670 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
671 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
672 (unsigned int)(unsigned long) c1
->c_nfs
,
673 (unsigned int)(unsigned long) c1
->c_fmt
);
674 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
675 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
676 c1
->c_cwidth
, c1
->c_length
);
677 fprintf (stderr
, "\tflags=%s\n",
678 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
684 global
.c_flags
|= CLEARSCR
;
687 global
.c_flags
&= ~CLEARSCR
;
690 switch (bellflg
) { /* command line may override format file */
692 global
.c_flags
|= BELL
;
695 global
.c_flags
&= ~BELL
;
700 global
.c_length
= length
;
702 global
.c_width
= width
;
703 if (global
.c_length
< 5)
704 global
.c_length
= 10000;
705 if (global
.c_width
< 5)
706 global
.c_width
= 10000;
711 evalvar (struct mcomp
*c1
)
713 char *cp
, name
[NAMESZ
];
718 strncpy (name
, parse(), sizeof(name
));
720 if (!strcasecmp (name
, "component")) {
721 if (ptos (name
, &c1
->c_text
))
723 c1
->c_flags
&= ~NOCOMPONENT
;
727 if (!strcasecmp (name
, "overflowtext"))
728 return ptos (name
, &c1
->c_ovtxt
);
730 if (!strcasecmp (name
, "formatfield")) {
731 if (ptos (name
, &cp
))
733 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
734 compile_formatfield(c1
);
735 c1
->c_flags
|= FORMAT
;
739 if (!strcasecmp (name
, "decode")) {
740 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
741 compile_formatfield(c1
);
742 c1
->c_flags
|= FORMAT
;
746 if (!strcasecmp (name
, "offset"))
747 return ptoi (name
, &c1
->c_offset
);
748 if (!strcasecmp (name
, "overflowoffset"))
749 return ptoi (name
, &c1
->c_ovoff
);
750 if (!strcasecmp (name
, "width"))
751 return ptoi (name
, &c1
->c_width
);
752 if (!strcasecmp (name
, "compwidth"))
753 return ptoi (name
, &c1
->c_cwidth
);
754 if (!strcasecmp (name
, "length"))
755 return ptoi (name
, &c1
->c_length
);
756 if (!strcasecmp (name
, "nodashstuffing"))
757 return (dashstuff
= -1);
759 for (ap
= triples
; ap
->t_name
; ap
++)
760 if (!strcasecmp (ap
->t_name
, name
)) {
761 c1
->c_flags
|= ap
->t_on
;
762 c1
->c_flags
&= ~ap
->t_off
;
766 if (!strcasecmp (name
, "formatarg")) {
767 struct arglist
*args
;
769 if (ptos (name
, &cp
))
772 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
773 advise (NULL
, "format filters are currently only supported on "
774 "the \"body\" component");
778 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
781 arglist_tail
->a_next
= args
;
788 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
799 ptoi (char *name
, int *i
)
803 if (*parptr
++ != '=' || !*(cp
= parse ())) {
804 advise (NULL
, "missing argument to variable %s", name
);
814 ptos (char *name
, char **s
)
818 if (*parptr
++ != '=') {
819 advise (NULL
, "missing argument to variable %s", name
);
823 if (*parptr
!= '"') {
825 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
829 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
830 if (*parptr
== QUOTE
)
837 if ((*parptr
= c
) == '"')
848 static char result
[NAMESZ
];
850 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
869 * Process one file/message
873 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
880 /* volatile to prevent "might be clobbered" warning from gcc: */
881 char *volatile fname2
= fname
? fname
: "(stdin)";
883 switch (setjmp (env
)) {
886 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
888 advise (fname
, "unable to open");
895 if (fstat(fileno(fp
), &st
) == 0) {
896 filesize
= st
.st_size
;
900 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : getcpy (fname2
);
902 SIGNAL (SIGINT
, intrser
);
903 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
905 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
906 fmt_free(ap
->a_fmt
, 0);
915 SIGNAL (SIGINT
, SIG_IGN
);
916 if (mhl_action
== NULL
&& fp
!= stdin
)
920 free (holder
.c_text
);
921 holder
.c_text
= NULL
;
923 free_queue (&msghd
, &msgtl
);
924 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
925 c1
->c_flags
&= ~HDROUTPUT
;
933 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
936 struct mcomp
*c1
, *c2
, *c3
;
937 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
938 m_getfld_state_t gstate
= 0;
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
);
1001 int bufsz
= sizeof buf
;
1002 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1005 bucket
= fmt_addcomptext(name
, buf
);
1006 for (ip
= ignores
; *ip
; ip
++)
1007 if (!strcasecmp (name
, *ip
)) {
1008 while (state
== FLDPLUS
) {
1010 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1011 fmt_appendcomp(bucket
, name
, buf
);
1018 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1019 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "", name
))
1022 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1023 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1024 if (!strcasecmp (c1
->c_name
? c1
->c_name
: "",
1025 c3
->c_name
? c3
->c_name
: "")) {
1027 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1031 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1032 while (state
== FLDPLUS
) {
1034 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1035 c1
->c_text
= add (buf
, c1
->c_text
);
1036 fmt_appendcomp(bucket
, name
, buf
);
1039 c1
->c_flags
|= EXTRA
;
1045 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1046 if (c1
->c_flags
& CLEARTEXT
) {
1047 putcomp (c1
, c1
, ONECOMP
);
1051 !strcasecmp (c1
->c_name
, "messagename")) {
1052 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1054 putcomp (c1
, &holder
, ONECOMP
);
1055 free (holder
.c_text
);
1056 holder
.c_text
= NULL
;
1059 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1060 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1061 if (c2
->c_flags
& EXTRA
)
1062 putcomp (c1
, c2
, TWOCOMP
);
1065 if (dobody
&& (!c1
->c_name
||
1066 !strcasecmp (c1
->c_name
, "body"))) {
1067 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1068 formatproc
!= NULL
) {
1069 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1071 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1072 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1073 while (state
== BODY
) {
1074 putcomp (c1
, &holder
, BODYCOMP
);
1076 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1079 free (holder
.c_text
);
1080 holder
.c_text
= NULL
;
1084 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1085 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "",
1086 c1
->c_name
? c1
->c_name
: "")) {
1087 putcomp (c1
, c2
, ONECOMP
);
1088 if (!(c1
->c_flags
& SPLIT
))
1092 m_getfld_state_destroy (&gstate
);
1097 advise (NULL
, "format error in message %s", mname
);
1099 m_getfld_state_destroy (&gstate
);
1103 adios (NULL
, "getfld() returned %d", state
);
1110 mcomp_flags (char *name
)
1114 for (ap
= pairs
; ap
->p_name
; ap
++)
1115 if (!strcasecmp (ap
->p_name
, name
))
1116 return (ap
->p_flags
);
1123 mcomp_add (long flags
, char *s1
, char *s2
)
1127 if (!(flags
& ADDRFMT
))
1128 return add (s1
, s2
);
1130 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1133 return add (s1
, add (",\n", s2
));
1140 struct pqpair
*pq_next
;
1145 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1149 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1150 struct pqpair
*p
, *q
;
1152 struct mailname
*mp
;
1159 dat
[3] = sizeof(buffer
) - 1;
1162 if (!(c1
->c_flags
& ADDRFMT
)) {
1164 c1
->c_c_text
->c_text
= ap
;
1165 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1169 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1,
1171 /* Don't need to append a newline, dctime() already did */
1172 c2
->c_text
= getcpy (buffer
);
1174 /* ap is now owned by the component struct, so do NOT free it here */
1178 (q
= &pq
)->pq_next
= NULL
;
1179 while ((cp
= getname (ap
))) {
1180 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1181 adios (NULL
, "unable to allocate pqpair memory");
1183 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1184 p
->pq_text
= getcpy (cp
);
1185 p
->pq_error
= getcpy (error
);
1187 p
->pq_text
= getcpy (mp
->m_text
);
1190 q
= (q
->pq_next
= p
);
1193 for (p
= pq
.pq_next
; p
; p
= q
) {
1195 c1
->c_c_text
->c_text
= p
->pq_text
;
1198 if (c1
->c_c_error
) {
1199 c1
->c_c_error
->c_text
= p
->pq_error
;
1203 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1,
1207 c2
->c_text
= add (",\n", c2
->c_text
);
1208 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1210 c2
->c_text
= add (buffer
, c2
->c_text
);
1221 c2
->c_text
= add ("\n", c2
->c_text
);
1226 static struct mcomp
*
1227 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1231 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1232 adios (NULL
, "unable to allocate comp memory");
1234 c1
->c_flags
= flags
& ~INIT
;
1235 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1236 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1237 c1
->c_text
= text
? getcpy (text
) : NULL
;
1240 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1241 c1
->c_offset
= global
.c_offset
;
1242 c1
->c_ovoff
= global
. c_ovoff
;
1243 c1
->c_width
= c1
->c_length
= 0;
1244 c1
->c_cwidth
= global
.c_cwidth
;
1245 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1250 (*tail
)->c_next
= c1
;
1258 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1260 struct mcomp
*c1
, *c2
;
1262 for (c1
= *head
; c1
; c1
= c2
) {
1273 fmt_free (c1
->c_fmt
, 0);
1277 *head
= *tail
= NULL
;
1282 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1289 llim
= c1
->c_length
? c1
->c_length
: -1;
1290 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1291 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1293 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1295 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1296 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1297 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1300 if (c1
->c_flags
& CLEARTEXT
) {
1301 putstr (c1
->c_text
, c1
->c_flags
);
1302 putstr ("\n", c1
->c_flags
);
1306 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1307 mcomp_format (c1
, c2
);
1309 if (c1
->c_flags
& CENTER
) {
1310 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1311 - c1
->c_offset
- strlen (c2
->c_text
);
1312 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1313 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1314 lm
= c1
->c_offset
+ (count
/ 2);
1320 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1321 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1322 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1323 if (islower ((unsigned char) *cp
))
1324 *cp
= toupper ((unsigned char) *cp
);
1325 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1326 if (flag
!= BODYCOMP
) {
1327 putstr (": ", c1
->c_flags
);
1328 if (!(c1
->c_flags
& SPLIT
))
1329 c1
->c_flags
|= HDROUTPUT
;
1332 if ((count
= c1
->c_cwidth
-
1333 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1335 putstr (" ", c1
->c_flags
);
1338 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1342 && !(c2
->c_flags
& HDROUTPUT
)
1343 && !(c2
->c_flags
& NOCOMPONENT
)) {
1344 if (c1
->c_flags
& UPPERCASE
)
1345 for (cp
= c2
->c_name
; *cp
; cp
++)
1346 if (islower ((unsigned char) *cp
))
1347 *cp
= toupper ((unsigned char) *cp
);
1348 putstr (c2
->c_name
, c1
->c_flags
);
1349 putstr (": ", c1
->c_flags
);
1350 if (!(c1
->c_flags
& SPLIT
))
1351 c2
->c_flags
|= HDROUTPUT
;
1354 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1356 putstr (" ", c1
->c_flags
);
1358 if (c1
->c_flags
& UPPERCASE
)
1359 for (cp
= c2
->c_text
; *cp
; cp
++)
1360 if (islower ((unsigned char) *cp
))
1361 *cp
= toupper ((unsigned char) *cp
);
1365 if (flag
== TWOCOMP
)
1366 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1367 : (int) strlen (c2
->c_name
) + 2;
1369 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1370 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1372 count
+= c1
->c_offset
;
1374 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1375 putstr(cp
, c1
->c_flags
);
1377 putstr ("\n", c1
->c_flags
);
1378 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1380 if (flag
== BODYCOMP
1381 && !(c1
->c_flags
& NOCOMPONENT
))
1382 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1384 putstr (cp
, c1
->c_flags
);
1386 putstr ("\n", c1
->c_flags
);
1388 if (flag
== BODYCOMP
&& term
== '\n')
1389 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1394 oneline (char *stuff
, long flags
)
1402 return (onelp
= NULL
);
1406 if (flags
& COMPRESS
) {
1407 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1408 if (isspace ((unsigned char) *onelp
)) {
1409 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1428 while (*onelp
&& *onelp
!= '\n')
1430 if (*onelp
== '\n') {
1434 if (flags
& LEFTADJUST
)
1435 while (*ret
== ' ' || *ret
== '\t')
1438 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1446 putstr (char *string
, long flags
)
1448 if (!column
&& lm
> 0) {
1451 putch ('\t', flags
);
1461 putch (*string
++, flags
);
1466 putch (char ch
, long flags
)
1479 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1481 if (global
.c_flags
& BELL
)
1485 read (fileno (stdout
), buf
, sizeof(buf
));
1486 if (strchr(buf
, '\n')) {
1487 if (global
.c_flags
& CLEARSCR
)
1492 row
= global
.c_length
/ 3;
1511 * If we are forwarding this message, and the first
1512 * column contains a dash, then add a dash and a space.
1514 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1523 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1524 putch ('\n', flags
);
1527 putstr (ovtxt
? ovtxt
: "", flags
);
1543 longjmp (env
, DONE
);
1568 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1570 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1573 struct arglist
*a
, *a2
;
1575 switch (setjmp (mhlenv
)) {
1578 sleepsw
= 0; /* XXX */
1579 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1582 mhl_action
= action
;
1585 * If signal is at default action, then start ignoring
1586 * it, else let it set to its current action.
1588 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1589 SIGNAL (SIGINT
, istat
);
1590 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1591 SIGNAL (SIGQUIT
, qstat
);
1592 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1593 mhl (argc
, argv
); /* FALL THROUGH! */
1596 SIGNAL (SIGINT
, istat
);
1597 SIGNAL (SIGQUIT
, qstat
);
1598 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1601 SIGNAL (SIGPIPE
, pstat
);
1603 if (holder
.c_text
) {
1604 free (holder
.c_text
);
1605 holder
.c_text
= NULL
;
1607 free_queue (&msghd
, &msgtl
);
1608 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1609 c1
->c_flags
&= ~HDROUTPUT
;
1627 mhladios (char *what
, char *fmt
, ...)
1632 advertise (what
, NULL
, fmt
, ap
);
1639 mhldone (int status
)
1643 longjmp (mhlenv
, DONE
);
1649 static int m_pid
= NOTOK
;
1650 static int sd
= NOTOK
;
1653 m_popen (char *name
)
1659 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1660 adios ("standard output", "unable to dup()");
1662 if (pipe (pd
) == NOTOK
)
1663 adios ("pipe", "unable to");
1665 switch (m_pid
= fork()) {
1667 adios ("fork", "unable to");
1670 SIGNAL (SIGINT
, SIG_DFL
);
1671 SIGNAL (SIGQUIT
, SIG_DFL
);
1674 if (pd
[0] != fileno (stdin
)) {
1675 dup2 (pd
[0], fileno (stdin
));
1678 arglist
= argsplit(name
, &file
, NULL
);
1679 execvp (file
, arglist
);
1680 fprintf (stderr
, "unable to exec ");
1686 if (pd
[1] != fileno (stdout
)) {
1687 dup2 (pd
[1], fileno (stdout
));
1702 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1703 adios ("standard output", "unable to dup2()");
1712 pidwait (m_pid
, OK
);
1718 * Compile a format string used by the formatfield option and save it
1721 * We will want the {text} (and possibly {error}) components for later,
1722 * so look for them and save them if we find them.
1726 compile_formatfield(struct mcomp
*c1
)
1728 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1731 * As a note to myself and any other poor bastard who is looking through
1732 * this code in the future ....
1734 * When the format hash table is reset later on (as it almost certainly
1735 * will be), there will still be references to these components in the
1736 * compiled format instructions. Thus these component references will
1737 * be free'd when the format instructions are free'd (by fmt_free()).
1739 * So, in other words ... don't go free'ing them yourself!
1742 c1
->c_c_text
= fmt_findcomp("text");
1743 c1
->c_c_error
= fmt_findcomp("error");
1747 * Compile all of the arguments for our format list.
1749 * Iterate through the linked list of format strings and compile them.
1750 * Note that we reset the format hash table before we start, but we do NOT
1751 * reset it between calls to fmt_compile().
1756 compile_filterargs (void)
1758 struct arglist
*arg
= arglist_head
;
1765 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1770 * Search through and mark any components that are address components
1773 for (ap
= addrcomps
; *ap
; ap
++) {
1774 cptr
= fmt_findcomp (*ap
);
1776 cptr
->c_type
|= CT_ADDR
;
1781 * Filter the body of a message through a specified format program
1785 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1786 m_getfld_state_t gstate
)
1788 struct mcomp holder
;
1790 int fdinput
[2], fdoutput
[2], waitstat
;
1792 pid_t writerpid
, filterpid
;
1795 * Create pipes so we can communicate with our filter process.
1798 if (pipe(fdinput
) < 0) {
1799 adios(NULL
, "Unable to create input pipe");
1802 if (pipe(fdoutput
) < 0) {
1803 adios(NULL
, "Unable to create output pipe");
1807 * Here's what we're doing to do.
1809 * - Fork ourselves and start writing data to the write side of the
1810 * input pipe (fdinput[1]).
1812 * - Fork and exec our filter program. We set the standard input of
1813 * our filter program to be the read side of our input pipe (fdinput[0]).
1814 * Standard output is set to the write side of our output pipe
1817 * - We read from the read side of the output pipe (fdoutput[0]).
1819 * We're forking because that's the simplest way to prevent any deadlocks.
1820 * (without doing something like switching to non-blocking I/O and using
1821 * select or poll, and I'm not interested in doing that).
1824 switch (writerpid
= fork()) {
1827 * Our child process - just write to the filter input (fdinput[1]).
1828 * Close all other descriptors that we don't need.
1836 * Call m_getfld() until we're no longer in the BODY state
1839 while (state
== BODY
) {
1841 write(fdinput
[1], buf
, strlen(buf
));
1842 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1846 * We should be done; time to exit.
1851 * Make sure we call _exit(), otherwise we may flush out the stdio
1852 * buffers that we have duplicated from the parent.
1856 adios(NULL
, "Unable to fork for filter writer process");
1861 * Fork and exec() our filter program, after redirecting standard in
1862 * and standard out appropriately.
1865 switch (filterpid
= fork()) {
1866 char **args
, *program
;
1868 int i
, dat
[5], s
, argp
;
1872 * Configure an argument array for us
1875 args
= argsplit(formatproc
, &program
, &argp
);
1876 args
[argp
+ filter_nargs
] = NULL
;
1884 * Pull out each argument and scan them.
1887 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1888 args
[i
] = mh_xmalloc(BUFSIZ
);
1889 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
- 1, BUFSIZ
, dat
, NULL
);
1891 * fmt_scan likes to put a trailing newline at the end of the
1892 * format string. If we have one, get rid of it.
1894 s
= strlen(args
[i
]);
1895 if (args
[i
][s
- 1] == '\n')
1896 args
[i
][s
- 1] = '\0';
1899 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1903 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1904 adios("formatproc", "Unable to dup2() standard input");
1906 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1907 adios("formatproc", "Unable to dup2() standard output");
1911 * Close everything (especially the old input and output
1912 * descriptors, since they've been dup'd to stdin and stdout),
1913 * and exec the formatproc.
1921 execvp(formatproc
, args
);
1923 adios(formatproc
, "Unable to execute filter");
1928 adios(NULL
, "Unable to fork format program");
1932 * Close everything except our reader (fdoutput[0]);
1940 * As we read in this data, send it to putcomp
1943 holder
.c_text
= buf
;
1945 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1947 putcomp(c1
, &holder
, BODYCOMP
);
1951 adios(NULL
, "reading from formatproc");
1955 * See if we got any errors along the way. I'm a little leery of calling
1956 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1959 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1960 if (errno
!= ECHILD
) {
1961 adios("filterproc", "Unable to determine status");
1964 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1965 pidstatus(waitstat
, stderr
, "filterproc");
1969 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1970 if (errno
!= ECHILD
) {
1971 adios("writer process", "Unable to determine status");
1975 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1976 pidstatus(waitstat
, stderr
, "writer process");