]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
17ef4d22251c3bdd7c6a315b3f215ff0c0c6b11a
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 */
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
)
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 * If form is "mhl.null", suppress display of header.
115 if (!strcmp (formsw
, "mhl.null"))
118 for (ctp
= cts
; *ctp
; ctp
++) {
121 /* if top-level type is ok, then display message */
122 if (type_ok (ct
, 1)) {
124 * Compile the content marker format line
126 struct format
*mfmt
= compile_marker(markerform
);
130 * Compile the header format line
132 struct format
*hfmt
= compile_header(headerform
);
133 output_header(ct
, hfmt
);
137 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
143 /* Reset the format component hashtable, which free's any components which
151 * Entry point to show/display a single message
155 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
156 int inlineonly
, struct format
*fmt
)
162 /* Allow user executable bit so that temporary directories created by
163 * the viewer (e.g., lynx) are going to be accessible */
164 umask (ct
->c_umask
& ~(0100));
167 * If you have a format file, then display
168 * the message headers.
171 DisplayMsgHeader(ct
, form
, concatsw
);
173 /* Show the body of the message */
174 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
180 if (ct
->c_ceclosefnx
)
181 (*ct
->c_ceclosefnx
) (ct
);
183 /* block a few signals */
185 sigaddset (&set
, SIGHUP
);
186 sigaddset (&set
, SIGINT
);
187 sigaddset (&set
, SIGQUIT
);
188 sigaddset (&set
, SIGTERM
);
189 sigprocmask (SIG_BLOCK
, &set
, &oset
);
191 while (!concatsw
&& wait (&status
) != NOTOK
) {
196 /* reset the signal mask */
197 sigprocmask (SIG_SETMASK
, &oset
, &set
);
204 * Use the mhlproc to show the header fields
208 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
215 vec
= argsplit(mhlproc
, &file
, &vecp
);
216 vec
[vecp
++] = mh_xstrdup("-form");
217 vec
[vecp
++] = mh_xstrdup(form
);
218 vec
[vecp
++] = mh_xstrdup("-nobody");
219 vec
[vecp
++] = getcpy(ct
->c_file
);
222 * If we've specified -(no)moreproc,
223 * then just pass that along.
225 if (nomore
|| concatsw
) {
226 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
228 vec
[vecp
++] = mh_xstrdup("-moreproc");
229 vec
[vecp
++] = mh_xstrdup(progsw
);
238 adios ("fork", "unable to");
243 fprintf (stderr
, "unable to exec ");
249 pidcheck(pidwait(child_id
, NOTOK
));
253 arglist_free(file
, vec
);
258 * Switching routine. Call the correct routine
259 * based on content type.
263 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
266 switch (ct
->c_type
) {
268 return show_multi (ct
, alternate
, concatsw
, textonly
,
272 switch (ct
->c_subtype
) {
273 case MESSAGE_PARTIAL
:
274 return show_partial (ct
, alternate
);
276 case MESSAGE_EXTERNAL
:
277 return show_external (ct
, alternate
, concatsw
, textonly
,
281 return show_message_rfc822 (ct
, alternate
, fmt
);
284 * Treat unknown message types as equivalent to
285 * application/octet-stream for now
288 return show_content (ct
, alternate
, textonly
,
293 return show_text (ct
, alternate
, concatsw
, fmt
);
300 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
303 return 0; /* NOT REACHED */
308 * Generic method for displaying content
312 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
316 CI ci
= &ct
->c_ctinfo
;
319 * If we're here, we are not a text type. So we don't need to check
323 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
324 output_marker(ct
, fmt
, 1);
328 /* Check for invo_name-show-type[/subtype] */
329 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
330 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
332 if ((cp
= ct
->c_showproc
))
333 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
335 /* complain if we are not a part of a multipart/alternative */
337 content_error (NULL
, ct
, "don't know how to display content");
344 * Parse the display string for displaying generic content
348 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
351 int xstdin
= 0, xlist
= 0;
352 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
354 if (!ct
->c_ceopenfnx
) {
356 content_error (NULL
, ct
, "don't know how to decode content");
361 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
363 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
366 if (! strcmp(invo_name
, "mhshow") &&
367 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
368 /* This has to be done after calling c_ceopenfnx, so
369 unfortunately the type checks are necessary without
370 some code rearrangement. And to make this really ugly,
371 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
372 if (convert_content_charset (ct
, &file
) == OK
) {
373 (*ct
->c_ceclosefnx
) (ct
);
374 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
377 char *charset
= content_charset (ct
);
378 inform("unable to convert character set%s%s from %s, continuing...",
379 ct
->c_partno
? " of part " : "",
380 FENDNULL(ct
->c_partno
),
387 strncpy (buffer
, cp
, sizeof(buffer
));
391 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
392 sizeof(buffer
) - 1, 0)) {
393 inform("Buffer overflow constructing show command, continuing...");
398 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
399 fd
, xlist
, xstdin
, fmt
);
404 * Routine to actually display the content
408 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
409 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
415 if (debugsw
|| cracked
) {
418 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
421 fprintf (stderr
, " part %s", ct
->c_partno
);
423 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
425 fprintf (stderr
, " using command %s\n", buffer
);
429 output_marker(ct
, fmt
, 0);
433 * If the command is a zero-length string, just write the output on
437 if (buffer
[0] == '\0') {
438 char readbuf
[BUFSIZ
];
440 char lastchar
= '\n';
443 inform("Cannot use NULL command to display content-type "
444 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
448 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
449 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
450 advise ("putline", "fwrite");
452 lastchar
= readbuf
[cc
- 1];
456 advise("read", "while reading text content");
461 * The MIME standards allow content to not have a trailing newline.
462 * But because we are (presumably) sending this to stdout, include
463 * a newline for text content if the final character was not a
464 * newline. Only do this for mhshow.
467 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
468 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
477 vec
= argsplit(buffer
, &file
, &vecp
);
484 advise ("fork", "unable to");
485 (*ct
->c_ceclosefnx
) (ct
);
490 if (chdir (cracked
) < 0) {
491 advise (cracked
, "chdir");
498 fprintf (stderr
, "unable to exec ");
505 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
506 /* Copy the real display program name. This relies on the
507 specific construction of vec[] by argsplit(). */
511 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
513 arglist_free(file
, vec
);
515 (*ct
->c_ceclosefnx
) (ct
);
516 return alternate
? OK
: status
;
523 * show content of type "text"
527 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
529 char *cp
, buffer
[BUFSIZ
];
530 CI ci
= &ct
->c_ctinfo
;
532 /* Check for invo_name-show-type[/subtype] */
533 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
534 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
537 * Use default method if content is text/plain, or if
538 * if it is not a text part of a multipart/alternative
540 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
543 snprintf(buffer
, sizeof(buffer
), "%%lcat");
545 snprintf(buffer
, sizeof(buffer
), "%%l");
547 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
548 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
549 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
550 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
558 * show message body of type "multipart"
562 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
566 CI ci
= &ct
->c_ctinfo
;
568 /* Check for invo_name-show-type[/subtype] */
569 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
570 return show_multi_aux (ct
, alternate
, cp
, fmt
);
572 if ((cp
= ct
->c_showproc
)) {
573 return show_multi_aux (ct
, alternate
, cp
, fmt
);
577 * Use default method to display this multipart content. Even
578 * unknown types are displayable, since they're treated as mixed
581 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
587 * show message body of subtypes of multipart that
588 * we understand directly (mixed, alternate, etc...)
592 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
593 int inlineonly
, struct format
*fmt
)
595 int alternating
, nowalternate
, result
;
596 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
598 bool request_matched
;
599 bool display_success
;
605 nowalternate
= alternate
;
607 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
613 * alternate -> we are a part inside a multipart/alternative
614 * alternating -> we are a multipart/alternative
618 request_matched
= false;
619 display_success
= false;
620 mult_alt_done
= false;
622 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
625 /* while looking for the right displayable alternative, we
626 * use a looser search criterion than we do after finding it.
627 * specifically, while still looking, part_ok() will match
628 * "parent" parts (e.g. "-part 2" where 2 is a high-level
629 * multipart). after finding it, we use part_exact() to only
630 * choose a part that was requested explicitly.
632 if ((part_exact(p
) && type_ok(p
, 1)) ||
633 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
637 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
639 switch (inneresult
) {
640 case NOTOK
: /* hard display error */
641 request_matched
= true;
642 if (alternate
&& !alternating
) {
648 case DONE
: /* found no match on content type */
651 case OK
: /* display successful */
652 request_matched
= true;
653 display_success
= true;
656 /* if we got success on a sub-part of
657 * multipart/alternative, we're done, unless
658 * there's a chance an explicit part should be
659 * matched later in the alternatives. */
661 mult_alt_done
= true;
662 } else if (alternate
) {
663 alternate
= nowalternate
= 0;
671 /* we're supposed to be displaying at least something from a
672 * multipart/alternative. if we've had parts to consider, and
673 * we've had no success, then we should complain. we shouldn't
674 * complain if none of the parts matched any -part or -type option.
676 if (alternating
&& request_matched
&& !display_success
) {
677 /* if we're ourselves an alternate. don't complain yet. */
679 content_error (NULL
, ct
, "don't know how to display any of the contents");
684 /* if no parts matched what was requested, there can't have been
685 * any display errors. we report DONE rather than OK. */
686 ret
= request_matched
? result
: DONE
;
692 * Parse display string for multipart content
693 * and use external program to display it.
697 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
699 /* xstdin is only used in the call to parse_display_string():
700 its value is ignored in the function. */
701 int xstdin
= 0, xlist
= 0;
702 char *file
= NULL
, buffer
[BUFSIZ
];
703 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
707 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
710 if (!p
->c_ceopenfnx
) {
712 content_error (NULL
, p
, "don't know how to decode content");
716 if (p
->c_storage
== NULL
) {
717 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
720 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
722 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
724 (*p
->c_ceclosefnx
) (p
);
728 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
729 buffer
, sizeof(buffer
) - 1, 1)) {
730 inform("Buffer overflow constructing show command, continuing...");
734 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
739 * show content of type "message/rfc822"
743 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
746 CI ci
= &ct
->c_ctinfo
;
748 /* Check for invo_name-show-type[/subtype] */
749 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
750 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
752 if ((cp
= ct
->c_showproc
))
753 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
755 /* default method for message/rfc822 */
756 if (ct
->c_subtype
== MESSAGE_RFC822
) {
757 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
758 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
761 /* complain if we are not a part of a multipart/alternative */
763 content_error (NULL
, ct
, "don't know how to display content");
770 * Show content of type "message/partial".
774 show_partial (CT ct
, int alternate
)
776 NMH_UNUSED (alternate
);
778 content_error (NULL
, ct
,
779 "in order to display this message, you must reassemble it");
785 * Show content of type "message/external".
787 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
791 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
794 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
795 CT p
= e
->eb_content
;
800 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
805 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
806 char *file
, char *buffer
, size_t buflen
,
811 char *bp
= buffer
, *pp
;
812 CI ci
= &ct
->c_ctinfo
;
814 bp
[0] = bp
[buflen
] = '\0';
816 for ( ; *cp
&& buflen
> 0; cp
++) {
822 /* insert parameters from Content-Type field */
827 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
828 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
829 get_param_value(pm
, '?'));
839 /* insert content description */
843 s
= trimcpy (ct
->c_descr
);
844 strncpy (bp
, s
, buflen
);
850 /* no longer implemented */
854 /* %f, and stdin is terminal not content */
860 /* insert filename(s) containing content */
861 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
866 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
869 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
876 /* insert filename containing content */
877 snprintf (bp
, buflen
, "%s", file
);
880 * Old comments below are left here for posterity.
881 * This was/is tricky.
883 /* since we've quoted the file argument, set things up
884 * to look past it, to avoid problems with the quoting
885 * logic below. (I know, I should figure out what's
886 * broken with the quoting logic, but..)
889 * Here's the email that submitted the patch with
891 * https://www.mail-archive.com/nmh-workers@mhost.com/
893 * I can't tell from that exactly what was broken,
894 * beyond misquoting of the filename. The profile
895 * had appearances of %F both with and without quotes.
896 * The unquoted ones should have been quoted by the
898 * The fix was to always quote the filename. But
899 * that broke '%F' because it expanded to ''filename''.
902 * Old comments above are left here for posterity.
903 * The quoting below should work properly now.
909 /* No longer supported */
913 /* display listing prior to displaying content */
918 /* insert subtype of content */
919 strncpy (bp
, ci
->ci_subtype
, buflen
);
923 /* insert character % */
927 const char *closing_brace
= strchr(cp
, '}');
930 const size_t param_len
= closing_brace
- cp
- 1;
931 char *param
= mh_xmalloc(param_len
+ 1);
934 (void) strncpy(param
, cp
+ 1, param_len
);
935 param
[param_len
] = '\0';
936 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
939 cp
+= param_len
+ 1; /* Skip both braces, too. */
942 /* %{param} is set in the Content-Type header.
943 After the break below, quote it if necessary. */
944 (void) strncpy(bp
, value
, buflen
);
947 /* %{param} not found, so skip it completely. cp
948 was advanced above. */
952 /* This will get confused if there are multiple %{}'s,
953 but its real purpose is to avoid doing bad things
954 above if a closing brace wasn't found. */
955 inform("no closing brace for display string escape %s, continuing...",
972 /* Did we actually insert something? */
974 /* Insert single quote if not inside quotes already */
975 if (!quoted
&& buflen
) {
977 memmove (pp
+ 1, pp
, len
+1);
983 /* Escape existing quotes */
984 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
987 /* Quoted. Let this quote close that quoting.
988 Insert an escaped quote to replace it and
989 another quote to reopen quoting, which will be
991 memmove (pp
+ 2, pp
, len
);
998 /* Not quoted. This should not be reached with
999 the current code, but handle the condition
1000 in case the code changes. Just escape the
1002 memmove (pp
, pp
-1, len
+1);
1008 /* If pp is still set, that means we ran out of space. */
1011 /* Close quoting. */
1012 if (quoted
&& buflen
) {
1013 /* See if we need to close the quote by looking
1014 for an odd number of unescaped close quotes in
1015 the remainder of the display string. */
1016 int found_quote
= 0, escaped
= 0;
1019 for (c
= cp
+1; *c
; ++c
) {
1021 escaped
= ! escaped
;
1027 found_quote
= ! found_quote
;
1032 if (! found_quote
) {
1052 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1053 /* content_error would provide a more useful error message
1054 * here, except that if we got overrun, it probably would
1060 /* use charset string to modify display method */
1061 if (ct
->c_termproc
) {
1064 strncpy (term
, buffer
, sizeof(term
));
1065 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1073 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1075 char *src_charset
= content_charset (ct
);
1078 if (strcasecmp (src_charset
, dest_charset
)) {
1080 iconv_t conv_desc
= NULL
;
1087 bool opened_input_file
= false;
1088 char src_buffer
[BUFSIZ
];
1089 size_t dest_buffer_size
= BUFSIZ
;
1090 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1093 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1095 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1097 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1102 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1103 die("unable to create temporary file in %s",
1106 dest
= mh_xstrdup(tempfile
);
1108 if (ct
->c_cefile
.ce_file
) {
1109 file
= &ct
->c_cefile
.ce_file
;
1110 fp
= &ct
->c_cefile
.ce_fp
;
1112 } else if (ct
->c_file
) {
1115 begin
= (size_t) ct
->c_begin
;
1116 end
= (size_t) ct
->c_end
;
1117 } /* else no input file: shouldn't happen */
1119 if (file
&& *file
&& fp
) {
1121 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1122 advise (*file
, "unable to open for reading");
1125 opened_input_file
= true;
1132 size_t bytes_to_read
=
1133 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1135 fseeko (*fp
, begin
, SEEK_SET
);
1136 while ((inbytes
= fread (src_buffer
, 1,
1137 min (bytes_to_read
, sizeof src_buffer
),
1139 ICONV_CONST
char *ib
= src_buffer
;
1140 char *ob
= dest_buffer
;
1141 size_t outbytes
= dest_buffer_size
;
1142 size_t outbytes_before
= outbytes
;
1144 if (end
> 0) bytes_to_read
-= inbytes
;
1147 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1149 if (errno
== E2BIG
) {
1151 * Bump up the buffer by at least a factor of 2
1152 * over what we need.
1154 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1155 dest_buffer_size
+= bumpup
;
1156 dest_buffer
= mh_xrealloc(dest_buffer
,
1158 ob
= dest_buffer
+ ob_off
;
1160 outbytes_before
+= bumpup
;
1163 if (errno
== EINVAL
) {
1164 /* middle of multi-byte sequence */
1165 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1166 advise (dest
, "write");
1168 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1169 if (end
> 0) bytes_to_read
+= inbytes
;
1170 /* inform("convert_charset: EINVAL"); */
1173 if (errno
== EILSEQ
) {
1174 /* invalid multi-byte sequence */
1176 for (++ib
, --inbytes
;
1178 (((unsigned char) *ib
) & 0xc0) == 0x80;
1182 ib
++; inbytes
--; /* skip it */
1184 (*ob
++) = '?'; outbytes
--;
1185 /* inform("convert_charset: EILSEQ"); */
1188 inform("convert_charset: errno = %d", errno
);
1193 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1194 advise (dest
, "write");
1198 if (opened_input_file
) {
1204 iconv_close (conv_desc
);
1208 /* Replace the decoded file with the converted one. */
1209 if (ct
->c_cefile
.ce_file
) {
1210 if (ct
->c_cefile
.ce_unlink
) {
1211 (void) m_unlink (ct
->c_cefile
.ce_file
);
1213 free (ct
->c_cefile
.ce_file
);
1215 ct
->c_cefile
.ce_file
= dest
;
1216 ct
->c_cefile
.ce_unlink
= 1;
1220 /* Update ct->c_ctline. */
1222 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1223 ct
->c_ctinfo
.ci_subtype
, NULL
);
1226 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1227 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1229 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1230 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1232 ctline
= add(outline
, ctline
);
1236 free (ct
->c_ctline
);
1237 ct
->c_ctline
= ctline
;
1238 } /* else no CT line, which is odd */
1240 /* Update Content-Type header field. */
1241 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1242 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1243 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1251 (void) m_unlink (dest
);
1254 #else /* ! HAVE_ICONV */
1255 NMH_UNUSED (message_mods
);
1257 inform("Can't convert %s to %s without iconv", src_charset
,
1261 #endif /* ! HAVE_ICONV */
1270 convert_content_charset (CT ct
, char **file
)
1275 /* Using current locale, see if the content needs to be converted. */
1277 /* content_charset() cannot return NULL. */
1278 char *src_charset
= content_charset (ct
);
1280 if (! check_charset (src_charset
, strlen (src_charset
))) {
1283 char *dest_charset
= getcpy (get_charset ());
1285 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1286 *file
= ct
->c_cefile
.ce_file
;
1291 free (dest_charset
);
1294 #else /* ! HAVE_ICONV */
1297 #endif /* ! HAVE_ICONV */
1303 * Compile our format string and save any parameters we care about.
1306 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1307 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1308 "%<{description}%{description}" \
1309 "%?{cdispo-filename}%{cdispo-filename}" \
1310 "%|%{ctype-name}%> " \
1311 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1313 static struct format
*
1314 compile_header(char *form
)
1318 struct comp
*comp
= NULL
;
1319 unsigned int bucket
;
1321 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1323 (void) fmt_compile(fmtstring
, &fmt
, 0);
1326 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1327 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1335 static struct format
*
1336 compile_marker(char *form
)
1340 struct comp
*comp
= NULL
;
1341 unsigned int bucket
;
1342 struct param_comp_list
*pc_entry
;
1344 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1346 (void) fmt_compile(fmtstring
, &fmt
, 0);
1350 * Things we care about:
1352 * part - Part name (e.g., 1.1)
1353 * content-type - Content-Type
1354 * description - Content-Description
1355 * disposition - Content-Disposition (inline, attachment)
1356 * ctype-<param> - Content-Type parameter
1357 * cdispo-<param> - Content-Disposition parameter
1360 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1361 if (strcasecmp(comp
->c_name
, "part") == 0) {
1363 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1365 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1366 description_comp
= comp
;
1367 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1369 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1370 strlen(comp
->c_name
) > 6) {
1372 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1373 pc_entry
->comp
= comp
;
1374 pc_entry
->next
= ctype_pc_list
;
1375 ctype_pc_list
= pc_entry
;
1376 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1377 strlen(comp
->c_name
) > 7) {
1379 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1380 pc_entry
->comp
= comp
;
1381 pc_entry
->next
= dispo_pc_list
;
1382 dispo_pc_list
= pc_entry
;
1390 * Output on stdout an appropriate marker for this content, using mh-format
1394 output_header(CT ct
, struct format
*fmt
)
1396 charstring_t outbuf
= charstring_create (BUFSIZ
);
1402 folder_comp
->c_text
= getcpy(folder
);
1404 if (ct
->c_file
&& *ct
->c_file
) {
1405 message
= strtol(ct
->c_file
, &endp
, 10);
1406 if (*endp
) message
= 0;
1410 /* it would be nice to populate dat[2], for %(size) here,
1411 * but it's not available. it might also be nice to know
1412 * if the message originally had any mime parts or not -- but
1413 * there's also no record of that. (except for MIME-version:)
1416 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1418 fputs(charstring_buffer (outbuf
), stdout
);
1419 charstring_free (outbuf
);
1425 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1427 charstring_t outbuf
= charstring_create (BUFSIZ
);
1428 struct param_comp_list
*pcentry
;
1435 * Grab any items we care about.
1438 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1439 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1440 ct
->c_ctinfo
.ci_subtype
, NULL
);
1443 if (part_comp
&& ct
->c_partno
) {
1444 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1447 if (description_comp
&& ct
->c_descr
) {
1448 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1451 if (dispo_comp
&& ct
->c_dispo_type
) {
1452 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1455 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1456 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1457 pcentry
->param
, '?', 0);
1460 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1461 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1462 pcentry
->param
, '?', 0);
1465 if (ct
->c_cesizefnx
)
1466 partsize
= (*ct
->c_cesizefnx
) (ct
);
1468 partsize
= ct
->c_end
- ct
->c_begin
;
1470 if (ct
->c_file
&& *ct
->c_file
) {
1471 message
= strtol(ct
->c_file
, &endp
, 10);
1472 if (*endp
) message
= 0;
1477 /* make the part's hidden aspect available by overloading the
1478 * %(unseen) function. make the part's size available via %(size).
1479 * see comments in h/fmt_scan.h.
1483 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1485 fputs(charstring_buffer (outbuf
), stdout
);
1486 charstring_free (outbuf
);
1492 * Reset (and free) any of the saved marker text
1496 free_markercomps(void)
1498 struct param_comp_list
*pc_entry
, *pc2
;
1503 description_comp
= NULL
;
1506 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1507 free(pc_entry
->param
);
1508 pc2
= pc_entry
->next
;
1513 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1514 free(pc_entry
->param
);
1515 pc2
= pc_entry
->next
;
1522 * Exit if the display process returned with a nonzero exit code, or terminated
1523 * with a SIGQUIT signal.
1527 pidcheck (int status
)
1529 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)