]>
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 LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017RTRIM\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
97 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
100 * A format string to be used as a command-line argument to the body
105 struct format
*a_fmt
;
107 struct arglist
*a_next
;
111 * Linked list of command line arguments for the body format filter. This
112 * USED to be in "struct mcomp", but the format API got cleaned up and even
113 * though it reduced the code we had to do, it make things more complicated
114 * for us. Specifically:
116 * - The interface to the hash table has been cleaned up, which means the
117 * rooting around in the hash table is no longer necessary (yay!). But
118 * this ALSO means that we have to make sure that we call our format
119 * compilation routines before we process the message, because the
120 * components need to be visible in the hash table so we can save them for
121 * later. So we moved them out of "mcomp" and now compile them right before
122 * header processing starts.
123 * - We also use format strings to handle other components in the mhl
124 * configuration (using "formatfield" and "decode"), but here life
125 * gets complicated: they aren't dealt with in the normal way. Instead
126 * of referring to a component like {from}, each component is processed
127 * using the special {text} component. But these format strings need to be
128 * compiled BEFORE we compile the format arguments; in the previous
129 * implementation they were compiled and scanned as the headers were
130 * read, and that would reset the hash table that we need to populate
131 * the components used by the body format filter. So we are compiling
132 * the formatfield component strings ahead of time and then scanning them
135 * Okay, fine ... this was broken before. But you know what? Fixing this
136 * the right way will make things easier down the road.
138 * One side-effect to this change: format strings are now compiled only once
139 * for components specified with "formatfield", but they are compiled for
140 * every message for format arguments.
143 static struct arglist
*arglist_head
;
144 static struct arglist
*arglist_tail
;
145 static int filter_nargs
= 0;
148 * Flags/options for each component
152 char *c_name
; /* component name */
153 char *c_text
; /* component text */
154 char *c_ovtxt
; /* text overflow indicator */
155 char *c_nfs
; /* iff FORMAT */
156 struct format
*c_fmt
; /* .. */
157 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
158 struct comp
*c_c_error
; /* Ref to {error} */
159 int c_offset
; /* left margin indentation */
160 int c_ovoff
; /* overflow indentation */
161 int c_width
; /* width of field */
162 int c_cwidth
; /* width of component */
163 int c_length
; /* length in lines */
165 struct mcomp
*c_next
;
168 static struct mcomp
*msghd
= NULL
;
169 static struct mcomp
*msgtl
= NULL
;
170 static struct mcomp
*fmthd
= NULL
;
171 static struct mcomp
*fmttl
= NULL
;
173 static struct mcomp global
= {
174 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
177 static struct mcomp holder
= {
178 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
186 static struct pair pairs
[] = {
189 { "Sender", ADDRFMT
},
190 { "Reply-To", ADDRFMT
},
194 { "Resent-Date", DATEFMT
},
195 { "Resent-From", ADDRFMT
},
196 { "Resent-Sender", ADDRFMT
},
197 { "Resent-Reply-To", ADDRFMT
},
198 { "Resent-To", ADDRFMT
},
199 { "Resent-cc", ADDRFMT
},
200 { "Resent-Bcc", ADDRFMT
},
210 static struct triple triples
[] = {
211 { "nocomponent", NOCOMPONENT
, 0 },
212 { "uppercase", UPPERCASE
, 0 },
213 { "nouppercase", 0, UPPERCASE
},
214 { "center", CENTER
, 0 },
215 { "nocenter", 0, CENTER
},
216 { "clearscreen", CLEARSCR
, 0 },
217 { "noclearscreen", 0, CLEARSCR
},
218 { "noclear", 0, CLEARSCR
},
219 { "leftadjust", LEFTADJUST
, 0 },
220 { "noleftadjust", 0, LEFTADJUST
},
221 { "compress", COMPRESS
, 0 },
222 { "nocompress", 0, COMPRESS
},
223 { "split", SPLIT
, 0 },
224 { "nosplit", 0, SPLIT
},
225 { "rtrim", RTRIM
, 0 },
226 { "nortrim", 0, RTRIM
},
227 { "addrfield", ADDRFMT
, DATEFMT
},
229 { "nobell", 0, BELL
},
230 { "datefield", DATEFMT
, ADDRFMT
},
231 { "newline", 0, NONEWLINE
},
232 { "nonewline", NONEWLINE
, 0 },
233 { "wrap", 0, NOWRAP
},
234 { "nowrap", NOWRAP
, 0 },
235 { "format", FMTFILTER
, 0 },
236 { "noformat", 0, FMTFILTER
},
240 static char *addrcomps
[] = {
257 static int bellflg
= 0;
258 static int clearflg
= 0;
259 static int dashstuff
= 0;
260 static int dobody
= 1;
261 static int forwflg
= 0;
262 static int forwall
= 0;
264 static int sleepsw
= NOTOK
;
266 static char *digest
= NULL
;
267 static int volume
= 0;
268 static int issue
= 0;
270 static int exitstat
= 0;
271 static int mhldebug
= 0;
273 static int filesize
= 0;
278 static int ontty
= NOTTY
;
281 static unsigned int column
;
287 static unsigned int wid
;
295 static int num_ignores
= 0;
296 static char *ignores
[MAXARGS
];
299 static jmp_buf mhlenv
;
301 static char delim3
[] = /* from forw.c */
302 "\n----------------------------------------------------------------------\n\n";
303 static char delim4
[] = "\n------------------------------\n\n";
305 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
308 * Redefine a couple of functions.
309 * These are undefined later in the code.
311 #define adios mhladios
317 static void mhl_format (char *, int, int);
318 static int evalvar (struct mcomp
*);
319 static int ptoi (char *, int *);
320 static int ptos (char *, char **);
321 static char *parse (void);
322 static void process (char *, char *, int, int);
323 static void mhlfile (FILE *, char *, int, int);
324 static int mcomp_flags (char *);
325 static char *mcomp_add (long, char *, char *);
326 static void mcomp_format (struct mcomp
*, struct mcomp
*);
327 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
328 static void free_queue (struct mcomp
**, struct mcomp
**);
329 static void putcomp (struct mcomp
*, struct mcomp
*, int);
330 static char *oneline (char *, long);
331 static void putstr (char *, long);
332 static void putch (char, long);
333 static void intrser (int);
334 static void pipeser (int);
335 static void quitser (int);
336 static void mhladios (char *, char *, ...);
337 static void mhldone (int);
338 static void filterbody (struct mcomp
*, char *, int, int, FILE *,
340 static void compile_formatfield(struct mcomp
*);
341 static void compile_filterargs (void);
345 mhl (int argc
, char **argv
)
347 int length
= 0, nomore
= 0;
348 unsigned int i
, vecp
= 0;
350 char *cp
, *folder
= NULL
, *form
= NULL
;
351 char buf
[BUFSIZ
], *files
[MAXARGS
];
352 char **argp
, **arguments
;
354 /* Need this if called from main() of show(1). */
355 invo_name
= r1bindex (argv
[0], '/');
357 arguments
= getarguments (invo_name
, argc
, argv
, 1);
360 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
363 while ((cp
= *argp
++)) {
365 switch (smatch (++cp
, mhlswitches
)) {
367 ambigsw (cp
, mhlswitches
);
370 adios (NULL
, "-%s unknown\n", cp
);
373 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
374 print_help (buf
, mhlswitches
, 1);
377 print_version(invo_name
);
395 if (!(folder
= *argp
++) || *folder
== '-')
396 adios (NULL
, "missing argument to %s", argp
[-2]);
399 if (!(form
= *argp
++) || *form
== '-')
400 adios (NULL
, "missing argument to %s", argp
[-2]);
404 if (!(cp
= *argp
++) || *cp
== '-')
405 adios (NULL
, "missing argument to %s", argp
[-2]);
407 sleepsw
= atoi (cp
);/* ZERO ok! */
411 if (!(moreproc
= *argp
++) || *moreproc
== '-')
412 adios (NULL
, "missing argument to %s", argp
[-2]);
419 if (!(formatproc
= *argp
++) || *formatproc
== '-')
420 adios (NULL
, "missing argument to %s", argp
[-2]);
427 if (!(cp
= *argp
++) || *cp
== '-')
428 adios (NULL
, "missing argument to %s", argp
[-2]);
429 else if ((length
= atoi (cp
)) < 1)
430 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
433 if (!(cp
= *argp
++) || *cp
== '-')
434 adios (NULL
, "missing argument to %s", argp
[-2]);
435 else if ((width
= atoi (cp
)) < 1)
436 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
440 if (!(digest
= *argp
++) || *digest
== '-')
441 adios (NULL
, "missing argument to %s", argp
[-2]);
444 if (!(cp
= *argp
++) || *cp
== '-')
445 adios (NULL
, "missing argument to %s", argp
[-2]);
446 else if ((issue
= atoi (cp
)) < 1)
447 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
450 if (!(cp
= *argp
++) || *cp
== '-')
451 adios (NULL
, "missing argument to %s", argp
[-2]);
452 else if ((volume
= atoi (cp
)) < 1)
453 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
457 forwall
++; /* fall */
460 clearflg
= -1;/* XXX */
464 dashstuff
= 1; /* trinary logic */
467 dashstuff
= -1; /* trinary logic */
479 folder
= getenv ("mhfolder");
481 if (isatty (fileno (stdout
))) {
482 if (!nomore
&& moreproc
&& *moreproc
!= '\0') {
484 SIGNAL (SIGINT
, SIG_IGN
);
485 SIGNAL2 (SIGQUIT
, quitser
);
487 SIGNAL2 (SIGPIPE
, pipeser
);
488 m_popen (moreproc
, mhl_action
!= NULL
);
491 SIGNAL (SIGINT
, SIG_IGN
);
492 SIGNAL2 (SIGQUIT
, quitser
);
499 mhl_format (form
? form
: mhlformat
, length
, width
);
502 process (folder
, NULL
, 1, vecp
= 1);
504 for (i
= 0; i
< vecp
; i
++)
505 process (folder
, files
[i
], i
+ 1, vecp
);
510 printf ("%s", delim4
);
512 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
514 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
515 digest
, volume
, issue
);
518 for (cp
= buf
+ i
; i
> 1; i
--)
525 printf ("\n------- End of Forwarded Message%s\n",
526 vecp
> 1 ? "s" : "");
531 adios("output", "error writing");
534 if (clearflg
> 0 && ontty
== NOTTY
)
545 mhl_format (char *file
, int length
, int width
)
549 char *ap
, name
[NAMESZ
];
553 static dev_t dev
= 0;
554 static ino_t ino
= 0;
555 static time_t mtime
= 0;
558 if (stat (etcpath (file
), &st
) != NOTOK
559 && mtime
== st
.st_mtime
563 free_queue (&fmthd
, &fmttl
);
566 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
567 adios (file
, "unable to open format file");
569 if (fstat (fileno (fp
), &st
) != NOTOK
) {
575 global
.c_ovtxt
= global
.c_nfs
= NULL
;
579 if ((i
= sc_width ()) > 5)
581 global
.c_cwidth
= -1;
582 if ((i
= sc_length ()) > 5)
583 global
.c_length
= i
- 1;
584 global
.c_flags
= BELL
; /* BELL is default */
585 *(ip
= ignores
) = NULL
;
588 while (vfgets (fp
, &ap
) == OK
) {
593 TrimSuffixC(bp
, '\n');
596 (void) add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
601 strncpy (name
, parse(), sizeof(name
));
607 * Split this list of fields to ignore, and copy
608 * it to the end of the current "ignores" list.
610 if (!strcasecmp (name
, "ignores")) {
611 char **tmparray
, **p
;
614 /* split the fields */
615 tmparray
= brkstring (mh_xstrdup(++parptr
), ",", NULL
);
617 /* count number of fields split */
622 /* copy pointers to split fields to ignores array */
623 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
629 if (evalvar (&global
))
630 adios (NULL
, "format file syntax error: %s", bp
);
637 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
638 while (*parptr
== ':' || *parptr
== ',') {
641 adios (NULL
, "format file syntax error: %s", bp
);
643 if (!c1
->c_nfs
&& global
.c_nfs
) {
644 if (c1
->c_flags
& DATEFMT
) {
645 if (global
.c_flags
& DATEFMT
) {
646 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
647 compile_formatfield(c1
);
651 if (c1
->c_flags
& ADDRFMT
) {
652 if (global
.c_flags
& ADDRFMT
) {
653 c1
->c_nfs
= mh_xstrdup(global
.c_nfs
);
654 compile_formatfield(c1
);
661 adios (NULL
, "format file syntax error: %s", bp
);
667 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
670 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
671 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
672 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
673 (unsigned int)(unsigned long) c1
->c_nfs
,
674 (unsigned int)(unsigned long) c1
->c_fmt
);
675 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
676 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
677 c1
->c_cwidth
, c1
->c_length
);
678 fprintf (stderr
, "\tflags=%s\n",
679 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
685 global
.c_flags
|= CLEARSCR
;
688 global
.c_flags
&= ~CLEARSCR
;
691 switch (bellflg
) { /* command line may override format file */
693 global
.c_flags
|= BELL
;
696 global
.c_flags
&= ~BELL
;
701 global
.c_length
= length
;
703 global
.c_width
= width
;
704 if (global
.c_length
< 5)
705 global
.c_length
= 10000;
706 if (global
.c_width
< 5)
707 global
.c_width
= 10000;
712 evalvar (struct mcomp
*c1
)
714 char *cp
, name
[NAMESZ
];
719 strncpy (name
, parse(), sizeof(name
));
721 if (!strcasecmp (name
, "component")) {
722 if (ptos (name
, &c1
->c_text
))
724 c1
->c_flags
&= ~NOCOMPONENT
;
728 if (!strcasecmp (name
, "overflowtext"))
729 return ptos (name
, &c1
->c_ovtxt
);
731 if (!strcasecmp (name
, "formatfield")) {
732 if (ptos (name
, &cp
))
734 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
735 compile_formatfield(c1
);
736 c1
->c_flags
|= FORMAT
;
740 if (!strcasecmp (name
, "decode")) {
741 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
742 compile_formatfield(c1
);
743 c1
->c_flags
|= FORMAT
;
747 if (!strcasecmp (name
, "offset"))
748 return ptoi (name
, &c1
->c_offset
);
749 if (!strcasecmp (name
, "overflowoffset"))
750 return ptoi (name
, &c1
->c_ovoff
);
751 if (!strcasecmp (name
, "width"))
752 return ptoi (name
, &c1
->c_width
);
753 if (!strcasecmp (name
, "compwidth"))
754 return ptoi (name
, &c1
->c_cwidth
);
755 if (!strcasecmp (name
, "length"))
756 return ptoi (name
, &c1
->c_length
);
757 if (!strcasecmp (name
, "nodashstuffing"))
758 return (dashstuff
= -1);
760 for (ap
= triples
; ap
->t_name
; ap
++)
761 if (!strcasecmp (ap
->t_name
, name
)) {
762 c1
->c_flags
|= ap
->t_on
;
763 c1
->c_flags
&= ~ap
->t_off
;
767 if (!strcasecmp (name
, "formatarg")) {
768 struct arglist
*args
;
770 if (ptos (name
, &cp
))
773 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
774 advise (NULL
, "format filters are currently only supported on "
775 "the \"body\" component");
782 arglist_tail
->a_next
= args
;
789 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
800 ptoi (char *name
, int *i
)
804 if (*parptr
++ != '=' || !*(cp
= parse ())) {
805 advise (NULL
, "missing argument to variable %s", name
);
815 ptos (char *name
, char **s
)
819 if (*parptr
++ != '=') {
820 advise (NULL
, "missing argument to variable %s", name
);
824 if (*parptr
!= '"') {
826 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
830 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
831 if (*parptr
== QUOTE
)
838 if ((*parptr
= c
) == '"')
849 static char result
[NAMESZ
];
851 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
870 * Process one file/message
874 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
876 /* static to prevent "might be clobbered" warning from gcc 4.9.2: */
882 /* volatile to prevent "might be clobbered" warning from gcc: */
883 char *volatile fname2
= fname
? fname
: "(stdin)";
888 switch (setjmp (env
)) {
891 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
893 advise (fname
, "unable to open");
900 if (fstat(fileno(fp
), &st
) == 0) {
901 filesize
= st
.st_size
;
905 cp
= folder
? concat (folder
, ":", fname2
, NULL
) : mh_xstrdup(fname2
);
907 SIGNAL (SIGINT
, intrser
);
908 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
911 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
912 fmt_free(ap
->a_fmt
, 0);
921 SIGNAL (SIGINT
, SIG_IGN
);
922 if (mhl_action
== NULL
&& fp
!= stdin
&& fp
!= NULL
)
925 free (holder
.c_text
);
926 holder
.c_text
= NULL
;
928 free_queue (&msghd
, &msgtl
);
929 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
930 c1
->c_flags
&= ~HDROUTPUT
;
938 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
941 struct mcomp
*c1
, *c2
, *c3
;
942 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
943 m_getfld_state_t gstate
= 0;
945 compile_filterargs();
949 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
951 printf ("\n-------");
953 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
955 printf (" Message %d", ofilen
);
963 if ((global
.c_flags
& CLEARSCR
))
968 printf (">>> %s\n\n", mname
);
973 strncpy (buf
, "\n", sizeof(buf
));
975 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
978 printf ("Press <return> to list \"%s\"...", mname
);
982 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
983 advise ("stdout", "read");
986 if (strchr(buf
, '\n')) {
987 if ((global
.c_flags
& CLEARSCR
))
1001 printf (">>> %s\n\n", mname
);
1008 int bufsz
= sizeof buf
;
1009 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1012 bucket
= fmt_addcomptext(name
, buf
);
1013 for (ip
= ignores
; *ip
; ip
++)
1014 if (!strcasecmp (name
, *ip
)) {
1015 while (state
== FLDPLUS
) {
1017 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1018 fmt_appendcomp(bucket
, name
, buf
);
1025 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1026 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "", name
))
1029 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1030 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1031 if (!strcasecmp (c1
->c_name
? c1
->c_name
: "",
1032 c3
->c_name
? c3
->c_name
: "")) {
1034 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1038 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1039 while (state
== FLDPLUS
) {
1041 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1042 c1
->c_text
= add (buf
, c1
->c_text
);
1043 fmt_appendcomp(bucket
, name
, buf
);
1046 c1
->c_flags
|= EXTRA
;
1052 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1053 if (c1
->c_flags
& CLEARTEXT
) {
1054 putcomp (c1
, c1
, ONECOMP
);
1058 !strcasecmp (c1
->c_name
, "messagename")) {
1059 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1061 putcomp (c1
, &holder
, ONECOMP
);
1062 free (holder
.c_text
);
1063 holder
.c_text
= NULL
;
1066 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1067 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1068 if (c2
->c_flags
& EXTRA
)
1069 putcomp (c1
, c2
, TWOCOMP
);
1072 if (dobody
&& (!c1
->c_name
||
1073 !strcasecmp (c1
->c_name
, "body"))) {
1074 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1075 formatproc
!= NULL
) {
1076 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1078 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1079 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1080 while (state
== BODY
) {
1081 putcomp (c1
, &holder
, BODYCOMP
);
1083 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1086 free (holder
.c_text
);
1087 holder
.c_text
= NULL
;
1091 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1092 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "",
1093 c1
->c_name
? c1
->c_name
: "")) {
1094 putcomp (c1
, c2
, ONECOMP
);
1095 if (!(c1
->c_flags
& SPLIT
))
1099 m_getfld_state_destroy (&gstate
);
1104 advise (NULL
, "format error in message %s", mname
);
1106 m_getfld_state_destroy (&gstate
);
1110 adios (NULL
, "getfld() returned %d", state
);
1117 mcomp_flags (char *name
)
1121 for (ap
= pairs
; ap
->p_name
; ap
++)
1122 if (!strcasecmp (ap
->p_name
, name
))
1123 return (ap
->p_flags
);
1130 mcomp_add (long flags
, char *s1
, char *s2
)
1134 if (!(flags
& ADDRFMT
))
1135 return add (s1
, s2
);
1137 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1140 return add (s1
, add (",\n", s2
));
1147 struct pqpair
*pq_next
;
1152 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1157 struct pqpair
*p
, *q
;
1159 struct mailname
*mp
;
1166 dat
[3] = BUFSIZ
- 1;
1169 if (!(c1
->c_flags
& ADDRFMT
)) {
1170 charstring_t scanl
= charstring_create (BUFSIZ
);
1173 c1
->c_c_text
->c_text
= ap
;
1174 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1178 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1179 /* Don't need to append a newline, dctime() already did */
1180 c2
->c_text
= charstring_buffer_copy (scanl
);
1181 charstring_free (scanl
);
1183 /* ap is now owned by the component struct, so do NOT free it here */
1187 (q
= &pq
)->pq_next
= NULL
;
1188 while ((cp
= getname (ap
))) {
1190 if ((mp
= getm (cp
, NULL
, 0, error
, sizeof(error
))) == NULL
) {
1191 p
->pq_text
= mh_xstrdup(cp
);
1192 p
->pq_error
= mh_xstrdup(error
);
1194 p
->pq_text
= getcpy (mp
->m_text
);
1197 q
= (q
->pq_next
= p
);
1200 for (p
= pq
.pq_next
; p
; p
= q
) {
1201 charstring_t scanl
= charstring_create (BUFSIZ
);
1205 c1
->c_c_text
->c_text
= p
->pq_text
;
1208 if (c1
->c_c_error
) {
1209 c1
->c_c_error
->c_text
= p
->pq_error
;
1213 fmt_scan (c1
->c_fmt
, scanl
, BUFSIZ
- 1, dat
, NULL
);
1214 buffer
= charstring_buffer_copy (scanl
);
1217 c2
->c_text
= add (",\n", c2
->c_text
);
1218 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1220 c2
->c_text
= add (buffer
, c2
->c_text
);
1222 charstring_free (scanl
);
1232 c2
->c_text
= add ("\n", c2
->c_text
);
1237 static struct mcomp
*
1238 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1243 c1
->c_flags
= flags
& ~INIT
;
1244 if ((c1
->c_name
= name
? mh_xstrdup(name
) : NULL
))
1245 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1246 c1
->c_text
= text
? mh_xstrdup(text
) : NULL
;
1249 c1
->c_ovtxt
= mh_xstrdup(global
.c_ovtxt
);
1250 c1
->c_offset
= global
.c_offset
;
1251 c1
->c_ovoff
= global
. c_ovoff
;
1252 c1
->c_width
= c1
->c_length
= 0;
1253 c1
->c_cwidth
= global
.c_cwidth
;
1254 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1259 (*tail
)->c_next
= c1
;
1267 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1269 struct mcomp
*c1
, *c2
;
1271 for (c1
= *head
; c1
; c1
= c2
) {
1282 fmt_free (c1
->c_fmt
, 0);
1286 *head
= *tail
= NULL
;
1291 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1296 * Create a copy of c1->c_text with trailing whitespace
1297 * trimmed, for use with blank lines.
1299 char *trimmed_prefix
=
1300 rtrim (add (c1
->c_text
? c1
->c_text
: c1
->c_name
, NULL
));
1304 llim
= c1
->c_length
? c1
->c_length
: -1;
1305 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1306 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1308 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1310 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1311 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1312 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1315 if (c1
->c_flags
& CLEARTEXT
) {
1316 putstr (c1
->c_flags
& RTRIM
? rtrim (c1
->c_text
) : c1
->c_text
,
1318 putstr ("\n", c1
->c_flags
);
1322 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1323 mcomp_format (c1
, c2
);
1325 if (c1
->c_flags
& CENTER
) {
1326 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1327 - c1
->c_offset
- strlen (c2
->c_text
);
1328 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1329 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1330 lm
= c1
->c_offset
+ (count
/ 2);
1336 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1337 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1338 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1339 if (islower ((unsigned char) *cp
))
1340 *cp
= toupper ((unsigned char) *cp
);
1341 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1342 if (flag
!= BODYCOMP
) {
1343 putstr (": ", c1
->c_flags
);
1344 if (!(c1
->c_flags
& SPLIT
))
1345 c1
->c_flags
|= HDROUTPUT
;
1348 if ((count
= c1
->c_cwidth
-
1349 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1351 putstr (" ", c1
->c_flags
);
1354 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1358 && !(c2
->c_flags
& HDROUTPUT
)
1359 && !(c2
->c_flags
& NOCOMPONENT
)) {
1360 if (c1
->c_flags
& UPPERCASE
)
1361 for (cp
= c2
->c_name
; *cp
; cp
++)
1362 if (islower ((unsigned char) *cp
))
1363 *cp
= toupper ((unsigned char) *cp
);
1364 putstr (c2
->c_name
, c1
->c_flags
);
1365 putstr (": ", c1
->c_flags
);
1366 if (!(c1
->c_flags
& SPLIT
))
1367 c2
->c_flags
|= HDROUTPUT
;
1370 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1372 putstr (" ", c1
->c_flags
);
1374 if (c1
->c_flags
& UPPERCASE
)
1375 for (cp
= c2
->c_text
; *cp
; cp
++)
1376 if (islower ((unsigned char) *cp
))
1377 *cp
= toupper ((unsigned char) *cp
);
1381 if (flag
== TWOCOMP
)
1382 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1383 : (int) strlen (c2
->c_name
) + 2;
1385 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1386 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1388 count
+= c1
->c_offset
;
1390 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1391 /* Output line, trimming trailing whitespace if requested. */
1392 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1394 putstr ("\n", c1
->c_flags
);
1395 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1397 if (flag
== BODYCOMP
1398 && !(c1
->c_flags
& NOCOMPONENT
)) {
1399 /* Output component, trimming trailing whitespace if there
1400 is no text on the line. */
1402 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1404 putstr (trimmed_prefix
, c1
->c_flags
);
1408 /* Output line, trimming trailing whitespace if requested. */
1409 putstr (c1
->c_flags
& RTRIM
? rtrim (cp
) : cp
, c1
->c_flags
);
1412 putstr ("\n", c1
->c_flags
);
1414 if (flag
== BODYCOMP
&& term
== '\n')
1415 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1417 free (trimmed_prefix
);
1422 oneline (char *stuff
, long flags
)
1430 return (onelp
= NULL
);
1434 if (flags
& COMPRESS
) {
1435 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1436 if (isspace ((unsigned char) *onelp
)) {
1437 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1456 while (*onelp
&& *onelp
!= '\n')
1458 if (*onelp
== '\n') {
1462 if (flags
& LEFTADJUST
)
1463 while (*ret
== ' ' || *ret
== '\t')
1466 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1474 putstr (char *string
, long flags
)
1476 if (!column
&& lm
> 0) {
1479 putch ('\t', flags
);
1489 putch (*string
++, flags
);
1494 putch (char ch
, long flags
)
1507 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1509 if (global
.c_flags
& BELL
)
1513 if (read (fileno (stdout
), buf
, sizeof(buf
)) < 0) {
1514 advise ("stdout", "read");
1516 if (strchr(buf
, '\n')) {
1517 if (global
.c_flags
& CLEARSCR
)
1518 nmh_clear_screen ();
1522 row
= global
.c_length
/ 3;
1541 * If we are forwarding this message, and the first
1542 * column contains a dash, then add a dash and a space.
1544 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1553 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1554 putch ('\n', flags
);
1557 putstr (ovtxt
? ovtxt
: "", flags
);
1573 longjmp (env
, DONE
);
1601 mhladios (char *what
, char *fmt
, ...)
1606 advertise (what
, NULL
, fmt
, ap
);
1613 mhldone (int status
)
1617 longjmp (mhlenv
, DONE
);
1624 * Compile a format string used by the formatfield option and save it
1627 * We will want the {text} (and possibly {error}) components for later,
1628 * so look for them and save them if we find them.
1632 compile_formatfield(struct mcomp
*c1
)
1634 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1637 * As a note to myself and any other poor bastard who is looking through
1638 * this code in the future ....
1640 * When the format hash table is reset later on (as it almost certainly
1641 * will be), there will still be references to these components in the
1642 * compiled format instructions. Thus these component references will
1643 * be free'd when the format instructions are free'd (by fmt_free()).
1645 * So, in other words ... don't go free'ing them yourself!
1648 c1
->c_c_text
= fmt_findcomp("text");
1649 c1
->c_c_error
= fmt_findcomp("error");
1653 * Compile all of the arguments for our format list.
1655 * Iterate through the linked list of format strings and compile them.
1656 * Note that we reset the format hash table before we start, but we do NOT
1657 * reset it between calls to fmt_compile().
1662 compile_filterargs (void)
1664 struct arglist
*arg
= arglist_head
;
1671 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1676 * Search through and mark any components that are address components
1679 for (ap
= addrcomps
; *ap
; ap
++) {
1680 cptr
= fmt_findcomp (*ap
);
1682 cptr
->c_type
|= CT_ADDR
;
1687 * Filter the body of a message through a specified format program
1691 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1692 m_getfld_state_t gstate
)
1694 struct mcomp holder
;
1696 int fdinput
[2], fdoutput
[2], waitstat
;
1698 pid_t writerpid
, filterpid
;
1701 * Create pipes so we can communicate with our filter process.
1704 if (pipe(fdinput
) < 0) {
1705 adios(NULL
, "Unable to create input pipe");
1708 if (pipe(fdoutput
) < 0) {
1709 adios(NULL
, "Unable to create output pipe");
1713 * Here's what we're doing to do.
1715 * - Fork ourselves and start writing data to the write side of the
1716 * input pipe (fdinput[1]).
1718 * - Fork and exec our filter program. We set the standard input of
1719 * our filter program to be the read side of our input pipe (fdinput[0]).
1720 * Standard output is set to the write side of our output pipe
1723 * - We read from the read side of the output pipe (fdoutput[0]).
1725 * We're forking because that's the simplest way to prevent any deadlocks.
1726 * (without doing something like switching to non-blocking I/O and using
1727 * select or poll, and I'm not interested in doing that).
1730 switch (writerpid
= fork()) {
1733 * Our child process - just write to the filter input (fdinput[1]).
1734 * Close all other descriptors that we don't need.
1742 * Call m_getfld() until we're no longer in the BODY state
1745 while (state
== BODY
) {
1747 if (write(fdinput
[1], buf
, strlen(buf
)) < 0) {
1748 advise ("pipe output", "write");
1750 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1754 * We should be done; time to exit.
1759 * Make sure we call _exit(), otherwise we may flush out the stdio
1760 * buffers that we have duplicated from the parent.
1764 adios(NULL
, "Unable to fork for filter writer process");
1769 * Fork and exec() our filter program, after redirecting standard in
1770 * and standard out appropriately.
1773 switch (filterpid
= fork()) {
1774 char **args
, *program
;
1776 int i
, dat
[5], s
, argp
;
1780 * Configure an argument array for us
1783 args
= argsplit(formatproc
, &program
, &argp
);
1784 args
[argp
+ filter_nargs
] = NULL
;
1792 * Pull out each argument and scan them.
1795 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1796 charstring_t scanl
= charstring_create (BUFSIZ
);
1798 fmt_scan(a
->a_fmt
, scanl
, BUFSIZ
, dat
, NULL
);
1799 args
[i
] = charstring_buffer_copy (scanl
);
1800 charstring_free (scanl
);
1802 * fmt_scan likes to put a trailing newline at the end of the
1803 * format string. If we have one, get rid of it.
1805 s
= strlen(args
[i
]);
1806 if (args
[i
][s
- 1] == '\n')
1807 args
[i
][s
- 1] = '\0';
1810 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1814 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1815 adios("formatproc", "Unable to dup2() standard input");
1817 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1818 adios("formatproc", "Unable to dup2() standard output");
1822 * Close everything (especially the old input and output
1823 * descriptors, since they've been dup'd to stdin and stdout),
1824 * and exec the formatproc.
1832 execvp(formatproc
, args
);
1834 adios(formatproc
, "Unable to execute filter");
1839 adios(NULL
, "Unable to fork format program");
1843 * Close everything except our reader (fdoutput[0]);
1851 * As we read in this data, send it to putcomp
1854 holder
.c_text
= buf
;
1856 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1858 putcomp(c1
, &holder
, BODYCOMP
);
1862 adios(NULL
, "reading from formatproc");
1866 * See if we got any errors along the way. I'm a little leery of calling
1867 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1870 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1871 if (errno
!= ECHILD
) {
1872 adios("filterproc", "Unable to determine status");
1875 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1876 pidstatus(waitstat
, stderr
, "filterproc");
1880 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1881 if (errno
!= ECHILD
) {
1882 adios("writer process", "Unable to determine status");
1886 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1887 pidstatus(waitstat
, stderr
, "writer process");