]> diplodocus.org Git - nmh/blob - uip/mhlsbr.c
Ken noted that "make check" can be run before installation because the only program...
[nmh] / uip / mhlsbr.c
1
2 /*
3 * mhlsbr.c -- main routines for nmh message lister
4 *
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.
8 */
9
10 #include <h/mh.h>
11 #include <h/signals.h>
12 #include <h/addrsbr.h>
13 #include <h/fmt_scan.h>
14 #include <h/tws.h>
15 #include <h/utils.h>
16 #include <h/m_setjmp.h>
17 #include <signal.h>
18 #include <errno.h>
19 #include <sys/wait.h>
20 #include <sys/types.h>
21
22 /*
23 * MAJOR BUG:
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
30 *
31 * (*onelp == '\n' && !onelp[1])
32 *
33 * being a terminating condition,
34 *
35 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
36 *
37 * is used instead. This cuts the line prematurely, and gives us a much
38 * better chance of getting things right.
39 */
40
41 #define ONECOMP 0
42 #define TWOCOMP 1
43 #define BODYCOMP 2
44
45 #define QUOTE '\\'
46
47 static struct swit mhlswitches[] = {
48 #define BELLSW 0
49 { "bell", 0 },
50 #define NBELLSW 1
51 { "nobell", 0 },
52 #define CLRSW 2
53 { "clear", 0 },
54 #define NCLRSW 3
55 { "noclear", 0 },
56 #define FACESW 4
57 { "faceproc program", 0 },
58 #define NFACESW 5
59 { "nofaceproc", 0 },
60 #define FOLDSW 6
61 { "folder +folder", 0 },
62 #define FORMSW 7
63 { "form formfile", 0 },
64 #define PROGSW 8
65 { "moreproc program", 0 },
66 #define NPROGSW 9
67 { "nomoreproc", 0 },
68 #define LENSW 10
69 { "length lines", 0 },
70 #define WIDTHSW 11
71 { "width columns", 0 },
72 #define SLEEPSW 12
73 { "sleep seconds", 0 },
74 #define BITSTUFFSW 13
75 { "dashstuffing", -12 }, /* interface from forw */
76 #define NBITSTUFFSW 14
77 { "nodashstuffing", -14 }, /* interface from forw */
78 #define VERSIONSW 15
79 { "version", 0 },
80 #define HELPSW 16
81 { "help", 0 },
82 #define FORW1SW 17
83 { "forward", -7 }, /* interface from forw */
84 #define FORW2SW 18
85 { "forwall", -7 }, /* interface from forw */
86 #define DGSTSW 19
87 { "digest list", -6 },
88 #define VOLUMSW 20
89 { "volume number", -6 },
90 #define ISSUESW 21
91 { "issue number", -5 },
92 #define NBODYSW 22
93 { "nobody", -6 },
94 { NULL, 0 }
95 };
96
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)
119
120 struct mcomp {
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 */
132 long c_flags;
133 struct mcomp *c_next;
134 };
135
136 static struct mcomp *msghd = NULL;
137 static struct mcomp *msgtl = NULL;
138 static struct mcomp *fmthd = NULL;
139 static struct mcomp *fmttl = NULL;
140
141 static struct mcomp global = {
142 NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL
143 };
144
145 static struct mcomp holder = {
146 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL
147 };
148
149 struct pair {
150 char *p_name;
151 long p_flags;
152 };
153
154 static struct pair pairs[] = {
155 { "Date", DATEFMT },
156 { "From", ADDRFMT|FACEDFLT },
157 { "Sender", ADDRFMT },
158 { "Reply-To", ADDRFMT },
159 { "To", ADDRFMT },
160 { "cc", ADDRFMT },
161 { "Bcc", 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 },
169 { "Face", FACEFMT },
170 { NULL, 0 }
171 };
172
173 struct triple {
174 char *t_name;
175 long t_on;
176 long t_off;
177 };
178
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 },
195 { "bell", BELL, 0 },
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 },
204 { NULL, 0, 0 }
205 };
206
207
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;
214
215 static int sleepsw = NOTOK;
216
217 static char *digest = NULL;
218 static int volume = 0;
219 static int issue = 0;
220
221 static int exitstat = 0;
222 static int mhldebug = 0;
223
224 #define PITTY (-1)
225 #define NOTTY 0
226 #define ISTTY 1
227 static int ontty = NOTTY;
228
229 static int row;
230 static unsigned int column;
231
232 static int lm;
233 static int llim;
234 static int ovoff;
235 static int term;
236 static unsigned int wid;
237
238 static char *ovtxt;
239
240 static unsigned char *onelp;
241
242 static char *parptr;
243
244 static int num_ignores = 0;
245 static char *ignores[MAXARGS];
246
247 static jmp_buf env;
248 static jmp_buf mhlenv;
249
250 static char delim3[] = /* from forw.c */
251 "\n----------------------------------------------------------------------\n\n";
252 static char delim4[] = "\n------------------------------\n\n";
253
254 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
255
256
257 /*
258 * Redefine a couple of functions.
259 * These are undefined later in the code.
260 */
261 #define adios mhladios
262 #define done mhldone
263
264 /*
265 * prototypes
266 */
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 *);
292
293 int mhl (int, char **);
294 int mhlsbr (int, char **, FILE *(*)());
295 void m_pclose (void);
296
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 */
302
303
304 int
305 mhl (int argc, char **argv)
306 {
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;
312
313 invo_name = r1bindex (argv[0], '/');
314
315 /* read user profile/context */
316 context_read();
317
318 arguments = getarguments (invo_name, argc, argv, 1);
319 argp = arguments;
320
321 if ((cp = getenv ("MHLDEBUG")) && *cp)
322 mhldebug++;
323
324 if ((cp = getenv ("FACEPROC")))
325 faceproc = cp;
326
327 while ((cp = *argp++)) {
328 if (*cp == '-') {
329 switch (smatch (++cp, mhlswitches)) {
330 case AMBIGSW:
331 ambigsw (cp, mhlswitches);
332 done (1);
333 case UNKWNSW:
334 adios (NULL, "-%s unknown\n", cp);
335
336 case HELPSW:
337 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
338 print_help (buf, mhlswitches, 1);
339 done (1);
340 case VERSIONSW:
341 print_version(invo_name);
342 done (1);
343
344 case BELLSW:
345 bellflg = 1;
346 continue;
347 case NBELLSW:
348 bellflg = -1;
349 continue;
350
351 case CLRSW:
352 clearflg = 1;
353 continue;
354 case NCLRSW:
355 clearflg = -1;
356 continue;
357
358 case FOLDSW:
359 if (!(folder = *argp++) || *folder == '-')
360 adios (NULL, "missing argument to %s", argp[-2]);
361 continue;
362 case FORMSW:
363 if (!(form = *argp++) || *form == '-')
364 adios (NULL, "missing argument to %s", argp[-2]);
365 continue;
366
367 case FACESW:
368 if (!(faceproc = *argp++) || *faceproc == '-')
369 adios (NULL, "missing argument to %s", argp[-2]);
370 continue;
371 case NFACESW:
372 faceproc = NULL;
373 continue;
374 case SLEEPSW:
375 if (!(cp = *argp++) || *cp == '-')
376 adios (NULL, "missing argument to %s", argp[-2]);
377 sleepsw = atoi (cp);/* ZERO ok! */
378 continue;
379
380 case PROGSW:
381 if (!(moreproc = *argp++) || *moreproc == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
383 continue;
384 case NPROGSW:
385 nomore++;
386 continue;
387
388 case LENSW:
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);
393 continue;
394 case WIDTHSW:
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);
399 continue;
400
401 case DGSTSW:
402 if (!(digest = *argp++) || *digest == '-')
403 adios (NULL, "missing argument to %s", argp[-2]);
404 continue;
405 case ISSUESW:
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);
410 continue;
411 case VOLUMSW:
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);
416 continue;
417
418 case FORW2SW:
419 forwall++; /* fall */
420 case FORW1SW:
421 forwflg++;
422 clearflg = -1;/* XXX */
423 continue;
424
425 case BITSTUFFSW:
426 dashstuff = 1; /* trinary logic */
427 continue;
428 case NBITSTUFFSW:
429 dashstuff = -1; /* trinary logic */
430 continue;
431
432 case NBODYSW:
433 dobody = 0;
434 continue;
435 }
436 }
437 files[vecp++] = cp;
438 }
439
440 if (!folder)
441 folder = getenv ("mhfolder");
442
443 if (isatty (fileno (stdout))) {
444 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
445 if (mhl_action) {
446 SIGNAL (SIGINT, SIG_IGN);
447 SIGNAL2 (SIGQUIT, quitser);
448 }
449 SIGNAL2 (SIGPIPE, pipeser);
450 m_popen (moreproc);
451 ontty = PITTY;
452 } else {
453 SIGNAL (SIGINT, SIG_IGN);
454 SIGNAL2 (SIGQUIT, quitser);
455 ontty = ISTTY;
456 }
457 } else {
458 ontty = NOTTY;
459 }
460
461 mhl_format (form ? form : mhlformat, length, width);
462
463 if (vecp == 0) {
464 process (folder, NULL, 1, vecp = 1);
465 } else {
466 for (i = 0; i < vecp; i++)
467 process (folder, files[i], i + 1, vecp);
468 }
469
470 if (forwall) {
471 if (digest) {
472 printf ("%s", delim4);
473 if (volume == 0) {
474 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
475 } else {
476 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
477 digest, volume, issue);
478 }
479 i = strlen (buf);
480 for (cp = buf + i; i > 1; i--)
481 *cp++ = '*';
482 *cp++ = '\n';
483 *cp = 0;
484 printf ("%s", buf);
485 }
486 else
487 printf ("\n------- End of Forwarded Message%s\n\n",
488 vecp > 1 ? "s" : "");
489 }
490
491 fflush(stdout);
492 if(ferror(stdout)){
493 adios("output", "error writing");
494 }
495
496 if (clearflg > 0 && ontty == NOTTY)
497 clear_screen ();
498
499 if (ontty == PITTY)
500 m_pclose ();
501
502 return exitstat;
503 }
504
505
506 static void
507 mhl_format (char *file, int length, int width)
508 {
509 int i;
510 char *bp, *cp, **ip;
511 char *ap, buffer[BUFSIZ], name[NAMESZ];
512 struct mcomp *c1;
513 struct stat st;
514 FILE *fp;
515 static dev_t dev = 0;
516 static ino_t ino = 0;
517 static time_t mtime = 0;
518
519 if (fmthd != NULL) {
520 if (stat (etcpath (file), &st) != NOTOK
521 && mtime == st.st_mtime
522 && dev == st.st_dev
523 && ino == st.st_ino)
524 goto out;
525 else
526 free_queue (&fmthd, &fmttl);
527 }
528
529 if ((fp = fopen (etcpath (file), "r")) == NULL)
530 adios (file, "unable to open format file");
531
532 if (fstat (fileno (fp), &st) != NOTOK) {
533 mtime = st.st_mtime;
534 dev = st.st_dev;
535 ino = st.st_ino;
536 }
537
538 global.c_ovtxt = global.c_nfs = NULL;
539 global.c_fmt = NULL;
540 global.c_offset = 0;
541 global.c_ovoff = -1;
542 if ((i = sc_width ()) > 5)
543 global.c_width = i;
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;
549
550 while (vfgets (fp, &ap) == OK) {
551 bp = ap;
552 if (*bp == ';')
553 continue;
554
555 if ((cp = strchr(bp, '\n')))
556 *cp = 0;
557
558 if (*bp == ':') {
559 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
560 continue;
561 }
562
563 parptr = bp;
564 strncpy (name, parse(), sizeof(name));
565 switch (*parptr) {
566 case '\0':
567 case ',':
568 case '=':
569 /*
570 * Split this list of fields to ignore, and copy
571 * it to the end of the current "ignores" list.
572 */
573 if (!mh_strcasecmp (name, "ignores")) {
574 char **tmparray, **p;
575 int n = 0;
576
577 /* split the fields */
578 tmparray = brkstring (getcpy (++parptr), ",", NULL);
579
580 /* count number of fields split */
581 p = tmparray;
582 while (*p++)
583 n++;
584
585 /* copy pointers to split fields to ignores array */
586 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
587 num_ignores += n;
588 continue;
589 }
590 parptr = bp;
591 while (*parptr) {
592 if (evalvar (&global))
593 adios (NULL, "format file syntax error: %s", bp);
594 if (*parptr)
595 parptr++;
596 }
597 continue;
598
599 case ':':
600 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
601 while (*parptr == ':' || *parptr == ',') {
602 parptr++;
603 if (evalvar (c1))
604 adios (NULL, "format file syntax error: %s", bp);
605 }
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);
610 }
611 else
612 if (c1->c_flags & ADDRFMT) {
613 if (global.c_flags & ADDRFMT)
614 c1->c_nfs = getcpy (global.c_nfs);
615 }
616 }
617 continue;
618
619 default:
620 adios (NULL, "format file syntax error: %s", bp);
621 }
622 }
623 fclose (fp);
624
625 if (mhldebug) {
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));
637 }
638 }
639
640 out:
641 if (clearflg == 1) {
642 global.c_flags |= CLEARSCR;
643 } else {
644 if (clearflg == -1)
645 global.c_flags &= ~CLEARSCR;
646 }
647
648 switch (bellflg) { /* command line may override format file */
649 case 1:
650 global.c_flags |= BELL;
651 break;
652 case -1:
653 global.c_flags &= ~BELL;
654 break;
655 }
656
657 if (length)
658 global.c_length = length;
659 if (width)
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;
665 }
666
667
668 static int
669 evalvar (struct mcomp *c1)
670 {
671 char *cp, name[NAMESZ];
672 struct triple *ap;
673
674 if (!*parptr)
675 return 0;
676 strncpy (name, parse(), sizeof(name));
677
678 if (!mh_strcasecmp (name, "component")) {
679 if (ptos (name, &c1->c_text))
680 return 1;
681 c1->c_flags &= ~NOCOMPONENT;
682 return 0;
683 }
684
685 if (!mh_strcasecmp (name, "overflowtext"))
686 return ptos (name, &c1->c_ovtxt);
687
688 if (!mh_strcasecmp (name, "formatfield")) {
689 char *nfs;
690
691 if (ptos (name, &cp))
692 return 1;
693 nfs = new_fs (NULL, NULL, cp);
694 c1->c_nfs = getcpy (nfs);
695 c1->c_flags |= FORMAT;
696 return 0;
697 }
698
699 if (!mh_strcasecmp (name, "decode")) {
700 char *nfs;
701
702 nfs = new_fs (NULL, NULL, "%(decode{text})");
703 c1->c_nfs = getcpy (nfs);
704 c1->c_flags |= FORMAT;
705 return 0;
706 }
707
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);
720
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;
725 return 0;
726 }
727
728 return 1;
729 }
730
731
732 static int
733 ptoi (char *name, int *i)
734 {
735 char *cp;
736
737 if (*parptr++ != '=' || !*(cp = parse ())) {
738 advise (NULL, "missing argument to variable %s", name);
739 return 1;
740 }
741
742 *i = atoi (cp);
743 return 0;
744 }
745
746
747 static int
748 ptos (char *name, char **s)
749 {
750 char c, *cp;
751
752 if (*parptr++ != '=') {
753 advise (NULL, "missing argument to variable %s", name);
754 return 1;
755 }
756
757 if (*parptr != '"') {
758 for (cp = parptr;
759 *parptr && *parptr != ':' && *parptr != ',';
760 parptr++)
761 continue;
762 } else {
763 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
764 if (*parptr == QUOTE)
765 if (!*++parptr)
766 parptr--;
767 }
768 c = *parptr;
769 *parptr = 0;
770 *s = getcpy (cp);
771 if ((*parptr = c) == '"')
772 parptr++;
773 return 0;
774 }
775
776
777 static char *
778 parse (void)
779 {
780 int c;
781 char *cp;
782 static char result[NAMESZ];
783
784 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
785 c = *parptr;
786 if (isalnum (c)
787 || c == '.'
788 || c == '-'
789 || c == '_'
790 || c =='['
791 || c == ']')
792 *cp++ = c;
793 else
794 break;
795 }
796 *cp = '\0';
797
798 return result;
799 }
800
801
802 static void
803 process (char *folder, char *fname, int ofilen, int ofilec)
804 {
805 char *cp = NULL;
806 FILE *fp = NULL;
807 struct mcomp *c1;
808
809 switch (m_setjmp (env)) {
810 case OK:
811 if (fname) {
812 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
813 if (fp == NULL) {
814 advise (fname, "unable to open");
815 exitstat++;
816 return;
817 }
818 } else {
819 fname = "(stdin)";
820 fp = stdin;
821 }
822 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
823 if (ontty != PITTY)
824 SIGNAL (SIGINT, intrser);
825 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
826
827 default:
828 if (ontty != PITTY)
829 SIGNAL (SIGINT, SIG_IGN);
830 if (mhl_action == NULL && fp != stdin)
831 fclose (fp);
832 free (cp);
833 if (holder.c_text) {
834 free (holder.c_text);
835 holder.c_text = NULL;
836 }
837 free_queue (&msghd, &msgtl);
838 for (c1 = fmthd; c1; c1 = c1->c_next)
839 c1->c_flags &= ~HDROUTPUT;
840 break;
841 }
842 }
843
844
845 static void
846 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
847 {
848 int state;
849 struct mcomp *c1, *c2, *c3;
850 char **ip, name[NAMESZ], buf[BUFSIZ];
851
852 if (forwall) {
853 if (digest)
854 printf ("%s", ofilen == 1 ? delim3 : delim4);
855 else {
856 printf ("\n-------");
857 if (ofilen == 1)
858 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
859 else
860 printf (" Message %d", ofilen);
861 printf ("\n\n");
862 }
863 } else {
864 switch (ontty) {
865 case PITTY:
866 if (ofilec > 1) {
867 if (ofilen > 1) {
868 if ((global.c_flags & CLEARSCR))
869 clear_screen ();
870 else
871 printf ("\n\n\n");
872 }
873 printf (">>> %s\n\n", mname);
874 }
875 break;
876
877 case ISTTY:
878 strncpy (buf, "\n", sizeof(buf));
879 if (ofilec > 1) {
880 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
881 if (ofilen > 1)
882 printf ("\n\n\n");
883 printf ("Press <return> to list \"%s\"...", mname);
884 }
885 fflush (stdout);
886 buf[0] = 0;
887 read (fileno (stdout), buf, sizeof(buf));
888 }
889 if (strchr(buf, '\n')) {
890 if ((global.c_flags & CLEARSCR))
891 clear_screen ();
892 }
893 else
894 printf ("\n");
895 break;
896
897 default:
898 if (ofilec > 1) {
899 if (ofilen > 1) {
900 printf ("\n\n\n");
901 if (clearflg > 0)
902 clear_screen ();
903 }
904 printf (">>> %s\n\n", mname);
905 }
906 break;
907 }
908 }
909
910 for (state = FLD;;) {
911 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
912 case FLD:
913 case FLDPLUS:
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);
918 break;
919 }
920 if (*ip)
921 continue;
922
923 for (c2 = fmthd; c2; c2 = c2->c_next)
924 if (!mh_strcasecmp (c2->c_name, name))
925 break;
926 c1 = NULL;
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)) {
930 c1->c_text =
931 mcomp_add (c1->c_flags, buf, c1->c_text);
932 break;
933 }
934 if (c1 == NULL)
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);
939 }
940 if (c2 == NULL)
941 c1->c_flags |= EXTRA;
942 continue;
943
944 case BODY:
945 case FILEEOF:
946 row = column = 0;
947 for (c1 = fmthd; c1; c1 = c1->c_next) {
948 if (c1->c_flags & CLEARTEXT) {
949 putcomp (c1, c1, ONECOMP);
950 continue;
951 }
952 if (!mh_strcasecmp (c1->c_name, "messagename")) {
953 holder.c_text = concat ("(Message ", mname, ")\n",
954 NULL);
955 putcomp (c1, &holder, ONECOMP);
956 free (holder.c_text);
957 holder.c_text = NULL;
958 continue;
959 }
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);
964 continue;
965 }
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);
970 } else {
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,
976 sizeof(buf), fp);
977 }
978 free (holder.c_text);
979 holder.c_text = NULL;
980 }
981 continue;
982 }
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))
987 break;
988 }
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)
993 face_format (c2);
994 if ((holder.c_text = c2->c_face)) {
995 putcomp (c1, &holder, ONECOMP);
996 holder.c_text = NULL;
997 }
998 break;
999 }
1000 }
1001 return;
1002
1003 case LENERR:
1004 case FMTERR:
1005 advise (NULL, "format error in message %s", mname);
1006 exitstat++;
1007 return;
1008
1009 default:
1010 adios (NULL, "getfld() returned %d", state);
1011 }
1012 }
1013 }
1014
1015
1016 static int
1017 mcomp_flags (char *name)
1018 {
1019 struct pair *ap;
1020
1021 for (ap = pairs; ap->p_name; ap++)
1022 if (!mh_strcasecmp (ap->p_name, name))
1023 return (ap->p_flags);
1024
1025 return 0;
1026 }
1027
1028
1029 static char *
1030 mcomp_add (long flags, char *s1, char *s2)
1031 {
1032 char *dp;
1033
1034 if (!(flags & ADDRFMT))
1035 return add (s1, s2);
1036
1037 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1038 *dp = 0;
1039
1040 return add (s1, add (",\n", s2));
1041 }
1042
1043
1044 struct pqpair {
1045 char *pq_text;
1046 char *pq_error;
1047 struct pqpair *pq_next;
1048 };
1049
1050
1051 static void
1052 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1053 {
1054 int dat[5];
1055 char *ap, *cp;
1056 char buffer[BUFSIZ], error[BUFSIZ];
1057 struct comp *cptr;
1058 struct pqpair *p, *q;
1059 struct pqpair pq;
1060 struct mailname *mp;
1061
1062 ap = c2->c_text;
1063 c2->c_text = NULL;
1064 dat[0] = 0;
1065 dat[1] = 0;
1066 dat[2] = 0;
1067 dat[3] = sizeof(buffer) - 1;
1068 dat[4] = 0;
1069 fmt_compile (c1->c_nfs, &c1->c_fmt);
1070
1071 if (!(c1->c_flags & ADDRFMT)) {
1072 FINDCOMP (cptr, "text");
1073 if (cptr)
1074 cptr->c_text = ap;
1075 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1076 if (!cp[1])
1077 *cp = 0;
1078
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);
1082
1083 free (ap);
1084 return;
1085 }
1086
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");
1091
1092 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1093 p->pq_text = getcpy (cp);
1094 p->pq_error = getcpy (error);
1095 } else {
1096 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1097 char *h, *o;
1098 if ((h = mp->m_host) == NULL)
1099 h = LocalName (0);
1100 if ((o = OfficialName (h)))
1101 h = o;
1102 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1103 NULL);
1104 }
1105 p->pq_text = getcpy (mp->m_text);
1106 mnfree (mp);
1107 }
1108 q = (q->pq_next = p);
1109 }
1110
1111 for (p = pq.pq_next; p; p = q) {
1112 FINDCOMP (cptr, "text");
1113 if (cptr)
1114 cptr->c_text = p->pq_text;
1115 FINDCOMP (cptr, "error");
1116 if (cptr)
1117 cptr->c_text = p->pq_error;
1118
1119 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1120 if (*buffer) {
1121 if (c2->c_text)
1122 c2->c_text = add (",\n", c2->c_text);
1123 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1124 *cp = 0;
1125 c2->c_text = add (buffer, c2->c_text);
1126 }
1127
1128 free (p->pq_text);
1129 if (p->pq_error)
1130 free (p->pq_error);
1131 q = p->pq_next;
1132 free ((char *) p);
1133 }
1134
1135 c2->c_text = add ("\n", c2->c_text);
1136 free (ap);
1137 }
1138
1139
1140 static struct mcomp *
1141 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1142 {
1143 struct mcomp *c1;
1144
1145 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1146 adios (NULL, "unable to allocate comp memory");
1147
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;
1152 if (flags & INIT) {
1153 if (global.c_ovtxt)
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;
1160 }
1161 if (*head == NULL)
1162 *head = c1;
1163 if (*tail != NULL)
1164 (*tail)->c_next = c1;
1165 *tail = c1;
1166
1167 return c1;
1168 }
1169
1170
1171 static void
1172 free_queue (struct mcomp **head, struct mcomp **tail)
1173 {
1174 struct mcomp *c1, *c2;
1175
1176 for (c1 = *head; c1; c1 = c2) {
1177 c2 = c1->c_next;
1178 if (c1->c_name)
1179 free (c1->c_name);
1180 if (c1->c_text)
1181 free (c1->c_text);
1182 if (c1->c_ovtxt)
1183 free (c1->c_ovtxt);
1184 if (c1->c_nfs)
1185 free (c1->c_nfs);
1186 if (c1->c_fmt)
1187 free ((char *) c1->c_fmt);
1188 if (c1->c_face)
1189 free (c1->c_face);
1190 free ((char *) c1);
1191 }
1192
1193 *head = *tail = NULL;
1194 }
1195
1196
1197 static void
1198 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1199 {
1200 int count, cchdr;
1201 unsigned char *cp;
1202
1203 cchdr = 0;
1204 lm = 0;
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)
1208 + c1->c_offset;
1209 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1210 ovtxt = "";
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);
1214 onelp = NULL;
1215
1216 if (c1->c_flags & CLEARTEXT) {
1217 putstr (c1->c_text, c1->c_flags);
1218 putstr ("\n", c1->c_flags);
1219 return;
1220 }
1221
1222 if (c1->c_flags & FACEFMT)
1223 switch (doface (c2)) {
1224 case NOTOK: /* error */
1225 case OK: /* async faceproc */
1226 return;
1227
1228 default: /* sync faceproc */
1229 break;
1230 }
1231
1232 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1233 mcomp_format (c1, c2);
1234
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);
1241 } else {
1242 if (c1->c_offset)
1243 lm = c1->c_offset;
1244 }
1245
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++)
1249 if (islower (*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;
1256
1257 cchdr++;
1258 if ((count = c1->c_cwidth -
1259 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1260 while (count--)
1261 putstr (" ", c1->c_flags);
1262 }
1263 else
1264 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1265 }
1266
1267 if (flag == TWOCOMP
1268 && !(c2->c_flags & HDROUTPUT)
1269 && !(c2->c_flags & NOCOMPONENT)) {
1270 if (c1->c_flags & UPPERCASE)
1271 for (cp = c2->c_name; *cp; cp++)
1272 if (islower (*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;
1278
1279 cchdr++;
1280 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1281 while (count--)
1282 putstr (" ", c1->c_flags);
1283 }
1284 if (c1->c_flags & UPPERCASE)
1285 for (cp = c2->c_text; *cp; cp++)
1286 if (islower (*cp))
1287 *cp = toupper (*cp);
1288
1289 count = 0;
1290 if (cchdr) {
1291 if (flag == TWOCOMP)
1292 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1293 : (int) strlen (c2->c_name) + 2;
1294 else
1295 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1296 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1297 }
1298 count += c1->c_offset;
1299
1300 if ((cp = oneline (c2->c_text, c1->c_flags)))
1301 putstr(cp, c1->c_flags);
1302 if (term == '\n')
1303 putstr ("\n", c1->c_flags);
1304 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1305 lm = count;
1306 if (flag == BODYCOMP
1307 && !(c1->c_flags & NOCOMPONENT))
1308 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1309 if (*cp)
1310 putstr (cp, c1->c_flags);
1311 if (term == '\n')
1312 putstr ("\n", c1->c_flags);
1313 }
1314 if (flag == BODYCOMP && term == '\n')
1315 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1316 }
1317
1318
1319 static char *
1320 oneline (char *stuff, long flags)
1321 {
1322 int spc;
1323 char *cp, *ret;
1324
1325 if (onelp == NULL)
1326 onelp = stuff;
1327 if (*onelp == 0)
1328 return (onelp = NULL);
1329
1330 ret = onelp;
1331 term = 0;
1332 if (flags & COMPRESS) {
1333 for (spc = 1, cp = ret; *onelp; onelp++)
1334 if (isspace (*onelp)) {
1335 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1336 term = '\n';
1337 *onelp++ = 0;
1338 break;
1339 }
1340 else
1341 if (!spc) {
1342 *cp++ = ' ';
1343 spc++;
1344 }
1345 }
1346 else {
1347 *cp++ = *onelp;
1348 spc = 0;
1349 }
1350
1351 *cp = 0;
1352 }
1353 else {
1354 while (*onelp && *onelp != '\n')
1355 onelp++;
1356 if (*onelp == '\n') {
1357 term = '\n';
1358 *onelp++ = 0;
1359 }
1360 if (flags & LEFTADJUST)
1361 while (*ret == ' ' || *ret == '\t')
1362 ret++;
1363 }
1364 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1365 term = 0;
1366
1367 return ret;
1368 }
1369
1370
1371 static void
1372 putstr (char *string, long flags)
1373 {
1374 if (!column && lm > 0) {
1375 while (lm > 0)
1376 if (lm >= 8) {
1377 putch ('\t', flags);
1378 lm -= 8;
1379 }
1380 else {
1381 putch (' ', flags);
1382 lm--;
1383 }
1384 }
1385 lm = 0;
1386 while (*string)
1387 putch (*string++, flags);
1388 }
1389
1390
1391 static void
1392 putch (char ch, long flags)
1393 {
1394 char buf[BUFSIZ];
1395
1396 if (llim == 0)
1397 return;
1398
1399 switch (ch) {
1400 case '\n':
1401 if (llim > 0)
1402 llim--;
1403 column = 0;
1404 row++;
1405 if (ontty != ISTTY || row != global.c_length)
1406 break;
1407 if (global.c_flags & BELL)
1408 putchar ('\007');
1409 fflush (stdout);
1410 buf[0] = 0;
1411 read (fileno (stdout), buf, sizeof(buf));
1412 if (strchr(buf, '\n')) {
1413 if (global.c_flags & CLEARSCR)
1414 clear_screen ();
1415 row = 0;
1416 } else {
1417 putchar ('\n');
1418 row = global.c_length / 3;
1419 }
1420 return;
1421
1422 case '\t':
1423 column |= 07;
1424 column++;
1425 break;
1426
1427 case '\b':
1428 column--;
1429 break;
1430
1431 case '\r':
1432 column = 0;
1433 break;
1434
1435 default:
1436 /*
1437 * If we are forwarding this message, and the first
1438 * column contains a dash, then add a dash and a space.
1439 */
1440 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1441 putchar ('-');
1442 putchar (' ');
1443 }
1444 if (ch >= ' ')
1445 column++;
1446 break;
1447 }
1448
1449 if (column >= wid && (flags & NOWRAP) == 0) {
1450 putch ('\n', flags);
1451 if (ovoff > 0)
1452 lm = ovoff;
1453 putstr (ovtxt ? ovtxt : "", flags);
1454 putch (ch, flags);
1455 return;
1456 }
1457
1458 putchar (ch);
1459 }
1460
1461
1462 static void
1463 intrser (int i)
1464 {
1465 NMH_UNUSED (i);
1466
1467 discard (stdout);
1468 putchar ('\n');
1469 longjmp (env, DONE);
1470 }
1471
1472
1473 static void
1474 pipeser (int i)
1475 {
1476 NMH_UNUSED (i);
1477
1478 done (NOTOK);
1479 }
1480
1481
1482 static void
1483 quitser (int i)
1484 {
1485 NMH_UNUSED (i);
1486
1487 putchar ('\n');
1488 fflush (stdout);
1489 done (NOTOK);
1490 }
1491
1492
1493 static void
1494 face_format (struct mcomp *c1)
1495 {
1496 char *cp;
1497 struct mailname *mp;
1498
1499 if ((cp = c1->c_text) == NULL)
1500 return;
1501
1502 if ((cp = getname (cp))) {
1503 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1504 char *h, *o;
1505 if ((h = mp->m_host) == NULL)
1506 h = LocalName (0);
1507 if ((o = OfficialName (h)))
1508 h = o;
1509 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1510 }
1511
1512 while ((cp = getname (cp)))
1513 continue;
1514 }
1515 }
1516
1517
1518 /*
1519 * faceproc is two elements defining the image agent's location:
1520 * Internet host
1521 * UDP port
1522 */
1523
1524 #include <sys/socket.h>
1525 #include <netinet/in.h>
1526 #include <netdb.h>
1527 #include <arpa/inet.h>
1528
1529 static int
1530 doface (struct mcomp *c1)
1531 {
1532 int result, sd;
1533 static int inited = OK;
1534 static struct sockaddr_storage ss;
1535 static socklen_t socklen;
1536 static int socktype;
1537 static int protocol;
1538
1539 if (inited == OK) {
1540 char *cp;
1541 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1542 struct addrinfo hints, *res;
1543
1544 if (ap[0] == NULL || ap[1] == NULL) {
1545 bad_faceproc: ;
1546 free (cp);
1547 return (inited = NOTOK);
1548 }
1549
1550 memset(&hints, 0, sizeof(hints));
1551 #ifdef AI_ADDRCONFIG
1552 hints.ai_flags = AI_ADDRCONFIG;
1553 #endif
1554 hints.ai_family = PF_UNSPEC;
1555 hints.ai_socktype = SOCK_DGRAM;
1556
1557 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1558 goto bad_faceproc;
1559
1560 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1561 socklen = res->ai_addrlen;
1562 socktype = res->ai_socktype;
1563 protocol = res->ai_protocol;
1564 freeaddrinfo(res);
1565
1566 inited = DONE;
1567 }
1568 if (inited == NOTOK)
1569 return NOTOK;
1570
1571 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1572 return NOTOK;
1573
1574 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1575 (struct sockaddr *) &ss, socklen);
1576
1577 close (sd);
1578
1579 return (result != NOTOK ? OK : NOTOK);
1580 }
1581
1582 /*
1583 * COMMENTED OUT
1584 * This version doesn't use sockets
1585 */
1586 #if 0
1587
1588 static int
1589 doface (struct mcomp *c1)
1590 {
1591 int i, len, vecp;
1592 pid_t child_id;
1593 int result, pdi[2], pdo[2];
1594 char *bp, *cp;
1595 char buffer[BUFSIZ], *vec[10];
1596
1597 if (pipe (pdi) == NOTOK)
1598 return NOTOK;
1599 if (pipe (pdo) == NOTOK) {
1600 close (pdi[0]);
1601 close (pdi[1]);
1602 return NOTOK;
1603 }
1604
1605 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1606 sleep (5);
1607
1608 switch (child_id) {
1609 case NOTOK:
1610 /* oops... fork error */
1611 return NOTOK;
1612
1613 case OK:
1614 /* child process */
1615 SIGNAL (SIGINT, SIG_IGN);
1616 SIGNAL (SIGQUIT, SIG_IGN);
1617 if (pdi[0] != fileno (stdin)) {
1618 dup2 (pdi[0], fileno (stdin));
1619 close (pdi[0]);
1620 }
1621 close (pdi[1]);
1622 close (pdo[0]);
1623 if (pdo[1] != fileno (stdout)) {
1624 dup2 (pdo[1], fileno (stdout));
1625 close (pdo[1]);
1626 }
1627 vecp = 0;
1628 vec[vecp++] = r1bindex (faceproc, '/');
1629 vec[vecp++] = "-e";
1630 if (sleepsw != NOTOK) {
1631 vec[vecp++] = "-s";
1632 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1633 vec[vecp++] = buffer;
1634 }
1635 vec[vecp] = NULL;
1636 execvp (faceproc, vec);
1637 fprintf (stderr, "unable to exec ");
1638 perror (faceproc);
1639 _exit (-1); /* NOTREACHED */
1640
1641 default:
1642 /* parent process */
1643 close (pdi[0]);
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;
1648 close (pdi[1]);
1649
1650 close (pdo[1]);
1651 cp = NULL, len = 0;
1652 result = DONE;
1653 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1654 if (cp) {
1655 int j;
1656 char *dp;
1657 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1658 memcpy(dp + len, buffer, i);
1659 cp = dp, len = j;
1660 }
1661 else {
1662 cp = mh_xmalloc ((unsigned) i);
1663 memcpy(cp, buffer, i);
1664 len = i;
1665 }
1666 if (result == DONE)
1667 for (bp = buffer + i - 1; bp >= buffer; bp--)
1668 if (!isascii (*bp) || iscntrl (*bp)) {
1669 result = OK;
1670 break;
1671 }
1672 }
1673 close (pdo[0]);
1674
1675 /* no waiting for child... */
1676
1677 if (result == OK) { /* binary */
1678 if (write (1, cp, len) != len)
1679 adios ("writing", "error");
1680 free (cp);
1681 }
1682 else /* empty */
1683 if ((c1->c_text = cp) == NULL)
1684 result = OK;
1685 break;
1686 }
1687
1688 return result;
1689 }
1690 #endif /* COMMENTED OUT */
1691
1692
1693 int
1694 mhlsbr (int argc, char **argv, FILE *(*action)())
1695 {
1696 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1697 char *cp = NULL;
1698 struct mcomp *c1;
1699
1700 switch (m_setjmp (mhlenv)) {
1701 case OK:
1702 cp = invo_name;
1703 sleepsw = 0; /* XXX */
1704 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1705 digest = NULL;
1706 ontty = NOTTY;
1707 mhl_action = action;
1708
1709 /*
1710 * If signal is at default action, then start ignoring
1711 * it, else let it set to its current action.
1712 */
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! */
1719
1720 default:
1721 SIGNAL (SIGINT, istat);
1722 SIGNAL (SIGQUIT, qstat);
1723 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1724 if (ontty == PITTY)
1725 m_pclose ();
1726 SIGNAL (SIGPIPE, pstat);
1727 invo_name = cp;
1728 if (holder.c_text) {
1729 free (holder.c_text);
1730 holder.c_text = NULL;
1731 }
1732 free_queue (&msghd, &msgtl);
1733 for (c1 = fmthd; c1; c1 = c1->c_next)
1734 c1->c_flags &= ~HDROUTPUT;
1735 return exitstat;
1736 }
1737 }
1738
1739 #undef adios
1740 #undef done
1741
1742 static void
1743 mhladios (char *what, char *fmt, ...)
1744 {
1745 va_list ap;
1746
1747 va_start(ap, fmt);
1748 advertise (what, NULL, fmt, ap);
1749 va_end(ap);
1750 mhldone (1);
1751 }
1752
1753
1754 static void
1755 mhldone (int status)
1756 {
1757 exitstat = status;
1758 if (mhl_action)
1759 longjmp (mhlenv, DONE);
1760 else
1761 done (exitstat);
1762 }
1763
1764
1765 static int m_pid = NOTOK;
1766 static int sd = NOTOK;
1767
1768 static void
1769 m_popen (char *name)
1770 {
1771 int pd[2];
1772
1773 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1774 adios ("standard output", "unable to dup()");
1775
1776 if (pipe (pd) == NOTOK)
1777 adios ("pipe", "unable to");
1778
1779 switch (m_pid = vfork()) {
1780 case NOTOK:
1781 adios ("fork", "unable to");
1782
1783 case OK:
1784 SIGNAL (SIGINT, SIG_DFL);
1785 SIGNAL (SIGQUIT, SIG_DFL);
1786
1787 close (pd[1]);
1788 if (pd[0] != fileno (stdin)) {
1789 dup2 (pd[0], fileno (stdin));
1790 close (pd[0]);
1791 }
1792 execlp (name, r1bindex (name, '/'), NULL);
1793 fprintf (stderr, "unable to exec ");
1794 perror (name);
1795 _exit (-1);
1796
1797 default:
1798 close (pd[0]);
1799 if (pd[1] != fileno (stdout)) {
1800 dup2 (pd[1], fileno (stdout));
1801 close (pd[1]);
1802 }
1803 }
1804 }
1805
1806
1807 void
1808 m_pclose (void)
1809 {
1810 if (m_pid == NOTOK)
1811 return;
1812
1813 if (sd != NOTOK) {
1814 fflush (stdout);
1815 if (dup2 (sd, fileno (stdout)) == NOTOK)
1816 adios ("standard output", "unable to dup2()");
1817
1818 clearerr (stdout);
1819 close (sd);
1820 sd = NOTOK;
1821 }
1822 else
1823 fclose (stdout);
1824
1825 pidwait (m_pid, OK);
1826 m_pid = NOTOK;
1827 }
1828
1829
1830 /*
1831 * Filter the body of a message through a specified format program
1832 */
1833
1834 void
1835 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
1836 {
1837 struct mcomp holder;
1838 char name[NAMESZ];
1839 int fdinput[2], fdoutput[2], waitstat;
1840 ssize_t cc;
1841 pid_t writerpid, filterpid;
1842
1843 /*
1844 * Create pipes so we can communicate with our filter process.
1845 */
1846
1847 if (pipe(fdinput) < 0) {
1848 adios(NULL, "Unable to create input pipe");
1849 }
1850
1851 if (pipe(fdoutput) < 0) {
1852 adios(NULL, "Unable to create output pipe");
1853 }
1854
1855 /*
1856 * Here's what we're doing to do.
1857 *
1858 * - Fork ourselves and start writing data to the write side of the
1859 * input pipe (fdinput[1]).
1860 *
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
1864 * (fdoutput[1]).
1865 *
1866 * - We read from the read side of the output pipe (fdoutput[0]).
1867 *
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).
1871 */
1872
1873 switch (writerpid = fork()) {
1874 case 0:
1875 /*
1876 * Our child process - just write to the filter input (fdinput[1]).
1877 * Close all other descriptors that we don't need.
1878 */
1879
1880 close(fdinput[0]);
1881 close(fdoutput[0]);
1882 close(fdoutput[1]);
1883
1884 /*
1885 * Call m_getfld() until we're no longer in the BODY state
1886 */
1887
1888 while (state == BODY) {
1889 write(fdinput[1], buf, strlen(buf));
1890 state = m_getfld(state, name, buf, bufsz, fp);
1891 }
1892
1893 /*
1894 * We should be done; time to exit.
1895 */
1896
1897 close(fdinput[1]);
1898 exit(0);
1899 break;
1900 case -1:
1901 adios(NULL, "Unable to fork for filter writer process");
1902 break;
1903 }
1904
1905 /*
1906 * Fork and exec() our filter program, after redirecting standard in
1907 * and standard out appropriately.
1908 */
1909
1910 switch (filterpid = fork()) {
1911 case 0:
1912 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
1913 adios("formatproc", "Unable to dup2() standard input");
1914 }
1915 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
1916 adios("formatproc", "Unable to dup2() standard output");
1917 }
1918
1919 /*
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.
1923 */
1924
1925 close(fdinput[0]);
1926 close(fdinput[1]);
1927 close(fdoutput[0]);
1928 close(fdoutput[1]);
1929
1930 execlp(formatproc, formatproc, (char *) NULL);
1931
1932 adios(formatproc, "Unable to execute filter");
1933
1934 break;
1935
1936 case -1:
1937 adios(NULL, "Unable to fork format program");
1938 }
1939
1940 /*
1941 * Close everything except our reader (fdoutput[0]);
1942 */
1943
1944 close(fdinput[0]);
1945 close(fdinput[1]);
1946 close(fdoutput[1]);
1947
1948 /*
1949 * As we read in this data, send it to putcomp
1950 */
1951
1952 holder.c_text = buf;
1953
1954 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
1955 buf[cc] = '\0';
1956 putcomp(c1, &holder, BODYCOMP);
1957 }
1958
1959 if (cc < 0) {
1960 adios(NULL, "reading from formatproc");
1961 }
1962
1963 /*
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.
1966 */
1967
1968 if (waitpid(filterpid, &waitstat, 0) < 0) {
1969 if (errno != ECHILD) {
1970 adios("filterproc", "Unable to determine status");
1971 }
1972 } else {
1973 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1974 pidstatus(waitstat, stderr, "filterproc");
1975 }
1976 }
1977
1978 if (waitpid(writerpid, &waitstat, 0) < 0) {
1979 if (errno != ECHILD) {
1980 adios("writer process", "Unable to determine status");
1981 done(1);
1982 }
1983 } else {
1984 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
1985 pidstatus(waitstat, stderr, "writer process");
1986 done(1);
1987 }
1988 }
1989
1990 close(fdoutput[0]);
1991 }