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>
15 #include <h/m_setjmp.h>
20 #include <h/mhparse.h>
32 /* flags for moreproc/header display */
38 static sigjmp_buf intrenv;
42 int SOprintf (char *, ...);
48 int part_ok (CT, int);
49 int type_ok (CT, int);
50 void content_error (char *, CT, char *, ...);
51 void flush_errors (void);
54 int list_switch (CT, int, int, int, int);
55 int list_content (CT, int, int, int, int);
60 void show_all_messages (CT *);
61 int show_content_aux (CT, int, int, char *, char *);
66 static void show_single_message (CT, char *);
67 static void DisplayMsgHeader (CT, char *);
68 static int show_switch (CT, int, int);
69 static int show_content (CT, int, int);
70 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
71 static int show_text (CT, int, int);
72 static int show_multi (CT, int, int);
73 static int show_multi_internal (CT, int, int);
74 static int show_multi_aux (CT, int, int, char *);
75 static int show_message_rfc822 (CT, int, int);
76 static int show_partial (CT, int, int);
77 static int show_external (CT, int, int);
78 static void intrser (int);
82 * Top level entry point to show/display a group of messages
86 show_all_messages (CT *cts)
91 * If form is not specified, then get default form
92 * for showing headers of MIME messages.
95 formsw = getcpy (etcpath ("mhl.headers"));
98 * If form is "mhl.null", suppress display of header.
100 if (!strcmp (formsw, "mhl.null"))
103 for (ctp = cts; *ctp; ctp++) {
106 /* if top-level type is ok, then display message */
108 show_single_message (ct, formsw);
114 * Entry point to show/display a single message
118 show_single_message (CT ct, char *form)
124 /* Allow user executable bit so that temporary directories created by
125 * the viewer (e.g., lynx) are going to be accessible */
126 umask (ct->c_umask & ~(0100));
129 * If you have a format file, then display
130 * the message headers.
133 DisplayMsgHeader(ct, form);
137 /* Show the body of the message */
138 show_switch (ct, 1, 0);
144 if (ct->c_ceclosefnx)
145 (*ct->c_ceclosefnx) (ct);
147 /* block a few signals */
149 sigaddset (&set, SIGHUP);
150 sigaddset (&set, SIGINT);
151 sigaddset (&set, SIGQUIT);
152 sigaddset (&set, SIGTERM);
153 sigprocmask (SIG_BLOCK, &set, &oset);
155 while (wait (&status) != NOTOK) {
160 /* reset the signal mask */
161 sigprocmask (SIG_SETMASK, &oset, &set);
169 * Use the mhlproc to show the header fields
173 DisplayMsgHeader (CT ct, char *form)
180 vec[vecp++] = r1bindex (mhlproc, '/');
181 vec[vecp++] = "-form";
183 vec[vecp++] = "-nobody";
184 vec[vecp++] = ct->c_file;
187 * If we've specified -(no)moreproc,
188 * then just pass that along.
191 vec[vecp++] = "-nomoreproc";
193 vec[vecp++] = "-moreproc";
194 vec[vecp++] = progsw;
200 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
205 adios ("fork", "unable to");
209 execvp (mhlproc, vec);
210 fprintf (stderr, "unable to exec ");
223 * Switching routine. Call the correct routine
224 * based on content type.
228 show_switch (CT ct, int serial, int alternate)
230 switch (ct->c_type) {
232 return show_multi (ct, serial, alternate);
236 switch (ct->c_subtype) {
237 case MESSAGE_PARTIAL:
238 return show_partial (ct, serial, alternate);
241 case MESSAGE_EXTERNAL:
242 return show_external (ct, serial, alternate);
247 return show_message_rfc822 (ct, serial, alternate);
253 return show_text (ct, serial, alternate);
260 return show_content (ct, serial, alternate);
264 adios (NULL, "unknown content type %d", ct->c_type);
268 return 0; /* NOT REACHED */
273 * Generic method for displaying content
277 show_content (CT ct, int serial, int alternate)
279 char *cp, buffer[BUFSIZ];
280 CI ci = &ct->c_ctinfo;
282 /* Check for mhn-show-type/subtype */
283 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
284 invo_name, ci->ci_type, ci->ci_subtype);
285 if ((cp = context_find (buffer)) && *cp != '\0')
286 return show_content_aux (ct, serial, alternate, cp, NULL);
288 /* Check for mhn-show-type */
289 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
290 if ((cp = context_find (buffer)) && *cp != '\0')
291 return show_content_aux (ct, serial, alternate, cp, NULL);
293 if ((cp = ct->c_showproc))
294 return show_content_aux (ct, serial, alternate, cp, NULL);
296 /* complain if we are not a part of a multipart/alternative */
298 content_error (NULL, ct, "don't know how to display content");
305 * Parse the display string for displaying generic content
309 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
311 int fd, len, buflen, quoted;
312 int xstdin, xlist, xpause, xtty;
313 char *bp, *pp, *file, buffer[BUFSIZ];
314 CI ci = &ct->c_ctinfo;
316 if (!ct->c_ceopenfnx) {
318 content_error (NULL, ct, "don't know how to decode content");
324 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
326 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
327 return (alternate ? DONE : OK);
335 strncpy (buffer, cp, sizeof(buffer));
339 /* get buffer ready to go */
341 buflen = sizeof(buffer) - 1;
342 bp[0] = bp[buflen] = '\0';
345 /* Now parse display string */
346 for ( ; *cp && buflen > 0; cp++) {
352 /* insert parameters from Content-Type field */
357 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
358 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
368 /* insert content description */
372 s = trimcpy (ct->c_descr);
373 strncpy (bp, s, buflen);
379 /* exclusive execution */
384 /* %e, %f, and stdin is terminal not content */
390 /* insert filename containing content */
391 snprintf (bp, buflen, "'%s'", file);
392 /* since we've quoted the file argument, set things up
393 * to look past it, to avoid problems with the quoting
394 * logic below. (I know, I should figure out what's
395 * broken with the quoting logic, but..)
404 /* %l, and pause prior to displaying content */
409 /* display listing prior to displaying content */
414 /* insert subtype of content */
415 strncpy (bp, ci->ci_subtype, buflen);
419 /* insert character % */
432 /* Did we actually insert something? */
434 /* Insert single quote if not inside quotes already */
435 if (!quoted && buflen) {
437 memmove (pp + 1, pp, len);
442 /* Escape existing quotes */
443 while ((pp = strchr (pp, '\'')) && buflen > 3) {
445 memmove (pp + 3, pp, len);
452 /* If pp is still set, that means we ran out of space. */
455 if (!quoted && buflen) {
473 (ct->c_termproc && (size_t) buflen <= strlen(ct->c_termproc))) {
474 /* content_error would provide a more useful error message
475 * here, except that if we got overrun, it probably would
478 fprintf(stderr, "Buffer overflow constructing show command!\n");
482 /* use charset string to modify display method */
483 if (ct->c_termproc) {
486 strncpy (term, buffer, sizeof(term));
487 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
491 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
492 fd, xlist, xpause, xstdin, xtty);
497 * Routine to actually display the content
501 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
502 int fd, int xlist, int xpause, int xstdin, int xtty)
506 char *vec[4], exec[BUFSIZ + sizeof "exec "];
508 if (debugsw || cracked) {
511 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
514 fprintf (stderr, " part %s", ct->c_partno);
516 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
518 fprintf (stderr, " using command %s\n", buffer);
521 if (xpid < 0 || (xtty && xpid)) {
524 pidcheck(pidwait (xpid, NOTOK));
531 if (ct->c_type == CT_MULTIPART)
532 list_content (ct, -1, 1, 0, 0);
534 list_switch (ct, -1, 1, 0, 0);
536 if (xpause && SOprintf ("Press <return> to show content..."))
537 printf ("Press <return> to show content...");
541 SIGNAL_HANDLER istat;
543 istat = SIGNAL (SIGINT, intrser);
544 if ((intr = m_sigsetjmp (intrenv, 1)) == OK) {
547 read (fileno (stdout), prompt, sizeof(prompt));
549 SIGNAL (SIGINT, istat);
550 if (intr != OK || prompt[0] == 'n') {
551 (*ct->c_ceclosefnx) (ct);
552 return (alternate ? DONE : NOTOK);
554 if (prompt[0] == 'q') done(OK);
558 snprintf (exec, sizeof(exec), "exec %s", buffer);
567 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
571 advise ("fork", "unable to");
572 (*ct->c_ceclosefnx) (ct);
581 execvp ("/bin/sh", vec);
582 fprintf (stderr, "unable to exec ");
589 ct->c_pid = child_id;
593 pidcheck (pidXwait (child_id, NULL));
597 (*ct->c_ceclosefnx) (ct);
598 return (alternate ? DONE : OK);
604 * show content of type "text"
608 show_text (CT ct, int serial, int alternate)
610 char *cp, buffer[BUFSIZ];
611 CI ci = &ct->c_ctinfo;
613 /* Check for mhn-show-type/subtype */
614 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
615 invo_name, ci->ci_type, ci->ci_subtype);
616 if ((cp = context_find (buffer)) && *cp != '\0')
617 return show_content_aux (ct, serial, alternate, cp, NULL);
619 /* Check for mhn-show-type */
620 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
621 if ((cp = context_find (buffer)) && *cp != '\0')
622 return show_content_aux (ct, serial, alternate, cp, NULL);
625 * Use default method if content is text/plain, or if
626 * if it is not a text part of a multipart/alternative
628 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
629 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
630 moreproc && *moreproc ? moreproc : "more");
631 cp = (ct->c_showproc = add (buffer, NULL));
632 return show_content_aux (ct, serial, alternate, cp, NULL);
640 * show message body of type "multipart"
644 show_multi (CT ct, int serial, int alternate)
646 char *cp, buffer[BUFSIZ];
647 CI ci = &ct->c_ctinfo;
649 /* Check for mhn-show-type/subtype */
650 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
651 invo_name, ci->ci_type, ci->ci_subtype);
652 if ((cp = context_find (buffer)) && *cp != '\0')
653 return show_multi_aux (ct, serial, alternate, cp);
655 /* Check for mhn-show-type */
656 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
657 if ((cp = context_find (buffer)) && *cp != '\0')
658 return show_multi_aux (ct, serial, alternate, cp);
660 if ((cp = ct->c_showproc))
661 return show_multi_aux (ct, serial, alternate, cp);
664 * Use default method to display this multipart content
665 * if it is not a (nested) part of a multipart/alternative,
666 * or if it is one of the known subtypes of multipart.
668 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
669 return show_multi_internal (ct, serial, alternate);
676 * show message body of subtypes of multipart that
677 * we understand directly (mixed, alternate, etc...)
681 show_multi_internal (CT ct, int serial, int alternate)
683 int alternating, nowalternate, nowserial, result;
684 struct multipart *m = (struct multipart *) ct->c_ctparams;
690 nowalternate = alternate;
692 if (ct->c_subtype == MULTI_PARALLEL) {
693 nowserial = serialsw;
694 } else if (ct->c_subtype == MULTI_ALTERNATE) {
702 * unknown subtypes of multipart (treat as mixed per rfc2046)
707 /* block a few signals */
710 sigaddset (&set, SIGHUP);
711 sigaddset (&set, SIGINT);
712 sigaddset (&set, SIGQUIT);
713 sigaddset (&set, SIGTERM);
714 sigprocmask (SIG_BLOCK, &set, &oset);
718 * alternate -> we are a part inside an multipart/alternative
719 * alternating -> we are a multipart/alternative
722 result = alternate ? NOTOK : OK;
724 for (part = m->mp_parts; part; part = part->mp_next) {
727 if (part_ok (p, 1) && type_ok (p, 1)) {
730 inneresult = show_switch (p, nowserial, nowalternate);
731 switch (inneresult) {
733 if (alternate && !alternating) {
746 alternate = nowalternate = 0;
756 if (alternating && !part) {
758 content_error (NULL, ct, "don't know how to display any of the contents");
763 if (serial && !nowserial) {
769 for (part = m->mp_parts; part; part = part->mp_next) {
773 if (kill (p->c_pid, 0) == NOTOK)
780 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
783 for (part = m->mp_parts; part; part = part->mp_next) {
788 if (p->c_pid == pid) {
799 /* reset the signal mask */
800 sigprocmask (SIG_SETMASK, &oset, &set);
808 * Parse display string for multipart content
809 * and use external program to display it.
813 show_multi_aux (CT ct, int serial, int alternate, char *cp)
815 int len, buflen, quoted;
816 int xlist, xpause, xtty;
817 char *bp, *pp, *file, buffer[BUFSIZ];
818 struct multipart *m = (struct multipart *) ct->c_ctparams;
820 CI ci = &ct->c_ctinfo;
823 for (part = m->mp_parts; part; part = part->mp_next) {
826 if (!p->c_ceopenfnx) {
828 content_error (NULL, p, "don't know how to decode content");
832 if (p->c_storage == NULL) {
834 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
837 /* I'm not sure if this is necessary? */
838 p->c_storage = add (file, NULL);
840 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
841 return (alternate ? DONE : OK);
842 (*p->c_ceclosefnx) (p);
850 /* get buffer ready to go */
852 buflen = sizeof(buffer) - 1;
853 bp[0] = bp[buflen] = '\0';
856 /* Now parse display string */
857 for ( ; *cp && buflen > 0; cp++) {
862 /* insert parameters from Content-Type field */
867 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
868 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
878 /* insert content description */
882 s = trimcpy (ct->c_descr);
883 strncpy (bp, s, buflen);
889 /* exclusive execution */
899 /* insert filename(s) containing content */
903 for (part = m->mp_parts; part; part = part->mp_next) {
906 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
912 /* set our starting pointer back to bp, to avoid
913 * requoting the filenames we just added
920 /* %l, and pause prior to displaying content */
925 /* display listing prior to displaying content */
930 /* insert subtype of content */
931 strncpy (bp, ci->ci_subtype, buflen);
935 /* insert character % */
948 /* Did we actually insert something? */
950 /* Insert single quote if not inside quotes already */
951 if (!quoted && buflen) {
953 memmove (pp + 1, pp, len);
958 /* Escape existing quotes */
959 while ((pp = strchr (pp, '\'')) && buflen > 3) {
961 memmove (pp + 3, pp, len);
968 /* If pp is still set, that means we ran out of space. */
971 if (!quoted && buflen) {
989 (ct->c_termproc && (size_t) buflen <= strlen(ct->c_termproc))) {
990 /* content_error would provide a more useful error message
991 * here, except that if we got overrun, it probably would
994 fprintf(stderr, "Buffer overflow constructing show command!\n");
998 /* use charset string to modify display method */
999 if (ct->c_termproc) {
1002 strncpy (term, buffer, sizeof(term));
1003 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1006 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1007 NOTOK, xlist, xpause, 0, xtty);
1012 * show content of type "message/rfc822"
1016 show_message_rfc822 (CT ct, int serial, int alternate)
1018 char *cp, buffer[BUFSIZ];
1019 CI ci = &ct->c_ctinfo;
1021 /* Check for mhn-show-type/subtype */
1022 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1023 invo_name, ci->ci_type, ci->ci_subtype);
1024 if ((cp = context_find (buffer)) && *cp != '\0')
1025 return show_content_aux (ct, serial, alternate, cp, NULL);
1027 /* Check for mhn-show-type */
1028 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1029 if ((cp = context_find (buffer)) && *cp != '\0')
1030 return show_content_aux (ct, serial, alternate, cp, NULL);
1032 if ((cp = ct->c_showproc))
1033 return show_content_aux (ct, serial, alternate, cp, NULL);
1035 /* default method for message/rfc822 */
1036 if (ct->c_subtype == MESSAGE_RFC822) {
1037 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1038 return show_content_aux (ct, serial, alternate, cp, NULL);
1041 /* complain if we are not a part of a multipart/alternative */
1043 content_error (NULL, ct, "don't know how to display content");
1050 * Show content of type "message/partial".
1054 show_partial (CT ct, int serial, int alternate)
1056 NMH_UNUSED (serial);
1057 NMH_UNUSED (alternate);
1059 content_error (NULL, ct,
1060 "in order to display this message, you must reassemble it");
1066 * Show content of type "message/external".
1068 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1072 show_external (CT ct, int serial, int alternate)
1074 struct exbody *e = (struct exbody *) ct->c_ctparams;
1075 CT p = e->eb_content;
1077 if (!type_ok (p, 0))
1080 return show_switch (p, serial, alternate);
1083 content_error (NULL, p, "don't know how to display content");
1095 siglongjmp (intrenv, DONE);