]>
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/brkstring.h"
10 #include "sbr/ambigsw.h"
11 #include "sbr/pidstatus.h"
12 #include "sbr/print_version.h"
13 #include "sbr/print_help.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
16 #include "h/signals.h"
17 #include "h/addrsbr.h"
18 #include "h/fmt_scan.h"
22 #include "sbr/m_popen.h"
24 #include <sys/types.h>
25 #include "sbr/terminal.h"
29 * for a component containing addresses, ADDRFMT, if COMPRESS is also
30 * set, then addresses get split wrong (not at the spaces between commas).
31 * To fix this correctly, putstr() should know about "atomic" strings that
32 * must NOT be broken across lines. That's too difficult for right now
33 * (it turns out that there are a number of degenerate cases), so in
34 * oneline(), instead of
36 * (*onelp == '\n' && !onelp[1])
38 * being a terminating condition,
40 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
42 * is used instead. This cuts the line prematurely, and gives us a much
43 * better chance of getting things right.
52 #define MHL_SWITCHES \
53 X("bell", 0, BELLSW) \
54 X("nobell", 0, NBELLSW) \
55 X("clear", 0, CLRSW) \
56 X("noclear", 0, NCLRSW) \
57 X("folder +folder", 0, FOLDSW) \
58 X("form formfile", 0, FORMSW) \
59 X("moreproc program", 0, PROGSW) \
60 X("nomoreproc", 0, NPROGSW) \
61 X("length lines", 0, LENSW) \
62 X("width columns", 0, WIDTHSW) \
63 X("sleep seconds", 0, SLEEPSW) \
64 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
65 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
66 X("version", 0, VERSIONSW) \
67 X("help", 0, HELPSW) \
68 X("forward", -7, FORW1SW) /* interface from forw */ \
69 X("forwall", -7, FORW2SW) /* interface from forw */ \
70 X("digest list", -6, DGSTSW) \
71 X("volume number", -6, VOLUMSW) \
72 X("issue number", -5, ISSUESW) \
73 X("nobody", -6, NBODYSW) \
74 X("fmtproc program", 0, FMTPROCSW) \
75 X("nofmtproc", 0, NFMTPROCSW) \
77 #define X(sw, minchars, id) id,
78 DEFINE_SWITCH_ENUM(MHL
);
81 #define X(sw, minchars, id) { sw, minchars, id },
82 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
85 #define NOCOMPONENT 0x000001 /* don't show component name */
86 #define UPPERCASE 0x000002 /* display in all upper case */
87 #define CENTER 0x000004 /* center line */
88 #define CLEARTEXT 0x000008 /* cleartext */
89 #define EXTRA 0x000010 /* an "extra" component */
90 #define HDROUTPUT 0x000020 /* already output */
91 #define CLEARSCR 0x000040 /* clear screen */
92 #define LEFTADJUST 0x000080 /* left justify multiple lines */
93 #define COMPRESS 0x000100 /* compress text */
94 #define ADDRFMT 0x000200 /* contains addresses */
95 #define BELL 0x000400 /* sound bell at EOP */
96 #define DATEFMT 0x000800 /* contains dates */
97 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
98 #define INIT 0x002000 /* initialize component */
99 #define RTRIM 0x004000 /* trim trailing whitespace */
100 #define SPLIT 0x010000 /* split headers (don't concatenate) */
101 #define NONEWLINE 0x020000 /* don't write trailing newline */
102 #define NOWRAP 0x040000 /* Don't wrap lines ever */
103 #define FMTFILTER 0x080000 /* Filter through format filter */
104 #define INVISIBLE 0x100000 /* count byte in display columns? */
105 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
106 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
107 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
110 * A format string to be used as a command-line argument to the body
115 struct format
*a_fmt
;
117 struct arglist
*a_next
;
121 * Linked list of command line arguments for the body format filter. This
122 * USED to be in "struct mcomp", but the format API got cleaned up and even
123 * though it reduced the code we had to do, it make things more complicated
124 * for us. Specifically:
126 * - The interface to the hash table has been cleaned up, which means the
127 * rooting around in the hash table is no longer necessary (yay!). But
128 * this ALSO means that we have to make sure that we call our format
129 * compilation routines before we process the message, because the
130 * components need to be visible in the hash table so we can save them for
131 * later. So we moved them out of "mcomp" and now compile them right before
132 * header processing starts.
133 * - We also use format strings to handle other components in the mhl
134 * configuration (using "formatfield" and "decode"), but here life
135 * gets complicated: they aren't dealt with in the normal way. Instead
136 * of referring to a component like {from}, each component is processed
137 * using the special {text} component. But these format strings need to be
138 * compiled BEFORE we compile the format arguments; in the previous
139 * implementation they were compiled and scanned as the headers were
140 * read, and that would reset the hash table that we need to populate
141 * the components used by the body format filter. So we are compiling
142 * the formatfield component strings ahead of time and then scanning them
145 * Okay, fine ... this was broken before. But you know what? Fixing this
146 * the right way will make things easier down the road.
148 * One side-effect to this change: format strings are now compiled only once
149 * for components specified with "formatfield", but they are compiled for
150 * every message for format arguments.
153 static struct arglist
*arglist_head
;
154 static struct arglist
*arglist_tail
;
155 static int filter_nargs
= 0;
158 * Flags/options for each component
162 char *c_name
; /* component name */
163 char *c_text
; /* component text */
164 char *c_ovtxt
; /* text overflow indicator */
165 char *c_nfs
; /* iff FORMAT */
166 struct format
*c_fmt
; /* .. */
167 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
168 struct comp
*c_c_error
; /* Ref to {error} */
169 int c_offset
; /* left margin indentation */
170 int c_ovoff
; /* overflow indentation */
171 int c_width
; /* width of field */
172 int c_cwidth
; /* width of component */
173 int c_length
; /* length in lines */
174 unsigned long c_flags
;
175 struct mcomp
*c_next
;
178 static struct mcomp
*msghd
= NULL
;
179 static struct mcomp
*msgtl
= NULL
;
180 static struct mcomp
*fmthd
= NULL
;
181 static struct mcomp
*fmttl
= NULL
;
183 static struct mcomp global
= {
184 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
187 static struct mcomp holder
= {
188 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
193 unsigned long p_flags
;
196 static struct pair pairs
[] = {
199 { "Sender", ADDRFMT
},
200 { "Reply-To", ADDRFMT
},
204 { "Resent-Date", DATEFMT
},
205 { "Resent-From", ADDRFMT
},
206 { "Resent-Sender", ADDRFMT
},
207 { "Resent-Reply-To", ADDRFMT
},
208 { "Resent-To", ADDRFMT
},
209 { "Resent-cc", ADDRFMT
},
210 { "Resent-Bcc", ADDRFMT
},
220 static struct triple triples
[] = {
221 { "nocomponent", NOCOMPONENT
, 0 },
222 { "uppercase", UPPERCASE
, 0 },
223 { "nouppercase", 0, UPPERCASE
},
224 { "center", CENTER
, 0 },
225 { "nocenter", 0, CENTER
},
226 { "clearscreen", CLEARSCR
, 0 },
227 { "noclearscreen", 0, CLEARSCR
},
228 { "noclear", 0, CLEARSCR
},
229 { "leftadjust", LEFTADJUST
, 0 },
230 { "noleftadjust", 0, LEFTADJUST
},
231 { "compress", COMPRESS
, 0 },
232 { "nocompress", 0, COMPRESS
},
233 { "split", SPLIT
, 0 },
234 { "nosplit", 0, SPLIT
},
235 { "rtrim", RTRIM
, 0 },
236 { "nortrim", 0, RTRIM
},
237 { "addrfield", ADDRFMT
, DATEFMT
},
239 { "nobell", 0, BELL
},
240 { "datefield", DATEFMT
, ADDRFMT
},
241 { "newline", 0, NONEWLINE
},
242 { "nonewline", NONEWLINE
, 0 },
243 { "wrap", 0, NOWRAP
},
244 { "nowrap", NOWRAP
, 0 },
245 { "format", FMTFILTER
, 0 },
246 { "noformat", 0, FMTFILTER
},
250 static char *addrcomps
[] = {
267 static int bellflg
= 0;
268 static int clearflg
= 0;
269 static int dashstuff
= 0;
270 static bool dobody
= true;
274 static int sleepsw
= NOTOK
;
276 static char *digest
= NULL
;
277 static int volume
= 0;
278 static int issue
= 0;
280 static int exitstat
= 0;
281 static bool mhldebug
;
283 static int filesize
= 0;
288 static int ontty
= NOTTY
;
291 static unsigned int column
;
297 static unsigned int wid
;
305 static int num_ignores
= 0;
306 static char *ignores
[MAXARGS
];
310 static char delim3
[] = /* from forw.c */
311 "\n----------------------------------------------------------------------\n\n";
312 static char delim4
[] = "\n------------------------------\n\n";
317 static void mhl_format (char *, int, int);
318 static int evalvar (struct mcomp
*);
319 static int ptoi (char *, int *);
320 static int ptos (char *, char **);
321 static char *parse (void);
322 static void process (char *, char *, int, int);
323 static void mhlfile (FILE *, char *, int, int);
324 static int mcomp_flags (char *) PURE
;
325 static char *mcomp_add (unsigned long, char *, char *);
326 static void mcomp_format (struct mcomp
*, struct mcomp
*);
327 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
328 static void free_queue (struct mcomp
**, struct mcomp
**);
329 static void putcomp (struct mcomp
*, struct mcomp
*, int);
330 static char *oneline (char *, unsigned long);
331 static void putstr (char *, unsigned long);
332 static void putch (char, unsigned long);
333 static bool linefeed_typed(void);
334 static void intrser (int);
335 static void pipeser (int);
336 static void quitser (int);
337 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
338 static void mhldone (int) NORETURN
;
339 static void filterbody (struct mcomp
*, char *, int, int,
341 static void compile_formatfield(struct mcomp
*);
342 static void compile_filterargs (void);
346 mhl (int argc
, char **argv
)
350 unsigned int i
, vecp
= 0;
352 char *cp
, *folder
= NULL
, *form
= NULL
;
353 char buf
[BUFSIZ
], *files
[MAXARGS
];
354 char **argp
, **arguments
;
356 /* Need this if called from main() of show(1). */
357 invo_name
= r1bindex (argv
[0], '/');
359 arguments
= getarguments (invo_name
, argc
, argv
, 1);
362 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
365 while ((cp
= *argp
++)) {
367 switch (smatch (++cp
, mhlswitches
)) {
369 ambigsw (cp
, mhlswitches
);
372 mhladios (NULL
, "-%s unknown\n", cp
);
375 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
376 print_help (buf
, mhlswitches
, 1);
379 print_version(invo_name
);
397 if (!(folder
= *argp
++) || *folder
== '-')
398 mhladios (NULL
, "missing argument to %s", argp
[-2]);
401 if (!(form
= *argp
++) || *form
== '-')
402 mhladios (NULL
, "missing argument to %s", argp
[-2]);
406 if (!(cp
= *argp
++) || *cp
== '-')
407 mhladios (NULL
, "missing argument to %s", argp
[-2]);
408 sleepsw
= atoi (cp
);/* ZERO ok! */
412 if (!(moreproc
= *argp
++) || *moreproc
== '-')
413 mhladios (NULL
, "missing argument to %s", argp
[-2]);
420 if (!(formatproc
= *argp
++) || *formatproc
== '-')
421 mhladios (NULL
, "missing argument to %s", argp
[-2]);
428 if (!(cp
= *argp
++) || *cp
== '-')
429 mhladios (NULL
, "missing argument to %s", argp
[-2]);
430 if ((length
= atoi (cp
)) < 1)
431 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
434 if (!(cp
= *argp
++) || *cp
== '-')
435 mhladios (NULL
, "missing argument to %s", argp
[-2]);
436 if ((width
= atoi (cp
)) < 1)
437 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
441 if (!(digest
= *argp
++) || *digest
== '-')
442 mhladios (NULL
, "missing argument to %s", argp
[-2]);
445 if (!(cp
= *argp
++) || *cp
== '-')
446 mhladios (NULL
, "missing argument to %s", argp
[-2]);
447 if ((issue
= atoi (cp
)) < 1)
448 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
451 if (!(cp
= *argp
++) || *cp
== '-')
452 mhladios (NULL
, "missing argument to %s", argp
[-2]);
453 if ((volume
= atoi (cp
)) < 1)
454 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
462 clearflg
= -1;/* XXX */
466 dashstuff
= 1; /* ternary logic */
469 dashstuff
= -1; /* ternary logic */
481 folder
= getenv ("mhfolder");
483 if (isatty (fileno (stdout
))) {
484 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
485 SIGNAL2 (SIGPIPE
, pipeser
);
486 m_popen(moreproc
, false);
489 SIGNAL (SIGINT
, SIG_IGN
);
490 SIGNAL2 (SIGQUIT
, quitser
);
497 mhl_format (form
? form
: mhlformat
, length
, width
);
500 process (folder
, NULL
, 1, vecp
= 1);
502 for (i
= 0; i
< vecp
; i
++)
503 process (folder
, files
[i
], i
+ 1, vecp
);
508 fputs(delim4
, stdout
);
510 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
512 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
513 digest
, volume
, issue
);
516 for (cp
= buf
+ i
; i
> 1; i
--)
523 printf ("\n------- End of Forwarded Message%s\n",
529 mhladios("output", "error writing");
532 if (clearflg
> 0 && ontty
== NOTTY
)
543 mhl_format (char *file
, int length
, int width
)
547 char *ap
, name
[NAMESZ
];
551 static dev_t dev
= 0;
552 static ino_t ino
= 0;
553 static time_t mtime
= 0;
556 if (stat (etcpath (file
), &st
) != NOTOK
557 && mtime
== st
.st_mtime
561 free_queue (&fmthd
, &fmttl
);
564 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
565 mhladios (file
, "unable to open format file");
567 if (fstat (fileno (fp
), &st
) != NOTOK
) {
573 global
.c_ovtxt
= global
.c_nfs
= NULL
;
577 if ((i
= sc_width ()) > 5)
579 global
.c_cwidth
= -1;
580 if ((i
= sc_length ()) > 5)
581 global
.c_length
= i
- 1;
582 global
.c_flags
= BELL
; /* BELL is default */
583 *(ip
= ignores
) = NULL
;
586 while (vfgets (fp
, &ap
) == OK
) {
591 trim_suffix_c(bp
, '\n');
594 (void) 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 (mh_xstrdup(++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 mhladios (NULL
, "format file syntax error: %s", bp
);
635 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
636 while (*parptr
== ':' || *parptr
== ',') {
639 mhladios (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
= mh_xstrdup(global
.c_nfs
);
645 compile_formatfield(c1
);
647 } else if (c1
->c_flags
& ADDRFMT
) {
648 if (global
.c_flags
& ADDRFMT
) {
649 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
650 compile_formatfield(c1
);
657 mhladios (NULL
, "format file syntax error: %s", bp
);
663 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
666 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
667 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
668 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
669 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
670 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
671 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
672 c1
->c_cwidth
, c1
->c_length
);
673 fprintf (stderr
, "\tflags=%s\n",
674 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
680 global
.c_flags
|= CLEARSCR
;
683 global
.c_flags
&= ~CLEARSCR
;
686 switch (bellflg
) { /* command line may override format file */
688 global
.c_flags
|= BELL
;
691 global
.c_flags
&= ~BELL
;
696 global
.c_length
= length
;
698 global
.c_width
= width
;
699 if (global
.c_length
< 5)
700 global
.c_length
= 10000;
701 if (global
.c_width
< 5)
702 global
.c_width
= 10000;
707 evalvar (struct mcomp
*c1
)
709 char *cp
, name
[NAMESZ
];
714 strncpy (name
, parse(), sizeof(name
));
716 if (!strcasecmp (name
, "component")) {
717 if (ptos (name
, &c1
->c_text
))
719 c1
->c_flags
&= ~NOCOMPONENT
;
723 if (!strcasecmp (name
, "overflowtext"))
724 return ptos (name
, &c1
->c_ovtxt
);
726 if (!strcasecmp (name
, "formatfield")) {
727 if (ptos (name
, &cp
))
729 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
730 compile_formatfield(c1
);
731 c1
->c_flags
|= FORMAT
;
735 if (!strcasecmp (name
, "decode")) {
736 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
737 compile_formatfield(c1
);
738 c1
->c_flags
|= FORMAT
;
742 if (!strcasecmp (name
, "offset"))
743 return ptoi (name
, &c1
->c_offset
);
744 if (!strcasecmp (name
, "overflowoffset"))
745 return ptoi (name
, &c1
->c_ovoff
);
746 if (!strcasecmp (name
, "width"))
747 return ptoi (name
, &c1
->c_width
);
748 if (!strcasecmp (name
, "compwidth"))
749 return ptoi (name
, &c1
->c_cwidth
);
750 if (!strcasecmp (name
, "length"))
751 return ptoi (name
, &c1
->c_length
);
752 if (!strcasecmp (name
, "nodashstuffing"))
753 return dashstuff
= -1;
755 for (ap
= triples
; ap
->t_name
; ap
++)
756 if (!strcasecmp (ap
->t_name
, name
)) {
757 c1
->c_flags
|= ap
->t_on
;
758 c1
->c_flags
&= ~ap
->t_off
;
762 if (!strcasecmp (name
, "formatarg")) {
763 struct arglist
*args
;
765 if (ptos (name
, &cp
))
768 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
769 inform("format filters are currently only supported on "
770 "the \"body\" component");
777 arglist_tail
->a_next
= args
;
784 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
795 ptoi (char *name
, int *i
)
799 if (*parptr
++ != '=' || !*(cp
= parse ())) {
800 inform("missing argument to variable %s", name
);
810 ptos (char *name
, char **s
)
814 if (*parptr
++ != '=') {
815 inform("missing argument to variable %s", name
);
819 if (*parptr
!= '"') {
821 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
825 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
826 if (*parptr
== QUOTE
)
833 if ((*parptr
= c
) == '"')
844 static char result
[NAMESZ
];
846 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
864 * Process one file/message
868 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
870 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
876 /* volatile to prevent "might be clobbered" warning from gcc: */
877 char *volatile fname2
= fname
? fname
: "(stdin)";
882 switch (setjmp (env
)) {
885 fp
= 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
) : mh_xstrdup(fname2
);
901 SIGNAL (SIGINT
, intrser
);
902 mhlfile (fp
, cp
, ofilen
, ofilec
);
905 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
906 fmt_free(ap
->a_fmt
, 0);
916 SIGNAL (SIGINT
, SIG_IGN
);
917 if (fp
!= stdin
&& fp
!= NULL
)
920 holder
.c_text
= NULL
;
921 free_queue (&msghd
, &msgtl
);
922 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
923 c1
->c_flags
&= ~HDROUTPUT
;
931 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
934 struct mcomp
*c1
, *c2
, *c3
;
935 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
936 m_getfld_state_t gstate
;
938 compile_filterargs();
942 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
944 fputs("\n-------", stdout
);
946 printf (" Forwarded Message%s", PLURALS(ofilec
));
948 printf (" Message %d", ofilen
);
956 if ((global
.c_flags
& CLEARSCR
))
961 printf (">>> %s\n\n", mname
);
967 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
970 printf ("Press <return> to list \"%s\"...", mname
);
974 if (ofilec
== 1 || linefeed_typed()) {
975 if ((global
.c_flags
& CLEARSCR
))
989 printf (">>> %s\n\n", mname
);
995 gstate
= m_getfld_state_init(fp
);
997 int bufsz
= sizeof buf
;
998 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1001 bucket
= fmt_addcomptext(name
, buf
);
1002 for (ip
= ignores
; *ip
; ip
++)
1003 if (!strcasecmp (name
, *ip
)) {
1004 while (state
== FLDPLUS
) {
1006 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1007 fmt_appendcomp(bucket
, name
, buf
);
1014 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1015 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1018 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1019 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1020 if (!strcasecmp (FENDNULL(c1
->c_name
),
1021 FENDNULL(c3
->c_name
))) {
1023 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1027 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1028 while (state
== FLDPLUS
) {
1030 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1031 c1
->c_text
= add (buf
, c1
->c_text
);
1032 fmt_appendcomp(bucket
, name
, buf
);
1035 c1
->c_flags
|= EXTRA
;
1041 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1042 if (c1
->c_flags
& CLEARTEXT
) {
1043 putcomp (c1
, c1
, ONECOMP
);
1047 !strcasecmp (c1
->c_name
, "messagename")) {
1048 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1050 putcomp (c1
, &holder
, ONECOMP
);
1051 free (holder
.c_text
);
1052 holder
.c_text
= NULL
;
1055 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1056 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1057 if (c2
->c_flags
& EXTRA
)
1058 putcomp (c1
, c2
, TWOCOMP
);
1061 if (dobody
&& (!c1
->c_name
||
1062 !strcasecmp (c1
->c_name
, "body"))) {
1063 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1064 formatproc
!= NULL
) {
1065 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1067 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1068 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1069 while (state
== BODY
) {
1070 putcomp (c1
, &holder
, BODYCOMP
);
1072 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1075 free (holder
.c_text
);
1076 holder
.c_text
= NULL
;
1080 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1081 if (!strcasecmp (FENDNULL(c2
->c_name
),
1082 FENDNULL(c1
->c_name
))) {
1083 putcomp (c1
, c2
, ONECOMP
);
1084 if (!(c1
->c_flags
& SPLIT
))
1088 m_getfld_state_destroy (&gstate
);
1093 inform("format error in message %s", mname
);
1095 m_getfld_state_destroy (&gstate
);
1099 mhladios (NULL
, "getfld() returned %d", state
);
1106 mcomp_flags (char *name
)
1110 for (ap
= pairs
; ap
->p_name
; ap
++)
1111 if (!strcasecmp (ap
->p_name
, name
))
1119 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1123 if (!(flags
& ADDRFMT
))
1124 return add (s1
, s2
);
1126 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1129 return add (s1
, add (",\n", s2
));
1136 struct pqpair
*pq_next
;
1141 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1146 struct pqpair
*p
, *q
;
1148 struct mailname
*mp
;
1155 dat
[3] = BUFSIZ
- 1;
1158 if (!(c1
->c_flags
& ADDRFMT
)) {
1159 charstring_t scanl
= charstring_create (BUFSIZ
);
1162 c1
->c_c_text
->c_text
= ap
;
1163 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1167 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1168 /* Don't need to append a newline, dctime() already did */
1169 c2
->c_text
= charstring_buffer_copy (scanl
);
1170 charstring_free (scanl
);
1172 /* ap is now owned by the component struct, so do NOT free it here */
1176 (q
= &pq
)->pq_next
= NULL
;
1177 while ((cp
= getname (ap
))) {
1179 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1180 p
->pq_text
= mh_xstrdup(cp
);
1181 p
->pq_error
= mh_xstrdup(error
);
1183 p
->pq_text
= getcpy (mp
->m_text
);
1186 q
= (q
->pq_next
= p
);
1189 for (p
= pq
.pq_next
; p
; p
= q
) {
1190 charstring_t scanl
= charstring_create (BUFSIZ
);
1194 c1
->c_c_text
->c_text
= p
->pq_text
;
1197 if (c1
->c_c_error
) {
1198 c1
->c_c_error
->c_text
= p
->pq_error
;
1202 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1203 buffer
= charstring_buffer_copy (scanl
);
1206 c2
->c_text
= add (",\n", c2
->c_text
);
1207 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1209 c2
->c_text
= add (buffer
, c2
->c_text
);
1211 charstring_free (scanl
);
1219 c2
->c_text
= add ("\n", c2
->c_text
);
1224 static struct mcomp
*
1225 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1230 c1
->c_flags
= flags
& ~INIT
;
1231 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1232 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1233 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1236 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1237 c1
->c_offset
= global
.c_offset
;
1238 c1
->c_ovoff
= global
. c_ovoff
;
1239 c1
->c_width
= c1
->c_length
= 0;
1240 c1
->c_cwidth
= global
.c_cwidth
;
1241 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1246 (*tail
)->c_next
= c1
;
1254 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1256 struct mcomp
*c1
, *c2
;
1258 for (c1
= *head
; c1
; c1
= c2
) {
1265 fmt_free (c1
->c_fmt
, 0);
1269 *head
= *tail
= NULL
;
1274 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1276 char *text
; /* c1's text, or the name as a fallback. */
1277 char *trimmed_prefix
;
1281 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1283 if (! utf8
&& flag
!= BODYCOMP
) {
1284 /* Don't print 8-bit bytes in header field values if not in a
1285 UTF-8 locale, as required by RFC 6532. */
1286 c1
->c_flags
|= FORCE7BIT
;
1289 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1290 /* Create a copy with trailing whitespace trimmed, for use with
1292 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
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 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1304 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1307 if (c1
->c_flags
& CLEARTEXT
) {
1308 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1310 putstr ("\n", c1
->c_flags
);
1314 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1315 mcomp_format (c1
, c2
);
1317 if (c1
->c_flags
& CENTER
) {
1318 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1319 - c1
->c_offset
- strlen (c2
->c_text
);
1320 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1321 count
-= strlen(text
) + 2;
1322 lm
= c1
->c_offset
+ (count
/ 2);
1328 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1329 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1331 putstr(text
, c1
->c_flags
);
1332 if (flag
!= BODYCOMP
) {
1333 putstr (": ", c1
->c_flags
);
1334 if (!(c1
->c_flags
& SPLIT
))
1335 c1
->c_flags
|= HDROUTPUT
;
1338 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1340 putstr (" ", c1
->c_flags
);
1343 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1347 && !(c2
->c_flags
& HDROUTPUT
)
1348 && !(c2
->c_flags
& NOCOMPONENT
)) {
1349 if (c1
->c_flags
& UPPERCASE
)
1350 to_upper(c2
->c_name
);
1351 putstr (c2
->c_name
, c1
->c_flags
);
1352 putstr (": ", c1
->c_flags
);
1353 if (!(c1
->c_flags
& SPLIT
))
1354 c2
->c_flags
|= HDROUTPUT
;
1357 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1359 putstr (" ", c1
->c_flags
);
1361 if (c1
->c_flags
& UPPERCASE
)
1362 to_upper(c2
->c_text
);
1366 if (flag
== TWOCOMP
)
1367 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1368 : (int) strlen (c2
->c_name
) + 2;
1370 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1373 count
+= c1
->c_offset
;
1375 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1376 /* Output line, trimming trailing whitespace if requested. */
1377 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1379 putstr ("\n", c1
->c_flags
);
1380 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1382 if (flag
== BODYCOMP
1383 && !(c1
->c_flags
& NOCOMPONENT
)) {
1384 /* Output component, trimming trailing whitespace if there
1385 is no text on the line. */
1387 putstr(text
, c1
->c_flags
);
1389 putstr (trimmed_prefix
, c1
->c_flags
);
1393 /* Output line, trimming trailing whitespace if requested. */
1394 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1397 putstr ("\n", c1
->c_flags
);
1399 if (flag
== BODYCOMP
&& term
== '\n')
1400 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1402 free (trimmed_prefix
);
1407 oneline (char *stuff
, unsigned long flags
)
1415 return onelp
= NULL
;
1419 if (flags
& COMPRESS
) {
1420 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1421 if (isspace ((unsigned char) *onelp
)) {
1422 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1440 while (*onelp
&& *onelp
!= '\n')
1442 if (*onelp
== '\n') {
1446 if (flags
& LEFTADJUST
)
1447 while (*ret
== ' ' || *ret
== '\t')
1450 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1458 putstr (char *string
, unsigned long flags
)
1460 /* To not count, for the purpose of counting columns, all of
1461 the bytes of a multibyte character. */
1464 if (!column
&& lm
> 0) {
1467 putch ('\t', flags
);
1477 #ifdef MULTIBYTE_SUPPORT
1478 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1481 NMH_UNUSED (char_len
);
1485 flags
&= ~INVISIBLE
;
1486 #ifdef MULTIBYTE_SUPPORT
1487 /* mbtowc should never return 0, because *string is non-NULL. */
1488 if (char_len
<= 0) {
1489 /* Find number of bytes in next character. */
1491 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1495 /* Multibyte character, after the first byte. */
1501 putch (*string
++, flags
);
1507 putch (char ch
, unsigned long flags
)
1518 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1520 if (global
.c_flags
& BELL
)
1523 if (linefeed_typed()) {
1524 if (global
.c_flags
& CLEARSCR
)
1525 nmh_clear_screen ();
1529 row
= global
.c_length
/ 3;
1548 * If we are forwarding this message, and the first
1549 * column contains a dash, then add a dash and a space.
1551 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1556 * Increment the character count, unless
1557 * 1) In UTF-8 locale, this is other than the last byte of
1558 a multibyte character, or
1559 * 2) In C locale, will print a non-printable character.
1561 if ((flags
& FORCE7BIT
) == 0) {
1563 if ((flags
& INVISIBLE
) == 0) {
1564 /* If multibyte character, its first byte only. */
1568 /* If not an ASCII character, the replace character will be
1569 displayed. Count it. */
1570 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1577 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1578 putch ('\n', flags
);
1581 putstr (FENDNULL(ovtxt
), flags
);
1586 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1593 /* linefeed_typed() makes a single read(2) from stdin and returns true
1594 * if a linefeed character is amongst the characters read.
1595 * A read error is treated as if linefeed wasn't typed.
1597 * Typing on a TTY can cause read() to return data without typing Enter
1598 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1599 * The linefeed can also be escaped with the TTY's LNEXT character,
1600 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1601 * It's not possible to distinguish between the user typing a buffer's
1602 * worth of characters and then EOT, or more than the buffer can hold.
1603 * Either way, the result depends on ASCII LF, either from typing Enter
1604 * or an escaped Ctrl-J, being amongst the read characters.
1607 linefeed_typed(void)
1612 n
= read(0, buf
, sizeof buf
);
1614 advise("stdin", "read");
1615 return false; /* Treat as EOF. */
1618 return memchr(buf
, '\n', n
);
1629 longjmp (env
, DONE
);
1654 mhladios (char *what
, char *fmt
, ...)
1659 advertise (what
, NULL
, fmt
, ap
);
1666 mhldone (int status
)
1674 * Compile a format string used by the formatfield option and save it
1677 * We will want the {text} (and possibly {error}) components for later,
1678 * so look for them and save them if we find them.
1682 compile_formatfield(struct mcomp
*c1
)
1684 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1687 * As a note to myself and any other poor bastard who is looking through
1688 * this code in the future ....
1690 * When the format hash table is reset later on (as it almost certainly
1691 * will be), there will still be references to these components in the
1692 * compiled format instructions. Thus these component references will
1693 * be free'd when the format instructions are free'd (by fmt_free()).
1695 * So, in other words ... don't go free'ing them yourself!
1698 c1
->c_c_text
= fmt_findcomp("text");
1699 c1
->c_c_error
= fmt_findcomp("error");
1703 * Compile all of the arguments for our format list.
1705 * Iterate through the linked list of format strings and compile them.
1706 * Note that we reset the format hash table before we start, but we do NOT
1707 * reset it between calls to fmt_compile().
1712 compile_filterargs (void)
1714 struct arglist
*arg
= arglist_head
;
1721 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1726 * Search through and mark any components that are address components
1729 for (ap
= addrcomps
; *ap
; ap
++) {
1730 cptr
= fmt_findcomp (*ap
);
1732 cptr
->c_type
|= CT_ADDR
;
1737 * Filter the body of a message through a specified format program
1741 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1742 m_getfld_state_t gstate
)
1744 struct mcomp holder
;
1746 int fdinput
[2], fdoutput
[2], waitstat
;
1748 pid_t writerpid
, filterpid
;
1751 * Create pipes so we can communicate with our filter process.
1754 if (pipe(fdinput
) < 0) {
1755 die("Unable to create input pipe");
1758 if (pipe(fdoutput
) < 0) {
1759 die("Unable to create output pipe");
1763 * Here's what we're doing to do.
1765 * - Fork ourselves and start writing data to the write side of the
1766 * input pipe (fdinput[1]).
1768 * - Fork and exec our filter program. We set the standard input of
1769 * our filter program to be the read side of our input pipe (fdinput[0]).
1770 * Standard output is set to the write side of our output pipe
1773 * - We read from the read side of the output pipe (fdoutput[0]).
1775 * We're forking because that's the simplest way to prevent any deadlocks.
1776 * (without doing something like switching to non-blocking I/O and using
1777 * select or poll, and I'm not interested in doing that).
1780 switch (writerpid
= fork()) {
1783 * Our child process - just write to the filter input (fdinput[1]).
1784 * Close all other descriptors that we don't need.
1792 * Call m_getfld2() until we're no longer in the BODY state
1795 while (state
== BODY
) {
1797 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1798 advise ("pipe output", "write");
1800 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1804 * We should be done; time to exit.
1809 * Make sure we call _exit(), otherwise we may flush out the stdio
1810 * buffers that we have duplicated from the parent.
1814 die("Unable to fork for filter writer process");
1819 * Fork and exec() our filter program, after redirecting standard in
1820 * and standard out appropriately.
1823 switch (filterpid
= fork()) {
1824 char **args
, *program
;
1826 int i
, dat
[5], s
, argp
;
1830 * Configure an argument array for us
1833 args
= argsplit(formatproc
, &program
, &argp
);
1834 args
[argp
+ filter_nargs
] = NULL
;
1842 * Pull out each argument and scan them.
1845 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1846 charstring_t scanl
= charstring_create (BUFSIZ
);
1848 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1849 args
[i
] = charstring_buffer_copy (scanl
);
1850 charstring_free (scanl
);
1852 * fmt_scan likes to put a trailing newline at the end of the
1853 * format string. If we have one, get rid of it.
1855 s
= strlen(args
[i
]);
1856 if (args
[i
][s
- 1] == '\n')
1857 args
[i
][s
- 1] = '\0';
1860 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1864 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1865 adios("formatproc", "Unable to dup2() standard input");
1867 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1868 adios("formatproc", "Unable to dup2() standard output");
1872 * Close everything (especially the old input and output
1873 * descriptors, since they've been dup'd to stdin and stdout),
1874 * and exec the formatproc.
1882 execvp(formatproc
, args
);
1884 adios(formatproc
, "Unable to execute filter");
1889 die("Unable to fork format program");
1893 * Close everything except our reader (fdoutput[0]);
1901 * As we read in this data, send it to putcomp
1904 holder
.c_text
= buf
;
1906 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1908 putcomp(c1
, &holder
, BODYCOMP
);
1912 die("reading from formatproc");
1916 * See if we got any errors along the way. I'm a little leery of calling
1917 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1920 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1921 if (errno
!= ECHILD
) {
1922 adios("filterproc", "Unable to determine status");
1925 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1926 pidstatus(waitstat
, stderr
, "filterproc");
1930 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1931 if (errno
!= ECHILD
) {
1932 adios("writer process", "Unable to determine status");
1936 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1937 pidstatus(waitstat
, stderr
, "writer process");