]>
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 *,
358 static void compile_formatfield(struct mcomp
*);
359 static void compile_filterargs (void);
363 mhl (int argc
, char **argv
)
365 int length
= 0, nomore
= 0;
366 unsigned int i
, vecp
= 0;
368 char *cp
, *folder
= NULL
, *form
= NULL
;
369 char buf
[BUFSIZ
], *files
[MAXARGS
];
370 char **argp
, **arguments
;
372 invo_name
= r1bindex (argv
[0], '/');
374 /* read user profile/context */
377 arguments
= getarguments (invo_name
, argc
, argv
, 1);
380 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
383 while ((cp
= *argp
++)) {
385 switch (smatch (++cp
, mhlswitches
)) {
387 ambigsw (cp
, mhlswitches
);
390 adios (NULL
, "-%s unknown\n", cp
);
393 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
394 print_help (buf
, mhlswitches
, 1);
397 print_version(invo_name
);
415 if (!(folder
= *argp
++) || *folder
== '-')
416 adios (NULL
, "missing argument to %s", argp
[-2]);
419 if (!(form
= *argp
++) || *form
== '-')
420 adios (NULL
, "missing argument to %s", argp
[-2]);
424 if (!(cp
= *argp
++) || *cp
== '-')
425 adios (NULL
, "missing argument to %s", argp
[-2]);
426 sleepsw
= atoi (cp
);/* ZERO ok! */
430 if (!(moreproc
= *argp
++) || *moreproc
== '-')
431 adios (NULL
, "missing argument to %s", argp
[-2]);
438 if (!(formatproc
= *argp
++) || *formatproc
== '-')
439 adios (NULL
, "missing argument to %s", argp
[-2]);
446 if (!(cp
= *argp
++) || *cp
== '-')
447 adios (NULL
, "missing argument to %s", argp
[-2]);
448 if ((length
= 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 ((width
= atoi (cp
)) < 1)
455 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
459 if (!(digest
= *argp
++) || *digest
== '-')
460 adios (NULL
, "missing argument to %s", argp
[-2]);
463 if (!(cp
= *argp
++) || *cp
== '-')
464 adios (NULL
, "missing argument to %s", argp
[-2]);
465 if ((issue
= atoi (cp
)) < 1)
466 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
469 if (!(cp
= *argp
++) || *cp
== '-')
470 adios (NULL
, "missing argument to %s", argp
[-2]);
471 if ((volume
= atoi (cp
)) < 1)
472 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
476 forwall
++; /* fall */
479 clearflg
= -1;/* XXX */
483 dashstuff
= 1; /* trinary logic */
486 dashstuff
= -1; /* trinary logic */
498 folder
= getenv ("mhfolder");
500 if (isatty (fileno (stdout
))) {
501 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
503 SIGNAL (SIGINT
, SIG_IGN
);
504 SIGNAL2 (SIGQUIT
, quitser
);
506 SIGNAL2 (SIGPIPE
, pipeser
);
510 SIGNAL (SIGINT
, SIG_IGN
);
511 SIGNAL2 (SIGQUIT
, quitser
);
518 mhl_format (form
? form
: mhlformat
, length
, width
);
521 process (folder
, NULL
, 1, vecp
= 1);
523 for (i
= 0; i
< vecp
; i
++)
524 process (folder
, files
[i
], i
+ 1, vecp
);
529 printf ("%s", delim4
);
531 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
533 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
534 digest
, volume
, issue
);
537 for (cp
= buf
+ i
; i
> 1; i
--)
544 printf ("\n------- End of Forwarded Message%s\n",
545 vecp
> 1 ? "s" : "");
550 adios("output", "error writing");
553 if (clearflg
> 0 && ontty
== NOTTY
)
564 mhl_format (char *file
, int length
, int width
)
568 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
572 static dev_t dev
= 0;
573 static ino_t ino
= 0;
574 static time_t mtime
= 0;
577 if (stat (etcpath (file
), &st
) != NOTOK
578 && mtime
== st
.st_mtime
583 free_queue (&fmthd
, &fmttl
);
586 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
587 adios (file
, "unable to open format file");
589 if (fstat (fileno (fp
), &st
) != NOTOK
) {
595 global
.c_ovtxt
= global
.c_nfs
= NULL
;
599 if ((i
= sc_width ()) > 5)
601 global
.c_cwidth
= -1;
602 if ((i
= sc_length ()) > 5)
603 global
.c_length
= i
- 1;
604 global
.c_flags
= BELL
; /* BELL is default */
605 *(ip
= ignores
) = NULL
;
608 while (vfgets (fp
, &ap
) == OK
) {
613 if ((cp
= strchr(bp
, '\n')))
617 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
622 strncpy (name
, parse(), sizeof(name
));
628 * Split this list of fields to ignore, and copy
629 * it to the end of the current "ignores" list.
631 if (!mh_strcasecmp (name
, "ignores")) {
632 char **tmparray
, **p
;
635 /* split the fields */
636 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
638 /* count number of fields split */
643 /* copy pointers to split fields to ignores array */
644 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
650 if (evalvar (&global
))
651 adios (NULL
, "format file syntax error: %s", bp
);
658 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
659 while (*parptr
== ':' || *parptr
== ',') {
662 adios (NULL
, "format file syntax error: %s", bp
);
664 if (!c1
->c_nfs
&& global
.c_nfs
) {
665 if (c1
->c_flags
& DATEFMT
) {
666 if (global
.c_flags
& DATEFMT
) {
667 c1
->c_nfs
= getcpy (global
.c_nfs
);
668 compile_formatfield(c1
);
672 if (c1
->c_flags
& ADDRFMT
) {
673 if (global
.c_flags
& ADDRFMT
) {
674 c1
->c_nfs
= getcpy (global
.c_nfs
);
675 compile_formatfield(c1
);
682 adios (NULL
, "format file syntax error: %s", bp
);
688 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
689 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
690 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
691 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
692 (unsigned int)(unsigned long) c1
->c_nfs
,
693 (unsigned int)(unsigned long) c1
->c_fmt
);
694 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
695 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
696 c1
->c_cwidth
, c1
->c_length
);
697 fprintf (stderr
, "\tflags=%s\n",
698 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
704 global
.c_flags
|= CLEARSCR
;
707 global
.c_flags
&= ~CLEARSCR
;
710 switch (bellflg
) { /* command line may override format file */
712 global
.c_flags
|= BELL
;
715 global
.c_flags
&= ~BELL
;
720 global
.c_length
= length
;
722 global
.c_width
= width
;
723 if (global
.c_length
< 5)
724 global
.c_length
= 10000;
725 if (global
.c_width
< 5)
726 global
.c_width
= 10000;
731 evalvar (struct mcomp
*c1
)
733 char *cp
, name
[NAMESZ
];
738 strncpy (name
, parse(), sizeof(name
));
740 if (!mh_strcasecmp (name
, "component")) {
741 if (ptos (name
, &c1
->c_text
))
743 c1
->c_flags
&= ~NOCOMPONENT
;
747 if (!mh_strcasecmp (name
, "overflowtext"))
748 return ptos (name
, &c1
->c_ovtxt
);
750 if (!mh_strcasecmp (name
, "formatfield")) {
751 if (ptos (name
, &cp
))
753 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
754 compile_formatfield(c1
);
755 c1
->c_flags
|= FORMAT
;
759 if (!mh_strcasecmp (name
, "decode")) {
760 c1
->c_nfs
= getcpy (new_fs (NULL
, NULL
, "%(decode{text})"));
761 compile_formatfield(c1
);
762 c1
->c_flags
|= FORMAT
;
766 if (!mh_strcasecmp (name
, "offset"))
767 return ptoi (name
, &c1
->c_offset
);
768 if (!mh_strcasecmp (name
, "overflowoffset"))
769 return ptoi (name
, &c1
->c_ovoff
);
770 if (!mh_strcasecmp (name
, "width"))
771 return ptoi (name
, &c1
->c_width
);
772 if (!mh_strcasecmp (name
, "compwidth"))
773 return ptoi (name
, &c1
->c_cwidth
);
774 if (!mh_strcasecmp (name
, "length"))
775 return ptoi (name
, &c1
->c_length
);
776 if (!mh_strcasecmp (name
, "nodashstuffing"))
777 return (dashstuff
= -1);
779 for (ap
= triples
; ap
->t_name
; ap
++)
780 if (!mh_strcasecmp (ap
->t_name
, name
)) {
781 c1
->c_flags
|= ap
->t_on
;
782 c1
->c_flags
&= ~ap
->t_off
;
786 if (!mh_strcasecmp (name
, "formatarg")) {
787 struct arglist
*args
;
789 if (ptos (name
, &cp
))
792 if (mh_strcasecmp (c1
->c_name
, "body")) {
793 advise (NULL
, "format filters are currently only supported on "
794 "the \"body\" component");
798 args
= (struct arglist
*) calloc((size_t) 1, sizeof(struct arglist
));
801 arglist_tail
->a_next
= args
;
808 args
->a_nfs
= getcpy (new_fs (NULL
, NULL
, cp
));
819 ptoi (char *name
, int *i
)
823 if (*parptr
++ != '=' || !*(cp
= parse ())) {
824 advise (NULL
, "missing argument to variable %s", name
);
834 ptos (char *name
, char **s
)
838 if (*parptr
++ != '=') {
839 advise (NULL
, "missing argument to variable %s", name
);
843 if (*parptr
!= '"') {
845 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
849 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
850 if (*parptr
== QUOTE
)
857 if ((*parptr
= c
) == '"')
868 static char result
[NAMESZ
];
870 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
889 * Process one file/message
893 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
901 switch (setjmp (env
)) {
904 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
906 advise (fname
, "unable to open");
914 if (fstat(fileno(fp
), &st
) == 0) {
915 filesize
= st
.st_size
;
919 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
921 SIGNAL (SIGINT
, intrser
);
922 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
924 for (ap
= arglist_head
; ap
; ap
= ap
->a_next
) {
925 fmt_free(ap
->a_fmt
, 0);
934 SIGNAL (SIGINT
, SIG_IGN
);
935 if (mhl_action
== NULL
&& fp
!= stdin
)
939 free (holder
.c_text
);
940 holder
.c_text
= NULL
;
942 free_queue (&msghd
, &msgtl
);
943 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
944 c1
->c_flags
&= ~HDROUTPUT
;
952 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
955 struct mcomp
*c1
, *c2
, *c3
;
956 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
957 m_getfld_state_t gstate
;
959 compile_filterargs();
963 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
965 printf ("\n-------");
967 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
969 printf (" Message %d", ofilen
);
977 if ((global
.c_flags
& CLEARSCR
))
982 printf (">>> %s\n\n", mname
);
987 strncpy (buf
, "\n", sizeof(buf
));
989 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
992 printf ("Press <return> to list \"%s\"...", mname
);
996 read (fileno (stdout
), buf
, sizeof(buf
));
998 if (strchr(buf
, '\n')) {
999 if ((global
.c_flags
& CLEARSCR
))
1013 printf (">>> %s\n\n", mname
);
1019 m_getfld_state_init (&gstate
);
1021 int bufsz
= sizeof buf
;
1022 switch (state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
)) {
1025 bucket
= fmt_addcomptext(name
, buf
);
1026 for (ip
= ignores
; *ip
; ip
++)
1027 if (!mh_strcasecmp (name
, *ip
)) {
1028 while (state
== FLDPLUS
) {
1030 state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
);
1031 fmt_appendcomp(bucket
, name
, buf
);
1038 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
1039 if (!mh_strcasecmp (c2
->c_name
, name
))
1042 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
1043 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
1044 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
1046 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
1050 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
1051 while (state
== FLDPLUS
) {
1053 state
= m_getfld (gstate
, name
, buf
, &bufsz
, fp
);
1054 c1
->c_text
= add (buf
, c1
->c_text
);
1055 fmt_appendcomp(bucket
, name
, buf
);
1058 c1
->c_flags
|= EXTRA
;
1064 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1065 if (c1
->c_flags
& CLEARTEXT
) {
1066 putcomp (c1
, c1
, ONECOMP
);
1069 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
1070 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1072 putcomp (c1
, &holder
, ONECOMP
);
1073 free (holder
.c_text
);
1074 holder
.c_text
= NULL
;
1077 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
1078 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1079 if (c2
->c_flags
& EXTRA
)
1080 putcomp (c1
, c2
, TWOCOMP
);
1083 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
1084 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1085 formatproc
!= NULL
) {
1086 filterbody(c1
, buf
, sizeof(buf
), state
, fp
, gstate
);
1088 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1089 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1090 while (state
== BODY
) {
1091 putcomp (c1
, &holder
, BODYCOMP
);
1093 state
= m_getfld (gstate
, name
, holder
.c_text
,
1096 free (holder
.c_text
);
1097 holder
.c_text
= NULL
;
1101 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1102 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
1103 putcomp (c1
, c2
, ONECOMP
);
1104 if (!(c1
->c_flags
& SPLIT
))
1112 advise (NULL
, "format error in message %s", mname
);
1117 adios (NULL
, "getfld() returned %d", state
);
1120 m_getfld_state_destroy (&gstate
);
1125 mcomp_flags (char *name
)
1129 for (ap
= pairs
; ap
->p_name
; ap
++)
1130 if (!mh_strcasecmp (ap
->p_name
, name
))
1131 return (ap
->p_flags
);
1138 mcomp_add (long flags
, char *s1
, char *s2
)
1142 if (!(flags
& ADDRFMT
))
1143 return add (s1
, s2
);
1145 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1148 return add (s1
, add (",\n", s2
));
1155 struct pqpair
*pq_next
;
1160 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1164 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1165 struct pqpair
*p
, *q
;
1167 struct mailname
*mp
;
1174 dat
[3] = sizeof(buffer
) - 1;
1177 if (!(c1
->c_flags
& ADDRFMT
)) {
1179 c1
->c_c_text
->c_text
= ap
;
1180 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1184 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1185 /* Don't need to append a newline, dctime() already did */
1186 c2
->c_text
= getcpy (buffer
);
1188 /* ap is now owned by the component struct, so do NOT free it here */
1192 (q
= &pq
)->pq_next
= NULL
;
1193 while ((cp
= getname (ap
))) {
1194 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1195 adios (NULL
, "unable to allocate pqpair memory");
1197 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1198 p
->pq_text
= getcpy (cp
);
1199 p
->pq_error
= getcpy (error
);
1201 p
->pq_text
= getcpy (mp
->m_text
);
1204 q
= (q
->pq_next
= p
);
1207 for (p
= pq
.pq_next
; p
; p
= q
) {
1209 c1
->c_c_text
->c_text
= p
->pq_text
;
1212 if (c1
->c_c_error
) {
1213 c1
->c_c_error
->c_text
= p
->pq_error
;
1217 fmt_scan (c1
->c_fmt
, buffer
, sizeof buffer
- 1, sizeof buffer
- 1, dat
);
1220 c2
->c_text
= add (",\n", c2
->c_text
);
1221 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1223 c2
->c_text
= add (buffer
, c2
->c_text
);
1234 c2
->c_text
= add ("\n", c2
->c_text
);
1239 static struct mcomp
*
1240 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1244 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1245 adios (NULL
, "unable to allocate comp memory");
1247 c1
->c_flags
= flags
& ~INIT
;
1248 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1249 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1250 c1
->c_text
= text
? getcpy (text
) : NULL
;
1253 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1254 c1
->c_offset
= global
.c_offset
;
1255 c1
->c_ovoff
= global
. c_ovoff
;
1256 c1
->c_width
= c1
->c_length
= 0;
1257 c1
->c_cwidth
= global
.c_cwidth
;
1258 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1263 (*tail
)->c_next
= c1
;
1271 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1273 struct mcomp
*c1
, *c2
;
1275 for (c1
= *head
; c1
; c1
= c2
) {
1286 fmt_free (c1
->c_fmt
, 0);
1290 *head
= *tail
= NULL
;
1295 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1302 llim
= c1
->c_length
? c1
->c_length
: -1;
1303 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1304 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1306 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1308 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1309 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1310 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1313 if (c1
->c_flags
& CLEARTEXT
) {
1314 putstr (c1
->c_text
, c1
->c_flags
);
1315 putstr ("\n", c1
->c_flags
);
1319 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1320 mcomp_format (c1
, c2
);
1322 if (c1
->c_flags
& CENTER
) {
1323 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1324 - c1
->c_offset
- strlen (c2
->c_text
);
1325 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1326 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1327 lm
= c1
->c_offset
+ (count
/ 2);
1333 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1334 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1335 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1337 *cp
= toupper (*cp
);
1338 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1339 if (flag
!= BODYCOMP
) {
1340 putstr (": ", c1
->c_flags
);
1341 if (!(c1
->c_flags
& SPLIT
))
1342 c1
->c_flags
|= HDROUTPUT
;
1345 if ((count
= c1
->c_cwidth
-
1346 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1348 putstr (" ", c1
->c_flags
);
1351 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1355 && !(c2
->c_flags
& HDROUTPUT
)
1356 && !(c2
->c_flags
& NOCOMPONENT
)) {
1357 if (c1
->c_flags
& UPPERCASE
)
1358 for (cp
= c2
->c_name
; *cp
; cp
++)
1360 *cp
= toupper (*cp
);
1361 putstr (c2
->c_name
, c1
->c_flags
);
1362 putstr (": ", c1
->c_flags
);
1363 if (!(c1
->c_flags
& SPLIT
))
1364 c2
->c_flags
|= HDROUTPUT
;
1367 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1369 putstr (" ", c1
->c_flags
);
1371 if (c1
->c_flags
& UPPERCASE
)
1372 for (cp
= c2
->c_text
; *cp
; cp
++)
1374 *cp
= toupper (*cp
);
1378 if (flag
== TWOCOMP
)
1379 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1380 : (int) strlen (c2
->c_name
) + 2;
1382 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1383 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1385 count
+= c1
->c_offset
;
1387 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1388 putstr(cp
, c1
->c_flags
);
1390 putstr ("\n", c1
->c_flags
);
1391 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1393 if (flag
== BODYCOMP
1394 && !(c1
->c_flags
& NOCOMPONENT
))
1395 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1397 putstr (cp
, c1
->c_flags
);
1399 putstr ("\n", c1
->c_flags
);
1401 if (flag
== BODYCOMP
&& term
== '\n')
1402 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1407 oneline (char *stuff
, long flags
)
1415 return (onelp
= NULL
);
1419 if (flags
& COMPRESS
) {
1420 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1421 if (isspace (*onelp
)) {
1422 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1441 while (*onelp
&& *onelp
!= '\n')
1443 if (*onelp
== '\n') {
1447 if (flags
& LEFTADJUST
)
1448 while (*ret
== ' ' || *ret
== '\t')
1451 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1459 putstr (char *string
, long flags
)
1461 if (!column
&& lm
> 0) {
1464 putch ('\t', flags
);
1474 putch (*string
++, flags
);
1479 putch (char ch
, long flags
)
1492 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1494 if (global
.c_flags
& BELL
)
1498 read (fileno (stdout
), buf
, sizeof(buf
));
1499 if (strchr(buf
, '\n')) {
1500 if (global
.c_flags
& CLEARSCR
)
1505 row
= global
.c_length
/ 3;
1524 * If we are forwarding this message, and the first
1525 * column contains a dash, then add a dash and a space.
1527 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1536 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1537 putch ('\n', flags
);
1540 putstr (ovtxt
? ovtxt
: "", flags
);
1556 longjmp (env
, DONE
);
1581 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1583 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1586 struct arglist
*a
, *a2
;
1588 switch (setjmp (mhlenv
)) {
1591 sleepsw
= 0; /* XXX */
1592 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1595 mhl_action
= action
;
1598 * If signal is at default action, then start ignoring
1599 * it, else let it set to its current action.
1601 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1602 SIGNAL (SIGINT
, istat
);
1603 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1604 SIGNAL (SIGQUIT
, qstat
);
1605 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1606 mhl (argc
, argv
); /* FALL THROUGH! */
1609 SIGNAL (SIGINT
, istat
);
1610 SIGNAL (SIGQUIT
, qstat
);
1611 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1614 SIGNAL (SIGPIPE
, pstat
);
1616 if (holder
.c_text
) {
1617 free (holder
.c_text
);
1618 holder
.c_text
= NULL
;
1620 free_queue (&msghd
, &msgtl
);
1621 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1622 c1
->c_flags
&= ~HDROUTPUT
;
1640 mhladios (char *what
, char *fmt
, ...)
1645 advertise (what
, NULL
, fmt
, ap
);
1652 mhldone (int status
)
1656 longjmp (mhlenv
, DONE
);
1662 static int m_pid
= NOTOK
;
1663 static int sd
= NOTOK
;
1666 m_popen (char *name
)
1670 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1671 adios ("standard output", "unable to dup()");
1673 if (pipe (pd
) == NOTOK
)
1674 adios ("pipe", "unable to");
1676 switch (m_pid
= vfork()) {
1678 adios ("fork", "unable to");
1681 SIGNAL (SIGINT
, SIG_DFL
);
1682 SIGNAL (SIGQUIT
, SIG_DFL
);
1685 if (pd
[0] != fileno (stdin
)) {
1686 dup2 (pd
[0], fileno (stdin
));
1689 execlp (name
, r1bindex (name
, '/'), NULL
);
1690 fprintf (stderr
, "unable to exec ");
1696 if (pd
[1] != fileno (stdout
)) {
1697 dup2 (pd
[1], fileno (stdout
));
1712 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1713 adios ("standard output", "unable to dup2()");
1722 pidwait (m_pid
, OK
);
1728 * Compile a format string used by the formatfield option and save it
1731 * We will want the {text} (and possibly {error}) components for later,
1732 * so look for them and save them if we find them.
1736 compile_formatfield(struct mcomp
*c1
)
1738 fmt_compile(c1
->c_nfs
, &c1
->c_fmt
, 1);
1741 * As a note to myself and any other poor bastard who is looking through
1742 * this code in the future ....
1744 * When the format hash table is reset later on (as it almost certainly
1745 * will be), there will still be references to these components in the
1746 * compiled format instructions. Thus these component references will
1747 * be free'd when the format instructions are free'd (by fmt_free()).
1749 * So, in other words ... don't go free'ing them yourself!
1752 c1
->c_c_text
= fmt_findcomp("text");
1753 c1
->c_c_error
= fmt_findcomp("error");
1757 * Compile all of the arguments for our format list.
1759 * Iterate through the linked list of format strings and compile them.
1760 * Note that we reset the format hash table before we start, but we do NOT
1761 * reset it between calls to fmt_compile().
1766 compile_filterargs (void)
1768 struct arglist
*arg
= arglist_head
;
1775 fmt_compile(arg
->a_nfs
, &arg
->a_fmt
, 0);
1780 * Search through and mark any components that are address components
1783 for (ap
= addrcomps
; *ap
; ap
++) {
1784 cptr
= fmt_findcomp (*ap
);
1786 cptr
->c_type
|= CT_ADDR
;
1791 * Filter the body of a message through a specified format program
1795 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
,
1796 m_getfld_state_t gstate
)
1798 struct mcomp holder
;
1800 int fdinput
[2], fdoutput
[2], waitstat
;
1802 pid_t writerpid
, filterpid
;
1805 * Create pipes so we can communicate with our filter process.
1808 if (pipe(fdinput
) < 0) {
1809 adios(NULL
, "Unable to create input pipe");
1812 if (pipe(fdoutput
) < 0) {
1813 adios(NULL
, "Unable to create output pipe");
1817 * Here's what we're doing to do.
1819 * - Fork ourselves and start writing data to the write side of the
1820 * input pipe (fdinput[1]).
1822 * - Fork and exec our filter program. We set the standard input of
1823 * our filter program to be the read side of our input pipe (fdinput[0]).
1824 * Standard output is set to the write side of our output pipe
1827 * - We read from the read side of the output pipe (fdoutput[0]).
1829 * We're forking because that's the simplest way to prevent any deadlocks.
1830 * (without doing something like switching to non-blocking I/O and using
1831 * select or poll, and I'm not interested in doing that).
1834 switch (writerpid
= fork()) {
1837 * Our child process - just write to the filter input (fdinput[1]).
1838 * Close all other descriptors that we don't need.
1846 * Call m_getfld() until we're no longer in the BODY state
1849 while (state
== BODY
) {
1851 write(fdinput
[1], buf
, strlen(buf
));
1852 state
= m_getfld (gstate
, name
, buf
, &bufsz2
, fp
);
1856 * We should be done; time to exit.
1861 * Make sure we call _exit(), otherwise we may flush out the stdio
1862 * buffers that we have duplicated from the parent.
1867 adios(NULL
, "Unable to fork for filter writer process");
1872 * Fork and exec() our filter program, after redirecting standard in
1873 * and standard out appropriately.
1876 switch (filterpid
= fork()) {
1883 * Allocate an argument array for us
1886 args
= (char **) mh_xmalloc((filter_nargs
+ 2) * sizeof(char *));
1887 args
[0] = formatproc
;
1888 args
[filter_nargs
+ 1] = NULL
;
1896 * Pull out each argument and scan them.
1899 for (a
= arglist_head
, i
= 1; a
!= NULL
; a
= a
->a_next
, i
++) {
1900 args
[i
] = mh_xmalloc(BUFSIZ
);
1901 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
- 1, BUFSIZ
, dat
);
1903 * fmt_scan likes to put a trailing newline at the end of the
1904 * format string. If we have one, get rid of it.
1906 s
= strlen(args
[i
]);
1907 if (args
[i
][s
- 1] == '\n')
1908 args
[i
][s
- 1] = '\0';
1911 fprintf(stderr
, "filterarg: fmt=\"%s\", output=\"%s\"\n",
1915 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1916 adios("formatproc", "Unable to dup2() standard input");
1918 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1919 adios("formatproc", "Unable to dup2() standard output");
1923 * Close everything (especially the old input and output
1924 * descriptors, since they've been dup'd to stdin and stdout),
1925 * and exec the formatproc.
1933 execvp(formatproc
, args
);
1935 adios(formatproc
, "Unable to execute filter");
1940 adios(NULL
, "Unable to fork format program");
1944 * Close everything except our reader (fdoutput[0]);
1952 * As we read in this data, send it to putcomp
1955 holder
.c_text
= buf
;
1957 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1959 putcomp(c1
, &holder
, BODYCOMP
);
1963 adios(NULL
, "reading from formatproc");
1967 * See if we got any errors along the way. I'm a little leery of calling
1968 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1971 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1972 if (errno
!= ECHILD
) {
1973 adios("filterproc", "Unable to determine status");
1976 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1977 pidstatus(waitstat
, stderr
, "filterproc");
1981 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1982 if (errno
!= ECHILD
) {
1983 adios("writer process", "Unable to determine status");
1987 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1988 pidstatus(waitstat
, stderr
, "writer process");