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 */
50 int SOprintf(char *, ...);
58 void content_error(char *, CT, char *, ...);
59 void flush_errors(void);
62 int list_switch(CT, int, int, int, int);
63 int list_content(CT, int, int, int, int);
68 void show_all_messages(CT *);
69 int show_content_aux(CT, int, int, char *, char *);
74 static void show_single_message(CT, char *);
75 static void DisplayMsgHeader(CT, char *);
76 static int show_switch(CT, int, int);
77 static int show_content(CT, int, int);
78 static int show_content_aux2(CT, int, int, char *, char *, int, int, int, int);
79 static int show_text(CT, int, int);
80 static int show_multi(CT, int, int);
81 static int show_multi_internal(CT, int, int);
82 static int show_multi_aux(CT, int, int, char *);
83 static int show_message_rfc822(CT, int, int);
84 static int show_partial(CT, int, int);
85 static int show_external(CT, int, int);
89 ** Top level entry point to show/display a group of messages
93 show_all_messages(CT *cts)
98 ** If form is not specified, then get default form
99 ** for showing headers of MIME messages.
102 formsw = getcpy(etcpath("mhl.headers"));
105 ** If form is "mhl.null", suppress display of header.
107 if (strcmp(formsw, "mhl.null")==0)
110 for (ctp = cts; *ctp; ctp++) {
113 /* if top-level type is ok, then display message */
115 show_single_message(ct, formsw);
121 ** Entry point to show/display a single message
125 show_single_message(CT ct, char *form)
129 #ifdef HAVE_UNION_WAIT
136 ** Allow user executable bit so that temporary directories created by
137 ** the viewer (e.g., lynx) are going to be accessible
139 umask(ct->c_umask & ~(0100));
142 ** If you have a format file, then display
143 ** the message headers.
146 DisplayMsgHeader(ct, form);
150 /* Show the body of the message */
151 show_switch(ct, 1, 0);
157 if (ct->c_ceclosefnx)
158 (*ct->c_ceclosefnx) (ct);
160 /* block a few signals */
162 sigaddset(&set, SIGHUP);
163 sigaddset(&set, SIGINT);
164 sigaddset(&set, SIGQUIT);
165 sigaddset(&set, SIGTERM);
166 SIGPROCMASK(SIG_BLOCK, &set, &oset);
168 while (wait(&status) != NOTOK) {
169 #ifdef HAVE_UNION_WAIT
170 pidcheck(status.w_status);
177 /* reset the signal mask */
178 SIGPROCMASK(SIG_SETMASK, &oset, &set);
186 ** Use the mhlproc to show the header fields
190 DisplayMsgHeader(CT ct, char *form)
197 vec[vecp++] = mhbasename(mhlproc);
198 vec[vecp++] = "-form";
200 vec[vecp++] = "-nobody";
201 vec[vecp++] = ct->c_file;
204 ** If we've specified -(no)moreproc,
205 ** then just pass that along.
208 vec[vecp++] = "-nomoreproc";
210 vec[vecp++] = "-moreproc";
211 vec[vecp++] = progsw;
217 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
222 adios("fork", "unable to");
226 execvp(mhlproc, vec);
227 fprintf(stderr, "unable to exec ");
240 ** Switching routine. Call the correct routine
241 ** based on content type.
245 show_switch(CT ct, int serial, int alternate)
247 switch (ct->c_type) {
249 return show_multi(ct, serial, alternate);
253 switch (ct->c_subtype) {
254 case MESSAGE_PARTIAL:
255 return show_partial(ct, serial, alternate);
258 case MESSAGE_EXTERNAL:
259 return show_external(ct, serial, alternate);
264 return show_message_rfc822(ct, serial,
271 return show_text(ct, serial, alternate);
278 return show_content(ct, serial, alternate);
282 adios(NULL, "unknown content type %d", ct->c_type);
286 return 0; /* NOT REACHED */
291 ** Generic method for displaying content
295 show_content(CT ct, int serial, int alternate)
297 char *cp, buffer[BUFSIZ];
298 CI ci = &ct->c_ctinfo;
300 /* Check for mhshow-show-type/subtype */
301 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
302 invo_name, ci->ci_type, ci->ci_subtype);
303 if ((cp = context_find(buffer)) && *cp != '\0')
304 return show_content_aux(ct, serial, alternate, cp, NULL);
306 /* Check for mhshow-show-type */
307 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
308 if ((cp = context_find(buffer)) && *cp != '\0')
309 return show_content_aux(ct, serial, alternate, cp, NULL);
311 if ((cp = ct->c_showproc))
312 return show_content_aux(ct, serial, alternate, cp, NULL);
314 /* complain if we are not a part of a multipart/alternative */
316 content_error(NULL, ct, "don't know how to display content");
323 ** Parse the display string for displaying generic content
327 show_content_aux(CT ct, int serial, int alternate, char *cp, char *cracked)
329 int fd, len, buflen, quoted;
330 int xstdin, xlist, xtty;
331 char *bp, *pp, *file, buffer[BUFSIZ];
332 CI ci = &ct->c_ctinfo;
334 if (!ct->c_ceopenfnx) {
336 content_error(NULL, ct,
337 "don't know how to decode content");
343 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
345 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
346 return (alternate ? DONE : OK);
353 strncpy(buffer, cp, sizeof(buffer));
357 /* get buffer ready to go */
359 buflen = sizeof(buffer) - 1;
360 bp[0] = bp[buflen] = '\0';
363 /* Now parse display string */
364 for ( ; *cp && buflen > 0; cp++) {
370 /* insert parameters from Content-Type field */
375 for (ap = ci->ci_attrs, ep = ci->ci_values;
377 snprintf(bp, buflen, "%s%s=\"%s\"",
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);
413 ** since we've quoted the file argument,
414 ** set things up to look past it, to avoid
415 ** problems with the quoting logic below.
416 ** (I know, I should figure out what's
417 ** broken with the quoting logic, but..)
428 ** 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? */
455 ** Insert single quote if not inside quotes
458 if (!quoted && buflen) {
460 memmove(pp + 1, pp, len);
465 /* Escape existing quotes */
466 while ((pp = strchr(pp, '\'')) && buflen > 3) {
468 memmove(pp + 3, pp, len);
476 ** If pp is still set, that means we ran
481 if (!quoted && buflen) {
498 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
500 ** content_error would provide a more useful error message
501 ** here, except that if we got overrun, it probably would
504 fprintf(stderr, "Buffer overflow constructing show command!\n");
508 /* use charset string to modify display method */
509 if (ct->c_termproc) {
512 strncpy(term, buffer, sizeof(term));
513 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
517 return show_content_aux2(ct, serial, alternate, cracked, buffer,
518 fd, xlist, xstdin, xtty);
523 ** Routine to actually display the content
527 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
528 char *buffer, int fd, int xlist, int xstdin, int xtty)
532 char *vec[4], exec[BUFSIZ + sizeof "exec "];
534 if (debugsw || cracked) {
537 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
540 fprintf(stderr, " part %s", ct->c_partno);
542 fprintf(stderr, " using command (cd %s; %s)\n",
545 fprintf(stderr, " using command %s\n", buffer);
548 if (xpid < 0 || (xtty && xpid)) {
551 pidcheck(pidwait(xpid, NOTOK));
556 if (ct->c_type == CT_MULTIPART)
557 list_content(ct, -1, 1, 0, 0);
559 list_switch(ct, -1, 1, 0, 0);
562 snprintf(exec, sizeof(exec), "exec %s", buffer);
571 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
575 advise("fork", "unable to");
576 (*ct->c_ceclosefnx) (ct);
585 execvp("/bin/sh", vec);
586 fprintf(stderr, "unable to exec ");
593 ct->c_pid = child_id;
597 pidcheck(pidXwait(child_id, NULL));
601 (*ct->c_ceclosefnx) (ct);
602 return (alternate ? DONE : OK);
608 ** show content of type "text"
612 show_text(CT ct, int serial, int alternate)
614 char *cp, buffer[BUFSIZ];
615 CI ci = &ct->c_ctinfo;
617 /* Check for mhshow-show-type/subtype */
618 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
619 invo_name, ci->ci_type, ci->ci_subtype);
620 if ((cp = context_find(buffer)) && *cp != '\0')
621 return show_content_aux(ct, serial, alternate, cp, NULL);
623 /* Check for mhshow-show-type */
624 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
625 if ((cp = context_find(buffer)) && *cp != '\0')
626 return show_content_aux(ct, serial, alternate, cp, NULL);
629 ** Use default method if content is text/plain, or if
630 ** if it is not a text part of a multipart/alternative
632 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
633 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'",
634 progsw ? progsw : moreproc && *moreproc ?
636 cp = (ct->c_showproc = getcpy(buffer));
637 return show_content_aux(ct, serial, alternate, cp, NULL);
645 ** show message body of type "multipart"
649 show_multi(CT ct, int serial, int alternate)
651 char *cp, buffer[BUFSIZ];
652 CI ci = &ct->c_ctinfo;
654 /* Check for mhshow-show-type/subtype */
655 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
656 invo_name, ci->ci_type, ci->ci_subtype);
657 if ((cp = context_find(buffer)) && *cp != '\0')
658 return show_multi_aux(ct, serial, alternate, cp);
660 /* Check for mhshow-show-type */
661 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
662 if ((cp = context_find(buffer)) && *cp != '\0')
663 return show_multi_aux(ct, serial, alternate, cp);
665 if ((cp = ct->c_showproc))
666 return show_multi_aux(ct, serial, alternate, cp);
669 ** Use default method to display this multipart content
670 ** if it is not a (nested) part of a multipart/alternative,
671 ** or if it is one of the known subtypes of multipart.
673 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
674 return show_multi_internal(ct, serial, alternate);
681 ** show message body of subtypes of multipart that
682 ** we understand directly (mixed, alternate, etc...)
686 show_multi_internal(CT ct, int serial, int alternate)
688 int alternating, nowalternate, nowserial, result;
689 struct multipart *m = (struct multipart *) ct->c_ctparams;
695 nowalternate = alternate;
697 if (ct->c_subtype == MULTI_PARALLEL) {
698 nowserial = serialsw;
699 } else if (ct->c_subtype == MULTI_ALTERNATE) {
707 ** unknown subtypes of multipart (treat as mixed per rfc2046)
712 /* block a few signals */
715 sigaddset(&set, SIGHUP);
716 sigaddset(&set, SIGINT);
717 sigaddset(&set, SIGQUIT);
718 sigaddset(&set, SIGTERM);
719 SIGPROCMASK(SIG_BLOCK, &set, &oset);
723 ** alternate -> we are a part inside an multipart/alternative
724 ** alternating -> we are a multipart/alternative
727 result = alternate ? NOTOK : OK;
729 for (part = m->mp_parts; part; part = part->mp_next) {
732 if (part_ok(p, 0) && type_ok(p, 0)) {
735 inneresult = show_switch(p, nowserial, nowalternate);
736 switch (inneresult) {
738 if (alternate && !alternating) {
751 alternate = nowalternate = 0;
761 if (alternating && !part) {
763 content_error(NULL, ct, "don't know how to display any of the contents");
768 if (serial && !nowserial) {
771 #ifdef HAVE_UNION_WAIT
778 for (part = m->mp_parts; part; part = part->mp_next) {
782 if (kill(p->c_pid, 0) == NOTOK)
789 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
790 #ifdef HAVE_UNION_WAIT
791 pidcheck(status.w_status);
796 for (part = m->mp_parts; part; part = part->mp_next) {
801 if (p->c_pid == pid) {
812 /* reset the signal mask */
813 SIGPROCMASK(SIG_SETMASK, &oset, &set);
821 ** Parse display string for multipart content
822 ** and use external program to display it.
826 show_multi_aux(CT ct, int serial, int alternate, char *cp)
828 int len, buflen, quoted;
830 char *bp, *pp, *file, buffer[BUFSIZ];
831 struct multipart *m = (struct multipart *) ct->c_ctparams;
833 CI ci = &ct->c_ctinfo;
836 for (part = m->mp_parts; part; part = part->mp_next) {
839 if (!p->c_ceopenfnx) {
841 content_error(NULL, p, "don't know how to decode content");
845 if (p->c_storage == NULL) {
847 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
850 /* I'm not sure if this is necessary? */
851 p->c_storage = getcpy(file);
853 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
854 return (alternate ? DONE : OK);
855 (*p->c_ceclosefnx) (p);
862 /* get buffer ready to go */
864 buflen = sizeof(buffer) - 1;
865 bp[0] = bp[buflen] = '\0';
868 /* Now parse display string */
869 for ( ; *cp && buflen > 0; cp++) {
874 /* insert parameters from Content-Type field */
879 for (ap = ci->ci_attrs, ep = ci->ci_values;
881 snprintf(bp, buflen, "%s%s=\"%s\"",
892 /* insert content description */
896 s = trimcpy(ct->c_descr);
897 strncpy(bp, s, buflen);
903 /* exclusive execution */
913 /* insert filename(s) containing content */
917 for (part = m->mp_parts; part;
918 part = part->mp_next) {
921 snprintf(bp, buflen, "%s'%s'",
929 ** set our starting pointer back to bp,
930 ** to avoid requoting the filenames we
940 ** display listing prior to displaying content
946 /* insert subtype of content */
947 strncpy(bp, ci->ci_subtype, buflen);
951 /* insert character % */
964 /* Did we actually insert something? */
967 ** Insert single quote if not inside quotes
970 if (!quoted && buflen) {
972 memmove(pp + 1, pp, len);
977 /* Escape existing quotes */
978 while ((pp = strchr(pp, '\'')) && buflen > 3) {
980 memmove(pp + 3, pp, len);
988 ** If pp is still set, that means we ran
993 if (!quoted && buflen) {
1011 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1013 ** content_error would provide a more useful error message
1014 ** here, except that if we got overrun, it probably would
1017 fprintf(stderr, "Buffer overflow constructing show command!\n");
1021 /* use charset string to modify display method */
1022 if (ct->c_termproc) {
1025 strncpy(term, buffer, sizeof(term));
1026 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1029 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1030 NOTOK, xlist, 0, xtty);
1035 ** show content of type "message/rfc822"
1039 show_message_rfc822(CT ct, int serial, int alternate)
1041 char *cp, buffer[BUFSIZ];
1042 CI ci = &ct->c_ctinfo;
1044 /* Check for mhshow-show-type/subtype */
1045 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1046 invo_name, ci->ci_type, ci->ci_subtype);
1047 if ((cp = context_find(buffer)) && *cp != '\0')
1048 return show_content_aux(ct, serial, alternate, cp, NULL);
1050 /* Check for mhshow-show-type */
1051 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1052 if ((cp = context_find(buffer)) && *cp != '\0')
1053 return show_content_aux(ct, serial, alternate, cp, NULL);
1055 if ((cp = ct->c_showproc))
1056 return show_content_aux(ct, serial, alternate, cp, NULL);
1058 /* default method for message/rfc822 */
1059 if (ct->c_subtype == MESSAGE_RFC822) {
1060 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1061 return show_content_aux(ct, serial, alternate, cp, NULL);
1064 /* complain if we are not a part of a multipart/alternative */
1066 content_error(NULL, ct, "don't know how to display content");
1073 ** Show content of type "message/partial".
1077 show_partial(CT ct, int serial, int alternate)
1079 content_error(NULL, ct,
1080 "in order to display this message, you must reassemble it");
1086 ** Show content of type "message/external".
1088 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1092 show_external(CT ct, int serial, int alternate)
1094 struct exbody *e = (struct exbody *) ct->c_ctparams;
1095 CT p = e->eb_content;
1100 return show_switch(p, serial, alternate);
1103 content_error(NULL, p, "don't know how to display content");