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>
18 #include <h/mhparse.h>
21 #ifdef HAVE_SYS_WAIT_H
22 # include <sys/wait.h>
26 ** Just use sigjmp/longjmp on older machines that
27 ** don't have sigsetjmp/siglongjmp.
29 #ifndef HAVE_SIGSETJMP
30 # define sigjmp_buf jmp_buf
31 # define sigsetjmp(env,mask) setjmp(env)
32 # define siglongjmp(env,val) longjmp(env,val)
42 /* flags for moreproc/header display */
54 void content_error(char *, CT, char *, ...);
55 void flush_errors(void);
58 int list_switch(CT, int, int, int, int);
59 int list_content(CT, int, int, int, int);
64 void show_all_messages(CT *);
65 int show_content_aux(CT, int, int, char *, char *);
70 static void show_single_message(CT, char *);
71 static void DisplayMsgHeader(CT, char *);
72 static int show_switch(CT, int, int);
73 static int show_content(CT, int, int);
74 static int show_content_aux2(CT, int, int, char *, char *, int, int, int, int);
75 static int show_text(CT, int, int);
76 static int show_multi(CT, int, int);
77 static int show_multi_internal(CT, int, int);
78 static int show_multi_aux(CT, int, int, char *);
79 static int show_message_rfc822(CT, int, int);
80 static int show_partial(CT, int, int);
81 static int show_external(CT, int, int);
85 ** Top level entry point to show/display a group of messages
89 show_all_messages(CT *cts)
94 ** If form is not specified, then get default form
95 ** for showing headers of MIME messages.
98 formsw = getcpy(etcpath("mhl.headers"));
101 ** If form is "mhl.null", suppress display of header.
103 if (strcmp(formsw, "mhl.null")==0)
106 for (ctp = cts; *ctp; ctp++) {
109 /* if top-level type is ok, then display message */
111 show_single_message(ct, formsw);
117 ** Entry point to show/display a single message
121 show_single_message(CT ct, char *form)
125 #ifdef HAVE_UNION_WAIT
132 ** Allow user executable bit so that temporary directories created by
133 ** 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) {
165 #ifdef HAVE_UNION_WAIT
166 pidcheck(status.w_status);
173 /* reset the signal mask */
174 SIGPROCMASK(SIG_SETMASK, &oset, &set);
182 ** Use the mhlproc to show the header fields
186 DisplayMsgHeader(CT ct, char *form)
193 vec[vecp++] = mhbasename(mhlproc);
194 vec[vecp++] = "-form";
196 vec[vecp++] = "-nobody";
197 vec[vecp++] = ct->c_file;
199 /* If we've specified -moreproc, then just pass that along. */
201 vec[vecp++] = "-moreproc";
202 vec[vecp++] = progsw;
208 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
213 adios("fork", "unable to");
217 execvp(mhlproc, vec);
218 fprintf(stderr, "unable to exec ");
231 ** Switching routine. Call the correct routine
232 ** based on content type.
236 show_switch(CT ct, int serial, int alternate)
238 switch (ct->c_type) {
240 return show_multi(ct, serial, alternate);
244 switch (ct->c_subtype) {
245 case MESSAGE_PARTIAL:
246 return show_partial(ct, serial, alternate);
249 case MESSAGE_EXTERNAL:
250 return show_external(ct, serial, alternate);
255 return show_message_rfc822(ct, serial,
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 mhshow-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 mhshow-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, xtty;
322 char *bp, *pp, *file, buffer[BUFSIZ];
323 CI ci = &ct->c_ctinfo;
325 if (!ct->c_ceopenfnx) {
327 content_error(NULL, ct,
328 "don't know how to decode content");
334 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
336 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
337 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;
368 snprintf(bp, buflen, "%s%s=\"%s\"",
379 /* insert content description */
383 s = trimcpy(ct->c_descr);
384 strncpy(bp, s, buflen);
390 /* exclusive execution */
395 /* %e, %f, and stdin is terminal not content */
401 /* insert filename containing content */
402 snprintf(bp, buflen, "'%s'", file);
404 ** since we've quoted the file argument,
405 ** set things up to look past it, to avoid
406 ** problems with the quoting logic below.
407 ** (I know, I should figure out what's
408 ** broken with the quoting logic, but..)
419 ** display listing prior to displaying content
425 /* insert subtype of content */
426 strncpy(bp, ci->ci_subtype, buflen);
430 /* insert character % */
443 /* Did we actually insert something? */
446 ** Insert single quote if not inside quotes
449 if (!quoted && buflen) {
451 memmove(pp + 1, pp, len);
456 /* Escape existing quotes */
457 while ((pp = strchr(pp, '\'')) && buflen > 3) {
459 memmove(pp + 3, pp, len);
467 ** If pp is still set, that means we ran
472 if (!quoted && buflen) {
489 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
491 ** content_error would provide a more useful error message
492 ** here, except that if we got overrun, it probably would
495 fprintf(stderr, "Buffer overflow constructing show command!\n");
499 /* use charset string to modify display method */
500 if (ct->c_termproc) {
503 strncpy(term, buffer, sizeof(term));
504 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
508 return show_content_aux2(ct, serial, alternate, cracked, buffer,
509 fd, xlist, xstdin, xtty);
514 ** Routine to actually display the content
518 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
519 char *buffer, int fd, int xlist, int xstdin, int xtty)
523 char *vec[4], exec[BUFSIZ + sizeof "exec "];
525 if (debugsw || cracked) {
528 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
531 fprintf(stderr, " part %s", ct->c_partno);
533 fprintf(stderr, " using command (cd %s; %s)\n",
536 fprintf(stderr, " using command %s\n", buffer);
539 if (xpid < 0 || (xtty && xpid)) {
542 pidcheck(pidwait(xpid, NOTOK));
547 if (ct->c_type == CT_MULTIPART)
548 list_content(ct, -1, 1, 0, 0);
550 list_switch(ct, -1, 1, 0, 0);
553 snprintf(exec, sizeof(exec), "exec %s", buffer);
562 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
566 advise("fork", "unable to");
567 (*ct->c_ceclosefnx) (ct);
576 execvp("/bin/sh", vec);
577 fprintf(stderr, "unable to exec ");
584 ct->c_pid = child_id;
588 pidcheck(pidXwait(child_id, NULL));
592 (*ct->c_ceclosefnx) (ct);
593 return (alternate ? DONE : OK);
599 ** show content of type "text"
603 show_text(CT ct, int serial, int alternate)
605 char *cp, buffer[BUFSIZ];
606 CI ci = &ct->c_ctinfo;
608 /* Check for mhshow-show-type/subtype */
609 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
610 invo_name, ci->ci_type, ci->ci_subtype);
611 if ((cp = context_find(buffer)) && *cp != '\0')
612 return show_content_aux(ct, serial, alternate, cp, NULL);
614 /* Check for mhshow-show-type */
615 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
616 if ((cp = context_find(buffer)) && *cp != '\0')
617 return show_content_aux(ct, serial, alternate, cp, NULL);
620 ** Use default method if content is text/plain, or if
621 ** if it is not a text part of a multipart/alternative
623 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
624 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'",
625 progsw ? progsw : moreproc);
626 cp = (ct->c_showproc = getcpy(buffer));
627 return show_content_aux(ct, serial, alternate, cp, NULL);
635 ** show message body of type "multipart"
639 show_multi(CT ct, int serial, int alternate)
641 char *cp, buffer[BUFSIZ];
642 CI ci = &ct->c_ctinfo;
644 /* Check for mhshow-show-type/subtype */
645 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
646 invo_name, ci->ci_type, ci->ci_subtype);
647 if ((cp = context_find(buffer)) && *cp != '\0')
648 return show_multi_aux(ct, serial, alternate, cp);
650 /* Check for mhshow-show-type */
651 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
652 if ((cp = context_find(buffer)) && *cp != '\0')
653 return show_multi_aux(ct, serial, alternate, cp);
655 if ((cp = ct->c_showproc))
656 return show_multi_aux(ct, serial, alternate, cp);
659 ** Use default method to display this multipart content
660 ** if it is not a (nested) part of a multipart/alternative,
661 ** or if it is one of the known subtypes of multipart.
663 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
664 return show_multi_internal(ct, serial, alternate);
671 ** show message body of subtypes of multipart that
672 ** we understand directly (mixed, alternate, etc...)
676 show_multi_internal(CT ct, int serial, int alternate)
678 int alternating, nowalternate, nowserial, result;
679 struct multipart *m = (struct multipart *) ct->c_ctparams;
685 nowalternate = alternate;
687 if (ct->c_subtype == MULTI_PARALLEL) {
688 nowserial = serialsw;
689 } else if (ct->c_subtype == MULTI_ALTERNATE) {
697 ** unknown subtypes of multipart (treat as mixed per rfc2046)
702 /* block a few signals */
705 sigaddset(&set, SIGHUP);
706 sigaddset(&set, SIGINT);
707 sigaddset(&set, SIGQUIT);
708 sigaddset(&set, SIGTERM);
709 SIGPROCMASK(SIG_BLOCK, &set, &oset);
713 ** alternate -> we are a part inside an multipart/alternative
714 ** alternating -> we are a multipart/alternative
717 result = alternate ? NOTOK : OK;
719 for (part = m->mp_parts; part; part = part->mp_next) {
722 if (part_ok(p, 0) && type_ok(p, 0)) {
725 inneresult = show_switch(p, nowserial, nowalternate);
726 switch (inneresult) {
728 if (alternate && !alternating) {
741 alternate = nowalternate = 0;
751 if (alternating && !part) {
753 content_error(NULL, ct, "don't know how to display any of the contents");
758 if (serial && !nowserial) {
761 #ifdef HAVE_UNION_WAIT
768 for (part = m->mp_parts; part; part = part->mp_next) {
772 if (kill(p->c_pid, 0) == NOTOK)
779 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
780 #ifdef HAVE_UNION_WAIT
781 pidcheck(status.w_status);
786 for (part = m->mp_parts; part; part = part->mp_next) {
791 if (p->c_pid == pid) {
802 /* reset the signal mask */
803 SIGPROCMASK(SIG_SETMASK, &oset, &set);
811 ** Parse display string for multipart content
812 ** and use external program to display it.
816 show_multi_aux(CT ct, int serial, int alternate, char *cp)
818 int len, buflen, quoted;
820 char *bp, *pp, *file, buffer[BUFSIZ];
821 struct multipart *m = (struct multipart *) ct->c_ctparams;
823 CI ci = &ct->c_ctinfo;
826 for (part = m->mp_parts; part; part = part->mp_next) {
829 if (!p->c_ceopenfnx) {
831 content_error(NULL, p, "don't know how to decode content");
835 if (p->c_storage == NULL) {
837 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
840 /* I'm not sure if this is necessary? */
841 p->c_storage = getcpy(file);
843 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
844 return (alternate ? DONE : OK);
845 (*p->c_ceclosefnx) (p);
852 /* get buffer ready to go */
854 buflen = sizeof(buffer) - 1;
855 bp[0] = bp[buflen] = '\0';
858 /* Now parse display string */
859 for ( ; *cp && buflen > 0; cp++) {
864 /* insert parameters from Content-Type field */
869 for (ap = ci->ci_attrs, ep = ci->ci_values;
871 snprintf(bp, buflen, "%s%s=\"%s\"",
882 /* insert content description */
886 s = trimcpy(ct->c_descr);
887 strncpy(bp, s, buflen);
893 /* exclusive execution */
903 /* insert filename(s) containing content */
907 for (part = m->mp_parts; part;
908 part = part->mp_next) {
911 snprintf(bp, buflen, "%s'%s'",
919 ** set our starting pointer back to bp,
920 ** to avoid requoting the filenames we
930 ** display listing prior to displaying content
936 /* insert subtype of content */
937 strncpy(bp, ci->ci_subtype, buflen);
941 /* insert character % */
954 /* Did we actually insert something? */
957 ** Insert single quote if not inside quotes
960 if (!quoted && buflen) {
962 memmove(pp + 1, pp, len);
967 /* Escape existing quotes */
968 while ((pp = strchr(pp, '\'')) && buflen > 3) {
970 memmove(pp + 3, pp, len);
978 ** If pp is still set, that means we ran
983 if (!quoted && buflen) {
1001 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1003 ** content_error would provide a more useful error message
1004 ** here, except that if we got overrun, it probably would
1007 fprintf(stderr, "Buffer overflow constructing show command!\n");
1011 /* use charset string to modify display method */
1012 if (ct->c_termproc) {
1015 strncpy(term, buffer, sizeof(term));
1016 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1019 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1020 NOTOK, xlist, 0, xtty);
1025 ** show content of type "message/rfc822"
1029 show_message_rfc822(CT ct, int serial, int alternate)
1031 char *cp, buffer[BUFSIZ];
1032 CI ci = &ct->c_ctinfo;
1034 /* Check for mhshow-show-type/subtype */
1035 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1036 invo_name, ci->ci_type, ci->ci_subtype);
1037 if ((cp = context_find(buffer)) && *cp != '\0')
1038 return show_content_aux(ct, serial, alternate, cp, NULL);
1040 /* Check for mhshow-show-type */
1041 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1042 if ((cp = context_find(buffer)) && *cp != '\0')
1043 return show_content_aux(ct, serial, alternate, cp, NULL);
1045 if ((cp = ct->c_showproc))
1046 return show_content_aux(ct, serial, alternate, cp, NULL);
1048 /* default method for message/rfc822 */
1049 if (ct->c_subtype == MESSAGE_RFC822) {
1050 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1051 return show_content_aux(ct, serial, alternate, cp, NULL);
1054 /* complain if we are not a part of a multipart/alternative */
1056 content_error(NULL, ct, "don't know how to display content");
1063 ** Show content of type "message/partial".
1067 show_partial(CT ct, int serial, int alternate)
1069 content_error(NULL, ct,
1070 "in order to display this message, you must reassemble it");
1076 ** Show content of type "message/external".
1078 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1082 show_external(CT ct, int serial, int alternate)
1084 struct exbody *e = (struct exbody *) ct->c_ctparams;
1085 CT p = e->eb_content;
1090 return show_switch(p, serial, alternate);
1093 content_error(NULL, p, "don't know how to display content");