3 * mhshowsbr.c -- routines to display the contents of MIME messages
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.
12 #include <h/signals.h>
20 #include <h/mhparse.h>
32 /* flags for moreproc/header display */
38 static sigjmp_buf intrenv;
42 int part_ok (CT, int);
43 int type_ok (CT, int);
44 void content_error (char *, CT, char *, ...);
45 void flush_errors (void);
48 int list_switch (CT, int, int, int, int);
49 int list_content (CT, int, int, int, int);
54 void show_all_messages (CT *);
55 int show_content_aux (CT, int, int, char *, char *);
60 static void show_single_message (CT, char *);
61 static void DisplayMsgHeader (CT, char *);
62 static int show_switch (CT, int, int);
63 static int show_content (CT, int, int);
64 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
65 static int show_text (CT, int, int);
66 static int show_multi (CT, int, int);
67 static int show_multi_internal (CT, int, int);
68 static int show_multi_aux (CT, int, int, char *);
69 static int show_message_rfc822 (CT, int, int);
70 static int show_partial (CT, int, int);
71 static int show_external (CT, int, int);
72 static void intrser (int);
76 * Top level entry point to show/display a group of messages
80 show_all_messages (CT *cts)
85 * If form is not specified, then get default form
86 * for showing headers of MIME messages.
89 formsw = getcpy (etcpath ("mhl.headers"));
92 * If form is "mhl.null", suppress display of header.
94 if (!strcmp (formsw, "mhl.null"))
97 for (ctp = cts; *ctp; ctp++) {
100 /* if top-level type is ok, then display message */
102 show_single_message (ct, formsw);
108 * Entry point to show/display a single message
112 show_single_message (CT ct, char *form)
118 /* Allow user executable bit so that temporary directories created by
119 * the viewer (e.g., lynx) are going to be accessible */
120 umask (ct->c_umask & ~(0100));
123 * If you have a format file, then display
124 * the message headers.
127 DisplayMsgHeader(ct, form);
131 /* Show the body of the message */
132 show_switch (ct, 1, 0);
138 if (ct->c_ceclosefnx)
139 (*ct->c_ceclosefnx) (ct);
141 /* block a few signals */
143 sigaddset (&set, SIGHUP);
144 sigaddset (&set, SIGINT);
145 sigaddset (&set, SIGQUIT);
146 sigaddset (&set, SIGTERM);
147 sigprocmask (SIG_BLOCK, &set, &oset);
149 while (wait (&status) != NOTOK) {
154 /* reset the signal mask */
155 sigprocmask (SIG_SETMASK, &oset, &set);
163 * Use the mhlproc to show the header fields
167 DisplayMsgHeader (CT ct, char *form)
174 vec[vecp++] = r1bindex (mhlproc, '/');
175 vec[vecp++] = "-form";
177 vec[vecp++] = "-nobody";
178 vec[vecp++] = ct->c_file;
181 * If we've specified -(no)moreproc,
182 * then just pass that along.
185 vec[vecp++] = "-nomoreproc";
187 vec[vecp++] = "-moreproc";
188 vec[vecp++] = progsw;
194 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
199 adios ("fork", "unable to");
203 execvp (mhlproc, vec);
204 fprintf (stderr, "unable to exec ");
217 * Switching routine. Call the correct routine
218 * based on content type.
222 show_switch (CT ct, int serial, int alternate)
224 switch (ct->c_type) {
226 return show_multi (ct, serial, alternate);
230 switch (ct->c_subtype) {
231 case MESSAGE_PARTIAL:
232 return show_partial (ct, serial, alternate);
235 case MESSAGE_EXTERNAL:
236 return show_external (ct, serial, alternate);
241 return show_message_rfc822 (ct, serial, alternate);
247 return show_text (ct, serial, alternate);
254 return show_content (ct, serial, alternate);
258 adios (NULL, "unknown content type %d", ct->c_type);
262 return 0; /* NOT REACHED */
267 * Generic method for displaying content
271 show_content (CT ct, int serial, int alternate)
273 char *cp, buffer[BUFSIZ];
274 CI ci = &ct->c_ctinfo;
276 /* Check for mhn-show-type/subtype */
277 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
278 invo_name, ci->ci_type, ci->ci_subtype);
279 if ((cp = context_find (buffer)) && *cp != '\0')
280 return show_content_aux (ct, serial, alternate, cp, NULL);
282 /* Check for mhn-show-type */
283 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
284 if ((cp = context_find (buffer)) && *cp != '\0')
285 return show_content_aux (ct, serial, alternate, cp, NULL);
287 if ((cp = ct->c_showproc))
288 return show_content_aux (ct, serial, alternate, cp, NULL);
290 /* complain if we are not a part of a multipart/alternative */
292 content_error (NULL, ct, "don't know how to display content");
299 * Parse the display string for displaying generic content
303 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
305 int fd, len, buflen, quoted;
306 int xstdin, xlist, xpause, xtty;
307 char *bp, *pp, *file, buffer[BUFSIZ];
308 CI ci = &ct->c_ctinfo;
310 if (!ct->c_ceopenfnx) {
312 content_error (NULL, ct, "don't know how to decode content");
318 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
320 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
321 return (alternate ? DONE : OK);
329 strncpy (buffer, cp, sizeof(buffer));
333 /* get buffer ready to go */
335 buflen = sizeof(buffer) - 1;
336 bp[0] = bp[buflen] = '\0';
339 /* Now parse display string */
340 for ( ; *cp && buflen > 0; cp++) {
346 /* insert parameters from Content-Type field */
351 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
352 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
362 /* insert content description */
366 s = trimcpy (ct->c_descr);
367 strncpy (bp, s, buflen);
373 /* exclusive execution */
378 /* %e, %f, and stdin is terminal not content */
384 /* insert filename containing content */
385 snprintf (bp, buflen, "'%s'", file);
386 /* since we've quoted the file argument, set things up
387 * to look past it, to avoid problems with the quoting
388 * logic below. (I know, I should figure out what's
389 * broken with the quoting logic, but..)
398 /* %l, and pause prior to displaying content */
403 /* display listing prior to displaying content */
408 /* insert subtype of content */
409 strncpy (bp, ci->ci_subtype, buflen);
413 /* insert character % */
426 /* Did we actually insert something? */
428 /* Insert single quote if not inside quotes already */
429 if (!quoted && buflen) {
431 memmove (pp + 1, pp, len);
436 /* Escape existing quotes */
437 while ((pp = strchr (pp, '\'')) && buflen > 3) {
439 memmove (pp + 3, pp, len);
446 /* If pp is still set, that means we ran out of space. */
449 if (!quoted && buflen) {
467 (ct->c_termproc && (size_t) buflen <= strlen(ct->c_termproc))) {
468 /* content_error would provide a more useful error message
469 * here, except that if we got overrun, it probably would
472 fprintf(stderr, "Buffer overflow constructing show command!\n");
476 /* use charset string to modify display method */
477 if (ct->c_termproc) {
480 strncpy (term, buffer, sizeof(term));
481 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
485 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
486 fd, xlist, xpause, xstdin, xtty);
491 * Routine to actually display the content
495 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
496 int fd, int xlist, int xpause, int xstdin, int xtty)
500 char *vec[4], exec[BUFSIZ + sizeof "exec "];
502 if (debugsw || cracked) {
505 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
508 fprintf (stderr, " part %s", ct->c_partno);
510 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
512 fprintf (stderr, " using command %s\n", buffer);
515 if (xpid < 0 || (xtty && xpid)) {
518 pidcheck(pidwait (xpid, NOTOK));
525 if (ct->c_type == CT_MULTIPART)
526 list_content (ct, -1, 1, 0, 0);
528 list_switch (ct, -1, 1, 0, 0);
530 if (xpause && isatty (fileno (stdout))) {
532 SIGNAL_HANDLER istat;
534 if (SOprintf ("Press <return> to show content..."))
535 printf ("Press <return> to show content...");
537 istat = SIGNAL (SIGINT, intrser);
538 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
541 read (fileno (stdout), prompt, sizeof(prompt));
543 SIGNAL (SIGINT, istat);
544 if (intr != OK || prompt[0] == 'n') {
545 (*ct->c_ceclosefnx) (ct);
546 return (alternate ? DONE : NOTOK);
548 if (prompt[0] == 'q') done(OK);
552 snprintf (exec, sizeof(exec), "exec %s", buffer);
561 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
565 advise ("fork", "unable to");
566 (*ct->c_ceclosefnx) (ct);
575 execvp ("/bin/sh", vec);
576 fprintf (stderr, "unable to exec ");
583 ct->c_pid = child_id;
587 pidcheck (pidXwait (child_id, NULL));
591 (*ct->c_ceclosefnx) (ct);
592 return (alternate ? DONE : OK);
598 * show content of type "text"
602 show_text (CT ct, int serial, int alternate)
604 char *cp, buffer[BUFSIZ];
605 CI ci = &ct->c_ctinfo;
607 /* Check for mhn-show-type/subtype */
608 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
609 invo_name, ci->ci_type, ci->ci_subtype);
610 if ((cp = context_find (buffer)) && *cp != '\0')
611 return show_content_aux (ct, serial, alternate, cp, NULL);
613 /* Check for mhn-show-type */
614 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
615 if ((cp = context_find (buffer)) && *cp != '\0')
616 return show_content_aux (ct, serial, alternate, cp, NULL);
619 * Use default method if content is text/plain, or if
620 * if it is not a text part of a multipart/alternative
622 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
623 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
624 moreproc && *moreproc ? moreproc : "more");
625 cp = (ct->c_showproc = add (buffer, NULL));
626 return show_content_aux (ct, serial, alternate, cp, NULL);
634 * show message body of type "multipart"
638 show_multi (CT ct, int serial, int alternate)
640 char *cp, buffer[BUFSIZ];
641 CI ci = &ct->c_ctinfo;
643 /* Check for mhn-show-type/subtype */
644 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
645 invo_name, ci->ci_type, ci->ci_subtype);
646 if ((cp = context_find (buffer)) && *cp != '\0')
647 return show_multi_aux (ct, serial, alternate, cp);
649 /* Check for mhn-show-type */
650 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
651 if ((cp = context_find (buffer)) && *cp != '\0')
652 return show_multi_aux (ct, serial, alternate, cp);
654 if ((cp = ct->c_showproc))
655 return show_multi_aux (ct, serial, alternate, cp);
658 * Use default method to display this multipart content
659 * if it is not a (nested) part of a multipart/alternative,
660 * or if it is one of the known subtypes of multipart.
662 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
663 return show_multi_internal (ct, serial, alternate);
670 * show message body of subtypes of multipart that
671 * we understand directly (mixed, alternate, etc...)
675 show_multi_internal (CT ct, int serial, int alternate)
677 int alternating, nowalternate, nowserial, result;
678 struct multipart *m = (struct multipart *) ct->c_ctparams;
684 nowalternate = alternate;
686 if (ct->c_subtype == MULTI_PARALLEL) {
687 nowserial = serialsw;
688 } else if (ct->c_subtype == MULTI_ALTERNATE) {
696 * unknown subtypes of multipart (treat as mixed per rfc2046)
701 /* block a few signals */
704 sigaddset (&set, SIGHUP);
705 sigaddset (&set, SIGINT);
706 sigaddset (&set, SIGQUIT);
707 sigaddset (&set, SIGTERM);
708 sigprocmask (SIG_BLOCK, &set, &oset);
712 * alternate -> we are a part inside an multipart/alternative
713 * alternating -> we are a multipart/alternative
716 result = alternate ? NOTOK : OK;
718 for (part = m->mp_parts; part; part = part->mp_next) {
721 if (part_ok (p, 1) && type_ok (p, 1)) {
724 inneresult = show_switch (p, nowserial, nowalternate);
725 switch (inneresult) {
727 if (alternate && !alternating) {
740 alternate = nowalternate = 0;
750 if (alternating && !part) {
752 content_error (NULL, ct, "don't know how to display any of the contents");
757 if (serial && !nowserial) {
763 for (part = m->mp_parts; part; part = part->mp_next) {
767 if (kill (p->c_pid, 0) == NOTOK)
774 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
777 for (part = m->mp_parts; part; part = part->mp_next) {
782 if (p->c_pid == pid) {
793 /* reset the signal mask */
794 sigprocmask (SIG_SETMASK, &oset, &set);
802 * Parse display string for multipart content
803 * and use external program to display it.
807 show_multi_aux (CT ct, int serial, int alternate, char *cp)
809 int len, buflen, quoted;
810 int xlist, xpause, xtty;
811 char *bp, *pp, *file, buffer[BUFSIZ];
812 struct multipart *m = (struct multipart *) ct->c_ctparams;
814 CI ci = &ct->c_ctinfo;
817 for (part = m->mp_parts; part; part = part->mp_next) {
820 if (!p->c_ceopenfnx) {
822 content_error (NULL, p, "don't know how to decode content");
826 if (p->c_storage == NULL) {
828 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
831 /* I'm not sure if this is necessary? */
832 p->c_storage = add (file, NULL);
834 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
835 return (alternate ? DONE : OK);
836 (*p->c_ceclosefnx) (p);
844 /* get buffer ready to go */
846 buflen = sizeof(buffer) - 1;
847 bp[0] = bp[buflen] = '\0';
850 /* Now parse display string */
851 for ( ; *cp && buflen > 0; cp++) {
856 /* insert parameters from Content-Type field */
861 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
862 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
872 /* insert content description */
876 s = trimcpy (ct->c_descr);
877 strncpy (bp, s, buflen);
883 /* exclusive execution */
893 /* insert filename(s) containing content */
897 for (part = m->mp_parts; part; part = part->mp_next) {
900 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
906 /* set our starting pointer back to bp, to avoid
907 * requoting the filenames we just added
914 /* %l, and pause prior to displaying content */
919 /* display listing prior to displaying content */
924 /* insert subtype of content */
925 strncpy (bp, ci->ci_subtype, buflen);
929 /* insert character % */
942 /* Did we actually insert something? */
944 /* Insert single quote if not inside quotes already */
945 if (!quoted && buflen) {
947 memmove (pp + 1, pp, len);
952 /* Escape existing quotes */
953 while ((pp = strchr (pp, '\'')) && buflen > 3) {
955 memmove (pp + 3, pp, len);
962 /* If pp is still set, that means we ran out of space. */
965 if (!quoted && buflen) {
983 (ct->c_termproc && (size_t) buflen <= strlen(ct->c_termproc))) {
984 /* content_error would provide a more useful error message
985 * here, except that if we got overrun, it probably would
988 fprintf(stderr, "Buffer overflow constructing show command!\n");
992 /* use charset string to modify display method */
993 if (ct->c_termproc) {
996 strncpy (term, buffer, sizeof(term));
997 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1000 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1001 NOTOK, xlist, xpause, 0, xtty);
1006 * show content of type "message/rfc822"
1010 show_message_rfc822 (CT ct, int serial, int alternate)
1012 char *cp, buffer[BUFSIZ];
1013 CI ci = &ct->c_ctinfo;
1015 /* Check for mhn-show-type/subtype */
1016 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1017 invo_name, ci->ci_type, ci->ci_subtype);
1018 if ((cp = context_find (buffer)) && *cp != '\0')
1019 return show_content_aux (ct, serial, alternate, cp, NULL);
1021 /* Check for mhn-show-type */
1022 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1023 if ((cp = context_find (buffer)) && *cp != '\0')
1024 return show_content_aux (ct, serial, alternate, cp, NULL);
1026 if ((cp = ct->c_showproc))
1027 return show_content_aux (ct, serial, alternate, cp, NULL);
1029 /* default method for message/rfc822 */
1030 if (ct->c_subtype == MESSAGE_RFC822) {
1031 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1032 return show_content_aux (ct, serial, alternate, cp, NULL);
1035 /* complain if we are not a part of a multipart/alternative */
1037 content_error (NULL, ct, "don't know how to display content");
1044 * Show content of type "message/partial".
1048 show_partial (CT ct, int serial, int alternate)
1050 NMH_UNUSED (serial);
1051 NMH_UNUSED (alternate);
1053 content_error (NULL, ct,
1054 "in order to display this message, you must reassemble it");
1060 * Show content of type "message/external".
1062 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1066 show_external (CT ct, int serial, int alternate)
1068 struct exbody *e = (struct exbody *) ct->c_ctparams;
1069 CT p = e->eb_content;
1071 if (!type_ok (p, 0))
1074 return show_switch (p, serial, alternate);
1077 content_error (NULL, p, "don't know how to display content");
1089 siglongjmp (intrenv, DONE);