]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
013b27a09a7d676a3d2a86db115c19bcd4d03249
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/trimcpy.h"
10 #include "sbr/vfgets.h"
11 #include "sbr/check_charset.h"
12 #include "sbr/getcpy.h"
13 #include "sbr/brkstring.h"
14 #include "sbr/ambigsw.h"
15 #include "sbr/pidstatus.h"
16 #include "sbr/print_version.h"
17 #include "sbr/print_help.h"
18 #include "sbr/arglist.h"
19 #include "sbr/error.h"
20 #include "h/signals.h"
21 #include "h/addrsbr.h"
22 #include "h/fmt_scan.h"
26 #include "sbr/m_popen.h"
28 #include <sys/types.h>
29 #include "sbr/terminal.h"
33 * for a component containing addresses, ADDRFMT, if COMPRESS is also
34 * set, then addresses get split wrong (not at the spaces between commas).
35 * To fix this correctly, putstr() should know about "atomic" strings that
36 * must NOT be broken across lines. That's too difficult for right now
37 * (it turns out that there are a number of degenerate cases), so in
38 * oneline(), instead of
40 * (*onelp == '\n' && !onelp[1])
42 * being a terminating condition,
44 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
46 * is used instead. This cuts the line prematurely, and gives us a much
47 * better chance of getting things right.
56 #define MHL_SWITCHES \
57 X("bell", 0, BELLSW) \
58 X("nobell", 0, NBELLSW) \
59 X("clear", 0, CLRSW) \
60 X("noclear", 0, NCLRSW) \
61 X("folder +folder", 0, FOLDSW) \
62 X("form formfile", 0, FORMSW) \
63 X("moreproc program", 0, PROGSW) \
64 X("nomoreproc", 0, NPROGSW) \
65 X("length lines", 0, LENSW) \
66 X("width columns", 0, WIDTHSW) \
67 X("sleep seconds", 0, SLEEPSW) \
68 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
69 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
70 X("version", 0, VERSIONSW) \
71 X("help", 0, HELPSW) \
72 X("forward", -7, FORW1SW) /* interface from forw */ \
73 X("forwall", -7, FORW2SW) /* interface from forw */ \
74 X("digest list", -6, DGSTSW) \
75 X("volume number", -6, VOLUMSW) \
76 X("issue number", -5, ISSUESW) \
77 X("nobody", -6, NBODYSW) \
78 X("fmtproc program", 0, FMTPROCSW) \
79 X("nofmtproc", 0, NFMTPROCSW) \
81 #define X(sw, minchars, id) id,
82 DEFINE_SWITCH_ENUM(MHL
);
85 #define X(sw, minchars, id) { sw, minchars, id },
86 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
89 #define NOCOMPONENT 0x000001 /* don't show component name */
90 #define UPPERCASE 0x000002 /* display in all upper case */
91 #define CENTER 0x000004 /* center line */
92 #define CLEARTEXT 0x000008 /* cleartext */
93 #define EXTRA 0x000010 /* an "extra" component */
94 #define HDROUTPUT 0x000020 /* already output */
95 #define CLEARSCR 0x000040 /* clear screen */
96 #define LEFTADJUST 0x000080 /* left justify multiple lines */
97 #define COMPRESS 0x000100 /* compress text */
98 #define ADDRFMT 0x000200 /* contains addresses */
99 #define BELL 0x000400 /* sound bell at EOP */
100 #define DATEFMT 0x000800 /* contains dates */
101 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
102 #define INIT 0x002000 /* initialize component */
103 #define RTRIM 0x004000 /* trim trailing whitespace */
104 #define SPLIT 0x010000 /* split headers (don't concatenate) */
105 #define NONEWLINE 0x020000 /* don't write trailing newline */
106 #define NOWRAP 0x040000 /* Don't wrap lines ever */
107 #define FMTFILTER 0x080000 /* Filter through format filter */
108 #define INVISIBLE 0x100000 /* count byte in display columns? */
109 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
110 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
111 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
114 * A format string to be used as a command-line argument to the body
119 struct format
*a_fmt
;
121 struct arglist
*a_next
;
125 * Linked list of command line arguments for the body format filter. This
126 * USED to be in "struct mcomp", but the format API got cleaned up and even
127 * though it reduced the code we had to do, it make things more complicated
128 * for us. Specifically:
130 * - The interface to the hash table has been cleaned up, which means the
131 * rooting around in the hash table is no longer necessary (yay!). But
132 * this ALSO means that we have to make sure that we call our format
133 * compilation routines before we process the message, because the
134 * components need to be visible in the hash table so we can save them for
135 * later. So we moved them out of "mcomp" and now compile them right before
136 * header processing starts.
137 * - We also use format strings to handle other components in the mhl
138 * configuration (using "formatfield" and "decode"), but here life
139 * gets complicated: they aren't dealt with in the normal way. Instead
140 * of referring to a component like {from}, each component is processed
141 * using the special {text} component. But these format strings need to be
142 * compiled BEFORE we compile the format arguments; in the previous
143 * implementation they were compiled and scanned as the headers were
144 * read, and that would reset the hash table that we need to populate
145 * the components used by the body format filter. So we are compiling
146 * the formatfield component strings ahead of time and then scanning them
149 * Okay, fine ... this was broken before. But you know what? Fixing this
150 * the right way will make things easier down the road.
152 * One side-effect to this change: format strings are now compiled only once
153 * for components specified with "formatfield", but they are compiled for
154 * every message for format arguments.
157 static struct arglist
*arglist_head
;
158 static struct arglist
*arglist_tail
;
159 static int filter_nargs
= 0;
162 * Flags/options for each component
166 char *c_name
; /* component name */
167 char *c_text
; /* component text */
168 char *c_ovtxt
; /* text overflow indicator */
169 char *c_nfs
; /* iff FORMAT */
170 struct format
*c_fmt
; /* .. */
171 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
172 struct comp
*c_c_error
; /* Ref to {error} */
173 int c_offset
; /* left margin indentation */
174 int c_ovoff
; /* overflow indentation */
175 int c_width
; /* width of field */
176 int c_cwidth
; /* width of component */
177 int c_length
; /* length in lines */
178 unsigned long c_flags
;
179 struct mcomp
*c_next
;
182 static struct mcomp
*msghd
= NULL
;
183 static struct mcomp
*msgtl
= NULL
;
184 static struct mcomp
*fmthd
= NULL
;
185 static struct mcomp
*fmttl
= NULL
;
187 static struct mcomp global
= {
188 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
191 static struct mcomp holder
= {
192 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
197 unsigned long p_flags
;
200 static struct pair pairs
[] = {
203 { "Sender", ADDRFMT
},
204 { "Reply-To", ADDRFMT
},
208 { "Resent-Date", DATEFMT
},
209 { "Resent-From", ADDRFMT
},
210 { "Resent-Sender", ADDRFMT
},
211 { "Resent-Reply-To", ADDRFMT
},
212 { "Resent-To", ADDRFMT
},
213 { "Resent-cc", ADDRFMT
},
214 { "Resent-Bcc", ADDRFMT
},
224 static struct triple triples
[] = {
225 { "nocomponent", NOCOMPONENT
, 0 },
226 { "uppercase", UPPERCASE
, 0 },
227 { "nouppercase", 0, UPPERCASE
},
228 { "center", CENTER
, 0 },
229 { "nocenter", 0, CENTER
},
230 { "clearscreen", CLEARSCR
, 0 },
231 { "noclearscreen", 0, CLEARSCR
},
232 { "noclear", 0, CLEARSCR
},
233 { "leftadjust", LEFTADJUST
, 0 },
234 { "noleftadjust", 0, LEFTADJUST
},
235 { "compress", COMPRESS
, 0 },
236 { "nocompress", 0, COMPRESS
},
237 { "split", SPLIT
, 0 },
238 { "nosplit", 0, SPLIT
},
239 { "rtrim", RTRIM
, 0 },
240 { "nortrim", 0, RTRIM
},
241 { "addrfield", ADDRFMT
, DATEFMT
},
243 { "nobell", 0, BELL
},
244 { "datefield", DATEFMT
, ADDRFMT
},
245 { "newline", 0, NONEWLINE
},
246 { "nonewline", NONEWLINE
, 0 },
247 { "wrap", 0, NOWRAP
},
248 { "nowrap", NOWRAP
, 0 },
249 { "format", FMTFILTER
, 0 },
250 { "noformat", 0, FMTFILTER
},
254 static char *addrcomps
[] = {
271 static int bellflg
= 0;
272 static int clearflg
= 0;
273 static int dashstuff
= 0;
274 static bool dobody
= true;
278 static int sleepsw
= NOTOK
;
280 static char *digest
= NULL
;
281 static int volume
= 0;
282 static int issue
= 0;
284 static int exitstat
= 0;
285 static bool mhldebug
;
287 static int filesize
= 0;
292 static int ontty
= NOTTY
;
295 static unsigned int column
;
301 static unsigned int wid
;
309 static int num_ignores
= 0;
310 static char *ignores
[MAXARGS
];
314 static char delim3
[] = /* from forw.c */
315 "\n----------------------------------------------------------------------\n\n";
316 static char delim4
[] = "\n------------------------------\n\n";
321 static void mhl_format (char *, int, int);
322 static int evalvar (struct mcomp
*);
323 static int ptoi (char *, int *);
324 static int ptos (char *, char **);
325 static char *parse (void);
326 static void process (char *, char *, int, int);
327 static void mhlfile (FILE *, char *, int, int);
328 static int mcomp_flags (char *) PURE
;
329 static char *mcomp_add (unsigned long, char *, char *);
330 static void mcomp_format (struct mcomp
*, struct mcomp
*);
331 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
332 static void free_queue (struct mcomp
**, struct mcomp
**);
333 static void putcomp (struct mcomp
*, struct mcomp
*, int);
334 static char *oneline (char *, unsigned long);
335 static void putstr (char *, unsigned long);
336 static void putch (char, unsigned long);
337 static bool linefeed_typed(void);
338 static void intrser (int);
339 static void pipeser (int);
340 static void quitser (int);
341 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
342 static void mhldone (int) NORETURN
;
343 static void filterbody (struct mcomp
*, char *, int, int,
345 static void compile_formatfield(struct mcomp
*);
346 static void compile_filterargs (void);
350 mhl (int argc
, char **argv
)
354 unsigned int i
, vecp
= 0;
356 char *cp
, *folder
= NULL
, *form
= NULL
;
357 char buf
[BUFSIZ
], *files
[MAXARGS
];
358 char **argp
, **arguments
;
360 /* Need this if called from main() of show(1). */
361 invo_name
= r1bindex (argv
[0], '/');
363 arguments
= getarguments (invo_name
, argc
, argv
, 1);
366 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
369 while ((cp
= *argp
++)) {
371 switch (smatch (++cp
, mhlswitches
)) {
373 ambigsw (cp
, mhlswitches
);
376 mhladios (NULL
, "-%s unknown\n", cp
);
379 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
380 print_help (buf
, mhlswitches
, 1);
383 print_version(invo_name
);
401 if (!(folder
= *argp
++) || *folder
== '-')
402 mhladios (NULL
, "missing argument to %s", argp
[-2]);
405 if (!(form
= *argp
++) || *form
== '-')
406 mhladios (NULL
, "missing argument to %s", argp
[-2]);
410 if (!(cp
= *argp
++) || *cp
== '-')
411 mhladios (NULL
, "missing argument to %s", argp
[-2]);
412 sleepsw
= atoi (cp
);/* ZERO ok! */
416 if (!(moreproc
= *argp
++) || *moreproc
== '-')
417 mhladios (NULL
, "missing argument to %s", argp
[-2]);
424 if (!(formatproc
= *argp
++) || *formatproc
== '-')
425 mhladios (NULL
, "missing argument to %s", argp
[-2]);
432 if (!(cp
= *argp
++) || *cp
== '-')
433 mhladios (NULL
, "missing argument to %s", argp
[-2]);
434 if ((length
= atoi (cp
)) < 1)
435 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
438 if (!(cp
= *argp
++) || *cp
== '-')
439 mhladios (NULL
, "missing argument to %s", argp
[-2]);
440 if ((width
= atoi (cp
)) < 1)
441 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
445 if (!(digest
= *argp
++) || *digest
== '-')
446 mhladios (NULL
, "missing argument to %s", argp
[-2]);
449 if (!(cp
= *argp
++) || *cp
== '-')
450 mhladios (NULL
, "missing argument to %s", argp
[-2]);
451 if ((issue
= atoi (cp
)) < 1)
452 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
455 if (!(cp
= *argp
++) || *cp
== '-')
456 mhladios (NULL
, "missing argument to %s", argp
[-2]);
457 if ((volume
= atoi (cp
)) < 1)
458 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
466 clearflg
= -1;/* XXX */
470 dashstuff
= 1; /* ternary logic */
473 dashstuff
= -1; /* ternary logic */
485 folder
= getenv ("mhfolder");
487 if (isatty (fileno (stdout
))) {
488 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
489 SIGNAL2 (SIGPIPE
, pipeser
);
490 m_popen(moreproc
, false);
493 SIGNAL (SIGINT
, SIG_IGN
);
494 SIGNAL2 (SIGQUIT
, quitser
);
501 mhl_format (form
? form
: mhlformat
, length
, width
);
504 process (folder
, NULL
, 1, vecp
= 1);
506 for (i
= 0; i
< vecp
; i
++)
507 process (folder
, files
[i
], i
+ 1, vecp
);
512 fputs(delim4
, stdout
);
514 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
516 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
517 digest
, volume
, issue
);
520 for (cp
= buf
+ i
; i
> 1; i
--)
527 printf ("\n------- End of Forwarded Message%s\n",
533 mhladios("output", "error writing");
536 if (clearflg
> 0 && ontty
== NOTTY
)
547 mhl_format (char *file
, int length
, int width
)
551 char *ap
, name
[NAMESZ
];
555 static dev_t dev
= 0;
556 static ino_t ino
= 0;
557 static time_t mtime
= 0;
560 if (stat (etcpath (file
), &st
) != NOTOK
561 && mtime
== st
.st_mtime
565 free_queue (&fmthd
, &fmttl
);
568 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
569 mhladios (file
, "unable to open format file");
571 if (fstat (fileno (fp
), &st
) != NOTOK
) {
577 global
.c_ovtxt
= global
.c_nfs
= NULL
;
581 if ((i
= sc_width ()) > 5)
583 global
.c_cwidth
= -1;
584 if ((i
= sc_length ()) > 5)
585 global
.c_length
= i
- 1;
586 global
.c_flags
= BELL
; /* BELL is default */
587 *(ip
= ignores
) = NULL
;
590 while (vfgets (fp
, &ap
) == OK
) {
595 trim_suffix_c(bp
, '\n');
598 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
603 strncpy (name
, parse(), sizeof(name
));
609 * Split this list of fields to ignore, and copy
610 * it to the end of the current "ignores" list.
612 if (!strcasecmp (name
, "ignores")) {
613 char **tmparray
, **p
;
616 /* split the fields */
617 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
619 /* count number of fields split */
624 /* copy pointers to split fields to ignores array */
625 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
631 if (evalvar (&global
))
632 mhladios (NULL
, "format file syntax error: %s", bp
);
639 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
640 while (*parptr
== ':' || *parptr
== ',') {
643 mhladios (NULL
, "format file syntax error: %s", bp
);
645 if (!c1
->c_nfs
&& global
.c_nfs
) {
646 if (c1
->c_flags
& DATEFMT
) {
647 if (global
.c_flags
& DATEFMT
) {
648 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
649 compile_formatfield(c1
);
651 } else if (c1
->c_flags
& ADDRFMT
) {
652 if (global
.c_flags
& ADDRFMT
) {
653 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
654 compile_formatfield(c1
);
661 mhladios (NULL
, "format file syntax error: %s", bp
);
667 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
670 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
671 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
672 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
673 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
674 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
675 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
676 c1
->c_cwidth
, c1
->c_length
);
677 fprintf (stderr
, "\tflags=%s\n",
678 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
684 global
.c_flags
|= CLEARSCR
;
687 global
.c_flags
&= ~CLEARSCR
;
690 switch (bellflg
) { /* command line may override format file */
692 global
.c_flags
|= BELL
;
695 global
.c_flags
&= ~BELL
;
700 global
.c_length
= length
;
702 global
.c_width
= width
;
703 if (global
.c_length
< 5)
704 global
.c_length
= 10000;
705 if (global
.c_width
< 5)
706 global
.c_width
= 10000;
711 evalvar (struct mcomp
*c1
)
713 char *cp
, name
[NAMESZ
];
718 strncpy (name
, parse(), sizeof(name
));
720 if (!strcasecmp (name
, "component")) {
721 if (ptos (name
, &c1
->c_text
))
723 c1
->c_flags
&= ~NOCOMPONENT
;
727 if (!strcasecmp (name
, "overflowtext"))
728 return ptos (name
, &c1
->c_ovtxt
);
730 if (!strcasecmp (name
, "formatfield")) {
731 if (ptos (name
, &cp
))
733 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
734 compile_formatfield(c1
);
735 c1
->c_flags
|= FORMAT
;
739 if (!strcasecmp (name
, "decode")) {
740 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
741 compile_formatfield(c1
);
742 c1
->c_flags
|= FORMAT
;
746 if (!strcasecmp (name
, "offset"))
747 return ptoi (name
, &c1
->c_offset
);
748 if (!strcasecmp (name
, "overflowoffset"))
749 return ptoi (name
, &c1
->c_ovoff
);
750 if (!strcasecmp (name
, "width"))
751 return ptoi (name
, &c1
->c_width
);
752 if (!strcasecmp (name
, "compwidth"))
753 return ptoi (name
, &c1
->c_cwidth
);
754 if (!strcasecmp (name
, "length"))
755 return ptoi (name
, &c1
->c_length
);
756 if (!strcasecmp (name
, "nodashstuffing"))
757 return dashstuff
= -1;
759 for (ap
= triples
; ap
->t_name
; ap
++)
760 if (!strcasecmp (ap
->t_name
, name
)) {
761 c1
->c_flags
|= ap
->t_on
;
762 c1
->c_flags
&= ~ap
->t_off
;
766 if (!strcasecmp (name
, "formatarg")) {
767 struct arglist
*args
;
769 if (ptos (name
, &cp
))
772 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
773 inform("format filters are currently only supported on "
774 "the \"body\" component");
781 arglist_tail
->a_next
= args
;
788 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
799 ptoi (char *name
, int *i
)
803 if (*parptr
++ != '=' || !*(cp
= parse ())) {
804 inform("missing argument to variable %s", name
);
814 ptos (char *name
, char **s
)
818 if (*parptr
++ != '=') {
819 inform("missing argument to variable %s", name
);
823 if (*parptr
!= '"') {
825 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
829 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
830 if (*parptr
== QUOTE
)
837 if ((*parptr
= c
) == '"')
848 static char result
[NAMESZ
];
850 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
868 * Process one file/message
872 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
874 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
880 /* volatile to prevent "might be clobbered" warning from gcc: */
881 char *volatile fname2
= fname
? fname
: "(stdin)";
886 switch (setjmp (env
)) {
889 fp
= fopen(fname
, "r");
891 advise (fname
, "unable to open");
898 if (fstat(fileno(fp
), &st
) == 0) {
899 filesize
= st
.st_size
;
903 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
905 SIGNAL (SIGINT
, intrser
);
906 mhlfile (fp
, cp
, ofilen
, ofilec
);
909 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
910 fmt_free(ap
->a_fmt
, 0);
920 SIGNAL (SIGINT
, SIG_IGN
);
921 if (fp
!= stdin
&& fp
!= NULL
)
924 holder
.c_text
= NULL
;
925 free_queue (&msghd
, &msgtl
);
926 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
927 c1
->c_flags
&= ~HDROUTPUT
;
935 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
938 struct mcomp
*c1
, *c2
, *c3
;
939 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
940 m_getfld_state_t gstate
;
942 compile_filterargs();
946 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
948 fputs("\n-------", stdout
);
950 printf (" Forwarded Message%s", PLURALS(ofilec
));
952 printf (" Message %d", ofilen
);
960 if ((global
.c_flags
& CLEARSCR
))
965 printf (">>> %s\n\n", mname
);
971 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
974 printf ("Press <return> to list \"%s\"...", mname
);
978 if (ofilec
== 1 || linefeed_typed()) {
979 if ((global
.c_flags
& CLEARSCR
))
993 printf (">>> %s\n\n", mname
);
999 gstate
= m_getfld_state_init(fp
);
1001 int bufsz
= sizeof buf
;
1002 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1005 bucket
= fmt_addcomptext(name
, buf
);
1006 for (ip
= ignores
; *ip
; ip
++)
1007 if (!strcasecmp (name
, *ip
)) {
1008 while (state
== FLDPLUS
) {
1010 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1011 fmt_appendcomp(bucket
, name
, buf
);
1018 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1019 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1022 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1023 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1024 if (!strcasecmp (FENDNULL(c1
->c_name
),
1025 FENDNULL(c3
->c_name
))) {
1027 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1031 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1032 while (state
== FLDPLUS
) {
1034 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1035 c1
->c_text
= add (buf
, c1
->c_text
);
1036 fmt_appendcomp(bucket
, name
, buf
);
1039 c1
->c_flags
|= EXTRA
;
1045 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1046 if (c1
->c_flags
& CLEARTEXT
) {
1047 putcomp (c1
, c1
, ONECOMP
);
1051 !strcasecmp (c1
->c_name
, "messagename")) {
1052 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1054 putcomp (c1
, &holder
, ONECOMP
);
1055 free (holder
.c_text
);
1056 holder
.c_text
= NULL
;
1059 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1060 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1061 if (c2
->c_flags
& EXTRA
)
1062 putcomp (c1
, c2
, TWOCOMP
);
1065 if (dobody
&& (!c1
->c_name
||
1066 !strcasecmp (c1
->c_name
, "body"))) {
1067 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1068 formatproc
!= NULL
) {
1069 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1071 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1072 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1073 while (state
== BODY
) {
1074 putcomp (c1
, &holder
, BODYCOMP
);
1076 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1079 free (holder
.c_text
);
1080 holder
.c_text
= NULL
;
1084 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1085 if (!strcasecmp (FENDNULL(c2
->c_name
),
1086 FENDNULL(c1
->c_name
))) {
1087 putcomp (c1
, c2
, ONECOMP
);
1088 if (!(c1
->c_flags
& SPLIT
))
1092 m_getfld_state_destroy (&gstate
);
1097 inform("format error in message %s", mname
);
1099 m_getfld_state_destroy (&gstate
);
1103 mhladios (NULL
, "getfld() returned %d", state
);
1110 mcomp_flags (char *name
)
1114 for (ap
= pairs
; ap
->p_name
; ap
++)
1115 if (!strcasecmp (ap
->p_name
, name
))
1123 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1127 if (!(flags
& ADDRFMT
))
1128 return add (s1
, s2
);
1130 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1133 return add (s1
, add (",\n", s2
));
1140 struct pqpair
*pq_next
;
1145 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1150 struct pqpair
*p
, *q
;
1152 struct mailname
*mp
;
1159 dat
[3] = BUFSIZ
- 1;
1162 if (!(c1
->c_flags
& ADDRFMT
)) {
1163 charstring_t scanl
= charstring_create (BUFSIZ
);
1166 c1
->c_c_text
->c_text
= ap
;
1167 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1171 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1172 /* Don't need to append a newline, dctime() already did */
1173 c2
->c_text
= charstring_buffer_copy (scanl
);
1174 charstring_free (scanl
);
1176 /* ap is now owned by the component struct, so do NOT free it here */
1180 (q
= &pq
)->pq_next
= NULL
;
1181 while ((cp
= getname (ap
))) {
1183 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1184 p
->pq_text
= mh_xstrdup(cp
);
1185 p
->pq_error
= mh_xstrdup(error
);
1187 p
->pq_text
= getcpy (mp
->m_text
);
1190 q
= (q
->pq_next
= p
);
1193 for (p
= pq
.pq_next
; p
; p
= q
) {
1194 charstring_t scanl
= charstring_create (BUFSIZ
);
1198 c1
->c_c_text
->c_text
= p
->pq_text
;
1201 if (c1
->c_c_error
) {
1202 c1
->c_c_error
->c_text
= p
->pq_error
;
1206 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1207 buffer
= charstring_buffer_copy (scanl
);
1210 c2
->c_text
= add (",\n", c2
->c_text
);
1211 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1213 c2
->c_text
= add (buffer
, c2
->c_text
);
1215 charstring_free (scanl
);
1223 c2
->c_text
= add ("\n", c2
->c_text
);
1228 static struct mcomp
*
1229 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1234 c1
->c_flags
= flags
& ~INIT
;
1235 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1236 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1237 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1240 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1241 c1
->c_offset
= global
.c_offset
;
1242 c1
->c_ovoff
= global
. c_ovoff
;
1243 c1
->c_width
= c1
->c_length
= 0;
1244 c1
->c_cwidth
= global
.c_cwidth
;
1245 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1250 (*tail
)->c_next
= c1
;
1258 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1260 struct mcomp
*c1
, *c2
;
1262 for (c1
= *head
; c1
; c1
= c2
) {
1269 fmt_free (c1
->c_fmt
, 0);
1273 *head
= *tail
= NULL
;
1278 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1280 char *text
; /* c1's text, or the name as a fallback. */
1281 char *trimmed_prefix
;
1285 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1287 if (! utf8
&& flag
!= BODYCOMP
) {
1288 /* Don't print 8-bit bytes in header field values if not in a
1289 UTF-8 locale, as required by RFC 6532. */
1290 c1
->c_flags
|= FORCE7BIT
;
1293 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1294 /* Create a copy with trailing whitespace trimmed, for use with
1296 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1300 llim
= c1
->c_length
? c1
->c_length
: -1;
1301 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1302 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1304 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1306 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1307 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1308 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1311 if (c1
->c_flags
& CLEARTEXT
) {
1312 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1314 putstr ("\n", c1
->c_flags
);
1318 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1319 mcomp_format (c1
, c2
);
1321 if (c1
->c_flags
& CENTER
) {
1322 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1323 - c1
->c_offset
- strlen (c2
->c_text
);
1324 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1325 count
-= strlen(text
) + 2;
1326 lm
= c1
->c_offset
+ (count
/ 2);
1332 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1333 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1335 putstr(text
, c1
->c_flags
);
1336 if (flag
!= BODYCOMP
) {
1337 putstr (": ", c1
->c_flags
);
1338 if (!(c1
->c_flags
& SPLIT
))
1339 c1
->c_flags
|= HDROUTPUT
;
1342 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1344 putstr (" ", c1
->c_flags
);
1347 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1351 && !(c2
->c_flags
& HDROUTPUT
)
1352 && !(c2
->c_flags
& NOCOMPONENT
)) {
1353 if (c1
->c_flags
& UPPERCASE
)
1354 to_upper(c2
->c_name
);
1355 putstr (c2
->c_name
, c1
->c_flags
);
1356 putstr (": ", c1
->c_flags
);
1357 if (!(c1
->c_flags
& SPLIT
))
1358 c2
->c_flags
|= HDROUTPUT
;
1361 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1363 putstr (" ", c1
->c_flags
);
1365 if (c1
->c_flags
& UPPERCASE
)
1366 to_upper(c2
->c_text
);
1370 if (flag
== TWOCOMP
)
1371 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1372 : (int) strlen (c2
->c_name
) + 2;
1374 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1377 count
+= c1
->c_offset
;
1379 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1380 /* Output line, trimming trailing whitespace if requested. */
1381 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1383 putstr ("\n", c1
->c_flags
);
1384 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1386 if (flag
== BODYCOMP
1387 && !(c1
->c_flags
& NOCOMPONENT
)) {
1388 /* Output component, trimming trailing whitespace if there
1389 is no text on the line. */
1391 putstr(text
, c1
->c_flags
);
1393 putstr (trimmed_prefix
, c1
->c_flags
);
1397 /* Output line, trimming trailing whitespace if requested. */
1398 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1401 putstr ("\n", c1
->c_flags
);
1403 if (flag
== BODYCOMP
&& term
== '\n')
1404 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1406 free (trimmed_prefix
);
1411 oneline (char *stuff
, unsigned long flags
)
1419 return onelp
= NULL
;
1423 if (flags
& COMPRESS
) {
1424 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1425 if (isspace ((unsigned char) *onelp
)) {
1426 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1444 while (*onelp
&& *onelp
!= '\n')
1446 if (*onelp
== '\n') {
1450 if (flags
& LEFTADJUST
)
1451 while (*ret
== ' ' || *ret
== '\t')
1454 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1462 putstr (char *string
, unsigned long flags
)
1464 /* To not count, for the purpose of counting columns, all of
1465 the bytes of a multibyte character. */
1468 if (!column
&& lm
> 0) {
1471 putch ('\t', flags
);
1481 #ifdef MULTIBYTE_SUPPORT
1482 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1485 NMH_UNUSED (char_len
);
1489 flags
&= ~INVISIBLE
;
1490 #ifdef MULTIBYTE_SUPPORT
1491 /* mbtowc should never return 0, because *string is non-NULL. */
1492 if (char_len
<= 0) {
1493 /* Find number of bytes in next character. */
1495 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1499 /* Multibyte character, after the first byte. */
1505 putch (*string
++, flags
);
1511 putch (char ch
, unsigned long flags
)
1522 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1524 if (global
.c_flags
& BELL
)
1527 if (linefeed_typed()) {
1528 if (global
.c_flags
& CLEARSCR
)
1529 nmh_clear_screen ();
1533 row
= global
.c_length
/ 3;
1552 * If we are forwarding this message, and the first
1553 * column contains a dash, then add a dash and a space.
1555 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1560 * Increment the character count, unless
1561 * 1) In UTF-8 locale, this is other than the last byte of
1562 a multibyte character, or
1563 * 2) In C locale, will print a non-printable character.
1565 if ((flags
& FORCE7BIT
) == 0) {
1567 if ((flags
& INVISIBLE
) == 0) {
1568 /* If multibyte character, its first byte only. */
1572 /* If not an ASCII character, the replace character will be
1573 displayed. Count it. */
1574 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1581 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1582 putch ('\n', flags
);
1585 putstr (FENDNULL(ovtxt
), flags
);
1590 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1597 /* linefeed_typed() makes a single read(2) from stdin and returns true
1598 * if a linefeed character is amongst the characters read.
1599 * A read error is treated as if linefeed wasn't typed.
1601 * Typing on a TTY can cause read() to return data without typing Enter
1602 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1603 * The linefeed can also be escaped with the TTY's LNEXT character,
1604 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1605 * It's not possible to distinguish between the user typing a buffer's
1606 * worth of characters and then EOT, or more than the buffer can hold.
1607 * Either way, the result depends on ASCII LF, either from typing Enter
1608 * or an escaped Ctrl-J, being amongst the read characters.
1611 linefeed_typed(void)
1616 n
= read(0, buf
, sizeof buf
);
1618 advise("stdin", "read");
1619 return false; /* Treat as EOF. */
1622 return memchr(buf
, '\n', n
);
1633 longjmp (env
, DONE
);
1658 mhladios (char *what
, char *fmt
, ...)
1663 advertise (what
, NULL
, fmt
, ap
);
1670 mhldone (int status
)
1678 * Compile a format string used by the formatfield option and save it
1681 * We will want the {text} (and possibly {error}) components for later,
1682 * so look for them and save them if we find them.
1686 compile_formatfield(struct mcomp
*c1
)
1688 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1691 * As a note to myself and any other poor bastard who is looking through
1692 * this code in the future ....
1694 * When the format hash table is reset later on (as it almost certainly
1695 * will be), there will still be references to these components in the
1696 * compiled format instructions. Thus these component references will
1697 * be free'd when the format instructions are free'd (by fmt_free()).
1699 * So, in other words ... don't go free'ing them yourself!
1702 c1
->c_c_text
= fmt_findcomp("text");
1703 c1
->c_c_error
= fmt_findcomp("error");
1707 * Compile all of the arguments for our format list.
1709 * Iterate through the linked list of format strings and compile them.
1710 * Note that we reset the format hash table before we start, but we do NOT
1711 * reset it between calls to fmt_compile().
1716 compile_filterargs (void)
1718 struct arglist
*arg
= arglist_head
;
1725 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1730 * Search through and mark any components that are address components
1733 for (ap
= addrcomps
; *ap
; ap
++) {
1734 cptr
= fmt_findcomp (*ap
);
1736 cptr
->c_type
|= CT_ADDR
;
1741 * Filter the body of a message through a specified format program
1745 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1746 m_getfld_state_t gstate
)
1748 struct mcomp holder
;
1750 int fdinput
[2], fdoutput
[2], waitstat
;
1752 pid_t writerpid
, filterpid
;
1755 * Create pipes so we can communicate with our filter process.
1758 if (pipe(fdinput
) < 0) {
1759 die("Unable to create input pipe");
1762 if (pipe(fdoutput
) < 0) {
1763 die("Unable to create output pipe");
1767 * Here's what we're doing to do.
1769 * - Fork ourselves and start writing data to the write side of the
1770 * input pipe (fdinput[1]).
1772 * - Fork and exec our filter program. We set the standard input of
1773 * our filter program to be the read side of our input pipe (fdinput[0]).
1774 * Standard output is set to the write side of our output pipe
1777 * - We read from the read side of the output pipe (fdoutput[0]).
1779 * We're forking because that's the simplest way to prevent any deadlocks.
1780 * (without doing something like switching to non-blocking I/O and using
1781 * select or poll, and I'm not interested in doing that).
1784 switch (writerpid
= fork()) {
1787 * Our child process - just write to the filter input (fdinput[1]).
1788 * Close all other descriptors that we don't need.
1796 * Call m_getfld2() until we're no longer in the BODY state
1799 while (state
== BODY
) {
1801 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1802 advise ("pipe output", "write");
1804 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1808 * We should be done; time to exit.
1813 * Make sure we call _exit(), otherwise we may flush out the stdio
1814 * buffers that we have duplicated from the parent.
1818 die("Unable to fork for filter writer process");
1823 * Fork and exec() our filter program, after redirecting standard in
1824 * and standard out appropriately.
1827 switch (filterpid
= fork()) {
1828 char **args
, *program
;
1830 int i
, dat
[5], s
, argp
;
1834 * Configure an argument array for us
1837 args
= argsplit(formatproc
, &program
, &argp
);
1838 args
[argp
+ filter_nargs
] = NULL
;
1846 * Pull out each argument and scan them.
1849 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1850 charstring_t scanl
= charstring_create (BUFSIZ
);
1852 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1853 args
[i
] = charstring_buffer_copy (scanl
);
1854 charstring_free (scanl
);
1856 * fmt_scan likes to put a trailing newline at the end of the
1857 * format string. If we have one, get rid of it.
1859 s
= strlen(args
[i
]);
1860 if (args
[i
][s
- 1] == '\n')
1861 args
[i
][s
- 1] = '\0';
1864 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1868 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1869 adios("formatproc", "Unable to dup2() standard input");
1871 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1872 adios("formatproc", "Unable to dup2() standard output");
1876 * Close everything (especially the old input and output
1877 * descriptors, since they've been dup'd to stdin and stdout),
1878 * and exec the formatproc.
1886 execvp(formatproc
, args
);
1888 adios(formatproc
, "Unable to execute filter");
1893 die("Unable to fork format program");
1897 * Close everything except our reader (fdoutput[0]);
1905 * As we read in this data, send it to putcomp
1908 holder
.c_text
= buf
;
1910 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1912 putcomp(c1
, &holder
, BODYCOMP
);
1916 die("reading from formatproc");
1920 * See if we got any errors along the way. I'm a little leery of calling
1921 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1924 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1925 if (errno
!= ECHILD
) {
1926 adios("filterproc", "Unable to determine status");
1929 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1930 pidstatus(waitstat
, stderr
, "filterproc");
1934 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1935 if (errno
!= ECHILD
) {
1936 adios("writer process", "Unable to determine status");
1940 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1941 pidstatus(waitstat
, stderr
, "writer process");