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);
629 cp = (ct->c_showproc = getcpy(buffer));
630 return show_content_aux(ct, serial, alternate, cp, NULL);
638 ** show message body of type "multipart"
642 show_multi(CT ct, int serial, int alternate)
644 char *cp, buffer[BUFSIZ];
645 CI ci = &ct->c_ctinfo;
647 /* Check for mhshow-show-type/subtype */
648 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
649 invo_name, ci->ci_type, ci->ci_subtype);
650 if ((cp = context_find(buffer)) && *cp != '\0')
651 return show_multi_aux(ct, serial, alternate, cp);
653 /* Check for mhshow-show-type */
654 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
655 if ((cp = context_find(buffer)) && *cp != '\0')
656 return show_multi_aux(ct, serial, alternate, cp);
658 if ((cp = ct->c_showproc))
659 return show_multi_aux(ct, serial, alternate, cp);
662 ** Use default method to display this multipart content
663 ** if it is not a (nested) part of a multipart/alternative,
664 ** or if it is one of the known subtypes of multipart.
666 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
667 return show_multi_internal(ct, serial, alternate);
674 ** show message body of subtypes of multipart that
675 ** we understand directly (mixed, alternate, etc...)
679 show_multi_internal(CT ct, int serial, int alternate)
681 int alternating, nowalternate, nowserial, result;
682 struct multipart *m = (struct multipart *) ct->c_ctparams;
688 nowalternate = alternate;
690 if (ct->c_subtype == MULTI_PARALLEL) {
691 nowserial = serialsw;
692 } else if (ct->c_subtype == MULTI_ALTERNATE) {
700 ** unknown subtypes of multipart (treat as mixed per rfc2046)
705 /* block a few signals */
708 sigaddset(&set, SIGHUP);
709 sigaddset(&set, SIGINT);
710 sigaddset(&set, SIGQUIT);
711 sigaddset(&set, SIGTERM);
712 SIGPROCMASK(SIG_BLOCK, &set, &oset);
716 ** alternate -> we are a part inside an multipart/alternative
717 ** alternating -> we are a multipart/alternative
720 result = alternate ? NOTOK : OK;
722 for (part = m->mp_parts; part; part = part->mp_next) {
725 if (part_ok(p, 0) && type_ok(p, 0)) {
728 inneresult = show_switch(p, nowserial, nowalternate);
729 switch (inneresult) {
731 if (alternate && !alternating) {
744 alternate = nowalternate = 0;
754 if (alternating && !part) {
756 content_error(NULL, ct, "don't know how to display any of the contents");
761 if (serial && !nowserial) {
764 #ifdef HAVE_UNION_WAIT
771 for (part = m->mp_parts; part; part = part->mp_next) {
775 if (kill(p->c_pid, 0) == NOTOK)
782 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
783 #ifdef HAVE_UNION_WAIT
784 pidcheck(status.w_status);
789 for (part = m->mp_parts; part; part = part->mp_next) {
794 if (p->c_pid == pid) {
805 /* reset the signal mask */
806 SIGPROCMASK(SIG_SETMASK, &oset, &set);
814 ** Parse display string for multipart content
815 ** and use external program to display it.
819 show_multi_aux(CT ct, int serial, int alternate, char *cp)
821 int len, buflen, quoted;
823 char *bp, *pp, *file, buffer[BUFSIZ];
824 struct multipart *m = (struct multipart *) ct->c_ctparams;
826 CI ci = &ct->c_ctinfo;
829 for (part = m->mp_parts; part; part = part->mp_next) {
832 if (!p->c_ceopenfnx) {
834 content_error(NULL, p, "don't know how to decode content");
838 if (p->c_storage == NULL) {
840 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
843 /* I'm not sure if this is necessary? */
844 p->c_storage = getcpy(file);
846 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
847 return (alternate ? DONE : OK);
848 (*p->c_ceclosefnx) (p);
855 /* get buffer ready to go */
857 buflen = sizeof(buffer) - 1;
858 bp[0] = bp[buflen] = '\0';
861 /* Now parse display string */
862 for ( ; *cp && buflen > 0; cp++) {
867 /* insert parameters from Content-Type field */
872 for (ap = ci->ci_attrs, ep = ci->ci_values;
874 snprintf(bp, buflen, "%s%s=\"%s\"",
885 /* insert content description */
889 s = trimcpy(ct->c_descr);
890 strncpy(bp, s, buflen);
896 /* exclusive execution */
906 /* insert filename(s) containing content */
910 for (part = m->mp_parts; part;
911 part = part->mp_next) {
914 snprintf(bp, buflen, "%s'%s'",
922 ** set our starting pointer back to bp,
923 ** to avoid requoting the filenames we
933 ** display listing prior to displaying content
939 /* insert subtype of content */
940 strncpy(bp, ci->ci_subtype, buflen);
944 /* insert character % */
957 /* Did we actually insert something? */
960 ** Insert single quote if not inside quotes
963 if (!quoted && buflen) {
965 memmove(pp + 1, pp, len);
970 /* Escape existing quotes */
971 while ((pp = strchr(pp, '\'')) && buflen > 3) {
973 memmove(pp + 3, pp, len);
981 ** If pp is still set, that means we ran
986 if (!quoted && buflen) {
1004 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
1006 ** content_error would provide a more useful error message
1007 ** here, except that if we got overrun, it probably would
1010 fprintf(stderr, "Buffer overflow constructing show command!\n");
1014 /* use charset string to modify display method */
1015 if (ct->c_termproc) {
1018 strncpy(term, buffer, sizeof(term));
1019 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1022 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1023 NOTOK, xlist, 0, xtty);
1028 ** show content of type "message/rfc822"
1032 show_message_rfc822(CT ct, int serial, int alternate)
1034 char *cp, buffer[BUFSIZ];
1035 CI ci = &ct->c_ctinfo;
1037 /* Check for mhshow-show-type/subtype */
1038 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1039 invo_name, ci->ci_type, ci->ci_subtype);
1040 if ((cp = context_find(buffer)) && *cp != '\0')
1041 return show_content_aux(ct, serial, alternate, cp, NULL);
1043 /* Check for mhshow-show-type */
1044 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1045 if ((cp = context_find(buffer)) && *cp != '\0')
1046 return show_content_aux(ct, serial, alternate, cp, NULL);
1048 if ((cp = ct->c_showproc))
1049 return show_content_aux(ct, serial, alternate, cp, NULL);
1051 /* default method for message/rfc822 */
1052 if (ct->c_subtype == MESSAGE_RFC822) {
1053 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1054 return show_content_aux(ct, serial, alternate, cp, NULL);
1057 /* complain if we are not a part of a multipart/alternative */
1059 content_error(NULL, ct, "don't know how to display content");
1066 ** Show content of type "message/partial".
1070 show_partial(CT ct, int serial, int alternate)
1072 content_error(NULL, ct,
1073 "in order to display this message, you must reassemble it");
1079 ** Show content of type "message/external".
1081 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1085 show_external(CT ct, int serial, int alternate)
1087 struct exbody *e = (struct exbody *) ct->c_ctparams;
1088 CT p = e->eb_content;
1093 return show_switch(p, serial, alternate);
1096 content_error(NULL, p, "don't know how to display content");