]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
59f6f4375af2b786a6917404639ea833b4a8d335
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/check_charset.h"
10 #include "sbr/getcpy.h"
11 #include "sbr/context_find.h"
12 #include "sbr/pidstatus.h"
13 #include "sbr/arglist.h"
14 #include "sbr/error.h"
16 #include "h/signals.h"
21 #include "h/mhparse.h"
22 #include "h/fmt_scan.h"
26 #include "mhshowsbr.h"
27 #include "sbr/m_mktemp.h"
30 #endif /* ! HAVE_ICONV */
38 /* flags for moreproc/header display */
42 /* for output markers and headers */
52 static void show_single_message (CT
, char *, int, int, int, struct format
*);
53 static void DisplayMsgHeader (CT
, char *, int);
54 static int show_switch (CT
, int, int, int, int, struct format
*);
55 static int show_content (CT
, int, int, int, struct format
*);
56 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
57 static int show_text (CT
, int, int, struct format
*);
58 static int show_multi (CT
, int, int, int, int, struct format
*);
59 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
60 static int show_multi_aux (CT
, int, char *, struct format
*);
61 static int show_message_rfc822 (CT
, int, struct format
*);
62 static int show_partial (CT
, int);
63 static int show_external (CT
, int, int, int, int, struct format
*);
64 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
65 size_t, int multipart
);
66 static int convert_content_charset (CT
, char **);
67 static struct format
*compile_header(char *);
68 static struct format
*compile_marker(char *);
69 static void output_header (CT
, struct format
*);
70 static void output_marker (CT
, struct format
*, int);
71 static void free_markercomps (void);
72 static int pidcheck(int);
75 * Components (and list of parameters/components) we care about for the
76 * content marker display.
79 static struct comp
*folder_comp
= NULL
;
80 static struct comp
*part_comp
= NULL
;
81 static struct comp
*ctype_comp
= NULL
;
82 static struct comp
*description_comp
= NULL
;
83 static struct comp
*dispo_comp
= NULL
;
85 struct param_comp_list
{
88 struct param_comp_list
*next
;
91 static struct param_comp_list
*ctype_pc_list
= NULL
;
92 static struct param_comp_list
*dispo_pc_list
= NULL
;
96 * Top level entry point to show/display a group of messages
100 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
103 struct format
*hfmt
, *mfmt
;
106 * If form is not specified, then get default form
107 * for showing headers of MIME messages.
110 formsw
= mh_xstrdup(etcpath("mhl.headers"));
113 * Compile the content marker and header format lines
115 mfmt
= compile_marker(markerform
);
116 hfmt
= compile_header(headerform
);
119 * If form is "mhl.null", suppress display of header.
121 if (!strcmp (formsw
, "mhl.null"))
124 for (ctp
= cts
; *ctp
; ctp
++) {
127 /* if top-level type is ok, then display message */
128 if (type_ok (ct
, 1)) {
129 if (headersw
) output_header(ct
, hfmt
);
131 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
143 * Entry point to show/display a single message
147 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
148 int inlineonly
, struct format
*fmt
)
154 /* Allow user executable bit so that temporary directories created by
155 * the viewer (e.g., lynx) are going to be accessible */
156 umask (ct
->c_umask
& ~(0100));
159 * If you have a format file, then display
160 * the message headers.
163 DisplayMsgHeader(ct
, form
, concatsw
);
165 /* Show the body of the message */
166 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
172 if (ct
->c_ceclosefnx
)
173 (*ct
->c_ceclosefnx
) (ct
);
175 /* block a few signals */
177 sigaddset (&set
, SIGHUP
);
178 sigaddset (&set
, SIGINT
);
179 sigaddset (&set
, SIGQUIT
);
180 sigaddset (&set
, SIGTERM
);
181 sigprocmask (SIG_BLOCK
, &set
, &oset
);
183 while (!concatsw
&& wait (&status
) != NOTOK
) {
188 /* reset the signal mask */
189 sigprocmask (SIG_SETMASK
, &oset
, &set
);
196 * Use the mhlproc to show the header fields
200 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
207 vec
= argsplit(mhlproc
, &file
, &vecp
);
208 vec
[vecp
++] = mh_xstrdup("-form");
209 vec
[vecp
++] = mh_xstrdup(form
);
210 vec
[vecp
++] = mh_xstrdup("-nobody");
211 vec
[vecp
++] = getcpy(ct
->c_file
);
214 * If we've specified -(no)moreproc,
215 * then just pass that along.
217 if (nomore
|| concatsw
) {
218 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
220 vec
[vecp
++] = mh_xstrdup("-moreproc");
221 vec
[vecp
++] = mh_xstrdup(progsw
);
230 adios ("fork", "unable to");
235 fprintf (stderr
, "unable to exec ");
241 pidcheck(pidwait(child_id
, NOTOK
));
245 arglist_free(file
, vec
);
250 * Switching routine. Call the correct routine
251 * based on content type.
255 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
258 switch (ct
->c_type
) {
260 return show_multi (ct
, alternate
, concatsw
, textonly
,
264 switch (ct
->c_subtype
) {
265 case MESSAGE_PARTIAL
:
266 return show_partial (ct
, alternate
);
268 case MESSAGE_EXTERNAL
:
269 return show_external (ct
, alternate
, concatsw
, textonly
,
273 return show_message_rfc822 (ct
, alternate
, fmt
);
276 * Treat unknown message types as equivalent to
277 * application/octet-stream for now
280 return show_content (ct
, alternate
, textonly
,
285 return show_text (ct
, alternate
, concatsw
, fmt
);
292 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
295 return 0; /* NOT REACHED */
300 * Generic method for displaying content
304 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
308 CI ci
= &ct
->c_ctinfo
;
311 * If we're here, we are not a text type. So we don't need to check
315 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
316 output_marker(ct
, fmt
, 1);
320 /* Check for invo_name-show-type[/subtype] */
321 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
322 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
324 if ((cp
= ct
->c_showproc
))
325 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
327 /* complain if we are not a part of a multipart/alternative */
329 content_error (NULL
, ct
, "don't know how to display content");
336 * Parse the display string for displaying generic content
340 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
343 int xstdin
= 0, xlist
= 0;
344 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
346 if (!ct
->c_ceopenfnx
) {
348 content_error (NULL
, ct
, "don't know how to decode content");
353 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
355 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
358 if (! strcmp(invo_name
, "mhshow") &&
359 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
360 /* This has to be done after calling c_ceopenfnx, so
361 unfortunately the type checks are necessary without
362 some code rearrangement. And to make this really ugly,
363 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
364 if (convert_content_charset (ct
, &file
) == OK
) {
365 (*ct
->c_ceclosefnx
) (ct
);
366 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
369 char *charset
= content_charset (ct
);
370 inform("unable to convert character set%s%s from %s, continuing...",
371 ct
->c_partno
? " of part " : "",
372 FENDNULL(ct
->c_partno
),
379 strncpy (buffer
, cp
, sizeof(buffer
));
383 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
384 sizeof(buffer
) - 1, 0)) {
385 inform("Buffer overflow constructing show command, continuing...");
390 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
391 fd
, xlist
, xstdin
, fmt
);
396 * Routine to actually display the content
400 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
401 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
407 if (debugsw
|| cracked
) {
410 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
413 fprintf (stderr
, " part %s", ct
->c_partno
);
415 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
417 fprintf (stderr
, " using command %s\n", buffer
);
421 output_marker(ct
, fmt
, 0);
425 * If the command is a zero-length string, just write the output on
429 if (buffer
[0] == '\0') {
430 char readbuf
[BUFSIZ
];
432 char lastchar
= '\n';
435 inform("Cannot use NULL command to display content-type "
436 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
440 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
441 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
442 advise ("putline", "fwrite");
444 lastchar
= readbuf
[cc
- 1];
448 advise("read", "while reading text content");
453 * The MIME standards allow content to not have a trailing newline.
454 * But because we are (presumably) sending this to stdout, include
455 * a newline for text content if the final character was not a
456 * newline. Only do this for mhshow.
459 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
460 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
469 vec
= argsplit(buffer
, &file
, &vecp
);
477 advise ("fork", "unable to");
478 (*ct
->c_ceclosefnx
) (ct
);
483 if (chdir (cracked
) < 0) {
484 advise (cracked
, "chdir");
491 fprintf (stderr
, "unable to exec ");
498 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
499 /* Copy the real display program name. This relies on the
500 specific construction of vec[] by argsplit(). */
504 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
506 arglist_free(file
, vec
);
508 (*ct
->c_ceclosefnx
) (ct
);
509 return alternate
? OK
: status
;
516 * show content of type "text"
520 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
522 char *cp
, buffer
[BUFSIZ
];
523 CI ci
= &ct
->c_ctinfo
;
525 /* Check for invo_name-show-type[/subtype] */
526 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
527 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
530 * Use default method if content is text/plain, or if
531 * if it is not a text part of a multipart/alternative
533 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
536 snprintf(buffer
, sizeof(buffer
), "%%lcat");
538 snprintf(buffer
, sizeof(buffer
), "%%l");
540 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
541 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
542 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
543 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
551 * show message body of type "multipart"
555 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
559 CI ci
= &ct
->c_ctinfo
;
561 /* Check for invo_name-show-type[/subtype] */
562 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
563 return show_multi_aux (ct
, alternate
, cp
, fmt
);
565 if ((cp
= ct
->c_showproc
)) {
566 return show_multi_aux (ct
, alternate
, cp
, fmt
);
570 * Use default method to display this multipart content. Even
571 * unknown types are displayable, since they're treated as mixed
574 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
580 * show message body of subtypes of multipart that
581 * we understand directly (mixed, alternate, etc...)
585 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
586 int inlineonly
, struct format
*fmt
)
588 int alternating
, nowalternate
, result
;
589 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
591 bool request_matched
;
592 bool display_success
;
598 nowalternate
= alternate
;
600 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
606 * alternate -> we are a part inside a multipart/alternative
607 * alternating -> we are a multipart/alternative
611 request_matched
= false;
612 display_success
= false;
613 mult_alt_done
= false;
615 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
618 /* while looking for the right displayable alternative, we
619 * use a looser search criterion than we do after finding it.
620 * specifically, while still looking, part_ok() will match
621 * "parent" parts (e.g. "-part 2" where 2 is a high-level
622 * multipart). after finding it, we use part_exact() to only
623 * choose a part that was requested explicitly.
625 if ((part_exact(p
) && type_ok(p
, 1)) ||
626 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
630 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
632 switch (inneresult
) {
633 case NOTOK
: /* hard display error */
634 request_matched
= true;
635 if (alternate
&& !alternating
) {
641 case DONE
: /* found no match on content type */
644 case OK
: /* display successful */
645 request_matched
= true;
646 display_success
= true;
649 /* if we got success on a sub-part of
650 * multipart/alternative, we're done, unless
651 * there's a chance an explicit part should be
652 * matched later in the alternatives. */
654 mult_alt_done
= true;
655 } else if (alternate
) {
656 alternate
= nowalternate
= 0;
664 /* we're supposed to be displaying at least something from a
665 * multipart/alternative. if we've had parts to consider, and
666 * we've had no success, then we should complain. we shouldn't
667 * complain if none of the parts matched any -part or -type option.
669 if (alternating
&& request_matched
&& !display_success
) {
670 /* if we're ourselves an alternate. don't complain yet. */
672 content_error (NULL
, ct
, "don't know how to display any of the contents");
677 /* if no parts matched what was requested, there can't have been
678 * any display errors. we report DONE rather than OK. */
679 ret
= request_matched
? result
: DONE
;
685 * Parse display string for multipart content
686 * and use external program to display it.
690 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
692 /* xstdin is only used in the call to parse_display_string():
693 its value is ignored in the function. */
694 int xstdin
= 0, xlist
= 0;
695 char *file
= NULL
, buffer
[BUFSIZ
];
696 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
700 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
703 if (!p
->c_ceopenfnx
) {
705 content_error (NULL
, p
, "don't know how to decode content");
709 if (p
->c_storage
== NULL
) {
710 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
713 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
715 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
717 (*p
->c_ceclosefnx
) (p
);
721 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
722 buffer
, sizeof(buffer
) - 1, 1)) {
723 inform("Buffer overflow constructing show command, continuing...");
727 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
732 * show content of type "message/rfc822"
736 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
739 CI ci
= &ct
->c_ctinfo
;
741 /* Check for invo_name-show-type[/subtype] */
742 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
743 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
745 if ((cp
= ct
->c_showproc
))
746 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
748 /* default method for message/rfc822 */
749 if (ct
->c_subtype
== MESSAGE_RFC822
) {
750 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
751 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
754 /* complain if we are not a part of a multipart/alternative */
756 content_error (NULL
, ct
, "don't know how to display content");
763 * Show content of type "message/partial".
767 show_partial (CT ct
, int alternate
)
769 NMH_UNUSED (alternate
);
771 content_error (NULL
, ct
,
772 "in order to display this message, you must reassemble it");
778 * Show content of type "message/external".
780 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
784 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
787 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
788 CT p
= e
->eb_content
;
793 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
798 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
799 char *file
, char *buffer
, size_t buflen
,
804 char *bp
= buffer
, *pp
;
805 CI ci
= &ct
->c_ctinfo
;
807 bp
[0] = bp
[buflen
] = '\0';
809 for ( ; *cp
&& buflen
> 0; cp
++) {
815 /* insert parameters from Content-Type field */
820 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
821 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
822 get_param_value(pm
, '?'));
832 /* insert content description */
836 s
= trimcpy (ct
->c_descr
);
837 strncpy (bp
, s
, buflen
);
843 /* no longer implemented */
847 /* %f, and stdin is terminal not content */
853 /* insert filename(s) containing content */
854 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
859 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
862 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
869 /* insert filename containing content */
870 snprintf (bp
, buflen
, "%s", file
);
873 * Old comments below are left here for posterity.
874 * This was/is tricky.
876 /* since we've quoted the file argument, set things up
877 * to look past it, to avoid problems with the quoting
878 * logic below. (I know, I should figure out what's
879 * broken with the quoting logic, but..)
882 * Here's the email that submitted the patch with
884 * https://www.mail-archive.com/nmh-workers@mhost.com/
886 * I can't tell from that exactly what was broken,
887 * beyond misquoting of the filename. The profile
888 * had appearances of %F both with and without quotes.
889 * The unquoted ones should have been quoted by the
891 * The fix was to always quote the filename. But
892 * that broke '%F' because it expanded to ''filename''.
895 * Old comments above are left here for posterity.
896 * The quoting below should work properly now.
902 /* No longer supported */
906 /* display listing prior to displaying content */
911 /* insert subtype of content */
912 strncpy (bp
, ci
->ci_subtype
, buflen
);
916 /* insert character % */
920 const char *closing_brace
= strchr(cp
, '}');
923 const size_t param_len
= closing_brace
- cp
- 1;
924 char *param
= mh_xmalloc(param_len
+ 1);
927 (void) strncpy(param
, cp
+ 1, param_len
);
928 param
[param_len
] = '\0';
929 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
932 cp
+= param_len
+ 1; /* Skip both braces, too. */
935 /* %{param} is set in the Content-Type header.
936 After the break below, quote it if necessary. */
937 (void) strncpy(bp
, value
, buflen
);
940 /* %{param} not found, so skip it completely. cp
941 was advanced above. */
945 /* This will get confused if there are multiple %{}'s,
946 but its real purpose is to avoid doing bad things
947 above if a closing brace wasn't found. */
948 inform("no closing brace for display string escape %s, continuing...",
965 /* Did we actually insert something? */
967 /* Insert single quote if not inside quotes already */
968 if (!quoted
&& buflen
) {
970 memmove (pp
+ 1, pp
, len
+1);
976 /* Escape existing quotes */
977 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
980 /* Quoted. Let this quote close that quoting.
981 Insert an escaped quote to replace it and
982 another quote to reopen quoting, which will be
984 memmove (pp
+ 2, pp
, len
);
991 /* Not quoted. This should not be reached with
992 the current code, but handle the condition
993 in case the code changes. Just escape the
995 memmove (pp
, pp
-1, len
+1);
1001 /* If pp is still set, that means we ran out of space. */
1004 /* Close quoting. */
1005 if (quoted
&& buflen
) {
1006 /* See if we need to close the quote by looking
1007 for an odd number of unescaped close quotes in
1008 the remainder of the display string. */
1009 int found_quote
= 0, escaped
= 0;
1012 for (c
= cp
+1; *c
; ++c
) {
1014 escaped
= ! escaped
;
1020 found_quote
= ! found_quote
;
1025 if (! found_quote
) {
1045 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1046 /* content_error would provide a more useful error message
1047 * here, except that if we got overrun, it probably would
1053 /* use charset string to modify display method */
1054 if (ct
->c_termproc
) {
1057 strncpy (term
, buffer
, sizeof(term
));
1058 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1066 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1068 char *src_charset
= content_charset (ct
);
1071 if (strcasecmp (src_charset
, dest_charset
)) {
1073 iconv_t conv_desc
= NULL
;
1080 bool opened_input_file
= false;
1081 char src_buffer
[BUFSIZ
];
1082 size_t dest_buffer_size
= BUFSIZ
;
1083 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1086 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1088 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1090 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1095 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1096 die("unable to create temporary file in %s",
1099 dest
= mh_xstrdup(tempfile
);
1101 if (ct
->c_cefile
.ce_file
) {
1102 file
= &ct
->c_cefile
.ce_file
;
1103 fp
= &ct
->c_cefile
.ce_fp
;
1105 } else if (ct
->c_file
) {
1108 begin
= (size_t) ct
->c_begin
;
1109 end
= (size_t) ct
->c_end
;
1110 } /* else no input file: shouldn't happen */
1112 if (file
&& *file
&& fp
) {
1114 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1115 advise (*file
, "unable to open for reading");
1118 opened_input_file
= true;
1125 size_t bytes_to_read
=
1126 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1128 fseeko (*fp
, begin
, SEEK_SET
);
1129 while ((inbytes
= fread (src_buffer
, 1,
1130 min (bytes_to_read
, sizeof src_buffer
),
1132 ICONV_CONST
char *ib
= src_buffer
;
1133 char *ob
= dest_buffer
;
1134 size_t outbytes
= dest_buffer_size
;
1135 size_t outbytes_before
= outbytes
;
1137 if (end
> 0) bytes_to_read
-= inbytes
;
1140 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1142 if (errno
== E2BIG
) {
1144 * Bump up the buffer by at least a factor of 2
1145 * over what we need.
1147 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1148 dest_buffer_size
+= bumpup
;
1149 dest_buffer
= mh_xrealloc(dest_buffer
,
1151 ob
= dest_buffer
+ ob_off
;
1153 outbytes_before
+= bumpup
;
1156 if (errno
== EINVAL
) {
1157 /* middle of multi-byte sequence */
1158 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1159 advise (dest
, "write");
1161 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1162 if (end
> 0) bytes_to_read
+= inbytes
;
1163 /* inform("convert_charset: EINVAL"); */
1166 if (errno
== EILSEQ
) {
1167 /* invalid multi-byte sequence */
1169 for (++ib
, --inbytes
;
1171 (((unsigned char) *ib
) & 0xc0) == 0x80;
1175 ib
++; inbytes
--; /* skip it */
1177 (*ob
++) = '?'; outbytes
--;
1178 /* inform("convert_charset: EILSEQ"); */
1181 inform("convert_charset: errno = %d", errno
);
1186 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1187 advise (dest
, "write");
1191 if (opened_input_file
) {
1197 iconv_close (conv_desc
);
1201 /* Replace the decoded file with the converted one. */
1202 if (ct
->c_cefile
.ce_file
) {
1203 if (ct
->c_cefile
.ce_unlink
) {
1204 (void) m_unlink (ct
->c_cefile
.ce_file
);
1206 free (ct
->c_cefile
.ce_file
);
1208 ct
->c_cefile
.ce_file
= dest
;
1209 ct
->c_cefile
.ce_unlink
= 1;
1213 /* Update ct->c_ctline. */
1215 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1216 ct
->c_ctinfo
.ci_subtype
, NULL
);
1219 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1220 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1222 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1223 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1225 ctline
= add(outline
, ctline
);
1229 free (ct
->c_ctline
);
1230 ct
->c_ctline
= ctline
;
1231 } /* else no CT line, which is odd */
1233 /* Update Content-Type header field. */
1234 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1235 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1236 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1244 (void) m_unlink (dest
);
1247 #else /* ! HAVE_ICONV */
1248 NMH_UNUSED (message_mods
);
1250 inform("Can't convert %s to %s without iconv", src_charset
,
1254 #endif /* ! HAVE_ICONV */
1263 convert_content_charset (CT ct
, char **file
)
1268 /* Using current locale, see if the content needs to be converted. */
1270 /* content_charset() cannot return NULL. */
1271 char *src_charset
= content_charset (ct
);
1273 if (! check_charset (src_charset
, strlen (src_charset
))) {
1276 char *dest_charset
= getcpy (get_charset ());
1278 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1279 *file
= ct
->c_cefile
.ce_file
;
1284 free (dest_charset
);
1287 #else /* ! HAVE_ICONV */
1290 #endif /* ! HAVE_ICONV */
1296 * Compile our format string and save any parameters we care about.
1299 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1300 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1301 "%<{description}%{description}" \
1302 "%?{cdispo-filename}%{cdispo-filename}" \
1303 "%|%{ctype-name}%> " \
1304 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1306 static struct format
*
1307 compile_header(char *form
)
1311 struct comp
*comp
= NULL
;
1312 unsigned int bucket
;
1314 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1316 (void) fmt_compile(fmtstring
, &fmt
, 1);
1319 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1320 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1328 static struct format
*
1329 compile_marker(char *form
)
1333 struct comp
*comp
= NULL
;
1334 unsigned int bucket
;
1335 struct param_comp_list
*pc_entry
;
1337 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1339 (void) fmt_compile(fmtstring
, &fmt
, 1);
1343 * Things we care about:
1345 * part - Part name (e.g., 1.1)
1346 * content-type - Content-Type
1347 * description - Content-Description
1348 * disposition - Content-Disposition (inline, attachment)
1349 * ctype-<param> - Content-Type parameter
1350 * cdispo-<param> - Content-Disposition parameter
1353 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1354 if (strcasecmp(comp
->c_name
, "part") == 0) {
1356 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1358 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1359 description_comp
= comp
;
1360 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1362 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1363 strlen(comp
->c_name
) > 6) {
1365 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1366 pc_entry
->comp
= comp
;
1367 pc_entry
->next
= ctype_pc_list
;
1368 ctype_pc_list
= pc_entry
;
1369 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1370 strlen(comp
->c_name
) > 7) {
1372 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1373 pc_entry
->comp
= comp
;
1374 pc_entry
->next
= dispo_pc_list
;
1375 dispo_pc_list
= pc_entry
;
1383 * Output on stdout an appropriate marker for this content, using mh-format
1387 output_header(CT ct
, struct format
*fmt
)
1389 charstring_t outbuf
= charstring_create (BUFSIZ
);
1395 folder_comp
->c_text
= getcpy(folder
);
1397 if (ct
->c_file
&& *ct
->c_file
) {
1398 message
= strtol(ct
->c_file
, &endp
, 10);
1399 if (*endp
) message
= 0;
1403 /* it would be nice to populate dat[2], for %(size) here,
1404 * but it's not available. it might also be nice to know
1405 * if the message originally had any mime parts or not -- but
1406 * there's also no record of that. (except for MIME-version:)
1409 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1411 fputs(charstring_buffer (outbuf
), stdout
);
1412 charstring_free (outbuf
);
1418 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1420 charstring_t outbuf
= charstring_create (BUFSIZ
);
1421 struct param_comp_list
*pcentry
;
1428 * Grab any items we care about.
1431 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1432 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1433 ct
->c_ctinfo
.ci_subtype
, NULL
);
1436 if (part_comp
&& ct
->c_partno
) {
1437 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1440 if (description_comp
&& ct
->c_descr
) {
1441 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1444 if (dispo_comp
&& ct
->c_dispo_type
) {
1445 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1448 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1449 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1450 pcentry
->param
, '?', 0);
1453 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1454 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1455 pcentry
->param
, '?', 0);
1458 if (ct
->c_cesizefnx
)
1459 partsize
= (*ct
->c_cesizefnx
) (ct
);
1461 partsize
= ct
->c_end
- ct
->c_begin
;
1463 if (ct
->c_file
&& *ct
->c_file
) {
1464 message
= strtol(ct
->c_file
, &endp
, 10);
1465 if (*endp
) message
= 0;
1470 /* make the part's hidden aspect available by overloading the
1471 * %(unseen) function. make the part's size available via %(size).
1472 * see comments in h/fmt_scan.h.
1476 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1478 fputs(charstring_buffer (outbuf
), stdout
);
1479 charstring_free (outbuf
);
1485 * Reset (and free) any of the saved marker text
1489 free_markercomps(void)
1491 struct param_comp_list
*pc_entry
, *pc2
;
1496 description_comp
= NULL
;
1499 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1500 free(pc_entry
->param
);
1501 pc2
= pc_entry
->next
;
1506 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1507 free(pc_entry
->param
);
1508 pc2
= pc_entry
->next
;
1515 * Exit if the display process returned with a nonzero exit code, or terminated
1516 * with a SIGQUIT signal.
1520 pidcheck (int status
)
1522 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)