]> diplodocus.org Git - nmh/blob - uip/mhlsbr.c
Formatting cleanup.
[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 <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 #define FMTPROCSW 23
95 { "fmtproc program", 0 },
96 #define NFMTPROCSW 24
97 { "nofmtproc", 0 },
98 { NULL, 0 }
99 };
100
101 #define NOCOMPONENT 0x000001 /* don't show component name */
102 #define UPPERCASE 0x000002 /* display in all upper case */
103 #define CENTER 0x000004 /* center line */
104 #define CLEARTEXT 0x000008 /* cleartext */
105 #define EXTRA 0x000010 /* an "extra" component */
106 #define HDROUTPUT 0x000020 /* already output */
107 #define CLEARSCR 0x000040 /* clear screen */
108 #define LEFTADJUST 0x000080 /* left justify multiple lines */
109 #define COMPRESS 0x000100 /* compress text */
110 #define ADDRFMT 0x000200 /* contains addresses */
111 #define BELL 0x000400 /* sound bell at EOP */
112 #define DATEFMT 0x000800 /* contains dates */
113 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
114 #define INIT 0x002000 /* initialize component */
115 #define FACEFMT 0x004000 /* contains face */
116 #define FACEDFLT 0x008000 /* default for face */
117 #define SPLIT 0x010000 /* split headers (don't concatenate) */
118 #define NONEWLINE 0x020000 /* don't write trailing newline */
119 #define NOWRAP 0x040000 /* Don't wrap lines ever */
120 #define FMTFILTER 0x080000 /* Filter through format filter */
121 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE\023NOWRAP\024FMTFILTER"
122 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT | NOWRAP)
123
124 /*
125 * A list of format arguments
126 */
127
128 struct arglist {
129 struct format *a_fmt;
130 char *a_nfs;
131 struct arglist *a_next;
132 };
133
134 struct mcomp {
135 char *c_name; /* component name */
136 char *c_text; /* component text */
137 char *c_ovtxt; /* text overflow indicator */
138 char *c_nfs; /* iff FORMAT */
139 struct format *c_fmt; /* .. */
140 char *c_face; /* face designator */
141 int c_offset; /* left margin indentation */
142 int c_ovoff; /* overflow indentation */
143 int c_width; /* width of field */
144 int c_cwidth; /* width of component */
145 int c_length; /* length in lines */
146 long c_flags;
147 struct arglist *c_f_args; /* Argument list for filter*/
148 struct arglist *c_f_tail; /* Pointer to tail of list */
149 int c_nargs; /* Number of arguments */
150 struct mcomp *c_next;
151 };
152
153 static struct mcomp *msghd = NULL;
154 static struct mcomp *msgtl = NULL;
155 static struct mcomp *fmthd = NULL;
156 static struct mcomp *fmttl = NULL;
157
158 static struct mcomp global = {
159 NULL, NULL, NULL, NULL, NULL, NULL, 0, -1, 80, -1, 40, BELL, NULL, NULL,
160 0, NULL
161 };
162
163 static struct mcomp holder = {
164 NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, NULL, NULL,
165 0, NULL
166 };
167
168 struct pair {
169 char *p_name;
170 long p_flags;
171 };
172
173 static struct pair pairs[] = {
174 { "Date", DATEFMT },
175 { "From", ADDRFMT|FACEDFLT },
176 { "Sender", ADDRFMT },
177 { "Reply-To", ADDRFMT },
178 { "To", ADDRFMT },
179 { "cc", ADDRFMT },
180 { "Bcc", ADDRFMT },
181 { "Resent-Date", DATEFMT },
182 { "Resent-From", ADDRFMT },
183 { "Resent-Sender", ADDRFMT },
184 { "Resent-Reply-To", ADDRFMT },
185 { "Resent-To", ADDRFMT },
186 { "Resent-cc", ADDRFMT },
187 { "Resent-Bcc", ADDRFMT },
188 { "Face", FACEFMT },
189 { NULL, 0 }
190 };
191
192 struct triple {
193 char *t_name;
194 long t_on;
195 long t_off;
196 };
197
198 static struct triple triples[] = {
199 { "nocomponent", NOCOMPONENT, 0 },
200 { "uppercase", UPPERCASE, 0 },
201 { "nouppercase", 0, UPPERCASE },
202 { "center", CENTER, 0 },
203 { "nocenter", 0, CENTER },
204 { "clearscreen", CLEARSCR, 0 },
205 { "noclearscreen", 0, CLEARSCR },
206 { "noclear", 0, CLEARSCR },
207 { "leftadjust", LEFTADJUST, 0 },
208 { "noleftadjust", 0, LEFTADJUST },
209 { "compress", COMPRESS, 0 },
210 { "nocompress", 0, COMPRESS },
211 { "split", SPLIT, 0 },
212 { "nosplit", 0, SPLIT },
213 { "addrfield", ADDRFMT, DATEFMT },
214 { "bell", BELL, 0 },
215 { "nobell", 0, BELL },
216 { "datefield", DATEFMT, ADDRFMT },
217 { "newline", 0, NONEWLINE },
218 { "nonewline", NONEWLINE, 0 },
219 { "wrap", 0, NOWRAP },
220 { "nowrap", NOWRAP, 0 },
221 { "format", FMTFILTER, 0 },
222 { "noformat", 0, FMTFILTER },
223 { NULL, 0, 0 }
224 };
225
226 static char *addrcomps[] = {
227 "from",
228 "sender",
229 "reply-to",
230 "to",
231 "cc",
232 "bcc",
233 "resent-from",
234 "resent-sender",
235 "resent-reply-to",
236 "resent-to",
237 "resent-cc",
238 "resent-bcc",
239 NULL
240 };
241
242
243 static int bellflg = 0;
244 static int clearflg = 0;
245 static int dashstuff = 0;
246 static int dobody = 1;
247 static int forwflg = 0;
248 static int forwall = 0;
249
250 static int sleepsw = NOTOK;
251
252 static char *digest = NULL;
253 static int volume = 0;
254 static int issue = 0;
255
256 static int exitstat = 0;
257 static int mhldebug = 0;
258
259 static int filesize = 0;
260
261 #define PITTY (-1)
262 #define NOTTY 0
263 #define ISTTY 1
264 static int ontty = NOTTY;
265
266 static int row;
267 static unsigned int column;
268
269 static int lm;
270 static int llim;
271 static int ovoff;
272 static int term;
273 static unsigned int wid;
274
275 static char *ovtxt;
276
277 static unsigned char *onelp;
278
279 static char *parptr;
280
281 static int num_ignores = 0;
282 static char *ignores[MAXARGS];
283
284 static jmp_buf env;
285 static jmp_buf mhlenv;
286
287 static char delim3[] = /* from forw.c */
288 "\n----------------------------------------------------------------------\n\n";
289 static char delim4[] = "\n------------------------------\n\n";
290
291 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
292
293 static struct comp *mhlcomp[128];
294
295 /*
296 * Redefine a couple of functions.
297 * These are undefined later in the code.
298 */
299 #define adios mhladios
300 #define done mhldone
301
302 /*
303 * prototypes
304 */
305 static void mhl_format (char *, int, int);
306 static int evalvar (struct mcomp *);
307 static int ptoi (char *, int *);
308 static int ptos (char *, char **);
309 static char *parse (void);
310 static void process (char *, char *, int, int);
311 static void mhlfile (FILE *, char *, int, int);
312 static int mcomp_flags (char *);
313 static char *mcomp_add (long, char *, char *);
314 static void mcomp_format (struct mcomp *, struct mcomp *);
315 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
316 static void free_queue (struct mcomp **, struct mcomp **);
317 static void putcomp (struct mcomp *, struct mcomp *, int);
318 static char *oneline (char *, long);
319 static void putstr (char *, long);
320 static void putch (char, long);
321 static void intrser (int);
322 static void pipeser (int);
323 static void quitser (int);
324 static void face_format (struct mcomp *);
325 static int doface (struct mcomp *);
326 static void mhladios (char *, char *, ...);
327 static void mhldone (int);
328 static void m_popen (char *);
329 static void filterbody (struct mcomp *, char *, int, int, FILE *);
330 static int compileargs (struct mcomp *, char *);
331 static int checkcomp (char *, char *);
332 static void addcomp (int, char *, char *);
333 static void freecomps (void);
334 static void freecomptext (void);
335
336
337 int
338 mhl (int argc, char **argv)
339 {
340 int length = 0, nomore = 0;
341 unsigned int i, vecp = 0;;
342 int width = 0;
343 char *cp, *folder = NULL, *form = NULL;
344 char buf[BUFSIZ], *files[MAXARGS];
345 char **argp, **arguments;
346
347 invo_name = r1bindex (argv[0], '/');
348
349 /* read user profile/context */
350 context_read();
351
352 arguments = getarguments (invo_name, argc, argv, 1);
353 argp = arguments;
354
355 if ((cp = getenv ("MHLDEBUG")) && *cp)
356 mhldebug++;
357
358 if ((cp = getenv ("FACEPROC")))
359 faceproc = cp;
360
361 while ((cp = *argp++)) {
362 if (*cp == '-') {
363 switch (smatch (++cp, mhlswitches)) {
364 case AMBIGSW:
365 ambigsw (cp, mhlswitches);
366 done (1);
367 case UNKWNSW:
368 adios (NULL, "-%s unknown\n", cp);
369
370 case HELPSW:
371 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
372 print_help (buf, mhlswitches, 1);
373 done (1);
374 case VERSIONSW:
375 print_version(invo_name);
376 done (1);
377
378 case BELLSW:
379 bellflg = 1;
380 continue;
381 case NBELLSW:
382 bellflg = -1;
383 continue;
384
385 case CLRSW:
386 clearflg = 1;
387 continue;
388 case NCLRSW:
389 clearflg = -1;
390 continue;
391
392 case FOLDSW:
393 if (!(folder = *argp++) || *folder == '-')
394 adios (NULL, "missing argument to %s", argp[-2]);
395 continue;
396 case FORMSW:
397 if (!(form = *argp++) || *form == '-')
398 adios (NULL, "missing argument to %s", argp[-2]);
399 continue;
400
401 case FACESW:
402 if (!(faceproc = *argp++) || *faceproc == '-')
403 adios (NULL, "missing argument to %s", argp[-2]);
404 continue;
405 case NFACESW:
406 faceproc = NULL;
407 continue;
408 case SLEEPSW:
409 if (!(cp = *argp++) || *cp == '-')
410 adios (NULL, "missing argument to %s", argp[-2]);
411 sleepsw = atoi (cp);/* ZERO ok! */
412 continue;
413
414 case PROGSW:
415 if (!(moreproc = *argp++) || *moreproc == '-')
416 adios (NULL, "missing argument to %s", argp[-2]);
417 continue;
418 case NPROGSW:
419 nomore++;
420 continue;
421
422 case FMTPROCSW:
423 if (!(formatproc = *argp++) || *formatproc == '-')
424 adios (NULL, "missing argument to %s", argp[-2]);
425 continue;
426 case NFMTPROCSW:
427 formatproc = NULL;
428 continue;
429
430 case LENSW:
431 if (!(cp = *argp++) || *cp == '-')
432 adios (NULL, "missing argument to %s", argp[-2]);
433 if ((length = atoi (cp)) < 1)
434 adios (NULL, "bad argument %s %s", argp[-2], cp);
435 continue;
436 case WIDTHSW:
437 if (!(cp = *argp++) || *cp == '-')
438 adios (NULL, "missing argument to %s", argp[-2]);
439 if ((width = atoi (cp)) < 1)
440 adios (NULL, "bad argument %s %s", argp[-2], cp);
441 continue;
442
443 case DGSTSW:
444 if (!(digest = *argp++) || *digest == '-')
445 adios (NULL, "missing argument to %s", argp[-2]);
446 continue;
447 case ISSUESW:
448 if (!(cp = *argp++) || *cp == '-')
449 adios (NULL, "missing argument to %s", argp[-2]);
450 if ((issue = atoi (cp)) < 1)
451 adios (NULL, "bad argument %s %s", argp[-2], cp);
452 continue;
453 case VOLUMSW:
454 if (!(cp = *argp++) || *cp == '-')
455 adios (NULL, "missing argument to %s", argp[-2]);
456 if ((volume = atoi (cp)) < 1)
457 adios (NULL, "bad argument %s %s", argp[-2], cp);
458 continue;
459
460 case FORW2SW:
461 forwall++; /* fall */
462 case FORW1SW:
463 forwflg++;
464 clearflg = -1;/* XXX */
465 continue;
466
467 case BITSTUFFSW:
468 dashstuff = 1; /* trinary logic */
469 continue;
470 case NBITSTUFFSW:
471 dashstuff = -1; /* trinary logic */
472 continue;
473
474 case NBODYSW:
475 dobody = 0;
476 continue;
477 }
478 }
479 files[vecp++] = cp;
480 }
481
482 if (!folder)
483 folder = getenv ("mhfolder");
484
485 if (isatty (fileno (stdout))) {
486 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
487 if (mhl_action) {
488 SIGNAL (SIGINT, SIG_IGN);
489 SIGNAL2 (SIGQUIT, quitser);
490 }
491 SIGNAL2 (SIGPIPE, pipeser);
492 m_popen (moreproc);
493 ontty = PITTY;
494 } else {
495 SIGNAL (SIGINT, SIG_IGN);
496 SIGNAL2 (SIGQUIT, quitser);
497 ontty = ISTTY;
498 }
499 } else {
500 ontty = NOTTY;
501 }
502
503 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++)
504 mhlcomp[i] = NULL;
505
506 mhl_format (form ? form : mhlformat, length, width);
507
508 if (vecp == 0) {
509 process (folder, NULL, 1, vecp = 1);
510 } else {
511 for (i = 0; i < vecp; i++)
512 process (folder, files[i], i + 1, vecp);
513 }
514
515 freecomps();
516
517 if (forwall) {
518 if (digest) {
519 printf ("%s", delim4);
520 if (volume == 0) {
521 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
522 } else {
523 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
524 digest, volume, issue);
525 }
526 i = strlen (buf);
527 for (cp = buf + i; i > 1; i--)
528 *cp++ = '*';
529 *cp++ = '\n';
530 *cp = 0;
531 printf ("%s", buf);
532 }
533 else
534 printf ("\n------- End of Forwarded Message%s\n",
535 vecp > 1 ? "s" : "");
536 }
537
538 fflush(stdout);
539 if(ferror(stdout)){
540 adios("output", "error writing");
541 }
542
543 if (clearflg > 0 && ontty == NOTTY)
544 clear_screen ();
545
546 if (ontty == PITTY)
547 m_pclose ();
548
549 return exitstat;
550 }
551
552
553 static void
554 mhl_format (char *file, int length, int width)
555 {
556 int i;
557 char *bp, *cp, **ip;
558 char *ap, buffer[BUFSIZ], name[NAMESZ];
559 struct mcomp *c1;
560 struct stat st;
561 FILE *fp;
562 static dev_t dev = 0;
563 static ino_t ino = 0;
564 static time_t mtime = 0;
565
566 if (fmthd != NULL) {
567 if (stat (etcpath (file), &st) != NOTOK
568 && mtime == st.st_mtime
569 && dev == st.st_dev
570 && ino == st.st_ino)
571 goto out;
572 else
573 free_queue (&fmthd, &fmttl);
574 }
575
576 if ((fp = fopen (etcpath (file), "r")) == NULL)
577 adios (file, "unable to open format file");
578
579 if (fstat (fileno (fp), &st) != NOTOK) {
580 mtime = st.st_mtime;
581 dev = st.st_dev;
582 ino = st.st_ino;
583 }
584
585 global.c_ovtxt = global.c_nfs = NULL;
586 global.c_fmt = NULL;
587 global.c_offset = 0;
588 global.c_ovoff = -1;
589 if ((i = sc_width ()) > 5)
590 global.c_width = i;
591 global.c_cwidth = -1;
592 if ((i = sc_length ()) > 5)
593 global.c_length = i - 1;
594 global.c_flags = BELL; /* BELL is default */
595 *(ip = ignores) = NULL;
596
597 while (vfgets (fp, &ap) == OK) {
598 bp = ap;
599 if (*bp == ';')
600 continue;
601
602 if ((cp = strchr(bp, '\n')))
603 *cp = 0;
604
605 if (*bp == ':') {
606 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
607 continue;
608 }
609
610 parptr = bp;
611 strncpy (name, parse(), sizeof(name));
612 switch (*parptr) {
613 case '\0':
614 case ',':
615 case '=':
616 /*
617 * Split this list of fields to ignore, and copy
618 * it to the end of the current "ignores" list.
619 */
620 if (!mh_strcasecmp (name, "ignores")) {
621 char **tmparray, **p;
622 int n = 0;
623
624 /* split the fields */
625 tmparray = brkstring (getcpy (++parptr), ",", NULL);
626
627 /* count number of fields split */
628 p = tmparray;
629 while (*p++)
630 n++;
631
632 /* copy pointers to split fields to ignores array */
633 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
634 num_ignores += n;
635 continue;
636 }
637 parptr = bp;
638 while (*parptr) {
639 if (evalvar (&global))
640 adios (NULL, "format file syntax error: %s", bp);
641 if (*parptr)
642 parptr++;
643 }
644 continue;
645
646 case ':':
647 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
648 while (*parptr == ':' || *parptr == ',') {
649 parptr++;
650 if (evalvar (c1))
651 adios (NULL, "format file syntax error: %s", bp);
652 }
653 if (!c1->c_nfs && global.c_nfs) {
654 if (c1->c_flags & DATEFMT) {
655 if (global.c_flags & DATEFMT)
656 c1->c_nfs = getcpy (global.c_nfs);
657 }
658 else
659 if (c1->c_flags & ADDRFMT) {
660 if (global.c_flags & ADDRFMT)
661 c1->c_nfs = getcpy (global.c_nfs);
662 }
663 }
664 continue;
665
666 default:
667 adios (NULL, "format file syntax error: %s", bp);
668 }
669 }
670 fclose (fp);
671
672 if (mhldebug) {
673 for (c1 = fmthd; c1; c1 = c1->c_next) {
674 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
675 c1->c_name, c1->c_text, c1->c_ovtxt);
676 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
677 (unsigned int)(unsigned long) c1->c_nfs,
678 (unsigned int)(unsigned long) c1->c_fmt);
679 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
680 c1->c_offset, c1->c_ovoff, c1->c_width,
681 c1->c_cwidth, c1->c_length);
682 fprintf (stderr, "\tflags=%s\n",
683 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
684 }
685 }
686
687 out:
688 if (clearflg == 1) {
689 global.c_flags |= CLEARSCR;
690 } else {
691 if (clearflg == -1)
692 global.c_flags &= ~CLEARSCR;
693 }
694
695 switch (bellflg) { /* command line may override format file */
696 case 1:
697 global.c_flags |= BELL;
698 break;
699 case -1:
700 global.c_flags &= ~BELL;
701 break;
702 }
703
704 if (length)
705 global.c_length = length;
706 if (width)
707 global.c_width = width;
708 if (global.c_length < 5)
709 global.c_length = 10000;
710 if (global.c_width < 5)
711 global.c_width = 10000;
712 }
713
714
715 static int
716 evalvar (struct mcomp *c1)
717 {
718 char *cp, name[NAMESZ];
719 struct triple *ap;
720
721 if (!*parptr)
722 return 0;
723 strncpy (name, parse(), sizeof(name));
724
725 if (!mh_strcasecmp (name, "component")) {
726 if (ptos (name, &c1->c_text))
727 return 1;
728 c1->c_flags &= ~NOCOMPONENT;
729 return 0;
730 }
731
732 if (!mh_strcasecmp (name, "overflowtext"))
733 return ptos (name, &c1->c_ovtxt);
734
735 if (!mh_strcasecmp (name, "formatfield")) {
736 char *nfs;
737
738 if (ptos (name, &cp))
739 return 1;
740 nfs = new_fs (NULL, NULL, cp);
741 c1->c_nfs = getcpy (nfs);
742 c1->c_flags |= FORMAT;
743 return 0;
744 }
745
746 if (!mh_strcasecmp (name, "decode")) {
747 char *nfs;
748
749 nfs = new_fs (NULL, NULL, "%(decode{text})");
750 c1->c_nfs = getcpy (nfs);
751 c1->c_flags |= FORMAT;
752 return 0;
753 }
754
755 if (!mh_strcasecmp (name, "offset"))
756 return ptoi (name, &c1->c_offset);
757 if (!mh_strcasecmp (name, "overflowoffset"))
758 return ptoi (name, &c1->c_ovoff);
759 if (!mh_strcasecmp (name, "width"))
760 return ptoi (name, &c1->c_width);
761 if (!mh_strcasecmp (name, "compwidth"))
762 return ptoi (name, &c1->c_cwidth);
763 if (!mh_strcasecmp (name, "length"))
764 return ptoi (name, &c1->c_length);
765 if (!mh_strcasecmp (name, "nodashstuffing"))
766 return (dashstuff = -1);
767
768 for (ap = triples; ap->t_name; ap++)
769 if (!mh_strcasecmp (ap->t_name, name)) {
770 c1->c_flags |= ap->t_on;
771 c1->c_flags &= ~ap->t_off;
772 return 0;
773 }
774
775 if (!mh_strcasecmp (name, "formatarg")) {
776 char *nfs;
777 int rc;
778
779 if (ptos (name, &cp))
780 return 1;
781 nfs = new_fs (NULL, NULL, cp);
782
783 rc = compileargs(c1, nfs);
784
785 return rc;
786 }
787
788
789 return 1;
790 }
791
792
793 static int
794 ptoi (char *name, int *i)
795 {
796 char *cp;
797
798 if (*parptr++ != '=' || !*(cp = parse ())) {
799 advise (NULL, "missing argument to variable %s", name);
800 return 1;
801 }
802
803 *i = atoi (cp);
804 return 0;
805 }
806
807
808 static int
809 ptos (char *name, char **s)
810 {
811 char c, *cp;
812
813 if (*parptr++ != '=') {
814 advise (NULL, "missing argument to variable %s", name);
815 return 1;
816 }
817
818 if (*parptr != '"') {
819 for (cp = parptr;
820 *parptr && *parptr != ':' && *parptr != ',';
821 parptr++)
822 continue;
823 } else {
824 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
825 if (*parptr == QUOTE)
826 if (!*++parptr)
827 parptr--;
828 }
829 c = *parptr;
830 *parptr = 0;
831 *s = getcpy (cp);
832 if ((*parptr = c) == '"')
833 parptr++;
834 return 0;
835 }
836
837
838 static char *
839 parse (void)
840 {
841 int c;
842 char *cp;
843 static char result[NAMESZ];
844
845 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
846 c = *parptr;
847 if (isalnum (c)
848 || c == '.'
849 || c == '-'
850 || c == '_'
851 || c =='['
852 || c == ']')
853 *cp++ = c;
854 else
855 break;
856 }
857 *cp = '\0';
858
859 return result;
860 }
861
862
863 static void
864 process (char *folder, char *fname, int ofilen, int ofilec)
865 {
866 char *cp = NULL;
867 FILE *fp = NULL;
868 struct mcomp *c1;
869 struct stat st;
870
871 switch (setjmp (env)) {
872 case OK:
873 if (fname) {
874 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
875 if (fp == NULL) {
876 advise (fname, "unable to open");
877 exitstat++;
878 return;
879 }
880 } else {
881 fname = "(stdin)";
882 fp = stdin;
883 }
884 if (fstat(fileno(fp), &st) == 0) {
885 filesize = st.st_size;
886 } else {
887 filesize = 0;
888 }
889 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
890 if (ontty != PITTY)
891 SIGNAL (SIGINT, intrser);
892 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
893
894 default:
895 if (ontty != PITTY)
896 SIGNAL (SIGINT, SIG_IGN);
897 if (mhl_action == NULL && fp != stdin)
898 fclose (fp);
899 free (cp);
900 if (holder.c_text) {
901 free (holder.c_text);
902 holder.c_text = NULL;
903 }
904 free_queue (&msghd, &msgtl);
905 for (c1 = fmthd; c1; c1 = c1->c_next)
906 c1->c_flags &= ~HDROUTPUT;
907 break;
908 }
909
910 freecomptext();
911 }
912
913
914 static void
915 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
916 {
917 int state, bucket;
918 struct mcomp *c1, *c2, *c3;
919 char **ip, name[NAMESZ], buf[BUFSIZ];
920
921 if (forwall) {
922 if (digest)
923 printf ("%s", ofilen == 1 ? delim3 : delim4);
924 else {
925 printf ("\n-------");
926 if (ofilen == 1)
927 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
928 else
929 printf (" Message %d", ofilen);
930 printf ("\n\n");
931 }
932 } else {
933 switch (ontty) {
934 case PITTY:
935 if (ofilec > 1) {
936 if (ofilen > 1) {
937 if ((global.c_flags & CLEARSCR))
938 clear_screen ();
939 else
940 printf ("\n\n\n");
941 }
942 printf (">>> %s\n\n", mname);
943 }
944 break;
945
946 case ISTTY:
947 strncpy (buf, "\n", sizeof(buf));
948 if (ofilec > 1) {
949 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
950 if (ofilen > 1)
951 printf ("\n\n\n");
952 printf ("Press <return> to list \"%s\"...", mname);
953 }
954 fflush (stdout);
955 buf[0] = 0;
956 read (fileno (stdout), buf, sizeof(buf));
957 }
958 if (strchr(buf, '\n')) {
959 if ((global.c_flags & CLEARSCR))
960 clear_screen ();
961 }
962 else
963 printf ("\n");
964 break;
965
966 default:
967 if (ofilec > 1) {
968 if (ofilen > 1) {
969 printf ("\n\n\n");
970 if (clearflg > 0)
971 clear_screen ();
972 }
973 printf (">>> %s\n\n", mname);
974 }
975 break;
976 }
977 }
978
979 for (state = FLD;;) {
980 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
981 case FLD:
982 case FLDPLUS:
983 bucket = checkcomp(name, buf);
984 for (ip = ignores; *ip; ip++)
985 if (!mh_strcasecmp (name, *ip)) {
986 while (state == FLDPLUS) {
987 state = m_getfld (state, name, buf, sizeof(buf), fp);
988 addcomp(bucket, name, buf);
989 }
990 break;
991 }
992 if (*ip)
993 continue;
994
995 for (c2 = fmthd; c2; c2 = c2->c_next)
996 if (!mh_strcasecmp (c2->c_name, name))
997 break;
998 c1 = NULL;
999 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
1000 for (c1 = msghd; c1; c1 = c1->c_next)
1001 if (!mh_strcasecmp (c1->c_name, c3->c_name)) {
1002 c1->c_text =
1003 mcomp_add (c1->c_flags, buf, c1->c_text);
1004 break;
1005 }
1006 if (c1 == NULL)
1007 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
1008 while (state == FLDPLUS) {
1009 state = m_getfld (state, name, buf, sizeof(buf), fp);
1010 c1->c_text = add (buf, c1->c_text);
1011 addcomp(bucket, name, buf);
1012 }
1013 if (c2 == NULL)
1014 c1->c_flags |= EXTRA;
1015 continue;
1016
1017 case BODY:
1018 case FILEEOF:
1019 row = column = 0;
1020 for (c1 = fmthd; c1; c1 = c1->c_next) {
1021 if (c1->c_flags & CLEARTEXT) {
1022 putcomp (c1, c1, ONECOMP);
1023 continue;
1024 }
1025 if (!mh_strcasecmp (c1->c_name, "messagename")) {
1026 holder.c_text = concat ("(Message ", mname, ")\n",
1027 NULL);
1028 putcomp (c1, &holder, ONECOMP);
1029 free (holder.c_text);
1030 holder.c_text = NULL;
1031 continue;
1032 }
1033 if (!mh_strcasecmp (c1->c_name, "extras")) {
1034 for (c2 = msghd; c2; c2 = c2->c_next)
1035 if (c2->c_flags & EXTRA)
1036 putcomp (c1, c2, TWOCOMP);
1037 continue;
1038 }
1039 if (dobody && !mh_strcasecmp (c1->c_name, "body")) {
1040 if (c1->c_flags & FMTFILTER && state == BODY &&
1041 formatproc != NULL) {
1042 filterbody(c1, buf, sizeof(buf), state, fp);
1043 } else {
1044 holder.c_text = mh_xmalloc (sizeof(buf));
1045 strncpy (holder.c_text, buf, sizeof(buf));
1046 while (state == BODY) {
1047 putcomp (c1, &holder, BODYCOMP);
1048 state = m_getfld (state, name, holder.c_text,
1049 sizeof(buf), fp);
1050 }
1051 free (holder.c_text);
1052 holder.c_text = NULL;
1053 }
1054 continue;
1055 }
1056 for (c2 = msghd; c2; c2 = c2->c_next)
1057 if (!mh_strcasecmp (c2->c_name, c1->c_name)) {
1058 putcomp (c1, c2, ONECOMP);
1059 if (!(c1->c_flags & SPLIT))
1060 break;
1061 }
1062 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
1063 for (c2 = msghd; c2; c2 = c2->c_next)
1064 if (c2->c_flags & FACEDFLT) {
1065 if (c2->c_face == NULL)
1066 face_format (c2);
1067 if ((holder.c_text = c2->c_face)) {
1068 putcomp (c1, &holder, ONECOMP);
1069 holder.c_text = NULL;
1070 }
1071 break;
1072 }
1073 }
1074 return;
1075
1076 case LENERR:
1077 case FMTERR:
1078 advise (NULL, "format error in message %s", mname);
1079 exitstat++;
1080 return;
1081
1082 default:
1083 adios (NULL, "getfld() returned %d", state);
1084 }
1085 }
1086 }
1087
1088
1089 static int
1090 mcomp_flags (char *name)
1091 {
1092 struct pair *ap;
1093
1094 for (ap = pairs; ap->p_name; ap++)
1095 if (!mh_strcasecmp (ap->p_name, name))
1096 return (ap->p_flags);
1097
1098 return 0;
1099 }
1100
1101
1102 static char *
1103 mcomp_add (long flags, char *s1, char *s2)
1104 {
1105 char *dp;
1106
1107 if (!(flags & ADDRFMT))
1108 return add (s1, s2);
1109
1110 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1111 *dp = 0;
1112
1113 return add (s1, add (",\n", s2));
1114 }
1115
1116
1117 struct pqpair {
1118 char *pq_text;
1119 char *pq_error;
1120 struct pqpair *pq_next;
1121 };
1122
1123
1124 static void
1125 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1126 {
1127 int dat[5];
1128 char *ap, *cp;
1129 char buffer[BUFSIZ], error[BUFSIZ];
1130 struct comp *cptr;
1131 struct pqpair *p, *q;
1132 struct pqpair pq;
1133 struct mailname *mp;
1134
1135 ap = c2->c_text;
1136 c2->c_text = NULL;
1137 dat[0] = 0;
1138 dat[1] = 0;
1139 dat[2] = filesize;
1140 dat[3] = sizeof(buffer) - 1;
1141 dat[4] = 0;
1142 fmt_compile (c1->c_nfs, &c1->c_fmt);
1143
1144 if (!(c1->c_flags & ADDRFMT)) {
1145 FINDCOMP (cptr, "text");
1146 if (cptr)
1147 cptr->c_text = ap;
1148 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1149 if (!cp[1])
1150 *cp = 0;
1151
1152 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1153 /* Don't need to append a newline, dctime() already did */
1154 c2->c_text = getcpy (buffer);
1155
1156 free (ap);
1157 return;
1158 }
1159
1160 (q = &pq)->pq_next = NULL;
1161 while ((cp = getname (ap))) {
1162 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1163 adios (NULL, "unable to allocate pqpair memory");
1164
1165 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1166 p->pq_text = getcpy (cp);
1167 p->pq_error = getcpy (error);
1168 } else {
1169 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1170 char *h = mp->m_host ? mp->m_host : LocalName (0);
1171 c2->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1172 }
1173 p->pq_text = getcpy (mp->m_text);
1174 mnfree (mp);
1175 }
1176 q = (q->pq_next = p);
1177 }
1178
1179 for (p = pq.pq_next; p; p = q) {
1180 FINDCOMP (cptr, "text");
1181 if (cptr)
1182 cptr->c_text = p->pq_text;
1183 FINDCOMP (cptr, "error");
1184 if (cptr)
1185 cptr->c_text = p->pq_error;
1186
1187 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1188 if (*buffer) {
1189 if (c2->c_text)
1190 c2->c_text = add (",\n", c2->c_text);
1191 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1192 *cp = 0;
1193 c2->c_text = add (buffer, c2->c_text);
1194 }
1195
1196 free (p->pq_text);
1197 if (p->pq_error)
1198 free (p->pq_error);
1199 q = p->pq_next;
1200 free ((char *) p);
1201 }
1202
1203 c2->c_text = add ("\n", c2->c_text);
1204 free (ap);
1205 }
1206
1207
1208 static struct mcomp *
1209 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1210 {
1211 struct mcomp *c1;
1212
1213 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1214 adios (NULL, "unable to allocate comp memory");
1215
1216 c1->c_flags = flags & ~INIT;
1217 if ((c1->c_name = name ? getcpy (name) : NULL))
1218 c1->c_flags |= mcomp_flags (c1->c_name);
1219 c1->c_text = text ? getcpy (text) : NULL;
1220 if (flags & INIT) {
1221 if (global.c_ovtxt)
1222 c1->c_ovtxt = getcpy (global.c_ovtxt);
1223 c1->c_offset = global.c_offset;
1224 c1->c_ovoff = global. c_ovoff;
1225 c1->c_width = c1->c_length = 0;
1226 c1->c_cwidth = global.c_cwidth;
1227 c1->c_flags |= global.c_flags & GFLAGS;
1228 }
1229 if (*head == NULL)
1230 *head = c1;
1231 if (*tail != NULL)
1232 (*tail)->c_next = c1;
1233 *tail = c1;
1234
1235 return c1;
1236 }
1237
1238
1239 static void
1240 free_queue (struct mcomp **head, struct mcomp **tail)
1241 {
1242 struct mcomp *c1, *c2;
1243
1244 for (c1 = *head; c1; c1 = c2) {
1245 c2 = c1->c_next;
1246 if (c1->c_name)
1247 free (c1->c_name);
1248 if (c1->c_text)
1249 free (c1->c_text);
1250 if (c1->c_ovtxt)
1251 free (c1->c_ovtxt);
1252 if (c1->c_nfs)
1253 free (c1->c_nfs);
1254 if (c1->c_fmt)
1255 free ((char *) c1->c_fmt);
1256 if (c1->c_face)
1257 free (c1->c_face);
1258 if (c1->c_f_args) {
1259 struct arglist *a1, *a2;
1260 for (a1 = c1->c_f_args; a1; a1 = a2) {
1261 a2 = a1->a_next;
1262 if (a1->a_fmt)
1263 free(a1->a_fmt);
1264 if (a1->a_nfs)
1265 free(a1->a_nfs);
1266 }
1267 free(a1);
1268 }
1269 free ((char *) c1);
1270 }
1271
1272 *head = *tail = NULL;
1273 }
1274
1275
1276 static void
1277 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1278 {
1279 int count, cchdr;
1280 unsigned char *cp;
1281
1282 cchdr = 0;
1283 lm = 0;
1284 llim = c1->c_length ? c1->c_length : -1;
1285 wid = c1->c_width ? c1->c_width : global.c_width;
1286 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1287 + c1->c_offset;
1288 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1289 ovtxt = "";
1290 if (wid < ovoff + strlen (ovtxt) + 5)
1291 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1292 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1293 onelp = NULL;
1294
1295 if (c1->c_flags & CLEARTEXT) {
1296 putstr (c1->c_text, c1->c_flags);
1297 putstr ("\n", c1->c_flags);
1298 return;
1299 }
1300
1301 if (c1->c_flags & FACEFMT)
1302 switch (doface (c2)) {
1303 case NOTOK: /* error */
1304 case OK: /* async faceproc */
1305 return;
1306
1307 default: /* sync faceproc */
1308 break;
1309 }
1310
1311 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1312 mcomp_format (c1, c2);
1313
1314 if (c1->c_flags & CENTER) {
1315 count = (c1->c_width ? c1->c_width : global.c_width)
1316 - c1->c_offset - strlen (c2->c_text);
1317 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1318 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1319 lm = c1->c_offset + (count / 2);
1320 } else {
1321 if (c1->c_offset)
1322 lm = c1->c_offset;
1323 }
1324
1325 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1326 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1327 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1328 if (islower (*cp))
1329 *cp = toupper (*cp);
1330 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1331 if (flag != BODYCOMP) {
1332 putstr (": ", c1->c_flags);
1333 if (!(c1->c_flags & SPLIT))
1334 c1->c_flags |= HDROUTPUT;
1335
1336 cchdr++;
1337 if ((count = c1->c_cwidth -
1338 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1339 while (count--)
1340 putstr (" ", c1->c_flags);
1341 }
1342 else
1343 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1344 }
1345
1346 if (flag == TWOCOMP
1347 && !(c2->c_flags & HDROUTPUT)
1348 && !(c2->c_flags & NOCOMPONENT)) {
1349 if (c1->c_flags & UPPERCASE)
1350 for (cp = c2->c_name; *cp; cp++)
1351 if (islower (*cp))
1352 *cp = toupper (*cp);
1353 putstr (c2->c_name, c1->c_flags);
1354 putstr (": ", c1->c_flags);
1355 if (!(c1->c_flags & SPLIT))
1356 c2->c_flags |= HDROUTPUT;
1357
1358 cchdr++;
1359 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1360 while (count--)
1361 putstr (" ", c1->c_flags);
1362 }
1363 if (c1->c_flags & UPPERCASE)
1364 for (cp = c2->c_text; *cp; cp++)
1365 if (islower (*cp))
1366 *cp = toupper (*cp);
1367
1368 count = 0;
1369 if (cchdr) {
1370 if (flag == TWOCOMP)
1371 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1372 : (int) strlen (c2->c_name) + 2;
1373 else
1374 count = (c1->c_cwidth >= 0) ? (size_t) c1->c_cwidth
1375 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1376 }
1377 count += c1->c_offset;
1378
1379 if ((cp = oneline (c2->c_text, c1->c_flags)))
1380 putstr(cp, c1->c_flags);
1381 if (term == '\n')
1382 putstr ("\n", c1->c_flags);
1383 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1384 lm = count;
1385 if (flag == BODYCOMP
1386 && !(c1->c_flags & NOCOMPONENT))
1387 putstr (c1->c_text ? c1->c_text : c1->c_name, c1->c_flags);
1388 if (*cp)
1389 putstr (cp, c1->c_flags);
1390 if (term == '\n')
1391 putstr ("\n", c1->c_flags);
1392 }
1393 if (flag == BODYCOMP && term == '\n')
1394 c1->c_flags &= ~HDROUTPUT; /* Buffer ended on a newline */
1395 }
1396
1397
1398 static char *
1399 oneline (char *stuff, long flags)
1400 {
1401 int spc;
1402 char *cp, *ret;
1403
1404 if (onelp == NULL)
1405 onelp = stuff;
1406 if (*onelp == 0)
1407 return (onelp = NULL);
1408
1409 ret = onelp;
1410 term = 0;
1411 if (flags & COMPRESS) {
1412 for (spc = 1, cp = ret; *onelp; onelp++)
1413 if (isspace (*onelp)) {
1414 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1415 term = '\n';
1416 *onelp++ = 0;
1417 break;
1418 }
1419 else
1420 if (!spc) {
1421 *cp++ = ' ';
1422 spc++;
1423 }
1424 }
1425 else {
1426 *cp++ = *onelp;
1427 spc = 0;
1428 }
1429
1430 *cp = 0;
1431 }
1432 else {
1433 while (*onelp && *onelp != '\n')
1434 onelp++;
1435 if (*onelp == '\n') {
1436 term = '\n';
1437 *onelp++ = 0;
1438 }
1439 if (flags & LEFTADJUST)
1440 while (*ret == ' ' || *ret == '\t')
1441 ret++;
1442 }
1443 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1444 term = 0;
1445
1446 return ret;
1447 }
1448
1449
1450 static void
1451 putstr (char *string, long flags)
1452 {
1453 if (!column && lm > 0) {
1454 while (lm > 0)
1455 if (lm >= 8) {
1456 putch ('\t', flags);
1457 lm -= 8;
1458 }
1459 else {
1460 putch (' ', flags);
1461 lm--;
1462 }
1463 }
1464 lm = 0;
1465 while (*string)
1466 putch (*string++, flags);
1467 }
1468
1469
1470 static void
1471 putch (char ch, long flags)
1472 {
1473 char buf[BUFSIZ];
1474
1475 if (llim == 0)
1476 return;
1477
1478 switch (ch) {
1479 case '\n':
1480 if (llim > 0)
1481 llim--;
1482 column = 0;
1483 row++;
1484 if (ontty != ISTTY || row != global.c_length)
1485 break;
1486 if (global.c_flags & BELL)
1487 putchar ('\007');
1488 fflush (stdout);
1489 buf[0] = 0;
1490 read (fileno (stdout), buf, sizeof(buf));
1491 if (strchr(buf, '\n')) {
1492 if (global.c_flags & CLEARSCR)
1493 clear_screen ();
1494 row = 0;
1495 } else {
1496 putchar ('\n');
1497 row = global.c_length / 3;
1498 }
1499 return;
1500
1501 case '\t':
1502 column |= 07;
1503 column++;
1504 break;
1505
1506 case '\b':
1507 column--;
1508 break;
1509
1510 case '\r':
1511 column = 0;
1512 break;
1513
1514 default:
1515 /*
1516 * If we are forwarding this message, and the first
1517 * column contains a dash, then add a dash and a space.
1518 */
1519 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1520 putchar ('-');
1521 putchar (' ');
1522 }
1523 if (ch >= ' ')
1524 column++;
1525 break;
1526 }
1527
1528 if (column >= wid && (flags & NOWRAP) == 0) {
1529 putch ('\n', flags);
1530 if (ovoff > 0)
1531 lm = ovoff;
1532 putstr (ovtxt ? ovtxt : "", flags);
1533 putch (ch, flags);
1534 return;
1535 }
1536
1537 putchar (ch);
1538 }
1539
1540
1541 static void
1542 intrser (int i)
1543 {
1544 NMH_UNUSED (i);
1545
1546 discard (stdout);
1547 putchar ('\n');
1548 longjmp (env, DONE);
1549 }
1550
1551
1552 static void
1553 pipeser (int i)
1554 {
1555 NMH_UNUSED (i);
1556
1557 done (NOTOK);
1558 }
1559
1560
1561 static void
1562 quitser (int i)
1563 {
1564 NMH_UNUSED (i);
1565
1566 putchar ('\n');
1567 fflush (stdout);
1568 done (NOTOK);
1569 }
1570
1571
1572 static void
1573 face_format (struct mcomp *c1)
1574 {
1575 char *cp;
1576 struct mailname *mp;
1577
1578 if ((cp = c1->c_text) == NULL)
1579 return;
1580
1581 if ((cp = getname (cp))) {
1582 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1583 char *h = mp->m_host ? mp->m_host : LocalName (0);
1584 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1585 }
1586
1587 while ((cp = getname (cp)))
1588 continue;
1589 }
1590 }
1591
1592
1593 /*
1594 * faceproc is two elements defining the image agent's location:
1595 * Internet host
1596 * UDP port
1597 */
1598
1599 #include <sys/socket.h>
1600 #include <netinet/in.h>
1601 #include <netdb.h>
1602 #include <arpa/inet.h>
1603
1604 static int
1605 doface (struct mcomp *c1)
1606 {
1607 int result, sd;
1608 static int inited = OK;
1609 static struct sockaddr_storage ss;
1610 static socklen_t socklen;
1611 static int socktype;
1612 static int protocol;
1613
1614 if (inited == OK) {
1615 char *cp;
1616 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1617 struct addrinfo hints, *res;
1618
1619 if (ap[0] == NULL || ap[1] == NULL) {
1620 bad_faceproc: ;
1621 free (cp);
1622 return (inited = NOTOK);
1623 }
1624
1625 memset(&hints, 0, sizeof(hints));
1626 #ifdef AI_ADDRCONFIG
1627 hints.ai_flags = AI_ADDRCONFIG;
1628 #endif
1629 hints.ai_family = PF_UNSPEC;
1630 hints.ai_socktype = SOCK_DGRAM;
1631
1632 if (getaddrinfo(ap[0], ap[1], &hints, &res) != 0)
1633 goto bad_faceproc;
1634
1635 memcpy(&ss, res->ai_addr, res->ai_addrlen);
1636 socklen = res->ai_addrlen;
1637 socktype = res->ai_socktype;
1638 protocol = res->ai_protocol;
1639 freeaddrinfo(res);
1640
1641 inited = DONE;
1642 }
1643 if (inited == NOTOK)
1644 return NOTOK;
1645
1646 if ((sd = socket (ss.ss_family, socktype, protocol)) == NOTOK)
1647 return NOTOK;
1648
1649 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1650 (struct sockaddr *) &ss, socklen);
1651
1652 close (sd);
1653
1654 return (result != NOTOK ? OK : NOTOK);
1655 }
1656
1657 /*
1658 * COMMENTED OUT
1659 * This version doesn't use sockets
1660 */
1661 #if 0
1662
1663 static int
1664 doface (struct mcomp *c1)
1665 {
1666 int i, len, vecp;
1667 pid_t child_id;
1668 int result, pdi[2], pdo[2];
1669 char *bp, *cp;
1670 char buffer[BUFSIZ], *vec[10];
1671
1672 if (pipe (pdi) == NOTOK)
1673 return NOTOK;
1674 if (pipe (pdo) == NOTOK) {
1675 close (pdi[0]);
1676 close (pdi[1]);
1677 return NOTOK;
1678 }
1679
1680 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1681 sleep (5);
1682
1683 switch (child_id) {
1684 case NOTOK:
1685 /* oops... fork error */
1686 return NOTOK;
1687
1688 case OK:
1689 /* child process */
1690 SIGNAL (SIGINT, SIG_IGN);
1691 SIGNAL (SIGQUIT, SIG_IGN);
1692 if (pdi[0] != fileno (stdin)) {
1693 dup2 (pdi[0], fileno (stdin));
1694 close (pdi[0]);
1695 }
1696 close (pdi[1]);
1697 close (pdo[0]);
1698 if (pdo[1] != fileno (stdout)) {
1699 dup2 (pdo[1], fileno (stdout));
1700 close (pdo[1]);
1701 }
1702 vecp = 0;
1703 vec[vecp++] = r1bindex (faceproc, '/');
1704 vec[vecp++] = "-e";
1705 if (sleepsw != NOTOK) {
1706 vec[vecp++] = "-s";
1707 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1708 vec[vecp++] = buffer;
1709 }
1710 vec[vecp] = NULL;
1711 execvp (faceproc, vec);
1712 fprintf (stderr, "unable to exec ");
1713 perror (faceproc);
1714 _exit (-1); /* NOTREACHED */
1715
1716 default:
1717 /* parent process */
1718 close (pdi[0]);
1719 i = strlen (c1->c_text);
1720 if (write (pdi[1], c1->c_text, i) != i)
1721 adios ("pipe", "error writing to");
1722 free (c1->c_text), c1->c_text = NULL;
1723 close (pdi[1]);
1724
1725 close (pdo[1]);
1726 cp = NULL, len = 0;
1727 result = DONE;
1728 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1729 if (cp) {
1730 int j;
1731 char *dp;
1732 dp = mh_xrealloc (cp, (unsigned) (j = len + i));
1733 memcpy(dp + len, buffer, i);
1734 cp = dp, len = j;
1735 }
1736 else {
1737 cp = mh_xmalloc ((unsigned) i);
1738 memcpy(cp, buffer, i);
1739 len = i;
1740 }
1741 if (result == DONE)
1742 for (bp = buffer + i - 1; bp >= buffer; bp--)
1743 if (!isascii (*bp) || iscntrl (*bp)) {
1744 result = OK;
1745 break;
1746 }
1747 }
1748 close (pdo[0]);
1749
1750 /* no waiting for child... */
1751
1752 if (result == OK) { /* binary */
1753 if (write (1, cp, len) != len)
1754 adios ("writing", "error");
1755 free (cp);
1756 }
1757 else /* empty */
1758 if ((c1->c_text = cp) == NULL)
1759 result = OK;
1760 break;
1761 }
1762
1763 return result;
1764 }
1765 #endif /* COMMENTED OUT */
1766
1767
1768 int
1769 mhlsbr (int argc, char **argv, FILE *(*action)())
1770 {
1771 SIGNAL_HANDLER istat = NULL, pstat = NULL, qstat = NULL;
1772 char *cp = NULL;
1773 struct mcomp *c1;
1774
1775 switch (setjmp (mhlenv)) {
1776 case OK:
1777 cp = invo_name;
1778 sleepsw = 0; /* XXX */
1779 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1780 digest = NULL;
1781 ontty = NOTTY;
1782 mhl_action = action;
1783
1784 /*
1785 * If signal is at default action, then start ignoring
1786 * it, else let it set to its current action.
1787 */
1788 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1789 SIGNAL (SIGINT, istat);
1790 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1791 SIGNAL (SIGQUIT, qstat);
1792 pstat = SIGNAL (SIGPIPE, pipeser);
1793 mhl (argc, argv); /* FALL THROUGH! */
1794
1795 default:
1796 SIGNAL (SIGINT, istat);
1797 SIGNAL (SIGQUIT, qstat);
1798 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1799 if (ontty == PITTY)
1800 m_pclose ();
1801 SIGNAL (SIGPIPE, pstat);
1802 invo_name = cp;
1803 if (holder.c_text) {
1804 free (holder.c_text);
1805 holder.c_text = NULL;
1806 }
1807 free_queue (&msghd, &msgtl);
1808 for (c1 = fmthd; c1; c1 = c1->c_next)
1809 c1->c_flags &= ~HDROUTPUT;
1810 return exitstat;
1811 }
1812 }
1813
1814 #undef adios
1815 #undef done
1816
1817 static void
1818 mhladios (char *what, char *fmt, ...)
1819 {
1820 va_list ap;
1821
1822 va_start(ap, fmt);
1823 advertise (what, NULL, fmt, ap);
1824 va_end(ap);
1825 mhldone (1);
1826 }
1827
1828
1829 static void
1830 mhldone (int status)
1831 {
1832 exitstat = status;
1833 if (mhl_action)
1834 longjmp (mhlenv, DONE);
1835 else
1836 done (exitstat);
1837 }
1838
1839
1840 static int m_pid = NOTOK;
1841 static int sd = NOTOK;
1842
1843 static void
1844 m_popen (char *name)
1845 {
1846 int pd[2];
1847
1848 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1849 adios ("standard output", "unable to dup()");
1850
1851 if (pipe (pd) == NOTOK)
1852 adios ("pipe", "unable to");
1853
1854 switch (m_pid = vfork()) {
1855 case NOTOK:
1856 adios ("fork", "unable to");
1857
1858 case OK:
1859 SIGNAL (SIGINT, SIG_DFL);
1860 SIGNAL (SIGQUIT, SIG_DFL);
1861
1862 close (pd[1]);
1863 if (pd[0] != fileno (stdin)) {
1864 dup2 (pd[0], fileno (stdin));
1865 close (pd[0]);
1866 }
1867 execlp (name, r1bindex (name, '/'), NULL);
1868 fprintf (stderr, "unable to exec ");
1869 perror (name);
1870 _exit (-1);
1871
1872 default:
1873 close (pd[0]);
1874 if (pd[1] != fileno (stdout)) {
1875 dup2 (pd[1], fileno (stdout));
1876 close (pd[1]);
1877 }
1878 }
1879 }
1880
1881
1882 void
1883 m_pclose (void)
1884 {
1885 if (m_pid == NOTOK)
1886 return;
1887
1888 if (sd != NOTOK) {
1889 fflush (stdout);
1890 if (dup2 (sd, fileno (stdout)) == NOTOK)
1891 adios ("standard output", "unable to dup2()");
1892
1893 clearerr (stdout);
1894 close (sd);
1895 sd = NOTOK;
1896 }
1897 else
1898 fclose (stdout);
1899
1900 pidwait (m_pid, OK);
1901 m_pid = NOTOK;
1902 }
1903
1904
1905 /*
1906 * Compile a format string and add it to the list of arguments used by
1907 * the formatproc.
1908 *
1909 * This deserves some explanation. Here's the deal:
1910 *
1911 * We want to keep track of components used as arguments by formatproc,
1912 * but the hash table is reset every time fmt_compile is called. So we
1913 * iterate through the function list looking for things that use components
1914 * and save the name. And because we might get the same components used
1915 * by different arguments we need to keep track to every reference of
1916 * every component so we can add them when the message is processed. So
1917 * we compile the argument string now (to get the components we use) and
1918 * save them for later.
1919 */
1920
1921 static int
1922 compileargs (struct mcomp *c1, char *nfs)
1923 {
1924 struct format *fmt;
1925 struct arglist *args;
1926 char **ap;
1927 struct comp *cptr;
1928 unsigned int i;
1929
1930 i = fmt_compile(nfs, &fmt);
1931
1932 /*
1933 * Search through and mark any components that are address components
1934 */
1935
1936 for (ap = addrcomps; *ap; ap++) {
1937 FINDCOMP (cptr, *ap);
1938 if (cptr)
1939 cptr->c_type |= CT_ADDR;
1940 }
1941
1942 args = (struct arglist *) mh_xmalloc(sizeof(struct arglist));
1943
1944 if (! args)
1945 adios (NULL, "Unable to allocate formatproc args storage");
1946
1947 args->a_fmt = fmt;
1948 args->a_nfs = format_string;
1949 args->a_next = NULL;
1950 c1->c_nargs++;
1951 format_string = NULL;
1952
1953 if (c1->c_f_tail)
1954 c1->c_f_tail->a_next = args;
1955
1956 c1->c_f_tail = args;
1957
1958 if (! c1->c_f_args)
1959 c1->c_f_args = args;
1960
1961 if (i == 0)
1962 return 0;
1963
1964 /*
1965 * If wantcomp ever changes size, we need to change the size
1966 * of mhlcomp as well
1967 */
1968
1969 for (i = 0; i < sizeof(wantcomp)/sizeof(wantcomp[0]); i++) {
1970 if (wantcomp[i]) {
1971 if (mhlcomp[i]) {
1972 struct comp *c;
1973 for (c = mhlcomp[i]; c->c_next != NULL; c = c->c_next)
1974 ;
1975 c->c_next = wantcomp[i];
1976 } else
1977 mhlcomp[i] = wantcomp[i];
1978 }
1979 }
1980
1981 return 0;
1982 }
1983
1984 /*
1985 * Check to see if we are interested in a component. If we are, save
1986 * the text.
1987 */
1988
1989 static int
1990 checkcomp(char *name, char *buf)
1991 {
1992 int found = 0, i;
1993 struct comp *c;
1994 int bucket = CHASH(name);
1995 char *cp;
1996
1997 if ((c = mhlcomp[bucket])) {
1998 do {
1999 if (mh_strcasecmp(name, c->c_name) == 0) {
2000 found++;
2001 if (! c->c_text) {
2002 c->c_text = strdup(buf);
2003 } else {
2004 i = strlen(cp = c->c_text) - 1;
2005 if (cp[i] == '\n') {
2006 if (c->c_type & CT_ADDR) {
2007 cp[i] = '\0';
2008 cp = add (",\n\t", cp);
2009 } else {
2010 cp = add ("\t", cp);
2011 }
2012 }
2013 c->c_text = add (buf, cp);
2014 }
2015 }
2016 } while ((c = c->c_next));
2017 }
2018
2019 return found ? bucket : -1;
2020 }
2021
2022 /*
2023 * Add text to an existing component
2024 */
2025
2026 static void
2027 addcomp(int bucket, char *name, char *buf)
2028 {
2029 struct comp *c;
2030
2031 if (bucket != -1) {
2032 c = mhlcomp[bucket];
2033 do {
2034 if (mh_strcasecmp(name, c->c_name) == 0)
2035 c->c_text = add (buf, c->c_text);
2036 } while ((c = c->c_next));
2037 }
2038 }
2039
2040 /*
2041 * Free up saved component structures
2042 */
2043
2044 static void
2045 freecomps(void)
2046 {
2047 struct comp *c1, *c2;
2048 unsigned int i;
2049
2050 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2051 if ((c1 = mhlcomp[i]))
2052 for (; c1; c1 = c2) {
2053 c2 = c1->c_next;
2054 if (c1->c_text)
2055 free(c1->c_text);
2056 free(c1);
2057 }
2058 }
2059 }
2060
2061 /*
2062 * Just free up the component text.
2063 */
2064
2065 static void
2066 freecomptext(void)
2067 {
2068 struct comp *c1;
2069 unsigned int i;
2070
2071 for (i = 0; i < sizeof(mhlcomp)/sizeof(mhlcomp[0]); i++) {
2072 if ((c1 = mhlcomp[i]))
2073 for (; c1; c1 = c1->c_next) {
2074 if (c1->c_text) {
2075 free(c1->c_text);
2076 c1->c_text = NULL;
2077 }
2078 }
2079 }
2080 }
2081
2082 /*
2083 * Filter the body of a message through a specified format program
2084 */
2085
2086 static void
2087 filterbody (struct mcomp *c1, char *buf, int bufsz, int state, FILE *fp)
2088 {
2089 struct mcomp holder;
2090 char name[NAMESZ];
2091 int fdinput[2], fdoutput[2], waitstat;
2092 ssize_t cc;
2093 pid_t writerpid, filterpid;
2094
2095 /*
2096 * Create pipes so we can communicate with our filter process.
2097 */
2098
2099 if (pipe(fdinput) < 0) {
2100 adios(NULL, "Unable to create input pipe");
2101 }
2102
2103 if (pipe(fdoutput) < 0) {
2104 adios(NULL, "Unable to create output pipe");
2105 }
2106
2107 /*
2108 * Here's what we're doing to do.
2109 *
2110 * - Fork ourselves and start writing data to the write side of the
2111 * input pipe (fdinput[1]).
2112 *
2113 * - Fork and exec our filter program. We set the standard input of
2114 * our filter program to be the read side of our input pipe (fdinput[0]).
2115 * Standard output is set to the write side of our output pipe
2116 * (fdoutput[1]).
2117 *
2118 * - We read from the read side of the output pipe (fdoutput[0]).
2119 *
2120 * We're forking because that's the simplest way to prevent any deadlocks.
2121 * (without doing something like switching to non-blocking I/O and using
2122 * select or poll, and I'm not interested in doing that).
2123 */
2124
2125 switch (writerpid = fork()) {
2126 case 0:
2127 /*
2128 * Our child process - just write to the filter input (fdinput[1]).
2129 * Close all other descriptors that we don't need.
2130 */
2131
2132 close(fdinput[0]);
2133 close(fdoutput[0]);
2134 close(fdoutput[1]);
2135
2136 /*
2137 * Call m_getfld() until we're no longer in the BODY state
2138 */
2139
2140 while (state == BODY) {
2141 write(fdinput[1], buf, strlen(buf));
2142 state = m_getfld(state, name, buf, bufsz, fp);
2143 }
2144
2145 /*
2146 * We should be done; time to exit.
2147 */
2148
2149 close(fdinput[1]);
2150 /*
2151 * Make sure we call _exit(), otherwise we may flush out the stdio
2152 * buffers that we have duplicated from the parent.
2153 */
2154 _exit(0);
2155 break;
2156 case -1:
2157 adios(NULL, "Unable to fork for filter writer process");
2158 break;
2159 }
2160
2161 /*
2162 * Fork and exec() our filter program, after redirecting standard in
2163 * and standard out appropriately.
2164 */
2165
2166 switch (filterpid = fork()) {
2167 char **args;
2168 struct arglist *a;
2169 int i, dat[5], s;
2170
2171 case 0:
2172 /*
2173 * Allocate an argument array for us
2174 */
2175
2176 args = (char **) mh_xmalloc((c1->c_nargs + 2) * sizeof(char *));
2177 args[0] = formatproc;
2178 args[c1->c_nargs + 1] = NULL;
2179 dat[0] = 0;
2180 dat[1] = 0;
2181 dat[2] = 0;
2182 dat[3] = BUFSIZ;
2183 dat[4] = 0;
2184
2185 /*
2186 * Pull out each argument and scan them.
2187 */
2188
2189 for (a = c1->c_f_args, i = 1; a != NULL; a = a->a_next, i++) {
2190 args[i] = mh_xmalloc(BUFSIZ);
2191 fmt_scan(a->a_fmt, args[i], BUFSIZ, dat);
2192 /*
2193 * fmt_scan likes to put a trailing newline at the end of the
2194 * format string. If we have one, get rid of it.
2195 */
2196 s = strlen(args[i]);
2197 if (args[i][s - 1] == '\n')
2198 args[i][s - 1] = '\0';
2199 }
2200
2201 if (dup2(fdinput[0], STDIN_FILENO) < 0) {
2202 adios("formatproc", "Unable to dup2() standard input");
2203 }
2204 if (dup2(fdoutput[1], STDOUT_FILENO) < 0) {
2205 adios("formatproc", "Unable to dup2() standard output");
2206 }
2207
2208 /*
2209 * Close everything (especially the old input and output
2210 * descriptors, since they've been dup'd to stdin and stdout),
2211 * and exec the formatproc.
2212 */
2213
2214 close(fdinput[0]);
2215 close(fdinput[1]);
2216 close(fdoutput[0]);
2217 close(fdoutput[1]);
2218
2219 execvp(formatproc, args);
2220
2221 adios(formatproc, "Unable to execute filter");
2222
2223 break;
2224
2225 case -1:
2226 adios(NULL, "Unable to fork format program");
2227 }
2228
2229 /*
2230 * Close everything except our reader (fdoutput[0]);
2231 */
2232
2233 close(fdinput[0]);
2234 close(fdinput[1]);
2235 close(fdoutput[1]);
2236
2237 /*
2238 * As we read in this data, send it to putcomp
2239 */
2240
2241 holder.c_text = buf;
2242
2243 while ((cc = read(fdoutput[0], buf, bufsz - 1)) > 0) {
2244 buf[cc] = '\0';
2245 putcomp(c1, &holder, BODYCOMP);
2246 }
2247
2248 if (cc < 0) {
2249 adios(NULL, "reading from formatproc");
2250 }
2251
2252 /*
2253 * See if we got any errors along the way. I'm a little leery of calling
2254 * waitpid() without WNOHANG, but it seems to be the most correct solution.
2255 */
2256
2257 if (waitpid(filterpid, &waitstat, 0) < 0) {
2258 if (errno != ECHILD) {
2259 adios("filterproc", "Unable to determine status");
2260 }
2261 } else {
2262 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2263 pidstatus(waitstat, stderr, "filterproc");
2264 }
2265 }
2266
2267 if (waitpid(writerpid, &waitstat, 0) < 0) {
2268 if (errno != ECHILD) {
2269 adios("writer process", "Unable to determine status");
2270 done(1);
2271 }
2272 } else {
2273 if (! (WIFEXITED(waitstat) && WEXITSTATUS(waitstat) == 0)) {
2274 pidstatus(waitstat, stderr, "writer process");
2275 done(1);
2276 }
2277 }
2278
2279 close(fdoutput[0]);
2280 }