]>
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>
20 #include <sys/types.h>
24 * for a component containing addresses, ADDRFMT, if COMPRESS is also
25 * set, then addresses get split wrong (not at the spaces between commas).
26 * To fix this correctly, putstr() should know about "atomic" strings that
27 * must NOT be broken across lines. That's too difficult for right now
28 * (it turns out that there are a number of degernate cases), so in
29 * oneline(), instead of
31 * (*onelp == '\n' && !onelp[1])
33 * being a terminating condition,
35 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
37 * is used instead. This cuts the line prematurely, and gives us a much
38 * better chance of getting things right.
47 #define MHL_SWITCHES \
48 X("bell", 0, BELLSW) \
49 X("nobell", 0, NBELLSW) \
50 X("clear", 0, CLRSW) \
51 X("noclear", 0, NCLRSW) \
52 X("folder +folder", 0, FOLDSW) \
53 X("form formfile", 0, FORMSW) \
54 X("moreproc program", 0, PROGSW) \
55 X("nomoreproc", 0, NPROGSW) \
56 X("length lines", 0, LENSW) \
57 X("width columns", 0, WIDTHSW) \
58 X("sleep seconds", 0, SLEEPSW) \
59 X("dashstuffing", -12, BITSTUFFSW) /* interface from forw */ \
60 X("nodashstuffing", -14, NBITSTUFFSW) /* interface from forw */ \
61 X("version", 0, VERSIONSW) \
62 X("help", 0, HELPSW) \
63 X("forward", -7, FORW1SW) /* interface from forw */ \
64 X("forwall", -7, FORW2SW) /* interface from forw */ \
65 X("digest list", -6, DGSTSW) \
66 X("volume number", -6, VOLUMSW) \
67 X("issue number", -5, ISSUESW) \
68 X("nobody", -6, NBODYSW) \
69 X("fmtproc program", 0, FMTPROCSW) \
70 X("nofmtproc", 0, NFMTPROCSW) \
72 #define X(sw, minchars, id) id,
73 DEFINE_SWITCH_ENUM(MHL
);
76 #define X(sw, minchars, id) { sw, minchars, id },
77 DEFINE_SWITCH_ARRAY(MHL
, mhlswitches
);
80 #define NOCOMPONENT 0x000001 /* don't show component name */
81 #define UPPERCASE 0x000002 /* display in all upper case */
82 #define CENTER 0x000004 /* center line */
83 #define CLEARTEXT 0x000008 /* cleartext */
84 #define EXTRA 0x000010 /* an "extra" component */
85 #define HDROUTPUT 0x000020 /* already output */
86 #define CLEARSCR 0x000040 /* clear screen */
87 #define LEFTADJUST 0x000080 /* left justify multiple lines */
88 #define COMPRESS 0x000100 /* compress text */
89 #define ADDRFMT 0x000200 /* contains addresses */
90 #define BELL 0x000400 /* sound bell at EOP */
91 #define DATEFMT 0x000800 /* contains dates */
92 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
93 #define INIT 0x002000 /* initialize component */
94 #define SPLIT 0x010000 /* split headers (don't concatenate) */
95 #define NONEWLINE 0x020000 /* don't write trailing newline */
96 #define NOWRAP 0x040000 /* Don't wrap lines ever */
97 #define FMTFILTER 0x080000 /* Filter through format filter */
98 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
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 */
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
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 { "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 m_popen (char *);
339 static void filterbody (struct mcomp
*, char *, int, int, FILE *,
341 static void compile_formatfield(struct mcomp
*);
342 static void compile_filterargs (void);
346 mhl (int argc
, char **argv
)
348 int length
= 0, nomore
= 0;
349 unsigned int i
, vecp
= 0;
351 char *cp
, *folder
= NULL
, *form
= NULL
;
352 char buf
[BUFSIZ
], *files
[MAXARGS
];
353 char **argp
, **arguments
;
355 invo_name
= r1bindex (argv
[0], '/');
357 /* read user profile/context */
360 arguments
= getarguments (invo_name
, argc
, argv
, 1);
363 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
366 while ((cp
= *argp
++)) {
368 switch (smatch (++cp
, mhlswitches
)) {
370 ambigsw (cp
, mhlswitches
);
373 adios (NULL
, "-%s unknown\n", cp
);
376 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
377 print_help (buf
, mhlswitches
, 1);
380 print_version(invo_name
);
398 if (!(folder
= *argp
++) || *folder
== '-')
399 adios (NULL
, "missing argument to %s", argp
[-2]);
402 if (!(form
= *argp
++) || *form
== '-')
403 adios (NULL
, "missing argument to %s", argp
[-2]);
407 if (!(cp
= *argp
++) || *cp
== '-')
408 adios (NULL
, "missing argument to %s", argp
[-2]);
409 sleepsw
= atoi (cp
);/* ZERO ok! */
413 if (!(moreproc
= *argp
++) || *moreproc
== '-')
414 adios (NULL
, "missing argument to %s", argp
[-2]);
421 if (!(formatproc
= *argp
++) || *formatproc
== '-')
422 adios (NULL
, "missing argument to %s", argp
[-2]);
429 if (!(cp
= *argp
++) || *cp
== '-')
430 adios (NULL
, "missing argument to %s", argp
[-2]);
431 if ((length
= atoi (cp
)) < 1)
432 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
435 if (!(cp
= *argp
++) || *cp
== '-')
436 adios (NULL
, "missing argument to %s", argp
[-2]);
437 if ((width
= atoi (cp
)) < 1)
438 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
442 if (!(digest
= *argp
++) || *digest
== '-')
443 adios (NULL
, "missing argument to %s", argp
[-2]);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((issue
= atoi (cp
)) < 1)
449 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
452 if (!(cp
= *argp
++) || *cp
== '-')
453 adios (NULL
, "missing argument to %s", argp
[-2]);
454 if ((volume
= atoi (cp
)) < 1)
455 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
459 forwall
++; /* fall */
462 clearflg
= -1;/* XXX */
466 dashstuff
= 1; /* trinary logic */
469 dashstuff
= -1; /* trinary logic */
481 folder
= getenv ("mhfolder");
483 if (isatty (fileno (stdout
))) {
484 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
486 SIGNAL (SIGINT
, SIG_IGN
);
487 SIGNAL2 (SIGQUIT
, quitser
);
489 SIGNAL2 (SIGPIPE
, pipeser
);
493 SIGNAL (SIGINT
, SIG_IGN
);
494 SIGNAL2 (SIGQUIT
, quitser
);
501 mhl_format (form
? form
: mhlformat
, length
, width
);
504 process (folder
, NULL
, 1, vecp
= 1);
506 for (i
= 0; i
< vecp
; i
++)
507 process (folder
, files
[i
], i
+ 1, vecp
);
512 printf ("%s", delim4
);
514 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
516 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
517 digest
, volume
, issue
);
520 for (cp
= buf
+ i
; i
> 1; i
--)
527 printf ("\n------- End of Forwarded Message%s\n",
528 vecp
> 1 ? "s" : "");
533 adios("output", "error writing");
536 if (clearflg
> 0 && ontty
== NOTTY
)
547 mhl_format (char *file
, int length
, int width
)
551 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
555 static dev_t dev
= 0;
556 static ino_t ino
= 0;
557 static time_t mtime
= 0;
560 if (stat (etcpath (file
), &st
) != NOTOK
561 && mtime
== st
.st_mtime
566 free_queue (&fmthd
, &fmttl
);
569 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
570 adios (file
, "unable to open format file");
572 if (fstat (fileno (fp
), &st
) != NOTOK
) {
578 global
.c_ovtxt
= global
.c_nfs
= NULL
;
582 if ((i
= sc_width ()) > 5)
584 global
.c_cwidth
= -1;
585 if ((i
= sc_length ()) > 5)
586 global
.c_length
= i
- 1;
587 global
.c_flags
= BELL
; /* BELL is default */
588 *(ip
= ignores
) = NULL
;
591 while (vfgets (fp
, &ap
) == OK
) {
596 if ((cp
= strchr(bp
, '\n')))
600 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
605 strncpy (name
, parse(), sizeof(name
));
611 * Split this list of fields to ignore, and copy
612 * it to the end of the current "ignores" list.
614 if (!strcasecmp (name
, "ignores")) {
615 char **tmparray
, **p
;
618 /* split the fields */
619 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
621 /* count number of fields split */
626 /* copy pointers to split fields to ignores array */
627 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
633 if (evalvar (&global
))
634 adios (NULL
, "format file syntax error: %s", bp
);
641 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
642 while (*parptr
== ':' || *parptr
== ',') {
645 adios (NULL
, "format file syntax error: %s", bp
);
647 if (!c1
->c_nfs
&& global
.c_nfs
) {
648 if (c1
->c_flags
& DATEFMT
) {
649 if (global
.c_flags
& DATEFMT
) {
650 c1
->c_nfs
= getcpy (global
.c_nfs
);
651 compile_formatfield(c1
);
655 if (c1
->c_flags
& ADDRFMT
) {
656 if (global
.c_flags
& ADDRFMT
) {
657 c1
->c_nfs
= getcpy (global
.c_nfs
);
658 compile_formatfield(c1
);
665 adios (NULL
, "format file syntax error: %s", bp
);
671 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
672 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
673 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
674 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
675 (unsigned int)(unsigned long) c1
->c_nfs
,
676 (unsigned int)(unsigned long) c1
->c_fmt
);
677 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
678 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
679 c1
->c_cwidth
, c1
->c_length
);
680 fprintf (stderr
, "\tflags=%s\n",
681 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
687 global
.c_flags
|= CLEARSCR
;
690 global
.c_flags
&= ~CLEARSCR
;
693 switch (bellflg
) { /* command line may override format file */
695 global
.c_flags
|= BELL
;
698 global
.c_flags
&= ~BELL
;
703 global
.c_length
= length
;
705 global
.c_width
= width
;
706 if (global
.c_length
< 5)
707 global
.c_length
= 10000;
708 if (global
.c_width
< 5)
709 global
.c_width
= 10000;
714 evalvar (struct mcomp
*c1
)
716 char *cp
, name
[NAMESZ
];
721 strncpy (name
, parse(), sizeof(name
));
723 if (!strcasecmp (name
, "component")) {
724 if (ptos (name
, &c1
->c_text
))
726 c1
->c_flags
&= ~NOCOMPONENT
;
730 if (!strcasecmp (name
, "overflowtext"))
731 return ptos (name
, &c1
->c_ovtxt
);
733 if (!strcasecmp (name
, "formatfield")) {
734 if (ptos (name
, &cp
))
736 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
737 compile_formatfield(c1
);
738 c1
->c_flags
|= FORMAT
;
742 if (!strcasecmp (name
, "decode")) {
743 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
744 compile_formatfield(c1
);
745 c1
->c_flags
|= FORMAT
;
749 if (!strcasecmp (name
, "offset"))
750 return ptoi (name
, &c1
->c_offset
);
751 if (!strcasecmp (name
, "overflowoffset"))
752 return ptoi (name
, &c1
->c_ovoff
);
753 if (!strcasecmp (name
, "width"))
754 return ptoi (name
, &c1
->c_width
);
755 if (!strcasecmp (name
, "compwidth"))
756 return ptoi (name
, &c1
->c_cwidth
);
757 if (!strcasecmp (name
, "length"))
758 return ptoi (name
, &c1
->c_length
);
759 if (!strcasecmp (name
, "nodashstuffing"))
760 return (dashstuff
= -1);
762 for (ap
= triples
; ap
->t_name
; ap
++)
763 if (!strcasecmp (ap
->t_name
, name
)) {
764 c1
->c_flags
|= ap
->t_on
;
765 c1
->c_flags
&= ~ap
->t_off
;
769 if (!strcasecmp (name
, "formatarg")) {
770 struct arglist
*args
;
772 if (ptos (name
, &cp
))
775 if (! c1
->c_name
|| strcasecmp (c1
->c_name
, "body")) {
776 advise (NULL
, "format filters are currently only supported on "
777 "the \"body\" component");
781 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
784 arglist_tail
->a_next
= args
;
791 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
802 ptoi (char *name
, int *i
)
806 if (*parptr
++ != '=' || !*(cp
= parse ())) {
807 advise (NULL
, "missing argument to variable %s", name
);
817 ptos (char *name
, char **s
)
821 if (*parptr
++ != '=') {
822 advise (NULL
, "missing argument to variable %s", name
);
826 if (*parptr
!= '"') {
828 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
832 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
833 if (*parptr
== QUOTE
)
840 if ((*parptr
= c
) == '"')
851 static char result
[NAMESZ
];
853 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
872 * Process one file/message
876 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
884 switch (setjmp (env
)) {
887 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
889 advise (fname
, "unable to open");
897 if (fstat(fileno(fp
), &st
) == 0) {
898 filesize
= st
.st_size
;
902 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
904 SIGNAL (SIGINT
, intrser
);
905 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
907 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
908 fmt_free(ap
->a_fmt
, 0);
917 SIGNAL (SIGINT
, SIG_IGN
);
918 if (mhl_action
== NULL
&& fp
!= stdin
)
922 free (holder
.c_text
);
923 holder
.c_text
= NULL
;
925 free_queue (&msghd
, &msgtl
);
926 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
927 c1
->c_flags
&= ~HDROUTPUT
;
935 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
938 struct mcomp
*c1
, *c2
, *c3
;
939 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
940 m_getfld_state_t gstate
= 0;
942 compile_filterargs();
946 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
948 printf ("\n-------");
950 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
952 printf (" Message %d", ofilen
);
960 if ((global
.c_flags
& CLEARSCR
))
965 printf (">>> %s\n\n", mname
);
970 strncpy (buf
, "\n", sizeof(buf
));
972 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
975 printf ("Press <return> to list \"%s\"...", mname
);
979 read (fileno (stdout
), buf
, sizeof(buf
));
981 if (strchr(buf
, '\n')) {
982 if ((global
.c_flags
& CLEARSCR
))
996 printf (">>> %s\n\n", mname
);
1003 int bufsz
= sizeof buf
;
1004 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
)) {
1007 bucket
= fmt_addcomptext(name
, buf
);
1008 for (ip
= ignores
; *ip
; ip
++)
1009 if (!strcasecmp (name
, *ip
)) {
1010 while (state
== FLDPLUS
) {
1012 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1013 fmt_appendcomp(bucket
, name
, buf
);
1020 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1021 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "", name
))
1024 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1025 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1026 if (!strcasecmp (c1
->c_name
? c1
->c_name
: "",
1027 c3
->c_name
? c3
->c_name
: "")) {
1029 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1033 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1034 while (state
== FLDPLUS
) {
1036 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fp
);
1037 c1
->c_text
= add (buf
, c1
->c_text
);
1038 fmt_appendcomp(bucket
, name
, buf
);
1041 c1
->c_flags
|= EXTRA
;
1047 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1048 if (c1
->c_flags
& CLEARTEXT
) {
1049 putcomp (c1
, c1
, ONECOMP
);
1053 !strcasecmp (c1
->c_name
, "messagename")) {
1054 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1056 putcomp (c1
, &holder
, ONECOMP
);
1057 free (holder
.c_text
);
1058 holder
.c_text
= NULL
;
1061 if (!c1
->c_name
|| !strcasecmp (c1
->c_name
, "extras")) {
1062 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1063 if (c2
->c_flags
& EXTRA
)
1064 putcomp (c1
, c2
, TWOCOMP
);
1067 if (dobody
&& (!c1
->c_name
||
1068 !strcasecmp (c1
->c_name
, "body"))) {
1069 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1070 formatproc
!= NULL
) {
1071 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1073 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1074 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1075 while (state
== BODY
) {
1076 putcomp (c1
, &holder
, BODYCOMP
);
1078 state
= m_getfld (&gstate
, name
, holder
.c_text
,
1081 free (holder
.c_text
);
1082 holder
.c_text
= NULL
;
1086 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1087 if (!strcasecmp (c2
->c_name
? c2
->c_name
: "",
1088 c1
->c_name
? c1
->c_name
: "")) {
1089 putcomp (c1
, c2
, ONECOMP
);
1090 if (!(c1
->c_flags
& SPLIT
))
1094 m_getfld_state_destroy (&gstate
);
1099 advise (NULL
, "format error in message %s", mname
);
1101 m_getfld_state_destroy (&gstate
);
1105 adios (NULL
, "getfld() returned %d", state
);
1112 mcomp_flags (char *name
)
1116 for (ap
= pairs
; ap
->p_name
; ap
++)
1117 if (!strcasecmp (ap
->p_name
, name
))
1118 return (ap
->p_flags
);
1125 mcomp_add (long flags
, char *s1
, char *s2
)
1129 if (!(flags
& ADDRFMT
))
1130 return add (s1
, s2
);
1132 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1135 return add (s1
, add (",\n", s2
));
1142 struct pqpair
*pq_next
;
1147 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1151 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1152 struct pqpair
*p
, *q
;
1154 struct mailname
*mp
;
1161 dat
[3] = sizeof(buffer
) - 1;
1164 if (!(c1
->c_flags
& ADDRFMT
)) {
1166 c1
->c_c_text
->c_text
= ap
;
1167 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1171 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1,
1173 /* Don't need to append a newline, dctime() already did */
1174 c2
->c_text
= getcpy (buffer
);
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
))) {
1182 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1183 adios (NULL
, "unable to allocate pqpair memory");
1185 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1186 p
->pq_text
= getcpy (cp
);
1187 p
->pq_error
= getcpy (error
);
1189 p
->pq_text
= getcpy (mp
->m_text
);
1192 q
= (q
->pq_next
= p
);
1195 for (p
= pq
.pq_next
; p
; p
= q
) {
1197 c1
->c_c_text
->c_text
= p
->pq_text
;
1200 if (c1
->c_c_error
) {
1201 c1
->c_c_error
->c_text
= p
->pq_error
;
1205 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1,
1209 c2
->c_text
= add (",\n", c2
->c_text
);
1210 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1212 c2
->c_text
= add (buffer
, c2
->c_text
);
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
)
1233 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1234 adios (NULL
, "unable to allocate comp memory");
1236 c1
->c_flags
= flags
& ~INIT
;
1237 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1238 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1239 c1
->c_text
= text
? getcpy (text
) : NULL
;
1242 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1243 c1
->c_offset
= global
.c_offset
;
1244 c1
->c_ovoff
= global
. c_ovoff
;
1245 c1
->c_width
= c1
->c_length
= 0;
1246 c1
->c_cwidth
= global
.c_cwidth
;
1247 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1252 (*tail
)->c_next
= c1
;
1260 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1262 struct mcomp
*c1
, *c2
;
1264 for (c1
= *head
; c1
; c1
= c2
) {
1275 fmt_free (c1
->c_fmt
, 0);
1279 *head
= *tail
= NULL
;
1284 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1291 llim
= c1
->c_length
? c1
->c_length
: -1;
1292 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1293 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1295 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1297 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1298 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1299 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1302 if (c1
->c_flags
& CLEARTEXT
) {
1303 putstr (c1
->c_text
, c1
->c_flags
);
1304 putstr ("\n", c1
->c_flags
);
1308 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1309 mcomp_format (c1
, c2
);
1311 if (c1
->c_flags
& CENTER
) {
1312 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1313 - c1
->c_offset
- strlen (c2
->c_text
);
1314 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1315 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1316 lm
= c1
->c_offset
+ (count
/ 2);
1322 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1323 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1324 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1325 if (islower ((unsigned char) *cp
))
1326 *cp
= toupper ((unsigned char) *cp
);
1327 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1328 if (flag
!= BODYCOMP
) {
1329 putstr (": ", c1
->c_flags
);
1330 if (!(c1
->c_flags
& SPLIT
))
1331 c1
->c_flags
|= HDROUTPUT
;
1334 if ((count
= c1
->c_cwidth
-
1335 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1337 putstr (" ", c1
->c_flags
);
1340 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1344 && !(c2
->c_flags
& HDROUTPUT
)
1345 && !(c2
->c_flags
& NOCOMPONENT
)) {
1346 if (c1
->c_flags
& UPPERCASE
)
1347 for (cp
= c2
->c_name
; *cp
; cp
++)
1348 if (islower ((unsigned char) *cp
))
1349 *cp
= toupper ((unsigned char) *cp
);
1350 putstr (c2
->c_name
, c1
->c_flags
);
1351 putstr (": ", c1
->c_flags
);
1352 if (!(c1
->c_flags
& SPLIT
))
1353 c2
->c_flags
|= HDROUTPUT
;
1356 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1358 putstr (" ", c1
->c_flags
);
1360 if (c1
->c_flags
& UPPERCASE
)
1361 for (cp
= c2
->c_text
; *cp
; cp
++)
1362 if (islower ((unsigned char) *cp
))
1363 *cp
= toupper ((unsigned char) *cp
);
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
1372 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1374 count
+= c1
->c_offset
;
1376 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1377 putstr(cp
, c1
->c_flags
);
1379 putstr ("\n", c1
->c_flags
);
1380 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1382 if (flag
== BODYCOMP
1383 && !(c1
->c_flags
& NOCOMPONENT
))
1384 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1386 putstr (cp
, c1
->c_flags
);
1388 putstr ("\n", c1
->c_flags
);
1390 if (flag
== BODYCOMP
&& term
== '\n')
1391 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1396 oneline (char *stuff
, long flags
)
1404 return (onelp
= NULL
);
1408 if (flags
& COMPRESS
) {
1409 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1410 if (isspace ((unsigned char) *onelp
)) {
1411 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1430 while (*onelp
&& *onelp
!= '\n')
1432 if (*onelp
== '\n') {
1436 if (flags
& LEFTADJUST
)
1437 while (*ret
== ' ' || *ret
== '\t')
1440 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1448 putstr (char *string
, long flags
)
1450 if (!column
&& lm
> 0) {
1453 putch ('\t', flags
);
1463 putch (*string
++, flags
);
1468 putch (char ch
, long flags
)
1481 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1483 if (global
.c_flags
& BELL
)
1487 read (fileno (stdout
), buf
, sizeof(buf
));
1488 if (strchr(buf
, '\n')) {
1489 if (global
.c_flags
& CLEARSCR
)
1494 row
= global
.c_length
/ 3;
1513 * If we are forwarding this message, and the first
1514 * column contains a dash, then add a dash and a space.
1516 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1525 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1526 putch ('\n', flags
);
1529 putstr (ovtxt
? ovtxt
: "", flags
);
1545 longjmp (env
, DONE
);
1570 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1572 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1575 struct arglist
*a
, *a2
;
1577 switch (setjmp (mhlenv
)) {
1580 sleepsw
= 0; /* XXX */
1581 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1584 mhl_action
= action
;
1587 * If signal is at default action, then start ignoring
1588 * it, else let it set to its current action.
1590 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1591 SIGNAL (SIGINT
, istat
);
1592 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1593 SIGNAL (SIGQUIT
, qstat
);
1594 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1595 mhl (argc
, argv
); /* FALL THROUGH! */
1598 SIGNAL (SIGINT
, istat
);
1599 SIGNAL (SIGQUIT
, qstat
);
1600 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1603 SIGNAL (SIGPIPE
, pstat
);
1605 if (holder
.c_text
) {
1606 free (holder
.c_text
);
1607 holder
.c_text
= NULL
;
1609 free_queue (&msghd
, &msgtl
);
1610 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1611 c1
->c_flags
&= ~HDROUTPUT
;
1629 mhladios (char *what
, char *fmt
, ...)
1634 advertise (what
, NULL
, fmt
, ap
);
1641 mhldone (int status
)
1645 longjmp (mhlenv
, DONE
);
1651 static int m_pid
= NOTOK
;
1652 static int sd
= NOTOK
;
1655 m_popen (char *name
)
1661 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1662 adios ("standard output", "unable to dup()");
1664 if (pipe (pd
) == NOTOK
)
1665 adios ("pipe", "unable to");
1667 switch (m_pid
= fork()) {
1669 adios ("fork", "unable to");
1672 SIGNAL (SIGINT
, SIG_DFL
);
1673 SIGNAL (SIGQUIT
, SIG_DFL
);
1676 if (pd
[0] != fileno (stdin
)) {
1677 dup2 (pd
[0], fileno (stdin
));
1680 arglist
= argsplit(name
, &file
, NULL
);
1681 execvp (file
, arglist
);
1682 fprintf (stderr
, "unable to exec ");
1688 if (pd
[1] != fileno (stdout
)) {
1689 dup2 (pd
[1], fileno (stdout
));
1704 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1705 adios ("standard output", "unable to dup2()");
1714 pidwait (m_pid
, OK
);
1720 * Compile a format string used by the formatfield option and save it
1723 * We will want the {text} (and possibly {error}) components for later,
1724 * so look for them and save them if we find them.
1728 compile_formatfield(struct mcomp
*c1
)
1730 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1733 * As a note to myself and any other poor bastard who is looking through
1734 * this code in the future ....
1736 * When the format hash table is reset later on (as it almost certainly
1737 * will be), there will still be references to these components in the
1738 * compiled format instructions. Thus these component references will
1739 * be free'd when the format instructions are free'd (by fmt_free()).
1741 * So, in other words ... don't go free'ing them yourself!
1744 c1
->c_c_text
= fmt_findcomp("text");
1745 c1
->c_c_error
= fmt_findcomp("error");
1749 * Compile all of the arguments for our format list.
1751 * Iterate through the linked list of format strings and compile them.
1752 * Note that we reset the format hash table before we start, but we do NOT
1753 * reset it between calls to fmt_compile().
1758 compile_filterargs (void)
1760 struct arglist
*arg
= arglist_head
;
1767 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1772 * Search through and mark any components that are address components
1775 for (ap
= addrcomps
; *ap
; ap
++) {
1776 cptr
= fmt_findcomp (*ap
);
1778 cptr
->c_type
|= CT_ADDR
;
1783 * Filter the body of a message through a specified format program
1787 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1788 m_getfld_state_t gstate
)
1790 struct mcomp holder
;
1792 int fdinput
[2], fdoutput
[2], waitstat
;
1794 pid_t writerpid
, filterpid
;
1797 * Create pipes so we can communicate with our filter process.
1800 if (pipe(fdinput
) < 0) {
1801 adios(NULL
, "Unable to create input pipe");
1804 if (pipe(fdoutput
) < 0) {
1805 adios(NULL
, "Unable to create output pipe");
1809 * Here's what we're doing to do.
1811 * - Fork ourselves and start writing data to the write side of the
1812 * input pipe (fdinput[1]).
1814 * - Fork and exec our filter program. We set the standard input of
1815 * our filter program to be the read side of our input pipe (fdinput[0]).
1816 * Standard output is set to the write side of our output pipe
1819 * - We read from the read side of the output pipe (fdoutput[0]).
1821 * We're forking because that's the simplest way to prevent any deadlocks.
1822 * (without doing something like switching to non-blocking I/O and using
1823 * select or poll, and I'm not interested in doing that).
1826 switch (writerpid
= fork()) {
1829 * Our child process - just write to the filter input (fdinput[1]).
1830 * Close all other descriptors that we don't need.
1838 * Call m_getfld() until we're no longer in the BODY state
1841 while (state
== BODY
) {
1843 write(fdinput
[1], buf
, strlen(buf
));
1844 state
= m_getfld (&gstate
, name
, buf
, &bufsz2
, fp
);
1848 * We should be done; time to exit.
1853 * Make sure we call _exit(), otherwise we may flush out the stdio
1854 * buffers that we have duplicated from the parent.
1859 adios(NULL
, "Unable to fork for filter writer process");
1864 * Fork and exec() our filter program, after redirecting standard in
1865 * and standard out appropriately.
1868 switch (filterpid
= fork()) {
1869 char **args
, *program
;
1871 int i
, dat
[5], s
, argp
;
1875 * Configure an argument array for us
1878 args
= argsplit(formatproc
, &program
, &argp
);
1879 args
[argp
+ filter_nargs
] = NULL
;
1887 * Pull out each argument and scan them.
1890 for (a
= arglist_head
, i
= argp
; a
!= NULL
; a
= a
->a_next
, i
++) {
1891 args
[i
] = mh_xmalloc(BUFSIZ
);
1892 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
- 1, BUFSIZ
, dat
, NULL
);
1894 * fmt_scan likes to put a trailing newline at the end of the
1895 * format string. If we have one, get rid of it.
1897 s
= strlen(args
[i
]);
1898 if (args
[i
][s
- 1] == '\n')
1899 args
[i
][s
- 1] = '\0';
1902 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1906 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1907 adios("formatproc", "Unable to dup2() standard input");
1909 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1910 adios("formatproc", "Unable to dup2() standard output");
1914 * Close everything (especially the old input and output
1915 * descriptors, since they've been dup'd to stdin and stdout),
1916 * and exec the formatproc.
1924 execvp(formatproc
, args
);
1926 adios(formatproc
, "Unable to execute filter");
1931 adios(NULL
, "Unable to fork format program");
1935 * Close everything except our reader (fdoutput[0]);
1943 * As we read in this data, send it to putcomp
1946 holder
.c_text
= buf
;
1948 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1950 putcomp(c1
, &holder
, BODYCOMP
);
1954 adios(NULL
, "reading from formatproc");
1958 * See if we got any errors along the way. I'm a little leery of calling
1959 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1962 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1963 if (errno
!= ECHILD
) {
1964 adios("filterproc", "Unable to determine status");
1967 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1968 pidstatus(waitstat
, stderr
, "filterproc");
1972 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1973 if (errno
!= ECHILD
) {
1974 adios("writer process", "Unable to determine status");
1978 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1979 pidstatus(waitstat
, stderr
, "writer process");