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 */
49 int SOprintf(char *, ...);
57 void content_error(char *, CT, char *, ...);
58 void flush_errors(void);
61 int list_switch(CT, int, int, int, int);
62 int list_content(CT, int, int, int, int);
67 void show_all_messages(CT *);
68 int show_content_aux(CT, int, int, char *, char *);
73 static void show_single_message(CT, char *);
74 static void DisplayMsgHeader(CT, char *);
75 static int show_switch(CT, int, int);
76 static int show_content(CT, int, int);
77 static int show_content_aux2(CT, int, int, char *, char *, int, int, int, int);
78 static int show_text(CT, int, int);
79 static int show_multi(CT, int, int);
80 static int show_multi_internal(CT, int, int);
81 static int show_multi_aux(CT, int, int, char *);
82 static int show_message_rfc822(CT, int, int);
83 static int show_partial(CT, int, int);
84 static int show_external(CT, int, int);
88 ** Top level entry point to show/display a group of messages
92 show_all_messages(CT *cts)
97 ** If form is not specified, then get default form
98 ** for showing headers of MIME messages.
101 formsw = getcpy(etcpath("mhl.headers"));
104 ** If form is "mhl.null", suppress display of header.
106 if (strcmp(formsw, "mhl.null")==0)
109 for (ctp = cts; *ctp; ctp++) {
112 /* if top-level type is ok, then display message */
114 show_single_message(ct, formsw);
120 ** Entry point to show/display a single message
124 show_single_message(CT ct, char *form)
128 #ifdef HAVE_UNION_WAIT
135 ** Allow user executable bit so that temporary directories created by
136 ** the viewer (e.g., lynx) are going to be accessible
138 umask(ct->c_umask & ~(0100));
141 ** If you have a format file, then display
142 ** the message headers.
145 DisplayMsgHeader(ct, form);
149 /* Show the body of the message */
150 show_switch(ct, 1, 0);
156 if (ct->c_ceclosefnx)
157 (*ct->c_ceclosefnx) (ct);
159 /* block a few signals */
161 sigaddset(&set, SIGHUP);
162 sigaddset(&set, SIGINT);
163 sigaddset(&set, SIGQUIT);
164 sigaddset(&set, SIGTERM);
165 SIGPROCMASK(SIG_BLOCK, &set, &oset);
167 while (wait(&status) != NOTOK) {
168 #ifdef HAVE_UNION_WAIT
169 pidcheck(status.w_status);
176 /* reset the signal mask */
177 SIGPROCMASK(SIG_SETMASK, &oset, &set);
185 ** Use the mhlproc to show the header fields
189 DisplayMsgHeader(CT ct, char *form)
196 vec[vecp++] = mhbasename(mhlproc);
197 vec[vecp++] = "-form";
199 vec[vecp++] = "-nobody";
200 vec[vecp++] = ct->c_file;
202 /* If we've specified -moreproc, then just pass that along. */
204 vec[vecp++] = "-moreproc";
205 vec[vecp++] = progsw;
211 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
216 adios("fork", "unable to");
220 execvp(mhlproc, vec);
221 fprintf(stderr, "unable to exec ");
234 ** Switching routine. Call the correct routine
235 ** based on content type.
239 show_switch(CT ct, int serial, int alternate)
241 switch (ct->c_type) {
243 return show_multi(ct, serial, alternate);
247 switch (ct->c_subtype) {
248 case MESSAGE_PARTIAL:
249 return show_partial(ct, serial, alternate);
252 case MESSAGE_EXTERNAL:
253 return show_external(ct, serial, alternate);
258 return show_message_rfc822(ct, serial,
265 return show_text(ct, serial, alternate);
272 return show_content(ct, serial, alternate);
276 adios(NULL, "unknown content type %d", ct->c_type);
280 return 0; /* NOT REACHED */
285 ** Generic method for displaying content
289 show_content(CT ct, int serial, int alternate)
291 char *cp, buffer[BUFSIZ];
292 CI ci = &ct->c_ctinfo;
294 /* Check for mhshow-show-type/subtype */
295 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
296 invo_name, ci->ci_type, ci->ci_subtype);
297 if ((cp = context_find(buffer)) && *cp != '\0')
298 return show_content_aux(ct, serial, alternate, cp, NULL);
300 /* Check for mhshow-show-type */
301 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
302 if ((cp = context_find(buffer)) && *cp != '\0')
303 return show_content_aux(ct, serial, alternate, cp, NULL);
305 if ((cp = ct->c_showproc))
306 return show_content_aux(ct, serial, alternate, cp, NULL);
308 /* complain if we are not a part of a multipart/alternative */
310 content_error(NULL, ct, "don't know how to display content");
317 ** Parse the display string for displaying generic content
321 show_content_aux(CT ct, int serial, int alternate, char *cp, char *cracked)
323 int fd, len, buflen, quoted;
324 int xstdin, xlist, xtty;
325 char *bp, *pp, *file, buffer[BUFSIZ];
326 CI ci = &ct->c_ctinfo;
328 if (!ct->c_ceopenfnx) {
330 content_error(NULL, ct,
331 "don't know how to decode content");
337 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
339 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
340 return (alternate ? DONE : OK);
347 strncpy(buffer, cp, sizeof(buffer));
351 /* get buffer ready to go */
353 buflen = sizeof(buffer) - 1;
354 bp[0] = bp[buflen] = '\0';
357 /* Now parse display string */
358 for ( ; *cp && buflen > 0; cp++) {
364 /* insert parameters from Content-Type field */
369 for (ap = ci->ci_attrs, ep = ci->ci_values;
371 snprintf(bp, buflen, "%s%s=\"%s\"",
382 /* insert content description */
386 s = trimcpy(ct->c_descr);
387 strncpy(bp, s, buflen);
393 /* exclusive execution */
398 /* %e, %f, and stdin is terminal not content */
404 /* insert filename containing content */
405 snprintf(bp, buflen, "'%s'", file);
407 ** since we've quoted the file argument,
408 ** set things up to look past it, to avoid
409 ** problems with the quoting logic below.
410 ** (I know, I should figure out what's
411 ** broken with the quoting logic, but..)
422 ** display listing prior to displaying content
428 /* insert subtype of content */
429 strncpy(bp, ci->ci_subtype, buflen);
433 /* insert character % */
446 /* Did we actually insert something? */
449 ** Insert single quote if not inside quotes
452 if (!quoted && buflen) {
454 memmove(pp + 1, pp, len);
459 /* Escape existing quotes */
460 while ((pp = strchr(pp, '\'')) && buflen > 3) {
462 memmove(pp + 3, pp, len);
470 ** If pp is still set, that means we ran
475 if (!quoted && buflen) {
492 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
494 ** content_error would provide a more useful error message
495 ** here, except that if we got overrun, it probably would
498 fprintf(stderr, "Buffer overflow constructing show command!\n");
502 /* use charset string to modify display method */
503 if (ct->c_termproc) {
506 strncpy(term, buffer, sizeof(term));
507 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
511 return show_content_aux2(ct, serial, alternate, cracked, buffer,
512 fd, xlist, xstdin, xtty);
517 ** Routine to actually display the content
521 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
522 char *buffer, int fd, int xlist, int xstdin, int xtty)
526 char *vec[4], exec[BUFSIZ + sizeof "exec "];
528 if (debugsw || cracked) {
531 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
534 fprintf(stderr, " part %s", ct->c_partno);
536 fprintf(stderr, " using command (cd %s; %s)\n",
539 fprintf(stderr, " using command %s\n", buffer);
542 if (xpid < 0 || (xtty && xpid)) {
545 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);
556 snprintf(exec, sizeof(exec), "exec %s", buffer);
565 for (i = 0; (child_id = fork()) == NOTOK && i < 5; i++)
569 advise("fork", "unable to");
570 (*ct->c_ceclosefnx) (ct);
579 execvp("/bin/sh", vec);
580 fprintf(stderr, "unable to exec ");
587 ct->c_pid = child_id;
591 pidcheck(pidXwait(child_id, NULL));
595 (*ct->c_ceclosefnx) (ct);
596 return (alternate ? DONE : OK);
602 ** show content of type "text"
606 show_text(CT ct, int serial, int alternate)
608 char *cp, buffer[BUFSIZ];
609 CI ci = &ct->c_ctinfo;
611 /* Check for mhshow-show-type/subtype */
612 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
613 invo_name, ci->ci_type, ci->ci_subtype);
614 if ((cp = context_find(buffer)) && *cp != '\0')
615 return show_content_aux(ct, serial, alternate, cp, NULL);
617 /* Check for mhshow-show-type */
618 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
619 if ((cp = context_find(buffer)) && *cp != '\0')
620 return show_content_aux(ct, serial, alternate, cp, NULL);
623 ** Use default method if content is text/plain, or if
624 ** if it is not a text part of a multipart/alternative
626 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
627 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'",
628 progsw ? progsw : moreproc && *moreproc ?
630 cp = (ct->c_showproc = getcpy(buffer));
631 return show_content_aux(ct, serial, alternate, cp, NULL);
639 ** show message body of type "multipart"
643 show_multi(CT ct, int serial, int alternate)
645 char *cp, buffer[BUFSIZ];
646 CI ci = &ct->c_ctinfo;
648 /* Check for mhshow-show-type/subtype */
649 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
650 invo_name, ci->ci_type, ci->ci_subtype);
651 if ((cp = context_find(buffer)) && *cp != '\0')
652 return show_multi_aux(ct, serial, alternate, cp);
654 /* Check for mhshow-show-type */
655 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
656 if ((cp = context_find(buffer)) && *cp != '\0')
657 return show_multi_aux(ct, serial, alternate, cp);
659 if ((cp = ct->c_showproc))
660 return show_multi_aux(ct, serial, alternate, cp);
663 ** Use default method to display this multipart content
664 ** if it is not a (nested) part of a multipart/alternative,
665 ** or if it is one of the known subtypes of multipart.
667 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
668 return show_multi_internal(ct, serial, alternate);
675 ** show message body of subtypes of multipart that
676 ** we understand directly (mixed, alternate, etc...)
680 show_multi_internal(CT ct, int serial, int alternate)
682 int alternating, nowalternate, nowserial, result;
683 struct multipart *m = (struct multipart *) ct->c_ctparams;
689 nowalternate = alternate;
691 if (ct->c_subtype == MULTI_PARALLEL) {
692 nowserial = serialsw;
693 } else if (ct->c_subtype == MULTI_ALTERNATE) {
701 ** unknown subtypes of multipart (treat as mixed per rfc2046)
706 /* block a few signals */
709 sigaddset(&set, SIGHUP);
710 sigaddset(&set, SIGINT);
711 sigaddset(&set, SIGQUIT);
712 sigaddset(&set, SIGTERM);
713 SIGPROCMASK(SIG_BLOCK, &set, &oset);
717 ** alternate -> we are a part inside an multipart/alternative
718 ** alternating -> we are a multipart/alternative
721 result = alternate ? NOTOK : OK;
723 for (part = m->mp_parts; part; part = part->mp_next) {
726 if (part_ok(p, 0) && type_ok(p, 0)) {
729 inneresult = show_switch(p, nowserial, nowalternate);
730 switch (inneresult) {
732 if (alternate && !alternating) {
745 alternate = nowalternate = 0;
755 if (alternating && !part) {
757 content_error(NULL, ct, "don't know how to display any of the contents");
762 if (serial && !nowserial) {
765 #ifdef HAVE_UNION_WAIT
772 for (part = m->mp_parts; part; part = part->mp_next) {
776 if (kill(p->c_pid, 0) == NOTOK)
783 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
784 #ifdef HAVE_UNION_WAIT
785 pidcheck(status.w_status);
790 for (part = m->mp_parts; part; part = part->mp_next) {
795 if (p->c_pid == pid) {
806 /* reset the signal mask */
807 SIGPROCMASK(SIG_SETMASK, &oset, &set);
815 ** Parse display string for multipart content
816 ** and use external program to display it.
820 show_multi_aux(CT ct, int serial, int alternate, char *cp)
822 int len, buflen, quoted;
824 char *bp, *pp, *file, buffer[BUFSIZ];
825 struct multipart *m = (struct multipart *) ct->c_ctparams;
827 CI ci = &ct->c_ctinfo;
830 for (part = m->mp_parts; part; part = part->mp_next) {
833 if (!p->c_ceopenfnx) {
835 content_error(NULL, p, "don't know how to decode content");
839 if (p->c_storage == NULL) {
841 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
844 /* I'm not sure if this is necessary? */
845 p->c_storage = getcpy(file);
847 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
848 return (alternate ? DONE : OK);
849 (*p->c_ceclosefnx) (p);
856 /* get buffer ready to go */
858 buflen = sizeof(buffer) - 1;
859 bp[0] = bp[buflen] = '\0';
862 /* Now parse display string */
863 for ( ; *cp && buflen > 0; cp++) {
868 /* insert parameters from Content-Type field */
873 for (ap = ci->ci_attrs, ep = ci->ci_values;
875 snprintf(bp, buflen, "%s%s=\"%s\"",
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;
912 part = part->mp_next) {
915 snprintf(bp, buflen, "%s'%s'",
923 ** set our starting pointer back to bp,
924 ** to avoid requoting the filenames we
934 ** display listing prior to displaying content
940 /* insert subtype of content */
941 strncpy(bp, ci->ci_subtype, buflen);
945 /* insert character % */
958 /* Did we actually insert something? */
961 ** Insert single quote if not inside quotes
964 if (!quoted && buflen) {
966 memmove(pp + 1, pp, len);
971 /* Escape existing quotes */
972 while ((pp = strchr(pp, '\'')) && buflen > 3) {
974 memmove(pp + 3, pp, len);
982 ** If pp is still set, that means we ran
987 if (!quoted && buflen) {
1005 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1007 ** content_error would provide a more useful error message
1008 ** here, except that if we got overrun, it probably would
1011 fprintf(stderr, "Buffer overflow constructing show command!\n");
1015 /* use charset string to modify display method */
1016 if (ct->c_termproc) {
1019 strncpy(term, buffer, sizeof(term));
1020 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1023 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1024 NOTOK, xlist, 0, xtty);
1029 ** show content of type "message/rfc822"
1033 show_message_rfc822(CT ct, int serial, int alternate)
1035 char *cp, buffer[BUFSIZ];
1036 CI ci = &ct->c_ctinfo;
1038 /* Check for mhshow-show-type/subtype */
1039 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1040 invo_name, ci->ci_type, ci->ci_subtype);
1041 if ((cp = context_find(buffer)) && *cp != '\0')
1042 return show_content_aux(ct, serial, alternate, cp, NULL);
1044 /* Check for mhshow-show-type */
1045 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1046 if ((cp = context_find(buffer)) && *cp != '\0')
1047 return show_content_aux(ct, serial, alternate, cp, NULL);
1049 if ((cp = ct->c_showproc))
1050 return show_content_aux(ct, serial, alternate, cp, NULL);
1052 /* default method for message/rfc822 */
1053 if (ct->c_subtype == MESSAGE_RFC822) {
1054 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1055 return show_content_aux(ct, serial, alternate, cp, NULL);
1058 /* complain if we are not a part of a multipart/alternative */
1060 content_error(NULL, ct, "don't know how to display content");
1067 ** Show content of type "message/partial".
1071 show_partial(CT ct, int serial, int alternate)
1073 content_error(NULL, ct,
1074 "in order to display this message, you must reassemble it");
1080 ** Show content of type "message/external".
1082 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1086 show_external(CT ct, int serial, int alternate)
1088 struct exbody *e = (struct exbody *) ct->c_ctparams;
1089 CT p = e->eb_content;
1094 return show_switch(p, serial, alternate);
1097 content_error(NULL, p, "don't know how to display content");