]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3 * mhshowsbr.c -- routines to display the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
18 #include <h/mhparse.h>
29 /* flags for moreproc/header display */
35 static sigjmp_buf intrenv
;
39 int part_ok (CT
, int);
40 int type_ok (CT
, int);
41 void content_error (char *, CT
, char *, ...);
42 void flush_errors (void);
45 int list_switch (CT
, int, int, int, int);
46 int list_content (CT
, int, int, int, int);
51 void show_all_messages (CT
*);
52 int show_content_aux (CT
, int, int, char *, char *);
57 static void show_single_message (CT
, char *);
58 static void DisplayMsgHeader (CT
, char *);
59 static int show_switch (CT
, int, int);
60 static int show_content (CT
, int, int);
61 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
62 static int show_text (CT
, int, int);
63 static int show_multi (CT
, int, int);
64 static int show_multi_internal (CT
, int, int);
65 static int show_multi_aux (CT
, int, int, char *);
66 static int show_message_rfc822 (CT
, int, int);
67 static int show_partial (CT
, int, int);
68 static int show_external (CT
, int, int);
69 static void intrser (int);
73 * Top level entry point to show/display a group of messages
77 show_all_messages (CT
*cts
)
82 * If form is not specified, then get default form
83 * for showing headers of MIME messages.
86 formsw
= getcpy (etcpath ("mhl.headers"));
89 * If form is "mhl.null", suppress display of header.
91 if (!strcmp (formsw
, "mhl.null"))
94 for (ctp
= cts
; *ctp
; ctp
++) {
97 /* if top-level type is ok, then display message */
99 show_single_message (ct
, formsw
);
105 * Entry point to show/display a single message
109 show_single_message (CT ct
, char *form
)
115 /* Allow user executable bit so that temporary directories created by
116 * the viewer (e.g., lynx) are going to be accessible */
117 umask (ct
->c_umask
& ~(0100));
120 * If you have a format file, then display
121 * the message headers.
124 DisplayMsgHeader(ct
, form
);
128 /* Show the body of the message */
129 show_switch (ct
, 1, 0);
135 if (ct
->c_ceclosefnx
)
136 (*ct
->c_ceclosefnx
) (ct
);
138 /* block a few signals */
140 sigaddset (&set
, SIGHUP
);
141 sigaddset (&set
, SIGINT
);
142 sigaddset (&set
, SIGQUIT
);
143 sigaddset (&set
, SIGTERM
);
144 sigprocmask (SIG_BLOCK
, &set
, &oset
);
146 while (wait (&status
) != NOTOK
) {
151 /* reset the signal mask */
152 sigprocmask (SIG_SETMASK
, &oset
, &set
);
160 * Use the mhlproc to show the header fields
164 DisplayMsgHeader (CT ct
, char *form
)
171 vec
= argsplit(mhlproc
, &file
, &vecp
);
172 vec
[vecp
++] = getcpy("-form");
173 vec
[vecp
++] = getcpy(form
);
174 vec
[vecp
++] = getcpy("-nobody");
175 vec
[vecp
++] = getcpy(ct
->c_file
);
178 * If we've specified -(no)moreproc,
179 * then just pass that along.
182 vec
[vecp
++] = getcpy("-nomoreproc");
184 vec
[vecp
++] = getcpy("-moreproc");
185 vec
[vecp
++] = getcpy(progsw
);
191 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
196 adios ("fork", "unable to");
201 fprintf (stderr
, "unable to exec ");
211 arglist_free(file
, vec
);
216 * Switching routine. Call the correct routine
217 * based on content type.
221 show_switch (CT ct
, int serial
, int alternate
)
223 switch (ct
->c_type
) {
225 return show_multi (ct
, serial
, alternate
);
229 switch (ct
->c_subtype
) {
230 case MESSAGE_PARTIAL
:
231 return show_partial (ct
, serial
, alternate
);
234 case MESSAGE_EXTERNAL
:
235 return show_external (ct
, serial
, alternate
);
240 return show_message_rfc822 (ct
, serial
, alternate
);
246 return show_text (ct
, serial
, alternate
);
253 return show_content (ct
, serial
, alternate
);
257 adios (NULL
, "unknown content type %d", ct
->c_type
);
261 return 0; /* NOT REACHED */
266 * Generic method for displaying content
270 show_content (CT ct
, int serial
, int alternate
)
272 char *cp
, buffer
[BUFSIZ
];
273 CI ci
= &ct
->c_ctinfo
;
275 /* Check for mhn-show-type/subtype */
276 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
277 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
278 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
279 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
281 /* Check for mhn-show-type */
282 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
283 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
284 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
286 if ((cp
= ct
->c_showproc
))
287 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
289 /* complain if we are not a part of a multipart/alternative */
291 content_error (NULL
, ct
, "don't know how to display content");
298 * Parse the display string for displaying generic content
302 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
304 int fd
, len
, buflen
, quoted
;
305 int xstdin
, xlist
, xpause
, xtty
;
306 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
307 CI ci
= &ct
->c_ctinfo
;
309 if (!ct
->c_ceopenfnx
) {
311 content_error (NULL
, ct
, "don't know how to decode content");
317 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
319 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
320 return (alternate
? DONE
: OK
);
328 strncpy (buffer
, cp
, sizeof(buffer
));
332 /* get buffer ready to go */
334 buflen
= sizeof(buffer
) - 1;
335 bp
[0] = bp
[buflen
] = '\0';
338 /* Now parse display string */
339 for ( ; *cp
&& buflen
> 0; cp
++) {
345 /* insert parameters from Content-Type field */
350 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
351 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
361 /* insert content description */
365 s
= trimcpy (ct
->c_descr
);
366 strncpy (bp
, s
, buflen
);
372 /* exclusive execution */
377 /* %e, %f, and stdin is terminal not content */
383 /* insert filename containing content */
384 snprintf (bp
, buflen
, "'%s'", file
);
385 /* since we've quoted the file argument, set things up
386 * to look past it, to avoid problems with the quoting
387 * logic below. (I know, I should figure out what's
388 * broken with the quoting logic, but..)
397 /* %l, and pause prior to displaying content */
402 /* display listing prior to displaying content */
407 /* insert subtype of content */
408 strncpy (bp
, ci
->ci_subtype
, buflen
);
412 /* insert character % */
425 /* Did we actually insert something? */
427 /* Insert single quote if not inside quotes already */
428 if (!quoted
&& buflen
) {
430 memmove (pp
+ 1, pp
, len
);
435 /* Escape existing quotes */
436 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
438 memmove (pp
+ 3, pp
, len
);
445 /* If pp is still set, that means we ran out of space. */
448 if (!quoted
&& buflen
) {
466 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
467 /* content_error would provide a more useful error message
468 * here, except that if we got overrun, it probably would
471 fprintf(stderr
, "Buffer overflow constructing show command!\n");
475 /* use charset string to modify display method */
476 if (ct
->c_termproc
) {
479 strncpy (term
, buffer
, sizeof(term
));
480 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
484 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
485 fd
, xlist
, xpause
, xstdin
, xtty
);
490 * Routine to actually display the content
494 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
495 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
499 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
501 if (debugsw
|| cracked
) {
504 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
507 fprintf (stderr
, " part %s", ct
->c_partno
);
509 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
511 fprintf (stderr
, " using command %s\n", buffer
);
514 if (xpid
< 0 || (xtty
&& xpid
)) {
517 pidcheck(pidwait (xpid
, NOTOK
));
524 if (ct
->c_type
== CT_MULTIPART
)
525 list_content (ct
, -1, 1, 0, 0);
527 list_switch (ct
, -1, 1, 0, 0);
529 if (xpause
&& isatty (fileno (stdout
))) {
531 SIGNAL_HANDLER istat
;
533 if (SOprintf ("Press <return> to show content..."))
534 printf ("Press <return> to show content...");
536 istat
= SIGNAL (SIGINT
, intrser
);
537 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
540 read (fileno (stdout
), prompt
, sizeof(prompt
));
542 SIGNAL (SIGINT
, istat
);
543 if (intr
!= OK
|| prompt
[0] == 'n') {
544 (*ct
->c_ceclosefnx
) (ct
);
545 return (alternate
? DONE
: NOTOK
);
547 if (prompt
[0] == 'q') done(OK
);
551 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
560 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
564 advise ("fork", "unable to");
565 (*ct
->c_ceclosefnx
) (ct
);
574 execvp ("/bin/sh", vec
);
575 fprintf (stderr
, "unable to exec ");
582 ct
->c_pid
= child_id
;
586 pidcheck (pidXwait (child_id
, NULL
));
590 (*ct
->c_ceclosefnx
) (ct
);
591 return (alternate
? DONE
: OK
);
597 * show content of type "text"
601 show_text (CT ct
, int serial
, int alternate
)
603 char *cp
, buffer
[BUFSIZ
];
604 CI ci
= &ct
->c_ctinfo
;
606 /* Check for mhn-show-type/subtype */
607 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
608 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
609 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
610 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
612 /* Check for mhn-show-type */
613 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
614 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
615 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
618 * Use default method if content is text/plain, or if
619 * if it is not a text part of a multipart/alternative
621 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
622 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
623 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
624 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
625 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
633 * show message body of type "multipart"
637 show_multi (CT ct
, int serial
, int alternate
)
639 char *cp
, buffer
[BUFSIZ
];
640 CI ci
= &ct
->c_ctinfo
;
642 /* Check for mhn-show-type/subtype */
643 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
644 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
645 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
646 return show_multi_aux (ct
, serial
, alternate
, cp
);
648 /* Check for mhn-show-type */
649 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
650 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
651 return show_multi_aux (ct
, serial
, alternate
, cp
);
653 if ((cp
= ct
->c_showproc
))
654 return show_multi_aux (ct
, serial
, alternate
, cp
);
657 * Use default method to display this multipart content. Even
658 * unknown types are displayable, since they're treated as mixed
661 return show_multi_internal (ct
, serial
, alternate
);
666 * show message body of subtypes of multipart that
667 * we understand directly (mixed, alternate, etc...)
671 show_multi_internal (CT ct
, int serial
, int alternate
)
673 int alternating
, nowalternate
, nowserial
, result
;
674 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
680 nowalternate
= alternate
;
682 if (ct
->c_subtype
== MULTI_PARALLEL
) {
683 nowserial
= serialsw
;
684 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
692 * unknown subtypes of multipart (treat as mixed per rfc2046)
697 /* block a few signals */
700 sigaddset (&set
, SIGHUP
);
701 sigaddset (&set
, SIGINT
);
702 sigaddset (&set
, SIGQUIT
);
703 sigaddset (&set
, SIGTERM
);
704 sigprocmask (SIG_BLOCK
, &set
, &oset
);
708 * alternate -> we are a part inside an multipart/alternative
709 * alternating -> we are a multipart/alternative
712 result
= alternate
? NOTOK
: OK
;
714 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
717 if (part_ok (p
, 1) && type_ok (p
, 1)) {
720 inneresult
= show_switch (p
, nowserial
, nowalternate
);
721 switch (inneresult
) {
723 if (alternate
&& !alternating
) {
736 alternate
= nowalternate
= 0;
746 if (alternating
&& !part
) {
748 content_error (NULL
, ct
, "don't know how to display any of the contents");
753 if (serial
&& !nowserial
) {
759 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
763 if (kill (p
->c_pid
, 0) == NOTOK
)
770 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
773 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
778 if (p
->c_pid
== pid
) {
789 /* reset the signal mask */
790 sigprocmask (SIG_SETMASK
, &oset
, &set
);
798 * Parse display string for multipart content
799 * and use external program to display it.
803 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
805 int len
, buflen
, quoted
;
806 int xlist
, xpause
, xtty
;
807 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
808 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
810 CI ci
= &ct
->c_ctinfo
;
813 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
816 if (!p
->c_ceopenfnx
) {
818 content_error (NULL
, p
, "don't know how to decode content");
822 if (p
->c_storage
== NULL
) {
824 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
827 /* I'm not sure if this is necessary? */
828 p
->c_storage
= add (file
, NULL
);
830 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
831 return (alternate
? DONE
: OK
);
832 (*p
->c_ceclosefnx
) (p
);
840 /* get buffer ready to go */
842 buflen
= sizeof(buffer
) - 1;
843 bp
[0] = bp
[buflen
] = '\0';
846 /* Now parse display string */
847 for ( ; *cp
&& buflen
> 0; cp
++) {
852 /* insert parameters from Content-Type field */
857 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
858 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
868 /* insert content description */
872 s
= trimcpy (ct
->c_descr
);
873 strncpy (bp
, s
, buflen
);
879 /* exclusive execution */
889 /* insert filename(s) containing content */
893 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
896 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
902 /* set our starting pointer back to bp, to avoid
903 * requoting the filenames we just added
910 /* %l, and pause prior to displaying content */
915 /* display listing prior to displaying content */
920 /* insert subtype of content */
921 strncpy (bp
, ci
->ci_subtype
, buflen
);
925 /* insert character % */
938 /* Did we actually insert something? */
940 /* Insert single quote if not inside quotes already */
941 if (!quoted
&& buflen
) {
943 memmove (pp
+ 1, pp
, len
);
948 /* Escape existing quotes */
949 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
951 memmove (pp
+ 3, pp
, len
);
958 /* If pp is still set, that means we ran out of space. */
961 if (!quoted
&& buflen
) {
979 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
980 /* content_error would provide a more useful error message
981 * here, except that if we got overrun, it probably would
984 fprintf(stderr
, "Buffer overflow constructing show command!\n");
988 /* use charset string to modify display method */
989 if (ct
->c_termproc
) {
992 strncpy (term
, buffer
, sizeof(term
));
993 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
996 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
997 NOTOK
, xlist
, xpause
, 0, xtty
);
1002 * show content of type "message/rfc822"
1006 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1008 char *cp
, buffer
[BUFSIZ
];
1009 CI ci
= &ct
->c_ctinfo
;
1011 /* Check for mhn-show-type/subtype */
1012 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1013 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1014 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1015 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1017 /* Check for mhn-show-type */
1018 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1019 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1020 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1022 if ((cp
= ct
->c_showproc
))
1023 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1025 /* default method for message/rfc822 */
1026 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1027 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1028 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1031 /* complain if we are not a part of a multipart/alternative */
1033 content_error (NULL
, ct
, "don't know how to display content");
1040 * Show content of type "message/partial".
1044 show_partial (CT ct
, int serial
, int alternate
)
1046 NMH_UNUSED (serial
);
1047 NMH_UNUSED (alternate
);
1049 content_error (NULL
, ct
,
1050 "in order to display this message, you must reassemble it");
1056 * Show content of type "message/external".
1058 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1062 show_external (CT ct
, int serial
, int alternate
)
1064 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1065 CT p
= e
->eb_content
;
1067 if (!type_ok (p
, 0))
1070 return show_switch (p
, serial
, alternate
);
1080 siglongjmp (intrenv
, DONE
);