3 * mhshowsbr.c -- routines to display the contents of MIME messages
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.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
24 #ifdef HAVE_SYS_WAIT_H
25 # include <sys/wait.h>
29 * Just use sigjmp/longjmp on older machines that
30 * don't have sigsetjmp/siglongjmp.
32 #ifndef HAVE_SIGSETJMP
33 # define sigjmp_buf jmp_buf
34 # define sigsetjmp(env,mask) setjmp(env)
35 # define siglongjmp(env,val) longjmp(env,val)
46 /* flags for moreproc/header display */
52 static sigjmp_buf intrenv;
56 int SOprintf (char *, ...);
62 int part_ok (CT, int);
63 int type_ok (CT, int);
64 void content_error (char *, CT, char *, ...);
65 void flush_errors (void);
68 int list_switch (CT, int, int, int, int);
69 int list_content (CT, int, int, int, int);
74 void show_all_messages (CT *);
75 int show_content_aux (CT, int, int, char *, char *);
80 static void show_single_message (CT, char *);
81 static void DisplayMsgHeader (CT, char *);
82 static int show_switch (CT, int, int);
83 static int show_content (CT, int, int);
84 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
85 static int show_text (CT, int, int);
86 static int show_multi (CT, int, int);
87 static int show_multi_internal (CT, int, int);
88 static int show_multi_aux (CT, int, int, char *);
89 static int show_message_rfc822 (CT, int, int);
90 static int show_partial (CT, int, int);
91 static int show_external (CT, int, int);
92 static RETSIGTYPE intrser (int);
96 * Top level entry point to show/display a group of messages
100 show_all_messages (CT *cts)
105 * If form is not specified, then get default form
106 * for showing headers of MIME messages.
109 formsw = getcpy (etcpath ("mhl.headers"));
112 * If form is "mhl.null", suppress display of header.
114 if (!strcmp (formsw, "mhl.null"))
117 for (ctp = cts; *ctp; ctp++) {
120 /* if top-level type is ok, then display message */
122 show_single_message (ct, formsw);
128 * Entry point to show/display a single message
132 show_single_message (CT ct, char *form)
142 /* Allow user executable bit so that temporary directories created by
143 * the viewer (e.g., lynx) are going to be accessible */
144 umask (ct->c_umask & ~(0100));
147 * If you have a format file, then display
148 * the message headers.
151 DisplayMsgHeader(ct, form);
155 /* Show the body of the message */
156 show_switch (ct, 1, 0);
162 if (ct->c_ceclosefnx)
163 (*ct->c_ceclosefnx) (ct);
165 /* block a few signals */
167 sigaddset (&set, SIGHUP);
168 sigaddset (&set, SIGINT);
169 sigaddset (&set, SIGQUIT);
170 sigaddset (&set, SIGTERM);
171 SIGPROCMASK (SIG_BLOCK, &set, &oset);
173 while (wait (&status) != NOTOK) {
177 pidcheck (status.w_status);
182 /* reset the signal mask */
183 SIGPROCMASK (SIG_SETMASK, &oset, &set);
191 * Use the mhlproc to show the header fields
195 DisplayMsgHeader (CT ct, char *form)
202 vec[vecp++] = r1bindex (mhlproc, '/');
203 vec[vecp++] = "-form";
205 vec[vecp++] = "-nobody";
206 vec[vecp++] = ct->c_file;
209 * If we've specified -(no)moreproc,
210 * then just pass that along.
213 vec[vecp++] = "-nomoreproc";
215 vec[vecp++] = "-moreproc";
216 vec[vecp++] = progsw;
222 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
227 adios ("fork", "unable to");
231 execvp (mhlproc, vec);
232 fprintf (stderr, "unable to exec ");
245 * Switching routine. Call the correct routine
246 * based on content type.
250 show_switch (CT ct, int serial, int alternate)
252 switch (ct->c_type) {
254 return show_multi (ct, serial, alternate);
258 switch (ct->c_subtype) {
259 case MESSAGE_PARTIAL:
260 return show_partial (ct, serial, alternate);
263 case MESSAGE_EXTERNAL:
264 return show_external (ct, serial, alternate);
269 return show_message_rfc822 (ct, serial, alternate);
275 return show_text (ct, serial, alternate);
282 return show_content (ct, serial, alternate);
286 adios (NULL, "unknown content type %d", ct->c_type);
290 return 0; /* NOT REACHED */
295 * Generic method for displaying content
299 show_content (CT ct, int serial, int alternate)
301 char *cp, buffer[BUFSIZ];
302 CI ci = &ct->c_ctinfo;
304 /* Check for mhn-show-type/subtype */
305 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
306 invo_name, ci->ci_type, ci->ci_subtype);
307 if ((cp = context_find (buffer)) && *cp != '\0')
308 return show_content_aux (ct, serial, alternate, cp, NULL);
310 /* Check for mhn-show-type */
311 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
312 if ((cp = context_find (buffer)) && *cp != '\0')
313 return show_content_aux (ct, serial, alternate, cp, NULL);
315 if ((cp = ct->c_showproc))
316 return show_content_aux (ct, serial, alternate, cp, NULL);
318 /* complain if we are not a part of a multipart/alternative */
320 content_error (NULL, ct, "don't know how to display content");
327 * Parse the display string for displaying generic content
331 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
333 int fd, len, buflen, quoted;
334 int xstdin, xlist, xpause, xtty;
335 char *bp, *pp, *file, buffer[BUFSIZ];
336 CI ci = &ct->c_ctinfo;
338 if (!ct->c_ceopenfnx) {
340 content_error (NULL, ct, "don't know how to decode content");
346 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
348 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
349 return (alternate ? DONE : OK);
357 strncpy (buffer, cp, sizeof(buffer));
361 /* get buffer ready to go */
363 buflen = sizeof(buffer) - 1;
364 bp[0] = bp[buflen] = '\0';
367 /* Now parse display string */
368 for ( ; *cp && buflen > 0; cp++) {
374 /* insert parameters from Content-Type field */
379 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
380 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
390 /* insert content description */
394 s = trimcpy (ct->c_descr);
395 strncpy (bp, s, buflen);
401 /* exclusive execution */
406 /* %e, %f, and stdin is terminal not content */
412 /* insert filename containing content */
413 snprintf (bp, buflen, "'%s'", file);
414 /* since we've quoted the file argument, set things up
415 * to look past it, to avoid problems with the quoting
416 * logic below. (I know, I should figure out what's
417 * broken with the quoting logic, but..)
426 /* %l, and pause prior to displaying content */
431 /* display listing prior to displaying content */
436 /* insert subtype of content */
437 strncpy (bp, ci->ci_subtype, buflen);
441 /* insert character % */
454 /* Did we actually insert something? */
456 /* Insert single quote if not inside quotes already */
457 if (!quoted && buflen) {
459 memmove (pp + 1, pp, len);
464 /* Escape existing quotes */
465 while ((pp = strchr (pp, '\'')) && buflen > 3) {
467 memmove (pp + 3, pp, len);
474 /* If pp is still set, that means we ran out of space. */
477 if (!quoted && buflen) {
494 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
495 /* content_error would provide a more useful error message
496 * here, except that if we got overrun, it probably would
499 fprintf(stderr, "Buffer overflow constructing show command!\n");
503 /* use charset string to modify display method */
504 if (ct->c_termproc) {
507 strncpy (term, buffer, sizeof(term));
508 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
512 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
513 fd, xlist, xpause, xstdin, xtty);
518 * Routine to actually display the content
522 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
523 int fd, int xlist, int xpause, int xstdin, int xtty)
527 char *vec[4], exec[BUFSIZ + sizeof "exec "];
529 if (debugsw || cracked) {
532 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
535 fprintf (stderr, " part %s", ct->c_partno);
537 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
539 fprintf (stderr, " using command %s\n", buffer);
542 if (xpid < 0 || (xtty && xpid)) {
545 pidcheck(pidwait (xpid, NOTOK));
552 if (ct->c_type == CT_MULTIPART)
553 list_content (ct, -1, 1, 0, 0);
555 list_switch (ct, -1, 1, 0, 0);
557 if (xpause && SOprintf ("Press <return> to show content..."))
558 printf ("Press <return> to show content...");
562 SIGNAL_HANDLER istat;
564 istat = SIGNAL (SIGINT, intrser);
565 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
568 read (fileno (stdout), prompt, sizeof(prompt));
570 SIGNAL (SIGINT, istat);
571 if (intr != OK || prompt[0] == 'n') {
572 (*ct->c_ceclosefnx) (ct);
573 return (alternate ? DONE : NOTOK);
575 if (prompt[0] == 'q') done(OK);
579 snprintf (exec, sizeof(exec), "exec %s", buffer);
588 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
592 advise ("fork", "unable to");
593 (*ct->c_ceclosefnx) (ct);
602 execvp ("/bin/sh", vec);
603 fprintf (stderr, "unable to exec ");
610 ct->c_pid = child_id;
614 pidcheck (pidXwait (child_id, NULL));
618 (*ct->c_ceclosefnx) (ct);
619 return (alternate ? DONE : OK);
625 * show content of type "text"
629 show_text (CT ct, int serial, int alternate)
631 char *cp, buffer[BUFSIZ];
632 CI ci = &ct->c_ctinfo;
634 /* Check for mhn-show-type/subtype */
635 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
636 invo_name, ci->ci_type, ci->ci_subtype);
637 if ((cp = context_find (buffer)) && *cp != '\0')
638 return show_content_aux (ct, serial, alternate, cp, NULL);
640 /* Check for mhn-show-type */
641 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
642 if ((cp = context_find (buffer)) && *cp != '\0')
643 return show_content_aux (ct, serial, alternate, cp, NULL);
646 * Use default method if content is text/plain, or if
647 * if it is not a text part of a multipart/alternative
649 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
650 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
651 moreproc && *moreproc ? moreproc : "more");
652 cp = (ct->c_showproc = add (buffer, NULL));
653 return show_content_aux (ct, serial, alternate, cp, NULL);
661 * show message body of type "multipart"
665 show_multi (CT ct, int serial, int alternate)
667 char *cp, buffer[BUFSIZ];
668 CI ci = &ct->c_ctinfo;
670 /* Check for mhn-show-type/subtype */
671 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
672 invo_name, ci->ci_type, ci->ci_subtype);
673 if ((cp = context_find (buffer)) && *cp != '\0')
674 return show_multi_aux (ct, serial, alternate, cp);
676 /* Check for mhn-show-type */
677 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
678 if ((cp = context_find (buffer)) && *cp != '\0')
679 return show_multi_aux (ct, serial, alternate, cp);
681 if ((cp = ct->c_showproc))
682 return show_multi_aux (ct, serial, alternate, cp);
685 * Use default method to display this multipart content
686 * if it is not a (nested) part of a multipart/alternative,
687 * or if it is one of the known subtypes of multipart.
689 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
690 return show_multi_internal (ct, serial, alternate);
697 * show message body of subtypes of multipart that
698 * we understand directly (mixed, alternate, etc...)
702 show_multi_internal (CT ct, int serial, int alternate)
704 int alternating, nowalternate, nowserial, result;
705 struct multipart *m = (struct multipart *) ct->c_ctparams;
711 nowalternate = alternate;
713 if (ct->c_subtype == MULTI_PARALLEL) {
714 nowserial = serialsw;
715 } else if (ct->c_subtype == MULTI_ALTERNATE) {
723 * unknown subtypes of multipart (treat as mixed per rfc2046)
728 /* block a few signals */
731 sigaddset (&set, SIGHUP);
732 sigaddset (&set, SIGINT);
733 sigaddset (&set, SIGQUIT);
734 sigaddset (&set, SIGTERM);
735 SIGPROCMASK (SIG_BLOCK, &set, &oset);
739 * alternate -> we are a part inside an multipart/alternative
740 * alternating -> we are a multipart/alternative
743 result = alternate ? NOTOK : OK;
745 for (part = m->mp_parts; part; part = part->mp_next) {
748 if (part_ok (p, 0) && type_ok (p, 0)) {
751 inneresult = show_switch (p, nowserial, nowalternate);
752 switch (inneresult) {
754 if (alternate && !alternating) {
767 alternate = nowalternate = 0;
777 if (alternating && !part) {
779 content_error (NULL, ct, "don't know how to display any of the contents");
784 if (serial && !nowserial) {
794 for (part = m->mp_parts; part; part = part->mp_next) {
798 if (kill (p->c_pid, 0) == NOTOK)
805 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
809 pidcheck (status.w_status);
812 for (part = m->mp_parts; part; part = part->mp_next) {
817 if (p->c_pid == pid) {
828 /* reset the signal mask */
829 SIGPROCMASK (SIG_SETMASK, &oset, &set);
837 * Parse display string for multipart content
838 * and use external program to display it.
842 show_multi_aux (CT ct, int serial, int alternate, char *cp)
844 int len, buflen, quoted;
845 int xlist, xpause, xtty;
846 char *bp, *pp, *file, buffer[BUFSIZ];
847 struct multipart *m = (struct multipart *) ct->c_ctparams;
849 CI ci = &ct->c_ctinfo;
852 for (part = m->mp_parts; part; part = part->mp_next) {
855 if (!p->c_ceopenfnx) {
857 content_error (NULL, p, "don't know how to decode content");
861 if (p->c_storage == NULL) {
863 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
866 /* I'm not sure if this is necessary? */
867 p->c_storage = add (file, NULL);
869 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
870 return (alternate ? DONE : OK);
871 (*p->c_ceclosefnx) (p);
879 /* get buffer ready to go */
881 buflen = sizeof(buffer) - 1;
882 bp[0] = bp[buflen] = '\0';
885 /* Now parse display string */
886 for ( ; *cp && buflen > 0; cp++) {
891 /* insert parameters from Content-Type field */
896 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
897 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
907 /* insert content description */
911 s = trimcpy (ct->c_descr);
912 strncpy (bp, s, buflen);
918 /* exclusive execution */
928 /* insert filename(s) containing content */
932 for (part = m->mp_parts; part; part = part->mp_next) {
935 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
941 /* set our starting pointer back to bp, to avoid
942 * requoting the filenames we just added
949 /* %l, and pause prior to displaying content */
954 /* display listing prior to displaying content */
959 /* insert subtype of content */
960 strncpy (bp, ci->ci_subtype, buflen);
964 /* insert character % */
977 /* Did we actually insert something? */
979 /* Insert single quote if not inside quotes already */
980 if (!quoted && buflen) {
982 memmove (pp + 1, pp, len);
987 /* Escape existing quotes */
988 while ((pp = strchr (pp, '\'')) && buflen > 3) {
990 memmove (pp + 3, pp, len);
997 /* If pp is still set, that means we ran out of space. */
1000 if (!quoted && buflen) {
1017 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1018 /* content_error would provide a more useful error message
1019 * here, except that if we got overrun, it probably would
1022 fprintf(stderr, "Buffer overflow constructing show command!\n");
1026 /* use charset string to modify display method */
1027 if (ct->c_termproc) {
1030 strncpy (term, buffer, sizeof(term));
1031 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1034 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1035 NOTOK, xlist, xpause, 0, xtty);
1040 * show content of type "message/rfc822"
1044 show_message_rfc822 (CT ct, int serial, int alternate)
1046 char *cp, buffer[BUFSIZ];
1047 CI ci = &ct->c_ctinfo;
1049 /* Check for mhn-show-type/subtype */
1050 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1051 invo_name, ci->ci_type, ci->ci_subtype);
1052 if ((cp = context_find (buffer)) && *cp != '\0')
1053 return show_content_aux (ct, serial, alternate, cp, NULL);
1055 /* Check for mhn-show-type */
1056 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1057 if ((cp = context_find (buffer)) && *cp != '\0')
1058 return show_content_aux (ct, serial, alternate, cp, NULL);
1060 if ((cp = ct->c_showproc))
1061 return show_content_aux (ct, serial, alternate, cp, NULL);
1063 /* default method for message/rfc822 */
1064 if (ct->c_subtype == MESSAGE_RFC822) {
1065 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1066 return show_content_aux (ct, serial, alternate, cp, NULL);
1069 /* complain if we are not a part of a multipart/alternative */
1071 content_error (NULL, ct, "don't know how to display content");
1078 * Show content of type "message/partial".
1082 show_partial (CT ct, int serial, int alternate)
1084 content_error (NULL, ct,
1085 "in order to display this message, you must reassemble it");
1091 * Show content of type "message/external".
1093 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1097 show_external (CT ct, int serial, int alternate)
1099 struct exbody *e = (struct exbody *) ct->c_ctparams;
1100 CT p = e->eb_content;
1102 if (!type_ok (p, 0))
1105 return show_switch (p, serial, alternate);
1108 content_error (NULL, p, "don't know how to display content");
1117 #ifndef RELIABLE_SIGNALS
1118 SIGNAL (SIGINT, intrser);
1122 siglongjmp (intrenv, DONE);