]>
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>
21 * for a component containing addresses, ADDRFMT, if COMPRESS is also
22 * set, then addresses get split wrong (not at the spaces between commas).
23 * To fix this correctly, putstr() should know about "atomic" strings that
24 * must NOT be broken across lines. That's too difficult for right now
25 * (it turns out that there are a number of degernate cases), so in
26 * oneline(), instead of
28 * (*onelp == '\n' && !onelp[1])
30 * being a terminating condition,
32 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
34 * is used instead. This cuts the line prematurely, and gives us a much
35 * better chance of getting things right.
44 static struct swit mhlswitches
[] = {
54 { "faceproc program", 0 },
58 { "folder +folder", 0 },
60 { "form formfile", 0 },
62 { "moreproc program", 0 },
66 { "length lines", 0 },
68 { "width columns", 0 },
70 { "sleep seconds", 0 },
72 { "dashstuffing", -12 }, /* interface from forw */
73 #define NBITSTUFFSW 14
74 { "nodashstuffing", -14 }, /* interface from forw */
80 { "forward", -7 }, /* interface from forw */
82 { "forwall", -7 }, /* interface from forw */
84 { "digest list", -6 },
86 { "volume number", -6 },
88 { "issue number", -5 },
94 #define NOCOMPONENT 0x000001 /* don't show component name */
95 #define UPPERCASE 0x000002 /* display in all upper case */
96 #define CENTER 0x000004 /* center line */
97 #define CLEARTEXT 0x000008 /* cleartext */
98 #define EXTRA 0x000010 /* an "extra" component */
99 #define HDROUTPUT 0x000020 /* already output */
100 #define CLEARSCR 0x000040 /* clear screen */
101 #define LEFTADJUST 0x000080 /* left justify multiple lines */
102 #define COMPRESS 0x000100 /* compress text */
103 #define ADDRFMT 0x000200 /* contains addresses */
104 #define BELL 0x000400 /* sound bell at EOP */
105 #define DATEFMT 0x000800 /* contains dates */
106 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
107 #define INIT 0x002000 /* initialize component */
108 #define FACEFMT 0x004000 /* contains face */
109 #define FACEDFLT 0x008000 /* default for face */
110 #define SPLIT 0x010000 /* split headers (don't concatenate) */
111 #define NONEWLINE 0x020000 /* don't write trailing newline */
112 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
113 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
116 char *c_name
; /* component name */
117 char *c_text
; /* component text */
118 char *c_ovtxt
; /* text overflow indicator */
119 char *c_nfs
; /* iff FORMAT */
120 struct format
*c_fmt
; /* .. */
121 char *c_face
; /* face designator */
122 int c_offset
; /* left margin indentation */
123 int c_ovoff
; /* overflow indentation */
124 int c_width
; /* width of field */
125 int c_cwidth
; /* width of component */
126 int c_length
; /* length in lines */
128 struct mcomp
*c_next
;
131 static struct mcomp
*msghd
= NULL
;
132 static struct mcomp
*msgtl
= NULL
;
133 static struct mcomp
*fmthd
= NULL
;
134 static struct mcomp
*fmttl
= NULL
;
136 static struct mcomp global
= {
137 NULL
, NULL
, "", NULL
, NULL
, 0, -1, 80, -1, 40, BELL
, 0
140 static struct mcomp holder
= {
141 NULL
, NULL
, NULL
, NULL
, NULL
, 0, 0, 0, 0, 0, NOCOMPONENT
, 0
149 static struct pair pairs
[] = {
151 { "From", ADDRFMT
|FACEDFLT
},
152 { "Sender", ADDRFMT
},
153 { "Reply-To", ADDRFMT
},
157 { "Resent-Date", DATEFMT
},
158 { "Resent-From", ADDRFMT
},
159 { "Resent-Sender", ADDRFMT
},
160 { "Resent-Reply-To", ADDRFMT
},
161 { "Resent-To", ADDRFMT
},
162 { "Resent-cc", ADDRFMT
},
163 { "Resent-Bcc", ADDRFMT
},
174 static struct triple triples
[] = {
175 { "nocomponent", NOCOMPONENT
, 0 },
176 { "uppercase", UPPERCASE
, 0 },
177 { "nouppercase", 0, UPPERCASE
},
178 { "center", CENTER
, 0 },
179 { "nocenter", 0, CENTER
},
180 { "clearscreen", CLEARSCR
, 0 },
181 { "noclearscreen", 0, CLEARSCR
},
182 { "noclear", 0, CLEARSCR
},
183 { "leftadjust", LEFTADJUST
, 0 },
184 { "noleftadjust", 0, LEFTADJUST
},
185 { "compress", COMPRESS
, 0 },
186 { "nocompress", 0, COMPRESS
},
187 { "split", SPLIT
, 0 },
188 { "nosplit", 0, SPLIT
},
189 { "addrfield", ADDRFMT
, DATEFMT
},
191 { "nobell", 0, BELL
},
192 { "datefield", DATEFMT
, ADDRFMT
},
193 { "newline", 0, NONEWLINE
},
194 { "nonewline", NONEWLINE
, 0 },
199 static int bellflg
= 0;
200 static int clearflg
= 0;
201 static int dashstuff
= 0;
202 static int dobody
= 1;
203 static int forwflg
= 0;
204 static int forwall
= 0;
206 static int sleepsw
= NOTOK
;
208 static char *digest
= NULL
;
209 static int volume
= 0;
210 static int issue
= 0;
212 static int exitstat
= 0;
213 static int mhldebug
= 0;
218 static int ontty
= NOTTY
;
231 static unsigned char *onelp
;
235 static int num_ignores
= 0;
236 static char *ignores
[MAXARGS
];
239 static jmp_buf mhlenv
;
241 static char delim3
[] = /* from forw.c */
242 "\n----------------------------------------------------------------------\n\n";
243 static char delim4
[] = "\n------------------------------\n\n";
245 static FILE *(*mhl_action
) () = (FILE *(*) ()) 0;
249 * Redefine a couple of functions.
250 * These are undefined later in the code.
252 #define adios mhladios
258 static void mhl_format (char *, int, int);
259 static int evalvar (struct mcomp
*);
260 static int ptoi (char *, int *);
261 static int ptos (char *, char **);
262 static char *parse (void);
263 static void process (char *, char *, int, int);
264 static void mhlfile (FILE *, char *, int, int);
265 static int mcomp_flags (char *);
266 static char *mcomp_add (long, char *, char *);
267 static void mcomp_format (struct mcomp
*, struct mcomp
*);
268 static struct mcomp
*add_queue (struct mcomp
**, struct mcomp
**, char *, char *, int);
269 static void free_queue (struct mcomp
**, struct mcomp
**);
270 static void putcomp (struct mcomp
*, struct mcomp
*, int);
271 static char *oneline (char *, long);
272 static void putstr (char *);
273 static void putch (char);
274 static RETSIGTYPE
intrser (int);
275 static RETSIGTYPE
pipeser (int);
276 static RETSIGTYPE
quitser (int);
277 static void face_format (struct mcomp
*);
278 static int doface (struct mcomp
*);
279 static void mhladios (char *, char *, ...);
280 static void mhldone (int);
281 static void m_popen (char *);
283 int mhl (int, char **);
284 int mhlsbr (int, char **, FILE *(*)());
285 void m_pclose (void);
287 void clear_screen (void); /* from termsbr.c */
288 int SOprintf (char *, ...); /* from termsbr.c */
289 int sc_width (void); /* from termsbr.c */
290 int sc_length (void); /* from termsbr.c */
291 int sc_hardcopy (void); /* from termsbr.c */
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\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 (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 holder
.c_text
= mh_xmalloc (sizeof(buf
));
958 strncpy (holder
.c_text
, buf
, sizeof(buf
));
959 while (state
== BODY
) {
960 putcomp (c1
, &holder
, BODYCOMP
);
961 state
= m_getfld (state
, name
, holder
.c_text
,
964 free (holder
.c_text
);
965 holder
.c_text
= NULL
;
968 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
969 if (!mh_strcasecmp (c2
->c_name
, c1
->c_name
)) {
970 putcomp (c1
, c2
, ONECOMP
);
971 if (!(c1
->c_flags
& SPLIT
))
974 if (faceproc
&& c2
== NULL
&& (c1
->c_flags
& FACEFMT
))
975 for (c2
= msghd
; c2
; c2
= c2
->c_next
)
976 if (c2
->c_flags
& FACEDFLT
) {
977 if (c2
->c_face
== NULL
)
979 if ((holder
.c_text
= c2
->c_face
)) {
980 putcomp (c1
, &holder
, ONECOMP
);
981 holder
.c_text
= NULL
;
990 advise (NULL
, "format error in message %s", mname
);
995 adios (NULL
, "getfld() returned %d", state
);
1002 mcomp_flags (char *name
)
1006 for (ap
= pairs
; ap
->p_name
; ap
++)
1007 if (!mh_strcasecmp (ap
->p_name
, name
))
1008 return (ap
->p_flags
);
1015 mcomp_add (long flags
, char *s1
, char *s2
)
1019 if (!(flags
& ADDRFMT
))
1020 return add (s1
, s2
);
1022 if (s2
&& *(dp
= s2
+ strlen (s2
) - 1) == '\n')
1025 return add (s1
, add (",\n", s2
));
1032 struct pqpair
*pq_next
;
1037 mcomp_format (struct mcomp
*c1
, struct mcomp
*c2
)
1041 char buffer
[BUFSIZ
], error
[BUFSIZ
];
1043 struct pqpair
*p
, *q
;
1045 struct mailname
*mp
;
1052 dat
[3] = sizeof(buffer
) - 1;
1054 fmt_compile (c1
->c_nfs
, &c1
->c_fmt
);
1056 if (!(c1
->c_flags
& ADDRFMT
)) {
1057 FINDCOMP (cptr
, "text");
1060 if ((cp
= strrchr(ap
, '\n'))) /* drop ending newline */
1064 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1065 /* Don't need to append a newline, dctime() already did */
1066 c2
->c_text
= getcpy (buffer
);
1072 (q
= &pq
)->pq_next
= NULL
;
1073 while ((cp
= getname (ap
))) {
1074 if ((p
= (struct pqpair
*) calloc ((size_t) 1, sizeof(*p
))) == NULL
)
1075 adios (NULL
, "unable to allocate pqpair memory");
1077 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, error
)) == NULL
) {
1078 p
->pq_text
= getcpy (cp
);
1079 p
->pq_error
= getcpy (error
);
1081 if ((c1
->c_flags
& FACEDFLT
) && c2
->c_face
== NULL
) {
1083 if ((h
= mp
->m_host
) == NULL
)
1085 if ((o
= OfficialName (h
)))
1087 c2
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
,
1090 p
->pq_text
= getcpy (mp
->m_text
);
1093 q
= (q
->pq_next
= p
);
1096 for (p
= pq
.pq_next
; p
; p
= q
) {
1097 FINDCOMP (cptr
, "text");
1099 cptr
->c_text
= p
->pq_text
;
1100 FINDCOMP (cptr
, "error");
1102 cptr
->c_text
= p
->pq_error
;
1104 fmt_scan (c1
->c_fmt
, buffer
, sizeof(buffer
) - 1, dat
);
1107 c2
->c_text
= add (",\n", c2
->c_text
);
1108 if (*(cp
= buffer
+ strlen (buffer
) - 1) == '\n')
1110 c2
->c_text
= add (buffer
, c2
->c_text
);
1120 c2
->c_text
= add ("\n", c2
->c_text
);
1125 static struct mcomp
*
1126 add_queue (struct mcomp
**head
, struct mcomp
**tail
, char *name
, char *text
, int flags
)
1130 if ((c1
= (struct mcomp
*) calloc ((size_t) 1, sizeof(*c1
))) == NULL
)
1131 adios (NULL
, "unable to allocate comp memory");
1133 c1
->c_flags
= flags
& ~INIT
;
1134 if ((c1
->c_name
= name
? getcpy (name
) : NULL
))
1135 c1
->c_flags
|= mcomp_flags (c1
->c_name
);
1136 c1
->c_text
= text
? getcpy (text
) : NULL
;
1139 c1
->c_ovtxt
= getcpy (global
.c_ovtxt
);
1140 c1
->c_offset
= global
.c_offset
;
1141 c1
->c_ovoff
= global
. c_ovoff
;
1142 c1
->c_width
= c1
->c_length
= 0;
1143 c1
->c_cwidth
= global
.c_cwidth
;
1144 c1
->c_flags
|= global
.c_flags
& GFLAGS
;
1149 (*tail
)->c_next
= c1
;
1157 free_queue (struct mcomp
**head
, struct mcomp
**tail
)
1159 struct mcomp
*c1
, *c2
;
1161 for (c1
= *head
; c1
; c1
= c2
) {
1172 free ((char *) c1
->c_fmt
);
1178 *head
= *tail
= NULL
;
1183 putcomp (struct mcomp
*c1
, struct mcomp
*c2
, int flag
)
1190 llim
= c1
->c_length
? c1
->c_length
: -1;
1191 wid
= c1
->c_width
? c1
->c_width
: global
.c_width
;
1192 ovoff
= (c1
->c_ovoff
>= 0 ? c1
->c_ovoff
: global
.c_ovoff
)
1194 if ((ovtxt
= c1
->c_ovtxt
? c1
->c_ovtxt
: global
.c_ovtxt
) == NULL
)
1196 if (wid
< ovoff
+ strlen (ovtxt
) + 5)
1197 adios (NULL
, "component: %s width(%d) too small for overflow(%d)",
1198 c1
->c_name
, wid
, ovoff
+ strlen (ovtxt
) + 5);
1201 if (c1
->c_flags
& CLEARTEXT
) {
1202 putstr (c1
->c_text
);
1207 if (c1
->c_flags
& FACEFMT
)
1208 switch (doface (c2
)) {
1209 case NOTOK
: /* error */
1210 case OK
: /* async faceproc */
1213 default: /* sync faceproc */
1217 if (c1
->c_nfs
&& (c1
->c_flags
& (ADDRFMT
| DATEFMT
| FORMAT
)))
1218 mcomp_format (c1
, c2
);
1220 if (c1
->c_flags
& CENTER
) {
1221 count
= (c1
->c_width
? c1
->c_width
: global
.c_width
)
1222 - c1
->c_offset
- strlen (c2
->c_text
);
1223 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
))
1224 count
-= strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1225 lm
= c1
->c_offset
+ (count
/ 2);
1231 if (!(c1
->c_flags
& HDROUTPUT
) && !(c1
->c_flags
& NOCOMPONENT
)) {
1232 if (c1
->c_flags
& UPPERCASE
) /* uppercase component also */
1233 for (cp
= (c1
->c_text
? c1
->c_text
: c1
->c_name
); *cp
; cp
++)
1235 *cp
= toupper (*cp
);
1236 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
);
1237 if (flag
!= BODYCOMP
) {
1239 if (!(c1
->c_flags
& SPLIT
))
1240 c1
->c_flags
|= HDROUTPUT
;
1243 if ((count
= c1
->c_cwidth
-
1244 strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) - 2) > 0)
1249 c1
->c_flags
|= HDROUTPUT
; /* for BODYCOMP */
1253 && !(c2
->c_flags
& HDROUTPUT
)
1254 && !(c2
->c_flags
& NOCOMPONENT
)) {
1255 if (c1
->c_flags
& UPPERCASE
)
1256 for (cp
= c2
->c_name
; *cp
; cp
++)
1258 *cp
= toupper (*cp
);
1259 putstr (c2
->c_name
);
1261 if (!(c1
->c_flags
& SPLIT
))
1262 c2
->c_flags
|= HDROUTPUT
;
1265 if ((count
= c1
->c_cwidth
- strlen (c2
->c_name
) - 2) > 0)
1269 if (c1
->c_flags
& UPPERCASE
)
1270 for (cp
= c2
->c_text
; *cp
; cp
++)
1272 *cp
= toupper (*cp
);
1276 if (flag
== TWOCOMP
)
1277 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1278 : strlen (c2
->c_name
) + 2;
1280 count
= (c1
->c_cwidth
>= 0) ? c1
->c_cwidth
1281 : strlen (c1
->c_text
? c1
->c_text
: c1
->c_name
) + 2;
1283 count
+= c1
->c_offset
;
1285 if ((cp
= oneline (c2
->c_text
, c1
->c_flags
)))
1289 while ((cp
= oneline (c2
->c_text
, c1
->c_flags
))) {
1291 if (flag
== BODYCOMP
1292 && !(c1
->c_flags
& NOCOMPONENT
))
1293 putstr (c1
->c_text
? c1
->c_text
: c1
->c_name
);
1299 if (flag
== BODYCOMP
&& term
== '\n')
1300 c1
->c_flags
&= ~HDROUTPUT
; /* Buffer ended on a newline */
1305 oneline (char *stuff
, long flags
)
1313 return (onelp
= NULL
);
1317 if (flags
& COMPRESS
) {
1318 for (spc
= 1, cp
= ret
; *onelp
; onelp
++)
1319 if (isspace (*onelp
)) {
1320 if (*onelp
== '\n' && (!onelp
[1] || (flags
& ADDRFMT
))) {
1339 while (*onelp
&& *onelp
!= '\n')
1341 if (*onelp
== '\n') {
1345 if (flags
& LEFTADJUST
)
1346 while (*ret
== ' ' || *ret
== '\t')
1349 if (*onelp
== 0 && term
== '\n' && (flags
& NONEWLINE
))
1357 putstr (char *string
)
1359 if (!column
&& lm
> 0) {
1390 if (ontty
!= ISTTY
|| row
!= global
.c_length
)
1392 if (global
.c_flags
& BELL
)
1396 read (fileno (stdout
), buf
, sizeof(buf
));
1397 if (strchr(buf
, '\n')) {
1398 if (global
.c_flags
& CLEARSCR
)
1403 row
= global
.c_length
/ 3;
1422 * If we are forwarding this message, and the first
1423 * column contains a dash, then add a dash and a space.
1425 if (column
== 0 && forwflg
&& (dashstuff
>= 0) && ch
== '-') {
1434 if (column
>= wid
) {
1438 putstr (ovtxt
? ovtxt
: "");
1450 #ifndef RELIABLE_SIGNALS
1451 SIGNAL (SIGINT
, intrser
);
1456 longjmp (env
, DONE
);
1463 #ifndef RELIABLE_SIGNALS
1464 SIGNAL (SIGPIPE
, pipeser
);
1474 #ifndef RELIABLE_SIGNALS
1475 SIGNAL (SIGQUIT
, quitser
);
1485 face_format (struct mcomp
*c1
)
1488 struct mailname
*mp
;
1490 if ((cp
= c1
->c_text
) == NULL
)
1493 if ((cp
= getname (cp
))) {
1494 if ((mp
= getm (cp
, NULL
, 0, AD_NAME
, NULL
))) {
1496 if ((h
= mp
->m_host
) == NULL
)
1498 if ((o
= OfficialName (h
)))
1500 c1
->c_face
= concat ("address ", h
, " ", mp
->m_mbox
, NULL
);
1503 while ((cp
= getname (cp
)))
1510 * faceproc is two elements defining the image agent's location:
1515 #include <sys/socket.h>
1516 #include <netinet/in.h>
1518 #include <arpa/inet.h>
1521 doface (struct mcomp
*c1
)
1524 static int inited
= OK
;
1525 static struct sockaddr_storage ss
;
1526 static socklen_t socklen
;
1527 static int socktype
;
1528 static int protocol
;
1532 char **ap
= brkstring (cp
= getcpy (faceproc
), " ", "\n");
1533 struct addrinfo hints
, *res
;
1535 if (ap
[0] == NULL
|| ap
[1] == NULL
) {
1538 return (inited
= NOTOK
);
1541 memset(&hints
, 0, sizeof(hints
));
1542 #ifdef AI_ADDRCONFIG
1543 hints
.ai_flags
= AI_ADDRCONFIG
;
1545 hints
.ai_family
= PF_UNSPEC
;
1546 hints
.ai_socktype
= SOCK_DGRAM
;
1548 if (getaddrinfo(ap
[0], ap
[1], &hints
, &res
) != 0)
1551 memcpy(&ss
, res
->ai_addr
, res
->ai_addrlen
);
1552 socklen
= res
->ai_addrlen
;
1553 socktype
= res
->ai_socktype
;
1554 protocol
= res
->ai_protocol
;
1559 if (inited
== NOTOK
)
1562 if ((sd
= socket (ss
.ss_family
, socktype
, protocol
)) == NOTOK
)
1565 result
= sendto (sd
, c1
->c_text
, strlen (c1
->c_text
), 0,
1566 (struct sockaddr
*) &ss
, socklen
);
1570 return (result
!= NOTOK
? OK
: NOTOK
);
1575 * This version doesn't use sockets
1580 doface (struct mcomp
*c1
)
1584 int result
, pdi
[2], pdo
[2];
1586 char buffer
[BUFSIZ
], *vec
[10];
1588 if (pipe (pdi
) == NOTOK
)
1590 if (pipe (pdo
) == NOTOK
) {
1596 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
1601 /* oops... fork error */
1606 SIGNAL (SIGINT
, SIG_IGN
);
1607 SIGNAL (SIGQUIT
, SIG_IGN
);
1608 if (pdi
[0] != fileno (stdin
)) {
1609 dup2 (pdi
[0], fileno (stdin
));
1614 if (pdo
[1] != fileno (stdout
)) {
1615 dup2 (pdo
[1], fileno (stdout
));
1619 vec
[vecp
++] = r1bindex (faceproc
, '/');
1621 if (sleepsw
!= NOTOK
) {
1623 snprintf (buffer
, sizeof(buffer
), "%d", sleepsw
);
1624 vec
[vecp
++] = buffer
;
1627 execvp (faceproc
, vec
);
1628 fprintf (stderr
, "unable to exec ");
1630 _exit (-1); /* NOTREACHED */
1633 /* parent process */
1635 i
= strlen (c1
->c_text
);
1636 if (write (pdi
[1], c1
->c_text
, i
) != i
)
1637 adios ("pipe", "error writing to");
1638 free (c1
->c_text
), c1
->c_text
= NULL
;
1644 while ((i
= read (pdo
[0], buffer
, strlen (buffer
))) > 0) {
1648 dp
= mh_xrealloc (cp
, (unsigned) (j
= len
+ i
));
1649 memcpy(dp
+ len
, buffer
, i
);
1653 cp
= mh_xmalloc ((unsigned) i
);
1654 memcpy(cp
, buffer
, i
);
1658 for (bp
= buffer
+ i
- 1; bp
>= buffer
; bp
--)
1659 if (!isascii (*bp
) || iscntrl (*bp
)) {
1666 /* no waiting for child... */
1668 if (result
== OK
) { /* binary */
1669 if (write (1, cp
, len
) != len
)
1670 adios ("writing", "error");
1674 if ((c1
->c_text
= cp
) == NULL
)
1681 #endif /* COMMENTED OUT */
1685 mhlsbr (int argc
, char **argv
, FILE *(*action
)())
1687 SIGNAL_HANDLER istat
= NULL
, pstat
= NULL
, qstat
= NULL
;
1691 switch (setjmp (mhlenv
)) {
1694 sleepsw
= 0; /* XXX */
1695 bellflg
= clearflg
= forwflg
= forwall
= exitstat
= 0;
1698 mhl_action
= action
;
1701 * If signal is at default action, then start ignoring
1702 * it, else let it set to its current action.
1704 if ((istat
= SIGNAL (SIGINT
, SIG_IGN
)) != SIG_DFL
)
1705 SIGNAL (SIGINT
, istat
);
1706 if ((qstat
= SIGNAL (SIGQUIT
, SIG_IGN
)) != SIG_DFL
)
1707 SIGNAL (SIGQUIT
, qstat
);
1708 pstat
= SIGNAL (SIGPIPE
, pipeser
);
1709 mhl (argc
, argv
); /* FALL THROUGH! */
1712 SIGNAL (SIGINT
, istat
);
1713 SIGNAL (SIGQUIT
, qstat
);
1714 SIGNAL (SIGPIPE
, SIG_IGN
);/* should probably change to block instead */
1717 SIGNAL (SIGPIPE
, pstat
);
1719 if (holder
.c_text
) {
1720 free (holder
.c_text
);
1721 holder
.c_text
= NULL
;
1723 free_queue (&msghd
, &msgtl
);
1724 for (c1
= fmthd
; c1
; c1
= c1
->c_next
)
1725 c1
->c_flags
&= ~HDROUTPUT
;
1734 mhladios (char *what
, char *fmt
, ...)
1739 advertise (what
, NULL
, fmt
, ap
);
1746 mhldone (int status
)
1750 longjmp (mhlenv
, DONE
);
1756 static int m_pid
= NOTOK
;
1757 static int sd
= NOTOK
;
1760 m_popen (char *name
)
1764 if (mhl_action
&& (sd
= dup (fileno (stdout
))) == NOTOK
)
1765 adios ("standard output", "unable to dup()");
1767 if (pipe (pd
) == NOTOK
)
1768 adios ("pipe", "unable to");
1770 switch (m_pid
= vfork ()) {
1772 adios ("fork", "unable to");
1775 SIGNAL (SIGINT
, SIG_DFL
);
1776 SIGNAL (SIGQUIT
, SIG_DFL
);
1779 if (pd
[0] != fileno (stdin
)) {
1780 dup2 (pd
[0], fileno (stdin
));
1783 execlp (name
, r1bindex (name
, '/'), NULL
);
1784 fprintf (stderr
, "unable to exec ");
1790 if (pd
[1] != fileno (stdout
)) {
1791 dup2 (pd
[1], fileno (stdout
));
1806 if (dup2 (sd
, fileno (stdout
)) == NOTOK
)
1807 adios ("standard output", "unable to dup2()");
1816 pidwait (m_pid
, OK
);