]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3d2d8eb0db647e5e1d04679c311ae79bec317255
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>
20 #include <h/mhparse.h>
31 /* flags for moreproc/header display */
37 static sigjmp_buf intrenv
;
41 int part_ok (CT
, int);
42 int type_ok (CT
, int);
43 void content_error (char *, CT
, char *, ...);
44 void flush_errors (void);
47 int list_switch (CT
, int, int, int, int);
48 int list_content (CT
, int, int, int, int);
53 void show_all_messages (CT
*);
54 int show_content_aux (CT
, int, int, char *, char *);
59 static void show_single_message (CT
, char *);
60 static void DisplayMsgHeader (CT
, char *);
61 static int show_switch (CT
, int, int);
62 static int show_content (CT
, int, int);
63 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
64 static int show_text (CT
, int, int);
65 static int show_multi (CT
, int, int);
66 static int show_multi_internal (CT
, int, int);
67 static int show_multi_aux (CT
, int, int, char *);
68 static int show_message_rfc822 (CT
, int, int);
69 static int show_partial (CT
, int, int);
70 static int show_external (CT
, int, int);
71 static void intrser (int);
75 * Top level entry point to show/display a group of messages
79 show_all_messages (CT
*cts
)
84 * If form is not specified, then get default form
85 * for showing headers of MIME messages.
88 formsw
= getcpy (etcpath ("mhl.headers"));
91 * If form is "mhl.null", suppress display of header.
93 if (!strcmp (formsw
, "mhl.null"))
96 for (ctp
= cts
; *ctp
; ctp
++) {
99 /* if top-level type is ok, then display message */
101 show_single_message (ct
, formsw
);
107 * Entry point to show/display a single message
111 show_single_message (CT ct
, char *form
)
117 /* Allow user executable bit so that temporary directories created by
118 * the viewer (e.g., lynx) are going to be accessible */
119 umask (ct
->c_umask
& ~(0100));
122 * If you have a format file, then display
123 * the message headers.
126 DisplayMsgHeader(ct
, form
);
130 /* Show the body of the message */
131 show_switch (ct
, 1, 0);
137 if (ct
->c_ceclosefnx
)
138 (*ct
->c_ceclosefnx
) (ct
);
140 /* block a few signals */
142 sigaddset (&set
, SIGHUP
);
143 sigaddset (&set
, SIGINT
);
144 sigaddset (&set
, SIGQUIT
);
145 sigaddset (&set
, SIGTERM
);
146 sigprocmask (SIG_BLOCK
, &set
, &oset
);
148 while (wait (&status
) != NOTOK
) {
153 /* reset the signal mask */
154 sigprocmask (SIG_SETMASK
, &oset
, &set
);
162 * Use the mhlproc to show the header fields
166 DisplayMsgHeader (CT ct
, char *form
)
173 vec
= argsplit(mhlproc
, &file
, &vecp
);
174 vec
[vecp
++] = getcpy("-form");
175 vec
[vecp
++] = getcpy(form
);
176 vec
[vecp
++] = getcpy("-nobody");
177 vec
[vecp
++] = getcpy(ct
->c_file
);
180 * If we've specified -(no)moreproc,
181 * then just pass that along.
184 vec
[vecp
++] = getcpy("-nomoreproc");
186 vec
[vecp
++] = getcpy("-moreproc");
187 vec
[vecp
++] = getcpy(progsw
);
193 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
198 adios ("fork", "unable to");
203 fprintf (stderr
, "unable to exec ");
213 arglist_free(file
, vec
);
218 * Switching routine. Call the correct routine
219 * based on content type.
223 show_switch (CT ct
, int serial
, int alternate
)
225 switch (ct
->c_type
) {
227 return show_multi (ct
, serial
, alternate
);
231 switch (ct
->c_subtype
) {
232 case MESSAGE_PARTIAL
:
233 return show_partial (ct
, serial
, alternate
);
236 case MESSAGE_EXTERNAL
:
237 return show_external (ct
, serial
, alternate
);
242 return show_message_rfc822 (ct
, serial
, alternate
);
248 return show_text (ct
, serial
, alternate
);
255 return show_content (ct
, serial
, alternate
);
259 adios (NULL
, "unknown content type %d", ct
->c_type
);
263 return 0; /* NOT REACHED */
268 * Generic method for displaying content
272 show_content (CT ct
, int serial
, int alternate
)
274 char *cp
, buffer
[BUFSIZ
];
275 CI ci
= &ct
->c_ctinfo
;
277 /* Check for mhn-show-type/subtype */
278 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
279 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
280 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
281 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
283 /* Check for mhn-show-type */
284 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
285 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
286 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
288 if ((cp
= ct
->c_showproc
))
289 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
291 /* complain if we are not a part of a multipart/alternative */
293 content_error (NULL
, ct
, "don't know how to display content");
300 * Parse the display string for displaying generic content
304 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
306 int fd
, len
, buflen
, quoted
;
307 int xstdin
, xlist
, xpause
, xtty
;
308 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
309 CI ci
= &ct
->c_ctinfo
;
311 if (!ct
->c_ceopenfnx
) {
313 content_error (NULL
, ct
, "don't know how to decode content");
319 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
321 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
322 return (alternate
? DONE
: OK
);
330 strncpy (buffer
, cp
, sizeof(buffer
));
334 /* get buffer ready to go */
336 buflen
= sizeof(buffer
) - 1;
337 bp
[0] = bp
[buflen
] = '\0';
340 /* Now parse display string */
341 for ( ; *cp
&& buflen
> 0; cp
++) {
347 /* insert parameters from Content-Type field */
352 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
353 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
363 /* insert content description */
367 s
= trimcpy (ct
->c_descr
);
368 strncpy (bp
, s
, buflen
);
374 /* exclusive execution */
379 /* %e, %f, and stdin is terminal not content */
385 /* insert filename containing content */
386 snprintf (bp
, buflen
, "'%s'", file
);
387 /* since we've quoted the file argument, set things up
388 * to look past it, to avoid problems with the quoting
389 * logic below. (I know, I should figure out what's
390 * broken with the quoting logic, but..)
399 /* %l, and pause prior to displaying content */
404 /* display listing prior to displaying content */
409 /* insert subtype of content */
410 strncpy (bp
, ci
->ci_subtype
, buflen
);
414 /* insert character % */
427 /* Did we actually insert something? */
429 /* Insert single quote if not inside quotes already */
430 if (!quoted
&& buflen
) {
432 memmove (pp
+ 1, pp
, len
);
437 /* Escape existing quotes */
438 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
440 memmove (pp
+ 3, pp
, len
);
447 /* If pp is still set, that means we ran out of space. */
450 if (!quoted
&& buflen
) {
468 (ct
->c_termproc
&& (size_t) buflen
<= strlen(ct
->c_termproc
))) {
469 /* content_error would provide a more useful error message
470 * here, except that if we got overrun, it probably would
473 fprintf(stderr
, "Buffer overflow constructing show command!\n");
477 /* use charset string to modify display method */
478 if (ct
->c_termproc
) {
481 strncpy (term
, buffer
, sizeof(term
));
482 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
486 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
487 fd
, xlist
, xpause
, xstdin
, xtty
);
492 * Routine to actually display the content
496 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
497 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
501 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
503 if (debugsw
|| cracked
) {
506 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
509 fprintf (stderr
, " part %s", ct
->c_partno
);
511 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
513 fprintf (stderr
, " using command %s\n", buffer
);
516 if (xpid
< 0 || (xtty
&& xpid
)) {
519 pidcheck(pidwait (xpid
, NOTOK
));
526 if (ct
->c_type
== CT_MULTIPART
)
527 list_content (ct
, -1, 1, 0, 0);
529 list_switch (ct
, -1, 1, 0, 0);
531 if (xpause
&& isatty (fileno (stdout
))) {
533 SIGNAL_HANDLER istat
;
535 if (SOprintf ("Press <return> to show content..."))
536 printf ("Press <return> to show content...");
538 istat
= SIGNAL (SIGINT
, intrser
);
539 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
542 read (fileno (stdout
), prompt
, sizeof(prompt
));
544 SIGNAL (SIGINT
, istat
);
545 if (intr
!= OK
|| prompt
[0] == 'n') {
546 (*ct
->c_ceclosefnx
) (ct
);
547 return (alternate
? DONE
: NOTOK
);
549 if (prompt
[0] == 'q') done(OK
);
553 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
562 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
566 advise ("fork", "unable to");
567 (*ct
->c_ceclosefnx
) (ct
);
576 execvp ("/bin/sh", vec
);
577 fprintf (stderr
, "unable to exec ");
584 ct
->c_pid
= child_id
;
588 pidcheck (pidXwait (child_id
, NULL
));
592 (*ct
->c_ceclosefnx
) (ct
);
593 return (alternate
? DONE
: OK
);
599 * show content of type "text"
603 show_text (CT ct
, int serial
, int alternate
)
605 char *cp
, buffer
[BUFSIZ
];
606 CI ci
= &ct
->c_ctinfo
;
608 /* Check for mhn-show-type/subtype */
609 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
610 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
611 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
612 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
614 /* Check for mhn-show-type */
615 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
616 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
617 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
620 * Use default method if content is text/plain, or if
621 * if it is not a text part of a multipart/alternative
623 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
624 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
625 moreproc
&& *moreproc
? moreproc
: "more");
626 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
627 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
635 * show message body of type "multipart"
639 show_multi (CT ct
, int serial
, int alternate
)
641 char *cp
, buffer
[BUFSIZ
];
642 CI ci
= &ct
->c_ctinfo
;
644 /* Check for mhn-show-type/subtype */
645 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
646 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
647 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
648 return show_multi_aux (ct
, serial
, alternate
, cp
);
650 /* Check for mhn-show-type */
651 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
652 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
653 return show_multi_aux (ct
, serial
, alternate
, cp
);
655 if ((cp
= ct
->c_showproc
))
656 return show_multi_aux (ct
, serial
, alternate
, cp
);
659 * Use default method to display this multipart content. Even
660 * unknown types are displayable, since they're treated as mixed
663 return show_multi_internal (ct
, serial
, alternate
);
668 * show message body of subtypes of multipart that
669 * we understand directly (mixed, alternate, etc...)
673 show_multi_internal (CT ct
, int serial
, int alternate
)
675 int alternating
, nowalternate
, nowserial
, result
;
676 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
682 nowalternate
= alternate
;
684 if (ct
->c_subtype
== MULTI_PARALLEL
) {
685 nowserial
= serialsw
;
686 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
694 * unknown subtypes of multipart (treat as mixed per rfc2046)
699 /* block a few signals */
702 sigaddset (&set
, SIGHUP
);
703 sigaddset (&set
, SIGINT
);
704 sigaddset (&set
, SIGQUIT
);
705 sigaddset (&set
, SIGTERM
);
706 sigprocmask (SIG_BLOCK
, &set
, &oset
);
710 * alternate -> we are a part inside an multipart/alternative
711 * alternating -> we are a multipart/alternative
714 result
= alternate
? NOTOK
: OK
;
716 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
719 if (part_ok (p
, 1) && type_ok (p
, 1)) {
722 inneresult
= show_switch (p
, nowserial
, nowalternate
);
723 switch (inneresult
) {
725 if (alternate
&& !alternating
) {
738 alternate
= nowalternate
= 0;
748 if (alternating
&& !part
) {
750 content_error (NULL
, ct
, "don't know how to display any of the contents");
755 if (serial
&& !nowserial
) {
761 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
765 if (kill (p
->c_pid
, 0) == NOTOK
)
772 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
775 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
780 if (p
->c_pid
== pid
) {
791 /* reset the signal mask */
792 sigprocmask (SIG_SETMASK
, &oset
, &set
);
800 * Parse display string for multipart content
801 * and use external program to display it.
805 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
807 int len
, buflen
, quoted
;
808 int xlist
, xpause
, xtty
;
809 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
810 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
812 CI ci
= &ct
->c_ctinfo
;
815 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
818 if (!p
->c_ceopenfnx
) {
820 content_error (NULL
, p
, "don't know how to decode content");
824 if (p
->c_storage
== NULL
) {
826 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
829 /* I'm not sure if this is necessary? */
830 p
->c_storage
= add (file
, NULL
);
832 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
833 return (alternate
? DONE
: OK
);
834 (*p
->c_ceclosefnx
) (p
);
842 /* get buffer ready to go */
844 buflen
= sizeof(buffer
) - 1;
845 bp
[0] = bp
[buflen
] = '\0';
848 /* Now parse display string */
849 for ( ; *cp
&& buflen
> 0; cp
++) {
854 /* insert parameters from Content-Type field */
859 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
860 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
870 /* insert content description */
874 s
= trimcpy (ct
->c_descr
);
875 strncpy (bp
, s
, buflen
);
881 /* exclusive execution */
891 /* insert filename(s) containing content */
895 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
898 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
904 /* set our starting pointer back to bp, to avoid
905 * requoting the filenames we just added
912 /* %l, and pause prior to displaying content */
917 /* display listing prior to displaying content */
922 /* insert subtype of content */
923 strncpy (bp
, ci
->ci_subtype
, buflen
);
927 /* insert character % */
940 /* Did we actually insert something? */
942 /* Insert single quote if not inside quotes already */
943 if (!quoted
&& buflen
) {
945 memmove (pp
+ 1, pp
, len
);
950 /* Escape existing quotes */
951 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
953 memmove (pp
+ 3, pp
, len
);
960 /* If pp is still set, that means we ran out of space. */
963 if (!quoted
&& buflen
) {
981 (ct
->c_termproc
&& buflen
<= (ssize_t
) strlen(ct
->c_termproc
))) {
982 /* content_error would provide a more useful error message
983 * here, except that if we got overrun, it probably would
986 fprintf(stderr
, "Buffer overflow constructing show command!\n");
990 /* use charset string to modify display method */
991 if (ct
->c_termproc
) {
994 strncpy (term
, buffer
, sizeof(term
));
995 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
998 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
999 NOTOK
, xlist
, xpause
, 0, xtty
);
1004 * show content of type "message/rfc822"
1008 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1010 char *cp
, buffer
[BUFSIZ
];
1011 CI ci
= &ct
->c_ctinfo
;
1013 /* Check for mhn-show-type/subtype */
1014 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1015 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1016 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1017 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1019 /* Check for mhn-show-type */
1020 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1021 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1022 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1024 if ((cp
= ct
->c_showproc
))
1025 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1027 /* default method for message/rfc822 */
1028 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1029 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1030 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1033 /* complain if we are not a part of a multipart/alternative */
1035 content_error (NULL
, ct
, "don't know how to display content");
1042 * Show content of type "message/partial".
1046 show_partial (CT ct
, int serial
, int alternate
)
1048 NMH_UNUSED (serial
);
1049 NMH_UNUSED (alternate
);
1051 content_error (NULL
, ct
,
1052 "in order to display this message, you must reassemble it");
1058 * Show content of type "message/external".
1060 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1064 show_external (CT ct
, int serial
, int alternate
)
1066 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1067 CT p
= e
->eb_content
;
1069 if (!type_ok (p
, 0))
1072 return show_switch (p
, serial
, alternate
);
1082 siglongjmp (intrenv
, DONE
);