]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3ed6f8b995d2ec9190707df89aa2a16fc0548cb6
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.
11 #include <h/signals.h>
16 #include <h/mhparse.h>
17 #include <h/fmt_scan.h>
21 #include "mhshowsbr.h"
22 #include "sbr/m_mktemp.h"
25 #endif /* ! HAVE_ICONV */
33 /* flags for moreproc/header display */
37 /* for output markers and headers */
47 static void show_single_message (CT
, char *, int, int, int, struct format
*);
48 static void DisplayMsgHeader (CT
, char *, int);
49 static int show_switch (CT
, int, int, int, int, struct format
*);
50 static int show_content (CT
, int, int, int, struct format
*);
51 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
52 static int show_text (CT
, int, int, struct format
*);
53 static int show_multi (CT
, int, int, int, int, struct format
*);
54 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
55 static int show_multi_aux (CT
, int, char *, struct format
*);
56 static int show_message_rfc822 (CT
, int, struct format
*);
57 static int show_partial (CT
, int);
58 static int show_external (CT
, int, int, int, int, struct format
*);
59 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
60 size_t, int multipart
);
61 static int convert_content_charset (CT
, char **);
62 static struct format
*compile_header(char *);
63 static struct format
*compile_marker(char *);
64 static void output_header (CT
, struct format
*);
65 static void output_marker (CT
, struct format
*, int);
66 static void free_markercomps (void);
67 static int pidcheck(int);
70 * Components (and list of parameters/components) we care about for the
71 * content marker display.
74 static struct comp
*folder_comp
= NULL
;
75 static struct comp
*part_comp
= NULL
;
76 static struct comp
*ctype_comp
= NULL
;
77 static struct comp
*description_comp
= NULL
;
78 static struct comp
*dispo_comp
= NULL
;
80 struct param_comp_list
{
83 struct param_comp_list
*next
;
86 static struct param_comp_list
*ctype_pc_list
= NULL
;
87 static struct param_comp_list
*dispo_pc_list
= NULL
;
91 * Top level entry point to show/display a group of messages
95 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
98 struct format
*hfmt
, *mfmt
;
101 * If form is not specified, then get default form
102 * for showing headers of MIME messages.
105 formsw
= mh_xstrdup(etcpath("mhl.headers"));
108 * Compile the content marker and header format lines
110 mfmt
= compile_marker(markerform
);
111 hfmt
= compile_header(headerform
);
114 * If form is "mhl.null", suppress display of header.
116 if (!strcmp (formsw
, "mhl.null"))
119 for (ctp
= cts
; *ctp
; ctp
++) {
122 /* if top-level type is ok, then display message */
123 if (type_ok (ct
, 1)) {
124 if (headersw
) output_header(ct
, hfmt
);
126 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
138 * Entry point to show/display a single message
142 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
143 int inlineonly
, struct format
*fmt
)
149 /* Allow user executable bit so that temporary directories created by
150 * the viewer (e.g., lynx) are going to be accessible */
151 umask (ct
->c_umask
& ~(0100));
154 * If you have a format file, then display
155 * the message headers.
158 DisplayMsgHeader(ct
, form
, concatsw
);
160 /* Show the body of the message */
161 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
167 if (ct
->c_ceclosefnx
)
168 (*ct
->c_ceclosefnx
) (ct
);
170 /* block a few signals */
172 sigaddset (&set
, SIGHUP
);
173 sigaddset (&set
, SIGINT
);
174 sigaddset (&set
, SIGQUIT
);
175 sigaddset (&set
, SIGTERM
);
176 sigprocmask (SIG_BLOCK
, &set
, &oset
);
178 while (!concatsw
&& wait (&status
) != NOTOK
) {
183 /* reset the signal mask */
184 sigprocmask (SIG_SETMASK
, &oset
, &set
);
191 * Use the mhlproc to show the header fields
195 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
202 vec
= argsplit(mhlproc
, &file
, &vecp
);
203 vec
[vecp
++] = mh_xstrdup("-form");
204 vec
[vecp
++] = mh_xstrdup(form
);
205 vec
[vecp
++] = mh_xstrdup("-nobody");
206 vec
[vecp
++] = getcpy(ct
->c_file
);
209 * If we've specified -(no)moreproc,
210 * then just pass that along.
212 if (nomore
|| concatsw
) {
213 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
215 vec
[vecp
++] = mh_xstrdup("-moreproc");
216 vec
[vecp
++] = mh_xstrdup(progsw
);
225 adios ("fork", "unable to");
230 fprintf (stderr
, "unable to exec ");
236 pidcheck(pidwait(child_id
, NOTOK
));
240 arglist_free(file
, vec
);
245 * Switching routine. Call the correct routine
246 * based on content type.
250 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
253 switch (ct
->c_type
) {
255 return show_multi (ct
, alternate
, concatsw
, textonly
,
259 switch (ct
->c_subtype
) {
260 case MESSAGE_PARTIAL
:
261 return show_partial (ct
, alternate
);
263 case MESSAGE_EXTERNAL
:
264 return show_external (ct
, alternate
, concatsw
, textonly
,
268 return show_message_rfc822 (ct
, alternate
, fmt
);
271 * Treat unknown message types as equivalent to
272 * application/octet-stream for now
275 return show_content (ct
, alternate
, textonly
,
280 return show_text (ct
, alternate
, concatsw
, fmt
);
287 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
290 return 0; /* NOT REACHED */
295 * Generic method for displaying content
299 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
303 CI ci
= &ct
->c_ctinfo
;
306 * If we're here, we are not a text type. So we don't need to check
310 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
311 output_marker(ct
, fmt
, 1);
315 /* Check for invo_name-show-type[/subtype] */
316 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
317 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
319 if ((cp
= ct
->c_showproc
))
320 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
322 /* complain if we are not a part of a multipart/alternative */
324 content_error (NULL
, ct
, "don't know how to display content");
331 * Parse the display string for displaying generic content
335 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
338 int xstdin
= 0, xlist
= 0;
339 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
341 if (!ct
->c_ceopenfnx
) {
343 content_error (NULL
, ct
, "don't know how to decode content");
348 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
350 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
353 if (! strcmp(invo_name
, "mhshow") &&
354 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
355 /* This has to be done after calling c_ceopenfnx, so
356 unfortunately the type checks are necessary without
357 some code rearrangement. And to make this really ugly,
358 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
359 if (convert_content_charset (ct
, &file
) == OK
) {
360 (*ct
->c_ceclosefnx
) (ct
);
361 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
364 char *charset
= content_charset (ct
);
365 inform("unable to convert character set%s%s from %s, continuing...",
366 ct
->c_partno
? " of part " : "",
367 FENDNULL(ct
->c_partno
),
374 strncpy (buffer
, cp
, sizeof(buffer
));
378 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
379 sizeof(buffer
) - 1, 0)) {
380 inform("Buffer overflow constructing show command, continuing...");
385 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
386 fd
, xlist
, xstdin
, fmt
);
391 * Routine to actually display the content
395 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
396 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
402 if (debugsw
|| cracked
) {
405 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
408 fprintf (stderr
, " part %s", ct
->c_partno
);
410 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
412 fprintf (stderr
, " using command %s\n", buffer
);
416 output_marker(ct
, fmt
, 0);
420 * If the command is a zero-length string, just write the output on
424 if (buffer
[0] == '\0') {
425 char readbuf
[BUFSIZ
];
427 char lastchar
= '\n';
430 inform("Cannot use NULL command to display content-type "
431 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
435 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
436 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
437 advise ("putline", "fwrite");
439 lastchar
= readbuf
[cc
- 1];
443 advise("read", "while reading text content");
448 * The MIME standards allow content to not have a trailing newline.
449 * But because we are (presumably) sending this to stdout, include
450 * a newline for text content if the final character was not a
451 * newline. Only do this for mhshow.
454 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
455 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
464 vec
= argsplit(buffer
, &file
, &vecp
);
472 advise ("fork", "unable to");
473 (*ct
->c_ceclosefnx
) (ct
);
478 if (chdir (cracked
) < 0) {
479 advise (cracked
, "chdir");
486 fprintf (stderr
, "unable to exec ");
493 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
494 /* Copy the real display program name. This relies on the
495 specific construction of vec[] by argsplit(). */
499 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
501 arglist_free(file
, vec
);
503 (*ct
->c_ceclosefnx
) (ct
);
504 return alternate
? OK
: status
;
511 * show content of type "text"
515 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
517 char *cp
, buffer
[BUFSIZ
];
518 CI ci
= &ct
->c_ctinfo
;
520 /* Check for invo_name-show-type[/subtype] */
521 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
522 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
525 * Use default method if content is text/plain, or if
526 * if it is not a text part of a multipart/alternative
528 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
531 snprintf(buffer
, sizeof(buffer
), "%%lcat");
533 snprintf(buffer
, sizeof(buffer
), "%%l");
535 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
536 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
537 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
538 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
546 * show message body of type "multipart"
550 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
554 CI ci
= &ct
->c_ctinfo
;
556 /* Check for invo_name-show-type[/subtype] */
557 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
558 return show_multi_aux (ct
, alternate
, cp
, fmt
);
560 if ((cp
= ct
->c_showproc
)) {
561 return show_multi_aux (ct
, alternate
, cp
, fmt
);
565 * Use default method to display this multipart content. Even
566 * unknown types are displayable, since they're treated as mixed
569 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
575 * show message body of subtypes of multipart that
576 * we understand directly (mixed, alternate, etc...)
580 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
581 int inlineonly
, struct format
*fmt
)
583 int alternating
, nowalternate
, result
;
584 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
586 bool request_matched
;
587 bool display_success
;
593 nowalternate
= alternate
;
595 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
601 * alternate -> we are a part inside a multipart/alternative
602 * alternating -> we are a multipart/alternative
606 request_matched
= false;
607 display_success
= false;
608 mult_alt_done
= false;
610 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
613 /* while looking for the right displayable alternative, we
614 * use a looser search criterion than we do after finding it.
615 * specifically, while still looking, part_ok() will match
616 * "parent" parts (e.g. "-part 2" where 2 is a high-level
617 * multipart). after finding it, we use part_exact() to only
618 * choose a part that was requested explicitly.
620 if ((part_exact(p
) && type_ok(p
, 1)) ||
621 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
625 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
627 switch (inneresult
) {
628 case NOTOK
: /* hard display error */
629 request_matched
= true;
630 if (alternate
&& !alternating
) {
636 case DONE
: /* found no match on content type */
639 case OK
: /* display successful */
640 request_matched
= true;
641 display_success
= true;
644 /* if we got success on a sub-part of
645 * multipart/alternative, we're done, unless
646 * there's a chance an explicit part should be
647 * matched later in the alternatives. */
649 mult_alt_done
= true;
650 } else if (alternate
) {
651 alternate
= nowalternate
= 0;
659 /* we're supposed to be displaying at least something from a
660 * multipart/alternative. if we've had parts to consider, and
661 * we've had no success, then we should complain. we shouldn't
662 * complain if none of the parts matched any -part or -type option.
664 if (alternating
&& request_matched
&& !display_success
) {
665 /* if we're ourselves an alternate. don't complain yet. */
667 content_error (NULL
, ct
, "don't know how to display any of the contents");
672 /* if no parts matched what was requested, there can't have been
673 * any display errors. we report DONE rather than OK. */
674 ret
= request_matched
? result
: DONE
;
680 * Parse display string for multipart content
681 * and use external program to display it.
685 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
687 /* xstdin is only used in the call to parse_display_string():
688 its value is ignored in the function. */
689 int xstdin
= 0, xlist
= 0;
690 char *file
= NULL
, buffer
[BUFSIZ
];
691 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
695 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
698 if (!p
->c_ceopenfnx
) {
700 content_error (NULL
, p
, "don't know how to decode content");
704 if (p
->c_storage
== NULL
) {
705 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
708 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
710 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
712 (*p
->c_ceclosefnx
) (p
);
716 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
717 buffer
, sizeof(buffer
) - 1, 1)) {
718 inform("Buffer overflow constructing show command, continuing...");
722 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
727 * show content of type "message/rfc822"
731 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
734 CI ci
= &ct
->c_ctinfo
;
736 /* Check for invo_name-show-type[/subtype] */
737 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
738 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
740 if ((cp
= ct
->c_showproc
))
741 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
743 /* default method for message/rfc822 */
744 if (ct
->c_subtype
== MESSAGE_RFC822
) {
745 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
746 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
749 /* complain if we are not a part of a multipart/alternative */
751 content_error (NULL
, ct
, "don't know how to display content");
758 * Show content of type "message/partial".
762 show_partial (CT ct
, int alternate
)
764 NMH_UNUSED (alternate
);
766 content_error (NULL
, ct
,
767 "in order to display this message, you must reassemble it");
773 * Show content of type "message/external".
775 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
779 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
782 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
783 CT p
= e
->eb_content
;
788 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
793 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
794 char *file
, char *buffer
, size_t buflen
,
799 char *bp
= buffer
, *pp
;
800 CI ci
= &ct
->c_ctinfo
;
802 bp
[0] = bp
[buflen
] = '\0';
804 for ( ; *cp
&& buflen
> 0; cp
++) {
810 /* insert parameters from Content-Type field */
815 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
816 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
817 get_param_value(pm
, '?'));
827 /* insert content description */
831 s
= trimcpy (ct
->c_descr
);
832 strncpy (bp
, s
, buflen
);
838 /* no longer implemented */
842 /* %f, and stdin is terminal not content */
848 /* insert filename(s) containing content */
849 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
854 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
857 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
864 /* insert filename containing content */
865 snprintf (bp
, buflen
, "%s", file
);
868 * Old comments below are left here for posterity.
869 * This was/is tricky.
871 /* since we've quoted the file argument, set things up
872 * to look past it, to avoid problems with the quoting
873 * logic below. (I know, I should figure out what's
874 * broken with the quoting logic, but..)
877 * Here's the email that submitted the patch with
879 * https://www.mail-archive.com/nmh-workers@mhost.com/
881 * I can't tell from that exactly what was broken,
882 * beyond misquoting of the filename. The profile
883 * had appearances of %F both with and without quotes.
884 * The unquoted ones should have been quoted by the
886 * The fix was to always quote the filename. But
887 * that broke '%F' because it expanded to ''filename''.
890 * Old comments above are left here for posterity.
891 * The quoting below should work properly now.
897 /* No longer supported */
901 /* display listing prior to displaying content */
906 /* insert subtype of content */
907 strncpy (bp
, ci
->ci_subtype
, buflen
);
911 /* insert character % */
915 const char *closing_brace
= strchr(cp
, '}');
918 const size_t param_len
= closing_brace
- cp
- 1;
919 char *param
= mh_xmalloc(param_len
+ 1);
922 (void) strncpy(param
, cp
+ 1, param_len
);
923 param
[param_len
] = '\0';
924 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
927 cp
+= param_len
+ 1; /* Skip both braces, too. */
930 /* %{param} is set in the Content-Type header.
931 After the break below, quote it if necessary. */
932 (void) strncpy(bp
, value
, buflen
);
935 /* %{param} not found, so skip it completely. cp
936 was advanced above. */
940 /* This will get confused if there are multiple %{}'s,
941 but its real purpose is to avoid doing bad things
942 above if a closing brace wasn't found. */
943 inform("no closing brace for display string escape %s, continuing...",
960 /* Did we actually insert something? */
962 /* Insert single quote if not inside quotes already */
963 if (!quoted
&& buflen
) {
965 memmove (pp
+ 1, pp
, len
+1);
971 /* Escape existing quotes */
972 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
975 /* Quoted. Let this quote close that quoting.
976 Insert an escaped quote to replace it and
977 another quote to reopen quoting, which will be
979 memmove (pp
+ 2, pp
, len
);
986 /* Not quoted. This should not be reached with
987 the current code, but handle the condition
988 in case the code changes. Just escape the
990 memmove (pp
, pp
-1, len
+1);
996 /* If pp is still set, that means we ran out of space. */
1000 if (quoted
&& buflen
) {
1001 /* See if we need to close the quote by looking
1002 for an odd number of unescaped close quotes in
1003 the remainder of the display string. */
1004 int found_quote
= 0, escaped
= 0;
1007 for (c
= cp
+1; *c
; ++c
) {
1009 escaped
= ! escaped
;
1015 found_quote
= ! found_quote
;
1020 if (! found_quote
) {
1040 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1041 /* content_error would provide a more useful error message
1042 * here, except that if we got overrun, it probably would
1048 /* use charset string to modify display method */
1049 if (ct
->c_termproc
) {
1052 strncpy (term
, buffer
, sizeof(term
));
1053 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1061 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1063 char *src_charset
= content_charset (ct
);
1066 if (strcasecmp (src_charset
, dest_charset
)) {
1068 iconv_t conv_desc
= NULL
;
1075 bool opened_input_file
= false;
1076 char src_buffer
[BUFSIZ
];
1077 size_t dest_buffer_size
= BUFSIZ
;
1078 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1081 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1083 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1085 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1090 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1091 die("unable to create temporary file in %s",
1094 dest
= mh_xstrdup(tempfile
);
1096 if (ct
->c_cefile
.ce_file
) {
1097 file
= &ct
->c_cefile
.ce_file
;
1098 fp
= &ct
->c_cefile
.ce_fp
;
1100 } else if (ct
->c_file
) {
1103 begin
= (size_t) ct
->c_begin
;
1104 end
= (size_t) ct
->c_end
;
1105 } /* else no input file: shouldn't happen */
1107 if (file
&& *file
&& fp
) {
1109 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1110 advise (*file
, "unable to open for reading");
1113 opened_input_file
= true;
1120 size_t bytes_to_read
=
1121 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1123 fseeko (*fp
, begin
, SEEK_SET
);
1124 while ((inbytes
= fread (src_buffer
, 1,
1125 min (bytes_to_read
, sizeof src_buffer
),
1127 ICONV_CONST
char *ib
= src_buffer
;
1128 char *ob
= dest_buffer
;
1129 size_t outbytes
= dest_buffer_size
;
1130 size_t outbytes_before
= outbytes
;
1132 if (end
> 0) bytes_to_read
-= inbytes
;
1135 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1137 if (errno
== E2BIG
) {
1139 * Bump up the buffer by at least a factor of 2
1140 * over what we need.
1142 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1143 dest_buffer_size
+= bumpup
;
1144 dest_buffer
= mh_xrealloc(dest_buffer
,
1146 ob
= dest_buffer
+ ob_off
;
1148 outbytes_before
+= bumpup
;
1151 if (errno
== EINVAL
) {
1152 /* middle of multi-byte sequence */
1153 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1154 advise (dest
, "write");
1156 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1157 if (end
> 0) bytes_to_read
+= inbytes
;
1158 /* inform("convert_charset: EINVAL"); */
1161 if (errno
== EILSEQ
) {
1162 /* invalid multi-byte sequence */
1164 for (++ib
, --inbytes
;
1166 (((unsigned char) *ib
) & 0xc0) == 0x80;
1170 ib
++; inbytes
--; /* skip it */
1172 (*ob
++) = '?'; outbytes
--;
1173 /* inform("convert_charset: EILSEQ"); */
1176 inform("convert_charset: errno = %d", errno
);
1181 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1182 advise (dest
, "write");
1186 if (opened_input_file
) {
1192 iconv_close (conv_desc
);
1196 /* Replace the decoded file with the converted one. */
1197 if (ct
->c_cefile
.ce_file
) {
1198 if (ct
->c_cefile
.ce_unlink
) {
1199 (void) m_unlink (ct
->c_cefile
.ce_file
);
1201 free (ct
->c_cefile
.ce_file
);
1203 ct
->c_cefile
.ce_file
= dest
;
1204 ct
->c_cefile
.ce_unlink
= 1;
1208 /* Update ct->c_ctline. */
1210 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1211 ct
->c_ctinfo
.ci_subtype
, NULL
);
1214 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1215 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1217 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1218 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1220 ctline
= add(outline
, ctline
);
1224 free (ct
->c_ctline
);
1225 ct
->c_ctline
= ctline
;
1226 } /* else no CT line, which is odd */
1228 /* Update Content-Type header field. */
1229 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1230 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1231 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1239 (void) m_unlink (dest
);
1242 #else /* ! HAVE_ICONV */
1243 NMH_UNUSED (message_mods
);
1245 inform("Can't convert %s to %s without iconv", src_charset
,
1249 #endif /* ! HAVE_ICONV */
1258 convert_content_charset (CT ct
, char **file
)
1263 /* Using current locale, see if the content needs to be converted. */
1265 /* content_charset() cannot return NULL. */
1266 char *src_charset
= content_charset (ct
);
1268 if (! check_charset (src_charset
, strlen (src_charset
))) {
1271 char *dest_charset
= getcpy (get_charset ());
1273 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1274 *file
= ct
->c_cefile
.ce_file
;
1279 free (dest_charset
);
1282 #else /* ! HAVE_ICONV */
1285 #endif /* ! HAVE_ICONV */
1291 * Compile our format string and save any parameters we care about.
1294 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1295 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1296 "%<{description}%{description}" \
1297 "%?{cdispo-filename}%{cdispo-filename}" \
1298 "%|%{ctype-name}%> " \
1299 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1301 static struct format
*
1302 compile_header(char *form
)
1306 struct comp
*comp
= NULL
;
1307 unsigned int bucket
;
1309 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1311 (void) fmt_compile(fmtstring
, &fmt
, 1);
1314 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1315 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1323 static struct format
*
1324 compile_marker(char *form
)
1328 struct comp
*comp
= NULL
;
1329 unsigned int bucket
;
1330 struct param_comp_list
*pc_entry
;
1332 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1334 (void) fmt_compile(fmtstring
, &fmt
, 1);
1338 * Things we care about:
1340 * part - Part name (e.g., 1.1)
1341 * content-type - Content-Type
1342 * description - Content-Description
1343 * disposition - Content-Disposition (inline, attachment)
1344 * ctype-<param> - Content-Type parameter
1345 * cdispo-<param> - Content-Disposition parameter
1348 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1349 if (strcasecmp(comp
->c_name
, "part") == 0) {
1351 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1353 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1354 description_comp
= comp
;
1355 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1357 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1358 strlen(comp
->c_name
) > 6) {
1360 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1361 pc_entry
->comp
= comp
;
1362 pc_entry
->next
= ctype_pc_list
;
1363 ctype_pc_list
= pc_entry
;
1364 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1365 strlen(comp
->c_name
) > 7) {
1367 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1368 pc_entry
->comp
= comp
;
1369 pc_entry
->next
= dispo_pc_list
;
1370 dispo_pc_list
= pc_entry
;
1378 * Output on stdout an appropriate marker for this content, using mh-format
1382 output_header(CT ct
, struct format
*fmt
)
1384 charstring_t outbuf
= charstring_create (BUFSIZ
);
1390 folder_comp
->c_text
= getcpy(folder
);
1392 if (ct
->c_file
&& *ct
->c_file
) {
1393 message
= strtol(ct
->c_file
, &endp
, 10);
1394 if (*endp
) message
= 0;
1398 /* it would be nice to populate dat[2], for %(size) here,
1399 * but it's not available. it might also be nice to know
1400 * if the message originally had any mime parts or not -- but
1401 * there's also no record of that. (except for MIME-version:)
1404 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1406 fputs(charstring_buffer (outbuf
), stdout
);
1407 charstring_free (outbuf
);
1413 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1415 charstring_t outbuf
= charstring_create (BUFSIZ
);
1416 struct param_comp_list
*pcentry
;
1423 * Grab any items we care about.
1426 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1427 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1428 ct
->c_ctinfo
.ci_subtype
, NULL
);
1431 if (part_comp
&& ct
->c_partno
) {
1432 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1435 if (description_comp
&& ct
->c_descr
) {
1436 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1439 if (dispo_comp
&& ct
->c_dispo_type
) {
1440 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1443 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1444 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1445 pcentry
->param
, '?', 0);
1448 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1449 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1450 pcentry
->param
, '?', 0);
1453 if (ct
->c_cesizefnx
)
1454 partsize
= (*ct
->c_cesizefnx
) (ct
);
1456 partsize
= ct
->c_end
- ct
->c_begin
;
1458 if (ct
->c_file
&& *ct
->c_file
) {
1459 message
= strtol(ct
->c_file
, &endp
, 10);
1460 if (*endp
) message
= 0;
1465 /* make the part's hidden aspect available by overloading the
1466 * %(unseen) function. make the part's size available via %(size).
1467 * see comments in h/fmt_scan.h.
1471 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1473 fputs(charstring_buffer (outbuf
), stdout
);
1474 charstring_free (outbuf
);
1480 * Reset (and free) any of the saved marker text
1484 free_markercomps(void)
1486 struct param_comp_list
*pc_entry
, *pc2
;
1491 description_comp
= NULL
;
1494 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1495 free(pc_entry
->param
);
1496 pc2
= pc_entry
->next
;
1501 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1502 free(pc_entry
->param
);
1503 pc2
= pc_entry
->next
;
1510 * Exit if the display process returned with a nonzero exit code, or terminated
1511 * with a SIGQUIT signal.
1515 pidcheck (int status
)
1517 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)