]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
4a54f7104b7a80620407018e404d5198b3bd7908
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/arglist.h"
10 #include "sbr/error.h"
11 #include "h/signals.h"
12 #include "h/addrsbr.h"
13 #include "h/fmt_scan.h"
17 #include "sbr/m_popen.h"
19 #include <sys/types.h>
20 #include "sbr/terminal.h"
24 * for a component containing addresses, ADDRFMT, if COMPRESS is also
25 * set, then addresses get split wrong (not at the spaces between commas).
26 * To fix this correctly, putstr() should know about "atomic" strings that
27 * must NOT be broken across lines. That's too difficult for right now
28 * (it turns out that there are a number of degenerate cases), so in
29 * oneline(), instead of
31 * (*onelp == '\n' && !onelp[1])
33 * being a terminating condition,
35 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
37 * is used instead. This cuts the line prematurely, and gives us a much
38 * better chance of getting things right.
47 #define MHL_SWITCHES \
48 X("bell", 0, BELLSW) \
49 X("nobell", 0, NBELLSW) \
50 X("clear", 0, CLRSW) \
51 X("noclear", 0, NCLRSW) \
52 X("folder +folder", 0, FOLDSW) \
53 X("form formfile", 0, FORMSW) \
54 X("moreproc program", 0, PROGSW) \
55 X("nomoreproc", 0, NPROGSW) \
56 X("length lines", 0, LENSW) \
57 X("width columns", 0, WIDTHSW) \
58 X("sleep seconds", 0, SLEEPSW) \
59 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
60 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("forward", -7, FORW1SW) /* interface from forw */ \
64 X("forwall", -7, FORW2SW) /* interface from forw */ \
65 X("digest list", -6, DGSTSW) \
66 X("volume number", -6, VOLUMSW) \
67 X("issue number", -5, ISSUESW) \
68 X("nobody", -6, NBODYSW) \
69 X("fmtproc program", 0, FMTPROCSW) \
70 X("nofmtproc", 0, NFMTPROCSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(MHL
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
80 #define NOCOMPONENT 0x000001 /* don't show component name */
81 #define UPPERCASE 0x000002 /* display in all upper case */
82 #define CENTER 0x000004 /* center line */
83 #define CLEARTEXT 0x000008 /* cleartext */
84 #define EXTRA 0x000010 /* an "extra" component */
85 #define HDROUTPUT 0x000020 /* already output */
86 #define CLEARSCR 0x000040 /* clear screen */
87 #define LEFTADJUST 0x000080 /* left justify multiple lines */
88 #define COMPRESS 0x000100 /* compress text */
89 #define ADDRFMT 0x000200 /* contains addresses */
90 #define BELL 0x000400 /* sound bell at EOP */
91 #define DATEFMT 0x000800 /* contains dates */
92 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
93 #define INIT 0x002000 /* initialize component */
94 #define RTRIM 0x004000 /* trim trailing whitespace */
95 #define SPLIT 0x010000 /* split headers (don't concatenate) */
96 #define NONEWLINE 0x020000 /* don't write trailing newline */
97 #define NOWRAP 0x040000 /* Don't wrap lines ever */
98 #define FMTFILTER 0x080000 /* Filter through format filter */
99 #define INVISIBLE 0x100000 /* count byte in display columns? */
100 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
101 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
102 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
105 * A format string to be used as a command-line argument to the body
110 struct format
*a_fmt
;
112 struct arglist
*a_next
;
116 * Linked list of command line arguments for the body format filter. This
117 * USED to be in "struct mcomp", but the format API got cleaned up and even
118 * though it reduced the code we had to do, it make things more complicated
119 * for us. Specifically:
121 * - The interface to the hash table has been cleaned up, which means the
122 * rooting around in the hash table is no longer necessary (yay!). But
123 * this ALSO means that we have to make sure that we call our format
124 * compilation routines before we process the message, because the
125 * components need to be visible in the hash table so we can save them for
126 * later. So we moved them out of "mcomp" and now compile them right before
127 * header processing starts.
128 * - We also use format strings to handle other components in the mhl
129 * configuration (using "formatfield" and "decode"), but here life
130 * gets complicated: they aren't dealt with in the normal way. Instead
131 * of referring to a component like {from}, each component is processed
132 * using the special {text} component. But these format strings need to be
133 * compiled BEFORE we compile the format arguments; in the previous
134 * implementation they were compiled and scanned as the headers were
135 * read, and that would reset the hash table that we need to populate
136 * the components used by the body format filter. So we are compiling
137 * the formatfield component strings ahead of time and then scanning them
140 * Okay, fine ... this was broken before. But you know what? Fixing this
141 * the right way will make things easier down the road.
143 * One side-effect to this change: format strings are now compiled only once
144 * for components specified with "formatfield", but they are compiled for
145 * every message for format arguments.
148 static struct arglist
*arglist_head
;
149 static struct arglist
*arglist_tail
;
150 static int filter_nargs
= 0;
153 * Flags/options for each component
157 char *c_name
; /* component name */
158 char *c_text
; /* component text */
159 char *c_ovtxt
; /* text overflow indicator */
160 char *c_nfs
; /* iff FORMAT */
161 struct format
*c_fmt
; /* .. */
162 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
163 struct comp
*c_c_error
; /* Ref to {error} */
164 int c_offset
; /* left margin indentation */
165 int c_ovoff
; /* overflow indentation */
166 int c_width
; /* width of field */
167 int c_cwidth
; /* width of component */
168 int c_length
; /* length in lines */
169 unsigned long c_flags
;
170 struct mcomp
*c_next
;
173 static struct mcomp
*msghd
= NULL
;
174 static struct mcomp
*msgtl
= NULL
;
175 static struct mcomp
*fmthd
= NULL
;
176 static struct mcomp
*fmttl
= NULL
;
178 static struct mcomp global
= {
179 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
182 static struct mcomp holder
= {
183 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
188 unsigned long p_flags
;
191 static struct pair pairs
[] = {
194 { "Sender", ADDRFMT
},
195 { "Reply-To", ADDRFMT
},
199 { "Resent-Date", DATEFMT
},
200 { "Resent-From", ADDRFMT
},
201 { "Resent-Sender", ADDRFMT
},
202 { "Resent-Reply-To", ADDRFMT
},
203 { "Resent-To", ADDRFMT
},
204 { "Resent-cc", ADDRFMT
},
205 { "Resent-Bcc", ADDRFMT
},
215 static struct triple triples
[] = {
216 { "nocomponent", NOCOMPONENT
, 0 },
217 { "uppercase", UPPERCASE
, 0 },
218 { "nouppercase", 0, UPPERCASE
},
219 { "center", CENTER
, 0 },
220 { "nocenter", 0, CENTER
},
221 { "clearscreen", CLEARSCR
, 0 },
222 { "noclearscreen", 0, CLEARSCR
},
223 { "noclear", 0, CLEARSCR
},
224 { "leftadjust", LEFTADJUST
, 0 },
225 { "noleftadjust", 0, LEFTADJUST
},
226 { "compress", COMPRESS
, 0 },
227 { "nocompress", 0, COMPRESS
},
228 { "split", SPLIT
, 0 },
229 { "nosplit", 0, SPLIT
},
230 { "rtrim", RTRIM
, 0 },
231 { "nortrim", 0, RTRIM
},
232 { "addrfield", ADDRFMT
, DATEFMT
},
234 { "nobell", 0, BELL
},
235 { "datefield", DATEFMT
, ADDRFMT
},
236 { "newline", 0, NONEWLINE
},
237 { "nonewline", NONEWLINE
, 0 },
238 { "wrap", 0, NOWRAP
},
239 { "nowrap", NOWRAP
, 0 },
240 { "format", FMTFILTER
, 0 },
241 { "noformat", 0, FMTFILTER
},
245 static char *addrcomps
[] = {
262 static int bellflg
= 0;
263 static int clearflg
= 0;
264 static int dashstuff
= 0;
265 static bool dobody
= true;
269 static int sleepsw
= NOTOK
;
271 static char *digest
= NULL
;
272 static int volume
= 0;
273 static int issue
= 0;
275 static int exitstat
= 0;
276 static bool mhldebug
;
278 static int filesize
= 0;
283 static int ontty
= NOTTY
;
286 static unsigned int column
;
292 static unsigned int wid
;
300 static int num_ignores
= 0;
301 static char *ignores
[MAXARGS
];
305 static char delim3
[] = /* from forw.c */
306 "\n----------------------------------------------------------------------\n\n";
307 static char delim4
[] = "\n------------------------------\n\n";
312 static void mhl_format (char *, int, int);
313 static int evalvar (struct mcomp
*);
314 static int ptoi (char *, int *);
315 static int ptos (char *, char **);
316 static char *parse (void);
317 static void process (char *, char *, int, int);
318 static void mhlfile (FILE *, char *, int, int);
319 static int mcomp_flags (char *) PURE
;
320 static char *mcomp_add (unsigned long, char *, char *);
321 static void mcomp_format (struct mcomp
*, struct mcomp
*);
322 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
323 static void free_queue (struct mcomp
**, struct mcomp
**);
324 static void putcomp (struct mcomp
*, struct mcomp
*, int);
325 static char *oneline (char *, unsigned long);
326 static void putstr (char *, unsigned long);
327 static void putch (char, unsigned long);
328 static bool linefeed_typed(void);
329 static void intrser (int);
330 static void pipeser (int);
331 static void quitser (int);
332 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
333 static void mhldone (int) NORETURN
;
334 static void filterbody (struct mcomp
*, char *, int, int,
336 static void compile_formatfield(struct mcomp
*);
337 static void compile_filterargs (void);
341 mhl (int argc
, char **argv
)
345 unsigned int i
, vecp
= 0;
347 char *cp
, *folder
= NULL
, *form
= NULL
;
348 char buf
[BUFSIZ
], *files
[MAXARGS
];
349 char **argp
, **arguments
;
351 /* Need this if called from main() of show(1). */
352 invo_name
= r1bindex (argv
[0], '/');
354 arguments
= getarguments (invo_name
, argc
, argv
, 1);
357 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
360 while ((cp
= *argp
++)) {
362 switch (smatch (++cp
, mhlswitches
)) {
364 ambigsw (cp
, mhlswitches
);
367 mhladios (NULL
, "-%s unknown\n", cp
);
370 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
371 print_help (buf
, mhlswitches
, 1);
374 print_version(invo_name
);
392 if (!(folder
= *argp
++) || *folder
== '-')
393 mhladios (NULL
, "missing argument to %s", argp
[-2]);
396 if (!(form
= *argp
++) || *form
== '-')
397 mhladios (NULL
, "missing argument to %s", argp
[-2]);
401 if (!(cp
= *argp
++) || *cp
== '-')
402 mhladios (NULL
, "missing argument to %s", argp
[-2]);
403 sleepsw
= atoi (cp
);/* ZERO ok! */
407 if (!(moreproc
= *argp
++) || *moreproc
== '-')
408 mhladios (NULL
, "missing argument to %s", argp
[-2]);
415 if (!(formatproc
= *argp
++) || *formatproc
== '-')
416 mhladios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(cp
= *argp
++) || *cp
== '-')
424 mhladios (NULL
, "missing argument to %s", argp
[-2]);
425 if ((length
= atoi (cp
)) < 1)
426 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
429 if (!(cp
= *argp
++) || *cp
== '-')
430 mhladios (NULL
, "missing argument to %s", argp
[-2]);
431 if ((width
= atoi (cp
)) < 1)
432 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
436 if (!(digest
= *argp
++) || *digest
== '-')
437 mhladios (NULL
, "missing argument to %s", argp
[-2]);
440 if (!(cp
= *argp
++) || *cp
== '-')
441 mhladios (NULL
, "missing argument to %s", argp
[-2]);
442 if ((issue
= atoi (cp
)) < 1)
443 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 mhladios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((volume
= atoi (cp
)) < 1)
449 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
457 clearflg
= -1;/* XXX */
461 dashstuff
= 1; /* ternary logic */
464 dashstuff
= -1; /* ternary logic */
476 folder
= getenv ("mhfolder");
478 if (isatty (fileno (stdout
))) {
479 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
480 SIGNAL2 (SIGPIPE
, pipeser
);
481 m_popen(moreproc
, false);
484 SIGNAL (SIGINT
, SIG_IGN
);
485 SIGNAL2 (SIGQUIT
, quitser
);
492 mhl_format (form
? form
: mhlformat
, length
, width
);
495 process (folder
, NULL
, 1, vecp
= 1);
497 for (i
= 0; i
< vecp
; i
++)
498 process (folder
, files
[i
], i
+ 1, vecp
);
503 fputs(delim4
, stdout
);
505 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
507 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
508 digest
, volume
, issue
);
511 for (cp
= buf
+ i
; i
> 1; i
--)
518 printf ("\n------- End of Forwarded Message%s\n",
524 mhladios("output", "error writing");
527 if (clearflg
> 0 && ontty
== NOTTY
)
538 mhl_format (char *file
, int length
, int width
)
542 char *ap
, name
[NAMESZ
];
546 static dev_t dev
= 0;
547 static ino_t ino
= 0;
548 static time_t mtime
= 0;
551 if (stat (etcpath (file
), &st
) != NOTOK
552 && mtime
== st
.st_mtime
556 free_queue (&fmthd
, &fmttl
);
559 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
560 mhladios (file
, "unable to open format file");
562 if (fstat (fileno (fp
), &st
) != NOTOK
) {
568 global
.c_ovtxt
= global
.c_nfs
= NULL
;
572 if ((i
= sc_width ()) > 5)
574 global
.c_cwidth
= -1;
575 if ((i
= sc_length ()) > 5)
576 global
.c_length
= i
- 1;
577 global
.c_flags
= BELL
; /* BELL is default */
578 *(ip
= ignores
) = NULL
;
581 while (vfgets (fp
, &ap
) == OK
) {
586 trim_suffix_c(bp
, '\n');
589 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
594 strncpy (name
, parse(), sizeof(name
));
600 * Split this list of fields to ignore, and copy
601 * it to the end of the current "ignores" list.
603 if (!strcasecmp (name
, "ignores")) {
604 char **tmparray
, **p
;
607 /* split the fields */
608 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
610 /* count number of fields split */
615 /* copy pointers to split fields to ignores array */
616 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
622 if (evalvar (&global
))
623 mhladios (NULL
, "format file syntax error: %s", bp
);
630 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
631 while (*parptr
== ':' || *parptr
== ',') {
634 mhladios (NULL
, "format file syntax error: %s", bp
);
636 if (!c1
->c_nfs
&& global
.c_nfs
) {
637 if (c1
->c_flags
& DATEFMT
) {
638 if (global
.c_flags
& DATEFMT
) {
639 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
640 compile_formatfield(c1
);
642 } else if (c1
->c_flags
& ADDRFMT
) {
643 if (global
.c_flags
& ADDRFMT
) {
644 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
645 compile_formatfield(c1
);
652 mhladios (NULL
, "format file syntax error: %s", bp
);
658 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
661 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
662 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
663 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
664 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
665 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
666 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
667 c1
->c_cwidth
, c1
->c_length
);
668 fprintf (stderr
, "\tflags=%s\n",
669 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
675 global
.c_flags
|= CLEARSCR
;
678 global
.c_flags
&= ~CLEARSCR
;
681 switch (bellflg
) { /* command line may override format file */
683 global
.c_flags
|= BELL
;
686 global
.c_flags
&= ~BELL
;
691 global
.c_length
= length
;
693 global
.c_width
= width
;
694 if (global
.c_length
< 5)
695 global
.c_length
= 10000;
696 if (global
.c_width
< 5)
697 global
.c_width
= 10000;
702 evalvar (struct mcomp
*c1
)
704 char *cp
, name
[NAMESZ
];
709 strncpy (name
, parse(), sizeof(name
));
711 if (!strcasecmp (name
, "component")) {
712 if (ptos (name
, &c1
->c_text
))
714 c1
->c_flags
&= ~NOCOMPONENT
;
718 if (!strcasecmp (name
, "overflowtext"))
719 return ptos (name
, &c1
->c_ovtxt
);
721 if (!strcasecmp (name
, "formatfield")) {
722 if (ptos (name
, &cp
))
724 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
725 compile_formatfield(c1
);
726 c1
->c_flags
|= FORMAT
;
730 if (!strcasecmp (name
, "decode")) {
731 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
732 compile_formatfield(c1
);
733 c1
->c_flags
|= FORMAT
;
737 if (!strcasecmp (name
, "offset"))
738 return ptoi (name
, &c1
->c_offset
);
739 if (!strcasecmp (name
, "overflowoffset"))
740 return ptoi (name
, &c1
->c_ovoff
);
741 if (!strcasecmp (name
, "width"))
742 return ptoi (name
, &c1
->c_width
);
743 if (!strcasecmp (name
, "compwidth"))
744 return ptoi (name
, &c1
->c_cwidth
);
745 if (!strcasecmp (name
, "length"))
746 return ptoi (name
, &c1
->c_length
);
747 if (!strcasecmp (name
, "nodashstuffing"))
748 return dashstuff
= -1;
750 for (ap
= triples
; ap
->t_name
; ap
++)
751 if (!strcasecmp (ap
->t_name
, name
)) {
752 c1
->c_flags
|= ap
->t_on
;
753 c1
->c_flags
&= ~ap
->t_off
;
757 if (!strcasecmp (name
, "formatarg")) {
758 struct arglist
*args
;
760 if (ptos (name
, &cp
))
763 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
764 inform("format filters are currently only supported on "
765 "the \"body\" component");
772 arglist_tail
->a_next
= args
;
779 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
790 ptoi (char *name
, int *i
)
794 if (*parptr
++ != '=' || !*(cp
= parse ())) {
795 inform("missing argument to variable %s", name
);
805 ptos (char *name
, char **s
)
809 if (*parptr
++ != '=') {
810 inform("missing argument to variable %s", name
);
814 if (*parptr
!= '"') {
816 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
820 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
821 if (*parptr
== QUOTE
)
828 if ((*parptr
= c
) == '"')
839 static char result
[NAMESZ
];
841 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
859 * Process one file/message
863 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
865 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
871 /* volatile to prevent "might be clobbered" warning from gcc: */
872 char *volatile fname2
= fname
? fname
: "(stdin)";
877 switch (setjmp (env
)) {
880 fp
= fopen(fname
, "r");
882 advise (fname
, "unable to open");
889 if (fstat(fileno(fp
), &st
) == 0) {
890 filesize
= st
.st_size
;
894 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
896 SIGNAL (SIGINT
, intrser
);
897 mhlfile (fp
, cp
, ofilen
, ofilec
);
900 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
901 fmt_free(ap
->a_fmt
, 0);
911 SIGNAL (SIGINT
, SIG_IGN
);
912 if (fp
!= stdin
&& fp
!= NULL
)
915 holder
.c_text
= NULL
;
916 free_queue (&msghd
, &msgtl
);
917 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
918 c1
->c_flags
&= ~HDROUTPUT
;
926 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
929 struct mcomp
*c1
, *c2
, *c3
;
930 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
931 m_getfld_state_t gstate
;
933 compile_filterargs();
937 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
939 fputs("\n-------", stdout
);
941 printf (" Forwarded Message%s", PLURALS(ofilec
));
943 printf (" Message %d", ofilen
);
951 if ((global
.c_flags
& CLEARSCR
))
956 printf (">>> %s\n\n", mname
);
962 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
965 printf ("Press <return> to list \"%s\"...", mname
);
969 if (ofilec
== 1 || linefeed_typed()) {
970 if ((global
.c_flags
& CLEARSCR
))
984 printf (">>> %s\n\n", mname
);
990 gstate
= m_getfld_state_init(fp
);
992 int bufsz
= sizeof buf
;
993 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
996 bucket
= fmt_addcomptext(name
, buf
);
997 for (ip
= ignores
; *ip
; ip
++)
998 if (!strcasecmp (name
, *ip
)) {
999 while (state
== FLDPLUS
) {
1001 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1002 fmt_appendcomp(bucket
, name
, buf
);
1009 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1010 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1013 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1014 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1015 if (!strcasecmp (FENDNULL(c1
->c_name
),
1016 FENDNULL(c3
->c_name
))) {
1018 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1022 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1023 while (state
== FLDPLUS
) {
1025 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1026 c1
->c_text
= add (buf
, c1
->c_text
);
1027 fmt_appendcomp(bucket
, name
, buf
);
1030 c1
->c_flags
|= EXTRA
;
1036 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1037 if (c1
->c_flags
& CLEARTEXT
) {
1038 putcomp (c1
, c1
, ONECOMP
);
1042 !strcasecmp (c1
->c_name
, "messagename")) {
1043 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1045 putcomp (c1
, &holder
, ONECOMP
);
1046 free (holder
.c_text
);
1047 holder
.c_text
= NULL
;
1050 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1051 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1052 if (c2
->c_flags
& EXTRA
)
1053 putcomp (c1
, c2
, TWOCOMP
);
1056 if (dobody
&& (!c1
->c_name
||
1057 !strcasecmp (c1
->c_name
, "body"))) {
1058 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1059 formatproc
!= NULL
) {
1060 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1062 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1063 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1064 while (state
== BODY
) {
1065 putcomp (c1
, &holder
, BODYCOMP
);
1067 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1070 free (holder
.c_text
);
1071 holder
.c_text
= NULL
;
1075 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1076 if (!strcasecmp (FENDNULL(c2
->c_name
),
1077 FENDNULL(c1
->c_name
))) {
1078 putcomp (c1
, c2
, ONECOMP
);
1079 if (!(c1
->c_flags
& SPLIT
))
1083 m_getfld_state_destroy (&gstate
);
1088 inform("format error in message %s", mname
);
1090 m_getfld_state_destroy (&gstate
);
1094 mhladios (NULL
, "getfld() returned %d", state
);
1101 mcomp_flags (char *name
)
1105 for (ap
= pairs
; ap
->p_name
; ap
++)
1106 if (!strcasecmp (ap
->p_name
, name
))
1114 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1118 if (!(flags
& ADDRFMT
))
1119 return add (s1
, s2
);
1121 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1124 return add (s1
, add (",\n", s2
));
1131 struct pqpair
*pq_next
;
1136 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1141 struct pqpair
*p
, *q
;
1143 struct mailname
*mp
;
1150 dat
[3] = BUFSIZ
- 1;
1153 if (!(c1
->c_flags
& ADDRFMT
)) {
1154 charstring_t scanl
= charstring_create (BUFSIZ
);
1157 c1
->c_c_text
->c_text
= ap
;
1158 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1162 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1163 /* Don't need to append a newline, dctime() already did */
1164 c2
->c_text
= charstring_buffer_copy (scanl
);
1165 charstring_free (scanl
);
1167 /* ap is now owned by the component struct, so do NOT free it here */
1171 (q
= &pq
)->pq_next
= NULL
;
1172 while ((cp
= getname (ap
))) {
1174 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1175 p
->pq_text
= mh_xstrdup(cp
);
1176 p
->pq_error
= mh_xstrdup(error
);
1178 p
->pq_text
= getcpy (mp
->m_text
);
1181 q
= (q
->pq_next
= p
);
1184 for (p
= pq
.pq_next
; p
; p
= q
) {
1185 charstring_t scanl
= charstring_create (BUFSIZ
);
1189 c1
->c_c_text
->c_text
= p
->pq_text
;
1192 if (c1
->c_c_error
) {
1193 c1
->c_c_error
->c_text
= p
->pq_error
;
1197 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1198 buffer
= charstring_buffer_copy (scanl
);
1201 c2
->c_text
= add (",\n", c2
->c_text
);
1202 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1204 c2
->c_text
= add (buffer
, c2
->c_text
);
1206 charstring_free (scanl
);
1214 c2
->c_text
= add ("\n", c2
->c_text
);
1219 static struct mcomp
*
1220 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1225 c1
->c_flags
= flags
& ~INIT
;
1226 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1227 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1228 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1231 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1232 c1
->c_offset
= global
.c_offset
;
1233 c1
->c_ovoff
= global
. c_ovoff
;
1234 c1
->c_width
= c1
->c_length
= 0;
1235 c1
->c_cwidth
= global
.c_cwidth
;
1236 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1241 (*tail
)->c_next
= c1
;
1249 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1251 struct mcomp
*c1
, *c2
;
1253 for (c1
= *head
; c1
; c1
= c2
) {
1260 fmt_free (c1
->c_fmt
, 0);
1264 *head
= *tail
= NULL
;
1269 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1271 char *text
; /* c1's text, or the name as a fallback. */
1272 char *trimmed_prefix
;
1276 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1278 if (! utf8
&& flag
!= BODYCOMP
) {
1279 /* Don't print 8-bit bytes in header field values if not in a
1280 UTF-8 locale, as required by RFC 6532. */
1281 c1
->c_flags
|= FORCE7BIT
;
1284 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1285 /* Create a copy with trailing whitespace trimmed, for use with
1287 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1291 llim
= c1
->c_length
? c1
->c_length
: -1;
1292 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1293 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1295 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1297 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1298 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1299 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1302 if (c1
->c_flags
& CLEARTEXT
) {
1303 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1305 putstr ("\n", c1
->c_flags
);
1309 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1310 mcomp_format (c1
, c2
);
1312 if (c1
->c_flags
& CENTER
) {
1313 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1314 - c1
->c_offset
- strlen (c2
->c_text
);
1315 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1316 count
-= strlen(text
) + 2;
1317 lm
= c1
->c_offset
+ (count
/ 2);
1323 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1324 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1326 putstr(text
, c1
->c_flags
);
1327 if (flag
!= BODYCOMP
) {
1328 putstr (": ", c1
->c_flags
);
1329 if (!(c1
->c_flags
& SPLIT
))
1330 c1
->c_flags
|= HDROUTPUT
;
1333 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1335 putstr (" ", c1
->c_flags
);
1338 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1342 && !(c2
->c_flags
& HDROUTPUT
)
1343 && !(c2
->c_flags
& NOCOMPONENT
)) {
1344 if (c1
->c_flags
& UPPERCASE
)
1345 to_upper(c2
->c_name
);
1346 putstr (c2
->c_name
, c1
->c_flags
);
1347 putstr (": ", c1
->c_flags
);
1348 if (!(c1
->c_flags
& SPLIT
))
1349 c2
->c_flags
|= HDROUTPUT
;
1352 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1354 putstr (" ", c1
->c_flags
);
1356 if (c1
->c_flags
& UPPERCASE
)
1357 to_upper(c2
->c_text
);
1361 if (flag
== TWOCOMP
)
1362 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1363 : (int) strlen (c2
->c_name
) + 2;
1365 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1368 count
+= c1
->c_offset
;
1370 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1371 /* Output line, trimming trailing whitespace if requested. */
1372 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1374 putstr ("\n", c1
->c_flags
);
1375 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1377 if (flag
== BODYCOMP
1378 && !(c1
->c_flags
& NOCOMPONENT
)) {
1379 /* Output component, trimming trailing whitespace if there
1380 is no text on the line. */
1382 putstr(text
, c1
->c_flags
);
1384 putstr (trimmed_prefix
, c1
->c_flags
);
1388 /* Output line, trimming trailing whitespace if requested. */
1389 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1392 putstr ("\n", c1
->c_flags
);
1394 if (flag
== BODYCOMP
&& term
== '\n')
1395 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1397 free (trimmed_prefix
);
1402 oneline (char *stuff
, unsigned long flags
)
1410 return onelp
= NULL
;
1414 if (flags
& COMPRESS
) {
1415 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1416 if (isspace ((unsigned char) *onelp
)) {
1417 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1435 while (*onelp
&& *onelp
!= '\n')
1437 if (*onelp
== '\n') {
1441 if (flags
& LEFTADJUST
)
1442 while (*ret
== ' ' || *ret
== '\t')
1445 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1453 putstr (char *string
, unsigned long flags
)
1455 /* To not count, for the purpose of counting columns, all of
1456 the bytes of a multibyte character. */
1459 if (!column
&& lm
> 0) {
1462 putch ('\t', flags
);
1472 #ifdef MULTIBYTE_SUPPORT
1473 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1476 NMH_UNUSED (char_len
);
1480 flags
&= ~INVISIBLE
;
1481 #ifdef MULTIBYTE_SUPPORT
1482 /* mbtowc should never return 0, because *string is non-NULL. */
1483 if (char_len
<= 0) {
1484 /* Find number of bytes in next character. */
1486 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1490 /* Multibyte character, after the first byte. */
1496 putch (*string
++, flags
);
1502 putch (char ch
, unsigned long flags
)
1513 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1515 if (global
.c_flags
& BELL
)
1518 if (linefeed_typed()) {
1519 if (global
.c_flags
& CLEARSCR
)
1520 nmh_clear_screen ();
1524 row
= global
.c_length
/ 3;
1543 * If we are forwarding this message, and the first
1544 * column contains a dash, then add a dash and a space.
1546 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1551 * Increment the character count, unless
1552 * 1) In UTF-8 locale, this is other than the last byte of
1553 a multibyte character, or
1554 * 2) In C locale, will print a non-printable character.
1556 if ((flags
& FORCE7BIT
) == 0) {
1558 if ((flags
& INVISIBLE
) == 0) {
1559 /* If multibyte character, its first byte only. */
1563 /* If not an ASCII character, the replace character will be
1564 displayed. Count it. */
1565 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1572 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1573 putch ('\n', flags
);
1576 putstr (FENDNULL(ovtxt
), flags
);
1581 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1588 /* linefeed_typed() makes a single read(2) from stdin and returns true
1589 * if a linefeed character is amongst the characters read.
1590 * A read error is treated as if linefeed wasn't typed.
1592 * Typing on a TTY can cause read() to return data without typing Enter
1593 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1594 * The linefeed can also be escaped with the TTY's LNEXT character,
1595 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1596 * It's not possible to distinguish between the user typing a buffer's
1597 * worth of characters and then EOT, or more than the buffer can hold.
1598 * Either way, the result depends on ASCII LF, either from typing Enter
1599 * or an escaped Ctrl-J, being amongst the read characters.
1602 linefeed_typed(void)
1607 n
= read(0, buf
, sizeof buf
);
1609 advise("stdin", "read");
1610 return false; /* Treat as EOF. */
1613 return memchr(buf
, '\n', n
);
1624 longjmp (env
, DONE
);
1649 mhladios (char *what
, char *fmt
, ...)
1654 advertise (what
, NULL
, fmt
, ap
);
1661 mhldone (int status
)
1669 * Compile a format string used by the formatfield option and save it
1672 * We will want the {text} (and possibly {error}) components for later,
1673 * so look for them and save them if we find them.
1677 compile_formatfield(struct mcomp
*c1
)
1679 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1682 * As a note to myself and any other poor bastard who is looking through
1683 * this code in the future ....
1685 * When the format hash table is reset later on (as it almost certainly
1686 * will be), there will still be references to these components in the
1687 * compiled format instructions. Thus these component references will
1688 * be free'd when the format instructions are free'd (by fmt_free()).
1690 * So, in other words ... don't go free'ing them yourself!
1693 c1
->c_c_text
= fmt_findcomp("text");
1694 c1
->c_c_error
= fmt_findcomp("error");
1698 * Compile all of the arguments for our format list.
1700 * Iterate through the linked list of format strings and compile them.
1701 * Note that we reset the format hash table before we start, but we do NOT
1702 * reset it between calls to fmt_compile().
1707 compile_filterargs (void)
1709 struct arglist
*arg
= arglist_head
;
1716 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1721 * Search through and mark any components that are address components
1724 for (ap
= addrcomps
; *ap
; ap
++) {
1725 cptr
= fmt_findcomp (*ap
);
1727 cptr
->c_type
|= CT_ADDR
;
1732 * Filter the body of a message through a specified format program
1736 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1737 m_getfld_state_t gstate
)
1739 struct mcomp holder
;
1741 int fdinput
[2], fdoutput
[2], waitstat
;
1743 pid_t writerpid
, filterpid
;
1746 * Create pipes so we can communicate with our filter process.
1749 if (pipe(fdinput
) < 0) {
1750 die("Unable to create input pipe");
1753 if (pipe(fdoutput
) < 0) {
1754 die("Unable to create output pipe");
1758 * Here's what we're doing to do.
1760 * - Fork ourselves and start writing data to the write side of the
1761 * input pipe (fdinput[1]).
1763 * - Fork and exec our filter program. We set the standard input of
1764 * our filter program to be the read side of our input pipe (fdinput[0]).
1765 * Standard output is set to the write side of our output pipe
1768 * - We read from the read side of the output pipe (fdoutput[0]).
1770 * We're forking because that's the simplest way to prevent any deadlocks.
1771 * (without doing something like switching to non-blocking I/O and using
1772 * select or poll, and I'm not interested in doing that).
1775 switch (writerpid
= fork()) {
1778 * Our child process - just write to the filter input (fdinput[1]).
1779 * Close all other descriptors that we don't need.
1787 * Call m_getfld2() until we're no longer in the BODY state
1790 while (state
== BODY
) {
1792 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1793 advise ("pipe output", "write");
1795 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1799 * We should be done; time to exit.
1804 * Make sure we call _exit(), otherwise we may flush out the stdio
1805 * buffers that we have duplicated from the parent.
1809 die("Unable to fork for filter writer process");
1814 * Fork and exec() our filter program, after redirecting standard in
1815 * and standard out appropriately.
1818 switch (filterpid
= fork()) {
1819 char **args
, *program
;
1821 int i
, dat
[5], s
, argp
;
1825 * Configure an argument array for us
1828 args
= argsplit(formatproc
, &program
, &argp
);
1829 args
[argp
+ filter_nargs
] = NULL
;
1837 * Pull out each argument and scan them.
1840 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1841 charstring_t scanl
= charstring_create (BUFSIZ
);
1843 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1844 args
[i
] = charstring_buffer_copy (scanl
);
1845 charstring_free (scanl
);
1847 * fmt_scan likes to put a trailing newline at the end of the
1848 * format string. If we have one, get rid of it.
1850 s
= strlen(args
[i
]);
1851 if (args
[i
][s
- 1] == '\n')
1852 args
[i
][s
- 1] = '\0';
1855 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1859 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1860 adios("formatproc", "Unable to dup2() standard input");
1862 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1863 adios("formatproc", "Unable to dup2() standard output");
1867 * Close everything (especially the old input and output
1868 * descriptors, since they've been dup'd to stdin and stdout),
1869 * and exec the formatproc.
1877 execvp(formatproc
, args
);
1879 adios(formatproc
, "Unable to execute filter");
1884 die("Unable to fork format program");
1888 * Close everything except our reader (fdoutput[0]);
1896 * As we read in this data, send it to putcomp
1899 holder
.c_text
= buf
;
1901 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1903 putcomp(c1
, &holder
, BODYCOMP
);
1907 die("reading from formatproc");
1911 * See if we got any errors along the way. I'm a little leery of calling
1912 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1915 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1916 if (errno
!= ECHILD
) {
1917 adios("filterproc", "Unable to determine status");
1920 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1921 pidstatus(waitstat
, stderr
, "filterproc");
1925 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1926 if (errno
!= ECHILD
) {
1927 adios("writer process", "Unable to determine status");
1931 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1932 pidstatus(waitstat
, stderr
, "writer process");