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 switch (child_id = fork()) {
210 adios("fork", "unable to");
214 execvp(mhlproc, vec);
215 fprintf(stderr, "unable to exec ");
228 ** Switching routine. Call the correct routine
229 ** based on content type.
233 show_switch(CT ct, int serial, int alternate)
235 switch (ct->c_type) {
237 return show_multi(ct, serial, alternate);
241 switch (ct->c_subtype) {
242 case MESSAGE_PARTIAL:
243 return show_partial(ct, serial, alternate);
246 case MESSAGE_EXTERNAL:
247 return show_external(ct, serial, alternate);
252 return show_message_rfc822(ct, serial,
259 return show_text(ct, serial, alternate);
266 return show_content(ct, serial, alternate);
270 adios(NULL, "unknown content type %d", ct->c_type);
274 return 0; /* NOT REACHED */
279 ** Generic method for displaying content
283 show_content(CT ct, int serial, int alternate)
285 char *cp, buffer[BUFSIZ];
286 CI ci = &ct->c_ctinfo;
288 /* Check for mhshow-show-type/subtype */
289 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
290 invo_name, ci->ci_type, ci->ci_subtype);
291 if ((cp = context_find(buffer)) && *cp != '\0')
292 return show_content_aux(ct, serial, alternate, cp, NULL);
294 /* Check for mhshow-show-type */
295 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
296 if ((cp = context_find(buffer)) && *cp != '\0')
297 return show_content_aux(ct, serial, alternate, cp, NULL);
299 if ((cp = ct->c_showproc))
300 return show_content_aux(ct, serial, alternate, cp, NULL);
302 /* complain if we are not a part of a multipart/alternative */
304 content_error(NULL, ct, "don't know how to display content");
311 ** Parse the display string for displaying generic content
315 show_content_aux(CT ct, int serial, int alternate, char *cp, char *cracked)
317 int fd, len, buflen, quoted;
318 int xstdin, xlist, xtty;
319 char *bp, *pp, *file, buffer[BUFSIZ];
320 CI ci = &ct->c_ctinfo;
322 if (!ct->c_ceopenfnx) {
324 content_error(NULL, ct,
325 "don't know how to decode content");
331 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
333 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
334 return (alternate ? DONE : OK);
341 strncpy(buffer, cp, sizeof(buffer));
345 /* get buffer ready to go */
347 buflen = sizeof(buffer) - 1;
348 bp[0] = bp[buflen] = '\0';
351 /* Now parse display string */
352 for ( ; *cp && buflen > 0; cp++) {
358 /* insert parameters from Content-Type field */
363 for (ap = ci->ci_attrs, ep = ci->ci_values;
365 snprintf(bp, buflen, "%s%s=\"%s\"",
376 /* insert content description */
380 s = trimcpy(ct->c_descr);
381 strncpy(bp, s, buflen);
387 /* exclusive execution */
392 /* %e, %f, and stdin is terminal not content */
398 /* insert filename containing content */
399 snprintf(bp, buflen, "'%s'", file);
401 ** since we've quoted the file argument,
402 ** set things up to look past it, to avoid
403 ** problems with the quoting logic below.
404 ** (I know, I should figure out what's
405 ** broken with the quoting logic, but..)
416 ** display listing prior to displaying content
422 /* insert subtype of content */
423 strncpy(bp, ci->ci_subtype, buflen);
427 /* insert character % */
440 /* Did we actually insert something? */
443 ** Insert single quote if not inside quotes
446 if (!quoted && buflen) {
448 memmove(pp + 1, pp, len);
453 /* Escape existing quotes */
454 while ((pp = strchr(pp, '\'')) && buflen > 3) {
456 memmove(pp + 3, pp, len);
464 ** If pp is still set, that means we ran
469 if (!quoted && buflen) {
486 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
488 ** content_error would provide a more useful error message
489 ** here, except that if we got overrun, it probably would
492 fprintf(stderr, "Buffer overflow constructing show command!\n");
496 /* use charset string to modify display method */
497 if (ct->c_termproc) {
500 strncpy(term, buffer, sizeof(term));
501 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
505 return show_content_aux2(ct, serial, alternate, cracked, buffer,
506 fd, xlist, xstdin, xtty);
511 ** Routine to actually display the content
515 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
516 char *buffer, int fd, int xlist, int xstdin, int xtty)
519 char *vec[4], exec[BUFSIZ + sizeof "exec "];
521 if (debugsw || cracked) {
524 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
527 fprintf(stderr, " part %s", ct->c_partno);
529 fprintf(stderr, " using command (cd %s; %s)\n",
532 fprintf(stderr, " using command %s\n", buffer);
535 if (xpid < 0 || (xtty && xpid)) {
538 pidcheck(pidwait(xpid, NOTOK));
543 if (ct->c_type == CT_MULTIPART)
544 list_content(ct, -1, 1, 0, 0);
546 list_switch(ct, -1, 1, 0, 0);
549 snprintf(exec, sizeof(exec), "exec %s", buffer);
558 switch (child_id = fork()) {
560 advise("fork", "unable to");
561 (*ct->c_ceclosefnx) (ct);
570 execvp("/bin/sh", vec);
571 fprintf(stderr, "unable to exec ");
578 ct->c_pid = child_id;
582 pidcheck(pidXwait(child_id, NULL));
586 (*ct->c_ceclosefnx) (ct);
587 return (alternate ? DONE : OK);
593 ** show content of type "text"
597 show_text(CT ct, int serial, int alternate)
599 char *cp, buffer[BUFSIZ];
600 CI ci = &ct->c_ctinfo;
602 /* Check for mhshow-show-type/subtype */
603 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
604 invo_name, ci->ci_type, ci->ci_subtype);
605 if ((cp = context_find(buffer)) && *cp != '\0')
606 return show_content_aux(ct, serial, alternate, cp, NULL);
608 /* Check for mhshow-show-type */
609 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
610 if ((cp = context_find(buffer)) && *cp != '\0')
611 return show_content_aux(ct, serial, alternate, cp, NULL);
614 ** Use default method if content is text/plain, or if
615 ** if it is not a text part of a multipart/alternative
617 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
618 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'",
619 progsw ? progsw : moreproc);
620 cp = (ct->c_showproc = getcpy(buffer));
621 return show_content_aux(ct, serial, alternate, cp, NULL);
629 ** show message body of type "multipart"
633 show_multi(CT ct, int serial, int alternate)
635 char *cp, buffer[BUFSIZ];
636 CI ci = &ct->c_ctinfo;
638 /* Check for mhshow-show-type/subtype */
639 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
640 invo_name, ci->ci_type, ci->ci_subtype);
641 if ((cp = context_find(buffer)) && *cp != '\0')
642 return show_multi_aux(ct, serial, alternate, cp);
644 /* Check for mhshow-show-type */
645 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
646 if ((cp = context_find(buffer)) && *cp != '\0')
647 return show_multi_aux(ct, serial, alternate, cp);
649 if ((cp = ct->c_showproc))
650 return show_multi_aux(ct, serial, alternate, cp);
653 ** Use default method to display this multipart content
654 ** if it is not a (nested) part of a multipart/alternative,
655 ** or if it is one of the known subtypes of multipart.
657 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
658 return show_multi_internal(ct, serial, alternate);
665 ** show message body of subtypes of multipart that
666 ** we understand directly (mixed, alternate, etc...)
670 show_multi_internal(CT ct, int serial, int alternate)
672 int alternating, nowalternate, nowserial, result;
673 struct multipart *m = (struct multipart *) ct->c_ctparams;
679 nowalternate = alternate;
681 if (ct->c_subtype == MULTI_PARALLEL) {
682 nowserial = serialsw;
683 } else if (ct->c_subtype == MULTI_ALTERNATE) {
691 ** unknown subtypes of multipart (treat as mixed per rfc2046)
696 /* block a few signals */
699 sigaddset(&set, SIGHUP);
700 sigaddset(&set, SIGINT);
701 sigaddset(&set, SIGQUIT);
702 sigaddset(&set, SIGTERM);
703 SIGPROCMASK(SIG_BLOCK, &set, &oset);
707 ** alternate -> we are a part inside an multipart/alternative
708 ** alternating -> we are a multipart/alternative
711 result = alternate ? NOTOK : OK;
713 for (part = m->mp_parts; part; part = part->mp_next) {
716 if (part_ok(p, 0) && type_ok(p, 0)) {
719 inneresult = show_switch(p, nowserial, nowalternate);
720 switch (inneresult) {
722 if (alternate && !alternating) {
735 alternate = nowalternate = 0;
745 if (alternating && !part) {
747 content_error(NULL, ct, "don't know how to display any of the contents");
752 if (serial && !nowserial) {
755 #ifdef HAVE_UNION_WAIT
762 for (part = m->mp_parts; part; part = part->mp_next) {
766 if (kill(p->c_pid, 0) == NOTOK)
773 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
774 #ifdef HAVE_UNION_WAIT
775 pidcheck(status.w_status);
780 for (part = m->mp_parts; part; part = part->mp_next) {
785 if (p->c_pid == pid) {
796 /* reset the signal mask */
797 SIGPROCMASK(SIG_SETMASK, &oset, &set);
805 ** Parse display string for multipart content
806 ** and use external program to display it.
810 show_multi_aux(CT ct, int serial, int alternate, char *cp)
812 int len, buflen, quoted;
814 char *bp, *pp, *file, buffer[BUFSIZ];
815 struct multipart *m = (struct multipart *) ct->c_ctparams;
817 CI ci = &ct->c_ctinfo;
820 for (part = m->mp_parts; part; part = part->mp_next) {
823 if (!p->c_ceopenfnx) {
825 content_error(NULL, p, "don't know how to decode content");
829 if (p->c_storage == NULL) {
831 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
834 /* I'm not sure if this is necessary? */
835 p->c_storage = getcpy(file);
837 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
838 return (alternate ? DONE : OK);
839 (*p->c_ceclosefnx) (p);
846 /* get buffer ready to go */
848 buflen = sizeof(buffer) - 1;
849 bp[0] = bp[buflen] = '\0';
852 /* Now parse display string */
853 for ( ; *cp && buflen > 0; cp++) {
858 /* insert parameters from Content-Type field */
863 for (ap = ci->ci_attrs, ep = ci->ci_values;
865 snprintf(bp, buflen, "%s%s=\"%s\"",
876 /* insert content description */
880 s = trimcpy(ct->c_descr);
881 strncpy(bp, s, buflen);
887 /* exclusive execution */
897 /* insert filename(s) containing content */
901 for (part = m->mp_parts; part;
902 part = part->mp_next) {
905 snprintf(bp, buflen, "%s'%s'",
913 ** set our starting pointer back to bp,
914 ** to avoid requoting the filenames we
924 ** display listing prior to displaying content
930 /* insert subtype of content */
931 strncpy(bp, ci->ci_subtype, buflen);
935 /* insert character % */
948 /* Did we actually insert something? */
951 ** Insert single quote if not inside quotes
954 if (!quoted && buflen) {
956 memmove(pp + 1, pp, len);
961 /* Escape existing quotes */
962 while ((pp = strchr(pp, '\'')) && buflen > 3) {
964 memmove(pp + 3, pp, len);
972 ** If pp is still set, that means we ran
977 if (!quoted && buflen) {
995 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
997 ** content_error would provide a more useful error message
998 ** here, except that if we got overrun, it probably would
1001 fprintf(stderr, "Buffer overflow constructing show command!\n");
1005 /* use charset string to modify display method */
1006 if (ct->c_termproc) {
1009 strncpy(term, buffer, sizeof(term));
1010 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1013 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1014 NOTOK, xlist, 0, xtty);
1019 ** show content of type "message/rfc822"
1023 show_message_rfc822(CT ct, int serial, int alternate)
1025 char *cp, buffer[BUFSIZ];
1026 CI ci = &ct->c_ctinfo;
1028 /* Check for mhshow-show-type/subtype */
1029 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1030 invo_name, ci->ci_type, ci->ci_subtype);
1031 if ((cp = context_find(buffer)) && *cp != '\0')
1032 return show_content_aux(ct, serial, alternate, cp, NULL);
1034 /* Check for mhshow-show-type */
1035 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1036 if ((cp = context_find(buffer)) && *cp != '\0')
1037 return show_content_aux(ct, serial, alternate, cp, NULL);
1039 if ((cp = ct->c_showproc))
1040 return show_content_aux(ct, serial, alternate, cp, NULL);
1042 /* default method for message/rfc822 */
1043 if (ct->c_subtype == MESSAGE_RFC822) {
1044 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1045 return show_content_aux(ct, serial, alternate, cp, NULL);
1048 /* complain if we are not a part of a multipart/alternative */
1050 content_error(NULL, ct, "don't know how to display content");
1057 ** Show content of type "message/partial".
1061 show_partial(CT ct, int serial, int alternate)
1063 content_error(NULL, ct,
1064 "in order to display this message, you must reassemble it");
1070 ** Show content of type "message/external".
1072 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1076 show_external(CT ct, int serial, int alternate)
1078 struct exbody *e = (struct exbody *) ct->c_ctparams;
1079 CT p = e->eb_content;
1084 return show_switch(p, serial, alternate);
1087 content_error(NULL, p, "don't know how to display content");