2 * mhshowsbr.c -- routines to display the contents of MIME messages
4 * This code is Copyright (c) 2002, by the authors of nmh. See the
5 * COPYRIGHT file in the root directory of the nmh distribution for
6 * complete copyright information.
11 #include <h/signals.h>
19 #include <h/mhparse.h>
22 #ifdef HAVE_SYS_WAIT_H
23 # include <sys/wait.h>
27 * Just use sigjmp/longjmp on older machines that
28 * don't have sigsetjmp/siglongjmp.
30 #ifndef HAVE_SIGSETJMP
31 # define sigjmp_buf jmp_buf
32 # define sigsetjmp(env,mask) setjmp(env)
33 # define siglongjmp(env,val) longjmp(env,val)
44 /* flags for moreproc/header display */
50 static sigjmp_buf intrenv;
54 int SOprintf (char *, ...);
60 int part_ok (CT, int);
61 int type_ok (CT, int);
62 void content_error (char *, CT, char *, ...);
63 void flush_errors (void);
66 int list_switch (CT, int, int, int, int);
67 int list_content (CT, int, int, int, int);
72 void show_all_messages (CT *);
73 int show_content_aux (CT, int, int, char *, char *);
78 static void show_single_message (CT, char *);
79 static void DisplayMsgHeader (CT, char *);
80 static int show_switch (CT, int, int);
81 static int show_content (CT, int, int);
82 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
83 static int show_text (CT, int, int);
84 static int show_multi (CT, int, int);
85 static int show_multi_internal (CT, int, int);
86 static int show_multi_aux (CT, int, int, char *);
87 static int show_message_rfc822 (CT, int, int);
88 static int show_partial (CT, int, int);
89 static int show_external (CT, int, int);
90 static RETSIGTYPE intrser (int);
94 * Top level entry point to show/display a group of messages
98 show_all_messages (CT *cts)
103 * If form is not specified, then get default form
104 * for showing headers of MIME messages.
107 formsw = getcpy (etcpath ("mhl.headers"));
110 * If form is "mhl.null", suppress display of header.
112 if (!strcmp (formsw, "mhl.null"))
115 for (ctp = cts; *ctp; ctp++) {
118 /* if top-level type is ok, then display message */
120 show_single_message (ct, formsw);
126 * Entry point to show/display a single message
130 show_single_message (CT ct, char *form)
134 #ifdef HAVE_UNION_WAIT
140 /* Allow user executable bit so that temporary directories created by
141 * the viewer (e.g., lynx) are going to be accessible */
142 umask (ct->c_umask & ~(0100));
145 * If you have a format file, then display
146 * the message headers.
149 DisplayMsgHeader(ct, form);
153 /* Show the body of the message */
154 show_switch (ct, 1, 0);
160 if (ct->c_ceclosefnx)
161 (*ct->c_ceclosefnx) (ct);
163 /* block a few signals */
165 sigaddset (&set, SIGHUP);
166 sigaddset (&set, SIGINT);
167 sigaddset (&set, SIGQUIT);
168 sigaddset (&set, SIGTERM);
169 SIGPROCMASK (SIG_BLOCK, &set, &oset);
171 while (wait (&status) != NOTOK) {
172 #ifdef HAVE_UNION_WAIT
173 pidcheck (status.w_status);
180 /* reset the signal mask */
181 SIGPROCMASK (SIG_SETMASK, &oset, &set);
189 * Use the mhlproc to show the header fields
193 DisplayMsgHeader (CT ct, char *form)
200 vec[vecp++] = r1bindex (mhlproc, '/');
201 vec[vecp++] = "-form";
203 vec[vecp++] = "-nobody";
204 vec[vecp++] = ct->c_file;
207 * If we've specified -(no)moreproc,
208 * then just pass that along.
211 vec[vecp++] = "-nomoreproc";
213 vec[vecp++] = "-moreproc";
214 vec[vecp++] = progsw;
220 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
225 adios ("fork", "unable to");
229 execvp (mhlproc, vec);
230 fprintf (stderr, "unable to exec ");
243 * Switching routine. Call the correct routine
244 * based on content type.
248 show_switch (CT ct, int serial, int alternate)
250 switch (ct->c_type) {
252 return show_multi (ct, serial, alternate);
256 switch (ct->c_subtype) {
257 case MESSAGE_PARTIAL:
258 return show_partial (ct, serial, alternate);
261 case MESSAGE_EXTERNAL:
262 return show_external (ct, serial, alternate);
267 return show_message_rfc822 (ct, serial, alternate);
273 return show_text (ct, serial, alternate);
280 return show_content (ct, serial, alternate);
284 adios (NULL, "unknown content type %d", ct->c_type);
288 return 0; /* NOT REACHED */
293 * Generic method for displaying content
297 show_content (CT ct, int serial, int alternate)
299 char *cp, buffer[BUFSIZ];
300 CI ci = &ct->c_ctinfo;
302 /* Check for mhshow-show-type/subtype */
303 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
304 invo_name, ci->ci_type, ci->ci_subtype);
305 if ((cp = context_find (buffer)) && *cp != '\0')
306 return show_content_aux (ct, serial, alternate, cp, NULL);
308 /* Check for mhshow-show-type */
309 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
310 if ((cp = context_find (buffer)) && *cp != '\0')
311 return show_content_aux (ct, serial, alternate, cp, NULL);
313 if ((cp = ct->c_showproc))
314 return show_content_aux (ct, serial, alternate, cp, NULL);
316 /* complain if we are not a part of a multipart/alternative */
318 content_error (NULL, ct, "don't know how to display content");
325 * Parse the display string for displaying generic content
329 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
331 int fd, len, buflen, quoted;
332 int xstdin, xlist, xpause, xtty;
333 char *bp, *pp, *file, buffer[BUFSIZ];
334 CI ci = &ct->c_ctinfo;
336 if (!ct->c_ceopenfnx) {
338 content_error (NULL, ct, "don't know how to decode content");
344 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
346 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
347 return (alternate ? DONE : OK);
355 strncpy (buffer, cp, sizeof(buffer));
359 /* get buffer ready to go */
361 buflen = sizeof(buffer) - 1;
362 bp[0] = bp[buflen] = '\0';
365 /* Now parse display string */
366 for ( ; *cp && buflen > 0; cp++) {
372 /* insert parameters from Content-Type field */
377 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
378 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
388 /* insert content description */
392 s = trimcpy (ct->c_descr);
393 strncpy (bp, s, buflen);
399 /* exclusive execution */
404 /* %e, %f, and stdin is terminal not content */
410 /* insert filename containing content */
411 snprintf (bp, buflen, "'%s'", file);
412 /* since we've quoted the file argument, set things up
413 * to look past it, to avoid problems with the quoting
414 * logic below. (I know, I should figure out what's
415 * broken with the quoting logic, but..)
424 /* %l, and pause prior to displaying content */
429 /* display listing prior to displaying content */
434 /* insert subtype of content */
435 strncpy (bp, ci->ci_subtype, buflen);
439 /* insert character % */
452 /* Did we actually insert something? */
454 /* Insert single quote if not inside quotes already */
455 if (!quoted && buflen) {
457 memmove (pp + 1, pp, len);
462 /* Escape existing quotes */
463 while ((pp = strchr (pp, '\'')) && buflen > 3) {
465 memmove (pp + 3, pp, len);
472 /* If pp is still set, that means we ran out of space. */
475 if (!quoted && buflen) {
492 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
493 /* content_error would provide a more useful error message
494 * here, except that if we got overrun, it probably would
497 fprintf(stderr, "Buffer overflow constructing show command!\n");
501 /* use charset string to modify display method */
502 if (ct->c_termproc) {
505 strncpy (term, buffer, sizeof(term));
506 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
510 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
511 fd, xlist, xpause, xstdin, xtty);
516 * Routine to actually display the content
520 show_content_aux2 (CT ct, int serial, int alternate, char *cracked,
521 char *buffer, int fd, int xlist, int xpause, int xstdin, int xtty)
525 char *vec[4], exec[BUFSIZ + sizeof "exec "];
527 if (debugsw || cracked) {
530 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
533 fprintf (stderr, " part %s", ct->c_partno);
535 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
537 fprintf (stderr, " using command %s\n", buffer);
540 if (xpid < 0 || (xtty && xpid)) {
543 pidcheck(pidwait (xpid, NOTOK));
550 if (ct->c_type == CT_MULTIPART)
551 list_content (ct, -1, 1, 0, 0);
553 list_switch (ct, -1, 1, 0, 0);
555 if (xpause && SOprintf ("Press <return> to show content..."))
556 printf ("Press <return> to show content...");
560 SIGNAL_HANDLER istat;
562 istat = SIGNAL (SIGINT, intrser);
563 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
566 read (fileno (stdout), prompt, sizeof(prompt));
568 SIGNAL (SIGINT, istat);
569 if (intr != OK || prompt[0] == 'n') {
570 (*ct->c_ceclosefnx) (ct);
571 return (alternate ? DONE : NOTOK);
573 if (prompt[0] == 'q') done(OK);
577 snprintf (exec, sizeof(exec), "exec %s", buffer);
586 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
590 advise ("fork", "unable to");
591 (*ct->c_ceclosefnx) (ct);
600 execvp ("/bin/sh", vec);
601 fprintf (stderr, "unable to exec ");
608 ct->c_pid = child_id;
612 pidcheck (pidXwait (child_id, NULL));
616 (*ct->c_ceclosefnx) (ct);
617 return (alternate ? DONE : OK);
623 * show content of type "text"
627 show_text (CT ct, int serial, int alternate)
629 char *cp, buffer[BUFSIZ];
630 CI ci = &ct->c_ctinfo;
632 /* Check for mhshow-show-type/subtype */
633 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
634 invo_name, ci->ci_type, ci->ci_subtype);
635 if ((cp = context_find (buffer)) && *cp != '\0')
636 return show_content_aux (ct, serial, alternate, cp, NULL);
638 /* Check for mhshow-show-type */
639 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
640 if ((cp = context_find (buffer)) && *cp != '\0')
641 return show_content_aux (ct, serial, alternate, cp, NULL);
644 * Use default method if content is text/plain, or if
645 * if it is not a text part of a multipart/alternative
647 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
648 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
649 moreproc && *moreproc ? moreproc : "more");
650 cp = (ct->c_showproc = add (buffer, NULL));
651 return show_content_aux (ct, serial, alternate, cp, NULL);
659 * show message body of type "multipart"
663 show_multi (CT ct, int serial, int alternate)
665 char *cp, buffer[BUFSIZ];
666 CI ci = &ct->c_ctinfo;
668 /* Check for mhshow-show-type/subtype */
669 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
670 invo_name, ci->ci_type, ci->ci_subtype);
671 if ((cp = context_find (buffer)) && *cp != '\0')
672 return show_multi_aux (ct, serial, alternate, cp);
674 /* Check for mhshow-show-type */
675 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
676 if ((cp = context_find (buffer)) && *cp != '\0')
677 return show_multi_aux (ct, serial, alternate, cp);
679 if ((cp = ct->c_showproc))
680 return show_multi_aux (ct, serial, alternate, cp);
683 * Use default method to display this multipart content
684 * if it is not a (nested) part of a multipart/alternative,
685 * or if it is one of the known subtypes of multipart.
687 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
688 return show_multi_internal (ct, serial, alternate);
695 * show message body of subtypes of multipart that
696 * we understand directly (mixed, alternate, etc...)
700 show_multi_internal (CT ct, int serial, int alternate)
702 int alternating, nowalternate, nowserial, result;
703 struct multipart *m = (struct multipart *) ct->c_ctparams;
709 nowalternate = alternate;
711 if (ct->c_subtype == MULTI_PARALLEL) {
712 nowserial = serialsw;
713 } else if (ct->c_subtype == MULTI_ALTERNATE) {
721 * unknown subtypes of multipart (treat as mixed per rfc2046)
726 /* block a few signals */
729 sigaddset (&set, SIGHUP);
730 sigaddset (&set, SIGINT);
731 sigaddset (&set, SIGQUIT);
732 sigaddset (&set, SIGTERM);
733 SIGPROCMASK (SIG_BLOCK, &set, &oset);
737 * alternate -> we are a part inside an multipart/alternative
738 * alternating -> we are a multipart/alternative
741 result = alternate ? NOTOK : OK;
743 for (part = m->mp_parts; part; part = part->mp_next) {
746 if (part_ok (p, 0) && type_ok (p, 0)) {
749 inneresult = show_switch (p, nowserial, nowalternate);
750 switch (inneresult) {
752 if (alternate && !alternating) {
765 alternate = nowalternate = 0;
775 if (alternating && !part) {
777 content_error (NULL, ct, "don't know how to display any of the contents");
782 if (serial && !nowserial) {
785 #ifdef HAVE_UNION_WAIT
792 for (part = m->mp_parts; part; part = part->mp_next) {
796 if (kill (p->c_pid, 0) == NOTOK)
803 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
804 #ifdef HAVE_UNION_WAIT
805 pidcheck (status.w_status);
810 for (part = m->mp_parts; part; part = part->mp_next) {
815 if (p->c_pid == pid) {
826 /* reset the signal mask */
827 SIGPROCMASK (SIG_SETMASK, &oset, &set);
835 * Parse display string for multipart content
836 * and use external program to display it.
840 show_multi_aux (CT ct, int serial, int alternate, char *cp)
842 int len, buflen, quoted;
843 int xlist, xpause, xtty;
844 char *bp, *pp, *file, buffer[BUFSIZ];
845 struct multipart *m = (struct multipart *) ct->c_ctparams;
847 CI ci = &ct->c_ctinfo;
850 for (part = m->mp_parts; part; part = part->mp_next) {
853 if (!p->c_ceopenfnx) {
855 content_error (NULL, p, "don't know how to decode content");
859 if (p->c_storage == NULL) {
861 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
864 /* I'm not sure if this is necessary? */
865 p->c_storage = add (file, NULL);
867 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
868 return (alternate ? DONE : OK);
869 (*p->c_ceclosefnx) (p);
877 /* get buffer ready to go */
879 buflen = sizeof(buffer) - 1;
880 bp[0] = bp[buflen] = '\0';
883 /* Now parse display string */
884 for ( ; *cp && buflen > 0; cp++) {
889 /* insert parameters from Content-Type field */
894 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
895 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
905 /* insert content description */
909 s = trimcpy (ct->c_descr);
910 strncpy (bp, s, buflen);
916 /* exclusive execution */
926 /* insert filename(s) containing content */
930 for (part = m->mp_parts; part; part = part->mp_next) {
933 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
939 /* set our starting pointer back to bp, to avoid
940 * requoting the filenames we just added
947 /* %l, and pause prior to displaying content */
952 /* display listing prior to displaying content */
957 /* insert subtype of content */
958 strncpy (bp, ci->ci_subtype, buflen);
962 /* insert character % */
975 /* Did we actually insert something? */
977 /* Insert single quote if not inside quotes already */
978 if (!quoted && buflen) {
980 memmove (pp + 1, pp, len);
985 /* Escape existing quotes */
986 while ((pp = strchr (pp, '\'')) && buflen > 3) {
988 memmove (pp + 3, pp, len);
995 /* If pp is still set, that means we ran out of space. */
998 if (!quoted && buflen) {
1015 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1016 /* content_error would provide a more useful error message
1017 * here, except that if we got overrun, it probably would
1020 fprintf(stderr, "Buffer overflow constructing show command!\n");
1024 /* use charset string to modify display method */
1025 if (ct->c_termproc) {
1028 strncpy (term, buffer, sizeof(term));
1029 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1032 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1033 NOTOK, xlist, xpause, 0, xtty);
1038 * show content of type "message/rfc822"
1042 show_message_rfc822 (CT ct, int serial, int alternate)
1044 char *cp, buffer[BUFSIZ];
1045 CI ci = &ct->c_ctinfo;
1047 /* Check for mhshow-show-type/subtype */
1048 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1049 invo_name, ci->ci_type, ci->ci_subtype);
1050 if ((cp = context_find (buffer)) && *cp != '\0')
1051 return show_content_aux (ct, serial, alternate, cp, NULL);
1053 /* Check for mhshow-show-type */
1054 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1055 if ((cp = context_find (buffer)) && *cp != '\0')
1056 return show_content_aux (ct, serial, alternate, cp, NULL);
1058 if ((cp = ct->c_showproc))
1059 return show_content_aux (ct, serial, alternate, cp, NULL);
1061 /* default method for message/rfc822 */
1062 if (ct->c_subtype == MESSAGE_RFC822) {
1063 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1064 return show_content_aux (ct, serial, alternate, cp, NULL);
1067 /* complain if we are not a part of a multipart/alternative */
1069 content_error (NULL, ct, "don't know how to display content");
1076 * Show content of type "message/partial".
1080 show_partial (CT ct, int serial, int alternate)
1082 content_error (NULL, ct,
1083 "in order to display this message, you must reassemble it");
1089 * Show content of type "message/external".
1091 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1095 show_external (CT ct, int serial, int alternate)
1097 struct exbody *e = (struct exbody *) ct->c_ctparams;
1098 CT p = e->eb_content;
1100 if (!type_ok (p, 0))
1103 return show_switch (p, serial, alternate);
1106 content_error (NULL, p, "don't know how to display content");
1115 #ifndef RELIABLE_SIGNALS
1116 SIGNAL (SIGINT, intrser);
1120 siglongjmp (intrenv, DONE);