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 *, ...);
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 *,
83 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
142 ** Allow user executable bit so that temporary directories created by
143 ** the viewer (e.g., lynx) are going to be accessible
145 umask(ct->c_umask & ~(0100));
148 ** If you have a format file, then display
149 ** the message headers.
152 DisplayMsgHeader(ct, form);
156 /* Show the body of the message */
157 show_switch(ct, 1, 0);
163 if (ct->c_ceclosefnx)
164 (*ct->c_ceclosefnx) (ct);
166 /* block a few signals */
168 sigaddset(&set, SIGHUP);
169 sigaddset(&set, SIGINT);
170 sigaddset(&set, SIGQUIT);
171 sigaddset(&set, SIGTERM);
172 SIGPROCMASK(SIG_BLOCK, &set, &oset);
174 while (wait(&status) != NOTOK) {
175 #ifdef HAVE_UNION_WAIT
176 pidcheck(status.w_status);
183 /* reset the signal mask */
184 SIGPROCMASK(SIG_SETMASK, &oset, &set);
192 ** Use the mhlproc to show the header fields
196 DisplayMsgHeader(CT ct, char *form)
203 vec[vecp++] = mhbasename(mhlproc);
204 vec[vecp++] = "-form";
206 vec[vecp++] = "-nobody";
207 vec[vecp++] = ct->c_file;
210 ** If we've specified -(no)moreproc,
211 ** then just pass that along.
214 vec[vecp++] = "-nomoreproc";
216 vec[vecp++] = "-moreproc";
217 vec[vecp++] = progsw;
223 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
228 adios("fork", "unable to");
232 execvp(mhlproc, vec);
233 fprintf(stderr, "unable to exec ");
246 ** Switching routine. Call the correct routine
247 ** based on content type.
251 show_switch(CT ct, int serial, int alternate)
253 switch (ct->c_type) {
255 return show_multi(ct, serial, alternate);
259 switch (ct->c_subtype) {
260 case MESSAGE_PARTIAL:
261 return show_partial(ct, serial,
265 case MESSAGE_EXTERNAL:
266 return show_external(ct, serial,
272 return show_message_rfc822(ct, serial,
279 return show_text(ct, serial, alternate);
286 return show_content(ct, serial, alternate);
290 adios(NULL, "unknown content type %d", ct->c_type);
294 return 0; /* NOT REACHED */
299 ** Generic method for displaying content
303 show_content(CT ct, int serial, int alternate)
305 char *cp, buffer[BUFSIZ];
306 CI ci = &ct->c_ctinfo;
308 /* Check for mhshow-show-type/subtype */
309 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
310 invo_name, ci->ci_type, ci->ci_subtype);
311 if ((cp = context_find(buffer)) && *cp != '\0')
312 return show_content_aux(ct, serial, alternate, cp, NULL);
314 /* Check for mhshow-show-type */
315 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
316 if ((cp = context_find(buffer)) && *cp != '\0')
317 return show_content_aux(ct, serial, alternate, cp, NULL);
319 if ((cp = ct->c_showproc))
320 return show_content_aux(ct, serial, alternate, cp, NULL);
322 /* complain if we are not a part of a multipart/alternative */
324 content_error(NULL, ct, "don't know how to display content");
331 ** Parse the display string for displaying generic content
335 show_content_aux(CT ct, int serial, int alternate, char *cp, char *cracked)
337 int fd, len, buflen, quoted;
338 int xstdin, xlist, xpause, xtty;
339 char *bp, *pp, *file, buffer[BUFSIZ];
340 CI ci = &ct->c_ctinfo;
342 if (!ct->c_ceopenfnx) {
344 content_error(NULL, ct,
345 "don't know how to decode content");
351 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
353 if (ct->c_showproc && !strcmp(ct->c_showproc, "true"))
354 return (alternate ? DONE : OK);
362 strncpy(buffer, cp, sizeof(buffer));
366 /* get buffer ready to go */
368 buflen = sizeof(buffer) - 1;
369 bp[0] = bp[buflen] = '\0';
372 /* Now parse display string */
373 for ( ; *cp && buflen > 0; cp++) {
379 /* insert parameters from Content-Type field */
384 for (ap = ci->ci_attrs, ep = ci->ci_values;
386 snprintf(bp, buflen, "%s%s=\"%s\"",
397 /* insert content description */
401 s = trimcpy(ct->c_descr);
402 strncpy(bp, s, buflen);
408 /* exclusive execution */
413 /* %e, %f, and stdin is terminal not content */
419 /* insert filename containing content */
420 snprintf(bp, buflen, "'%s'", file);
422 ** since we've quoted the file argument,
423 ** set things up to look past it, to avoid
424 ** problems with the quoting logic below.
425 ** (I know, I should figure out what's
426 ** broken with the quoting logic, but..)
435 /* %l, and pause prior to displaying content */
441 ** display listing prior to displaying content
447 /* insert subtype of content */
448 strncpy(bp, ci->ci_subtype, buflen);
452 /* insert character % */
465 /* Did we actually insert something? */
468 ** Insert single quote if not inside quotes
471 if (!quoted && buflen) {
473 memmove(pp + 1, pp, len);
478 /* Escape existing quotes */
479 while ((pp = strchr(pp, '\'')) && buflen > 3) {
481 memmove(pp + 3, pp, len);
489 ** If pp is still set, that means we ran
494 if (!quoted && buflen) {
511 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
513 ** content_error would provide a more useful error message
514 ** here, except that if we got overrun, it probably would
517 fprintf(stderr, "Buffer overflow constructing show command!\n");
521 /* use charset string to modify display method */
522 if (ct->c_termproc) {
525 strncpy(term, buffer, sizeof(term));
526 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
530 return show_content_aux2(ct, serial, alternate, cracked, buffer,
531 fd, xlist, xpause, xstdin, xtty);
536 ** Routine to actually display the content
540 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
541 char *buffer, int fd, int xlist, int xpause, int xstdin, int xtty)
545 char *vec[4], exec[BUFSIZ + sizeof "exec "];
547 if (debugsw || cracked) {
550 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
553 fprintf(stderr, " part %s", ct->c_partno);
555 fprintf(stderr, " using command (cd %s; %s)\n",
558 fprintf(stderr, " using command %s\n", buffer);
561 if (xpid < 0 || (xtty && xpid)) {
564 pidcheck(pidwait(xpid, NOTOK));
571 if (ct->c_type == CT_MULTIPART)
572 list_content(ct, -1, 1, 0, 0);
574 list_switch(ct, -1, 1, 0, 0);
576 if (xpause && SOprintf("Press <return> to show content..."))
577 printf("Press <return> to show content...");
581 SIGNAL_HANDLER istat;
583 istat = SIGNAL(SIGINT, intrser);
584 if ((intr = sigsetjmp(intrenv, 1)) == OK) {
587 read(fileno(stdout), prompt, sizeof(prompt));
589 SIGNAL(SIGINT, istat);
590 if (intr != OK || prompt[0] == 'n') {
591 (*ct->c_ceclosefnx) (ct);
592 return (alternate ? DONE : NOTOK);
594 if (prompt[0] == 'q') done(OK);
598 snprintf(exec, sizeof(exec), "exec %s", buffer);
607 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
611 advise("fork", "unable to");
612 (*ct->c_ceclosefnx) (ct);
621 execvp("/bin/sh", vec);
622 fprintf(stderr, "unable to exec ");
629 ct->c_pid = child_id;
633 pidcheck(pidXwait(child_id, NULL));
637 (*ct->c_ceclosefnx) (ct);
638 return (alternate ? DONE : OK);
644 ** show content of type "text"
648 show_text(CT ct, int serial, int alternate)
650 char *cp, buffer[BUFSIZ];
651 CI ci = &ct->c_ctinfo;
653 /* Check for mhshow-show-type/subtype */
654 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
655 invo_name, ci->ci_type, ci->ci_subtype);
656 if ((cp = context_find(buffer)) && *cp != '\0')
657 return show_content_aux(ct, serial, alternate, cp, NULL);
659 /* Check for mhshow-show-type */
660 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
661 if ((cp = context_find(buffer)) && *cp != '\0')
662 return show_content_aux(ct, serial, alternate, cp, NULL);
665 ** Use default method if content is text/plain, or if
666 ** if it is not a text part of a multipart/alternative
668 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
669 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'",
670 progsw ? progsw : moreproc && *moreproc ?
672 cp = (ct->c_showproc = getcpy(buffer));
673 return show_content_aux(ct, serial, alternate, cp, NULL);
681 ** show message body of type "multipart"
685 show_multi(CT ct, int serial, int alternate)
687 char *cp, buffer[BUFSIZ];
688 CI ci = &ct->c_ctinfo;
690 /* Check for mhshow-show-type/subtype */
691 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
692 invo_name, ci->ci_type, ci->ci_subtype);
693 if ((cp = context_find(buffer)) && *cp != '\0')
694 return show_multi_aux(ct, serial, alternate, cp);
696 /* Check for mhshow-show-type */
697 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
698 if ((cp = context_find(buffer)) && *cp != '\0')
699 return show_multi_aux(ct, serial, alternate, cp);
701 if ((cp = ct->c_showproc))
702 return show_multi_aux(ct, serial, alternate, cp);
705 ** Use default method to display this multipart content
706 ** if it is not a (nested) part of a multipart/alternative,
707 ** or if it is one of the known subtypes of multipart.
709 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
710 return show_multi_internal(ct, serial, alternate);
717 ** show message body of subtypes of multipart that
718 ** we understand directly (mixed, alternate, etc...)
722 show_multi_internal(CT ct, int serial, int alternate)
724 int alternating, nowalternate, nowserial, result;
725 struct multipart *m = (struct multipart *) ct->c_ctparams;
731 nowalternate = alternate;
733 if (ct->c_subtype == MULTI_PARALLEL) {
734 nowserial = serialsw;
735 } else if (ct->c_subtype == MULTI_ALTERNATE) {
743 ** unknown subtypes of multipart (treat as mixed per rfc2046)
748 /* block a few signals */
751 sigaddset(&set, SIGHUP);
752 sigaddset(&set, SIGINT);
753 sigaddset(&set, SIGQUIT);
754 sigaddset(&set, SIGTERM);
755 SIGPROCMASK(SIG_BLOCK, &set, &oset);
759 ** alternate -> we are a part inside an multipart/alternative
760 ** alternating -> we are a multipart/alternative
763 result = alternate ? NOTOK : OK;
765 for (part = m->mp_parts; part; part = part->mp_next) {
768 if (part_ok(p, 0) && type_ok(p, 0)) {
771 inneresult = show_switch(p, nowserial, nowalternate);
772 switch (inneresult) {
774 if (alternate && !alternating) {
787 alternate = nowalternate = 0;
797 if (alternating && !part) {
799 content_error(NULL, ct, "don't know how to display any of the contents");
804 if (serial && !nowserial) {
807 #ifdef HAVE_UNION_WAIT
814 for (part = m->mp_parts; part; part = part->mp_next) {
818 if (kill(p->c_pid, 0) == NOTOK)
825 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
826 #ifdef HAVE_UNION_WAIT
827 pidcheck(status.w_status);
832 for (part = m->mp_parts; part; part = part->mp_next) {
837 if (p->c_pid == pid) {
848 /* reset the signal mask */
849 SIGPROCMASK(SIG_SETMASK, &oset, &set);
857 ** Parse display string for multipart content
858 ** and use external program to display it.
862 show_multi_aux(CT ct, int serial, int alternate, char *cp)
864 int len, buflen, quoted;
865 int xlist, xpause, xtty;
866 char *bp, *pp, *file, buffer[BUFSIZ];
867 struct multipart *m = (struct multipart *) ct->c_ctparams;
869 CI ci = &ct->c_ctinfo;
872 for (part = m->mp_parts; part; part = part->mp_next) {
875 if (!p->c_ceopenfnx) {
877 content_error(NULL, p, "don't know how to decode content");
881 if (p->c_storage == NULL) {
883 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
886 /* I'm not sure if this is necessary? */
887 p->c_storage = getcpy(file);
889 if (p->c_showproc && !strcmp(p->c_showproc, "true"))
890 return (alternate ? DONE : OK);
891 (*p->c_ceclosefnx) (p);
899 /* get buffer ready to go */
901 buflen = sizeof(buffer) - 1;
902 bp[0] = bp[buflen] = '\0';
905 /* Now parse display string */
906 for ( ; *cp && buflen > 0; cp++) {
911 /* insert parameters from Content-Type field */
916 for (ap = ci->ci_attrs, ep = ci->ci_values;
918 snprintf(bp, buflen, "%s%s=\"%s\"",
929 /* insert content description */
933 s = trimcpy(ct->c_descr);
934 strncpy(bp, s, buflen);
940 /* exclusive execution */
950 /* insert filename(s) containing content */
954 for (part = m->mp_parts; part;
955 part = part->mp_next) {
958 snprintf(bp, buflen, "%s'%s'",
966 ** set our starting pointer back to bp,
967 ** to avoid requoting the filenames we
975 /* %l, and pause prior to displaying content */
981 ** display listing prior to displaying content
987 /* insert subtype of content */
988 strncpy(bp, ci->ci_subtype, buflen);
992 /* insert character % */
1005 /* Did we actually insert something? */
1008 ** Insert single quote if not inside quotes
1011 if (!quoted && buflen) {
1013 memmove(pp + 1, pp, len);
1018 /* Escape existing quotes */
1019 while ((pp = strchr(pp, '\'')) && buflen > 3) {
1021 memmove(pp + 3, pp, len);
1029 ** If pp is still set, that means we ran
1034 if (!quoted && buflen) {
1052 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1054 ** content_error would provide a more useful error message
1055 ** here, except that if we got overrun, it probably would
1058 fprintf(stderr, "Buffer overflow constructing show command!\n");
1062 /* use charset string to modify display method */
1063 if (ct->c_termproc) {
1066 strncpy(term, buffer, sizeof(term));
1067 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1070 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1071 NOTOK, xlist, xpause, 0, xtty);
1076 ** show content of type "message/rfc822"
1080 show_message_rfc822(CT ct, int serial, int alternate)
1082 char *cp, buffer[BUFSIZ];
1083 CI ci = &ct->c_ctinfo;
1085 /* Check for mhshow-show-type/subtype */
1086 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1087 invo_name, ci->ci_type, ci->ci_subtype);
1088 if ((cp = context_find(buffer)) && *cp != '\0')
1089 return show_content_aux(ct, serial, alternate, cp, NULL);
1091 /* Check for mhshow-show-type */
1092 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1093 if ((cp = context_find(buffer)) && *cp != '\0')
1094 return show_content_aux(ct, serial, alternate, cp, NULL);
1096 if ((cp = ct->c_showproc))
1097 return show_content_aux(ct, serial, alternate, cp, NULL);
1099 /* default method for message/rfc822 */
1100 if (ct->c_subtype == MESSAGE_RFC822) {
1101 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1102 return show_content_aux(ct, serial, alternate, cp, NULL);
1105 /* complain if we are not a part of a multipart/alternative */
1107 content_error(NULL, ct, "don't know how to display content");
1114 ** Show content of type "message/partial".
1118 show_partial(CT ct, int serial, int alternate)
1120 content_error(NULL, ct,
1121 "in order to display this message, you must reassemble it");
1127 ** Show content of type "message/external".
1129 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1133 show_external(CT ct, int serial, int alternate)
1135 struct exbody *e = (struct exbody *) ct->c_ctparams;
1136 CT p = e->eb_content;
1141 return show_switch(p, serial, alternate);
1144 content_error(NULL, p, "don't know how to display content");
1153 #ifndef RELIABLE_SIGNALS
1154 SIGNAL(SIGINT, intrser);
1158 siglongjmp(intrenv, DONE);