]>
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 filterbody (struct mcomp
*, char *, int, int, FILE *,
337 static void compile_formatfield(struct mcomp
*);
338 static void compile_filterargs (void);
342 mhl (int argc
, char **argv
)
344 int length
= 0, nomore
= 0;
345 unsigned int i
, vecp
= 0;
347 char *cp
, *folder
= NULL
, *form
= NULL
;
348 char buf
[BUFSIZ
], *files
[MAXARGS
];
349 char **argp
, **arguments
;
351 /* Need this if called from main() of show(1). */
352 invo_name
= r1bindex (argv
[0], '/');
354 arguments
= getarguments (invo_name
, argc
, argv
, 1);
357 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
360 while ((cp
= *argp
++)) {
362 switch (smatch (++cp
, mhlswitches
)) {
364 ambigsw (cp
, mhlswitches
);
367 adios (NULL
, "-%s unknown\n", cp
);
370 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
371 print_help (buf
, mhlswitches
, 1);
374 print_version(invo_name
);
392 if (!(folder
= *argp
++) || *folder
== '-')
393 adios (NULL
, "missing argument to %s", argp
[-2]);
396 if (!(form
= *argp
++) || *form
== '-')
397 adios (NULL
, "missing argument to %s", argp
[-2]);
401 if (!(cp
= *argp
++) || *cp
== '-')
402 adios (NULL
, "missing argument to %s", argp
[-2]);
403 sleepsw
= atoi (cp
);/* ZERO ok! */
407 if (!(moreproc
= *argp
++) || *moreproc
== '-')
408 adios (NULL
, "missing argument to %s", argp
[-2]);
415 if (!(formatproc
= *argp
++) || *formatproc
== '-')
416 adios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(cp
= *argp
++) || *cp
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
425 if ((length
= atoi (cp
)) < 1)
426 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
429 if (!(cp
= *argp
++) || *cp
== '-')
430 adios (NULL
, "missing argument to %s", argp
[-2]);
431 if ((width
= atoi (cp
)) < 1)
432 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
436 if (!(digest
= *argp
++) || *digest
== '-')
437 adios (NULL
, "missing argument to %s", argp
[-2]);
440 if (!(cp
= *argp
++) || *cp
== '-')
441 adios (NULL
, "missing argument to %s", argp
[-2]);
442 if ((issue
= atoi (cp
)) < 1)
443 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((volume
= atoi (cp
)) < 1)
449 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
453 forwall
++; /* fall */
456 clearflg
= -1;/* XXX */
460 dashstuff
= 1; /* trinary logic */
463 dashstuff
= -1; /* trinary logic */
475 folder
= getenv ("mhfolder");
477 if (isatty (fileno (stdout
))) {
478 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
480 SIGNAL (SIGINT
, SIG_IGN
);
481 SIGNAL2 (SIGQUIT
, quitser
);
483 SIGNAL2 (SIGPIPE
, pipeser
);
484 m_popen (moreproc
, mhl_action
!= NULL
);
487 SIGNAL (SIGINT
, SIG_IGN
);
488 SIGNAL2 (SIGQUIT
, quitser
);
495 mhl_format (form
? form
: mhlformat
, length
, width
);
498 process (folder
, NULL
, 1, vecp
= 1);
500 for (i
= 0; i
< vecp
; i
++)
501 process (folder
, files
[i
], i
+ 1, vecp
);
506 printf ("%s", delim4
);
508 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
510 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
511 digest
, volume
, issue
);
514 for (cp
= buf
+ i
; i
> 1; i
--)
521 printf ("\n------- End of Forwarded Message%s\n",
522 vecp
> 1 ? "s" : "");
527 adios("output", "error writing");
530 if (clearflg
> 0 && ontty
== NOTTY
)
541 mhl_format (char *file
, int length
, int width
)
545 char *ap
, name
[NAMESZ
];
549 static dev_t dev
= 0;
550 static ino_t ino
= 0;
551 static time_t mtime
= 0;
554 if (stat (etcpath (file
), &st
) != NOTOK
555 && mtime
== st
.st_mtime
560 free_queue (&fmthd
, &fmttl
);
563 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
564 adios (file
, "unable to open format file");
566 if (fstat (fileno (fp
), &st
) != NOTOK
) {
572 global
.c_ovtxt
= global
.c_nfs
= NULL
;
576 if ((i
= sc_width ()) > 5)
578 global
.c_cwidth
= -1;
579 if ((i
= sc_length ()) > 5)
580 global
.c_length
= i
- 1;
581 global
.c_flags
= BELL
; /* BELL is default */
582 *(ip
= ignores
) = NULL
;
585 while (vfgets (fp
, &ap
) == OK
) {
590 if ((cp
= strchr(bp
, '\n')))
594 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
599 strncpy (name
, parse(), sizeof(name
));
605 * Split this list of fields to ignore, and copy
606 * it to the end of the current "ignores" list.
608 if (!strcasecmp (name
, "ignores")) {
609 char **tmparray
, **p
;
612 /* split the fields */
613 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
615 /* count number of fields split */
620 /* copy pointers to split fields to ignores array */
621 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
627 if (evalvar (&global
))
628 adios (NULL
, "format file syntax error: %s", bp
);
635 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
636 while (*parptr
== ':' || *parptr
== ',') {
639 adios (NULL
, "format file syntax error: %s", bp
);
641 if (!c1
->c_nfs
&& global
.c_nfs
) {
642 if (c1
->c_flags
& DATEFMT
) {
643 if (global
.c_flags
& DATEFMT
) {
644 c1
->c_nfs
= getcpy (global
.c_nfs
);
645 compile_formatfield(c1
);
649 if (c1
->c_flags
& ADDRFMT
) {
650 if (global
.c_flags
& ADDRFMT
) {
651 c1
->c_nfs
= getcpy (global
.c_nfs
);
652 compile_formatfield(c1
);
659 adios (NULL
, "format file syntax error: %s", bp
);
665 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
668 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
669 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
670 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
671 (unsigned int)(unsigned long) c1
->c_nfs
,
672 (unsigned int)(unsigned long) c1
->c_fmt
);
673 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
674 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
675 c1
->c_cwidth
, c1
->c_length
);
676 fprintf (stderr
, "\tflags=%s\n",
677 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
683 global
.c_flags
|= CLEARSCR
;
686 global
.c_flags
&= ~CLEARSCR
;
689 switch (bellflg
) { /* command line may override format file */
691 global
.c_flags
|= BELL
;
694 global
.c_flags
&= ~BELL
;
699 global
.c_length
= length
;
701 global
.c_width
= width
;
702 if (global
.c_length
< 5)
703 global
.c_length
= 10000;
704 if (global
.c_width
< 5)
705 global
.c_width
= 10000;
710 evalvar (struct mcomp
*c1
)
712 char *cp
, name
[NAMESZ
];
717 strncpy (name
, parse(), sizeof(name
));
719 if (!strcasecmp (name
, "component")) {
720 if (ptos (name
, &c1
->c_text
))
722 c1
->c_flags
&= ~NOCOMPONENT
;
726 if (!strcasecmp (name
, "overflowtext"))
727 return ptos (name
, &c1
->c_ovtxt
);
729 if (!strcasecmp (name
, "formatfield")) {
730 if (ptos (name
, &cp
))
732 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
733 compile_formatfield(c1
);
734 c1
->c_flags
|= FORMAT
;
738 if (!strcasecmp (name
, "decode")) {
739 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
740 compile_formatfield(c1
);
741 c1
->c_flags
|= FORMAT
;
745 if (!strcasecmp (name
, "offset"))
746 return ptoi (name
, &c1
->c_offset
);
747 if (!strcasecmp (name
, "overflowoffset"))
748 return ptoi (name
, &c1
->c_ovoff
);
749 if (!strcasecmp (name
, "width"))
750 return ptoi (name
, &c1
->c_width
);
751 if (!strcasecmp (name
, "compwidth"))
752 return ptoi (name
, &c1
->c_cwidth
);
753 if (!strcasecmp (name
, "length"))
754 return ptoi (name
, &c1
->c_length
);
755 if (!strcasecmp (name
, "nodashstuffing"))
756 return (dashstuff
= -1);
758 for (ap
= triples
; ap
->t_name
; ap
++)
759 if (!strcasecmp (ap
->t_name
, name
)) {
760 c1
->c_flags
|= ap
->t_on
;
761 c1
->c_flags
&= ~ap
->t_off
;
765 if (!strcasecmp (name
, "formatarg")) {
766 struct arglist
*args
;
768 if (ptos (name
, &cp
))
771 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
772 advise (NULL
, "format filters are currently only supported on "
773 "the \"body\" component");
777 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
780 arglist_tail
->a_next
= args
;
787 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
798 ptoi (char *name
, int *i
)
802 if (*parptr
++ != '=' || !*(cp
= parse ())) {
803 advise (NULL
, "missing argument to variable %s", name
);
813 ptos (char *name
, char **s
)
817 if (*parptr
++ != '=') {
818 advise (NULL
, "missing argument to variable %s", name
);
822 if (*parptr
!= '"') {
824 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
828 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
829 if (*parptr
== QUOTE
)
836 if ((*parptr
= c
) == '"')
847 static char result
[NAMESZ
];
849 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
868 * Process one file/message
872 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
879 /* volatile to prevent "might be clobbered" warning from gcc: */
880 char *volatile fname2
= fname
? fname
: "(stdin)";
882 switch (setjmp (env
)) {
885 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
887 advise (fname
, "unable to open");
894 if (fstat(fileno(fp
), &st
) == 0) {
895 filesize
= st
.st_size
;
899 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : getcpy (fname2
);
901 SIGNAL (SIGINT
, intrser
);
902 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
904 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
905 fmt_free(ap
->a_fmt
, 0);
914 SIGNAL (SIGINT
, SIG_IGN
);
915 if (mhl_action
== NULL
&& fp
!= stdin
)
919 free (holder
.c_text
);
920 holder
.c_text
= NULL
;
922 free_queue (&msghd
, &msgtl
);
923 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
924 c1
->c_flags
&= ~HDROUTPUT
;
932 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
935 struct mcomp
*c1
, *c2
, *c3
;
936 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
937 m_getfld_state_t gstate
= 0;
939 compile_filterargs();
943 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
945 printf ("\n-------");
947 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
949 printf (" Message %d", ofilen
);
957 if ((global
.c_flags
& CLEARSCR
))
962 printf (">>> %s\n\n", mname
);
967 strncpy (buf
, "\n", sizeof(buf
));
969 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
972 printf ("Press <return> to list \"%s\"...", mname
);
976 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
977 advise ("stdout", "read");
980 if (strchr(buf
, '\n')) {
981 if ((global
.c_flags
& CLEARSCR
))
995 printf (">>> %s\n\n", mname
);
1002 int bufsz
= sizeof buf
;
1003 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1006 bucket
= fmt_addcomptext(name
, buf
);
1007 for (ip
= ignores
; *ip
; ip
++)
1008 if (!strcasecmp (name
, *ip
)) {
1009 while (state
== FLDPLUS
) {
1011 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1012 fmt_appendcomp(bucket
, name
, buf
);
1019 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1020 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "", name
))
1023 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1024 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1025 if (!strcasecmp (c1
->c_name
? c1
->c_name
: "",
1026 c3
->c_name
? c3
->c_name
: "")) {
1028 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1032 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1033 while (state
== FLDPLUS
) {
1035 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1036 c1
->c_text
= add (buf
, c1
->c_text
);
1037 fmt_appendcomp(bucket
, name
, buf
);
1040 c1
->c_flags
|= EXTRA
;
1046 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1047 if (c1
->c_flags
& CLEARTEXT
) {
1048 putcomp (c1
, c1
, ONECOMP
);
1052 !strcasecmp (c1
->c_name
, "messagename")) {
1053 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1055 putcomp (c1
, &holder
, ONECOMP
);
1056 free (holder
.c_text
);
1057 holder
.c_text
= NULL
;
1060 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1061 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1062 if (c2
->c_flags
& EXTRA
)
1063 putcomp (c1
, c2
, TWOCOMP
);
1066 if (dobody
&& (!c1
->c_name
||
1067 !strcasecmp (c1
->c_name
, "body"))) {
1068 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1069 formatproc
!= NULL
) {
1070 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1072 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1073 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1074 while (state
== BODY
) {
1075 putcomp (c1
, &holder
, BODYCOMP
);
1077 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1080 free (holder
.c_text
);
1081 holder
.c_text
= NULL
;
1085 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1086 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "",
1087 c1
->c_name
? c1
->c_name
: "")) {
1088 putcomp (c1
, c2
, ONECOMP
);
1089 if (!(c1
->c_flags
& SPLIT
))
1093 m_getfld_state_destroy (&gstate
);
1098 advise (NULL
, "format error in message %s", mname
);
1100 m_getfld_state_destroy (&gstate
);
1104 adios (NULL
, "getfld() returned %d", state
);
1111 mcomp_flags (char *name
)
1115 for (ap
= pairs
; ap
->p_name
; ap
++)
1116 if (!strcasecmp (ap
->p_name
, name
))
1117 return (ap
->p_flags
);
1124 mcomp_add (long flags
, char *s1
, char *s2
)
1128 if (!(flags
& ADDRFMT
))
1129 return add (s1
, s2
);
1131 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1134 return add (s1
, add (",\n", s2
));
1141 struct pqpair
*pq_next
;
1146 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1151 struct pqpair
*p
, *q
;
1153 struct mailname
*mp
;
1160 dat
[3] = BUFSIZ
- 1;
1163 if (!(c1
->c_flags
& ADDRFMT
)) {
1164 charstring_t scanl
= charstring_create (BUFSIZ
);
1167 c1
->c_c_text
->c_text
= ap
;
1168 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1172 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1173 /* Don't need to append a newline, dctime() already did */
1174 c2
->c_text
= charstring_buffer_copy (scanl
);
1175 charstring_free (scanl
);
1177 /* ap is now owned by the component struct, so do NOT free it here */
1181 (q
= &pq
)->pq_next
= NULL
;
1182 while ((cp
= getname (ap
))) {
1183 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1184 adios (NULL
, "unable to allocate pqpair memory");
1186 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1187 p
->pq_text
= getcpy (cp
);
1188 p
->pq_error
= getcpy (error
);
1190 p
->pq_text
= getcpy (mp
->m_text
);
1193 q
= (q
->pq_next
= p
);
1196 for (p
= pq
.pq_next
; p
; p
= q
) {
1197 charstring_t scanl
= charstring_create (BUFSIZ
);
1201 c1
->c_c_text
->c_text
= p
->pq_text
;
1204 if (c1
->c_c_error
) {
1205 c1
->c_c_error
->c_text
= p
->pq_error
;
1209 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1210 buffer
= charstring_buffer_copy (scanl
);
1211 if (strlen (buffer
) > 0) {
1213 c2
->c_text
= add (",\n", c2
->c_text
);
1214 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1216 c2
->c_text
= add (buffer
, c2
->c_text
);
1218 charstring_free (scanl
);
1228 c2
->c_text
= add ("\n", c2
->c_text
);
1233 static struct mcomp
*
1234 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1238 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1239 adios (NULL
, "unable to allocate comp memory");
1241 c1
->c_flags
= flags
& ~INIT
;
1242 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1243 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1244 c1
->c_text
= text
? getcpy (text
) : NULL
;
1247 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1248 c1
->c_offset
= global
.c_offset
;
1249 c1
->c_ovoff
= global
. c_ovoff
;
1250 c1
->c_width
= c1
->c_length
= 0;
1251 c1
->c_cwidth
= global
.c_cwidth
;
1252 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1257 (*tail
)->c_next
= c1
;
1265 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1267 struct mcomp
*c1
, *c2
;
1269 for (c1
= *head
; c1
; c1
= c2
) {
1280 fmt_free (c1
->c_fmt
, 0);
1284 *head
= *tail
= NULL
;
1289 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1296 llim
= c1
->c_length
? c1
->c_length
: -1;
1297 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1298 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1300 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1302 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1303 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1304 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1307 if (c1
->c_flags
& CLEARTEXT
) {
1308 putstr (c1
->c_text
, c1
->c_flags
);
1309 putstr ("\n", c1
->c_flags
);
1313 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1314 mcomp_format (c1
, c2
);
1316 if (c1
->c_flags
& CENTER
) {
1317 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1318 - c1
->c_offset
- strlen (c2
->c_text
);
1319 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1320 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1321 lm
= c1
->c_offset
+ (count
/ 2);
1327 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1328 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1329 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1330 if (islower ((unsigned char) *cp
))
1331 *cp
= toupper ((unsigned char) *cp
);
1332 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1333 if (flag
!= BODYCOMP
) {
1334 putstr (": ", c1
->c_flags
);
1335 if (!(c1
->c_flags
& SPLIT
))
1336 c1
->c_flags
|= HDROUTPUT
;
1339 if ((count
= c1
->c_cwidth
-
1340 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1342 putstr (" ", c1
->c_flags
);
1345 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1349 && !(c2
->c_flags
& HDROUTPUT
)
1350 && !(c2
->c_flags
& NOCOMPONENT
)) {
1351 if (c1
->c_flags
& UPPERCASE
)
1352 for (cp
= c2
->c_name
; *cp
; cp
++)
1353 if (islower ((unsigned char) *cp
))
1354 *cp
= toupper ((unsigned char) *cp
);
1355 putstr (c2
->c_name
, c1
->c_flags
);
1356 putstr (": ", c1
->c_flags
);
1357 if (!(c1
->c_flags
& SPLIT
))
1358 c2
->c_flags
|= HDROUTPUT
;
1361 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1363 putstr (" ", c1
->c_flags
);
1365 if (c1
->c_flags
& UPPERCASE
)
1366 for (cp
= c2
->c_text
; *cp
; cp
++)
1367 if (islower ((unsigned char) *cp
))
1368 *cp
= toupper ((unsigned char) *cp
);
1372 if (flag
== TWOCOMP
)
1373 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1374 : (int) strlen (c2
->c_name
) + 2;
1376 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1377 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1379 count
+= c1
->c_offset
;
1381 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1382 putstr(cp
, c1
->c_flags
);
1384 putstr ("\n", c1
->c_flags
);
1385 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1387 if (flag
== BODYCOMP
1388 && !(c1
->c_flags
& NOCOMPONENT
))
1389 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1391 putstr (cp
, c1
->c_flags
);
1393 putstr ("\n", c1
->c_flags
);
1395 if (flag
== BODYCOMP
&& term
== '\n')
1396 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1401 oneline (char *stuff
, long flags
)
1409 return (onelp
= NULL
);
1413 if (flags
& COMPRESS
) {
1414 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1415 if (isspace ((unsigned char) *onelp
)) {
1416 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1435 while (*onelp
&& *onelp
!= '\n')
1437 if (*onelp
== '\n') {
1441 if (flags
& LEFTADJUST
)
1442 while (*ret
== ' ' || *ret
== '\t')
1445 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1453 putstr (char *string
, long flags
)
1455 if (!column
&& lm
> 0) {
1458 putch ('\t', flags
);
1468 putch (*string
++, flags
);
1473 putch (char ch
, long flags
)
1486 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1488 if (global
.c_flags
& BELL
)
1492 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
1493 advise ("stdout", "read");
1495 if (strchr(buf
, '\n')) {
1496 if (global
.c_flags
& CLEARSCR
)
1497 nmh_clear_screen ();
1501 row
= global
.c_length
/ 3;
1520 * If we are forwarding this message, and the first
1521 * column contains a dash, then add a dash and a space.
1523 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1532 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1533 putch ('\n', flags
);
1536 putstr (ovtxt
? ovtxt
: "", flags
);
1552 longjmp (env
, DONE
);
1580 mhladios (char *what
, char *fmt
, ...)
1585 advertise (what
, NULL
, fmt
, ap
);
1592 mhldone (int status
)
1596 longjmp (mhlenv
, DONE
);
1603 * Compile a format string used by the formatfield option and save it
1606 * We will want the {text} (and possibly {error}) components for later,
1607 * so look for them and save them if we find them.
1611 compile_formatfield(struct mcomp
*c1
)
1613 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1616 * As a note to myself and any other poor bastard who is looking through
1617 * this code in the future ....
1619 * When the format hash table is reset later on (as it almost certainly
1620 * will be), there will still be references to these components in the
1621 * compiled format instructions. Thus these component references will
1622 * be free'd when the format instructions are free'd (by fmt_free()).
1624 * So, in other words ... don't go free'ing them yourself!
1627 c1
->c_c_text
= fmt_findcomp("text");
1628 c1
->c_c_error
= fmt_findcomp("error");
1632 * Compile all of the arguments for our format list.
1634 * Iterate through the linked list of format strings and compile them.
1635 * Note that we reset the format hash table before we start, but we do NOT
1636 * reset it between calls to fmt_compile().
1641 compile_filterargs (void)
1643 struct arglist
*arg
= arglist_head
;
1650 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1655 * Search through and mark any components that are address components
1658 for (ap
= addrcomps
; *ap
; ap
++) {
1659 cptr
= fmt_findcomp (*ap
);
1661 cptr
->c_type
|= CT_ADDR
;
1666 * Filter the body of a message through a specified format program
1670 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1671 m_getfld_state_t gstate
)
1673 struct mcomp holder
;
1675 int fdinput
[2], fdoutput
[2], waitstat
;
1677 pid_t writerpid
, filterpid
;
1680 * Create pipes so we can communicate with our filter process.
1683 if (pipe(fdinput
) < 0) {
1684 adios(NULL
, "Unable to create input pipe");
1687 if (pipe(fdoutput
) < 0) {
1688 adios(NULL
, "Unable to create output pipe");
1692 * Here's what we're doing to do.
1694 * - Fork ourselves and start writing data to the write side of the
1695 * input pipe (fdinput[1]).
1697 * - Fork and exec our filter program. We set the standard input of
1698 * our filter program to be the read side of our input pipe (fdinput[0]).
1699 * Standard output is set to the write side of our output pipe
1702 * - We read from the read side of the output pipe (fdoutput[0]).
1704 * We're forking because that's the simplest way to prevent any deadlocks.
1705 * (without doing something like switching to non-blocking I/O and using
1706 * select or poll, and I'm not interested in doing that).
1709 switch (writerpid
= fork()) {
1712 * Our child process - just write to the filter input (fdinput[1]).
1713 * Close all other descriptors that we don't need.
1721 * Call m_getfld() until we're no longer in the BODY state
1724 while (state
== BODY
) {
1726 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1727 advise ("pipe output", "write");
1729 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1733 * We should be done; time to exit.
1738 * Make sure we call _exit(), otherwise we may flush out the stdio
1739 * buffers that we have duplicated from the parent.
1743 adios(NULL
, "Unable to fork for filter writer process");
1748 * Fork and exec() our filter program, after redirecting standard in
1749 * and standard out appropriately.
1752 switch (filterpid
= fork()) {
1753 char **args
, *program
;
1755 int i
, dat
[5], s
, argp
;
1759 * Configure an argument array for us
1762 args
= argsplit(formatproc
, &program
, &argp
);
1763 args
[argp
+ filter_nargs
] = NULL
;
1771 * Pull out each argument and scan them.
1774 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1775 charstring_t scanl
= charstring_create (BUFSIZ
);
1777 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1778 args
[i
] = charstring_buffer_copy (scanl
);
1779 charstring_free (scanl
);
1781 * fmt_scan likes to put a trailing newline at the end of the
1782 * format string. If we have one, get rid of it.
1784 s
= strlen(args
[i
]);
1785 if (args
[i
][s
- 1] == '\n')
1786 args
[i
][s
- 1] = '\0';
1789 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1793 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1794 adios("formatproc", "Unable to dup2() standard input");
1796 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1797 adios("formatproc", "Unable to dup2() standard output");
1801 * Close everything (especially the old input and output
1802 * descriptors, since they've been dup'd to stdin and stdout),
1803 * and exec the formatproc.
1811 execvp(formatproc
, args
);
1813 adios(formatproc
, "Unable to execute filter");
1818 adios(NULL
, "Unable to fork format program");
1822 * Close everything except our reader (fdoutput[0]);
1830 * As we read in this data, send it to putcomp
1833 holder
.c_text
= buf
;
1835 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1837 putcomp(c1
, &holder
, BODYCOMP
);
1841 adios(NULL
, "reading from formatproc");
1845 * See if we got any errors along the way. I'm a little leery of calling
1846 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1849 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1850 if (errno
!= ECHILD
) {
1851 adios("filterproc", "Unable to determine status");
1854 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1855 pidstatus(waitstat
, stderr
, "filterproc");
1859 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1860 if (errno
!= ECHILD
) {
1861 adios("writer process", "Unable to determine status");
1865 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1866 pidstatus(waitstat
, stderr
, "writer process");