]>
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 static struct swit mhlswitches
[] = {
57 { "folder +folder", 0 },
59 { "form formfile", 0 },
61 { "moreproc program", 0 },
65 { "length lines", 0 },
67 { "width columns", 0 },
69 { "sleep seconds", 0 },
71 { "dashstuffing", -12 }, /* interface from forw */
72 #define NBITSTUFFSW 12
73 { "nodashstuffing", -14 }, /* interface from forw */
79 { "forward", -7 }, /* interface from forw */
81 { "forwall", -7 }, /* interface from forw */
83 { "digest list", -6 },
85 { "volume number", -6 },
87 { "issue number", -5 },
91 { "fmtproc program", 0 },
97 #define NOCOMPONENT 0x000001 /* don't show component name */
98 #define UPPERCASE 0x000002 /* display in all upper case */
99 #define CENTER 0x000004 /* center line */
100 #define CLEARTEXT 0x000008 /* cleartext */
101 #define EXTRA 0x000010 /* an "extra" component */
102 #define HDROUTPUT 0x000020 /* already output */
103 #define CLEARSCR 0x000040 /* clear screen */
104 #define LEFTADJUST 0x000080 /* left justify multiple lines */
105 #define COMPRESS 0x000100 /* compress text */
106 #define ADDRFMT 0x000200 /* contains addresses */
107 #define BELL 0x000400 /* sound bell at EOP */
108 #define DATEFMT 0x000800 /* contains dates */
109 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
110 #define INIT 0x002000 /* initialize component */
111 #define SPLIT 0x010000 /* split headers (don't concatenate) */
112 #define NONEWLINE 0x020000 /* don't write trailing newline */
113 #define NOWRAP 0x040000 /* Don't wrap lines ever */
114 #define FMTFILTER 0x080000 /* Filter through format filter */
115 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
116 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
119 * A format string to be used as a command-line argument to the body
124 struct format
*a_fmt
;
126 struct arglist
*a_next
;
130 * Linked list of command line arguments for the body format filter. This
131 * USED to be in "struct mcomp", but the format API got cleaned up and even
132 * though it reduced the code we had to do, it make things more complicated
133 * for us. Specifically:
135 * - The interface to the hash table has been cleaned up, which means the
136 * rooting around in the hash table is no longer necessary (yay!). But
137 * this ALSO means that we have to make sure that we call our format
138 * compilation routines before we process the message, because the
139 * components need to be visible in the hash table so we can save them for
140 * later. So we moved them out of "mcomp" and now compile them right before
141 * header processing starts.
142 * - We also use format strings to handle other components in the mhl
143 * configuration (using "formatfield" and "decode"), but here life
144 * gets complicated: they aren't dealt with in the normal way. Instead
145 * of referring to a component like {from}, each component is processed
146 * using the special {text} component. But these format strings need to be
147 * compiled BEFORE we compile the format arguments; in the previous
148 * implementation they were compiled and scanned as the headers were
149 * read, and that would reset the hash table that we need to populate
150 * the components used by the body format filter. So we are compiling
151 * the formatfield component strings ahead of time and then scanning them
154 * Okay, fine ... this was broken before. But you know what? Fixing this
155 * the right way will make things easier down the road.
157 * One side-effect to this change: format strings are now compiled only once
158 * for components specified with "formatfield", but they are compiled for
159 * every message for format arguments.
162 static struct arglist
*arglist_head
;
163 static struct arglist
*arglist_tail
;
164 static int filter_nargs
= 0;
167 * Flags/options for each component
171 char *c_name
; /* component name */
172 char *c_text
; /* component text */
173 char *c_ovtxt
; /* text overflow indicator */
174 char *c_nfs
; /* iff FORMAT */
175 struct format
*c_fmt
; /* .. */
176 struct comp
*c_c_text
; /* Ref to {text} in FORMAT */
177 struct comp
*c_c_error
; /* Ref to {error} */
178 int c_offset
; /* left margin indentation */
179 int c_ovoff
; /* overflow indentation */
180 int c_width
; /* width of field */
181 int c_cwidth
; /* width of component */
182 int c_length
; /* length in lines */
184 struct mcomp
*c_next
;
187 static struct mcomp
*msghd
= NULL
;
188 static struct mcomp
*msgtl
= NULL
;
189 static struct mcomp
*fmthd
= NULL
;
190 static struct mcomp
*fmttl
= NULL
;
192 static struct mcomp global
= {
193 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
196 static struct mcomp holder
= {
197 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
205 static struct pair pairs
[] = {
208 { "Sender", ADDRFMT
},
209 { "Reply-To", ADDRFMT
},
213 { "Resent-Date", DATEFMT
},
214 { "Resent-From", ADDRFMT
},
215 { "Resent-Sender", ADDRFMT
},
216 { "Resent-Reply-To", ADDRFMT
},
217 { "Resent-To", ADDRFMT
},
218 { "Resent-cc", ADDRFMT
},
219 { "Resent-Bcc", ADDRFMT
},
229 static struct triple triples
[] = {
230 { "nocomponent", NOCOMPONENT
, 0 },
231 { "uppercase", UPPERCASE
, 0 },
232 { "nouppercase", 0, UPPERCASE
},
233 { "center", CENTER
, 0 },
234 { "nocenter", 0, CENTER
},
235 { "clearscreen", CLEARSCR
, 0 },
236 { "noclearscreen", 0, CLEARSCR
},
237 { "noclear", 0, CLEARSCR
},
238 { "leftadjust", LEFTADJUST
, 0 },
239 { "noleftadjust", 0, LEFTADJUST
},
240 { "compress", COMPRESS
, 0 },
241 { "nocompress", 0, COMPRESS
},
242 { "split", SPLIT
, 0 },
243 { "nosplit", 0, SPLIT
},
244 { "addrfield", ADDRFMT
, DATEFMT
},
246 { "nobell", 0, BELL
},
247 { "datefield", DATEFMT
, ADDRFMT
},
248 { "newline", 0, NONEWLINE
},
249 { "nonewline", NONEWLINE
, 0 },
250 { "wrap", 0, NOWRAP
},
251 { "nowrap", NOWRAP
, 0 },
252 { "format", FMTFILTER
, 0 },
253 { "noformat", 0, FMTFILTER
},
257 static char *addrcomps
[] = {
274 static int bellflg
= 0;
275 static int clearflg
= 0;
276 static int dashstuff
= 0;
277 static int dobody
= 1;
278 static int forwflg
= 0;
279 static int forwall
= 0;
281 static int sleepsw
= NOTOK
;
283 static char *digest
= NULL
;
284 static int volume
= 0;
285 static int issue
= 0;
287 static int exitstat
= 0;
288 static int mhldebug
= 0;
290 static int filesize
= 0;
295 static int ontty
= NOTTY
;
298 static unsigned int column
;
304 static unsigned int wid
;
308 static unsigned char *onelp
;
312 static int num_ignores
= 0;
313 static char *ignores
[MAXARGS
];
316 static jmp_buf mhlenv
;
318 static char delim3
[] = /* from forw.c */
319 "\n----------------------------------------------------------------------\n\n";
320 static char delim4
[] = "\n------------------------------\n\n";
322 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
325 * Redefine a couple of functions.
326 * These are undefined later in the code.
328 #define adios mhladios
334 static void mhl_format (char *, int, int);
335 static int evalvar (struct mcomp
*);
336 static int ptoi (char *, int *);
337 static int ptos (char *, char **);
338 static char *parse (void);
339 static void process (char *, char *, int, int);
340 static void mhlfile (FILE *, char *, int, int);
341 static int mcomp_flags (char *);
342 static char *mcomp_add (long, char *, char *);
343 static void mcomp_format (struct mcomp
*, struct mcomp
*);
344 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
345 static void free_queue (struct mcomp
**, struct mcomp
**);
346 static void putcomp (struct mcomp
*, struct mcomp
*, int);
347 static char *oneline (char *, long);
348 static void putstr (char *, long);
349 static void putch (char, long);
350 static void intrser (int);
351 static void pipeser (int);
352 static void quitser (int);
353 static void mhladios (char *, char *, ...);
354 static void mhldone (int);
355 static void m_popen (char *);
356 static void filterbody (struct mcomp
*, char *, int, int, FILE *);
357 static void compile_formatfield(struct mcomp
*);
358 static void compile_filterargs (void);
362 mhl (int argc
, char **argv
)
364 int length
= 0, nomore
= 0;
365 unsigned int i
, vecp
= 0;
367 char *cp
, *folder
= NULL
, *form
= NULL
;
368 char buf
[BUFSIZ
], *files
[MAXARGS
];
369 char **argp
, **arguments
;
371 invo_name
= r1bindex (argv
[0], '/');
373 /* read user profile/context */
376 arguments
= getarguments (invo_name
, argc
, argv
, 1);
379 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
382 while ((cp
= *argp
++)) {
384 switch (smatch (++cp
, mhlswitches
)) {
386 ambigsw (cp
, mhlswitches
);
389 adios (NULL
, "-%s unknown\n", cp
);
392 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
393 print_help (buf
, mhlswitches
, 1);
396 print_version(invo_name
);
414 if (!(folder
= *argp
++) || *folder
== '-')
415 adios (NULL
, "missing argument to %s", argp
[-2]);
418 if (!(form
= *argp
++) || *form
== '-')
419 adios (NULL
, "missing argument to %s", argp
[-2]);
423 if (!(cp
= *argp
++) || *cp
== '-')
424 adios (NULL
, "missing argument to %s", argp
[-2]);
425 sleepsw
= atoi (cp
);/* ZERO ok! */
429 if (!(moreproc
= *argp
++) || *moreproc
== '-')
430 adios (NULL
, "missing argument to %s", argp
[-2]);
437 if (!(formatproc
= *argp
++) || *formatproc
== '-')
438 adios (NULL
, "missing argument to %s", argp
[-2]);
445 if (!(cp
= *argp
++) || *cp
== '-')
446 adios (NULL
, "missing argument to %s", argp
[-2]);
447 if ((length
= atoi (cp
)) < 1)
448 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
451 if (!(cp
= *argp
++) || *cp
== '-')
452 adios (NULL
, "missing argument to %s", argp
[-2]);
453 if ((width
= atoi (cp
)) < 1)
454 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
458 if (!(digest
= *argp
++) || *digest
== '-')
459 adios (NULL
, "missing argument to %s", argp
[-2]);
462 if (!(cp
= *argp
++) || *cp
== '-')
463 adios (NULL
, "missing argument to %s", argp
[-2]);
464 if ((issue
= atoi (cp
)) < 1)
465 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
468 if (!(cp
= *argp
++) || *cp
== '-')
469 adios (NULL
, "missing argument to %s", argp
[-2]);
470 if ((volume
= atoi (cp
)) < 1)
471 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
475 forwall
++; /* fall */
478 clearflg
= -1;/* XXX */
482 dashstuff
= 1; /* trinary logic */
485 dashstuff
= -1; /* trinary logic */
497 folder
= getenv ("mhfolder");
499 if (isatty (fileno (stdout
))) {
500 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
502 SIGNAL (SIGINT
, SIG_IGN
);
503 SIGNAL2 (SIGQUIT
, quitser
);
505 SIGNAL2 (SIGPIPE
, pipeser
);
509 SIGNAL (SIGINT
, SIG_IGN
);
510 SIGNAL2 (SIGQUIT
, quitser
);
517 mhl_format (form
? form
: mhlformat
, length
, width
);
520 process (folder
, NULL
, 1, vecp
= 1);
522 for (i
= 0; i
< vecp
; i
++)
523 process (folder
, files
[i
], i
+ 1, vecp
);
528 printf ("%s", delim4
);
530 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
532 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
533 digest
, volume
, issue
);
536 for (cp
= buf
+ i
; i
> 1; i
--)
543 printf ("\n------- End of Forwarded Message%s\n",
544 vecp
> 1 ? "s" : "");
549 adios("output", "error writing");
552 if (clearflg
> 0 && ontty
== NOTTY
)
563 mhl_format (char *file
, int length
, int width
)
567 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
571 static dev_t dev
= 0;
572 static ino_t ino
= 0;
573 static time_t mtime
= 0;
576 if (stat (etcpath (file
), &st
) != NOTOK
577 && mtime
== st
.st_mtime
582 free_queue (&fmthd
, &fmttl
);
585 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
586 adios (file
, "unable to open format file");
588 if (fstat (fileno (fp
), &st
) != NOTOK
) {
594 global
.c_ovtxt
= global
.c_nfs
= NULL
;
598 if ((i
= sc_width ()) > 5)
600 global
.c_cwidth
= -1;
601 if ((i
= sc_length ()) > 5)
602 global
.c_length
= i
- 1;
603 global
.c_flags
= BELL
; /* BELL is default */
604 *(ip
= ignores
) = NULL
;
607 while (vfgets (fp
, &ap
) == OK
) {
612 if ((cp
= strchr(bp
, '\n')))
616 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
621 strncpy (name
, parse(), sizeof(name
));
627 * Split this list of fields to ignore, and copy
628 * it to the end of the current "ignores" list.
630 if (!mh_strcasecmp (name
, "ignores")) {
631 char **tmparray
, **p
;
634 /* split the fields */
635 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
637 /* count number of fields split */
642 /* copy pointers to split fields to ignores array */
643 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
649 if (evalvar (&global
))
650 adios (NULL
, "format file syntax error: %s", bp
);
657 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
658 while (*parptr
== ':' || *parptr
== ',') {
661 adios (NULL
, "format file syntax error: %s", bp
);
663 if (!c1
->c_nfs
&& global
.c_nfs
) {
664 if (c1
->c_flags
& DATEFMT
) {
665 if (global
.c_flags
& DATEFMT
) {
666 c1
->c_nfs
= getcpy (global
.c_nfs
);
667 compile_formatfield(c1
);
671 if (c1
->c_flags
& ADDRFMT
) {
672 if (global
.c_flags
& ADDRFMT
) {
673 c1
->c_nfs
= getcpy (global
.c_nfs
);
674 compile_formatfield(c1
);
681 adios (NULL
, "format file syntax error: %s", bp
);
687 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
688 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
689 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
690 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
691 (unsigned int)(unsigned long) c1
->c_nfs
,
692 (unsigned int)(unsigned long) c1
->c_fmt
);
693 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
694 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
695 c1
->c_cwidth
, c1
->c_length
);
696 fprintf (stderr
, "\tflags=%s\n",
697 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
703 global
.c_flags
|= CLEARSCR
;
706 global
.c_flags
&= ~CLEARSCR
;
709 switch (bellflg
) { /* command line may override format file */
711 global
.c_flags
|= BELL
;
714 global
.c_flags
&= ~BELL
;
719 global
.c_length
= length
;
721 global
.c_width
= width
;
722 if (global
.c_length
< 5)
723 global
.c_length
= 10000;
724 if (global
.c_width
< 5)
725 global
.c_width
= 10000;
730 evalvar (struct mcomp
*c1
)
732 char *cp
, name
[NAMESZ
];
737 strncpy (name
, parse(), sizeof(name
));
739 if (!mh_strcasecmp (name
, "component")) {
740 if (ptos (name
, &c1
->c_text
))
742 c1
->c_flags
&= ~NOCOMPONENT
;
746 if (!mh_strcasecmp (name
, "overflowtext"))
747 return ptos (name
, &c1
->c_ovtxt
);
749 if (!mh_strcasecmp (name
, "formatfield")) {
750 if (ptos (name
, &cp
))
752 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
753 compile_formatfield(c1
);
754 c1
->c_flags
|= FORMAT
;
758 if (!mh_strcasecmp (name
, "decode")) {
759 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
760 compile_formatfield(c1
);
761 c1
->c_flags
|= FORMAT
;
765 if (!mh_strcasecmp (name
, "offset"))
766 return ptoi (name
, &c1
->c_offset
);
767 if (!mh_strcasecmp (name
, "overflowoffset"))
768 return ptoi (name
, &c1
->c_ovoff
);
769 if (!mh_strcasecmp (name
, "width"))
770 return ptoi (name
, &c1
->c_width
);
771 if (!mh_strcasecmp (name
, "compwidth"))
772 return ptoi (name
, &c1
->c_cwidth
);
773 if (!mh_strcasecmp (name
, "length"))
774 return ptoi (name
, &c1
->c_length
);
775 if (!mh_strcasecmp (name
, "nodashstuffing"))
776 return (dashstuff
= -1);
778 for (ap
= triples
; ap
->t_name
; ap
++)
779 if (!mh_strcasecmp (ap
->t_name
, name
)) {
780 c1
->c_flags
|= ap
->t_on
;
781 c1
->c_flags
&= ~ap
->t_off
;
785 if (!mh_strcasecmp (name
, "formatarg")) {
786 struct arglist
*args
;
788 if (ptos (name
, &cp
))
791 if (mh_strcasecmp (c1
->c_name
, "body")) {
792 advise (NULL
, "format filters are currently only supported on "
793 "the \"body\" component");
797 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
800 arglist_tail
->a_next
= args
;
807 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
818 ptoi (char *name
, int *i
)
822 if (*parptr
++ != '=' || !*(cp
= parse ())) {
823 advise (NULL
, "missing argument to variable %s", name
);
833 ptos (char *name
, char **s
)
837 if (*parptr
++ != '=') {
838 advise (NULL
, "missing argument to variable %s", name
);
842 if (*parptr
!= '"') {
844 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
848 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
849 if (*parptr
== QUOTE
)
856 if ((*parptr
= c
) == '"')
867 static char result
[NAMESZ
];
869 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
888 * Process one file/message
892 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
900 switch (setjmp (env
)) {
903 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
905 advise (fname
, "unable to open");
913 if (fstat(fileno(fp
), &st
) == 0) {
914 filesize
= st
.st_size
;
918 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
920 SIGNAL (SIGINT
, intrser
);
921 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
923 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
924 fmt_free(ap
->a_fmt
, 0);
933 SIGNAL (SIGINT
, SIG_IGN
);
934 if (mhl_action
== NULL
&& fp
!= stdin
)
938 free (holder
.c_text
);
939 holder
.c_text
= NULL
;
941 free_queue (&msghd
, &msgtl
);
942 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
943 c1
->c_flags
&= ~HDROUTPUT
;
951 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
954 struct mcomp
*c1
, *c2
, *c3
;
955 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
957 compile_filterargs();
961 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
963 printf ("\n-------");
965 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
967 printf (" Message %d", ofilen
);
975 if ((global
.c_flags
& CLEARSCR
))
980 printf (">>> %s\n\n", mname
);
985 strncpy (buf
, "\n", sizeof(buf
));
987 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
990 printf ("Press <return> to list \"%s\"...", mname
);
994 read (fileno (stdout
), buf
, sizeof(buf
));
996 if (strchr(buf
, '\n')) {
997 if ((global
.c_flags
& CLEARSCR
))
1011 printf (">>> %s\n\n", mname
);
1017 for (state
= FLD
;;) {
1018 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
1021 bucket
= fmt_addcomptext(name
, buf
);
1022 for (ip
= ignores
; *ip
; ip
++)
1023 if (!mh_strcasecmp (name
, *ip
)) {
1024 while (state
== FLDPLUS
) {
1025 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
1026 fmt_appendcomp(bucket
, name
, buf
);
1033 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1034 if (!mh_strcasecmp (c2
->c_name
, name
))
1037 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1038 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1039 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
1041 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1045 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1046 while (state
== FLDPLUS
) {
1047 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
1048 c1
->c_text
= add (buf
, c1
->c_text
);
1049 fmt_appendcomp(bucket
, name
, buf
);
1052 c1
->c_flags
|= EXTRA
;
1058 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1059 if (c1
->c_flags
& CLEARTEXT
) {
1060 putcomp (c1
, c1
, ONECOMP
);
1063 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
1064 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1066 putcomp (c1
, &holder
, ONECOMP
);
1067 free (holder
.c_text
);
1068 holder
.c_text
= NULL
;
1071 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
1072 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1073 if (c2
->c_flags
& EXTRA
)
1074 putcomp (c1
, c2
, TWOCOMP
);
1077 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
1078 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1079 formatproc
!= NULL
) {
1080 filterbody(c1
, buf
, sizeof(buf
), state
, fp
);
1082 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1083 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1084 while (state
== BODY
) {
1085 putcomp (c1
, &holder
, BODYCOMP
);
1086 state
= m_getfld (state
, name
, holder
.c_text
,
1089 free (holder
.c_text
);
1090 holder
.c_text
= NULL
;
1094 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1095 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
1096 putcomp (c1
, c2
, ONECOMP
);
1097 if (!(c1
->c_flags
& SPLIT
))
1105 advise (NULL
, "format error in message %s", mname
);
1110 adios (NULL
, "getfld() returned %d", state
);
1117 mcomp_flags (char *name
)
1121 for (ap
= pairs
; ap
->p_name
; ap
++)
1122 if (!mh_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
)
1156 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1157 struct pqpair
*p
, *q
;
1159 struct mailname
*mp
;
1166 dat
[3] = sizeof(buffer
) - 1;
1169 if (!(c1
->c_flags
& ADDRFMT
)) {
1171 c1
->c_c_text
->c_text
= ap
;
1172 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1176 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1177 /* Don't need to append a newline, dctime() already did */
1178 c2
->c_text
= getcpy (buffer
);
1180 /* ap is now owned by the component struct, so do NOT free it here */
1184 (q
= &pq
)->pq_next
= NULL
;
1185 while ((cp
= getname (ap
))) {
1186 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1187 adios (NULL
, "unable to allocate pqpair memory");
1189 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1190 p
->pq_text
= getcpy (cp
);
1191 p
->pq_error
= getcpy (error
);
1193 p
->pq_text
= getcpy (mp
->m_text
);
1196 q
= (q
->pq_next
= p
);
1199 for (p
= pq
.pq_next
; p
; p
= q
) {
1201 c1
->c_c_text
->c_text
= p
->pq_text
;
1204 if (c1
->c_c_error
) {
1205 c1
->c_c_error
->c_text
= p
->pq_error
;
1209 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1212 c2
->c_text
= add (",\n", c2
->c_text
);
1213 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1215 c2
->c_text
= add (buffer
, c2
->c_text
);
1226 c2
->c_text
= add ("\n", c2
->c_text
);
1231 static struct mcomp
*
1232 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1236 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1237 adios (NULL
, "unable to allocate comp memory");
1239 c1
->c_flags
= flags
& ~INIT
;
1240 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1241 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1242 c1
->c_text
= text
? getcpy (text
) : NULL
;
1245 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1246 c1
->c_offset
= global
.c_offset
;
1247 c1
->c_ovoff
= global
. c_ovoff
;
1248 c1
->c_width
= c1
->c_length
= 0;
1249 c1
->c_cwidth
= global
.c_cwidth
;
1250 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1255 (*tail
)->c_next
= c1
;
1263 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1265 struct mcomp
*c1
, *c2
;
1267 for (c1
= *head
; c1
; c1
= c2
) {
1278 fmt_free (c1
->c_fmt
, 0);
1282 *head
= *tail
= NULL
;
1287 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1294 llim
= c1
->c_length
? c1
->c_length
: -1;
1295 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1296 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1298 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1300 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1301 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1302 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1305 if (c1
->c_flags
& CLEARTEXT
) {
1306 putstr (c1
->c_text
, c1
->c_flags
);
1307 putstr ("\n", c1
->c_flags
);
1311 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1312 mcomp_format (c1
, c2
);
1314 if (c1
->c_flags
& CENTER
) {
1315 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1316 - c1
->c_offset
- strlen (c2
->c_text
);
1317 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1318 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1319 lm
= c1
->c_offset
+ (count
/ 2);
1325 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1326 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1327 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1329 *cp
= toupper (*cp
);
1330 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1331 if (flag
!= BODYCOMP
) {
1332 putstr (": ", c1
->c_flags
);
1333 if (!(c1
->c_flags
& SPLIT
))
1334 c1
->c_flags
|= HDROUTPUT
;
1337 if ((count
= c1
->c_cwidth
-
1338 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1340 putstr (" ", c1
->c_flags
);
1343 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1347 && !(c2
->c_flags
& HDROUTPUT
)
1348 && !(c2
->c_flags
& NOCOMPONENT
)) {
1349 if (c1
->c_flags
& UPPERCASE
)
1350 for (cp
= c2
->c_name
; *cp
; cp
++)
1352 *cp
= toupper (*cp
);
1353 putstr (c2
->c_name
, c1
->c_flags
);
1354 putstr (": ", c1
->c_flags
);
1355 if (!(c1
->c_flags
& SPLIT
))
1356 c2
->c_flags
|= HDROUTPUT
;
1359 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1361 putstr (" ", c1
->c_flags
);
1363 if (c1
->c_flags
& UPPERCASE
)
1364 for (cp
= c2
->c_text
; *cp
; cp
++)
1366 *cp
= toupper (*cp
);
1370 if (flag
== TWOCOMP
)
1371 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1372 : (int) strlen (c2
->c_name
) + 2;
1374 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1375 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1377 count
+= c1
->c_offset
;
1379 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1380 putstr(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 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1389 putstr (cp
, c1
->c_flags
);
1391 putstr ("\n", c1
->c_flags
);
1393 if (flag
== BODYCOMP
&& term
== '\n')
1394 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1399 oneline (char *stuff
, long flags
)
1407 return (onelp
= NULL
);
1411 if (flags
& COMPRESS
) {
1412 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1413 if (isspace (*onelp
)) {
1414 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1433 while (*onelp
&& *onelp
!= '\n')
1435 if (*onelp
== '\n') {
1439 if (flags
& LEFTADJUST
)
1440 while (*ret
== ' ' || *ret
== '\t')
1443 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1451 putstr (char *string
, long flags
)
1453 if (!column
&& lm
> 0) {
1456 putch ('\t', flags
);
1466 putch (*string
++, flags
);
1471 putch (char ch
, long flags
)
1484 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1486 if (global
.c_flags
& BELL
)
1490 read (fileno (stdout
), buf
, sizeof(buf
));
1491 if (strchr(buf
, '\n')) {
1492 if (global
.c_flags
& CLEARSCR
)
1497 row
= global
.c_length
/ 3;
1516 * If we are forwarding this message, and the first
1517 * column contains a dash, then add a dash and a space.
1519 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1528 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1529 putch ('\n', flags
);
1532 putstr (ovtxt
? ovtxt
: "", flags
);
1548 longjmp (env
, DONE
);
1573 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1575 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1578 struct arglist
*a
, *a2
;
1580 switch (setjmp (mhlenv
)) {
1583 sleepsw
= 0; /* XXX */
1584 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1587 mhl_action
= action
;
1590 * If signal is at default action, then start ignoring
1591 * it, else let it set to its current action.
1593 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1594 SIGNAL (SIGINT
, istat
);
1595 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1596 SIGNAL (SIGQUIT
, qstat
);
1597 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1598 mhl (argc
, argv
); /* FALL THROUGH! */
1601 SIGNAL (SIGINT
, istat
);
1602 SIGNAL (SIGQUIT
, qstat
);
1603 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1606 SIGNAL (SIGPIPE
, pstat
);
1608 if (holder
.c_text
) {
1609 free (holder
.c_text
);
1610 holder
.c_text
= NULL
;
1612 free_queue (&msghd
, &msgtl
);
1613 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1614 c1
->c_flags
&= ~HDROUTPUT
;
1632 mhladios (char *what
, char *fmt
, ...)
1637 advertise (what
, NULL
, fmt
, ap
);
1644 mhldone (int status
)
1648 longjmp (mhlenv
, DONE
);
1654 static int m_pid
= NOTOK
;
1655 static int sd
= NOTOK
;
1658 m_popen (char *name
)
1662 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1663 adios ("standard output", "unable to dup()");
1665 if (pipe (pd
) == NOTOK
)
1666 adios ("pipe", "unable to");
1668 switch (m_pid
= vfork()) {
1670 adios ("fork", "unable to");
1673 SIGNAL (SIGINT
, SIG_DFL
);
1674 SIGNAL (SIGQUIT
, SIG_DFL
);
1677 if (pd
[0] != fileno (stdin
)) {
1678 dup2 (pd
[0], fileno (stdin
));
1681 execlp (name
, r1bindex (name
, '/'), NULL
);
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
)
1789 struct mcomp holder
;
1791 int fdinput
[2], fdoutput
[2], waitstat
;
1793 pid_t writerpid
, filterpid
;
1796 * Create pipes so we can communicate with our filter process.
1799 if (pipe(fdinput
) < 0) {
1800 adios(NULL
, "Unable to create input pipe");
1803 if (pipe(fdoutput
) < 0) {
1804 adios(NULL
, "Unable to create output pipe");
1808 * Here's what we're doing to do.
1810 * - Fork ourselves and start writing data to the write side of the
1811 * input pipe (fdinput[1]).
1813 * - Fork and exec our filter program. We set the standard input of
1814 * our filter program to be the read side of our input pipe (fdinput[0]).
1815 * Standard output is set to the write side of our output pipe
1818 * - We read from the read side of the output pipe (fdoutput[0]).
1820 * We're forking because that's the simplest way to prevent any deadlocks.
1821 * (without doing something like switching to non-blocking I/O and using
1822 * select or poll, and I'm not interested in doing that).
1825 switch (writerpid
= fork()) {
1828 * Our child process - just write to the filter input (fdinput[1]).
1829 * Close all other descriptors that we don't need.
1837 * Call m_getfld() until we're no longer in the BODY state
1840 while (state
== BODY
) {
1841 write(fdinput
[1], buf
, strlen(buf
));
1842 state
= m_getfld(state
, name
, buf
, bufsz
, fp
);
1846 * We should be done; time to exit.
1851 * Make sure we call _exit(), otherwise we may flush out the stdio
1852 * buffers that we have duplicated from the parent.
1857 adios(NULL
, "Unable to fork for filter writer process");
1862 * Fork and exec() our filter program, after redirecting standard in
1863 * and standard out appropriately.
1866 switch (filterpid
= fork()) {
1873 * Allocate an argument array for us
1876 args
= (char **) mh_xmalloc((filter_nargs
+ 2) * sizeof(char *));
1877 args
[0] = formatproc
;
1878 args
[filter_nargs
+ 1] = NULL
;
1886 * Pull out each argument and scan them.
1889 for (a
= arglist_head
, i
= 1; a
!= NULL
; a
= a
->a_next
, i
++) {
1890 args
[i
] = mh_xmalloc(BUFSIZ
);
1891 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
- 1, BUFSIZ
, dat
);
1893 * fmt_scan likes to put a trailing newline at the end of the
1894 * format string. If we have one, get rid of it.
1896 s
= strlen(args
[i
]);
1897 if (args
[i
][s
- 1] == '\n')
1898 args
[i
][s
- 1] = '\0';
1901 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1905 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1906 adios("formatproc", "Unable to dup2() standard input");
1908 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1909 adios("formatproc", "Unable to dup2() standard output");
1913 * Close everything (especially the old input and output
1914 * descriptors, since they've been dup'd to stdin and stdout),
1915 * and exec the formatproc.
1923 execvp(formatproc
, args
);
1925 adios(formatproc
, "Unable to execute filter");
1930 adios(NULL
, "Unable to fork format program");
1934 * Close everything except our reader (fdoutput[0]);
1942 * As we read in this data, send it to putcomp
1945 holder
.c_text
= buf
;
1947 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1949 putcomp(c1
, &holder
, BODYCOMP
);
1953 adios(NULL
, "reading from formatproc");
1957 * See if we got any errors along the way. I'm a little leery of calling
1958 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1961 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1962 if (errno
!= ECHILD
) {
1963 adios("filterproc", "Unable to determine status");
1966 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1967 pidstatus(waitstat
, stderr
, "filterproc");
1971 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1972 if (errno
!= ECHILD
) {
1973 adios("writer process", "Unable to determine status");
1977 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1978 pidstatus(waitstat
, stderr
, "writer process");