]>
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.
10 #include <h/signals.h>
15 #include <h/mhparse.h>
16 #include <h/fmt_scan.h>
20 #include "mhshowsbr.h"
21 #include "sbr/m_mktemp.h"
24 #endif /* ! HAVE_ICONV */
32 /* flags for moreproc/header display */
36 /* for output markers and headers */
46 static void show_single_message (CT
, char *, int, int, int, struct format
*);
47 static void DisplayMsgHeader (CT
, char *, int);
48 static int show_switch (CT
, int, int, int, int, struct format
*);
49 static int show_content (CT
, int, int, int, struct format
*);
50 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
51 static int show_text (CT
, int, int, struct format
*);
52 static int show_multi (CT
, int, int, int, int, struct format
*);
53 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
54 static int show_multi_aux (CT
, int, char *, struct format
*);
55 static int show_message_rfc822 (CT
, int, struct format
*);
56 static int show_partial (CT
, int);
57 static int show_external (CT
, int, int, int, int, struct format
*);
58 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
59 size_t, int multipart
);
60 static int convert_content_charset (CT
, char **);
61 static struct format
*compile_header(char *);
62 static struct format
*compile_marker(char *);
63 static void output_header (CT
, struct format
*);
64 static void output_marker (CT
, struct format
*, int);
65 static void free_markercomps (void);
66 static int pidcheck(int);
69 * Components (and list of parameters/components) we care about for the
70 * content marker display.
73 static struct comp
*folder_comp
= NULL
;
74 static struct comp
*part_comp
= NULL
;
75 static struct comp
*ctype_comp
= NULL
;
76 static struct comp
*description_comp
= NULL
;
77 static struct comp
*dispo_comp
= NULL
;
79 struct param_comp_list
{
82 struct param_comp_list
*next
;
85 static struct param_comp_list
*ctype_pc_list
= NULL
;
86 static struct param_comp_list
*dispo_pc_list
= NULL
;
90 * Top level entry point to show/display a group of messages
94 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
97 struct format
*hfmt
, *mfmt
;
100 * If form is not specified, then get default form
101 * for showing headers of MIME messages.
104 formsw
= mh_xstrdup(etcpath("mhl.headers"));
107 * Compile the content marker and header format lines
109 mfmt
= compile_marker(markerform
);
110 hfmt
= compile_header(headerform
);
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)) {
123 if (headersw
) output_header(ct
, hfmt
);
125 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
137 * Entry point to show/display a single message
141 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
142 int inlineonly
, struct format
*fmt
)
148 /* Allow user executable bit so that temporary directories created by
149 * the viewer (e.g., lynx) are going to be accessible */
150 umask (ct
->c_umask
& ~(0100));
153 * If you have a format file, then display
154 * the message headers.
157 DisplayMsgHeader(ct
, form
, concatsw
);
159 /* Show the body of the message */
160 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
166 if (ct
->c_ceclosefnx
)
167 (*ct
->c_ceclosefnx
) (ct
);
169 /* block a few signals */
171 sigaddset (&set
, SIGHUP
);
172 sigaddset (&set
, SIGINT
);
173 sigaddset (&set
, SIGQUIT
);
174 sigaddset (&set
, SIGTERM
);
175 sigprocmask (SIG_BLOCK
, &set
, &oset
);
177 while (!concatsw
&& wait (&status
) != NOTOK
) {
182 /* reset the signal mask */
183 sigprocmask (SIG_SETMASK
, &oset
, &set
);
190 * Use the mhlproc to show the header fields
194 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
201 vec
= argsplit(mhlproc
, &file
, &vecp
);
202 vec
[vecp
++] = mh_xstrdup("-form");
203 vec
[vecp
++] = mh_xstrdup(form
);
204 vec
[vecp
++] = mh_xstrdup("-nobody");
205 vec
[vecp
++] = getcpy(ct
->c_file
);
208 * If we've specified -(no)moreproc,
209 * then just pass that along.
211 if (nomore
|| concatsw
) {
212 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
214 vec
[vecp
++] = mh_xstrdup("-moreproc");
215 vec
[vecp
++] = mh_xstrdup(progsw
);
224 adios ("fork", "unable to");
229 fprintf (stderr
, "unable to exec ");
235 pidcheck(pidwait(child_id
, NOTOK
));
239 arglist_free(file
, vec
);
244 * Switching routine. Call the correct routine
245 * based on content type.
249 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
252 switch (ct
->c_type
) {
254 return show_multi (ct
, alternate
, concatsw
, textonly
,
258 switch (ct
->c_subtype
) {
259 case MESSAGE_PARTIAL
:
260 return show_partial (ct
, alternate
);
262 case MESSAGE_EXTERNAL
:
263 return show_external (ct
, alternate
, concatsw
, textonly
,
267 return show_message_rfc822 (ct
, alternate
, fmt
);
270 * Treat unknown message types as equivalent to
271 * application/octet-stream for now
274 return show_content (ct
, alternate
, textonly
,
279 return show_text (ct
, alternate
, concatsw
, fmt
);
286 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
289 return 0; /* NOT REACHED */
294 * Generic method for displaying content
298 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
302 CI ci
= &ct
->c_ctinfo
;
305 * If we're here, we are not a text type. So we don't need to check
309 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
310 output_marker(ct
, fmt
, 1);
314 /* Check for invo_name-show-type[/subtype] */
315 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
316 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
318 if ((cp
= ct
->c_showproc
))
319 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
321 /* complain if we are not a part of a multipart/alternative */
323 content_error (NULL
, ct
, "don't know how to display content");
330 * Parse the display string for displaying generic content
334 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
337 int xstdin
= 0, xlist
= 0;
338 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
340 if (!ct
->c_ceopenfnx
) {
342 content_error (NULL
, ct
, "don't know how to decode content");
347 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
349 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
352 if (! strcmp(invo_name
, "mhshow") &&
353 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
354 /* This has to be done after calling c_ceopenfnx, so
355 unfortunately the type checks are necessary without
356 some code rearrangement. And to make this really ugly,
357 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
358 if (convert_content_charset (ct
, &file
) == OK
) {
359 (*ct
->c_ceclosefnx
) (ct
);
360 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
363 char *charset
= content_charset (ct
);
364 inform("unable to convert character set%s%s from %s, continuing...",
365 ct
->c_partno
? " of part " : "",
366 FENDNULL(ct
->c_partno
),
373 strncpy (buffer
, cp
, sizeof(buffer
));
377 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
378 sizeof(buffer
) - 1, 0)) {
379 inform("Buffer overflow constructing show command, continuing...");
384 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
385 fd
, xlist
, xstdin
, fmt
);
390 * Routine to actually display the content
394 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
395 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
401 if (debugsw
|| cracked
) {
404 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
407 fprintf (stderr
, " part %s", ct
->c_partno
);
409 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
411 fprintf (stderr
, " using command %s\n", buffer
);
415 output_marker(ct
, fmt
, 0);
419 * If the command is a zero-length string, just write the output on
423 if (buffer
[0] == '\0') {
424 char readbuf
[BUFSIZ
];
426 char lastchar
= '\n';
429 inform("Cannot use NULL command to display content-type "
430 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
434 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
435 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
436 advise ("putline", "fwrite");
438 lastchar
= readbuf
[cc
- 1];
442 advise("read", "while reading text content");
447 * The MIME standards allow content to not have a trailing newline.
448 * But because we are (presumably) sending this to stdout, include
449 * a newline for text content if the final character was not a
450 * newline. Only do this for mhshow.
453 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
454 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
463 vec
= argsplit(buffer
, &file
, &vecp
);
471 advise ("fork", "unable to");
472 (*ct
->c_ceclosefnx
) (ct
);
477 if (chdir (cracked
) < 0) {
478 advise (cracked
, "chdir");
485 fprintf (stderr
, "unable to exec ");
492 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
493 /* Copy the real display program name. This relies on the
494 specific construction of vec[] by argsplit(). */
498 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
500 arglist_free(file
, vec
);
502 (*ct
->c_ceclosefnx
) (ct
);
503 return alternate
? OK
: status
;
510 * show content of type "text"
514 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
516 char *cp
, buffer
[BUFSIZ
];
517 CI ci
= &ct
->c_ctinfo
;
519 /* Check for invo_name-show-type[/subtype] */
520 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
521 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
524 * Use default method if content is text/plain, or if
525 * if it is not a text part of a multipart/alternative
527 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
530 snprintf(buffer
, sizeof(buffer
), "%%lcat");
532 snprintf(buffer
, sizeof(buffer
), "%%l");
534 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
535 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
536 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
537 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
545 * show message body of type "multipart"
549 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
553 CI ci
= &ct
->c_ctinfo
;
555 /* Check for invo_name-show-type[/subtype] */
556 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
557 return show_multi_aux (ct
, alternate
, cp
, fmt
);
559 if ((cp
= ct
->c_showproc
)) {
560 return show_multi_aux (ct
, alternate
, cp
, fmt
);
564 * Use default method to display this multipart content. Even
565 * unknown types are displayable, since they're treated as mixed
568 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
574 * show message body of subtypes of multipart that
575 * we understand directly (mixed, alternate, etc...)
579 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
580 int inlineonly
, struct format
*fmt
)
582 int alternating
, nowalternate
, result
;
583 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
585 bool request_matched
;
586 bool display_success
;
592 nowalternate
= alternate
;
594 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
600 * alternate -> we are a part inside a multipart/alternative
601 * alternating -> we are a multipart/alternative
605 request_matched
= false;
606 display_success
= false;
607 mult_alt_done
= false;
609 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
612 /* while looking for the right displayable alternative, we
613 * use a looser search criterion than we do after finding it.
614 * specifically, while still looking, part_ok() will match
615 * "parent" parts (e.g. "-part 2" where 2 is a high-level
616 * multipart). after finding it, we use part_exact() to only
617 * choose a part that was requested explicitly.
619 if ((part_exact(p
) && type_ok(p
, 1)) ||
620 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
624 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
626 switch (inneresult
) {
627 case NOTOK
: /* hard display error */
628 request_matched
= true;
629 if (alternate
&& !alternating
) {
635 case DONE
: /* found no match on content type */
638 case OK
: /* display successful */
639 request_matched
= true;
640 display_success
= true;
643 /* if we got success on a sub-part of
644 * multipart/alternative, we're done, unless
645 * there's a chance an explicit part should be
646 * matched later in the alternatives. */
648 mult_alt_done
= true;
649 } else if (alternate
) {
650 alternate
= nowalternate
= 0;
658 /* we're supposed to be displaying at least something from a
659 * multipart/alternative. if we've had parts to consider, and
660 * we've had no success, then we should complain. we shouldn't
661 * complain if none of the parts matched any -part or -type option.
663 if (alternating
&& request_matched
&& !display_success
) {
664 /* if we're ourselves an alternate. don't complain yet. */
666 content_error (NULL
, ct
, "don't know how to display any of the contents");
671 /* if no parts matched what was requested, there can't have been
672 * any display errors. we report DONE rather than OK. */
673 ret
= request_matched
? result
: DONE
;
679 * Parse display string for multipart content
680 * and use external program to display it.
684 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
686 /* xstdin is only used in the call to parse_display_string():
687 its value is ignored in the function. */
688 int xstdin
= 0, xlist
= 0;
689 char *file
= NULL
, buffer
[BUFSIZ
];
690 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
694 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
697 if (!p
->c_ceopenfnx
) {
699 content_error (NULL
, p
, "don't know how to decode content");
703 if (p
->c_storage
== NULL
) {
704 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
707 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
709 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
711 (*p
->c_ceclosefnx
) (p
);
715 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
716 buffer
, sizeof(buffer
) - 1, 1)) {
717 inform("Buffer overflow constructing show command, continuing...");
721 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
726 * show content of type "message/rfc822"
730 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
733 CI ci
= &ct
->c_ctinfo
;
735 /* Check for invo_name-show-type[/subtype] */
736 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
737 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
739 if ((cp
= ct
->c_showproc
))
740 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
742 /* default method for message/rfc822 */
743 if (ct
->c_subtype
== MESSAGE_RFC822
) {
744 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
745 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
748 /* complain if we are not a part of a multipart/alternative */
750 content_error (NULL
, ct
, "don't know how to display content");
757 * Show content of type "message/partial".
761 show_partial (CT ct
, int alternate
)
763 NMH_UNUSED (alternate
);
765 content_error (NULL
, ct
,
766 "in order to display this message, you must reassemble it");
772 * Show content of type "message/external".
774 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
778 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
781 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
782 CT p
= e
->eb_content
;
787 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
792 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
793 char *file
, char *buffer
, size_t buflen
,
798 char *bp
= buffer
, *pp
;
799 CI ci
= &ct
->c_ctinfo
;
801 bp
[0] = bp
[buflen
] = '\0';
803 for ( ; *cp
&& buflen
> 0; cp
++) {
809 /* insert parameters from Content-Type field */
814 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
815 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
816 get_param_value(pm
, '?'));
826 /* insert content description */
830 s
= trimcpy (ct
->c_descr
);
831 strncpy (bp
, s
, buflen
);
837 /* no longer implemented */
841 /* %f, and stdin is terminal not content */
847 /* insert filename(s) containing content */
848 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
853 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
856 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
863 /* insert filename containing content */
864 snprintf (bp
, buflen
, "%s", file
);
867 * Old comments below are left here for posterity.
868 * This was/is tricky.
870 /* since we've quoted the file argument, set things up
871 * to look past it, to avoid problems with the quoting
872 * logic below. (I know, I should figure out what's
873 * broken with the quoting logic, but..)
876 * Here's the email that submitted the patch with
878 * https://www.mail-archive.com/nmh-workers@mhost.com/
880 * I can't tell from that exactly what was broken,
881 * beyond misquoting of the filename. The profile
882 * had appearances of %F both with and without quotes.
883 * The unquoted ones should have been quoted by the
885 * The fix was to always quote the filename. But
886 * that broke '%F' because it expanded to ''filename''.
889 * Old comments above are left here for posterity.
890 * The quoting below should work properly now.
896 /* No longer supported */
900 /* display listing prior to displaying content */
905 /* insert subtype of content */
906 strncpy (bp
, ci
->ci_subtype
, buflen
);
910 /* insert character % */
914 const char *closing_brace
= strchr(cp
, '}');
917 const size_t param_len
= closing_brace
- cp
- 1;
918 char *param
= mh_xmalloc(param_len
+ 1);
921 (void) strncpy(param
, cp
+ 1, param_len
);
922 param
[param_len
] = '\0';
923 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
926 cp
+= param_len
+ 1; /* Skip both braces, too. */
929 /* %{param} is set in the Content-Type header.
930 After the break below, quote it if necessary. */
931 (void) strncpy(bp
, value
, buflen
);
934 /* %{param} not found, so skip it completely. cp
935 was advanced above. */
939 /* This will get confused if there are multiple %{}'s,
940 but its real purpose is to avoid doing bad things
941 above if a closing brace wasn't found. */
942 inform("no closing brace for display string escape %s, continuing...",
959 /* Did we actually insert something? */
961 /* Insert single quote if not inside quotes already */
962 if (!quoted
&& buflen
) {
964 memmove (pp
+ 1, pp
, len
+1);
970 /* Escape existing quotes */
971 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
974 /* Quoted. Let this quote close that quoting.
975 Insert an escaped quote to replace it and
976 another quote to reopen quoting, which will be
978 memmove (pp
+ 2, pp
, len
);
985 /* Not quoted. This should not be reached with
986 the current code, but handle the condition
987 in case the code changes. Just escape the
989 memmove (pp
, pp
-1, len
+1);
995 /* If pp is still set, that means we ran out of space. */
999 if (quoted
&& buflen
) {
1000 /* See if we need to close the quote by looking
1001 for an odd number of unescaped close quotes in
1002 the remainder of the display string. */
1003 int found_quote
= 0, escaped
= 0;
1006 for (c
= cp
+1; *c
; ++c
) {
1008 escaped
= ! escaped
;
1014 found_quote
= ! found_quote
;
1019 if (! found_quote
) {
1039 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1040 /* content_error would provide a more useful error message
1041 * here, except that if we got overrun, it probably would
1047 /* use charset string to modify display method */
1048 if (ct
->c_termproc
) {
1051 strncpy (term
, buffer
, sizeof(term
));
1052 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1060 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1062 char *src_charset
= content_charset (ct
);
1065 if (strcasecmp (src_charset
, dest_charset
)) {
1067 iconv_t conv_desc
= NULL
;
1074 bool opened_input_file
= false;
1075 char src_buffer
[BUFSIZ
];
1076 size_t dest_buffer_size
= BUFSIZ
;
1077 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1080 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1082 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1084 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1089 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1090 die("unable to create temporary file in %s",
1093 dest
= mh_xstrdup(tempfile
);
1095 if (ct
->c_cefile
.ce_file
) {
1096 file
= &ct
->c_cefile
.ce_file
;
1097 fp
= &ct
->c_cefile
.ce_fp
;
1099 } else if (ct
->c_file
) {
1102 begin
= (size_t) ct
->c_begin
;
1103 end
= (size_t) ct
->c_end
;
1104 } /* else no input file: shouldn't happen */
1106 if (file
&& *file
&& fp
) {
1108 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1109 advise (*file
, "unable to open for reading");
1112 opened_input_file
= true;
1119 size_t bytes_to_read
=
1120 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1122 fseeko (*fp
, begin
, SEEK_SET
);
1123 while ((inbytes
= fread (src_buffer
, 1,
1124 min (bytes_to_read
, sizeof src_buffer
),
1126 ICONV_CONST
char *ib
= src_buffer
;
1127 char *ob
= dest_buffer
;
1128 size_t outbytes
= dest_buffer_size
;
1129 size_t outbytes_before
= outbytes
;
1131 if (end
> 0) bytes_to_read
-= inbytes
;
1134 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1136 if (errno
== E2BIG
) {
1138 * Bump up the buffer by at least a factor of 2
1139 * over what we need.
1141 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1142 dest_buffer_size
+= bumpup
;
1143 dest_buffer
= mh_xrealloc(dest_buffer
,
1145 ob
= dest_buffer
+ ob_off
;
1147 outbytes_before
+= bumpup
;
1150 if (errno
== EINVAL
) {
1151 /* middle of multi-byte sequence */
1152 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1153 advise (dest
, "write");
1155 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1156 if (end
> 0) bytes_to_read
+= inbytes
;
1157 /* inform("convert_charset: EINVAL"); */
1160 if (errno
== EILSEQ
) {
1161 /* invalid multi-byte sequence */
1163 for (++ib
, --inbytes
;
1165 (((unsigned char) *ib
) & 0xc0) == 0x80;
1169 ib
++; inbytes
--; /* skip it */
1171 (*ob
++) = '?'; outbytes
--;
1172 /* inform("convert_charset: EILSEQ"); */
1175 inform("convert_charset: errno = %d", errno
);
1180 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1181 advise (dest
, "write");
1185 if (opened_input_file
) {
1191 iconv_close (conv_desc
);
1195 /* Replace the decoded file with the converted one. */
1196 if (ct
->c_cefile
.ce_file
) {
1197 if (ct
->c_cefile
.ce_unlink
) {
1198 (void) m_unlink (ct
->c_cefile
.ce_file
);
1200 free (ct
->c_cefile
.ce_file
);
1202 ct
->c_cefile
.ce_file
= dest
;
1203 ct
->c_cefile
.ce_unlink
= 1;
1207 /* Update ct->c_ctline. */
1209 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1210 ct
->c_ctinfo
.ci_subtype
, NULL
);
1213 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1214 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1216 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1217 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1219 ctline
= add(outline
, ctline
);
1223 free (ct
->c_ctline
);
1224 ct
->c_ctline
= ctline
;
1225 } /* else no CT line, which is odd */
1227 /* Update Content-Type header field. */
1228 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1229 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1230 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1238 (void) m_unlink (dest
);
1241 #else /* ! HAVE_ICONV */
1242 NMH_UNUSED (message_mods
);
1244 inform("Can't convert %s to %s without iconv", src_charset
,
1248 #endif /* ! HAVE_ICONV */
1257 convert_content_charset (CT ct
, char **file
)
1262 /* Using current locale, see if the content needs to be converted. */
1264 /* content_charset() cannot return NULL. */
1265 char *src_charset
= content_charset (ct
);
1267 if (! check_charset (src_charset
, strlen (src_charset
))) {
1270 char *dest_charset
= getcpy (get_charset ());
1272 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1273 *file
= ct
->c_cefile
.ce_file
;
1278 free (dest_charset
);
1281 #else /* ! HAVE_ICONV */
1284 #endif /* ! HAVE_ICONV */
1290 * Compile our format string and save any parameters we care about.
1293 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1294 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1295 "%<{description}%{description}" \
1296 "%?{cdispo-filename}%{cdispo-filename}" \
1297 "%|%{ctype-name}%> " \
1298 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1300 static struct format
*
1301 compile_header(char *form
)
1305 struct comp
*comp
= NULL
;
1306 unsigned int bucket
;
1308 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1310 (void) fmt_compile(fmtstring
, &fmt
, 1);
1313 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1314 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1322 static struct format
*
1323 compile_marker(char *form
)
1327 struct comp
*comp
= NULL
;
1328 unsigned int bucket
;
1329 struct param_comp_list
*pc_entry
;
1331 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1333 (void) fmt_compile(fmtstring
, &fmt
, 1);
1337 * Things we care about:
1339 * part - Part name (e.g., 1.1)
1340 * content-type - Content-Type
1341 * description - Content-Description
1342 * disposition - Content-Disposition (inline, attachment)
1343 * ctype-<param> - Content-Type parameter
1344 * cdispo-<param> - Content-Disposition parameter
1347 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1348 if (strcasecmp(comp
->c_name
, "part") == 0) {
1350 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1352 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1353 description_comp
= comp
;
1354 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1356 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1357 strlen(comp
->c_name
) > 6) {
1359 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1360 pc_entry
->comp
= comp
;
1361 pc_entry
->next
= ctype_pc_list
;
1362 ctype_pc_list
= pc_entry
;
1363 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1364 strlen(comp
->c_name
) > 7) {
1366 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1367 pc_entry
->comp
= comp
;
1368 pc_entry
->next
= dispo_pc_list
;
1369 dispo_pc_list
= pc_entry
;
1377 * Output on stdout an appropriate marker for this content, using mh-format
1381 output_header(CT ct
, struct format
*fmt
)
1383 charstring_t outbuf
= charstring_create (BUFSIZ
);
1389 folder_comp
->c_text
= getcpy(folder
);
1391 if (ct
->c_file
&& *ct
->c_file
) {
1392 message
= strtol(ct
->c_file
, &endp
, 10);
1393 if (*endp
) message
= 0;
1397 /* it would be nice to populate dat[2], for %(size) here,
1398 * but it's not available. it might also be nice to know
1399 * if the message originally had any mime parts or not -- but
1400 * there's also no record of that. (except for MIME-version:)
1403 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1405 fputs(charstring_buffer (outbuf
), stdout
);
1406 charstring_free (outbuf
);
1412 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1414 charstring_t outbuf
= charstring_create (BUFSIZ
);
1415 struct param_comp_list
*pcentry
;
1422 * Grab any items we care about.
1425 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1426 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1427 ct
->c_ctinfo
.ci_subtype
, NULL
);
1430 if (part_comp
&& ct
->c_partno
) {
1431 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1434 if (description_comp
&& ct
->c_descr
) {
1435 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1438 if (dispo_comp
&& ct
->c_dispo_type
) {
1439 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1442 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1443 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1444 pcentry
->param
, '?', 0);
1447 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1448 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1449 pcentry
->param
, '?', 0);
1452 if (ct
->c_cesizefnx
)
1453 partsize
= (*ct
->c_cesizefnx
) (ct
);
1455 partsize
= ct
->c_end
- ct
->c_begin
;
1457 if (ct
->c_file
&& *ct
->c_file
) {
1458 message
= strtol(ct
->c_file
, &endp
, 10);
1459 if (*endp
) message
= 0;
1464 /* make the part's hidden aspect available by overloading the
1465 * %(unseen) function. make the part's size available via %(size).
1466 * see comments in h/fmt_scan.h.
1470 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1472 fputs(charstring_buffer (outbuf
), stdout
);
1473 charstring_free (outbuf
);
1479 * Reset (and free) any of the saved marker text
1483 free_markercomps(void)
1485 struct param_comp_list
*pc_entry
, *pc2
;
1490 description_comp
= NULL
;
1493 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1494 free(pc_entry
->param
);
1495 pc2
= pc_entry
->next
;
1500 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1501 free(pc_entry
->param
);
1502 pc2
= pc_entry
->next
;
1509 * Exit if the display process returned with a nonzero exit code, or terminated
1510 * with a SIGQUIT signal.
1514 pidcheck (int status
)
1516 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)