]>
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.
10 #include <h/addrsbr.h>
11 #include <h/fmt_scan.h>
15 #include "sbr/m_popen.h"
17 #include <sys/types.h>
18 #include "sbr/terminal.h"
22 * for a component containing addresses, ADDRFMT, if COMPRESS is also
23 * set, then addresses get split wrong (not at the spaces between commas).
24 * To fix this correctly, putstr() should know about "atomic" strings that
25 * must NOT be broken across lines. That's too difficult for right now
26 * (it turns out that there are a number of degenerate cases), so in
27 * oneline(), instead of
29 * (*onelp == '\n' && !onelp[1])
31 * being a terminating condition,
33 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
35 * is used instead. This cuts the line prematurely, and gives us a much
36 * better chance of getting things right.
45 #define MHL_SWITCHES \
46 X("bell", 0, BELLSW) \
47 X("nobell", 0, NBELLSW) \
48 X("clear", 0, CLRSW) \
49 X("noclear", 0, NCLRSW) \
50 X("folder +folder", 0, FOLDSW) \
51 X("form formfile", 0, FORMSW) \
52 X("moreproc program", 0, PROGSW) \
53 X("nomoreproc", 0, NPROGSW) \
54 X("length lines", 0, LENSW) \
55 X("width columns", 0, WIDTHSW) \
56 X("sleep seconds", 0, SLEEPSW) \
57 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
58 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
59 X("version", 0, VERSIONSW) \
60 X("help", 0, HELPSW) \
61 X("forward", -7, FORW1SW) /* interface from forw */ \
62 X("forwall", -7, FORW2SW) /* interface from forw */ \
63 X("digest list", -6, DGSTSW) \
64 X("volume number", -6, VOLUMSW) \
65 X("issue number", -5, ISSUESW) \
66 X("nobody", -6, NBODYSW) \
67 X("fmtproc program", 0, FMTPROCSW) \
68 X("nofmtproc", 0, NFMTPROCSW) \
70 #define X(sw, minchars, id) id,
71 DEFINE_SWITCH_ENUM(MHL
);
74 #define X(sw, minchars, id) { sw, minchars, id },
75 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
78 #define NOCOMPONENT 0x000001 /* don't show component name */
79 #define UPPERCASE 0x000002 /* display in all upper case */
80 #define CENTER 0x000004 /* center line */
81 #define CLEARTEXT 0x000008 /* cleartext */
82 #define EXTRA 0x000010 /* an "extra" component */
83 #define HDROUTPUT 0x000020 /* already output */
84 #define CLEARSCR 0x000040 /* clear screen */
85 #define LEFTADJUST 0x000080 /* left justify multiple lines */
86 #define COMPRESS 0x000100 /* compress text */
87 #define ADDRFMT 0x000200 /* contains addresses */
88 #define BELL 0x000400 /* sound bell at EOP */
89 #define DATEFMT 0x000800 /* contains dates */
90 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
91 #define INIT 0x002000 /* initialize component */
92 #define RTRIM 0x004000 /* trim trailing whitespace */
93 #define SPLIT 0x010000 /* split headers (don't concatenate) */
94 #define NONEWLINE 0x020000 /* don't write trailing newline */
95 #define NOWRAP 0x040000 /* Don't wrap lines ever */
96 #define FMTFILTER 0x080000 /* Filter through format filter */
97 #define INVISIBLE 0x100000 /* count byte in display columns? */
98 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
99 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
100 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
103 * A format string to be used as a command-line argument to the body
108 struct format
*a_fmt
;
110 struct arglist
*a_next
;
114 * Linked list of command line arguments for the body format filter. This
115 * USED to be in "struct mcomp", but the format API got cleaned up and even
116 * though it reduced the code we had to do, it make things more complicated
117 * for us. Specifically:
119 * - The interface to the hash table has been cleaned up, which means the
120 * rooting around in the hash table is no longer necessary (yay!). But
121 * this ALSO means that we have to make sure that we call our format
122 * compilation routines before we process the message, because the
123 * components need to be visible in the hash table so we can save them for
124 * later. So we moved them out of "mcomp" and now compile them right before
125 * header processing starts.
126 * - We also use format strings to handle other components in the mhl
127 * configuration (using "formatfield" and "decode"), but here life
128 * gets complicated: they aren't dealt with in the normal way. Instead
129 * of referring to a component like {from}, each component is processed
130 * using the special {text} component. But these format strings need to be
131 * compiled BEFORE we compile the format arguments; in the previous
132 * implementation they were compiled and scanned as the headers were
133 * read, and that would reset the hash table that we need to populate
134 * the components used by the body format filter. So we are compiling
135 * the formatfield component strings ahead of time and then scanning them
138 * Okay, fine ... this was broken before. But you know what? Fixing this
139 * the right way will make things easier down the road.
141 * One side-effect to this change: format strings are now compiled only once
142 * for components specified with "formatfield", but they are compiled for
143 * every message for format arguments.
146 static struct arglist
*arglist_head
;
147 static struct arglist
*arglist_tail
;
148 static int filter_nargs
= 0;
151 * Flags/options for each component
155 char *c_name
; /* component name */
156 char *c_text
; /* component text */
157 char *c_ovtxt
; /* text overflow indicator */
158 char *c_nfs
; /* iff FORMAT */
159 struct format
*c_fmt
; /* .. */
160 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
161 struct comp
*c_c_error
; /* Ref to {error} */
162 int c_offset
; /* left margin indentation */
163 int c_ovoff
; /* overflow indentation */
164 int c_width
; /* width of field */
165 int c_cwidth
; /* width of component */
166 int c_length
; /* length in lines */
167 unsigned long c_flags
;
168 struct mcomp
*c_next
;
171 static struct mcomp
*msghd
= NULL
;
172 static struct mcomp
*msgtl
= NULL
;
173 static struct mcomp
*fmthd
= NULL
;
174 static struct mcomp
*fmttl
= NULL
;
176 static struct mcomp global
= {
177 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
180 static struct mcomp holder
= {
181 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
186 unsigned long p_flags
;
189 static struct pair pairs
[] = {
192 { "Sender", ADDRFMT
},
193 { "Reply-To", ADDRFMT
},
197 { "Resent-Date", DATEFMT
},
198 { "Resent-From", ADDRFMT
},
199 { "Resent-Sender", ADDRFMT
},
200 { "Resent-Reply-To", ADDRFMT
},
201 { "Resent-To", ADDRFMT
},
202 { "Resent-cc", ADDRFMT
},
203 { "Resent-Bcc", ADDRFMT
},
213 static struct triple triples
[] = {
214 { "nocomponent", NOCOMPONENT
, 0 },
215 { "uppercase", UPPERCASE
, 0 },
216 { "nouppercase", 0, UPPERCASE
},
217 { "center", CENTER
, 0 },
218 { "nocenter", 0, CENTER
},
219 { "clearscreen", CLEARSCR
, 0 },
220 { "noclearscreen", 0, CLEARSCR
},
221 { "noclear", 0, CLEARSCR
},
222 { "leftadjust", LEFTADJUST
, 0 },
223 { "noleftadjust", 0, LEFTADJUST
},
224 { "compress", COMPRESS
, 0 },
225 { "nocompress", 0, COMPRESS
},
226 { "split", SPLIT
, 0 },
227 { "nosplit", 0, SPLIT
},
228 { "rtrim", RTRIM
, 0 },
229 { "nortrim", 0, RTRIM
},
230 { "addrfield", ADDRFMT
, DATEFMT
},
232 { "nobell", 0, BELL
},
233 { "datefield", DATEFMT
, ADDRFMT
},
234 { "newline", 0, NONEWLINE
},
235 { "nonewline", NONEWLINE
, 0 },
236 { "wrap", 0, NOWRAP
},
237 { "nowrap", NOWRAP
, 0 },
238 { "format", FMTFILTER
, 0 },
239 { "noformat", 0, FMTFILTER
},
243 static char *addrcomps
[] = {
260 static int bellflg
= 0;
261 static int clearflg
= 0;
262 static int dashstuff
= 0;
263 static int dobody
= 1;
264 static int forwflg
= 0;
265 static int forwall
= 0;
267 static int sleepsw
= NOTOK
;
269 static char *digest
= NULL
;
270 static int volume
= 0;
271 static int issue
= 0;
273 static int exitstat
= 0;
274 static int mhldebug
= 0;
276 static int filesize
= 0;
281 static int ontty
= NOTTY
;
284 static unsigned int column
;
290 static unsigned int wid
;
298 static int num_ignores
= 0;
299 static char *ignores
[MAXARGS
];
303 static char delim3
[] = /* from forw.c */
304 "\n----------------------------------------------------------------------\n\n";
305 static char delim4
[] = "\n------------------------------\n\n";
310 static void mhl_format (char *, int, int);
311 static int evalvar (struct mcomp
*);
312 static int ptoi (char *, int *);
313 static int ptos (char *, char **);
314 static char *parse (void);
315 static void process (char *, char *, int, int);
316 static void mhlfile (FILE *, char *, int, int);
317 static int mcomp_flags (char *) PURE
;
318 static char *mcomp_add (unsigned long, char *, char *);
319 static void mcomp_format (struct mcomp
*, struct mcomp
*);
320 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
321 static void free_queue (struct mcomp
**, struct mcomp
**);
322 static void putcomp (struct mcomp
*, struct mcomp
*, int);
323 static char *oneline (char *, unsigned long);
324 static void putstr (char *, unsigned long);
325 static void putch (char, unsigned long);
326 static bool linefeed_typed(void);
327 static void intrser (int);
328 static void pipeser (int);
329 static void quitser (int);
330 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
331 static void mhldone (int) NORETURN
;
332 static void filterbody (struct mcomp
*, char *, int, int,
334 static void compile_formatfield(struct mcomp
*);
335 static void compile_filterargs (void);
339 mhl (int argc
, char **argv
)
341 int length
= 0, nomore
= 0;
342 unsigned int i
, vecp
= 0;
344 char *cp
, *folder
= NULL
, *form
= NULL
;
345 char buf
[BUFSIZ
], *files
[MAXARGS
];
346 char **argp
, **arguments
;
348 /* Need this if called from main() of show(1). */
349 invo_name
= r1bindex (argv
[0], '/');
351 arguments
= getarguments (invo_name
, argc
, argv
, 1);
354 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
357 while ((cp
= *argp
++)) {
359 switch (smatch (++cp
, mhlswitches
)) {
361 ambigsw (cp
, mhlswitches
);
364 mhladios (NULL
, "-%s unknown\n", cp
);
367 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
368 print_help (buf
, mhlswitches
, 1);
371 print_version(invo_name
);
389 if (!(folder
= *argp
++) || *folder
== '-')
390 mhladios (NULL
, "missing argument to %s", argp
[-2]);
393 if (!(form
= *argp
++) || *form
== '-')
394 mhladios (NULL
, "missing argument to %s", argp
[-2]);
398 if (!(cp
= *argp
++) || *cp
== '-')
399 mhladios (NULL
, "missing argument to %s", argp
[-2]);
400 sleepsw
= atoi (cp
);/* ZERO ok! */
404 if (!(moreproc
= *argp
++) || *moreproc
== '-')
405 mhladios (NULL
, "missing argument to %s", argp
[-2]);
412 if (!(formatproc
= *argp
++) || *formatproc
== '-')
413 mhladios (NULL
, "missing argument to %s", argp
[-2]);
420 if (!(cp
= *argp
++) || *cp
== '-')
421 mhladios (NULL
, "missing argument to %s", argp
[-2]);
422 if ((length
= atoi (cp
)) < 1)
423 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
426 if (!(cp
= *argp
++) || *cp
== '-')
427 mhladios (NULL
, "missing argument to %s", argp
[-2]);
428 if ((width
= atoi (cp
)) < 1)
429 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
433 if (!(digest
= *argp
++) || *digest
== '-')
434 mhladios (NULL
, "missing argument to %s", argp
[-2]);
437 if (!(cp
= *argp
++) || *cp
== '-')
438 mhladios (NULL
, "missing argument to %s", argp
[-2]);
439 if ((issue
= atoi (cp
)) < 1)
440 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
443 if (!(cp
= *argp
++) || *cp
== '-')
444 mhladios (NULL
, "missing argument to %s", argp
[-2]);
445 if ((volume
= atoi (cp
)) < 1)
446 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
454 clearflg
= -1;/* XXX */
458 dashstuff
= 1; /* ternary logic */
461 dashstuff
= -1; /* ternary logic */
473 folder
= getenv ("mhfolder");
475 if (isatty (fileno (stdout
))) {
476 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
477 SIGNAL2 (SIGPIPE
, pipeser
);
478 m_popen(moreproc
, false);
481 SIGNAL (SIGINT
, SIG_IGN
);
482 SIGNAL2 (SIGQUIT
, quitser
);
489 mhl_format (form
? form
: mhlformat
, length
, width
);
492 process (folder
, NULL
, 1, vecp
= 1);
494 for (i
= 0; i
< vecp
; i
++)
495 process (folder
, files
[i
], i
+ 1, vecp
);
500 fputs(delim4
, stdout
);
502 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
504 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
505 digest
, volume
, issue
);
508 for (cp
= buf
+ i
; i
> 1; i
--)
515 printf ("\n------- End of Forwarded Message%s\n",
521 mhladios("output", "error writing");
524 if (clearflg
> 0 && ontty
== NOTTY
)
535 mhl_format (char *file
, int length
, int width
)
539 char *ap
, name
[NAMESZ
];
543 static dev_t dev
= 0;
544 static ino_t ino
= 0;
545 static time_t mtime
= 0;
548 if (stat (etcpath (file
), &st
) != NOTOK
549 && mtime
== st
.st_mtime
553 free_queue (&fmthd
, &fmttl
);
556 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
557 mhladios (file
, "unable to open format file");
559 if (fstat (fileno (fp
), &st
) != NOTOK
) {
565 global
.c_ovtxt
= global
.c_nfs
= NULL
;
569 if ((i
= sc_width ()) > 5)
571 global
.c_cwidth
= -1;
572 if ((i
= sc_length ()) > 5)
573 global
.c_length
= i
- 1;
574 global
.c_flags
= BELL
; /* BELL is default */
575 *(ip
= ignores
) = NULL
;
578 while (vfgets (fp
, &ap
) == OK
) {
583 trim_suffix_c(bp
, '\n');
586 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
591 strncpy (name
, parse(), sizeof(name
));
597 * Split this list of fields to ignore, and copy
598 * it to the end of the current "ignores" list.
600 if (!strcasecmp (name
, "ignores")) {
601 char **tmparray
, **p
;
604 /* split the fields */
605 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
607 /* count number of fields split */
612 /* copy pointers to split fields to ignores array */
613 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
619 if (evalvar (&global
))
620 mhladios (NULL
, "format file syntax error: %s", bp
);
627 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
628 while (*parptr
== ':' || *parptr
== ',') {
631 mhladios (NULL
, "format file syntax error: %s", bp
);
633 if (!c1
->c_nfs
&& global
.c_nfs
) {
634 if (c1
->c_flags
& DATEFMT
) {
635 if (global
.c_flags
& DATEFMT
) {
636 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
637 compile_formatfield(c1
);
639 } else if (c1
->c_flags
& ADDRFMT
) {
640 if (global
.c_flags
& ADDRFMT
) {
641 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
642 compile_formatfield(c1
);
649 mhladios (NULL
, "format file syntax error: %s", bp
);
655 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
658 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
659 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
660 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
661 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
662 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
663 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
664 c1
->c_cwidth
, c1
->c_length
);
665 fprintf (stderr
, "\tflags=%s\n",
666 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
672 global
.c_flags
|= CLEARSCR
;
675 global
.c_flags
&= ~CLEARSCR
;
678 switch (bellflg
) { /* command line may override format file */
680 global
.c_flags
|= BELL
;
683 global
.c_flags
&= ~BELL
;
688 global
.c_length
= length
;
690 global
.c_width
= width
;
691 if (global
.c_length
< 5)
692 global
.c_length
= 10000;
693 if (global
.c_width
< 5)
694 global
.c_width
= 10000;
699 evalvar (struct mcomp
*c1
)
701 char *cp
, name
[NAMESZ
];
706 strncpy (name
, parse(), sizeof(name
));
708 if (!strcasecmp (name
, "component")) {
709 if (ptos (name
, &c1
->c_text
))
711 c1
->c_flags
&= ~NOCOMPONENT
;
715 if (!strcasecmp (name
, "overflowtext"))
716 return ptos (name
, &c1
->c_ovtxt
);
718 if (!strcasecmp (name
, "formatfield")) {
719 if (ptos (name
, &cp
))
721 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
722 compile_formatfield(c1
);
723 c1
->c_flags
|= FORMAT
;
727 if (!strcasecmp (name
, "decode")) {
728 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
729 compile_formatfield(c1
);
730 c1
->c_flags
|= FORMAT
;
734 if (!strcasecmp (name
, "offset"))
735 return ptoi (name
, &c1
->c_offset
);
736 if (!strcasecmp (name
, "overflowoffset"))
737 return ptoi (name
, &c1
->c_ovoff
);
738 if (!strcasecmp (name
, "width"))
739 return ptoi (name
, &c1
->c_width
);
740 if (!strcasecmp (name
, "compwidth"))
741 return ptoi (name
, &c1
->c_cwidth
);
742 if (!strcasecmp (name
, "length"))
743 return ptoi (name
, &c1
->c_length
);
744 if (!strcasecmp (name
, "nodashstuffing"))
745 return dashstuff
= -1;
747 for (ap
= triples
; ap
->t_name
; ap
++)
748 if (!strcasecmp (ap
->t_name
, name
)) {
749 c1
->c_flags
|= ap
->t_on
;
750 c1
->c_flags
&= ~ap
->t_off
;
754 if (!strcasecmp (name
, "formatarg")) {
755 struct arglist
*args
;
757 if (ptos (name
, &cp
))
760 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
761 inform("format filters are currently only supported on "
762 "the \"body\" component");
769 arglist_tail
->a_next
= args
;
776 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
787 ptoi (char *name
, int *i
)
791 if (*parptr
++ != '=' || !*(cp
= parse ())) {
792 inform("missing argument to variable %s", name
);
802 ptos (char *name
, char **s
)
806 if (*parptr
++ != '=') {
807 inform("missing argument to variable %s", name
);
811 if (*parptr
!= '"') {
813 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
817 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
818 if (*parptr
== QUOTE
)
825 if ((*parptr
= c
) == '"')
836 static char result
[NAMESZ
];
838 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
856 * Process one file/message
860 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
862 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
868 /* volatile to prevent "might be clobbered" warning from gcc: */
869 char *volatile fname2
= fname
? fname
: "(stdin)";
874 switch (setjmp (env
)) {
877 fp
= fopen(fname
, "r");
879 advise (fname
, "unable to open");
886 if (fstat(fileno(fp
), &st
) == 0) {
887 filesize
= st
.st_size
;
891 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
893 SIGNAL (SIGINT
, intrser
);
894 mhlfile (fp
, cp
, ofilen
, ofilec
);
897 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
898 fmt_free(ap
->a_fmt
, 0);
908 SIGNAL (SIGINT
, SIG_IGN
);
909 if (fp
!= stdin
&& fp
!= NULL
)
912 holder
.c_text
= NULL
;
913 free_queue (&msghd
, &msgtl
);
914 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
915 c1
->c_flags
&= ~HDROUTPUT
;
923 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
926 struct mcomp
*c1
, *c2
, *c3
;
927 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
928 m_getfld_state_t gstate
;
930 compile_filterargs();
934 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
936 printf ("\n-------");
938 printf (" Forwarded Message%s", PLURALS(ofilec
));
940 printf (" Message %d", ofilen
);
948 if ((global
.c_flags
& CLEARSCR
))
953 printf (">>> %s\n\n", mname
);
959 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
962 printf ("Press <return> to list \"%s\"...", mname
);
966 if (ofilec
== 1 || linefeed_typed()) {
967 if ((global
.c_flags
& CLEARSCR
))
981 printf (">>> %s\n\n", mname
);
987 gstate
= m_getfld_state_init(fp
);
989 int bufsz
= sizeof buf
;
990 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
993 bucket
= fmt_addcomptext(name
, buf
);
994 for (ip
= ignores
; *ip
; ip
++)
995 if (!strcasecmp (name
, *ip
)) {
996 while (state
== FLDPLUS
) {
998 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
999 fmt_appendcomp(bucket
, name
, buf
);
1006 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1007 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1010 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1011 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1012 if (!strcasecmp (FENDNULL(c1
->c_name
),
1013 FENDNULL(c3
->c_name
))) {
1015 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1019 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1020 while (state
== FLDPLUS
) {
1022 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1023 c1
->c_text
= add (buf
, c1
->c_text
);
1024 fmt_appendcomp(bucket
, name
, buf
);
1027 c1
->c_flags
|= EXTRA
;
1033 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1034 if (c1
->c_flags
& CLEARTEXT
) {
1035 putcomp (c1
, c1
, ONECOMP
);
1039 !strcasecmp (c1
->c_name
, "messagename")) {
1040 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1042 putcomp (c1
, &holder
, ONECOMP
);
1043 free (holder
.c_text
);
1044 holder
.c_text
= NULL
;
1047 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1048 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1049 if (c2
->c_flags
& EXTRA
)
1050 putcomp (c1
, c2
, TWOCOMP
);
1053 if (dobody
&& (!c1
->c_name
||
1054 !strcasecmp (c1
->c_name
, "body"))) {
1055 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1056 formatproc
!= NULL
) {
1057 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1059 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1060 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1061 while (state
== BODY
) {
1062 putcomp (c1
, &holder
, BODYCOMP
);
1064 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1067 free (holder
.c_text
);
1068 holder
.c_text
= NULL
;
1072 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1073 if (!strcasecmp (FENDNULL(c2
->c_name
),
1074 FENDNULL(c1
->c_name
))) {
1075 putcomp (c1
, c2
, ONECOMP
);
1076 if (!(c1
->c_flags
& SPLIT
))
1080 m_getfld_state_destroy (&gstate
);
1085 inform("format error in message %s", mname
);
1087 m_getfld_state_destroy (&gstate
);
1091 mhladios (NULL
, "getfld() returned %d", state
);
1098 mcomp_flags (char *name
)
1102 for (ap
= pairs
; ap
->p_name
; ap
++)
1103 if (!strcasecmp (ap
->p_name
, name
))
1111 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1115 if (!(flags
& ADDRFMT
))
1116 return add (s1
, s2
);
1118 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1121 return add (s1
, add (",\n", s2
));
1128 struct pqpair
*pq_next
;
1133 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1138 struct pqpair
*p
, *q
;
1140 struct mailname
*mp
;
1147 dat
[3] = BUFSIZ
- 1;
1150 if (!(c1
->c_flags
& ADDRFMT
)) {
1151 charstring_t scanl
= charstring_create (BUFSIZ
);
1154 c1
->c_c_text
->c_text
= ap
;
1155 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1159 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1160 /* Don't need to append a newline, dctime() already did */
1161 c2
->c_text
= charstring_buffer_copy (scanl
);
1162 charstring_free (scanl
);
1164 /* ap is now owned by the component struct, so do NOT free it here */
1168 (q
= &pq
)->pq_next
= NULL
;
1169 while ((cp
= getname (ap
))) {
1171 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1172 p
->pq_text
= mh_xstrdup(cp
);
1173 p
->pq_error
= mh_xstrdup(error
);
1175 p
->pq_text
= getcpy (mp
->m_text
);
1178 q
= (q
->pq_next
= p
);
1181 for (p
= pq
.pq_next
; p
; p
= q
) {
1182 charstring_t scanl
= charstring_create (BUFSIZ
);
1186 c1
->c_c_text
->c_text
= p
->pq_text
;
1189 if (c1
->c_c_error
) {
1190 c1
->c_c_error
->c_text
= p
->pq_error
;
1194 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1195 buffer
= charstring_buffer_copy (scanl
);
1198 c2
->c_text
= add (",\n", c2
->c_text
);
1199 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1201 c2
->c_text
= add (buffer
, c2
->c_text
);
1203 charstring_free (scanl
);
1211 c2
->c_text
= add ("\n", c2
->c_text
);
1216 static struct mcomp
*
1217 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1222 c1
->c_flags
= flags
& ~INIT
;
1223 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1224 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1225 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1228 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1229 c1
->c_offset
= global
.c_offset
;
1230 c1
->c_ovoff
= global
. c_ovoff
;
1231 c1
->c_width
= c1
->c_length
= 0;
1232 c1
->c_cwidth
= global
.c_cwidth
;
1233 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1238 (*tail
)->c_next
= c1
;
1246 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1248 struct mcomp
*c1
, *c2
;
1250 for (c1
= *head
; c1
; c1
= c2
) {
1257 fmt_free (c1
->c_fmt
, 0);
1261 *head
= *tail
= NULL
;
1266 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1268 char *text
; /* c1's text, or the name as a fallback. */
1269 char *trimmed_prefix
;
1272 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1274 if (! utf8
&& flag
!= BODYCOMP
) {
1275 /* Don't print 8-bit bytes in header field values if not in a
1276 UTF-8 locale, as required by RFC 6532. */
1277 c1
->c_flags
|= FORCE7BIT
;
1280 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1281 /* Create a copy with trailing whitespace trimmed, for use with
1283 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1287 llim
= c1
->c_length
? c1
->c_length
: -1;
1288 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1289 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1291 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1293 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1294 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1295 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1298 if (c1
->c_flags
& CLEARTEXT
) {
1299 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1301 putstr ("\n", c1
->c_flags
);
1305 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1306 mcomp_format (c1
, c2
);
1308 if (c1
->c_flags
& CENTER
) {
1309 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1310 - c1
->c_offset
- strlen (c2
->c_text
);
1311 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1312 count
-= strlen(text
) + 2;
1313 lm
= c1
->c_offset
+ (count
/ 2);
1319 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1320 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1322 putstr(text
, c1
->c_flags
);
1323 if (flag
!= BODYCOMP
) {
1324 putstr (": ", c1
->c_flags
);
1325 if (!(c1
->c_flags
& SPLIT
))
1326 c1
->c_flags
|= HDROUTPUT
;
1329 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1331 putstr (" ", c1
->c_flags
);
1334 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1338 && !(c2
->c_flags
& HDROUTPUT
)
1339 && !(c2
->c_flags
& NOCOMPONENT
)) {
1340 if (c1
->c_flags
& UPPERCASE
)
1341 to_upper(c2
->c_name
);
1342 putstr (c2
->c_name
, c1
->c_flags
);
1343 putstr (": ", c1
->c_flags
);
1344 if (!(c1
->c_flags
& SPLIT
))
1345 c2
->c_flags
|= HDROUTPUT
;
1348 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1350 putstr (" ", c1
->c_flags
);
1352 if (c1
->c_flags
& UPPERCASE
)
1353 to_upper(c2
->c_text
);
1357 if (flag
== TWOCOMP
)
1358 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1359 : (int) strlen (c2
->c_name
) + 2;
1361 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1364 count
+= c1
->c_offset
;
1366 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1367 /* Output line, trimming trailing whitespace if requested. */
1368 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1370 putstr ("\n", c1
->c_flags
);
1371 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1373 if (flag
== BODYCOMP
1374 && !(c1
->c_flags
& NOCOMPONENT
)) {
1375 /* Output component, trimming trailing whitespace if there
1376 is no text on the line. */
1378 putstr(text
, c1
->c_flags
);
1380 putstr (trimmed_prefix
, c1
->c_flags
);
1384 /* Output line, trimming trailing whitespace if requested. */
1385 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1388 putstr ("\n", c1
->c_flags
);
1390 if (flag
== BODYCOMP
&& term
== '\n')
1391 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1393 free (trimmed_prefix
);
1398 oneline (char *stuff
, unsigned long flags
)
1406 return onelp
= NULL
;
1410 if (flags
& COMPRESS
) {
1411 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1412 if (isspace ((unsigned char) *onelp
)) {
1413 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1431 while (*onelp
&& *onelp
!= '\n')
1433 if (*onelp
== '\n') {
1437 if (flags
& LEFTADJUST
)
1438 while (*ret
== ' ' || *ret
== '\t')
1441 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1449 putstr (char *string
, unsigned long flags
)
1451 /* To not count, for the purpose of counting columns, all of
1452 the bytes of a multibyte character. */
1455 if (!column
&& lm
> 0) {
1458 putch ('\t', flags
);
1468 #ifdef MULTIBYTE_SUPPORT
1469 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1472 NMH_UNUSED (char_len
);
1476 flags
&= ~INVISIBLE
;
1477 #ifdef MULTIBYTE_SUPPORT
1478 /* mbtowc should never return 0, because *string is non-NULL. */
1479 if (char_len
<= 0) {
1480 /* Find number of bytes in next character. */
1482 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1486 /* Multibyte character, after the first byte. */
1492 putch (*string
++, flags
);
1498 putch (char ch
, unsigned long flags
)
1509 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1511 if (global
.c_flags
& BELL
)
1514 if (linefeed_typed()) {
1515 if (global
.c_flags
& CLEARSCR
)
1516 nmh_clear_screen ();
1520 row
= global
.c_length
/ 3;
1539 * If we are forwarding this message, and the first
1540 * column contains a dash, then add a dash and a space.
1542 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1547 * Increment the character count, unless
1548 * 1) In UTF-8 locale, this is other than the last byte of
1549 a multibyte character, or
1550 * 2) In C locale, will print a non-printable character.
1552 if ((flags
& FORCE7BIT
) == 0) {
1554 if ((flags
& INVISIBLE
) == 0) {
1555 /* If multibyte character, its first byte only. */
1559 /* If not an ASCII character, the replace character will be
1560 displayed. Count it. */
1561 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1568 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1569 putch ('\n', flags
);
1572 putstr (FENDNULL(ovtxt
), flags
);
1577 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1584 /* linefeed_typed() makes a single read(2) from stdin and returns true
1585 * if a linefeed character is amongst the characters read.
1586 * A read error is treated as if linefeed wasn't typed.
1588 * Typing on a TTY can cause read() to return data without typing Enter
1589 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1590 * The linefeed can also be escaped with the TTY's LNEXT character,
1591 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1592 * It's not possible to distinguish between the user typing a buffer's
1593 * worth of characters and then EOT, or more than the buffer can hold.
1594 * Either way, the result depends on ASCII LF, either from typing Enter
1595 * or an escaped Ctrl-J, being amongst the read characters.
1597 static bool linefeed_typed(void)
1602 n
= read(0, buf
, sizeof buf
);
1604 advise("stdin", "read");
1605 return false; /* Treat as EOF. */
1608 return memchr(buf
, '\n', n
);
1619 longjmp (env
, DONE
);
1644 mhladios (char *what
, char *fmt
, ...)
1649 advertise (what
, NULL
, fmt
, ap
);
1656 mhldone (int status
)
1664 * Compile a format string used by the formatfield option and save it
1667 * We will want the {text} (and possibly {error}) components for later,
1668 * so look for them and save them if we find them.
1672 compile_formatfield(struct mcomp
*c1
)
1674 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1677 * As a note to myself and any other poor bastard who is looking through
1678 * this code in the future ....
1680 * When the format hash table is reset later on (as it almost certainly
1681 * will be), there will still be references to these components in the
1682 * compiled format instructions. Thus these component references will
1683 * be free'd when the format instructions are free'd (by fmt_free()).
1685 * So, in other words ... don't go free'ing them yourself!
1688 c1
->c_c_text
= fmt_findcomp("text");
1689 c1
->c_c_error
= fmt_findcomp("error");
1693 * Compile all of the arguments for our format list.
1695 * Iterate through the linked list of format strings and compile them.
1696 * Note that we reset the format hash table before we start, but we do NOT
1697 * reset it between calls to fmt_compile().
1702 compile_filterargs (void)
1704 struct arglist
*arg
= arglist_head
;
1711 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1716 * Search through and mark any components that are address components
1719 for (ap
= addrcomps
; *ap
; ap
++) {
1720 cptr
= fmt_findcomp (*ap
);
1722 cptr
->c_type
|= CT_ADDR
;
1727 * Filter the body of a message through a specified format program
1731 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1732 m_getfld_state_t gstate
)
1734 struct mcomp holder
;
1736 int fdinput
[2], fdoutput
[2], waitstat
;
1738 pid_t writerpid
, filterpid
;
1741 * Create pipes so we can communicate with our filter process.
1744 if (pipe(fdinput
) < 0) {
1745 adios(NULL
, "Unable to create input pipe");
1748 if (pipe(fdoutput
) < 0) {
1749 adios(NULL
, "Unable to create output pipe");
1753 * Here's what we're doing to do.
1755 * - Fork ourselves and start writing data to the write side of the
1756 * input pipe (fdinput[1]).
1758 * - Fork and exec our filter program. We set the standard input of
1759 * our filter program to be the read side of our input pipe (fdinput[0]).
1760 * Standard output is set to the write side of our output pipe
1763 * - We read from the read side of the output pipe (fdoutput[0]).
1765 * We're forking because that's the simplest way to prevent any deadlocks.
1766 * (without doing something like switching to non-blocking I/O and using
1767 * select or poll, and I'm not interested in doing that).
1770 switch (writerpid
= fork()) {
1773 * Our child process - just write to the filter input (fdinput[1]).
1774 * Close all other descriptors that we don't need.
1782 * Call m_getfld2() until we're no longer in the BODY state
1785 while (state
== BODY
) {
1787 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1788 advise ("pipe output", "write");
1790 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1794 * We should be done; time to exit.
1799 * Make sure we call _exit(), otherwise we may flush out the stdio
1800 * buffers that we have duplicated from the parent.
1804 adios(NULL
, "Unable to fork for filter writer process");
1809 * Fork and exec() our filter program, after redirecting standard in
1810 * and standard out appropriately.
1813 switch (filterpid
= fork()) {
1814 char **args
, *program
;
1816 int i
, dat
[5], s
, argp
;
1820 * Configure an argument array for us
1823 args
= argsplit(formatproc
, &program
, &argp
);
1824 args
[argp
+ filter_nargs
] = NULL
;
1832 * Pull out each argument and scan them.
1835 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1836 charstring_t scanl
= charstring_create (BUFSIZ
);
1838 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1839 args
[i
] = charstring_buffer_copy (scanl
);
1840 charstring_free (scanl
);
1842 * fmt_scan likes to put a trailing newline at the end of the
1843 * format string. If we have one, get rid of it.
1845 s
= strlen(args
[i
]);
1846 if (args
[i
][s
- 1] == '\n')
1847 args
[i
][s
- 1] = '\0';
1850 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1854 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1855 adios("formatproc", "Unable to dup2() standard input");
1857 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1858 adios("formatproc", "Unable to dup2() standard output");
1862 * Close everything (especially the old input and output
1863 * descriptors, since they've been dup'd to stdin and stdout),
1864 * and exec the formatproc.
1872 execvp(formatproc
, args
);
1874 adios(formatproc
, "Unable to execute filter");
1879 adios(NULL
, "Unable to fork format program");
1883 * Close everything except our reader (fdoutput[0]);
1891 * As we read in this data, send it to putcomp
1894 holder
.c_text
= buf
;
1896 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1898 putcomp(c1
, &holder
, BODYCOMP
);
1902 adios(NULL
, "reading from formatproc");
1906 * See if we got any errors along the way. I'm a little leery of calling
1907 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1910 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1911 if (errno
!= ECHILD
) {
1912 adios("filterproc", "Unable to determine status");
1915 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1916 pidstatus(waitstat
, stderr
, "filterproc");
1920 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1921 if (errno
!= ECHILD
) {
1922 adios("writer process", "Unable to determine status");
1926 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1927 pidstatus(waitstat
, stderr
, "writer process");