]>
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/trimcpy.h"
10 #include "sbr/check_charset.h"
11 #include "sbr/getcpy.h"
12 #include "sbr/context_find.h"
13 #include "sbr/pidstatus.h"
14 #include "sbr/arglist.h"
15 #include "sbr/error.h"
17 #include "h/signals.h"
22 #include "h/mhparse.h"
23 #include "h/fmt_scan.h"
27 #include "mhshowsbr.h"
28 #include "sbr/m_mktemp.h"
31 #endif /* ! HAVE_ICONV */
39 /* flags for moreproc/header display */
43 /* for output markers and headers */
53 static void show_single_message (CT
, char *, int, int, int, struct format
*);
54 static void DisplayMsgHeader (CT
, char *, int);
55 static int show_switch (CT
, int, int, int, int, struct format
*);
56 static int show_content (CT
, int, int, int, struct format
*);
57 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
58 static int show_text (CT
, int, int, struct format
*);
59 static int show_multi (CT
, int, int, int, int, struct format
*);
60 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
61 static int show_multi_aux (CT
, int, char *, struct format
*);
62 static int show_message_rfc822 (CT
, int, struct format
*);
63 static int show_partial (CT
, int);
64 static int show_external (CT
, int, int, int, int, struct format
*);
65 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
66 size_t, int multipart
);
67 static int convert_content_charset (CT
, char **);
68 static struct format
*compile_header(char *);
69 static struct format
*compile_marker(char *);
70 static void output_header (CT
, struct format
*);
71 static void output_marker (CT
, struct format
*, int);
72 static void free_markercomps (void);
73 static int pidcheck(int);
76 * Components (and list of parameters/components) we care about for the
77 * content marker display.
80 static struct comp
*folder_comp
= NULL
;
81 static struct comp
*part_comp
= NULL
;
82 static struct comp
*ctype_comp
= NULL
;
83 static struct comp
*description_comp
= NULL
;
84 static struct comp
*dispo_comp
= NULL
;
86 struct param_comp_list
{
89 struct param_comp_list
*next
;
92 static struct param_comp_list
*ctype_pc_list
= NULL
;
93 static struct param_comp_list
*dispo_pc_list
= NULL
;
97 * Top level entry point to show/display a group of messages
101 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
104 struct format
*hfmt
, *mfmt
;
107 * If form is not specified, then get default form
108 * for showing headers of MIME messages.
111 formsw
= mh_xstrdup(etcpath("mhl.headers"));
114 * Compile the content marker and header format lines
116 mfmt
= compile_marker(markerform
);
117 hfmt
= compile_header(headerform
);
120 * If form is "mhl.null", suppress display of header.
122 if (!strcmp (formsw
, "mhl.null"))
125 for (ctp
= cts
; *ctp
; ctp
++) {
128 /* if top-level type is ok, then display message */
129 if (type_ok (ct
, 1)) {
130 if (headersw
) output_header(ct
, hfmt
);
132 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
144 * Entry point to show/display a single message
148 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
149 int inlineonly
, struct format
*fmt
)
155 /* Allow user executable bit so that temporary directories created by
156 * the viewer (e.g., lynx) are going to be accessible */
157 umask (ct
->c_umask
& ~(0100));
160 * If you have a format file, then display
161 * the message headers.
164 DisplayMsgHeader(ct
, form
, concatsw
);
166 /* Show the body of the message */
167 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
173 if (ct
->c_ceclosefnx
)
174 (*ct
->c_ceclosefnx
) (ct
);
176 /* block a few signals */
178 sigaddset (&set
, SIGHUP
);
179 sigaddset (&set
, SIGINT
);
180 sigaddset (&set
, SIGQUIT
);
181 sigaddset (&set
, SIGTERM
);
182 sigprocmask (SIG_BLOCK
, &set
, &oset
);
184 while (!concatsw
&& wait (&status
) != NOTOK
) {
189 /* reset the signal mask */
190 sigprocmask (SIG_SETMASK
, &oset
, &set
);
197 * Use the mhlproc to show the header fields
201 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
208 vec
= argsplit(mhlproc
, &file
, &vecp
);
209 vec
[vecp
++] = mh_xstrdup("-form");
210 vec
[vecp
++] = mh_xstrdup(form
);
211 vec
[vecp
++] = mh_xstrdup("-nobody");
212 vec
[vecp
++] = getcpy(ct
->c_file
);
215 * If we've specified -(no)moreproc,
216 * then just pass that along.
218 if (nomore
|| concatsw
) {
219 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
221 vec
[vecp
++] = mh_xstrdup("-moreproc");
222 vec
[vecp
++] = mh_xstrdup(progsw
);
231 adios ("fork", "unable to");
236 fprintf (stderr
, "unable to exec ");
242 pidcheck(pidwait(child_id
, NOTOK
));
246 arglist_free(file
, vec
);
251 * Switching routine. Call the correct routine
252 * based on content type.
256 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
259 switch (ct
->c_type
) {
261 return show_multi (ct
, alternate
, concatsw
, textonly
,
265 switch (ct
->c_subtype
) {
266 case MESSAGE_PARTIAL
:
267 return show_partial (ct
, alternate
);
269 case MESSAGE_EXTERNAL
:
270 return show_external (ct
, alternate
, concatsw
, textonly
,
274 return show_message_rfc822 (ct
, alternate
, fmt
);
277 * Treat unknown message types as equivalent to
278 * application/octet-stream for now
281 return show_content (ct
, alternate
, textonly
,
286 return show_text (ct
, alternate
, concatsw
, fmt
);
293 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
296 return 0; /* NOT REACHED */
301 * Generic method for displaying content
305 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
309 CI ci
= &ct
->c_ctinfo
;
312 * If we're here, we are not a text type. So we don't need to check
316 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
317 output_marker(ct
, fmt
, 1);
321 /* Check for invo_name-show-type[/subtype] */
322 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
323 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
325 if ((cp
= ct
->c_showproc
))
326 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
328 /* complain if we are not a part of a multipart/alternative */
330 content_error (NULL
, ct
, "don't know how to display content");
337 * Parse the display string for displaying generic content
341 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
344 int xstdin
= 0, xlist
= 0;
345 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
347 if (!ct
->c_ceopenfnx
) {
349 content_error (NULL
, ct
, "don't know how to decode content");
354 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
356 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
359 if (! strcmp(invo_name
, "mhshow") &&
360 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
361 /* This has to be done after calling c_ceopenfnx, so
362 unfortunately the type checks are necessary without
363 some code rearrangement. And to make this really ugly,
364 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
365 if (convert_content_charset (ct
, &file
) == OK
) {
366 (*ct
->c_ceclosefnx
) (ct
);
367 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
370 char *charset
= content_charset (ct
);
371 inform("unable to convert character set%s%s from %s, continuing...",
372 ct
->c_partno
? " of part " : "",
373 FENDNULL(ct
->c_partno
),
380 strncpy (buffer
, cp
, sizeof(buffer
));
384 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
385 sizeof(buffer
) - 1, 0)) {
386 inform("Buffer overflow constructing show command, continuing...");
391 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
392 fd
, xlist
, xstdin
, fmt
);
397 * Routine to actually display the content
401 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
402 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
408 if (debugsw
|| cracked
) {
411 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
414 fprintf (stderr
, " part %s", ct
->c_partno
);
416 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
418 fprintf (stderr
, " using command %s\n", buffer
);
422 output_marker(ct
, fmt
, 0);
426 * If the command is a zero-length string, just write the output on
430 if (buffer
[0] == '\0') {
431 char readbuf
[BUFSIZ
];
433 char lastchar
= '\n';
436 inform("Cannot use NULL command to display content-type "
437 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
441 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
442 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
443 advise ("putline", "fwrite");
445 lastchar
= readbuf
[cc
- 1];
449 advise("read", "while reading text content");
454 * The MIME standards allow content to not have a trailing newline.
455 * But because we are (presumably) sending this to stdout, include
456 * a newline for text content if the final character was not a
457 * newline. Only do this for mhshow.
460 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
461 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
470 vec
= argsplit(buffer
, &file
, &vecp
);
478 advise ("fork", "unable to");
479 (*ct
->c_ceclosefnx
) (ct
);
484 if (chdir (cracked
) < 0) {
485 advise (cracked
, "chdir");
492 fprintf (stderr
, "unable to exec ");
499 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
500 /* Copy the real display program name. This relies on the
501 specific construction of vec[] by argsplit(). */
505 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
507 arglist_free(file
, vec
);
509 (*ct
->c_ceclosefnx
) (ct
);
510 return alternate
? OK
: status
;
517 * show content of type "text"
521 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
523 char *cp
, buffer
[BUFSIZ
];
524 CI ci
= &ct
->c_ctinfo
;
526 /* Check for invo_name-show-type[/subtype] */
527 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
528 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
531 * Use default method if content is text/plain, or if
532 * if it is not a text part of a multipart/alternative
534 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
537 snprintf(buffer
, sizeof(buffer
), "%%lcat");
539 snprintf(buffer
, sizeof(buffer
), "%%l");
541 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
542 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
543 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
544 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
552 * show message body of type "multipart"
556 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
560 CI ci
= &ct
->c_ctinfo
;
562 /* Check for invo_name-show-type[/subtype] */
563 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
564 return show_multi_aux (ct
, alternate
, cp
, fmt
);
566 if ((cp
= ct
->c_showproc
)) {
567 return show_multi_aux (ct
, alternate
, cp
, fmt
);
571 * Use default method to display this multipart content. Even
572 * unknown types are displayable, since they're treated as mixed
575 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
581 * show message body of subtypes of multipart that
582 * we understand directly (mixed, alternate, etc...)
586 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
587 int inlineonly
, struct format
*fmt
)
589 int alternating
, nowalternate
, result
;
590 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
592 bool request_matched
;
593 bool display_success
;
599 nowalternate
= alternate
;
601 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
607 * alternate -> we are a part inside a multipart/alternative
608 * alternating -> we are a multipart/alternative
612 request_matched
= false;
613 display_success
= false;
614 mult_alt_done
= false;
616 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
619 /* while looking for the right displayable alternative, we
620 * use a looser search criterion than we do after finding it.
621 * specifically, while still looking, part_ok() will match
622 * "parent" parts (e.g. "-part 2" where 2 is a high-level
623 * multipart). after finding it, we use part_exact() to only
624 * choose a part that was requested explicitly.
626 if ((part_exact(p
) && type_ok(p
, 1)) ||
627 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
631 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
633 switch (inneresult
) {
634 case NOTOK
: /* hard display error */
635 request_matched
= true;
636 if (alternate
&& !alternating
) {
642 case DONE
: /* found no match on content type */
645 case OK
: /* display successful */
646 request_matched
= true;
647 display_success
= true;
650 /* if we got success on a sub-part of
651 * multipart/alternative, we're done, unless
652 * there's a chance an explicit part should be
653 * matched later in the alternatives. */
655 mult_alt_done
= true;
656 } else if (alternate
) {
657 alternate
= nowalternate
= 0;
665 /* we're supposed to be displaying at least something from a
666 * multipart/alternative. if we've had parts to consider, and
667 * we've had no success, then we should complain. we shouldn't
668 * complain if none of the parts matched any -part or -type option.
670 if (alternating
&& request_matched
&& !display_success
) {
671 /* if we're ourselves an alternate. don't complain yet. */
673 content_error (NULL
, ct
, "don't know how to display any of the contents");
678 /* if no parts matched what was requested, there can't have been
679 * any display errors. we report DONE rather than OK. */
680 ret
= request_matched
? result
: DONE
;
686 * Parse display string for multipart content
687 * and use external program to display it.
691 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
693 /* xstdin is only used in the call to parse_display_string():
694 its value is ignored in the function. */
695 int xstdin
= 0, xlist
= 0;
696 char *file
= NULL
, buffer
[BUFSIZ
];
697 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
701 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
704 if (!p
->c_ceopenfnx
) {
706 content_error (NULL
, p
, "don't know how to decode content");
710 if (p
->c_storage
== NULL
) {
711 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
714 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
716 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
718 (*p
->c_ceclosefnx
) (p
);
722 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
723 buffer
, sizeof(buffer
) - 1, 1)) {
724 inform("Buffer overflow constructing show command, continuing...");
728 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
733 * show content of type "message/rfc822"
737 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
740 CI ci
= &ct
->c_ctinfo
;
742 /* Check for invo_name-show-type[/subtype] */
743 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
744 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
746 if ((cp
= ct
->c_showproc
))
747 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
749 /* default method for message/rfc822 */
750 if (ct
->c_subtype
== MESSAGE_RFC822
) {
751 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
752 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
755 /* complain if we are not a part of a multipart/alternative */
757 content_error (NULL
, ct
, "don't know how to display content");
764 * Show content of type "message/partial".
768 show_partial (CT ct
, int alternate
)
770 NMH_UNUSED (alternate
);
772 content_error (NULL
, ct
,
773 "in order to display this message, you must reassemble it");
779 * Show content of type "message/external".
781 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
785 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
788 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
789 CT p
= e
->eb_content
;
794 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
799 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
800 char *file
, char *buffer
, size_t buflen
,
805 char *bp
= buffer
, *pp
;
806 CI ci
= &ct
->c_ctinfo
;
808 bp
[0] = bp
[buflen
] = '\0';
810 for ( ; *cp
&& buflen
> 0; cp
++) {
816 /* insert parameters from Content-Type field */
821 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
822 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
823 get_param_value(pm
, '?'));
833 /* insert content description */
837 s
= trimcpy (ct
->c_descr
);
838 strncpy (bp
, s
, buflen
);
844 /* no longer implemented */
848 /* %f, and stdin is terminal not content */
854 /* insert filename(s) containing content */
855 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
860 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
863 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
870 /* insert filename containing content */
871 snprintf (bp
, buflen
, "%s", file
);
874 * Old comments below are left here for posterity.
875 * This was/is tricky.
877 /* since we've quoted the file argument, set things up
878 * to look past it, to avoid problems with the quoting
879 * logic below. (I know, I should figure out what's
880 * broken with the quoting logic, but..)
883 * Here's the email that submitted the patch with
885 * https://www.mail-archive.com/nmh-workers@mhost.com/
887 * I can't tell from that exactly what was broken,
888 * beyond misquoting of the filename. The profile
889 * had appearances of %F both with and without quotes.
890 * The unquoted ones should have been quoted by the
892 * The fix was to always quote the filename. But
893 * that broke '%F' because it expanded to ''filename''.
896 * Old comments above are left here for posterity.
897 * The quoting below should work properly now.
903 /* No longer supported */
907 /* display listing prior to displaying content */
912 /* insert subtype of content */
913 strncpy (bp
, ci
->ci_subtype
, buflen
);
917 /* insert character % */
921 const char *closing_brace
= strchr(cp
, '}');
924 const size_t param_len
= closing_brace
- cp
- 1;
925 char *param
= mh_xmalloc(param_len
+ 1);
928 (void) strncpy(param
, cp
+ 1, param_len
);
929 param
[param_len
] = '\0';
930 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
933 cp
+= param_len
+ 1; /* Skip both braces, too. */
936 /* %{param} is set in the Content-Type header.
937 After the break below, quote it if necessary. */
938 (void) strncpy(bp
, value
, buflen
);
941 /* %{param} not found, so skip it completely. cp
942 was advanced above. */
946 /* This will get confused if there are multiple %{}'s,
947 but its real purpose is to avoid doing bad things
948 above if a closing brace wasn't found. */
949 inform("no closing brace for display string escape %s, continuing...",
966 /* Did we actually insert something? */
968 /* Insert single quote if not inside quotes already */
969 if (!quoted
&& buflen
) {
971 memmove (pp
+ 1, pp
, len
+1);
977 /* Escape existing quotes */
978 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
981 /* Quoted. Let this quote close that quoting.
982 Insert an escaped quote to replace it and
983 another quote to reopen quoting, which will be
985 memmove (pp
+ 2, pp
, len
);
992 /* Not quoted. This should not be reached with
993 the current code, but handle the condition
994 in case the code changes. Just escape the
996 memmove (pp
, pp
-1, len
+1);
1002 /* If pp is still set, that means we ran out of space. */
1005 /* Close quoting. */
1006 if (quoted
&& buflen
) {
1007 /* See if we need to close the quote by looking
1008 for an odd number of unescaped close quotes in
1009 the remainder of the display string. */
1010 int found_quote
= 0, escaped
= 0;
1013 for (c
= cp
+1; *c
; ++c
) {
1015 escaped
= ! escaped
;
1021 found_quote
= ! found_quote
;
1026 if (! found_quote
) {
1046 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1047 /* content_error would provide a more useful error message
1048 * here, except that if we got overrun, it probably would
1054 /* use charset string to modify display method */
1055 if (ct
->c_termproc
) {
1058 strncpy (term
, buffer
, sizeof(term
));
1059 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1067 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1069 char *src_charset
= content_charset (ct
);
1072 if (strcasecmp (src_charset
, dest_charset
)) {
1074 iconv_t conv_desc
= NULL
;
1081 bool opened_input_file
= false;
1082 char src_buffer
[BUFSIZ
];
1083 size_t dest_buffer_size
= BUFSIZ
;
1084 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1087 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1089 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1091 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1096 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1097 die("unable to create temporary file in %s",
1100 dest
= mh_xstrdup(tempfile
);
1102 if (ct
->c_cefile
.ce_file
) {
1103 file
= &ct
->c_cefile
.ce_file
;
1104 fp
= &ct
->c_cefile
.ce_fp
;
1106 } else if (ct
->c_file
) {
1109 begin
= (size_t) ct
->c_begin
;
1110 end
= (size_t) ct
->c_end
;
1111 } /* else no input file: shouldn't happen */
1113 if (file
&& *file
&& fp
) {
1115 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1116 advise (*file
, "unable to open for reading");
1119 opened_input_file
= true;
1126 size_t bytes_to_read
=
1127 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1129 fseeko (*fp
, begin
, SEEK_SET
);
1130 while ((inbytes
= fread (src_buffer
, 1,
1131 min (bytes_to_read
, sizeof src_buffer
),
1133 ICONV_CONST
char *ib
= src_buffer
;
1134 char *ob
= dest_buffer
;
1135 size_t outbytes
= dest_buffer_size
;
1136 size_t outbytes_before
= outbytes
;
1138 if (end
> 0) bytes_to_read
-= inbytes
;
1141 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1143 if (errno
== E2BIG
) {
1145 * Bump up the buffer by at least a factor of 2
1146 * over what we need.
1148 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1149 dest_buffer_size
+= bumpup
;
1150 dest_buffer
= mh_xrealloc(dest_buffer
,
1152 ob
= dest_buffer
+ ob_off
;
1154 outbytes_before
+= bumpup
;
1157 if (errno
== EINVAL
) {
1158 /* middle of multi-byte sequence */
1159 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1160 advise (dest
, "write");
1162 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1163 if (end
> 0) bytes_to_read
+= inbytes
;
1164 /* inform("convert_charset: EINVAL"); */
1167 if (errno
== EILSEQ
) {
1168 /* invalid multi-byte sequence */
1170 for (++ib
, --inbytes
;
1172 (((unsigned char) *ib
) & 0xc0) == 0x80;
1176 ib
++; inbytes
--; /* skip it */
1178 (*ob
++) = '?'; outbytes
--;
1179 /* inform("convert_charset: EILSEQ"); */
1182 inform("convert_charset: errno = %d", errno
);
1187 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1188 advise (dest
, "write");
1192 if (opened_input_file
) {
1198 iconv_close (conv_desc
);
1202 /* Replace the decoded file with the converted one. */
1203 if (ct
->c_cefile
.ce_file
) {
1204 if (ct
->c_cefile
.ce_unlink
) {
1205 (void) m_unlink (ct
->c_cefile
.ce_file
);
1207 free (ct
->c_cefile
.ce_file
);
1209 ct
->c_cefile
.ce_file
= dest
;
1210 ct
->c_cefile
.ce_unlink
= 1;
1214 /* Update ct->c_ctline. */
1216 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1217 ct
->c_ctinfo
.ci_subtype
, NULL
);
1220 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1221 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1223 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1224 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1226 ctline
= add(outline
, ctline
);
1230 free (ct
->c_ctline
);
1231 ct
->c_ctline
= ctline
;
1232 } /* else no CT line, which is odd */
1234 /* Update Content-Type header field. */
1235 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1236 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1237 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1245 (void) m_unlink (dest
);
1248 #else /* ! HAVE_ICONV */
1249 NMH_UNUSED (message_mods
);
1251 inform("Can't convert %s to %s without iconv", src_charset
,
1255 #endif /* ! HAVE_ICONV */
1264 convert_content_charset (CT ct
, char **file
)
1269 /* Using current locale, see if the content needs to be converted. */
1271 /* content_charset() cannot return NULL. */
1272 char *src_charset
= content_charset (ct
);
1274 if (! check_charset (src_charset
, strlen (src_charset
))) {
1277 char *dest_charset
= getcpy (get_charset ());
1279 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1280 *file
= ct
->c_cefile
.ce_file
;
1285 free (dest_charset
);
1288 #else /* ! HAVE_ICONV */
1291 #endif /* ! HAVE_ICONV */
1297 * Compile our format string and save any parameters we care about.
1300 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1301 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1302 "%<{description}%{description}" \
1303 "%?{cdispo-filename}%{cdispo-filename}" \
1304 "%|%{ctype-name}%> " \
1305 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1307 static struct format
*
1308 compile_header(char *form
)
1312 struct comp
*comp
= NULL
;
1313 unsigned int bucket
;
1315 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1317 (void) fmt_compile(fmtstring
, &fmt
, 1);
1320 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1321 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1329 static struct format
*
1330 compile_marker(char *form
)
1334 struct comp
*comp
= NULL
;
1335 unsigned int bucket
;
1336 struct param_comp_list
*pc_entry
;
1338 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1340 (void) fmt_compile(fmtstring
, &fmt
, 1);
1344 * Things we care about:
1346 * part - Part name (e.g., 1.1)
1347 * content-type - Content-Type
1348 * description - Content-Description
1349 * disposition - Content-Disposition (inline, attachment)
1350 * ctype-<param> - Content-Type parameter
1351 * cdispo-<param> - Content-Disposition parameter
1354 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1355 if (strcasecmp(comp
->c_name
, "part") == 0) {
1357 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1359 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1360 description_comp
= comp
;
1361 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1363 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1364 strlen(comp
->c_name
) > 6) {
1366 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1367 pc_entry
->comp
= comp
;
1368 pc_entry
->next
= ctype_pc_list
;
1369 ctype_pc_list
= pc_entry
;
1370 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1371 strlen(comp
->c_name
) > 7) {
1373 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1374 pc_entry
->comp
= comp
;
1375 pc_entry
->next
= dispo_pc_list
;
1376 dispo_pc_list
= pc_entry
;
1384 * Output on stdout an appropriate marker for this content, using mh-format
1388 output_header(CT ct
, struct format
*fmt
)
1390 charstring_t outbuf
= charstring_create (BUFSIZ
);
1396 folder_comp
->c_text
= getcpy(folder
);
1398 if (ct
->c_file
&& *ct
->c_file
) {
1399 message
= strtol(ct
->c_file
, &endp
, 10);
1400 if (*endp
) message
= 0;
1404 /* it would be nice to populate dat[2], for %(size) here,
1405 * but it's not available. it might also be nice to know
1406 * if the message originally had any mime parts or not -- but
1407 * there's also no record of that. (except for MIME-version:)
1410 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1412 fputs(charstring_buffer (outbuf
), stdout
);
1413 charstring_free (outbuf
);
1419 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1421 charstring_t outbuf
= charstring_create (BUFSIZ
);
1422 struct param_comp_list
*pcentry
;
1429 * Grab any items we care about.
1432 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1433 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1434 ct
->c_ctinfo
.ci_subtype
, NULL
);
1437 if (part_comp
&& ct
->c_partno
) {
1438 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1441 if (description_comp
&& ct
->c_descr
) {
1442 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1445 if (dispo_comp
&& ct
->c_dispo_type
) {
1446 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1449 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1450 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1451 pcentry
->param
, '?', 0);
1454 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1455 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1456 pcentry
->param
, '?', 0);
1459 if (ct
->c_cesizefnx
)
1460 partsize
= (*ct
->c_cesizefnx
) (ct
);
1462 partsize
= ct
->c_end
- ct
->c_begin
;
1464 if (ct
->c_file
&& *ct
->c_file
) {
1465 message
= strtol(ct
->c_file
, &endp
, 10);
1466 if (*endp
) message
= 0;
1471 /* make the part's hidden aspect available by overloading the
1472 * %(unseen) function. make the part's size available via %(size).
1473 * see comments in h/fmt_scan.h.
1477 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1479 fputs(charstring_buffer (outbuf
), stdout
);
1480 charstring_free (outbuf
);
1486 * Reset (and free) any of the saved marker text
1490 free_markercomps(void)
1492 struct param_comp_list
*pc_entry
, *pc2
;
1497 description_comp
= NULL
;
1500 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1501 free(pc_entry
->param
);
1502 pc2
= pc_entry
->next
;
1507 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1508 free(pc_entry
->param
);
1509 pc2
= pc_entry
->next
;
1516 * Exit if the display process returned with a nonzero exit code, or terminated
1517 * with a SIGQUIT signal.
1521 pidcheck (int status
)
1523 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)