]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
1 /* mhlsbr.c -- main routines for nmh message lister
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/concat.h"
10 #include "sbr/smatch.h"
11 #include "sbr/r1bindex.h"
12 #include "sbr/snprintb.h"
13 #include "sbr/copyip.h"
14 #include "sbr/discard.h"
15 #include "sbr/trimcpy.h"
16 #include "sbr/vfgets.h"
17 #include "sbr/check_charset.h"
18 #include "sbr/getcpy.h"
19 #include "sbr/brkstring.h"
20 #include "sbr/ambigsw.h"
21 #include "sbr/pidstatus.h"
22 #include "sbr/print_version.h"
23 #include "sbr/print_help.h"
24 #include "sbr/arglist.h"
25 #include "sbr/error.h"
26 #include "h/signals.h"
27 #include "h/addrsbr.h"
28 #include "h/fmt_scan.h"
32 #include "sbr/m_popen.h"
34 #include <sys/types.h>
35 #include "sbr/terminal.h"
39 * for a component containing addresses, ADDRFMT, if COMPRESS is also
40 * set, then addresses get split wrong (not at the spaces between commas).
41 * To fix this correctly, putstr() should know about "atomic" strings that
42 * must NOT be broken across lines. That's too difficult for right now
43 * (it turns out that there are a number of degenerate cases), so in
44 * oneline(), instead of
46 * (*onelp == '\n' && !onelp[1])
48 * being a terminating condition,
50 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
52 * is used instead. This cuts the line prematurely, and gives us a much
53 * better chance of getting things right.
62 #define MHL_SWITCHES \
63 X("bell", 0, BELLSW) \
64 X("nobell", 0, NBELLSW) \
65 X("clear", 0, CLRSW) \
66 X("noclear", 0, NCLRSW) \
67 X("folder +folder", 0, FOLDSW) \
68 X("form formfile", 0, FORMSW) \
69 X("moreproc program", 0, PROGSW) \
70 X("nomoreproc", 0, NPROGSW) \
71 X("length lines", 0, LENSW) \
72 X("width columns", 0, WIDTHSW) \
73 X("sleep seconds", 0, SLEEPSW) \
74 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
75 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
76 X("version", 0, VERSIONSW) \
77 X("help", 0, HELPSW) \
78 X("forward", -7, FORW1SW) /* interface from forw */ \
79 X("forwall", -7, FORW2SW) /* interface from forw */ \
80 X("digest list", -6, DGSTSW) \
81 X("volume number", -6, VOLUMSW) \
82 X("issue number", -5, ISSUESW) \
83 X("nobody", -6, NBODYSW) \
84 X("fmtproc program", 0, FMTPROCSW) \
85 X("nofmtproc", 0, NFMTPROCSW) \
87 #define X(sw, minchars, id) id,
88 DEFINE_SWITCH_ENUM(MHL
);
91 #define X(sw, minchars, id) { sw, minchars, id },
92 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
95 #define NOCOMPONENT 0x000001 /* don't show component name */
96 #define UPPERCASE 0x000002 /* display in all upper case */
97 #define CENTER 0x000004 /* center line */
98 #define CLEARTEXT 0x000008 /* cleartext */
99 #define EXTRA 0x000010 /* an "extra" component */
100 #define HDROUTPUT 0x000020 /* already output */
101 #define CLEARSCR 0x000040 /* clear screen */
102 #define LEFTADJUST 0x000080 /* left justify multiple lines */
103 #define COMPRESS 0x000100 /* compress text */
104 #define ADDRFMT 0x000200 /* contains addresses */
105 #define BELL 0x000400 /* sound bell at EOP */
106 #define DATEFMT 0x000800 /* contains dates */
107 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
108 #define INIT 0x002000 /* initialize component */
109 #define RTRIM 0x004000 /* trim trailing whitespace */
110 #define SPLIT 0x010000 /* split headers (don't concatenate) */
111 #define NONEWLINE 0x020000 /* don't write trailing newline */
112 #define NOWRAP 0x040000 /* Don't wrap lines ever */
113 #define FMTFILTER 0x080000 /* Filter through format filter */
114 #define INVISIBLE 0x100000 /* count byte in display columns? */
115 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
116 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
117 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
120 * A format string to be used as a command-line argument to the body
125 struct format
*a_fmt
;
127 struct arglist
*a_next
;
131 * Linked list of command line arguments for the body format filter. This
132 * USED to be in "struct mcomp", but the format API got cleaned up and even
133 * though it reduced the code we had to do, it make things more complicated
134 * for us. Specifically:
136 * - The interface to the hash table has been cleaned up, which means the
137 * rooting around in the hash table is no longer necessary (yay!). But
138 * this ALSO means that we have to make sure that we call our format
139 * compilation routines before we process the message, because the
140 * components need to be visible in the hash table so we can save them for
141 * later. So we moved them out of "mcomp" and now compile them right before
142 * header processing starts.
143 * - We also use format strings to handle other components in the mhl
144 * configuration (using "formatfield" and "decode"), but here life
145 * gets complicated: they aren't dealt with in the normal way. Instead
146 * of referring to a component like {from}, each component is processed
147 * using the special {text} component. But these format strings need to be
148 * compiled BEFORE we compile the format arguments; in the previous
149 * implementation they were compiled and scanned as the headers were
150 * read, and that would reset the hash table that we need to populate
151 * the components used by the body format filter. So we are compiling
152 * the formatfield component strings ahead of time and then scanning them
155 * Okay, fine ... this was broken before. But you know what? Fixing this
156 * the right way will make things easier down the road.
158 * One side-effect to this change: format strings are now compiled only once
159 * for components specified with "formatfield", but they are compiled for
160 * every message for format arguments.
163 static struct arglist
*arglist_head
;
164 static struct arglist
*arglist_tail
;
165 static int filter_nargs
= 0;
168 * Flags/options for each component
172 char *c_name
; /* component name */
173 char *c_text
; /* component text */
174 char *c_ovtxt
; /* text overflow indicator */
175 char *c_nfs
; /* iff FORMAT */
176 struct format
*c_fmt
; /* .. */
177 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
178 struct comp
*c_c_error
; /* Ref to {error} */
179 int c_offset
; /* left margin indentation */
180 int c_ovoff
; /* overflow indentation */
181 int c_width
; /* width of field */
182 int c_cwidth
; /* width of component */
183 int c_length
; /* length in lines */
184 unsigned long c_flags
;
185 struct mcomp
*c_next
;
188 static struct mcomp
*msghd
= NULL
;
189 static struct mcomp
*msgtl
= NULL
;
190 static struct mcomp
*fmthd
= NULL
;
191 static struct mcomp
*fmttl
= NULL
;
193 static struct mcomp global
= {
194 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
197 static struct mcomp holder
= {
198 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
203 unsigned long p_flags
;
206 static struct pair pairs
[] = {
209 { "Sender", ADDRFMT
},
210 { "Reply-To", ADDRFMT
},
214 { "Resent-Date", DATEFMT
},
215 { "Resent-From", ADDRFMT
},
216 { "Resent-Sender", ADDRFMT
},
217 { "Resent-Reply-To", ADDRFMT
},
218 { "Resent-To", ADDRFMT
},
219 { "Resent-cc", ADDRFMT
},
220 { "Resent-Bcc", ADDRFMT
},
230 static struct triple triples
[] = {
231 { "nocomponent", NOCOMPONENT
, 0 },
232 { "uppercase", UPPERCASE
, 0 },
233 { "nouppercase", 0, UPPERCASE
},
234 { "center", CENTER
, 0 },
235 { "nocenter", 0, CENTER
},
236 { "clearscreen", CLEARSCR
, 0 },
237 { "noclearscreen", 0, CLEARSCR
},
238 { "noclear", 0, CLEARSCR
},
239 { "leftadjust", LEFTADJUST
, 0 },
240 { "noleftadjust", 0, LEFTADJUST
},
241 { "compress", COMPRESS
, 0 },
242 { "nocompress", 0, COMPRESS
},
243 { "split", SPLIT
, 0 },
244 { "nosplit", 0, SPLIT
},
245 { "rtrim", RTRIM
, 0 },
246 { "nortrim", 0, RTRIM
},
247 { "addrfield", ADDRFMT
, DATEFMT
},
249 { "nobell", 0, BELL
},
250 { "datefield", DATEFMT
, ADDRFMT
},
251 { "newline", 0, NONEWLINE
},
252 { "nonewline", NONEWLINE
, 0 },
253 { "wrap", 0, NOWRAP
},
254 { "nowrap", NOWRAP
, 0 },
255 { "format", FMTFILTER
, 0 },
256 { "noformat", 0, FMTFILTER
},
260 static char *addrcomps
[] = {
277 static int bellflg
= 0;
278 static int clearflg
= 0;
279 static int dashstuff
= 0;
280 static bool dobody
= true;
284 static int sleepsw
= NOTOK
;
286 static char *digest
= NULL
;
287 static int volume
= 0;
288 static int issue
= 0;
290 static int exitstat
= 0;
291 static bool mhldebug
;
293 static int filesize
= 0;
298 static int ontty
= NOTTY
;
301 static unsigned int column
;
307 static unsigned int wid
;
315 static int num_ignores
= 0;
316 static char *ignores
[MAXARGS
];
320 static char delim3
[] = /* from forw.c */
321 "\n----------------------------------------------------------------------\n\n";
322 static char delim4
[] = "\n------------------------------\n\n";
327 static void mhl_format (char *, int, int);
328 static int evalvar (struct mcomp
*);
329 static int ptoi (char *, int *);
330 static int ptos (char *, char **);
331 static char *parse (void);
332 static void process (char *, char *, int, int);
333 static void mhlfile (FILE *, char *, int, int);
334 static int mcomp_flags (char *) PURE
;
335 static char *mcomp_add (unsigned long, char *, char *);
336 static void mcomp_format (struct mcomp
*, struct mcomp
*);
337 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
338 static void free_queue (struct mcomp
**, struct mcomp
**);
339 static void putcomp (struct mcomp
*, struct mcomp
*, int);
340 static char *oneline (char *, unsigned long);
341 static void putstr (char *, unsigned long);
342 static void putch (char, unsigned long);
343 static bool linefeed_typed(void);
344 static void intrser (int);
345 static void pipeser (int);
346 static void quitser (int);
347 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
348 static void mhldone (int) NORETURN
;
349 static void filterbody (struct mcomp
*, char *, int, int,
351 static void compile_formatfield(struct mcomp
*);
352 static void compile_filterargs (void);
356 mhl (int argc
, char **argv
)
360 unsigned int i
, vecp
= 0;
362 char *cp
, *folder
= NULL
, *form
= NULL
;
363 char buf
[BUFSIZ
], *files
[MAXARGS
];
364 char **argp
, **arguments
;
366 /* Need this if called from main() of show(1). */
367 invo_name
= r1bindex (argv
[0], '/');
369 arguments
= getarguments (invo_name
, argc
, argv
, 1);
372 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
375 while ((cp
= *argp
++)) {
377 switch (smatch (++cp
, mhlswitches
)) {
379 ambigsw (cp
, mhlswitches
);
382 mhladios (NULL
, "-%s unknown\n", cp
);
385 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
386 print_help (buf
, mhlswitches
, 1);
389 print_version(invo_name
);
407 if (!(folder
= *argp
++) || *folder
== '-')
408 mhladios (NULL
, "missing argument to %s", argp
[-2]);
411 if (!(form
= *argp
++) || *form
== '-')
412 mhladios (NULL
, "missing argument to %s", argp
[-2]);
416 if (!(cp
= *argp
++) || *cp
== '-')
417 mhladios (NULL
, "missing argument to %s", argp
[-2]);
418 sleepsw
= atoi (cp
);/* ZERO ok! */
422 if (!(moreproc
= *argp
++) || *moreproc
== '-')
423 mhladios (NULL
, "missing argument to %s", argp
[-2]);
430 if (!(formatproc
= *argp
++) || *formatproc
== '-')
431 mhladios (NULL
, "missing argument to %s", argp
[-2]);
438 if (!(cp
= *argp
++) || *cp
== '-')
439 mhladios (NULL
, "missing argument to %s", argp
[-2]);
440 if ((length
= atoi (cp
)) < 1)
441 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
444 if (!(cp
= *argp
++) || *cp
== '-')
445 mhladios (NULL
, "missing argument to %s", argp
[-2]);
446 if ((width
= atoi (cp
)) < 1)
447 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
451 if (!(digest
= *argp
++) || *digest
== '-')
452 mhladios (NULL
, "missing argument to %s", argp
[-2]);
455 if (!(cp
= *argp
++) || *cp
== '-')
456 mhladios (NULL
, "missing argument to %s", argp
[-2]);
457 if ((issue
= atoi (cp
)) < 1)
458 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
461 if (!(cp
= *argp
++) || *cp
== '-')
462 mhladios (NULL
, "missing argument to %s", argp
[-2]);
463 if ((volume
= atoi (cp
)) < 1)
464 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
472 clearflg
= -1;/* XXX */
476 dashstuff
= 1; /* ternary logic */
479 dashstuff
= -1; /* ternary logic */
491 folder
= getenv ("mhfolder");
493 if (isatty (fileno (stdout
))) {
494 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
495 SIGNAL2 (SIGPIPE
, pipeser
);
496 m_popen(moreproc
, false);
499 SIGNAL (SIGINT
, SIG_IGN
);
500 SIGNAL2 (SIGQUIT
, quitser
);
507 mhl_format (form
? form
: mhlformat
, length
, width
);
510 process (folder
, NULL
, 1, vecp
= 1);
512 for (i
= 0; i
< vecp
; i
++)
513 process (folder
, files
[i
], i
+ 1, vecp
);
518 fputs(delim4
, stdout
);
520 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
522 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
523 digest
, volume
, issue
);
526 for (cp
= buf
+ i
; i
> 1; i
--)
533 printf ("\n------- End of Forwarded Message%s\n",
539 mhladios("output", "error writing");
542 if (clearflg
> 0 && ontty
== NOTTY
)
553 mhl_format (char *file
, int length
, int width
)
557 char *ap
, name
[NAMESZ
];
561 static dev_t dev
= 0;
562 static ino_t ino
= 0;
563 static time_t mtime
= 0;
566 if (stat (etcpath (file
), &st
) != NOTOK
567 && mtime
== st
.st_mtime
571 free_queue (&fmthd
, &fmttl
);
574 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
575 mhladios (file
, "unable to open format file");
577 if (fstat (fileno (fp
), &st
) != NOTOK
) {
583 global
.c_ovtxt
= global
.c_nfs
= NULL
;
587 if ((i
= sc_width ()) > 5)
589 global
.c_cwidth
= -1;
590 if ((i
= sc_length ()) > 5)
591 global
.c_length
= i
- 1;
592 global
.c_flags
= BELL
; /* BELL is default */
593 *(ip
= ignores
) = NULL
;
596 while (vfgets (fp
, &ap
) == OK
) {
601 trim_suffix_c(bp
, '\n');
604 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
609 strncpy (name
, parse(), sizeof(name
));
615 * Split this list of fields to ignore, and copy
616 * it to the end of the current "ignores" list.
618 if (!strcasecmp (name
, "ignores")) {
619 char **tmparray
, **p
;
622 /* split the fields */
623 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
625 /* count number of fields split */
630 /* copy pointers to split fields to ignores array */
631 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
637 if (evalvar (&global
))
638 mhladios (NULL
, "format file syntax error: %s", bp
);
645 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
646 while (*parptr
== ':' || *parptr
== ',') {
649 mhladios (NULL
, "format file syntax error: %s", bp
);
651 if (!c1
->c_nfs
&& global
.c_nfs
) {
652 if (c1
->c_flags
& DATEFMT
) {
653 if (global
.c_flags
& DATEFMT
) {
654 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
655 compile_formatfield(c1
);
657 } else if (c1
->c_flags
& ADDRFMT
) {
658 if (global
.c_flags
& ADDRFMT
) {
659 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
660 compile_formatfield(c1
);
667 mhladios (NULL
, "format file syntax error: %s", bp
);
673 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
676 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
677 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
678 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
679 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
680 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
681 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
682 c1
->c_cwidth
, c1
->c_length
);
683 fprintf (stderr
, "\tflags=%s\n",
684 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
690 global
.c_flags
|= CLEARSCR
;
693 global
.c_flags
&= ~CLEARSCR
;
696 switch (bellflg
) { /* command line may override format file */
698 global
.c_flags
|= BELL
;
701 global
.c_flags
&= ~BELL
;
706 global
.c_length
= length
;
708 global
.c_width
= width
;
709 if (global
.c_length
< 5)
710 global
.c_length
= 10000;
711 if (global
.c_width
< 5)
712 global
.c_width
= 10000;
717 evalvar (struct mcomp
*c1
)
719 char *cp
, name
[NAMESZ
];
724 strncpy (name
, parse(), sizeof(name
));
726 if (!strcasecmp (name
, "component")) {
727 if (ptos (name
, &c1
->c_text
))
729 c1
->c_flags
&= ~NOCOMPONENT
;
733 if (!strcasecmp (name
, "overflowtext"))
734 return ptos (name
, &c1
->c_ovtxt
);
736 if (!strcasecmp (name
, "formatfield")) {
737 if (ptos (name
, &cp
))
739 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
740 compile_formatfield(c1
);
741 c1
->c_flags
|= FORMAT
;
745 if (!strcasecmp (name
, "decode")) {
746 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
747 compile_formatfield(c1
);
748 c1
->c_flags
|= FORMAT
;
752 if (!strcasecmp (name
, "offset"))
753 return ptoi (name
, &c1
->c_offset
);
754 if (!strcasecmp (name
, "overflowoffset"))
755 return ptoi (name
, &c1
->c_ovoff
);
756 if (!strcasecmp (name
, "width"))
757 return ptoi (name
, &c1
->c_width
);
758 if (!strcasecmp (name
, "compwidth"))
759 return ptoi (name
, &c1
->c_cwidth
);
760 if (!strcasecmp (name
, "length"))
761 return ptoi (name
, &c1
->c_length
);
762 if (!strcasecmp (name
, "nodashstuffing"))
763 return dashstuff
= -1;
765 for (ap
= triples
; ap
->t_name
; ap
++)
766 if (!strcasecmp (ap
->t_name
, name
)) {
767 c1
->c_flags
|= ap
->t_on
;
768 c1
->c_flags
&= ~ap
->t_off
;
772 if (!strcasecmp (name
, "formatarg")) {
773 struct arglist
*args
;
775 if (ptos (name
, &cp
))
778 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
779 inform("format filters are currently only supported on "
780 "the \"body\" component");
787 arglist_tail
->a_next
= args
;
794 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
805 ptoi (char *name
, int *i
)
809 if (*parptr
++ != '=' || !*(cp
= parse ())) {
810 inform("missing argument to variable %s", name
);
820 ptos (char *name
, char **s
)
824 if (*parptr
++ != '=') {
825 inform("missing argument to variable %s", name
);
829 if (*parptr
!= '"') {
831 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
835 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
836 if (*parptr
== QUOTE
)
843 if ((*parptr
= c
) == '"')
854 static char result
[NAMESZ
];
856 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
874 * Process one file/message
878 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
880 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
886 /* volatile to prevent "might be clobbered" warning from gcc: */
887 char *volatile fname2
= fname
? fname
: "(stdin)";
892 switch (setjmp (env
)) {
895 fp
= fopen(fname
, "r");
897 advise (fname
, "unable to open");
904 if (fstat(fileno(fp
), &st
) == 0) {
905 filesize
= st
.st_size
;
909 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
911 SIGNAL (SIGINT
, intrser
);
912 mhlfile (fp
, cp
, ofilen
, ofilec
);
915 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
916 fmt_free(ap
->a_fmt
, 0);
926 SIGNAL (SIGINT
, SIG_IGN
);
927 if (fp
!= stdin
&& fp
!= NULL
)
930 holder
.c_text
= NULL
;
931 free_queue (&msghd
, &msgtl
);
932 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
933 c1
->c_flags
&= ~HDROUTPUT
;
941 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
944 struct mcomp
*c1
, *c2
, *c3
;
945 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
946 m_getfld_state_t gstate
;
948 compile_filterargs();
952 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
954 fputs("\n-------", stdout
);
956 printf (" Forwarded Message%s", PLURALS(ofilec
));
958 printf (" Message %d", ofilen
);
966 if ((global
.c_flags
& CLEARSCR
))
971 printf (">>> %s\n\n", mname
);
977 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
980 printf ("Press <return> to list \"%s\"...", mname
);
984 if (ofilec
== 1 || linefeed_typed()) {
985 if ((global
.c_flags
& CLEARSCR
))
999 printf (">>> %s\n\n", mname
);
1005 gstate
= m_getfld_state_init(fp
);
1007 int bufsz
= sizeof buf
;
1008 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1011 bucket
= fmt_addcomptext(name
, buf
);
1012 for (ip
= ignores
; *ip
; ip
++)
1013 if (!strcasecmp (name
, *ip
)) {
1014 while (state
== FLDPLUS
) {
1016 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1017 fmt_appendcomp(bucket
, name
, buf
);
1024 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1025 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1028 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1029 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1030 if (!strcasecmp (FENDNULL(c1
->c_name
),
1031 FENDNULL(c3
->c_name
))) {
1033 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1037 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1038 while (state
== FLDPLUS
) {
1040 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1041 c1
->c_text
= add (buf
, c1
->c_text
);
1042 fmt_appendcomp(bucket
, name
, buf
);
1045 c1
->c_flags
|= EXTRA
;
1051 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1052 if (c1
->c_flags
& CLEARTEXT
) {
1053 putcomp (c1
, c1
, ONECOMP
);
1057 !strcasecmp (c1
->c_name
, "messagename")) {
1058 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1060 putcomp (c1
, &holder
, ONECOMP
);
1061 free (holder
.c_text
);
1062 holder
.c_text
= NULL
;
1065 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1066 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1067 if (c2
->c_flags
& EXTRA
)
1068 putcomp (c1
, c2
, TWOCOMP
);
1071 if (dobody
&& (!c1
->c_name
||
1072 !strcasecmp (c1
->c_name
, "body"))) {
1073 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1074 formatproc
!= NULL
) {
1075 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1077 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1078 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1079 while (state
== BODY
) {
1080 putcomp (c1
, &holder
, BODYCOMP
);
1082 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1085 free (holder
.c_text
);
1086 holder
.c_text
= NULL
;
1090 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1091 if (!strcasecmp (FENDNULL(c2
->c_name
),
1092 FENDNULL(c1
->c_name
))) {
1093 putcomp (c1
, c2
, ONECOMP
);
1094 if (!(c1
->c_flags
& SPLIT
))
1098 m_getfld_state_destroy (&gstate
);
1103 inform("format error in message %s", mname
);
1105 m_getfld_state_destroy (&gstate
);
1109 mhladios (NULL
, "getfld() returned %d", state
);
1116 mcomp_flags (char *name
)
1120 for (ap
= pairs
; ap
->p_name
; ap
++)
1121 if (!strcasecmp (ap
->p_name
, name
))
1129 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1133 if (!(flags
& ADDRFMT
))
1134 return add (s1
, s2
);
1136 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1139 return add (s1
, add (",\n", s2
));
1146 struct pqpair
*pq_next
;
1151 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1156 struct pqpair
*p
, *q
;
1158 struct mailname
*mp
;
1165 dat
[3] = BUFSIZ
- 1;
1168 if (!(c1
->c_flags
& ADDRFMT
)) {
1169 charstring_t scanl
= charstring_create (BUFSIZ
);
1172 c1
->c_c_text
->c_text
= ap
;
1173 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1177 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1178 /* Don't need to append a newline, dctime() already did */
1179 c2
->c_text
= charstring_buffer_copy (scanl
);
1180 charstring_free (scanl
);
1182 /* ap is now owned by the component struct, so do NOT free it here */
1186 (q
= &pq
)->pq_next
= NULL
;
1187 while ((cp
= getname (ap
))) {
1189 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1190 p
->pq_text
= mh_xstrdup(cp
);
1191 p
->pq_error
= mh_xstrdup(error
);
1193 p
->pq_text
= getcpy (mp
->m_text
);
1196 q
= (q
->pq_next
= p
);
1199 for (p
= pq
.pq_next
; p
; p
= q
) {
1200 charstring_t scanl
= charstring_create (BUFSIZ
);
1204 c1
->c_c_text
->c_text
= p
->pq_text
;
1207 if (c1
->c_c_error
) {
1208 c1
->c_c_error
->c_text
= p
->pq_error
;
1212 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1213 buffer
= charstring_buffer_copy (scanl
);
1216 c2
->c_text
= add (",\n", c2
->c_text
);
1217 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1219 c2
->c_text
= add (buffer
, c2
->c_text
);
1221 charstring_free (scanl
);
1229 c2
->c_text
= add ("\n", c2
->c_text
);
1234 static struct mcomp
*
1235 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1240 c1
->c_flags
= flags
& ~INIT
;
1241 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1242 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1243 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1246 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1247 c1
->c_offset
= global
.c_offset
;
1248 c1
->c_ovoff
= global
. c_ovoff
;
1249 c1
->c_width
= c1
->c_length
= 0;
1250 c1
->c_cwidth
= global
.c_cwidth
;
1251 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1256 (*tail
)->c_next
= c1
;
1264 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1266 struct mcomp
*c1
, *c2
;
1268 for (c1
= *head
; c1
; c1
= c2
) {
1275 fmt_free (c1
->c_fmt
, 0);
1279 *head
= *tail
= NULL
;
1284 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1286 char *text
; /* c1's text, or the name as a fallback. */
1287 char *trimmed_prefix
;
1291 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1293 if (! utf8
&& flag
!= BODYCOMP
) {
1294 /* Don't print 8-bit bytes in header field values if not in a
1295 UTF-8 locale, as required by RFC 6532. */
1296 c1
->c_flags
|= FORCE7BIT
;
1299 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1300 /* Create a copy with trailing whitespace trimmed, for use with
1302 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1306 llim
= c1
->c_length
? c1
->c_length
: -1;
1307 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1308 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1310 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1312 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1313 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1314 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1317 if (c1
->c_flags
& CLEARTEXT
) {
1318 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1320 putstr ("\n", c1
->c_flags
);
1324 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1325 mcomp_format (c1
, c2
);
1327 if (c1
->c_flags
& CENTER
) {
1328 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1329 - c1
->c_offset
- strlen (c2
->c_text
);
1330 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1331 count
-= strlen(text
) + 2;
1332 lm
= c1
->c_offset
+ (count
/ 2);
1338 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1339 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1341 putstr(text
, c1
->c_flags
);
1342 if (flag
!= BODYCOMP
) {
1343 putstr (": ", c1
->c_flags
);
1344 if (!(c1
->c_flags
& SPLIT
))
1345 c1
->c_flags
|= HDROUTPUT
;
1348 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1350 putstr (" ", c1
->c_flags
);
1353 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1357 && !(c2
->c_flags
& HDROUTPUT
)
1358 && !(c2
->c_flags
& NOCOMPONENT
)) {
1359 if (c1
->c_flags
& UPPERCASE
)
1360 to_upper(c2
->c_name
);
1361 putstr (c2
->c_name
, c1
->c_flags
);
1362 putstr (": ", c1
->c_flags
);
1363 if (!(c1
->c_flags
& SPLIT
))
1364 c2
->c_flags
|= HDROUTPUT
;
1367 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1369 putstr (" ", c1
->c_flags
);
1371 if (c1
->c_flags
& UPPERCASE
)
1372 to_upper(c2
->c_text
);
1376 if (flag
== TWOCOMP
)
1377 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1378 : (int) strlen (c2
->c_name
) + 2;
1380 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1383 count
+= c1
->c_offset
;
1385 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1386 /* Output line, trimming trailing whitespace if requested. */
1387 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1389 putstr ("\n", c1
->c_flags
);
1390 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1392 if (flag
== BODYCOMP
1393 && !(c1
->c_flags
& NOCOMPONENT
)) {
1394 /* Output component, trimming trailing whitespace if there
1395 is no text on the line. */
1397 putstr(text
, c1
->c_flags
);
1399 putstr (trimmed_prefix
, c1
->c_flags
);
1403 /* Output line, trimming trailing whitespace if requested. */
1404 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1407 putstr ("\n", c1
->c_flags
);
1409 if (flag
== BODYCOMP
&& term
== '\n')
1410 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1412 free (trimmed_prefix
);
1417 oneline (char *stuff
, unsigned long flags
)
1425 return onelp
= NULL
;
1429 if (flags
& COMPRESS
) {
1430 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1431 if (isspace ((unsigned char) *onelp
)) {
1432 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1450 while (*onelp
&& *onelp
!= '\n')
1452 if (*onelp
== '\n') {
1456 if (flags
& LEFTADJUST
)
1457 while (*ret
== ' ' || *ret
== '\t')
1460 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1468 putstr (char *string
, unsigned long flags
)
1470 /* To not count, for the purpose of counting columns, all of
1471 the bytes of a multibyte character. */
1474 if (!column
&& lm
> 0) {
1477 putch ('\t', flags
);
1487 #ifdef MULTIBYTE_SUPPORT
1488 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1491 NMH_UNUSED (char_len
);
1495 flags
&= ~INVISIBLE
;
1496 #ifdef MULTIBYTE_SUPPORT
1497 /* mbtowc should never return 0, because *string is non-NULL. */
1498 if (char_len
<= 0) {
1499 /* Find number of bytes in next character. */
1501 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1505 /* Multibyte character, after the first byte. */
1511 putch (*string
++, flags
);
1517 putch (char ch
, unsigned long flags
)
1528 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1530 if (global
.c_flags
& BELL
)
1533 if (linefeed_typed()) {
1534 if (global
.c_flags
& CLEARSCR
)
1535 nmh_clear_screen ();
1539 row
= global
.c_length
/ 3;
1558 * If we are forwarding this message, and the first
1559 * column contains a dash, then add a dash and a space.
1561 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1566 * Increment the character count, unless
1567 * 1) In UTF-8 locale, this is other than the last byte of
1568 a multibyte character, or
1569 * 2) In C locale, will print a non-printable character.
1571 if ((flags
& FORCE7BIT
) == 0) {
1573 if ((flags
& INVISIBLE
) == 0) {
1574 /* If multibyte character, its first byte only. */
1578 /* If not an ASCII character, the replace character will be
1579 displayed. Count it. */
1580 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1587 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1588 putch ('\n', flags
);
1591 putstr (FENDNULL(ovtxt
), flags
);
1596 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1603 /* linefeed_typed() makes a single read(2) from stdin and returns true
1604 * if a linefeed character is amongst the characters read.
1605 * A read error is treated as if linefeed wasn't typed.
1607 * Typing on a TTY can cause read() to return data without typing Enter
1608 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1609 * The linefeed can also be escaped with the TTY's LNEXT character,
1610 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1611 * It's not possible to distinguish between the user typing a buffer's
1612 * worth of characters and then EOT, or more than the buffer can hold.
1613 * Either way, the result depends on ASCII LF, either from typing Enter
1614 * or an escaped Ctrl-J, being amongst the read characters.
1617 linefeed_typed(void)
1622 n
= read(0, buf
, sizeof buf
);
1624 advise("stdin", "read");
1625 return false; /* Treat as EOF. */
1628 return memchr(buf
, '\n', n
);
1639 longjmp (env
, DONE
);
1664 mhladios (char *what
, char *fmt
, ...)
1669 advertise (what
, NULL
, fmt
, ap
);
1676 mhldone (int status
)
1684 * Compile a format string used by the formatfield option and save it
1687 * We will want the {text} (and possibly {error}) components for later,
1688 * so look for them and save them if we find them.
1692 compile_formatfield(struct mcomp
*c1
)
1694 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1697 * As a note to myself and any other poor bastard who is looking through
1698 * this code in the future ....
1700 * When the format hash table is reset later on (as it almost certainly
1701 * will be), there will still be references to these components in the
1702 * compiled format instructions. Thus these component references will
1703 * be free'd when the format instructions are free'd (by fmt_free()).
1705 * So, in other words ... don't go free'ing them yourself!
1708 c1
->c_c_text
= fmt_findcomp("text");
1709 c1
->c_c_error
= fmt_findcomp("error");
1713 * Compile all of the arguments for our format list.
1715 * Iterate through the linked list of format strings and compile them.
1716 * Note that we reset the format hash table before we start, but we do NOT
1717 * reset it between calls to fmt_compile().
1722 compile_filterargs (void)
1724 struct arglist
*arg
= arglist_head
;
1731 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1736 * Search through and mark any components that are address components
1739 for (ap
= addrcomps
; *ap
; ap
++) {
1740 cptr
= fmt_findcomp (*ap
);
1742 cptr
->c_type
|= CT_ADDR
;
1747 * Filter the body of a message through a specified format program
1751 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1752 m_getfld_state_t gstate
)
1754 struct mcomp holder
;
1756 int fdinput
[2], fdoutput
[2], waitstat
;
1758 pid_t writerpid
, filterpid
;
1761 * Create pipes so we can communicate with our filter process.
1764 if (pipe(fdinput
) < 0) {
1765 die("Unable to create input pipe");
1768 if (pipe(fdoutput
) < 0) {
1769 die("Unable to create output pipe");
1773 * Here's what we're doing to do.
1775 * - Fork ourselves and start writing data to the write side of the
1776 * input pipe (fdinput[1]).
1778 * - Fork and exec our filter program. We set the standard input of
1779 * our filter program to be the read side of our input pipe (fdinput[0]).
1780 * Standard output is set to the write side of our output pipe
1783 * - We read from the read side of the output pipe (fdoutput[0]).
1785 * We're forking because that's the simplest way to prevent any deadlocks.
1786 * (without doing something like switching to non-blocking I/O and using
1787 * select or poll, and I'm not interested in doing that).
1790 switch (writerpid
= fork()) {
1793 * Our child process - just write to the filter input (fdinput[1]).
1794 * Close all other descriptors that we don't need.
1802 * Call m_getfld2() until we're no longer in the BODY state
1805 while (state
== BODY
) {
1807 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1808 advise ("pipe output", "write");
1810 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1814 * We should be done; time to exit.
1819 * Make sure we call _exit(), otherwise we may flush out the stdio
1820 * buffers that we have duplicated from the parent.
1824 die("Unable to fork for filter writer process");
1829 * Fork and exec() our filter program, after redirecting standard in
1830 * and standard out appropriately.
1833 switch (filterpid
= fork()) {
1834 char **args
, *program
;
1836 int i
, dat
[5], s
, argp
;
1840 * Configure an argument array for us
1843 args
= argsplit(formatproc
, &program
, &argp
);
1844 args
[argp
+ filter_nargs
] = NULL
;
1852 * Pull out each argument and scan them.
1855 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1856 charstring_t scanl
= charstring_create (BUFSIZ
);
1858 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1859 args
[i
] = charstring_buffer_copy (scanl
);
1860 charstring_free (scanl
);
1862 * fmt_scan likes to put a trailing newline at the end of the
1863 * format string. If we have one, get rid of it.
1865 s
= strlen(args
[i
]);
1866 if (args
[i
][s
- 1] == '\n')
1867 args
[i
][s
- 1] = '\0';
1870 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1874 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1875 adios("formatproc", "Unable to dup2() standard input");
1877 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1878 adios("formatproc", "Unable to dup2() standard output");
1882 * Close everything (especially the old input and output
1883 * descriptors, since they've been dup'd to stdin and stdout),
1884 * and exec the formatproc.
1892 execvp(formatproc
, args
);
1894 adios(formatproc
, "Unable to execute filter");
1899 die("Unable to fork format program");
1903 * Close everything except our reader (fdoutput[0]);
1911 * As we read in this data, send it to putcomp
1914 holder
.c_text
= buf
;
1916 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1918 putcomp(c1
, &holder
, BODYCOMP
);
1922 die("reading from formatproc");
1926 * See if we got any errors along the way. I'm a little leery of calling
1927 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1930 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1931 if (errno
!= ECHILD
) {
1932 adios("filterproc", "Unable to determine status");
1935 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1936 pidstatus(waitstat
, stderr
, "filterproc");
1940 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1941 if (errno
!= ECHILD
) {
1942 adios("writer process", "Unable to determine status");
1946 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1947 pidstatus(waitstat
, stderr
, "writer process");