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)
328 int fd, len, buflen, quoted;
329 int xstdin, xlist, xpause, xtty;
330 char *bp, *pp, *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 */
358 buflen = sizeof(buffer) - 1;
359 bp[0] = bp[buflen] = '\0';
362 /* Now parse display string */
363 for ( ; *cp && buflen > 0; cp++) {
369 /* insert parameters from Content-Type field */
374 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
375 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
385 /* insert content description */
389 s = trimcpy (ct->c_descr);
390 strncpy (bp, s, buflen);
396 /* exclusive execution */
401 /* %e, %f, and stdin is terminal not content */
407 /* insert filename containing content */
408 snprintf (bp, buflen, "%s", file);
412 /* %l, and pause prior to displaying content */
417 /* 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? */
442 /* Insert single quote if not inside quotes already */
443 if (!quoted && buflen) {
445 memmove (pp + 1, pp, len);
450 /* Escape existing quotes */
451 while ((pp = strchr (pp, '\'')) && buflen > 3) {
453 memmove (pp + 3, pp, len);
460 /* If pp is still set, that means we ran out of space. */
463 if (!quoted && buflen) {
480 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
481 /* content_error would provide a more useful error message
482 * here, except that if we got overrun, it probably would
485 fprintf(stderr, "Buffer overflow constructing show command!\n");
489 /* use charset string to modify display method */
490 if (ct->c_termproc) {
493 strncpy (term, buffer, sizeof(term));
494 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
498 return show_content_aux2 (ct, serial, alternate, cracked, buffer,
499 fd, xlist, xpause, xstdin, xtty);
504 * Routine to actually display the content
508 show_content_aux2 (CT ct, int serial, int alternate, char *cracked, char *buffer,
509 int fd, int xlist, int xpause, int xstdin, int xtty)
513 char *vec[4], exec[BUFSIZ + sizeof "exec "];
515 if (debugsw || cracked) {
518 fprintf (stderr, "%s msg %s", cracked ? "storing" : "show",
521 fprintf (stderr, " part %s", ct->c_partno);
523 fprintf (stderr, " using command (cd %s; %s)\n", cracked, buffer);
525 fprintf (stderr, " using command %s\n", buffer);
528 if (xpid < 0 || (xtty && xpid)) {
531 pidcheck(pidwait (xpid, NOTOK));
538 if (ct->c_type == CT_MULTIPART)
539 list_content (ct, -1, 1, 0, 0);
541 list_switch (ct, -1, 1, 0, 0);
543 if (xpause && SOprintf ("Press <return> to show content..."))
544 printf ("Press <return> to show content...");
548 SIGNAL_HANDLER istat;
550 istat = SIGNAL (SIGINT, intrser);
551 if ((intr = sigsetjmp (intrenv, 1)) == OK) {
554 read (fileno (stdout), prompt, sizeof(prompt));
556 SIGNAL (SIGINT, istat);
557 if (intr != OK || prompt[0] == 'n') {
558 (*ct->c_ceclosefnx) (ct);
559 return (alternate ? DONE : NOTOK);
561 if (prompt[0] == 'q') done(OK);
565 snprintf (exec, sizeof(exec), "exec %s", buffer);
574 for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++)
578 advise ("fork", "unable to");
579 (*ct->c_ceclosefnx) (ct);
588 execvp ("/bin/sh", vec);
589 fprintf (stderr, "unable to exec ");
596 ct->c_pid = child_id;
600 pidcheck (pidXwait (child_id, NULL));
604 (*ct->c_ceclosefnx) (ct);
605 return (alternate ? DONE : OK);
611 * show content of type "text"
615 show_text (CT ct, int serial, int alternate)
617 char *cp, buffer[BUFSIZ];
618 CI ci = &ct->c_ctinfo;
620 /* Check for mhn-show-type/subtype */
621 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
622 invo_name, ci->ci_type, ci->ci_subtype);
623 if ((cp = context_find (buffer)) && *cp != '\0')
624 return show_content_aux (ct, serial, alternate, cp, NULL);
626 /* Check for mhn-show-type */
627 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
628 if ((cp = context_find (buffer)) && *cp != '\0')
629 return show_content_aux (ct, serial, alternate, cp, NULL);
632 * Use default method if content is text/plain, or if
633 * if it is not a text part of a multipart/alternative
635 if (!alternate || ct->c_subtype == TEXT_PLAIN) {
636 snprintf (buffer, sizeof(buffer), "%%p%s '%%F'", progsw ? progsw :
637 moreproc && *moreproc ? moreproc : "more");
638 cp = (ct->c_showproc = add (buffer, NULL));
639 return show_content_aux (ct, serial, alternate, cp, NULL);
647 * show message body of type "multipart"
651 show_multi (CT ct, int serial, int alternate)
653 char *cp, buffer[BUFSIZ];
654 CI ci = &ct->c_ctinfo;
656 /* Check for mhn-show-type/subtype */
657 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
658 invo_name, ci->ci_type, ci->ci_subtype);
659 if ((cp = context_find (buffer)) && *cp != '\0')
660 return show_multi_aux (ct, serial, alternate, cp);
662 /* Check for mhn-show-type */
663 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
664 if ((cp = context_find (buffer)) && *cp != '\0')
665 return show_multi_aux (ct, serial, alternate, cp);
667 if ((cp = ct->c_showproc))
668 return show_multi_aux (ct, serial, alternate, cp);
671 * Use default method to display this multipart content
672 * if it is not a (nested) part of a multipart/alternative,
673 * or if it is one of the known subtypes of multipart.
675 if (!alternate || ct->c_subtype != MULTI_UNKNOWN)
676 return show_multi_internal (ct, serial, alternate);
683 * show message body of subtypes of multipart that
684 * we understand directly (mixed, alternate, etc...)
688 show_multi_internal (CT ct, int serial, int alternate)
690 int alternating, nowalternate, nowserial, result;
691 struct multipart *m = (struct multipart *) ct->c_ctparams;
697 nowalternate = alternate;
699 if (ct->c_subtype == MULTI_PARALLEL) {
700 nowserial = serialsw;
701 } else if (ct->c_subtype == MULTI_ALTERNATE) {
709 * unknown subtypes of multipart (treat as mixed per rfc2046)
714 /* block a few signals */
717 sigaddset (&set, SIGHUP);
718 sigaddset (&set, SIGINT);
719 sigaddset (&set, SIGQUIT);
720 sigaddset (&set, SIGTERM);
721 SIGPROCMASK (SIG_BLOCK, &set, &oset);
725 * alternate -> we are a part inside an multipart/alternative
726 * alternating -> we are a multipart/alternative
729 result = alternate ? NOTOK : OK;
731 for (part = m->mp_parts; part; part = part->mp_next) {
734 if (part_ok (p, 0) && type_ok (p, 0)) {
737 inneresult = show_switch (p, nowserial, nowalternate);
738 switch (inneresult) {
740 if (alternate && !alternating) {
753 alternate = nowalternate = 0;
763 if (alternating && !part) {
765 content_error (NULL, ct, "don't know how to display any of the contents");
770 if (serial && !nowserial) {
780 for (part = m->mp_parts; part; part = part->mp_next) {
784 if (kill (p->c_pid, 0) == NOTOK)
791 while (kids > 0 && (pid = wait (&status)) != NOTOK) {
795 pidcheck (status.w_status);
798 for (part = m->mp_parts; part; part = part->mp_next) {
803 if (p->c_pid == pid) {
814 /* reset the signal mask */
815 SIGPROCMASK (SIG_SETMASK, &oset, &set);
823 * Parse display string for multipart content
824 * and use external program to display it.
828 show_multi_aux (CT ct, int serial, int alternate, char *cp)
830 int len, buflen, quoted;
831 int xlist, xpause, xtty;
832 char *bp, *pp, *file, buffer[BUFSIZ];
833 struct multipart *m = (struct multipart *) ct->c_ctparams;
835 CI ci = &ct->c_ctinfo;
838 for (part = m->mp_parts; part; part = part->mp_next) {
841 if (!p->c_ceopenfnx) {
843 content_error (NULL, p, "don't know how to decode content");
847 if (p->c_storage == NULL) {
849 if ((*p->c_ceopenfnx) (p, &file) == NOTOK)
852 /* I'm not sure if this is necessary? */
853 p->c_storage = add (file, NULL);
855 if (p->c_showproc && !strcmp (p->c_showproc, "true"))
856 return (alternate ? DONE : OK);
857 (*p->c_ceclosefnx) (p);
865 /* get buffer ready to go */
867 buflen = sizeof(buffer) - 1;
868 bp[0] = bp[buflen] = '\0';
871 /* Now parse display string */
876 /* insert parameters from Content-Type field */
881 for (ap = ci->ci_attrs, ep = ci->ci_values; *ap; ap++, ep++) {
882 snprintf (bp, buflen, "%s%s=\"%s\"", s, *ap, *ep);
892 /* insert content description */
896 s = trimcpy (ct->c_descr);
897 strncpy (bp, s, buflen);
903 /* exclusive execution */
913 /* insert filename(s) containing content */
917 for (part = m->mp_parts; part; part = part->mp_next) {
920 snprintf (bp, buflen, "%s'%s'", s, p->c_storage);
930 /* %l, and pause prior to displaying content */
935 /* display listing prior to displaying content */
940 /* insert subtype of content */
941 strncpy (bp, ci->ci_subtype, buflen);
945 /* insert character % */
958 /* Did we actually insert something? */
960 /* Insert single quote if not inside quotes already */
961 if (!quoted && buflen) {
963 memmove (pp + 1, pp, len);
968 /* Escape existing quotes */
969 while ((pp = strchr (pp, '\'')) && buflen > 3) {
971 memmove (pp + 3, pp, len);
978 /* If pp is still set, that means we ran out of space. */
981 if (!quoted && buflen) {
998 if (buflen <= 0 || (ct->c_termproc && buflen <= strlen(ct->c_termproc))) {
999 /* content_error would provide a more useful error message
1000 * here, except that if we got overrun, it probably would
1003 fprintf(stderr, "Buffer overflow constructing show command!\n");
1007 /* use charset string to modify display method */
1008 if (ct->c_termproc) {
1011 strncpy (term, buffer, sizeof(term));
1012 snprintf (buffer, sizeof(buffer), ct->c_termproc, term);
1015 return show_content_aux2 (ct, serial, alternate, NULL, buffer,
1016 NOTOK, xlist, xpause, 0, xtty);
1021 * show content of type "message/rfc822"
1025 show_message_rfc822 (CT ct, int serial, int alternate)
1027 char *cp, buffer[BUFSIZ];
1028 CI ci = &ct->c_ctinfo;
1030 /* Check for mhn-show-type/subtype */
1031 snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
1032 invo_name, ci->ci_type, ci->ci_subtype);
1033 if ((cp = context_find (buffer)) && *cp != '\0')
1034 return show_content_aux (ct, serial, alternate, cp, NULL);
1036 /* Check for mhn-show-type */
1037 snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
1038 if ((cp = context_find (buffer)) && *cp != '\0')
1039 return show_content_aux (ct, serial, alternate, cp, NULL);
1041 if ((cp = ct->c_showproc))
1042 return show_content_aux (ct, serial, alternate, cp, NULL);
1044 /* default method for message/rfc822 */
1045 if (ct->c_subtype == MESSAGE_RFC822) {
1046 cp = (ct->c_showproc = add ("%pshow -file '%F'", NULL));
1047 return show_content_aux (ct, serial, alternate, cp, NULL);
1050 /* complain if we are not a part of a multipart/alternative */
1052 content_error (NULL, ct, "don't know how to display content");
1059 * Show content of type "message/partial".
1063 show_partial (CT ct, int serial, int alternate)
1065 content_error (NULL, ct,
1066 "in order to display this message, you must reassemble it");
1072 * Show content of type "message/external".
1074 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1078 show_external (CT ct, int serial, int alternate)
1080 struct exbody *e = (struct exbody *) ct->c_ctparams;
1081 CT p = e->eb_content;
1083 if (!type_ok (p, 0))
1086 return show_switch (p, serial, alternate);
1089 content_error (NULL, p, "don't know how to display content");
1098 #ifndef RELIABLE_SIGNALS
1099 SIGNAL (SIGINT, intrser);
1103 siglongjmp (intrenv, DONE);