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>
24 * Just use sigjmp/longjmp on older machines that
25 * don't have sigsetjmp/siglongjmp.
27 #ifndef HAVE_SIGSETJMP
28 # define sigjmp_buf jmp_buf
29 # define sigsetjmp(env,mask) setjmp(env)
30 # define siglongjmp(env,val) longjmp(env,val)
41 /* flags for moreproc/header display */
47 static sigjmp_buf intrenv;
51 int SOprintf (char *, ...);
57 int part_ok (CT, int);
58 int type_ok (CT, int);
59 void content_error (char *, CT, char *, ...);
60 void flush_errors (void);
63 int list_switch (CT, int, int, int, int);
64 int list_content (CT, int, int, int, int);
69 void show_all_messages (CT *);
70 int show_content_aux (CT, int, int, char *, char *);
75 static void show_single_message (CT, char *);
76 static void DisplayMsgHeader (CT, char *);
77 static int show_switch (CT, int, int);
78 static int show_content (CT, int, int);
79 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
80 static int show_text (CT, int, int);
81 static int show_multi (CT, int, int);
82 static int show_multi_internal (CT, int, int);
83 static int show_multi_aux (CT, int, int, char *);
84 static int show_message_rfc822 (CT, int, int);
85 static int show_partial (CT, int, int);
86 static int show_external (CT, int, int);
87 static RETSIGTYPE intrser (int);
91 * Top level entry point to show/display a group of messages
95 show_all_messages (CT *cts)
100 * If form is not specified, then get default form
101 * for showing headers of MIME messages.
104 formsw = getcpy (etcpath ("mhl.headers"));
107 * If form is "mhl.null", suppress display of header.
109 if (!strcmp (formsw, "mhl.null"))
112 for (ctp = cts; *ctp; ctp++) {
115 /* if top-level type is ok, then display message */
117 show_single_message (ct, formsw);
123 * Entry point to show/display a single message
127 show_single_message (CT ct, char *form)
133 /* Allow user executable bit so that temporary directories created by
134 * the viewer (e.g., lynx) are going to be accessible */
135 umask (ct->c_umask & ~(0100));
138 * If you have a format file, then display
139 * the message headers.
142 DisplayMsgHeader(ct, form);
146 /* Show the body of the message */
147 show_switch (ct, 1, 0);
153 if (ct->c_ceclosefnx)
154 (*ct->c_ceclosefnx) (ct);
156 /* block a few signals */
158 sigaddset (&set, SIGHUP);
159 sigaddset (&set, SIGINT);
160 sigaddset (&set, SIGQUIT);
161 sigaddset (&set, SIGTERM);
162 SIGPROCMASK (SIG_BLOCK, &set, &oset);
164 while (wait (&status) != NOTOK) {
169 /* reset the signal mask */
170 SIGPROCMASK (SIG_SETMASK, &oset, &set);
178 * Use the mhlproc to show the header fields
182 DisplayMsgHeader (CT ct, char *form)
189 vec[vecp++] = r1bindex (mhlproc, '/');
190 vec[vecp++] = "-form";
192 vec[vecp++] = "-nobody";
193 vec[vecp++] = ct->c_file;
196 * If we've specified -(no)moreproc,
197 * then just pass that along.
200 vec[vecp++] = "-nomoreproc";
202 vec[vecp++] = "-moreproc";
203 vec[vecp++] = progsw;
209 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
214 adios ("fork", "unable to");
218 execvp (mhlproc, vec);
219 fprintf (stderr, "unable to exec ");
232 * Switching routine. Call the correct routine
233 * based on content type.
237 show_switch (CT ct, int serial, int alternate)
239 switch (ct->c_type) {
241 return show_multi (ct, serial, alternate);
245 switch (ct->c_subtype) {
246 case MESSAGE_PARTIAL:
247 return show_partial (ct, serial, alternate);
250 case MESSAGE_EXTERNAL:
251 return show_external (ct, serial, alternate);
256 return show_message_rfc822 (ct, serial, alternate);
262 return show_text (ct, serial, alternate);
269 return show_content (ct, serial, alternate);
273 adios (NULL, "unknown content type %d", ct->c_type);
277 return 0; /* NOT REACHED */
282 * Generic method for displaying content
286 show_content (CT ct, int serial, int alternate)
288 char *cp, buffer[BUFSIZ];
289 CI ci = &ct->c_ctinfo;
291 /* Check for mhn-show-type/subtype */
292 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
293 invo_name, ci->ci_type, ci->ci_subtype);
294 if ((cp = context_find (buffer)) && *cp != '\0')
295 return show_content_aux (ct, serial, alternate, cp, NULL);
297 /* Check for mhn-show-type */
298 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
299 if ((cp = context_find (buffer)) && *cp != '\0')
300 return show_content_aux (ct, serial, alternate, cp, NULL);
302 if ((cp = ct->c_showproc))
303 return show_content_aux (ct, serial, alternate, cp, NULL);
305 /* complain if we are not a part of a multipart/alternative */
307 content_error (NULL, ct, "don't know how to display content");
314 * Parse the display string for displaying generic content
318 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
320 int fd, len, buflen, quoted;
321 int xstdin, xlist, xpause, xtty;
322 char *bp, *pp, *file, buffer[BUFSIZ];
323 CI ci = &ct->c_ctinfo;
325 if (!ct->c_ceopenfnx) {
327 content_error (NULL, ct, "don't know how to decode content");
333 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
335 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
336 return (alternate ? DONE : OK);
344 strncpy (buffer, cp, sizeof(buffer));
348 /* get buffer ready to go */
350 buflen = sizeof(buffer) - 1;
351 bp[0] = bp[buflen] = '\0';
354 /* Now parse display string */
355 for ( ; *cp && buflen > 0; cp++) {
361 /* insert parameters from Content-Type field */
366 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
367 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
377 /* insert content description */
381 s = trimcpy (ct->c_descr);
382 strncpy (bp, s, buflen);
388 /* exclusive execution */
393 /* %e, %f, and stdin is terminal not content */
399 /* insert filename containing content */
400 snprintf (bp, buflen, "'%s'", file);
401 /* since we've quoted the file argument, set things up
402 * to look past it, to avoid problems with the quoting
403 * logic below. (I know, I should figure out what's
404 * broken with the quoting logic, but..)
413 /* %l, and pause prior to displaying content */
418 /* display listing prior to displaying content */
423 /* insert subtype of content */
424 strncpy (bp, ci->ci_subtype, buflen);
428 /* insert character % */
441 /* Did we actually insert something? */
443 /* Insert single quote if not inside quotes already */
444 if (!quoted && buflen) {
446 memmove (pp + 1, pp, len);
451 /* Escape existing quotes */
452 while ((pp = strchr (pp, '\'')) && buflen > 3) {
454 memmove (pp + 3, pp, len);
461 /* If pp is still set, that means we ran out of space. */
464 if (!quoted && buflen) {
481 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
482 /* content_error would provide a more useful error message
483 * here, except that if we got overrun, it probably would
486 fprintf(stderr, "Buffer overflow constructing show command!\n");
490 /* use charset string to modify display method */
491 if (ct->c_termproc) {
494 strncpy (term, buffer, sizeof(term));
495 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
499 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
500 fd, xlist, xpause, xstdin, xtty);
505 * Routine to actually display the content
509 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
510 int fd, int xlist, int xpause, int xstdin, int xtty)
514 char *vec[4], exec[BUFSIZ + sizeof "exec "];
516 if (debugsw || cracked) {
519 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
522 fprintf (stderr, " part %s", ct->c_partno);
524 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
526 fprintf (stderr, " using command %s\n", buffer);
529 if (xpid < 0 || (xtty && xpid)) {
532 pidcheck(pidwait (xpid, NOTOK));
539 if (ct->c_type == CT_MULTIPART)
540 list_content (ct, -1, 1, 0, 0);
542 list_switch (ct, -1, 1, 0, 0);
544 if (xpause && SOprintf ("Press <return> to show content..."))
545 printf ("Press <return> to show content...");
549 SIGNAL_HANDLER istat;
551 istat = SIGNAL (SIGINT, intrser);
552 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
555 read (fileno (stdout), prompt, sizeof(prompt));
557 SIGNAL (SIGINT, istat);
558 if (intr != OK || prompt[0] == 'n') {
559 (*ct->c_ceclosefnx) (ct);
560 return (alternate ? DONE : NOTOK);
562 if (prompt[0] == 'q') done(OK);
566 snprintf (exec, sizeof(exec), "exec %s", buffer);
575 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
579 advise ("fork", "unable to");
580 (*ct->c_ceclosefnx) (ct);
589 execvp ("/bin/sh", vec);
590 fprintf (stderr, "unable to exec ");
597 ct->c_pid = child_id;
601 pidcheck (pidXwait (child_id, NULL));
605 (*ct->c_ceclosefnx) (ct);
606 return (alternate ? DONE : OK);
612 * show content of type "text"
616 show_text (CT ct, int serial, int alternate)
618 char *cp, buffer[BUFSIZ];
619 CI ci = &ct->c_ctinfo;
621 /* Check for mhn-show-type/subtype */
622 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
623 invo_name, ci->ci_type, ci->ci_subtype);
624 if ((cp = context_find (buffer)) && *cp != '\0')
625 return show_content_aux (ct, serial, alternate, cp, NULL);
627 /* Check for mhn-show-type */
628 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
629 if ((cp = context_find (buffer)) && *cp != '\0')
630 return show_content_aux (ct, serial, alternate, cp, NULL);
633 * Use default method if content is text/plain, or if
634 * if it is not a text part of a multipart/alternative
636 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
637 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
638 moreproc && *moreproc ? moreproc : "more");
639 cp = (ct->c_showproc = add (buffer, NULL));
640 return show_content_aux (ct, serial, alternate, cp, NULL);
648 * show message body of type "multipart"
652 show_multi (CT ct, int serial, int alternate)
654 char *cp, buffer[BUFSIZ];
655 CI ci = &ct->c_ctinfo;
657 /* Check for mhn-show-type/subtype */
658 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
659 invo_name, ci->ci_type, ci->ci_subtype);
660 if ((cp = context_find (buffer)) && *cp != '\0')
661 return show_multi_aux (ct, serial, alternate, cp);
663 /* Check for mhn-show-type */
664 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
665 if ((cp = context_find (buffer)) && *cp != '\0')
666 return show_multi_aux (ct, serial, alternate, cp);
668 if ((cp = ct->c_showproc))
669 return show_multi_aux (ct, serial, alternate, cp);
672 * Use default method to display this multipart content
673 * if it is not a (nested) part of a multipart/alternative,
674 * or if it is one of the known subtypes of multipart.
676 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
677 return show_multi_internal (ct, serial, alternate);
684 * show message body of subtypes of multipart that
685 * we understand directly (mixed, alternate, etc...)
689 show_multi_internal (CT ct, int serial, int alternate)
691 int alternating, nowalternate, nowserial, result;
692 struct multipart *m = (struct multipart *) ct->c_ctparams;
698 nowalternate = alternate;
700 if (ct->c_subtype == MULTI_PARALLEL) {
701 nowserial = serialsw;
702 } else if (ct->c_subtype == MULTI_ALTERNATE) {
710 * unknown subtypes of multipart (treat as mixed per rfc2046)
715 /* block a few signals */
718 sigaddset (&set, SIGHUP);
719 sigaddset (&set, SIGINT);
720 sigaddset (&set, SIGQUIT);
721 sigaddset (&set, SIGTERM);
722 SIGPROCMASK (SIG_BLOCK, &set, &oset);
726 * alternate -> we are a part inside an multipart/alternative
727 * alternating -> we are a multipart/alternative
730 result = alternate ? NOTOK : OK;
732 for (part = m->mp_parts; part; part = part->mp_next) {
735 if (part_ok (p, 1) && type_ok (p, 1)) {
738 inneresult = show_switch (p, nowserial, nowalternate);
739 switch (inneresult) {
741 if (alternate && !alternating) {
754 alternate = nowalternate = 0;
764 if (alternating && !part) {
766 content_error (NULL, ct, "don't know how to display any of the contents");
771 if (serial && !nowserial) {
777 for (part = m->mp_parts; part; part = part->mp_next) {
781 if (kill (p->c_pid, 0) == NOTOK)
788 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
791 for (part = m->mp_parts; part; part = part->mp_next) {
796 if (p->c_pid == pid) {
807 /* reset the signal mask */
808 SIGPROCMASK (SIG_SETMASK, &oset, &set);
816 * Parse display string for multipart content
817 * and use external program to display it.
821 show_multi_aux (CT ct, int serial, int alternate, char *cp)
823 int len, buflen, quoted;
824 int xlist, xpause, xtty;
825 char *bp, *pp, *file, buffer[BUFSIZ];
826 struct multipart *m = (struct multipart *) ct->c_ctparams;
828 CI ci = &ct->c_ctinfo;
831 for (part = m->mp_parts; part; part = part->mp_next) {
834 if (!p->c_ceopenfnx) {
836 content_error (NULL, p, "don't know how to decode content");
840 if (p->c_storage == NULL) {
842 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
845 /* I'm not sure if this is necessary? */
846 p->c_storage = add (file, NULL);
848 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
849 return (alternate ? DONE : OK);
850 (*p->c_ceclosefnx) (p);
858 /* get buffer ready to go */
860 buflen = sizeof(buffer) - 1;
861 bp[0] = bp[buflen] = '\0';
864 /* Now parse display string */
865 for ( ; *cp && buflen > 0; cp++) {
870 /* insert parameters from Content-Type field */
875 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
876 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
886 /* insert content description */
890 s = trimcpy (ct->c_descr);
891 strncpy (bp, s, buflen);
897 /* exclusive execution */
907 /* insert filename(s) containing content */
911 for (part = m->mp_parts; part; part = part->mp_next) {
914 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
920 /* set our starting pointer back to bp, to avoid
921 * requoting the filenames we just added
928 /* %l, and pause prior to displaying content */
933 /* display listing prior to displaying content */
938 /* insert subtype of content */
939 strncpy (bp, ci->ci_subtype, buflen);
943 /* insert character % */
956 /* Did we actually insert something? */
958 /* Insert single quote if not inside quotes already */
959 if (!quoted && buflen) {
961 memmove (pp + 1, pp, len);
966 /* Escape existing quotes */
967 while ((pp = strchr (pp, '\'')) && buflen > 3) {
969 memmove (pp + 3, pp, len);
976 /* If pp is still set, that means we ran out of space. */
979 if (!quoted && buflen) {
996 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
997 /* content_error would provide a more useful error message
998 * here, except that if we got overrun, it probably would
1001 fprintf(stderr, "Buffer overflow constructing show command!\n");
1005 /* use charset string to modify display method */
1006 if (ct->c_termproc) {
1009 strncpy (term, buffer, sizeof(term));
1010 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1013 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1014 NOTOK, xlist, xpause, 0, xtty);
1019 * show content of type "message/rfc822"
1023 show_message_rfc822 (CT ct, int serial, int alternate)
1025 char *cp, buffer[BUFSIZ];
1026 CI ci = &ct->c_ctinfo;
1028 /* Check for mhn-show-type/subtype */
1029 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1030 invo_name, ci->ci_type, ci->ci_subtype);
1031 if ((cp = context_find (buffer)) && *cp != '\0')
1032 return show_content_aux (ct, serial, alternate, cp, NULL);
1034 /* Check for mhn-show-type */
1035 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1036 if ((cp = context_find (buffer)) && *cp != '\0')
1037 return show_content_aux (ct, serial, alternate, cp, NULL);
1039 if ((cp = ct->c_showproc))
1040 return show_content_aux (ct, serial, alternate, cp, NULL);
1042 /* default method for message/rfc822 */
1043 if (ct->c_subtype == MESSAGE_RFC822) {
1044 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1045 return show_content_aux (ct, serial, alternate, cp, NULL);
1048 /* complain if we are not a part of a multipart/alternative */
1050 content_error (NULL, ct, "don't know how to display content");
1057 * Show content of type "message/partial".
1061 show_partial (CT ct, int serial, int alternate)
1063 content_error (NULL, ct,
1064 "in order to display this message, you must reassemble it");
1070 * Show content of type "message/external".
1072 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1076 show_external (CT ct, int serial, int alternate)
1078 struct exbody *e = (struct exbody *) ct->c_ctparams;
1079 CT p = e->eb_content;
1081 if (!type_ok (p, 0))
1084 return show_switch (p, serial, alternate);
1087 content_error (NULL, p, "don't know how to display content");
1096 #ifndef RELIABLE_SIGNALS
1097 SIGNAL (SIGINT, intrser);
1101 siglongjmp (intrenv, DONE);