]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
fccf3c185d39139762939491b29e35869888a044
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/fmt_new.h"
10 #include "sbr/concat.h"
11 #include "sbr/trimcpy.h"
12 #include "sbr/check_charset.h"
13 #include "sbr/getcpy.h"
14 #include "sbr/context_find.h"
15 #include "sbr/pidstatus.h"
16 #include "sbr/arglist.h"
17 #include "sbr/error.h"
19 #include "h/signals.h"
24 #include "h/mhparse.h"
25 #include "h/fmt_scan.h"
29 #include "mhshowsbr.h"
30 #include "sbr/m_mktemp.h"
33 #endif /* ! HAVE_ICONV */
41 /* flags for moreproc/header display */
45 /* for output markers and headers */
55 static void show_single_message (CT
, char *, int, int, int, struct format
*);
56 static void DisplayMsgHeader (CT
, char *, int);
57 static int show_switch (CT
, int, int, int, int, struct format
*);
58 static int show_content (CT
, int, int, int, struct format
*);
59 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
60 static int show_text (CT
, int, int, struct format
*);
61 static int show_multi (CT
, int, int, int, int, struct format
*);
62 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
63 static int show_multi_aux (CT
, int, char *, struct format
*);
64 static int show_message_rfc822 (CT
, int, struct format
*);
65 static int show_partial (CT
, int);
66 static int show_external (CT
, int, int, int, int, struct format
*);
67 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
68 size_t, int multipart
);
69 static int convert_content_charset (CT
, char **);
70 static struct format
*compile_header(char *);
71 static struct format
*compile_marker(char *);
72 static void output_header (CT
, struct format
*);
73 static void output_marker (CT
, struct format
*, int);
74 static void free_markercomps (void);
75 static int pidcheck(int);
78 * Components (and list of parameters/components) we care about for the
79 * content marker display.
82 static struct comp
*folder_comp
= NULL
;
83 static struct comp
*part_comp
= NULL
;
84 static struct comp
*ctype_comp
= NULL
;
85 static struct comp
*description_comp
= NULL
;
86 static struct comp
*dispo_comp
= NULL
;
88 struct param_comp_list
{
91 struct param_comp_list
*next
;
94 static struct param_comp_list
*ctype_pc_list
= NULL
;
95 static struct param_comp_list
*dispo_pc_list
= NULL
;
99 * Top level entry point to show/display a group of messages
103 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
106 struct format
*hfmt
, *mfmt
;
109 * If form is not specified, then get default form
110 * for showing headers of MIME messages.
113 formsw
= mh_xstrdup(etcpath("mhl.headers"));
116 * Compile the content marker and header format lines
118 mfmt
= compile_marker(markerform
);
119 hfmt
= compile_header(headerform
);
122 * If form is "mhl.null", suppress display of header.
124 if (!strcmp (formsw
, "mhl.null"))
127 for (ctp
= cts
; *ctp
; ctp
++) {
130 /* if top-level type is ok, then display message */
131 if (type_ok (ct
, 1)) {
132 if (headersw
) output_header(ct
, hfmt
);
134 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
146 * Entry point to show/display a single message
150 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
151 int inlineonly
, struct format
*fmt
)
157 /* Allow user executable bit so that temporary directories created by
158 * the viewer (e.g., lynx) are going to be accessible */
159 umask (ct
->c_umask
& ~(0100));
162 * If you have a format file, then display
163 * the message headers.
166 DisplayMsgHeader(ct
, form
, concatsw
);
168 /* Show the body of the message */
169 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
175 if (ct
->c_ceclosefnx
)
176 (*ct
->c_ceclosefnx
) (ct
);
178 /* block a few signals */
180 sigaddset (&set
, SIGHUP
);
181 sigaddset (&set
, SIGINT
);
182 sigaddset (&set
, SIGQUIT
);
183 sigaddset (&set
, SIGTERM
);
184 sigprocmask (SIG_BLOCK
, &set
, &oset
);
186 while (!concatsw
&& wait (&status
) != NOTOK
) {
191 /* reset the signal mask */
192 sigprocmask (SIG_SETMASK
, &oset
, &set
);
199 * Use the mhlproc to show the header fields
203 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
210 vec
= argsplit(mhlproc
, &file
, &vecp
);
211 vec
[vecp
++] = mh_xstrdup("-form");
212 vec
[vecp
++] = mh_xstrdup(form
);
213 vec
[vecp
++] = mh_xstrdup("-nobody");
214 vec
[vecp
++] = getcpy(ct
->c_file
);
217 * If we've specified -(no)moreproc,
218 * then just pass that along.
220 if (nomore
|| concatsw
) {
221 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
223 vec
[vecp
++] = mh_xstrdup("-moreproc");
224 vec
[vecp
++] = mh_xstrdup(progsw
);
233 adios ("fork", "unable to");
238 fprintf (stderr
, "unable to exec ");
244 pidcheck(pidwait(child_id
, NOTOK
));
248 arglist_free(file
, vec
);
253 * Switching routine. Call the correct routine
254 * based on content type.
258 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
261 switch (ct
->c_type
) {
263 return show_multi (ct
, alternate
, concatsw
, textonly
,
267 switch (ct
->c_subtype
) {
268 case MESSAGE_PARTIAL
:
269 return show_partial (ct
, alternate
);
271 case MESSAGE_EXTERNAL
:
272 return show_external (ct
, alternate
, concatsw
, textonly
,
276 return show_message_rfc822 (ct
, alternate
, fmt
);
279 * Treat unknown message types as equivalent to
280 * application/octet-stream for now
283 return show_content (ct
, alternate
, textonly
,
288 return show_text (ct
, alternate
, concatsw
, fmt
);
295 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
298 return 0; /* NOT REACHED */
303 * Generic method for displaying content
307 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
311 CI ci
= &ct
->c_ctinfo
;
314 * If we're here, we are not a text type. So we don't need to check
318 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
319 output_marker(ct
, fmt
, 1);
323 /* Check for invo_name-show-type[/subtype] */
324 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
325 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
327 if ((cp
= ct
->c_showproc
))
328 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
330 /* complain if we are not a part of a multipart/alternative */
332 content_error (NULL
, ct
, "don't know how to display content");
339 * Parse the display string for displaying generic content
343 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
346 int xstdin
= 0, xlist
= 0;
347 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
349 if (!ct
->c_ceopenfnx
) {
351 content_error (NULL
, ct
, "don't know how to decode content");
356 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
358 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
361 if (! strcmp(invo_name
, "mhshow") &&
362 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
363 /* This has to be done after calling c_ceopenfnx, so
364 unfortunately the type checks are necessary without
365 some code rearrangement. And to make this really ugly,
366 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
367 if (convert_content_charset (ct
, &file
) == OK
) {
368 (*ct
->c_ceclosefnx
) (ct
);
369 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
372 char *charset
= content_charset (ct
);
373 inform("unable to convert character set%s%s from %s, continuing...",
374 ct
->c_partno
? " of part " : "",
375 FENDNULL(ct
->c_partno
),
382 strncpy (buffer
, cp
, sizeof(buffer
));
386 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
387 sizeof(buffer
) - 1, 0)) {
388 inform("Buffer overflow constructing show command, continuing...");
393 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
394 fd
, xlist
, xstdin
, fmt
);
399 * Routine to actually display the content
403 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
404 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
410 if (debugsw
|| cracked
) {
413 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
416 fprintf (stderr
, " part %s", ct
->c_partno
);
418 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
420 fprintf (stderr
, " using command %s\n", buffer
);
424 output_marker(ct
, fmt
, 0);
428 * If the command is a zero-length string, just write the output on
432 if (buffer
[0] == '\0') {
433 char readbuf
[BUFSIZ
];
435 char lastchar
= '\n';
438 inform("Cannot use NULL command to display content-type "
439 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
443 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
444 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
445 advise ("putline", "fwrite");
447 lastchar
= readbuf
[cc
- 1];
451 advise("read", "while reading text content");
456 * The MIME standards allow content to not have a trailing newline.
457 * But because we are (presumably) sending this to stdout, include
458 * a newline for text content if the final character was not a
459 * newline. Only do this for mhshow.
462 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
463 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
472 vec
= argsplit(buffer
, &file
, &vecp
);
480 advise ("fork", "unable to");
481 (*ct
->c_ceclosefnx
) (ct
);
486 if (chdir (cracked
) < 0) {
487 advise (cracked
, "chdir");
494 fprintf (stderr
, "unable to exec ");
501 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
502 /* Copy the real display program name. This relies on the
503 specific construction of vec[] by argsplit(). */
507 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
509 arglist_free(file
, vec
);
511 (*ct
->c_ceclosefnx
) (ct
);
512 return alternate
? OK
: status
;
519 * show content of type "text"
523 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
525 char *cp
, buffer
[BUFSIZ
];
526 CI ci
= &ct
->c_ctinfo
;
528 /* Check for invo_name-show-type[/subtype] */
529 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
530 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
533 * Use default method if content is text/plain, or if
534 * if it is not a text part of a multipart/alternative
536 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
539 snprintf(buffer
, sizeof(buffer
), "%%lcat");
541 snprintf(buffer
, sizeof(buffer
), "%%l");
543 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
544 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
545 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
546 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
554 * show message body of type "multipart"
558 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
562 CI ci
= &ct
->c_ctinfo
;
564 /* Check for invo_name-show-type[/subtype] */
565 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
566 return show_multi_aux (ct
, alternate
, cp
, fmt
);
568 if ((cp
= ct
->c_showproc
)) {
569 return show_multi_aux (ct
, alternate
, cp
, fmt
);
573 * Use default method to display this multipart content. Even
574 * unknown types are displayable, since they're treated as mixed
577 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
583 * show message body of subtypes of multipart that
584 * we understand directly (mixed, alternate, etc...)
588 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
589 int inlineonly
, struct format
*fmt
)
591 int alternating
, nowalternate
, result
;
592 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
594 bool request_matched
;
595 bool display_success
;
601 nowalternate
= alternate
;
603 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
609 * alternate -> we are a part inside a multipart/alternative
610 * alternating -> we are a multipart/alternative
614 request_matched
= false;
615 display_success
= false;
616 mult_alt_done
= false;
618 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
621 /* while looking for the right displayable alternative, we
622 * use a looser search criterion than we do after finding it.
623 * specifically, while still looking, part_ok() will match
624 * "parent" parts (e.g. "-part 2" where 2 is a high-level
625 * multipart). after finding it, we use part_exact() to only
626 * choose a part that was requested explicitly.
628 if ((part_exact(p
) && type_ok(p
, 1)) ||
629 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
633 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
635 switch (inneresult
) {
636 case NOTOK
: /* hard display error */
637 request_matched
= true;
638 if (alternate
&& !alternating
) {
644 case DONE
: /* found no match on content type */
647 case OK
: /* display successful */
648 request_matched
= true;
649 display_success
= true;
652 /* if we got success on a sub-part of
653 * multipart/alternative, we're done, unless
654 * there's a chance an explicit part should be
655 * matched later in the alternatives. */
657 mult_alt_done
= true;
658 } else if (alternate
) {
659 alternate
= nowalternate
= 0;
667 /* we're supposed to be displaying at least something from a
668 * multipart/alternative. if we've had parts to consider, and
669 * we've had no success, then we should complain. we shouldn't
670 * complain if none of the parts matched any -part or -type option.
672 if (alternating
&& request_matched
&& !display_success
) {
673 /* if we're ourselves an alternate. don't complain yet. */
675 content_error (NULL
, ct
, "don't know how to display any of the contents");
680 /* if no parts matched what was requested, there can't have been
681 * any display errors. we report DONE rather than OK. */
682 ret
= request_matched
? result
: DONE
;
688 * Parse display string for multipart content
689 * and use external program to display it.
693 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
695 /* xstdin is only used in the call to parse_display_string():
696 its value is ignored in the function. */
697 int xstdin
= 0, xlist
= 0;
698 char *file
= NULL
, buffer
[BUFSIZ
];
699 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
703 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
706 if (!p
->c_ceopenfnx
) {
708 content_error (NULL
, p
, "don't know how to decode content");
712 if (p
->c_storage
== NULL
) {
713 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
716 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
718 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
720 (*p
->c_ceclosefnx
) (p
);
724 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
725 buffer
, sizeof(buffer
) - 1, 1)) {
726 inform("Buffer overflow constructing show command, continuing...");
730 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
735 * show content of type "message/rfc822"
739 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
742 CI ci
= &ct
->c_ctinfo
;
744 /* Check for invo_name-show-type[/subtype] */
745 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
746 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
748 if ((cp
= ct
->c_showproc
))
749 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
751 /* default method for message/rfc822 */
752 if (ct
->c_subtype
== MESSAGE_RFC822
) {
753 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
754 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
757 /* complain if we are not a part of a multipart/alternative */
759 content_error (NULL
, ct
, "don't know how to display content");
766 * Show content of type "message/partial".
770 show_partial (CT ct
, int alternate
)
772 NMH_UNUSED (alternate
);
774 content_error (NULL
, ct
,
775 "in order to display this message, you must reassemble it");
781 * Show content of type "message/external".
783 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
787 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
790 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
791 CT p
= e
->eb_content
;
796 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
801 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
802 char *file
, char *buffer
, size_t buflen
,
807 char *bp
= buffer
, *pp
;
808 CI ci
= &ct
->c_ctinfo
;
810 bp
[0] = bp
[buflen
] = '\0';
812 for ( ; *cp
&& buflen
> 0; cp
++) {
818 /* insert parameters from Content-Type field */
823 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
824 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
825 get_param_value(pm
, '?'));
835 /* insert content description */
839 s
= trimcpy (ct
->c_descr
);
840 strncpy (bp
, s
, buflen
);
846 /* no longer implemented */
850 /* %f, and stdin is terminal not content */
856 /* insert filename(s) containing content */
857 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
862 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
865 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
872 /* insert filename containing content */
873 snprintf (bp
, buflen
, "%s", file
);
876 * Old comments below are left here for posterity.
877 * This was/is tricky.
879 /* since we've quoted the file argument, set things up
880 * to look past it, to avoid problems with the quoting
881 * logic below. (I know, I should figure out what's
882 * broken with the quoting logic, but..)
885 * Here's the email that submitted the patch with
887 * https://www.mail-archive.com/nmh-workers@mhost.com/
889 * I can't tell from that exactly what was broken,
890 * beyond misquoting of the filename. The profile
891 * had appearances of %F both with and without quotes.
892 * The unquoted ones should have been quoted by the
894 * The fix was to always quote the filename. But
895 * that broke '%F' because it expanded to ''filename''.
898 * Old comments above are left here for posterity.
899 * The quoting below should work properly now.
905 /* No longer supported */
909 /* display listing prior to displaying content */
914 /* insert subtype of content */
915 strncpy (bp
, ci
->ci_subtype
, buflen
);
919 /* insert character % */
923 const char *closing_brace
= strchr(cp
, '}');
926 const size_t param_len
= closing_brace
- cp
- 1;
927 char *param
= mh_xmalloc(param_len
+ 1);
930 (void) strncpy(param
, cp
+ 1, param_len
);
931 param
[param_len
] = '\0';
932 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
935 cp
+= param_len
+ 1; /* Skip both braces, too. */
938 /* %{param} is set in the Content-Type header.
939 After the break below, quote it if necessary. */
940 (void) strncpy(bp
, value
, buflen
);
943 /* %{param} not found, so skip it completely. cp
944 was advanced above. */
948 /* This will get confused if there are multiple %{}'s,
949 but its real purpose is to avoid doing bad things
950 above if a closing brace wasn't found. */
951 inform("no closing brace for display string escape %s, continuing...",
968 /* Did we actually insert something? */
970 /* Insert single quote if not inside quotes already */
971 if (!quoted
&& buflen
) {
973 memmove (pp
+ 1, pp
, len
+1);
979 /* Escape existing quotes */
980 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
983 /* Quoted. Let this quote close that quoting.
984 Insert an escaped quote to replace it and
985 another quote to reopen quoting, which will be
987 memmove (pp
+ 2, pp
, len
);
994 /* Not quoted. This should not be reached with
995 the current code, but handle the condition
996 in case the code changes. Just escape the
998 memmove (pp
, pp
-1, len
+1);
1004 /* If pp is still set, that means we ran out of space. */
1007 /* Close quoting. */
1008 if (quoted
&& buflen
) {
1009 /* See if we need to close the quote by looking
1010 for an odd number of unescaped close quotes in
1011 the remainder of the display string. */
1012 int found_quote
= 0, escaped
= 0;
1015 for (c
= cp
+1; *c
; ++c
) {
1017 escaped
= ! escaped
;
1023 found_quote
= ! found_quote
;
1028 if (! found_quote
) {
1048 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1049 /* content_error would provide a more useful error message
1050 * here, except that if we got overrun, it probably would
1056 /* use charset string to modify display method */
1057 if (ct
->c_termproc
) {
1060 strncpy (term
, buffer
, sizeof(term
));
1061 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1069 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1071 char *src_charset
= content_charset (ct
);
1074 if (strcasecmp (src_charset
, dest_charset
)) {
1076 iconv_t conv_desc
= NULL
;
1083 bool opened_input_file
= false;
1084 char src_buffer
[BUFSIZ
];
1085 size_t dest_buffer_size
= BUFSIZ
;
1086 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1089 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1091 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1093 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1098 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1099 die("unable to create temporary file in %s",
1102 dest
= mh_xstrdup(tempfile
);
1104 if (ct
->c_cefile
.ce_file
) {
1105 file
= &ct
->c_cefile
.ce_file
;
1106 fp
= &ct
->c_cefile
.ce_fp
;
1108 } else if (ct
->c_file
) {
1111 begin
= (size_t) ct
->c_begin
;
1112 end
= (size_t) ct
->c_end
;
1113 } /* else no input file: shouldn't happen */
1115 if (file
&& *file
&& fp
) {
1117 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1118 advise (*file
, "unable to open for reading");
1121 opened_input_file
= true;
1128 size_t bytes_to_read
=
1129 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1131 fseeko (*fp
, begin
, SEEK_SET
);
1132 while ((inbytes
= fread (src_buffer
, 1,
1133 min (bytes_to_read
, sizeof src_buffer
),
1135 ICONV_CONST
char *ib
= src_buffer
;
1136 char *ob
= dest_buffer
;
1137 size_t outbytes
= dest_buffer_size
;
1138 size_t outbytes_before
= outbytes
;
1140 if (end
> 0) bytes_to_read
-= inbytes
;
1143 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1145 if (errno
== E2BIG
) {
1147 * Bump up the buffer by at least a factor of 2
1148 * over what we need.
1150 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1151 dest_buffer_size
+= bumpup
;
1152 dest_buffer
= mh_xrealloc(dest_buffer
,
1154 ob
= dest_buffer
+ ob_off
;
1156 outbytes_before
+= bumpup
;
1159 if (errno
== EINVAL
) {
1160 /* middle of multi-byte sequence */
1161 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1162 advise (dest
, "write");
1164 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1165 if (end
> 0) bytes_to_read
+= inbytes
;
1166 /* inform("convert_charset: EINVAL"); */
1169 if (errno
== EILSEQ
) {
1170 /* invalid multi-byte sequence */
1172 for (++ib
, --inbytes
;
1174 (((unsigned char) *ib
) & 0xc0) == 0x80;
1178 ib
++; inbytes
--; /* skip it */
1180 (*ob
++) = '?'; outbytes
--;
1181 /* inform("convert_charset: EILSEQ"); */
1184 inform("convert_charset: errno = %d", errno
);
1189 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1190 advise (dest
, "write");
1194 if (opened_input_file
) {
1200 iconv_close (conv_desc
);
1204 /* Replace the decoded file with the converted one. */
1205 if (ct
->c_cefile
.ce_file
) {
1206 if (ct
->c_cefile
.ce_unlink
) {
1207 (void) m_unlink (ct
->c_cefile
.ce_file
);
1209 free (ct
->c_cefile
.ce_file
);
1211 ct
->c_cefile
.ce_file
= dest
;
1212 ct
->c_cefile
.ce_unlink
= 1;
1216 /* Update ct->c_ctline. */
1218 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1219 ct
->c_ctinfo
.ci_subtype
, NULL
);
1222 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1223 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1225 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1226 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1228 ctline
= add(outline
, ctline
);
1232 free (ct
->c_ctline
);
1233 ct
->c_ctline
= ctline
;
1234 } /* else no CT line, which is odd */
1236 /* Update Content-Type header field. */
1237 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1238 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1239 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1247 (void) m_unlink (dest
);
1250 #else /* ! HAVE_ICONV */
1251 NMH_UNUSED (message_mods
);
1253 inform("Can't convert %s to %s without iconv", src_charset
,
1257 #endif /* ! HAVE_ICONV */
1266 convert_content_charset (CT ct
, char **file
)
1271 /* Using current locale, see if the content needs to be converted. */
1273 /* content_charset() cannot return NULL. */
1274 char *src_charset
= content_charset (ct
);
1276 if (! check_charset (src_charset
, strlen (src_charset
))) {
1279 char *dest_charset
= getcpy (get_charset ());
1281 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1282 *file
= ct
->c_cefile
.ce_file
;
1287 free (dest_charset
);
1290 #else /* ! HAVE_ICONV */
1293 #endif /* ! HAVE_ICONV */
1299 * Compile our format string and save any parameters we care about.
1302 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1303 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1304 "%<{description}%{description}" \
1305 "%?{cdispo-filename}%{cdispo-filename}" \
1306 "%|%{ctype-name}%> " \
1307 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1309 static struct format
*
1310 compile_header(char *form
)
1314 struct comp
*comp
= NULL
;
1315 unsigned int bucket
;
1317 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1319 (void) fmt_compile(fmtstring
, &fmt
, 1);
1322 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1323 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1331 static struct format
*
1332 compile_marker(char *form
)
1336 struct comp
*comp
= NULL
;
1337 unsigned int bucket
;
1338 struct param_comp_list
*pc_entry
;
1340 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1342 (void) fmt_compile(fmtstring
, &fmt
, 1);
1346 * Things we care about:
1348 * part - Part name (e.g., 1.1)
1349 * content-type - Content-Type
1350 * description - Content-Description
1351 * disposition - Content-Disposition (inline, attachment)
1352 * ctype-<param> - Content-Type parameter
1353 * cdispo-<param> - Content-Disposition parameter
1356 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1357 if (strcasecmp(comp
->c_name
, "part") == 0) {
1359 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1361 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1362 description_comp
= comp
;
1363 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1365 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1366 strlen(comp
->c_name
) > 6) {
1368 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1369 pc_entry
->comp
= comp
;
1370 pc_entry
->next
= ctype_pc_list
;
1371 ctype_pc_list
= pc_entry
;
1372 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1373 strlen(comp
->c_name
) > 7) {
1375 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1376 pc_entry
->comp
= comp
;
1377 pc_entry
->next
= dispo_pc_list
;
1378 dispo_pc_list
= pc_entry
;
1386 * Output on stdout an appropriate marker for this content, using mh-format
1390 output_header(CT ct
, struct format
*fmt
)
1392 charstring_t outbuf
= charstring_create (BUFSIZ
);
1398 folder_comp
->c_text
= getcpy(folder
);
1400 if (ct
->c_file
&& *ct
->c_file
) {
1401 message
= strtol(ct
->c_file
, &endp
, 10);
1402 if (*endp
) message
= 0;
1406 /* it would be nice to populate dat[2], for %(size) here,
1407 * but it's not available. it might also be nice to know
1408 * if the message originally had any mime parts or not -- but
1409 * there's also no record of that. (except for MIME-version:)
1412 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1414 fputs(charstring_buffer (outbuf
), stdout
);
1415 charstring_free (outbuf
);
1421 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1423 charstring_t outbuf
= charstring_create (BUFSIZ
);
1424 struct param_comp_list
*pcentry
;
1431 * Grab any items we care about.
1434 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1435 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1436 ct
->c_ctinfo
.ci_subtype
, NULL
);
1439 if (part_comp
&& ct
->c_partno
) {
1440 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1443 if (description_comp
&& ct
->c_descr
) {
1444 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1447 if (dispo_comp
&& ct
->c_dispo_type
) {
1448 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1451 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1452 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1453 pcentry
->param
, '?', 0);
1456 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1457 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1458 pcentry
->param
, '?', 0);
1461 if (ct
->c_cesizefnx
)
1462 partsize
= (*ct
->c_cesizefnx
) (ct
);
1464 partsize
= ct
->c_end
- ct
->c_begin
;
1466 if (ct
->c_file
&& *ct
->c_file
) {
1467 message
= strtol(ct
->c_file
, &endp
, 10);
1468 if (*endp
) message
= 0;
1473 /* make the part's hidden aspect available by overloading the
1474 * %(unseen) function. make the part's size available via %(size).
1475 * see comments in h/fmt_scan.h.
1479 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1481 fputs(charstring_buffer (outbuf
), stdout
);
1482 charstring_free (outbuf
);
1488 * Reset (and free) any of the saved marker text
1492 free_markercomps(void)
1494 struct param_comp_list
*pc_entry
, *pc2
;
1499 description_comp
= NULL
;
1502 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1503 free(pc_entry
->param
);
1504 pc2
= pc_entry
->next
;
1509 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1510 free(pc_entry
->param
);
1511 pc2
= pc_entry
->next
;
1518 * Exit if the display process returned with a nonzero exit code, or terminated
1519 * with a SIGQUIT signal.
1523 pidcheck (int status
)
1525 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)