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>
35 void content_error(char *, CT, char *, ...);
36 void flush_errors(void);
39 int list_switch(CT, int, int, int, int);
40 int list_content(CT, int, int, int, int);
45 void show_all_messages(CT *);
46 int show_content_aux(CT, int, char *, char *);
51 static void show_single_message(CT, char *);
52 static void DisplayMsgHeader(CT, char *);
53 static int show_switch(CT, int);
54 static int show_content(CT, int);
55 static int show_content_aux2(CT, int, char *, char *, int, int, int, int);
56 static int show_text(CT, int);
57 static int show_multi(CT, int);
58 static int show_multi_internal(CT, int);
59 static int show_multi_aux(CT, int, char *);
60 static int show_message_rfc822(CT, int);
61 static int show_partial(CT, int);
62 static int show_external(CT, int);
66 ** Top level entry point to show/display a group of messages
70 show_all_messages(CT *cts)
75 ** If form is not specified, then get default form
76 ** for showing headers of MIME messages.
79 formsw = getcpy(etcpath("mhl.headers"));
82 ** If form is "mhl.null", suppress display of header.
84 if (strcmp(formsw, "mhl.null")==0)
87 for (ctp = cts; *ctp; ctp++) {
90 /* if top-level type is ok, then display message */
92 show_single_message(ct, formsw);
98 ** Entry point to show/display a single message
102 show_single_message(CT ct, char *form)
108 ** Allow user executable bit so that temporary directories created by
109 ** the viewer (e.g., lynx) are going to be accessible
111 umask(ct->c_umask & ~(0100));
114 ** If you have a format file, then display
115 ** the message headers.
118 DisplayMsgHeader(ct, form);
122 /* Show the body of the message */
129 if (ct->c_ceclosefnx)
130 (*ct->c_ceclosefnx) (ct);
132 /* block a few signals */
134 sigaddset(&set, SIGHUP);
135 sigaddset(&set, SIGINT);
136 sigaddset(&set, SIGQUIT);
137 sigaddset(&set, SIGTERM);
138 sigprocmask(SIG_BLOCK, &set, &oset);
140 while (wait(&status) != NOTOK) {
145 /* reset the signal mask */
146 sigprocmask(SIG_SETMASK, &oset, &set);
154 ** Use mhl to show the header fields
157 DisplayMsgHeader(CT ct, char *form)
165 vec[vecp++] = "-form";
167 vec[vecp++] = "-nobody";
168 vec[vecp++] = ct->c_file;
173 switch (child_id = fork()) {
175 adios("fork", "unable to");
180 fprintf(stderr, "unable to exec ");
193 ** Switching routine. Call the correct routine
194 ** based on content type.
198 show_switch(CT ct, int alternate)
200 switch (ct->c_type) {
202 return show_multi(ct, alternate);
206 switch (ct->c_subtype) {
207 case MESSAGE_PARTIAL:
208 return show_partial(ct, alternate);
211 case MESSAGE_EXTERNAL:
212 return show_external(ct, alternate);
217 return show_message_rfc822(ct, alternate);
223 return show_text(ct, alternate);
230 return show_content(ct, alternate);
234 adios(NULL, "unknown content type %d", ct->c_type);
238 return 0; /* NOT REACHED */
243 ** Generic method for displaying content
247 show_content(CT ct, int alternate)
249 char *cp, buffer[BUFSIZ];
250 CI ci = &ct->c_ctinfo;
252 /* Check for mhshow-show-type/subtype */
253 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
254 invo_name, ci->ci_type, ci->ci_subtype);
255 if ((cp = context_find(buffer)) && *cp != '\0')
256 return show_content_aux(ct, alternate, cp, NULL);
258 /* Check for mhshow-show-type */
259 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
260 if ((cp = context_find(buffer)) && *cp != '\0')
261 return show_content_aux(ct, alternate, cp, NULL);
263 if ((cp = ct->c_showproc))
264 return show_content_aux(ct, alternate, cp, NULL);
266 /* complain if we are not a part of a multipart/alternative */
268 content_error(NULL, ct, "don't know how to display content");
275 ** Parse the display string for displaying generic content
279 show_content_aux(CT ct, int alternate, char *cp, char *cracked)
281 int fd, len, buflen, quoted;
282 int xstdin, xlist, xtty;
283 char *bp, *pp, *file, buffer[BUFSIZ];
284 CI ci = &ct->c_ctinfo;
286 if (!ct->c_ceopenfnx) {
288 content_error(NULL, ct,
289 "don't know how to decode content");
295 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
297 if (ct->c_showproc && strcmp(ct->c_showproc, "true")==0)
298 return (alternate ? DONE : OK);
305 strncpy(buffer, cp, sizeof(buffer));
309 /* get buffer ready to go */
311 buflen = sizeof(buffer) - 1;
312 bp[0] = bp[buflen] = '\0';
315 /* Now parse display string */
316 for ( ; *cp && buflen > 0; cp++) {
322 /* insert parameters from Content-Type field */
327 for (ap = ci->ci_attrs, ep = ci->ci_values;
329 snprintf(bp, buflen, "%s%s=\"%s\"",
340 /* insert content description */
344 s = trimcpy(ct->c_descr);
345 strncpy(bp, s, buflen);
351 /* exclusive execution */
356 /* %e, %f, and stdin is terminal not content */
362 /* insert filename containing content */
363 snprintf(bp, buflen, "'%s'", file);
365 ** since we've quoted the file argument,
366 ** set things up to look past it, to avoid
367 ** problems with the quoting logic below.
368 ** (I know, I should figure out what's
369 ** broken with the quoting logic, but..)
380 ** display listing prior to displaying content
386 /* insert subtype of content */
387 strncpy(bp, ci->ci_subtype, buflen);
391 /* insert character % */
404 /* Did we actually insert something? */
407 ** Insert single quote if not inside quotes
410 if (!quoted && buflen) {
412 memmove(pp + 1, pp, len);
417 /* Escape existing quotes */
418 while ((pp = strchr(pp, '\'')) && buflen > 3) {
420 memmove(pp + 3, pp, len);
428 ** If pp is still set, that means we ran
433 if (!quoted && buflen) {
450 if (buflen <= 0 || (ct->c_termproc &&
451 (size_t)buflen <= strlen(ct->c_termproc))) {
453 ** content_error would provide a more useful error message
454 ** here, except that if we got overrun, it probably would
457 fprintf(stderr, "Buffer overflow constructing show command!\n");
461 /* use charset string to modify display method */
462 if (ct->c_termproc) {
465 strncpy(term, buffer, sizeof(term));
466 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
470 return show_content_aux2(ct, alternate, cracked, buffer,
471 fd, xlist, xstdin, xtty);
476 ** Routine to actually display the content
480 show_content_aux2(CT ct, int alternate, char *cracked,
481 char *buffer, int fd, int xlist, int xstdin, int xtty)
484 char *vec[4], exec[BUFSIZ + sizeof "exec "];
486 if (debugsw || cracked) {
489 fprintf(stderr, "%s msg %s", cracked ? "storing" : "show",
492 fprintf(stderr, " part %s", ct->c_partno);
494 fprintf(stderr, " using command (cd %s; %s)\n",
497 fprintf(stderr, " using command %s\n", buffer);
500 if (xpid < 0 || (xtty && xpid)) {
503 pidcheck(pidwait(xpid, NOTOK));
508 if (ct->c_type == CT_MULTIPART)
509 list_content(ct, -1, 1, 0, 0);
511 list_switch(ct, -1, 1, 0, 0);
514 snprintf(exec, sizeof(exec), "exec %s", buffer);
523 switch (child_id = fork()) {
525 advise("fork", "unable to");
526 (*ct->c_ceclosefnx) (ct);
535 execvp("/bin/sh", vec);
536 fprintf(stderr, "unable to exec ");
542 pidcheck(pidXwait(child_id, NULL));
545 (*ct->c_ceclosefnx) (ct);
546 return (alternate ? DONE : OK);
552 ** show content of type "text"
556 show_text(CT ct, int alternate)
558 char *cp, buffer[BUFSIZ];
559 CI ci = &ct->c_ctinfo;
561 /* Check for mhshow-show-type/subtype */
562 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
563 invo_name, ci->ci_type, ci->ci_subtype);
564 if ((cp = context_find(buffer)) && *cp != '\0')
565 return show_content_aux(ct, alternate, cp, NULL);
567 /* Check for mhshow-show-type */
568 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
569 if ((cp = context_find(buffer)) && *cp != '\0')
570 return show_content_aux(ct, alternate, cp, NULL);
573 ** Use default method if content is text/plain, or if
574 ** if it is not a text part of a multipart/alternative
576 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
577 snprintf(buffer, sizeof(buffer), "%%p%s '%%F'", defaultpager);
578 cp = (ct->c_showproc = getcpy(buffer));
579 return show_content_aux(ct, alternate, cp, NULL);
587 ** show message body of type "multipart"
591 show_multi(CT ct, int alternate)
593 char *cp, buffer[BUFSIZ];
594 CI ci = &ct->c_ctinfo;
596 /* Check for mhshow-show-type/subtype */
597 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
598 invo_name, ci->ci_type, ci->ci_subtype);
599 if ((cp = context_find(buffer)) && *cp != '\0')
600 return show_multi_aux(ct, alternate, cp);
602 /* Check for mhshow-show-type */
603 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
604 if ((cp = context_find(buffer)) && *cp != '\0')
605 return show_multi_aux(ct, alternate, cp);
607 if ((cp = ct->c_showproc))
608 return show_multi_aux(ct, alternate, cp);
611 ** Use default method to display this multipart content
612 ** if it is not a (nested) part of a multipart/alternative,
613 ** or if it is one of the known subtypes of multipart.
615 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
616 return show_multi_internal(ct, alternate);
623 ** show message body of subtypes of multipart that
624 ** we understand directly (mixed, alternate, etc...)
628 show_multi_internal(CT ct, int alternate)
630 int alternating, nowalternate, result;
631 struct multipart *m = (struct multipart *) ct->c_ctparams;
636 nowalternate = alternate;
638 if (ct->c_subtype == MULTI_ALTERNATE) {
644 ** Other possible multipart types are:
645 ** - multipart/parallel
647 ** - multipart/digest
648 ** - unknown subtypes of multipart (treat as mixed per rfc2046)
652 ** alternate -> we are a part inside an multipart/alternative
653 ** alternating -> we are a multipart/alternative
656 result = alternate ? NOTOK : OK;
658 for (part = m->mp_parts; part; part = part->mp_next) {
661 if (part_ok(p, 1) && type_ok(p, 1)) {
664 inneresult = show_switch(p, nowalternate);
665 switch (inneresult) {
667 if (alternate && !alternating) {
680 alternate = nowalternate = 0;
690 if (alternating && !part) {
692 content_error(NULL, ct, "don't know how to display any of the contents");
702 ** Parse display string for multipart content
703 ** and use external program to display it.
707 show_multi_aux(CT ct, int alternate, char *cp)
709 int len, buflen, quoted;
711 char *bp, *pp, *file, buffer[BUFSIZ];
712 struct multipart *m = (struct multipart *) ct->c_ctparams;
714 CI ci = &ct->c_ctinfo;
717 for (part = m->mp_parts; part; part = part->mp_next) {
720 if (!p->c_ceopenfnx) {
722 content_error(NULL, p, "don't know how to decode content");
726 if (p->c_storage == NULL) {
728 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
731 /* I'm not sure if this is necessary? */
732 p->c_storage = getcpy(file);
734 if (p->c_showproc && strcmp(p->c_showproc, "true")==0)
735 return (alternate ? DONE : OK);
736 (*p->c_ceclosefnx) (p);
743 /* get buffer ready to go */
745 buflen = sizeof(buffer) - 1;
746 bp[0] = bp[buflen] = '\0';
749 /* Now parse display string */
750 for ( ; *cp && buflen > 0; cp++) {
755 /* insert parameters from Content-Type field */
760 for (ap = ci->ci_attrs, ep = ci->ci_values;
762 snprintf(bp, buflen, "%s%s=\"%s\"",
773 /* insert content description */
777 s = trimcpy(ct->c_descr);
778 strncpy(bp, s, buflen);
784 /* exclusive execution */
794 /* insert filename(s) containing content */
798 for (part = m->mp_parts; part;
799 part = part->mp_next) {
802 snprintf(bp, buflen, "%s'%s'",
810 ** set our starting pointer back to bp,
811 ** to avoid requoting the filenames we
821 ** display listing prior to displaying content
827 /* insert subtype of content */
828 strncpy(bp, ci->ci_subtype, buflen);
832 /* insert character % */
845 /* Did we actually insert something? */
848 ** Insert single quote if not inside quotes
851 if (!quoted && buflen) {
853 memmove(pp + 1, pp, len);
858 /* Escape existing quotes */
859 while ((pp = strchr(pp, '\'')) && buflen > 3) {
861 memmove(pp + 3, pp, len);
869 ** If pp is still set, that means we ran
874 if (!quoted && buflen) {
891 if (buflen <= 0 || (ct->c_termproc &&
892 (size_t)buflen <= strlen(ct->c_termproc))) {
894 ** content_error would provide a more useful error message
895 ** here, except that if we got overrun, it probably would
898 fprintf(stderr, "Buffer overflow constructing show command!\n");
902 /* use charset string to modify display method */
903 if (ct->c_termproc) {
906 strncpy(term, buffer, sizeof(term));
907 snprintf(buffer, sizeof(buffer), ct->c_termproc, term);
910 return show_content_aux2(ct, alternate, NULL, buffer,
911 NOTOK, xlist, 0, xtty);
916 ** show content of type "message/rfc822"
920 show_message_rfc822(CT ct, int alternate)
922 char *cp, buffer[BUFSIZ];
923 CI ci = &ct->c_ctinfo;
925 /* Check for mhshow-show-type/subtype */
926 snprintf(buffer, sizeof(buffer), "%s-show-%s/%s",
927 invo_name, ci->ci_type, ci->ci_subtype);
928 if ((cp = context_find(buffer)) && *cp != '\0')
929 return show_content_aux(ct, alternate, cp, NULL);
931 /* Check for mhshow-show-type */
932 snprintf(buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
933 if ((cp = context_find(buffer)) && *cp != '\0')
934 return show_content_aux(ct, alternate, cp, NULL);
936 if ((cp = ct->c_showproc))
937 return show_content_aux(ct, alternate, cp, NULL);
939 /* default method for message/rfc822 */
940 if (ct->c_subtype == MESSAGE_RFC822) {
941 cp = (ct->c_showproc = getcpy("%pshow -file '%F'"));
942 return show_content_aux(ct, alternate, cp, NULL);
945 /* complain if we are not a part of a multipart/alternative */
947 content_error(NULL, ct, "don't know how to display content");
954 ** Show content of type "message/partial".
958 show_partial(CT ct, int alternate)
960 content_error(NULL, ct,
961 "in order to display this message, you must reassemble it");
967 ** Show content of type "message/external".
969 ** THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
973 show_external(CT ct, int alternate)
975 struct exbody *e = (struct exbody *) ct->c_ctparams;
976 CT p = e->eb_content;
981 return show_switch(p, alternate);
984 content_error(NULL, p, "don't know how to display content");