]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3 * mhshowsbr.c -- routines to display the contents of MIME messages
7 * This code is Copyright (c) 2002, by the authors of nmh. See the
8 * COPYRIGHT file in the root directory of the nmh distribution for
9 * complete copyright information.
14 #include <h/signals.h>
22 #include <h/mhparse.h>
25 #ifdef HAVE_SYS_WAIT_H
26 # include <sys/wait.h>
30 * Just use sigjmp/longjmp on older machines that
31 * don't have sigsetjmp/siglongjmp.
33 #ifndef HAVE_SIGSETJMP
34 # define sigjmp_buf jmp_buf
35 # define sigsetjmp(env,mask) setjmp(env)
36 # define siglongjmp(env,val) longjmp(env,val)
47 /* flags for moreproc/header display */
53 static sigjmp_buf intrenv
;
57 int SOprintf (char *, ...);
63 int part_ok (CT
, int);
64 int type_ok (CT
, int);
65 void content_error (char *, CT
, char *, ...);
66 void flush_errors (void);
69 int list_switch (CT
, int, int, int, int);
70 int list_content (CT
, int, int, int, int);
75 void show_all_messages (CT
*);
76 int show_content_aux (CT
, int, int, char *, char *);
81 static void show_single_message (CT
, char *);
82 static void DisplayMsgHeader (CT
, char *);
83 static int show_switch (CT
, int, int);
84 static int show_content (CT
, int, int);
85 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
86 static int show_text (CT
, int, int);
87 static int show_multi (CT
, int, int);
88 static int show_multi_internal (CT
, int, int);
89 static int show_multi_aux (CT
, int, int, char *);
90 static int show_message_rfc822 (CT
, int, int);
91 static int show_partial (CT
, int, int);
92 static int show_external (CT
, int, int);
93 static RETSIGTYPE
intrser (int);
97 * Top level entry point to show/display a group of messages
101 show_all_messages (CT
*cts
)
106 * If form is not specified, then get default form
107 * for showing headers of MIME messages.
110 formsw
= getcpy (etcpath ("mhl.headers"));
113 * If form is "mhl.null", suppress display of header.
115 if (!strcmp (formsw
, "mhl.null"))
118 for (ctp
= cts
; *ctp
; ctp
++) {
121 /* if top-level type is ok, then display message */
123 show_single_message (ct
, formsw
);
129 * Entry point to show/display a single message
133 show_single_message (CT ct
, char *form
)
137 #ifdef HAVE_UNION_WAIT
143 /* Allow user executable bit so that temporary directories created by
144 * the viewer (e.g., lynx) are going to be accessible */
145 umask (ct
->c_umask
& ~(0100));
148 * If you have a format file, then display
149 * the message headers.
152 DisplayMsgHeader(ct
, form
);
156 /* Show the body of the message */
157 show_switch (ct
, 1, 0);
163 if (ct
->c_ceclosefnx
)
164 (*ct
->c_ceclosefnx
) (ct
);
166 /* block a few signals */
168 sigaddset (&set
, SIGHUP
);
169 sigaddset (&set
, SIGINT
);
170 sigaddset (&set
, SIGQUIT
);
171 sigaddset (&set
, SIGTERM
);
172 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
174 while (wait (&status
) != NOTOK
) {
175 #ifdef HAVE_UNION_WAIT
176 pidcheck (status
.w_status
);
183 /* reset the signal mask */
184 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
192 * Use the mhlproc to show the header fields
196 DisplayMsgHeader (CT ct
, char *form
)
203 vec
[vecp
++] = r1bindex (mhlproc
, '/');
204 vec
[vecp
++] = "-form";
206 vec
[vecp
++] = "-nobody";
207 vec
[vecp
++] = ct
->c_file
;
210 * If we've specified -(no)moreproc,
211 * then just pass that along.
214 vec
[vecp
++] = "-nomoreproc";
216 vec
[vecp
++] = "-moreproc";
217 vec
[vecp
++] = progsw
;
223 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
228 adios ("fork", "unable to");
232 execvp (mhlproc
, vec
);
233 fprintf (stderr
, "unable to exec ");
246 * Switching routine. Call the correct routine
247 * based on content type.
251 show_switch (CT ct
, int serial
, int alternate
)
253 switch (ct
->c_type
) {
255 return show_multi (ct
, serial
, alternate
);
259 switch (ct
->c_subtype
) {
260 case MESSAGE_PARTIAL
:
261 return show_partial (ct
, serial
, alternate
);
264 case MESSAGE_EXTERNAL
:
265 return show_external (ct
, serial
, alternate
);
270 return show_message_rfc822 (ct
, serial
, alternate
);
276 return show_text (ct
, serial
, alternate
);
283 return show_content (ct
, serial
, alternate
);
287 adios (NULL
, "unknown content type %d", ct
->c_type
);
291 return 0; /* NOT REACHED */
296 * Generic method for displaying content
300 show_content (CT ct
, int serial
, int alternate
)
302 char *cp
, buffer
[BUFSIZ
];
303 CI ci
= &ct
->c_ctinfo
;
305 /* Check for mhn-show-type/subtype */
306 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
307 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
308 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
309 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
311 /* Check for mhn-show-type */
312 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
313 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
314 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
316 if ((cp
= ct
->c_showproc
))
317 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
319 /* complain if we are not a part of a multipart/alternative */
321 content_error (NULL
, ct
, "don't know how to display content");
328 * Parse the display string for displaying generic content
332 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
334 int fd
, len
, buflen
, quoted
;
335 int xstdin
, xlist
, xpause
, xtty
;
336 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
337 CI ci
= &ct
->c_ctinfo
;
339 if (!ct
->c_ceopenfnx
) {
341 content_error (NULL
, ct
, "don't know how to decode content");
347 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
349 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
350 return (alternate
? DONE
: OK
);
358 strncpy (buffer
, cp
, sizeof(buffer
));
362 /* get buffer ready to go */
364 buflen
= sizeof(buffer
) - 1;
365 bp
[0] = bp
[buflen
] = '\0';
368 /* Now parse display string */
369 for ( ; *cp
&& buflen
> 0; cp
++) {
375 /* insert parameters from Content-Type field */
380 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
381 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
391 /* insert content description */
395 s
= trimcpy (ct
->c_descr
);
396 strncpy (bp
, s
, buflen
);
402 /* exclusive execution */
407 /* %e, %f, and stdin is terminal not content */
413 /* insert filename containing content */
414 snprintf (bp
, buflen
, "'%s'", file
);
415 /* since we've quoted the file argument, set things up
416 * to look past it, to avoid problems with the quoting
417 * logic below. (I know, I should figure out what's
418 * broken with the quoting logic, but..)
427 /* %l, and pause prior to displaying content */
432 /* display listing prior to displaying content */
437 /* insert subtype of content */
438 strncpy (bp
, ci
->ci_subtype
, buflen
);
442 /* insert character % */
455 /* Did we actually insert something? */
457 /* Insert single quote if not inside quotes already */
458 if (!quoted
&& buflen
) {
460 memmove (pp
+ 1, pp
, len
);
465 /* Escape existing quotes */
466 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
468 memmove (pp
+ 3, pp
, len
);
475 /* If pp is still set, that means we ran out of space. */
478 if (!quoted
&& buflen
) {
495 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
496 /* content_error would provide a more useful error message
497 * here, except that if we got overrun, it probably would
500 fprintf(stderr
, "Buffer overflow constructing show command!\n");
504 /* use charset string to modify display method */
505 if (ct
->c_termproc
) {
508 strncpy (term
, buffer
, sizeof(term
));
509 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
513 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
514 fd
, xlist
, xpause
, xstdin
, xtty
);
519 * Routine to actually display the content
523 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
524 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
528 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
530 if (debugsw
|| cracked
) {
533 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
536 fprintf (stderr
, " part %s", ct
->c_partno
);
538 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
540 fprintf (stderr
, " using command %s\n", buffer
);
543 if (xpid
< 0 || (xtty
&& xpid
)) {
546 pidcheck(pidwait (xpid
, NOTOK
));
553 if (ct
->c_type
== CT_MULTIPART
)
554 list_content (ct
, -1, 1, 0, 0);
556 list_switch (ct
, -1, 1, 0, 0);
558 if (xpause
&& SOprintf ("Press <return> to show content..."))
559 printf ("Press <return> to show content...");
563 SIGNAL_HANDLER istat
;
565 istat
= SIGNAL (SIGINT
, intrser
);
566 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
569 read (fileno (stdout
), prompt
, sizeof(prompt
));
571 SIGNAL (SIGINT
, istat
);
572 if (intr
!= OK
|| prompt
[0] == 'n') {
573 (*ct
->c_ceclosefnx
) (ct
);
574 return (alternate
? DONE
: NOTOK
);
576 if (prompt
[0] == 'q') done(OK
);
580 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
589 for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
593 advise ("fork", "unable to");
594 (*ct
->c_ceclosefnx
) (ct
);
603 execvp ("/bin/sh", vec
);
604 fprintf (stderr
, "unable to exec ");
611 ct
->c_pid
= child_id
;
615 pidcheck (pidXwait (child_id
, NULL
));
619 (*ct
->c_ceclosefnx
) (ct
);
620 return (alternate
? DONE
: OK
);
626 * show content of type "text"
630 show_text (CT ct
, int serial
, int alternate
)
632 char *cp
, buffer
[BUFSIZ
];
633 CI ci
= &ct
->c_ctinfo
;
635 /* Check for mhn-show-type/subtype */
636 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
637 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
638 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
639 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
641 /* Check for mhn-show-type */
642 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
643 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
644 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
647 * Use default method if content is text/plain, or if
648 * if it is not a text part of a multipart/alternative
650 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
651 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
652 moreproc
&& *moreproc
? moreproc
: "more");
653 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
654 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
662 * show message body of type "multipart"
666 show_multi (CT ct
, int serial
, int alternate
)
668 char *cp
, buffer
[BUFSIZ
];
669 CI ci
= &ct
->c_ctinfo
;
671 /* Check for mhn-show-type/subtype */
672 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
673 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
674 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
675 return show_multi_aux (ct
, serial
, alternate
, cp
);
677 /* Check for mhn-show-type */
678 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
679 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
680 return show_multi_aux (ct
, serial
, alternate
, cp
);
682 if ((cp
= ct
->c_showproc
))
683 return show_multi_aux (ct
, serial
, alternate
, cp
);
686 * Use default method to display this multipart content
687 * if it is not a (nested) part of a multipart/alternative,
688 * or if it is one of the known subtypes of multipart.
690 if (!alternate
|| ct
->c_subtype
!= MULTI_UNKNOWN
)
691 return show_multi_internal (ct
, serial
, alternate
);
698 * show message body of subtypes of multipart that
699 * we understand directly (mixed, alternate, etc...)
703 show_multi_internal (CT ct
, int serial
, int alternate
)
705 int alternating
, nowalternate
, nowserial
, result
;
706 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
712 nowalternate
= alternate
;
714 if (ct
->c_subtype
== MULTI_PARALLEL
) {
715 nowserial
= serialsw
;
716 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
724 * unknown subtypes of multipart (treat as mixed per rfc2046)
729 /* block a few signals */
732 sigaddset (&set
, SIGHUP
);
733 sigaddset (&set
, SIGINT
);
734 sigaddset (&set
, SIGQUIT
);
735 sigaddset (&set
, SIGTERM
);
736 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
740 * alternate -> we are a part inside an multipart/alternative
741 * alternating -> we are a multipart/alternative
744 result
= alternate
? NOTOK
: OK
;
746 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
749 if (part_ok (p
, 0) && type_ok (p
, 0)) {
752 inneresult
= show_switch (p
, nowserial
, nowalternate
);
753 switch (inneresult
) {
755 if (alternate
&& !alternating
) {
768 alternate
= nowalternate
= 0;
778 if (alternating
&& !part
) {
780 content_error (NULL
, ct
, "don't know how to display any of the contents");
785 if (serial
&& !nowserial
) {
788 #ifdef HAVE_UNION_WAIT
795 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
799 if (kill (p
->c_pid
, 0) == NOTOK
)
806 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
807 #ifdef HAVE_UNION_WAIT
808 pidcheck (status
.w_status
);
813 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
818 if (p
->c_pid
== pid
) {
829 /* reset the signal mask */
830 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
838 * Parse display string for multipart content
839 * and use external program to display it.
843 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
845 int len
, buflen
, quoted
;
846 int xlist
, xpause
, xtty
;
847 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
848 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
850 CI ci
= &ct
->c_ctinfo
;
853 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
856 if (!p
->c_ceopenfnx
) {
858 content_error (NULL
, p
, "don't know how to decode content");
862 if (p
->c_storage
== NULL
) {
864 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
867 /* I'm not sure if this is necessary? */
868 p
->c_storage
= add (file
, NULL
);
870 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
871 return (alternate
? DONE
: OK
);
872 (*p
->c_ceclosefnx
) (p
);
880 /* get buffer ready to go */
882 buflen
= sizeof(buffer
) - 1;
883 bp
[0] = bp
[buflen
] = '\0';
886 /* Now parse display string */
887 for ( ; *cp
&& buflen
> 0; cp
++) {
892 /* insert parameters from Content-Type field */
897 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
898 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
908 /* insert content description */
912 s
= trimcpy (ct
->c_descr
);
913 strncpy (bp
, s
, buflen
);
919 /* exclusive execution */
929 /* insert filename(s) containing content */
933 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
936 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
942 /* set our starting pointer back to bp, to avoid
943 * requoting the filenames we just added
950 /* %l, and pause prior to displaying content */
955 /* display listing prior to displaying content */
960 /* insert subtype of content */
961 strncpy (bp
, ci
->ci_subtype
, buflen
);
965 /* insert character % */
978 /* Did we actually insert something? */
980 /* Insert single quote if not inside quotes already */
981 if (!quoted
&& buflen
) {
983 memmove (pp
+ 1, pp
, len
);
988 /* Escape existing quotes */
989 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
991 memmove (pp
+ 3, pp
, len
);
998 /* If pp is still set, that means we ran out of space. */
1001 if (!quoted
&& buflen
) {
1018 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1019 /* content_error would provide a more useful error message
1020 * here, except that if we got overrun, it probably would
1023 fprintf(stderr
, "Buffer overflow constructing show command!\n");
1027 /* use charset string to modify display method */
1028 if (ct
->c_termproc
) {
1031 strncpy (term
, buffer
, sizeof(term
));
1032 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
1035 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1036 NOTOK
, xlist
, xpause
, 0, xtty
);
1041 * show content of type "message/rfc822"
1045 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1047 char *cp
, buffer
[BUFSIZ
];
1048 CI ci
= &ct
->c_ctinfo
;
1050 /* Check for mhn-show-type/subtype */
1051 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1052 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1053 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1054 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1056 /* Check for mhn-show-type */
1057 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1058 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1059 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1061 if ((cp
= ct
->c_showproc
))
1062 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1064 /* default method for message/rfc822 */
1065 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1066 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1067 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1070 /* complain if we are not a part of a multipart/alternative */
1072 content_error (NULL
, ct
, "don't know how to display content");
1079 * Show content of type "message/partial".
1083 show_partial (CT ct
, int serial
, int alternate
)
1085 content_error (NULL
, ct
,
1086 "in order to display this message, you must reassemble it");
1092 * Show content of type "message/external".
1094 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1098 show_external (CT ct
, int serial
, int alternate
)
1100 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1101 CT p
= e
->eb_content
;
1103 if (!type_ok (p
, 0))
1106 return show_switch (p
, serial
, alternate
);
1109 content_error (NULL
, p
, "don't know how to display content");
1118 #ifndef RELIABLE_SIGNALS
1119 SIGNAL (SIGINT
, intrser
);
1123 siglongjmp (intrenv
, DONE
);