]>
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 *);
295 mhl (int argc
, char **argv
)
297 int length
= 0, nomore
= 0;
298 int i
, width
= 0, vecp
= 0;
299 char *cp
, *folder
= NULL
, *form
= NULL
;
300 char buf
[BUFSIZ
], *files
[MAXARGS
];
301 char **argp
, **arguments
;
303 invo_name
= r1bindex (argv
[0], '/');
305 /* read user profile/context */
308 arguments
= getarguments (invo_name
, argc
, argv
, 1);
311 if ((cp
= getenv ("MHLDEBUG")) && *cp
)
314 if ((cp
= getenv ("FACEPROC")))
317 while ((cp
= *argp
++)) {
319 switch (smatch (++cp
, mhlswitches
)) {
321 ambigsw (cp
, mhlswitches
);
324 adios (NULL
, "-%s unknown\n", cp
);
327 snprintf (buf
, sizeof(buf
), "%s [switches] [files ...]", invo_name
);
328 print_help (buf
, mhlswitches
, 1);
331 print_version(invo_name
);
349 if (!(folder
= *argp
++) || *folder
== '-')
350 adios (NULL
, "missing argument to %s", argp
[-2]);
353 if (!(form
= *argp
++) || *form
== '-')
354 adios (NULL
, "missing argument to %s", argp
[-2]);
358 if (!(faceproc
= *argp
++) || *faceproc
== '-')
359 adios (NULL
, "missing argument to %s", argp
[-2]);
365 if (!(cp
= *argp
++) || *cp
== '-')
366 adios (NULL
, "missing argument to %s", argp
[-2]);
367 sleepsw
= atoi (cp
);/* ZERO ok! */
371 if (!(moreproc
= *argp
++) || *moreproc
== '-')
372 adios (NULL
, "missing argument to %s", argp
[-2]);
379 if (!(cp
= *argp
++) || *cp
== '-')
380 adios (NULL
, "missing argument to %s", argp
[-2]);
381 if ((length
= atoi (cp
)) < 1)
382 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
385 if (!(cp
= *argp
++) || *cp
== '-')
386 adios (NULL
, "missing argument to %s", argp
[-2]);
387 if ((width
= atoi (cp
)) < 1)
388 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
392 if (!(digest
= *argp
++) || *digest
== '-')
393 adios (NULL
, "missing argument to %s", argp
[-2]);
396 if (!(cp
= *argp
++) || *cp
== '-')
397 adios (NULL
, "missing argument to %s", argp
[-2]);
398 if ((issue
= atoi (cp
)) < 1)
399 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
402 if (!(cp
= *argp
++) || *cp
== '-')
403 adios (NULL
, "missing argument to %s", argp
[-2]);
404 if ((volume
= atoi (cp
)) < 1)
405 adios (NULL
, "bad argument %s %s", argp
[-2], cp
);
409 forwall
++; /* fall */
412 clearflg
= -1;/* XXX */
416 dashstuff
= 1; /* trinary logic */
419 dashstuff
= -1; /* trinary logic */
431 folder
= getenv ("mhfolder");
433 if (isatty (fileno (stdout
))) {
434 if (!nomore
&& !sc_hardcopy() && moreproc
&& *moreproc
!= '\0') {
436 SIGNAL (SIGINT
, SIG_IGN
);
437 SIGNAL2 (SIGQUIT
, quitser
);
439 SIGNAL2 (SIGPIPE
, pipeser
);
443 SIGNAL (SIGINT
, SIG_IGN
);
444 SIGNAL2 (SIGQUIT
, quitser
);
451 mhl_format (form
? form
: mhlformat
, length
, width
);
454 process (folder
, NULL
, 1, vecp
= 1);
456 for (i
= 0; i
< vecp
; i
++)
457 process (folder
, files
[i
], i
+ 1, vecp
);
462 printf ("%s", delim4
);
464 snprintf (buf
, sizeof(buf
), "End of %s Digest\n", digest
);
466 snprintf (buf
, sizeof(buf
), "End of %s Digest [Volume %d Issue %d]\n",
467 digest
, volume
, issue
);
470 for (cp
= buf
+ i
; i
> 1; i
--)
477 printf ("\n------- End of Forwarded Message%s\n",
478 vecp
> 1 ? "s" : "");
483 adios("output", "error writing");
486 if (clearflg
> 0 && ontty
== NOTTY
)
497 mhl_format (char *file
, int length
, int width
)
501 char *ap
, buffer
[BUFSIZ
], name
[NAMESZ
];
505 static dev_t dev
= 0;
506 static ino_t ino
= 0;
507 static time_t mtime
= 0;
510 if (stat (etcpath (file
), &st
) != NOTOK
511 && mtime
== st
.st_mtime
516 free_queue (&fmthd
, &fmttl
);
519 if ((fp
= fopen (etcpath (file
), "r")) == NULL
)
520 adios (file
, "unable to open format file");
522 if (fstat (fileno (fp
), &st
) != NOTOK
) {
528 global
.c_ovtxt
= global
.c_nfs
= NULL
;
532 if ((i
= sc_width ()) > 5)
534 global
.c_cwidth
= -1;
535 if ((i
= sc_length ()) > 5)
536 global
.c_length
= i
- 1;
537 global
.c_flags
= BELL
; /* BELL is default */
538 *(ip
= ignores
) = NULL
;
540 while (vfgets (fp
, &ap
) == OK
) {
545 if ((cp
= strchr(bp
, '\n')))
549 c1
= add_queue (&fmthd
, &fmttl
, NULL
, bp
+ 1, CLEARTEXT
);
554 strncpy (name
, parse(), sizeof(name
));
560 * Split this list of fields to ignore, and copy
561 * it to the end of the current "ignores" list.
563 if (!mh_strcasecmp (name
, "ignores")) {
564 char **tmparray
, **p
;
567 /* split the fields */
568 tmparray
= brkstring (getcpy (++parptr
), ",", NULL
);
570 /* count number of fields split */
575 /* copy pointers to split fields to ignores array */
576 ip
= copyip (tmparray
, ip
, MAXARGS
- num_ignores
);
582 if (evalvar (&global
))
583 adios (NULL
, "format file syntax error: %s", bp
);
590 c1
= add_queue (&fmthd
, &fmttl
, name
, NULL
, INIT
);
591 while (*parptr
== ':' || *parptr
== ',') {
594 adios (NULL
, "format file syntax error: %s", bp
);
596 if (!c1
->c_nfs
&& global
.c_nfs
) {
597 if (c1
->c_flags
& DATEFMT
) {
598 if (global
.c_flags
& DATEFMT
)
599 c1
->c_nfs
= getcpy (global
.c_nfs
);
602 if (c1
->c_flags
& ADDRFMT
) {
603 if (global
.c_flags
& ADDRFMT
)
604 c1
->c_nfs
= getcpy (global
.c_nfs
);
610 adios (NULL
, "format file syntax error: %s", bp
);
616 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
617 fprintf (stderr
, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
618 c1
->c_name
, c1
->c_text
, c1
->c_ovtxt
);
619 fprintf (stderr
, "\tnfs=0x%x fmt=0x%x\n",
620 (unsigned int)(unsigned long) c1
->c_nfs
,
621 (unsigned int)(unsigned long) c1
->c_fmt
);
622 fprintf (stderr
, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
623 c1
->c_offset
, c1
->c_ovoff
, c1
->c_width
,
624 c1
->c_cwidth
, c1
->c_length
);
625 fprintf (stderr
, "\tflags=%s\n",
626 snprintb (buffer
, sizeof(buffer
), (unsigned) c1
->c_flags
, LBITS
));
632 global
.c_flags
|= CLEARSCR
;
635 global
.c_flags
&= ~CLEARSCR
;
638 switch (bellflg
) { /* command line may override format file */
640 global
.c_flags
|= BELL
;
643 global
.c_flags
&= ~BELL
;
648 global
.c_length
= length
;
650 global
.c_width
= width
;
651 if (global
.c_length
< 5)
652 global
.c_length
= 10000;
653 if (global
.c_width
< 5)
654 global
.c_width
= 10000;
659 evalvar (struct mcomp
*c1
)
661 char *cp
, name
[NAMESZ
];
666 strncpy (name
, parse(), sizeof(name
));
668 if (!mh_strcasecmp (name
, "component")) {
669 if (ptos (name
, &c1
->c_text
))
671 c1
->c_flags
&= ~NOCOMPONENT
;
675 if (!mh_strcasecmp (name
, "overflowtext"))
676 return ptos (name
, &c1
->c_ovtxt
);
678 if (!mh_strcasecmp (name
, "formatfield")) {
681 if (ptos (name
, &cp
))
683 nfs
= new_fs (NULL
, NULL
, cp
);
684 c1
->c_nfs
= getcpy (nfs
);
685 c1
->c_flags
|= FORMAT
;
689 if (!mh_strcasecmp (name
, "decode")) {
692 nfs
= new_fs (NULL
, NULL
, "%(decode{text})");
693 c1
->c_nfs
= getcpy (nfs
);
694 c1
->c_flags
|= FORMAT
;
698 if (!mh_strcasecmp (name
, "offset"))
699 return ptoi (name
, &c1
->c_offset
);
700 if (!mh_strcasecmp (name
, "overflowoffset"))
701 return ptoi (name
, &c1
->c_ovoff
);
702 if (!mh_strcasecmp (name
, "width"))
703 return ptoi (name
, &c1
->c_width
);
704 if (!mh_strcasecmp (name
, "compwidth"))
705 return ptoi (name
, &c1
->c_cwidth
);
706 if (!mh_strcasecmp (name
, "length"))
707 return ptoi (name
, &c1
->c_length
);
708 if (!mh_strcasecmp (name
, "nodashstuffing"))
709 return (dashstuff
= -1);
711 for (ap
= triples
; ap
->t_name
; ap
++)
712 if (!mh_strcasecmp (ap
->t_name
, name
)) {
713 c1
->c_flags
|= ap
->t_on
;
714 c1
->c_flags
&= ~ap
->t_off
;
723 ptoi (char *name
, int *i
)
727 if (*parptr
++ != '=' || !*(cp
= parse ())) {
728 advise (NULL
, "missing argument to variable %s", name
);
738 ptos (char *name
, char **s
)
742 if (*parptr
++ != '=') {
743 advise (NULL
, "missing argument to variable %s", name
);
747 if (*parptr
!= '"') {
749 *parptr
&& *parptr
!= ':' && *parptr
!= ',';
753 for (cp
= ++parptr
; *parptr
&& *parptr
!= '"'; parptr
++)
754 if (*parptr
== QUOTE
)
761 if ((*parptr
= c
) == '"')
772 static char result
[NAMESZ
];
774 for (cp
= result
; *parptr
&& (cp
- result
< NAMESZ
); parptr
++) {
793 process (char *folder
, char *fname
, int ofilen
, int ofilec
)
799 switch (m_setjmp (env
)) {
802 fp
= mhl_action
? (*mhl_action
) (fname
) : fopen (fname
, "r");
804 advise (fname
, "unable to open");
812 cp
= folder
? concat (folder
, ":", fname
, NULL
) : getcpy (fname
);
814 SIGNAL (SIGINT
, intrser
);
815 mhlfile (fp
, cp
, ofilen
, ofilec
); /* FALL THROUGH! */
819 SIGNAL (SIGINT
, SIG_IGN
);
820 if (mhl_action
== NULL
&& fp
!= stdin
)
824 free (holder
.c_text
);
825 holder
.c_text
= NULL
;
827 free_queue (&msghd
, &msgtl
);
828 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
829 c1
->c_flags
&= ~HDROUTPUT
;
836 mhlfile (FILE *fp
, char *mname
, int ofilen
, int ofilec
)
839 struct mcomp
*c1
, *c2
, *c3
;
840 char **ip
, name
[NAMESZ
], buf
[BUFSIZ
];
844 printf ("%s", ofilen
== 1 ? delim3
: delim4
);
846 printf ("\n-------");
848 printf (" Forwarded Message%s", ofilec
> 1 ? "s" : "");
850 printf (" Message %d", ofilen
);
858 if ((global
.c_flags
& CLEARSCR
))
863 printf (">>> %s\n\n", mname
);
868 strncpy (buf
, "\n", sizeof(buf
));
870 if (SOprintf ("Press <return> to list \"%s\"...", mname
)) {
873 printf ("Press <return> to list \"%s\"...", mname
);
877 read (fileno (stdout
), buf
, sizeof(buf
));
879 if (strchr(buf
, '\n')) {
880 if ((global
.c_flags
& CLEARSCR
))
894 printf (">>> %s\n\n", mname
);
900 for (state
= FLD
;;) {
901 switch (state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
)) {
904 for (ip
= ignores
; *ip
; ip
++)
905 if (!mh_strcasecmp (name
, *ip
)) {
906 while (state
== FLDPLUS
)
907 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
913 for (c2
= fmthd
; c2
; c2
= c2
->c_next
)
914 if (!mh_strcasecmp (c2
->c_name
, name
))
917 if (!((c3
= c2
? c2
: &global
)->c_flags
& SPLIT
))
918 for (c1
= msghd
; c1
; c1
= c1
->c_next
)
919 if (!mh_strcasecmp (c1
->c_name
, c3
->c_name
)) {
921 mcomp_add (c1
->c_flags
, buf
, c1
->c_text
);
925 c1
= add_queue (&msghd
, &msgtl
, name
, buf
, 0);
926 while (state
== FLDPLUS
) {
927 state
= m_getfld (state
, name
, buf
, sizeof(buf
), fp
);
928 c1
->c_text
= add (buf
, c1
->c_text
);
931 c1
->c_flags
|= EXTRA
;
937 for (c1
= fmthd
; c1
; c1
= c1
->c_next
) {
938 if (c1
->c_flags
& CLEARTEXT
) {
939 putcomp (c1
, c1
, ONECOMP
);
942 if (!mh_strcasecmp (c1
->c_name
, "messagename")) {
943 holder
.c_text
= concat ("(Message ", mname
, ")\n",
945 putcomp (c1
, &holder
, ONECOMP
);
946 free (holder
.c_text
);
947 holder
.c_text
= NULL
;
950 if (!mh_strcasecmp (c1
->c_name
, "extras")) {
951 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
952 if (c2
->c_flags
& EXTRA
)
953 putcomp (c1
, c2
, TWOCOMP
);
956 if (dobody
&& !mh_strcasecmp (c1
->c_name
, "body")) {
957 if (c1
->c_flags
& FMTFILTER
&& state
== BODY
&&
958 formatproc
!= NULL
) {
959 filterbody(c1
, buf
, sizeof(buf
), state
, fp
);
961 holder
.c_text
= mh_xmalloc (sizeof(buf
));
962 strncpy (holder
.c_text
, buf
, sizeof(buf
));
963 while (state
== BODY
) {
964 putcomp (c1
, &holder
, BODYCOMP
);
965 state
= m_getfld (state
, name
, holder
.c_text
,
968 free (holder
.c_text
);
969 holder
.c_text
= NULL
;
973 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
974 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
975 putcomp (c1
, c2
, ONECOMP
);
976 if (!(c1
->c_flags
& SPLIT
))
979 if (faceproc
&& c2
== NULL
&& (c1
->c_flags
& FACEFMT
))
980 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
981 if (c2
->c_flags
& FACEDFLT
) {
982 if (c2
->c_face
== NULL
)
984 if ((holder
.c_text
= c2
->c_face
)) {
985 putcomp (c1
, &holder
, ONECOMP
);
986 holder
.c_text
= NULL
;
995 advise (NULL
, "format error in message %s", mname
);
1000 adios (NULL
, "getfld() returned %d", state
);
1007 mcomp_flags (char *name
)
1011 for (ap
= pairs
; ap
->p_name
; ap
++)
1012 if (!mh_strcasecmp (ap
->p_name
, name
))
1013 return (ap
->p_flags
);
1020 mcomp_add (long flags
, char *s1
, char *s2
)
1024 if (!(flags
& ADDRFMT
))
1025 return add (s1
, s2
);
1027 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1030 return add (s1
, add (",\n", s2
));
1037 struct pqpair
*pq_next
;
1042 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1046 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1048 struct pqpair
*p
, *q
;
1050 struct mailname
*mp
;
1057 dat
[3] = sizeof(buffer
) - 1;
1059 fmt_compile (c1
->c_nfs
, &c1
->c_fmt
);
1061 if (!(c1
->c_flags
& ADDRFMT
)) {
1062 FINDCOMP (cptr
, "text");
1065 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1069 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1070 /* Don't need to append a newline, dctime() already did */
1071 c2
->c_text
= getcpy (buffer
);
1077 (q
= &pq
)->pq_next
= NULL
;
1078 while ((cp
= getname (ap
))) {
1079 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1080 adios (NULL
, "unable to allocate pqpair memory");
1082 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1083 p
->pq_text
= getcpy (cp
);
1084 p
->pq_error
= getcpy (error
);
1086 if ((c1
->c_flags
& FACEDFLT
) && c2
->c_face
== NULL
) {
1088 if ((h
= mp
->m_host
) == NULL
)
1090 if ((o
= OfficialName (h
)))
1092 c2
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
,
1095 p
->pq_text
= getcpy (mp
->m_text
);
1098 q
= (q
->pq_next
= p
);
1101 for (p
= pq
.pq_next
; p
; p
= q
) {
1102 FINDCOMP (cptr
, "text");
1104 cptr
->c_text
= p
->pq_text
;
1105 FINDCOMP (cptr
, "error");
1107 cptr
->c_text
= p
->pq_error
;
1109 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1112 c2
->c_text
= add (",\n", c2
->c_text
);
1113 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1115 c2
->c_text
= add (buffer
, c2
->c_text
);
1125 c2
->c_text
= add ("\n", c2
->c_text
);
1130 static struct mcomp
*
1131 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1135 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1136 adios (NULL
, "unable to allocate comp memory");
1138 c1
->c_flags
= flags
& ~INIT
;
1139 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1140 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1141 c1
->c_text
= text
? getcpy (text
) : NULL
;
1144 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1145 c1
->c_offset
= global
.c_offset
;
1146 c1
->c_ovoff
= global
. c_ovoff
;
1147 c1
->c_width
= c1
->c_length
= 0;
1148 c1
->c_cwidth
= global
.c_cwidth
;
1149 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1154 (*tail
)->c_next
= c1
;
1162 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1164 struct mcomp
*c1
, *c2
;
1166 for (c1
= *head
; c1
; c1
= c2
) {
1177 free ((char *) c1
->c_fmt
);
1183 *head
= *tail
= NULL
;
1188 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1195 llim
= c1
->c_length
? c1
->c_length
: -1;
1196 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1197 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1199 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1201 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1202 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1203 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1206 if (c1
->c_flags
& CLEARTEXT
) {
1207 putstr (c1
->c_text
, c1
->c_flags
);
1208 putstr ("\n", c1
->c_flags
);
1212 if (c1
->c_flags
& FACEFMT
)
1213 switch (doface (c2
)) {
1214 case NOTOK
: /* error */
1215 case OK
: /* async faceproc */
1218 default: /* sync faceproc */
1222 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1223 mcomp_format (c1
, c2
);
1225 if (c1
->c_flags
& CENTER
) {
1226 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1227 - c1
->c_offset
- strlen (c2
->c_text
);
1228 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1229 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1230 lm
= c1
->c_offset
+ (count
/ 2);
1236 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1237 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1238 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1240 *cp
= toupper (*cp
);
1241 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1242 if (flag
!= BODYCOMP
) {
1243 putstr (": ", c1
->c_flags
);
1244 if (!(c1
->c_flags
& SPLIT
))
1245 c1
->c_flags
|= HDROUTPUT
;
1248 if ((count
= c1
->c_cwidth
-
1249 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1251 putstr (" ", c1
->c_flags
);
1254 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1258 && !(c2
->c_flags
& HDROUTPUT
)
1259 && !(c2
->c_flags
& NOCOMPONENT
)) {
1260 if (c1
->c_flags
& UPPERCASE
)
1261 for (cp
= c2
->c_name
; *cp
; cp
++)
1263 *cp
= toupper (*cp
);
1264 putstr (c2
->c_name
, c1
->c_flags
);
1265 putstr (": ", c1
->c_flags
);
1266 if (!(c1
->c_flags
& SPLIT
))
1267 c2
->c_flags
|= HDROUTPUT
;
1270 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1272 putstr (" ", c1
->c_flags
);
1274 if (c1
->c_flags
& UPPERCASE
)
1275 for (cp
= c2
->c_text
; *cp
; cp
++)
1277 *cp
= toupper (*cp
);
1281 if (flag
== TWOCOMP
)
1282 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1283 : (int) strlen (c2
->c_name
) + 2;
1285 count
= (c1
->c_cwidth
>= 0) ? (size_t) c1
->c_cwidth
1286 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1288 count
+= c1
->c_offset
;
1290 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1291 putstr(cp
, c1
->c_flags
);
1293 putstr ("\n", c1
->c_flags
);
1294 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1296 if (flag
== BODYCOMP
1297 && !(c1
->c_flags
& NOCOMPONENT
))
1298 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
, c1
->c_flags
);
1300 putstr (cp
, c1
->c_flags
);
1302 putstr ("\n", c1
->c_flags
);
1304 if (flag
== BODYCOMP
&& term
== '\n')
1305 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1310 oneline (char *stuff
, long flags
)
1318 return (onelp
= NULL
);
1322 if (flags
& COMPRESS
) {
1323 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1324 if (isspace (*onelp
)) {
1325 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1344 while (*onelp
&& *onelp
!= '\n')
1346 if (*onelp
== '\n') {
1350 if (flags
& LEFTADJUST
)
1351 while (*ret
== ' ' || *ret
== '\t')
1354 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1362 putstr (char *string
, long flags
)
1364 if (!column
&& lm
> 0) {
1367 putch ('\t', flags
);
1377 putch (*string
++, flags
);
1382 putch (char ch
, long flags
)
1395 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1397 if (global
.c_flags
& BELL
)
1401 read (fileno (stdout
), buf
, sizeof(buf
));
1402 if (strchr(buf
, '\n')) {
1403 if (global
.c_flags
& CLEARSCR
)
1408 row
= global
.c_length
/ 3;
1427 * If we are forwarding this message, and the first
1428 * column contains a dash, then add a dash and a space.
1430 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1439 if (column
>= wid
&& (flags
& NOWRAP
) == 0) {
1440 putch ('\n', flags
);
1443 putstr (ovtxt
? ovtxt
: "", flags
);
1459 longjmp (env
, DONE
);
1484 face_format (struct mcomp
*c1
)
1487 struct mailname
*mp
;
1489 if ((cp
= c1
->c_text
) == NULL
)
1492 if ((cp
= getname (cp
))) {
1493 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, NULL
))) {
1495 if ((h
= mp
->m_host
) == NULL
)
1497 if ((o
= OfficialName (h
)))
1499 c1
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
, NULL
);
1502 while ((cp
= getname (cp
)))
1509 * faceproc is two elements defining the image agent's location:
1514 #include <sys/socket.h>
1515 #include <netinet/in.h>
1517 #include <arpa/inet.h>
1520 doface (struct mcomp
*c1
)
1523 static int inited
= OK
;
1524 static struct sockaddr_storage ss
;
1525 static socklen_t socklen
;
1526 static int socktype
;
1527 static int protocol
;
1531 char **ap
= brkstring (cp
= getcpy (faceproc
), " ", "\n");
1532 struct addrinfo hints
, *res
;
1534 if (ap
[0] == NULL
|| ap
[1] == NULL
) {
1537 return (inited
= NOTOK
);
1540 memset(&hints
, 0, sizeof(hints
));
1541 #ifdef AI_ADDRCONFIG
1542 hints
.ai_flags
= AI_ADDRCONFIG
;
1544 hints
.ai_family
= PF_UNSPEC
;
1545 hints
.ai_socktype
= SOCK_DGRAM
;
1547 if (getaddrinfo(ap
[0], ap
[1], &hints
, &res
) != 0)
1550 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
1551 socklen
= res
->ai_addrlen
;
1552 socktype
= res
->ai_socktype
;
1553 protocol
= res
->ai_protocol
;
1558 if (inited
== NOTOK
)
1561 if ((sd
= socket (ss
.ss_family
, socktype
, protocol
)) == NOTOK
)
1564 result
= sendto (sd
, c1
->c_text
, strlen (c1
->c_text
), 0,
1565 (struct sockaddr
*) &ss
, socklen
);
1569 return (result
!= NOTOK
? OK
: NOTOK
);
1574 * This version doesn't use sockets
1579 doface (struct mcomp
*c1
)
1583 int result
, pdi
[2], pdo
[2];
1585 char buffer
[BUFSIZ
], *vec
[10];
1587 if (pipe (pdi
) == NOTOK
)
1589 if (pipe (pdo
) == NOTOK
) {
1595 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
1600 /* oops... fork error */
1605 SIGNAL (SIGINT
, SIG_IGN
);
1606 SIGNAL (SIGQUIT
, SIG_IGN
);
1607 if (pdi
[0] != fileno (stdin
)) {
1608 dup2 (pdi
[0], fileno (stdin
));
1613 if (pdo
[1] != fileno (stdout
)) {
1614 dup2 (pdo
[1], fileno (stdout
));
1618 vec
[vecp
++] = r1bindex (faceproc
, '/');
1620 if (sleepsw
!= NOTOK
) {
1622 snprintf (buffer
, sizeof(buffer
), "%d", sleepsw
);
1623 vec
[vecp
++] = buffer
;
1626 execvp (faceproc
, vec
);
1627 fprintf (stderr
, "unable to exec ");
1629 _exit (-1); /* NOTREACHED */
1632 /* parent process */
1634 i
= strlen (c1
->c_text
);
1635 if (write (pdi
[1], c1
->c_text
, i
) != i
)
1636 adios ("pipe", "error writing to");
1637 free (c1
->c_text
), c1
->c_text
= NULL
;
1643 while ((i
= read (pdo
[0], buffer
, strlen (buffer
))) > 0) {
1647 dp
= mh_xrealloc (cp
, (unsigned) (j
= len
+ i
));
1648 memcpy(dp
+ len
, buffer
, i
);
1652 cp
= mh_xmalloc ((unsigned) i
);
1653 memcpy(cp
, buffer
, i
);
1657 for (bp
= buffer
+ i
- 1; bp
>= buffer
; bp
--)
1658 if (!isascii (*bp
) || iscntrl (*bp
)) {
1665 /* no waiting for child... */
1667 if (result
== OK
) { /* binary */
1668 if (write (1, cp
, len
) != len
)
1669 adios ("writing", "error");
1673 if ((c1
->c_text
= cp
) == NULL
)
1680 #endif /* COMMENTED OUT */
1684 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1686 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1690 switch (m_setjmp (mhlenv
)) {
1693 sleepsw
= 0; /* XXX */
1694 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1697 mhl_action
= action
;
1700 * If signal is at default action, then start ignoring
1701 * it, else let it set to its current action.
1703 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1704 SIGNAL (SIGINT
, istat
);
1705 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1706 SIGNAL (SIGQUIT
, qstat
);
1707 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1708 mhl (argc
, argv
); /* FALL THROUGH! */
1711 SIGNAL (SIGINT
, istat
);
1712 SIGNAL (SIGQUIT
, qstat
);
1713 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1716 SIGNAL (SIGPIPE
, pstat
);
1718 if (holder
.c_text
) {
1719 free (holder
.c_text
);
1720 holder
.c_text
= NULL
;
1722 free_queue (&msghd
, &msgtl
);
1723 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1724 c1
->c_flags
&= ~HDROUTPUT
;
1733 mhladios (char *what
, char *fmt
, ...)
1738 advertise (what
, NULL
, fmt
, ap
);
1745 mhldone (int status
)
1749 longjmp (mhlenv
, DONE
);
1755 static int m_pid
= NOTOK
;
1756 static int sd
= NOTOK
;
1759 m_popen (char *name
)
1763 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1764 adios ("standard output", "unable to dup()");
1766 if (pipe (pd
) == NOTOK
)
1767 adios ("pipe", "unable to");
1769 switch (m_pid
= vfork()) {
1771 adios ("fork", "unable to");
1774 SIGNAL (SIGINT
, SIG_DFL
);
1775 SIGNAL (SIGQUIT
, SIG_DFL
);
1778 if (pd
[0] != fileno (stdin
)) {
1779 dup2 (pd
[0], fileno (stdin
));
1782 execlp (name
, r1bindex (name
, '/'), NULL
);
1783 fprintf (stderr
, "unable to exec ");
1789 if (pd
[1] != fileno (stdout
)) {
1790 dup2 (pd
[1], fileno (stdout
));
1805 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1806 adios ("standard output", "unable to dup2()");
1815 pidwait (m_pid
, OK
);
1821 * Filter the body of a message through a specified format program
1825 filterbody (struct mcomp
*c1
, char *buf
, int bufsz
, int state
, FILE *fp
)
1827 struct mcomp holder
;
1829 int fdinput
[2], fdoutput
[2], waitstat
;
1831 pid_t writerpid
, filterpid
;
1834 * Create pipes so we can communicate with our filter process.
1837 if (pipe(fdinput
) < 0) {
1838 adios(NULL
, "Unable to create input pipe");
1841 if (pipe(fdoutput
) < 0) {
1842 adios(NULL
, "Unable to create output pipe");
1846 * Here's what we're doing to do.
1848 * - Fork ourselves and start writing data to the write side of the
1849 * input pipe (fdinput[1]).
1851 * - Fork and exec our filter program. We set the standard input of
1852 * our filter program to be the read side of our input pipe (fdinput[0]).
1853 * Standard output is set to the write side of our output pipe
1856 * - We read from the read side of the output pipe (fdoutput[0]).
1858 * We're forking because that's the simplest way to prevent any deadlocks.
1859 * (without doing something like switching to non-blocking I/O and using
1860 * select or poll, and I'm not interested in doing that).
1863 switch (writerpid
= fork()) {
1866 * Our child process - just write to the filter input (fdinput[1]).
1867 * Close all other descriptors that we don't need.
1875 * Call m_getfld() until we're no longer in the BODY state
1878 while (state
== BODY
) {
1879 write(fdinput
[1], buf
, strlen(buf
));
1880 state
= m_getfld(state
, name
, buf
, bufsz
, fp
);
1884 * We should be done; time to exit.
1891 adios(NULL
, "Unable to fork for filter writer process");
1896 * Fork and exec() our filter program, after redirecting standard in
1897 * and standard out appropriately.
1900 switch (filterpid
= fork()) {
1902 if (dup2(fdinput
[0], STDIN_FILENO
) < 0) {
1903 adios("formatproc", "Unable to dup2() standard input");
1905 if (dup2(fdoutput
[1], STDOUT_FILENO
) < 0) {
1906 adios("formatproc", "Unable to dup2() standard output");
1910 * Close everything (especially the old input and output
1911 * descriptors, since they've been dup'd to stdin and stdout),
1912 * and exec the formatproc.
1920 execlp(formatproc
, formatproc
, (char *) NULL
);
1922 adios(formatproc
, "Unable to execute filter");
1927 adios(NULL
, "Unable to fork format program");
1931 * Close everything except our reader (fdoutput[0]);
1939 * As we read in this data, send it to putcomp
1942 holder
.c_text
= buf
;
1944 while ((cc
= read(fdoutput
[0], buf
, bufsz
- 1)) > 0) {
1946 putcomp(c1
, &holder
, BODYCOMP
);
1950 adios(NULL
, "reading from formatproc");
1954 * See if we got any errors along the way. I'm a little leery of calling
1955 * waitpid() without WNOHANG, but it seems to be the most correct solution.
1958 if (waitpid(filterpid
, &waitstat
, 0) < 0) {
1959 if (errno
!= ECHILD
) {
1960 adios("filterproc", "Unable to determine status");
1963 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1964 pidstatus(waitstat
, stderr
, "filterproc");
1968 if (waitpid(writerpid
, &waitstat
, 0) < 0) {
1969 if (errno
!= ECHILD
) {
1970 adios("writer process", "Unable to determine status");
1974 if (! (WIFEXITED(waitstat
) && WEXITSTATUS(waitstat
) == 0)) {
1975 pidstatus(waitstat
, stderr
, "writer process");