]>
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>
17 #include "sbr/terminal.h"
21 * for a component containing addresses, ADDRFMT, if COMPRESS is also
22 * set, then addresses get split wrong (not at the spaces between commas).
23 * To fix this correctly, putstr() should know about "atomic" strings that
24 * must NOT be broken across lines. That's too difficult for right now
25 * (it turns out that there are a number of degenerate cases), so in
26 * oneline(), instead of
28 * (*onelp == '\n' && !onelp[1])
30 * being a terminating condition,
32 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
34 * is used instead. This cuts the line prematurely, and gives us a much
35 * better chance of getting things right.
44 #define MHL_SWITCHES \
45 X("bell", 0, BELLSW) \
46 X("nobell", 0, NBELLSW) \
47 X("clear", 0, CLRSW) \
48 X("noclear", 0, NCLRSW) \
49 X("folder +folder", 0, FOLDSW) \
50 X("form formfile", 0, FORMSW) \
51 X("moreproc program", 0, PROGSW) \
52 X("nomoreproc", 0, NPROGSW) \
53 X("length lines", 0, LENSW) \
54 X("width columns", 0, WIDTHSW) \
55 X("sleep seconds", 0, SLEEPSW) \
56 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
57 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
58 X("version", 0, VERSIONSW) \
59 X("help", 0, HELPSW) \
60 X("forward", -7, FORW1SW) /* interface from forw */ \
61 X("forwall", -7, FORW2SW) /* interface from forw */ \
62 X("digest list", -6, DGSTSW) \
63 X("volume number", -6, VOLUMSW) \
64 X("issue number", -5, ISSUESW) \
65 X("nobody", -6, NBODYSW) \
66 X("fmtproc program", 0, FMTPROCSW) \
67 X("nofmtproc", 0, NFMTPROCSW) \
69 #define X(sw, minchars, id) id,
70 DEFINE_SWITCH_ENUM(MHL
);
73 #define X(sw, minchars, id) { sw, minchars, id },
74 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
77 #define NOCOMPONENT 0x000001 /* don't show component name */
78 #define UPPERCASE 0x000002 /* display in all upper case */
79 #define CENTER 0x000004 /* center line */
80 #define CLEARTEXT 0x000008 /* cleartext */
81 #define EXTRA 0x000010 /* an "extra" component */
82 #define HDROUTPUT 0x000020 /* already output */
83 #define CLEARSCR 0x000040 /* clear screen */
84 #define LEFTADJUST 0x000080 /* left justify multiple lines */
85 #define COMPRESS 0x000100 /* compress text */
86 #define ADDRFMT 0x000200 /* contains addresses */
87 #define BELL 0x000400 /* sound bell at EOP */
88 #define DATEFMT 0x000800 /* contains dates */
89 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
90 #define INIT 0x002000 /* initialize component */
91 #define RTRIM 0x004000 /* trim trailing whitespace */
92 #define SPLIT 0x010000 /* split headers (don't concatenate) */
93 #define NONEWLINE 0x020000 /* don't write trailing newline */
94 #define NOWRAP 0x040000 /* Don't wrap lines ever */
95 #define FMTFILTER 0x080000 /* Filter through format filter */
96 #define INVISIBLE 0x100000 /* count byte in display columns? */
97 #define FORCE7BIT 0x200000 /* don't display 8-bit bytes */
98 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER\025INVISIBLE\026FORCE7BIT"
99 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
102 * A format string to be used as a command-line argument to the body
107 struct format
*a_fmt
;
109 struct arglist
*a_next
;
113 * Linked list of command line arguments for the body format filter. This
114 * USED to be in "struct mcomp", but the format API got cleaned up and even
115 * though it reduced the code we had to do, it make things more complicated
116 * for us. Specifically:
118 * - The interface to the hash table has been cleaned up, which means the
119 * rooting around in the hash table is no longer necessary (yay!). But
120 * this ALSO means that we have to make sure that we call our format
121 * compilation routines before we process the message, because the
122 * components need to be visible in the hash table so we can save them for
123 * later. So we moved them out of "mcomp" and now compile them right before
124 * header processing starts.
125 * - We also use format strings to handle other components in the mhl
126 * configuration (using "formatfield" and "decode"), but here life
127 * gets complicated: they aren't dealt with in the normal way. Instead
128 * of referring to a component like {from}, each component is processed
129 * using the special {text} component. But these format strings need to be
130 * compiled BEFORE we compile the format arguments; in the previous
131 * implementation they were compiled and scanned as the headers were
132 * read, and that would reset the hash table that we need to populate
133 * the components used by the body format filter. So we are compiling
134 * the formatfield component strings ahead of time and then scanning them
137 * Okay, fine ... this was broken before. But you know what? Fixing this
138 * the right way will make things easier down the road.
140 * One side-effect to this change: format strings are now compiled only once
141 * for components specified with "formatfield", but they are compiled for
142 * every message for format arguments.
145 static struct arglist
*arglist_head
;
146 static struct arglist
*arglist_tail
;
147 static int filter_nargs
= 0;
150 * Flags/options for each component
154 char *c_name
; /* component name */
155 char *c_text
; /* component text */
156 char *c_ovtxt
; /* text overflow indicator */
157 char *c_nfs
; /* iff FORMAT */
158 struct format
*c_fmt
; /* .. */
159 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
160 struct comp
*c_c_error
; /* Ref to {error} */
161 int c_offset
; /* left margin indentation */
162 int c_ovoff
; /* overflow indentation */
163 int c_width
; /* width of field */
164 int c_cwidth
; /* width of component */
165 int c_length
; /* length in lines */
166 unsigned long c_flags
;
167 struct mcomp
*c_next
;
170 static struct mcomp
*msghd
= NULL
;
171 static struct mcomp
*msgtl
= NULL
;
172 static struct mcomp
*fmthd
= NULL
;
173 static struct mcomp
*fmttl
= NULL
;
175 static struct mcomp global
= {
176 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
179 static struct mcomp holder
= {
180 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
185 unsigned long p_flags
;
188 static struct pair pairs
[] = {
191 { "Sender", ADDRFMT
},
192 { "Reply-To", ADDRFMT
},
196 { "Resent-Date", DATEFMT
},
197 { "Resent-From", ADDRFMT
},
198 { "Resent-Sender", ADDRFMT
},
199 { "Resent-Reply-To", ADDRFMT
},
200 { "Resent-To", ADDRFMT
},
201 { "Resent-cc", ADDRFMT
},
202 { "Resent-Bcc", ADDRFMT
},
212 static struct triple triples
[] = {
213 { "nocomponent", NOCOMPONENT
, 0 },
214 { "uppercase", UPPERCASE
, 0 },
215 { "nouppercase", 0, UPPERCASE
},
216 { "center", CENTER
, 0 },
217 { "nocenter", 0, CENTER
},
218 { "clearscreen", CLEARSCR
, 0 },
219 { "noclearscreen", 0, CLEARSCR
},
220 { "noclear", 0, CLEARSCR
},
221 { "leftadjust", LEFTADJUST
, 0 },
222 { "noleftadjust", 0, LEFTADJUST
},
223 { "compress", COMPRESS
, 0 },
224 { "nocompress", 0, COMPRESS
},
225 { "split", SPLIT
, 0 },
226 { "nosplit", 0, SPLIT
},
227 { "rtrim", RTRIM
, 0 },
228 { "nortrim", 0, RTRIM
},
229 { "addrfield", ADDRFMT
, DATEFMT
},
231 { "nobell", 0, BELL
},
232 { "datefield", DATEFMT
, ADDRFMT
},
233 { "newline", 0, NONEWLINE
},
234 { "nonewline", NONEWLINE
, 0 },
235 { "wrap", 0, NOWRAP
},
236 { "nowrap", NOWRAP
, 0 },
237 { "format", FMTFILTER
, 0 },
238 { "noformat", 0, FMTFILTER
},
242 static char *addrcomps
[] = {
259 static int bellflg
= 0;
260 static int clearflg
= 0;
261 static int dashstuff
= 0;
262 static int dobody
= 1;
263 static int forwflg
= 0;
264 static int forwall
= 0;
266 static int sleepsw
= NOTOK
;
268 static char *digest
= NULL
;
269 static int volume
= 0;
270 static int issue
= 0;
272 static int exitstat
= 0;
273 static int mhldebug
= 0;
275 static int filesize
= 0;
280 static int ontty
= NOTTY
;
283 static unsigned int column
;
289 static unsigned int wid
;
297 static int num_ignores
= 0;
298 static char *ignores
[MAXARGS
];
301 static jmp_buf mhlenv
;
303 static char delim3
[] = /* from forw.c */
304 "\n----------------------------------------------------------------------\n\n";
305 static char delim4
[] = "\n------------------------------\n\n";
307 static FILE *(*mhl_action
)(char *);
312 static void mhl_format (char *, int, int);
313 static int evalvar (struct mcomp
*);
314 static int ptoi (char *, int *);
315 static int ptos (char *, char **);
316 static char *parse (void);
317 static void process (char *, char *, int, int);
318 static void mhlfile (FILE *, char *, int, int);
319 static int mcomp_flags (char *) PURE
;
320 static char *mcomp_add (unsigned long, char *, char *);
321 static void mcomp_format (struct mcomp
*, struct mcomp
*);
322 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
323 static void free_queue (struct mcomp
**, struct mcomp
**);
324 static void putcomp (struct mcomp
*, struct mcomp
*, int);
325 static char *oneline (char *, unsigned long);
326 static void putstr (char *, unsigned long);
327 static void putch (char, unsigned long);
328 static void intrser (int);
329 static void pipeser (int);
330 static void quitser (int);
331 static void mhladios (char *, char *, ...) CHECK_PRINTF(2, 3) NORETURN
;
332 static void mhldone (int) NORETURN
;
333 static void filterbody (struct mcomp
*, char *, int, int,
335 static void compile_formatfield(struct mcomp
*);
336 static void compile_filterargs (void);
340 mhl (int argc
, char **argv
)
342 int length
= 0, nomore
= 0;
343 unsigned int i
, vecp
= 0;
345 char *cp
, *folder
= NULL
, *form
= NULL
;
346 char buf
[BUFSIZ
], *files
[MAXARGS
];
347 char **argp
, **arguments
;
349 /* Need this if called from main() of show(1). */
350 invo_name
= r1bindex (argv
[0], '/');
352 arguments
= getarguments (invo_name
, argc
, argv
, 1);
355 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
358 while ((cp
= *argp
++)) {
360 switch (smatch (++cp
, mhlswitches
)) {
362 ambigsw (cp
, mhlswitches
);
365 mhladios (NULL
, "-%s unknown\n", cp
);
368 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
369 print_help (buf
, mhlswitches
, 1);
372 print_version(invo_name
);
390 if (!(folder
= *argp
++) || *folder
== '-')
391 mhladios (NULL
, "missing argument to %s", argp
[-2]);
394 if (!(form
= *argp
++) || *form
== '-')
395 mhladios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(cp
= *argp
++) || *cp
== '-')
400 mhladios (NULL
, "missing argument to %s", argp
[-2]);
401 sleepsw
= atoi (cp
);/* ZERO ok! */
405 if (!(moreproc
= *argp
++) || *moreproc
== '-')
406 mhladios (NULL
, "missing argument to %s", argp
[-2]);
413 if (!(formatproc
= *argp
++) || *formatproc
== '-')
414 mhladios (NULL
, "missing argument to %s", argp
[-2]);
421 if (!(cp
= *argp
++) || *cp
== '-')
422 mhladios (NULL
, "missing argument to %s", argp
[-2]);
423 if ((length
= atoi (cp
)) < 1)
424 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
427 if (!(cp
= *argp
++) || *cp
== '-')
428 mhladios (NULL
, "missing argument to %s", argp
[-2]);
429 if ((width
= atoi (cp
)) < 1)
430 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
434 if (!(digest
= *argp
++) || *digest
== '-')
435 mhladios (NULL
, "missing argument to %s", argp
[-2]);
438 if (!(cp
= *argp
++) || *cp
== '-')
439 mhladios (NULL
, "missing argument to %s", argp
[-2]);
440 if ((issue
= atoi (cp
)) < 1)
441 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
444 if (!(cp
= *argp
++) || *cp
== '-')
445 mhladios (NULL
, "missing argument to %s", argp
[-2]);
446 if ((volume
= atoi (cp
)) < 1)
447 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
455 clearflg
= -1;/* XXX */
459 dashstuff
= 1; /* ternary logic */
462 dashstuff
= -1; /* ternary logic */
474 folder
= getenv ("mhfolder");
476 if (isatty (fileno (stdout
))) {
477 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
479 SIGNAL (SIGINT
, SIG_IGN
);
480 SIGNAL2 (SIGQUIT
, quitser
);
482 SIGNAL2 (SIGPIPE
, pipeser
);
483 m_popen (moreproc
, mhl_action
!= NULL
);
486 SIGNAL (SIGINT
, SIG_IGN
);
487 SIGNAL2 (SIGQUIT
, quitser
);
494 mhl_format (form
? form
: mhlformat
, length
, width
);
497 process (folder
, NULL
, 1, vecp
= 1);
499 for (i
= 0; i
< vecp
; i
++)
500 process (folder
, files
[i
], i
+ 1, vecp
);
505 fputs(delim4
, stdout
);
507 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
509 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
510 digest
, volume
, issue
);
513 for (cp
= buf
+ i
; i
> 1; i
--)
520 printf ("\n------- End of Forwarded Message%s\n",
526 mhladios("output", "error writing");
529 if (clearflg
> 0 && ontty
== NOTTY
)
540 mhl_format (char *file
, int length
, int width
)
544 char *ap
, name
[NAMESZ
];
548 static dev_t dev
= 0;
549 static ino_t ino
= 0;
550 static time_t mtime
= 0;
553 if (stat (etcpath (file
), &st
) != NOTOK
554 && mtime
== st
.st_mtime
558 free_queue (&fmthd
, &fmttl
);
561 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
562 mhladios (file
, "unable to open format file");
564 if (fstat (fileno (fp
), &st
) != NOTOK
) {
570 global
.c_ovtxt
= global
.c_nfs
= NULL
;
574 if ((i
= sc_width ()) > 5)
576 global
.c_cwidth
= -1;
577 if ((i
= sc_length ()) > 5)
578 global
.c_length
= i
- 1;
579 global
.c_flags
= BELL
; /* BELL is default */
580 *(ip
= ignores
) = NULL
;
583 while (vfgets (fp
, &ap
) == OK
) {
588 trim_suffix_c(bp
, '\n');
591 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
596 strncpy (name
, parse(), sizeof(name
));
602 * Split this list of fields to ignore, and copy
603 * it to the end of the current "ignores" list.
605 if (!strcasecmp (name
, "ignores")) {
606 char **tmparray
, **p
;
609 /* split the fields */
610 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
612 /* count number of fields split */
617 /* copy pointers to split fields to ignores array */
618 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
624 if (evalvar (&global
))
625 mhladios (NULL
, "format file syntax error: %s", bp
);
632 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
633 while (*parptr
== ':' || *parptr
== ',') {
636 mhladios (NULL
, "format file syntax error: %s", bp
);
638 if (!c1
->c_nfs
&& global
.c_nfs
) {
639 if (c1
->c_flags
& DATEFMT
) {
640 if (global
.c_flags
& DATEFMT
) {
641 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
642 compile_formatfield(c1
);
644 } else if (c1
->c_flags
& ADDRFMT
) {
645 if (global
.c_flags
& ADDRFMT
) {
646 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
647 compile_formatfield(c1
);
654 mhladios (NULL
, "format file syntax error: %s", bp
);
660 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
663 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
664 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
665 fprintf(stderr
, "\tnfs=%p fmt=%p\n",
666 (void *)c1
->c_nfs
, (void *)c1
->c_fmt
);
667 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
668 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
669 c1
->c_cwidth
, c1
->c_length
);
670 fprintf (stderr
, "\tflags=%s\n",
671 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
677 global
.c_flags
|= CLEARSCR
;
680 global
.c_flags
&= ~CLEARSCR
;
683 switch (bellflg
) { /* command line may override format file */
685 global
.c_flags
|= BELL
;
688 global
.c_flags
&= ~BELL
;
693 global
.c_length
= length
;
695 global
.c_width
= width
;
696 if (global
.c_length
< 5)
697 global
.c_length
= 10000;
698 if (global
.c_width
< 5)
699 global
.c_width
= 10000;
704 evalvar (struct mcomp
*c1
)
706 char *cp
, name
[NAMESZ
];
711 strncpy (name
, parse(), sizeof(name
));
713 if (!strcasecmp (name
, "component")) {
714 if (ptos (name
, &c1
->c_text
))
716 c1
->c_flags
&= ~NOCOMPONENT
;
720 if (!strcasecmp (name
, "overflowtext"))
721 return ptos (name
, &c1
->c_ovtxt
);
723 if (!strcasecmp (name
, "formatfield")) {
724 if (ptos (name
, &cp
))
726 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
727 compile_formatfield(c1
);
728 c1
->c_flags
|= FORMAT
;
732 if (!strcasecmp (name
, "decode")) {
733 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
734 compile_formatfield(c1
);
735 c1
->c_flags
|= FORMAT
;
739 if (!strcasecmp (name
, "offset"))
740 return ptoi (name
, &c1
->c_offset
);
741 if (!strcasecmp (name
, "overflowoffset"))
742 return ptoi (name
, &c1
->c_ovoff
);
743 if (!strcasecmp (name
, "width"))
744 return ptoi (name
, &c1
->c_width
);
745 if (!strcasecmp (name
, "compwidth"))
746 return ptoi (name
, &c1
->c_cwidth
);
747 if (!strcasecmp (name
, "length"))
748 return ptoi (name
, &c1
->c_length
);
749 if (!strcasecmp (name
, "nodashstuffing"))
750 return (dashstuff
= -1);
752 for (ap
= triples
; ap
->t_name
; ap
++)
753 if (!strcasecmp (ap
->t_name
, name
)) {
754 c1
->c_flags
|= ap
->t_on
;
755 c1
->c_flags
&= ~ap
->t_off
;
759 if (!strcasecmp (name
, "formatarg")) {
760 struct arglist
*args
;
762 if (ptos (name
, &cp
))
765 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
766 inform("format filters are currently only supported on "
767 "the \"body\" component");
774 arglist_tail
->a_next
= args
;
781 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
792 ptoi (char *name
, int *i
)
796 if (*parptr
++ != '=' || !*(cp
= parse ())) {
797 inform("missing argument to variable %s", name
);
807 ptos (char *name
, char **s
)
811 if (*parptr
++ != '=') {
812 inform("missing argument to variable %s", name
);
816 if (*parptr
!= '"') {
818 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
822 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
823 if (*parptr
== QUOTE
)
830 if ((*parptr
= c
) == '"')
841 static char result
[NAMESZ
];
843 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
861 * Process one file/message
865 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
867 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
873 /* volatile to prevent "might be clobbered" warning from gcc: */
874 char *volatile fname2
= fname
? fname
: "(stdin)";
879 switch (setjmp (env
)) {
882 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
884 advise (fname
, "unable to open");
891 if (fstat(fileno(fp
), &st
) == 0) {
892 filesize
= st
.st_size
;
896 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
898 SIGNAL (SIGINT
, intrser
);
899 mhlfile (fp
, cp
, ofilen
, ofilec
);
902 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
903 fmt_free(ap
->a_fmt
, 0);
913 SIGNAL (SIGINT
, SIG_IGN
);
914 if (mhl_action
== NULL
&& fp
!= stdin
&& fp
!= NULL
)
917 holder
.c_text
= NULL
;
918 free_queue (&msghd
, &msgtl
);
919 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
920 c1
->c_flags
&= ~HDROUTPUT
;
928 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
931 struct mcomp
*c1
, *c2
, *c3
;
932 char **ip
, name
[NAMESZ
], buf
[NMH_BUFSIZ
];
933 m_getfld_state_t gstate
;
935 compile_filterargs();
939 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
941 printf ("\n-------");
943 printf (" Forwarded Message%s", PLURALS(ofilec
));
945 printf (" Message %d", ofilen
);
953 if ((global
.c_flags
& CLEARSCR
))
958 printf (">>> %s\n\n", mname
);
963 strncpy (buf
, "\n", sizeof(buf
));
965 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
968 printf ("Press <return> to list \"%s\"...", mname
);
972 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
973 advise ("stdout", "read");
976 if (strchr(buf
, '\n')) {
977 if ((global
.c_flags
& CLEARSCR
))
991 printf (">>> %s\n\n", mname
);
997 gstate
= m_getfld_state_init(fp
);
999 int bufsz
= sizeof buf
;
1000 switch (state
= m_getfld2(&gstate
, name
, buf
, &bufsz
)) {
1003 bucket
= fmt_addcomptext(name
, buf
);
1004 for (ip
= ignores
; *ip
; ip
++)
1005 if (!strcasecmp (name
, *ip
)) {
1006 while (state
== FLDPLUS
) {
1008 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1009 fmt_appendcomp(bucket
, name
, buf
);
1016 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1017 if (!strcasecmp (FENDNULL(c2
->c_name
), name
))
1020 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1021 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1022 if (!strcasecmp (FENDNULL(c1
->c_name
),
1023 FENDNULL(c3
->c_name
))) {
1025 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1029 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1030 while (state
== FLDPLUS
) {
1032 state
= m_getfld2(&gstate
, name
, buf
, &bufsz
);
1033 c1
->c_text
= add (buf
, c1
->c_text
);
1034 fmt_appendcomp(bucket
, name
, buf
);
1037 c1
->c_flags
|= EXTRA
;
1043 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1044 if (c1
->c_flags
& CLEARTEXT
) {
1045 putcomp (c1
, c1
, ONECOMP
);
1049 !strcasecmp (c1
->c_name
, "messagename")) {
1050 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1052 putcomp (c1
, &holder
, ONECOMP
);
1053 free (holder
.c_text
);
1054 holder
.c_text
= NULL
;
1057 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1058 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1059 if (c2
->c_flags
& EXTRA
)
1060 putcomp (c1
, c2
, TWOCOMP
);
1063 if (dobody
&& (!c1
->c_name
||
1064 !strcasecmp (c1
->c_name
, "body"))) {
1065 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1066 formatproc
!= NULL
) {
1067 filterbody(c1
, buf
, sizeof(buf
), state
, gstate
);
1069 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1070 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1071 while (state
== BODY
) {
1072 putcomp (c1
, &holder
, BODYCOMP
);
1074 state
= m_getfld2(&gstate
, name
, holder
.c_text
,
1077 free (holder
.c_text
);
1078 holder
.c_text
= NULL
;
1082 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1083 if (!strcasecmp (FENDNULL(c2
->c_name
),
1084 FENDNULL(c1
->c_name
))) {
1085 putcomp (c1
, c2
, ONECOMP
);
1086 if (!(c1
->c_flags
& SPLIT
))
1090 m_getfld_state_destroy (&gstate
);
1095 inform("format error in message %s", mname
);
1097 m_getfld_state_destroy (&gstate
);
1101 mhladios (NULL
, "getfld() returned %d", state
);
1108 mcomp_flags (char *name
)
1112 for (ap
= pairs
; ap
->p_name
; ap
++)
1113 if (!strcasecmp (ap
->p_name
, name
))
1114 return (ap
->p_flags
);
1121 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1125 if (!(flags
& ADDRFMT
))
1126 return add (s1
, s2
);
1128 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1131 return add (s1
, add (",\n", s2
));
1138 struct pqpair
*pq_next
;
1143 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1148 struct pqpair
*p
, *q
;
1150 struct mailname
*mp
;
1157 dat
[3] = BUFSIZ
- 1;
1160 if (!(c1
->c_flags
& ADDRFMT
)) {
1161 charstring_t scanl
= charstring_create (BUFSIZ
);
1164 c1
->c_c_text
->c_text
= ap
;
1165 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1169 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1170 /* Don't need to append a newline, dctime() already did */
1171 c2
->c_text
= charstring_buffer_copy (scanl
);
1172 charstring_free (scanl
);
1174 /* ap is now owned by the component struct, so do NOT free it here */
1178 (q
= &pq
)->pq_next
= NULL
;
1179 while ((cp
= getname (ap
))) {
1181 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1182 p
->pq_text
= mh_xstrdup(cp
);
1183 p
->pq_error
= mh_xstrdup(error
);
1185 p
->pq_text
= getcpy (mp
->m_text
);
1188 q
= (q
->pq_next
= p
);
1191 for (p
= pq
.pq_next
; p
; p
= q
) {
1192 charstring_t scanl
= charstring_create (BUFSIZ
);
1196 c1
->c_c_text
->c_text
= p
->pq_text
;
1199 if (c1
->c_c_error
) {
1200 c1
->c_c_error
->c_text
= p
->pq_error
;
1204 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1205 buffer
= charstring_buffer_copy (scanl
);
1208 c2
->c_text
= add (",\n", c2
->c_text
);
1209 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1211 c2
->c_text
= add (buffer
, c2
->c_text
);
1213 charstring_free (scanl
);
1221 c2
->c_text
= add ("\n", c2
->c_text
);
1226 static struct mcomp
*
1227 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1232 c1
->c_flags
= flags
& ~INIT
;
1233 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1234 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1235 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1238 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1239 c1
->c_offset
= global
.c_offset
;
1240 c1
->c_ovoff
= global
. c_ovoff
;
1241 c1
->c_width
= c1
->c_length
= 0;
1242 c1
->c_cwidth
= global
.c_cwidth
;
1243 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1248 (*tail
)->c_next
= c1
;
1256 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1258 struct mcomp
*c1
, *c2
;
1260 for (c1
= *head
; c1
; c1
= c2
) {
1267 fmt_free (c1
->c_fmt
, 0);
1271 *head
= *tail
= NULL
;
1276 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1278 char *text
; /* c1's text, or the name as a fallback. */
1279 char *trimmed_prefix
;
1282 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1284 if (! utf8
&& flag
!= BODYCOMP
) {
1285 /* Don't print 8-bit bytes in header field values if not in a
1286 UTF-8 locale, as required by RFC 6532. */
1287 c1
->c_flags
|= FORCE7BIT
;
1290 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1291 /* Create a copy with trailing whitespace trimmed, for use with
1293 trimmed_prefix
= rtrim(mh_xstrdup(FENDNULL(text
)));
1297 llim
= c1
->c_length
? c1
->c_length
: -1;
1298 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1299 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1301 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1303 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1304 mhladios(NULL
, "component: %s width(%d) too small for overflow(%zu)",
1305 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1308 if (c1
->c_flags
& CLEARTEXT
) {
1309 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1311 putstr ("\n", c1
->c_flags
);
1315 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1316 mcomp_format (c1
, c2
);
1318 if (c1
->c_flags
& CENTER
) {
1319 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1320 - c1
->c_offset
- strlen (c2
->c_text
);
1321 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1322 count
-= strlen(text
) + 2;
1323 lm
= c1
->c_offset
+ (count
/ 2);
1329 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1330 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1332 putstr(text
, c1
->c_flags
);
1333 if (flag
!= BODYCOMP
) {
1334 putstr (": ", c1
->c_flags
);
1335 if (!(c1
->c_flags
& SPLIT
))
1336 c1
->c_flags
|= HDROUTPUT
;
1339 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1341 putstr (" ", c1
->c_flags
);
1344 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1348 && !(c2
->c_flags
& HDROUTPUT
)
1349 && !(c2
->c_flags
& NOCOMPONENT
)) {
1350 if (c1
->c_flags
& UPPERCASE
)
1351 to_upper(c2
->c_name
);
1352 putstr (c2
->c_name
, c1
->c_flags
);
1353 putstr (": ", c1
->c_flags
);
1354 if (!(c1
->c_flags
& SPLIT
))
1355 c2
->c_flags
|= HDROUTPUT
;
1358 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1360 putstr (" ", c1
->c_flags
);
1362 if (c1
->c_flags
& UPPERCASE
)
1363 to_upper(c2
->c_text
);
1367 if (flag
== TWOCOMP
)
1368 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1369 : (int) strlen (c2
->c_name
) + 2;
1371 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1374 count
+= c1
->c_offset
;
1376 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1377 /* Output line, trimming trailing whitespace if requested. */
1378 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1380 putstr ("\n", c1
->c_flags
);
1381 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1383 if (flag
== BODYCOMP
1384 && !(c1
->c_flags
& NOCOMPONENT
)) {
1385 /* Output component, trimming trailing whitespace if there
1386 is no text on the line. */
1388 putstr(text
, c1
->c_flags
);
1390 putstr (trimmed_prefix
, c1
->c_flags
);
1394 /* Output line, trimming trailing whitespace if requested. */
1395 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1398 putstr ("\n", c1
->c_flags
);
1400 if (flag
== BODYCOMP
&& term
== '\n')
1401 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1403 free (trimmed_prefix
);
1408 oneline (char *stuff
, unsigned long flags
)
1416 return (onelp
= NULL
);
1420 if (flags
& COMPRESS
) {
1421 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1422 if (isspace ((unsigned char) *onelp
)) {
1423 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1441 while (*onelp
&& *onelp
!= '\n')
1443 if (*onelp
== '\n') {
1447 if (flags
& LEFTADJUST
)
1448 while (*ret
== ' ' || *ret
== '\t')
1451 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1459 putstr (char *string
, unsigned long flags
)
1461 /* To not count, for the purpose of counting columns, all of
1462 the bytes of a multibyte character. */
1465 if (!column
&& lm
> 0) {
1468 putch ('\t', flags
);
1478 #ifdef MULTIBYTE_SUPPORT
1479 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1482 NMH_UNUSED (char_len
);
1486 flags
&= ~INVISIBLE
;
1487 #ifdef MULTIBYTE_SUPPORT
1488 /* mbtowc should never return 0, because *string is non-NULL. */
1489 if (char_len
<= 0) {
1490 /* Find number of bytes in next character. */
1492 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1496 /* Multibyte character, after the first byte. */
1502 putch (*string
++, flags
);
1508 putch (char ch
, unsigned long flags
)
1521 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1523 if (global
.c_flags
& BELL
)
1527 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
1528 advise ("stdout", "read");
1530 if (strchr(buf
, '\n')) {
1531 if (global
.c_flags
& CLEARSCR
)
1532 nmh_clear_screen ();
1536 row
= global
.c_length
/ 3;
1555 * If we are forwarding this message, and the first
1556 * column contains a dash, then add a dash and a space.
1558 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1563 * Increment the character count, unless
1564 * 1) In UTF-8 locale, this is other than the last byte of
1565 a multibyte character, or
1566 * 2) In C locale, will print a non-printable character.
1568 if ((flags
& FORCE7BIT
) == 0) {
1570 if ((flags
& INVISIBLE
) == 0) {
1571 /* If multibyte character, its first byte only. */
1575 /* If not an ASCII character, the replace character will be
1576 displayed. Count it. */
1577 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1584 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1585 putch ('\n', flags
);
1588 putstr (FENDNULL(ovtxt
), flags
);
1593 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1608 longjmp (env
, DONE
);
1633 mhladios (char *what
, char *fmt
, ...)
1638 advertise (what
, NULL
, fmt
, ap
);
1645 mhldone (int status
)
1649 longjmp (mhlenv
, DONE
);
1655 * Compile a format string used by the formatfield option and save it
1658 * We will want the {text} (and possibly {error}) components for later,
1659 * so look for them and save them if we find them.
1663 compile_formatfield(struct mcomp
*c1
)
1665 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1668 * As a note to myself and any other poor bastard who is looking through
1669 * this code in the future ....
1671 * When the format hash table is reset later on (as it almost certainly
1672 * will be), there will still be references to these components in the
1673 * compiled format instructions. Thus these component references will
1674 * be free'd when the format instructions are free'd (by fmt_free()).
1676 * So, in other words ... don't go free'ing them yourself!
1679 c1
->c_c_text
= fmt_findcomp("text");
1680 c1
->c_c_error
= fmt_findcomp("error");
1684 * Compile all of the arguments for our format list.
1686 * Iterate through the linked list of format strings and compile them.
1687 * Note that we reset the format hash table before we start, but we do NOT
1688 * reset it between calls to fmt_compile().
1693 compile_filterargs (void)
1695 struct arglist
*arg
= arglist_head
;
1702 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1707 * Search through and mark any components that are address components
1710 for (ap
= addrcomps
; *ap
; ap
++) {
1711 cptr
= fmt_findcomp (*ap
);
1713 cptr
->c_type
|= CT_ADDR
;
1718 * Filter the body of a message through a specified format program
1722 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
,
1723 m_getfld_state_t gstate
)
1725 struct mcomp holder
;
1727 int fdinput
[2], fdoutput
[2], waitstat
;
1729 pid_t writerpid
, filterpid
;
1732 * Create pipes so we can communicate with our filter process.
1735 if (pipe(fdinput
) < 0) {
1736 adios(NULL
, "Unable to create input pipe");
1739 if (pipe(fdoutput
) < 0) {
1740 adios(NULL
, "Unable to create output pipe");
1744 * Here's what we're doing to do.
1746 * - Fork ourselves and start writing data to the write side of the
1747 * input pipe (fdinput[1]).
1749 * - Fork and exec our filter program. We set the standard input of
1750 * our filter program to be the read side of our input pipe (fdinput[0]).
1751 * Standard output is set to the write side of our output pipe
1754 * - We read from the read side of the output pipe (fdoutput[0]).
1756 * We're forking because that's the simplest way to prevent any deadlocks.
1757 * (without doing something like switching to non-blocking I/O and using
1758 * select or poll, and I'm not interested in doing that).
1761 switch (writerpid
= fork()) {
1764 * Our child process - just write to the filter input (fdinput[1]).
1765 * Close all other descriptors that we don't need.
1773 * Call m_getfld2() until we're no longer in the BODY state
1776 while (state
== BODY
) {
1778 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1779 advise ("pipe output", "write");
1781 state
= m_getfld2(&gstate
, name
, buf
, &bufsz2
);
1785 * We should be done; time to exit.
1790 * Make sure we call _exit(), otherwise we may flush out the stdio
1791 * buffers that we have duplicated from the parent.
1795 adios(NULL
, "Unable to fork for filter writer process");
1800 * Fork and exec() our filter program, after redirecting standard in
1801 * and standard out appropriately.
1804 switch (filterpid
= fork()) {
1805 char **args
, *program
;
1807 int i
, dat
[5], s
, argp
;
1811 * Configure an argument array for us
1814 args
= argsplit(formatproc
, &program
, &argp
);
1815 args
[argp
+ filter_nargs
] = NULL
;
1823 * Pull out each argument and scan them.
1826 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1827 charstring_t scanl
= charstring_create (BUFSIZ
);
1829 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1830 args
[i
] = charstring_buffer_copy (scanl
);
1831 charstring_free (scanl
);
1833 * fmt_scan likes to put a trailing newline at the end of the
1834 * format string. If we have one, get rid of it.
1836 s
= strlen(args
[i
]);
1837 if (args
[i
][s
- 1] == '\n')
1838 args
[i
][s
- 1] = '\0';
1841 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1845 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1846 adios("formatproc", "Unable to dup2() standard input");
1848 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1849 adios("formatproc", "Unable to dup2() standard output");
1853 * Close everything (especially the old input and output
1854 * descriptors, since they've been dup'd to stdin and stdout),
1855 * and exec the formatproc.
1863 execvp(formatproc
, args
);
1865 adios(formatproc
, "Unable to execute filter");
1870 adios(NULL
, "Unable to fork format program");
1874 * Close everything except our reader (fdoutput[0]);
1882 * As we read in this data, send it to putcomp
1885 holder
.c_text
= buf
;
1887 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1889 putcomp(c1
, &holder
, BODYCOMP
);
1893 adios(NULL
, "reading from formatproc");
1897 * See if we got any errors along the way. I'm a little leery of calling
1898 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1901 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1902 if (errno
!= ECHILD
) {
1903 adios("filterproc", "Unable to determine status");
1906 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1907 pidstatus(waitstat
, stderr
, "filterproc");
1911 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1912 if (errno
!= ECHILD
) {
1913 adios("writer process", "Unable to determine status");
1917 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1918 pidstatus(waitstat
, stderr
, "writer process");