]> diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
Added first cut at nmh.spec, along with rpm: target in Makefile.in.
[nmh] / uip / mhshowsbr.c
1
2 /*
3 * mhshowsbr.c -- routines to display the contents of MIME messages
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 <fcntl.h>
12 #include <h/signals.h>
13 #include <h/md5.h>
14 #include <errno.h>
15 #include <setjmp.h>
16 #include <signal.h>
17 #include <h/mts.h>
18 #include <h/tws.h>
19 #include <h/mime.h>
20 #include <h/mhparse.h>
21 #include <h/utils.h>
22
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
25 #endif
26
27 /*
28 * Just use sigjmp/longjmp on older machines that
29 * don't have sigsetjmp/siglongjmp.
30 */
31 #ifndef HAVE_SIGSETJMP
32 # define sigjmp_buf jmp_buf
33 # define sigsetjmp(env,mask) setjmp(env)
34 # define siglongjmp(env,val) longjmp(env,val)
35 #endif
36
37 extern int debugsw;
38
39 int pausesw = 1;
40 int serialsw = 0;
41 int nolist = 0;
42
43 char *progsw = NULL;
44
45 /* flags for moreproc/header display */
46 int nomore = 0;
47 char *formsw = NULL;
48
49 pid_t xpid = 0;
50
51 static sigjmp_buf intrenv;
52
53
54 /* termsbr.c */
55 int SOprintf (char *, ...);
56
57 /* mhparse.c */
58 int pidcheck (int);
59
60 /* mhmisc.c */
61 int part_ok (CT, int);
62 int type_ok (CT, int);
63 void content_error (char *, CT, char *, ...);
64 void flush_errors (void);
65
66 /* mhlistsbr.c */
67 int list_switch (CT, int, int, int, int);
68 int list_content (CT, int, int, int, int);
69
70 /*
71 * prototypes
72 */
73 void show_all_messages (CT *);
74 int show_content_aux (CT, int, int, char *, char *);
75
76 /*
77 * static prototypes
78 */
79 static void show_single_message (CT, char *);
80 static void DisplayMsgHeader (CT, char *);
81 static int show_switch (CT, int, int);
82 static int show_content (CT, int, int);
83 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
84 static int show_text (CT, int, int);
85 static int show_multi (CT, int, int);
86 static int show_multi_internal (CT, int, int);
87 static int show_multi_aux (CT, int, int, char *);
88 static int show_message_rfc822 (CT, int, int);
89 static int show_partial (CT, int, int);
90 static int show_external (CT, int, int);
91 static RETSIGTYPE intrser (int);
92
93
94 /*
95 * Top level entry point to show/display a group of messages
96 */
97
98 void
99 show_all_messages (CT *cts)
100 {
101 CT ct, *ctp;
102
103 /*
104 * If form is not specified, then get default form
105 * for showing headers of MIME messages.
106 */
107 if (!formsw)
108 formsw = getcpy (etcpath ("mhl.headers"));
109
110 /*
111 * If form is "mhl.null", suppress display of header.
112 */
113 if (!strcmp (formsw, "mhl.null"))
114 formsw = NULL;
115
116 for (ctp = cts; *ctp; ctp++) {
117 ct = *ctp;
118
119 /* if top-level type is ok, then display message */
120 if (type_ok (ct, 1))
121 show_single_message (ct, formsw);
122 }
123 }
124
125
126 /*
127 * Entry point to show/display a single message
128 */
129
130 static void
131 show_single_message (CT ct, char *form)
132 {
133 sigset_t set, oset;
134
135 #ifdef HAVE_UNION_WAIT
136 union wait status;
137 #else
138 int status;
139 #endif
140
141 /* Allow user executable bit so that temporary directories created by
142 * the viewer (e.g., lynx) are going to be accessible */
143 umask (ct->c_umask & ~(0100));
144
145 /*
146 * If you have a format file, then display
147 * the message headers.
148 */
149 if (form)
150 DisplayMsgHeader(ct, form);
151 else
152 xpid = 0;
153
154 /* Show the body of the message */
155 show_switch (ct, 1, 0);
156
157 if (ct->c_fp) {
158 fclose (ct->c_fp);
159 ct->c_fp = NULL;
160 }
161 if (ct->c_ceclosefnx)
162 (*ct->c_ceclosefnx) (ct);
163
164 /* block a few signals */
165 sigemptyset (&set);
166 sigaddset (&set, SIGHUP);
167 sigaddset (&set, SIGINT);
168 sigaddset (&set, SIGQUIT);
169 sigaddset (&set, SIGTERM);
170 SIGPROCMASK (SIG_BLOCK, &set, &oset);
171
172 while (wait (&status) != NOTOK) {
173 #ifdef HAVE_UNION_WAIT
174 pidcheck (status.w_status);
175 #else
176 pidcheck (status);
177 #endif
178 continue;
179 }
180
181 /* reset the signal mask */
182 SIGPROCMASK (SIG_SETMASK, &oset, &set);
183
184 xpid = 0;
185 flush_errors ();
186 }
187
188
189 /*
190 * Use the mhlproc to show the header fields
191 */
192
193 static void
194 DisplayMsgHeader (CT ct, char *form)
195 {
196 pid_t child_id;
197 int i, vecp;
198 char *vec[8];
199
200 vecp = 0;
201 vec[vecp++] = r1bindex (mhlproc, '/');
202 vec[vecp++] = "-form";
203 vec[vecp++] = form;
204 vec[vecp++] = "-nobody";
205 vec[vecp++] = ct->c_file;
206
207 /*
208 * If we've specified -(no)moreproc,
209 * then just pass that along.
210 */
211 if (nomore) {
212 vec[vecp++] = "-nomoreproc";
213 } else if (progsw) {
214 vec[vecp++] = "-moreproc";
215 vec[vecp++] = progsw;
216 }
217 vec[vecp] = NULL;
218
219 fflush (stdout);
220
221 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
222 sleep (5);
223
224 switch (child_id) {
225 case NOTOK:
226 adios ("fork", "unable to");
227 /* NOTREACHED */
228
229 case OK:
230 execvp (mhlproc, vec);
231 fprintf (stderr, "unable to exec ");
232 perror (mhlproc);
233 _exit (-1);
234 /* NOTREACHED */
235
236 default:
237 xpid = -child_id;
238 break;
239 }
240 }
241
242
243 /*
244 * Switching routine. Call the correct routine
245 * based on content type.
246 */
247
248 static int
249 show_switch (CT ct, int serial, int alternate)
250 {
251 switch (ct->c_type) {
252 case CT_MULTIPART:
253 return show_multi (ct, serial, alternate);
254 break;
255
256 case CT_MESSAGE:
257 switch (ct->c_subtype) {
258 case MESSAGE_PARTIAL:
259 return show_partial (ct, serial, alternate);
260 break;
261
262 case MESSAGE_EXTERNAL:
263 return show_external (ct, serial, alternate);
264 break;
265
266 case MESSAGE_RFC822:
267 default:
268 return show_message_rfc822 (ct, serial, alternate);
269 break;
270 }
271 break;
272
273 case CT_TEXT:
274 return show_text (ct, serial, alternate);
275 break;
276
277 case CT_AUDIO:
278 case CT_IMAGE:
279 case CT_VIDEO:
280 case CT_APPLICATION:
281 return show_content (ct, serial, alternate);
282 break;
283
284 default:
285 adios (NULL, "unknown content type %d", ct->c_type);
286 break;
287 }
288
289 return 0; /* NOT REACHED */
290 }
291
292
293 /*
294 * Generic method for displaying content
295 */
296
297 static int
298 show_content (CT ct, int serial, int alternate)
299 {
300 char *cp, buffer[BUFSIZ];
301 CI ci = &ct->c_ctinfo;
302
303 /* Check for mhn-show-type/subtype */
304 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
305 invo_name, ci->ci_type, ci->ci_subtype);
306 if ((cp = context_find (buffer)) && *cp != '\0')
307 return show_content_aux (ct, serial, alternate, cp, NULL);
308
309 /* Check for mhn-show-type */
310 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
311 if ((cp = context_find (buffer)) && *cp != '\0')
312 return show_content_aux (ct, serial, alternate, cp, NULL);
313
314 if ((cp = ct->c_showproc))
315 return show_content_aux (ct, serial, alternate, cp, NULL);
316
317 /* complain if we are not a part of a multipart/alternative */
318 if (!alternate)
319 content_error (NULL, ct, "don't know how to display content");
320
321 return NOTOK;
322 }
323
324
325 /*
326 * Parse the display string for displaying generic content
327 */
328
329 int
330 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
331 {
332 int fd, len, buflen, quoted;
333 int xstdin, xlist, xpause, xtty;
334 char *bp, *pp, *file, buffer[BUFSIZ];
335 CI ci = &ct->c_ctinfo;
336
337 if (!ct->c_ceopenfnx) {
338 if (!alternate)
339 content_error (NULL, ct, "don't know how to decode content");
340
341 return NOTOK;
342 }
343
344 file = NULL;
345 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
346 return NOTOK;
347 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
348 return (alternate ? DONE : OK);
349
350 xlist = 0;
351 xpause = 0;
352 xstdin = 0;
353 xtty = 0;
354
355 if (cracked) {
356 strncpy (buffer, cp, sizeof(buffer));
357 goto got_command;
358 }
359
360 /* get buffer ready to go */
361 bp = buffer;
362 buflen = sizeof(buffer) - 1;
363 bp[0] = bp[buflen] = '\0';
364 quoted = 0;
365
366 /* Now parse display string */
367 for ( ; *cp && buflen > 0; cp++) {
368 if (*cp == '%') {
369 pp = bp;
370
371 switch (*++cp) {
372 case 'a':
373 /* insert parameters from Content-Type field */
374 {
375 char **ap, **ep;
376 char *s = "";
377
378 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
379 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
380 len = strlen (bp);
381 bp += len;
382 buflen -= len;
383 s = " ";
384 }
385 }
386 break;
387
388 case 'd':
389 /* insert content description */
390 if (ct->c_descr) {
391 char *s;
392
393 s = trimcpy (ct->c_descr);
394 strncpy (bp, s, buflen);
395 free (s);
396 }
397 break;
398
399 case 'e':
400 /* exclusive execution */
401 xtty = 1;
402 break;
403
404 case 'F':
405 /* %e, %f, and stdin is terminal not content */
406 xstdin = 1;
407 xtty = 1;
408 /* and fall... */
409
410 case 'f':
411 /* insert filename containing content */
412 snprintf (bp, buflen, "'%s'", file);
413 /* since we've quoted the file argument, set things up
414 * to look past it, to avoid problems with the quoting
415 * logic below. (I know, I should figure out what's
416 * broken with the quoting logic, but..)
417 */
418 len = strlen(bp);
419 buflen -= len;
420 bp += len;
421 pp = bp;
422 break;
423
424 case 'p':
425 /* %l, and pause prior to displaying content */
426 xpause = pausesw;
427 /* and fall... */
428
429 case 'l':
430 /* display listing prior to displaying content */
431 xlist = !nolist;
432 break;
433
434 case 's':
435 /* insert subtype of content */
436 strncpy (bp, ci->ci_subtype, buflen);
437 break;
438
439 case '%':
440 /* insert character % */
441 goto raw;
442
443 default:
444 *bp++ = *--cp;
445 *bp = '\0';
446 buflen--;
447 continue;
448 }
449 len = strlen (bp);
450 bp += len;
451 buflen -= len;
452
453 /* Did we actually insert something? */
454 if (bp != pp) {
455 /* Insert single quote if not inside quotes already */
456 if (!quoted && buflen) {
457 len = strlen (pp);
458 memmove (pp + 1, pp, len);
459 *pp++ = '\'';
460 buflen--;
461 bp++;
462 }
463 /* Escape existing quotes */
464 while ((pp = strchr (pp, '\'')) && buflen > 3) {
465 len = strlen (pp++);
466 memmove (pp + 3, pp, len);
467 *pp++ = '\\';
468 *pp++ = '\'';
469 *pp++ = '\'';
470 buflen -= 3;
471 bp += 3;
472 }
473 /* If pp is still set, that means we ran out of space. */
474 if (pp)
475 buflen = 0;
476 if (!quoted && buflen) {
477 *bp++ = '\'';
478 *bp = '\0';
479 buflen--;
480 }
481 }
482 } else {
483 raw:
484 *bp++ = *cp;
485 *bp = '\0';
486 buflen--;
487
488 if (*cp == '\'')
489 quoted = !quoted;
490 }
491 }
492
493 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
494 /* content_error would provide a more useful error message
495 * here, except that if we got overrun, it probably would
496 * too.
497 */
498 fprintf(stderr, "Buffer overflow constructing show command!\n");
499 return NOTOK;
500 }
501
502 /* use charset string to modify display method */
503 if (ct->c_termproc) {
504 char term[BUFSIZ];
505
506 strncpy (term, buffer, sizeof(term));
507 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
508 }
509
510 got_command:
511 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
512 fd, xlist, xpause, xstdin, xtty);
513 }
514
515
516 /*
517 * Routine to actually display the content
518 */
519
520 static int
521 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
522 int fd, int xlist, int xpause, int xstdin, int xtty)
523 {
524 pid_t child_id;
525 int i;
526 char *vec[4], exec[BUFSIZ + sizeof "exec "];
527
528 if (debugsw || cracked) {
529 fflush (stdout);
530
531 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
532 ct->c_file);
533 if (ct->c_partno)
534 fprintf (stderr, " part %s", ct->c_partno);
535 if (cracked)
536 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
537 else
538 fprintf (stderr, " using command %s\n", buffer);
539 }
540
541 if (xpid < 0 || (xtty && xpid)) {
542 if (xpid < 0)
543 xpid = -xpid;
544 pidcheck(pidwait (xpid, NOTOK));
545 xpid = 0;
546 }
547
548 if (xlist) {
549 char prompt[BUFSIZ];
550
551 if (ct->c_type == CT_MULTIPART)
552 list_content (ct, -1, 1, 0, 0);
553 else
554 list_switch (ct, -1, 1, 0, 0);
555
556 if (xpause && SOprintf ("Press <return> to show content..."))
557 printf ("Press <return> to show content...");
558
559 if (xpause) {
560 int intr;
561 SIGNAL_HANDLER istat;
562
563 istat = SIGNAL (SIGINT, intrser);
564 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
565 fflush (stdout);
566 prompt[0] = 0;
567 read (fileno (stdout), prompt, sizeof(prompt));
568 }
569 SIGNAL (SIGINT, istat);
570 if (intr != OK || prompt[0] == 'n') {
571 (*ct->c_ceclosefnx) (ct);
572 return (alternate ? DONE : NOTOK);
573 }
574 if (prompt[0] == 'q') done(OK);
575 }
576 }
577
578 snprintf (exec, sizeof(exec), "exec %s", buffer);
579
580 vec[0] = "/bin/sh";
581 vec[1] = "-c";
582 vec[2] = exec;
583 vec[3] = NULL;
584
585 fflush (stdout);
586
587 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
588 sleep (5);
589 switch (child_id) {
590 case NOTOK:
591 advise ("fork", "unable to");
592 (*ct->c_ceclosefnx) (ct);
593 return NOTOK;
594
595 case OK:
596 if (cracked)
597 chdir (cracked);
598 if (!xstdin)
599 dup2 (fd, 0);
600 close (fd);
601 execvp ("/bin/sh", vec);
602 fprintf (stderr, "unable to exec ");
603 perror ("/bin/sh");
604 _exit (-1);
605 /* NOTREACHED */
606
607 default:
608 if (!serial) {
609 ct->c_pid = child_id;
610 if (xtty)
611 xpid = child_id;
612 } else {
613 pidcheck (pidXwait (child_id, NULL));
614 }
615
616 if (fd != NOTOK)
617 (*ct->c_ceclosefnx) (ct);
618 return (alternate ? DONE : OK);
619 }
620 }
621
622
623 /*
624 * show content of type "text"
625 */
626
627 static int
628 show_text (CT ct, int serial, int alternate)
629 {
630 char *cp, buffer[BUFSIZ];
631 CI ci = &ct->c_ctinfo;
632
633 /* Check for mhn-show-type/subtype */
634 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
635 invo_name, ci->ci_type, ci->ci_subtype);
636 if ((cp = context_find (buffer)) && *cp != '\0')
637 return show_content_aux (ct, serial, alternate, cp, NULL);
638
639 /* Check for mhn-show-type */
640 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
641 if ((cp = context_find (buffer)) && *cp != '\0')
642 return show_content_aux (ct, serial, alternate, cp, NULL);
643
644 /*
645 * Use default method if content is text/plain, or if
646 * if it is not a text part of a multipart/alternative
647 */
648 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
649 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
650 moreproc && *moreproc ? moreproc : "more");
651 cp = (ct->c_showproc = add (buffer, NULL));
652 return show_content_aux (ct, serial, alternate, cp, NULL);
653 }
654
655 return NOTOK;
656 }
657
658
659 /*
660 * show message body of type "multipart"
661 */
662
663 static int
664 show_multi (CT ct, int serial, int alternate)
665 {
666 char *cp, buffer[BUFSIZ];
667 CI ci = &ct->c_ctinfo;
668
669 /* Check for mhn-show-type/subtype */
670 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
671 invo_name, ci->ci_type, ci->ci_subtype);
672 if ((cp = context_find (buffer)) && *cp != '\0')
673 return show_multi_aux (ct, serial, alternate, cp);
674
675 /* Check for mhn-show-type */
676 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
677 if ((cp = context_find (buffer)) && *cp != '\0')
678 return show_multi_aux (ct, serial, alternate, cp);
679
680 if ((cp = ct->c_showproc))
681 return show_multi_aux (ct, serial, alternate, cp);
682
683 /*
684 * Use default method to display this multipart content
685 * if it is not a (nested) part of a multipart/alternative,
686 * or if it is one of the known subtypes of multipart.
687 */
688 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
689 return show_multi_internal (ct, serial, alternate);
690
691 return NOTOK;
692 }
693
694
695 /*
696 * show message body of subtypes of multipart that
697 * we understand directly (mixed, alternate, etc...)
698 */
699
700 static int
701 show_multi_internal (CT ct, int serial, int alternate)
702 {
703 int alternating, nowalternate, nowserial, result;
704 struct multipart *m = (struct multipart *) ct->c_ctparams;
705 struct part *part;
706 CT p;
707 sigset_t set, oset;
708
709 alternating = 0;
710 nowalternate = alternate;
711
712 if (ct->c_subtype == MULTI_PARALLEL) {
713 nowserial = serialsw;
714 } else if (ct->c_subtype == MULTI_ALTERNATE) {
715 nowalternate = 1;
716 alternating = 1;
717 nowserial = serial;
718 } else {
719 /*
720 * multipart/mixed
721 * mutlipart/digest
722 * unknown subtypes of multipart (treat as mixed per rfc2046)
723 */
724 nowserial = serial;
725 }
726
727 /* block a few signals */
728 if (!nowserial) {
729 sigemptyset (&set);
730 sigaddset (&set, SIGHUP);
731 sigaddset (&set, SIGINT);
732 sigaddset (&set, SIGQUIT);
733 sigaddset (&set, SIGTERM);
734 SIGPROCMASK (SIG_BLOCK, &set, &oset);
735 }
736
737 /*
738 * alternate -> we are a part inside an multipart/alternative
739 * alternating -> we are a multipart/alternative
740 */
741
742 result = alternate ? NOTOK : OK;
743
744 for (part = m->mp_parts; part; part = part->mp_next) {
745 p = part->mp_part;
746
747 if (part_ok (p, 0) && type_ok (p, 0)) {
748 int inneresult;
749
750 inneresult = show_switch (p, nowserial, nowalternate);
751 switch (inneresult) {
752 case NOTOK:
753 if (alternate && !alternating) {
754 result = NOTOK;
755 goto out;
756 }
757 continue;
758
759 case OK:
760 case DONE:
761 if (alternating) {
762 result = DONE;
763 break;
764 }
765 if (alternate) {
766 alternate = nowalternate = 0;
767 if (result == NOTOK)
768 result = inneresult;
769 }
770 continue;
771 }
772 break;
773 }
774 }
775
776 if (alternating && !part) {
777 if (!alternate)
778 content_error (NULL, ct, "don't know how to display any of the contents");
779 result = NOTOK;
780 goto out;
781 }
782
783 if (serial && !nowserial) {
784 pid_t pid;
785 int kids;
786 #ifdef HAVE_UNION_WAIT
787 union wait status;
788 #else
789 int status;
790 #endif
791
792 kids = 0;
793 for (part = m->mp_parts; part; part = part->mp_next) {
794 p = part->mp_part;
795
796 if (p->c_pid > OK) {
797 if (kill (p->c_pid, 0) == NOTOK)
798 p->c_pid = 0;
799 else
800 kids++;
801 }
802 }
803
804 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
805 #ifdef HAVE_UNION_WAIT
806 pidcheck (status.w_status);
807 #else
808 pidcheck (status);
809 #endif
810
811 for (part = m->mp_parts; part; part = part->mp_next) {
812 p = part->mp_part;
813
814 if (xpid == pid)
815 xpid = 0;
816 if (p->c_pid == pid) {
817 p->c_pid = 0;
818 kids--;
819 break;
820 }
821 }
822 }
823 }
824
825 out:
826 if (!nowserial) {
827 /* reset the signal mask */
828 SIGPROCMASK (SIG_SETMASK, &oset, &set);
829 }
830
831 return result;
832 }
833
834
835 /*
836 * Parse display string for multipart content
837 * and use external program to display it.
838 */
839
840 static int
841 show_multi_aux (CT ct, int serial, int alternate, char *cp)
842 {
843 int len, buflen, quoted;
844 int xlist, xpause, xtty;
845 char *bp, *pp, *file, buffer[BUFSIZ];
846 struct multipart *m = (struct multipart *) ct->c_ctparams;
847 struct part *part;
848 CI ci = &ct->c_ctinfo;
849 CT p;
850
851 for (part = m->mp_parts; part; part = part->mp_next) {
852 p = part->mp_part;
853
854 if (!p->c_ceopenfnx) {
855 if (!alternate)
856 content_error (NULL, p, "don't know how to decode content");
857 return NOTOK;
858 }
859
860 if (p->c_storage == NULL) {
861 file = NULL;
862 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
863 return NOTOK;
864
865 /* I'm not sure if this is necessary? */
866 p->c_storage = add (file, NULL);
867
868 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
869 return (alternate ? DONE : OK);
870 (*p->c_ceclosefnx) (p);
871 }
872 }
873
874 xlist = 0;
875 xpause = 0;
876 xtty = 0;
877
878 /* get buffer ready to go */
879 bp = buffer;
880 buflen = sizeof(buffer) - 1;
881 bp[0] = bp[buflen] = '\0';
882 quoted = 0;
883
884 /* Now parse display string */
885 for ( ; *cp && buflen > 0; cp++) {
886 if (*cp == '%') {
887 pp = bp;
888 switch (*++cp) {
889 case 'a':
890 /* insert parameters from Content-Type field */
891 {
892 char **ap, **ep;
893 char *s = "";
894
895 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
896 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
897 len = strlen (bp);
898 bp += len;
899 buflen -= len;
900 s = " ";
901 }
902 }
903 break;
904
905 case 'd':
906 /* insert content description */
907 if (ct->c_descr) {
908 char *s;
909
910 s = trimcpy (ct->c_descr);
911 strncpy (bp, s, buflen);
912 free (s);
913 }
914 break;
915
916 case 'e':
917 /* exclusive execution */
918 xtty = 1;
919 break;
920
921 case 'F':
922 /* %e and %f */
923 xtty = 1;
924 /* and fall... */
925
926 case 'f':
927 /* insert filename(s) containing content */
928 {
929 char *s = "";
930
931 for (part = m->mp_parts; part; part = part->mp_next) {
932 p = part->mp_part;
933
934 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
935 len = strlen (bp);
936 bp += len;
937 buflen -= len;
938 s = " ";
939 }
940 /* set our starting pointer back to bp, to avoid
941 * requoting the filenames we just added
942 */
943 pp = bp;
944 }
945 break;
946
947 case 'p':
948 /* %l, and pause prior to displaying content */
949 xpause = pausesw;
950 /* and fall... */
951
952 case 'l':
953 /* display listing prior to displaying content */
954 xlist = !nolist;
955 break;
956
957 case 's':
958 /* insert subtype of content */
959 strncpy (bp, ci->ci_subtype, buflen);
960 break;
961
962 case '%':
963 /* insert character % */
964 goto raw;
965
966 default:
967 *bp++ = *--cp;
968 *bp = '\0';
969 buflen--;
970 continue;
971 }
972 len = strlen (bp);
973 bp += len;
974 buflen -= len;
975
976 /* Did we actually insert something? */
977 if (bp != pp) {
978 /* Insert single quote if not inside quotes already */
979 if (!quoted && buflen) {
980 len = strlen (pp);
981 memmove (pp + 1, pp, len);
982 *pp++ = '\'';
983 buflen--;
984 bp++;
985 }
986 /* Escape existing quotes */
987 while ((pp = strchr (pp, '\'')) && buflen > 3) {
988 len = strlen (pp++);
989 memmove (pp + 3, pp, len);
990 *pp++ = '\\';
991 *pp++ = '\'';
992 *pp++ = '\'';
993 buflen -= 3;
994 bp += 3;
995 }
996 /* If pp is still set, that means we ran out of space. */
997 if (pp)
998 buflen = 0;
999 if (!quoted && buflen) {
1000 *bp++ = '\'';
1001 *bp = '\0';
1002 buflen--;
1003 }
1004 }
1005 } else {
1006 raw:
1007 *bp++ = *cp;
1008 *bp = '\0';
1009 buflen--;
1010
1011 if (*cp == '\'')
1012 quoted = !quoted;
1013 }
1014 }
1015
1016 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1017 /* content_error would provide a more useful error message
1018 * here, except that if we got overrun, it probably would
1019 * too.
1020 */
1021 fprintf(stderr, "Buffer overflow constructing show command!\n");
1022 return NOTOK;
1023 }
1024
1025 /* use charset string to modify display method */
1026 if (ct->c_termproc) {
1027 char term[BUFSIZ];
1028
1029 strncpy (term, buffer, sizeof(term));
1030 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1031 }
1032
1033 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1034 NOTOK, xlist, xpause, 0, xtty);
1035 }
1036
1037
1038 /*
1039 * show content of type "message/rfc822"
1040 */
1041
1042 static int
1043 show_message_rfc822 (CT ct, int serial, int alternate)
1044 {
1045 char *cp, buffer[BUFSIZ];
1046 CI ci = &ct->c_ctinfo;
1047
1048 /* Check for mhn-show-type/subtype */
1049 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1050 invo_name, ci->ci_type, ci->ci_subtype);
1051 if ((cp = context_find (buffer)) && *cp != '\0')
1052 return show_content_aux (ct, serial, alternate, cp, NULL);
1053
1054 /* Check for mhn-show-type */
1055 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1056 if ((cp = context_find (buffer)) && *cp != '\0')
1057 return show_content_aux (ct, serial, alternate, cp, NULL);
1058
1059 if ((cp = ct->c_showproc))
1060 return show_content_aux (ct, serial, alternate, cp, NULL);
1061
1062 /* default method for message/rfc822 */
1063 if (ct->c_subtype == MESSAGE_RFC822) {
1064 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1065 return show_content_aux (ct, serial, alternate, cp, NULL);
1066 }
1067
1068 /* complain if we are not a part of a multipart/alternative */
1069 if (!alternate)
1070 content_error (NULL, ct, "don't know how to display content");
1071
1072 return NOTOK;
1073 }
1074
1075
1076 /*
1077 * Show content of type "message/partial".
1078 */
1079
1080 static int
1081 show_partial (CT ct, int serial, int alternate)
1082 {
1083 content_error (NULL, ct,
1084 "in order to display this message, you must reassemble it");
1085 return NOTOK;
1086 }
1087
1088
1089 /*
1090 * Show content of type "message/external".
1091 *
1092 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1093 */
1094
1095 static int
1096 show_external (CT ct, int serial, int alternate)
1097 {
1098 struct exbody *e = (struct exbody *) ct->c_ctparams;
1099 CT p = e->eb_content;
1100
1101 if (!type_ok (p, 0))
1102 return OK;
1103
1104 return show_switch (p, serial, alternate);
1105
1106 #if 0
1107 content_error (NULL, p, "don't know how to display content");
1108 return NOTOK;
1109 #endif
1110 }
1111
1112
1113 static RETSIGTYPE
1114 intrser (int i)
1115 {
1116 #ifndef RELIABLE_SIGNALS
1117 SIGNAL (SIGINT, intrser);
1118 #endif
1119
1120 putchar ('\n');
1121 siglongjmp (intrenv, DONE);
1122 }