]>
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);
47 void show_all_messages (CT
*);
48 int show_content_aux (CT
, int, int, char *, char *);
53 static void show_single_message (CT
, char *);
54 static void DisplayMsgHeader (CT
, char *);
55 static int show_switch (CT
, int, int);
56 static int show_content (CT
, int, int);
57 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
58 static int show_text (CT
, int, int);
59 static int show_multi (CT
, int, int);
60 static int show_multi_internal (CT
, int, int);
61 static int show_multi_aux (CT
, int, int, char *);
62 static int show_message_rfc822 (CT
, int, int);
63 static int show_partial (CT
, int, int);
64 static int show_external (CT
, int, int);
65 static void intrser (int);
69 * Top level entry point to show/display a group of messages
73 show_all_messages (CT
*cts
)
78 * If form is not specified, then get default form
79 * for showing headers of MIME messages.
82 formsw
= getcpy (etcpath ("mhl.headers"));
85 * If form is "mhl.null", suppress display of header.
87 if (!strcmp (formsw
, "mhl.null"))
90 for (ctp
= cts
; *ctp
; ctp
++) {
93 /* if top-level type is ok, then display message */
95 show_single_message (ct
, formsw
);
101 * Entry point to show/display a single message
105 show_single_message (CT ct
, char *form
)
111 /* Allow user executable bit so that temporary directories created by
112 * the viewer (e.g., lynx) are going to be accessible */
113 umask (ct
->c_umask
& ~(0100));
116 * If you have a format file, then display
117 * the message headers.
120 DisplayMsgHeader(ct
, form
);
124 /* Show the body of the message */
125 show_switch (ct
, 1, 0);
131 if (ct
->c_ceclosefnx
)
132 (*ct
->c_ceclosefnx
) (ct
);
134 /* block a few signals */
136 sigaddset (&set
, SIGHUP
);
137 sigaddset (&set
, SIGINT
);
138 sigaddset (&set
, SIGQUIT
);
139 sigaddset (&set
, SIGTERM
);
140 sigprocmask (SIG_BLOCK
, &set
, &oset
);
142 while (wait (&status
) != NOTOK
) {
147 /* reset the signal mask */
148 sigprocmask (SIG_SETMASK
, &oset
, &set
);
156 * Use the mhlproc to show the header fields
160 DisplayMsgHeader (CT ct
, char *form
)
167 vec
= argsplit(mhlproc
, &file
, &vecp
);
168 vec
[vecp
++] = getcpy("-form");
169 vec
[vecp
++] = getcpy(form
);
170 vec
[vecp
++] = getcpy("-nobody");
171 vec
[vecp
++] = getcpy(ct
->c_file
);
174 * If we've specified -(no)moreproc,
175 * then just pass that along.
178 vec
[vecp
++] = getcpy("-nomoreproc");
180 vec
[vecp
++] = getcpy("-moreproc");
181 vec
[vecp
++] = getcpy(progsw
);
187 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
192 adios ("fork", "unable to");
197 fprintf (stderr
, "unable to exec ");
207 arglist_free(file
, vec
);
212 * Switching routine. Call the correct routine
213 * based on content type.
217 show_switch (CT ct
, int serial
, int alternate
)
219 switch (ct
->c_type
) {
221 return show_multi (ct
, serial
, alternate
);
224 switch (ct
->c_subtype
) {
225 case MESSAGE_PARTIAL
:
226 return show_partial (ct
, serial
, alternate
);
228 case MESSAGE_EXTERNAL
:
229 return show_external (ct
, serial
, alternate
);
233 return show_message_rfc822 (ct
, serial
, alternate
);
237 return show_text (ct
, serial
, alternate
);
243 return show_content (ct
, serial
, alternate
);
246 adios (NULL
, "unknown content type %d", ct
->c_type
);
249 return 0; /* NOT REACHED */
254 * Generic method for displaying content
258 show_content (CT ct
, int serial
, int alternate
)
260 char *cp
, buffer
[BUFSIZ
];
261 CI ci
= &ct
->c_ctinfo
;
263 /* Check for mhn-show-type/subtype */
264 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
265 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
266 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
267 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
269 /* Check for mhn-show-type */
270 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
271 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
272 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
274 if ((cp
= ct
->c_showproc
))
275 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
277 /* complain if we are not a part of a multipart/alternative */
279 content_error (NULL
, ct
, "don't know how to display content");
286 * Parse the display string for displaying generic content
290 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
292 int fd
, len
, buflen
, quoted
;
293 int xstdin
, xlist
, xpause
, xtty
;
294 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
295 CI ci
= &ct
->c_ctinfo
;
297 if (!ct
->c_ceopenfnx
) {
299 content_error (NULL
, ct
, "don't know how to decode content");
305 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
307 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
308 return (alternate
? DONE
: OK
);
316 strncpy (buffer
, cp
, sizeof(buffer
));
320 /* get buffer ready to go */
322 buflen
= sizeof(buffer
) - 1;
323 bp
[0] = bp
[buflen
] = '\0';
326 /* Now parse display string */
327 for ( ; *cp
&& buflen
> 0; cp
++) {
333 /* insert parameters from Content-Type field */
338 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
339 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
340 get_param_value(pm
, '?'));
350 /* insert content description */
354 s
= trimcpy (ct
->c_descr
);
355 strncpy (bp
, s
, buflen
);
361 /* exclusive execution */
366 /* %e, %f, and stdin is terminal not content */
372 /* insert filename containing content */
373 snprintf (bp
, buflen
, "'%s'", file
);
374 /* since we've quoted the file argument, set things up
375 * to look past it, to avoid problems with the quoting
376 * logic below. (I know, I should figure out what's
377 * broken with the quoting logic, but..)
386 /* %l, and pause prior to displaying content */
391 /* display listing prior to displaying content */
396 /* insert subtype of content */
397 strncpy (bp
, ci
->ci_subtype
, buflen
);
401 /* insert character % */
414 /* Did we actually insert something? */
416 /* Insert single quote if not inside quotes already */
417 if (!quoted
&& buflen
) {
419 memmove (pp
+ 1, pp
, len
);
424 /* Escape existing quotes */
425 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
427 memmove (pp
+ 3, pp
, len
);
434 /* If pp is still set, that means we ran out of space. */
437 if (!quoted
&& buflen
) {
455 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
456 /* content_error would provide a more useful error message
457 * here, except that if we got overrun, it probably would
460 fprintf(stderr
, "Buffer overflow constructing show command!\n");
464 /* use charset string to modify display method */
465 if (ct
->c_termproc
) {
468 strncpy (term
, buffer
, sizeof(term
));
469 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
473 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
474 fd
, xlist
, xpause
, xstdin
, xtty
);
479 * Routine to actually display the content
483 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
484 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
488 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
490 if (debugsw
|| cracked
) {
493 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
496 fprintf (stderr
, " part %s", ct
->c_partno
);
498 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
500 fprintf (stderr
, " using command %s\n", buffer
);
503 if (xpid
< 0 || (xtty
&& xpid
)) {
506 pidcheck(pidwait (xpid
, NOTOK
));
513 if (ct
->c_type
== CT_MULTIPART
)
514 list_content (ct
, -1, 1, 0, 0, 0);
516 list_switch (ct
, -1, 1, 0, 0, 0);
518 if (xpause
&& isatty (fileno (stdout
))) {
520 SIGNAL_HANDLER istat
;
522 if (SOprintf ("Press <return> to show content..."))
523 printf ("Press <return> to show content...");
525 istat
= SIGNAL (SIGINT
, intrser
);
526 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
529 read (fileno (stdout
), prompt
, sizeof(prompt
));
531 SIGNAL (SIGINT
, istat
);
532 if (intr
!= OK
|| prompt
[0] == 'n') {
533 (*ct
->c_ceclosefnx
) (ct
);
534 return (alternate
? DONE
: NOTOK
);
536 if (prompt
[0] == 'q') done(OK
);
540 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
549 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
553 advise ("fork", "unable to");
554 (*ct
->c_ceclosefnx
) (ct
);
563 execvp ("/bin/sh", vec
);
564 fprintf (stderr
, "unable to exec ");
571 ct
->c_pid
= child_id
;
575 pidcheck (pidXwait (child_id
, NULL
));
579 (*ct
->c_ceclosefnx
) (ct
);
580 return (alternate
? DONE
: OK
);
586 * show content of type "text"
590 show_text (CT ct
, int serial
, int alternate
)
592 char *cp
, buffer
[BUFSIZ
];
593 CI ci
= &ct
->c_ctinfo
;
595 /* Check for mhn-show-type/subtype */
596 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
597 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
598 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
599 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
601 /* Check for mhn-show-type */
602 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
603 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
604 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
607 * Use default method if content is text/plain, or if
608 * if it is not a text part of a multipart/alternative
610 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
611 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
612 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
613 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
614 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
622 * show message body of type "multipart"
626 show_multi (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_multi_aux (ct
, serial
, alternate
, cp
);
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_multi_aux (ct
, serial
, alternate
, cp
);
642 if ((cp
= ct
->c_showproc
))
643 return show_multi_aux (ct
, serial
, alternate
, cp
);
646 * Use default method to display this multipart content. Even
647 * unknown types are displayable, since they're treated as mixed
650 return show_multi_internal (ct
, serial
, alternate
);
655 * show message body of subtypes of multipart that
656 * we understand directly (mixed, alternate, etc...)
660 show_multi_internal (CT ct
, int serial
, int alternate
)
662 int alternating
, nowalternate
, nowserial
, result
;
663 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
669 nowalternate
= alternate
;
671 if (ct
->c_subtype
== MULTI_PARALLEL
) {
672 nowserial
= serialsw
;
673 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
681 * unknown subtypes of multipart (treat as mixed per rfc2046)
686 /* block a few signals */
689 sigaddset (&set
, SIGHUP
);
690 sigaddset (&set
, SIGINT
);
691 sigaddset (&set
, SIGQUIT
);
692 sigaddset (&set
, SIGTERM
);
693 sigprocmask (SIG_BLOCK
, &set
, &oset
);
697 * alternate -> we are a part inside an multipart/alternative
698 * alternating -> we are a multipart/alternative
701 result
= alternate
? NOTOK
: OK
;
703 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
706 if (part_ok (p
, 1) && type_ok (p
, 1)) {
709 inneresult
= show_switch (p
, nowserial
, nowalternate
);
710 switch (inneresult
) {
712 if (alternate
&& !alternating
) {
725 alternate
= nowalternate
= 0;
735 if (alternating
&& !part
) {
737 content_error (NULL
, ct
, "don't know how to display any of the contents");
742 if (serial
&& !nowserial
) {
748 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
752 if (kill (p
->c_pid
, 0) == NOTOK
)
759 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
762 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
767 if (p
->c_pid
== pid
) {
778 /* reset the signal mask */
779 sigprocmask (SIG_SETMASK
, &oset
, &set
);
787 * Parse display string for multipart content
788 * and use external program to display it.
792 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
794 int len
, buflen
, quoted
;
795 int xlist
, xpause
, xtty
;
796 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
797 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
799 CI ci
= &ct
->c_ctinfo
;
802 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
805 if (!p
->c_ceopenfnx
) {
807 content_error (NULL
, p
, "don't know how to decode content");
811 if (p
->c_storage
== NULL
) {
813 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
816 /* I'm not sure if this is necessary? */
817 p
->c_storage
= add (file
, NULL
);
819 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
820 return (alternate
? DONE
: OK
);
821 (*p
->c_ceclosefnx
) (p
);
829 /* get buffer ready to go */
831 buflen
= sizeof(buffer
) - 1;
832 bp
[0] = bp
[buflen
] = '\0';
835 /* Now parse display string */
836 for ( ; *cp
&& buflen
> 0; cp
++) {
841 /* insert parameters from Content-Type field */
846 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
847 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
848 get_param_value(pm
, '?'));
858 /* insert content description */
862 s
= trimcpy (ct
->c_descr
);
863 strncpy (bp
, s
, buflen
);
869 /* exclusive execution */
879 /* insert filename(s) containing content */
883 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
886 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
892 /* set our starting pointer back to bp, to avoid
893 * requoting the filenames we just added
900 /* %l, and pause prior to displaying content */
905 /* display listing prior to displaying content */
910 /* insert subtype of content */
911 strncpy (bp
, ci
->ci_subtype
, buflen
);
915 /* insert character % */
928 /* Did we actually insert something? */
930 /* Insert single quote if not inside quotes already */
931 if (!quoted
&& buflen
) {
933 memmove (pp
+ 1, pp
, len
);
938 /* Escape existing quotes */
939 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
941 memmove (pp
+ 3, pp
, len
);
948 /* If pp is still set, that means we ran out of space. */
951 if (!quoted
&& buflen
) {
969 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
970 /* content_error would provide a more useful error message
971 * here, except that if we got overrun, it probably would
974 fprintf(stderr
, "Buffer overflow constructing show command!\n");
978 /* use charset string to modify display method */
979 if (ct
->c_termproc
) {
982 strncpy (term
, buffer
, sizeof(term
));
983 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
986 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
987 NOTOK
, xlist
, xpause
, 0, xtty
);
992 * show content of type "message/rfc822"
996 show_message_rfc822 (CT ct
, int serial
, int alternate
)
998 char *cp
, buffer
[BUFSIZ
];
999 CI ci
= &ct
->c_ctinfo
;
1001 /* Check for mhn-show-type/subtype */
1002 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1003 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1004 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1005 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1007 /* Check for mhn-show-type */
1008 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1009 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1010 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1012 if ((cp
= ct
->c_showproc
))
1013 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1015 /* default method for message/rfc822 */
1016 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1017 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1018 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1021 /* complain if we are not a part of a multipart/alternative */
1023 content_error (NULL
, ct
, "don't know how to display content");
1030 * Show content of type "message/partial".
1034 show_partial (CT ct
, int serial
, int alternate
)
1036 NMH_UNUSED (serial
);
1037 NMH_UNUSED (alternate
);
1039 content_error (NULL
, ct
,
1040 "in order to display this message, you must reassemble it");
1046 * Show content of type "message/external".
1048 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1052 show_external (CT ct
, int serial
, int alternate
)
1054 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1055 CT p
= e
->eb_content
;
1057 if (!type_ok (p
, 0))
1060 return show_switch (p
, serial
, alternate
);
1070 siglongjmp (intrenv
, DONE
);