]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3 * mhshowsbr.c -- routines to display the contents of MIME messages
10 #include <h/signals.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
)
139 /* Allow user executable bit so that temporary directories created by
140 * the viewer (e.g., lynx) are going to be accessible */
141 umask (ct
->c_umask
& ~(0100));
144 * If you have a format file, then display
145 * the message headers.
148 DisplayMsgHeader(ct
, form
);
152 /* Show the body of the message */
153 show_switch (ct
, 1, 0);
159 if (ct
->c_ceclosefnx
)
160 (*ct
->c_ceclosefnx
) (ct
);
162 /* block a few signals */
164 sigaddset (&set
, SIGHUP
);
165 sigaddset (&set
, SIGINT
);
166 sigaddset (&set
, SIGQUIT
);
167 sigaddset (&set
, SIGTERM
);
168 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
170 while (wait (&status
) != NOTOK
) {
174 pidcheck (status
.w_status
);
179 /* reset the signal mask */
180 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
188 * Use the mhlproc to show the header fields
192 DisplayMsgHeader (CT ct
, char *form
)
199 vec
[vecp
++] = r1bindex (mhlproc
, '/');
200 vec
[vecp
++] = "-form";
202 vec
[vecp
++] = "-nobody";
203 vec
[vecp
++] = ct
->c_file
;
206 * If we've specified -(no)moreproc,
207 * then just pass that along.
210 vec
[vecp
++] = "-nomoreproc";
212 vec
[vecp
++] = "-moreproc";
213 vec
[vecp
++] = progsw
;
219 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
224 adios ("fork", "unable to");
228 execvp (mhlproc
, vec
);
229 fprintf (stderr
, "unable to exec ");
242 * Switching routine. Call the correct routine
243 * based on content type.
247 show_switch (CT ct
, int serial
, int alternate
)
249 switch (ct
->c_type
) {
251 return show_multi (ct
, serial
, alternate
);
255 switch (ct
->c_subtype
) {
256 case MESSAGE_PARTIAL
:
257 return show_partial (ct
, serial
, alternate
);
260 case MESSAGE_EXTERNAL
:
261 return show_external (ct
, serial
, alternate
);
266 return show_message_rfc822 (ct
, serial
, alternate
);
272 return show_text (ct
, serial
, alternate
);
279 return show_content (ct
, serial
, alternate
);
283 adios (NULL
, "unknown content type %d", ct
->c_type
);
287 return 0; /* NOT REACHED */
292 * Generic method for displaying content
296 show_content (CT ct
, int serial
, int alternate
)
298 char *cp
, buffer
[BUFSIZ
];
299 CI ci
= &ct
->c_ctinfo
;
301 /* Check for mhn-show-type/subtype */
302 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
303 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
304 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
305 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
307 /* Check for mhn-show-type */
308 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
309 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
310 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
312 if ((cp
= ct
->c_showproc
))
313 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
315 /* complain if we are not a part of a multipart/alternative */
317 content_error (NULL
, ct
, "don't know how to display content");
324 * Parse the display string for displaying generic content
328 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
330 int fd
, len
, buflen
, quoted
;
331 int xstdin
, xlist
, xpause
, xtty
;
332 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
333 CI ci
= &ct
->c_ctinfo
;
335 if (!ct
->c_ceopenfnx
) {
337 content_error (NULL
, ct
, "don't know how to decode content");
343 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
345 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
346 return (alternate
? DONE
: OK
);
354 strncpy (buffer
, cp
, sizeof(buffer
));
358 /* get buffer ready to go */
360 buflen
= sizeof(buffer
) - 1;
361 bp
[0] = bp
[buflen
] = '\0';
364 /* Now parse display string */
365 for ( ; *cp
&& buflen
> 0; cp
++) {
371 /* insert parameters from Content-Type field */
376 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
377 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
387 /* insert content description */
391 s
= trimcpy (ct
->c_descr
);
392 strncpy (bp
, s
, buflen
);
398 /* exclusive execution */
403 /* %e, %f, and stdin is terminal not content */
409 /* insert filename containing content */
410 snprintf (bp
, buflen
, "'%s'", file
);
411 /* since we've quoted the file argument, set things up
412 * to look past it, to avoid problems with the quoting
413 * logic below. (I know, I should figure out what's
414 * broken with the quoting logic, but..)
423 /* %l, and pause prior to displaying content */
428 /* display listing prior to displaying content */
433 /* insert subtype of content */
434 strncpy (bp
, ci
->ci_subtype
, buflen
);
438 /* insert character % */
451 /* Did we actually insert something? */
453 /* Insert single quote if not inside quotes already */
454 if (!quoted
&& buflen
) {
456 memmove (pp
+ 1, pp
, len
);
461 /* Escape existing quotes */
462 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
464 memmove (pp
+ 3, pp
, len
);
471 /* If pp is still set, that means we ran out of space. */
474 if (!quoted
&& buflen
) {
491 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
492 /* content_error would provide a more useful error message
493 * here, except that if we got overrun, it probably would
496 fprintf(stderr
, "Buffer overflow constructing show command!\n");
500 /* use charset string to modify display method */
501 if (ct
->c_termproc
) {
504 strncpy (term
, buffer
, sizeof(term
));
505 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
509 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
510 fd
, xlist
, xpause
, xstdin
, xtty
);
515 * Routine to actually display the content
519 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
520 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
524 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
526 if (debugsw
|| cracked
) {
529 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
532 fprintf (stderr
, " part %s", ct
->c_partno
);
534 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
536 fprintf (stderr
, " using command %s\n", buffer
);
539 if (xpid
< 0 || (xtty
&& xpid
)) {
542 pidcheck(pidwait (xpid
, NOTOK
));
549 if (ct
->c_type
== CT_MULTIPART
)
550 list_content (ct
, -1, 1, 0, 0);
552 list_switch (ct
, -1, 1, 0, 0);
554 if (xpause
&& SOprintf ("Press <return> to show content..."))
555 printf ("Press <return> to show content...");
559 SIGNAL_HANDLER istat
;
561 istat
= SIGNAL (SIGINT
, intrser
);
562 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
565 read (fileno (stdout
), prompt
, sizeof(prompt
));
567 SIGNAL (SIGINT
, istat
);
568 if (intr
!= OK
|| prompt
[0] == 'n') {
569 (*ct
->c_ceclosefnx
) (ct
);
570 return (alternate
? DONE
: NOTOK
);
572 if (prompt
[0] == 'q') done(OK
);
576 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
585 for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
589 advise ("fork", "unable to");
590 (*ct
->c_ceclosefnx
) (ct
);
599 execvp ("/bin/sh", vec
);
600 fprintf (stderr
, "unable to exec ");
607 ct
->c_pid
= child_id
;
611 pidcheck (pidXwait (child_id
, NULL
));
615 (*ct
->c_ceclosefnx
) (ct
);
616 return (alternate
? DONE
: OK
);
622 * show content of type "text"
626 show_text (CT ct
, int serial
, int alternate
)
628 char *cp
, buffer
[BUFSIZ
];
629 CI ci
= &ct
->c_ctinfo
;
631 /* Check for mhn-show-type/subtype */
632 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
633 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
634 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
635 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
637 /* Check for mhn-show-type */
638 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
639 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
640 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
643 * Use default method if content is text/plain, or if
644 * if it is not a text part of a multipart/alternative
646 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
647 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
648 moreproc
&& *moreproc
? moreproc
: "more");
649 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
650 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
658 * show message body of type "multipart"
662 show_multi (CT ct
, int serial
, int alternate
)
664 char *cp
, buffer
[BUFSIZ
];
665 CI ci
= &ct
->c_ctinfo
;
667 /* Check for mhn-show-type/subtype */
668 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
669 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
670 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
671 return show_multi_aux (ct
, serial
, alternate
, cp
);
673 /* Check for mhn-show-type */
674 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
675 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
676 return show_multi_aux (ct
, serial
, alternate
, cp
);
678 if ((cp
= ct
->c_showproc
))
679 return show_multi_aux (ct
, serial
, alternate
, cp
);
682 * Use default method to display this multipart content
683 * if it is not a (nested) part of a multipart/alternative,
684 * or if it is one of the known subtypes of multipart.
686 if (!alternate
|| ct
->c_subtype
!= MULTI_UNKNOWN
)
687 return show_multi_internal (ct
, serial
, alternate
);
694 * show message body of subtypes of multipart that
695 * we understand directly (mixed, alternate, etc...)
699 show_multi_internal (CT ct
, int serial
, int alternate
)
701 int alternating
, nowalternate
, nowserial
, result
;
702 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
708 nowalternate
= alternate
;
710 if (ct
->c_subtype
== MULTI_PARALLEL
) {
711 nowserial
= serialsw
;
712 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
720 * unknown subtypes of multipart (treat as mixed per rfc2046)
725 /* block a few signals */
728 sigaddset (&set
, SIGHUP
);
729 sigaddset (&set
, SIGINT
);
730 sigaddset (&set
, SIGQUIT
);
731 sigaddset (&set
, SIGTERM
);
732 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
736 * alternate -> we are a part inside an multipart/alternative
737 * alternating -> we are a multipart/alternative
740 result
= alternate
? NOTOK
: OK
;
742 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
745 if (part_ok (p
, 0) && type_ok (p
, 0)) {
748 inneresult
= show_switch (p
, nowserial
, nowalternate
);
749 switch (inneresult
) {
751 if (alternate
&& !alternating
) {
764 alternate
= nowalternate
= 0;
774 if (alternating
&& !part
) {
776 content_error (NULL
, ct
, "don't know how to display any of the contents");
781 if (serial
&& !nowserial
) {
791 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
795 if (kill (p
->c_pid
, 0) == NOTOK
)
802 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
806 pidcheck (status
.w_status
);
809 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
814 if (p
->c_pid
== pid
) {
825 /* reset the signal mask */
826 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
834 * Parse display string for multipart content
835 * and use external program to display it.
839 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
841 int len
, buflen
, quoted
;
842 int xlist
, xpause
, xtty
;
843 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
844 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
846 CI ci
= &ct
->c_ctinfo
;
849 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
852 if (!p
->c_ceopenfnx
) {
854 content_error (NULL
, p
, "don't know how to decode content");
858 if (p
->c_storage
== NULL
) {
860 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
863 /* I'm not sure if this is necessary? */
864 p
->c_storage
= add (file
, NULL
);
866 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
867 return (alternate
? DONE
: OK
);
868 (*p
->c_ceclosefnx
) (p
);
876 /* get buffer ready to go */
878 buflen
= sizeof(buffer
) - 1;
879 bp
[0] = bp
[buflen
] = '\0';
882 /* Now parse display string */
883 for ( ; *cp
&& buflen
> 0; cp
++) {
888 /* insert parameters from Content-Type field */
893 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
894 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
904 /* insert content description */
908 s
= trimcpy (ct
->c_descr
);
909 strncpy (bp
, s
, buflen
);
915 /* exclusive execution */
925 /* insert filename(s) containing content */
929 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
932 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
938 /* set our starting pointer back to bp, to avoid
939 * requoting the filenames we just added
946 /* %l, and pause prior to displaying content */
951 /* display listing prior to displaying content */
956 /* insert subtype of content */
957 strncpy (bp
, ci
->ci_subtype
, buflen
);
961 /* insert character % */
974 /* Did we actually insert something? */
976 /* Insert single quote if not inside quotes already */
977 if (!quoted
&& buflen
) {
979 memmove (pp
+ 1, pp
, len
);
984 /* Escape existing quotes */
985 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
987 memmove (pp
+ 3, pp
, len
);
994 /* If pp is still set, that means we ran out of space. */
997 if (!quoted
&& buflen
) {
1014 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1015 /* content_error would provide a more useful error message
1016 * here, except that if we got overrun, it probably would
1019 fprintf(stderr
, "Buffer overflow constructing show command!\n");
1023 /* use charset string to modify display method */
1024 if (ct
->c_termproc
) {
1027 strncpy (term
, buffer
, sizeof(term
));
1028 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
1031 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1032 NOTOK
, xlist
, xpause
, 0, xtty
);
1037 * show content of type "message/rfc822"
1041 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1043 char *cp
, buffer
[BUFSIZ
];
1044 CI ci
= &ct
->c_ctinfo
;
1046 /* Check for mhn-show-type/subtype */
1047 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1048 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1049 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1050 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1052 /* Check for mhn-show-type */
1053 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1054 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1055 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1057 if ((cp
= ct
->c_showproc
))
1058 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1060 /* default method for message/rfc822 */
1061 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1062 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1063 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1066 /* complain if we are not a part of a multipart/alternative */
1068 content_error (NULL
, ct
, "don't know how to display content");
1075 * Show content of type "message/partial".
1079 show_partial (CT ct
, int serial
, int alternate
)
1081 content_error (NULL
, ct
,
1082 "in order to display this message, you must reassemble it");
1088 * Show content of type "message/external".
1090 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1094 show_external (CT ct
, int serial
, int alternate
)
1096 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1097 CT p
= e
->eb_content
;
1099 if (!type_ok (p
, 0))
1102 return show_switch (p
, serial
, alternate
);
1105 content_error (NULL
, p
, "don't know how to display content");
1114 #ifndef RELIABLE_SIGNALS
1115 SIGNAL (SIGINT
, intrser
);
1119 siglongjmp (intrenv
, DONE
);