]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3 * mhshowsbr.c -- routines to display the contents of MIME messages
10 #include <h/signals.h>
15 #include <zotnet/mts/mts.h>
16 #include <zotnet/tws/tws.h>
18 #include <h/mhparse.h>
20 #ifdef HAVE_SYS_WAIT_H
21 # include <sys/wait.h>
25 * Just use sigjmp/longjmp on older machines that
26 * don't have sigsetjmp/siglongjmp.
28 #ifndef HAVE_SIGSETJMP
29 # define sigjmp_buf jmp_buf
30 # define sigsetjmp(env,mask) setjmp(env)
31 # define siglongjmp(env,val) longjmp(env,val)
43 /* flags for moreproc/header display */
49 static sigjmp_buf intrenv
;
53 int SOprintf (char *, ...);
59 int part_ok (CT
, int);
60 int type_ok (CT
, int);
61 void content_error (char *, CT
, char *, ...);
62 void flush_errors (void);
65 int list_switch (CT
, int, int, int, int);
66 int list_content (CT
, int, int, int, int);
71 void show_all_messages (CT
*);
72 int show_content_aux (CT
, int, int, char *, char *);
77 static void show_single_message (CT
, char *);
78 static void DisplayMsgHeader (CT
, char *);
79 static int show_switch (CT
, int, int);
80 static int show_content (CT
, int, int);
81 static int show_content_aux2 (CT
, int, int, char *, char *, int, int, int, int, int);
82 static int show_text (CT
, int, int);
83 static int show_multi (CT
, int, int);
84 static int show_multi_internal (CT
, int, int);
85 static int show_multi_aux (CT
, int, int, char *);
86 static int show_message_rfc822 (CT
, int, int);
87 static int show_partial (CT
, int, int);
88 static int show_external (CT
, int, int);
89 static RETSIGTYPE
intrser (int);
93 * Top level entry point to show/display a group of messages
97 show_all_messages (CT
*cts
)
102 * If form is not specified, then get default form
103 * for showing headers of MIME messages.
106 formsw
= getcpy (etcpath ("mhl.headers"));
109 * If form is "mhl.null", suppress display of header.
111 if (!strcmp (formsw
, "mhl.null"))
114 for (ctp
= cts
; *ctp
; ctp
++) {
117 /* if top-level type is ok, then display message */
119 show_single_message (ct
, formsw
);
125 * Entry point to show/display a single message
129 show_single_message (CT ct
, char *form
)
142 * If you have a format file, then display
143 * the message headers.
146 DisplayMsgHeader(ct
, form
);
150 /* Show the body of the message */
151 show_switch (ct
, 1, 0);
157 if (ct
->c_ceclosefnx
)
158 (*ct
->c_ceclosefnx
) (ct
);
160 /* block a few signals */
162 sigaddset (&set
, SIGHUP
);
163 sigaddset (&set
, SIGINT
);
164 sigaddset (&set
, SIGQUIT
);
165 sigaddset (&set
, SIGTERM
);
166 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
168 while (wait (&status
) != NOTOK
) {
172 pidcheck (status
.w_status
);
177 /* reset the signal mask */
178 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
186 * Use the mhlproc to show the header fields
190 DisplayMsgHeader (CT ct
, char *form
)
197 vec
[vecp
++] = r1bindex (mhlproc
, '/');
198 vec
[vecp
++] = "-form";
200 vec
[vecp
++] = "-nobody";
201 vec
[vecp
++] = ct
->c_file
;
204 * If we've specified -(no)moreproc,
205 * then just pass that along.
208 vec
[vecp
++] = "-nomoreproc";
210 vec
[vecp
++] = "-moreproc";
211 vec
[vecp
++] = progsw
;
217 for (i
= 0; (child_id
= vfork()) == NOTOK
&& i
< 5; i
++)
222 adios ("fork", "unable to");
226 execvp (mhlproc
, vec
);
227 fprintf (stderr
, "unable to exec ");
240 * Switching routine. Call the correct routine
241 * based on content type.
245 show_switch (CT ct
, int serial
, int alternate
)
247 switch (ct
->c_type
) {
249 return show_multi (ct
, serial
, alternate
);
253 switch (ct
->c_subtype
) {
254 case MESSAGE_PARTIAL
:
255 return show_partial (ct
, serial
, alternate
);
258 case MESSAGE_EXTERNAL
:
259 return show_external (ct
, serial
, alternate
);
264 return show_message_rfc822 (ct
, serial
, alternate
);
270 return show_text (ct
, serial
, alternate
);
277 return show_content (ct
, serial
, alternate
);
281 adios (NULL
, "unknown content type %d", ct
->c_type
);
285 return 0; /* NOT REACHED */
290 * Generic method for displaying content
294 show_content (CT ct
, int serial
, int alternate
)
296 char *cp
, buffer
[BUFSIZ
];
297 CI ci
= &ct
->c_ctinfo
;
299 /* Check for mhn-show-type/subtype */
300 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
301 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
302 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
303 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
305 /* Check for mhn-show-type */
306 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
307 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
308 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
310 if ((cp
= ct
->c_showproc
))
311 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
313 /* complain if we are not a part of a multipart/alternative */
315 content_error (NULL
, ct
, "don't know how to display content");
322 * Parse the display string for displaying generic content
326 show_content_aux (CT ct
, int serial
, int alternate
, char *cp
, char *cracked
)
328 int fd
, len
, buflen
, quoted
;
329 int xstdin
, xlist
, xpause
, xtty
;
330 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
331 CI ci
= &ct
->c_ctinfo
;
333 if (!ct
->c_ceopenfnx
) {
335 content_error (NULL
, ct
, "don't know how to decode content");
341 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
343 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
344 return (alternate
? DONE
: OK
);
352 strncpy (buffer
, cp
, sizeof(buffer
));
356 /* get buffer ready to go */
358 buflen
= sizeof(buffer
) - 1;
359 bp
[0] = bp
[buflen
] = '\0';
362 /* Now parse display string */
363 for ( ; *cp
&& buflen
> 0; cp
++) {
369 /* insert parameters from Content-Type field */
374 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
375 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
385 /* insert content description */
389 s
= trimcpy (ct
->c_descr
);
390 strncpy (bp
, s
, buflen
);
396 /* exclusive execution */
401 /* %e, %f, and stdin is terminal not content */
407 /* insert filename containing content */
408 snprintf (bp
, buflen
, "'%s'", file
);
409 /* since we've quoted the file argument, set things up
410 * to look past it, to avoid problems with the quoting
411 * logic below. (I know, I should figure out what's
412 * broken with the quoting logic, but..)
421 /* %l, and pause prior to displaying content */
426 /* display listing prior to displaying content */
431 /* insert subtype of content */
432 strncpy (bp
, ci
->ci_subtype
, buflen
);
436 /* insert character % */
449 /* Did we actually insert something? */
451 /* Insert single quote if not inside quotes already */
452 if (!quoted
&& buflen
) {
454 memmove (pp
+ 1, pp
, len
);
459 /* Escape existing quotes */
460 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
462 memmove (pp
+ 3, pp
, len
);
469 /* If pp is still set, that means we ran out of space. */
472 if (!quoted
&& buflen
) {
489 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
490 /* content_error would provide a more useful error message
491 * here, except that if we got overrun, it probably would
494 fprintf(stderr
, "Buffer overflow constructing show command!\n");
498 /* use charset string to modify display method */
499 if (ct
->c_termproc
) {
502 strncpy (term
, buffer
, sizeof(term
));
503 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
507 return show_content_aux2 (ct
, serial
, alternate
, cracked
, buffer
,
508 fd
, xlist
, xpause
, xstdin
, xtty
);
513 * Routine to actually display the content
517 show_content_aux2 (CT ct
, int serial
, int alternate
, char *cracked
, char *buffer
,
518 int fd
, int xlist
, int xpause
, int xstdin
, int xtty
)
522 char *vec
[4], exec
[BUFSIZ
+ sizeof "exec "];
524 if (debugsw
|| cracked
) {
527 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
530 fprintf (stderr
, " part %s", ct
->c_partno
);
532 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
534 fprintf (stderr
, " using command %s\n", buffer
);
537 if (xpid
< 0 || (xtty
&& xpid
)) {
540 pidcheck(pidwait (xpid
, NOTOK
));
547 if (ct
->c_type
== CT_MULTIPART
)
548 list_content (ct
, -1, 1, 0, 0);
550 list_switch (ct
, -1, 1, 0, 0);
552 if (xpause
&& SOprintf ("Press <return> to show content..."))
553 printf ("Press <return> to show content...");
557 SIGNAL_HANDLER istat
;
559 istat
= SIGNAL (SIGINT
, intrser
);
560 if ((intr
= sigsetjmp (intrenv
, 1)) == OK
) {
563 read (fileno (stdout
), prompt
, sizeof(prompt
));
565 SIGNAL (SIGINT
, istat
);
566 if (intr
!= OK
|| prompt
[0] == 'n') {
567 (*ct
->c_ceclosefnx
) (ct
);
568 return (alternate
? DONE
: NOTOK
);
570 if (prompt
[0] == 'q') done(OK
);
574 snprintf (exec
, sizeof(exec
), "exec %s", buffer
);
583 for (i
= 0; (child_id
= vfork ()) == NOTOK
&& i
< 5; i
++)
587 advise ("fork", "unable to");
588 (*ct
->c_ceclosefnx
) (ct
);
597 execvp ("/bin/sh", vec
);
598 fprintf (stderr
, "unable to exec ");
605 ct
->c_pid
= child_id
;
609 pidcheck (pidXwait (child_id
, NULL
));
613 (*ct
->c_ceclosefnx
) (ct
);
614 return (alternate
? DONE
: OK
);
620 * show content of type "text"
624 show_text (CT ct
, int serial
, int alternate
)
626 char *cp
, buffer
[BUFSIZ
];
627 CI ci
= &ct
->c_ctinfo
;
629 /* Check for mhn-show-type/subtype */
630 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
631 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
632 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
633 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
635 /* Check for mhn-show-type */
636 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
637 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
638 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
641 * Use default method if content is text/plain, or if
642 * if it is not a text part of a multipart/alternative
644 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
645 snprintf (buffer
, sizeof(buffer
), "%%p%s '%%F'", progsw
? progsw
:
646 moreproc
&& *moreproc
? moreproc
: "more");
647 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
648 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
656 * show message body of type "multipart"
660 show_multi (CT ct
, int serial
, int alternate
)
662 char *cp
, buffer
[BUFSIZ
];
663 CI ci
= &ct
->c_ctinfo
;
665 /* Check for mhn-show-type/subtype */
666 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
667 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
668 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
669 return show_multi_aux (ct
, serial
, alternate
, cp
);
671 /* Check for mhn-show-type */
672 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
673 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
674 return show_multi_aux (ct
, serial
, alternate
, cp
);
676 if ((cp
= ct
->c_showproc
))
677 return show_multi_aux (ct
, serial
, alternate
, cp
);
680 * Use default method to display this multipart content
681 * if it is not a (nested) part of a multipart/alternative,
682 * or if it is one of the known subtypes of multipart.
684 if (!alternate
|| ct
->c_subtype
!= MULTI_UNKNOWN
)
685 return show_multi_internal (ct
, serial
, alternate
);
692 * show message body of subtypes of multipart that
693 * we understand directly (mixed, alternate, etc...)
697 show_multi_internal (CT ct
, int serial
, int alternate
)
699 int alternating
, nowalternate
, nowserial
, result
;
700 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
706 nowalternate
= alternate
;
708 if (ct
->c_subtype
== MULTI_PARALLEL
) {
709 nowserial
= serialsw
;
710 } else if (ct
->c_subtype
== MULTI_ALTERNATE
) {
718 * unknown subtypes of multipart (treat as mixed per rfc2046)
723 /* block a few signals */
726 sigaddset (&set
, SIGHUP
);
727 sigaddset (&set
, SIGINT
);
728 sigaddset (&set
, SIGQUIT
);
729 sigaddset (&set
, SIGTERM
);
730 SIGPROCMASK (SIG_BLOCK
, &set
, &oset
);
734 * alternate -> we are a part inside an multipart/alternative
735 * alternating -> we are a multipart/alternative
738 result
= alternate
? NOTOK
: OK
;
740 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
743 if (part_ok (p
, 0) && type_ok (p
, 0)) {
746 inneresult
= show_switch (p
, nowserial
, nowalternate
);
747 switch (inneresult
) {
749 if (alternate
&& !alternating
) {
762 alternate
= nowalternate
= 0;
772 if (alternating
&& !part
) {
774 content_error (NULL
, ct
, "don't know how to display any of the contents");
779 if (serial
&& !nowserial
) {
789 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
793 if (kill (p
->c_pid
, 0) == NOTOK
)
800 while (kids
> 0 && (pid
= wait (&status
)) != NOTOK
) {
804 pidcheck (status
.w_status
);
807 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
812 if (p
->c_pid
== pid
) {
823 /* reset the signal mask */
824 SIGPROCMASK (SIG_SETMASK
, &oset
, &set
);
832 * Parse display string for multipart content
833 * and use external program to display it.
837 show_multi_aux (CT ct
, int serial
, int alternate
, char *cp
)
839 int len
, buflen
, quoted
;
840 int xlist
, xpause
, xtty
;
841 char *bp
, *pp
, *file
, buffer
[BUFSIZ
];
842 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
844 CI ci
= &ct
->c_ctinfo
;
847 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
850 if (!p
->c_ceopenfnx
) {
852 content_error (NULL
, p
, "don't know how to decode content");
856 if (p
->c_storage
== NULL
) {
858 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
861 /* I'm not sure if this is necessary? */
862 p
->c_storage
= add (file
, NULL
);
864 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
865 return (alternate
? DONE
: OK
);
866 (*p
->c_ceclosefnx
) (p
);
874 /* get buffer ready to go */
876 buflen
= sizeof(buffer
) - 1;
877 bp
[0] = bp
[buflen
] = '\0';
880 /* Now parse display string */
881 for ( ; *cp
&& buflen
> 0; cp
++) {
886 /* insert parameters from Content-Type field */
891 for (ap
= ci
->ci_attrs
, ep
= ci
->ci_values
; *ap
; ap
++, ep
++) {
892 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, *ap
, *ep
);
902 /* insert content description */
906 s
= trimcpy (ct
->c_descr
);
907 strncpy (bp
, s
, buflen
);
913 /* exclusive execution */
923 /* insert filename(s) containing content */
927 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
930 snprintf (bp
, buflen
, "%s'%s'", s
, p
->c_storage
);
936 /* set our starting pointer back to bp, to avoid
937 * requoting the filenames we just added
944 /* %l, and pause prior to displaying content */
949 /* display listing prior to displaying content */
954 /* insert subtype of content */
955 strncpy (bp
, ci
->ci_subtype
, buflen
);
959 /* insert character % */
972 /* Did we actually insert something? */
974 /* Insert single quote if not inside quotes already */
975 if (!quoted
&& buflen
) {
977 memmove (pp
+ 1, pp
, len
);
982 /* Escape existing quotes */
983 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
985 memmove (pp
+ 3, pp
, len
);
992 /* If pp is still set, that means we ran out of space. */
995 if (!quoted
&& buflen
) {
1012 if (buflen
<= 0 || (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1013 /* content_error would provide a more useful error message
1014 * here, except that if we got overrun, it probably would
1017 fprintf(stderr
, "Buffer overflow constructing show command!\n");
1021 /* use charset string to modify display method */
1022 if (ct
->c_termproc
) {
1025 strncpy (term
, buffer
, sizeof(term
));
1026 snprintf (buffer
, sizeof(buffer
), ct
->c_termproc
, term
);
1029 return show_content_aux2 (ct
, serial
, alternate
, NULL
, buffer
,
1030 NOTOK
, xlist
, xpause
, 0, xtty
);
1035 * show content of type "message/rfc822"
1039 show_message_rfc822 (CT ct
, int serial
, int alternate
)
1041 char *cp
, buffer
[BUFSIZ
];
1042 CI ci
= &ct
->c_ctinfo
;
1044 /* Check for mhn-show-type/subtype */
1045 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
1046 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
1047 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1048 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1050 /* Check for mhn-show-type */
1051 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
1052 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
1053 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1055 if ((cp
= ct
->c_showproc
))
1056 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1058 /* default method for message/rfc822 */
1059 if (ct
->c_subtype
== MESSAGE_RFC822
) {
1060 cp
= (ct
->c_showproc
= add ("%pshow -file '%F'", NULL
));
1061 return show_content_aux (ct
, serial
, alternate
, cp
, NULL
);
1064 /* complain if we are not a part of a multipart/alternative */
1066 content_error (NULL
, ct
, "don't know how to display content");
1073 * Show content of type "message/partial".
1077 show_partial (CT ct
, int serial
, int alternate
)
1079 content_error (NULL
, ct
,
1080 "in order to display this message, you must reassemble it");
1086 * Show content of type "message/external".
1088 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
1092 show_external (CT ct
, int serial
, int alternate
)
1094 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1095 CT p
= e
->eb_content
;
1097 if (!type_ok (p
, 0))
1100 return show_switch (p
, serial
, alternate
);
1103 content_error (NULL
, p
, "don't know how to display content");
1112 #ifndef RELIABLE_SIGNALS
1113 SIGNAL (SIGINT
, intrser
);
1117 siglongjmp (intrenv
, DONE
);