3 * mhshowsbr.c -- routines to display the contents of MIME messages
10 #include <h/signals.h>
15 #include <zotnet/mts/mts.h>
16 #include <zotnet/tws/tws.h>
18 #include <h/mhparse.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * Just use sigjmp/longjmp on older machines that
26 * don't have sigsetjmp/siglongjmp.
28 #ifndef HAVE_SIGSETJMP
29 # define sigjmp_buf jmp_buf
30 # define sigsetjmp(env,mask) setjmp(env)
31 # define siglongjmp(env,val) longjmp(env,val)
43 /* flags for moreproc/header display */
49 static sigjmp_buf intrenv;
53 int SOprintf (char *, ...);
59 int part_ok (CT, int);
60 int type_ok (CT, int);
61 void content_error (char *, CT, char *, ...);
62 void flush_errors (void);
65 int list_switch (CT, int, int, int, int);
66 int list_content (CT, int, int, int, int);
71 void show_all_messages (CT *);
72 int show_content_aux (CT, int, int, char *, char *);
77 static void show_single_message (CT, char *);
78 static void DisplayMsgHeader (CT, char *);
79 static int show_switch (CT, int, int);
80 static int show_content (CT, int, int);
81 static int show_content_aux2 (CT, int, int, char *, char *, int, int, int, int, int);
82 static int show_text (CT, int, int);
83 static int show_multi (CT, int, int);
84 static int show_multi_internal (CT, int, int);
85 static int show_multi_aux (CT, int, int, char *);
86 static int show_message_rfc822 (CT, int, int);
87 static int show_partial (CT, int, int);
88 static int show_external (CT, int, int);
89 static RETSIGTYPE intrser (int);
93 * Top level entry point to show/display a group of messages
97 show_all_messages (CT *cts)
102 * If form is not specified, then get default form
103 * for showing headers of MIME messages.
106 formsw = getcpy (etcpath ("mhl.headers"));
109 * If form is "mhl.null", suppress display of header.
111 if (!strcmp (formsw, "mhl.null"))
114 for (ctp = cts; *ctp; ctp++) {
117 /* if top-level type is ok, then display message */
119 show_single_message (ct, formsw);
125 * Entry point to show/display a single message
129 show_single_message (CT ct, char *form)
142 * If you have a format file, then display
143 * the message headers.
146 DisplayMsgHeader(ct, form);
150 /* Show the body of the message */
151 show_switch (ct, 1, 0);
157 if (ct->c_ceclosefnx)
158 (*ct->c_ceclosefnx) (ct);
160 /* block a few signals */
162 sigaddset (&set, SIGHUP);
163 sigaddset (&set, SIGINT);
164 sigaddset (&set, SIGQUIT);
165 sigaddset (&set, SIGTERM);
166 SIGPROCMASK (SIG_BLOCK, &set, &oset);
168 while (wait (&status) != NOTOK) {
172 pidcheck (status.w_status);
177 /* reset the signal mask */
178 SIGPROCMASK (SIG_SETMASK, &oset, &set);
186 * Use the mhlproc to show the header fields
190 DisplayMsgHeader (CT ct, char *form)
197 vec[vecp++] = r1bindex (mhlproc, '/');
198 vec[vecp++] = "-form";
200 vec[vecp++] = "-nobody";
201 vec[vecp++] = ct->c_file;
204 * If we've specified -(no)moreproc,
205 * then just pass that along.
208 vec[vecp++] = "-nomoreproc";
210 vec[vecp++] = "-moreproc";
211 vec[vecp++] = progsw;
217 for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++)
222 adios ("fork", "unable to");
226 execvp (mhlproc, vec);
227 fprintf (stderr, "unable to exec ");
240 * Switching routine. Call the correct routine
241 * based on content type.
245 show_switch (CT ct, int serial, int alternate)
247 switch (ct->c_type) {
249 return show_multi (ct, serial, alternate);
253 switch (ct->c_subtype) {
254 case MESSAGE_PARTIAL:
255 return show_partial (ct, serial, alternate);
258 case MESSAGE_EXTERNAL:
259 return show_external (ct, serial, alternate);
264 return show_message_rfc822 (ct, serial, alternate);
270 return show_text (ct, serial, alternate);
277 return show_content (ct, serial, alternate);
281 adios (NULL, "unknown content type %d", ct->c_type);
285 return 0; /* NOT REACHED */
290 * Generic method for displaying content
294 show_content (CT ct, int serial, int alternate)
296 char *cp, buffer[BUFSIZ];
297 CI ci = &ct->c_ctinfo;
299 /* Check for mhn-show-type/subtype */
300 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
301 invo_name, ci->ci_type, ci->ci_subtype);
302 if ((cp = context_find (buffer)) && *cp != '\0')
303 return show_content_aux (ct, serial, alternate, cp, NULL);
305 /* Check for mhn-show-type */
306 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
307 if ((cp = context_find (buffer)) && *cp != '\0')
308 return show_content_aux (ct, serial, alternate, cp, NULL);
310 if ((cp = ct->c_showproc))
311 return show_content_aux (ct, serial, alternate, cp, NULL);
313 /* complain if we are not a part of a multipart/alternative */
315 content_error (NULL, ct, "don't know how to display content");
322 * Parse the display string for displaying generic content
326 show_content_aux (CT ct, int serial, int alternate, char *cp, char *cracked)
329 int xstdin, xlist, xpause, xtty;
330 char *bp, *file, buffer[BUFSIZ];
331 CI ci = &ct->c_ctinfo;
333 if (!ct->c_ceopenfnx) {
335 content_error (NULL, ct, "don't know how to decode content");
341 if ((fd = (*ct->c_ceopenfnx) (ct, &file)) == NOTOK)
343 if (ct->c_showproc && !strcmp (ct->c_showproc, "true"))
344 return (alternate ? DONE : OK);
352 strncpy (buffer, cp, sizeof(buffer));
356 /* get buffer ready to go */
359 buflen = sizeof(buffer);
361 /* Now parse display string */
366 /* insert parameters from Content-Type field */
371 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
372 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
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);
409 /* %l, and pause prior to displaying content */
414 /* display listing prior to displaying content */
419 /* insert subtype of content */
420 strncpy (bp, ci->ci_subtype, buflen);
424 /* insert character % */
444 /* use charset string to modify display method */
445 if (ct->c_termproc) {
448 strncpy (term, buffer, sizeof(term));
449 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
453 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
454 fd, xlist, xpause, xstdin, xtty);
459 * Routine to actually display the content
463 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
464 int fd, int xlist, int xpause, int xstdin, int xtty)
468 char *vec[4], exec[BUFSIZ + sizeof "exec "];
470 if (debugsw || cracked) {
473 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
476 fprintf (stderr, " part %s", ct->c_partno);
478 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
480 fprintf (stderr, " using command %s\n", buffer);
483 if (xpid < 0 || (xtty && xpid)) {
486 pidcheck(pidwait (xpid, NOTOK));
493 if (ct->c_type == CT_MULTIPART)
494 list_content (ct, -1, 1, 0, 0);
496 list_switch (ct, -1, 1, 0, 0);
498 if (xpause && SOprintf ("Press <return> to show content..."))
499 printf ("Press <return> to show content...");
503 SIGNAL_HANDLER istat;
505 istat = SIGNAL (SIGINT, intrser);
506 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
509 read (fileno (stdout), prompt, sizeof(prompt));
511 SIGNAL (SIGINT, istat);
513 (*ct->c_ceclosefnx) (ct);
514 return (alternate ? DONE : NOTOK);
519 snprintf (exec, sizeof(exec), "exec %s", buffer);
528 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
532 advise ("fork", "unable to");
533 (*ct->c_ceclosefnx) (ct);
542 execvp ("/bin/sh", vec);
543 fprintf (stderr, "unable to exec ");
550 ct->c_pid = child_id;
554 pidcheck (pidXwait (child_id, NULL));
558 (*ct->c_ceclosefnx) (ct);
559 return (alternate ? DONE : OK);
565 * show content of type "text"
569 show_text (CT ct, int serial, int alternate)
571 char *cp, buffer[BUFSIZ];
572 CI ci = &ct->c_ctinfo;
574 /* Check for mhn-show-type/subtype */
575 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
576 invo_name, ci->ci_type, ci->ci_subtype);
577 if ((cp = context_find (buffer)) && *cp != '\0')
578 return show_content_aux (ct, serial, alternate, cp, NULL);
580 /* Check for mhn-show-type */
581 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
582 if ((cp = context_find (buffer)) && *cp != '\0')
583 return show_content_aux (ct, serial, alternate, cp, NULL);
586 * Use default method if content is text/plain, or if
587 * if it is not a text part of a multipart/alternative
589 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
590 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
591 moreproc && *moreproc ? moreproc : "more");
592 cp = (ct->c_showproc = add (buffer, NULL));
593 return show_content_aux (ct, serial, alternate, cp, NULL);
601 * show message body of type "multipart"
605 show_multi (CT ct, int serial, int alternate)
607 char *cp, buffer[BUFSIZ];
608 CI ci = &ct->c_ctinfo;
610 /* Check for mhn-show-type/subtype */
611 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
612 invo_name, ci->ci_type, ci->ci_subtype);
613 if ((cp = context_find (buffer)) && *cp != '\0')
614 return show_multi_aux (ct, serial, alternate, cp);
616 /* Check for mhn-show-type */
617 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
618 if ((cp = context_find (buffer)) && *cp != '\0')
619 return show_multi_aux (ct, serial, alternate, cp);
621 if ((cp = ct->c_showproc))
622 return show_multi_aux (ct, serial, alternate, cp);
625 * Use default method to display this multipart content
626 * if it is not a (nested) part of a multipart/alternative,
627 * or if it is one of the known subtypes of multipart.
629 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
630 return show_multi_internal (ct, serial, alternate);
637 * show message body of subtypes of multipart that
638 * we understand directly (mixed, alternate, etc...)
642 show_multi_internal (CT ct, int serial, int alternate)
644 int alternating, nowalternate, nowserial, result;
645 struct multipart *m = (struct multipart *) ct->c_ctparams;
651 nowalternate = alternate;
653 if (ct->c_subtype == MULTI_PARALLEL) {
654 nowserial = serialsw;
655 } else if (ct->c_subtype == MULTI_ALTERNATE) {
663 * unknown subtypes of multipart (treat as mixed per rfc2046)
668 /* block a few signals */
671 sigaddset (&set, SIGHUP);
672 sigaddset (&set, SIGINT);
673 sigaddset (&set, SIGQUIT);
674 sigaddset (&set, SIGTERM);
675 SIGPROCMASK (SIG_BLOCK, &set, &oset);
679 * alternate -> we are a part inside an multipart/alternative
680 * alternating -> we are a multipart/alternative
683 result = alternate ? NOTOK : OK;
685 for (part = m->mp_parts; part; part = part->mp_next) {
688 if (part_ok (p, 0) && type_ok (p, 0)) {
691 inneresult = show_switch (p, nowserial, nowalternate);
692 switch (inneresult) {
694 if (alternate && !alternating) {
707 alternate = nowalternate = 0;
717 if (alternating && !part) {
719 content_error (NULL, ct, "don't know how to display any of the contents");
724 if (serial && !nowserial) {
734 for (part = m->mp_parts; part; part = part->mp_next) {
738 if (kill (p->c_pid, 0) == NOTOK)
744 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
748 pidcheck (status.w_status);
751 for (part = m->mp_parts; part; part = part->mp_next) {
756 if (p->c_pid == pid) {
767 /* reset the signal mask */
768 SIGPROCMASK (SIG_SETMASK, &oset, &set);
776 * Parse display string for multipart content
777 * and use external program to display it.
781 show_multi_aux (CT ct, int serial, int alternate, char *cp)
784 int xlist, xpause, xtty;
785 char *bp, *file, buffer[BUFSIZ];
786 struct multipart *m = (struct multipart *) ct->c_ctparams;
788 CI ci = &ct->c_ctinfo;
791 for (part = m->mp_parts; part; part = part->mp_next) {
794 if (!p->c_ceopenfnx) {
796 content_error (NULL, p, "don't know how to decode content");
800 if (p->c_storage == NULL) {
802 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
805 /* I'm not sure if this is necessary? */
806 p->c_storage = add (file, NULL);
808 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
809 return (alternate ? DONE : OK);
810 (*p->c_ceclosefnx) (p);
818 /* get buffer ready to go */
821 buflen = sizeof(buffer);
823 /* Now parse display string */
828 /* insert parameters from Content-Type field */
833 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
834 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
844 /* insert content description */
848 s = trimcpy (ct->c_descr);
849 strncpy (bp, s, buflen);
855 /* exclusive execution */
865 /* insert filename(s) containing content */
869 for (part = m->mp_parts; part; part = part->mp_next) {
872 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
882 /* %l, and pause prior to displaying content */
887 /* display listing prior to displaying content */
892 /* insert subtype of content */
893 strncpy (bp, ci->ci_subtype, buflen);
897 /* insert character % */
917 /* use charset string to modify display method */
918 if (ct->c_termproc) {
921 strncpy (term, buffer, sizeof(term));
922 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
925 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
926 NOTOK, xlist, xpause, 0, xtty);
931 * show content of type "message/rfc822"
935 show_message_rfc822 (CT ct, int serial, int alternate)
937 char *cp, buffer[BUFSIZ];
938 CI ci = &ct->c_ctinfo;
940 /* Check for mhn-show-type/subtype */
941 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
942 invo_name, ci->ci_type, ci->ci_subtype);
943 if ((cp = context_find (buffer)) && *cp != '\0')
944 return show_content_aux (ct, serial, alternate, cp, NULL);
946 /* Check for mhn-show-type */
947 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
948 if ((cp = context_find (buffer)) && *cp != '\0')
949 return show_content_aux (ct, serial, alternate, cp, NULL);
951 if ((cp = ct->c_showproc))
952 return show_content_aux (ct, serial, alternate, cp, NULL);
954 /* default method for message/rfc822 */
955 if (ct->c_subtype == MESSAGE_RFC822) {
956 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
957 return show_content_aux (ct, serial, alternate, cp, NULL);
960 /* complain if we are not a part of a multipart/alternative */
962 content_error (NULL, ct, "don't know how to display content");
969 * Show content of type "message/partial".
973 show_partial (CT ct, int serial, int alternate)
975 content_error (NULL, ct,
976 "in order to display this message, you must reassemble it");
982 * Show content of type "message/external".
984 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
988 show_external (CT ct, int serial, int alternate)
990 struct exbody *e = (struct exbody *) ct->c_ctparams;
991 CT p = e->eb_content;
996 return show_switch (p, serial, alternate);
999 content_error (NULL, p, "don't know how to display content");
1008 #ifndef RELIABLE_SIGNALS
1009 SIGNAL (SIGINT, intrser);
1013 siglongjmp (intrenv, DONE);