]>
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>
14 #include "../sbr/m_popen.h"
16 #include <sys/types.h>
20 * for a component containing addresses, ADDRFMT, if COMPRESS is also
21 * set, then addresses get split wrong (not at the spaces between commas).
22 * To fix this correctly, putstr() should know about "atomic" strings that
23 * must NOT be broken across lines. That's too difficult for right now
24 * (it turns out that there are a number of degenerate cases), so in
25 * oneline(), instead of
27 * (*onelp == '\n' && !onelp[1])
29 * being a terminating condition,
31 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
33 * is used instead. This cuts the line prematurely, and gives us a much
34 * better chance of getting things right.
43 #define MHL_SWITCHES \
44 X("bell", 0, BELLSW) \
45 X("nobell", 0, NBELLSW) \
46 X("clear", 0, CLRSW) \
47 X("noclear", 0, NCLRSW) \
48 X("folder +folder", 0, FOLDSW) \
49 X("form formfile", 0, FORMSW) \
50 X("moreproc program", 0, PROGSW) \
51 X("nomoreproc", 0, NPROGSW) \
52 X("length lines", 0, LENSW) \
53 X("width columns", 0, WIDTHSW) \
54 X("sleep seconds", 0, SLEEPSW) \
55 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
56 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
57 X("version", 0, VERSIONSW) \
58 X("help", 0, HELPSW) \
59 X("forward", -7, FORW1SW) /* interface from forw */ \
60 X("forwall", -7, FORW2SW) /* interface from forw */ \
61 X("digest list", -6, DGSTSW) \
62 X("volume number", -6, VOLUMSW) \
63 X("issue number", -5, ISSUESW) \
64 X("nobody", -6, NBODYSW) \
65 X("fmtproc program", 0, FMTPROCSW) \
66 X("nofmtproc", 0, NFMTPROCSW) \
68 #define X(sw, minchars, id) id,
69 DEFINE_SWITCH_ENUM(MHL
);
72 #define X(sw, minchars, id) { sw, minchars, id },
73 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
76 #define NOCOMPONENT 0x000001 /* don't show component name */
77 #define UPPERCASE 0x000002 /* display in all upper case */
78 #define CENTER 0x000004 /* center line */
79 #define CLEARTEXT 0x000008 /* cleartext */
80 #define EXTRA 0x000010 /* an "extra" component */
81 #define HDROUTPUT 0x000020 /* already output */
82 #define CLEARSCR 0x000040 /* clear screen */
83 #define LEFTADJUST 0x000080 /* left justify multiple lines */
84 #define COMPRESS 0x000100 /* compress text */
85 #define ADDRFMT 0x000200 /* contains addresses */
86 #define BELL 0x000400 /* sound bell at EOP */
87 #define DATEFMT 0x000800 /* contains dates */
88 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
89 #define INIT 0x002000 /* initialize component */
90 #define RTRIM 0x004000 /* trim trailing whitespace */
91 #define SPLIT 0x010000 /* split headers (don't concatenate) */
92 #define NONEWLINE 0x020000 /* don't write trailing newline */
93 #define NOWRAP 0x040000 /* Don't wrap lines ever */
94 #define FMTFILTER 0x080000 /* Filter through format filter */
95 #define INVISIBLE 0x100000 /* count byte in display columns? */
96 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
97 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
98 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
101 * A format string to be used as a command-line argument to the body
106 struct format
*a_fmt
;
108 struct arglist
*a_next
;
112 * Linked list of command line arguments for the body format filter. This
113 * USED to be in "struct mcomp", but the format API got cleaned up and even
114 * though it reduced the code we had to do, it make things more complicated
115 * for us. Specifically:
117 * - The interface to the hash table has been cleaned up, which means the
118 * rooting around in the hash table is no longer necessary (yay!). But
119 * this ALSO means that we have to make sure that we call our format
120 * compilation routines before we process the message, because the
121 * components need to be visible in the hash table so we can save them for
122 * later. So we moved them out of "mcomp" and now compile them right before
123 * header processing starts.
124 * - We also use format strings to handle other components in the mhl
125 * configuration (using "formatfield" and "decode"), but here life
126 * gets complicated: they aren't dealt with in the normal way. Instead
127 * of referring to a component like {from}, each component is processed
128 * using the special {text} component. But these format strings need to be
129 * compiled BEFORE we compile the format arguments; in the previous
130 * implementation they were compiled and scanned as the headers were
131 * read, and that would reset the hash table that we need to populate
132 * the components used by the body format filter. So we are compiling
133 * the formatfield component strings ahead of time and then scanning them
136 * Okay, fine ... this was broken before. But you know what? Fixing this
137 * the right way will make things easier down the road.
139 * One side-effect to this change: format strings are now compiled only once
140 * for components specified with "formatfield", but they are compiled for
141 * every message for format arguments.
144 static struct arglist
*arglist_head
;
145 static struct arglist
*arglist_tail
;
146 static int filter_nargs
= 0;
149 * Flags/options for each component
153 char *c_name
; /* component name */
154 char *c_text
; /* component text */
155 char *c_ovtxt
; /* text overflow indicator */
156 char *c_nfs
; /* iff FORMAT */
157 struct format
*c_fmt
; /* .. */
158 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
159 struct comp
*c_c_error
; /* Ref to {error} */
160 int c_offset
; /* left margin indentation */
161 int c_ovoff
; /* overflow indentation */
162 int c_width
; /* width of field */
163 int c_cwidth
; /* width of component */
164 int c_length
; /* length in lines */
165 unsigned long c_flags
;
166 struct mcomp
*c_next
;
169 static struct mcomp
*msghd
= NULL
;
170 static struct mcomp
*msgtl
= NULL
;
171 static struct mcomp
*fmthd
= NULL
;
172 static struct mcomp
*fmttl
= NULL
;
174 static struct mcomp global
= {
175 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
178 static struct mcomp holder
= {
179 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
184 unsigned long p_flags
;
187 static struct pair pairs
[] = {
190 { "Sender", ADDRFMT
},
191 { "Reply-To", ADDRFMT
},
195 { "Resent-Date", DATEFMT
},
196 { "Resent-From", ADDRFMT
},
197 { "Resent-Sender", ADDRFMT
},
198 { "Resent-Reply-To", ADDRFMT
},
199 { "Resent-To", ADDRFMT
},
200 { "Resent-cc", ADDRFMT
},
201 { "Resent-Bcc", ADDRFMT
},
211 static struct triple triples
[] = {
212 { "nocomponent", NOCOMPONENT
, 0 },
213 { "uppercase", UPPERCASE
, 0 },
214 { "nouppercase", 0, UPPERCASE
},
215 { "center", CENTER
, 0 },
216 { "nocenter", 0, CENTER
},
217 { "clearscreen", CLEARSCR
, 0 },
218 { "noclearscreen", 0, CLEARSCR
},
219 { "noclear", 0, CLEARSCR
},
220 { "leftadjust", LEFTADJUST
, 0 },
221 { "noleftadjust", 0, LEFTADJUST
},
222 { "compress", COMPRESS
, 0 },
223 { "nocompress", 0, COMPRESS
},
224 { "split", SPLIT
, 0 },
225 { "nosplit", 0, SPLIT
},
226 { "rtrim", RTRIM
, 0 },
227 { "nortrim", 0, RTRIM
},
228 { "addrfield", ADDRFMT
, DATEFMT
},
230 { "nobell", 0, BELL
},
231 { "datefield", DATEFMT
, ADDRFMT
},
232 { "newline", 0, NONEWLINE
},
233 { "nonewline", NONEWLINE
, 0 },
234 { "wrap", 0, NOWRAP
},
235 { "nowrap", NOWRAP
, 0 },
236 { "format", FMTFILTER
, 0 },
237 { "noformat", 0, FMTFILTER
},
241 static char *addrcomps
[] = {
258 static int bellflg
= 0;
259 static int clearflg
= 0;
260 static int dashstuff
= 0;
261 static int dobody
= 1;
262 static int forwflg
= 0;
263 static int forwall
= 0;
265 static int sleepsw
= NOTOK
;
267 static char *digest
= NULL
;
268 static int volume
= 0;
269 static int issue
= 0;
271 static int exitstat
= 0;
272 static int mhldebug
= 0;
274 static int filesize
= 0;
279 static int ontty
= NOTTY
;
282 static unsigned int column
;
288 static unsigned int wid
;
296 static int num_ignores
= 0;
297 static char *ignores
[MAXARGS
];
300 static jmp_buf mhlenv
;
302 static char delim3
[] = /* from forw.c */
303 "\n----------------------------------------------------------------------\n\n";
304 static char delim4
[] = "\n------------------------------\n\n";
306 static FILE *(*mhl_action
)(char *);
311 static void mhl_format (char *, int, int);
312 static int evalvar (struct mcomp
*);
313 static int ptoi (char *, int *);
314 static int ptos (char *, char **);
315 static char *parse (void);
316 static void process (char *, char *, int, int);
317 static void mhlfile (FILE *, char *, int, int);
318 static int mcomp_flags (char *);
319 static char *mcomp_add (unsigned long, char *, char *);
320 static void mcomp_format (struct mcomp
*, struct mcomp
*);
321 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
322 static void free_queue (struct mcomp
**, struct mcomp
**);
323 static void putcomp (struct mcomp
*, struct mcomp
*, int);
324 static char *oneline (char *, unsigned long);
325 static void putstr (char *, unsigned long);
326 static void putch (char, unsigned long);
327 static void intrser (int);
328 static void pipeser (int);
329 static void quitser (int);
330 static void mhladios (char *, char *, ...);
331 static void mhldone (int);
332 static void filterbody (struct mcomp
*, char *, int, int, FILE *,
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
);
365 mhladios (NULL
, "-%s unknown\n", cp
);
369 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
370 print_help (buf
, mhlswitches
, 1);
374 print_version(invo_name
);
393 if (!(folder
= *argp
++) || *folder
== '-')
394 mhladios (NULL
, "missing argument to %s", argp
[-2]);
397 if (!(form
= *argp
++) || *form
== '-')
398 mhladios (NULL
, "missing argument to %s", argp
[-2]);
402 if (!(cp
= *argp
++) || *cp
== '-')
403 mhladios (NULL
, "missing argument to %s", argp
[-2]);
405 sleepsw
= atoi (cp
);/* ZERO ok! */
409 if (!(moreproc
= *argp
++) || *moreproc
== '-')
410 mhladios (NULL
, "missing argument to %s", argp
[-2]);
417 if (!(formatproc
= *argp
++) || *formatproc
== '-')
418 mhladios (NULL
, "missing argument to %s", argp
[-2]);
425 if (!(cp
= *argp
++) || *cp
== '-')
426 mhladios (NULL
, "missing argument to %s", argp
[-2]);
427 else if ((length
= atoi (cp
)) < 1)
428 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
431 if (!(cp
= *argp
++) || *cp
== '-')
432 mhladios (NULL
, "missing argument to %s", argp
[-2]);
433 else if ((width
= atoi (cp
)) < 1)
434 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
438 if (!(digest
= *argp
++) || *digest
== '-')
439 mhladios (NULL
, "missing argument to %s", argp
[-2]);
442 if (!(cp
= *argp
++) || *cp
== '-')
443 mhladios (NULL
, "missing argument to %s", argp
[-2]);
444 else if ((issue
= atoi (cp
)) < 1)
445 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
448 if (!(cp
= *argp
++) || *cp
== '-')
449 mhladios (NULL
, "missing argument to %s", argp
[-2]);
450 else if ((volume
= atoi (cp
)) < 1)
451 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
459 clearflg
= -1;/* XXX */
463 dashstuff
= 1; /* ternary logic */
466 dashstuff
= -1; /* ternary logic */
478 folder
= getenv ("mhfolder");
480 if (isatty (fileno (stdout
))) {
481 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
483 SIGNAL (SIGINT
, SIG_IGN
);
484 SIGNAL2 (SIGQUIT
, quitser
);
486 SIGNAL2 (SIGPIPE
, pipeser
);
487 m_popen (moreproc
, mhl_action
!= NULL
);
490 SIGNAL (SIGINT
, SIG_IGN
);
491 SIGNAL2 (SIGQUIT
, quitser
);
498 mhl_format (form
? form
: mhlformat
, length
, width
);
501 process (folder
, NULL
, 1, vecp
= 1);
503 for (i
= 0; i
< vecp
; i
++)
504 process (folder
, files
[i
], i
+ 1, vecp
);
509 fputs(delim4
, stdout
);
511 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
513 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
514 digest
, volume
, issue
);
517 for (cp
= buf
+ i
; i
> 1; i
--)
524 printf ("\n------- End of Forwarded Message%s\n",
530 mhladios("output", "error writing");
533 if (clearflg
> 0 && ontty
== NOTTY
)
544 mhl_format (char *file
, int length
, int width
)
548 char *ap
, name
[NAMESZ
];
552 static dev_t dev
= 0;
553 static ino_t ino
= 0;
554 static time_t mtime
= 0;
557 if (stat (etcpath (file
), &st
) != NOTOK
558 && mtime
== st
.st_mtime
562 free_queue (&fmthd
, &fmttl
);
565 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
566 mhladios (file
, "unable to open format file");
568 if (fstat (fileno (fp
), &st
) != NOTOK
) {
574 global
.c_ovtxt
= global
.c_nfs
= NULL
;
578 if ((i
= sc_width ()) > 5)
580 global
.c_cwidth
= -1;
581 if ((i
= sc_length ()) > 5)
582 global
.c_length
= i
- 1;
583 global
.c_flags
= BELL
; /* BELL is default */
584 *(ip
= ignores
) = NULL
;
587 while (vfgets (fp
, &ap
) == OK
) {
592 trim_suffix_c(bp
, '\n');
595 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
600 strncpy (name
, parse(), sizeof(name
));
606 * Split this list of fields to ignore, and copy
607 * it to the end of the current "ignores" list.
609 if (!strcasecmp (name
, "ignores")) {
610 char **tmparray
, **p
;
613 /* split the fields */
614 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
616 /* count number of fields split */
621 /* copy pointers to split fields to ignores array */
622 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
628 if (evalvar (&global
))
629 mhladios (NULL
, "format file syntax error: %s", bp
);
636 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
637 while (*parptr
== ':' || *parptr
== ',') {
640 mhladios (NULL
, "format file syntax error: %s", bp
);
642 if (!c1
->c_nfs
&& global
.c_nfs
) {
643 if (c1
->c_flags
& DATEFMT
) {
644 if (global
.c_flags
& DATEFMT
) {
645 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
646 compile_formatfield(c1
);
650 if (c1
->c_flags
& ADDRFMT
) {
651 if (global
.c_flags
& ADDRFMT
) {
652 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
653 compile_formatfield(c1
);
660 mhladios (NULL
, "format file syntax error: %s", bp
);
666 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
669 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
670 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
671 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
672 (unsigned int)(unsigned long) c1
->c_nfs
,
673 (unsigned int)(unsigned long) 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
= mhl_action
? (*mhl_action
) (fname
) : 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 (mhl_action
== NULL
&& fp
!= stdin
&& fp
!= NULL
)
923 mh_xfree(holder
.c_text
);
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
= 0;
942 compile_filterargs();
946 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
948 printf ("\n-------");
950 printf (" Forwarded Message%s", PLURALS(ofilec
));
952 printf (" Message %d", ofilen
);
960 if ((global
.c_flags
& CLEARSCR
))
965 printf (">>> %s\n\n", mname
);
970 strncpy (buf
, "\n", sizeof(buf
));
972 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
975 printf ("Press <return> to list \"%s\"...", mname
);
979 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
980 advise ("stdout", "read");
983 if (strchr(buf
, '\n')) {
984 if ((global
.c_flags
& CLEARSCR
))
998 printf (">>> %s\n\n", mname
);
1005 int bufsz
= sizeof buf
;
1006 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1009 bucket
= fmt_addcomptext(name
, buf
);
1010 for (ip
= ignores
; *ip
; ip
++)
1011 if (!strcasecmp (name
, *ip
)) {
1012 while (state
== FLDPLUS
) {
1014 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1015 fmt_appendcomp(bucket
, name
, buf
);
1022 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1023 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1026 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1027 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1028 if (!strcasecmp (FENDNULL(c1
->c_name
),
1029 FENDNULL(c3
->c_name
))) {
1031 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1035 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1036 while (state
== FLDPLUS
) {
1038 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1039 c1
->c_text
= add (buf
, c1
->c_text
);
1040 fmt_appendcomp(bucket
, name
, buf
);
1043 c1
->c_flags
|= EXTRA
;
1049 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1050 if (c1
->c_flags
& CLEARTEXT
) {
1051 putcomp (c1
, c1
, ONECOMP
);
1055 !strcasecmp (c1
->c_name
, "messagename")) {
1056 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1058 putcomp (c1
, &holder
, ONECOMP
);
1059 free (holder
.c_text
);
1060 holder
.c_text
= NULL
;
1063 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1064 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1065 if (c2
->c_flags
& EXTRA
)
1066 putcomp (c1
, c2
, TWOCOMP
);
1069 if (dobody
&& (!c1
->c_name
||
1070 !strcasecmp (c1
->c_name
, "body"))) {
1071 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1072 formatproc
!= NULL
) {
1073 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1075 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1076 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1077 while (state
== BODY
) {
1078 putcomp (c1
, &holder
, BODYCOMP
);
1080 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1083 free (holder
.c_text
);
1084 holder
.c_text
= NULL
;
1088 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1089 if (!strcasecmp (FENDNULL(c2
->c_name
),
1090 FENDNULL(c1
->c_name
))) {
1091 putcomp (c1
, c2
, ONECOMP
);
1092 if (!(c1
->c_flags
& SPLIT
))
1096 m_getfld_state_destroy (&gstate
);
1101 inform("format error in message %s", mname
);
1103 m_getfld_state_destroy (&gstate
);
1107 mhladios (NULL
, "getfld() returned %d", state
);
1114 mcomp_flags (char *name
)
1118 for (ap
= pairs
; ap
->p_name
; ap
++)
1119 if (!strcasecmp (ap
->p_name
, name
))
1120 return (ap
->p_flags
);
1127 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1131 if (!(flags
& ADDRFMT
))
1132 return add (s1
, s2
);
1134 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1137 return add (s1
, add (",\n", s2
));
1144 struct pqpair
*pq_next
;
1149 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1154 struct pqpair
*p
, *q
;
1156 struct mailname
*mp
;
1163 dat
[3] = BUFSIZ
- 1;
1166 if (!(c1
->c_flags
& ADDRFMT
)) {
1167 charstring_t scanl
= charstring_create (BUFSIZ
);
1170 c1
->c_c_text
->c_text
= ap
;
1171 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1175 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1176 /* Don't need to append a newline, dctime() already did */
1177 c2
->c_text
= charstring_buffer_copy (scanl
);
1178 charstring_free (scanl
);
1180 /* ap is now owned by the component struct, so do NOT free it here */
1184 (q
= &pq
)->pq_next
= NULL
;
1185 while ((cp
= getname (ap
))) {
1187 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1188 p
->pq_text
= mh_xstrdup(cp
);
1189 p
->pq_error
= mh_xstrdup(error
);
1191 p
->pq_text
= getcpy (mp
->m_text
);
1194 q
= (q
->pq_next
= p
);
1197 for (p
= pq
.pq_next
; p
; p
= q
) {
1198 charstring_t scanl
= charstring_create (BUFSIZ
);
1202 c1
->c_c_text
->c_text
= p
->pq_text
;
1205 if (c1
->c_c_error
) {
1206 c1
->c_c_error
->c_text
= p
->pq_error
;
1210 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1211 buffer
= charstring_buffer_copy (scanl
);
1214 c2
->c_text
= add (",\n", c2
->c_text
);
1215 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1217 c2
->c_text
= add (buffer
, c2
->c_text
);
1219 charstring_free (scanl
);
1221 mh_xfree(p
->pq_text
);
1222 mh_xfree(p
->pq_error
);
1227 c2
->c_text
= add ("\n", c2
->c_text
);
1232 static struct mcomp
*
1233 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1238 c1
->c_flags
= flags
& ~INIT
;
1239 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1240 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1241 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1244 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1245 c1
->c_offset
= global
.c_offset
;
1246 c1
->c_ovoff
= global
. c_ovoff
;
1247 c1
->c_width
= c1
->c_length
= 0;
1248 c1
->c_cwidth
= global
.c_cwidth
;
1249 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1254 (*tail
)->c_next
= c1
;
1262 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1264 struct mcomp
*c1
, *c2
;
1266 for (c1
= *head
; c1
; c1
= c2
) {
1268 mh_xfree(c1
->c_name
);
1269 mh_xfree(c1
->c_text
);
1270 mh_xfree(c1
->c_ovtxt
);
1271 mh_xfree(c1
->c_nfs
);
1273 fmt_free (c1
->c_fmt
, 0);
1277 *head
= *tail
= NULL
;
1282 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1284 char *text
; /* c1's text, or the name as a fallback. */
1285 char *trimmed_prefix
;
1288 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1290 if (! utf8
&& flag
!= BODYCOMP
) {
1291 /* Don't print 8-bit bytes in header field values if not in a
1292 UTF-8 locale, as required by RFC 6532. */
1293 c1
->c_flags
|= FORCE7BIT
;
1296 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1297 /* Create a copy with trailing whitespace trimmed, for use with
1299 trimmed_prefix
= rtrim(add(text
, NULL
));
1303 llim
= c1
->c_length
? c1
->c_length
: -1;
1304 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1305 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1307 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1309 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1310 mhladios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1311 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1314 if (c1
->c_flags
& CLEARTEXT
) {
1315 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1317 putstr ("\n", c1
->c_flags
);
1321 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1322 mcomp_format (c1
, c2
);
1324 if (c1
->c_flags
& CENTER
) {
1325 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1326 - c1
->c_offset
- strlen (c2
->c_text
);
1327 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1328 count
-= strlen(text
) + 2;
1329 lm
= c1
->c_offset
+ (count
/ 2);
1335 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1336 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1338 putstr(text
, c1
->c_flags
);
1339 if (flag
!= BODYCOMP
) {
1340 putstr (": ", c1
->c_flags
);
1341 if (!(c1
->c_flags
& SPLIT
))
1342 c1
->c_flags
|= HDROUTPUT
;
1345 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1347 putstr (" ", c1
->c_flags
);
1350 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1354 && !(c2
->c_flags
& HDROUTPUT
)
1355 && !(c2
->c_flags
& NOCOMPONENT
)) {
1356 if (c1
->c_flags
& UPPERCASE
)
1357 to_upper(c2
->c_name
);
1358 putstr (c2
->c_name
, c1
->c_flags
);
1359 putstr (": ", c1
->c_flags
);
1360 if (!(c1
->c_flags
& SPLIT
))
1361 c2
->c_flags
|= HDROUTPUT
;
1364 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1366 putstr (" ", c1
->c_flags
);
1368 if (c1
->c_flags
& UPPERCASE
)
1369 to_upper(c2
->c_text
);
1373 if (flag
== TWOCOMP
)
1374 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1375 : (int) strlen (c2
->c_name
) + 2;
1377 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1380 count
+= c1
->c_offset
;
1382 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1383 /* Output line, trimming trailing whitespace if requested. */
1384 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1386 putstr ("\n", c1
->c_flags
);
1387 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1389 if (flag
== BODYCOMP
1390 && !(c1
->c_flags
& NOCOMPONENT
)) {
1391 /* Output component, trimming trailing whitespace if there
1392 is no text on the line. */
1394 putstr(text
, c1
->c_flags
);
1396 putstr (trimmed_prefix
, c1
->c_flags
);
1400 /* Output line, trimming trailing whitespace if requested. */
1401 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1404 putstr ("\n", c1
->c_flags
);
1406 if (flag
== BODYCOMP
&& term
== '\n')
1407 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1409 free (trimmed_prefix
);
1414 oneline (char *stuff
, unsigned long flags
)
1422 return (onelp
= NULL
);
1426 if (flags
& COMPRESS
) {
1427 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1428 if (isspace ((unsigned char) *onelp
)) {
1429 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1447 while (*onelp
&& *onelp
!= '\n')
1449 if (*onelp
== '\n') {
1453 if (flags
& LEFTADJUST
)
1454 while (*ret
== ' ' || *ret
== '\t')
1457 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1465 putstr (char *string
, unsigned long flags
)
1467 /* To not count, for the purpose of counting columns, all of
1468 the bytes of a multibyte character. */
1471 if (!column
&& lm
> 0) {
1474 putch ('\t', flags
);
1484 #ifdef MULTIBYTE_SUPPORT
1485 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1488 NMH_UNUSED (char_len
);
1492 flags
&= ~INVISIBLE
;
1493 #ifdef MULTIBYTE_SUPPORT
1494 /* mbtowc should never return 0, because *string is non-NULL. */
1495 if (char_len
<= 0) {
1496 /* Find number of bytes in next character. */
1498 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1502 /* Multibyte character, after the first byte. */
1508 putch (*string
++, flags
);
1514 putch (char ch
, unsigned long flags
)
1527 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1529 if (global
.c_flags
& BELL
)
1533 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
1534 advise ("stdout", "read");
1536 if (strchr(buf
, '\n')) {
1537 if (global
.c_flags
& CLEARSCR
)
1538 nmh_clear_screen ();
1542 row
= global
.c_length
/ 3;
1561 * If we are forwarding this message, and the first
1562 * column contains a dash, then add a dash and a space.
1564 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1569 * Increment the character count, unless
1570 * 1) In UTF-8 locale, this is other than the last byte of
1571 a multibyte character, or
1572 * 2) In C locale, will print a non-printable character.
1574 if ((flags
& FORCE7BIT
) == 0) {
1576 if ((flags
& INVISIBLE
) == 0) {
1577 /* If multibyte character, its first byte only. */
1581 /* If not an ASCII character, the replace character will be
1582 displayed. Count it. */
1583 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1590 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1591 putch ('\n', flags
);
1594 putstr (FENDNULL(ovtxt
), flags
);
1599 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1614 longjmp (env
, DONE
);
1639 mhladios (char *what
, char *fmt
, ...)
1644 advertise (what
, NULL
, fmt
, ap
);
1651 mhldone (int status
)
1655 longjmp (mhlenv
, DONE
);
1662 * Compile a format string used by the formatfield option and save it
1665 * We will want the {text} (and possibly {error}) components for later,
1666 * so look for them and save them if we find them.
1670 compile_formatfield(struct mcomp
*c1
)
1672 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1675 * As a note to myself and any other poor bastard who is looking through
1676 * this code in the future ....
1678 * When the format hash table is reset later on (as it almost certainly
1679 * will be), there will still be references to these components in the
1680 * compiled format instructions. Thus these component references will
1681 * be free'd when the format instructions are free'd (by fmt_free()).
1683 * So, in other words ... don't go free'ing them yourself!
1686 c1
->c_c_text
= fmt_findcomp("text");
1687 c1
->c_c_error
= fmt_findcomp("error");
1691 * Compile all of the arguments for our format list.
1693 * Iterate through the linked list of format strings and compile them.
1694 * Note that we reset the format hash table before we start, but we do NOT
1695 * reset it between calls to fmt_compile().
1700 compile_filterargs (void)
1702 struct arglist
*arg
= arglist_head
;
1709 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1714 * Search through and mark any components that are address components
1717 for (ap
= addrcomps
; *ap
; ap
++) {
1718 cptr
= fmt_findcomp (*ap
);
1720 cptr
->c_type
|= CT_ADDR
;
1725 * Filter the body of a message through a specified format program
1729 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1730 m_getfld_state_t gstate
)
1732 struct mcomp holder
;
1734 int fdinput
[2], fdoutput
[2], waitstat
;
1736 pid_t writerpid
, filterpid
;
1739 * Create pipes so we can communicate with our filter process.
1742 if (pipe(fdinput
) < 0) {
1743 adios(NULL
, "Unable to create input pipe");
1746 if (pipe(fdoutput
) < 0) {
1747 adios(NULL
, "Unable to create output pipe");
1751 * Here's what we're doing to do.
1753 * - Fork ourselves and start writing data to the write side of the
1754 * input pipe (fdinput[1]).
1756 * - Fork and exec our filter program. We set the standard input of
1757 * our filter program to be the read side of our input pipe (fdinput[0]).
1758 * Standard output is set to the write side of our output pipe
1761 * - We read from the read side of the output pipe (fdoutput[0]).
1763 * We're forking because that's the simplest way to prevent any deadlocks.
1764 * (without doing something like switching to non-blocking I/O and using
1765 * select or poll, and I'm not interested in doing that).
1768 switch (writerpid
= fork()) {
1771 * Our child process - just write to the filter input (fdinput[1]).
1772 * Close all other descriptors that we don't need.
1780 * Call m_getfld() until we're no longer in the BODY state
1783 while (state
== BODY
) {
1785 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1786 advise ("pipe output", "write");
1788 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1792 * We should be done; time to exit.
1797 * Make sure we call _exit(), otherwise we may flush out the stdio
1798 * buffers that we have duplicated from the parent.
1802 adios(NULL
, "Unable to fork for filter writer process");
1807 * Fork and exec() our filter program, after redirecting standard in
1808 * and standard out appropriately.
1811 switch (filterpid
= fork()) {
1812 char **args
, *program
;
1814 int i
, dat
[5], s
, argp
;
1818 * Configure an argument array for us
1821 args
= argsplit(formatproc
, &program
, &argp
);
1822 args
[argp
+ filter_nargs
] = NULL
;
1830 * Pull out each argument and scan them.
1833 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1834 charstring_t scanl
= charstring_create (BUFSIZ
);
1836 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1837 args
[i
] = charstring_buffer_copy (scanl
);
1838 charstring_free (scanl
);
1840 * fmt_scan likes to put a trailing newline at the end of the
1841 * format string. If we have one, get rid of it.
1843 s
= strlen(args
[i
]);
1844 if (args
[i
][s
- 1] == '\n')
1845 args
[i
][s
- 1] = '\0';
1848 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1852 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1853 adios("formatproc", "Unable to dup2() standard input");
1855 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1856 adios("formatproc", "Unable to dup2() standard output");
1860 * Close everything (especially the old input and output
1861 * descriptors, since they've been dup'd to stdin and stdout),
1862 * and exec the formatproc.
1870 execvp(formatproc
, args
);
1872 adios(formatproc
, "Unable to execute filter");
1877 adios(NULL
, "Unable to fork format program");
1881 * Close everything except our reader (fdoutput[0]);
1889 * As we read in this data, send it to putcomp
1892 holder
.c_text
= buf
;
1894 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1896 putcomp(c1
, &holder
, BODYCOMP
);
1900 adios(NULL
, "reading from formatproc");
1904 * See if we got any errors along the way. I'm a little leery of calling
1905 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1908 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1909 if (errno
!= ECHILD
) {
1910 adios("filterproc", "Unable to determine status");
1913 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1914 pidstatus(waitstat
, stderr
, "filterproc");
1918 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1919 if (errno
!= ECHILD
) {
1920 adios("writer process", "Unable to determine status");
1924 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1925 pidstatus(waitstat
, stderr
, "writer process");