]>
diplodocus.org Git - nmh/blob - uip/mhlsbr.c
3 * mhlsbr.c -- main routines for nmh message lister
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
11 #include <h/signals.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
17 #include <sys/types.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 degernate 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
) () = (FILE *(*) ()) 0;
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 *);
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 *, ...);
332 static void mhldone (int);
333 static void filterbody (struct mcomp
*, char *, int, int, FILE *,
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]);
402 sleepsw
= atoi (cp
);/* ZERO ok! */
406 if (!(moreproc
= *argp
++) || *moreproc
== '-')
407 mhladios (NULL
, "missing argument to %s", argp
[-2]);
414 if (!(formatproc
= *argp
++) || *formatproc
== '-')
415 mhladios (NULL
, "missing argument to %s", argp
[-2]);
422 if (!(cp
= *argp
++) || *cp
== '-')
423 mhladios (NULL
, "missing argument to %s", argp
[-2]);
424 else if ((length
= atoi (cp
)) < 1)
425 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
428 if (!(cp
= *argp
++) || *cp
== '-')
429 mhladios (NULL
, "missing argument to %s", argp
[-2]);
430 else if ((width
= atoi (cp
)) < 1)
431 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
435 if (!(digest
= *argp
++) || *digest
== '-')
436 mhladios (NULL
, "missing argument to %s", argp
[-2]);
439 if (!(cp
= *argp
++) || *cp
== '-')
440 mhladios (NULL
, "missing argument to %s", argp
[-2]);
441 else if ((issue
= atoi (cp
)) < 1)
442 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
445 if (!(cp
= *argp
++) || *cp
== '-')
446 mhladios (NULL
, "missing argument to %s", argp
[-2]);
447 else if ((volume
= atoi (cp
)) < 1)
448 mhladios (NULL
, "bad argument %s %s", argp
[-2], cp
);
452 forwall
++; /* fall */
455 clearflg
= -1;/* XXX */
459 dashstuff
= 1; /* trinary logic */
462 dashstuff
= -1; /* trinary 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",
521 vecp
> 1 ? "s" : "");
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
);
646 if (c1
->c_flags
& ADDRFMT
) {
647 if (global
.c_flags
& ADDRFMT
) {
648 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
649 compile_formatfield(c1
);
656 mhladios (NULL
, "format file syntax error: %s", bp
);
662 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
665 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
666 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
667 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
668 (unsigned int)(unsigned long) c1
->c_nfs
,
669 (unsigned int)(unsigned long) c1
->c_fmt
);
670 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
671 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
672 c1
->c_cwidth
, c1
->c_length
);
673 fprintf (stderr
, "\tflags=%s\n",
674 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
680 global
.c_flags
|= CLEARSCR
;
683 global
.c_flags
&= ~CLEARSCR
;
686 switch (bellflg
) { /* command line may override format file */
688 global
.c_flags
|= BELL
;
691 global
.c_flags
&= ~BELL
;
696 global
.c_length
= length
;
698 global
.c_width
= width
;
699 if (global
.c_length
< 5)
700 global
.c_length
= 10000;
701 if (global
.c_width
< 5)
702 global
.c_width
= 10000;
707 evalvar (struct mcomp
*c1
)
709 char *cp
, name
[NAMESZ
];
714 strncpy (name
, parse(), sizeof(name
));
716 if (!strcasecmp (name
, "component")) {
717 if (ptos (name
, &c1
->c_text
))
719 c1
->c_flags
&= ~NOCOMPONENT
;
723 if (!strcasecmp (name
, "overflowtext"))
724 return ptos (name
, &c1
->c_ovtxt
);
726 if (!strcasecmp (name
, "formatfield")) {
727 if (ptos (name
, &cp
))
729 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
730 compile_formatfield(c1
);
731 c1
->c_flags
|= FORMAT
;
735 if (!strcasecmp (name
, "decode")) {
736 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
737 compile_formatfield(c1
);
738 c1
->c_flags
|= FORMAT
;
742 if (!strcasecmp (name
, "offset"))
743 return ptoi (name
, &c1
->c_offset
);
744 if (!strcasecmp (name
, "overflowoffset"))
745 return ptoi (name
, &c1
->c_ovoff
);
746 if (!strcasecmp (name
, "width"))
747 return ptoi (name
, &c1
->c_width
);
748 if (!strcasecmp (name
, "compwidth"))
749 return ptoi (name
, &c1
->c_cwidth
);
750 if (!strcasecmp (name
, "length"))
751 return ptoi (name
, &c1
->c_length
);
752 if (!strcasecmp (name
, "nodashstuffing"))
753 return (dashstuff
= -1);
755 for (ap
= triples
; ap
->t_name
; ap
++)
756 if (!strcasecmp (ap
->t_name
, name
)) {
757 c1
->c_flags
|= ap
->t_on
;
758 c1
->c_flags
&= ~ap
->t_off
;
762 if (!strcasecmp (name
, "formatarg")) {
763 struct arglist
*args
;
765 if (ptos (name
, &cp
))
768 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
769 advise (NULL
, "format filters are currently only supported on "
770 "the \"body\" component");
777 arglist_tail
->a_next
= args
;
784 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
795 ptoi (char *name
, int *i
)
799 if (*parptr
++ != '=' || !*(cp
= parse ())) {
800 advise (NULL
, "missing argument to variable %s", name
);
810 ptos (char *name
, char **s
)
814 if (*parptr
++ != '=') {
815 advise (NULL
, "missing argument to variable %s", name
);
819 if (*parptr
!= '"') {
821 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
825 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
826 if (*parptr
== QUOTE
)
833 if ((*parptr
= c
) == '"')
844 static char result
[NAMESZ
];
846 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
865 * Process one file/message
869 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
871 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
877 /* volatile to prevent "might be clobbered" warning from gcc: */
878 char *volatile fname2
= fname
? fname
: "(stdin)";
883 switch (setjmp (env
)) {
886 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
888 advise (fname
, "unable to open");
895 if (fstat(fileno(fp
), &st
) == 0) {
896 filesize
= st
.st_size
;
900 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
902 SIGNAL (SIGINT
, intrser
);
903 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
906 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
907 fmt_free(ap
->a_fmt
, 0);
916 SIGNAL (SIGINT
, SIG_IGN
);
917 if (mhl_action
== NULL
&& fp
!= stdin
&& fp
!= NULL
)
919 mh_xfree(holder
.c_text
);
920 holder
.c_text
= NULL
;
921 free_queue (&msghd
, &msgtl
);
922 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
923 c1
->c_flags
&= ~HDROUTPUT
;
931 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
934 struct mcomp
*c1
, *c2
, *c3
;
935 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
936 m_getfld_state_t gstate
= 0;
938 compile_filterargs();
942 fputs(ofilen
== 1 ? delim3
: delim4
, stdout
);
944 printf ("\n-------");
946 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
948 printf (" Message %d", ofilen
);
956 if ((global
.c_flags
& CLEARSCR
))
961 printf (">>> %s\n\n", mname
);
966 strncpy (buf
, "\n", sizeof(buf
));
968 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
971 printf ("Press <return> to list \"%s\"...", mname
);
975 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
976 advise ("stdout", "read");
979 if (strchr(buf
, '\n')) {
980 if ((global
.c_flags
& CLEARSCR
))
994 printf (">>> %s\n\n", mname
);
1001 int bufsz
= sizeof buf
;
1002 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1005 bucket
= fmt_addcomptext(name
, buf
);
1006 for (ip
= ignores
; *ip
; ip
++)
1007 if (!strcasecmp (name
, *ip
)) {
1008 while (state
== FLDPLUS
) {
1010 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1011 fmt_appendcomp(bucket
, name
, buf
);
1018 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1019 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "", name
))
1022 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1023 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1024 if (!strcasecmp (c1
->c_name
? c1
->c_name
: "",
1025 c3
->c_name
? c3
->c_name
: "")) {
1027 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1031 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1032 while (state
== FLDPLUS
) {
1034 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1035 c1
->c_text
= add (buf
, c1
->c_text
);
1036 fmt_appendcomp(bucket
, name
, buf
);
1039 c1
->c_flags
|= EXTRA
;
1045 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1046 if (c1
->c_flags
& CLEARTEXT
) {
1047 putcomp (c1
, c1
, ONECOMP
);
1051 !strcasecmp (c1
->c_name
, "messagename")) {
1052 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1054 putcomp (c1
, &holder
, ONECOMP
);
1055 free (holder
.c_text
);
1056 holder
.c_text
= NULL
;
1059 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1060 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1061 if (c2
->c_flags
& EXTRA
)
1062 putcomp (c1
, c2
, TWOCOMP
);
1065 if (dobody
&& (!c1
->c_name
||
1066 !strcasecmp (c1
->c_name
, "body"))) {
1067 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1068 formatproc
!= NULL
) {
1069 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1071 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1072 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1073 while (state
== BODY
) {
1074 putcomp (c1
, &holder
, BODYCOMP
);
1076 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1079 free (holder
.c_text
);
1080 holder
.c_text
= NULL
;
1084 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1085 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "",
1086 c1
->c_name
? c1
->c_name
: "")) {
1087 putcomp (c1
, c2
, ONECOMP
);
1088 if (!(c1
->c_flags
& SPLIT
))
1092 m_getfld_state_destroy (&gstate
);
1097 advise (NULL
, "format error in message %s", mname
);
1099 m_getfld_state_destroy (&gstate
);
1103 mhladios (NULL
, "getfld() returned %d", state
);
1110 mcomp_flags (char *name
)
1114 for (ap
= pairs
; ap
->p_name
; ap
++)
1115 if (!strcasecmp (ap
->p_name
, name
))
1116 return (ap
->p_flags
);
1123 mcomp_add (unsigned long flags
, char *s1
, char *s2
)
1127 if (!(flags
& ADDRFMT
))
1128 return add (s1
, s2
);
1130 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1133 return add (s1
, add (",\n", s2
));
1140 struct pqpair
*pq_next
;
1145 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1150 struct pqpair
*p
, *q
;
1152 struct mailname
*mp
;
1159 dat
[3] = BUFSIZ
- 1;
1162 if (!(c1
->c_flags
& ADDRFMT
)) {
1163 charstring_t scanl
= charstring_create (BUFSIZ
);
1166 c1
->c_c_text
->c_text
= ap
;
1167 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1171 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1172 /* Don't need to append a newline, dctime() already did */
1173 c2
->c_text
= charstring_buffer_copy (scanl
);
1174 charstring_free (scanl
);
1176 /* ap is now owned by the component struct, so do NOT free it here */
1180 (q
= &pq
)->pq_next
= NULL
;
1181 while ((cp
= getname (ap
))) {
1183 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1184 p
->pq_text
= mh_xstrdup(cp
);
1185 p
->pq_error
= mh_xstrdup(error
);
1187 p
->pq_text
= getcpy (mp
->m_text
);
1190 q
= (q
->pq_next
= p
);
1193 for (p
= pq
.pq_next
; p
; p
= q
) {
1194 charstring_t scanl
= charstring_create (BUFSIZ
);
1198 c1
->c_c_text
->c_text
= p
->pq_text
;
1201 if (c1
->c_c_error
) {
1202 c1
->c_c_error
->c_text
= p
->pq_error
;
1206 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1207 buffer
= charstring_buffer_copy (scanl
);
1210 c2
->c_text
= add (",\n", c2
->c_text
);
1211 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1213 c2
->c_text
= add (buffer
, c2
->c_text
);
1215 charstring_free (scanl
);
1217 mh_xfree(p
->pq_text
);
1218 mh_xfree(p
->pq_error
);
1223 c2
->c_text
= add ("\n", c2
->c_text
);
1228 static struct mcomp
*
1229 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1234 c1
->c_flags
= flags
& ~INIT
;
1235 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1236 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1237 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1240 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1241 c1
->c_offset
= global
.c_offset
;
1242 c1
->c_ovoff
= global
. c_ovoff
;
1243 c1
->c_width
= c1
->c_length
= 0;
1244 c1
->c_cwidth
= global
.c_cwidth
;
1245 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1250 (*tail
)->c_next
= c1
;
1258 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1260 struct mcomp
*c1
, *c2
;
1262 for (c1
= *head
; c1
; c1
= c2
) {
1264 mh_xfree(c1
->c_name
);
1265 mh_xfree(c1
->c_text
);
1266 mh_xfree(c1
->c_ovtxt
);
1267 mh_xfree(c1
->c_nfs
);
1269 fmt_free (c1
->c_fmt
, 0);
1273 *head
= *tail
= NULL
;
1278 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1280 char *text
; /* c1's text, or the name as a fallback. */
1281 char *trimmed_prefix
;
1284 const int utf8
= strcasecmp(get_charset(), "UTF-8") == 0;
1286 if (! utf8
&& flag
!= BODYCOMP
) {
1287 /* Don't print 8-bit bytes in header field values if not in a
1288 UTF-8 locale, as required by RFC 6532. */
1289 c1
->c_flags
|= FORCE7BIT
;
1292 text
= c1
->c_text
? c1
->c_text
: c1
->c_name
;
1293 /* Create a copy with trailing whitespace trimmed, for use with
1295 trimmed_prefix
= rtrim(add(text
, NULL
));
1299 llim
= c1
->c_length
? c1
->c_length
: -1;
1300 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1301 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1303 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1305 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1306 mhladios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1307 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1310 if (c1
->c_flags
& CLEARTEXT
) {
1311 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1313 putstr ("\n", c1
->c_flags
);
1317 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1318 mcomp_format (c1
, c2
);
1320 if (c1
->c_flags
& CENTER
) {
1321 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1322 - c1
->c_offset
- strlen (c2
->c_text
);
1323 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1324 count
-= strlen(text
) + 2;
1325 lm
= c1
->c_offset
+ (count
/ 2);
1331 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1332 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1334 putstr(text
, c1
->c_flags
);
1335 if (flag
!= BODYCOMP
) {
1336 putstr (": ", c1
->c_flags
);
1337 if (!(c1
->c_flags
& SPLIT
))
1338 c1
->c_flags
|= HDROUTPUT
;
1341 if ((count
= c1
->c_cwidth
- strlen(text
) - 2) > 0)
1343 putstr (" ", c1
->c_flags
);
1346 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1350 && !(c2
->c_flags
& HDROUTPUT
)
1351 && !(c2
->c_flags
& NOCOMPONENT
)) {
1352 if (c1
->c_flags
& UPPERCASE
)
1353 to_upper(c2
->c_name
);
1354 putstr (c2
->c_name
, c1
->c_flags
);
1355 putstr (": ", c1
->c_flags
);
1356 if (!(c1
->c_flags
& SPLIT
))
1357 c2
->c_flags
|= HDROUTPUT
;
1360 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1362 putstr (" ", c1
->c_flags
);
1364 if (c1
->c_flags
& UPPERCASE
)
1365 to_upper(c2
->c_text
);
1369 if (flag
== TWOCOMP
)
1370 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1371 : (int) strlen (c2
->c_name
) + 2;
1373 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1376 count
+= c1
->c_offset
;
1378 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1379 /* Output line, trimming trailing whitespace if requested. */
1380 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1382 putstr ("\n", c1
->c_flags
);
1383 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1385 if (flag
== BODYCOMP
1386 && !(c1
->c_flags
& NOCOMPONENT
)) {
1387 /* Output component, trimming trailing whitespace if there
1388 is no text on the line. */
1390 putstr(text
, c1
->c_flags
);
1392 putstr (trimmed_prefix
, c1
->c_flags
);
1396 /* Output line, trimming trailing whitespace if requested. */
1397 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1400 putstr ("\n", c1
->c_flags
);
1402 if (flag
== BODYCOMP
&& term
== '\n')
1403 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1405 free (trimmed_prefix
);
1410 oneline (char *stuff
, unsigned long flags
)
1418 return (onelp
= NULL
);
1422 if (flags
& COMPRESS
) {
1423 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1424 if (isspace ((unsigned char) *onelp
)) {
1425 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1444 while (*onelp
&& *onelp
!= '\n')
1446 if (*onelp
== '\n') {
1450 if (flags
& LEFTADJUST
)
1451 while (*ret
== ' ' || *ret
== '\t')
1454 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1462 putstr (char *string
, unsigned long flags
)
1464 /* To not count, for the purpose of counting columns, all of
1465 the bytes of a multibyte character. */
1468 if (!column
&& lm
> 0) {
1471 putch ('\t', flags
);
1481 #ifdef MULTIBYTE_SUPPORT
1482 if (mbtowc (NULL
, NULL
, 0)) {} /* reset shift state */
1485 NMH_UNUSED (char_len
);
1489 flags
&= ~INVISIBLE
;
1490 #ifdef MULTIBYTE_SUPPORT
1491 /* mbtowc should never return 0, because *string is non-NULL. */
1492 if (char_len
<= 0) {
1493 /* Find number of bytes in next character. */
1495 mbtowc (NULL
, string
, (size_t) MB_CUR_MAX
)) == -1) {
1499 /* Multibyte character, after the first byte. */
1505 putch (*string
++, flags
);
1511 putch (char ch
, unsigned long flags
)
1524 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1526 if (global
.c_flags
& BELL
)
1530 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
1531 advise ("stdout", "read");
1533 if (strchr(buf
, '\n')) {
1534 if (global
.c_flags
& CLEARSCR
)
1535 nmh_clear_screen ();
1539 row
= global
.c_length
/ 3;
1558 * If we are forwarding this message, and the first
1559 * column contains a dash, then add a dash and a space.
1561 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1566 * Increment the character count, unless
1567 * 1) In UTF-8 locale, this is other than the last byte of
1568 a multibyte character, or
1569 * 2) In C locale, will print a non-printable character.
1571 if ((flags
& FORCE7BIT
) == 0) {
1573 if ((flags
& INVISIBLE
) == 0) {
1574 /* If multibyte character, its first byte only. */
1578 /* If not an ASCII character, the replace character will be
1579 displayed. Count it. */
1580 if (! isascii((unsigned char) ch
) || isprint((unsigned char) ch
)) {
1587 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1588 putch ('\n', flags
);
1591 putstr (ovtxt
? ovtxt
: "", flags
);
1596 if (flags
& FORCE7BIT
&& ! isascii((unsigned char) ch
)) {
1611 longjmp (env
, DONE
);
1636 mhladios (char *what
, char *fmt
, ...)
1641 advertise (what
, NULL
, fmt
, ap
);
1648 mhldone (int status
)
1652 longjmp (mhlenv
, DONE
);
1659 * Compile a format string used by the formatfield option and save it
1662 * We will want the {text} (and possibly {error}) components for later,
1663 * so look for them and save them if we find them.
1667 compile_formatfield(struct mcomp
*c1
)
1669 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1672 * As a note to myself and any other poor bastard who is looking through
1673 * this code in the future ....
1675 * When the format hash table is reset later on (as it almost certainly
1676 * will be), there will still be references to these components in the
1677 * compiled format instructions. Thus these component references will
1678 * be free'd when the format instructions are free'd (by fmt_free()).
1680 * So, in other words ... don't go free'ing them yourself!
1683 c1
->c_c_text
= fmt_findcomp("text");
1684 c1
->c_c_error
= fmt_findcomp("error");
1688 * Compile all of the arguments for our format list.
1690 * Iterate through the linked list of format strings and compile them.
1691 * Note that we reset the format hash table before we start, but we do NOT
1692 * reset it between calls to fmt_compile().
1697 compile_filterargs (void)
1699 struct arglist
*arg
= arglist_head
;
1706 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1711 * Search through and mark any components that are address components
1714 for (ap
= addrcomps
; *ap
; ap
++) {
1715 cptr
= fmt_findcomp (*ap
);
1717 cptr
->c_type
|= CT_ADDR
;
1722 * Filter the body of a message through a specified format program
1726 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1727 m_getfld_state_t gstate
)
1729 struct mcomp holder
;
1731 int fdinput
[2], fdoutput
[2], waitstat
;
1733 pid_t writerpid
, filterpid
;
1736 * Create pipes so we can communicate with our filter process.
1739 if (pipe(fdinput
) < 0) {
1740 adios(NULL
, "Unable to create input pipe");
1743 if (pipe(fdoutput
) < 0) {
1744 adios(NULL
, "Unable to create output pipe");
1748 * Here's what we're doing to do.
1750 * - Fork ourselves and start writing data to the write side of the
1751 * input pipe (fdinput[1]).
1753 * - Fork and exec our filter program. We set the standard input of
1754 * our filter program to be the read side of our input pipe (fdinput[0]).
1755 * Standard output is set to the write side of our output pipe
1758 * - We read from the read side of the output pipe (fdoutput[0]).
1760 * We're forking because that's the simplest way to prevent any deadlocks.
1761 * (without doing something like switching to non-blocking I/O and using
1762 * select or poll, and I'm not interested in doing that).
1765 switch (writerpid
= fork()) {
1768 * Our child process - just write to the filter input (fdinput[1]).
1769 * Close all other descriptors that we don't need.
1777 * Call m_getfld() until we're no longer in the BODY state
1780 while (state
== BODY
) {
1782 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1783 advise ("pipe output", "write");
1785 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1789 * We should be done; time to exit.
1794 * Make sure we call _exit(), otherwise we may flush out the stdio
1795 * buffers that we have duplicated from the parent.
1799 adios(NULL
, "Unable to fork for filter writer process");
1804 * Fork and exec() our filter program, after redirecting standard in
1805 * and standard out appropriately.
1808 switch (filterpid
= fork()) {
1809 char **args
, *program
;
1811 int i
, dat
[5], s
, argp
;
1815 * Configure an argument array for us
1818 args
= argsplit(formatproc
, &program
, &argp
);
1819 args
[argp
+ filter_nargs
] = NULL
;
1827 * Pull out each argument and scan them.
1830 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1831 charstring_t scanl
= charstring_create (BUFSIZ
);
1833 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1834 args
[i
] = charstring_buffer_copy (scanl
);
1835 charstring_free (scanl
);
1837 * fmt_scan likes to put a trailing newline at the end of the
1838 * format string. If we have one, get rid of it.
1840 s
= strlen(args
[i
]);
1841 if (args
[i
][s
- 1] == '\n')
1842 args
[i
][s
- 1] = '\0';
1845 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1849 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1850 adios("formatproc", "Unable to dup2() standard input");
1852 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1853 adios("formatproc", "Unable to dup2() standard output");
1857 * Close everything (especially the old input and output
1858 * descriptors, since they've been dup'd to stdin and stdout),
1859 * and exec the formatproc.
1867 execvp(formatproc
, args
);
1869 adios(formatproc
, "Unable to execute filter");
1874 adios(NULL
, "Unable to fork format program");
1878 * Close everything except our reader (fdoutput[0]);
1886 * As we read in this data, send it to putcomp
1889 holder
.c_text
= buf
;
1891 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1893 putcomp(c1
, &holder
, BODYCOMP
);
1897 adios(NULL
, "reading from formatproc");
1901 * See if we got any errors along the way. I'm a little leery of calling
1902 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1905 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1906 if (errno
!= ECHILD
) {
1907 adios("filterproc", "Unable to determine status");
1910 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1911 pidstatus(waitstat
, stderr
, "filterproc");
1915 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1916 if (errno
!= ECHILD
) {
1917 adios("writer process", "Unable to determine status");
1921 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1922 pidstatus(waitstat
, stderr
, "writer process");