]>
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>
16 #include <h/m_setjmp.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 { "faceproc program", 0 },
61 { "folder +folder", 0 },
63 { "form formfile", 0 },
65 { "moreproc program", 0 },
69 { "length lines", 0 },
71 { "width columns", 0 },
73 { "sleep seconds", 0 },
75 { "dashstuffing", -12 }, /* interface from forw */
76 #define NBITSTUFFSW 14
77 { "nodashstuffing", -14 }, /* interface from forw */
83 { "forward", -7 }, /* interface from forw */
85 { "forwall", -7 }, /* interface from forw */
87 { "digest list", -6 },
89 { "volume number", -6 },
91 { "issue number", -5 },
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 FACEFMT 0x004000 /* contains face */
112 #define FACEDFLT 0x008000 /* default for face */
113 #define SPLIT 0x010000 /* split headers (don't concatenate) */
114 #define NONEWLINE 0x020000 /* don't write trailing newline */
115 #define NOWRAP 0x040000 /* Don't wrap lines ever */
116 #define FMTFILTER 0x080000 /* Filter through format filter */
117 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
118 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
121 char *c_name
; /* component name */
122 char *c_text
; /* component text */
123 char *c_ovtxt
; /* text overflow indicator */
124 char *c_nfs
; /* iff FORMAT */
125 struct format
*c_fmt
; /* .. */
126 char *c_face
; /* face designator */
127 int c_offset
; /* left margin indentation */
128 int c_ovoff
; /* overflow indentation */
129 int c_width
; /* width of field */
130 int c_cwidth
; /* width of component */
131 int c_length
; /* length in lines */
133 struct mcomp
*c_next
;
136 static struct mcomp
*msghd
= NULL
;
137 static struct mcomp
*msgtl
= NULL
;
138 static struct mcomp
*fmthd
= NULL
;
139 static struct mcomp
*fmttl
= NULL
;
141 static struct mcomp global
= {
142 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, NULL
145 static struct mcomp holder
= {
146 NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, NULL
154 static struct pair pairs
[] = {
156 { "From", ADDRFMT
|FACEDFLT
},
157 { "Sender", ADDRFMT
},
158 { "Reply-To", ADDRFMT
},
162 { "Resent-Date", DATEFMT
},
163 { "Resent-From", ADDRFMT
},
164 { "Resent-Sender", ADDRFMT
},
165 { "Resent-Reply-To", ADDRFMT
},
166 { "Resent-To", ADDRFMT
},
167 { "Resent-cc", ADDRFMT
},
168 { "Resent-Bcc", ADDRFMT
},
179 static struct triple triples
[] = {
180 { "nocomponent", NOCOMPONENT
, 0 },
181 { "uppercase", UPPERCASE
, 0 },
182 { "nouppercase", 0, UPPERCASE
},
183 { "center", CENTER
, 0 },
184 { "nocenter", 0, CENTER
},
185 { "clearscreen", CLEARSCR
, 0 },
186 { "noclearscreen", 0, CLEARSCR
},
187 { "noclear", 0, CLEARSCR
},
188 { "leftadjust", LEFTADJUST
, 0 },
189 { "noleftadjust", 0, LEFTADJUST
},
190 { "compress", COMPRESS
, 0 },
191 { "nocompress", 0, COMPRESS
},
192 { "split", SPLIT
, 0 },
193 { "nosplit", 0, SPLIT
},
194 { "addrfield", ADDRFMT
, DATEFMT
},
196 { "nobell", 0, BELL
},
197 { "datefield", DATEFMT
, ADDRFMT
},
198 { "newline", 0, NONEWLINE
},
199 { "nonewline", NONEWLINE
, 0 },
200 { "wrap", 0, NOWRAP
},
201 { "nowrap", NOWRAP
, 0 },
202 { "format", FMTFILTER
, 0 },
203 { "noformat", 0, FMTFILTER
},
208 static int bellflg
= 0;
209 static int clearflg
= 0;
210 static int dashstuff
= 0;
211 static int dobody
= 1;
212 static int forwflg
= 0;
213 static int forwall
= 0;
215 static int sleepsw
= NOTOK
;
217 static char *digest
= NULL
;
218 static int volume
= 0;
219 static int issue
= 0;
221 static int exitstat
= 0;
222 static int mhldebug
= 0;
227 static int ontty
= NOTTY
;
230 static unsigned int column
;
236 static unsigned int wid
;
240 static unsigned char *onelp
;
244 static int num_ignores
= 0;
245 static char *ignores
[MAXARGS
];
248 static jmp_buf mhlenv
;
250 static char delim3
[] = /* from forw.c */
251 "\n----------------------------------------------------------------------\n\n";
252 static char delim4
[] = "\n------------------------------\n\n";
254 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
258 * Redefine a couple of functions.
259 * These are undefined later in the code.
261 #define adios mhladios
267 static void mhl_format (char *, int, int);
268 static int evalvar (struct mcomp
*);
269 static int ptoi (char *, int *);
270 static int ptos (char *, char **);
271 static char *parse (void);
272 static void process (char *, char *, int, int);
273 static void mhlfile (FILE *, char *, int, int);
274 static int mcomp_flags (char *);
275 static char *mcomp_add (long, char *, char *);
276 static void mcomp_format (struct mcomp
*, struct mcomp
*);
277 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
278 static void free_queue (struct mcomp
**, struct mcomp
**);
279 static void putcomp (struct mcomp
*, struct mcomp
*, int);
280 static char *oneline (char *, long);
281 static void putstr (char *, long);
282 static void putch (char, long);
283 static void intrser (int);
284 static void pipeser (int);
285 static void quitser (int);
286 static void face_format (struct mcomp
*);
287 static int doface (struct mcomp
*);
288 static void mhladios (char *, char *, ...);
289 static void mhldone (int);
290 static void m_popen (char *);
291 static void filterbody (struct mcomp
*, char *, int, int, FILE *);
293 int mhl (int, char **);
294 int mhlsbr (int, char **, FILE *(*)());
295 void m_pclose (void);
297 void clear_screen (void); /* from termsbr.c */
298 int SOprintf (char *, ...); /* from termsbr.c */
299 int sc_width (void); /* from termsbr.c */
300 int sc_length (void); /* from termsbr.c */
301 int sc_hardcopy (void); /* from termsbr.c */
305 mhl (int argc
, char **argv
)
307 int length
= 0, nomore
= 0;
308 int i
, width
= 0, vecp
= 0;
309 char *cp
, *folder
= NULL
, *form
= NULL
;
310 char buf
[BUFSIZ
], *files
[MAXARGS
];
311 char **argp
, **arguments
;
313 invo_name
= r1bindex (argv
[0], '/');
315 /* read user profile/context */
318 arguments
= getarguments (invo_name
, argc
, argv
, 1);
321 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
324 if ((cp
= getenv ("FACEPROC")))
327 while ((cp
= *argp
++)) {
329 switch (smatch (++cp
, mhlswitches
)) {
331 ambigsw (cp
, mhlswitches
);
334 adios (NULL
, "-%s unknown\n", cp
);
337 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
338 print_help (buf
, mhlswitches
, 1);
341 print_version(invo_name
);
359 if (!(folder
= *argp
++) || *folder
== '-')
360 adios (NULL
, "missing argument to %s", argp
[-2]);
363 if (!(form
= *argp
++) || *form
== '-')
364 adios (NULL
, "missing argument to %s", argp
[-2]);
368 if (!(faceproc
= *argp
++) || *faceproc
== '-')
369 adios (NULL
, "missing argument to %s", argp
[-2]);
375 if (!(cp
= *argp
++) || *cp
== '-')
376 adios (NULL
, "missing argument to %s", argp
[-2]);
377 sleepsw
= atoi (cp
);/* ZERO ok! */
381 if (!(moreproc
= *argp
++) || *moreproc
== '-')
382 adios (NULL
, "missing argument to %s", argp
[-2]);
389 if (!(cp
= *argp
++) || *cp
== '-')
390 adios (NULL
, "missing argument to %s", argp
[-2]);
391 if ((length
= atoi (cp
)) < 1)
392 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
395 if (!(cp
= *argp
++) || *cp
== '-')
396 adios (NULL
, "missing argument to %s", argp
[-2]);
397 if ((width
= atoi (cp
)) < 1)
398 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
402 if (!(digest
= *argp
++) || *digest
== '-')
403 adios (NULL
, "missing argument to %s", argp
[-2]);
406 if (!(cp
= *argp
++) || *cp
== '-')
407 adios (NULL
, "missing argument to %s", argp
[-2]);
408 if ((issue
= atoi (cp
)) < 1)
409 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
412 if (!(cp
= *argp
++) || *cp
== '-')
413 adios (NULL
, "missing argument to %s", argp
[-2]);
414 if ((volume
= atoi (cp
)) < 1)
415 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
419 forwall
++; /* fall */
422 clearflg
= -1;/* XXX */
426 dashstuff
= 1; /* trinary logic */
429 dashstuff
= -1; /* trinary logic */
441 folder
= getenv ("mhfolder");
443 if (isatty (fileno (stdout
))) {
444 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
446 SIGNAL (SIGINT
, SIG_IGN
);
447 SIGNAL2 (SIGQUIT
, quitser
);
449 SIGNAL2 (SIGPIPE
, pipeser
);
453 SIGNAL (SIGINT
, SIG_IGN
);
454 SIGNAL2 (SIGQUIT
, quitser
);
461 mhl_format (form
? form
: mhlformat
, length
, width
);
464 process (folder
, NULL
, 1, vecp
= 1);
466 for (i
= 0; i
< vecp
; i
++)
467 process (folder
, files
[i
], i
+ 1, vecp
);
472 printf ("%s", delim4
);
474 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
476 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
477 digest
, volume
, issue
);
480 for (cp
= buf
+ i
; i
> 1; i
--)
487 printf ("\n------- End of Forwarded Message%s\n\n",
488 vecp
> 1 ? "s" : "");
493 adios("output", "error writing");
496 if (clearflg
> 0 && ontty
== NOTTY
)
507 mhl_format (char *file
, int length
, int width
)
511 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
515 static dev_t dev
= 0;
516 static ino_t ino
= 0;
517 static time_t mtime
= 0;
520 if (stat (etcpath (file
), &st
) != NOTOK
521 && mtime
== st
.st_mtime
526 free_queue (&fmthd
, &fmttl
);
529 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
530 adios (file
, "unable to open format file");
532 if (fstat (fileno (fp
), &st
) != NOTOK
) {
538 global
.c_ovtxt
= global
.c_nfs
= NULL
;
542 if ((i
= sc_width ()) > 5)
544 global
.c_cwidth
= -1;
545 if ((i
= sc_length ()) > 5)
546 global
.c_length
= i
- 1;
547 global
.c_flags
= BELL
; /* BELL is default */
548 *(ip
= ignores
) = NULL
;
550 while (vfgets (fp
, &ap
) == OK
) {
555 if ((cp
= strchr(bp
, '\n')))
559 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
564 strncpy (name
, parse(), sizeof(name
));
570 * Split this list of fields to ignore, and copy
571 * it to the end of the current "ignores" list.
573 if (!mh_strcasecmp (name
, "ignores")) {
574 char **tmparray
, **p
;
577 /* split the fields */
578 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
580 /* count number of fields split */
585 /* copy pointers to split fields to ignores array */
586 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
592 if (evalvar (&global
))
593 adios (NULL
, "format file syntax error: %s", bp
);
600 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
601 while (*parptr
== ':' || *parptr
== ',') {
604 adios (NULL
, "format file syntax error: %s", bp
);
606 if (!c1
->c_nfs
&& global
.c_nfs
) {
607 if (c1
->c_flags
& DATEFMT
) {
608 if (global
.c_flags
& DATEFMT
)
609 c1
->c_nfs
= getcpy (global
.c_nfs
);
612 if (c1
->c_flags
& ADDRFMT
) {
613 if (global
.c_flags
& ADDRFMT
)
614 c1
->c_nfs
= getcpy (global
.c_nfs
);
620 adios (NULL
, "format file syntax error: %s", bp
);
626 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
627 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
628 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
629 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
630 (unsigned int)(unsigned long) c1
->c_nfs
,
631 (unsigned int)(unsigned long) c1
->c_fmt
);
632 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
633 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
634 c1
->c_cwidth
, c1
->c_length
);
635 fprintf (stderr
, "\tflags=%s\n",
636 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
642 global
.c_flags
|= CLEARSCR
;
645 global
.c_flags
&= ~CLEARSCR
;
648 switch (bellflg
) { /* command line may override format file */
650 global
.c_flags
|= BELL
;
653 global
.c_flags
&= ~BELL
;
658 global
.c_length
= length
;
660 global
.c_width
= width
;
661 if (global
.c_length
< 5)
662 global
.c_length
= 10000;
663 if (global
.c_width
< 5)
664 global
.c_width
= 10000;
669 evalvar (struct mcomp
*c1
)
671 char *cp
, name
[NAMESZ
];
676 strncpy (name
, parse(), sizeof(name
));
678 if (!mh_strcasecmp (name
, "component")) {
679 if (ptos (name
, &c1
->c_text
))
681 c1
->c_flags
&= ~NOCOMPONENT
;
685 if (!mh_strcasecmp (name
, "overflowtext"))
686 return ptos (name
, &c1
->c_ovtxt
);
688 if (!mh_strcasecmp (name
, "formatfield")) {
691 if (ptos (name
, &cp
))
693 nfs
= new_fs (NULL
, NULL
, cp
);
694 c1
->c_nfs
= getcpy (nfs
);
695 c1
->c_flags
|= FORMAT
;
699 if (!mh_strcasecmp (name
, "decode")) {
702 nfs
= new_fs (NULL
, NULL
, "%(decode{text})");
703 c1
->c_nfs
= getcpy (nfs
);
704 c1
->c_flags
|= FORMAT
;
708 if (!mh_strcasecmp (name
, "offset"))
709 return ptoi (name
, &c1
->c_offset
);
710 if (!mh_strcasecmp (name
, "overflowoffset"))
711 return ptoi (name
, &c1
->c_ovoff
);
712 if (!mh_strcasecmp (name
, "width"))
713 return ptoi (name
, &c1
->c_width
);
714 if (!mh_strcasecmp (name
, "compwidth"))
715 return ptoi (name
, &c1
->c_cwidth
);
716 if (!mh_strcasecmp (name
, "length"))
717 return ptoi (name
, &c1
->c_length
);
718 if (!mh_strcasecmp (name
, "nodashstuffing"))
719 return (dashstuff
= -1);
721 for (ap
= triples
; ap
->t_name
; ap
++)
722 if (!mh_strcasecmp (ap
->t_name
, name
)) {
723 c1
->c_flags
|= ap
->t_on
;
724 c1
->c_flags
&= ~ap
->t_off
;
733 ptoi (char *name
, int *i
)
737 if (*parptr
++ != '=' || !*(cp
= parse ())) {
738 advise (NULL
, "missing argument to variable %s", name
);
748 ptos (char *name
, char **s
)
752 if (*parptr
++ != '=') {
753 advise (NULL
, "missing argument to variable %s", name
);
757 if (*parptr
!= '"') {
759 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
763 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
764 if (*parptr
== QUOTE
)
771 if ((*parptr
= c
) == '"')
782 static char result
[NAMESZ
];
784 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
803 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
809 switch (m_setjmp (env
)) {
812 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
814 advise (fname
, "unable to open");
822 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
824 SIGNAL (SIGINT
, intrser
);
825 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
829 SIGNAL (SIGINT
, SIG_IGN
);
830 if (mhl_action
== NULL
&& fp
!= stdin
)
834 free (holder
.c_text
);
835 holder
.c_text
= NULL
;
837 free_queue (&msghd
, &msgtl
);
838 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
839 c1
->c_flags
&= ~HDROUTPUT
;
846 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
849 struct mcomp
*c1
, *c2
, *c3
;
850 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
854 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
856 printf ("\n-------");
858 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
860 printf (" Message %d", ofilen
);
868 if ((global
.c_flags
& CLEARSCR
))
873 printf (">>> %s\n\n", mname
);
878 strncpy (buf
, "\n", sizeof(buf
));
880 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
883 printf ("Press <return> to list \"%s\"...", mname
);
887 read (fileno (stdout
), buf
, sizeof(buf
));
889 if (strchr(buf
, '\n')) {
890 if ((global
.c_flags
& CLEARSCR
))
904 printf (">>> %s\n\n", mname
);
910 for (state
= FLD
;;) {
911 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
914 for (ip
= ignores
; *ip
; ip
++)
915 if (!mh_strcasecmp (name
, *ip
)) {
916 while (state
== FLDPLUS
)
917 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
923 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
924 if (!mh_strcasecmp (c2
->c_name
, name
))
927 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
928 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
929 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
931 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
935 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
936 while (state
== FLDPLUS
) {
937 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
938 c1
->c_text
= add (buf
, c1
->c_text
);
941 c1
->c_flags
|= EXTRA
;
947 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
948 if (c1
->c_flags
& CLEARTEXT
) {
949 putcomp (c1
, c1
, ONECOMP
);
952 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
953 holder
.c_text
= concat ("(Message ", mname
, ")\n",
955 putcomp (c1
, &holder
, ONECOMP
);
956 free (holder
.c_text
);
957 holder
.c_text
= NULL
;
960 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
961 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
962 if (c2
->c_flags
& EXTRA
)
963 putcomp (c1
, c2
, TWOCOMP
);
966 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
967 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
968 formatproc
!= NULL
) {
969 filterbody(c1
, buf
, sizeof(buf
), state
, fp
);
971 holder
.c_text
= mh_xmalloc (sizeof(buf
));
972 strncpy (holder
.c_text
, buf
, sizeof(buf
));
973 while (state
== BODY
) {
974 putcomp (c1
, &holder
, BODYCOMP
);
975 state
= m_getfld (state
, name
, holder
.c_text
,
978 free (holder
.c_text
);
979 holder
.c_text
= NULL
;
983 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
984 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
985 putcomp (c1
, c2
, ONECOMP
);
986 if (!(c1
->c_flags
& SPLIT
))
989 if (faceproc
&& c2
== NULL
&& (c1
->c_flags
& FACEFMT
))
990 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
991 if (c2
->c_flags
& FACEDFLT
) {
992 if (c2
->c_face
== NULL
)
994 if ((holder
.c_text
= c2
->c_face
)) {
995 putcomp (c1
, &holder
, ONECOMP
);
996 holder
.c_text
= NULL
;
1005 advise (NULL
, "format error in message %s", mname
);
1010 adios (NULL
, "getfld() returned %d", state
);
1017 mcomp_flags (char *name
)
1021 for (ap
= pairs
; ap
->p_name
; ap
++)
1022 if (!mh_strcasecmp (ap
->p_name
, name
))
1023 return (ap
->p_flags
);
1030 mcomp_add (long flags
, char *s1
, char *s2
)
1034 if (!(flags
& ADDRFMT
))
1035 return add (s1
, s2
);
1037 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1040 return add (s1
, add (",\n", s2
));
1047 struct pqpair
*pq_next
;
1052 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1056 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1058 struct pqpair
*p
, *q
;
1060 struct mailname
*mp
;
1067 dat
[3] = sizeof(buffer
) - 1;
1069 fmt_compile (c1
->c_nfs
, &c1
->c_fmt
);
1071 if (!(c1
->c_flags
& ADDRFMT
)) {
1072 FINDCOMP (cptr
, "text");
1075 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1079 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1080 /* Don't need to append a newline, dctime() already did */
1081 c2
->c_text
= getcpy (buffer
);
1087 (q
= &pq
)->pq_next
= NULL
;
1088 while ((cp
= getname (ap
))) {
1089 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1090 adios (NULL
, "unable to allocate pqpair memory");
1092 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1093 p
->pq_text
= getcpy (cp
);
1094 p
->pq_error
= getcpy (error
);
1096 if ((c1
->c_flags
& FACEDFLT
) && c2
->c_face
== NULL
) {
1098 if ((h
= mp
->m_host
) == NULL
)
1100 if ((o
= OfficialName (h
)))
1102 c2
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
,
1105 p
->pq_text
= getcpy (mp
->m_text
);
1108 q
= (q
->pq_next
= p
);
1111 for (p
= pq
.pq_next
; p
; p
= q
) {
1112 FINDCOMP (cptr
, "text");
1114 cptr
->c_text
= p
->pq_text
;
1115 FINDCOMP (cptr
, "error");
1117 cptr
->c_text
= p
->pq_error
;
1119 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1122 c2
->c_text
= add (",\n", c2
->c_text
);
1123 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1125 c2
->c_text
= add (buffer
, c2
->c_text
);
1135 c2
->c_text
= add ("\n", c2
->c_text
);
1140 static struct mcomp
*
1141 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1145 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1146 adios (NULL
, "unable to allocate comp memory");
1148 c1
->c_flags
= flags
& ~INIT
;
1149 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1150 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1151 c1
->c_text
= text
? getcpy (text
) : NULL
;
1154 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1155 c1
->c_offset
= global
.c_offset
;
1156 c1
->c_ovoff
= global
. c_ovoff
;
1157 c1
->c_width
= c1
->c_length
= 0;
1158 c1
->c_cwidth
= global
.c_cwidth
;
1159 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1164 (*tail
)->c_next
= c1
;
1172 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1174 struct mcomp
*c1
, *c2
;
1176 for (c1
= *head
; c1
; c1
= c2
) {
1187 free ((char *) c1
->c_fmt
);
1193 *head
= *tail
= NULL
;
1198 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1205 llim
= c1
->c_length
? c1
->c_length
: -1;
1206 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1207 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1209 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1211 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1212 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1213 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1216 if (c1
->c_flags
& CLEARTEXT
) {
1217 putstr (c1
->c_text
, c1
->c_flags
);
1218 putstr ("\n", c1
->c_flags
);
1222 if (c1
->c_flags
& FACEFMT
)
1223 switch (doface (c2
)) {
1224 case NOTOK
: /* error */
1225 case OK
: /* async faceproc */
1228 default: /* sync faceproc */
1232 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1233 mcomp_format (c1
, c2
);
1235 if (c1
->c_flags
& CENTER
) {
1236 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1237 - c1
->c_offset
- strlen (c2
->c_text
);
1238 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1239 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1240 lm
= c1
->c_offset
+ (count
/ 2);
1246 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1247 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1248 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1250 *cp
= toupper (*cp
);
1251 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1252 if (flag
!= BODYCOMP
) {
1253 putstr (": ", c1
->c_flags
);
1254 if (!(c1
->c_flags
& SPLIT
))
1255 c1
->c_flags
|= HDROUTPUT
;
1258 if ((count
= c1
->c_cwidth
-
1259 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1261 putstr (" ", c1
->c_flags
);
1264 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1268 && !(c2
->c_flags
& HDROUTPUT
)
1269 && !(c2
->c_flags
& NOCOMPONENT
)) {
1270 if (c1
->c_flags
& UPPERCASE
)
1271 for (cp
= c2
->c_name
; *cp
; cp
++)
1273 *cp
= toupper (*cp
);
1274 putstr (c2
->c_name
, c1
->c_flags
);
1275 putstr (": ", c1
->c_flags
);
1276 if (!(c1
->c_flags
& SPLIT
))
1277 c2
->c_flags
|= HDROUTPUT
;
1280 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1282 putstr (" ", c1
->c_flags
);
1284 if (c1
->c_flags
& UPPERCASE
)
1285 for (cp
= c2
->c_text
; *cp
; cp
++)
1287 *cp
= toupper (*cp
);
1291 if (flag
== TWOCOMP
)
1292 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1293 : (int) strlen (c2
->c_name
) + 2;
1295 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1296 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1298 count
+= c1
->c_offset
;
1300 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1301 putstr(cp
, c1
->c_flags
);
1303 putstr ("\n", c1
->c_flags
);
1304 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1306 if (flag
== BODYCOMP
1307 && !(c1
->c_flags
& NOCOMPONENT
))
1308 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1310 putstr (cp
, c1
->c_flags
);
1312 putstr ("\n", c1
->c_flags
);
1314 if (flag
== BODYCOMP
&& term
== '\n')
1315 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1320 oneline (char *stuff
, long flags
)
1328 return (onelp
= NULL
);
1332 if (flags
& COMPRESS
) {
1333 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1334 if (isspace (*onelp
)) {
1335 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1354 while (*onelp
&& *onelp
!= '\n')
1356 if (*onelp
== '\n') {
1360 if (flags
& LEFTADJUST
)
1361 while (*ret
== ' ' || *ret
== '\t')
1364 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1372 putstr (char *string
, long flags
)
1374 if (!column
&& lm
> 0) {
1377 putch ('\t', flags
);
1387 putch (*string
++, flags
);
1392 putch (char ch
, long flags
)
1405 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1407 if (global
.c_flags
& BELL
)
1411 read (fileno (stdout
), buf
, sizeof(buf
));
1412 if (strchr(buf
, '\n')) {
1413 if (global
.c_flags
& CLEARSCR
)
1418 row
= global
.c_length
/ 3;
1437 * If we are forwarding this message, and the first
1438 * column contains a dash, then add a dash and a space.
1440 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1449 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1450 putch ('\n', flags
);
1453 putstr (ovtxt
? ovtxt
: "", flags
);
1469 longjmp (env
, DONE
);
1494 face_format (struct mcomp
*c1
)
1497 struct mailname
*mp
;
1499 if ((cp
= c1
->c_text
) == NULL
)
1502 if ((cp
= getname (cp
))) {
1503 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, NULL
))) {
1505 if ((h
= mp
->m_host
) == NULL
)
1507 if ((o
= OfficialName (h
)))
1509 c1
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
, NULL
);
1512 while ((cp
= getname (cp
)))
1519 * faceproc is two elements defining the image agent's location:
1524 #include <sys/socket.h>
1525 #include <netinet/in.h>
1527 #include <arpa/inet.h>
1530 doface (struct mcomp
*c1
)
1533 static int inited
= OK
;
1534 static struct sockaddr_storage ss
;
1535 static socklen_t socklen
;
1536 static int socktype
;
1537 static int protocol
;
1541 char **ap
= brkstring (cp
= getcpy (faceproc
), " ", "\n");
1542 struct addrinfo hints
, *res
;
1544 if (ap
[0] == NULL
|| ap
[1] == NULL
) {
1547 return (inited
= NOTOK
);
1550 memset(&hints
, 0, sizeof(hints
));
1551 #ifdef AI_ADDRCONFIG
1552 hints
.ai_flags
= AI_ADDRCONFIG
;
1554 hints
.ai_family
= PF_UNSPEC
;
1555 hints
.ai_socktype
= SOCK_DGRAM
;
1557 if (getaddrinfo(ap
[0], ap
[1], &hints
, &res
) != 0)
1560 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
1561 socklen
= res
->ai_addrlen
;
1562 socktype
= res
->ai_socktype
;
1563 protocol
= res
->ai_protocol
;
1568 if (inited
== NOTOK
)
1571 if ((sd
= socket (ss
.ss_family
, socktype
, protocol
)) == NOTOK
)
1574 result
= sendto (sd
, c1
->c_text
, strlen (c1
->c_text
), 0,
1575 (struct sockaddr
*) &ss
, socklen
);
1579 return (result
!= NOTOK
? OK
: NOTOK
);
1584 * This version doesn't use sockets
1589 doface (struct mcomp
*c1
)
1593 int result
, pdi
[2], pdo
[2];
1595 char buffer
[BUFSIZ
], *vec
[10];
1597 if (pipe (pdi
) == NOTOK
)
1599 if (pipe (pdo
) == NOTOK
) {
1605 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
1610 /* oops... fork error */
1615 SIGNAL (SIGINT
, SIG_IGN
);
1616 SIGNAL (SIGQUIT
, SIG_IGN
);
1617 if (pdi
[0] != fileno (stdin
)) {
1618 dup2 (pdi
[0], fileno (stdin
));
1623 if (pdo
[1] != fileno (stdout
)) {
1624 dup2 (pdo
[1], fileno (stdout
));
1628 vec
[vecp
++] = r1bindex (faceproc
, '/');
1630 if (sleepsw
!= NOTOK
) {
1632 snprintf (buffer
, sizeof(buffer
), "%d", sleepsw
);
1633 vec
[vecp
++] = buffer
;
1636 execvp (faceproc
, vec
);
1637 fprintf (stderr
, "unable to exec ");
1639 _exit (-1); /* NOTREACHED */
1642 /* parent process */
1644 i
= strlen (c1
->c_text
);
1645 if (write (pdi
[1], c1
->c_text
, i
) != i
)
1646 adios ("pipe", "error writing to");
1647 free (c1
->c_text
), c1
->c_text
= NULL
;
1653 while ((i
= read (pdo
[0], buffer
, strlen (buffer
))) > 0) {
1657 dp
= mh_xrealloc (cp
, (unsigned) (j
= len
+ i
));
1658 memcpy(dp
+ len
, buffer
, i
);
1662 cp
= mh_xmalloc ((unsigned) i
);
1663 memcpy(cp
, buffer
, i
);
1667 for (bp
= buffer
+ i
- 1; bp
>= buffer
; bp
--)
1668 if (!isascii (*bp
) || iscntrl (*bp
)) {
1675 /* no waiting for child... */
1677 if (result
== OK
) { /* binary */
1678 if (write (1, cp
, len
) != len
)
1679 adios ("writing", "error");
1683 if ((c1
->c_text
= cp
) == NULL
)
1690 #endif /* COMMENTED OUT */
1694 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1696 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1700 switch (m_setjmp (mhlenv
)) {
1703 sleepsw
= 0; /* XXX */
1704 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1707 mhl_action
= action
;
1710 * If signal is at default action, then start ignoring
1711 * it, else let it set to its current action.
1713 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1714 SIGNAL (SIGINT
, istat
);
1715 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1716 SIGNAL (SIGQUIT
, qstat
);
1717 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1718 mhl (argc
, argv
); /* FALL THROUGH! */
1721 SIGNAL (SIGINT
, istat
);
1722 SIGNAL (SIGQUIT
, qstat
);
1723 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1726 SIGNAL (SIGPIPE
, pstat
);
1728 if (holder
.c_text
) {
1729 free (holder
.c_text
);
1730 holder
.c_text
= NULL
;
1732 free_queue (&msghd
, &msgtl
);
1733 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1734 c1
->c_flags
&= ~HDROUTPUT
;
1743 mhladios (char *what
, char *fmt
, ...)
1748 advertise (what
, NULL
, fmt
, ap
);
1755 mhldone (int status
)
1759 longjmp (mhlenv
, DONE
);
1765 static int m_pid
= NOTOK
;
1766 static int sd
= NOTOK
;
1769 m_popen (char *name
)
1773 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1774 adios ("standard output", "unable to dup()");
1776 if (pipe (pd
) == NOTOK
)
1777 adios ("pipe", "unable to");
1779 switch (m_pid
= vfork()) {
1781 adios ("fork", "unable to");
1784 SIGNAL (SIGINT
, SIG_DFL
);
1785 SIGNAL (SIGQUIT
, SIG_DFL
);
1788 if (pd
[0] != fileno (stdin
)) {
1789 dup2 (pd
[0], fileno (stdin
));
1792 execlp (name
, r1bindex (name
, '/'), NULL
);
1793 fprintf (stderr
, "unable to exec ");
1799 if (pd
[1] != fileno (stdout
)) {
1800 dup2 (pd
[1], fileno (stdout
));
1815 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1816 adios ("standard output", "unable to dup2()");
1825 pidwait (m_pid
, OK
);
1831 * Filter the body of a message through a specified format program
1835 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
)
1837 struct mcomp holder
;
1839 int fdinput
[2], fdoutput
[2], waitstat
;
1841 pid_t writerpid
, filterpid
;
1844 * Create pipes so we can communicate with our filter process.
1847 if (pipe(fdinput
) < 0) {
1848 adios(NULL
, "Unable to create input pipe");
1851 if (pipe(fdoutput
) < 0) {
1852 adios(NULL
, "Unable to create output pipe");
1856 * Here's what we're doing to do.
1858 * - Fork ourselves and start writing data to the write side of the
1859 * input pipe (fdinput[1]).
1861 * - Fork and exec our filter program. We set the standard input of
1862 * our filter program to be the read side of our input pipe (fdinput[0]).
1863 * Standard output is set to the write side of our output pipe
1866 * - We read from the read side of the output pipe (fdoutput[0]).
1868 * We're forking because that's the simplest way to prevent any deadlocks.
1869 * (without doing something like switching to non-blocking I/O and using
1870 * select or poll, and I'm not interested in doing that).
1873 switch (writerpid
= fork()) {
1876 * Our child process - just write to the filter input (fdinput[1]).
1877 * Close all other descriptors that we don't need.
1885 * Call m_getfld() until we're no longer in the BODY state
1888 while (state
== BODY
) {
1889 write(fdinput
[1], buf
, strlen(buf
));
1890 state
= m_getfld(state
, name
, buf
, bufsz
, fp
);
1894 * We should be done; time to exit.
1901 adios(NULL
, "Unable to fork for filter writer process");
1906 * Fork and exec() our filter program, after redirecting standard in
1907 * and standard out appropriately.
1910 switch (filterpid
= fork()) {
1912 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1913 adios("formatproc", "Unable to dup2() standard input");
1915 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1916 adios("formatproc", "Unable to dup2() standard output");
1920 * Close everything (especially the old input and output
1921 * descriptors, since they've been dup'd to stdin and stdout),
1922 * and exec the formatproc.
1930 execlp(formatproc
, formatproc
, (char *) NULL
);
1932 adios(formatproc
, "Unable to execute filter");
1937 adios(NULL
, "Unable to fork format program");
1941 * Close everything except our reader (fdoutput[0]);
1949 * As we read in this data, send it to putcomp
1952 holder
.c_text
= buf
;
1954 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1956 putcomp(c1
, &holder
, BODYCOMP
);
1960 adios(NULL
, "reading from formatproc");
1964 * See if we got any errors along the way. I'm a little leery of calling
1965 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1968 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1969 if (errno
!= ECHILD
) {
1970 adios("filterproc", "Unable to determine status");
1973 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1974 pidstatus(waitstat
, stderr
, "filterproc");
1978 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1979 if (errno
!= ECHILD
) {
1980 adios("writer process", "Unable to determine status");
1984 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1985 pidstatus(waitstat
, stderr
, "writer process");