]>
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 list of format arguments
123 struct format
*a_fmt
;
125 struct arglist
*a_next
;
129 char *c_name
; /* component name */
130 char *c_text
; /* component text */
131 char *c_ovtxt
; /* text overflow indicator */
132 char *c_nfs
; /* iff FORMAT */
133 struct format
*c_fmt
; /* .. */
134 int c_offset
; /* left margin indentation */
135 int c_ovoff
; /* overflow indentation */
136 int c_width
; /* width of field */
137 int c_cwidth
; /* width of component */
138 int c_length
; /* length in lines */
140 struct arglist
*c_f_args
; /* Argument list for filter*/
141 struct arglist
*c_f_tail
; /* Pointer to tail of list */
142 int c_nargs
; /* Number of arguments */
143 struct mcomp
*c_next
;
146 static struct mcomp
*msghd
= NULL
;
147 static struct mcomp
*msgtl
= NULL
;
148 static struct mcomp
*fmthd
= NULL
;
149 static struct mcomp
*fmttl
= NULL
;
151 static struct mcomp global
= {
152 NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
, NULL
, 0,
156 static struct mcomp holder
= {
157 NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
, NULL
, 0,
166 static struct pair pairs
[] = {
169 { "Sender", ADDRFMT
},
170 { "Reply-To", ADDRFMT
},
174 { "Resent-Date", DATEFMT
},
175 { "Resent-From", ADDRFMT
},
176 { "Resent-Sender", ADDRFMT
},
177 { "Resent-Reply-To", ADDRFMT
},
178 { "Resent-To", ADDRFMT
},
179 { "Resent-cc", ADDRFMT
},
180 { "Resent-Bcc", ADDRFMT
},
190 static struct triple triples
[] = {
191 { "nocomponent", NOCOMPONENT
, 0 },
192 { "uppercase", UPPERCASE
, 0 },
193 { "nouppercase", 0, UPPERCASE
},
194 { "center", CENTER
, 0 },
195 { "nocenter", 0, CENTER
},
196 { "clearscreen", CLEARSCR
, 0 },
197 { "noclearscreen", 0, CLEARSCR
},
198 { "noclear", 0, CLEARSCR
},
199 { "leftadjust", LEFTADJUST
, 0 },
200 { "noleftadjust", 0, LEFTADJUST
},
201 { "compress", COMPRESS
, 0 },
202 { "nocompress", 0, COMPRESS
},
203 { "split", SPLIT
, 0 },
204 { "nosplit", 0, SPLIT
},
205 { "addrfield", ADDRFMT
, DATEFMT
},
207 { "nobell", 0, BELL
},
208 { "datefield", DATEFMT
, ADDRFMT
},
209 { "newline", 0, NONEWLINE
},
210 { "nonewline", NONEWLINE
, 0 },
211 { "wrap", 0, NOWRAP
},
212 { "nowrap", NOWRAP
, 0 },
213 { "format", FMTFILTER
, 0 },
214 { "noformat", 0, FMTFILTER
},
218 static char *addrcomps
[] = {
235 static int bellflg
= 0;
236 static int clearflg
= 0;
237 static int dashstuff
= 0;
238 static int dobody
= 1;
239 static int forwflg
= 0;
240 static int forwall
= 0;
242 static int sleepsw
= NOTOK
;
244 static char *digest
= NULL
;
245 static int volume
= 0;
246 static int issue
= 0;
248 static int exitstat
= 0;
249 static int mhldebug
= 0;
251 static int filesize
= 0;
256 static int ontty
= NOTTY
;
259 static unsigned int column
;
265 static unsigned int wid
;
269 static unsigned char *onelp
;
273 static int num_ignores
= 0;
274 static char *ignores
[MAXARGS
];
277 static jmp_buf mhlenv
;
279 static char delim3
[] = /* from forw.c */
280 "\n----------------------------------------------------------------------\n\n";
281 static char delim4
[] = "\n------------------------------\n\n";
283 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
285 static struct comp
*mhlcomp
[128];
288 * Redefine a couple of functions.
289 * These are undefined later in the code.
291 #define adios mhladios
297 static void mhl_format (char *, int, int);
298 static int evalvar (struct mcomp
*);
299 static int ptoi (char *, int *);
300 static int ptos (char *, char **);
301 static char *parse (void);
302 static void process (char *, char *, int, int);
303 static void mhlfile (FILE *, char *, int, int);
304 static int mcomp_flags (char *);
305 static char *mcomp_add (long, char *, char *);
306 static void mcomp_format (struct mcomp
*, struct mcomp
*);
307 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
308 static void free_queue (struct mcomp
**, struct mcomp
**);
309 static void putcomp (struct mcomp
*, struct mcomp
*, int);
310 static char *oneline (char *, long);
311 static void putstr (char *, long);
312 static void putch (char, long);
313 static void intrser (int);
314 static void pipeser (int);
315 static void quitser (int);
316 static void mhladios (char *, char *, ...);
317 static void mhldone (int);
318 static void m_popen (char *);
319 static void filterbody (struct mcomp
*, char *, int, int, FILE *);
320 static int compileargs (struct mcomp
*, char *);
321 static int checkcomp (char *, char *);
322 static void addcomp (int, char *, char *);
323 static void freecomps (void);
324 static void freecomptext (void);
328 mhl (int argc
, char **argv
)
330 int length
= 0, nomore
= 0;
331 unsigned int i
, vecp
= 0;
333 char *cp
, *folder
= NULL
, *form
= NULL
;
334 char buf
[BUFSIZ
], *files
[MAXARGS
];
335 char **argp
, **arguments
;
337 invo_name
= r1bindex (argv
[0], '/');
339 /* read user profile/context */
342 arguments
= getarguments (invo_name
, argc
, argv
, 1);
345 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
348 while ((cp
= *argp
++)) {
350 switch (smatch (++cp
, mhlswitches
)) {
352 ambigsw (cp
, mhlswitches
);
355 adios (NULL
, "-%s unknown\n", cp
);
358 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
359 print_help (buf
, mhlswitches
, 1);
362 print_version(invo_name
);
380 if (!(folder
= *argp
++) || *folder
== '-')
381 adios (NULL
, "missing argument to %s", argp
[-2]);
384 if (!(form
= *argp
++) || *form
== '-')
385 adios (NULL
, "missing argument to %s", argp
[-2]);
389 if (!(cp
= *argp
++) || *cp
== '-')
390 adios (NULL
, "missing argument to %s", argp
[-2]);
391 sleepsw
= atoi (cp
);/* ZERO ok! */
395 if (!(moreproc
= *argp
++) || *moreproc
== '-')
396 adios (NULL
, "missing argument to %s", argp
[-2]);
403 if (!(formatproc
= *argp
++) || *formatproc
== '-')
404 adios (NULL
, "missing argument to %s", argp
[-2]);
411 if (!(cp
= *argp
++) || *cp
== '-')
412 adios (NULL
, "missing argument to %s", argp
[-2]);
413 if ((length
= atoi (cp
)) < 1)
414 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
417 if (!(cp
= *argp
++) || *cp
== '-')
418 adios (NULL
, "missing argument to %s", argp
[-2]);
419 if ((width
= atoi (cp
)) < 1)
420 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
424 if (!(digest
= *argp
++) || *digest
== '-')
425 adios (NULL
, "missing argument to %s", argp
[-2]);
428 if (!(cp
= *argp
++) || *cp
== '-')
429 adios (NULL
, "missing argument to %s", argp
[-2]);
430 if ((issue
= atoi (cp
)) < 1)
431 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
434 if (!(cp
= *argp
++) || *cp
== '-')
435 adios (NULL
, "missing argument to %s", argp
[-2]);
436 if ((volume
= atoi (cp
)) < 1)
437 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
441 forwall
++; /* fall */
444 clearflg
= -1;/* XXX */
448 dashstuff
= 1; /* trinary logic */
451 dashstuff
= -1; /* trinary logic */
463 folder
= getenv ("mhfolder");
465 if (isatty (fileno (stdout
))) {
466 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
468 SIGNAL (SIGINT
, SIG_IGN
);
469 SIGNAL2 (SIGQUIT
, quitser
);
471 SIGNAL2 (SIGPIPE
, pipeser
);
475 SIGNAL (SIGINT
, SIG_IGN
);
476 SIGNAL2 (SIGQUIT
, quitser
);
483 for (i
= 0; i
< sizeof(mhlcomp
)/sizeof(mhlcomp
[0]); i
++)
486 mhl_format (form
? form
: mhlformat
, length
, width
);
489 process (folder
, NULL
, 1, vecp
= 1);
491 for (i
= 0; i
< vecp
; i
++)
492 process (folder
, files
[i
], i
+ 1, vecp
);
499 printf ("%s", delim4
);
501 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
503 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
504 digest
, volume
, issue
);
507 for (cp
= buf
+ i
; i
> 1; i
--)
514 printf ("\n------- End of Forwarded Message%s\n",
515 vecp
> 1 ? "s" : "");
520 adios("output", "error writing");
523 if (clearflg
> 0 && ontty
== NOTTY
)
534 mhl_format (char *file
, int length
, int width
)
538 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
542 static dev_t dev
= 0;
543 static ino_t ino
= 0;
544 static time_t mtime
= 0;
547 if (stat (etcpath (file
), &st
) != NOTOK
548 && mtime
== st
.st_mtime
553 free_queue (&fmthd
, &fmttl
);
556 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
557 adios (file
, "unable to open format file");
559 if (fstat (fileno (fp
), &st
) != NOTOK
) {
565 global
.c_ovtxt
= global
.c_nfs
= NULL
;
569 if ((i
= sc_width ()) > 5)
571 global
.c_cwidth
= -1;
572 if ((i
= sc_length ()) > 5)
573 global
.c_length
= i
- 1;
574 global
.c_flags
= BELL
; /* BELL is default */
575 *(ip
= ignores
) = NULL
;
577 while (vfgets (fp
, &ap
) == OK
) {
582 if ((cp
= strchr(bp
, '\n')))
586 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
591 strncpy (name
, parse(), sizeof(name
));
597 * Split this list of fields to ignore, and copy
598 * it to the end of the current "ignores" list.
600 if (!mh_strcasecmp (name
, "ignores")) {
601 char **tmparray
, **p
;
604 /* split the fields */
605 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
607 /* count number of fields split */
612 /* copy pointers to split fields to ignores array */
613 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
619 if (evalvar (&global
))
620 adios (NULL
, "format file syntax error: %s", bp
);
627 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
628 while (*parptr
== ':' || *parptr
== ',') {
631 adios (NULL
, "format file syntax error: %s", bp
);
633 if (!c1
->c_nfs
&& global
.c_nfs
) {
634 if (c1
->c_flags
& DATEFMT
) {
635 if (global
.c_flags
& DATEFMT
)
636 c1
->c_nfs
= getcpy (global
.c_nfs
);
639 if (c1
->c_flags
& ADDRFMT
) {
640 if (global
.c_flags
& ADDRFMT
)
641 c1
->c_nfs
= getcpy (global
.c_nfs
);
647 adios (NULL
, "format file syntax error: %s", bp
);
653 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
654 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
655 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
656 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
657 (unsigned int)(unsigned long) c1
->c_nfs
,
658 (unsigned int)(unsigned long) c1
->c_fmt
);
659 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
660 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
661 c1
->c_cwidth
, c1
->c_length
);
662 fprintf (stderr
, "\tflags=%s\n",
663 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
669 global
.c_flags
|= CLEARSCR
;
672 global
.c_flags
&= ~CLEARSCR
;
675 switch (bellflg
) { /* command line may override format file */
677 global
.c_flags
|= BELL
;
680 global
.c_flags
&= ~BELL
;
685 global
.c_length
= length
;
687 global
.c_width
= width
;
688 if (global
.c_length
< 5)
689 global
.c_length
= 10000;
690 if (global
.c_width
< 5)
691 global
.c_width
= 10000;
696 evalvar (struct mcomp
*c1
)
698 char *cp
, name
[NAMESZ
];
703 strncpy (name
, parse(), sizeof(name
));
705 if (!mh_strcasecmp (name
, "component")) {
706 if (ptos (name
, &c1
->c_text
))
708 c1
->c_flags
&= ~NOCOMPONENT
;
712 if (!mh_strcasecmp (name
, "overflowtext"))
713 return ptos (name
, &c1
->c_ovtxt
);
715 if (!mh_strcasecmp (name
, "formatfield")) {
718 if (ptos (name
, &cp
))
720 nfs
= new_fs (NULL
, NULL
, cp
);
721 c1
->c_nfs
= getcpy (nfs
);
722 c1
->c_flags
|= FORMAT
;
726 if (!mh_strcasecmp (name
, "decode")) {
729 nfs
= new_fs (NULL
, NULL
, "%(decode{text})");
730 c1
->c_nfs
= getcpy (nfs
);
731 c1
->c_flags
|= FORMAT
;
735 if (!mh_strcasecmp (name
, "offset"))
736 return ptoi (name
, &c1
->c_offset
);
737 if (!mh_strcasecmp (name
, "overflowoffset"))
738 return ptoi (name
, &c1
->c_ovoff
);
739 if (!mh_strcasecmp (name
, "width"))
740 return ptoi (name
, &c1
->c_width
);
741 if (!mh_strcasecmp (name
, "compwidth"))
742 return ptoi (name
, &c1
->c_cwidth
);
743 if (!mh_strcasecmp (name
, "length"))
744 return ptoi (name
, &c1
->c_length
);
745 if (!mh_strcasecmp (name
, "nodashstuffing"))
746 return (dashstuff
= -1);
748 for (ap
= triples
; ap
->t_name
; ap
++)
749 if (!mh_strcasecmp (ap
->t_name
, name
)) {
750 c1
->c_flags
|= ap
->t_on
;
751 c1
->c_flags
&= ~ap
->t_off
;
755 if (!mh_strcasecmp (name
, "formatarg")) {
759 if (ptos (name
, &cp
))
761 nfs
= new_fs (NULL
, NULL
, cp
);
763 rc
= compileargs(c1
, nfs
);
774 ptoi (char *name
, int *i
)
778 if (*parptr
++ != '=' || !*(cp
= parse ())) {
779 advise (NULL
, "missing argument to variable %s", name
);
789 ptos (char *name
, char **s
)
793 if (*parptr
++ != '=') {
794 advise (NULL
, "missing argument to variable %s", name
);
798 if (*parptr
!= '"') {
800 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
804 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
805 if (*parptr
== QUOTE
)
812 if ((*parptr
= c
) == '"')
823 static char result
[NAMESZ
];
825 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
844 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
851 switch (setjmp (env
)) {
854 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
856 advise (fname
, "unable to open");
864 if (fstat(fileno(fp
), &st
) == 0) {
865 filesize
= st
.st_size
;
869 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
871 SIGNAL (SIGINT
, intrser
);
872 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
876 SIGNAL (SIGINT
, SIG_IGN
);
877 if (mhl_action
== NULL
&& fp
!= stdin
)
881 free (holder
.c_text
);
882 holder
.c_text
= NULL
;
884 free_queue (&msghd
, &msgtl
);
885 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
886 c1
->c_flags
&= ~HDROUTPUT
;
895 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
898 struct mcomp
*c1
, *c2
, *c3
;
899 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
903 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
905 printf ("\n-------");
907 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
909 printf (" Message %d", ofilen
);
917 if ((global
.c_flags
& CLEARSCR
))
922 printf (">>> %s\n\n", mname
);
927 strncpy (buf
, "\n", sizeof(buf
));
929 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
932 printf ("Press <return> to list \"%s\"...", mname
);
936 read (fileno (stdout
), buf
, sizeof(buf
));
938 if (strchr(buf
, '\n')) {
939 if ((global
.c_flags
& CLEARSCR
))
953 printf (">>> %s\n\n", mname
);
959 for (state
= FLD
;;) {
960 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
963 bucket
= checkcomp(name
, buf
);
964 for (ip
= ignores
; *ip
; ip
++)
965 if (!mh_strcasecmp (name
, *ip
)) {
966 while (state
== FLDPLUS
) {
967 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
968 addcomp(bucket
, name
, buf
);
975 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
976 if (!mh_strcasecmp (c2
->c_name
, name
))
979 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
980 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
981 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
983 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
987 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
988 while (state
== FLDPLUS
) {
989 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
990 c1
->c_text
= add (buf
, c1
->c_text
);
991 addcomp(bucket
, name
, buf
);
994 c1
->c_flags
|= EXTRA
;
1000 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
1001 if (c1
->c_flags
& CLEARTEXT
) {
1002 putcomp (c1
, c1
, ONECOMP
);
1005 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
1006 holder
.c_text
= concat ("(Message ", mname
, ")\n",
1008 putcomp (c1
, &holder
, ONECOMP
);
1009 free (holder
.c_text
);
1010 holder
.c_text
= NULL
;
1013 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
1014 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1015 if (c2
->c_flags
& EXTRA
)
1016 putcomp (c1
, c2
, TWOCOMP
);
1019 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
1020 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
1021 formatproc
!= NULL
) {
1022 filterbody(c1
, buf
, sizeof(buf
), state
, fp
);
1024 holder
.c_text
= mh_xmalloc (sizeof(buf
));
1025 strncpy (holder
.c_text
, buf
, sizeof(buf
));
1026 while (state
== BODY
) {
1027 putcomp (c1
, &holder
, BODYCOMP
);
1028 state
= m_getfld (state
, name
, holder
.c_text
,
1031 free (holder
.c_text
);
1032 holder
.c_text
= NULL
;
1036 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
1037 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
1038 putcomp (c1
, c2
, ONECOMP
);
1039 if (!(c1
->c_flags
& SPLIT
))
1047 advise (NULL
, "format error in message %s", mname
);
1052 adios (NULL
, "getfld() returned %d", state
);
1059 mcomp_flags (char *name
)
1063 for (ap
= pairs
; ap
->p_name
; ap
++)
1064 if (!mh_strcasecmp (ap
->p_name
, name
))
1065 return (ap
->p_flags
);
1072 mcomp_add (long flags
, char *s1
, char *s2
)
1076 if (!(flags
& ADDRFMT
))
1077 return add (s1
, s2
);
1079 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1082 return add (s1
, add (",\n", s2
));
1089 struct pqpair
*pq_next
;
1094 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1098 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1100 struct pqpair
*p
, *q
;
1102 struct mailname
*mp
;
1109 dat
[3] = sizeof(buffer
) - 1;
1111 fmt_compile (c1
->c_nfs
, &c1
->c_fmt
);
1113 if (!(c1
->c_flags
& ADDRFMT
)) {
1114 FINDCOMP (cptr
, "text");
1117 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1121 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1122 /* Don't need to append a newline, dctime() already did */
1123 c2
->c_text
= getcpy (buffer
);
1129 (q
= &pq
)->pq_next
= NULL
;
1130 while ((cp
= getname (ap
))) {
1131 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1132 adios (NULL
, "unable to allocate pqpair memory");
1134 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1135 p
->pq_text
= getcpy (cp
);
1136 p
->pq_error
= getcpy (error
);
1138 p
->pq_text
= getcpy (mp
->m_text
);
1141 q
= (q
->pq_next
= p
);
1144 for (p
= pq
.pq_next
; p
; p
= q
) {
1145 FINDCOMP (cptr
, "text");
1147 cptr
->c_text
= p
->pq_text
;
1148 FINDCOMP (cptr
, "error");
1150 cptr
->c_text
= p
->pq_error
;
1152 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1155 c2
->c_text
= add (",\n", c2
->c_text
);
1156 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1158 c2
->c_text
= add (buffer
, c2
->c_text
);
1168 c2
->c_text
= add ("\n", c2
->c_text
);
1173 static struct mcomp
*
1174 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1178 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1179 adios (NULL
, "unable to allocate comp memory");
1181 c1
->c_flags
= flags
& ~INIT
;
1182 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1183 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1184 c1
->c_text
= text
? getcpy (text
) : NULL
;
1187 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1188 c1
->c_offset
= global
.c_offset
;
1189 c1
->c_ovoff
= global
. c_ovoff
;
1190 c1
->c_width
= c1
->c_length
= 0;
1191 c1
->c_cwidth
= global
.c_cwidth
;
1192 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1197 (*tail
)->c_next
= c1
;
1205 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1207 struct mcomp
*c1
, *c2
;
1209 for (c1
= *head
; c1
; c1
= c2
) {
1220 free ((char *) c1
->c_fmt
);
1222 struct arglist
*a1
, *a2
;
1223 for (a1
= c1
->c_f_args
; a1
; a1
= a2
) {
1235 *head
= *tail
= NULL
;
1240 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1247 llim
= c1
->c_length
? c1
->c_length
: -1;
1248 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1249 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1251 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1253 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1254 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1255 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1258 if (c1
->c_flags
& CLEARTEXT
) {
1259 putstr (c1
->c_text
, c1
->c_flags
);
1260 putstr ("\n", c1
->c_flags
);
1264 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1265 mcomp_format (c1
, c2
);
1267 if (c1
->c_flags
& CENTER
) {
1268 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1269 - c1
->c_offset
- strlen (c2
->c_text
);
1270 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1271 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1272 lm
= c1
->c_offset
+ (count
/ 2);
1278 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1279 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1280 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1282 *cp
= toupper (*cp
);
1283 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1284 if (flag
!= BODYCOMP
) {
1285 putstr (": ", c1
->c_flags
);
1286 if (!(c1
->c_flags
& SPLIT
))
1287 c1
->c_flags
|= HDROUTPUT
;
1290 if ((count
= c1
->c_cwidth
-
1291 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1293 putstr (" ", c1
->c_flags
);
1296 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1300 && !(c2
->c_flags
& HDROUTPUT
)
1301 && !(c2
->c_flags
& NOCOMPONENT
)) {
1302 if (c1
->c_flags
& UPPERCASE
)
1303 for (cp
= c2
->c_name
; *cp
; cp
++)
1305 *cp
= toupper (*cp
);
1306 putstr (c2
->c_name
, c1
->c_flags
);
1307 putstr (": ", c1
->c_flags
);
1308 if (!(c1
->c_flags
& SPLIT
))
1309 c2
->c_flags
|= HDROUTPUT
;
1312 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1314 putstr (" ", c1
->c_flags
);
1316 if (c1
->c_flags
& UPPERCASE
)
1317 for (cp
= c2
->c_text
; *cp
; cp
++)
1319 *cp
= toupper (*cp
);
1323 if (flag
== TWOCOMP
)
1324 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1325 : (int) strlen (c2
->c_name
) + 2;
1327 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1328 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1330 count
+= c1
->c_offset
;
1332 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1333 putstr(cp
, c1
->c_flags
);
1335 putstr ("\n", c1
->c_flags
);
1336 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1338 if (flag
== BODYCOMP
1339 && !(c1
->c_flags
& NOCOMPONENT
))
1340 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1342 putstr (cp
, c1
->c_flags
);
1344 putstr ("\n", c1
->c_flags
);
1346 if (flag
== BODYCOMP
&& term
== '\n')
1347 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1352 oneline (char *stuff
, long flags
)
1360 return (onelp
= NULL
);
1364 if (flags
& COMPRESS
) {
1365 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1366 if (isspace (*onelp
)) {
1367 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1386 while (*onelp
&& *onelp
!= '\n')
1388 if (*onelp
== '\n') {
1392 if (flags
& LEFTADJUST
)
1393 while (*ret
== ' ' || *ret
== '\t')
1396 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1404 putstr (char *string
, long flags
)
1406 if (!column
&& lm
> 0) {
1409 putch ('\t', flags
);
1419 putch (*string
++, flags
);
1424 putch (char ch
, long flags
)
1437 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1439 if (global
.c_flags
& BELL
)
1443 read (fileno (stdout
), buf
, sizeof(buf
));
1444 if (strchr(buf
, '\n')) {
1445 if (global
.c_flags
& CLEARSCR
)
1450 row
= global
.c_length
/ 3;
1469 * If we are forwarding this message, and the first
1470 * column contains a dash, then add a dash and a space.
1472 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1481 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1482 putch ('\n', flags
);
1485 putstr (ovtxt
? ovtxt
: "", flags
);
1501 longjmp (env
, DONE
);
1526 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1528 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1532 switch (setjmp (mhlenv
)) {
1535 sleepsw
= 0; /* XXX */
1536 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1539 mhl_action
= action
;
1542 * If signal is at default action, then start ignoring
1543 * it, else let it set to its current action.
1545 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1546 SIGNAL (SIGINT
, istat
);
1547 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1548 SIGNAL (SIGQUIT
, qstat
);
1549 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1550 mhl (argc
, argv
); /* FALL THROUGH! */
1553 SIGNAL (SIGINT
, istat
);
1554 SIGNAL (SIGQUIT
, qstat
);
1555 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1558 SIGNAL (SIGPIPE
, pstat
);
1560 if (holder
.c_text
) {
1561 free (holder
.c_text
);
1562 holder
.c_text
= NULL
;
1564 free_queue (&msghd
, &msgtl
);
1565 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1566 c1
->c_flags
&= ~HDROUTPUT
;
1575 mhladios (char *what
, char *fmt
, ...)
1580 advertise (what
, NULL
, fmt
, ap
);
1587 mhldone (int status
)
1591 longjmp (mhlenv
, DONE
);
1597 static int m_pid
= NOTOK
;
1598 static int sd
= NOTOK
;
1601 m_popen (char *name
)
1605 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1606 adios ("standard output", "unable to dup()");
1608 if (pipe (pd
) == NOTOK
)
1609 adios ("pipe", "unable to");
1611 switch (m_pid
= vfork()) {
1613 adios ("fork", "unable to");
1616 SIGNAL (SIGINT
, SIG_DFL
);
1617 SIGNAL (SIGQUIT
, SIG_DFL
);
1620 if (pd
[0] != fileno (stdin
)) {
1621 dup2 (pd
[0], fileno (stdin
));
1624 execlp (name
, r1bindex (name
, '/'), NULL
);
1625 fprintf (stderr
, "unable to exec ");
1631 if (pd
[1] != fileno (stdout
)) {
1632 dup2 (pd
[1], fileno (stdout
));
1647 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1648 adios ("standard output", "unable to dup2()");
1657 pidwait (m_pid
, OK
);
1663 * Compile a format string and add it to the list of arguments used by
1666 * This deserves some explanation. Here's the deal:
1668 * We want to keep track of components used as arguments by formatproc,
1669 * but the hash table is reset every time fmt_compile is called. So we
1670 * iterate through the function list looking for things that use components
1671 * and save the name. And because we might get the same components used
1672 * by different arguments we need to keep track to every reference of
1673 * every component so we can add them when the message is processed. So
1674 * we compile the argument string now (to get the components we use) and
1675 * save them for later.
1679 compileargs (struct mcomp
*c1
, char *nfs
)
1682 struct arglist
*args
;
1687 i
= fmt_compile(nfs
, &fmt
);
1690 * Search through and mark any components that are address components
1693 for (ap
= addrcomps
; *ap
; ap
++) {
1694 FINDCOMP (cptr
, *ap
);
1696 cptr
->c_type
|= CT_ADDR
;
1699 args
= (struct arglist
*) mh_xmalloc(sizeof(struct arglist
));
1702 adios (NULL
, "Unable to allocate formatproc args storage");
1705 args
->a_nfs
= format_string
;
1706 args
->a_next
= NULL
;
1708 format_string
= NULL
;
1711 c1
->c_f_tail
->a_next
= args
;
1713 c1
->c_f_tail
= args
;
1716 c1
->c_f_args
= args
;
1722 * If wantcomp ever changes size, we need to change the size
1723 * of mhlcomp as well
1726 for (i
= 0; i
< sizeof(wantcomp
)/sizeof(wantcomp
[0]); i
++) {
1730 for (c
= mhlcomp
[i
]; c
->c_next
!= NULL
; c
= c
->c_next
)
1732 c
->c_next
= wantcomp
[i
];
1734 mhlcomp
[i
] = wantcomp
[i
];
1742 * Check to see if we are interested in a component. If we are, save
1747 checkcomp(char *name
, char *buf
)
1751 int bucket
= CHASH(name
);
1754 if ((c
= mhlcomp
[bucket
])) {
1756 if (mh_strcasecmp(name
, c
->c_name
) == 0) {
1759 c
->c_text
= strdup(buf
);
1761 i
= strlen(cp
= c
->c_text
) - 1;
1762 if (cp
[i
] == '\n') {
1763 if (c
->c_type
& CT_ADDR
) {
1765 cp
= add (",\n\t", cp
);
1767 cp
= add ("\t", cp
);
1770 c
->c_text
= add (buf
, cp
);
1773 } while ((c
= c
->c_next
));
1776 return found
? bucket
: -1;
1780 * Add text to an existing component
1784 addcomp(int bucket
, char *name
, char *buf
)
1789 c
= mhlcomp
[bucket
];
1791 if (mh_strcasecmp(name
, c
->c_name
) == 0)
1792 c
->c_text
= add (buf
, c
->c_text
);
1793 } while ((c
= c
->c_next
));
1798 * Free up saved component structures
1804 struct comp
*c1
, *c2
;
1807 for (i
= 0; i
< sizeof(mhlcomp
)/sizeof(mhlcomp
[0]); i
++) {
1808 if ((c1
= mhlcomp
[i
]))
1809 for (; c1
; c1
= c2
) {
1819 * Just free up the component text.
1828 for (i
= 0; i
< sizeof(mhlcomp
)/sizeof(mhlcomp
[0]); i
++) {
1829 if ((c1
= mhlcomp
[i
]))
1830 for (; c1
; c1
= c1
->c_next
) {
1840 * Filter the body of a message through a specified format program
1844 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
)
1846 struct mcomp holder
;
1848 int fdinput
[2], fdoutput
[2], waitstat
;
1850 pid_t writerpid
, filterpid
;
1853 * Create pipes so we can communicate with our filter process.
1856 if (pipe(fdinput
) < 0) {
1857 adios(NULL
, "Unable to create input pipe");
1860 if (pipe(fdoutput
) < 0) {
1861 adios(NULL
, "Unable to create output pipe");
1865 * Here's what we're doing to do.
1867 * - Fork ourselves and start writing data to the write side of the
1868 * input pipe (fdinput[1]).
1870 * - Fork and exec our filter program. We set the standard input of
1871 * our filter program to be the read side of our input pipe (fdinput[0]).
1872 * Standard output is set to the write side of our output pipe
1875 * - We read from the read side of the output pipe (fdoutput[0]).
1877 * We're forking because that's the simplest way to prevent any deadlocks.
1878 * (without doing something like switching to non-blocking I/O and using
1879 * select or poll, and I'm not interested in doing that).
1882 switch (writerpid
= fork()) {
1885 * Our child process - just write to the filter input (fdinput[1]).
1886 * Close all other descriptors that we don't need.
1894 * Call m_getfld() until we're no longer in the BODY state
1897 while (state
== BODY
) {
1898 write(fdinput
[1], buf
, strlen(buf
));
1899 state
= m_getfld(state
, name
, buf
, bufsz
, fp
);
1903 * We should be done; time to exit.
1908 * Make sure we call _exit(), otherwise we may flush out the stdio
1909 * buffers that we have duplicated from the parent.
1914 adios(NULL
, "Unable to fork for filter writer process");
1919 * Fork and exec() our filter program, after redirecting standard in
1920 * and standard out appropriately.
1923 switch (filterpid
= fork()) {
1930 * Allocate an argument array for us
1933 args
= (char **) mh_xmalloc((c1
->c_nargs
+ 2) * sizeof(char *));
1934 args
[0] = formatproc
;
1935 args
[c1
->c_nargs
+ 1] = NULL
;
1943 * Pull out each argument and scan them.
1946 for (a
= c1
->c_f_args
, i
= 1; a
!= NULL
; a
= a
->a_next
, i
++) {
1947 args
[i
] = mh_xmalloc(BUFSIZ
);
1948 fmt_scan(a
->a_fmt
, args
[i
], BUFSIZ
, dat
);
1950 * fmt_scan likes to put a trailing newline at the end of the
1951 * format string. If we have one, get rid of it.
1953 s
= strlen(args
[i
]);
1954 if (args
[i
][s
- 1] == '\n')
1955 args
[i
][s
- 1] = '\0';
1958 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1959 adios("formatproc", "Unable to dup2() standard input");
1961 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1962 adios("formatproc", "Unable to dup2() standard output");
1966 * Close everything (especially the old input and output
1967 * descriptors, since they've been dup'd to stdin and stdout),
1968 * and exec the formatproc.
1976 execvp(formatproc
, args
);
1978 adios(formatproc
, "Unable to execute filter");
1983 adios(NULL
, "Unable to fork format program");
1987 * Close everything except our reader (fdoutput[0]);
1995 * As we read in this data, send it to putcomp
1998 holder
.c_text
= buf
;
2000 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
2002 putcomp(c1
, &holder
, BODYCOMP
);
2006 adios(NULL
, "reading from formatproc");
2010 * See if we got any errors along the way. I'm a little leery of calling
2011 * waitpid() without WNOHANG, but it seems to be the most correct solution.
2014 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
2015 if (errno
!= ECHILD
) {
2016 adios("filterproc", "Unable to determine status");
2019 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
2020 pidstatus(waitstat
, stderr
, "filterproc");
2024 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
2025 if (errno
!= ECHILD
) {
2026 adios("writer process", "Unable to determine status");
2030 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
2031 pidstatus(waitstat
, stderr
, "writer process");