]>
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>
17 #include <h/mhparse.h>
18 #include <h/fmt_scan.h>
22 #endif /* ! HAVE_ICONV */
32 /* flags for moreproc/header display */
40 int type_ok (CT
, int);
41 void content_error (char *, CT
, char *, ...);
42 void flush_errors (void);
47 static void show_single_message (CT
, char *, int, int, int, struct format
*);
48 static void DisplayMsgHeader (CT
, char *, int);
49 static int show_switch (CT
, int, int, int, int, struct format
*);
50 static int show_content (CT
, int, int, int, struct format
*);
51 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
52 static int show_text (CT
, int, int, struct format
*);
53 static int show_multi (CT
, int, int, int, int, struct format
*);
54 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
55 static int show_multi_aux (CT
, int, char *, struct format
*);
56 static int show_message_rfc822 (CT
, int, struct format
*);
57 static int show_partial (CT
, int);
58 static int show_external (CT
, int, int, int, int, struct format
*);
59 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
60 size_t, int multipart
);
61 static int convert_content_charset (CT
, char **);
62 static struct format
*compile_marker(char *);
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
*part_comp
= NULL
;
73 static struct comp
*ctype_comp
= NULL
;
74 static struct comp
*description_comp
= NULL
;
75 static struct comp
*dispo_comp
= NULL
;
77 struct param_comp_list
{
80 struct param_comp_list
*next
;
83 static struct param_comp_list
*ctype_pc_list
= NULL
;
84 static struct param_comp_list
*dispo_pc_list
= NULL
;
88 * Top level entry point to show/display a group of messages
92 show_all_messages (CT
*cts
, int concatsw
, int textonly
, int inlineonly
,
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 format line
108 fmt
= compile_marker(markerform
);
111 * If form is "mhl.null", suppress display of header.
113 if (!strcmp (formsw
, "mhl.null"))
116 for (ctp
= cts
; *ctp
; ctp
++) {
119 /* if top-level type is ok, then display message */
121 show_single_message (ct
, formsw
, concatsw
, textonly
, inlineonly
,
131 * Entry point to show/display a single message
135 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
136 int inlineonly
, struct format
*fmt
)
142 /* Allow user executable bit so that temporary directories created by
143 * the viewer (e.g., lynx) are going to be accessible */
144 umask (ct
->c_umask
& ~(0100));
147 * If you have a format file, then display
148 * the message headers.
151 DisplayMsgHeader(ct
, form
, concatsw
);
153 /* Show the body of the message */
154 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
160 if (ct
->c_ceclosefnx
)
161 (*ct
->c_ceclosefnx
) (ct
);
163 /* block a few signals */
165 sigaddset (&set
, SIGHUP
);
166 sigaddset (&set
, SIGINT
);
167 sigaddset (&set
, SIGQUIT
);
168 sigaddset (&set
, SIGTERM
);
169 sigprocmask (SIG_BLOCK
, &set
, &oset
);
171 while (!concatsw
&& wait (&status
) != NOTOK
) {
176 /* reset the signal mask */
177 sigprocmask (SIG_SETMASK
, &oset
, &set
);
184 * Use the mhlproc to show the header fields
188 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
195 vec
= argsplit(mhlproc
, &file
, &vecp
);
196 vec
[vecp
++] = getcpy("-form");
197 vec
[vecp
++] = getcpy(form
);
198 vec
[vecp
++] = getcpy("-nobody");
199 vec
[vecp
++] = getcpy(ct
->c_file
);
202 * If we've specified -(no)moreproc,
203 * then just pass that along.
205 if (nomore
|| concatsw
) {
206 vec
[vecp
++] = getcpy("-nomoreproc");
208 vec
[vecp
++] = getcpy("-moreproc");
209 vec
[vecp
++] = getcpy(progsw
);
215 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
220 adios ("fork", "unable to");
225 fprintf (stderr
, "unable to exec ");
231 pidcheck(pidwait(child_id
, NOTOK
));
235 arglist_free(file
, vec
);
240 * Switching routine. Call the correct routine
241 * based on content type.
245 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
248 switch (ct
->c_type
) {
250 return show_multi (ct
, alternate
, concatsw
, textonly
,
254 switch (ct
->c_subtype
) {
255 case MESSAGE_PARTIAL
:
256 return show_partial (ct
, alternate
);
258 case MESSAGE_EXTERNAL
:
259 return show_external (ct
, alternate
, concatsw
, textonly
,
263 return show_message_rfc822 (ct
, alternate
, fmt
);
266 * Treat unknown message types as equivalent to
267 * application/octet-stream for now
270 return show_content (ct
, alternate
, textonly
,
275 return show_text (ct
, alternate
, concatsw
, fmt
);
282 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
285 return 0; /* NOT REACHED */
290 * Generic method for displaying content
294 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
298 CI ci
= &ct
->c_ctinfo
;
301 * If we're here, we are not a text type. So we don't need to check
305 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
306 output_marker(ct
, fmt
, 1);
310 /* Check for invo_name-show-type[/subtype] */
311 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
312 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
314 if ((cp
= ct
->c_showproc
))
315 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
317 /* complain if we are not a part of a multipart/alternative */
319 content_error (NULL
, ct
, "don't know how to display content");
326 * Parse the display string for displaying generic content
330 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
333 int xstdin
= 0, xlist
= 0;
334 char *file
= NULL
, buffer
[BUFSIZ
];
336 if (!ct
->c_ceopenfnx
) {
338 content_error (NULL
, ct
, "don't know how to decode content");
343 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
345 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
348 if (! strcmp(invo_name
, "mhshow") &&
349 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
350 /* This has to be done after calling c_ceopenfnx, so
351 unfortunately the type checks are necessary without
352 some code rearrangement. And to make this really ugly,
353 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
354 if (convert_content_charset (ct
, &file
) == OK
) {
355 (*ct
->c_ceclosefnx
) (ct
);
356 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
359 char *charset
= content_charset (ct
);
360 admonish (NULL
, "unable to convert character set%s%s to %s",
361 ct
->c_partno
? " of part " : "",
362 ct
->c_partno
? ct
->c_partno
: "",
369 strncpy (buffer
, cp
, sizeof(buffer
));
373 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
374 sizeof(buffer
) - 1, 0)) {
375 admonish (NULL
, "Buffer overflow constructing show command!\n");
380 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
381 fd
, xlist
, xstdin
, fmt
);
386 * Routine to actually display the content
390 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
391 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
397 if (debugsw
|| cracked
) {
400 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
403 fprintf (stderr
, " part %s", ct
->c_partno
);
405 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
407 fprintf (stderr
, " using command %s\n", buffer
);
411 output_marker(ct
, fmt
, 0);
415 * If the command is a zero-length string, just write the output on
419 if (buffer
[0] == '\0') {
420 char readbuf
[BUFSIZ
];
422 char lastchar
= '\n';
425 advise(NULL
, "Cannot use NULL command to display content-type "
426 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
430 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
431 if ((ssize_t
) fwrite(readbuf
, sizeof(char), cc
, stdout
) < cc
) {
432 advise ("putline", "fwrite");
434 lastchar
= readbuf
[cc
- 1];
438 advise("read", "while reading text content");
443 * The MIME standards allow content to not have a trailing newline.
444 * But because we are (presumably) sending this to stdout, include
445 * a newline for text content if the final character was not a
446 * newline. Only do this for mhshow.
449 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
450 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
459 vec
= argsplit(buffer
, &file
, &vecp
);
464 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
468 advise ("fork", "unable to");
469 (*ct
->c_ceclosefnx
) (ct
);
474 if (chdir (cracked
) < 0) {
475 advise (cracked
, "chdir");
482 fprintf (stderr
, "unable to exec ");
490 arglist_free(file
, vec
);
492 pidcheck ((status
= pidXwait (child_id
, NULL
)));
495 (*ct
->c_ceclosefnx
) (ct
);
496 return (alternate
? OK
: status
);
503 * show content of type "text"
507 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
509 char *cp
, buffer
[BUFSIZ
];
510 CI ci
= &ct
->c_ctinfo
;
512 /* Check for invo_name-show-type[/subtype] */
513 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
514 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
517 * Use default method if content is text/plain, or if
518 * if it is not a text part of a multipart/alternative
520 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
523 snprintf(buffer
, sizeof(buffer
), "%%lcat");
525 snprintf(buffer
, sizeof(buffer
), "%%l");
527 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
528 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
529 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
530 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
538 * show message body of type "multipart"
542 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
546 CI ci
= &ct
->c_ctinfo
;
548 /* Check for invo_name-show-type[/subtype] */
549 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
550 return show_multi_aux (ct
, alternate
, cp
, fmt
);
552 if ((cp
= ct
->c_showproc
)) {
553 return show_multi_aux (ct
, alternate
, cp
, fmt
);
557 * Use default method to display this multipart content. Even
558 * unknown types are displayable, since they're treated as mixed
561 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
567 * show message body of subtypes of multipart that
568 * we understand directly (mixed, alternate, etc...)
572 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
573 int inlineonly
, struct format
*fmt
)
575 int alternating
, nowalternate
, result
;
576 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
585 nowalternate
= alternate
;
587 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
593 * alternate -> we are a part inside a multipart/alternative
594 * alternating -> we are a multipart/alternative
602 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
605 /* while looking for the right displayable alternative, we
606 * use a looser search criterion than we do after finding it.
607 * specifically, while still looking, part_ok() will match
608 * "parent" parts (e.g. "-part 2" where 2 is a high-level
609 * multipart). after finding it, we use part_exact() to only
610 * choose a part that was requested explicitly.
612 if ((part_exact(p
) && type_ok(p
, 1)) ||
613 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
617 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
619 switch (inneresult
) {
620 case NOTOK
: /* hard display error */
622 if (alternate
&& !alternating
) {
628 case DONE
: /* found no match on content type */
631 case OK
: /* display successful */
636 /* if we got success on a sub-part of
637 * multipart/alternative, we're done, unless
638 * there's a chance an explicit part should be
639 * matched later in the alternatives. */
642 } else if (alternate
) {
643 alternate
= nowalternate
= 0;
651 /* we're supposed to be displaying at least something from a
652 * multipart/alternative. if we've had parts to consider, and
653 * we've had no success, then we should complain. we shouldn't
654 * complain if none of the parts matched any -part or -type option.
656 if (alternating
&& request_matched
&& !display_success
) {
657 /* if we're ourselves an alternate. don't complain yet. */
659 content_error (NULL
, ct
, "don't know how to display any of the contents");
664 /* if no parts matched what was requested, there can't have been
665 * any display errors. we report DONE rather than OK. */
666 ret
= request_matched
? result
: DONE
;
672 * Parse display string for multipart content
673 * and use external program to display it.
677 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
679 /* xstdin is only used in the call to parse_display_string():
680 its value is ignored in the function. */
681 int xstdin
= 0, xlist
= 0;
682 char *file
= NULL
, buffer
[BUFSIZ
];
683 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
687 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
690 if (!p
->c_ceopenfnx
) {
692 content_error (NULL
, p
, "don't know how to decode content");
696 if (p
->c_storage
== NULL
) {
697 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
700 p
->c_storage
= add (file
, NULL
);
702 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
704 (*p
->c_ceclosefnx
) (p
);
708 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
709 buffer
, sizeof(buffer
) - 1, 1)) {
710 admonish (NULL
, "Buffer overflow constructing show command!\n");
714 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
719 * show content of type "message/rfc822"
723 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
726 CI ci
= &ct
->c_ctinfo
;
728 /* Check for invo_name-show-type[/subtype] */
729 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
730 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
732 if ((cp
= ct
->c_showproc
))
733 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
735 /* default method for message/rfc822 */
736 if (ct
->c_subtype
== MESSAGE_RFC822
) {
737 cp
= (ct
->c_showproc
= add ("%pshow -file %F", NULL
));
738 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
741 /* complain if we are not a part of a multipart/alternative */
743 content_error (NULL
, ct
, "don't know how to display content");
750 * Show content of type "message/partial".
754 show_partial (CT ct
, int alternate
)
756 NMH_UNUSED (alternate
);
758 content_error (NULL
, ct
,
759 "in order to display this message, you must reassemble it");
765 * Show content of type "message/external".
767 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
771 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
774 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
775 CT p
= e
->eb_content
;
780 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
785 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
786 char *file
, char *buffer
, size_t buflen
,
789 char *bp
= buffer
, *pp
;
790 CI ci
= &ct
->c_ctinfo
;
792 bp
[0] = bp
[buflen
] = '\0';
794 for ( ; *cp
&& buflen
> 0; cp
++) {
800 /* insert parameters from Content-Type field */
805 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
806 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
807 get_param_value(pm
, '?'));
817 /* insert content description */
821 s
= trimcpy (ct
->c_descr
);
822 strncpy (bp
, s
, buflen
);
828 /* no longer implemented */
832 /* %f, and stdin is terminal not content */
838 /* insert filename(s) containing content */
839 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
844 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
847 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
854 /* insert filename containing content */
855 snprintf (bp
, buflen
, "%s", file
);
858 * Old comments below are left here for posterity.
859 * This was/is tricky.
861 /* since we've quoted the file argument, set things up
862 * to look past it, to avoid problems with the quoting
863 * logic below. (I know, I should figure out what's
864 * broken with the quoting logic, but..)
867 * Here's the email that submitted the patch with
869 * https://www.mail-archive.com/nmh-workers@mhost.com/
871 * I can't tell from that exactly what was broken,
872 * beyond misquoting of the filename. The profile
873 * had appearances of %F both with and without quotes.
874 * The unquoted ones should have been quoted by the
876 * The fix was to always quote the filename. But
877 * that broke '%F' because it expanded to ''filename''.
880 * Old comments above are left here for posterity.
881 * The quoting below should work properly now.
887 /* No longer supported */
891 /* display listing prior to displaying content */
896 /* insert subtype of content */
897 strncpy (bp
, ci
->ci_subtype
, buflen
);
901 /* insert character % */
905 const char *closing_brace
= strchr(cp
, '}');
908 const size_t param_len
= closing_brace
- cp
- 1;
909 char *param
= mh_xmalloc(param_len
+ 1);
912 (void) strncpy(param
, cp
+ 1, param_len
);
913 param
[param_len
] = '\0';
914 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
917 cp
+= param_len
+ 1; /* Skip both braces, too. */
920 /* %{param} is set in the Content-Type header.
921 After the break below, quote it if necessary. */
922 (void) strncpy(bp
, value
, buflen
);
925 /* %{param} not found, so skip it completely. cp
926 was advanced above. */
930 /* This will get confused if there are multiple %{}'s,
931 but its real purpose is to avoid doing bad things
932 above if a closing brace wasn't found. */
934 "no closing brace for display string escape %s",
951 /* Did we actually insert something? */
953 /* Insert single quote if not inside quotes already */
954 if (!quoted
&& buflen
) {
956 memmove (pp
+ 1, pp
, len
+1);
962 /* Escape existing quotes */
963 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
966 /* Quoted. Let this quote close that quoting.
967 Insert an escaped quote to replace it and
968 another quote to reopen quoting, which will be
970 memmove (pp
+ 2, pp
, len
);
977 /* Not quoted. This should not be reached with
978 the current code, but handle the condition
979 in case the code changes. Just escape the
981 memmove (pp
, pp
-1, len
+1);
987 /* If pp is still set, that means we ran out of space. */
991 if (quoted
&& buflen
) {
992 /* See if we need to close the quote by looking
993 for an odd number of unescaped close quotes in
994 the remainder of the display string. */
995 int found_quote
= 0, escaped
= 0;
998 for (c
= cp
+1; *c
; ++c
) {
1000 escaped
= ! escaped
;
1006 found_quote
= ! found_quote
;
1011 if (! found_quote
) {
1031 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1032 /* content_error would provide a more useful error message
1033 * here, except that if we got overrun, it probably would
1039 /* use charset string to modify display method */
1040 if (ct
->c_termproc
) {
1043 strncpy (term
, buffer
, sizeof(term
));
1044 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1052 convert_charset (CT ct
, char *dest_charset
, int *message_mods
) {
1053 char *src_charset
= content_charset (ct
);
1056 if (strcasecmp (src_charset
, dest_charset
)) {
1058 iconv_t conv_desc
= NULL
;
1065 int opened_input_file
= 0;
1066 char src_buffer
[BUFSIZ
];
1067 size_t dest_buffer_size
= BUFSIZ
;
1068 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1072 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1074 advise (NULL
, "Can't convert %s to %s", src_charset
, dest_charset
);
1079 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1080 adios (NULL
, "unable to create temporary file in %s",
1083 dest
= add (tempfile
, NULL
);
1085 if (ct
->c_cefile
.ce_file
) {
1086 file
= &ct
->c_cefile
.ce_file
;
1087 fp
= &ct
->c_cefile
.ce_fp
;
1089 } else if (ct
->c_file
) {
1092 begin
= (size_t) ct
->c_begin
;
1093 end
= (size_t) ct
->c_end
;
1094 } /* else no input file: shouldn't happen */
1096 if (file
&& *file
&& fp
) {
1098 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1099 advise (*file
, "unable to open for reading");
1102 opened_input_file
= 1;
1109 size_t bytes_to_read
=
1110 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1112 fseeko (*fp
, begin
, SEEK_SET
);
1113 while ((inbytes
= fread (src_buffer
, 1,
1114 min (bytes_to_read
, sizeof src_buffer
),
1116 ICONV_CONST
char *ib
= src_buffer
;
1117 char *ob
= dest_buffer
;
1118 size_t outbytes
= dest_buffer_size
;
1119 size_t outbytes_before
= outbytes
;
1121 if (end
> 0) bytes_to_read
-= inbytes
;
1124 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1126 if (errno
== E2BIG
) {
1128 * Bump up the buffer by at least a factor of 2
1129 * over what we need.
1131 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1132 dest_buffer_size
+= bumpup
;
1133 dest_buffer
= mh_xrealloc(dest_buffer
,
1135 ob
= dest_buffer
+ ob_off
;
1137 outbytes_before
+= bumpup
;
1143 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
)
1145 advise (dest
, "write");
1150 if (opened_input_file
) {
1156 iconv_close (conv_desc
);
1160 /* Replace the decoded file with the converted one. */
1161 if (ct
->c_cefile
.ce_file
) {
1162 if (ct
->c_cefile
.ce_unlink
) {
1163 (void) m_unlink (ct
->c_cefile
.ce_file
);
1165 free (ct
->c_cefile
.ce_file
);
1167 ct
->c_cefile
.ce_file
= dest
;
1168 ct
->c_cefile
.ce_unlink
= 1;
1172 /* Update ct->c_ctline. */
1174 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1175 ct
->c_ctinfo
.ci_subtype
, NULL
);
1178 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1179 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1181 outline
= output_params(strlen(TYPE_FIELD
) + 1 + strlen(ctline
),
1182 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1184 ctline
= add(outline
, ctline
);
1188 free (ct
->c_ctline
);
1189 ct
->c_ctline
= ctline
;
1190 } /* else no CT line, which is odd */
1192 /* Update Content-Type header field. */
1193 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1194 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1195 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1203 (void) m_unlink (dest
);
1206 #else /* ! HAVE_ICONV */
1207 NMH_UNUSED (message_mods
);
1209 advise (NULL
, "Can't convert %s to %s without iconv", src_charset
,
1213 #endif /* ! HAVE_ICONV */
1222 convert_content_charset (CT ct
, char **file
) {
1226 /* Using current locale, see if the content needs to be converted. */
1228 /* content_charset() cannot return NULL. */
1229 char *charset
= content_charset (ct
);
1231 if (! check_charset (charset
, strlen (charset
))) {
1234 char *charset
= getcpy (get_charset ());
1236 if (convert_charset (ct
, charset
, &unused
) == 0) {
1237 *file
= ct
->c_cefile
.ce_file
;
1243 #else /* ! HAVE_ICONV */
1246 #endif /* ! HAVE_ICONV */
1252 * Compile our format string and save any parameters we care about.
1255 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1256 "%<{description}%{description}" \
1257 "%?{cdispo-filename}%{cdispo-filename}" \
1258 "%|%{ctype-name}%> " \
1259 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1261 static struct format
*
1262 compile_marker(char *markerform
)
1266 struct comp
*comp
= NULL
;
1267 unsigned int bucket
;
1268 struct param_comp_list
*pc_entry
;
1270 fmtstring
= new_fs(markerform
, NULL
, DEFAULT_MARKER
);
1272 (void) fmt_compile(fmtstring
, &fmt
, 1);
1276 * Things we care about:
1278 * part - Part name (e.g., 1.1)
1279 * content-type - Content-Type
1280 * description - Content-Description
1281 * disposition - Content-Disposition (inline, attachment)
1282 * ctype-<param> - Content-Type parameter
1283 * cdispo-<param> - Content-Disposition parameter
1286 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1287 if (strcasecmp(comp
->c_name
, "part") == 0) {
1289 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1291 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1292 description_comp
= comp
;
1293 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1295 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1296 strlen(comp
->c_name
) > 6) {
1297 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1298 pc_entry
->param
= getcpy(comp
->c_name
+ 6);
1299 pc_entry
->comp
= comp
;
1300 pc_entry
->next
= ctype_pc_list
;
1301 ctype_pc_list
= pc_entry
;
1302 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1303 strlen(comp
->c_name
) > 7) {
1304 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1305 pc_entry
->param
= getcpy(comp
->c_name
+ 7);
1306 pc_entry
->comp
= comp
;
1307 pc_entry
->next
= dispo_pc_list
;
1308 dispo_pc_list
= pc_entry
;
1316 * Output on stdout an appropriate marker for this content, using mh-format
1320 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1322 charstring_t outbuf
= charstring_create (BUFSIZ
);
1323 struct param_comp_list
*pcentry
;
1328 * Grab any items we care about.
1331 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1332 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1333 ct
->c_ctinfo
.ci_subtype
, NULL
);
1336 if (part_comp
&& ct
->c_partno
) {
1337 part_comp
->c_text
= getcpy(ct
->c_partno
);
1340 if (description_comp
&& ct
->c_descr
) {
1341 description_comp
->c_text
= getcpy(ct
->c_descr
);
1344 if (dispo_comp
&& ct
->c_dispo_type
) {
1345 dispo_comp
->c_text
= getcpy(ct
->c_dispo_type
);
1348 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1349 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1350 pcentry
->param
, '?', 0);
1353 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1354 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1355 pcentry
->param
, '?', 0);
1358 if (ct
->c_cesizefnx
)
1359 partsize
= (*ct
->c_cesizefnx
) (ct
);
1361 partsize
= ct
->c_end
- ct
->c_begin
;
1363 /* make the part's hidden aspect available by overloading the
1364 * %(unseen) function. make the part's size available via %(size).
1365 * see comments in h/fmt_scan.h.
1369 dat
[0] = dat
[1] = dat
[3] = 0;
1371 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1373 fputs(charstring_buffer (outbuf
), stdout
);
1374 charstring_free (outbuf
);
1380 * Reset (and free) any of the saved marker text
1384 free_markercomps(void)
1386 struct param_comp_list
*pc_entry
, *pc2
;
1390 description_comp
= NULL
;
1393 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1394 free(pc_entry
->param
);
1395 pc2
= pc_entry
->next
;
1400 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1401 free(pc_entry
->param
);
1402 pc2
= pc_entry
->next
;
1409 * Exit if the display process returned with a nonzero exit code, or terminated
1410 * with a SIGQUIT signal.
1414 pidcheck (int status
)
1416 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)