]>
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>
20 #include <h/mhparse.h>
32 /* flags for moreproc/header display */
38 static sigjmp_buf intrenv
;
42 int part_ok (CT
, int);
43 int type_ok (CT
, int);
44 void content_error (char *, CT
, char *, ...);
45 void flush_errors (void);
48 int list_switch (CT
, int, int, int, int);
49 int list_content (CT
, int, int, int, int);
54 void show_all_messages (CT
*);
55 int show_content_aux (CT
, int, int, char *, char *);
60 static void show_single_message (CT
, char *);
61 static void DisplayMsgHeader (CT
, char *);
62 static int show_switch (CT
, int, int);
63 static int show_content (CT
, int, int);
64 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
65 static int show_text (CT
, int, int);
66 static int show_multi (CT
, int, int);
67 static int show_multi_internal (CT
, int, int);
68 static int show_multi_aux (CT
, int, int, char *);
69 static int show_message_rfc822 (CT
, int, int);
70 static int show_partial (CT
, int, int);
71 static int show_external (CT
, int, int);
72 static void intrser (int);
76 * Top level entry point to show/display a group of messages
80 show_all_messages (CT
*cts
)
85 * If form is not specified, then get default form
86 * for showing headers of MIME messages.
89 formsw
= getcpy (etcpath ("mhl.headers"));
92 * If form is "mhl.null", suppress display of header.
94 if (!strcmp (formsw
, "mhl.null"))
97 for (ctp
= cts
; *ctp
; ctp
++) {
100 /* if top-level type is ok, then display message */
102 show_single_message (ct
, formsw
);
108 * Entry point to show/display a single message
112 show_single_message (CT ct
, char *form
)
118 /* Allow user executable bit so that temporary directories created by
119 * the viewer (e.g., lynx) are going to be accessible */
120 umask (ct
->c_umask
& ~(0100));
123 * If you have a format file, then display
124 * the message headers.
127 DisplayMsgHeader(ct
, form
);
131 /* Show the body of the message */
132 show_switch (ct
, 1, 0);
138 if (ct
->c_ceclosefnx
)
139 (*ct
->c_ceclosefnx
) (ct
);
141 /* block a few signals */
143 sigaddset (&set
, SIGHUP
);
144 sigaddset (&set
, SIGINT
);
145 sigaddset (&set
, SIGQUIT
);
146 sigaddset (&set
, SIGTERM
);
147 sigprocmask (SIG_BLOCK
, &set
, &oset
);
149 while (wait (&status
) != NOTOK
) {
154 /* reset the signal mask */
155 sigprocmask (SIG_SETMASK
, &oset
, &set
);
163 * Use the mhlproc to show the header fields
167 DisplayMsgHeader (CT ct
, char *form
)
174 vec
= argsplit(mhlproc
, &file
, &vecp
);
175 vec
[vecp
++] = getcpy("-form");
176 vec
[vecp
++] = getcpy(form
);
177 vec
[vecp
++] = getcpy("-nobody");
178 vec
[vecp
++] = getcpy(ct
->c_file
);
181 * If we've specified -(no)moreproc,
182 * then just pass that along.
185 vec
[vecp
++] = getcpy("-nomoreproc");
187 vec
[vecp
++] = getcpy("-moreproc");
188 vec
[vecp
++] = getcpy(progsw
);
194 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
199 adios ("fork", "unable to");
204 fprintf (stderr
, "unable to exec ");
214 arglist_free(file
, vec
);
219 * Switching routine. Call the correct routine
220 * based on content type.
224 show_switch (CT ct
, int serial
, int alternate
)
226 switch (ct
->c_type
) {
228 return show_multi (ct
, serial
, alternate
);
232 switch (ct
->c_subtype
) {
233 case MESSAGE_PARTIAL
:
234 return show_partial (ct
, serial
, alternate
);
237 case MESSAGE_EXTERNAL
:
238 return show_external (ct
, serial
, alternate
);
243 return show_message_rfc822 (ct
, serial
, alternate
);
249 return show_text (ct
, serial
, alternate
);
256 return show_content (ct
, serial
, alternate
);
260 adios (NULL
, "unknown content type %d", ct
->c_type
);
264 return 0; /* NOT REACHED */
269 * Generic method for displaying content
273 show_content (CT ct
, int serial
, int alternate
)
275 char *cp
, buffer
[BUFSIZ
];
276 CI ci
= &ct
->c_ctinfo
;
278 /* Check for mhn-show-type/subtype */
279 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
280 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
281 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
282 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
284 /* Check for mhn-show-type */
285 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
286 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
287 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
289 if ((cp
= ct
->c_showproc
))
290 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
292 /* complain if we are not a part of a multipart/alternative */
294 content_error (NULL
, ct
, "don't know how to display content");
301 * Parse the display string for displaying generic content
305 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
307 int fd
, len
, buflen
, quoted
;
308 int xstdin
, xlist
, xpause
, xtty
;
309 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
310 CI ci
= &ct
->c_ctinfo
;
312 if (!ct
->c_ceopenfnx
) {
314 content_error (NULL
, ct
, "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"))
323 return (alternate
? DONE
: OK
);
331 strncpy (buffer
, cp
, sizeof(buffer
));
335 /* get buffer ready to go */
337 buflen
= sizeof(buffer
) - 1;
338 bp
[0] = bp
[buflen
] = '\0';
341 /* Now parse display string */
342 for ( ; *cp
&& buflen
> 0; cp
++) {
348 /* insert parameters from Content-Type field */
353 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
354 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
364 /* insert content description */
368 s
= trimcpy (ct
->c_descr
);
369 strncpy (bp
, s
, buflen
);
375 /* exclusive execution */
380 /* %e, %f, and stdin is terminal not content */
386 /* insert filename containing content */
387 snprintf (bp
, buflen
, "'%s'", file
);
388 /* since we've quoted the file argument, set things up
389 * to look past it, to avoid problems with the quoting
390 * logic below. (I know, I should figure out what's
391 * broken with the quoting logic, but..)
400 /* %l, and pause prior to displaying content */
405 /* display listing prior to displaying content */
410 /* insert subtype of content */
411 strncpy (bp
, ci
->ci_subtype
, buflen
);
415 /* insert character % */
428 /* Did we actually insert something? */
430 /* Insert single quote if not inside quotes already */
431 if (!quoted
&& buflen
) {
433 memmove (pp
+ 1, pp
, len
);
438 /* Escape existing quotes */
439 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
441 memmove (pp
+ 3, pp
, len
);
448 /* If pp is still set, that means we ran out of space. */
451 if (!quoted
&& buflen
) {
469 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
470 /* content_error would provide a more useful error message
471 * here, except that if we got overrun, it probably would
474 fprintf(stderr
, "Buffer overflow constructing show command!\n");
478 /* use charset string to modify display method */
479 if (ct
->c_termproc
) {
482 strncpy (term
, buffer
, sizeof(term
));
483 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
487 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
488 fd
, xlist
, xpause
, xstdin
, xtty
);
493 * Routine to actually display the content
497 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
498 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
502 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
504 if (debugsw
|| cracked
) {
507 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
510 fprintf (stderr
, " part %s", ct
->c_partno
);
512 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
514 fprintf (stderr
, " using command %s\n", buffer
);
517 if (xpid
< 0 || (xtty
&& xpid
)) {
520 pidcheck(pidwait (xpid
, NOTOK
));
527 if (ct
->c_type
== CT_MULTIPART
)
528 list_content (ct
, -1, 1, 0, 0);
530 list_switch (ct
, -1, 1, 0, 0);
532 if (xpause
&& isatty (fileno (stdout
))) {
534 SIGNAL_HANDLER istat
;
536 if (SOprintf ("Press <return> to show content..."))
537 printf ("Press <return> to show content...");
539 istat
= SIGNAL (SIGINT
, intrser
);
540 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
543 read (fileno (stdout
), prompt
, sizeof(prompt
));
545 SIGNAL (SIGINT
, istat
);
546 if (intr
!= OK
|| prompt
[0] == 'n') {
547 (*ct
->c_ceclosefnx
) (ct
);
548 return (alternate
? DONE
: NOTOK
);
550 if (prompt
[0] == 'q') done(OK
);
554 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
563 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
567 advise ("fork", "unable to");
568 (*ct
->c_ceclosefnx
) (ct
);
577 execvp ("/bin/sh", vec
);
578 fprintf (stderr
, "unable to exec ");
585 ct
->c_pid
= child_id
;
589 pidcheck (pidXwait (child_id
, NULL
));
593 (*ct
->c_ceclosefnx
) (ct
);
594 return (alternate
? DONE
: OK
);
600 * show content of type "text"
604 show_text (CT ct
, int serial
, int alternate
)
606 char *cp
, buffer
[BUFSIZ
];
607 CI ci
= &ct
->c_ctinfo
;
609 /* Check for mhn-show-type/subtype */
610 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
611 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
612 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
613 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
615 /* Check for mhn-show-type */
616 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
617 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
618 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
621 * Use default method if content is text/plain, or if
622 * if it is not a text part of a multipart/alternative
624 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
625 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
626 moreproc
&& *moreproc
? moreproc
: "more");
627 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
628 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
636 * show message body of type "multipart"
640 show_multi (CT ct
, int serial
, int alternate
)
642 char *cp
, buffer
[BUFSIZ
];
643 CI ci
= &ct
->c_ctinfo
;
645 /* Check for mhn-show-type/subtype */
646 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
647 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
648 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
649 return show_multi_aux (ct
, serial
, alternate
, cp
);
651 /* Check for mhn-show-type */
652 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
653 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
654 return show_multi_aux (ct
, serial
, alternate
, cp
);
656 if ((cp
= ct
->c_showproc
))
657 return show_multi_aux (ct
, serial
, alternate
, cp
);
660 * Use default method to display this multipart content. Even
661 * unknown types are displayable, since they're treated as mixed
664 return show_multi_internal (ct
, serial
, alternate
);
669 * show message body of subtypes of multipart that
670 * we understand directly (mixed, alternate, etc...)
674 show_multi_internal (CT ct
, int serial
, int alternate
)
676 int alternating
, nowalternate
, nowserial
, result
;
677 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
683 nowalternate
= alternate
;
685 if (ct
->c_subtype
== MULTI_PARALLEL
) {
686 nowserial
= serialsw
;
687 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
695 * unknown subtypes of multipart (treat as mixed per rfc2046)
700 /* block a few signals */
703 sigaddset (&set
, SIGHUP
);
704 sigaddset (&set
, SIGINT
);
705 sigaddset (&set
, SIGQUIT
);
706 sigaddset (&set
, SIGTERM
);
707 sigprocmask (SIG_BLOCK
, &set
, &oset
);
711 * alternate -> we are a part inside an multipart/alternative
712 * alternating -> we are a multipart/alternative
715 result
= alternate
? NOTOK
: OK
;
717 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
720 if (part_ok (p
, 1) && type_ok (p
, 1)) {
723 inneresult
= show_switch (p
, nowserial
, nowalternate
);
724 switch (inneresult
) {
726 if (alternate
&& !alternating
) {
739 alternate
= nowalternate
= 0;
749 if (alternating
&& !part
) {
751 content_error (NULL
, ct
, "don't know how to display any of the contents");
756 if (serial
&& !nowserial
) {
762 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
766 if (kill (p
->c_pid
, 0) == NOTOK
)
773 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
776 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
781 if (p
->c_pid
== pid
) {
792 /* reset the signal mask */
793 sigprocmask (SIG_SETMASK
, &oset
, &set
);
801 * Parse display string for multipart content
802 * and use external program to display it.
806 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
808 int len
, buflen
, quoted
;
809 int xlist
, xpause
, xtty
;
810 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
811 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
813 CI ci
= &ct
->c_ctinfo
;
816 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
819 if (!p
->c_ceopenfnx
) {
821 content_error (NULL
, p
, "don't know how to decode content");
825 if (p
->c_storage
== NULL
) {
827 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
830 /* I'm not sure if this is necessary? */
831 p
->c_storage
= add (file
, NULL
);
833 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
834 return (alternate
? DONE
: OK
);
835 (*p
->c_ceclosefnx
) (p
);
843 /* get buffer ready to go */
845 buflen
= sizeof(buffer
) - 1;
846 bp
[0] = bp
[buflen
] = '\0';
849 /* Now parse display string */
850 for ( ; *cp
&& buflen
> 0; cp
++) {
855 /* insert parameters from Content-Type field */
860 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
861 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
871 /* insert content description */
875 s
= trimcpy (ct
->c_descr
);
876 strncpy (bp
, s
, buflen
);
882 /* exclusive execution */
892 /* insert filename(s) containing content */
896 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
899 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
905 /* set our starting pointer back to bp, to avoid
906 * requoting the filenames we just added
913 /* %l, and pause prior to displaying content */
918 /* display listing prior to displaying content */
923 /* insert subtype of content */
924 strncpy (bp
, ci
->ci_subtype
, buflen
);
928 /* insert character % */
941 /* Did we actually insert something? */
943 /* Insert single quote if not inside quotes already */
944 if (!quoted
&& buflen
) {
946 memmove (pp
+ 1, pp
, len
);
951 /* Escape existing quotes */
952 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
954 memmove (pp
+ 3, pp
, len
);
961 /* If pp is still set, that means we ran out of space. */
964 if (!quoted
&& buflen
) {
982 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
983 /* content_error would provide a more useful error message
984 * here, except that if we got overrun, it probably would
987 fprintf(stderr
, "Buffer overflow constructing show command!\n");
991 /* use charset string to modify display method */
992 if (ct
->c_termproc
) {
995 strncpy (term
, buffer
, sizeof(term
));
996 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
999 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1000 NOTOK
, xlist
, xpause
, 0, xtty
);
1005 * show content of type "message/rfc822"
1009 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1011 char *cp
, buffer
[BUFSIZ
];
1012 CI ci
= &ct
->c_ctinfo
;
1014 /* Check for mhn-show-type/subtype */
1015 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1016 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1017 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1018 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1020 /* Check for mhn-show-type */
1021 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1022 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1023 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1025 if ((cp
= ct
->c_showproc
))
1026 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1028 /* default method for message/rfc822 */
1029 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1030 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1031 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1034 /* complain if we are not a part of a multipart/alternative */
1036 content_error (NULL
, ct
, "don't know how to display content");
1043 * Show content of type "message/partial".
1047 show_partial (CT ct
, int serial
, int alternate
)
1049 NMH_UNUSED (serial
);
1050 NMH_UNUSED (alternate
);
1052 content_error (NULL
, ct
,
1053 "in order to display this message, you must reassemble it");
1059 * Show content of type "message/external".
1061 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1065 show_external (CT ct
, int serial
, int alternate
)
1067 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1068 CT p
= e
->eb_content
;
1070 if (!type_ok (p
, 0))
1073 return show_switch (p
, serial
, alternate
);
1083 siglongjmp (intrenv
, DONE
);