]>
diplodocus.org Git - nmh/blob - uip/mhshowsbr.c
b669ad53166756dc734b195e87da4b450570575b
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 #include "mhshowsbr.h"
21 #include "sbr/m_mktemp.h"
24 #endif /* ! HAVE_ICONV */
32 /* flags for moreproc/header display */
36 /* for output markers and headers */
46 static void show_single_message (CT
, char *, int, int, int, struct format
*);
47 static void DisplayMsgHeader (CT
, char *, int);
48 static int show_switch (CT
, int, int, int, int, struct format
*);
49 static int show_content (CT
, int, int, int, struct format
*);
50 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
51 static int show_text (CT
, int, int, struct format
*);
52 static int show_multi (CT
, int, int, int, int, struct format
*);
53 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
54 static int show_multi_aux (CT
, int, char *, struct format
*);
55 static int show_message_rfc822 (CT
, int, struct format
*);
56 static int show_partial (CT
, int);
57 static int show_external (CT
, int, int, int, int, struct format
*);
58 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
59 size_t, int multipart
);
60 static int convert_content_charset (CT
, char **);
61 static struct format
*compile_header(char *);
62 static struct format
*compile_marker(char *);
63 static void output_header (CT
, struct format
*);
64 static void output_marker (CT
, struct format
*, int);
65 static void free_markercomps (void);
66 static int pidcheck(int);
69 * Components (and list of parameters/components) we care about for the
70 * content marker display.
73 static struct comp
*folder_comp
= NULL
;
74 static struct comp
*part_comp
= NULL
;
75 static struct comp
*ctype_comp
= NULL
;
76 static struct comp
*description_comp
= NULL
;
77 static struct comp
*dispo_comp
= NULL
;
79 struct param_comp_list
{
82 struct param_comp_list
*next
;
85 static struct param_comp_list
*ctype_pc_list
= NULL
;
86 static struct param_comp_list
*dispo_pc_list
= NULL
;
90 * Top level entry point to show/display a group of messages
94 show_all_messages(CT
*cts
, int concat
, int textonly
, int inlineonly
)
97 struct format
*hfmt
, *mfmt
;
100 * If form is not specified, then get default form
101 * for showing headers of MIME messages.
104 formsw
= getcpy (etcpath ("mhl.headers"));
107 * Compile the content marker and header format lines
109 mfmt
= compile_marker(markerform
);
110 hfmt
= compile_header(headerform
);
113 * If form is "mhl.null", suppress display of header.
115 if (!strcmp (formsw
, "mhl.null"))
118 for (ctp
= cts
; *ctp
; ctp
++) {
121 /* if top-level type is ok, then display message */
122 if (type_ok (ct
, 1)) {
123 if (headersw
) output_header(ct
, hfmt
);
125 show_single_message (ct
, formsw
, concat
, textonly
, inlineonly
,
137 * Entry point to show/display a single message
141 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
142 int inlineonly
, struct format
*fmt
)
148 /* Allow user executable bit so that temporary directories created by
149 * the viewer (e.g., lynx) are going to be accessible */
150 umask (ct
->c_umask
& ~(0100));
153 * If you have a format file, then display
154 * the message headers.
157 DisplayMsgHeader(ct
, form
, concatsw
);
159 /* Show the body of the message */
160 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
166 if (ct
->c_ceclosefnx
)
167 (*ct
->c_ceclosefnx
) (ct
);
169 /* block a few signals */
171 sigaddset (&set
, SIGHUP
);
172 sigaddset (&set
, SIGINT
);
173 sigaddset (&set
, SIGQUIT
);
174 sigaddset (&set
, SIGTERM
);
175 sigprocmask (SIG_BLOCK
, &set
, &oset
);
177 while (!concatsw
&& wait (&status
) != NOTOK
) {
182 /* reset the signal mask */
183 sigprocmask (SIG_SETMASK
, &oset
, &set
);
190 * Use the mhlproc to show the header fields
194 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
201 vec
= argsplit(mhlproc
, &file
, &vecp
);
202 vec
[vecp
++] = mh_xstrdup("-form");
203 vec
[vecp
++] = mh_xstrdup(form
);
204 vec
[vecp
++] = mh_xstrdup("-nobody");
205 vec
[vecp
++] = getcpy(ct
->c_file
);
208 * If we've specified -(no)moreproc,
209 * then just pass that along.
211 if (nomore
|| concatsw
) {
212 vec
[vecp
++] = mh_xstrdup("-nomoreproc");
214 vec
[vecp
++] = mh_xstrdup("-moreproc");
215 vec
[vecp
++] = mh_xstrdup(progsw
);
224 adios ("fork", "unable to");
229 fprintf (stderr
, "unable to exec ");
235 pidcheck(pidwait(child_id
, NOTOK
));
239 arglist_free(file
, vec
);
244 * Switching routine. Call the correct routine
245 * based on content type.
249 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
252 switch (ct
->c_type
) {
254 return show_multi (ct
, alternate
, concatsw
, textonly
,
258 switch (ct
->c_subtype
) {
259 case MESSAGE_PARTIAL
:
260 return show_partial (ct
, alternate
);
262 case MESSAGE_EXTERNAL
:
263 return show_external (ct
, alternate
, concatsw
, textonly
,
267 return show_message_rfc822 (ct
, alternate
, fmt
);
270 * Treat unknown message types as equivalent to
271 * application/octet-stream for now
274 return show_content (ct
, alternate
, textonly
,
279 return show_text (ct
, alternate
, concatsw
, fmt
);
286 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
289 return 0; /* NOT REACHED */
294 * Generic method for displaying content
298 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
302 CI ci
= &ct
->c_ctinfo
;
305 * If we're here, we are not a text type. So we don't need to check
309 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
310 output_marker(ct
, fmt
, 1);
314 /* Check for invo_name-show-type[/subtype] */
315 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
316 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
318 if ((cp
= ct
->c_showproc
))
319 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
321 /* complain if we are not a part of a multipart/alternative */
323 content_error (NULL
, ct
, "don't know how to display content");
330 * Parse the display string for displaying generic content
334 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
337 int xstdin
= 0, xlist
= 0;
338 char *file
= NULL
, buffer
[NMH_BUFSIZ
];
340 if (!ct
->c_ceopenfnx
) {
342 content_error (NULL
, ct
, "don't know how to decode content");
347 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
349 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
352 if (! strcmp(invo_name
, "mhshow") &&
353 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
354 /* This has to be done after calling c_ceopenfnx, so
355 unfortunately the type checks are necessary without
356 some code rearrangement. And to make this really ugly,
357 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
358 if (convert_content_charset (ct
, &file
) == OK
) {
359 (*ct
->c_ceclosefnx
) (ct
);
360 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
363 char *charset
= content_charset (ct
);
364 inform("unable to convert character set%s%s from %s, continuing...",
365 ct
->c_partno
? " of part " : "",
366 FENDNULL(ct
->c_partno
),
373 strncpy (buffer
, cp
, sizeof(buffer
));
377 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
378 sizeof(buffer
) - 1, 0)) {
379 inform("Buffer overflow constructing show command, continuing...");
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 inform("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 if ((ssize_t
) fwrite(readbuf
, 1, cc
, stdout
) < cc
) {
436 advise ("putline", "fwrite");
438 lastchar
= readbuf
[cc
- 1];
442 advise("read", "while reading text content");
447 * The MIME standards allow content to not have a trailing newline.
448 * But because we are (presumably) sending this to stdout, include
449 * a newline for text content if the final character was not a
450 * newline. Only do this for mhshow.
453 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
454 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
463 vec
= argsplit(buffer
, &file
, &vecp
);
471 advise ("fork", "unable to");
472 (*ct
->c_ceclosefnx
) (ct
);
477 if (chdir (cracked
) < 0) {
478 advise (cracked
, "chdir");
485 fprintf (stderr
, "unable to exec ");
492 char *display_prog
= vecp
> 2 && vec
[2][0] != '\0'
493 /* Copy the real display program name. This relies on the
494 specific construction of vec[] by argsplit(). */
498 pidcheck ((status
= pidXwait (child_id
, display_prog
)));
500 arglist_free(file
, vec
);
502 (*ct
->c_ceclosefnx
) (ct
);
503 return alternate
? OK
: status
;
510 * show content of type "text"
514 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
516 char *cp
, buffer
[BUFSIZ
];
517 CI ci
= &ct
->c_ctinfo
;
519 /* Check for invo_name-show-type[/subtype] */
520 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
521 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
524 * Use default method if content is text/plain, or if
525 * if it is not a text part of a multipart/alternative
527 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
530 snprintf(buffer
, sizeof(buffer
), "%%lcat");
532 snprintf(buffer
, sizeof(buffer
), "%%l");
534 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
535 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
536 cp
= (ct
->c_showproc
= mh_xstrdup(buffer
));
537 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
545 * show message body of type "multipart"
549 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
553 CI ci
= &ct
->c_ctinfo
;
555 /* Check for invo_name-show-type[/subtype] */
556 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
557 return show_multi_aux (ct
, alternate
, cp
, fmt
);
559 if ((cp
= ct
->c_showproc
)) {
560 return show_multi_aux (ct
, alternate
, cp
, fmt
);
564 * Use default method to display this multipart content. Even
565 * unknown types are displayable, since they're treated as mixed
568 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
574 * show message body of subtypes of multipart that
575 * we understand directly (mixed, alternate, etc...)
579 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
580 int inlineonly
, struct format
*fmt
)
582 int alternating
, nowalternate
, result
;
583 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
592 nowalternate
= alternate
;
594 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
600 * alternate -> we are a part inside a multipart/alternative
601 * alternating -> we are a multipart/alternative
609 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
612 /* while looking for the right displayable alternative, we
613 * use a looser search criterion than we do after finding it.
614 * specifically, while still looking, part_ok() will match
615 * "parent" parts (e.g. "-part 2" where 2 is a high-level
616 * multipart). after finding it, we use part_exact() to only
617 * choose a part that was requested explicitly.
619 if ((part_exact(p
) && type_ok(p
, 1)) ||
620 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
624 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
626 switch (inneresult
) {
627 case NOTOK
: /* hard display error */
629 if (alternate
&& !alternating
) {
635 case DONE
: /* found no match on content type */
638 case OK
: /* display successful */
643 /* if we got success on a sub-part of
644 * multipart/alternative, we're done, unless
645 * there's a chance an explicit part should be
646 * matched later in the alternatives. */
649 } else if (alternate
) {
650 alternate
= nowalternate
= 0;
658 /* we're supposed to be displaying at least something from a
659 * multipart/alternative. if we've had parts to consider, and
660 * we've had no success, then we should complain. we shouldn't
661 * complain if none of the parts matched any -part or -type option.
663 if (alternating
&& request_matched
&& !display_success
) {
664 /* if we're ourselves an alternate. don't complain yet. */
666 content_error (NULL
, ct
, "don't know how to display any of the contents");
671 /* if no parts matched what was requested, there can't have been
672 * any display errors. we report DONE rather than OK. */
673 ret
= request_matched
? result
: DONE
;
679 * Parse display string for multipart content
680 * and use external program to display it.
684 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
686 /* xstdin is only used in the call to parse_display_string():
687 its value is ignored in the function. */
688 int xstdin
= 0, xlist
= 0;
689 char *file
= NULL
, buffer
[BUFSIZ
];
690 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
694 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
697 if (!p
->c_ceopenfnx
) {
699 content_error (NULL
, p
, "don't know how to decode content");
703 if (p
->c_storage
== NULL
) {
704 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
707 p
->c_storage
= mh_xstrdup(FENDNULL(file
));
709 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
711 (*p
->c_ceclosefnx
) (p
);
715 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
716 buffer
, sizeof(buffer
) - 1, 1)) {
717 inform("Buffer overflow constructing show command, continuing...");
721 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
726 * show content of type "message/rfc822"
730 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
733 CI ci
= &ct
->c_ctinfo
;
735 /* Check for invo_name-show-type[/subtype] */
736 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
737 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
739 if ((cp
= ct
->c_showproc
))
740 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
742 /* default method for message/rfc822 */
743 if (ct
->c_subtype
== MESSAGE_RFC822
) {
744 cp
= (ct
->c_showproc
= mh_xstrdup("%pshow -file %F"));
745 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
748 /* complain if we are not a part of a multipart/alternative */
750 content_error (NULL
, ct
, "don't know how to display content");
757 * Show content of type "message/partial".
761 show_partial (CT ct
, int alternate
)
763 NMH_UNUSED (alternate
);
765 content_error (NULL
, ct
,
766 "in order to display this message, you must reassemble it");
772 * Show content of type "message/external".
774 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
778 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
781 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
782 CT p
= e
->eb_content
;
787 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
792 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
793 char *file
, char *buffer
, size_t buflen
,
796 char *bp
= buffer
, *pp
;
797 CI ci
= &ct
->c_ctinfo
;
799 bp
[0] = bp
[buflen
] = '\0';
801 for ( ; *cp
&& buflen
> 0; cp
++) {
807 /* insert parameters from Content-Type field */
812 for (pm
= ci
->ci_first_pm
; pm
; pm
= pm
->pm_next
) {
813 snprintf (bp
, buflen
, "%s%s=\"%s\"", s
, pm
->pm_name
,
814 get_param_value(pm
, '?'));
824 /* insert content description */
828 s
= trimcpy (ct
->c_descr
);
829 strncpy (bp
, s
, buflen
);
835 /* no longer implemented */
839 /* %f, and stdin is terminal not content */
845 /* insert filename(s) containing content */
846 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
851 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
854 snprintf (bp
, buflen
, "%s%s", s
, p
->c_storage
);
861 /* insert filename containing content */
862 snprintf (bp
, buflen
, "%s", file
);
865 * Old comments below are left here for posterity.
866 * This was/is tricky.
868 /* since we've quoted the file argument, set things up
869 * to look past it, to avoid problems with the quoting
870 * logic below. (I know, I should figure out what's
871 * broken with the quoting logic, but..)
874 * Here's the email that submitted the patch with
876 * https://www.mail-archive.com/nmh-workers@mhost.com/
878 * I can't tell from that exactly what was broken,
879 * beyond misquoting of the filename. The profile
880 * had appearances of %F both with and without quotes.
881 * The unquoted ones should have been quoted by the
883 * The fix was to always quote the filename. But
884 * that broke '%F' because it expanded to ''filename''.
887 * Old comments above are left here for posterity.
888 * The quoting below should work properly now.
894 /* No longer supported */
898 /* display listing prior to displaying content */
903 /* insert subtype of content */
904 strncpy (bp
, ci
->ci_subtype
, buflen
);
908 /* insert character % */
912 const char *closing_brace
= strchr(cp
, '}');
915 const size_t param_len
= closing_brace
- cp
- 1;
916 char *param
= mh_xmalloc(param_len
+ 1);
919 (void) strncpy(param
, cp
+ 1, param_len
);
920 param
[param_len
] = '\0';
921 value
= get_param(ci
->ci_first_pm
, param
, '?', 0);
924 cp
+= param_len
+ 1; /* Skip both braces, too. */
927 /* %{param} is set in the Content-Type header.
928 After the break below, quote it if necessary. */
929 (void) strncpy(bp
, value
, buflen
);
932 /* %{param} not found, so skip it completely. cp
933 was advanced above. */
937 /* This will get confused if there are multiple %{}'s,
938 but its real purpose is to avoid doing bad things
939 above if a closing brace wasn't found. */
940 inform("no closing brace for display string escape %s, continuing...",
957 /* Did we actually insert something? */
959 /* Insert single quote if not inside quotes already */
960 if (!quoted
&& buflen
) {
962 memmove (pp
+ 1, pp
, len
+1);
968 /* Escape existing quotes */
969 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
972 /* Quoted. Let this quote close that quoting.
973 Insert an escaped quote to replace it and
974 another quote to reopen quoting, which will be
976 memmove (pp
+ 2, pp
, len
);
983 /* Not quoted. This should not be reached with
984 the current code, but handle the condition
985 in case the code changes. Just escape the
987 memmove (pp
, pp
-1, len
+1);
993 /* If pp is still set, that means we ran out of space. */
997 if (quoted
&& buflen
) {
998 /* See if we need to close the quote by looking
999 for an odd number of unescaped close quotes in
1000 the remainder of the display string. */
1001 int found_quote
= 0, escaped
= 0;
1004 for (c
= cp
+1; *c
; ++c
) {
1006 escaped
= ! escaped
;
1012 found_quote
= ! found_quote
;
1017 if (! found_quote
) {
1037 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1038 /* content_error would provide a more useful error message
1039 * here, except that if we got overrun, it probably would
1045 /* use charset string to modify display method */
1046 if (ct
->c_termproc
) {
1049 strncpy (term
, buffer
, sizeof(term
));
1050 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1058 convert_charset (CT ct
, char *dest_charset
, int *message_mods
) {
1059 char *src_charset
= content_charset (ct
);
1062 if (strcasecmp (src_charset
, dest_charset
)) {
1064 iconv_t conv_desc
= NULL
;
1071 int opened_input_file
= 0;
1072 char src_buffer
[BUFSIZ
];
1073 size_t dest_buffer_size
= BUFSIZ
;
1074 char *dest_buffer
= mh_xmalloc(dest_buffer_size
);
1077 int fromutf8
= !strcasecmp(src_charset
, "UTF-8");
1079 if ((conv_desc
= iconv_open (dest_charset
, src_charset
)) ==
1081 inform("Can't convert %s to %s", src_charset
, dest_charset
);
1086 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1087 adios (NULL
, "unable to create temporary file in %s",
1090 dest
= mh_xstrdup(tempfile
);
1092 if (ct
->c_cefile
.ce_file
) {
1093 file
= &ct
->c_cefile
.ce_file
;
1094 fp
= &ct
->c_cefile
.ce_fp
;
1096 } else if (ct
->c_file
) {
1099 begin
= (size_t) ct
->c_begin
;
1100 end
= (size_t) ct
->c_end
;
1101 } /* else no input file: shouldn't happen */
1103 if (file
&& *file
&& fp
) {
1105 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1106 advise (*file
, "unable to open for reading");
1109 opened_input_file
= 1;
1116 size_t bytes_to_read
=
1117 end
> 0 && end
> begin
? end
- begin
: sizeof src_buffer
;
1119 fseeko (*fp
, begin
, SEEK_SET
);
1120 while ((inbytes
= fread (src_buffer
, 1,
1121 min (bytes_to_read
, sizeof src_buffer
),
1123 ICONV_CONST
char *ib
= src_buffer
;
1124 char *ob
= dest_buffer
;
1125 size_t outbytes
= dest_buffer_size
;
1126 size_t outbytes_before
= outbytes
;
1128 if (end
> 0) bytes_to_read
-= inbytes
;
1131 if (iconv (conv_desc
, &ib
, &inbytes
, &ob
, &outbytes
) ==
1133 if (errno
== E2BIG
) {
1135 * Bump up the buffer by at least a factor of 2
1136 * over what we need.
1138 size_t bumpup
= inbytes
* 2, ob_off
= ob
- dest_buffer
;
1139 dest_buffer_size
+= bumpup
;
1140 dest_buffer
= mh_xrealloc(dest_buffer
,
1142 ob
= dest_buffer
+ ob_off
;
1144 outbytes_before
+= bumpup
;
1147 if (errno
== EINVAL
) {
1148 /* middle of multi-byte sequence */
1149 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1150 advise (dest
, "write");
1152 fseeko (*fp
, -inbytes
, SEEK_CUR
);
1153 if (end
> 0) bytes_to_read
+= inbytes
;
1154 /* inform("convert_charset: EINVAL"); */
1157 if (errno
== EILSEQ
) {
1158 /* invalid multi-byte sequence */
1160 for (++ib
, --inbytes
;
1162 (((unsigned char) *ib
) & 0xc0) == 0x80;
1166 ib
++; inbytes
--; /* skip it */
1168 (*ob
++) = '?'; outbytes
--;
1169 /* inform("convert_charset: EILSEQ"); */
1172 inform("convert_charset: errno = %d", errno
);
1177 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
) < 0) {
1178 advise (dest
, "write");
1182 if (opened_input_file
) {
1188 iconv_close (conv_desc
);
1192 /* Replace the decoded file with the converted one. */
1193 if (ct
->c_cefile
.ce_file
) {
1194 if (ct
->c_cefile
.ce_unlink
) {
1195 (void) m_unlink (ct
->c_cefile
.ce_file
);
1197 free (ct
->c_cefile
.ce_file
);
1199 ct
->c_cefile
.ce_file
= dest
;
1200 ct
->c_cefile
.ce_unlink
= 1;
1204 /* Update ct->c_ctline. */
1206 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1207 ct
->c_ctinfo
.ci_subtype
, NULL
);
1210 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1211 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1213 outline
= output_params(LEN(TYPE_FIELD
) + 1 + strlen(ctline
),
1214 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1216 ctline
= add(outline
, ctline
);
1220 free (ct
->c_ctline
);
1221 ct
->c_ctline
= ctline
;
1222 } /* else no CT line, which is odd */
1224 /* Update Content-Type header field. */
1225 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1226 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1227 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1235 (void) m_unlink (dest
);
1238 #else /* ! HAVE_ICONV */
1239 NMH_UNUSED (message_mods
);
1241 inform("Can't convert %s to %s without iconv", src_charset
,
1245 #endif /* ! HAVE_ICONV */
1254 convert_content_charset (CT ct
, char **file
) {
1258 /* Using current locale, see if the content needs to be converted. */
1260 /* content_charset() cannot return NULL. */
1261 char *src_charset
= content_charset (ct
);
1263 if (! check_charset (src_charset
, strlen (src_charset
))) {
1266 char *dest_charset
= getcpy (get_charset ());
1268 if (convert_charset (ct
, dest_charset
, &unused
) == 0) {
1269 *file
= ct
->c_cefile
.ce_file
;
1274 free (dest_charset
);
1277 #else /* ! HAVE_ICONV */
1280 #endif /* ! HAVE_ICONV */
1286 * Compile our format string and save any parameters we care about.
1289 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1290 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1291 "%<{description}%{description}" \
1292 "%?{cdispo-filename}%{cdispo-filename}" \
1293 "%|%{ctype-name}%> " \
1294 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1296 static struct format
*
1297 compile_header(char *form
)
1301 struct comp
*comp
= NULL
;
1302 unsigned int bucket
;
1304 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1306 (void) fmt_compile(fmtstring
, &fmt
, 1);
1309 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1310 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1318 static struct format
*
1319 compile_marker(char *form
)
1323 struct comp
*comp
= NULL
;
1324 unsigned int bucket
;
1325 struct param_comp_list
*pc_entry
;
1327 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1329 (void) fmt_compile(fmtstring
, &fmt
, 1);
1333 * Things we care about:
1335 * part - Part name (e.g., 1.1)
1336 * content-type - Content-Type
1337 * description - Content-Description
1338 * disposition - Content-Disposition (inline, attachment)
1339 * ctype-<param> - Content-Type parameter
1340 * cdispo-<param> - Content-Disposition parameter
1343 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1344 if (strcasecmp(comp
->c_name
, "part") == 0) {
1346 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1348 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1349 description_comp
= comp
;
1350 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1352 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1353 strlen(comp
->c_name
) > 6) {
1355 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 6);
1356 pc_entry
->comp
= comp
;
1357 pc_entry
->next
= ctype_pc_list
;
1358 ctype_pc_list
= pc_entry
;
1359 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1360 strlen(comp
->c_name
) > 7) {
1362 pc_entry
->param
= mh_xstrdup(comp
->c_name
+ 7);
1363 pc_entry
->comp
= comp
;
1364 pc_entry
->next
= dispo_pc_list
;
1365 dispo_pc_list
= pc_entry
;
1373 * Output on stdout an appropriate marker for this content, using mh-format
1377 output_header(CT ct
, struct format
*fmt
)
1379 charstring_t outbuf
= charstring_create (BUFSIZ
);
1385 folder_comp
->c_text
= getcpy(folder
);
1387 if (ct
->c_file
&& *ct
->c_file
) {
1388 message
= strtol(ct
->c_file
, &endp
, 10);
1389 if (*endp
) message
= 0;
1393 /* it would be nice to populate dat[2], for %(size) here,
1394 * but it's not available. it might also be nice to know
1395 * if the message originally had any mime parts or not -- but
1396 * there's also no record of that. (except for MIME-version:)
1399 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1401 fputs(charstring_buffer (outbuf
), stdout
);
1402 charstring_free (outbuf
);
1408 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1410 charstring_t outbuf
= charstring_create (BUFSIZ
);
1411 struct param_comp_list
*pcentry
;
1418 * Grab any items we care about.
1421 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1422 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1423 ct
->c_ctinfo
.ci_subtype
, NULL
);
1426 if (part_comp
&& ct
->c_partno
) {
1427 part_comp
->c_text
= mh_xstrdup(ct
->c_partno
);
1430 if (description_comp
&& ct
->c_descr
) {
1431 description_comp
->c_text
= mh_xstrdup(ct
->c_descr
);
1434 if (dispo_comp
&& ct
->c_dispo_type
) {
1435 dispo_comp
->c_text
= mh_xstrdup(ct
->c_dispo_type
);
1438 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1439 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1440 pcentry
->param
, '?', 0);
1443 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1444 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1445 pcentry
->param
, '?', 0);
1448 if (ct
->c_cesizefnx
)
1449 partsize
= (*ct
->c_cesizefnx
) (ct
);
1451 partsize
= ct
->c_end
- ct
->c_begin
;
1453 if (ct
->c_file
&& *ct
->c_file
) {
1454 message
= strtol(ct
->c_file
, &endp
, 10);
1455 if (*endp
) message
= 0;
1460 /* make the part's hidden aspect available by overloading the
1461 * %(unseen) function. make the part's size available via %(size).
1462 * see comments in h/fmt_scan.h.
1466 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1468 fputs(charstring_buffer (outbuf
), stdout
);
1469 charstring_free (outbuf
);
1475 * Reset (and free) any of the saved marker text
1479 free_markercomps(void)
1481 struct param_comp_list
*pc_entry
, *pc2
;
1486 description_comp
= NULL
;
1489 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1490 free(pc_entry
->param
);
1491 pc2
= pc_entry
->next
;
1496 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1497 free(pc_entry
->param
);
1498 pc2
= pc_entry
->next
;
1505 * Exit if the display process returned with a nonzero exit code, or terminated
1506 * with a SIGQUIT signal.
1510 pidcheck (int status
)
1512 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)