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)
49 void content_error(char *, CT, char *, ...);
50 void flush_errors(void);
53 int list_switch(CT, int, int, int, int);
54 int list_content(CT, int, int, int, int);
59 void show_all_messages(CT *);
60 int show_content_aux(CT, int, int, char *, char *);
65 static void show_single_message(CT, char *);
66 static void DisplayMsgHeader(CT, char *);
67 static int show_switch(CT, int, int);
68 static int show_content(CT, int, int);
69 static int show_content_aux2(CT, int, int, char *, char *, int, int, int, int);
70 static int show_text(CT, int, int);
71 static int show_multi(CT, int, int);
72 static int show_multi_internal(CT, int, int);
73 static int show_multi_aux(CT, int, int, char *);
74 static int show_message_rfc822(CT, int, int);
75 static int show_partial(CT, int, int);
76 static int show_external(CT, int, int);
80 ** Top level entry point to show/display a group of messages
84 show_all_messages(CT *cts)
89 ** If form is not specified, then get default form
90 ** for showing headers of MIME messages.
93 formsw = getcpy(etcpath("mhl.headers"));
96 ** If form is "mhl.null", suppress display of header.
98 if (strcmp(formsw, "mhl.null")==0)
101 for (ctp = cts; *ctp; ctp++) {
104 /* if top-level type is ok, then display message */
106 show_single_message(ct, formsw);
112 ** Entry point to show/display a single message
116 show_single_message(CT ct, char *form)
120 #ifdef HAVE_UNION_WAIT
127 ** Allow user executable bit so that temporary directories created by
128 ** the viewer (e.g., lynx) are going to be accessible
130 umask(ct->c_umask & ~(0100));
133 ** If you have a format file, then display
134 ** the message headers.
137 DisplayMsgHeader(ct, form);
141 /* Show the body of the message */
142 show_switch(ct, 1, 0);
148 if (ct->c_ceclosefnx)
149 (*ct->c_ceclosefnx) (ct);
151 /* block a few signals */
153 sigaddset(&set, SIGHUP);
154 sigaddset(&set, SIGINT);
155 sigaddset(&set, SIGQUIT);
156 sigaddset(&set, SIGTERM);
157 SIGPROCMASK(SIG_BLOCK, &set, &oset);
159 while (wait(&status) != NOTOK) {
160 #ifdef HAVE_UNION_WAIT
161 pidcheck(status.w_status);
168 /* reset the signal mask */
169 SIGPROCMASK(SIG_SETMASK, &oset, &set);
177 ** Use the mhlproc to show the header fields
181 DisplayMsgHeader(CT ct, char *form)
188 vec[vecp++] = mhbasename(mhlproc);
189 vec[vecp++] = "-form";
191 vec[vecp++] = "-nobody";
192 vec[vecp++] = ct->c_file;
197 switch (child_id = fork()) {
199 adios("fork", "unable to");
203 execvp(mhlproc, vec);
204 fprintf(stderr, "unable to exec ");
217 ** Switching routine. Call the correct routine
218 ** based on content type.
222 show_switch(CT ct, int serial, int alternate)
224 switch (ct->c_type) {
226 return show_multi(ct, serial, alternate);
230 switch (ct->c_subtype) {
231 case MESSAGE_PARTIAL:
232 return show_partial(ct, serial, alternate);
235 case MESSAGE_EXTERNAL:
236 return show_external(ct, serial, alternate);
241 return show_message_rfc822(ct, serial,
248 return show_text(ct, serial, alternate);
255 return show_content(ct, serial, alternate);
259 adios(NULL, "unknown content type %d", ct->c_type);
263 return 0; /* NOT REACHED */
268 ** Generic method for displaying content
272 show_content(CT ct, int serial, int alternate)
274 char *cp, buffer[BUFSIZ];
275 CI ci = &ct->c_ctinfo;
277 /* Check for mhshow-show-type/subtype */
278 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
279 invo_name, ci->ci_type, ci->ci_subtype);
280 if ((cp = context_find(buffer)) && *cp != '\0')
281 return show_content_aux(ct, serial, alternate, cp, NULL);
283 /* Check for mhshow-show-type */
284 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
285 if ((cp = context_find(buffer)) && *cp != '\0')
286 return show_content_aux(ct, serial, alternate, cp, NULL);
288 if ((cp = ct->c_showproc))
289 return show_content_aux(ct, serial, alternate, cp, NULL);
291 /* complain if we are not a part of a multipart/alternative */
293 content_error(NULL, ct, "don't know how to display content");
300 ** Parse the display string for displaying generic content
304 show_content_aux(CT ct, int serial, int alternate, char *cp, char *cracked)
306 int fd, len, buflen, quoted;
307 int xstdin, xlist, xtty;
308 char *bp, *pp, *file, buffer[BUFSIZ];
309 CI ci = &ct->c_ctinfo;
311 if (!ct->c_ceopenfnx) {
313 content_error(NULL, ct,
314 "don't know how to decode content");
320 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
322 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
323 return (alternate ? DONE : OK);
330 strncpy(buffer, cp, sizeof(buffer));
334 /* get buffer ready to go */
336 buflen = sizeof(buffer) - 1;
337 bp[0] = bp[buflen] = '\0';
340 /* Now parse display string */
341 for ( ; *cp && buflen > 0; cp++) {
347 /* insert parameters from Content-Type field */
352 for (ap = ci->ci_attrs, ep = ci->ci_values;
354 snprintf(bp, buflen, "%s%s=\"%s\"",
365 /* insert content description */
369 s = trimcpy(ct->c_descr);
370 strncpy(bp, s, buflen);
376 /* exclusive execution */
381 /* %e, %f, and stdin is terminal not content */
387 /* insert filename containing content */
388 snprintf(bp, buflen, "'%s'", file);
390 ** since we've quoted the file argument,
391 ** set things up to look past it, to avoid
392 ** problems with the quoting logic below.
393 ** (I know, I should figure out what's
394 ** broken with the quoting logic, but..)
405 ** display listing prior to displaying content
411 /* insert subtype of content */
412 strncpy(bp, ci->ci_subtype, buflen);
416 /* insert character % */
429 /* Did we actually insert something? */
432 ** Insert single quote if not inside quotes
435 if (!quoted && buflen) {
437 memmove(pp + 1, pp, len);
442 /* Escape existing quotes */
443 while ((pp = strchr(pp, '\'')) && buflen > 3) {
445 memmove(pp + 3, pp, len);
453 ** If pp is still set, that means we ran
458 if (!quoted && buflen) {
475 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
477 ** content_error would provide a more useful error message
478 ** here, except that if we got overrun, it probably would
481 fprintf(stderr, "Buffer overflow constructing show command!\n");
485 /* use charset string to modify display method */
486 if (ct->c_termproc) {
489 strncpy(term, buffer, sizeof(term));
490 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
494 return show_content_aux2(ct, serial, alternate, cracked, buffer,
495 fd, xlist, xstdin, xtty);
500 ** Routine to actually display the content
504 show_content_aux2(CT ct, int serial, int alternate, char *cracked,
505 char *buffer, int fd, int xlist, int xstdin, int xtty)
508 char *vec[4], exec[BUFSIZ + sizeof "exec "];
510 if (debugsw || cracked) {
513 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
516 fprintf(stderr, " part %s", ct->c_partno);
518 fprintf(stderr, " using command (cd %s; %s)\n",
521 fprintf(stderr, " using command %s\n", buffer);
524 if (xpid < 0 || (xtty && xpid)) {
527 pidcheck(pidwait(xpid, NOTOK));
532 if (ct->c_type == CT_MULTIPART)
533 list_content(ct, -1, 1, 0, 0);
535 list_switch(ct, -1, 1, 0, 0);
538 snprintf(exec, sizeof(exec), "exec %s", buffer);
547 switch (child_id = fork()) {
549 advise("fork", "unable to");
550 (*ct->c_ceclosefnx) (ct);
559 execvp("/bin/sh", vec);
560 fprintf(stderr, "unable to exec ");
567 ct->c_pid = child_id;
571 pidcheck(pidXwait(child_id, NULL));
575 (*ct->c_ceclosefnx) (ct);
576 return (alternate ? DONE : OK);
582 ** show content of type "text"
586 show_text(CT ct, int serial, int alternate)
588 char *cp, buffer[BUFSIZ];
589 CI ci = &ct->c_ctinfo;
591 /* Check for mhshow-show-type/subtype */
592 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
593 invo_name, ci->ci_type, ci->ci_subtype);
594 if ((cp = context_find(buffer)) && *cp != '\0')
595 return show_content_aux(ct, serial, alternate, cp, NULL);
597 /* Check for mhshow-show-type */
598 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
599 if ((cp = context_find(buffer)) && *cp != '\0')
600 return show_content_aux(ct, serial, alternate, cp, NULL);
603 ** Use default method if content is text/plain, or if
604 ** if it is not a text part of a multipart/alternative
606 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
607 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'", defaultpager);
608 cp = (ct->c_showproc = getcpy(buffer));
609 return show_content_aux(ct, serial, alternate, cp, NULL);
617 ** show message body of type "multipart"
621 show_multi(CT ct, int serial, int alternate)
623 char *cp, buffer[BUFSIZ];
624 CI ci = &ct->c_ctinfo;
626 /* Check for mhshow-show-type/subtype */
627 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
628 invo_name, ci->ci_type, ci->ci_subtype);
629 if ((cp = context_find(buffer)) && *cp != '\0')
630 return show_multi_aux(ct, serial, alternate, cp);
632 /* Check for mhshow-show-type */
633 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
634 if ((cp = context_find(buffer)) && *cp != '\0')
635 return show_multi_aux(ct, serial, alternate, cp);
637 if ((cp = ct->c_showproc))
638 return show_multi_aux(ct, serial, alternate, cp);
641 ** Use default method to display this multipart content
642 ** if it is not a (nested) part of a multipart/alternative,
643 ** or if it is one of the known subtypes of multipart.
645 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
646 return show_multi_internal(ct, serial, alternate);
653 ** show message body of subtypes of multipart that
654 ** we understand directly (mixed, alternate, etc...)
658 show_multi_internal(CT ct, int serial, int alternate)
660 int alternating, nowalternate, nowserial, result;
661 struct multipart *m = (struct multipart *) ct->c_ctparams;
667 nowalternate = alternate;
669 if (ct->c_subtype == MULTI_PARALLEL) {
670 nowserial = serialsw;
671 } else if (ct->c_subtype == MULTI_ALTERNATE) {
679 ** unknown subtypes of multipart (treat as mixed per rfc2046)
684 /* block a few signals */
687 sigaddset(&set, SIGHUP);
688 sigaddset(&set, SIGINT);
689 sigaddset(&set, SIGQUIT);
690 sigaddset(&set, SIGTERM);
691 SIGPROCMASK(SIG_BLOCK, &set, &oset);
695 ** alternate -> we are a part inside an multipart/alternative
696 ** alternating -> we are a multipart/alternative
699 result = alternate ? NOTOK : OK;
701 for (part = m->mp_parts; part; part = part->mp_next) {
704 if (part_ok(p, 0) && type_ok(p, 0)) {
707 inneresult = show_switch(p, nowserial, nowalternate);
708 switch (inneresult) {
710 if (alternate && !alternating) {
723 alternate = nowalternate = 0;
733 if (alternating && !part) {
735 content_error(NULL, ct, "don't know how to display any of the contents");
740 if (serial && !nowserial) {
743 #ifdef HAVE_UNION_WAIT
750 for (part = m->mp_parts; part; part = part->mp_next) {
754 if (kill(p->c_pid, 0) == NOTOK)
761 while (kids > 0 && (pid = wait(&status)) != NOTOK) {
762 #ifdef HAVE_UNION_WAIT
763 pidcheck(status.w_status);
768 for (part = m->mp_parts; part; part = part->mp_next) {
773 if (p->c_pid == pid) {
784 /* reset the signal mask */
785 SIGPROCMASK(SIG_SETMASK, &oset, &set);
793 ** Parse display string for multipart content
794 ** and use external program to display it.
798 show_multi_aux(CT ct, int serial, int alternate, char *cp)
800 int len, buflen, quoted;
802 char *bp, *pp, *file, buffer[BUFSIZ];
803 struct multipart *m = (struct multipart *) ct->c_ctparams;
805 CI ci = &ct->c_ctinfo;
808 for (part = m->mp_parts; part; part = part->mp_next) {
811 if (!p->c_ceopenfnx) {
813 content_error(NULL, p, "don't know how to decode content");
817 if (p->c_storage == NULL) {
819 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
822 /* I'm not sure if this is necessary? */
823 p->c_storage = getcpy(file);
825 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
826 return (alternate ? DONE : OK);
827 (*p->c_ceclosefnx) (p);
834 /* get buffer ready to go */
836 buflen = sizeof(buffer) - 1;
837 bp[0] = bp[buflen] = '\0';
840 /* Now parse display string */
841 for ( ; *cp && buflen > 0; cp++) {
846 /* insert parameters from Content-Type field */
851 for (ap = ci->ci_attrs, ep = ci->ci_values;
853 snprintf(bp, buflen, "%s%s=\"%s\"",
864 /* insert content description */
868 s = trimcpy(ct->c_descr);
869 strncpy(bp, s, buflen);
875 /* exclusive execution */
885 /* insert filename(s) containing content */
889 for (part = m->mp_parts; part;
890 part = part->mp_next) {
893 snprintf(bp, buflen, "%s'%s'",
901 ** set our starting pointer back to bp,
902 ** to avoid requoting the filenames we
912 ** display listing prior to displaying content
918 /* insert subtype of content */
919 strncpy(bp, ci->ci_subtype, buflen);
923 /* insert character % */
936 /* Did we actually insert something? */
939 ** Insert single quote if not inside quotes
942 if (!quoted && buflen) {
944 memmove(pp + 1, pp, len);
949 /* Escape existing quotes */
950 while ((pp = strchr(pp, '\'')) && buflen > 3) {
952 memmove(pp + 3, pp, len);
960 ** If pp is still set, that means we ran
965 if (!quoted && buflen) {
983 (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
985 ** content_error would provide a more useful error message
986 ** here, except that if we got overrun, it probably would
989 fprintf(stderr, "Buffer overflow constructing show command!\n");
993 /* use charset string to modify display method */
994 if (ct->c_termproc) {
997 strncpy(term, buffer, sizeof(term));
998 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
1001 return show_content_aux2(ct, serial, alternate, NULL, buffer,
1002 NOTOK, xlist, 0, xtty);
1007 ** show content of type "message/rfc822"
1011 show_message_rfc822(CT ct, int serial, int alternate)
1013 char *cp, buffer[BUFSIZ];
1014 CI ci = &ct->c_ctinfo;
1016 /* Check for mhshow-show-type/subtype */
1017 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
1018 invo_name, ci->ci_type, ci->ci_subtype);
1019 if ((cp = context_find(buffer)) && *cp != '\0')
1020 return show_content_aux(ct, serial, alternate, cp, NULL);
1022 /* Check for mhshow-show-type */
1023 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1024 if ((cp = context_find(buffer)) && *cp != '\0')
1025 return show_content_aux(ct, serial, alternate, cp, NULL);
1027 if ((cp = ct->c_showproc))
1028 return show_content_aux(ct, serial, alternate, cp, NULL);
1030 /* default method for message/rfc822 */
1031 if (ct->c_subtype == MESSAGE_RFC822) {
1032 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
1033 return show_content_aux(ct, serial, alternate, cp, NULL);
1036 /* complain if we are not a part of a multipart/alternative */
1038 content_error(NULL, ct, "don't know how to display content");
1045 ** Show content of type "message/partial".
1049 show_partial(CT ct, int serial, int alternate)
1051 content_error(NULL, ct,
1052 "in order to display this message, you must reassemble it");
1058 ** Show content of type "message/external".
1060 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1064 show_external(CT ct, int serial, int alternate)
1066 struct exbody *e = (struct exbody *) ct->c_ctparams;
1067 CT p = e->eb_content;
1072 return show_switch(p, serial, alternate);
1075 content_error(NULL, p, "don't know how to display content");