]> diplodocus.org Git - nmh/blob - uip/mhlsbr.c
Fix stupid accidental dependence on a bash quirk in previous
[nmh] / uip / mhlsbr.c
1
2 /*
3 * mhlsbr.c -- main routines for nmh message lister
4 *
5 * $Id$
6 *
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
10 */
11
12 #include <h/mh.h>
13 #include <h/signals.h>
14 #include <h/addrsbr.h>
15 #include <h/fmt_scan.h>
16 #include <h/tws.h>
17 #include <setjmp.h>
18 #include <signal.h>
19
20 /*
21 * MAJOR BUG:
22 * for a component containing addresses, ADDRFMT, if COMPRESS is also
23 * set, then addresses get split wrong (not at the spaces between commas).
24 * To fix this correctly, putstr() should know about "atomic" strings that
25 * must NOT be broken across lines. That's too difficult for right now
26 * (it turns out that there are a number of degernate cases), so in
27 * oneline(), instead of
28 *
29 * (*onelp == '\n' && !onelp[1])
30 *
31 * being a terminating condition,
32 *
33 * (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT)))
34 *
35 * is used instead. This cuts the line prematurely, and gives us a much
36 * better chance of getting things right.
37 */
38
39 #define ONECOMP 0
40 #define TWOCOMP 1
41 #define BODYCOMP 2
42
43 #define QUOTE '\\'
44
45 static struct swit mhlswitches[] = {
46 #define BELLSW 0
47 { "bell", 0 },
48 #define NBELLSW 1
49 { "nobell", 0 },
50 #define CLRSW 2
51 { "clear", 0 },
52 #define NCLRSW 3
53 { "noclear", 0 },
54 #define FACESW 4
55 { "faceproc program", 0 },
56 #define NFACESW 5
57 { "nofaceproc", 0 },
58 #define FOLDSW 6
59 { "folder +folder", 0 },
60 #define FORMSW 7
61 { "form formfile", 0 },
62 #define PROGSW 8
63 { "moreproc program", 0 },
64 #define NPROGSW 9
65 { "nomoreproc", 0 },
66 #define LENSW 10
67 { "length lines", 0 },
68 #define WIDTHSW 11
69 { "width columns", 0 },
70 #define SLEEPSW 12
71 { "sleep seconds", 0 },
72 #define BITSTUFFSW 13
73 { "dashstuffing", -12 }, /* interface from forw */
74 #define NBITSTUFFSW 14
75 { "nodashstuffing", -14 }, /* interface from forw */
76 #define VERSIONSW 15
77 { "version", 0 },
78 #define HELPSW 16
79 { "help", 0 },
80 #define FORW1SW 17
81 { "forward", -7 }, /* interface from forw */
82 #define FORW2SW 18
83 { "forwall", -7 }, /* interface from forw */
84 #define DGSTSW 19
85 { "digest list", -6 },
86 #define VOLUMSW 20
87 { "volume number", -6 },
88 #define ISSUESW 21
89 { "issue number", -5 },
90 #define NBODYSW 22
91 { "nobody", -6 },
92 { NULL, 0 }
93 };
94
95 #define NOCOMPONENT 0x000001 /* don't show component name */
96 #define UPPERCASE 0x000002 /* display in all upper case */
97 #define CENTER 0x000004 /* center line */
98 #define CLEARTEXT 0x000008 /* cleartext */
99 #define EXTRA 0x000010 /* an "extra" component */
100 #define HDROUTPUT 0x000020 /* already output */
101 #define CLEARSCR 0x000040 /* clear screen */
102 #define LEFTADJUST 0x000080 /* left justify multiple lines */
103 #define COMPRESS 0x000100 /* compress text */
104 #define ADDRFMT 0x000200 /* contains addresses */
105 #define BELL 0x000400 /* sound bell at EOP */
106 #define DATEFMT 0x000800 /* contains dates */
107 #define FORMAT 0x001000 /* parse address/date/RFC-2047 field */
108 #define INIT 0x002000 /* initialize component */
109 #define FACEFMT 0x004000 /* contains face */
110 #define FACEDFLT 0x008000 /* default for face */
111 #define SPLIT 0x010000 /* split headers (don't concatenate) */
112 #define NONEWLINE 0x020000 /* don't write trailing newline */
113 #define LBITS "\020\01NOCOMPONENT\02UPPERCASE\03CENTER\04CLEARTEXT\05EXTRA\06HDROUTPUT\07CLEARSCR\010LEFTADJUST\011COMPRESS\012ADDRFMT\013BELL\014DATEFMT\015FORMAT\016INIT\017FACEFMT\020FACEDFLT\021SPLIT\022NONEWLINE"
114 #define GFLAGS (NOCOMPONENT | UPPERCASE | CENTER | LEFTADJUST | COMPRESS | SPLIT)
115
116 struct mcomp {
117 char *c_name; /* component name */
118 char *c_text; /* component text */
119 char *c_ovtxt; /* text overflow indicator */
120 char *c_nfs; /* iff FORMAT */
121 struct format *c_fmt; /* .. */
122 char *c_face; /* face designator */
123 int c_offset; /* left margin indentation */
124 int c_ovoff; /* overflow indentation */
125 int c_width; /* width of field */
126 int c_cwidth; /* width of component */
127 int c_length; /* length in lines */
128 long c_flags;
129 struct mcomp *c_next;
130 };
131
132 static struct mcomp *msghd = NULL;
133 static struct mcomp *msgtl = NULL;
134 static struct mcomp *fmthd = NULL;
135 static struct mcomp *fmttl = NULL;
136
137 static struct mcomp global = {
138 NULL, NULL, "", NULL, NULL, 0, -1, 80, -1, 40, BELL, 0
139 };
140
141 static struct mcomp holder = {
142 NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, NOCOMPONENT, 0
143 };
144
145 struct pair {
146 char *p_name;
147 long p_flags;
148 };
149
150 static struct pair pairs[] = {
151 { "Date", DATEFMT },
152 { "From", ADDRFMT|FACEDFLT },
153 { "Sender", ADDRFMT },
154 { "Reply-To", ADDRFMT },
155 { "To", ADDRFMT },
156 { "cc", ADDRFMT },
157 { "Bcc", ADDRFMT },
158 { "Resent-Date", DATEFMT },
159 { "Resent-From", ADDRFMT },
160 { "Resent-Sender", ADDRFMT },
161 { "Resent-Reply-To", ADDRFMT },
162 { "Resent-To", ADDRFMT },
163 { "Resent-cc", ADDRFMT },
164 { "Resent-Bcc", ADDRFMT },
165 { "Face", FACEFMT },
166 { NULL, 0 }
167 };
168
169 struct triple {
170 char *t_name;
171 long t_on;
172 long t_off;
173 };
174
175 static struct triple triples[] = {
176 { "nocomponent", NOCOMPONENT, 0 },
177 { "uppercase", UPPERCASE, 0 },
178 { "nouppercase", 0, UPPERCASE },
179 { "center", CENTER, 0 },
180 { "nocenter", 0, CENTER },
181 { "clearscreen", CLEARSCR, 0 },
182 { "noclearscreen", 0, CLEARSCR },
183 { "noclear", 0, CLEARSCR },
184 { "leftadjust", LEFTADJUST, 0 },
185 { "noleftadjust", 0, LEFTADJUST },
186 { "compress", COMPRESS, 0 },
187 { "nocompress", 0, COMPRESS },
188 { "split", SPLIT, 0 },
189 { "nosplit", 0, SPLIT },
190 { "addrfield", ADDRFMT, DATEFMT },
191 { "bell", BELL, 0 },
192 { "nobell", 0, BELL },
193 { "datefield", DATEFMT, ADDRFMT },
194 { "newline", 0, NONEWLINE },
195 { "nonewline", NONEWLINE, 0 },
196 { NULL, 0, 0 }
197 };
198
199
200 static int bellflg = 0;
201 static int clearflg = 0;
202 static int dashstuff = 0;
203 static int dobody = 1;
204 static int forwflg = 0;
205 static int forwall = 0;
206
207 static int sleepsw = NOTOK;
208
209 static char *digest = NULL;
210 static int volume = 0;
211 static int issue = 0;
212
213 static int exitstat = 0;
214 static int mhldebug = 0;
215
216 #define PITTY (-1)
217 #define NOTTY 0
218 #define ISTTY 1
219 static int ontty = NOTTY;
220
221 static int row;
222 static int column;
223
224 static int lm;
225 static int llim;
226 static int ovoff;
227 static int term;
228 static int wid;
229
230 static char *ovtxt;
231
232 static char *onelp;
233
234 static char *parptr;
235
236 static int num_ignores = 0;
237 static char *ignores[MAXARGS];
238
239 static jmp_buf env;
240 static jmp_buf mhlenv;
241
242 static char delim3[] = /* from forw.c */
243 "\n----------------------------------------------------------------------\n\n";
244 static char delim4[] = "\n------------------------------\n\n";
245
246 static FILE *(*mhl_action) () = (FILE *(*) ()) 0;
247
248
249 /*
250 * Redefine a couple of functions.
251 * These are undefined later in the code.
252 */
253 #define adios mhladios
254 #define done mhldone
255
256 /*
257 * prototypes
258 */
259 static void mhl_format (char *, int, int);
260 static int evalvar (struct mcomp *);
261 static int ptoi (char *, int *);
262 static int ptos (char *, char **);
263 static char *parse (void);
264 static void process (char *, char *, int, int);
265 static void mhlfile (FILE *, char *, int, int);
266 static int mcomp_flags (char *);
267 static char *mcomp_add (long, char *, char *);
268 static void mcomp_format (struct mcomp *, struct mcomp *);
269 static struct mcomp *add_queue (struct mcomp **, struct mcomp **, char *, char *, int);
270 static void free_queue (struct mcomp **, struct mcomp **);
271 static void putcomp (struct mcomp *, struct mcomp *, int);
272 static char *oneline (char *, long);
273 static void putstr (char *);
274 static void putch (char);
275 static RETSIGTYPE intrser (int);
276 static RETSIGTYPE pipeser (int);
277 static RETSIGTYPE quitser (int);
278 static void face_format (struct mcomp *);
279 static int doface (struct mcomp *);
280 static void mhladios (char *, char *, ...);
281 static void mhldone (int);
282 static void m_popen (char *);
283
284 int mhl (int, char **);
285 int mhlsbr (int, char **, FILE *(*)());
286 void m_pclose (void);
287
288 void clear_screen (void); /* from termsbr.c */
289 int SOprintf (char *, ...); /* from termsbr.c */
290 int sc_width (void); /* from termsbr.c */
291 int sc_length (void); /* from termsbr.c */
292 int sc_hardcopy (void); /* from termsbr.c */
293 struct hostent *gethostbystring ();
294
295
296 int
297 mhl (int argc, char **argv)
298 {
299 int length = 0, nomore = 0;
300 int i, width = 0, vecp = 0;
301 char *cp, *folder = NULL, *form = NULL;
302 char buf[BUFSIZ], *files[MAXARGS];
303 char **argp, **arguments;
304
305 invo_name = r1bindex (argv[0], '/');
306
307 /* read user profile/context */
308 context_read();
309
310 arguments = getarguments (invo_name, argc, argv, 1);
311 argp = arguments;
312
313 if ((cp = getenv ("MHLDEBUG")) && *cp)
314 mhldebug++;
315
316 if ((cp = getenv ("FACEPROC")))
317 faceproc = cp;
318
319 while ((cp = *argp++)) {
320 if (*cp == '-') {
321 switch (smatch (++cp, mhlswitches)) {
322 case AMBIGSW:
323 ambigsw (cp, mhlswitches);
324 done (1);
325 case UNKWNSW:
326 adios (NULL, "-%s unknown\n", cp);
327
328 case HELPSW:
329 snprintf (buf, sizeof(buf), "%s [switches] [files ...]", invo_name);
330 print_help (buf, mhlswitches, 1);
331 done (1);
332 case VERSIONSW:
333 print_version(invo_name);
334 done (1);
335
336 case BELLSW:
337 bellflg = 1;
338 continue;
339 case NBELLSW:
340 bellflg = -1;
341 continue;
342
343 case CLRSW:
344 clearflg = 1;
345 continue;
346 case NCLRSW:
347 clearflg = -1;
348 continue;
349
350 case FOLDSW:
351 if (!(folder = *argp++) || *folder == '-')
352 adios (NULL, "missing argument to %s", argp[-2]);
353 continue;
354 case FORMSW:
355 if (!(form = *argp++) || *form == '-')
356 adios (NULL, "missing argument to %s", argp[-2]);
357 continue;
358
359 case FACESW:
360 if (!(faceproc = *argp++) || *faceproc == '-')
361 adios (NULL, "missing argument to %s", argp[-2]);
362 continue;
363 case NFACESW:
364 faceproc = NULL;
365 continue;
366 case SLEEPSW:
367 if (!(cp = *argp++) || *cp == '-')
368 adios (NULL, "missing argument to %s", argp[-2]);
369 sleepsw = atoi (cp);/* ZERO ok! */
370 continue;
371
372 case PROGSW:
373 if (!(moreproc = *argp++) || *moreproc == '-')
374 adios (NULL, "missing argument to %s", argp[-2]);
375 continue;
376 case NPROGSW:
377 nomore++;
378 continue;
379
380 case LENSW:
381 if (!(cp = *argp++) || *cp == '-')
382 adios (NULL, "missing argument to %s", argp[-2]);
383 if ((length = atoi (cp)) < 1)
384 adios (NULL, "bad argument %s %s", argp[-2], cp);
385 continue;
386 case WIDTHSW:
387 if (!(cp = *argp++) || *cp == '-')
388 adios (NULL, "missing argument to %s", argp[-2]);
389 if ((width = atoi (cp)) < 1)
390 adios (NULL, "bad argument %s %s", argp[-2], cp);
391 continue;
392
393 case DGSTSW:
394 if (!(digest = *argp++) || *digest == '-')
395 adios (NULL, "missing argument to %s", argp[-2]);
396 continue;
397 case ISSUESW:
398 if (!(cp = *argp++) || *cp == '-')
399 adios (NULL, "missing argument to %s", argp[-2]);
400 if ((issue = atoi (cp)) < 1)
401 adios (NULL, "bad argument %s %s", argp[-2], cp);
402 continue;
403 case VOLUMSW:
404 if (!(cp = *argp++) || *cp == '-')
405 adios (NULL, "missing argument to %s", argp[-2]);
406 if ((volume = atoi (cp)) < 1)
407 adios (NULL, "bad argument %s %s", argp[-2], cp);
408 continue;
409
410 case FORW2SW:
411 forwall++; /* fall */
412 case FORW1SW:
413 forwflg++;
414 clearflg = -1;/* XXX */
415 continue;
416
417 case BITSTUFFSW:
418 dashstuff = 1; /* trinary logic */
419 continue;
420 case NBITSTUFFSW:
421 dashstuff = -1; /* trinary logic */
422 continue;
423
424 case NBODYSW:
425 dobody = 0;
426 continue;
427 }
428 }
429 files[vecp++] = cp;
430 }
431
432 if (!folder)
433 folder = getenv ("mhfolder");
434
435 if (isatty (fileno (stdout))) {
436 if (!nomore && !sc_hardcopy() && moreproc && *moreproc != '\0') {
437 if (mhl_action) {
438 SIGNAL (SIGINT, SIG_IGN);
439 SIGNAL2 (SIGQUIT, quitser);
440 }
441 SIGNAL2 (SIGPIPE, pipeser);
442 m_popen (moreproc);
443 ontty = PITTY;
444 } else {
445 SIGNAL (SIGINT, SIG_IGN);
446 SIGNAL2 (SIGQUIT, quitser);
447 ontty = ISTTY;
448 }
449 } else {
450 ontty = NOTTY;
451 }
452
453 mhl_format (form ? form : mhlformat, length, width);
454
455 if (vecp == 0) {
456 process (folder, NULL, 1, vecp = 1);
457 } else {
458 for (i = 0; i < vecp; i++)
459 process (folder, files[i], i + 1, vecp);
460 }
461
462 if (forwall) {
463 if (digest) {
464 printf ("%s", delim4);
465 if (volume == 0) {
466 snprintf (buf, sizeof(buf), "End of %s Digest\n", digest);
467 } else {
468 snprintf (buf, sizeof(buf), "End of %s Digest [Volume %d Issue %d]\n",
469 digest, volume, issue);
470 }
471 i = strlen (buf);
472 for (cp = buf + i; i > 1; i--)
473 *cp++ = '*';
474 *cp++ = '\n';
475 *cp = 0;
476 printf ("%s", buf);
477 }
478 else
479 printf ("\n------- End of Forwarded Message%s\n\n",
480 vecp > 1 ? "s" : "");
481 }
482
483 fflush(stdout);
484 if(ferror(stdout)){
485 adios("output", "error writing");
486 }
487
488 if (clearflg > 0 && ontty == NOTTY)
489 clear_screen ();
490
491 if (ontty == PITTY)
492 m_pclose ();
493
494 return exitstat;
495 }
496
497
498 static void
499 mhl_format (char *file, int length, int width)
500 {
501 int i;
502 char *bp, *cp, **ip;
503 char *ap, buffer[BUFSIZ], name[NAMESZ];
504 struct mcomp *c1;
505 struct stat st;
506 FILE *fp;
507 static dev_t dev = 0;
508 static ino_t ino = 0;
509 static time_t mtime = 0;
510
511 if (fmthd != NULL) {
512 if (stat (etcpath (file), &st) != NOTOK
513 && mtime == st.st_mtime
514 && dev == st.st_dev
515 && ino == st.st_ino)
516 goto out;
517 else
518 free_queue (&fmthd, &fmttl);
519 }
520
521 if ((fp = fopen (etcpath (file), "r")) == NULL)
522 adios (file, "unable to open format file");
523
524 if (fstat (fileno (fp), &st) != NOTOK) {
525 mtime = st.st_mtime;
526 dev = st.st_dev;
527 ino = st.st_ino;
528 }
529
530 global.c_ovtxt = global.c_nfs = NULL;
531 global.c_fmt = NULL;
532 global.c_offset = 0;
533 global.c_ovoff = -1;
534 if ((i = sc_width ()) > 5)
535 global.c_width = i;
536 global.c_cwidth = -1;
537 if ((i = sc_length ()) > 5)
538 global.c_length = i - 1;
539 global.c_flags = BELL; /* BELL is default */
540 *(ip = ignores) = NULL;
541
542 while (vfgets (fp, &ap) == OK) {
543 bp = ap;
544 if (*bp == ';')
545 continue;
546
547 if ((cp = strchr(bp, '\n')))
548 *cp = 0;
549
550 if (*bp == ':') {
551 c1 = add_queue (&fmthd, &fmttl, NULL, bp + 1, CLEARTEXT);
552 continue;
553 }
554
555 parptr = bp;
556 strncpy (name, parse(), sizeof(name));
557 switch (*parptr) {
558 case '\0':
559 case ',':
560 case '=':
561 /*
562 * Split this list of fields to ignore, and copy
563 * it to the end of the current "ignores" list.
564 */
565 if (!strcasecmp (name, "ignores")) {
566 char **tmparray, **p;
567 int n = 0;
568
569 /* split the fields */
570 tmparray = brkstring (getcpy (++parptr), ",", NULL);
571
572 /* count number of fields split */
573 p = tmparray;
574 while (*p++)
575 n++;
576
577 /* copy pointers to split fields to ignores array */
578 ip = copyip (tmparray, ip, MAXARGS - num_ignores);
579 num_ignores += n;
580 continue;
581 }
582 parptr = bp;
583 while (*parptr) {
584 if (evalvar (&global))
585 adios (NULL, "format file syntax error: %s", bp);
586 if (*parptr)
587 parptr++;
588 }
589 continue;
590
591 case ':':
592 c1 = add_queue (&fmthd, &fmttl, name, NULL, INIT);
593 while (*parptr == ':' || *parptr == ',') {
594 parptr++;
595 if (evalvar (c1))
596 adios (NULL, "format file syntax error: %s", bp);
597 }
598 if (!c1->c_nfs && global.c_nfs) {
599 if (c1->c_flags & DATEFMT) {
600 if (global.c_flags & DATEFMT)
601 c1->c_nfs = getcpy (global.c_nfs);
602 }
603 else
604 if (c1->c_flags & ADDRFMT) {
605 if (global.c_flags & ADDRFMT)
606 c1->c_nfs = getcpy (global.c_nfs);
607 }
608 }
609 continue;
610
611 default:
612 adios (NULL, "format file syntax error: %s", bp);
613 }
614 }
615 fclose (fp);
616
617 if (mhldebug) {
618 for (c1 = fmthd; c1; c1 = c1->c_next) {
619 fprintf (stderr, "c1: name=\"%s\" text=\"%s\" ovtxt=\"%s\"\n",
620 c1->c_name, c1->c_text, c1->c_ovtxt);
621 fprintf (stderr, "\tnfs=0x%x fmt=0x%x\n",
622 (unsigned int) c1->c_nfs, (unsigned int) c1->c_fmt);
623 fprintf (stderr, "\toffset=%d ovoff=%d width=%d cwidth=%d length=%d\n",
624 c1->c_offset, c1->c_ovoff, c1->c_width,
625 c1->c_cwidth, c1->c_length);
626 fprintf (stderr, "\tflags=%s\n",
627 snprintb (buffer, sizeof(buffer), (unsigned) c1->c_flags, LBITS));
628 }
629 }
630
631 out:
632 if (clearflg == 1) {
633 global.c_flags |= CLEARSCR;
634 } else {
635 if (clearflg == -1)
636 global.c_flags &= ~CLEARSCR;
637 }
638
639 switch (bellflg) { /* command line may override format file */
640 case 1:
641 global.c_flags |= BELL;
642 break;
643 case -1:
644 global.c_flags &= ~BELL;
645 break;
646 }
647
648 if (length)
649 global.c_length = length;
650 if (width)
651 global.c_width = width;
652 if (global.c_length < 5)
653 global.c_length = 10000;
654 if (global.c_width < 5)
655 global.c_width = 10000;
656 }
657
658
659 static int
660 evalvar (struct mcomp *c1)
661 {
662 char *cp, name[NAMESZ];
663 struct triple *ap;
664
665 if (!*parptr)
666 return 0;
667 strncpy (name, parse(), sizeof(name));
668
669 if (!strcasecmp (name, "component")) {
670 if (ptos (name, &c1->c_text))
671 return 1;
672 c1->c_flags &= ~NOCOMPONENT;
673 return 0;
674 }
675
676 if (!strcasecmp (name, "overflowtext"))
677 return ptos (name, &c1->c_ovtxt);
678
679 if (!strcasecmp (name, "formatfield")) {
680 char *nfs;
681
682 if (ptos (name, &cp))
683 return 1;
684 nfs = new_fs (NULL, NULL, cp);
685 c1->c_nfs = getcpy (nfs);
686 c1->c_flags |= FORMAT;
687 return 0;
688 }
689
690 if (!strcasecmp (name, "decode")) {
691 char *nfs;
692
693 nfs = new_fs (NULL, NULL, "%(decode{text})");
694 c1->c_nfs = getcpy (nfs);
695 c1->c_flags |= FORMAT;
696 return 0;
697 }
698
699 if (!strcasecmp (name, "offset"))
700 return ptoi (name, &c1->c_offset);
701 if (!strcasecmp (name, "overflowoffset"))
702 return ptoi (name, &c1->c_ovoff);
703 if (!strcasecmp (name, "width"))
704 return ptoi (name, &c1->c_width);
705 if (!strcasecmp (name, "compwidth"))
706 return ptoi (name, &c1->c_cwidth);
707 if (!strcasecmp (name, "length"))
708 return ptoi (name, &c1->c_length);
709 if (!strcasecmp (name, "nodashstuffing"))
710 return (dashstuff = -1);
711
712 for (ap = triples; ap->t_name; ap++)
713 if (!strcasecmp (ap->t_name, name)) {
714 c1->c_flags |= ap->t_on;
715 c1->c_flags &= ~ap->t_off;
716 return 0;
717 }
718
719 return 1;
720 }
721
722
723 static int
724 ptoi (char *name, int *i)
725 {
726 char *cp;
727
728 if (*parptr++ != '=' || !*(cp = parse ())) {
729 advise (NULL, "missing argument to variable %s", name);
730 return 1;
731 }
732
733 *i = atoi (cp);
734 return 0;
735 }
736
737
738 static int
739 ptos (char *name, char **s)
740 {
741 char c, *cp;
742
743 if (*parptr++ != '=') {
744 advise (NULL, "missing argument to variable %s", name);
745 return 1;
746 }
747
748 if (*parptr != '"') {
749 for (cp = parptr;
750 *parptr && *parptr != ':' && *parptr != ',';
751 parptr++)
752 continue;
753 } else {
754 for (cp = ++parptr; *parptr && *parptr != '"'; parptr++)
755 if (*parptr == QUOTE)
756 if (!*++parptr)
757 parptr--;
758 }
759 c = *parptr;
760 *parptr = 0;
761 *s = getcpy (cp);
762 if ((*parptr = c) == '"')
763 parptr++;
764 return 0;
765 }
766
767
768 static char *
769 parse (void)
770 {
771 int c;
772 char *cp;
773 static char result[NAMESZ];
774
775 for (cp = result; *parptr && (cp - result < NAMESZ); parptr++) {
776 c = *parptr;
777 if (isalnum (c)
778 || c == '.'
779 || c == '-'
780 || c == '_'
781 || c =='['
782 || c == ']')
783 *cp++ = c;
784 else
785 break;
786 }
787 *cp = '\0';
788
789 return result;
790 }
791
792
793 static void
794 process (char *folder, char *fname, int ofilen, int ofilec)
795 {
796 char *cp;
797 FILE *fp;
798 struct mcomp *c1;
799
800 switch (setjmp (env)) {
801 case OK:
802 if (fname) {
803 fp = mhl_action ? (*mhl_action) (fname) : fopen (fname, "r");
804 if (fp == NULL) {
805 advise (fname, "unable to open");
806 exitstat++;
807 return;
808 }
809 } else {
810 fname = "(stdin)";
811 fp = stdin;
812 }
813 cp = folder ? concat (folder, ":", fname, NULL) : getcpy (fname);
814 if (ontty != PITTY)
815 SIGNAL (SIGINT, intrser);
816 mhlfile (fp, cp, ofilen, ofilec); /* FALL THROUGH! */
817
818 default:
819 if (ontty != PITTY)
820 SIGNAL (SIGINT, SIG_IGN);
821 if (mhl_action == NULL && fp != stdin)
822 fclose (fp);
823 free (cp);
824 if (holder.c_text) {
825 free (holder.c_text);
826 holder.c_text = NULL;
827 }
828 free_queue (&msghd, &msgtl);
829 for (c1 = fmthd; c1; c1 = c1->c_next)
830 c1->c_flags &= ~HDROUTPUT;
831 break;
832 }
833 }
834
835
836 static void
837 mhlfile (FILE *fp, char *mname, int ofilen, int ofilec)
838 {
839 int state;
840 struct mcomp *c1, *c2, *c3;
841 char **ip, name[NAMESZ], buf[BUFSIZ];
842
843 if (forwall) {
844 if (digest)
845 printf ("%s", ofilen == 1 ? delim3 : delim4);
846 else {
847 printf ("\n-------");
848 if (ofilen == 1)
849 printf (" Forwarded Message%s", ofilec > 1 ? "s" : "");
850 else
851 printf (" Message %d", ofilen);
852 printf ("\n\n");
853 }
854 } else {
855 switch (ontty) {
856 case PITTY:
857 if (ofilec > 1) {
858 if (ofilen > 1) {
859 if ((global.c_flags & CLEARSCR))
860 clear_screen ();
861 else
862 printf ("\n\n\n");
863 }
864 printf (">>> %s\n\n", mname);
865 }
866 break;
867
868 case ISTTY:
869 strncpy (buf, "\n", sizeof(buf));
870 if (ofilec > 1) {
871 if (SOprintf ("Press <return> to list \"%s\"...", mname)) {
872 if (ofilen > 1)
873 printf ("\n\n\n");
874 printf ("Press <return> to list \"%s\"...", mname);
875 }
876 fflush (stdout);
877 buf[0] = 0;
878 read (fileno (stdout), buf, sizeof(buf));
879 }
880 if (strchr(buf, '\n')) {
881 if ((global.c_flags & CLEARSCR))
882 clear_screen ();
883 }
884 else
885 printf ("\n");
886 break;
887
888 default:
889 if (ofilec > 1) {
890 if (ofilen > 1) {
891 printf ("\n\n\n");
892 if (clearflg > 0)
893 clear_screen ();
894 }
895 printf (">>> %s\n\n", mname);
896 }
897 break;
898 }
899 }
900
901 for (state = FLD;;) {
902 switch (state = m_getfld (state, name, buf, sizeof(buf), fp)) {
903 case FLD:
904 case FLDPLUS:
905 for (ip = ignores; *ip; ip++)
906 if (!strcasecmp (name, *ip)) {
907 while (state == FLDPLUS)
908 state = m_getfld (state, name, buf, sizeof(buf), fp);
909 break;
910 }
911 if (*ip)
912 continue;
913
914 for (c2 = fmthd; c2; c2 = c2->c_next)
915 if (!strcasecmp (c2->c_name, name))
916 break;
917 c1 = NULL;
918 if (!((c3 = c2 ? c2 : &global)->c_flags & SPLIT))
919 for (c1 = msghd; c1; c1 = c1->c_next)
920 if (!strcasecmp (c1->c_name, c3->c_name)) {
921 c1->c_text =
922 mcomp_add (c1->c_flags, buf, c1->c_text);
923 break;
924 }
925 if (c1 == NULL)
926 c1 = add_queue (&msghd, &msgtl, name, buf, 0);
927 while (state == FLDPLUS) {
928 state = m_getfld (state, name, buf, sizeof(buf), fp);
929 c1->c_text = add (buf, c1->c_text);
930 }
931 if (c2 == NULL)
932 c1->c_flags |= EXTRA;
933 continue;
934
935 case BODY:
936 case FILEEOF:
937 row = column = 0;
938 for (c1 = fmthd; c1; c1 = c1->c_next) {
939 if (c1->c_flags & CLEARTEXT) {
940 putcomp (c1, c1, ONECOMP);
941 continue;
942 }
943 if (!strcasecmp (c1->c_name, "messagename")) {
944 holder.c_text = concat ("(Message ", mname, ")\n",
945 NULL);
946 putcomp (c1, &holder, ONECOMP);
947 free (holder.c_text);
948 holder.c_text = NULL;
949 continue;
950 }
951 if (!strcasecmp (c1->c_name, "extras")) {
952 for (c2 = msghd; c2; c2 = c2->c_next)
953 if (c2->c_flags & EXTRA)
954 putcomp (c1, c2, TWOCOMP);
955 continue;
956 }
957 if (dobody && !strcasecmp (c1->c_name, "body")) {
958 if ((holder.c_text = malloc (sizeof(buf))) == NULL)
959 adios (NULL, "unable to allocate buffer memory");
960 strncpy (holder.c_text, buf, sizeof(buf));
961 while (state == BODY) {
962 putcomp (c1, &holder, BODYCOMP);
963 state = m_getfld (state, name, holder.c_text,
964 sizeof(buf), fp);
965 }
966 free (holder.c_text);
967 holder.c_text = NULL;
968 continue;
969 }
970 for (c2 = msghd; c2; c2 = c2->c_next)
971 if (!strcasecmp (c2->c_name, c1->c_name)) {
972 putcomp (c1, c2, ONECOMP);
973 if (!(c1->c_flags & SPLIT))
974 break;
975 }
976 if (faceproc && c2 == NULL && (c1->c_flags & FACEFMT))
977 for (c2 = msghd; c2; c2 = c2->c_next)
978 if (c2->c_flags & FACEDFLT) {
979 if (c2->c_face == NULL)
980 face_format (c2);
981 if ((holder.c_text = c2->c_face)) {
982 putcomp (c1, &holder, ONECOMP);
983 holder.c_text = NULL;
984 }
985 break;
986 }
987 }
988 return;
989
990 case LENERR:
991 case FMTERR:
992 advise (NULL, "format error in message %s", mname);
993 exitstat++;
994 return;
995
996 default:
997 adios (NULL, "getfld() returned %d", state);
998 }
999 }
1000 }
1001
1002
1003 static int
1004 mcomp_flags (char *name)
1005 {
1006 struct pair *ap;
1007
1008 for (ap = pairs; ap->p_name; ap++)
1009 if (!strcasecmp (ap->p_name, name))
1010 return (ap->p_flags);
1011
1012 return 0;
1013 }
1014
1015
1016 static char *
1017 mcomp_add (long flags, char *s1, char *s2)
1018 {
1019 char *dp;
1020
1021 if (!(flags & ADDRFMT))
1022 return add (s1, s2);
1023
1024 if (s2 && *(dp = s2 + strlen (s2) - 1) == '\n')
1025 *dp = 0;
1026
1027 return add (s1, add (",\n", s2));
1028 }
1029
1030
1031 struct pqpair {
1032 char *pq_text;
1033 char *pq_error;
1034 struct pqpair *pq_next;
1035 };
1036
1037
1038 static void
1039 mcomp_format (struct mcomp *c1, struct mcomp *c2)
1040 {
1041 int dat[5];
1042 char *ap, *cp;
1043 char buffer[BUFSIZ], error[BUFSIZ];
1044 struct comp *cptr;
1045 struct pqpair *p, *q;
1046 struct pqpair pq;
1047 struct mailname *mp;
1048
1049 ap = c2->c_text;
1050 c2->c_text = NULL;
1051 dat[0] = 0;
1052 dat[1] = 0;
1053 dat[2] = 0;
1054 dat[3] = sizeof(buffer) - 1;
1055 dat[4] = 0;
1056 fmt_compile (c1->c_nfs, &c1->c_fmt);
1057
1058 if (!(c1->c_flags & ADDRFMT)) {
1059 FINDCOMP (cptr, "text");
1060 if (cptr)
1061 cptr->c_text = ap;
1062 if ((cp = strrchr(ap, '\n'))) /* drop ending newline */
1063 if (!cp[1])
1064 *cp = 0;
1065
1066 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1067 /* Don't need to append a newline, dctime() already did */
1068 c2->c_text = getcpy (buffer);
1069
1070 free (ap);
1071 return;
1072 }
1073
1074 (q = &pq)->pq_next = NULL;
1075 while ((cp = getname (ap))) {
1076 if ((p = (struct pqpair *) calloc ((size_t) 1, sizeof(*p))) == NULL)
1077 adios (NULL, "unable to allocate pqpair memory");
1078
1079 if ((mp = getm (cp, NULL, 0, AD_NAME, error)) == NULL) {
1080 p->pq_text = getcpy (cp);
1081 p->pq_error = getcpy (error);
1082 } else {
1083 if ((c1->c_flags & FACEDFLT) && c2->c_face == NULL) {
1084 char *h, *o;
1085 if ((h = mp->m_host) == NULL)
1086 h = LocalName ();
1087 if ((o = OfficialName (h)))
1088 h = o;
1089 c2->c_face = concat ("address ", h, " ", mp->m_mbox,
1090 NULL);
1091 }
1092 p->pq_text = getcpy (mp->m_text);
1093 mnfree (mp);
1094 }
1095 q = (q->pq_next = p);
1096 }
1097
1098 for (p = pq.pq_next; p; p = q) {
1099 FINDCOMP (cptr, "text");
1100 if (cptr)
1101 cptr->c_text = p->pq_text;
1102 FINDCOMP (cptr, "error");
1103 if (cptr)
1104 cptr->c_text = p->pq_error;
1105
1106 fmt_scan (c1->c_fmt, buffer, sizeof(buffer) - 1, dat);
1107 if (*buffer) {
1108 if (c2->c_text)
1109 c2->c_text = add (",\n", c2->c_text);
1110 if (*(cp = buffer + strlen (buffer) - 1) == '\n')
1111 *cp = 0;
1112 c2->c_text = add (buffer, c2->c_text);
1113 }
1114
1115 free (p->pq_text);
1116 if (p->pq_error)
1117 free (p->pq_error);
1118 q = p->pq_next;
1119 free ((char *) p);
1120 }
1121
1122 c2->c_text = add ("\n", c2->c_text);
1123 free (ap);
1124 }
1125
1126
1127 static struct mcomp *
1128 add_queue (struct mcomp **head, struct mcomp **tail, char *name, char *text, int flags)
1129 {
1130 struct mcomp *c1;
1131
1132 if ((c1 = (struct mcomp *) calloc ((size_t) 1, sizeof(*c1))) == NULL)
1133 adios (NULL, "unable to allocate comp memory");
1134
1135 c1->c_flags = flags & ~INIT;
1136 if ((c1->c_name = name ? getcpy (name) : NULL))
1137 c1->c_flags |= mcomp_flags (c1->c_name);
1138 c1->c_text = text ? getcpy (text) : NULL;
1139 if (flags & INIT) {
1140 if (global.c_ovtxt)
1141 c1->c_ovtxt = getcpy (global.c_ovtxt);
1142 c1->c_offset = global.c_offset;
1143 c1->c_ovoff = global. c_ovoff;
1144 c1->c_width = c1->c_length = 0;
1145 c1->c_cwidth = global.c_cwidth;
1146 c1->c_flags |= global.c_flags & GFLAGS;
1147 }
1148 if (*head == NULL)
1149 *head = c1;
1150 if (*tail != NULL)
1151 (*tail)->c_next = c1;
1152 *tail = c1;
1153
1154 return c1;
1155 }
1156
1157
1158 static void
1159 free_queue (struct mcomp **head, struct mcomp **tail)
1160 {
1161 struct mcomp *c1, *c2;
1162
1163 for (c1 = *head; c1; c1 = c2) {
1164 c2 = c1->c_next;
1165 if (c1->c_name)
1166 free (c1->c_name);
1167 if (c1->c_text)
1168 free (c1->c_text);
1169 if (c1->c_ovtxt)
1170 free (c1->c_ovtxt);
1171 if (c1->c_nfs)
1172 free (c1->c_nfs);
1173 if (c1->c_fmt)
1174 free ((char *) c1->c_fmt);
1175 if (c1->c_face)
1176 free (c1->c_face);
1177 free ((char *) c1);
1178 }
1179
1180 *head = *tail = NULL;
1181 }
1182
1183
1184 static void
1185 putcomp (struct mcomp *c1, struct mcomp *c2, int flag)
1186 {
1187 int count, cchdr;
1188 char *cp;
1189
1190 cchdr = 0;
1191 lm = 0;
1192 llim = c1->c_length ? c1->c_length : -1;
1193 wid = c1->c_width ? c1->c_width : global.c_width;
1194 ovoff = (c1->c_ovoff >= 0 ? c1->c_ovoff : global.c_ovoff)
1195 + c1->c_offset;
1196 if ((ovtxt = c1->c_ovtxt ? c1->c_ovtxt : global.c_ovtxt) == NULL)
1197 ovtxt = "";
1198 if (wid < ovoff + strlen (ovtxt) + 5)
1199 adios (NULL, "component: %s width(%d) too small for overflow(%d)",
1200 c1->c_name, wid, ovoff + strlen (ovtxt) + 5);
1201 onelp = NULL;
1202
1203 if (c1->c_flags & CLEARTEXT) {
1204 putstr (c1->c_text);
1205 putstr ("\n");
1206 return;
1207 }
1208
1209 if (c1->c_flags & FACEFMT)
1210 switch (doface (c2)) {
1211 case NOTOK: /* error */
1212 case OK: /* async faceproc */
1213 return;
1214
1215 default: /* sync faceproc */
1216 break;
1217 }
1218
1219 if (c1->c_nfs && (c1->c_flags & (ADDRFMT | DATEFMT | FORMAT)))
1220 mcomp_format (c1, c2);
1221
1222 if (c1->c_flags & CENTER) {
1223 count = (c1->c_width ? c1->c_width : global.c_width)
1224 - c1->c_offset - strlen (c2->c_text);
1225 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT))
1226 count -= strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1227 lm = c1->c_offset + (count / 2);
1228 } else {
1229 if (c1->c_offset)
1230 lm = c1->c_offset;
1231 }
1232
1233 if (!(c1->c_flags & HDROUTPUT) && !(c1->c_flags & NOCOMPONENT)) {
1234 if (c1->c_flags & UPPERCASE) /* uppercase component also */
1235 for (cp = (c1->c_text ? c1->c_text : c1->c_name); *cp; cp++)
1236 if (islower (*cp))
1237 *cp = toupper (*cp);
1238 putstr (c1->c_text ? c1->c_text : c1->c_name);
1239 if (flag != BODYCOMP) {
1240 putstr (": ");
1241 if (!(c1->c_flags & SPLIT))
1242 c1->c_flags |= HDROUTPUT;
1243
1244 cchdr++;
1245 if ((count = c1->c_cwidth -
1246 strlen (c1->c_text ? c1->c_text : c1->c_name) - 2) > 0)
1247 while (count--)
1248 putstr (" ");
1249 }
1250 else
1251 c1->c_flags |= HDROUTPUT; /* for BODYCOMP */
1252 }
1253
1254 if (flag == TWOCOMP
1255 && !(c2->c_flags & HDROUTPUT)
1256 && !(c2->c_flags & NOCOMPONENT)) {
1257 if (c1->c_flags & UPPERCASE)
1258 for (cp = c2->c_name; *cp; cp++)
1259 if (islower (*cp))
1260 *cp = toupper (*cp);
1261 putstr (c2->c_name);
1262 putstr (": ");
1263 if (!(c1->c_flags & SPLIT))
1264 c2->c_flags |= HDROUTPUT;
1265
1266 cchdr++;
1267 if ((count = c1->c_cwidth - strlen (c2->c_name) - 2) > 0)
1268 while (count--)
1269 putstr (" ");
1270 }
1271 if (c1->c_flags & UPPERCASE)
1272 for (cp = c2->c_text; *cp; cp++)
1273 if (islower (*cp))
1274 *cp = toupper (*cp);
1275
1276 count = 0;
1277 if (cchdr) {
1278 if (flag == TWOCOMP)
1279 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1280 : strlen (c2->c_name) + 2;
1281 else
1282 count = (c1->c_cwidth >= 0) ? c1->c_cwidth
1283 : strlen (c1->c_text ? c1->c_text : c1->c_name) + 2;
1284 }
1285 count += c1->c_offset;
1286
1287 if ((cp = oneline (c2->c_text, c1->c_flags)))
1288 putstr(cp);
1289 if (term == '\n')
1290 putstr ("\n");
1291 while ((cp = oneline (c2->c_text, c1->c_flags))) {
1292 lm = count;
1293 if (flag == BODYCOMP
1294 && !(c1->c_flags & NOCOMPONENT))
1295 putstr (c1->c_text ? c1->c_text : c1->c_name);
1296 if (*cp)
1297 putstr (cp);
1298 if (term == '\n')
1299 putstr ("\n");
1300 }
1301 }
1302
1303
1304 static char *
1305 oneline (char *stuff, long flags)
1306 {
1307 int spc;
1308 char *cp, *ret;
1309
1310 if (onelp == NULL)
1311 onelp = stuff;
1312 if (*onelp == 0)
1313 return (onelp = NULL);
1314
1315 ret = onelp;
1316 term = 0;
1317 if (flags & COMPRESS) {
1318 for (spc = 1, cp = ret; *onelp; onelp++)
1319 if (isspace (*onelp)) {
1320 if (*onelp == '\n' && (!onelp[1] || (flags & ADDRFMT))) {
1321 term = '\n';
1322 *onelp++ = 0;
1323 break;
1324 }
1325 else
1326 if (!spc) {
1327 *cp++ = ' ';
1328 spc++;
1329 }
1330 }
1331 else {
1332 *cp++ = *onelp;
1333 spc = 0;
1334 }
1335
1336 *cp = 0;
1337 }
1338 else {
1339 while (*onelp && *onelp != '\n')
1340 onelp++;
1341 if (*onelp == '\n') {
1342 term = '\n';
1343 *onelp++ = 0;
1344 }
1345 if (flags & LEFTADJUST)
1346 while (*ret == ' ' || *ret == '\t')
1347 ret++;
1348 }
1349 if (*onelp == 0 && term == '\n' && (flags & NONEWLINE))
1350 term = 0;
1351
1352 return ret;
1353 }
1354
1355
1356 static void
1357 putstr (char *string)
1358 {
1359 if (!column && lm > 0) {
1360 while (lm > 0)
1361 if (lm >= 8) {
1362 putch ('\t');
1363 lm -= 8;
1364 }
1365 else {
1366 putch (' ');
1367 lm--;
1368 }
1369 }
1370 lm = 0;
1371 while (*string)
1372 putch (*string++);
1373 }
1374
1375
1376 static void
1377 putch (char ch)
1378 {
1379 char buf[BUFSIZ];
1380
1381 if (llim == 0)
1382 return;
1383
1384 switch (ch) {
1385 case '\n':
1386 if (llim > 0)
1387 llim--;
1388 column = 0;
1389 row++;
1390 if (ontty != ISTTY || row != global.c_length)
1391 break;
1392 if (global.c_flags & BELL)
1393 putchar ('\007');
1394 fflush (stdout);
1395 buf[0] = 0;
1396 read (fileno (stdout), buf, sizeof(buf));
1397 if (strchr(buf, '\n')) {
1398 if (global.c_flags & CLEARSCR)
1399 clear_screen ();
1400 row = 0;
1401 } else {
1402 putchar ('\n');
1403 row = global.c_length / 3;
1404 }
1405 return;
1406
1407 case '\t':
1408 column |= 07;
1409 column++;
1410 break;
1411
1412 case '\b':
1413 column--;
1414 break;
1415
1416 case '\r':
1417 column = 0;
1418 break;
1419
1420 default:
1421 /*
1422 * If we are forwarding this message, and the first
1423 * column contains a dash, then add a dash and a space.
1424 */
1425 if (column == 0 && forwflg && (dashstuff >= 0) && ch == '-') {
1426 putchar ('-');
1427 putchar (' ');
1428 }
1429 if (ch >= ' ')
1430 column++;
1431 break;
1432 }
1433
1434 if (column >= wid) {
1435 putch ('\n');
1436 if (ovoff > 0)
1437 lm = ovoff;
1438 putstr (ovtxt ? ovtxt : "");
1439 putch (ch);
1440 return;
1441 }
1442
1443 putchar (ch);
1444 }
1445
1446
1447 static RETSIGTYPE
1448 intrser (int i)
1449 {
1450 #ifndef RELIABLE_SIGNALS
1451 SIGNAL (SIGINT, intrser);
1452 #endif
1453
1454 discard (stdout);
1455 putchar ('\n');
1456 longjmp (env, DONE);
1457 }
1458
1459
1460 static RETSIGTYPE
1461 pipeser (int i)
1462 {
1463 #ifndef RELIABLE_SIGNALS
1464 SIGNAL (SIGPIPE, pipeser);
1465 #endif
1466
1467 done (NOTOK);
1468 }
1469
1470
1471 static RETSIGTYPE
1472 quitser (int i)
1473 {
1474 #ifndef RELIABLE_SIGNALS
1475 SIGNAL (SIGQUIT, quitser);
1476 #endif
1477
1478 putchar ('\n');
1479 fflush (stdout);
1480 done (NOTOK);
1481 }
1482
1483
1484 static void
1485 face_format (struct mcomp *c1)
1486 {
1487 char *cp;
1488 struct mailname *mp;
1489
1490 if ((cp = c1->c_text) == NULL)
1491 return;
1492
1493 if ((cp = getname (cp))) {
1494 if ((mp = getm (cp, NULL, 0, AD_NAME, NULL))) {
1495 char *h, *o;
1496 if ((h = mp->m_host) == NULL)
1497 h = LocalName ();
1498 if ((o = OfficialName (h)))
1499 h = o;
1500 c1->c_face = concat ("address ", h, " ", mp->m_mbox, NULL);
1501 }
1502
1503 while ((cp = getname (cp)))
1504 continue;
1505 }
1506 }
1507
1508
1509 /*
1510 * faceproc is two elements defining the image agent's location:
1511 * Internet host
1512 * UDP port
1513 */
1514
1515 #include <sys/socket.h>
1516 #include <netinet/in.h>
1517 #include <netdb.h>
1518
1519 #ifdef HAVE_ARPA_INET_H
1520 # include <arpa/inet.h>
1521 #endif
1522
1523 static int
1524 doface (struct mcomp *c1)
1525 {
1526 int result, sd;
1527 struct sockaddr_in in_socket;
1528 struct sockaddr_in *isock = &in_socket;
1529 static int inited = OK;
1530 static int addrlen;
1531 static struct in_addr addr;
1532 static unsigned short portno;
1533
1534 if (inited == OK) {
1535 char *cp;
1536 char **ap = brkstring (cp = getcpy (faceproc), " ", "\n");
1537 struct hostent *hp;
1538
1539 if (ap[0] == NULL || ap[1] == NULL) {
1540 bad_faceproc: ;
1541 free (cp);
1542 return (inited = NOTOK);
1543 }
1544
1545 if (!(hp = gethostbystring (ap[0])))
1546 goto bad_faceproc;
1547 memcpy((char *) &addr, hp->h_addr, addrlen = hp->h_length);
1548
1549 portno = htons ((unsigned short) atoi (ap[1]));
1550 free (cp);
1551
1552 inited = DONE;
1553 }
1554 if (inited == NOTOK)
1555 return NOTOK;
1556
1557 isock->sin_family = AF_INET;
1558 isock->sin_port = portno;
1559 memcpy((char *) &isock->sin_addr, (char *) &addr, addrlen);
1560
1561 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) == NOTOK)
1562 return NOTOK;
1563
1564 result = sendto (sd, c1->c_text, strlen (c1->c_text), 0,
1565 (struct sockaddr *) isock, sizeof(*isock));
1566
1567 close (sd);
1568
1569 return (result != NOTOK ? OK : NOTOK);
1570 }
1571
1572 /*
1573 * COMMENTED OUT
1574 * This version doesn't use sockets
1575 */
1576 #if 0
1577
1578 static int
1579 doface (struct mcomp *c1)
1580 {
1581 int i, len, vecp;
1582 pid_t child_id;
1583 int result, pdi[2], pdo[2];
1584 char *bp, *cp;
1585 char buffer[BUFSIZ], *vec[10];
1586
1587 if (pipe (pdi) == NOTOK)
1588 return NOTOK;
1589 if (pipe (pdo) == NOTOK) {
1590 close (pdi[0]);
1591 close (pdi[1]);
1592 return NOTOK;
1593 }
1594
1595 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
1596 sleep (5);
1597
1598 switch (child_id) {
1599 case NOTOK:
1600 /* oops... fork error */
1601 return NOTOK;
1602
1603 case OK:
1604 /* child process */
1605 SIGNAL (SIGINT, SIG_IGN);
1606 SIGNAL (SIGQUIT, SIG_IGN);
1607 if (pdi[0] != fileno (stdin)) {
1608 dup2 (pdi[0], fileno (stdin));
1609 close (pdi[0]);
1610 }
1611 close (pdi[1]);
1612 close (pdo[0]);
1613 if (pdo[1] != fileno (stdout)) {
1614 dup2 (pdo[1], fileno (stdout));
1615 close (pdo[1]);
1616 }
1617 vecp = 0;
1618 vec[vecp++] = r1bindex (faceproc, '/');
1619 vec[vecp++] = "-e";
1620 if (sleepsw != NOTOK) {
1621 vec[vecp++] = "-s";
1622 snprintf (buffer, sizeof(buffer), "%d", sleepsw);
1623 vec[vecp++] = buffer;
1624 }
1625 vec[vecp] = NULL;
1626 execvp (faceproc, vec);
1627 fprintf (stderr, "unable to exec ");
1628 perror (faceproc);
1629 _exit (-1); /* NOTREACHED */
1630
1631 default:
1632 /* parent process */
1633 close (pdi[0]);
1634 i = strlen (c1->c_text);
1635 if (write (pdi[1], c1->c_text, i) != i)
1636 adios ("pipe", "error writing to");
1637 free (c1->c_text), c1->c_text = NULL;
1638 close (pdi[1]);
1639
1640 close (pdo[1]);
1641 cp = NULL, len = 0;
1642 result = DONE;
1643 while ((i = read (pdo[0], buffer, strlen (buffer))) > 0) {
1644 if (cp) {
1645 int j;
1646 char *dp;
1647 if ((dp = realloc (cp, (unsigned) (j = len + i))) == NULL)
1648 adios (NULL, "unable to allocate face storage");
1649 memcpy(dp + len, buffer, i);
1650 cp = dp, len = j;
1651 }
1652 else {
1653 if ((cp = malloc ((unsigned) i)) == NULL)
1654 adios (NULL, "unable to allocate face storage");
1655 memcpy(cp, buffer, i);
1656 len = i;
1657 }
1658 if (result == DONE)
1659 for (bp = buffer + i - 1; bp >= buffer; bp--)
1660 if (!isascii (*bp) || iscntrl (*bp)) {
1661 result = OK;
1662 break;
1663 }
1664 }
1665 close (pdo[0]);
1666
1667 /* no waiting for child... */
1668
1669 if (result == OK) { /* binary */
1670 if (write (1, cp, len) != len)
1671 adios ("writing", "error");
1672 free (cp);
1673 }
1674 else /* empty */
1675 if ((c1->c_text = cp) == NULL)
1676 result = OK;
1677 break;
1678 }
1679
1680 return result;
1681 }
1682 #endif /* COMMENTED OUT */
1683
1684
1685 int
1686 mhlsbr (int argc, char **argv, FILE *(*action)())
1687 {
1688 SIGNAL_HANDLER istat, pstat, qstat;
1689 char *cp;
1690 struct mcomp *c1;
1691
1692 switch (setjmp (mhlenv)) {
1693 case OK:
1694 cp = invo_name;
1695 sleepsw = 0; /* XXX */
1696 bellflg = clearflg = forwflg = forwall = exitstat = 0;
1697 digest = NULL;
1698 ontty = NOTTY;
1699 mhl_action = action;
1700
1701 /*
1702 * If signal is at default action, then start ignoring
1703 * it, else let it set to its current action.
1704 */
1705 if ((istat = SIGNAL (SIGINT, SIG_IGN)) != SIG_DFL)
1706 SIGNAL (SIGINT, istat);
1707 if ((qstat = SIGNAL (SIGQUIT, SIG_IGN)) != SIG_DFL)
1708 SIGNAL (SIGQUIT, qstat);
1709 pstat = SIGNAL (SIGPIPE, pipeser);
1710 mhl (argc, argv); /* FALL THROUGH! */
1711
1712 default:
1713 SIGNAL (SIGINT, istat);
1714 SIGNAL (SIGQUIT, qstat);
1715 SIGNAL (SIGPIPE, SIG_IGN);/* should probably change to block instead */
1716 if (ontty == PITTY)
1717 m_pclose ();
1718 SIGNAL (SIGPIPE, pstat);
1719 invo_name = cp;
1720 if (holder.c_text) {
1721 free (holder.c_text);
1722 holder.c_text = NULL;
1723 }
1724 free_queue (&msghd, &msgtl);
1725 for (c1 = fmthd; c1; c1 = c1->c_next)
1726 c1->c_flags &= ~HDROUTPUT;
1727 return exitstat;
1728 }
1729 }
1730
1731 #undef adios
1732 #undef done
1733
1734 static void
1735 mhladios (char *what, char *fmt, ...)
1736 {
1737 va_list ap;
1738
1739 va_start(ap, fmt);
1740 advertise (what, NULL, fmt, ap);
1741 va_end(ap);
1742 mhldone (1);
1743 }
1744
1745
1746 static void
1747 mhldone (int status)
1748 {
1749 exitstat = status;
1750 if (mhl_action)
1751 longjmp (mhlenv, DONE);
1752 else
1753 done (exitstat);
1754 }
1755
1756
1757 static int m_pid = NOTOK;
1758 static int sd = NOTOK;
1759
1760 static void
1761 m_popen (char *name)
1762 {
1763 int pd[2];
1764
1765 if (mhl_action && (sd = dup (fileno (stdout))) == NOTOK)
1766 adios ("standard output", "unable to dup()");
1767
1768 if (pipe (pd) == NOTOK)
1769 adios ("pipe", "unable to");
1770
1771 switch (m_pid = vfork ()) {
1772 case NOTOK:
1773 adios ("fork", "unable to");
1774
1775 case OK:
1776 SIGNAL (SIGINT, SIG_DFL);
1777 SIGNAL (SIGQUIT, SIG_DFL);
1778
1779 close (pd[1]);
1780 if (pd[0] != fileno (stdin)) {
1781 dup2 (pd[0], fileno (stdin));
1782 close (pd[0]);
1783 }
1784 execlp (name, r1bindex (name, '/'), NULL);
1785 fprintf (stderr, "unable to exec ");
1786 perror (name);
1787 _exit (-1);
1788
1789 default:
1790 close (pd[0]);
1791 if (pd[1] != fileno (stdout)) {
1792 dup2 (pd[1], fileno (stdout));
1793 close (pd[1]);
1794 }
1795 }
1796 }
1797
1798
1799 void
1800 m_pclose (void)
1801 {
1802 if (m_pid == NOTOK)
1803 return;
1804
1805 if (sd != NOTOK) {
1806 fflush (stdout);
1807 if (dup2 (sd, fileno (stdout)) == NOTOK)
1808 adios ("standard output", "unable to dup2()");
1809
1810 clearerr (stdout);
1811 close (sd);
1812 sd = NOTOK;
1813 }
1814 else
1815 fclose (stdout);
1816
1817 pidwait (m_pid, OK);
1818 m_pid = NOTOK;
1819 }