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>
23 #ifdef HAVE_SYS_WAIT_H
24 # include <sys/wait.h>
28 * Just use sigjmp/longjmp on older machines that
29 * don't have sigsetjmp/siglongjmp.
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)
45 /* flags for moreproc/header display */
51 static sigjmp_buf intrenv;
55 int SOprintf (char *, ...);
61 int part_ok (CT, int);
62 int type_ok (CT, int);
63 void content_error (char *, CT, char *, ...);
64 void flush_errors (void);
67 int list_switch (CT, int, int, int, int);
68 int list_content (CT, int, int, int, int);
73 void show_all_messages (CT *);
74 int show_content_aux (CT, int, int, char *, char *);
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);
95 * Top level entry point to show/display a group of messages
99 show_all_messages (CT *cts)
104 * If form is not specified, then get default form
105 * for showing headers of MIME messages.
108 formsw = getcpy (etcpath ("mhl.headers"));
111 * If form is "mhl.null", suppress display of header.
113 if (!strcmp (formsw, "mhl.null"))
116 for (ctp = cts; *ctp; ctp++) {
119 /* if top-level type is ok, then display message */
121 show_single_message (ct, formsw);
127 * Entry point to show/display a single message
131 show_single_message (CT ct, char *form)
135 #ifdef HAVE_UNION_WAIT
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));
146 * If you have a format file, then display
147 * the message headers.
150 DisplayMsgHeader(ct, form);
154 /* Show the body of the message */
155 show_switch (ct, 1, 0);
161 if (ct->c_ceclosefnx)
162 (*ct->c_ceclosefnx) (ct);
164 /* block a few signals */
166 sigaddset (&set, SIGHUP);
167 sigaddset (&set, SIGINT);
168 sigaddset (&set, SIGQUIT);
169 sigaddset (&set, SIGTERM);
170 SIGPROCMASK (SIG_BLOCK, &set, &oset);
172 while (wait (&status) != NOTOK) {
173 #ifdef HAVE_UNION_WAIT
174 pidcheck (status.w_status);
181 /* reset the signal mask */
182 SIGPROCMASK (SIG_SETMASK, &oset, &set);
190 * Use the mhlproc to show the header fields
194 DisplayMsgHeader (CT ct, char *form)
201 vec[vecp++] = r1bindex (mhlproc, '/');
202 vec[vecp++] = "-form";
204 vec[vecp++] = "-nobody";
205 vec[vecp++] = ct->c_file;
208 * If we've specified -(no)moreproc,
209 * then just pass that along.
212 vec[vecp++] = "-nomoreproc";
214 vec[vecp++] = "-moreproc";
215 vec[vecp++] = progsw;
221 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
226 adios ("fork", "unable to");
230 execvp (mhlproc, vec);
231 fprintf (stderr, "unable to exec ");
244 * Switching routine. Call the correct routine
245 * based on content type.
249 show_switch (CT ct, int serial, int alternate)
251 switch (ct->c_type) {
253 return show_multi (ct, serial, alternate);
257 switch (ct->c_subtype) {
258 case MESSAGE_PARTIAL:
259 return show_partial (ct, serial, alternate);
262 case MESSAGE_EXTERNAL:
263 return show_external (ct, serial, alternate);
268 return show_message_rfc822 (ct, serial, alternate);
274 return show_text (ct, serial, alternate);
281 return show_content (ct, serial, alternate);
285 adios (NULL, "unknown content type %d", ct->c_type);
289 return 0; /* NOT REACHED */
294 * Generic method for displaying content
298 show_content (CT ct, int serial, int alternate)
300 char *cp, buffer[BUFSIZ];
301 CI ci = &ct->c_ctinfo;
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);
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);
314 if ((cp = ct->c_showproc))
315 return show_content_aux (ct, serial, alternate, cp, NULL);
317 /* complain if we are not a part of a multipart/alternative */
319 content_error (NULL, ct, "don't know how to display content");
326 * Parse the display string for displaying generic content
330 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
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;
337 if (!ct->c_ceopenfnx) {
339 content_error (NULL, ct, "don't know how to decode content");
345 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
347 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
348 return (alternate ? DONE : OK);
356 strncpy (buffer, cp, sizeof(buffer));
360 /* get buffer ready to go */
362 buflen = sizeof(buffer) - 1;
363 bp[0] = bp[buflen] = '\0';
366 /* Now parse display string */
367 for ( ; *cp && buflen > 0; cp++) {
373 /* insert parameters from Content-Type field */
378 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
379 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
389 /* insert content description */
393 s = trimcpy (ct->c_descr);
394 strncpy (bp, s, buflen);
400 /* exclusive execution */
405 /* %e, %f, and stdin is terminal not content */
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..)
425 /* %l, and pause prior to displaying content */
430 /* display listing prior to displaying content */
435 /* insert subtype of content */
436 strncpy (bp, ci->ci_subtype, buflen);
440 /* insert character % */
453 /* Did we actually insert something? */
455 /* Insert single quote if not inside quotes already */
456 if (!quoted && buflen) {
458 memmove (pp + 1, pp, len);
463 /* Escape existing quotes */
464 while ((pp = strchr (pp, '\'')) && buflen > 3) {
466 memmove (pp + 3, pp, len);
473 /* If pp is still set, that means we ran out of space. */
476 if (!quoted && buflen) {
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
498 fprintf(stderr, "Buffer overflow constructing show command!\n");
502 /* use charset string to modify display method */
503 if (ct->c_termproc) {
506 strncpy (term, buffer, sizeof(term));
507 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
511 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
512 fd, xlist, xpause, xstdin, xtty);
517 * Routine to actually display the content
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)
526 char *vec[4], exec[BUFSIZ + sizeof "exec "];
528 if (debugsw || cracked) {
531 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
534 fprintf (stderr, " part %s", ct->c_partno);
536 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
538 fprintf (stderr, " using command %s\n", buffer);
541 if (xpid < 0 || (xtty && xpid)) {
544 pidcheck(pidwait (xpid, NOTOK));
551 if (ct->c_type == CT_MULTIPART)
552 list_content (ct, -1, 1, 0, 0);
554 list_switch (ct, -1, 1, 0, 0);
556 if (xpause && SOprintf ("Press <return> to show content..."))
557 printf ("Press <return> to show content...");
561 SIGNAL_HANDLER istat;
563 istat = SIGNAL (SIGINT, intrser);
564 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
567 read (fileno (stdout), prompt, sizeof(prompt));
569 SIGNAL (SIGINT, istat);
570 if (intr != OK || prompt[0] == 'n') {
571 (*ct->c_ceclosefnx) (ct);
572 return (alternate ? DONE : NOTOK);
574 if (prompt[0] == 'q') done(OK);
578 snprintf (exec, sizeof(exec), "exec %s", buffer);
587 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
591 advise ("fork", "unable to");
592 (*ct->c_ceclosefnx) (ct);
601 execvp ("/bin/sh", vec);
602 fprintf (stderr, "unable to exec ");
609 ct->c_pid = child_id;
613 pidcheck (pidXwait (child_id, NULL));
617 (*ct->c_ceclosefnx) (ct);
618 return (alternate ? DONE : OK);
624 * show content of type "text"
628 show_text (CT ct, int serial, int alternate)
630 char *cp, buffer[BUFSIZ];
631 CI ci = &ct->c_ctinfo;
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);
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);
645 * Use default method if content is text/plain, or if
646 * if it is not a text part of a multipart/alternative
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);
660 * show message body of type "multipart"
664 show_multi (CT ct, int serial, int alternate)
666 char *cp, buffer[BUFSIZ];
667 CI ci = &ct->c_ctinfo;
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);
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);
680 if ((cp = ct->c_showproc))
681 return show_multi_aux (ct, serial, alternate, cp);
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.
688 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
689 return show_multi_internal (ct, serial, alternate);
696 * show message body of subtypes of multipart that
697 * we understand directly (mixed, alternate, etc...)
701 show_multi_internal (CT ct, int serial, int alternate)
703 int alternating, nowalternate, nowserial, result;
704 struct multipart *m = (struct multipart *) ct->c_ctparams;
710 nowalternate = alternate;
712 if (ct->c_subtype == MULTI_PARALLEL) {
713 nowserial = serialsw;
714 } else if (ct->c_subtype == MULTI_ALTERNATE) {
722 * unknown subtypes of multipart (treat as mixed per rfc2046)
727 /* block a few signals */
730 sigaddset (&set, SIGHUP);
731 sigaddset (&set, SIGINT);
732 sigaddset (&set, SIGQUIT);
733 sigaddset (&set, SIGTERM);
734 SIGPROCMASK (SIG_BLOCK, &set, &oset);
738 * alternate -> we are a part inside an multipart/alternative
739 * alternating -> we are a multipart/alternative
742 result = alternate ? NOTOK : OK;
744 for (part = m->mp_parts; part; part = part->mp_next) {
747 if (part_ok (p, 0) && type_ok (p, 0)) {
750 inneresult = show_switch (p, nowserial, nowalternate);
751 switch (inneresult) {
753 if (alternate && !alternating) {
766 alternate = nowalternate = 0;
776 if (alternating && !part) {
778 content_error (NULL, ct, "don't know how to display any of the contents");
783 if (serial && !nowserial) {
786 #ifdef HAVE_UNION_WAIT
793 for (part = m->mp_parts; part; part = part->mp_next) {
797 if (kill (p->c_pid, 0) == NOTOK)
804 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
805 #ifdef HAVE_UNION_WAIT
806 pidcheck (status.w_status);
811 for (part = m->mp_parts; part; part = part->mp_next) {
816 if (p->c_pid == pid) {
827 /* reset the signal mask */
828 SIGPROCMASK (SIG_SETMASK, &oset, &set);
836 * Parse display string for multipart content
837 * and use external program to display it.
841 show_multi_aux (CT ct, int serial, int alternate, char *cp)
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;
848 CI ci = &ct->c_ctinfo;
851 for (part = m->mp_parts; part; part = part->mp_next) {
854 if (!p->c_ceopenfnx) {
856 content_error (NULL, p, "don't know how to decode content");
860 if (p->c_storage == NULL) {
862 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
865 /* I'm not sure if this is necessary? */
866 p->c_storage = add (file, NULL);
868 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
869 return (alternate ? DONE : OK);
870 (*p->c_ceclosefnx) (p);
878 /* get buffer ready to go */
880 buflen = sizeof(buffer) - 1;
881 bp[0] = bp[buflen] = '\0';
884 /* Now parse display string */
885 for ( ; *cp && buflen > 0; cp++) {
890 /* insert parameters from Content-Type field */
895 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
896 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
906 /* insert content description */
910 s = trimcpy (ct->c_descr);
911 strncpy (bp, s, buflen);
917 /* exclusive execution */
927 /* insert filename(s) containing content */
931 for (part = m->mp_parts; part; part = part->mp_next) {
934 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
940 /* set our starting pointer back to bp, to avoid
941 * requoting the filenames we just added
948 /* %l, and pause prior to displaying content */
953 /* display listing prior to displaying content */
958 /* insert subtype of content */
959 strncpy (bp, ci->ci_subtype, buflen);
963 /* insert character % */
976 /* Did we actually insert something? */
978 /* Insert single quote if not inside quotes already */
979 if (!quoted && buflen) {
981 memmove (pp + 1, pp, len);
986 /* Escape existing quotes */
987 while ((pp = strchr (pp, '\'')) && buflen > 3) {
989 memmove (pp + 3, pp, len);
996 /* If pp is still set, that means we ran out of space. */
999 if (!quoted && buflen) {
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
1021 fprintf(stderr, "Buffer overflow constructing show command!\n");
1025 /* use charset string to modify display method */
1026 if (ct->c_termproc) {
1029 strncpy (term, buffer, sizeof(term));
1030 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1033 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1034 NOTOK, xlist, xpause, 0, xtty);
1039 * show content of type "message/rfc822"
1043 show_message_rfc822 (CT ct, int serial, int alternate)
1045 char *cp, buffer[BUFSIZ];
1046 CI ci = &ct->c_ctinfo;
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);
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);
1059 if ((cp = ct->c_showproc))
1060 return show_content_aux (ct, serial, alternate, cp, NULL);
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);
1068 /* complain if we are not a part of a multipart/alternative */
1070 content_error (NULL, ct, "don't know how to display content");
1077 * Show content of type "message/partial".
1081 show_partial (CT ct, int serial, int alternate)
1083 content_error (NULL, ct,
1084 "in order to display this message, you must reassemble it");
1090 * Show content of type "message/external".
1092 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1096 show_external (CT ct, int serial, int alternate)
1098 struct exbody *e = (struct exbody *) ct->c_ctparams;
1099 CT p = e->eb_content;
1101 if (!type_ok (p, 0))
1104 return show_switch (p, serial, alternate);
1107 content_error (NULL, p, "don't know how to display content");
1116 #ifndef RELIABLE_SIGNALS
1117 SIGNAL (SIGINT, intrser);
1121 siglongjmp (intrenv, DONE);