]>
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 #endif /* ! HAVE_ICONV */
30 /* flags for moreproc/header display */
34 /* for output markerss and headers */
44 int type_ok (CT
, int);
45 void content_error (char *, CT
, char *, ...);
46 void flush_errors (void);
51 static void show_single_message (CT
, char *, int, int, int, struct format
*);
52 static void DisplayMsgHeader (CT
, char *, int);
53 static int show_switch (CT
, int, int, int, int, struct format
*);
54 static int show_content (CT
, int, int, int, struct format
*);
55 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
56 static int show_text (CT
, int, int, struct format
*);
57 static int show_multi (CT
, int, int, int, int, struct format
*);
58 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
59 static int show_multi_aux (CT
, int, char *, struct format
*);
60 static int show_message_rfc822 (CT
, int, struct format
*);
61 static int show_partial (CT
, int);
62 static int show_external (CT
, int, int, int, int, struct format
*);
63 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
64 size_t, int multipart
);
65 static int convert_content_charset (CT
, char **);
66 static struct format
*compile_header(char *);
67 static struct format
*compile_marker(char *);
68 static void output_header (CT
, struct format
*);
69 static void output_marker (CT
, struct format
*, int);
70 static void free_markercomps (void);
71 static int pidcheck(int);
74 * Components (and list of parameters/components) we care about for the
75 * content marker display.
78 static struct comp
*folder_comp
= NULL
;
79 static struct comp
*part_comp
= NULL
;
80 static struct comp
*ctype_comp
= NULL
;
81 static struct comp
*description_comp
= NULL
;
82 static struct comp
*dispo_comp
= NULL
;
84 struct param_comp_list
{
87 struct param_comp_list
*next
;
90 static struct param_comp_list
*ctype_pc_list
= NULL
;
91 static struct param_comp_list
*dispo_pc_list
= NULL
;
95 * Top level entry point to show/display a group of messages
99 show_all_messages (CT
*cts
, int concatsw
, int textonly
, int inlineonly
)
102 struct format
*hfmt
, *mfmt
;
105 * If form is not specified, then get default form
106 * for showing headers of MIME messages.
109 formsw
= getcpy (etcpath ("mhl.headers"));
112 * Compile the content marker and header format lines
114 mfmt
= compile_marker(markerform
);
115 hfmt
= compile_header(headerform
);
118 * If form is "mhl.null", suppress display of header.
120 if (!strcmp (formsw
, "mhl.null"))
123 for (ctp
= cts
; *ctp
; ctp
++) {
126 /* if top-level type is ok, then display message */
127 if (type_ok (ct
, 1)) {
128 if (headersw
) output_header(ct
, hfmt
);
130 show_single_message (ct
, formsw
, concatsw
, textonly
, inlineonly
,
142 * Entry point to show/display a single message
146 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
147 int inlineonly
, struct format
*fmt
)
153 /* Allow user executable bit so that temporary directories created by
154 * the viewer (e.g., lynx) are going to be accessible */
155 umask (ct
->c_umask
& ~(0100));
158 * If you have a format file, then display
159 * the message headers.
162 DisplayMsgHeader(ct
, form
, concatsw
);
164 /* Show the body of the message */
165 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
171 if (ct
->c_ceclosefnx
)
172 (*ct
->c_ceclosefnx
) (ct
);
174 /* block a few signals */
176 sigaddset (&set
, SIGHUP
);
177 sigaddset (&set
, SIGINT
);
178 sigaddset (&set
, SIGQUIT
);
179 sigaddset (&set
, SIGTERM
);
180 sigprocmask (SIG_BLOCK
, &set
, &oset
);
182 while (!concatsw
&& wait (&status
) != NOTOK
) {
187 /* reset the signal mask */
188 sigprocmask (SIG_SETMASK
, &oset
, &set
);
195 * Use the mhlproc to show the header fields
199 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
206 vec
= argsplit(mhlproc
, &file
, &vecp
);
207 vec
[vecp
++] = mh_xstrdup("-form");
208 vec
[vecp
++] = mh_xstrdup(form
);
209 vec
[vecp
++] = mh_xstrdup("-nobody");
210 vec
[vecp
++] = getcpy(ct
->c_file
);
213 * If we've specified -(no)moreproc,
214 * then just pass that along.
216 if (nomore
|| concatsw
) {
217 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
219 vec
[vecp
++] = mh_xstrdup("-moreproc");
220 vec
[vecp
++] = mh_xstrdup(progsw
);
226 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
231 adios ("fork", "unable to");
236 fprintf (stderr
, "unable to exec ");
242 pidcheck(pidwait(child_id
, NOTOK
));
246 arglist_free(file
, vec
);
251 * Switching routine. Call the correct routine
252 * based on content type.
256 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
259 switch (ct
->c_type
) {
261 return show_multi (ct
, alternate
, concatsw
, textonly
,
265 switch (ct
->c_subtype
) {
266 case MESSAGE_PARTIAL
:
267 return show_partial (ct
, alternate
);
269 case MESSAGE_EXTERNAL
:
270 return show_external (ct
, alternate
, concatsw
, textonly
,
274 return show_message_rfc822 (ct
, alternate
, fmt
);
277 * Treat unknown message types as equivalent to
278 * application/octet-stream for now
281 return show_content (ct
, alternate
, textonly
,
286 return show_text (ct
, alternate
, concatsw
, fmt
);
293 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
296 return 0; /* NOT REACHED */
301 * Generic method for displaying content
305 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
309 CI ci
= &ct
->c_ctinfo
;
312 * If we're here, we are not a text type. So we don't need to check
316 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
317 output_marker(ct
, fmt
, 1);
321 /* Check for invo_name-show-type[/subtype] */
322 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
323 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
325 if ((cp
= ct
->c_showproc
))
326 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
328 /* complain if we are not a part of a multipart/alternative */
330 content_error (NULL
, ct
, "don't know how to display content");
337 * Parse the display string for displaying generic content
341 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
344 int xstdin
= 0, xlist
= 0;
345 char *file
= NULL
, buffer
[BUFSIZ
];
347 if (!ct
->c_ceopenfnx
) {
349 content_error (NULL
, ct
, "don't know how to decode content");
354 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
356 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
359 if (! strcmp(invo_name
, "mhshow") &&
360 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
361 /* This has to be done after calling c_ceopenfnx, so
362 unfortunately the type checks are necessary without
363 some code rearrangement. And to make this really ugly,
364 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
365 if (convert_content_charset (ct
, &file
) == OK
) {
366 (*ct
->c_ceclosefnx
) (ct
);
367 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
370 char *charset
= content_charset (ct
);
371 inform("unable to convert character set%s%s from %s, continuing...",
372 ct
->c_partno
? " of part " : "",
373 ct
->c_partno
? ct
->c_partno
: "",
380 strncpy (buffer
, cp
, sizeof(buffer
));
384 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
385 sizeof(buffer
) - 1, 0)) {
386 inform("Buffer overflow constructing show command, continuing...");
391 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
392 fd
, xlist
, xstdin
, fmt
);
397 * Routine to actually display the content
401 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
402 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
408 if (debugsw
|| cracked
) {
411 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
414 fprintf (stderr
, " part %s", ct
->c_partno
);
416 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
418 fprintf (stderr
, " using command %s\n", buffer
);
422 output_marker(ct
, fmt
, 0);
426 * If the command is a zero-length string, just write the output on
430 if (buffer
[0] == '\0') {
431 char readbuf
[BUFSIZ
];
433 char lastchar
= '\n';
436 inform("Cannot use NULL command to display content-type "
437 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
441 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
442 if ((ssize_t
) fwrite(readbuf
, sizeof(char), cc
, stdout
) < cc
) {
443 advise ("putline", "fwrite");
445 lastchar
= readbuf
[cc
- 1];
449 advise("read", "while reading text content");
454 * The MIME standards allow content to not have a trailing newline.
455 * But because we are (presumably) sending this to stdout, include
456 * a newline for text content if the final character was not a
457 * newline. Only do this for mhshow.
460 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
461 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
470 vec
= argsplit(buffer
, &file
, &vecp
);
475 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
479 advise ("fork", "unable to");
480 (*ct
->c_ceclosefnx
) (ct
);
485 if (chdir (cracked
) < 0) {
486 advise (cracked
, "chdir");
493 fprintf (stderr
, "unable to exec ");
501 arglist_free(file
, vec
);
503 pidcheck ((status
= pidXwait (child_id
, NULL
)));
506 (*ct
->c_ceclosefnx
) (ct
);
507 return (alternate
? OK
: status
);
514 * show content of type "text"
518 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
520 char *cp
, buffer
[BUFSIZ
];
521 CI ci
= &ct
->c_ctinfo
;
523 /* Check for invo_name-show-type[/subtype] */
524 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
525 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
528 * Use default method if content is text/plain, or if
529 * if it is not a text part of a multipart/alternative
531 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
534 snprintf(buffer
, sizeof(buffer
), "%%lcat");
536 snprintf(buffer
, sizeof(buffer
), "%%l");
538 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
539 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
540 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
541 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
549 * show message body of type "multipart"
553 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
557 CI ci
= &ct
->c_ctinfo
;
559 /* Check for invo_name-show-type[/subtype] */
560 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
561 return show_multi_aux (ct
, alternate
, cp
, fmt
);
563 if ((cp
= ct
->c_showproc
)) {
564 return show_multi_aux (ct
, alternate
, cp
, fmt
);
568 * Use default method to display this multipart content. Even
569 * unknown types are displayable, since they're treated as mixed
572 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
578 * show message body of subtypes of multipart that
579 * we understand directly (mixed, alternate, etc...)
583 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
584 int inlineonly
, struct format
*fmt
)
586 int alternating
, nowalternate
, result
;
587 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
596 nowalternate
= alternate
;
598 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
604 * alternate -> we are a part inside a multipart/alternative
605 * alternating -> we are a multipart/alternative
613 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
616 /* while looking for the right displayable alternative, we
617 * use a looser search criterion than we do after finding it.
618 * specifically, while still looking, part_ok() will match
619 * "parent" parts (e.g. "-part 2" where 2 is a high-level
620 * multipart). after finding it, we use part_exact() to only
621 * choose a part that was requested explicitly.
623 if ((part_exact(p
) && type_ok(p
, 1)) ||
624 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
628 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
630 switch (inneresult
) {
631 case NOTOK
: /* hard display error */
633 if (alternate
&& !alternating
) {
639 case DONE
: /* found no match on content type */
642 case OK
: /* display successful */
647 /* if we got success on a sub-part of
648 * multipart/alternative, we're done, unless
649 * there's a chance an explicit part should be
650 * matched later in the alternatives. */
653 } else if (alternate
) {
654 alternate
= nowalternate
= 0;
662 /* we're supposed to be displaying at least something from a
663 * multipart/alternative. if we've had parts to consider, and
664 * we've had no success, then we should complain. we shouldn't
665 * complain if none of the parts matched any -part or -type option.
667 if (alternating
&& request_matched
&& !display_success
) {
668 /* if we're ourselves an alternate. don't complain yet. */
670 content_error (NULL
, ct
, "don't know how to display any of the contents");
675 /* if no parts matched what was requested, there can't have been
676 * any display errors. we report DONE rather than OK. */
677 ret
= request_matched
? result
: DONE
;
683 * Parse display string for multipart content
684 * and use external program to display it.
688 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
690 /* xstdin is only used in the call to parse_display_string():
691 its value is ignored in the function. */
692 int xstdin
= 0, xlist
= 0;
693 char *file
= NULL
, buffer
[BUFSIZ
];
694 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
698 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
701 if (!p
->c_ceopenfnx
) {
703 content_error (NULL
, p
, "don't know how to decode content");
707 if (p
->c_storage
== NULL
) {
708 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
711 p
->c_storage
= add (file
, NULL
);
713 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
715 (*p
->c_ceclosefnx
) (p
);
719 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
720 buffer
, sizeof(buffer
) - 1, 1)) {
721 inform("Buffer overflow constructing show command, continuing...");
725 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
730 * show content of type "message/rfc822"
734 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
737 CI ci
= &ct
->c_ctinfo
;
739 /* Check for invo_name-show-type[/subtype] */
740 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
741 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
743 if ((cp
= ct
->c_showproc
))
744 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
746 /* default method for message/rfc822 */
747 if (ct
->c_subtype
== MESSAGE_RFC822
) {
748 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
749 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
752 /* complain if we are not a part of a multipart/alternative */
754 content_error (NULL
, ct
, "don't know how to display content");
761 * Show content of type "message/partial".
765 show_partial (CT ct
, int alternate
)
767 NMH_UNUSED (alternate
);
769 content_error (NULL
, ct
,
770 "in order to display this message, you must reassemble it");
776 * Show content of type "message/external".
778 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
782 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
785 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
786 CT p
= e
->eb_content
;
791 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
796 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
797 char *file
, char *buffer
, size_t buflen
,
800 char *bp
= buffer
, *pp
;
801 CI ci
= &ct
->c_ctinfo
;
803 bp
[0] = bp
[buflen
] = '\0';
805 for ( ; *cp
&& buflen
> 0; cp
++) {
811 /* insert parameters from Content-Type field */
816 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
817 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
818 get_param_value(pm
, '?'));
828 /* insert content description */
832 s
= trimcpy (ct
->c_descr
);
833 strncpy (bp
, s
, buflen
);
839 /* no longer implemented */
843 /* %f, and stdin is terminal not content */
849 /* insert filename(s) containing content */
850 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
855 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
858 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
865 /* insert filename containing content */
866 snprintf (bp
, buflen
, "%s", file
);
869 * Old comments below are left here for posterity.
870 * This was/is tricky.
872 /* since we've quoted the file argument, set things up
873 * to look past it, to avoid problems with the quoting
874 * logic below. (I know, I should figure out what's
875 * broken with the quoting logic, but..)
878 * Here's the email that submitted the patch with
880 * https://www.mail-archive.com/nmh-workers@mhost.com/
882 * I can't tell from that exactly what was broken,
883 * beyond misquoting of the filename. The profile
884 * had appearances of %F both with and without quotes.
885 * The unquoted ones should have been quoted by the
887 * The fix was to always quote the filename. But
888 * that broke '%F' because it expanded to ''filename''.
891 * Old comments above are left here for posterity.
892 * The quoting below should work properly now.
898 /* No longer supported */
902 /* display listing prior to displaying content */
907 /* insert subtype of content */
908 strncpy (bp
, ci
->ci_subtype
, buflen
);
912 /* insert character % */
916 const char *closing_brace
= strchr(cp
, '}');
919 const size_t param_len
= closing_brace
- cp
- 1;
920 char *param
= mh_xmalloc(param_len
+ 1);
923 (void) strncpy(param
, cp
+ 1, param_len
);
924 param
[param_len
] = '\0';
925 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
928 cp
+= param_len
+ 1; /* Skip both braces, too. */
931 /* %{param} is set in the Content-Type header.
932 After the break below, quote it if necessary. */
933 (void) strncpy(bp
, value
, buflen
);
936 /* %{param} not found, so skip it completely. cp
937 was advanced above. */
941 /* This will get confused if there are multiple %{}'s,
942 but its real purpose is to avoid doing bad things
943 above if a closing brace wasn't found. */
944 inform("no closing brace for display string escape %s, continuing...",
961 /* Did we actually insert something? */
963 /* Insert single quote if not inside quotes already */
964 if (!quoted
&& buflen
) {
966 memmove (pp
+ 1, pp
, len
+1);
972 /* Escape existing quotes */
973 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
976 /* Quoted. Let this quote close that quoting.
977 Insert an escaped quote to replace it and
978 another quote to reopen quoting, which will be
980 memmove (pp
+ 2, pp
, len
);
987 /* Not quoted. This should not be reached with
988 the current code, but handle the condition
989 in case the code changes. Just escape the
991 memmove (pp
, pp
-1, len
+1);
997 /* If pp is still set, that means we ran out of space. */
1000 /* Close quoting. */
1001 if (quoted
&& buflen
) {
1002 /* See if we need to close the quote by looking
1003 for an odd number of unescaped close quotes in
1004 the remainder of the display string. */
1005 int found_quote
= 0, escaped
= 0;
1008 for (c
= cp
+1; *c
; ++c
) {
1010 escaped
= ! escaped
;
1016 found_quote
= ! found_quote
;
1021 if (! found_quote
) {
1041 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1042 /* content_error would provide a more useful error message
1043 * here, except that if we got overrun, it probably would
1049 /* use charset string to modify display method */
1050 if (ct
->c_termproc
) {
1053 strncpy (term
, buffer
, sizeof(term
));
1054 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1062 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 int opened_input_file
= 0;
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 adios (NULL
, "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
= 1;
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
);
1180 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
)
1182 advise (dest
, "write");
1187 if (opened_input_file
) {
1193 iconv_close (conv_desc
);
1197 /* Replace the decoded file with the converted one. */
1198 if (ct
->c_cefile
.ce_file
) {
1199 if (ct
->c_cefile
.ce_unlink
) {
1200 (void) m_unlink (ct
->c_cefile
.ce_file
);
1202 free (ct
->c_cefile
.ce_file
);
1204 ct
->c_cefile
.ce_file
= dest
;
1205 ct
->c_cefile
.ce_unlink
= 1;
1209 /* Update ct->c_ctline. */
1211 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1212 ct
->c_ctinfo
.ci_subtype
, NULL
);
1215 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1216 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1218 outline
= output_params(strlen(TYPE_FIELD
) + 1 + strlen(ctline
),
1219 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1221 ctline
= add(outline
, ctline
);
1225 free (ct
->c_ctline
);
1226 ct
->c_ctline
= ctline
;
1227 } /* else no CT line, which is odd */
1229 /* Update Content-Type header field. */
1230 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1231 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1232 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1240 (void) m_unlink (dest
);
1243 #else /* ! HAVE_ICONV */
1244 NMH_UNUSED (message_mods
);
1246 inform("Can't convert %s to %s without iconv", src_charset
,
1250 #endif /* ! HAVE_ICONV */
1259 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
)