]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
1 /* mhshowsbr.c -- routines to display the contents of MIME messages
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
9 #include "sbr/concat.h"
10 #include "sbr/trimcpy.h"
11 #include "sbr/check_charset.h"
12 #include "sbr/getcpy.h"
13 #include "sbr/context_find.h"
14 #include "sbr/pidstatus.h"
15 #include "sbr/arglist.h"
16 #include "sbr/error.h"
18 #include "h/signals.h"
23 #include "h/mhparse.h"
24 #include "h/fmt_scan.h"
28 #include "mhshowsbr.h"
29 #include "sbr/m_mktemp.h"
32 #endif /* ! HAVE_ICONV */
40 /* flags for moreproc/header display */
44 /* for output markers and headers */
54 static void show_single_message (CT
, char *, int, int, int, struct format
*);
55 static void DisplayMsgHeader (CT
, char *, int);
56 static int show_switch (CT
, int, int, int, int, struct format
*);
57 static int show_content (CT
, int, int, int, struct format
*);
58 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
59 static int show_text (CT
, int, int, struct format
*);
60 static int show_multi (CT
, int, int, int, int, struct format
*);
61 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
62 static int show_multi_aux (CT
, int, char *, struct format
*);
63 static int show_message_rfc822 (CT
, int, struct format
*);
64 static int show_partial (CT
, int);
65 static int show_external (CT
, int, int, int, int, struct format
*);
66 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
67 size_t, int multipart
);
68 static int convert_content_charset (CT
, char **);
69 static struct format
*compile_header(char *);
70 static struct format
*compile_marker(char *);
71 static void output_header (CT
, struct format
*);
72 static void output_marker (CT
, struct format
*, int);
73 static void free_markercomps (void);
74 static int pidcheck(int);
77 * Components (and list of parameters/components) we care about for the
78 * content marker display.
81 static struct comp
*folder_comp
= NULL
;
82 static struct comp
*part_comp
= NULL
;
83 static struct comp
*ctype_comp
= NULL
;
84 static struct comp
*description_comp
= NULL
;
85 static struct comp
*dispo_comp
= NULL
;
87 struct param_comp_list
{
90 struct param_comp_list
*next
;
93 static struct param_comp_list
*ctype_pc_list
= NULL
;
94 static struct param_comp_list
*dispo_pc_list
= NULL
;
98 * Top level entry point to show/display a group of messages
102 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
105 struct format
*hfmt
, *mfmt
;
108 * If form is not specified, then get default form
109 * for showing headers of MIME messages.
112 formsw
= mh_xstrdup(etcpath("mhl.headers"));
115 * Compile the content marker and header format lines
117 mfmt
= compile_marker(markerform
);
118 hfmt
= compile_header(headerform
);
121 * If form is "mhl.null", suppress display of header.
123 if (!strcmp (formsw
, "mhl.null"))
126 for (ctp
= cts
; *ctp
; ctp
++) {
129 /* if top-level type is ok, then display message */
130 if (type_ok (ct
, 1)) {
131 if (headersw
) output_header(ct
, hfmt
);
133 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
145 * Entry point to show/display a single message
149 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
150 int inlineonly
, struct format
*fmt
)
156 /* Allow user executable bit so that temporary directories created by
157 * the viewer (e.g., lynx) are going to be accessible */
158 umask (ct
->c_umask
& ~(0100));
161 * If you have a format file, then display
162 * the message headers.
165 DisplayMsgHeader(ct
, form
, concatsw
);
167 /* Show the body of the message */
168 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
174 if (ct
->c_ceclosefnx
)
175 (*ct
->c_ceclosefnx
) (ct
);
177 /* block a few signals */
179 sigaddset (&set
, SIGHUP
);
180 sigaddset (&set
, SIGINT
);
181 sigaddset (&set
, SIGQUIT
);
182 sigaddset (&set
, SIGTERM
);
183 sigprocmask (SIG_BLOCK
, &set
, &oset
);
185 while (!concatsw
&& wait (&status
) != NOTOK
) {
190 /* reset the signal mask */
191 sigprocmask (SIG_SETMASK
, &oset
, &set
);
198 * Use the mhlproc to show the header fields
202 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
209 vec
= argsplit(mhlproc
, &file
, &vecp
);
210 vec
[vecp
++] = mh_xstrdup("-form");
211 vec
[vecp
++] = mh_xstrdup(form
);
212 vec
[vecp
++] = mh_xstrdup("-nobody");
213 vec
[vecp
++] = getcpy(ct
->c_file
);
216 * If we've specified -(no)moreproc,
217 * then just pass that along.
219 if (nomore
|| concatsw
) {
220 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
222 vec
[vecp
++] = mh_xstrdup("-moreproc");
223 vec
[vecp
++] = mh_xstrdup(progsw
);
232 adios ("fork", "unable to");
237 fprintf (stderr
, "unable to exec ");
243 pidcheck(pidwait(child_id
, NOTOK
));
247 arglist_free(file
, vec
);
252 * Switching routine. Call the correct routine
253 * based on content type.
257 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
260 switch (ct
->c_type
) {
262 return show_multi (ct
, alternate
, concatsw
, textonly
,
266 switch (ct
->c_subtype
) {
267 case MESSAGE_PARTIAL
:
268 return show_partial (ct
, alternate
);
270 case MESSAGE_EXTERNAL
:
271 return show_external (ct
, alternate
, concatsw
, textonly
,
275 return show_message_rfc822 (ct
, alternate
, fmt
);
278 * Treat unknown message types as equivalent to
279 * application/octet-stream for now
282 return show_content (ct
, alternate
, textonly
,
287 return show_text (ct
, alternate
, concatsw
, fmt
);
294 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
297 return 0; /* NOT REACHED */
302 * Generic method for displaying content
306 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
310 CI ci
= &ct
->c_ctinfo
;
313 * If we're here, we are not a text type. So we don't need to check
317 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
318 output_marker(ct
, fmt
, 1);
322 /* Check for invo_name-show-type[/subtype] */
323 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
324 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
326 if ((cp
= ct
->c_showproc
))
327 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
329 /* complain if we are not a part of a multipart/alternative */
331 content_error (NULL
, ct
, "don't know how to display content");
338 * Parse the display string for displaying generic content
342 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
345 int xstdin
= 0, xlist
= 0;
346 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
348 if (!ct
->c_ceopenfnx
) {
350 content_error (NULL
, ct
, "don't know how to decode content");
355 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
357 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
360 if (! strcmp(invo_name
, "mhshow") &&
361 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
362 /* This has to be done after calling c_ceopenfnx, so
363 unfortunately the type checks are necessary without
364 some code rearrangement. And to make this really ugly,
365 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
366 if (convert_content_charset (ct
, &file
) == OK
) {
367 (*ct
->c_ceclosefnx
) (ct
);
368 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
371 char *charset
= content_charset (ct
);
372 inform("unable to convert character set%s%s from %s, continuing...",
373 ct
->c_partno
? " of part " : "",
374 FENDNULL(ct
->c_partno
),
381 strncpy (buffer
, cp
, sizeof(buffer
));
385 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
386 sizeof(buffer
) - 1, 0)) {
387 inform("Buffer overflow constructing show command, continuing...");
392 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
393 fd
, xlist
, xstdin
, fmt
);
398 * Routine to actually display the content
402 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
403 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
409 if (debugsw
|| cracked
) {
412 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
415 fprintf (stderr
, " part %s", ct
->c_partno
);
417 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
419 fprintf (stderr
, " using command %s\n", buffer
);
423 output_marker(ct
, fmt
, 0);
427 * If the command is a zero-length string, just write the output on
431 if (buffer
[0] == '\0') {
432 char readbuf
[BUFSIZ
];
434 char lastchar
= '\n';
437 inform("Cannot use NULL command to display content-type "
438 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
442 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
443 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
444 advise ("putline", "fwrite");
446 lastchar
= readbuf
[cc
- 1];
450 advise("read", "while reading text content");
455 * The MIME standards allow content to not have a trailing newline.
456 * But because we are (presumably) sending this to stdout, include
457 * a newline for text content if the final character was not a
458 * newline. Only do this for mhshow.
461 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
462 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
471 vec
= argsplit(buffer
, &file
, &vecp
);
479 advise ("fork", "unable to");
480 (*ct
->c_ceclosefnx
) (ct
);
485 if (chdir (cracked
) < 0) {
486 advise (cracked
, "chdir");
493 fprintf (stderr
, "unable to exec ");
500 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
501 /* Copy the real display program name. This relies on the
502 specific construction of vec[] by argsplit(). */
506 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
508 arglist_free(file
, vec
);
510 (*ct
->c_ceclosefnx
) (ct
);
511 return alternate
? OK
: status
;
518 * show content of type "text"
522 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
524 char *cp
, buffer
[BUFSIZ
];
525 CI ci
= &ct
->c_ctinfo
;
527 /* Check for invo_name-show-type[/subtype] */
528 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
529 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
532 * Use default method if content is text/plain, or if
533 * if it is not a text part of a multipart/alternative
535 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
538 snprintf(buffer
, sizeof(buffer
), "%%lcat");
540 snprintf(buffer
, sizeof(buffer
), "%%l");
542 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
543 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
544 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
545 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
553 * show message body of type "multipart"
557 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
561 CI ci
= &ct
->c_ctinfo
;
563 /* Check for invo_name-show-type[/subtype] */
564 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
565 return show_multi_aux (ct
, alternate
, cp
, fmt
);
567 if ((cp
= ct
->c_showproc
)) {
568 return show_multi_aux (ct
, alternate
, cp
, fmt
);
572 * Use default method to display this multipart content. Even
573 * unknown types are displayable, since they're treated as mixed
576 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
582 * show message body of subtypes of multipart that
583 * we understand directly (mixed, alternate, etc...)
587 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
588 int inlineonly
, struct format
*fmt
)
590 int alternating
, nowalternate
, result
;
591 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
593 bool request_matched
;
594 bool display_success
;
600 nowalternate
= alternate
;
602 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
608 * alternate -> we are a part inside a multipart/alternative
609 * alternating -> we are a multipart/alternative
613 request_matched
= false;
614 display_success
= false;
615 mult_alt_done
= false;
617 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
620 /* while looking for the right displayable alternative, we
621 * use a looser search criterion than we do after finding it.
622 * specifically, while still looking, part_ok() will match
623 * "parent" parts (e.g. "-part 2" where 2 is a high-level
624 * multipart). after finding it, we use part_exact() to only
625 * choose a part that was requested explicitly.
627 if ((part_exact(p
) && type_ok(p
, 1)) ||
628 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
632 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
634 switch (inneresult
) {
635 case NOTOK
: /* hard display error */
636 request_matched
= true;
637 if (alternate
&& !alternating
) {
643 case DONE
: /* found no match on content type */
646 case OK
: /* display successful */
647 request_matched
= true;
648 display_success
= true;
651 /* if we got success on a sub-part of
652 * multipart/alternative, we're done, unless
653 * there's a chance an explicit part should be
654 * matched later in the alternatives. */
656 mult_alt_done
= true;
657 } else if (alternate
) {
658 alternate
= nowalternate
= 0;
666 /* we're supposed to be displaying at least something from a
667 * multipart/alternative. if we've had parts to consider, and
668 * we've had no success, then we should complain. we shouldn't
669 * complain if none of the parts matched any -part or -type option.
671 if (alternating
&& request_matched
&& !display_success
) {
672 /* if we're ourselves an alternate. don't complain yet. */
674 content_error (NULL
, ct
, "don't know how to display any of the contents");
679 /* if no parts matched what was requested, there can't have been
680 * any display errors. we report DONE rather than OK. */
681 ret
= request_matched
? result
: DONE
;
687 * Parse display string for multipart content
688 * and use external program to display it.
692 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
694 /* xstdin is only used in the call to parse_display_string():
695 its value is ignored in the function. */
696 int xstdin
= 0, xlist
= 0;
697 char *file
= NULL
, buffer
[BUFSIZ
];
698 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
702 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
705 if (!p
->c_ceopenfnx
) {
707 content_error (NULL
, p
, "don't know how to decode content");
711 if (p
->c_storage
== NULL
) {
712 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
715 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
717 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
719 (*p
->c_ceclosefnx
) (p
);
723 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
724 buffer
, sizeof(buffer
) - 1, 1)) {
725 inform("Buffer overflow constructing show command, continuing...");
729 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
734 * show content of type "message/rfc822"
738 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
741 CI ci
= &ct
->c_ctinfo
;
743 /* Check for invo_name-show-type[/subtype] */
744 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
745 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
747 if ((cp
= ct
->c_showproc
))
748 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
750 /* default method for message/rfc822 */
751 if (ct
->c_subtype
== MESSAGE_RFC822
) {
752 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
753 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
756 /* complain if we are not a part of a multipart/alternative */
758 content_error (NULL
, ct
, "don't know how to display content");
765 * Show content of type "message/partial".
769 show_partial (CT ct
, int alternate
)
771 NMH_UNUSED (alternate
);
773 content_error (NULL
, ct
,
774 "in order to display this message, you must reassemble it");
780 * Show content of type "message/external".
782 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
786 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
789 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
790 CT p
= e
->eb_content
;
795 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
800 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
801 char *file
, char *buffer
, size_t buflen
,
806 char *bp
= buffer
, *pp
;
807 CI ci
= &ct
->c_ctinfo
;
809 bp
[0] = bp
[buflen
] = '\0';
811 for ( ; *cp
&& buflen
> 0; cp
++) {
817 /* insert parameters from Content-Type field */
822 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
823 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
824 get_param_value(pm
, '?'));
834 /* insert content description */
838 s
= trimcpy (ct
->c_descr
);
839 strncpy (bp
, s
, buflen
);
845 /* no longer implemented */
849 /* %f, and stdin is terminal not content */
855 /* insert filename(s) containing content */
856 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
861 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
864 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
871 /* insert filename containing content */
872 snprintf (bp
, buflen
, "%s", file
);
875 * Old comments below are left here for posterity.
876 * This was/is tricky.
878 /* since we've quoted the file argument, set things up
879 * to look past it, to avoid problems with the quoting
880 * logic below. (I know, I should figure out what's
881 * broken with the quoting logic, but..)
884 * Here's the email that submitted the patch with
886 * https://www.mail-archive.com/nmh-workers@mhost.com/
888 * I can't tell from that exactly what was broken,
889 * beyond misquoting of the filename. The profile
890 * had appearances of %F both with and without quotes.
891 * The unquoted ones should have been quoted by the
893 * The fix was to always quote the filename. But
894 * that broke '%F' because it expanded to ''filename''.
897 * Old comments above are left here for posterity.
898 * The quoting below should work properly now.
904 /* No longer supported */
908 /* display listing prior to displaying content */
913 /* insert subtype of content */
914 strncpy (bp
, ci
->ci_subtype
, buflen
);
918 /* insert character % */
922 const char *closing_brace
= strchr(cp
, '}');
925 const size_t param_len
= closing_brace
- cp
- 1;
926 char *param
= mh_xmalloc(param_len
+ 1);
929 (void) strncpy(param
, cp
+ 1, param_len
);
930 param
[param_len
] = '\0';
931 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
934 cp
+= param_len
+ 1; /* Skip both braces, too. */
937 /* %{param} is set in the Content-Type header.
938 After the break below, quote it if necessary. */
939 (void) strncpy(bp
, value
, buflen
);
942 /* %{param} not found, so skip it completely. cp
943 was advanced above. */
947 /* This will get confused if there are multiple %{}'s,
948 but its real purpose is to avoid doing bad things
949 above if a closing brace wasn't found. */
950 inform("no closing brace for display string escape %s, continuing...",
967 /* Did we actually insert something? */
969 /* Insert single quote if not inside quotes already */
970 if (!quoted
&& buflen
) {
972 memmove (pp
+ 1, pp
, len
+1);
978 /* Escape existing quotes */
979 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
982 /* Quoted. Let this quote close that quoting.
983 Insert an escaped quote to replace it and
984 another quote to reopen quoting, which will be
986 memmove (pp
+ 2, pp
, len
);
993 /* Not quoted. This should not be reached with
994 the current code, but handle the condition
995 in case the code changes. Just escape the
997 memmove (pp
, pp
-1, len
+1);
1003 /* If pp is still set, that means we ran out of space. */
1006 /* Close quoting. */
1007 if (quoted
&& buflen
) {
1008 /* See if we need to close the quote by looking
1009 for an odd number of unescaped close quotes in
1010 the remainder of the display string. */
1011 int found_quote
= 0, escaped
= 0;
1014 for (c
= cp
+1; *c
; ++c
) {
1016 escaped
= ! escaped
;
1022 found_quote
= ! found_quote
;
1027 if (! found_quote
) {
1047 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1048 /* content_error would provide a more useful error message
1049 * here, except that if we got overrun, it probably would
1055 /* use charset string to modify display method */
1056 if (ct
->c_termproc
) {
1059 strncpy (term
, buffer
, sizeof(term
));
1060 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1068 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1070 char *src_charset
= content_charset (ct
);
1073 if (strcasecmp (src_charset
, dest_charset
)) {
1075 iconv_t conv_desc
= NULL
;
1082 bool opened_input_file
= false;
1083 char src_buffer
[BUFSIZ
];
1084 size_t dest_buffer_size
= BUFSIZ
;
1085 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1088 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1090 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1092 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1097 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1098 die("unable to create temporary file in %s",
1101 dest
= mh_xstrdup(tempfile
);
1103 if (ct
->c_cefile
.ce_file
) {
1104 file
= &ct
->c_cefile
.ce_file
;
1105 fp
= &ct
->c_cefile
.ce_fp
;
1107 } else if (ct
->c_file
) {
1110 begin
= (size_t) ct
->c_begin
;
1111 end
= (size_t) ct
->c_end
;
1112 } /* else no input file: shouldn't happen */
1114 if (file
&& *file
&& fp
) {
1116 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1117 advise (*file
, "unable to open for reading");
1120 opened_input_file
= true;
1127 size_t bytes_to_read
=
1128 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1130 fseeko (*fp
, begin
, SEEK_SET
);
1131 while ((inbytes
= fread (src_buffer
, 1,
1132 min (bytes_to_read
, sizeof src_buffer
),
1134 ICONV_CONST
char *ib
= src_buffer
;
1135 char *ob
= dest_buffer
;
1136 size_t outbytes
= dest_buffer_size
;
1137 size_t outbytes_before
= outbytes
;
1139 if (end
> 0) bytes_to_read
-= inbytes
;
1142 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1144 if (errno
== E2BIG
) {
1146 * Bump up the buffer by at least a factor of 2
1147 * over what we need.
1149 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1150 dest_buffer_size
+= bumpup
;
1151 dest_buffer
= mh_xrealloc(dest_buffer
,
1153 ob
= dest_buffer
+ ob_off
;
1155 outbytes_before
+= bumpup
;
1158 if (errno
== EINVAL
) {
1159 /* middle of multi-byte sequence */
1160 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1161 advise (dest
, "write");
1163 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1164 if (end
> 0) bytes_to_read
+= inbytes
;
1165 /* inform("convert_charset: EINVAL"); */
1168 if (errno
== EILSEQ
) {
1169 /* invalid multi-byte sequence */
1171 for (++ib
, --inbytes
;
1173 (((unsigned char) *ib
) & 0xc0) == 0x80;
1177 ib
++; inbytes
--; /* skip it */
1179 (*ob
++) = '?'; outbytes
--;
1180 /* inform("convert_charset: EILSEQ"); */
1183 inform("convert_charset: errno = %d", errno
);
1188 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1189 advise (dest
, "write");
1193 if (opened_input_file
) {
1199 iconv_close (conv_desc
);
1203 /* Replace the decoded file with the converted one. */
1204 if (ct
->c_cefile
.ce_file
) {
1205 if (ct
->c_cefile
.ce_unlink
) {
1206 (void) m_unlink (ct
->c_cefile
.ce_file
);
1208 free (ct
->c_cefile
.ce_file
);
1210 ct
->c_cefile
.ce_file
= dest
;
1211 ct
->c_cefile
.ce_unlink
= 1;
1215 /* Update ct->c_ctline. */
1217 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1218 ct
->c_ctinfo
.ci_subtype
, NULL
);
1221 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1222 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1224 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1225 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1227 ctline
= add(outline
, ctline
);
1231 free (ct
->c_ctline
);
1232 ct
->c_ctline
= ctline
;
1233 } /* else no CT line, which is odd */
1235 /* Update Content-Type header field. */
1236 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1237 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1238 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1246 (void) m_unlink (dest
);
1249 #else /* ! HAVE_ICONV */
1250 NMH_UNUSED (message_mods
);
1252 inform("Can't convert %s to %s without iconv", src_charset
,
1256 #endif /* ! HAVE_ICONV */
1265 convert_content_charset (CT ct
, char **file
)
1270 /* Using current locale, see if the content needs to be converted. */
1272 /* content_charset() cannot return NULL. */
1273 char *src_charset
= content_charset (ct
);
1275 if (! check_charset (src_charset
, strlen (src_charset
))) {
1278 char *dest_charset
= getcpy (get_charset ());
1280 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1281 *file
= ct
->c_cefile
.ce_file
;
1286 free (dest_charset
);
1289 #else /* ! HAVE_ICONV */
1292 #endif /* ! HAVE_ICONV */
1298 * Compile our format string and save any parameters we care about.
1301 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1302 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1303 "%<{description}%{description}" \
1304 "%?{cdispo-filename}%{cdispo-filename}" \
1305 "%|%{ctype-name}%> " \
1306 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1308 static struct format
*
1309 compile_header(char *form
)
1313 struct comp
*comp
= NULL
;
1314 unsigned int bucket
;
1316 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1318 (void) fmt_compile(fmtstring
, &fmt
, 1);
1321 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1322 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1330 static struct format
*
1331 compile_marker(char *form
)
1335 struct comp
*comp
= NULL
;
1336 unsigned int bucket
;
1337 struct param_comp_list
*pc_entry
;
1339 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1341 (void) fmt_compile(fmtstring
, &fmt
, 1);
1345 * Things we care about:
1347 * part - Part name (e.g., 1.1)
1348 * content-type - Content-Type
1349 * description - Content-Description
1350 * disposition - Content-Disposition (inline, attachment)
1351 * ctype-<param> - Content-Type parameter
1352 * cdispo-<param> - Content-Disposition parameter
1355 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1356 if (strcasecmp(comp
->c_name
, "part") == 0) {
1358 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1360 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1361 description_comp
= comp
;
1362 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1364 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1365 strlen(comp
->c_name
) > 6) {
1367 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1368 pc_entry
->comp
= comp
;
1369 pc_entry
->next
= ctype_pc_list
;
1370 ctype_pc_list
= pc_entry
;
1371 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1372 strlen(comp
->c_name
) > 7) {
1374 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1375 pc_entry
->comp
= comp
;
1376 pc_entry
->next
= dispo_pc_list
;
1377 dispo_pc_list
= pc_entry
;
1385 * Output on stdout an appropriate marker for this content, using mh-format
1389 output_header(CT ct
, struct format
*fmt
)
1391 charstring_t outbuf
= charstring_create (BUFSIZ
);
1397 folder_comp
->c_text
= getcpy(folder
);
1399 if (ct
->c_file
&& *ct
->c_file
) {
1400 message
= strtol(ct
->c_file
, &endp
, 10);
1401 if (*endp
) message
= 0;
1405 /* it would be nice to populate dat[2], for %(size) here,
1406 * but it's not available. it might also be nice to know
1407 * if the message originally had any mime parts or not -- but
1408 * there's also no record of that. (except for MIME-version:)
1411 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1413 fputs(charstring_buffer (outbuf
), stdout
);
1414 charstring_free (outbuf
);
1420 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1422 charstring_t outbuf
= charstring_create (BUFSIZ
);
1423 struct param_comp_list
*pcentry
;
1430 * Grab any items we care about.
1433 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1434 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1435 ct
->c_ctinfo
.ci_subtype
, NULL
);
1438 if (part_comp
&& ct
->c_partno
) {
1439 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1442 if (description_comp
&& ct
->c_descr
) {
1443 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1446 if (dispo_comp
&& ct
->c_dispo_type
) {
1447 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1450 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1451 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1452 pcentry
->param
, '?', 0);
1455 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1456 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1457 pcentry
->param
, '?', 0);
1460 if (ct
->c_cesizefnx
)
1461 partsize
= (*ct
->c_cesizefnx
) (ct
);
1463 partsize
= ct
->c_end
- ct
->c_begin
;
1465 if (ct
->c_file
&& *ct
->c_file
) {
1466 message
= strtol(ct
->c_file
, &endp
, 10);
1467 if (*endp
) message
= 0;
1472 /* make the part's hidden aspect available by overloading the
1473 * %(unseen) function. make the part's size available via %(size).
1474 * see comments in h/fmt_scan.h.
1478 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1480 fputs(charstring_buffer (outbuf
), stdout
);
1481 charstring_free (outbuf
);
1487 * Reset (and free) any of the saved marker text
1491 free_markercomps(void)
1493 struct param_comp_list
*pc_entry
, *pc2
;
1498 description_comp
= NULL
;
1501 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1502 free(pc_entry
->param
);
1503 pc2
= pc_entry
->next
;
1508 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1509 free(pc_entry
->param
);
1510 pc2
= pc_entry
->next
;
1517 * Exit if the display process returned with a nonzero exit code, or terminated
1518 * with a SIGQUIT signal.
1522 pidcheck (int status
)
1524 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)