]>
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
);
228 switch (ct
->c_subtype
) {
229 case MESSAGE_PARTIAL
:
230 return show_partial (ct
, serial
, alternate
);
232 case MESSAGE_EXTERNAL
:
233 return show_external (ct
, serial
, alternate
);
237 return show_message_rfc822 (ct
, serial
, alternate
);
241 return show_text (ct
, serial
, alternate
);
247 return show_content (ct
, serial
, alternate
);
250 adios (NULL
, "unknown content type %d", ct
->c_type
);
253 return 0; /* NOT REACHED */
258 * Generic method for displaying content
262 show_content (CT ct
, int serial
, int alternate
)
264 char *cp
, buffer
[BUFSIZ
];
265 CI ci
= &ct
->c_ctinfo
;
267 /* Check for mhn-show-type/subtype */
268 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
269 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
270 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
271 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
273 /* Check for mhn-show-type */
274 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
275 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
276 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
278 if ((cp
= ct
->c_showproc
))
279 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
281 /* complain if we are not a part of a multipart/alternative */
283 content_error (NULL
, ct
, "don't know how to display content");
290 * Parse the display string for displaying generic content
294 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
296 int fd
, len
, buflen
, quoted
;
297 int xstdin
, xlist
, xpause
, xtty
;
298 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
299 CI ci
= &ct
->c_ctinfo
;
301 if (!ct
->c_ceopenfnx
) {
303 content_error (NULL
, ct
, "don't know how to decode content");
309 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
311 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
312 return (alternate
? DONE
: OK
);
320 strncpy (buffer
, cp
, sizeof(buffer
));
324 /* get buffer ready to go */
326 buflen
= sizeof(buffer
) - 1;
327 bp
[0] = bp
[buflen
] = '\0';
330 /* Now parse display string */
331 for ( ; *cp
&& buflen
> 0; cp
++) {
337 /* insert parameters from Content-Type field */
342 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
343 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
354 /* insert content description */
358 s
= trimcpy (ct
->c_descr
);
359 strncpy (bp
, s
, buflen
);
365 /* exclusive execution */
370 /* %e, %f, and stdin is terminal not content */
376 /* insert filename containing content */
377 snprintf (bp
, buflen
, "'%s'", file
);
378 /* since we've quoted the file argument, set things up
379 * to look past it, to avoid problems with the quoting
380 * logic below. (I know, I should figure out what's
381 * broken with the quoting logic, but..)
390 /* %l, and pause prior to displaying content */
395 /* display listing prior to displaying content */
400 /* insert subtype of content */
401 strncpy (bp
, ci
->ci_subtype
, buflen
);
405 /* insert character % */
418 /* Did we actually insert something? */
420 /* Insert single quote if not inside quotes already */
421 if (!quoted
&& buflen
) {
423 memmove (pp
+ 1, pp
, len
);
428 /* Escape existing quotes */
429 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
431 memmove (pp
+ 3, pp
, len
);
438 /* If pp is still set, that means we ran out of space. */
441 if (!quoted
&& buflen
) {
459 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
460 /* content_error would provide a more useful error message
461 * here, except that if we got overrun, it probably would
464 fprintf(stderr
, "Buffer overflow constructing show command!\n");
468 /* use charset string to modify display method */
469 if (ct
->c_termproc
) {
472 strncpy (term
, buffer
, sizeof(term
));
473 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
477 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
478 fd
, xlist
, xpause
, xstdin
, xtty
);
483 * Routine to actually display the content
487 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
488 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
492 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
494 if (debugsw
|| cracked
) {
497 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
500 fprintf (stderr
, " part %s", ct
->c_partno
);
502 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
504 fprintf (stderr
, " using command %s\n", buffer
);
507 if (xpid
< 0 || (xtty
&& xpid
)) {
510 pidcheck(pidwait (xpid
, NOTOK
));
517 if (ct
->c_type
== CT_MULTIPART
)
518 list_content (ct
, -1, 1, 0, 0);
520 list_switch (ct
, -1, 1, 0, 0);
522 if (xpause
&& isatty (fileno (stdout
))) {
524 SIGNAL_HANDLER istat
;
526 if (SOprintf ("Press <return> to show content..."))
527 printf ("Press <return> to show content...");
529 istat
= SIGNAL (SIGINT
, intrser
);
530 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
533 read (fileno (stdout
), prompt
, sizeof(prompt
));
535 SIGNAL (SIGINT
, istat
);
536 if (intr
!= OK
|| prompt
[0] == 'n') {
537 (*ct
->c_ceclosefnx
) (ct
);
538 return (alternate
? DONE
: NOTOK
);
540 if (prompt
[0] == 'q') done(OK
);
544 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
553 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
557 advise ("fork", "unable to");
558 (*ct
->c_ceclosefnx
) (ct
);
567 execvp ("/bin/sh", vec
);
568 fprintf (stderr
, "unable to exec ");
575 ct
->c_pid
= child_id
;
579 pidcheck (pidXwait (child_id
, NULL
));
583 (*ct
->c_ceclosefnx
) (ct
);
584 return (alternate
? DONE
: OK
);
590 * show content of type "text"
594 show_text (CT ct
, int serial
, int alternate
)
596 char *cp
, buffer
[BUFSIZ
];
597 CI ci
= &ct
->c_ctinfo
;
599 /* Check for mhn-show-type/subtype */
600 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
601 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
602 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
603 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
605 /* Check for mhn-show-type */
606 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
607 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
608 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
611 * Use default method if content is text/plain, or if
612 * if it is not a text part of a multipart/alternative
614 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
615 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
616 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
617 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
618 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
626 * show message body of type "multipart"
630 show_multi (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_multi_aux (ct
, serial
, alternate
, cp
);
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_multi_aux (ct
, serial
, alternate
, cp
);
646 if ((cp
= ct
->c_showproc
))
647 return show_multi_aux (ct
, serial
, alternate
, cp
);
650 * Use default method to display this multipart content. Even
651 * unknown types are displayable, since they're treated as mixed
654 return show_multi_internal (ct
, serial
, alternate
);
659 * show message body of subtypes of multipart that
660 * we understand directly (mixed, alternate, etc...)
664 show_multi_internal (CT ct
, int serial
, int alternate
)
666 int alternating
, nowalternate
, nowserial
, result
;
667 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
673 nowalternate
= alternate
;
675 if (ct
->c_subtype
== MULTI_PARALLEL
) {
676 nowserial
= serialsw
;
677 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
685 * unknown subtypes of multipart (treat as mixed per rfc2046)
690 /* block a few signals */
693 sigaddset (&set
, SIGHUP
);
694 sigaddset (&set
, SIGINT
);
695 sigaddset (&set
, SIGQUIT
);
696 sigaddset (&set
, SIGTERM
);
697 sigprocmask (SIG_BLOCK
, &set
, &oset
);
701 * alternate -> we are a part inside an multipart/alternative
702 * alternating -> we are a multipart/alternative
705 result
= alternate
? NOTOK
: OK
;
707 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
710 if (part_ok (p
, 1) && type_ok (p
, 1)) {
713 inneresult
= show_switch (p
, nowserial
, nowalternate
);
714 switch (inneresult
) {
716 if (alternate
&& !alternating
) {
729 alternate
= nowalternate
= 0;
739 if (alternating
&& !part
) {
741 content_error (NULL
, ct
, "don't know how to display any of the contents");
746 if (serial
&& !nowserial
) {
752 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
756 if (kill (p
->c_pid
, 0) == NOTOK
)
763 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
766 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
771 if (p
->c_pid
== pid
) {
782 /* reset the signal mask */
783 sigprocmask (SIG_SETMASK
, &oset
, &set
);
791 * Parse display string for multipart content
792 * and use external program to display it.
796 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
798 int len
, buflen
, quoted
;
799 int xlist
, xpause
, xtty
;
800 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
801 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
803 CI ci
= &ct
->c_ctinfo
;
806 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
809 if (!p
->c_ceopenfnx
) {
811 content_error (NULL
, p
, "don't know how to decode content");
815 if (p
->c_storage
== NULL
) {
817 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
820 /* I'm not sure if this is necessary? */
821 p
->c_storage
= add (file
, NULL
);
823 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
824 return (alternate
? DONE
: OK
);
825 (*p
->c_ceclosefnx
) (p
);
833 /* get buffer ready to go */
835 buflen
= sizeof(buffer
) - 1;
836 bp
[0] = bp
[buflen
] = '\0';
839 /* Now parse display string */
840 for ( ; *cp
&& buflen
> 0; cp
++) {
845 /* insert parameters from Content-Type field */
850 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
851 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
862 /* insert content description */
866 s
= trimcpy (ct
->c_descr
);
867 strncpy (bp
, s
, buflen
);
873 /* exclusive execution */
883 /* insert filename(s) containing content */
887 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
890 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
896 /* set our starting pointer back to bp, to avoid
897 * requoting the filenames we just added
904 /* %l, and pause prior to displaying content */
909 /* display listing prior to displaying content */
914 /* insert subtype of content */
915 strncpy (bp
, ci
->ci_subtype
, buflen
);
919 /* insert character % */
932 /* Did we actually insert something? */
934 /* Insert single quote if not inside quotes already */
935 if (!quoted
&& buflen
) {
937 memmove (pp
+ 1, pp
, len
);
942 /* Escape existing quotes */
943 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
945 memmove (pp
+ 3, pp
, len
);
952 /* If pp is still set, that means we ran out of space. */
955 if (!quoted
&& buflen
) {
973 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
974 /* content_error would provide a more useful error message
975 * here, except that if we got overrun, it probably would
978 fprintf(stderr
, "Buffer overflow constructing show command!\n");
982 /* use charset string to modify display method */
983 if (ct
->c_termproc
) {
986 strncpy (term
, buffer
, sizeof(term
));
987 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
990 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
991 NOTOK
, xlist
, xpause
, 0, xtty
);
996 * show content of type "message/rfc822"
1000 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1002 char *cp
, buffer
[BUFSIZ
];
1003 CI ci
= &ct
->c_ctinfo
;
1005 /* Check for mhn-show-type/subtype */
1006 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1007 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1008 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1009 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1011 /* Check for mhn-show-type */
1012 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1013 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1014 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1016 if ((cp
= ct
->c_showproc
))
1017 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1019 /* default method for message/rfc822 */
1020 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1021 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1022 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1025 /* complain if we are not a part of a multipart/alternative */
1027 content_error (NULL
, ct
, "don't know how to display content");
1034 * Show content of type "message/partial".
1038 show_partial (CT ct
, int serial
, int alternate
)
1040 NMH_UNUSED (serial
);
1041 NMH_UNUSED (alternate
);
1043 content_error (NULL
, ct
,
1044 "in order to display this message, you must reassemble it");
1050 * Show content of type "message/external".
1052 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1056 show_external (CT ct
, int serial
, int alternate
)
1058 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1059 CT p
= e
->eb_content
;
1061 if (!type_ok (p
, 0))
1064 return show_switch (p
, serial
, alternate
);
1074 siglongjmp (intrenv
, DONE
);