]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
ade7e1c29953093164874b2b4b945645d3185f72
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/fmt_new.h"
11 #include "sbr/m_getfld.h"
12 #include "sbr/getarguments.h"
13 #include "sbr/concat.h"
14 #include "sbr/smatch.h"
15 #include "sbr/r1bindex.h"
16 #include "sbr/snprintb.h"
17 #include "sbr/copyip.h"
18 #include "sbr/discard.h"
19 #include "sbr/trimcpy.h"
20 #include "sbr/vfgets.h"
21 #include "sbr/check_charset.h"
22 #include "sbr/getcpy.h"
23 #include "sbr/brkstring.h"
24 #include "sbr/ambigsw.h"
25 #include "sbr/pidstatus.h"
26 #include "sbr/print_version.h"
27 #include "sbr/print_help.h"
28 #include "sbr/arglist.h"
29 #include "sbr/error.h"
30 #include "h/signals.h"
31 #include "h/addrsbr.h"
32 #include "h/fmt_scan.h"
36 #include "sbr/m_popen.h"
38 #include <sys/types.h>
39 #include "sbr/terminal.h"
43 * for a component containing addresses, ADDRFMT, if COMPRESS is also
44 * set, then addresses get split wrong (not at the spaces between commas).
45 * To fix this correctly, putstr() should know about "atomic" strings that
46 * must NOT be broken across lines. That's too difficult for right now
47 * (it turns out that there are a number of degenerate cases), so in
48 * oneline(), instead of
50 * (*onelp == '\n' && !onelp[1])
52 * being a terminating condition,
54 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
56 * is used instead. This cuts the line prematurely, and gives us a much
57 * better chance of getting things right.
66 #define MHL_SWITCHES \
67 X("bell", 0, BELLSW) \
68 X("nobell", 0, NBELLSW) \
69 X("clear", 0, CLRSW) \
70 X("noclear", 0, NCLRSW) \
71 X("folder +folder", 0, FOLDSW) \
72 X("form formfile", 0, FORMSW) \
73 X("moreproc program", 0, PROGSW) \
74 X("nomoreproc", 0, NPROGSW) \
75 X("length lines", 0, LENSW) \
76 X("width columns", 0, WIDTHSW) \
77 X("sleep seconds", 0, SLEEPSW) \
78 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
79 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
80 X("version", 0, VERSIONSW) \
81 X("help", 0, HELPSW) \
82 X("forward", -7, FORW1SW) /* interface from forw */ \
83 X("forwall", -7, FORW2SW) /* interface from forw */ \
84 X("digest list", -6, DGSTSW) \
85 X("volume number", -6, VOLUMSW) \
86 X("issue number", -5, ISSUESW) \
87 X("nobody", -6, NBODYSW) \
88 X("fmtproc program", 0, FMTPROCSW) \
89 X("nofmtproc", 0, NFMTPROCSW) \
91 #define X(sw, minchars, id) id,
92 DEFINE_SWITCH_ENUM(MHL
);
95 #define X(sw, minchars, id) { sw, minchars, id },
96 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
99 #define NOCOMPONENT 0x000001 /* don't show component name */
100 #define UPPERCASE 0x000002 /* display in all upper case */
101 #define CENTER 0x000004 /* center line */
102 #define CLEARTEXT 0x000008 /* cleartext */
103 #define EXTRA 0x000010 /* an "extra" component */
104 #define HDROUTPUT 0x000020 /* already output */
105 #define CLEARSCR 0x000040 /* clear screen */
106 #define LEFTADJUST 0x000080 /* left justify multiple lines */
107 #define COMPRESS 0x000100 /* compress text */
108 #define ADDRFMT 0x000200 /* contains addresses */
109 #define BELL 0x000400 /* sound bell at EOP */
110 #define DATEFMT 0x000800 /* contains dates */
111 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
112 #define INIT 0x002000 /* initialize component */
113 #define RTRIM 0x004000 /* trim trailing whitespace */
114 #define SPLIT 0x010000 /* split headers (don't concatenate) */
115 #define NONEWLINE 0x020000 /* don't write trailing newline */
116 #define NOWRAP 0x040000 /* Don't wrap lines ever */
117 #define FMTFILTER 0x080000 /* Filter through format filter */
118 #define INVISIBLE 0x100000 /* count byte in display columns? */
119 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
120 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
121 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
124 * A format string to be used as a command-line argument to the body
129 struct format
*a_fmt
;
131 struct arglist
*a_next
;
135 * Linked list of command line arguments for the body format filter. This
136 * USED to be in "struct mcomp", but the format API got cleaned up and even
137 * though it reduced the code we had to do, it make things more complicated
138 * for us. Specifically:
140 * - The interface to the hash table has been cleaned up, which means the
141 * rooting around in the hash table is no longer necessary (yay!). But
142 * this ALSO means that we have to make sure that we call our format
143 * compilation routines before we process the message, because the
144 * components need to be visible in the hash table so we can save them for
145 * later. So we moved them out of "mcomp" and now compile them right before
146 * header processing starts.
147 * - We also use format strings to handle other components in the mhl
148 * configuration (using "formatfield" and "decode"), but here life
149 * gets complicated: they aren't dealt with in the normal way. Instead
150 * of referring to a component like {from}, each component is processed
151 * using the special {text} component. But these format strings need to be
152 * compiled BEFORE we compile the format arguments; in the previous
153 * implementation they were compiled and scanned as the headers were
154 * read, and that would reset the hash table that we need to populate
155 * the components used by the body format filter. So we are compiling
156 * the formatfield component strings ahead of time and then scanning them
159 * Okay, fine ... this was broken before. But you know what? Fixing this
160 * the right way will make things easier down the road.
162 * One side-effect to this change: format strings are now compiled only once
163 * for components specified with "formatfield", but they are compiled for
164 * every message for format arguments.
167 static struct arglist
*arglist_head
;
168 static struct arglist
*arglist_tail
;
169 static int filter_nargs
= 0;
172 * Flags/options for each component
176 char *c_name
; /* component name */
177 char *c_text
; /* component text */
178 char *c_ovtxt
; /* text overflow indicator */
179 char *c_nfs
; /* iff FORMAT */
180 struct format
*c_fmt
; /* .. */
181 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
182 struct comp
*c_c_error
; /* Ref to {error} */
183 int c_offset
; /* left margin indentation */
184 int c_ovoff
; /* overflow indentation */
185 int c_width
; /* width of field */
186 int c_cwidth
; /* width of component */
187 int c_length
; /* length in lines */
188 unsigned long c_flags
;
189 struct mcomp
*c_next
;
192 static struct mcomp
*msghd
= NULL
;
193 static struct mcomp
*msgtl
= NULL
;
194 static struct mcomp
*fmthd
= NULL
;
195 static struct mcomp
*fmttl
= NULL
;
197 static struct mcomp global
= {
198 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
201 static struct mcomp holder
= {
202 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
207 unsigned long p_flags
;
210 static struct pair pairs
[] = {
213 { "Sender", ADDRFMT
},
214 { "Reply-To", ADDRFMT
},
218 { "Resent-Date", DATEFMT
},
219 { "Resent-From", ADDRFMT
},
220 { "Resent-Sender", ADDRFMT
},
221 { "Resent-Reply-To", ADDRFMT
},
222 { "Resent-To", ADDRFMT
},
223 { "Resent-cc", ADDRFMT
},
224 { "Resent-Bcc", ADDRFMT
},
234 static struct triple triples
[] = {
235 { "nocomponent", NOCOMPONENT
, 0 },
236 { "uppercase", UPPERCASE
, 0 },
237 { "nouppercase", 0, UPPERCASE
},
238 { "center", CENTER
, 0 },
239 { "nocenter", 0, CENTER
},
240 { "clearscreen", CLEARSCR
, 0 },
241 { "noclearscreen", 0, CLEARSCR
},
242 { "noclear", 0, CLEARSCR
},
243 { "leftadjust", LEFTADJUST
, 0 },
244 { "noleftadjust", 0, LEFTADJUST
},
245 { "compress", COMPRESS
, 0 },
246 { "nocompress", 0, COMPRESS
},
247 { "split", SPLIT
, 0 },
248 { "nosplit", 0, SPLIT
},
249 { "rtrim", RTRIM
, 0 },
250 { "nortrim", 0, RTRIM
},
251 { "addrfield", ADDRFMT
, DATEFMT
},
253 { "nobell", 0, BELL
},
254 { "datefield", DATEFMT
, ADDRFMT
},
255 { "newline", 0, NONEWLINE
},
256 { "nonewline", NONEWLINE
, 0 },
257 { "wrap", 0, NOWRAP
},
258 { "nowrap", NOWRAP
, 0 },
259 { "format", FMTFILTER
, 0 },
260 { "noformat", 0, FMTFILTER
},
264 static char *addrcomps
[] = {
281 static int bellflg
= 0;
282 static int clearflg
= 0;
283 static int dashstuff
= 0;
284 static bool dobody
= true;
288 static int sleepsw
= NOTOK
;
290 static char *digest
= NULL
;
291 static int volume
= 0;
292 static int issue
= 0;
294 static int exitstat
= 0;
295 static bool mhldebug
;
297 static int filesize
= 0;
302 static int ontty
= NOTTY
;
305 static unsigned int column
;
311 static unsigned int wid
;
319 static int num_ignores
= 0;
320 static char *ignores
[MAXARGS
];
324 static char delim3
[] = /* from forw.c */
325 "\n----------------------------------------------------------------------\n\n";
326 static char delim4
[] = "\n------------------------------\n\n";
331 static void mhl_format (char *, int, int);
332 static int evalvar (struct mcomp
*);
333 static int ptoi (char *, int *);
334 static int ptos (char *, char **);
335 static char *parse (void);
336 static void process (char *, char *, int, int);
337 static void mhlfile (FILE *, char *, int, int);
338 static int mcomp_flags (char *) PURE
;
339 static char *mcomp_add (unsigned long, char *, char *);
340 static void mcomp_format (struct mcomp
*, struct mcomp
*);
341 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
342 static void free_queue (struct mcomp
**, struct mcomp
**);
343 static void putcomp (struct mcomp
*, struct mcomp
*, int);
344 static char *oneline (char *, unsigned long);
345 static void putstr (char *, unsigned long);
346 static void putch (char, unsigned long);
347 static bool linefeed_typed(void);
348 static void intrser (int);
349 static void pipeser (int);
350 static void quitser (int);
351 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
352 static void mhldone (int) NORETURN
;
353 static void filterbody (struct mcomp
*, char *, int, int,
355 static void compile_formatfield(struct mcomp
*);
356 static void compile_filterargs (void);
360 mhl (int argc
, char **argv
)
364 unsigned int i
, vecp
= 0;
366 char *cp
, *folder
= NULL
, *form
= NULL
;
367 char buf
[BUFSIZ
], *files
[MAXARGS
];
368 char **argp
, **arguments
;
370 /* Need this if called from main() of show(1). */
371 invo_name
= r1bindex (argv
[0], '/');
373 arguments
= getarguments (invo_name
, argc
, argv
, 1);
376 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
379 while ((cp
= *argp
++)) {
381 switch (smatch (++cp
, mhlswitches
)) {
383 ambigsw (cp
, mhlswitches
);
386 mhladios (NULL
, "-%s unknown\n", cp
);
389 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
390 print_help (buf
, mhlswitches
, 1);
393 print_version(invo_name
);
411 if (!(folder
= *argp
++) || *folder
== '-')
412 mhladios (NULL
, "missing argument to %s", argp
[-2]);
415 if (!(form
= *argp
++) || *form
== '-')
416 mhladios (NULL
, "missing argument to %s", argp
[-2]);
420 if (!(cp
= *argp
++) || *cp
== '-')
421 mhladios (NULL
, "missing argument to %s", argp
[-2]);
422 sleepsw
= atoi (cp
);/* ZERO ok! */
426 if (!(moreproc
= *argp
++) || *moreproc
== '-')
427 mhladios (NULL
, "missing argument to %s", argp
[-2]);
434 if (!(formatproc
= *argp
++) || *formatproc
== '-')
435 mhladios (NULL
, "missing argument to %s", argp
[-2]);
442 if (!(cp
= *argp
++) || *cp
== '-')
443 mhladios (NULL
, "missing argument to %s", argp
[-2]);
444 if ((length
= atoi (cp
)) < 1)
445 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
448 if (!(cp
= *argp
++) || *cp
== '-')
449 mhladios (NULL
, "missing argument to %s", argp
[-2]);
450 if ((width
= atoi (cp
)) < 1)
451 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
455 if (!(digest
= *argp
++) || *digest
== '-')
456 mhladios (NULL
, "missing argument to %s", argp
[-2]);
459 if (!(cp
= *argp
++) || *cp
== '-')
460 mhladios (NULL
, "missing argument to %s", argp
[-2]);
461 if ((issue
= atoi (cp
)) < 1)
462 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
465 if (!(cp
= *argp
++) || *cp
== '-')
466 mhladios (NULL
, "missing argument to %s", argp
[-2]);
467 if ((volume
= atoi (cp
)) < 1)
468 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
476 clearflg
= -1;/* XXX */
480 dashstuff
= 1; /* ternary logic */
483 dashstuff
= -1; /* ternary logic */
495 folder
= getenv ("mhfolder");
497 if (isatty (fileno (stdout
))) {
498 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
499 SIGNAL2 (SIGPIPE
, pipeser
);
500 m_popen(moreproc
, false);
503 SIGNAL (SIGINT
, SIG_IGN
);
504 SIGNAL2 (SIGQUIT
, quitser
);
511 mhl_format (form
? form
: mhlformat
, length
, width
);
514 process (folder
, NULL
, 1, vecp
= 1);
516 for (i
= 0; i
< vecp
; i
++)
517 process (folder
, files
[i
], i
+ 1, vecp
);
522 fputs(delim4
, stdout
);
524 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
526 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
527 digest
, volume
, issue
);
530 for (cp
= buf
+ i
; i
> 1; i
--)
537 printf ("\n------- End of Forwarded Message%s\n",
543 mhladios("output", "error writing");
546 if (clearflg
> 0 && ontty
== NOTTY
)
557 mhl_format (char *file
, int length
, int width
)
561 char *ap
, name
[NAMESZ
];
565 static dev_t dev
= 0;
566 static ino_t ino
= 0;
567 static time_t mtime
= 0;
570 if (stat (etcpath (file
), &st
) != NOTOK
571 && mtime
== st
.st_mtime
575 free_queue (&fmthd
, &fmttl
);
578 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
579 mhladios (file
, "unable to open format file");
581 if (fstat (fileno (fp
), &st
) != NOTOK
) {
587 global
.c_ovtxt
= global
.c_nfs
= NULL
;
591 if ((i
= sc_width ()) > 5)
593 global
.c_cwidth
= -1;
594 if ((i
= sc_length ()) > 5)
595 global
.c_length
= i
- 1;
596 global
.c_flags
= BELL
; /* BELL is default */
597 *(ip
= ignores
) = NULL
;
600 while (vfgets (fp
, &ap
) == OK
) {
605 trim_suffix_c(bp
, '\n');
608 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
613 strncpy (name
, parse(), sizeof(name
) - 1);
619 * Split this list of fields to ignore, and copy
620 * it to the end of the current "ignores" list.
622 if (!strcasecmp (name
, "ignores")) {
623 char **tmparray
, **p
;
626 /* split the fields */
627 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
629 /* count number of fields split */
634 /* copy pointers to split fields to ignores array */
635 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
641 if (evalvar (&global
))
642 mhladios (NULL
, "format file syntax error: %s", bp
);
649 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
650 while (*parptr
== ':' || *parptr
== ',') {
653 mhladios (NULL
, "format file syntax error: %s", bp
);
655 if (!c1
->c_nfs
&& global
.c_nfs
) {
656 if (c1
->c_flags
& DATEFMT
) {
657 if (global
.c_flags
& DATEFMT
) {
658 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
659 compile_formatfield(c1
);
661 } else if (c1
->c_flags
& ADDRFMT
) {
662 if (global
.c_flags
& ADDRFMT
) {
663 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
664 compile_formatfield(c1
);
671 mhladios (NULL
, "format file syntax error: %s", bp
);
677 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
680 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
681 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
682 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
683 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
684 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
685 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
686 c1
->c_cwidth
, c1
->c_length
);
687 fprintf (stderr
, "\tflags=%s\n",
688 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
694 global
.c_flags
|= CLEARSCR
;
697 global
.c_flags
&= ~CLEARSCR
;
700 switch (bellflg
) { /* command line may override format file */
702 global
.c_flags
|= BELL
;
705 global
.c_flags
&= ~BELL
;
710 global
.c_length
= length
;
712 global
.c_width
= width
;
713 if (global
.c_length
< 5)
714 global
.c_length
= 10000;
715 if (global
.c_width
< 5)
716 global
.c_width
= 10000;
721 evalvar (struct mcomp
*c1
)
723 char *cp
, name
[NAMESZ
];
728 strncpy (name
, parse(), sizeof(name
) - 1);
730 if (!strcasecmp (name
, "component")) {
731 if (ptos (name
, &c1
->c_text
))
733 c1
->c_flags
&= ~NOCOMPONENT
;
737 if (!strcasecmp (name
, "overflowtext"))
738 return ptos (name
, &c1
->c_ovtxt
);
740 if (!strcasecmp (name
, "formatfield")) {
741 if (ptos (name
, &cp
))
743 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
744 compile_formatfield(c1
);
745 c1
->c_flags
|= FORMAT
;
749 if (!strcasecmp (name
, "decode")) {
750 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
751 compile_formatfield(c1
);
752 c1
->c_flags
|= FORMAT
;
756 if (!strcasecmp (name
, "offset"))
757 return ptoi (name
, &c1
->c_offset
);
758 if (!strcasecmp (name
, "overflowoffset"))
759 return ptoi (name
, &c1
->c_ovoff
);
760 if (!strcasecmp (name
, "width"))
761 return ptoi (name
, &c1
->c_width
);
762 if (!strcasecmp (name
, "compwidth"))
763 return ptoi (name
, &c1
->c_cwidth
);
764 if (!strcasecmp (name
, "length"))
765 return ptoi (name
, &c1
->c_length
);
766 if (!strcasecmp (name
, "nodashstuffing"))
767 return dashstuff
= -1;
769 for (ap
= triples
; ap
->t_name
; ap
++)
770 if (!strcasecmp (ap
->t_name
, name
)) {
771 c1
->c_flags
|= ap
->t_on
;
772 c1
->c_flags
&= ~ap
->t_off
;
776 if (!strcasecmp (name
, "formatarg")) {
777 struct arglist
*args
;
779 if (ptos (name
, &cp
))
782 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
783 inform("format filters are currently only supported on "
784 "the \"body\" component");
791 arglist_tail
->a_next
= args
;
798 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
809 ptoi (char *name
, int *i
)
813 if (*parptr
++ != '=' || !*(cp
= parse ())) {
814 inform("missing argument to variable %s", name
);
824 ptos (char *name
, char **s
)
828 if (*parptr
++ != '=') {
829 inform("missing argument to variable %s", name
);
833 if (*parptr
!= '"') {
835 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
839 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
840 if (*parptr
== QUOTE
)
847 if ((*parptr
= c
) == '"')
858 static char result
[NAMESZ
];
860 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
878 * Process one file/message
882 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
884 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
890 /* volatile to prevent "might be clobbered" warning from gcc: */
891 char *volatile fname2
= fname
? fname
: "(stdin)";
896 switch (setjmp (env
)) {
899 fp
= fopen(fname
, "r");
901 advise (fname
, "unable to open");
908 if (fstat(fileno(fp
), &st
) == 0) {
909 filesize
= st
.st_size
;
913 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
915 SIGNAL (SIGINT
, intrser
);
916 mhlfile (fp
, cp
, ofilen
, ofilec
);
919 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
920 fmt_free(ap
->a_fmt
, 0);
930 SIGNAL (SIGINT
, SIG_IGN
);
931 if (fp
!= stdin
&& fp
!= NULL
)
934 holder
.c_text
= NULL
;
935 free_queue (&msghd
, &msgtl
);
936 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
937 c1
->c_flags
&= ~HDROUTPUT
;
945 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
948 struct mcomp
*c1
, *c2
, *c3
;
949 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
950 m_getfld_state_t gstate
;
952 compile_filterargs();
956 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
958 fputs("\n-------", stdout
);
960 printf (" Forwarded Message%s", PLURALS(ofilec
));
962 printf (" Message %d", ofilen
);
970 if ((global
.c_flags
& CLEARSCR
))
975 printf (">>> %s\n\n", mname
);
981 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
984 printf ("Press <return> to list \"%s\"...", mname
);
988 if (ofilec
== 1 || linefeed_typed()) {
989 if ((global
.c_flags
& CLEARSCR
))
1001 nmh_clear_screen ();
1003 printf (">>> %s\n\n", mname
);
1009 gstate
= m_getfld_state_init(fp
);
1011 int bufsz
= sizeof buf
;
1012 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1015 bucket
= fmt_addcomptext(name
, buf
);
1016 for (ip
= ignores
; *ip
; ip
++)
1017 if (!strcasecmp (name
, *ip
)) {
1018 while (state
== FLDPLUS
) {
1020 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1021 fmt_appendcomp(bucket
, name
, buf
);
1028 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1029 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1032 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1033 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1034 if (!strcasecmp (FENDNULL(c1
->c_name
),
1035 FENDNULL(c3
->c_name
))) {
1037 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1041 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1042 while (state
== FLDPLUS
) {
1044 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1045 c1
->c_text
= add (buf
, c1
->c_text
);
1046 fmt_appendcomp(bucket
, name
, buf
);
1049 c1
->c_flags
|= EXTRA
;
1055 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1056 if (c1
->c_flags
& CLEARTEXT
) {
1057 putcomp (c1
, c1
, ONECOMP
);
1061 !strcasecmp (c1
->c_name
, "messagename")) {
1062 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1064 putcomp (c1
, &holder
, ONECOMP
);
1065 free (holder
.c_text
);
1066 holder
.c_text
= NULL
;
1069 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1070 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1071 if (c2
->c_flags
& EXTRA
)
1072 putcomp (c1
, c2
, TWOCOMP
);
1075 if (dobody
&& (!c1
->c_name
||
1076 !strcasecmp (c1
->c_name
, "body"))) {
1077 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1078 formatproc
!= NULL
) {
1079 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1082 holder
.c_text
= mh_xmalloc(bufsz
);
1083 strncpy(holder
.c_text
, buf
, bufsz
);
1084 while (state
== BODY
) {
1085 putcomp (c1
, &holder
, BODYCOMP
);
1087 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1090 free (holder
.c_text
);
1091 holder
.c_text
= NULL
;
1095 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1096 if (!strcasecmp (FENDNULL(c2
->c_name
),
1097 FENDNULL(c1
->c_name
))) {
1098 putcomp (c1
, c2
, ONECOMP
);
1099 if (!(c1
->c_flags
& SPLIT
))
1103 m_getfld_state_destroy (&gstate
);
1108 inform("format error in message %s", mname
);
1110 m_getfld_state_destroy (&gstate
);
1114 mhladios (NULL
, "getfld() returned %d", state
);
1121 mcomp_flags (char *name
)
1125 for (ap
= pairs
; ap
->p_name
; ap
++)
1126 if (!strcasecmp (ap
->p_name
, name
))
1134 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1138 if (!(flags
& ADDRFMT
))
1139 return add (s1
, s2
);
1141 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1144 return add (s1
, add (",\n", s2
));
1151 struct pqpair
*pq_next
;
1156 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1161 struct pqpair
*p
, *q
;
1163 struct mailname
*mp
;
1170 dat
[3] = BUFSIZ
- 1;
1173 if (!(c1
->c_flags
& ADDRFMT
)) {
1174 charstring_t scanl
= charstring_create (BUFSIZ
);
1177 c1
->c_c_text
->c_text
= ap
;
1178 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1182 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1183 /* Don't need to append a newline, dctime() already did */
1184 c2
->c_text
= charstring_buffer_copy (scanl
);
1185 charstring_free (scanl
);
1187 /* ap is now owned by the component struct, so do NOT free it here */
1191 (q
= &pq
)->pq_next
= NULL
;
1192 while ((cp
= getname (ap
))) {
1194 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1195 p
->pq_text
= mh_xstrdup(cp
);
1196 p
->pq_error
= mh_xstrdup(error
);
1198 p
->pq_text
= getcpy (mp
->m_text
);
1201 q
= (q
->pq_next
= p
);
1204 for (p
= pq
.pq_next
; p
; p
= q
) {
1205 charstring_t scanl
= charstring_create (BUFSIZ
);
1209 c1
->c_c_text
->c_text
= p
->pq_text
;
1212 if (c1
->c_c_error
) {
1213 c1
->c_c_error
->c_text
= p
->pq_error
;
1217 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1218 buffer
= charstring_buffer_copy (scanl
);
1221 c2
->c_text
= add (",\n", c2
->c_text
);
1222 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1224 c2
->c_text
= add (buffer
, c2
->c_text
);
1226 charstring_free (scanl
);
1234 c2
->c_text
= add ("\n", c2
->c_text
);
1239 static struct mcomp
*
1240 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1245 c1
->c_flags
= flags
& ~INIT
;
1246 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1247 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1248 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1251 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1252 c1
->c_offset
= global
.c_offset
;
1253 c1
->c_ovoff
= global
. c_ovoff
;
1254 c1
->c_width
= c1
->c_length
= 0;
1255 c1
->c_cwidth
= global
.c_cwidth
;
1256 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1261 (*tail
)->c_next
= c1
;
1269 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1271 struct mcomp
*c1
, *c2
;
1273 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
)
1291 char *text
; /* c1's text, or the name as a fallback. */
1292 char *trimmed_prefix
;
1296 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1298 if (! utf8
&& flag
!= BODYCOMP
) {
1299 /* Don't print 8-bit bytes in header field values if not in a
1300 UTF-8 locale, as required by RFC 6532. */
1301 c1
->c_flags
|= FORCE7BIT
;
1304 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1305 /* Create a copy with trailing whitespace trimmed, for use with
1307 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1311 llim
= c1
->c_length
? c1
->c_length
: -1;
1312 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1313 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1315 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1317 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1318 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1319 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1322 if (c1
->c_flags
& CLEARTEXT
) {
1323 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1325 putstr ("\n", c1
->c_flags
);
1329 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1330 mcomp_format (c1
, c2
);
1332 if (c1
->c_flags
& CENTER
) {
1333 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1334 - c1
->c_offset
- strlen (c2
->c_text
);
1335 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1336 count
-= strlen(text
) + 2;
1337 lm
= c1
->c_offset
+ (count
/ 2);
1343 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1344 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1346 putstr(text
, c1
->c_flags
);
1347 if (flag
!= BODYCOMP
) {
1348 putstr (": ", c1
->c_flags
);
1349 if (!(c1
->c_flags
& SPLIT
))
1350 c1
->c_flags
|= HDROUTPUT
;
1353 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1355 putstr (" ", c1
->c_flags
);
1358 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1362 && !(c2
->c_flags
& HDROUTPUT
)
1363 && !(c2
->c_flags
& NOCOMPONENT
)) {
1364 if (c1
->c_flags
& UPPERCASE
)
1365 to_upper(c2
->c_name
);
1366 putstr (c2
->c_name
, c1
->c_flags
);
1367 putstr (": ", c1
->c_flags
);
1368 if (!(c1
->c_flags
& SPLIT
))
1369 c2
->c_flags
|= HDROUTPUT
;
1372 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1374 putstr (" ", c1
->c_flags
);
1376 if (c1
->c_flags
& UPPERCASE
)
1377 to_upper(c2
->c_text
);
1381 if (flag
== TWOCOMP
)
1382 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1383 : (int) strlen (c2
->c_name
) + 2;
1385 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1388 count
+= c1
->c_offset
;
1390 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1391 /* Output line, trimming trailing whitespace if requested. */
1392 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1394 putstr ("\n", c1
->c_flags
);
1395 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1397 if (flag
== BODYCOMP
1398 && !(c1
->c_flags
& NOCOMPONENT
)) {
1399 /* Output component, trimming trailing whitespace if there
1400 is no text on the line. */
1402 putstr(text
, c1
->c_flags
);
1404 putstr (trimmed_prefix
, c1
->c_flags
);
1408 /* Output line, trimming trailing whitespace if requested. */
1409 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1412 putstr ("\n", c1
->c_flags
);
1414 if (flag
== BODYCOMP
&& term
== '\n')
1415 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1417 free (trimmed_prefix
);
1422 oneline (char *stuff
, unsigned long flags
)
1430 return onelp
= NULL
;
1434 if (flags
& COMPRESS
) {
1435 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1436 if (isspace ((unsigned char) *onelp
)) {
1437 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1455 while (*onelp
&& *onelp
!= '\n')
1457 if (*onelp
== '\n') {
1461 if (flags
& LEFTADJUST
)
1462 while (*ret
== ' ' || *ret
== '\t')
1465 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1473 putstr (char *string
, unsigned long flags
)
1475 /* To not count, for the purpose of counting columns, all of
1476 the bytes of a multibyte character. */
1479 if (!column
&& lm
> 0) {
1482 putch ('\t', flags
);
1492 #ifdef MULTIBYTE_SUPPORT
1493 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1496 NMH_UNUSED (char_len
);
1500 flags
&= ~INVISIBLE
;
1501 #ifdef MULTIBYTE_SUPPORT
1502 /* mbtowc should never return 0, because *string is non-NULL. */
1503 if (char_len
<= 0) {
1504 /* Find number of bytes in next character. */
1506 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1510 /* Multibyte character, after the first byte. */
1516 putch (*string
++, flags
);
1522 putch (char ch
, unsigned long flags
)
1533 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1535 if (global
.c_flags
& BELL
)
1538 if (linefeed_typed()) {
1539 if (global
.c_flags
& CLEARSCR
)
1540 nmh_clear_screen ();
1544 row
= global
.c_length
/ 3;
1563 * If we are forwarding this message, and the first
1564 * column contains a dash, then add a dash and a space.
1566 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1571 * Increment the character count, unless
1572 * 1) In UTF-8 locale, this is other than the last byte of
1573 a multibyte character, or
1574 * 2) In C locale, will print a non-printable character.
1576 if ((flags
& FORCE7BIT
) == 0) {
1578 if ((flags
& INVISIBLE
) == 0) {
1579 /* If multibyte character, its first byte only. */
1583 /* If not an ASCII character, the replace character will be
1584 displayed. Count it. */
1585 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1592 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1593 putch ('\n', flags
);
1596 putstr (FENDNULL(ovtxt
), flags
);
1601 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1608 /* linefeed_typed() makes a single read(2) from stdin and returns true
1609 * if a linefeed character is amongst the characters read.
1610 * A read error is treated as if linefeed wasn't typed.
1612 * Typing on a TTY can cause read() to return data without typing Enter
1613 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1614 * The linefeed can also be escaped with the TTY's LNEXT character,
1615 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1616 * It's not possible to distinguish between the user typing a buffer's
1617 * worth of characters and then EOT, or more than the buffer can hold.
1618 * Either way, the result depends on ASCII LF, either from typing Enter
1619 * or an escaped Ctrl-J, being amongst the read characters.
1622 linefeed_typed(void)
1627 n
= read(0, buf
, sizeof buf
);
1629 advise("stdin", "read");
1630 return false; /* Treat as EOF. */
1633 return memchr(buf
, '\n', n
);
1644 longjmp (env
, DONE
);
1669 mhladios (char *what
, char *fmt
, ...)
1674 advertise (what
, NULL
, fmt
, ap
);
1681 mhldone (int status
)
1689 * Compile a format string used by the formatfield option and save it
1692 * We will want the {text} (and possibly {error}) components for later,
1693 * so look for them and save them if we find them.
1697 compile_formatfield(struct mcomp
*c1
)
1699 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1702 * As a note to myself and any other poor bastard who is looking through
1703 * this code in the future ....
1705 * When the format hash table is reset later on (as it almost certainly
1706 * will be), there will still be references to these components in the
1707 * compiled format instructions. Thus these component references will
1708 * be free'd when the format instructions are free'd (by fmt_free()).
1710 * So, in other words ... don't go free'ing them yourself!
1713 c1
->c_c_text
= fmt_findcomp("text");
1714 c1
->c_c_error
= fmt_findcomp("error");
1718 * Compile all of the arguments for our format list.
1720 * Iterate through the linked list of format strings and compile them.
1721 * Note that we reset the format hash table before we start, but we do NOT
1722 * reset it between calls to fmt_compile().
1727 compile_filterargs (void)
1729 struct arglist
*arg
= arglist_head
;
1736 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1741 * Search through and mark any components that are address components
1744 for (ap
= addrcomps
; *ap
; ap
++) {
1745 cptr
= fmt_findcomp (*ap
);
1747 cptr
->c_type
|= CT_ADDR
;
1752 * Filter the body of a message through a specified format program
1756 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1757 m_getfld_state_t gstate
)
1759 struct mcomp holder
;
1761 int fdinput
[2], fdoutput
[2], waitstat
;
1763 pid_t writerpid
, filterpid
;
1766 * Create pipes so we can communicate with our filter process.
1769 if (pipe(fdinput
) < 0) {
1770 die("Unable to create input pipe");
1773 if (pipe(fdoutput
) < 0) {
1774 die("Unable to create output pipe");
1778 * Here's what we're doing to do.
1780 * - Fork ourselves and start writing data to the write side of the
1781 * input pipe (fdinput[1]).
1783 * - Fork and exec our filter program. We set the standard input of
1784 * our filter program to be the read side of our input pipe (fdinput[0]).
1785 * Standard output is set to the write side of our output pipe
1788 * - We read from the read side of the output pipe (fdoutput[0]).
1790 * We're forking because that's the simplest way to prevent any deadlocks.
1791 * (without doing something like switching to non-blocking I/O and using
1792 * select or poll, and I'm not interested in doing that).
1795 switch (writerpid
= fork()) {
1798 * Our child process - just write to the filter input (fdinput[1]).
1799 * Close all other descriptors that we don't need.
1807 * Call m_getfld2() until we're no longer in the BODY state
1810 while (state
== BODY
) {
1812 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1813 advise ("pipe output", "write");
1815 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1819 * We should be done; time to exit.
1824 * Make sure we call _exit(), otherwise we may flush out the stdio
1825 * buffers that we have duplicated from the parent.
1829 die("Unable to fork for filter writer process");
1834 * Fork and exec() our filter program, after redirecting standard in
1835 * and standard out appropriately.
1838 switch (filterpid
= fork()) {
1839 char **args
, *program
;
1841 int i
, dat
[5], argp
;
1845 * Configure an argument array for us
1848 args
= argsplit(formatproc
, &program
, &argp
);
1849 args
[argp
+ filter_nargs
] = NULL
;
1857 * Pull out each argument and scan them.
1860 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1861 charstring_t scanl
= charstring_create (BUFSIZ
);
1863 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1864 args
[i
] = charstring_buffer_copy (scanl
);
1865 charstring_free (scanl
);
1866 /* Trim a trailing linefeed that fmt_scan() likes to put at
1867 * the end of the format string. */
1868 trim_suffix_c(args
[i
], '\n');
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");