]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
efa9ad3c1e72fced6e977b286323e23de7e4e653
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 SOprintf (char *, ...);
47 int part_ok (CT
, int);
48 int type_ok (CT
, int);
49 void content_error (char *, CT
, char *, ...);
50 void flush_errors (void);
53 int list_switch (CT
, int, int, int, int);
54 int list_content (CT
, int, int, int, int);
59 void show_all_messages (CT
*);
60 int show_content_aux (CT
, int, int, char *, char *);
65 static void show_single_message (CT
, char *);
66 static void DisplayMsgHeader (CT
, char *);
67 static int show_switch (CT
, int, int);
68 static int show_content (CT
, int, int);
69 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
70 static int show_text (CT
, int, int);
71 static int show_multi (CT
, int, int);
72 static int show_multi_internal (CT
, int, int);
73 static int show_multi_aux (CT
, int, int, char *);
74 static int show_message_rfc822 (CT
, int, int);
75 static int show_partial (CT
, int, int);
76 static int show_external (CT
, int, int);
77 static RETSIGTYPE
intrser (int);
81 * Top level entry point to show/display a group of messages
85 show_all_messages (CT
*cts
)
90 * If form is not specified, then get default form
91 * for showing headers of MIME messages.
94 formsw
= getcpy (etcpath ("mhl.headers"));
97 * If form is "mhl.null", suppress display of header.
99 if (!strcmp (formsw
, "mhl.null"))
102 for (ctp
= cts
; *ctp
; ctp
++) {
105 /* if top-level type is ok, then display message */
107 show_single_message (ct
, formsw
);
113 * Entry point to show/display a single message
117 show_single_message (CT ct
, char *form
)
123 /* Allow user executable bit so that temporary directories created by
124 * the viewer (e.g., lynx) are going to be accessible */
125 umask (ct
->c_umask
& ~(0100));
128 * If you have a format file, then display
129 * the message headers.
132 DisplayMsgHeader(ct
, form
);
136 /* Show the body of the message */
137 show_switch (ct
, 1, 0);
143 if (ct
->c_ceclosefnx
)
144 (*ct
->c_ceclosefnx
) (ct
);
146 /* block a few signals */
148 sigaddset (&set
, SIGHUP
);
149 sigaddset (&set
, SIGINT
);
150 sigaddset (&set
, SIGQUIT
);
151 sigaddset (&set
, SIGTERM
);
152 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
154 while (wait (&status
) != NOTOK
) {
159 /* reset the signal mask */
160 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
168 * Use the mhlproc to show the header fields
172 DisplayMsgHeader (CT ct
, char *form
)
179 vec
[vecp
++] = r1bindex (mhlproc
, '/');
180 vec
[vecp
++] = "-form";
182 vec
[vecp
++] = "-nobody";
183 vec
[vecp
++] = ct
->c_file
;
186 * If we've specified -(no)moreproc,
187 * then just pass that along.
190 vec
[vecp
++] = "-nomoreproc";
192 vec
[vecp
++] = "-moreproc";
193 vec
[vecp
++] = progsw
;
199 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
204 adios ("fork", "unable to");
208 execvp (mhlproc
, vec
);
209 fprintf (stderr
, "unable to exec ");
222 * Switching routine. Call the correct routine
223 * based on content type.
227 show_switch (CT ct
, int serial
, int alternate
)
229 switch (ct
->c_type
) {
231 return show_multi (ct
, serial
, alternate
);
235 switch (ct
->c_subtype
) {
236 case MESSAGE_PARTIAL
:
237 return show_partial (ct
, serial
, alternate
);
240 case MESSAGE_EXTERNAL
:
241 return show_external (ct
, serial
, alternate
);
246 return show_message_rfc822 (ct
, serial
, alternate
);
252 return show_text (ct
, serial
, alternate
);
259 return show_content (ct
, serial
, alternate
);
263 adios (NULL
, "unknown content type %d", ct
->c_type
);
267 return 0; /* NOT REACHED */
272 * Generic method for displaying content
276 show_content (CT ct
, int serial
, int alternate
)
278 char *cp
, buffer
[BUFSIZ
];
279 CI ci
= &ct
->c_ctinfo
;
281 /* Check for mhn-show-type/subtype */
282 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
283 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
284 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
285 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
287 /* Check for mhn-show-type */
288 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
289 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
290 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
292 if ((cp
= ct
->c_showproc
))
293 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
295 /* complain if we are not a part of a multipart/alternative */
297 content_error (NULL
, ct
, "don't know how to display content");
304 * Parse the display string for displaying generic content
308 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
310 int fd
, len
, buflen
, quoted
;
311 int xstdin
, xlist
, xpause
, xtty
;
312 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
313 CI ci
= &ct
->c_ctinfo
;
315 if (!ct
->c_ceopenfnx
) {
317 content_error (NULL
, ct
, "don't know how to decode content");
323 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
325 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
326 return (alternate
? DONE
: OK
);
334 strncpy (buffer
, cp
, sizeof(buffer
));
338 /* get buffer ready to go */
340 buflen
= sizeof(buffer
) - 1;
341 bp
[0] = bp
[buflen
] = '\0';
344 /* Now parse display string */
345 for ( ; *cp
&& buflen
> 0; cp
++) {
351 /* insert parameters from Content-Type field */
356 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
357 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
367 /* insert content description */
371 s
= trimcpy (ct
->c_descr
);
372 strncpy (bp
, s
, buflen
);
378 /* exclusive execution */
383 /* %e, %f, and stdin is terminal not content */
389 /* insert filename containing content */
390 snprintf (bp
, buflen
, "'%s'", file
);
391 /* since we've quoted the file argument, set things up
392 * to look past it, to avoid problems with the quoting
393 * logic below. (I know, I should figure out what's
394 * broken with the quoting logic, but..)
403 /* %l, and pause prior to displaying content */
408 /* display listing prior to displaying content */
413 /* insert subtype of content */
414 strncpy (bp
, ci
->ci_subtype
, buflen
);
418 /* insert character % */
431 /* Did we actually insert something? */
433 /* Insert single quote if not inside quotes already */
434 if (!quoted
&& buflen
) {
436 memmove (pp
+ 1, pp
, len
);
441 /* Escape existing quotes */
442 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
444 memmove (pp
+ 3, pp
, len
);
451 /* If pp is still set, that means we ran out of space. */
454 if (!quoted
&& buflen
) {
471 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
472 /* content_error would provide a more useful error message
473 * here, except that if we got overrun, it probably would
476 fprintf(stderr
, "Buffer overflow constructing show command!\n");
480 /* use charset string to modify display method */
481 if (ct
->c_termproc
) {
484 strncpy (term
, buffer
, sizeof(term
));
485 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
489 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
490 fd
, xlist
, xpause
, xstdin
, xtty
);
495 * Routine to actually display the content
499 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
500 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
504 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
506 if (debugsw
|| cracked
) {
509 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
512 fprintf (stderr
, " part %s", ct
->c_partno
);
514 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
516 fprintf (stderr
, " using command %s\n", buffer
);
519 if (xpid
< 0 || (xtty
&& xpid
)) {
522 pidcheck(pidwait (xpid
, NOTOK
));
529 if (ct
->c_type
== CT_MULTIPART
)
530 list_content (ct
, -1, 1, 0, 0);
532 list_switch (ct
, -1, 1, 0, 0);
534 if (xpause
&& SOprintf ("Press <return> to show content..."))
535 printf ("Press <return> to show content...");
539 SIGNAL_HANDLER istat
;
541 istat
= SIGNAL (SIGINT
, intrser
);
542 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
545 read (fileno (stdout
), prompt
, sizeof(prompt
));
547 SIGNAL (SIGINT
, istat
);
548 if (intr
!= OK
|| prompt
[0] == 'n') {
549 (*ct
->c_ceclosefnx
) (ct
);
550 return (alternate
? DONE
: NOTOK
);
552 if (prompt
[0] == 'q') done(OK
);
556 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
565 for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
569 advise ("fork", "unable to");
570 (*ct
->c_ceclosefnx
) (ct
);
579 execvp ("/bin/sh", vec
);
580 fprintf (stderr
, "unable to exec ");
587 ct
->c_pid
= child_id
;
591 pidcheck (pidXwait (child_id
, NULL
));
595 (*ct
->c_ceclosefnx
) (ct
);
596 return (alternate
? DONE
: OK
);
602 * show content of type "text"
606 show_text (CT ct
, int serial
, int alternate
)
608 char *cp
, buffer
[BUFSIZ
];
609 CI ci
= &ct
->c_ctinfo
;
611 /* Check for mhn-show-type/subtype */
612 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
613 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
614 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
615 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
617 /* Check for mhn-show-type */
618 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
619 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
620 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
623 * Use default method if content is text/plain, or if
624 * if it is not a text part of a multipart/alternative
626 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
627 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
628 moreproc
&& *moreproc
? moreproc
: "more");
629 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
630 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
638 * show message body of type "multipart"
642 show_multi (CT ct
, int serial
, int alternate
)
644 char *cp
, buffer
[BUFSIZ
];
645 CI ci
= &ct
->c_ctinfo
;
647 /* Check for mhn-show-type/subtype */
648 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
649 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
650 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
651 return show_multi_aux (ct
, serial
, alternate
, cp
);
653 /* Check for mhn-show-type */
654 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
655 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
656 return show_multi_aux (ct
, serial
, alternate
, cp
);
658 if ((cp
= ct
->c_showproc
))
659 return show_multi_aux (ct
, serial
, alternate
, cp
);
662 * Use default method to display this multipart content
663 * if it is not a (nested) part of a multipart/alternative,
664 * or if it is one of the known subtypes of multipart.
666 if (!alternate
|| ct
->c_subtype
!= MULTI_UNKNOWN
)
667 return show_multi_internal (ct
, serial
, alternate
);
674 * show message body of subtypes of multipart that
675 * we understand directly (mixed, alternate, etc...)
679 show_multi_internal (CT ct
, int serial
, int alternate
)
681 int alternating
, nowalternate
, nowserial
, result
;
682 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
688 nowalternate
= alternate
;
690 if (ct
->c_subtype
== MULTI_PARALLEL
) {
691 nowserial
= serialsw
;
692 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
700 * unknown subtypes of multipart (treat as mixed per rfc2046)
705 /* block a few signals */
708 sigaddset (&set
, SIGHUP
);
709 sigaddset (&set
, SIGINT
);
710 sigaddset (&set
, SIGQUIT
);
711 sigaddset (&set
, SIGTERM
);
712 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
716 * alternate -> we are a part inside an multipart/alternative
717 * alternating -> we are a multipart/alternative
720 result
= alternate
? NOTOK
: OK
;
722 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
725 if (part_ok (p
, 1) && type_ok (p
, 1)) {
728 inneresult
= show_switch (p
, nowserial
, nowalternate
);
729 switch (inneresult
) {
731 if (alternate
&& !alternating
) {
744 alternate
= nowalternate
= 0;
754 if (alternating
&& !part
) {
756 content_error (NULL
, ct
, "don't know how to display any of the contents");
761 if (serial
&& !nowserial
) {
767 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
771 if (kill (p
->c_pid
, 0) == NOTOK
)
778 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
781 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
786 if (p
->c_pid
== pid
) {
797 /* reset the signal mask */
798 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
806 * Parse display string for multipart content
807 * and use external program to display it.
811 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
813 int len
, buflen
, quoted
;
814 int xlist
, xpause
, xtty
;
815 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
816 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
818 CI ci
= &ct
->c_ctinfo
;
821 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
824 if (!p
->c_ceopenfnx
) {
826 content_error (NULL
, p
, "don't know how to decode content");
830 if (p
->c_storage
== NULL
) {
832 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
835 /* I'm not sure if this is necessary? */
836 p
->c_storage
= add (file
, NULL
);
838 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
839 return (alternate
? DONE
: OK
);
840 (*p
->c_ceclosefnx
) (p
);
848 /* get buffer ready to go */
850 buflen
= sizeof(buffer
) - 1;
851 bp
[0] = bp
[buflen
] = '\0';
854 /* Now parse display string */
855 for ( ; *cp
&& buflen
> 0; cp
++) {
860 /* insert parameters from Content-Type field */
865 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
866 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
876 /* insert content description */
880 s
= trimcpy (ct
->c_descr
);
881 strncpy (bp
, s
, buflen
);
887 /* exclusive execution */
897 /* insert filename(s) containing content */
901 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
904 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
910 /* set our starting pointer back to bp, to avoid
911 * requoting the filenames we just added
918 /* %l, and pause prior to displaying content */
923 /* display listing prior to displaying content */
928 /* insert subtype of content */
929 strncpy (bp
, ci
->ci_subtype
, buflen
);
933 /* insert character % */
946 /* Did we actually insert something? */
948 /* Insert single quote if not inside quotes already */
949 if (!quoted
&& buflen
) {
951 memmove (pp
+ 1, pp
, len
);
956 /* Escape existing quotes */
957 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
959 memmove (pp
+ 3, pp
, len
);
966 /* If pp is still set, that means we ran out of space. */
969 if (!quoted
&& buflen
) {
986 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
987 /* content_error would provide a more useful error message
988 * here, except that if we got overrun, it probably would
991 fprintf(stderr
, "Buffer overflow constructing show command!\n");
995 /* use charset string to modify display method */
996 if (ct
->c_termproc
) {
999 strncpy (term
, buffer
, sizeof(term
));
1000 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
1003 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1004 NOTOK
, xlist
, xpause
, 0, xtty
);
1009 * show content of type "message/rfc822"
1013 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1015 char *cp
, buffer
[BUFSIZ
];
1016 CI ci
= &ct
->c_ctinfo
;
1018 /* Check for mhn-show-type/subtype */
1019 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1020 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1021 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1022 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1024 /* Check for mhn-show-type */
1025 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1026 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1027 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1029 if ((cp
= ct
->c_showproc
))
1030 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1032 /* default method for message/rfc822 */
1033 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1034 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1035 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1038 /* complain if we are not a part of a multipart/alternative */
1040 content_error (NULL
, ct
, "don't know how to display content");
1047 * Show content of type "message/partial".
1051 show_partial (CT ct
, int serial
, int alternate
)
1053 content_error (NULL
, ct
,
1054 "in order to display this message, you must reassemble it");
1060 * Show content of type "message/external".
1062 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1066 show_external (CT ct
, int serial
, int alternate
)
1068 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1069 CT p
= e
->eb_content
;
1071 if (!type_ok (p
, 0))
1074 return show_switch (p
, serial
, alternate
);
1077 content_error (NULL
, p
, "don't know how to display content");
1086 #ifndef RELIABLE_SIGNALS
1087 SIGNAL (SIGINT
, intrser
);
1091 siglongjmp (intrenv
, DONE
);