]>
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 (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
343 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
353 /* insert content description */
357 s
= trimcpy (ct
->c_descr
);
358 strncpy (bp
, s
, buflen
);
364 /* exclusive execution */
369 /* %e, %f, and stdin is terminal not content */
375 /* insert filename containing content */
376 snprintf (bp
, buflen
, "'%s'", file
);
377 /* since we've quoted the file argument, set things up
378 * to look past it, to avoid problems with the quoting
379 * logic below. (I know, I should figure out what's
380 * broken with the quoting logic, but..)
389 /* %l, and pause prior to displaying content */
394 /* display listing prior to displaying content */
399 /* insert subtype of content */
400 strncpy (bp
, ci
->ci_subtype
, buflen
);
404 /* insert character % */
417 /* Did we actually insert something? */
419 /* Insert single quote if not inside quotes already */
420 if (!quoted
&& buflen
) {
422 memmove (pp
+ 1, pp
, len
);
427 /* Escape existing quotes */
428 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
430 memmove (pp
+ 3, pp
, len
);
437 /* If pp is still set, that means we ran out of space. */
440 if (!quoted
&& buflen
) {
458 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
459 /* content_error would provide a more useful error message
460 * here, except that if we got overrun, it probably would
463 fprintf(stderr
, "Buffer overflow constructing show command!\n");
467 /* use charset string to modify display method */
468 if (ct
->c_termproc
) {
471 strncpy (term
, buffer
, sizeof(term
));
472 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
476 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
477 fd
, xlist
, xpause
, xstdin
, xtty
);
482 * Routine to actually display the content
486 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
487 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
491 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
493 if (debugsw
|| cracked
) {
496 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
499 fprintf (stderr
, " part %s", ct
->c_partno
);
501 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
503 fprintf (stderr
, " using command %s\n", buffer
);
506 if (xpid
< 0 || (xtty
&& xpid
)) {
509 pidcheck(pidwait (xpid
, NOTOK
));
516 if (ct
->c_type
== CT_MULTIPART
)
517 list_content (ct
, -1, 1, 0, 0);
519 list_switch (ct
, -1, 1, 0, 0);
521 if (xpause
&& isatty (fileno (stdout
))) {
523 SIGNAL_HANDLER istat
;
525 if (SOprintf ("Press <return> to show content..."))
526 printf ("Press <return> to show content...");
528 istat
= SIGNAL (SIGINT
, intrser
);
529 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
532 read (fileno (stdout
), prompt
, sizeof(prompt
));
534 SIGNAL (SIGINT
, istat
);
535 if (intr
!= OK
|| prompt
[0] == 'n') {
536 (*ct
->c_ceclosefnx
) (ct
);
537 return (alternate
? DONE
: NOTOK
);
539 if (prompt
[0] == 'q') done(OK
);
543 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
552 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
556 advise ("fork", "unable to");
557 (*ct
->c_ceclosefnx
) (ct
);
566 execvp ("/bin/sh", vec
);
567 fprintf (stderr
, "unable to exec ");
574 ct
->c_pid
= child_id
;
578 pidcheck (pidXwait (child_id
, NULL
));
582 (*ct
->c_ceclosefnx
) (ct
);
583 return (alternate
? DONE
: OK
);
589 * show content of type "text"
593 show_text (CT ct
, int serial
, int alternate
)
595 char *cp
, buffer
[BUFSIZ
];
596 CI ci
= &ct
->c_ctinfo
;
598 /* Check for mhn-show-type/subtype */
599 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
600 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
601 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
602 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
604 /* Check for mhn-show-type */
605 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
606 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
607 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
610 * Use default method if content is text/plain, or if
611 * if it is not a text part of a multipart/alternative
613 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
614 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
615 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
616 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
617 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
625 * show message body of type "multipart"
629 show_multi (CT ct
, int serial
, int alternate
)
631 char *cp
, buffer
[BUFSIZ
];
632 CI ci
= &ct
->c_ctinfo
;
634 /* Check for mhn-show-type/subtype */
635 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
636 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
637 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
638 return show_multi_aux (ct
, serial
, alternate
, cp
);
640 /* Check for mhn-show-type */
641 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
642 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
643 return show_multi_aux (ct
, serial
, alternate
, cp
);
645 if ((cp
= ct
->c_showproc
))
646 return show_multi_aux (ct
, serial
, alternate
, cp
);
649 * Use default method to display this multipart content. Even
650 * unknown types are displayable, since they're treated as mixed
653 return show_multi_internal (ct
, serial
, alternate
);
658 * show message body of subtypes of multipart that
659 * we understand directly (mixed, alternate, etc...)
663 show_multi_internal (CT ct
, int serial
, int alternate
)
665 int alternating
, nowalternate
, nowserial
, result
;
666 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
672 nowalternate
= alternate
;
674 if (ct
->c_subtype
== MULTI_PARALLEL
) {
675 nowserial
= serialsw
;
676 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
684 * unknown subtypes of multipart (treat as mixed per rfc2046)
689 /* block a few signals */
692 sigaddset (&set
, SIGHUP
);
693 sigaddset (&set
, SIGINT
);
694 sigaddset (&set
, SIGQUIT
);
695 sigaddset (&set
, SIGTERM
);
696 sigprocmask (SIG_BLOCK
, &set
, &oset
);
700 * alternate -> we are a part inside an multipart/alternative
701 * alternating -> we are a multipart/alternative
704 result
= alternate
? NOTOK
: OK
;
706 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
709 if (part_ok (p
, 1) && type_ok (p
, 1)) {
712 inneresult
= show_switch (p
, nowserial
, nowalternate
);
713 switch (inneresult
) {
715 if (alternate
&& !alternating
) {
728 alternate
= nowalternate
= 0;
738 if (alternating
&& !part
) {
740 content_error (NULL
, ct
, "don't know how to display any of the contents");
745 if (serial
&& !nowserial
) {
751 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
755 if (kill (p
->c_pid
, 0) == NOTOK
)
762 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
765 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
770 if (p
->c_pid
== pid
) {
781 /* reset the signal mask */
782 sigprocmask (SIG_SETMASK
, &oset
, &set
);
790 * Parse display string for multipart content
791 * and use external program to display it.
795 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
797 int len
, buflen
, quoted
;
798 int xlist
, xpause
, xtty
;
799 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
800 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
802 CI ci
= &ct
->c_ctinfo
;
805 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
808 if (!p
->c_ceopenfnx
) {
810 content_error (NULL
, p
, "don't know how to decode content");
814 if (p
->c_storage
== NULL
) {
816 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
819 /* I'm not sure if this is necessary? */
820 p
->c_storage
= add (file
, NULL
);
822 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
823 return (alternate
? DONE
: OK
);
824 (*p
->c_ceclosefnx
) (p
);
832 /* get buffer ready to go */
834 buflen
= sizeof(buffer
) - 1;
835 bp
[0] = bp
[buflen
] = '\0';
838 /* Now parse display string */
839 for ( ; *cp
&& buflen
> 0; cp
++) {
844 /* insert parameters from Content-Type field */
849 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
850 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
860 /* insert content description */
864 s
= trimcpy (ct
->c_descr
);
865 strncpy (bp
, s
, buflen
);
871 /* exclusive execution */
881 /* insert filename(s) containing content */
885 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
888 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
894 /* set our starting pointer back to bp, to avoid
895 * requoting the filenames we just added
902 /* %l, and pause prior to displaying content */
907 /* display listing prior to displaying content */
912 /* insert subtype of content */
913 strncpy (bp
, ci
->ci_subtype
, buflen
);
917 /* insert character % */
930 /* Did we actually insert something? */
932 /* Insert single quote if not inside quotes already */
933 if (!quoted
&& buflen
) {
935 memmove (pp
+ 1, pp
, len
);
940 /* Escape existing quotes */
941 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
943 memmove (pp
+ 3, pp
, len
);
950 /* If pp is still set, that means we ran out of space. */
953 if (!quoted
&& buflen
) {
971 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
972 /* content_error would provide a more useful error message
973 * here, except that if we got overrun, it probably would
976 fprintf(stderr
, "Buffer overflow constructing show command!\n");
980 /* use charset string to modify display method */
981 if (ct
->c_termproc
) {
984 strncpy (term
, buffer
, sizeof(term
));
985 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
988 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
989 NOTOK
, xlist
, xpause
, 0, xtty
);
994 * show content of type "message/rfc822"
998 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1000 char *cp
, buffer
[BUFSIZ
];
1001 CI ci
= &ct
->c_ctinfo
;
1003 /* Check for mhn-show-type/subtype */
1004 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1005 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1006 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1007 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1009 /* Check for mhn-show-type */
1010 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1011 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1012 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1014 if ((cp
= ct
->c_showproc
))
1015 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1017 /* default method for message/rfc822 */
1018 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1019 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1020 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1023 /* complain if we are not a part of a multipart/alternative */
1025 content_error (NULL
, ct
, "don't know how to display content");
1032 * Show content of type "message/partial".
1036 show_partial (CT ct
, int serial
, int alternate
)
1038 NMH_UNUSED (serial
);
1039 NMH_UNUSED (alternate
);
1041 content_error (NULL
, ct
,
1042 "in order to display this message, you must reassemble it");
1048 * Show content of type "message/external".
1050 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1054 show_external (CT ct
, int serial
, int alternate
)
1056 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1057 CT p
= e
->eb_content
;
1059 if (!type_ok (p
, 0))
1062 return show_switch (p
, serial
, alternate
);
1072 siglongjmp (intrenv
, DONE
);