]>
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>
19 #include "mhshowsbr.h"
20 #include "sbr/m_mktemp.h"
23 #endif /* ! HAVE_ICONV */
31 /* flags for moreproc/header display */
35 /* for output markers and headers */
45 static void show_single_message (CT
, char *, int, int, int, struct format
*);
46 static void DisplayMsgHeader (CT
, char *, int);
47 static int show_switch (CT
, int, int, int, int, struct format
*);
48 static int show_content (CT
, int, int, int, struct format
*);
49 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
50 static int show_text (CT
, int, int, struct format
*);
51 static int show_multi (CT
, int, int, int, int, struct format
*);
52 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
53 static int show_multi_aux (CT
, int, char *, struct format
*);
54 static int show_message_rfc822 (CT
, int, struct format
*);
55 static int show_partial (CT
, int);
56 static int show_external (CT
, int, int, int, int, struct format
*);
57 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
58 size_t, int multipart
);
59 static int convert_content_charset (CT
, char **);
60 static struct format
*compile_header(char *);
61 static struct format
*compile_marker(char *);
62 static void output_header (CT
, struct format
*);
63 static void output_marker (CT
, struct format
*, int);
64 static void free_markercomps (void);
65 static int pidcheck(int);
68 * Components (and list of parameters/components) we care about for the
69 * content marker display.
72 static struct comp
*folder_comp
= NULL
;
73 static struct comp
*part_comp
= NULL
;
74 static struct comp
*ctype_comp
= NULL
;
75 static struct comp
*description_comp
= NULL
;
76 static struct comp
*dispo_comp
= NULL
;
78 struct param_comp_list
{
81 struct param_comp_list
*next
;
84 static struct param_comp_list
*ctype_pc_list
= NULL
;
85 static struct param_comp_list
*dispo_pc_list
= NULL
;
89 * Top level entry point to show/display a group of messages
93 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
96 struct format
*hfmt
, *mfmt
;
99 * If form is not specified, then get default form
100 * for showing headers of MIME messages.
103 formsw
= getcpy (etcpath ("mhl.headers"));
106 * Compile the content marker and header format lines
108 mfmt
= compile_marker(markerform
);
109 hfmt
= compile_header(headerform
);
112 * If form is "mhl.null", suppress display of header.
114 if (!strcmp (formsw
, "mhl.null"))
117 for (ctp
= cts
; *ctp
; ctp
++) {
120 /* if top-level type is ok, then display message */
121 if (type_ok (ct
, 1)) {
122 if (headersw
) output_header(ct
, hfmt
);
124 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
136 * Entry point to show/display a single message
140 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
141 int inlineonly
, struct format
*fmt
)
147 /* Allow user executable bit so that temporary directories created by
148 * the viewer (e.g., lynx) are going to be accessible */
149 umask (ct
->c_umask
& ~(0100));
152 * If you have a format file, then display
153 * the message headers.
156 DisplayMsgHeader(ct
, form
, concatsw
);
158 /* Show the body of the message */
159 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
165 if (ct
->c_ceclosefnx
)
166 (*ct
->c_ceclosefnx
) (ct
);
168 /* block a few signals */
170 sigaddset (&set
, SIGHUP
);
171 sigaddset (&set
, SIGINT
);
172 sigaddset (&set
, SIGQUIT
);
173 sigaddset (&set
, SIGTERM
);
174 sigprocmask (SIG_BLOCK
, &set
, &oset
);
176 while (!concatsw
&& wait (&status
) != NOTOK
) {
181 /* reset the signal mask */
182 sigprocmask (SIG_SETMASK
, &oset
, &set
);
189 * Use the mhlproc to show the header fields
193 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
200 vec
= argsplit(mhlproc
, &file
, &vecp
);
201 vec
[vecp
++] = mh_xstrdup("-form");
202 vec
[vecp
++] = mh_xstrdup(form
);
203 vec
[vecp
++] = mh_xstrdup("-nobody");
204 vec
[vecp
++] = getcpy(ct
->c_file
);
207 * If we've specified -(no)moreproc,
208 * then just pass that along.
210 if (nomore
|| concatsw
) {
211 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
213 vec
[vecp
++] = mh_xstrdup("-moreproc");
214 vec
[vecp
++] = mh_xstrdup(progsw
);
223 adios ("fork", "unable to");
228 fprintf (stderr
, "unable to exec ");
234 pidcheck(pidwait(child_id
, NOTOK
));
238 arglist_free(file
, vec
);
243 * Switching routine. Call the correct routine
244 * based on content type.
248 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
251 switch (ct
->c_type
) {
253 return show_multi (ct
, alternate
, concatsw
, textonly
,
257 switch (ct
->c_subtype
) {
258 case MESSAGE_PARTIAL
:
259 return show_partial (ct
, alternate
);
261 case MESSAGE_EXTERNAL
:
262 return show_external (ct
, alternate
, concatsw
, textonly
,
266 return show_message_rfc822 (ct
, alternate
, fmt
);
269 * Treat unknown message types as equivalent to
270 * application/octet-stream for now
273 return show_content (ct
, alternate
, textonly
,
278 return show_text (ct
, alternate
, concatsw
, fmt
);
285 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
288 return 0; /* NOT REACHED */
293 * Generic method for displaying content
297 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
301 CI ci
= &ct
->c_ctinfo
;
304 * If we're here, we are not a text type. So we don't need to check
308 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
309 output_marker(ct
, fmt
, 1);
313 /* Check for invo_name-show-type[/subtype] */
314 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
315 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
317 if ((cp
= ct
->c_showproc
))
318 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
320 /* complain if we are not a part of a multipart/alternative */
322 content_error (NULL
, ct
, "don't know how to display content");
329 * Parse the display string for displaying generic content
333 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
336 int xstdin
= 0, xlist
= 0;
337 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
339 if (!ct
->c_ceopenfnx
) {
341 content_error (NULL
, ct
, "don't know how to decode content");
346 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
348 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
351 if (! strcmp(invo_name
, "mhshow") &&
352 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
353 /* This has to be done after calling c_ceopenfnx, so
354 unfortunately the type checks are necessary without
355 some code rearrangement. And to make this really ugly,
356 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
357 if (convert_content_charset (ct
, &file
) == OK
) {
358 (*ct
->c_ceclosefnx
) (ct
);
359 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
362 char *charset
= content_charset (ct
);
363 inform("unable to convert character set%s%s from %s, continuing...",
364 ct
->c_partno
? " of part " : "",
365 FENDNULL(ct
->c_partno
),
372 strncpy (buffer
, cp
, sizeof(buffer
));
376 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
377 sizeof(buffer
) - 1, 0)) {
378 inform("Buffer overflow constructing show command, continuing...");
383 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
384 fd
, xlist
, xstdin
, fmt
);
389 * Routine to actually display the content
393 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
394 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
400 if (debugsw
|| cracked
) {
403 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
406 fprintf (stderr
, " part %s", ct
->c_partno
);
408 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
410 fprintf (stderr
, " using command %s\n", buffer
);
414 output_marker(ct
, fmt
, 0);
418 * If the command is a zero-length string, just write the output on
422 if (buffer
[0] == '\0') {
423 char readbuf
[BUFSIZ
];
425 char lastchar
= '\n';
428 inform("Cannot use NULL command to display content-type "
429 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
433 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
434 if ((ssize_t
) fwrite(readbuf
, sizeof(char), cc
, stdout
) < cc
) {
435 advise ("putline", "fwrite");
437 lastchar
= readbuf
[cc
- 1];
441 advise("read", "while reading text content");
446 * The MIME standards allow content to not have a trailing newline.
447 * But because we are (presumably) sending this to stdout, include
448 * a newline for text content if the final character was not a
449 * newline. Only do this for mhshow.
452 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
453 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
462 vec
= argsplit(buffer
, &file
, &vecp
);
470 advise ("fork", "unable to");
471 (*ct
->c_ceclosefnx
) (ct
);
476 if (chdir (cracked
) < 0) {
477 advise (cracked
, "chdir");
484 fprintf (stderr
, "unable to exec ");
491 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
492 /* Copy the real display program name. This relies on the
493 specific construction of vec[] by argsplit(). */
497 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
499 arglist_free(file
, vec
);
501 (*ct
->c_ceclosefnx
) (ct
);
502 return (alternate
? OK
: status
);
509 * show content of type "text"
513 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
515 char *cp
, buffer
[BUFSIZ
];
516 CI ci
= &ct
->c_ctinfo
;
518 /* Check for invo_name-show-type[/subtype] */
519 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
520 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
523 * Use default method if content is text/plain, or if
524 * if it is not a text part of a multipart/alternative
526 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
529 snprintf(buffer
, sizeof(buffer
), "%%lcat");
531 snprintf(buffer
, sizeof(buffer
), "%%l");
533 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
534 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
535 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
536 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
544 * show message body of type "multipart"
548 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
552 CI ci
= &ct
->c_ctinfo
;
554 /* Check for invo_name-show-type[/subtype] */
555 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
556 return show_multi_aux (ct
, alternate
, cp
, fmt
);
558 if ((cp
= ct
->c_showproc
)) {
559 return show_multi_aux (ct
, alternate
, cp
, fmt
);
563 * Use default method to display this multipart content. Even
564 * unknown types are displayable, since they're treated as mixed
567 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
573 * show message body of subtypes of multipart that
574 * we understand directly (mixed, alternate, etc...)
578 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
579 int inlineonly
, struct format
*fmt
)
581 int alternating
, nowalternate
, result
;
582 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
591 nowalternate
= alternate
;
593 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
599 * alternate -> we are a part inside a multipart/alternative
600 * alternating -> we are a multipart/alternative
608 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
611 /* while looking for the right displayable alternative, we
612 * use a looser search criterion than we do after finding it.
613 * specifically, while still looking, part_ok() will match
614 * "parent" parts (e.g. "-part 2" where 2 is a high-level
615 * multipart). after finding it, we use part_exact() to only
616 * choose a part that was requested explicitly.
618 if ((part_exact(p
) && type_ok(p
, 1)) ||
619 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
623 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
625 switch (inneresult
) {
626 case NOTOK
: /* hard display error */
628 if (alternate
&& !alternating
) {
634 case DONE
: /* found no match on content type */
637 case OK
: /* display successful */
642 /* if we got success on a sub-part of
643 * multipart/alternative, we're done, unless
644 * there's a chance an explicit part should be
645 * matched later in the alternatives. */
648 } else if (alternate
) {
649 alternate
= nowalternate
= 0;
657 /* we're supposed to be displaying at least something from a
658 * multipart/alternative. if we've had parts to consider, and
659 * we've had no success, then we should complain. we shouldn't
660 * complain if none of the parts matched any -part or -type option.
662 if (alternating
&& request_matched
&& !display_success
) {
663 /* if we're ourselves an alternate. don't complain yet. */
665 content_error (NULL
, ct
, "don't know how to display any of the contents");
670 /* if no parts matched what was requested, there can't have been
671 * any display errors. we report DONE rather than OK. */
672 ret
= request_matched
? result
: DONE
;
678 * Parse display string for multipart content
679 * and use external program to display it.
683 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
685 /* xstdin is only used in the call to parse_display_string():
686 its value is ignored in the function. */
687 int xstdin
= 0, xlist
= 0;
688 char *file
= NULL
, buffer
[BUFSIZ
];
689 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
693 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
696 if (!p
->c_ceopenfnx
) {
698 content_error (NULL
, p
, "don't know how to decode content");
702 if (p
->c_storage
== NULL
) {
703 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
706 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
708 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
710 (*p
->c_ceclosefnx
) (p
);
714 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
715 buffer
, sizeof(buffer
) - 1, 1)) {
716 inform("Buffer overflow constructing show command, continuing...");
720 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
725 * show content of type "message/rfc822"
729 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
732 CI ci
= &ct
->c_ctinfo
;
734 /* Check for invo_name-show-type[/subtype] */
735 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
736 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
738 if ((cp
= ct
->c_showproc
))
739 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
741 /* default method for message/rfc822 */
742 if (ct
->c_subtype
== MESSAGE_RFC822
) {
743 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
744 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
747 /* complain if we are not a part of a multipart/alternative */
749 content_error (NULL
, ct
, "don't know how to display content");
756 * Show content of type "message/partial".
760 show_partial (CT ct
, int alternate
)
762 NMH_UNUSED (alternate
);
764 content_error (NULL
, ct
,
765 "in order to display this message, you must reassemble it");
771 * Show content of type "message/external".
773 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
777 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
780 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
781 CT p
= e
->eb_content
;
786 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
791 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
792 char *file
, char *buffer
, size_t buflen
,
795 char *bp
= buffer
, *pp
;
796 CI ci
= &ct
->c_ctinfo
;
798 bp
[0] = bp
[buflen
] = '\0';
800 for ( ; *cp
&& buflen
> 0; cp
++) {
806 /* insert parameters from Content-Type field */
811 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
812 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
813 get_param_value(pm
, '?'));
823 /* insert content description */
827 s
= trimcpy (ct
->c_descr
);
828 strncpy (bp
, s
, buflen
);
834 /* no longer implemented */
838 /* %f, and stdin is terminal not content */
844 /* insert filename(s) containing content */
845 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
850 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
853 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
860 /* insert filename containing content */
861 snprintf (bp
, buflen
, "%s", file
);
864 * Old comments below are left here for posterity.
865 * This was/is tricky.
867 /* since we've quoted the file argument, set things up
868 * to look past it, to avoid problems with the quoting
869 * logic below. (I know, I should figure out what's
870 * broken with the quoting logic, but..)
873 * Here's the email that submitted the patch with
875 * https://www.mail-archive.com/nmh-workers@mhost.com/
877 * I can't tell from that exactly what was broken,
878 * beyond misquoting of the filename. The profile
879 * had appearances of %F both with and without quotes.
880 * The unquoted ones should have been quoted by the
882 * The fix was to always quote the filename. But
883 * that broke '%F' because it expanded to ''filename''.
886 * Old comments above are left here for posterity.
887 * The quoting below should work properly now.
893 /* No longer supported */
897 /* display listing prior to displaying content */
902 /* insert subtype of content */
903 strncpy (bp
, ci
->ci_subtype
, buflen
);
907 /* insert character % */
911 const char *closing_brace
= strchr(cp
, '}');
914 const size_t param_len
= closing_brace
- cp
- 1;
915 char *param
= mh_xmalloc(param_len
+ 1);
918 (void) strncpy(param
, cp
+ 1, param_len
);
919 param
[param_len
] = '\0';
920 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
923 cp
+= param_len
+ 1; /* Skip both braces, too. */
926 /* %{param} is set in the Content-Type header.
927 After the break below, quote it if necessary. */
928 (void) strncpy(bp
, value
, buflen
);
931 /* %{param} not found, so skip it completely. cp
932 was advanced above. */
936 /* This will get confused if there are multiple %{}'s,
937 but its real purpose is to avoid doing bad things
938 above if a closing brace wasn't found. */
939 inform("no closing brace for display string escape %s, continuing...",
956 /* Did we actually insert something? */
958 /* Insert single quote if not inside quotes already */
959 if (!quoted
&& buflen
) {
961 memmove (pp
+ 1, pp
, len
+1);
967 /* Escape existing quotes */
968 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
971 /* Quoted. Let this quote close that quoting.
972 Insert an escaped quote to replace it and
973 another quote to reopen quoting, which will be
975 memmove (pp
+ 2, pp
, len
);
982 /* Not quoted. This should not be reached with
983 the current code, but handle the condition
984 in case the code changes. Just escape the
986 memmove (pp
, pp
-1, len
+1);
992 /* If pp is still set, that means we ran out of space. */
996 if (quoted
&& buflen
) {
997 /* See if we need to close the quote by looking
998 for an odd number of unescaped close quotes in
999 the remainder of the display string. */
1000 int found_quote
= 0, escaped
= 0;
1003 for (c
= cp
+1; *c
; ++c
) {
1005 escaped
= ! escaped
;
1011 found_quote
= ! found_quote
;
1016 if (! found_quote
) {
1036 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1037 /* content_error would provide a more useful error message
1038 * here, except that if we got overrun, it probably would
1044 /* use charset string to modify display method */
1045 if (ct
->c_termproc
) {
1048 strncpy (term
, buffer
, sizeof(term
));
1049 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1057 convert_charset (CT ct
, char *dest_charset
, int *message_mods
) {
1058 char *src_charset
= content_charset (ct
);
1061 if (strcasecmp (src_charset
, dest_charset
)) {
1063 iconv_t conv_desc
= NULL
;
1070 int opened_input_file
= 0;
1071 char src_buffer
[BUFSIZ
];
1072 size_t dest_buffer_size
= BUFSIZ
;
1073 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1076 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1078 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1080 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1085 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1086 adios (NULL
, "unable to create temporary file in %s",
1089 dest
= mh_xstrdup(tempfile
);
1091 if (ct
->c_cefile
.ce_file
) {
1092 file
= &ct
->c_cefile
.ce_file
;
1093 fp
= &ct
->c_cefile
.ce_fp
;
1095 } else if (ct
->c_file
) {
1098 begin
= (size_t) ct
->c_begin
;
1099 end
= (size_t) ct
->c_end
;
1100 } /* else no input file: shouldn't happen */
1102 if (file
&& *file
&& fp
) {
1104 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1105 advise (*file
, "unable to open for reading");
1108 opened_input_file
= 1;
1115 size_t bytes_to_read
=
1116 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1118 fseeko (*fp
, begin
, SEEK_SET
);
1119 while ((inbytes
= fread (src_buffer
, 1,
1120 min (bytes_to_read
, sizeof src_buffer
),
1122 ICONV_CONST
char *ib
= src_buffer
;
1123 char *ob
= dest_buffer
;
1124 size_t outbytes
= dest_buffer_size
;
1125 size_t outbytes_before
= outbytes
;
1127 if (end
> 0) bytes_to_read
-= inbytes
;
1130 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1132 if (errno
== E2BIG
) {
1134 * Bump up the buffer by at least a factor of 2
1135 * over what we need.
1137 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1138 dest_buffer_size
+= bumpup
;
1139 dest_buffer
= mh_xrealloc(dest_buffer
,
1141 ob
= dest_buffer
+ ob_off
;
1143 outbytes_before
+= bumpup
;
1146 if (errno
== EINVAL
) {
1147 /* middle of multi-byte sequence */
1148 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1149 advise (dest
, "write");
1151 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1152 if (end
> 0) bytes_to_read
+= inbytes
;
1153 /* inform("convert_charset: EINVAL"); */
1156 if (errno
== EILSEQ
) {
1157 /* invalid multi-byte sequence */
1159 for (++ib
, --inbytes
;
1161 (((unsigned char) *ib
) & 0xc0) == 0x80;
1165 ib
++; inbytes
--; /* skip it */
1167 (*ob
++) = '?'; outbytes
--;
1168 /* inform("convert_charset: EILSEQ"); */
1171 inform("convert_charset: errno = %d", errno
);
1176 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1177 advise (dest
, "write");
1181 if (opened_input_file
) {
1187 iconv_close (conv_desc
);
1191 /* Replace the decoded file with the converted one. */
1192 if (ct
->c_cefile
.ce_file
) {
1193 if (ct
->c_cefile
.ce_unlink
) {
1194 (void) m_unlink (ct
->c_cefile
.ce_file
);
1196 free (ct
->c_cefile
.ce_file
);
1198 ct
->c_cefile
.ce_file
= dest
;
1199 ct
->c_cefile
.ce_unlink
= 1;
1203 /* Update ct->c_ctline. */
1205 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1206 ct
->c_ctinfo
.ci_subtype
, NULL
);
1209 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1210 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1212 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1213 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1215 ctline
= add(outline
, ctline
);
1219 free (ct
->c_ctline
);
1220 ct
->c_ctline
= ctline
;
1221 } /* else no CT line, which is odd */
1223 /* Update Content-Type header field. */
1224 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1225 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1226 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1234 (void) m_unlink (dest
);
1237 #else /* ! HAVE_ICONV */
1238 NMH_UNUSED (message_mods
);
1240 inform("Can't convert %s to %s without iconv", src_charset
,
1244 #endif /* ! HAVE_ICONV */
1253 convert_content_charset (CT ct
, char **file
) {
1257 /* Using current locale, see if the content needs to be converted. */
1259 /* content_charset() cannot return NULL. */
1260 char *src_charset
= content_charset (ct
);
1262 if (! check_charset (src_charset
, strlen (src_charset
))) {
1265 char *dest_charset
= getcpy (get_charset ());
1267 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1268 *file
= ct
->c_cefile
.ce_file
;
1273 free (dest_charset
);
1276 #else /* ! HAVE_ICONV */
1279 #endif /* ! HAVE_ICONV */
1285 * Compile our format string and save any parameters we care about.
1288 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1289 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1290 "%<{description}%{description}" \
1291 "%?{cdispo-filename}%{cdispo-filename}" \
1292 "%|%{ctype-name}%> " \
1293 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1295 static struct format
*
1296 compile_header(char *form
)
1300 struct comp
*comp
= NULL
;
1301 unsigned int bucket
;
1303 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1305 (void) fmt_compile(fmtstring
, &fmt
, 1);
1308 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1309 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1317 static struct format
*
1318 compile_marker(char *form
)
1322 struct comp
*comp
= NULL
;
1323 unsigned int bucket
;
1324 struct param_comp_list
*pc_entry
;
1326 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1328 (void) fmt_compile(fmtstring
, &fmt
, 1);
1332 * Things we care about:
1334 * part - Part name (e.g., 1.1)
1335 * content-type - Content-Type
1336 * description - Content-Description
1337 * disposition - Content-Disposition (inline, attachment)
1338 * ctype-<param> - Content-Type parameter
1339 * cdispo-<param> - Content-Disposition parameter
1342 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1343 if (strcasecmp(comp
->c_name
, "part") == 0) {
1345 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1347 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1348 description_comp
= comp
;
1349 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1351 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1352 strlen(comp
->c_name
) > 6) {
1354 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1355 pc_entry
->comp
= comp
;
1356 pc_entry
->next
= ctype_pc_list
;
1357 ctype_pc_list
= pc_entry
;
1358 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1359 strlen(comp
->c_name
) > 7) {
1361 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1362 pc_entry
->comp
= comp
;
1363 pc_entry
->next
= dispo_pc_list
;
1364 dispo_pc_list
= pc_entry
;
1372 * Output on stdout an appropriate marker for this content, using mh-format
1376 output_header(CT ct
, struct format
*fmt
)
1378 charstring_t outbuf
= charstring_create (BUFSIZ
);
1384 folder_comp
->c_text
= getcpy(folder
);
1386 if (ct
->c_file
&& *ct
->c_file
) {
1387 message
= strtol(ct
->c_file
, &endp
, 10);
1388 if (*endp
) message
= 0;
1392 /* it would be nice to populate dat[2], for %(size) here,
1393 * but it's not available. it might also be nice to know
1394 * if the message originally had any mime parts or not -- but
1395 * there's also no record of that. (except for MIME-version:)
1398 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1400 fputs(charstring_buffer (outbuf
), stdout
);
1401 charstring_free (outbuf
);
1407 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1409 charstring_t outbuf
= charstring_create (BUFSIZ
);
1410 struct param_comp_list
*pcentry
;
1417 * Grab any items we care about.
1420 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1421 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1422 ct
->c_ctinfo
.ci_subtype
, NULL
);
1425 if (part_comp
&& ct
->c_partno
) {
1426 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1429 if (description_comp
&& ct
->c_descr
) {
1430 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1433 if (dispo_comp
&& ct
->c_dispo_type
) {
1434 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1437 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1438 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1439 pcentry
->param
, '?', 0);
1442 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1443 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1444 pcentry
->param
, '?', 0);
1447 if (ct
->c_cesizefnx
)
1448 partsize
= (*ct
->c_cesizefnx
) (ct
);
1450 partsize
= ct
->c_end
- ct
->c_begin
;
1452 if (ct
->c_file
&& *ct
->c_file
) {
1453 message
= strtol(ct
->c_file
, &endp
, 10);
1454 if (*endp
) message
= 0;
1459 /* make the part's hidden aspect available by overloading the
1460 * %(unseen) function. make the part's size available via %(size).
1461 * see comments in h/fmt_scan.h.
1465 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1467 fputs(charstring_buffer (outbuf
), stdout
);
1468 charstring_free (outbuf
);
1474 * Reset (and free) any of the saved marker text
1478 free_markercomps(void)
1480 struct param_comp_list
*pc_entry
, *pc2
;
1485 description_comp
= NULL
;
1488 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1489 free(pc_entry
->param
);
1490 pc2
= pc_entry
->next
;
1495 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1496 free(pc_entry
->param
);
1497 pc2
= pc_entry
->next
;
1504 * Exit if the display process returned with a nonzero exit code, or terminated
1505 * with a SIGQUIT signal.
1509 pidcheck (int status
)
1511 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)