]>
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>
20 #include <h/mhparse.h>
32 /* flags for moreproc/header display */
38 static sigjmp_buf intrenv
;
42 int SOprintf (char *, ...);
48 int part_ok (CT
, int);
49 int type_ok (CT
, int);
50 void content_error (char *, CT
, char *, ...);
51 void flush_errors (void);
54 int list_switch (CT
, int, int, int, int);
55 int list_content (CT
, int, int, int, int);
60 void show_all_messages (CT
*);
61 int show_content_aux (CT
, int, int, char *, char *);
66 static void show_single_message (CT
, char *);
67 static void DisplayMsgHeader (CT
, char *);
68 static int show_switch (CT
, int, int);
69 static int show_content (CT
, int, int);
70 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
71 static int show_text (CT
, int, int);
72 static int show_multi (CT
, int, int);
73 static int show_multi_internal (CT
, int, int);
74 static int show_multi_aux (CT
, int, int, char *);
75 static int show_message_rfc822 (CT
, int, int);
76 static int show_partial (CT
, int, int);
77 static int show_external (CT
, int, int);
78 static RETSIGTYPE
intrser (int);
82 * Top level entry point to show/display a group of messages
86 show_all_messages (CT
*cts
)
91 * If form is not specified, then get default form
92 * for showing headers of MIME messages.
95 formsw
= getcpy (etcpath ("mhl.headers"));
98 * If form is "mhl.null", suppress display of header.
100 if (!strcmp (formsw
, "mhl.null"))
103 for (ctp
= cts
; *ctp
; ctp
++) {
106 /* if top-level type is ok, then display message */
108 show_single_message (ct
, formsw
);
114 * Entry point to show/display a single message
118 show_single_message (CT ct
, char *form
)
124 /* Allow user executable bit so that temporary directories created by
125 * the viewer (e.g., lynx) are going to be accessible */
126 umask (ct
->c_umask
& ~(0100));
129 * If you have a format file, then display
130 * the message headers.
133 DisplayMsgHeader(ct
, form
);
137 /* Show the body of the message */
138 show_switch (ct
, 1, 0);
144 if (ct
->c_ceclosefnx
)
145 (*ct
->c_ceclosefnx
) (ct
);
147 /* block a few signals */
149 sigaddset (&set
, SIGHUP
);
150 sigaddset (&set
, SIGINT
);
151 sigaddset (&set
, SIGQUIT
);
152 sigaddset (&set
, SIGTERM
);
153 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
155 while (wait (&status
) != NOTOK
) {
160 /* reset the signal mask */
161 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
169 * Use the mhlproc to show the header fields
173 DisplayMsgHeader (CT ct
, char *form
)
180 vec
[vecp
++] = r1bindex (mhlproc
, '/');
181 vec
[vecp
++] = "-form";
183 vec
[vecp
++] = "-nobody";
184 vec
[vecp
++] = ct
->c_file
;
187 * If we've specified -(no)moreproc,
188 * then just pass that along.
191 vec
[vecp
++] = "-nomoreproc";
193 vec
[vecp
++] = "-moreproc";
194 vec
[vecp
++] = progsw
;
200 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
205 adios ("fork", "unable to");
209 execvp (mhlproc
, vec
);
210 fprintf (stderr
, "unable to exec ");
223 * Switching routine. Call the correct routine
224 * based on content type.
228 show_switch (CT ct
, int serial
, int alternate
)
230 switch (ct
->c_type
) {
232 return show_multi (ct
, serial
, alternate
);
236 switch (ct
->c_subtype
) {
237 case MESSAGE_PARTIAL
:
238 return show_partial (ct
, serial
, alternate
);
241 case MESSAGE_EXTERNAL
:
242 return show_external (ct
, serial
, alternate
);
247 return show_message_rfc822 (ct
, serial
, alternate
);
253 return show_text (ct
, serial
, alternate
);
260 return show_content (ct
, serial
, alternate
);
264 adios (NULL
, "unknown content type %d", ct
->c_type
);
268 return 0; /* NOT REACHED */
273 * Generic method for displaying content
277 show_content (CT ct
, int serial
, int alternate
)
279 char *cp
, buffer
[BUFSIZ
];
280 CI ci
= &ct
->c_ctinfo
;
282 /* Check for mhn-show-type/subtype */
283 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
284 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
285 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
286 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
288 /* Check for mhn-show-type */
289 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
290 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
291 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
293 if ((cp
= ct
->c_showproc
))
294 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
296 /* complain if we are not a part of a multipart/alternative */
298 content_error (NULL
, ct
, "don't know how to display content");
305 * Parse the display string for displaying generic content
309 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
311 int fd
, len
, buflen
, quoted
;
312 int xstdin
, xlist
, xpause
, xtty
;
313 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
314 CI ci
= &ct
->c_ctinfo
;
316 if (!ct
->c_ceopenfnx
) {
318 content_error (NULL
, ct
, "don't know how to decode content");
324 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
326 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
327 return (alternate
? DONE
: OK
);
335 strncpy (buffer
, cp
, sizeof(buffer
));
339 /* get buffer ready to go */
341 buflen
= sizeof(buffer
) - 1;
342 bp
[0] = bp
[buflen
] = '\0';
345 /* Now parse display string */
346 for ( ; *cp
&& buflen
> 0; cp
++) {
352 /* insert parameters from Content-Type field */
357 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
358 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
368 /* insert content description */
372 s
= trimcpy (ct
->c_descr
);
373 strncpy (bp
, s
, buflen
);
379 /* exclusive execution */
384 /* %e, %f, and stdin is terminal not content */
390 /* insert filename containing content */
391 snprintf (bp
, buflen
, "'%s'", file
);
392 /* since we've quoted the file argument, set things up
393 * to look past it, to avoid problems with the quoting
394 * logic below. (I know, I should figure out what's
395 * broken with the quoting logic, but..)
404 /* %l, and pause prior to displaying content */
409 /* display listing prior to displaying content */
414 /* insert subtype of content */
415 strncpy (bp
, ci
->ci_subtype
, buflen
);
419 /* insert character % */
432 /* Did we actually insert something? */
434 /* Insert single quote if not inside quotes already */
435 if (!quoted
&& buflen
) {
437 memmove (pp
+ 1, pp
, len
);
442 /* Escape existing quotes */
443 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
445 memmove (pp
+ 3, pp
, len
);
452 /* If pp is still set, that means we ran out of space. */
455 if (!quoted
&& buflen
) {
472 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
473 /* content_error would provide a more useful error message
474 * here, except that if we got overrun, it probably would
477 fprintf(stderr
, "Buffer overflow constructing show command!\n");
481 /* use charset string to modify display method */
482 if (ct
->c_termproc
) {
485 strncpy (term
, buffer
, sizeof(term
));
486 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
490 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
491 fd
, xlist
, xpause
, xstdin
, xtty
);
496 * Routine to actually display the content
500 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
501 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
505 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
507 if (debugsw
|| cracked
) {
510 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
513 fprintf (stderr
, " part %s", ct
->c_partno
);
515 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
517 fprintf (stderr
, " using command %s\n", buffer
);
520 if (xpid
< 0 || (xtty
&& xpid
)) {
523 pidcheck(pidwait (xpid
, NOTOK
));
530 if (ct
->c_type
== CT_MULTIPART
)
531 list_content (ct
, -1, 1, 0, 0);
533 list_switch (ct
, -1, 1, 0, 0);
535 if (xpause
&& SOprintf ("Press <return> to show content..."))
536 printf ("Press <return> to show content...");
540 SIGNAL_HANDLER istat
;
542 istat
= SIGNAL (SIGINT
, intrser
);
543 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
546 read (fileno (stdout
), prompt
, sizeof(prompt
));
548 SIGNAL (SIGINT
, istat
);
549 if (intr
!= OK
|| prompt
[0] == 'n') {
550 (*ct
->c_ceclosefnx
) (ct
);
551 return (alternate
? DONE
: NOTOK
);
553 if (prompt
[0] == 'q') done(OK
);
557 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
566 for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
570 advise ("fork", "unable to");
571 (*ct
->c_ceclosefnx
) (ct
);
580 execvp ("/bin/sh", vec
);
581 fprintf (stderr
, "unable to exec ");
588 ct
->c_pid
= child_id
;
592 pidcheck (pidXwait (child_id
, NULL
));
596 (*ct
->c_ceclosefnx
) (ct
);
597 return (alternate
? DONE
: OK
);
603 * show content of type "text"
607 show_text (CT ct
, int serial
, int alternate
)
609 char *cp
, buffer
[BUFSIZ
];
610 CI ci
= &ct
->c_ctinfo
;
612 /* Check for mhn-show-type/subtype */
613 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
614 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
615 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
616 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
618 /* Check for mhn-show-type */
619 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
620 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
621 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
624 * Use default method if content is text/plain, or if
625 * if it is not a text part of a multipart/alternative
627 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
628 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
629 moreproc
&& *moreproc
? moreproc
: "more");
630 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
631 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
639 * show message body of type "multipart"
643 show_multi (CT ct
, int serial
, int alternate
)
645 char *cp
, buffer
[BUFSIZ
];
646 CI ci
= &ct
->c_ctinfo
;
648 /* Check for mhn-show-type/subtype */
649 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
650 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
651 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
652 return show_multi_aux (ct
, serial
, alternate
, cp
);
654 /* Check for mhn-show-type */
655 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
656 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
657 return show_multi_aux (ct
, serial
, alternate
, cp
);
659 if ((cp
= ct
->c_showproc
))
660 return show_multi_aux (ct
, serial
, alternate
, cp
);
663 * Use default method to display this multipart content
664 * if it is not a (nested) part of a multipart/alternative,
665 * or if it is one of the known subtypes of multipart.
667 if (!alternate
|| ct
->c_subtype
!= MULTI_UNKNOWN
)
668 return show_multi_internal (ct
, serial
, alternate
);
675 * show message body of subtypes of multipart that
676 * we understand directly (mixed, alternate, etc...)
680 show_multi_internal (CT ct
, int serial
, int alternate
)
682 int alternating
, nowalternate
, nowserial
, result
;
683 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
689 nowalternate
= alternate
;
691 if (ct
->c_subtype
== MULTI_PARALLEL
) {
692 nowserial
= serialsw
;
693 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
701 * unknown subtypes of multipart (treat as mixed per rfc2046)
706 /* block a few signals */
709 sigaddset (&set
, SIGHUP
);
710 sigaddset (&set
, SIGINT
);
711 sigaddset (&set
, SIGQUIT
);
712 sigaddset (&set
, SIGTERM
);
713 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
717 * alternate -> we are a part inside an multipart/alternative
718 * alternating -> we are a multipart/alternative
721 result
= alternate
? NOTOK
: OK
;
723 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
726 if (part_ok (p
, 1) && type_ok (p
, 1)) {
729 inneresult
= show_switch (p
, nowserial
, nowalternate
);
730 switch (inneresult
) {
732 if (alternate
&& !alternating
) {
745 alternate
= nowalternate
= 0;
755 if (alternating
&& !part
) {
757 content_error (NULL
, ct
, "don't know how to display any of the contents");
762 if (serial
&& !nowserial
) {
768 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
772 if (kill (p
->c_pid
, 0) == NOTOK
)
779 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
782 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
787 if (p
->c_pid
== pid
) {
798 /* reset the signal mask */
799 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
807 * Parse display string for multipart content
808 * and use external program to display it.
812 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
814 int len
, buflen
, quoted
;
815 int xlist
, xpause
, xtty
;
816 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
817 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
819 CI ci
= &ct
->c_ctinfo
;
822 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
825 if (!p
->c_ceopenfnx
) {
827 content_error (NULL
, p
, "don't know how to decode content");
831 if (p
->c_storage
== NULL
) {
833 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
836 /* I'm not sure if this is necessary? */
837 p
->c_storage
= add (file
, NULL
);
839 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
840 return (alternate
? DONE
: OK
);
841 (*p
->c_ceclosefnx
) (p
);
849 /* get buffer ready to go */
851 buflen
= sizeof(buffer
) - 1;
852 bp
[0] = bp
[buflen
] = '\0';
855 /* Now parse display string */
856 for ( ; *cp
&& buflen
> 0; cp
++) {
861 /* insert parameters from Content-Type field */
866 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
867 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
877 /* insert content description */
881 s
= trimcpy (ct
->c_descr
);
882 strncpy (bp
, s
, buflen
);
888 /* exclusive execution */
898 /* insert filename(s) containing content */
902 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
905 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
911 /* set our starting pointer back to bp, to avoid
912 * requoting the filenames we just added
919 /* %l, and pause prior to displaying content */
924 /* display listing prior to displaying content */
929 /* insert subtype of content */
930 strncpy (bp
, ci
->ci_subtype
, buflen
);
934 /* insert character % */
947 /* Did we actually insert something? */
949 /* Insert single quote if not inside quotes already */
950 if (!quoted
&& buflen
) {
952 memmove (pp
+ 1, pp
, len
);
957 /* Escape existing quotes */
958 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
960 memmove (pp
+ 3, pp
, len
);
967 /* If pp is still set, that means we ran out of space. */
970 if (!quoted
&& buflen
) {
987 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
988 /* content_error would provide a more useful error message
989 * here, except that if we got overrun, it probably would
992 fprintf(stderr
, "Buffer overflow constructing show command!\n");
996 /* use charset string to modify display method */
997 if (ct
->c_termproc
) {
1000 strncpy (term
, buffer
, sizeof(term
));
1001 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
1004 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1005 NOTOK
, xlist
, xpause
, 0, xtty
);
1010 * show content of type "message/rfc822"
1014 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1016 char *cp
, buffer
[BUFSIZ
];
1017 CI ci
= &ct
->c_ctinfo
;
1019 /* Check for mhn-show-type/subtype */
1020 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1021 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1022 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1023 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1025 /* Check for mhn-show-type */
1026 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1027 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1028 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1030 if ((cp
= ct
->c_showproc
))
1031 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1033 /* default method for message/rfc822 */
1034 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1035 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1036 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1039 /* complain if we are not a part of a multipart/alternative */
1041 content_error (NULL
, ct
, "don't know how to display content");
1048 * Show content of type "message/partial".
1052 show_partial (CT ct
, int serial
, int alternate
)
1054 content_error (NULL
, ct
,
1055 "in order to display this message, you must reassemble it");
1061 * Show content of type "message/external".
1063 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1067 show_external (CT ct
, int serial
, int alternate
)
1069 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1070 CT p
= e
->eb_content
;
1072 if (!type_ok (p
, 0))
1075 return show_switch (p
, serial
, alternate
);
1078 content_error (NULL
, p
, "don't know how to display content");
1087 #ifndef RELIABLE_SIGNALS
1088 SIGNAL (SIGINT
, intrser
);
1092 siglongjmp (intrenv
, DONE
);