]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
3 * mhshowsbr.c -- routines to display the contents of MIME messages
5 * This code is Copyright (c) 2002, by the authors of nmh. See the
6 * COPYRIGHT file in the root directory of the nmh distribution for
7 * complete copyright information.
12 #include <h/signals.h>
18 #include <h/mhparse.h>
19 #include <h/fmt_scan.h>
23 #endif /* ! HAVE_ICONV */
31 /* flags for moreproc/header display */
37 int part_ok (CT
, int);
38 int type_ok (CT
, int);
39 void content_error (char *, CT
, char *, ...);
40 void flush_errors (void);
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_marker(char *);
61 static void output_marker (CT
, struct format
*, int);
62 static void free_markercomps (void);
63 static int pidcheck(int);
66 * Components (and list of parameters/components) we care about for the
67 * content marker display.
70 static struct comp
*part_comp
= NULL
;
71 static struct comp
*ctype_comp
= NULL
;
72 static struct comp
*description_comp
= NULL
;
73 static struct comp
*dispo_comp
= NULL
;
75 struct param_comp_list
{
78 struct param_comp_list
*next
;
81 static struct param_comp_list
*ctype_pc_list
= NULL
;
82 static struct param_comp_list
*dispo_pc_list
= NULL
;
86 * Top level entry point to show/display a group of messages
90 show_all_messages (CT
*cts
, int concatsw
, int textonly
, int inlineonly
,
97 * If form is not specified, then get default form
98 * for showing headers of MIME messages.
101 formsw
= getcpy (etcpath ("mhl.headers"));
104 * Compile the content marker format line
106 fmt
= compile_marker(markerform
);
109 * If form is "mhl.null", suppress display of header.
111 if (!strcmp (formsw
, "mhl.null"))
114 for (ctp
= cts
; *ctp
; ctp
++) {
117 /* if top-level type is ok, then display message */
119 show_single_message (ct
, formsw
, concatsw
, textonly
, inlineonly
,
129 * Entry point to show/display a single message
133 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
134 int inlineonly
, struct format
*fmt
)
140 /* Allow user executable bit so that temporary directories created by
141 * the viewer (e.g., lynx) are going to be accessible */
142 umask (ct
->c_umask
& ~(0100));
145 * If you have a format file, then display
146 * the message headers.
149 DisplayMsgHeader(ct
, form
, concatsw
);
151 /* Show the body of the message */
152 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
158 if (ct
->c_ceclosefnx
)
159 (*ct
->c_ceclosefnx
) (ct
);
161 /* block a few signals */
163 sigaddset (&set
, SIGHUP
);
164 sigaddset (&set
, SIGINT
);
165 sigaddset (&set
, SIGQUIT
);
166 sigaddset (&set
, SIGTERM
);
167 sigprocmask (SIG_BLOCK
, &set
, &oset
);
169 while (!concatsw
&& wait (&status
) != NOTOK
) {
174 /* reset the signal mask */
175 sigprocmask (SIG_SETMASK
, &oset
, &set
);
182 * Use the mhlproc to show the header fields
186 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
193 vec
= argsplit(mhlproc
, &file
, &vecp
);
194 vec
[vecp
++] = getcpy("-form");
195 vec
[vecp
++] = getcpy(form
);
196 vec
[vecp
++] = getcpy("-nobody");
197 vec
[vecp
++] = getcpy(ct
->c_file
);
200 * If we've specified -(no)moreproc,
201 * then just pass that along.
203 if (nomore
|| concatsw
) {
204 vec
[vecp
++] = getcpy("-nomoreproc");
206 vec
[vecp
++] = getcpy("-moreproc");
207 vec
[vecp
++] = getcpy(progsw
);
213 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
218 adios ("fork", "unable to");
223 fprintf (stderr
, "unable to exec ");
229 pidcheck(pidwait(child_id
, NOTOK
));
233 arglist_free(file
, vec
);
238 * Switching routine. Call the correct routine
239 * based on content type.
243 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
246 switch (ct
->c_type
) {
248 return show_multi (ct
, alternate
, concatsw
, textonly
,
252 switch (ct
->c_subtype
) {
253 case MESSAGE_PARTIAL
:
254 return show_partial (ct
, alternate
);
256 case MESSAGE_EXTERNAL
:
257 return show_external (ct
, alternate
, concatsw
, textonly
,
261 return show_message_rfc822 (ct
, alternate
, fmt
);
264 * Treat unknown message types as equivalent to
265 * application/octet-stream for now
268 return show_content (ct
, alternate
, textonly
,
273 return show_text (ct
, alternate
, concatsw
, fmt
);
280 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
283 return 0; /* NOT REACHED */
288 * Generic method for displaying content
292 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
295 char *cp
, buffer
[BUFSIZ
];
296 CI ci
= &ct
->c_ctinfo
;
299 * If we're here, we are not a text type. So we don't need to check
303 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
304 output_marker(ct
, fmt
, 1);
308 /* Check for invo_name-show-type/subtype */
309 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
310 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
311 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
312 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
314 /* Check for invo_name-show-type */
315 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
316 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
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
, buffer
[BUFSIZ
];
341 if (!ct
->c_ceopenfnx
) {
343 content_error (NULL
, ct
, "don't know how to decode content");
349 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
351 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
352 return (alternate
? DONE
: OK
);
354 if (! strcmp(invo_name
, "mhshow") &&
355 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
356 /* This has to be done after calling c_ceopenfnx, so
357 unfortunately the type checks are necessary without
358 some code rearrangement. And to make this really ugly,
359 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
360 if (convert_content_charset (ct
, &file
) == OK
) {
361 (*ct
->c_ceclosefnx
) (ct
);
362 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
365 admonish (NULL
, "unable to convert character set%s%s to %s",
366 ct
->c_partno
? " of part " : "",
367 ct
->c_partno
? ct
->c_partno
: "",
368 content_charset (ct
));
373 strncpy (buffer
, cp
, sizeof(buffer
));
377 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
378 sizeof(buffer
) - 1, 0)) {
379 admonish (NULL
, "Buffer overflow constructing show command!\n");
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 advise(NULL
, "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 fwrite(readbuf
, sizeof(char), cc
, stdout
);
436 lastchar
= readbuf
[cc
- 1];
440 advise("read", "while reading text content");
445 * The MIME standards allow content to not have a trailing newline.
446 * But because we are (presumably) sending this to stdout, include
447 * a newline for text content if the final character was not a
448 * newline. Only do this for mhshow.
451 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
452 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
461 vec
= argsplit(buffer
, &file
, &vecp
);
466 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
470 advise ("fork", "unable to");
471 (*ct
->c_ceclosefnx
) (ct
);
481 fprintf (stderr
, "unable to exec ");
487 arglist_free(file
, vec
);
489 pidcheck (pidXwait (child_id
, NULL
));
492 (*ct
->c_ceclosefnx
) (ct
);
493 return (alternate
? DONE
: OK
);
499 * show content of type "text"
503 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
505 char *cp
, buffer
[BUFSIZ
];
506 CI ci
= &ct
->c_ctinfo
;
508 /* Check for invo_name-show-type/subtype */
509 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
510 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
511 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
512 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
514 /* Check for invo_name-show-type */
515 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
516 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
517 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
520 * Use default method if content is text/plain, or if
521 * if it is not a text part of a multipart/alternative
523 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
526 snprintf(buffer
, sizeof(buffer
), "%%lcat");
528 snprintf(buffer
, sizeof(buffer
), "%%l");
530 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
531 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
532 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
533 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
541 * show message body of type "multipart"
545 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
548 char *cp
, buffer
[BUFSIZ
];
549 CI ci
= &ct
->c_ctinfo
;
551 /* Check for invo_name-show-type/subtype */
552 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
553 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
554 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
555 return show_multi_aux (ct
, alternate
, cp
, fmt
);
557 /* Check for invo_name-show-type */
558 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
559 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
560 return show_multi_aux (ct
, alternate
, cp
, fmt
);
562 if ((cp
= ct
->c_showproc
))
563 return show_multi_aux (ct
, alternate
, cp
, fmt
);
566 * Use default method to display this multipart content. Even
567 * unknown types are displayable, since they're treated as mixed
570 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
576 * show message body of subtypes of multipart that
577 * we understand directly (mixed, alternate, etc...)
581 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
582 int inlineonly
, struct format
*fmt
)
584 int alternating
, nowalternate
, result
;
585 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 an multipart/alternative
600 * alternating -> we are a multipart/alternative
603 result
= alternate
? NOTOK
: OK
;
606 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
609 if (part_ok (p
, 1) && type_ok (p
, 1)) {
614 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
616 switch (inneresult
) {
618 if (alternate
&& !alternating
) {
631 alternate
= nowalternate
= 0;
641 if (alternating
&& !part
&& any_part_ok
) {
643 content_error (NULL
, ct
, "don't know how to display any of the contents");
654 * Parse display string for multipart content
655 * and use external program to display it.
659 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
661 /* xstdin is only used in the call to parse_display_string():
662 its value is ignored in the function. */
663 int xstdin
= 0, xlist
= 0;
664 char *file
, buffer
[BUFSIZ
];
665 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
669 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
672 if (!p
->c_ceopenfnx
) {
674 content_error (NULL
, p
, "don't know how to decode content");
678 if (p
->c_storage
== NULL
) {
680 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
683 p
->c_storage
= add (file
, NULL
);
685 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
686 return (alternate
? DONE
: OK
);
687 (*p
->c_ceclosefnx
) (p
);
691 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
692 buffer
, sizeof(buffer
) - 1, 1)) {
693 admonish (NULL
, "Buffer overflow constructing show command!\n");
697 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
702 * show content of type "message/rfc822"
706 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
708 char *cp
, buffer
[BUFSIZ
];
709 CI ci
= &ct
->c_ctinfo
;
711 /* Check for invo_name-show-type/subtype */
712 snprintf (buffer
, sizeof(buffer
), "%s-show-%s/%s",
713 invo_name
, ci
->ci_type
, ci
->ci_subtype
);
714 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
715 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
717 /* Check for invo_name-show-type */
718 snprintf (buffer
, sizeof(buffer
), "%s-show-%s", invo_name
, ci
->ci_type
);
719 if ((cp
= context_find (buffer
)) && *cp
!= '\0')
720 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
722 if ((cp
= ct
->c_showproc
))
723 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
725 /* default method for message/rfc822 */
726 if (ct
->c_subtype
== MESSAGE_RFC822
) {
727 cp
= (ct
->c_showproc
= add ("%pshow -file %F", NULL
));
728 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
731 /* complain if we are not a part of a multipart/alternative */
733 content_error (NULL
, ct
, "don't know how to display content");
740 * Show content of type "message/partial".
744 show_partial (CT ct
, int alternate
)
746 NMH_UNUSED (alternate
);
748 content_error (NULL
, ct
,
749 "in order to display this message, you must reassemble it");
755 * Show content of type "message/external".
757 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
761 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
764 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
765 CT p
= e
->eb_content
;
770 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
775 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
776 char *file
, char *buffer
, size_t buflen
,
779 char *bp
= buffer
, *pp
;
780 CI ci
= &ct
->c_ctinfo
;
782 bp
[0] = bp
[buflen
] = '\0';
784 for ( ; *cp
&& buflen
> 0; cp
++) {
790 /* insert parameters from Content-Type field */
795 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
796 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
797 get_param_value(pm
, '?'));
807 /* insert content description */
811 s
= trimcpy (ct
->c_descr
);
812 strncpy (bp
, s
, buflen
);
818 /* no longer implemented */
822 /* %f, and stdin is terminal not content */
828 /* insert filename(s) containing content */
829 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
834 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
837 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
844 /* insert filename containing content */
845 snprintf (bp
, buflen
, "%s", file
);
848 * Old comments below are left here for posterity.
849 * This was/is tricky.
851 /* since we've quoted the file argument, set things up
852 * to look past it, to avoid problems with the quoting
853 * logic below. (I know, I should figure out what's
854 * broken with the quoting logic, but..)
857 * Here's the email that submitted the patch with
859 * https://www.mail-archive.com/nmh-workers@mhost.com/
861 * I can't tell from that exactly what was broken,
862 * beyond misquoting of the filename. The profile
863 * had appearances of %F both with and without quotes.
864 * The unquoted ones should have been quoted by the
866 * The fix was to always quote the filename. But
867 * that broke '%F' because it expanded to ''filename''.
870 * Old comments above are left here for posterity.
871 * The quoting below should work properly now.
877 /* No longer supported */
881 /* display listing prior to displaying content */
886 /* insert subtype of content */
887 strncpy (bp
, ci
->ci_subtype
, buflen
);
891 /* insert character % */
895 const char *closing_brace
= strchr(cp
, '}');
898 const size_t param_len
= closing_brace
- cp
- 1;
899 char *param
= mh_xmalloc(param_len
+ 1);
902 (void) strncpy(param
, cp
+ 1, param_len
);
903 param
[param_len
] = '\0';
904 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
907 cp
+= param_len
+ 1; /* Skip both braces, too. */
910 /* %{param} is set in the Content-Type header.
911 After the break below, quote it if necessary. */
912 (void) strncpy(bp
, value
, buflen
);
915 /* %{param} not found, so skip it completely. cp
916 was advanced above. */
920 /* This will get confused if there are multiple %{}'s,
921 but its real purpose is to avoid doing bad things
922 above if a closing brace wasn't found. */
924 "no closing brace for display string escape %s",
941 /* Did we actually insert something? */
943 /* Insert single quote if not inside quotes already */
944 if (!quoted
&& buflen
) {
946 memmove (pp
+ 1, pp
, len
+1);
952 /* Escape existing quotes */
953 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
956 /* Quoted. Let this quote close that quoting.
957 Insert an escaped quote to replace it and
958 another quote to reopen quoting, which will be
960 memmove (pp
+ 2, pp
, len
);
967 /* Not quoted. This should not be reached with
968 the current code, but handle the condition
969 in case the code changes. Just escape the
971 memmove (pp
, pp
-1, len
+1);
977 /* If pp is still set, that means we ran out of space. */
981 if (quoted
&& buflen
) {
982 /* See if we need to close the quote by looking
983 for an odd number of unescaped close quotes in
984 the remainder of the display string. */
985 int found_quote
= 0, escaped
= 0;
988 for (c
= cp
+1; *c
; ++c
) {
996 found_quote
= ! found_quote
;
1001 if (! found_quote
) {
1021 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1022 /* content_error would provide a more useful error message
1023 * here, except that if we got overrun, it probably would
1029 /* use charset string to modify display method */
1030 if (ct
->c_termproc
) {
1033 strncpy (term
, buffer
, sizeof(term
));
1034 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1042 convert_charset (CT ct
, char *dest_charset
, int *message_mods
) {
1043 char *src_charset
= content_charset (ct
);
1046 if (strcasecmp (src_charset
, dest_charset
)) {
1048 iconv_t conv_desc
= NULL
;
1055 int opened_input_file
= 0;
1056 char src_buffer
[BUFSIZ
];
1057 size_t dest_buffer_size
= BUFSIZ
;
1058 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1062 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1064 advise (NULL
, "Can't convert %s to %s", src_charset
, dest_charset
);
1068 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1069 adios (NULL
, "unable to create temporary file in %s",
1072 dest
= add (tempfile
, NULL
);
1074 if (ct
->c_cefile
.ce_file
) {
1075 file
= &ct
->c_cefile
.ce_file
;
1076 fp
= &ct
->c_cefile
.ce_fp
;
1078 } else if (ct
->c_file
) {
1081 begin
= (size_t) ct
->c_begin
;
1082 end
= (size_t) ct
->c_end
;
1083 } /* else no input file: shouldn't happen */
1085 if (file
&& *file
&& fp
) {
1087 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1088 advise (*file
, "unable to open for reading");
1091 opened_input_file
= 1;
1098 size_t bytes_to_read
=
1099 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1101 fseeko (*fp
, begin
, SEEK_SET
);
1102 while ((inbytes
= fread (src_buffer
, 1,
1103 min (bytes_to_read
, sizeof src_buffer
),
1105 ICONV_CONST
char *ib
= src_buffer
;
1106 char *ob
= dest_buffer
;
1107 size_t outbytes
= dest_buffer_size
;
1108 size_t outbytes_before
= outbytes
;
1110 if (end
> 0) bytes_to_read
-= inbytes
;
1113 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1115 if (errno
== E2BIG
) {
1117 * Bump up the buffer by at least a factor of 2
1118 * over what we need.
1120 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1121 dest_buffer_size
+= bumpup
;
1122 dest_buffer
= mh_xrealloc(dest_buffer
,
1124 ob
= dest_buffer
+ ob_off
;
1126 outbytes_before
+= bumpup
;
1132 write (fd
, dest_buffer
, outbytes_before
- outbytes
);
1136 if (opened_input_file
) {
1142 iconv_close (conv_desc
);
1146 /* Replace the decoded file with the converted one. */
1147 if (ct
->c_cefile
.ce_file
) {
1148 if (ct
->c_cefile
.ce_unlink
) {
1149 (void) m_unlink (ct
->c_cefile
.ce_file
);
1151 free (ct
->c_cefile
.ce_file
);
1153 ct
->c_cefile
.ce_file
= dest
;
1154 ct
->c_cefile
.ce_unlink
= 1;
1158 /* Update ct->c_ctline. */
1160 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1161 ct
->c_ctinfo
.ci_subtype
, NULL
);
1164 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1165 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1167 outline
= output_params(strlen(TYPE_FIELD
) + 1 + strlen(ctline
),
1168 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1170 ctline
= add(outline
, ctline
);
1174 free (ct
->c_ctline
);
1175 ct
->c_ctline
= ctline
;
1176 } /* else no CT line, which is odd */
1178 /* Update Content-Type header field. */
1179 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1180 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1181 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1189 (void) m_unlink (dest
);
1192 #else /* ! HAVE_ICONV */
1193 NMH_UNUSED (message_mods
);
1195 advise (NULL
, "Can't convert %s to %s without iconv", src_charset
,
1199 #endif /* ! HAVE_ICONV */
1207 convert_content_charset (CT ct
, char **file
) {
1211 /* Using current locale, see if the content needs to be converted. */
1213 /* content_charset() cannot return NULL. */
1214 char *charset
= content_charset (ct
);
1216 if (! check_charset (charset
, strlen (charset
))) {
1219 char *charset
= getcpy (get_charset ());
1221 if (convert_charset (ct
, charset
, &unused
) == 0) {
1222 *file
= ct
->c_cefile
.ce_file
;
1229 #else /* ! HAVE_ICONV */
1232 #endif /* ! HAVE_ICONV */
1238 * Compile our format string and save any parameters we care about.
1241 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1242 "%<{description}%{description}" \
1243 "%?{cdispo-filename}%{cdispo-filename}" \
1244 "%|%{ctype-name}%> " \
1245 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1247 static struct format
*
1248 compile_marker(char *markerform
)
1252 struct comp
*comp
= NULL
;
1253 unsigned int bucket
;
1254 struct param_comp_list
*pc_entry
;
1256 fmtstring
= new_fs(markerform
, NULL
, DEFAULT_MARKER
);
1258 (void) fmt_compile(fmtstring
, &fmt
, 1);
1262 * Things we care about:
1264 * part - Part name (e.g., 1.1)
1265 * content-type - Content-Type
1266 * description - Content-Description
1267 * disposition - Content-Disposition (inline, attachment)
1268 * ctype-<param> - Content-Type parameter
1269 * cdispo-<param> - Content-Disposition parameter
1272 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1273 if (strcasecmp(comp
->c_name
, "part") == 0) {
1275 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1277 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1278 description_comp
= comp
;
1279 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1281 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1282 strlen(comp
->c_name
) > 6) {
1283 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1284 pc_entry
->param
= getcpy(comp
->c_name
+ 6);
1285 pc_entry
->comp
= comp
;
1286 pc_entry
->next
= ctype_pc_list
;
1287 ctype_pc_list
= pc_entry
;
1288 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1289 strlen(comp
->c_name
) > 7) {
1290 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1291 pc_entry
->param
= getcpy(comp
->c_name
+ 7);
1292 pc_entry
->comp
= comp
;
1293 pc_entry
->next
= dispo_pc_list
;
1294 dispo_pc_list
= pc_entry
;
1302 * Output on stdout an appropriate marker for this content, using mh-format
1306 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1308 char outbuf
[BUFSIZ
];
1309 struct param_comp_list
*pcentry
;
1314 * Grab any items we care about.
1317 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1318 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1319 ct
->c_ctinfo
.ci_subtype
, NULL
);
1322 if (part_comp
&& ct
->c_partno
) {
1323 part_comp
->c_text
= getcpy(ct
->c_partno
);
1326 if (description_comp
&& ct
->c_descr
) {
1327 description_comp
->c_text
= getcpy(ct
->c_descr
);
1330 if (dispo_comp
&& ct
->c_dispo_type
) {
1331 dispo_comp
->c_text
= getcpy(ct
->c_dispo_type
);
1334 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1335 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1336 pcentry
->param
, '?', 0);
1339 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1340 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1341 pcentry
->param
, '?', 0);
1344 if (ct
->c_cesizefnx
)
1345 partsize
= (*ct
->c_cesizefnx
) (ct
);
1347 partsize
= ct
->c_end
- ct
->c_begin
;
1349 /* make the part's hidden aspect available by overloading the
1350 * %(unseen) function. make the part's size available via %(size).
1351 * see comments in h/fmt_scan.h.
1355 dat
[0] = dat
[1] = dat
[3] = 0;
1357 fmt_scan(fmt
, outbuf
, sizeof(outbuf
), sizeof(outbuf
), dat
, NULL
);
1359 fputs(outbuf
, stdout
);
1365 * Reset (and free) any of the saved marker text
1369 free_markercomps(void)
1371 struct param_comp_list
*pc_entry
, *pc2
;
1375 description_comp
= NULL
;
1378 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1379 free(pc_entry
->param
);
1380 pc2
= pc_entry
->next
;
1385 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1386 free(pc_entry
->param
);
1387 pc2
= pc_entry
->next
;
1394 * Exit if the display process returned with a nonzero exit code, or terminated
1395 * with a SIGQUIT signal.
1399 pidcheck (int status
)
1401 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)