]>
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/pidstatus.h"
10 #include "sbr/print_version.h"
11 #include "sbr/print_help.h"
12 #include "sbr/arglist.h"
13 #include "sbr/error.h"
14 #include "h/signals.h"
15 #include "h/addrsbr.h"
16 #include "h/fmt_scan.h"
20 #include "sbr/m_popen.h"
22 #include <sys/types.h>
23 #include "sbr/terminal.h"
27 * for a component containing addresses, ADDRFMT, if COMPRESS is also
28 * set, then addresses get split wrong (not at the spaces between commas).
29 * To fix this correctly, putstr() should know about "atomic" strings that
30 * must NOT be broken across lines. That's too difficult for right now
31 * (it turns out that there are a number of degenerate cases), so in
32 * oneline(), instead of
34 * (*onelp == '\n' && !onelp[1])
36 * being a terminating condition,
38 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
40 * is used instead. This cuts the line prematurely, and gives us a much
41 * better chance of getting things right.
50 #define MHL_SWITCHES \
51 X("bell", 0, BELLSW) \
52 X("nobell", 0, NBELLSW) \
53 X("clear", 0, CLRSW) \
54 X("noclear", 0, NCLRSW) \
55 X("folder +folder", 0, FOLDSW) \
56 X("form formfile", 0, FORMSW) \
57 X("moreproc program", 0, PROGSW) \
58 X("nomoreproc", 0, NPROGSW) \
59 X("length lines", 0, LENSW) \
60 X("width columns", 0, WIDTHSW) \
61 X("sleep seconds", 0, SLEEPSW) \
62 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
63 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
64 X("version", 0, VERSIONSW) \
65 X("help", 0, HELPSW) \
66 X("forward", -7, FORW1SW) /* interface from forw */ \
67 X("forwall", -7, FORW2SW) /* interface from forw */ \
68 X("digest list", -6, DGSTSW) \
69 X("volume number", -6, VOLUMSW) \
70 X("issue number", -5, ISSUESW) \
71 X("nobody", -6, NBODYSW) \
72 X("fmtproc program", 0, FMTPROCSW) \
73 X("nofmtproc", 0, NFMTPROCSW) \
75 #define X(sw, minchars, id) id,
76 DEFINE_SWITCH_ENUM(MHL
);
79 #define X(sw, minchars, id) { sw, minchars, id },
80 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
83 #define NOCOMPONENT 0x000001 /* don't show component name */
84 #define UPPERCASE 0x000002 /* display in all upper case */
85 #define CENTER 0x000004 /* center line */
86 #define CLEARTEXT 0x000008 /* cleartext */
87 #define EXTRA 0x000010 /* an "extra" component */
88 #define HDROUTPUT 0x000020 /* already output */
89 #define CLEARSCR 0x000040 /* clear screen */
90 #define LEFTADJUST 0x000080 /* left justify multiple lines */
91 #define COMPRESS 0x000100 /* compress text */
92 #define ADDRFMT 0x000200 /* contains addresses */
93 #define BELL 0x000400 /* sound bell at EOP */
94 #define DATEFMT 0x000800 /* contains dates */
95 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
96 #define INIT 0x002000 /* initialize component */
97 #define RTRIM 0x004000 /* trim trailing whitespace */
98 #define SPLIT 0x010000 /* split headers (don't concatenate) */
99 #define NONEWLINE 0x020000 /* don't write trailing newline */
100 #define NOWRAP 0x040000 /* Don't wrap lines ever */
101 #define FMTFILTER 0x080000 /* Filter through format filter */
102 #define INVISIBLE 0x100000 /* count byte in display columns? */
103 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
104 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
105 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
108 * A format string to be used as a command-line argument to the body
113 struct format
*a_fmt
;
115 struct arglist
*a_next
;
119 * Linked list of command line arguments for the body format filter. This
120 * USED to be in "struct mcomp", but the format API got cleaned up and even
121 * though it reduced the code we had to do, it make things more complicated
122 * for us. Specifically:
124 * - The interface to the hash table has been cleaned up, which means the
125 * rooting around in the hash table is no longer necessary (yay!). But
126 * this ALSO means that we have to make sure that we call our format
127 * compilation routines before we process the message, because the
128 * components need to be visible in the hash table so we can save them for
129 * later. So we moved them out of "mcomp" and now compile them right before
130 * header processing starts.
131 * - We also use format strings to handle other components in the mhl
132 * configuration (using "formatfield" and "decode"), but here life
133 * gets complicated: they aren't dealt with in the normal way. Instead
134 * of referring to a component like {from}, each component is processed
135 * using the special {text} component. But these format strings need to be
136 * compiled BEFORE we compile the format arguments; in the previous
137 * implementation they were compiled and scanned as the headers were
138 * read, and that would reset the hash table that we need to populate
139 * the components used by the body format filter. So we are compiling
140 * the formatfield component strings ahead of time and then scanning them
143 * Okay, fine ... this was broken before. But you know what? Fixing this
144 * the right way will make things easier down the road.
146 * One side-effect to this change: format strings are now compiled only once
147 * for components specified with "formatfield", but they are compiled for
148 * every message for format arguments.
151 static struct arglist
*arglist_head
;
152 static struct arglist
*arglist_tail
;
153 static int filter_nargs
= 0;
156 * Flags/options for each component
160 char *c_name
; /* component name */
161 char *c_text
; /* component text */
162 char *c_ovtxt
; /* text overflow indicator */
163 char *c_nfs
; /* iff FORMAT */
164 struct format
*c_fmt
; /* .. */
165 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
166 struct comp
*c_c_error
; /* Ref to {error} */
167 int c_offset
; /* left margin indentation */
168 int c_ovoff
; /* overflow indentation */
169 int c_width
; /* width of field */
170 int c_cwidth
; /* width of component */
171 int c_length
; /* length in lines */
172 unsigned long c_flags
;
173 struct mcomp
*c_next
;
176 static struct mcomp
*msghd
= NULL
;
177 static struct mcomp
*msgtl
= NULL
;
178 static struct mcomp
*fmthd
= NULL
;
179 static struct mcomp
*fmttl
= NULL
;
181 static struct mcomp global
= {
182 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
185 static struct mcomp holder
= {
186 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
191 unsigned long p_flags
;
194 static struct pair pairs
[] = {
197 { "Sender", ADDRFMT
},
198 { "Reply-To", ADDRFMT
},
202 { "Resent-Date", DATEFMT
},
203 { "Resent-From", ADDRFMT
},
204 { "Resent-Sender", ADDRFMT
},
205 { "Resent-Reply-To", ADDRFMT
},
206 { "Resent-To", ADDRFMT
},
207 { "Resent-cc", ADDRFMT
},
208 { "Resent-Bcc", ADDRFMT
},
218 static struct triple triples
[] = {
219 { "nocomponent", NOCOMPONENT
, 0 },
220 { "uppercase", UPPERCASE
, 0 },
221 { "nouppercase", 0, UPPERCASE
},
222 { "center", CENTER
, 0 },
223 { "nocenter", 0, CENTER
},
224 { "clearscreen", CLEARSCR
, 0 },
225 { "noclearscreen", 0, CLEARSCR
},
226 { "noclear", 0, CLEARSCR
},
227 { "leftadjust", LEFTADJUST
, 0 },
228 { "noleftadjust", 0, LEFTADJUST
},
229 { "compress", COMPRESS
, 0 },
230 { "nocompress", 0, COMPRESS
},
231 { "split", SPLIT
, 0 },
232 { "nosplit", 0, SPLIT
},
233 { "rtrim", RTRIM
, 0 },
234 { "nortrim", 0, RTRIM
},
235 { "addrfield", ADDRFMT
, DATEFMT
},
237 { "nobell", 0, BELL
},
238 { "datefield", DATEFMT
, ADDRFMT
},
239 { "newline", 0, NONEWLINE
},
240 { "nonewline", NONEWLINE
, 0 },
241 { "wrap", 0, NOWRAP
},
242 { "nowrap", NOWRAP
, 0 },
243 { "format", FMTFILTER
, 0 },
244 { "noformat", 0, FMTFILTER
},
248 static char *addrcomps
[] = {
265 static int bellflg
= 0;
266 static int clearflg
= 0;
267 static int dashstuff
= 0;
268 static bool dobody
= true;
272 static int sleepsw
= NOTOK
;
274 static char *digest
= NULL
;
275 static int volume
= 0;
276 static int issue
= 0;
278 static int exitstat
= 0;
279 static bool mhldebug
;
281 static int filesize
= 0;
286 static int ontty
= NOTTY
;
289 static unsigned int column
;
295 static unsigned int wid
;
303 static int num_ignores
= 0;
304 static char *ignores
[MAXARGS
];
308 static char delim3
[] = /* from forw.c */
309 "\n----------------------------------------------------------------------\n\n";
310 static char delim4
[] = "\n------------------------------\n\n";
315 static void mhl_format (char *, int, int);
316 static int evalvar (struct mcomp
*);
317 static int ptoi (char *, int *);
318 static int ptos (char *, char **);
319 static char *parse (void);
320 static void process (char *, char *, int, int);
321 static void mhlfile (FILE *, char *, int, int);
322 static int mcomp_flags (char *) PURE
;
323 static char *mcomp_add (unsigned long, char *, char *);
324 static void mcomp_format (struct mcomp
*, struct mcomp
*);
325 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
326 static void free_queue (struct mcomp
**, struct mcomp
**);
327 static void putcomp (struct mcomp
*, struct mcomp
*, int);
328 static char *oneline (char *, unsigned long);
329 static void putstr (char *, unsigned long);
330 static void putch (char, unsigned long);
331 static bool linefeed_typed(void);
332 static void intrser (int);
333 static void pipeser (int);
334 static void quitser (int);
335 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
336 static void mhldone (int) NORETURN
;
337 static void filterbody (struct mcomp
*, char *, int, int,
339 static void compile_formatfield(struct mcomp
*);
340 static void compile_filterargs (void);
344 mhl (int argc
, char **argv
)
348 unsigned int i
, vecp
= 0;
350 char *cp
, *folder
= NULL
, *form
= NULL
;
351 char buf
[BUFSIZ
], *files
[MAXARGS
];
352 char **argp
, **arguments
;
354 /* Need this if called from main() of show(1). */
355 invo_name
= r1bindex (argv
[0], '/');
357 arguments
= getarguments (invo_name
, argc
, argv
, 1);
360 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
363 while ((cp
= *argp
++)) {
365 switch (smatch (++cp
, mhlswitches
)) {
367 ambigsw (cp
, mhlswitches
);
370 mhladios (NULL
, "-%s unknown\n", cp
);
373 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
374 print_help (buf
, mhlswitches
, 1);
377 print_version(invo_name
);
395 if (!(folder
= *argp
++) || *folder
== '-')
396 mhladios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(form
= *argp
++) || *form
== '-')
400 mhladios (NULL
, "missing argument to %s", argp
[-2]);
404 if (!(cp
= *argp
++) || *cp
== '-')
405 mhladios (NULL
, "missing argument to %s", argp
[-2]);
406 sleepsw
= atoi (cp
);/* ZERO ok! */
410 if (!(moreproc
= *argp
++) || *moreproc
== '-')
411 mhladios (NULL
, "missing argument to %s", argp
[-2]);
418 if (!(formatproc
= *argp
++) || *formatproc
== '-')
419 mhladios (NULL
, "missing argument to %s", argp
[-2]);
426 if (!(cp
= *argp
++) || *cp
== '-')
427 mhladios (NULL
, "missing argument to %s", argp
[-2]);
428 if ((length
= atoi (cp
)) < 1)
429 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
432 if (!(cp
= *argp
++) || *cp
== '-')
433 mhladios (NULL
, "missing argument to %s", argp
[-2]);
434 if ((width
= atoi (cp
)) < 1)
435 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
439 if (!(digest
= *argp
++) || *digest
== '-')
440 mhladios (NULL
, "missing argument to %s", argp
[-2]);
443 if (!(cp
= *argp
++) || *cp
== '-')
444 mhladios (NULL
, "missing argument to %s", argp
[-2]);
445 if ((issue
= atoi (cp
)) < 1)
446 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
449 if (!(cp
= *argp
++) || *cp
== '-')
450 mhladios (NULL
, "missing argument to %s", argp
[-2]);
451 if ((volume
= atoi (cp
)) < 1)
452 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
460 clearflg
= -1;/* XXX */
464 dashstuff
= 1; /* ternary logic */
467 dashstuff
= -1; /* ternary logic */
479 folder
= getenv ("mhfolder");
481 if (isatty (fileno (stdout
))) {
482 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
483 SIGNAL2 (SIGPIPE
, pipeser
);
484 m_popen(moreproc
, false);
487 SIGNAL (SIGINT
, SIG_IGN
);
488 SIGNAL2 (SIGQUIT
, quitser
);
495 mhl_format (form
? form
: mhlformat
, length
, width
);
498 process (folder
, NULL
, 1, vecp
= 1);
500 for (i
= 0; i
< vecp
; i
++)
501 process (folder
, files
[i
], i
+ 1, vecp
);
506 fputs(delim4
, stdout
);
508 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
510 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
511 digest
, volume
, issue
);
514 for (cp
= buf
+ i
; i
> 1; i
--)
521 printf ("\n------- End of Forwarded Message%s\n",
527 mhladios("output", "error writing");
530 if (clearflg
> 0 && ontty
== NOTTY
)
541 mhl_format (char *file
, int length
, int width
)
545 char *ap
, name
[NAMESZ
];
549 static dev_t dev
= 0;
550 static ino_t ino
= 0;
551 static time_t mtime
= 0;
554 if (stat (etcpath (file
), &st
) != NOTOK
555 && mtime
== st
.st_mtime
559 free_queue (&fmthd
, &fmttl
);
562 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
563 mhladios (file
, "unable to open format file");
565 if (fstat (fileno (fp
), &st
) != NOTOK
) {
571 global
.c_ovtxt
= global
.c_nfs
= NULL
;
575 if ((i
= sc_width ()) > 5)
577 global
.c_cwidth
= -1;
578 if ((i
= sc_length ()) > 5)
579 global
.c_length
= i
- 1;
580 global
.c_flags
= BELL
; /* BELL is default */
581 *(ip
= ignores
) = NULL
;
584 while (vfgets (fp
, &ap
) == OK
) {
589 trim_suffix_c(bp
, '\n');
592 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
597 strncpy (name
, parse(), sizeof(name
));
603 * Split this list of fields to ignore, and copy
604 * it to the end of the current "ignores" list.
606 if (!strcasecmp (name
, "ignores")) {
607 char **tmparray
, **p
;
610 /* split the fields */
611 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
613 /* count number of fields split */
618 /* copy pointers to split fields to ignores array */
619 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
625 if (evalvar (&global
))
626 mhladios (NULL
, "format file syntax error: %s", bp
);
633 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
634 while (*parptr
== ':' || *parptr
== ',') {
637 mhladios (NULL
, "format file syntax error: %s", bp
);
639 if (!c1
->c_nfs
&& global
.c_nfs
) {
640 if (c1
->c_flags
& DATEFMT
) {
641 if (global
.c_flags
& DATEFMT
) {
642 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
643 compile_formatfield(c1
);
645 } else if (c1
->c_flags
& ADDRFMT
) {
646 if (global
.c_flags
& ADDRFMT
) {
647 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
648 compile_formatfield(c1
);
655 mhladios (NULL
, "format file syntax error: %s", bp
);
661 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
664 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
665 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
666 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
667 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
668 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
669 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
670 c1
->c_cwidth
, c1
->c_length
);
671 fprintf (stderr
, "\tflags=%s\n",
672 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
678 global
.c_flags
|= CLEARSCR
;
681 global
.c_flags
&= ~CLEARSCR
;
684 switch (bellflg
) { /* command line may override format file */
686 global
.c_flags
|= BELL
;
689 global
.c_flags
&= ~BELL
;
694 global
.c_length
= length
;
696 global
.c_width
= width
;
697 if (global
.c_length
< 5)
698 global
.c_length
= 10000;
699 if (global
.c_width
< 5)
700 global
.c_width
= 10000;
705 evalvar (struct mcomp
*c1
)
707 char *cp
, name
[NAMESZ
];
712 strncpy (name
, parse(), sizeof(name
));
714 if (!strcasecmp (name
, "component")) {
715 if (ptos (name
, &c1
->c_text
))
717 c1
->c_flags
&= ~NOCOMPONENT
;
721 if (!strcasecmp (name
, "overflowtext"))
722 return ptos (name
, &c1
->c_ovtxt
);
724 if (!strcasecmp (name
, "formatfield")) {
725 if (ptos (name
, &cp
))
727 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
728 compile_formatfield(c1
);
729 c1
->c_flags
|= FORMAT
;
733 if (!strcasecmp (name
, "decode")) {
734 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
735 compile_formatfield(c1
);
736 c1
->c_flags
|= FORMAT
;
740 if (!strcasecmp (name
, "offset"))
741 return ptoi (name
, &c1
->c_offset
);
742 if (!strcasecmp (name
, "overflowoffset"))
743 return ptoi (name
, &c1
->c_ovoff
);
744 if (!strcasecmp (name
, "width"))
745 return ptoi (name
, &c1
->c_width
);
746 if (!strcasecmp (name
, "compwidth"))
747 return ptoi (name
, &c1
->c_cwidth
);
748 if (!strcasecmp (name
, "length"))
749 return ptoi (name
, &c1
->c_length
);
750 if (!strcasecmp (name
, "nodashstuffing"))
751 return dashstuff
= -1;
753 for (ap
= triples
; ap
->t_name
; ap
++)
754 if (!strcasecmp (ap
->t_name
, name
)) {
755 c1
->c_flags
|= ap
->t_on
;
756 c1
->c_flags
&= ~ap
->t_off
;
760 if (!strcasecmp (name
, "formatarg")) {
761 struct arglist
*args
;
763 if (ptos (name
, &cp
))
766 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
767 inform("format filters are currently only supported on "
768 "the \"body\" component");
775 arglist_tail
->a_next
= args
;
782 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
793 ptoi (char *name
, int *i
)
797 if (*parptr
++ != '=' || !*(cp
= parse ())) {
798 inform("missing argument to variable %s", name
);
808 ptos (char *name
, char **s
)
812 if (*parptr
++ != '=') {
813 inform("missing argument to variable %s", name
);
817 if (*parptr
!= '"') {
819 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
823 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
824 if (*parptr
== QUOTE
)
831 if ((*parptr
= c
) == '"')
842 static char result
[NAMESZ
];
844 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
862 * Process one file/message
866 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
868 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
874 /* volatile to prevent "might be clobbered" warning from gcc: */
875 char *volatile fname2
= fname
? fname
: "(stdin)";
880 switch (setjmp (env
)) {
883 fp
= fopen(fname
, "r");
885 advise (fname
, "unable to open");
892 if (fstat(fileno(fp
), &st
) == 0) {
893 filesize
= st
.st_size
;
897 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
899 SIGNAL (SIGINT
, intrser
);
900 mhlfile (fp
, cp
, ofilen
, ofilec
);
903 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
904 fmt_free(ap
->a_fmt
, 0);
914 SIGNAL (SIGINT
, SIG_IGN
);
915 if (fp
!= stdin
&& fp
!= NULL
)
918 holder
.c_text
= NULL
;
919 free_queue (&msghd
, &msgtl
);
920 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
921 c1
->c_flags
&= ~HDROUTPUT
;
929 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
932 struct mcomp
*c1
, *c2
, *c3
;
933 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
934 m_getfld_state_t gstate
;
936 compile_filterargs();
940 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
942 fputs("\n-------", stdout
);
944 printf (" Forwarded Message%s", PLURALS(ofilec
));
946 printf (" Message %d", ofilen
);
954 if ((global
.c_flags
& CLEARSCR
))
959 printf (">>> %s\n\n", mname
);
965 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
968 printf ("Press <return> to list \"%s\"...", mname
);
972 if (ofilec
== 1 || linefeed_typed()) {
973 if ((global
.c_flags
& CLEARSCR
))
987 printf (">>> %s\n\n", mname
);
993 gstate
= m_getfld_state_init(fp
);
995 int bufsz
= sizeof buf
;
996 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
999 bucket
= fmt_addcomptext(name
, buf
);
1000 for (ip
= ignores
; *ip
; ip
++)
1001 if (!strcasecmp (name
, *ip
)) {
1002 while (state
== FLDPLUS
) {
1004 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1005 fmt_appendcomp(bucket
, name
, buf
);
1012 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1013 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1016 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1017 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1018 if (!strcasecmp (FENDNULL(c1
->c_name
),
1019 FENDNULL(c3
->c_name
))) {
1021 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1025 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1026 while (state
== FLDPLUS
) {
1028 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1029 c1
->c_text
= add (buf
, c1
->c_text
);
1030 fmt_appendcomp(bucket
, name
, buf
);
1033 c1
->c_flags
|= EXTRA
;
1039 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1040 if (c1
->c_flags
& CLEARTEXT
) {
1041 putcomp (c1
, c1
, ONECOMP
);
1045 !strcasecmp (c1
->c_name
, "messagename")) {
1046 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1048 putcomp (c1
, &holder
, ONECOMP
);
1049 free (holder
.c_text
);
1050 holder
.c_text
= NULL
;
1053 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1054 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1055 if (c2
->c_flags
& EXTRA
)
1056 putcomp (c1
, c2
, TWOCOMP
);
1059 if (dobody
&& (!c1
->c_name
||
1060 !strcasecmp (c1
->c_name
, "body"))) {
1061 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1062 formatproc
!= NULL
) {
1063 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1065 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1066 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1067 while (state
== BODY
) {
1068 putcomp (c1
, &holder
, BODYCOMP
);
1070 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1073 free (holder
.c_text
);
1074 holder
.c_text
= NULL
;
1078 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1079 if (!strcasecmp (FENDNULL(c2
->c_name
),
1080 FENDNULL(c1
->c_name
))) {
1081 putcomp (c1
, c2
, ONECOMP
);
1082 if (!(c1
->c_flags
& SPLIT
))
1086 m_getfld_state_destroy (&gstate
);
1091 inform("format error in message %s", mname
);
1093 m_getfld_state_destroy (&gstate
);
1097 mhladios (NULL
, "getfld() returned %d", state
);
1104 mcomp_flags (char *name
)
1108 for (ap
= pairs
; ap
->p_name
; ap
++)
1109 if (!strcasecmp (ap
->p_name
, name
))
1117 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1121 if (!(flags
& ADDRFMT
))
1122 return add (s1
, s2
);
1124 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1127 return add (s1
, add (",\n", s2
));
1134 struct pqpair
*pq_next
;
1139 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1144 struct pqpair
*p
, *q
;
1146 struct mailname
*mp
;
1153 dat
[3] = BUFSIZ
- 1;
1156 if (!(c1
->c_flags
& ADDRFMT
)) {
1157 charstring_t scanl
= charstring_create (BUFSIZ
);
1160 c1
->c_c_text
->c_text
= ap
;
1161 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1165 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1166 /* Don't need to append a newline, dctime() already did */
1167 c2
->c_text
= charstring_buffer_copy (scanl
);
1168 charstring_free (scanl
);
1170 /* ap is now owned by the component struct, so do NOT free it here */
1174 (q
= &pq
)->pq_next
= NULL
;
1175 while ((cp
= getname (ap
))) {
1177 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1178 p
->pq_text
= mh_xstrdup(cp
);
1179 p
->pq_error
= mh_xstrdup(error
);
1181 p
->pq_text
= getcpy (mp
->m_text
);
1184 q
= (q
->pq_next
= p
);
1187 for (p
= pq
.pq_next
; p
; p
= q
) {
1188 charstring_t scanl
= charstring_create (BUFSIZ
);
1192 c1
->c_c_text
->c_text
= p
->pq_text
;
1195 if (c1
->c_c_error
) {
1196 c1
->c_c_error
->c_text
= p
->pq_error
;
1200 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1201 buffer
= charstring_buffer_copy (scanl
);
1204 c2
->c_text
= add (",\n", c2
->c_text
);
1205 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1207 c2
->c_text
= add (buffer
, c2
->c_text
);
1209 charstring_free (scanl
);
1217 c2
->c_text
= add ("\n", c2
->c_text
);
1222 static struct mcomp
*
1223 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1228 c1
->c_flags
= flags
& ~INIT
;
1229 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1230 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1231 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1234 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1235 c1
->c_offset
= global
.c_offset
;
1236 c1
->c_ovoff
= global
. c_ovoff
;
1237 c1
->c_width
= c1
->c_length
= 0;
1238 c1
->c_cwidth
= global
.c_cwidth
;
1239 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1244 (*tail
)->c_next
= c1
;
1252 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1254 struct mcomp
*c1
, *c2
;
1256 for (c1
= *head
; c1
; c1
= c2
) {
1263 fmt_free (c1
->c_fmt
, 0);
1267 *head
= *tail
= NULL
;
1272 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1274 char *text
; /* c1's text, or the name as a fallback. */
1275 char *trimmed_prefix
;
1279 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1281 if (! utf8
&& flag
!= BODYCOMP
) {
1282 /* Don't print 8-bit bytes in header field values if not in a
1283 UTF-8 locale, as required by RFC 6532. */
1284 c1
->c_flags
|= FORCE7BIT
;
1287 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1288 /* Create a copy with trailing whitespace trimmed, for use with
1290 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1294 llim
= c1
->c_length
? c1
->c_length
: -1;
1295 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1296 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1298 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1300 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1301 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1302 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1305 if (c1
->c_flags
& CLEARTEXT
) {
1306 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1308 putstr ("\n", c1
->c_flags
);
1312 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1313 mcomp_format (c1
, c2
);
1315 if (c1
->c_flags
& CENTER
) {
1316 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1317 - c1
->c_offset
- strlen (c2
->c_text
);
1318 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1319 count
-= strlen(text
) + 2;
1320 lm
= c1
->c_offset
+ (count
/ 2);
1326 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1327 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1329 putstr(text
, c1
->c_flags
);
1330 if (flag
!= BODYCOMP
) {
1331 putstr (": ", c1
->c_flags
);
1332 if (!(c1
->c_flags
& SPLIT
))
1333 c1
->c_flags
|= HDROUTPUT
;
1336 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1338 putstr (" ", c1
->c_flags
);
1341 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1345 && !(c2
->c_flags
& HDROUTPUT
)
1346 && !(c2
->c_flags
& NOCOMPONENT
)) {
1347 if (c1
->c_flags
& UPPERCASE
)
1348 to_upper(c2
->c_name
);
1349 putstr (c2
->c_name
, c1
->c_flags
);
1350 putstr (": ", c1
->c_flags
);
1351 if (!(c1
->c_flags
& SPLIT
))
1352 c2
->c_flags
|= HDROUTPUT
;
1355 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1357 putstr (" ", c1
->c_flags
);
1359 if (c1
->c_flags
& UPPERCASE
)
1360 to_upper(c2
->c_text
);
1364 if (flag
== TWOCOMP
)
1365 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1366 : (int) strlen (c2
->c_name
) + 2;
1368 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1371 count
+= c1
->c_offset
;
1373 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1374 /* Output line, trimming trailing whitespace if requested. */
1375 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1377 putstr ("\n", c1
->c_flags
);
1378 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1380 if (flag
== BODYCOMP
1381 && !(c1
->c_flags
& NOCOMPONENT
)) {
1382 /* Output component, trimming trailing whitespace if there
1383 is no text on the line. */
1385 putstr(text
, c1
->c_flags
);
1387 putstr (trimmed_prefix
, c1
->c_flags
);
1391 /* Output line, trimming trailing whitespace if requested. */
1392 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1395 putstr ("\n", c1
->c_flags
);
1397 if (flag
== BODYCOMP
&& term
== '\n')
1398 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1400 free (trimmed_prefix
);
1405 oneline (char *stuff
, unsigned long flags
)
1413 return onelp
= NULL
;
1417 if (flags
& COMPRESS
) {
1418 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1419 if (isspace ((unsigned char) *onelp
)) {
1420 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1438 while (*onelp
&& *onelp
!= '\n')
1440 if (*onelp
== '\n') {
1444 if (flags
& LEFTADJUST
)
1445 while (*ret
== ' ' || *ret
== '\t')
1448 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1456 putstr (char *string
, unsigned long flags
)
1458 /* To not count, for the purpose of counting columns, all of
1459 the bytes of a multibyte character. */
1462 if (!column
&& lm
> 0) {
1465 putch ('\t', flags
);
1475 #ifdef MULTIBYTE_SUPPORT
1476 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1479 NMH_UNUSED (char_len
);
1483 flags
&= ~INVISIBLE
;
1484 #ifdef MULTIBYTE_SUPPORT
1485 /* mbtowc should never return 0, because *string is non-NULL. */
1486 if (char_len
<= 0) {
1487 /* Find number of bytes in next character. */
1489 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1493 /* Multibyte character, after the first byte. */
1499 putch (*string
++, flags
);
1505 putch (char ch
, unsigned long flags
)
1516 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1518 if (global
.c_flags
& BELL
)
1521 if (linefeed_typed()) {
1522 if (global
.c_flags
& CLEARSCR
)
1523 nmh_clear_screen ();
1527 row
= global
.c_length
/ 3;
1546 * If we are forwarding this message, and the first
1547 * column contains a dash, then add a dash and a space.
1549 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1554 * Increment the character count, unless
1555 * 1) In UTF-8 locale, this is other than the last byte of
1556 a multibyte character, or
1557 * 2) In C locale, will print a non-printable character.
1559 if ((flags
& FORCE7BIT
) == 0) {
1561 if ((flags
& INVISIBLE
) == 0) {
1562 /* If multibyte character, its first byte only. */
1566 /* If not an ASCII character, the replace character will be
1567 displayed. Count it. */
1568 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1575 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1576 putch ('\n', flags
);
1579 putstr (FENDNULL(ovtxt
), flags
);
1584 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1591 /* linefeed_typed() makes a single read(2) from stdin and returns true
1592 * if a linefeed character is amongst the characters read.
1593 * A read error is treated as if linefeed wasn't typed.
1595 * Typing on a TTY can cause read() to return data without typing Enter
1596 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1597 * The linefeed can also be escaped with the TTY's LNEXT character,
1598 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1599 * It's not possible to distinguish between the user typing a buffer's
1600 * worth of characters and then EOT, or more than the buffer can hold.
1601 * Either way, the result depends on ASCII LF, either from typing Enter
1602 * or an escaped Ctrl-J, being amongst the read characters.
1605 linefeed_typed(void)
1610 n
= read(0, buf
, sizeof buf
);
1612 advise("stdin", "read");
1613 return false; /* Treat as EOF. */
1616 return memchr(buf
, '\n', n
);
1627 longjmp (env
, DONE
);
1652 mhladios (char *what
, char *fmt
, ...)
1657 advertise (what
, NULL
, fmt
, ap
);
1664 mhldone (int status
)
1672 * Compile a format string used by the formatfield option and save it
1675 * We will want the {text} (and possibly {error}) components for later,
1676 * so look for them and save them if we find them.
1680 compile_formatfield(struct mcomp
*c1
)
1682 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1685 * As a note to myself and any other poor bastard who is looking through
1686 * this code in the future ....
1688 * When the format hash table is reset later on (as it almost certainly
1689 * will be), there will still be references to these components in the
1690 * compiled format instructions. Thus these component references will
1691 * be free'd when the format instructions are free'd (by fmt_free()).
1693 * So, in other words ... don't go free'ing them yourself!
1696 c1
->c_c_text
= fmt_findcomp("text");
1697 c1
->c_c_error
= fmt_findcomp("error");
1701 * Compile all of the arguments for our format list.
1703 * Iterate through the linked list of format strings and compile them.
1704 * Note that we reset the format hash table before we start, but we do NOT
1705 * reset it between calls to fmt_compile().
1710 compile_filterargs (void)
1712 struct arglist
*arg
= arglist_head
;
1719 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1724 * Search through and mark any components that are address components
1727 for (ap
= addrcomps
; *ap
; ap
++) {
1728 cptr
= fmt_findcomp (*ap
);
1730 cptr
->c_type
|= CT_ADDR
;
1735 * Filter the body of a message through a specified format program
1739 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1740 m_getfld_state_t gstate
)
1742 struct mcomp holder
;
1744 int fdinput
[2], fdoutput
[2], waitstat
;
1746 pid_t writerpid
, filterpid
;
1749 * Create pipes so we can communicate with our filter process.
1752 if (pipe(fdinput
) < 0) {
1753 die("Unable to create input pipe");
1756 if (pipe(fdoutput
) < 0) {
1757 die("Unable to create output pipe");
1761 * Here's what we're doing to do.
1763 * - Fork ourselves and start writing data to the write side of the
1764 * input pipe (fdinput[1]).
1766 * - Fork and exec our filter program. We set the standard input of
1767 * our filter program to be the read side of our input pipe (fdinput[0]).
1768 * Standard output is set to the write side of our output pipe
1771 * - We read from the read side of the output pipe (fdoutput[0]).
1773 * We're forking because that's the simplest way to prevent any deadlocks.
1774 * (without doing something like switching to non-blocking I/O and using
1775 * select or poll, and I'm not interested in doing that).
1778 switch (writerpid
= fork()) {
1781 * Our child process - just write to the filter input (fdinput[1]).
1782 * Close all other descriptors that we don't need.
1790 * Call m_getfld2() until we're no longer in the BODY state
1793 while (state
== BODY
) {
1795 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1796 advise ("pipe output", "write");
1798 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1802 * We should be done; time to exit.
1807 * Make sure we call _exit(), otherwise we may flush out the stdio
1808 * buffers that we have duplicated from the parent.
1812 die("Unable to fork for filter writer process");
1817 * Fork and exec() our filter program, after redirecting standard in
1818 * and standard out appropriately.
1821 switch (filterpid
= fork()) {
1822 char **args
, *program
;
1824 int i
, dat
[5], s
, argp
;
1828 * Configure an argument array for us
1831 args
= argsplit(formatproc
, &program
, &argp
);
1832 args
[argp
+ filter_nargs
] = NULL
;
1840 * Pull out each argument and scan them.
1843 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1844 charstring_t scanl
= charstring_create (BUFSIZ
);
1846 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1847 args
[i
] = charstring_buffer_copy (scanl
);
1848 charstring_free (scanl
);
1850 * fmt_scan likes to put a trailing newline at the end of the
1851 * format string. If we have one, get rid of it.
1853 s
= strlen(args
[i
]);
1854 if (args
[i
][s
- 1] == '\n')
1855 args
[i
][s
- 1] = '\0';
1858 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1862 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1863 adios("formatproc", "Unable to dup2() standard input");
1865 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1866 adios("formatproc", "Unable to dup2() standard output");
1870 * Close everything (especially the old input and output
1871 * descriptors, since they've been dup'd to stdin and stdout),
1872 * and exec the formatproc.
1880 execvp(formatproc
, args
);
1882 adios(formatproc
, "Unable to execute filter");
1887 die("Unable to fork format program");
1891 * Close everything except our reader (fdoutput[0]);
1899 * As we read in this data, send it to putcomp
1902 holder
.c_text
= buf
;
1904 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1906 putcomp(c1
, &holder
, BODYCOMP
);
1910 die("reading from formatproc");
1914 * See if we got any errors along the way. I'm a little leery of calling
1915 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1918 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1919 if (errno
!= ECHILD
) {
1920 adios("filterproc", "Unable to determine status");
1923 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1924 pidstatus(waitstat
, stderr
, "filterproc");
1928 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1929 if (errno
!= ECHILD
) {
1930 adios("writer process", "Unable to determine status");
1934 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1935 pidstatus(waitstat
, stderr
, "writer process");