]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
02257598b218d1637542848380838dbac5898ac3
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/discard.h"
10 #include "sbr/trimcpy.h"
11 #include "sbr/vfgets.h"
12 #include "sbr/check_charset.h"
13 #include "sbr/getcpy.h"
14 #include "sbr/brkstring.h"
15 #include "sbr/ambigsw.h"
16 #include "sbr/pidstatus.h"
17 #include "sbr/print_version.h"
18 #include "sbr/print_help.h"
19 #include "sbr/arglist.h"
20 #include "sbr/error.h"
21 #include "h/signals.h"
22 #include "h/addrsbr.h"
23 #include "h/fmt_scan.h"
27 #include "sbr/m_popen.h"
29 #include <sys/types.h>
30 #include "sbr/terminal.h"
34 * for a component containing addresses, ADDRFMT, if COMPRESS is also
35 * set, then addresses get split wrong (not at the spaces between commas).
36 * To fix this correctly, putstr() should know about "atomic" strings that
37 * must NOT be broken across lines. That's too difficult for right now
38 * (it turns out that there are a number of degenerate cases), so in
39 * oneline(), instead of
41 * (*onelp == '\n' && !onelp[1])
43 * being a terminating condition,
45 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
47 * is used instead. This cuts the line prematurely, and gives us a much
48 * better chance of getting things right.
57 #define MHL_SWITCHES \
58 X("bell", 0, BELLSW) \
59 X("nobell", 0, NBELLSW) \
60 X("clear", 0, CLRSW) \
61 X("noclear", 0, NCLRSW) \
62 X("folder +folder", 0, FOLDSW) \
63 X("form formfile", 0, FORMSW) \
64 X("moreproc program", 0, PROGSW) \
65 X("nomoreproc", 0, NPROGSW) \
66 X("length lines", 0, LENSW) \
67 X("width columns", 0, WIDTHSW) \
68 X("sleep seconds", 0, SLEEPSW) \
69 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
70 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
71 X("version", 0, VERSIONSW) \
72 X("help", 0, HELPSW) \
73 X("forward", -7, FORW1SW) /* interface from forw */ \
74 X("forwall", -7, FORW2SW) /* interface from forw */ \
75 X("digest list", -6, DGSTSW) \
76 X("volume number", -6, VOLUMSW) \
77 X("issue number", -5, ISSUESW) \
78 X("nobody", -6, NBODYSW) \
79 X("fmtproc program", 0, FMTPROCSW) \
80 X("nofmtproc", 0, NFMTPROCSW) \
82 #define X(sw, minchars, id) id,
83 DEFINE_SWITCH_ENUM(MHL
);
86 #define X(sw, minchars, id) { sw, minchars, id },
87 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
90 #define NOCOMPONENT 0x000001 /* don't show component name */
91 #define UPPERCASE 0x000002 /* display in all upper case */
92 #define CENTER 0x000004 /* center line */
93 #define CLEARTEXT 0x000008 /* cleartext */
94 #define EXTRA 0x000010 /* an "extra" component */
95 #define HDROUTPUT 0x000020 /* already output */
96 #define CLEARSCR 0x000040 /* clear screen */
97 #define LEFTADJUST 0x000080 /* left justify multiple lines */
98 #define COMPRESS 0x000100 /* compress text */
99 #define ADDRFMT 0x000200 /* contains addresses */
100 #define BELL 0x000400 /* sound bell at EOP */
101 #define DATEFMT 0x000800 /* contains dates */
102 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
103 #define INIT 0x002000 /* initialize component */
104 #define RTRIM 0x004000 /* trim trailing whitespace */
105 #define SPLIT 0x010000 /* split headers (don't concatenate) */
106 #define NONEWLINE 0x020000 /* don't write trailing newline */
107 #define NOWRAP 0x040000 /* Don't wrap lines ever */
108 #define FMTFILTER 0x080000 /* Filter through format filter */
109 #define INVISIBLE 0x100000 /* count byte in display columns? */
110 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
111 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
112 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
115 * A format string to be used as a command-line argument to the body
120 struct format
*a_fmt
;
122 struct arglist
*a_next
;
126 * Linked list of command line arguments for the body format filter. This
127 * USED to be in "struct mcomp", but the format API got cleaned up and even
128 * though it reduced the code we had to do, it make things more complicated
129 * for us. Specifically:
131 * - The interface to the hash table has been cleaned up, which means the
132 * rooting around in the hash table is no longer necessary (yay!). But
133 * this ALSO means that we have to make sure that we call our format
134 * compilation routines before we process the message, because the
135 * components need to be visible in the hash table so we can save them for
136 * later. So we moved them out of "mcomp" and now compile them right before
137 * header processing starts.
138 * - We also use format strings to handle other components in the mhl
139 * configuration (using "formatfield" and "decode"), but here life
140 * gets complicated: they aren't dealt with in the normal way. Instead
141 * of referring to a component like {from}, each component is processed
142 * using the special {text} component. But these format strings need to be
143 * compiled BEFORE we compile the format arguments; in the previous
144 * implementation they were compiled and scanned as the headers were
145 * read, and that would reset the hash table that we need to populate
146 * the components used by the body format filter. So we are compiling
147 * the formatfield component strings ahead of time and then scanning them
150 * Okay, fine ... this was broken before. But you know what? Fixing this
151 * the right way will make things easier down the road.
153 * One side-effect to this change: format strings are now compiled only once
154 * for components specified with "formatfield", but they are compiled for
155 * every message for format arguments.
158 static struct arglist
*arglist_head
;
159 static struct arglist
*arglist_tail
;
160 static int filter_nargs
= 0;
163 * Flags/options for each component
167 char *c_name
; /* component name */
168 char *c_text
; /* component text */
169 char *c_ovtxt
; /* text overflow indicator */
170 char *c_nfs
; /* iff FORMAT */
171 struct format
*c_fmt
; /* .. */
172 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
173 struct comp
*c_c_error
; /* Ref to {error} */
174 int c_offset
; /* left margin indentation */
175 int c_ovoff
; /* overflow indentation */
176 int c_width
; /* width of field */
177 int c_cwidth
; /* width of component */
178 int c_length
; /* length in lines */
179 unsigned long c_flags
;
180 struct mcomp
*c_next
;
183 static struct mcomp
*msghd
= NULL
;
184 static struct mcomp
*msgtl
= NULL
;
185 static struct mcomp
*fmthd
= NULL
;
186 static struct mcomp
*fmttl
= NULL
;
188 static struct mcomp global
= {
189 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
192 static struct mcomp holder
= {
193 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
198 unsigned long p_flags
;
201 static struct pair pairs
[] = {
204 { "Sender", ADDRFMT
},
205 { "Reply-To", ADDRFMT
},
209 { "Resent-Date", DATEFMT
},
210 { "Resent-From", ADDRFMT
},
211 { "Resent-Sender", ADDRFMT
},
212 { "Resent-Reply-To", ADDRFMT
},
213 { "Resent-To", ADDRFMT
},
214 { "Resent-cc", ADDRFMT
},
215 { "Resent-Bcc", ADDRFMT
},
225 static struct triple triples
[] = {
226 { "nocomponent", NOCOMPONENT
, 0 },
227 { "uppercase", UPPERCASE
, 0 },
228 { "nouppercase", 0, UPPERCASE
},
229 { "center", CENTER
, 0 },
230 { "nocenter", 0, CENTER
},
231 { "clearscreen", CLEARSCR
, 0 },
232 { "noclearscreen", 0, CLEARSCR
},
233 { "noclear", 0, CLEARSCR
},
234 { "leftadjust", LEFTADJUST
, 0 },
235 { "noleftadjust", 0, LEFTADJUST
},
236 { "compress", COMPRESS
, 0 },
237 { "nocompress", 0, COMPRESS
},
238 { "split", SPLIT
, 0 },
239 { "nosplit", 0, SPLIT
},
240 { "rtrim", RTRIM
, 0 },
241 { "nortrim", 0, RTRIM
},
242 { "addrfield", ADDRFMT
, DATEFMT
},
244 { "nobell", 0, BELL
},
245 { "datefield", DATEFMT
, ADDRFMT
},
246 { "newline", 0, NONEWLINE
},
247 { "nonewline", NONEWLINE
, 0 },
248 { "wrap", 0, NOWRAP
},
249 { "nowrap", NOWRAP
, 0 },
250 { "format", FMTFILTER
, 0 },
251 { "noformat", 0, FMTFILTER
},
255 static char *addrcomps
[] = {
272 static int bellflg
= 0;
273 static int clearflg
= 0;
274 static int dashstuff
= 0;
275 static bool dobody
= true;
279 static int sleepsw
= NOTOK
;
281 static char *digest
= NULL
;
282 static int volume
= 0;
283 static int issue
= 0;
285 static int exitstat
= 0;
286 static bool mhldebug
;
288 static int filesize
= 0;
293 static int ontty
= NOTTY
;
296 static unsigned int column
;
302 static unsigned int wid
;
310 static int num_ignores
= 0;
311 static char *ignores
[MAXARGS
];
315 static char delim3
[] = /* from forw.c */
316 "\n----------------------------------------------------------------------\n\n";
317 static char delim4
[] = "\n------------------------------\n\n";
322 static void mhl_format (char *, int, int);
323 static int evalvar (struct mcomp
*);
324 static int ptoi (char *, int *);
325 static int ptos (char *, char **);
326 static char *parse (void);
327 static void process (char *, char *, int, int);
328 static void mhlfile (FILE *, char *, int, int);
329 static int mcomp_flags (char *) PURE
;
330 static char *mcomp_add (unsigned long, char *, char *);
331 static void mcomp_format (struct mcomp
*, struct mcomp
*);
332 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
333 static void free_queue (struct mcomp
**, struct mcomp
**);
334 static void putcomp (struct mcomp
*, struct mcomp
*, int);
335 static char *oneline (char *, unsigned long);
336 static void putstr (char *, unsigned long);
337 static void putch (char, unsigned long);
338 static bool linefeed_typed(void);
339 static void intrser (int);
340 static void pipeser (int);
341 static void quitser (int);
342 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
343 static void mhldone (int) NORETURN
;
344 static void filterbody (struct mcomp
*, char *, int, int,
346 static void compile_formatfield(struct mcomp
*);
347 static void compile_filterargs (void);
351 mhl (int argc
, char **argv
)
355 unsigned int i
, vecp
= 0;
357 char *cp
, *folder
= NULL
, *form
= NULL
;
358 char buf
[BUFSIZ
], *files
[MAXARGS
];
359 char **argp
, **arguments
;
361 /* Need this if called from main() of show(1). */
362 invo_name
= r1bindex (argv
[0], '/');
364 arguments
= getarguments (invo_name
, argc
, argv
, 1);
367 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
370 while ((cp
= *argp
++)) {
372 switch (smatch (++cp
, mhlswitches
)) {
374 ambigsw (cp
, mhlswitches
);
377 mhladios (NULL
, "-%s unknown\n", cp
);
380 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
381 print_help (buf
, mhlswitches
, 1);
384 print_version(invo_name
);
402 if (!(folder
= *argp
++) || *folder
== '-')
403 mhladios (NULL
, "missing argument to %s", argp
[-2]);
406 if (!(form
= *argp
++) || *form
== '-')
407 mhladios (NULL
, "missing argument to %s", argp
[-2]);
411 if (!(cp
= *argp
++) || *cp
== '-')
412 mhladios (NULL
, "missing argument to %s", argp
[-2]);
413 sleepsw
= atoi (cp
);/* ZERO ok! */
417 if (!(moreproc
= *argp
++) || *moreproc
== '-')
418 mhladios (NULL
, "missing argument to %s", argp
[-2]);
425 if (!(formatproc
= *argp
++) || *formatproc
== '-')
426 mhladios (NULL
, "missing argument to %s", argp
[-2]);
433 if (!(cp
= *argp
++) || *cp
== '-')
434 mhladios (NULL
, "missing argument to %s", argp
[-2]);
435 if ((length
= atoi (cp
)) < 1)
436 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
439 if (!(cp
= *argp
++) || *cp
== '-')
440 mhladios (NULL
, "missing argument to %s", argp
[-2]);
441 if ((width
= atoi (cp
)) < 1)
442 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
446 if (!(digest
= *argp
++) || *digest
== '-')
447 mhladios (NULL
, "missing argument to %s", argp
[-2]);
450 if (!(cp
= *argp
++) || *cp
== '-')
451 mhladios (NULL
, "missing argument to %s", argp
[-2]);
452 if ((issue
= atoi (cp
)) < 1)
453 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
456 if (!(cp
= *argp
++) || *cp
== '-')
457 mhladios (NULL
, "missing argument to %s", argp
[-2]);
458 if ((volume
= atoi (cp
)) < 1)
459 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
467 clearflg
= -1;/* XXX */
471 dashstuff
= 1; /* ternary logic */
474 dashstuff
= -1; /* ternary logic */
486 folder
= getenv ("mhfolder");
488 if (isatty (fileno (stdout
))) {
489 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
490 SIGNAL2 (SIGPIPE
, pipeser
);
491 m_popen(moreproc
, false);
494 SIGNAL (SIGINT
, SIG_IGN
);
495 SIGNAL2 (SIGQUIT
, quitser
);
502 mhl_format (form
? form
: mhlformat
, length
, width
);
505 process (folder
, NULL
, 1, vecp
= 1);
507 for (i
= 0; i
< vecp
; i
++)
508 process (folder
, files
[i
], i
+ 1, vecp
);
513 fputs(delim4
, stdout
);
515 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
517 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
518 digest
, volume
, issue
);
521 for (cp
= buf
+ i
; i
> 1; i
--)
528 printf ("\n------- End of Forwarded Message%s\n",
534 mhladios("output", "error writing");
537 if (clearflg
> 0 && ontty
== NOTTY
)
548 mhl_format (char *file
, int length
, int width
)
552 char *ap
, name
[NAMESZ
];
556 static dev_t dev
= 0;
557 static ino_t ino
= 0;
558 static time_t mtime
= 0;
561 if (stat (etcpath (file
), &st
) != NOTOK
562 && mtime
== st
.st_mtime
566 free_queue (&fmthd
, &fmttl
);
569 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
570 mhladios (file
, "unable to open format file");
572 if (fstat (fileno (fp
), &st
) != NOTOK
) {
578 global
.c_ovtxt
= global
.c_nfs
= NULL
;
582 if ((i
= sc_width ()) > 5)
584 global
.c_cwidth
= -1;
585 if ((i
= sc_length ()) > 5)
586 global
.c_length
= i
- 1;
587 global
.c_flags
= BELL
; /* BELL is default */
588 *(ip
= ignores
) = NULL
;
591 while (vfgets (fp
, &ap
) == OK
) {
596 trim_suffix_c(bp
, '\n');
599 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
604 strncpy (name
, parse(), sizeof(name
));
610 * Split this list of fields to ignore, and copy
611 * it to the end of the current "ignores" list.
613 if (!strcasecmp (name
, "ignores")) {
614 char **tmparray
, **p
;
617 /* split the fields */
618 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
620 /* count number of fields split */
625 /* copy pointers to split fields to ignores array */
626 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
632 if (evalvar (&global
))
633 mhladios (NULL
, "format file syntax error: %s", bp
);
640 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
641 while (*parptr
== ':' || *parptr
== ',') {
644 mhladios (NULL
, "format file syntax error: %s", bp
);
646 if (!c1
->c_nfs
&& global
.c_nfs
) {
647 if (c1
->c_flags
& DATEFMT
) {
648 if (global
.c_flags
& DATEFMT
) {
649 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
650 compile_formatfield(c1
);
652 } else if (c1
->c_flags
& ADDRFMT
) {
653 if (global
.c_flags
& ADDRFMT
) {
654 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
655 compile_formatfield(c1
);
662 mhladios (NULL
, "format file syntax error: %s", bp
);
668 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
671 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
672 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
673 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
674 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
675 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
676 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
677 c1
->c_cwidth
, c1
->c_length
);
678 fprintf (stderr
, "\tflags=%s\n",
679 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
685 global
.c_flags
|= CLEARSCR
;
688 global
.c_flags
&= ~CLEARSCR
;
691 switch (bellflg
) { /* command line may override format file */
693 global
.c_flags
|= BELL
;
696 global
.c_flags
&= ~BELL
;
701 global
.c_length
= length
;
703 global
.c_width
= width
;
704 if (global
.c_length
< 5)
705 global
.c_length
= 10000;
706 if (global
.c_width
< 5)
707 global
.c_width
= 10000;
712 evalvar (struct mcomp
*c1
)
714 char *cp
, name
[NAMESZ
];
719 strncpy (name
, parse(), sizeof(name
));
721 if (!strcasecmp (name
, "component")) {
722 if (ptos (name
, &c1
->c_text
))
724 c1
->c_flags
&= ~NOCOMPONENT
;
728 if (!strcasecmp (name
, "overflowtext"))
729 return ptos (name
, &c1
->c_ovtxt
);
731 if (!strcasecmp (name
, "formatfield")) {
732 if (ptos (name
, &cp
))
734 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
735 compile_formatfield(c1
);
736 c1
->c_flags
|= FORMAT
;
740 if (!strcasecmp (name
, "decode")) {
741 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
742 compile_formatfield(c1
);
743 c1
->c_flags
|= FORMAT
;
747 if (!strcasecmp (name
, "offset"))
748 return ptoi (name
, &c1
->c_offset
);
749 if (!strcasecmp (name
, "overflowoffset"))
750 return ptoi (name
, &c1
->c_ovoff
);
751 if (!strcasecmp (name
, "width"))
752 return ptoi (name
, &c1
->c_width
);
753 if (!strcasecmp (name
, "compwidth"))
754 return ptoi (name
, &c1
->c_cwidth
);
755 if (!strcasecmp (name
, "length"))
756 return ptoi (name
, &c1
->c_length
);
757 if (!strcasecmp (name
, "nodashstuffing"))
758 return dashstuff
= -1;
760 for (ap
= triples
; ap
->t_name
; ap
++)
761 if (!strcasecmp (ap
->t_name
, name
)) {
762 c1
->c_flags
|= ap
->t_on
;
763 c1
->c_flags
&= ~ap
->t_off
;
767 if (!strcasecmp (name
, "formatarg")) {
768 struct arglist
*args
;
770 if (ptos (name
, &cp
))
773 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
774 inform("format filters are currently only supported on "
775 "the \"body\" component");
782 arglist_tail
->a_next
= args
;
789 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
800 ptoi (char *name
, int *i
)
804 if (*parptr
++ != '=' || !*(cp
= parse ())) {
805 inform("missing argument to variable %s", name
);
815 ptos (char *name
, char **s
)
819 if (*parptr
++ != '=') {
820 inform("missing argument to variable %s", name
);
824 if (*parptr
!= '"') {
826 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
830 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
831 if (*parptr
== QUOTE
)
838 if ((*parptr
= c
) == '"')
849 static char result
[NAMESZ
];
851 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
869 * Process one file/message
873 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
875 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
881 /* volatile to prevent "might be clobbered" warning from gcc: */
882 char *volatile fname2
= fname
? fname
: "(stdin)";
887 switch (setjmp (env
)) {
890 fp
= fopen(fname
, "r");
892 advise (fname
, "unable to open");
899 if (fstat(fileno(fp
), &st
) == 0) {
900 filesize
= st
.st_size
;
904 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
906 SIGNAL (SIGINT
, intrser
);
907 mhlfile (fp
, cp
, ofilen
, ofilec
);
910 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
911 fmt_free(ap
->a_fmt
, 0);
921 SIGNAL (SIGINT
, SIG_IGN
);
922 if (fp
!= stdin
&& fp
!= NULL
)
925 holder
.c_text
= NULL
;
926 free_queue (&msghd
, &msgtl
);
927 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
928 c1
->c_flags
&= ~HDROUTPUT
;
936 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
939 struct mcomp
*c1
, *c2
, *c3
;
940 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
941 m_getfld_state_t gstate
;
943 compile_filterargs();
947 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
949 fputs("\n-------", stdout
);
951 printf (" Forwarded Message%s", PLURALS(ofilec
));
953 printf (" Message %d", ofilen
);
961 if ((global
.c_flags
& CLEARSCR
))
966 printf (">>> %s\n\n", mname
);
972 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
975 printf ("Press <return> to list \"%s\"...", mname
);
979 if (ofilec
== 1 || linefeed_typed()) {
980 if ((global
.c_flags
& CLEARSCR
))
994 printf (">>> %s\n\n", mname
);
1000 gstate
= m_getfld_state_init(fp
);
1002 int bufsz
= sizeof buf
;
1003 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1006 bucket
= fmt_addcomptext(name
, buf
);
1007 for (ip
= ignores
; *ip
; ip
++)
1008 if (!strcasecmp (name
, *ip
)) {
1009 while (state
== FLDPLUS
) {
1011 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1012 fmt_appendcomp(bucket
, name
, buf
);
1019 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1020 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1023 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1024 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1025 if (!strcasecmp (FENDNULL(c1
->c_name
),
1026 FENDNULL(c3
->c_name
))) {
1028 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1032 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1033 while (state
== FLDPLUS
) {
1035 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1036 c1
->c_text
= add (buf
, c1
->c_text
);
1037 fmt_appendcomp(bucket
, name
, buf
);
1040 c1
->c_flags
|= EXTRA
;
1046 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1047 if (c1
->c_flags
& CLEARTEXT
) {
1048 putcomp (c1
, c1
, ONECOMP
);
1052 !strcasecmp (c1
->c_name
, "messagename")) {
1053 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1055 putcomp (c1
, &holder
, ONECOMP
);
1056 free (holder
.c_text
);
1057 holder
.c_text
= NULL
;
1060 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1061 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1062 if (c2
->c_flags
& EXTRA
)
1063 putcomp (c1
, c2
, TWOCOMP
);
1066 if (dobody
&& (!c1
->c_name
||
1067 !strcasecmp (c1
->c_name
, "body"))) {
1068 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1069 formatproc
!= NULL
) {
1070 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1072 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1073 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1074 while (state
== BODY
) {
1075 putcomp (c1
, &holder
, BODYCOMP
);
1077 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1080 free (holder
.c_text
);
1081 holder
.c_text
= NULL
;
1085 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1086 if (!strcasecmp (FENDNULL(c2
->c_name
),
1087 FENDNULL(c1
->c_name
))) {
1088 putcomp (c1
, c2
, ONECOMP
);
1089 if (!(c1
->c_flags
& SPLIT
))
1093 m_getfld_state_destroy (&gstate
);
1098 inform("format error in message %s", mname
);
1100 m_getfld_state_destroy (&gstate
);
1104 mhladios (NULL
, "getfld() returned %d", state
);
1111 mcomp_flags (char *name
)
1115 for (ap
= pairs
; ap
->p_name
; ap
++)
1116 if (!strcasecmp (ap
->p_name
, name
))
1124 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1128 if (!(flags
& ADDRFMT
))
1129 return add (s1
, s2
);
1131 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1134 return add (s1
, add (",\n", s2
));
1141 struct pqpair
*pq_next
;
1146 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1151 struct pqpair
*p
, *q
;
1153 struct mailname
*mp
;
1160 dat
[3] = BUFSIZ
- 1;
1163 if (!(c1
->c_flags
& ADDRFMT
)) {
1164 charstring_t scanl
= charstring_create (BUFSIZ
);
1167 c1
->c_c_text
->c_text
= ap
;
1168 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1172 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1173 /* Don't need to append a newline, dctime() already did */
1174 c2
->c_text
= charstring_buffer_copy (scanl
);
1175 charstring_free (scanl
);
1177 /* ap is now owned by the component struct, so do NOT free it here */
1181 (q
= &pq
)->pq_next
= NULL
;
1182 while ((cp
= getname (ap
))) {
1184 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1185 p
->pq_text
= mh_xstrdup(cp
);
1186 p
->pq_error
= mh_xstrdup(error
);
1188 p
->pq_text
= getcpy (mp
->m_text
);
1191 q
= (q
->pq_next
= p
);
1194 for (p
= pq
.pq_next
; p
; p
= q
) {
1195 charstring_t scanl
= charstring_create (BUFSIZ
);
1199 c1
->c_c_text
->c_text
= p
->pq_text
;
1202 if (c1
->c_c_error
) {
1203 c1
->c_c_error
->c_text
= p
->pq_error
;
1207 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1208 buffer
= charstring_buffer_copy (scanl
);
1211 c2
->c_text
= add (",\n", c2
->c_text
);
1212 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1214 c2
->c_text
= add (buffer
, c2
->c_text
);
1216 charstring_free (scanl
);
1224 c2
->c_text
= add ("\n", c2
->c_text
);
1229 static struct mcomp
*
1230 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1235 c1
->c_flags
= flags
& ~INIT
;
1236 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1237 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1238 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1241 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1242 c1
->c_offset
= global
.c_offset
;
1243 c1
->c_ovoff
= global
. c_ovoff
;
1244 c1
->c_width
= c1
->c_length
= 0;
1245 c1
->c_cwidth
= global
.c_cwidth
;
1246 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1251 (*tail
)->c_next
= c1
;
1259 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1261 struct mcomp
*c1
, *c2
;
1263 for (c1
= *head
; c1
; c1
= c2
) {
1270 fmt_free (c1
->c_fmt
, 0);
1274 *head
= *tail
= NULL
;
1279 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1281 char *text
; /* c1's text, or the name as a fallback. */
1282 char *trimmed_prefix
;
1286 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1288 if (! utf8
&& flag
!= BODYCOMP
) {
1289 /* Don't print 8-bit bytes in header field values if not in a
1290 UTF-8 locale, as required by RFC 6532. */
1291 c1
->c_flags
|= FORCE7BIT
;
1294 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1295 /* Create a copy with trailing whitespace trimmed, for use with
1297 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1301 llim
= c1
->c_length
? c1
->c_length
: -1;
1302 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1303 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1305 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1307 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1308 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1309 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1312 if (c1
->c_flags
& CLEARTEXT
) {
1313 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1315 putstr ("\n", c1
->c_flags
);
1319 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1320 mcomp_format (c1
, c2
);
1322 if (c1
->c_flags
& CENTER
) {
1323 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1324 - c1
->c_offset
- strlen (c2
->c_text
);
1325 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1326 count
-= strlen(text
) + 2;
1327 lm
= c1
->c_offset
+ (count
/ 2);
1333 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1334 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1336 putstr(text
, c1
->c_flags
);
1337 if (flag
!= BODYCOMP
) {
1338 putstr (": ", c1
->c_flags
);
1339 if (!(c1
->c_flags
& SPLIT
))
1340 c1
->c_flags
|= HDROUTPUT
;
1343 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1345 putstr (" ", c1
->c_flags
);
1348 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1352 && !(c2
->c_flags
& HDROUTPUT
)
1353 && !(c2
->c_flags
& NOCOMPONENT
)) {
1354 if (c1
->c_flags
& UPPERCASE
)
1355 to_upper(c2
->c_name
);
1356 putstr (c2
->c_name
, c1
->c_flags
);
1357 putstr (": ", c1
->c_flags
);
1358 if (!(c1
->c_flags
& SPLIT
))
1359 c2
->c_flags
|= HDROUTPUT
;
1362 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1364 putstr (" ", c1
->c_flags
);
1366 if (c1
->c_flags
& UPPERCASE
)
1367 to_upper(c2
->c_text
);
1371 if (flag
== TWOCOMP
)
1372 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1373 : (int) strlen (c2
->c_name
) + 2;
1375 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1378 count
+= c1
->c_offset
;
1380 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1381 /* Output line, trimming trailing whitespace if requested. */
1382 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1384 putstr ("\n", c1
->c_flags
);
1385 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1387 if (flag
== BODYCOMP
1388 && !(c1
->c_flags
& NOCOMPONENT
)) {
1389 /* Output component, trimming trailing whitespace if there
1390 is no text on the line. */
1392 putstr(text
, c1
->c_flags
);
1394 putstr (trimmed_prefix
, c1
->c_flags
);
1398 /* Output line, trimming trailing whitespace if requested. */
1399 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1402 putstr ("\n", c1
->c_flags
);
1404 if (flag
== BODYCOMP
&& term
== '\n')
1405 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1407 free (trimmed_prefix
);
1412 oneline (char *stuff
, unsigned long flags
)
1420 return onelp
= NULL
;
1424 if (flags
& COMPRESS
) {
1425 for (spc
= true, cp
= ret
; *onelp
; onelp
++)
1426 if (isspace ((unsigned char) *onelp
)) {
1427 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1445 while (*onelp
&& *onelp
!= '\n')
1447 if (*onelp
== '\n') {
1451 if (flags
& LEFTADJUST
)
1452 while (*ret
== ' ' || *ret
== '\t')
1455 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1463 putstr (char *string
, unsigned long flags
)
1465 /* To not count, for the purpose of counting columns, all of
1466 the bytes of a multibyte character. */
1469 if (!column
&& lm
> 0) {
1472 putch ('\t', flags
);
1482 #ifdef MULTIBYTE_SUPPORT
1483 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1486 NMH_UNUSED (char_len
);
1490 flags
&= ~INVISIBLE
;
1491 #ifdef MULTIBYTE_SUPPORT
1492 /* mbtowc should never return 0, because *string is non-NULL. */
1493 if (char_len
<= 0) {
1494 /* Find number of bytes in next character. */
1496 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1500 /* Multibyte character, after the first byte. */
1506 putch (*string
++, flags
);
1512 putch (char ch
, unsigned long flags
)
1523 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1525 if (global
.c_flags
& BELL
)
1528 if (linefeed_typed()) {
1529 if (global
.c_flags
& CLEARSCR
)
1530 nmh_clear_screen ();
1534 row
= global
.c_length
/ 3;
1553 * If we are forwarding this message, and the first
1554 * column contains a dash, then add a dash and a space.
1556 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1561 * Increment the character count, unless
1562 * 1) In UTF-8 locale, this is other than the last byte of
1563 a multibyte character, or
1564 * 2) In C locale, will print a non-printable character.
1566 if ((flags
& FORCE7BIT
) == 0) {
1568 if ((flags
& INVISIBLE
) == 0) {
1569 /* If multibyte character, its first byte only. */
1573 /* If not an ASCII character, the replace character will be
1574 displayed. Count it. */
1575 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1582 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1583 putch ('\n', flags
);
1586 putstr (FENDNULL(ovtxt
), flags
);
1591 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1598 /* linefeed_typed() makes a single read(2) from stdin and returns true
1599 * if a linefeed character is amongst the characters read.
1600 * A read error is treated as if linefeed wasn't typed.
1602 * Typing on a TTY can cause read() to return data without typing Enter
1603 * by using the TTY's EOF character instead, normally ASCII EOT, Ctrl-D.
1604 * The linefeed can also be escaped with the TTY's LNEXT character,
1605 * normally ASCII SYN, Ctrl-V, by typing Ctrl-V Ctrl-J.
1606 * It's not possible to distinguish between the user typing a buffer's
1607 * worth of characters and then EOT, or more than the buffer can hold.
1608 * Either way, the result depends on ASCII LF, either from typing Enter
1609 * or an escaped Ctrl-J, being amongst the read characters.
1612 linefeed_typed(void)
1617 n
= read(0, buf
, sizeof buf
);
1619 advise("stdin", "read");
1620 return false; /* Treat as EOF. */
1623 return memchr(buf
, '\n', n
);
1634 longjmp (env
, DONE
);
1659 mhladios (char *what
, char *fmt
, ...)
1664 advertise (what
, NULL
, fmt
, ap
);
1671 mhldone (int status
)
1679 * Compile a format string used by the formatfield option and save it
1682 * We will want the {text} (and possibly {error}) components for later,
1683 * so look for them and save them if we find them.
1687 compile_formatfield(struct mcomp
*c1
)
1689 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1692 * As a note to myself and any other poor bastard who is looking through
1693 * this code in the future ....
1695 * When the format hash table is reset later on (as it almost certainly
1696 * will be), there will still be references to these components in the
1697 * compiled format instructions. Thus these component references will
1698 * be free'd when the format instructions are free'd (by fmt_free()).
1700 * So, in other words ... don't go free'ing them yourself!
1703 c1
->c_c_text
= fmt_findcomp("text");
1704 c1
->c_c_error
= fmt_findcomp("error");
1708 * Compile all of the arguments for our format list.
1710 * Iterate through the linked list of format strings and compile them.
1711 * Note that we reset the format hash table before we start, but we do NOT
1712 * reset it between calls to fmt_compile().
1717 compile_filterargs (void)
1719 struct arglist
*arg
= arglist_head
;
1726 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1731 * Search through and mark any components that are address components
1734 for (ap
= addrcomps
; *ap
; ap
++) {
1735 cptr
= fmt_findcomp (*ap
);
1737 cptr
->c_type
|= CT_ADDR
;
1742 * Filter the body of a message through a specified format program
1746 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1747 m_getfld_state_t gstate
)
1749 struct mcomp holder
;
1751 int fdinput
[2], fdoutput
[2], waitstat
;
1753 pid_t writerpid
, filterpid
;
1756 * Create pipes so we can communicate with our filter process.
1759 if (pipe(fdinput
) < 0) {
1760 die("Unable to create input pipe");
1763 if (pipe(fdoutput
) < 0) {
1764 die("Unable to create output pipe");
1768 * Here's what we're doing to do.
1770 * - Fork ourselves and start writing data to the write side of the
1771 * input pipe (fdinput[1]).
1773 * - Fork and exec our filter program. We set the standard input of
1774 * our filter program to be the read side of our input pipe (fdinput[0]).
1775 * Standard output is set to the write side of our output pipe
1778 * - We read from the read side of the output pipe (fdoutput[0]).
1780 * We're forking because that's the simplest way to prevent any deadlocks.
1781 * (without doing something like switching to non-blocking I/O and using
1782 * select or poll, and I'm not interested in doing that).
1785 switch (writerpid
= fork()) {
1788 * Our child process - just write to the filter input (fdinput[1]).
1789 * Close all other descriptors that we don't need.
1797 * Call m_getfld2() until we're no longer in the BODY state
1800 while (state
== BODY
) {
1802 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1803 advise ("pipe output", "write");
1805 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1809 * We should be done; time to exit.
1814 * Make sure we call _exit(), otherwise we may flush out the stdio
1815 * buffers that we have duplicated from the parent.
1819 die("Unable to fork for filter writer process");
1824 * Fork and exec() our filter program, after redirecting standard in
1825 * and standard out appropriately.
1828 switch (filterpid
= fork()) {
1829 char **args
, *program
;
1831 int i
, dat
[5], s
, argp
;
1835 * Configure an argument array for us
1838 args
= argsplit(formatproc
, &program
, &argp
);
1839 args
[argp
+ filter_nargs
] = NULL
;
1847 * Pull out each argument and scan them.
1850 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1851 charstring_t scanl
= charstring_create (BUFSIZ
);
1853 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1854 args
[i
] = charstring_buffer_copy (scanl
);
1855 charstring_free (scanl
);
1857 * fmt_scan likes to put a trailing newline at the end of the
1858 * format string. If we have one, get rid of it.
1860 s
= strlen(args
[i
]);
1861 if (args
[i
][s
- 1] == '\n')
1862 args
[i
][s
- 1] = '\0';
1865 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1869 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1870 adios("formatproc", "Unable to dup2() standard input");
1872 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1873 adios("formatproc", "Unable to dup2() standard output");
1877 * Close everything (especially the old input and output
1878 * descriptors, since they've been dup'd to stdin and stdout),
1879 * and exec the formatproc.
1887 execvp(formatproc
, args
);
1889 adios(formatproc
, "Unable to execute filter");
1894 die("Unable to fork format program");
1898 * Close everything except our reader (fdoutput[0]);
1906 * As we read in this data, send it to putcomp
1909 holder
.c_text
= buf
;
1911 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1913 putcomp(c1
, &holder
, BODYCOMP
);
1917 die("reading from formatproc");
1921 * See if we got any errors along the way. I'm a little leery of calling
1922 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1925 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1926 if (errno
!= ECHILD
) {
1927 adios("filterproc", "Unable to determine status");
1930 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1931 pidstatus(waitstat
, stderr
, "filterproc");
1935 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1936 if (errno
!= ECHILD
) {
1937 adios("writer process", "Unable to determine status");
1941 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1942 pidstatus(waitstat
, stderr
, "writer process");