]>
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.
9 #include "sbr/pidstatus.h"
10 #include "sbr/arglist.h"
11 #include "sbr/error.h"
13 #include "h/signals.h"
18 #include "h/mhparse.h"
19 #include "h/fmt_scan.h"
23 #include "mhshowsbr.h"
24 #include "sbr/m_mktemp.h"
27 #endif /* ! HAVE_ICONV */
35 /* flags for moreproc/header display */
39 /* for output markers and headers */
49 static void show_single_message (CT
, char *, int, int, int, struct format
*);
50 static void DisplayMsgHeader (CT
, char *, int);
51 static int show_switch (CT
, int, int, int, int, struct format
*);
52 static int show_content (CT
, int, int, int, struct format
*);
53 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
54 static int show_text (CT
, int, int, struct format
*);
55 static int show_multi (CT
, int, int, int, int, struct format
*);
56 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
57 static int show_multi_aux (CT
, int, char *, struct format
*);
58 static int show_message_rfc822 (CT
, int, struct format
*);
59 static int show_partial (CT
, int);
60 static int show_external (CT
, int, int, int, int, struct format
*);
61 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
62 size_t, int multipart
);
63 static int convert_content_charset (CT
, char **);
64 static struct format
*compile_header(char *);
65 static struct format
*compile_marker(char *);
66 static void output_header (CT
, struct format
*);
67 static void output_marker (CT
, struct format
*, int);
68 static void free_markercomps (void);
69 static int pidcheck(int);
72 * Components (and list of parameters/components) we care about for the
73 * content marker display.
76 static struct comp
*folder_comp
= NULL
;
77 static struct comp
*part_comp
= NULL
;
78 static struct comp
*ctype_comp
= NULL
;
79 static struct comp
*description_comp
= NULL
;
80 static struct comp
*dispo_comp
= NULL
;
82 struct param_comp_list
{
85 struct param_comp_list
*next
;
88 static struct param_comp_list
*ctype_pc_list
= NULL
;
89 static struct param_comp_list
*dispo_pc_list
= NULL
;
93 * Top level entry point to show/display a group of messages
97 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
100 struct format
*hfmt
, *mfmt
;
103 * If form is not specified, then get default form
104 * for showing headers of MIME messages.
107 formsw
= mh_xstrdup(etcpath("mhl.headers"));
110 * Compile the content marker and header format lines
112 mfmt
= compile_marker(markerform
);
113 hfmt
= compile_header(headerform
);
116 * If form is "mhl.null", suppress display of header.
118 if (!strcmp (formsw
, "mhl.null"))
121 for (ctp
= cts
; *ctp
; ctp
++) {
124 /* if top-level type is ok, then display message */
125 if (type_ok (ct
, 1)) {
126 if (headersw
) output_header(ct
, hfmt
);
128 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
140 * Entry point to show/display a single message
144 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
145 int inlineonly
, struct format
*fmt
)
151 /* Allow user executable bit so that temporary directories created by
152 * the viewer (e.g., lynx) are going to be accessible */
153 umask (ct
->c_umask
& ~(0100));
156 * If you have a format file, then display
157 * the message headers.
160 DisplayMsgHeader(ct
, form
, concatsw
);
162 /* Show the body of the message */
163 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
169 if (ct
->c_ceclosefnx
)
170 (*ct
->c_ceclosefnx
) (ct
);
172 /* block a few signals */
174 sigaddset (&set
, SIGHUP
);
175 sigaddset (&set
, SIGINT
);
176 sigaddset (&set
, SIGQUIT
);
177 sigaddset (&set
, SIGTERM
);
178 sigprocmask (SIG_BLOCK
, &set
, &oset
);
180 while (!concatsw
&& wait (&status
) != NOTOK
) {
185 /* reset the signal mask */
186 sigprocmask (SIG_SETMASK
, &oset
, &set
);
193 * Use the mhlproc to show the header fields
197 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
204 vec
= argsplit(mhlproc
, &file
, &vecp
);
205 vec
[vecp
++] = mh_xstrdup("-form");
206 vec
[vecp
++] = mh_xstrdup(form
);
207 vec
[vecp
++] = mh_xstrdup("-nobody");
208 vec
[vecp
++] = getcpy(ct
->c_file
);
211 * If we've specified -(no)moreproc,
212 * then just pass that along.
214 if (nomore
|| concatsw
) {
215 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
217 vec
[vecp
++] = mh_xstrdup("-moreproc");
218 vec
[vecp
++] = mh_xstrdup(progsw
);
227 adios ("fork", "unable to");
232 fprintf (stderr
, "unable to exec ");
238 pidcheck(pidwait(child_id
, NOTOK
));
242 arglist_free(file
, vec
);
247 * Switching routine. Call the correct routine
248 * based on content type.
252 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
255 switch (ct
->c_type
) {
257 return show_multi (ct
, alternate
, concatsw
, textonly
,
261 switch (ct
->c_subtype
) {
262 case MESSAGE_PARTIAL
:
263 return show_partial (ct
, alternate
);
265 case MESSAGE_EXTERNAL
:
266 return show_external (ct
, alternate
, concatsw
, textonly
,
270 return show_message_rfc822 (ct
, alternate
, fmt
);
273 * Treat unknown message types as equivalent to
274 * application/octet-stream for now
277 return show_content (ct
, alternate
, textonly
,
282 return show_text (ct
, alternate
, concatsw
, fmt
);
289 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
292 return 0; /* NOT REACHED */
297 * Generic method for displaying content
301 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
305 CI ci
= &ct
->c_ctinfo
;
308 * If we're here, we are not a text type. So we don't need to check
312 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
313 output_marker(ct
, fmt
, 1);
317 /* Check for invo_name-show-type[/subtype] */
318 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
319 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
321 if ((cp
= ct
->c_showproc
))
322 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
324 /* complain if we are not a part of a multipart/alternative */
326 content_error (NULL
, ct
, "don't know how to display content");
333 * Parse the display string for displaying generic content
337 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
340 int xstdin
= 0, xlist
= 0;
341 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
343 if (!ct
->c_ceopenfnx
) {
345 content_error (NULL
, ct
, "don't know how to decode content");
350 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
352 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
355 if (! strcmp(invo_name
, "mhshow") &&
356 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
357 /* This has to be done after calling c_ceopenfnx, so
358 unfortunately the type checks are necessary without
359 some code rearrangement. And to make this really ugly,
360 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
361 if (convert_content_charset (ct
, &file
) == OK
) {
362 (*ct
->c_ceclosefnx
) (ct
);
363 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
366 char *charset
= content_charset (ct
);
367 inform("unable to convert character set%s%s from %s, continuing...",
368 ct
->c_partno
? " of part " : "",
369 FENDNULL(ct
->c_partno
),
376 strncpy (buffer
, cp
, sizeof(buffer
));
380 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
381 sizeof(buffer
) - 1, 0)) {
382 inform("Buffer overflow constructing show command, continuing...");
387 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
388 fd
, xlist
, xstdin
, fmt
);
393 * Routine to actually display the content
397 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
398 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
404 if (debugsw
|| cracked
) {
407 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
410 fprintf (stderr
, " part %s", ct
->c_partno
);
412 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
414 fprintf (stderr
, " using command %s\n", buffer
);
418 output_marker(ct
, fmt
, 0);
422 * If the command is a zero-length string, just write the output on
426 if (buffer
[0] == '\0') {
427 char readbuf
[BUFSIZ
];
429 char lastchar
= '\n';
432 inform("Cannot use NULL command to display content-type "
433 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
437 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
438 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
439 advise ("putline", "fwrite");
441 lastchar
= readbuf
[cc
- 1];
445 advise("read", "while reading text content");
450 * The MIME standards allow content to not have a trailing newline.
451 * But because we are (presumably) sending this to stdout, include
452 * a newline for text content if the final character was not a
453 * newline. Only do this for mhshow.
456 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
457 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
466 vec
= argsplit(buffer
, &file
, &vecp
);
474 advise ("fork", "unable to");
475 (*ct
->c_ceclosefnx
) (ct
);
480 if (chdir (cracked
) < 0) {
481 advise (cracked
, "chdir");
488 fprintf (stderr
, "unable to exec ");
495 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
496 /* Copy the real display program name. This relies on the
497 specific construction of vec[] by argsplit(). */
501 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
503 arglist_free(file
, vec
);
505 (*ct
->c_ceclosefnx
) (ct
);
506 return alternate
? OK
: status
;
513 * show content of type "text"
517 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
519 char *cp
, buffer
[BUFSIZ
];
520 CI ci
= &ct
->c_ctinfo
;
522 /* Check for invo_name-show-type[/subtype] */
523 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
524 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
527 * Use default method if content is text/plain, or if
528 * if it is not a text part of a multipart/alternative
530 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
533 snprintf(buffer
, sizeof(buffer
), "%%lcat");
535 snprintf(buffer
, sizeof(buffer
), "%%l");
537 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
538 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
539 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
540 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
548 * show message body of type "multipart"
552 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
556 CI ci
= &ct
->c_ctinfo
;
558 /* Check for invo_name-show-type[/subtype] */
559 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
560 return show_multi_aux (ct
, alternate
, cp
, fmt
);
562 if ((cp
= ct
->c_showproc
)) {
563 return show_multi_aux (ct
, alternate
, cp
, fmt
);
567 * Use default method to display this multipart content. Even
568 * unknown types are displayable, since they're treated as mixed
571 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
577 * show message body of subtypes of multipart that
578 * we understand directly (mixed, alternate, etc...)
582 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
583 int inlineonly
, struct format
*fmt
)
585 int alternating
, nowalternate
, result
;
586 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
588 bool request_matched
;
589 bool display_success
;
595 nowalternate
= alternate
;
597 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
603 * alternate -> we are a part inside a multipart/alternative
604 * alternating -> we are a multipart/alternative
608 request_matched
= false;
609 display_success
= false;
610 mult_alt_done
= false;
612 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
615 /* while looking for the right displayable alternative, we
616 * use a looser search criterion than we do after finding it.
617 * specifically, while still looking, part_ok() will match
618 * "parent" parts (e.g. "-part 2" where 2 is a high-level
619 * multipart). after finding it, we use part_exact() to only
620 * choose a part that was requested explicitly.
622 if ((part_exact(p
) && type_ok(p
, 1)) ||
623 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
627 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
629 switch (inneresult
) {
630 case NOTOK
: /* hard display error */
631 request_matched
= true;
632 if (alternate
&& !alternating
) {
638 case DONE
: /* found no match on content type */
641 case OK
: /* display successful */
642 request_matched
= true;
643 display_success
= true;
646 /* if we got success on a sub-part of
647 * multipart/alternative, we're done, unless
648 * there's a chance an explicit part should be
649 * matched later in the alternatives. */
651 mult_alt_done
= true;
652 } else if (alternate
) {
653 alternate
= nowalternate
= 0;
661 /* we're supposed to be displaying at least something from a
662 * multipart/alternative. if we've had parts to consider, and
663 * we've had no success, then we should complain. we shouldn't
664 * complain if none of the parts matched any -part or -type option.
666 if (alternating
&& request_matched
&& !display_success
) {
667 /* if we're ourselves an alternate. don't complain yet. */
669 content_error (NULL
, ct
, "don't know how to display any of the contents");
674 /* if no parts matched what was requested, there can't have been
675 * any display errors. we report DONE rather than OK. */
676 ret
= request_matched
? result
: DONE
;
682 * Parse display string for multipart content
683 * and use external program to display it.
687 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
689 /* xstdin is only used in the call to parse_display_string():
690 its value is ignored in the function. */
691 int xstdin
= 0, xlist
= 0;
692 char *file
= NULL
, buffer
[BUFSIZ
];
693 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
697 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
700 if (!p
->c_ceopenfnx
) {
702 content_error (NULL
, p
, "don't know how to decode content");
706 if (p
->c_storage
== NULL
) {
707 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
710 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
712 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
714 (*p
->c_ceclosefnx
) (p
);
718 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
719 buffer
, sizeof(buffer
) - 1, 1)) {
720 inform("Buffer overflow constructing show command, continuing...");
724 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
729 * show content of type "message/rfc822"
733 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
736 CI ci
= &ct
->c_ctinfo
;
738 /* Check for invo_name-show-type[/subtype] */
739 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
740 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
742 if ((cp
= ct
->c_showproc
))
743 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
745 /* default method for message/rfc822 */
746 if (ct
->c_subtype
== MESSAGE_RFC822
) {
747 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
748 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
751 /* complain if we are not a part of a multipart/alternative */
753 content_error (NULL
, ct
, "don't know how to display content");
760 * Show content of type "message/partial".
764 show_partial (CT ct
, int alternate
)
766 NMH_UNUSED (alternate
);
768 content_error (NULL
, ct
,
769 "in order to display this message, you must reassemble it");
775 * Show content of type "message/external".
777 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
781 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
784 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
785 CT p
= e
->eb_content
;
790 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
795 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
796 char *file
, char *buffer
, size_t buflen
,
801 char *bp
= buffer
, *pp
;
802 CI ci
= &ct
->c_ctinfo
;
804 bp
[0] = bp
[buflen
] = '\0';
806 for ( ; *cp
&& buflen
> 0; cp
++) {
812 /* insert parameters from Content-Type field */
817 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
818 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
819 get_param_value(pm
, '?'));
829 /* insert content description */
833 s
= trimcpy (ct
->c_descr
);
834 strncpy (bp
, s
, buflen
);
840 /* no longer implemented */
844 /* %f, and stdin is terminal not content */
850 /* insert filename(s) containing content */
851 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
856 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
859 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
866 /* insert filename containing content */
867 snprintf (bp
, buflen
, "%s", file
);
870 * Old comments below are left here for posterity.
871 * This was/is tricky.
873 /* since we've quoted the file argument, set things up
874 * to look past it, to avoid problems with the quoting
875 * logic below. (I know, I should figure out what's
876 * broken with the quoting logic, but..)
879 * Here's the email that submitted the patch with
881 * https://www.mail-archive.com/nmh-workers@mhost.com/
883 * I can't tell from that exactly what was broken,
884 * beyond misquoting of the filename. The profile
885 * had appearances of %F both with and without quotes.
886 * The unquoted ones should have been quoted by the
888 * The fix was to always quote the filename. But
889 * that broke '%F' because it expanded to ''filename''.
892 * Old comments above are left here for posterity.
893 * The quoting below should work properly now.
899 /* No longer supported */
903 /* display listing prior to displaying content */
908 /* insert subtype of content */
909 strncpy (bp
, ci
->ci_subtype
, buflen
);
913 /* insert character % */
917 const char *closing_brace
= strchr(cp
, '}');
920 const size_t param_len
= closing_brace
- cp
- 1;
921 char *param
= mh_xmalloc(param_len
+ 1);
924 (void) strncpy(param
, cp
+ 1, param_len
);
925 param
[param_len
] = '\0';
926 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
929 cp
+= param_len
+ 1; /* Skip both braces, too. */
932 /* %{param} is set in the Content-Type header.
933 After the break below, quote it if necessary. */
934 (void) strncpy(bp
, value
, buflen
);
937 /* %{param} not found, so skip it completely. cp
938 was advanced above. */
942 /* This will get confused if there are multiple %{}'s,
943 but its real purpose is to avoid doing bad things
944 above if a closing brace wasn't found. */
945 inform("no closing brace for display string escape %s, continuing...",
962 /* Did we actually insert something? */
964 /* Insert single quote if not inside quotes already */
965 if (!quoted
&& buflen
) {
967 memmove (pp
+ 1, pp
, len
+1);
973 /* Escape existing quotes */
974 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
977 /* Quoted. Let this quote close that quoting.
978 Insert an escaped quote to replace it and
979 another quote to reopen quoting, which will be
981 memmove (pp
+ 2, pp
, len
);
988 /* Not quoted. This should not be reached with
989 the current code, but handle the condition
990 in case the code changes. Just escape the
992 memmove (pp
, pp
-1, len
+1);
998 /* If pp is still set, that means we ran out of space. */
1001 /* Close quoting. */
1002 if (quoted
&& buflen
) {
1003 /* See if we need to close the quote by looking
1004 for an odd number of unescaped close quotes in
1005 the remainder of the display string. */
1006 int found_quote
= 0, escaped
= 0;
1009 for (c
= cp
+1; *c
; ++c
) {
1011 escaped
= ! escaped
;
1017 found_quote
= ! found_quote
;
1022 if (! found_quote
) {
1042 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1043 /* content_error would provide a more useful error message
1044 * here, except that if we got overrun, it probably would
1050 /* use charset string to modify display method */
1051 if (ct
->c_termproc
) {
1054 strncpy (term
, buffer
, sizeof(term
));
1055 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1063 convert_charset (CT ct
, char *dest_charset
, int *message_mods
)
1065 char *src_charset
= content_charset (ct
);
1068 if (strcasecmp (src_charset
, dest_charset
)) {
1070 iconv_t conv_desc
= NULL
;
1077 bool opened_input_file
= false;
1078 char src_buffer
[BUFSIZ
];
1079 size_t dest_buffer_size
= BUFSIZ
;
1080 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1083 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1085 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1087 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1092 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1093 die("unable to create temporary file in %s",
1096 dest
= mh_xstrdup(tempfile
);
1098 if (ct
->c_cefile
.ce_file
) {
1099 file
= &ct
->c_cefile
.ce_file
;
1100 fp
= &ct
->c_cefile
.ce_fp
;
1102 } else if (ct
->c_file
) {
1105 begin
= (size_t) ct
->c_begin
;
1106 end
= (size_t) ct
->c_end
;
1107 } /* else no input file: shouldn't happen */
1109 if (file
&& *file
&& fp
) {
1111 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1112 advise (*file
, "unable to open for reading");
1115 opened_input_file
= true;
1122 size_t bytes_to_read
=
1123 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1125 fseeko (*fp
, begin
, SEEK_SET
);
1126 while ((inbytes
= fread (src_buffer
, 1,
1127 min (bytes_to_read
, sizeof src_buffer
),
1129 ICONV_CONST
char *ib
= src_buffer
;
1130 char *ob
= dest_buffer
;
1131 size_t outbytes
= dest_buffer_size
;
1132 size_t outbytes_before
= outbytes
;
1134 if (end
> 0) bytes_to_read
-= inbytes
;
1137 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1139 if (errno
== E2BIG
) {
1141 * Bump up the buffer by at least a factor of 2
1142 * over what we need.
1144 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1145 dest_buffer_size
+= bumpup
;
1146 dest_buffer
= mh_xrealloc(dest_buffer
,
1148 ob
= dest_buffer
+ ob_off
;
1150 outbytes_before
+= bumpup
;
1153 if (errno
== EINVAL
) {
1154 /* middle of multi-byte sequence */
1155 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1156 advise (dest
, "write");
1158 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1159 if (end
> 0) bytes_to_read
+= inbytes
;
1160 /* inform("convert_charset: EINVAL"); */
1163 if (errno
== EILSEQ
) {
1164 /* invalid multi-byte sequence */
1166 for (++ib
, --inbytes
;
1168 (((unsigned char) *ib
) & 0xc0) == 0x80;
1172 ib
++; inbytes
--; /* skip it */
1174 (*ob
++) = '?'; outbytes
--;
1175 /* inform("convert_charset: EILSEQ"); */
1178 inform("convert_charset: errno = %d", errno
);
1183 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1184 advise (dest
, "write");
1188 if (opened_input_file
) {
1194 iconv_close (conv_desc
);
1198 /* Replace the decoded file with the converted one. */
1199 if (ct
->c_cefile
.ce_file
) {
1200 if (ct
->c_cefile
.ce_unlink
) {
1201 (void) m_unlink (ct
->c_cefile
.ce_file
);
1203 free (ct
->c_cefile
.ce_file
);
1205 ct
->c_cefile
.ce_file
= dest
;
1206 ct
->c_cefile
.ce_unlink
= 1;
1210 /* Update ct->c_ctline. */
1212 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1213 ct
->c_ctinfo
.ci_subtype
, NULL
);
1216 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1217 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1219 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1220 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1222 ctline
= add(outline
, ctline
);
1226 free (ct
->c_ctline
);
1227 ct
->c_ctline
= ctline
;
1228 } /* else no CT line, which is odd */
1230 /* Update Content-Type header field. */
1231 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1232 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1233 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1241 (void) m_unlink (dest
);
1244 #else /* ! HAVE_ICONV */
1245 NMH_UNUSED (message_mods
);
1247 inform("Can't convert %s to %s without iconv", src_charset
,
1251 #endif /* ! HAVE_ICONV */
1260 convert_content_charset (CT ct
, char **file
)
1265 /* Using current locale, see if the content needs to be converted. */
1267 /* content_charset() cannot return NULL. */
1268 char *src_charset
= content_charset (ct
);
1270 if (! check_charset (src_charset
, strlen (src_charset
))) {
1273 char *dest_charset
= getcpy (get_charset ());
1275 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1276 *file
= ct
->c_cefile
.ce_file
;
1281 free (dest_charset
);
1284 #else /* ! HAVE_ICONV */
1287 #endif /* ! HAVE_ICONV */
1293 * Compile our format string and save any parameters we care about.
1296 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1297 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1298 "%<{description}%{description}" \
1299 "%?{cdispo-filename}%{cdispo-filename}" \
1300 "%|%{ctype-name}%> " \
1301 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1303 static struct format
*
1304 compile_header(char *form
)
1308 struct comp
*comp
= NULL
;
1309 unsigned int bucket
;
1311 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1313 (void) fmt_compile(fmtstring
, &fmt
, 1);
1316 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1317 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1325 static struct format
*
1326 compile_marker(char *form
)
1330 struct comp
*comp
= NULL
;
1331 unsigned int bucket
;
1332 struct param_comp_list
*pc_entry
;
1334 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1336 (void) fmt_compile(fmtstring
, &fmt
, 1);
1340 * Things we care about:
1342 * part - Part name (e.g., 1.1)
1343 * content-type - Content-Type
1344 * description - Content-Description
1345 * disposition - Content-Disposition (inline, attachment)
1346 * ctype-<param> - Content-Type parameter
1347 * cdispo-<param> - Content-Disposition parameter
1350 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1351 if (strcasecmp(comp
->c_name
, "part") == 0) {
1353 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1355 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1356 description_comp
= comp
;
1357 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1359 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1360 strlen(comp
->c_name
) > 6) {
1362 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1363 pc_entry
->comp
= comp
;
1364 pc_entry
->next
= ctype_pc_list
;
1365 ctype_pc_list
= pc_entry
;
1366 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1367 strlen(comp
->c_name
) > 7) {
1369 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1370 pc_entry
->comp
= comp
;
1371 pc_entry
->next
= dispo_pc_list
;
1372 dispo_pc_list
= pc_entry
;
1380 * Output on stdout an appropriate marker for this content, using mh-format
1384 output_header(CT ct
, struct format
*fmt
)
1386 charstring_t outbuf
= charstring_create (BUFSIZ
);
1392 folder_comp
->c_text
= getcpy(folder
);
1394 if (ct
->c_file
&& *ct
->c_file
) {
1395 message
= strtol(ct
->c_file
, &endp
, 10);
1396 if (*endp
) message
= 0;
1400 /* it would be nice to populate dat[2], for %(size) here,
1401 * but it's not available. it might also be nice to know
1402 * if the message originally had any mime parts or not -- but
1403 * there's also no record of that. (except for MIME-version:)
1406 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1408 fputs(charstring_buffer (outbuf
), stdout
);
1409 charstring_free (outbuf
);
1415 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1417 charstring_t outbuf
= charstring_create (BUFSIZ
);
1418 struct param_comp_list
*pcentry
;
1425 * Grab any items we care about.
1428 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1429 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1430 ct
->c_ctinfo
.ci_subtype
, NULL
);
1433 if (part_comp
&& ct
->c_partno
) {
1434 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1437 if (description_comp
&& ct
->c_descr
) {
1438 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1441 if (dispo_comp
&& ct
->c_dispo_type
) {
1442 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1445 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1446 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1447 pcentry
->param
, '?', 0);
1450 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1451 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1452 pcentry
->param
, '?', 0);
1455 if (ct
->c_cesizefnx
)
1456 partsize
= (*ct
->c_cesizefnx
) (ct
);
1458 partsize
= ct
->c_end
- ct
->c_begin
;
1460 if (ct
->c_file
&& *ct
->c_file
) {
1461 message
= strtol(ct
->c_file
, &endp
, 10);
1462 if (*endp
) message
= 0;
1467 /* make the part's hidden aspect available by overloading the
1468 * %(unseen) function. make the part's size available via %(size).
1469 * see comments in h/fmt_scan.h.
1473 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1475 fputs(charstring_buffer (outbuf
), stdout
);
1476 charstring_free (outbuf
);
1482 * Reset (and free) any of the saved marker text
1486 free_markercomps(void)
1488 struct param_comp_list
*pc_entry
, *pc2
;
1493 description_comp
= NULL
;
1496 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1497 free(pc_entry
->param
);
1498 pc2
= pc_entry
->next
;
1503 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1504 free(pc_entry
->param
);
1505 pc2
= pc_entry
->next
;
1512 * Exit if the display process returned with a nonzero exit code, or terminated
1513 * with a SIGQUIT signal.
1517 pidcheck (int status
)
1519 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)