]>
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 */
36 /* for output markerss and headers */
46 int type_ok (CT
, int);
47 void content_error (char *, CT
, char *, ...);
48 void flush_errors (void);
53 static void show_single_message (CT
, char *, int, int, int, struct format
*);
54 static void DisplayMsgHeader (CT
, char *, int);
55 static int show_switch (CT
, int, int, int, int, struct format
*);
56 static int show_content (CT
, int, int, int, struct format
*);
57 static int show_content_aux2 (CT
, int, char *, char *, int, int, int, struct format
*);
58 static int show_text (CT
, int, int, struct format
*);
59 static int show_multi (CT
, int, int, int, int, struct format
*);
60 static int show_multi_internal (CT
, int, int, int, int, struct format
*);
61 static int show_multi_aux (CT
, int, char *, struct format
*);
62 static int show_message_rfc822 (CT
, int, struct format
*);
63 static int show_partial (CT
, int);
64 static int show_external (CT
, int, int, int, int, struct format
*);
65 static int parse_display_string (CT
, char *, int *, int *, char *, char *,
66 size_t, int multipart
);
67 static int convert_content_charset (CT
, char **);
68 static struct format
*compile_header(char *);
69 static struct format
*compile_marker(char *);
70 static void output_header (CT
, struct format
*);
71 static void output_marker (CT
, struct format
*, int);
72 static void free_markercomps (void);
73 static int pidcheck(int);
76 * Components (and list of parameters/components) we care about for the
77 * content marker display.
80 static struct comp
*folder_comp
= NULL
;
81 static struct comp
*part_comp
= NULL
;
82 static struct comp
*ctype_comp
= NULL
;
83 static struct comp
*description_comp
= NULL
;
84 static struct comp
*dispo_comp
= NULL
;
86 struct param_comp_list
{
89 struct param_comp_list
*next
;
92 static struct param_comp_list
*ctype_pc_list
= NULL
;
93 static struct param_comp_list
*dispo_pc_list
= NULL
;
97 * Top level entry point to show/display a group of messages
101 show_all_messages (CT
*cts
, int concatsw
, int textonly
, int inlineonly
)
104 struct format
*hfmt
, *mfmt
;
107 * If form is not specified, then get default form
108 * for showing headers of MIME messages.
111 formsw
= getcpy (etcpath ("mhl.headers"));
114 * Compile the content marker and header format lines
116 mfmt
= compile_marker(markerform
);
117 hfmt
= compile_header(headerform
);
120 * If form is "mhl.null", suppress display of header.
122 if (!strcmp (formsw
, "mhl.null"))
125 for (ctp
= cts
; *ctp
; ctp
++) {
128 /* if top-level type is ok, then display message */
130 if (headersw
) output_header(ct
, hfmt
);
132 show_single_message (ct
, formsw
, concatsw
, textonly
, inlineonly
,
143 * Entry point to show/display a single message
147 show_single_message (CT ct
, char *form
, int concatsw
, int textonly
,
148 int inlineonly
, struct format
*fmt
)
154 /* Allow user executable bit so that temporary directories created by
155 * the viewer (e.g., lynx) are going to be accessible */
156 umask (ct
->c_umask
& ~(0100));
159 * If you have a format file, then display
160 * the message headers.
163 DisplayMsgHeader(ct
, form
, concatsw
);
165 /* Show the body of the message */
166 show_switch (ct
, 0, concatsw
, textonly
, inlineonly
, fmt
);
172 if (ct
->c_ceclosefnx
)
173 (*ct
->c_ceclosefnx
) (ct
);
175 /* block a few signals */
177 sigaddset (&set
, SIGHUP
);
178 sigaddset (&set
, SIGINT
);
179 sigaddset (&set
, SIGQUIT
);
180 sigaddset (&set
, SIGTERM
);
181 sigprocmask (SIG_BLOCK
, &set
, &oset
);
183 while (!concatsw
&& wait (&status
) != NOTOK
) {
188 /* reset the signal mask */
189 sigprocmask (SIG_SETMASK
, &oset
, &set
);
196 * Use the mhlproc to show the header fields
200 DisplayMsgHeader (CT ct
, char *form
, int concatsw
)
207 vec
= argsplit(mhlproc
, &file
, &vecp
);
208 vec
[vecp
++] = getcpy("-form");
209 vec
[vecp
++] = getcpy(form
);
210 vec
[vecp
++] = getcpy("-nobody");
211 vec
[vecp
++] = getcpy(ct
->c_file
);
214 * If we've specified -(no)moreproc,
215 * then just pass that along.
217 if (nomore
|| concatsw
) {
218 vec
[vecp
++] = getcpy("-nomoreproc");
220 vec
[vecp
++] = getcpy("-moreproc");
221 vec
[vecp
++] = getcpy(progsw
);
227 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
232 adios ("fork", "unable to");
237 fprintf (stderr
, "unable to exec ");
243 pidcheck(pidwait(child_id
, NOTOK
));
247 arglist_free(file
, vec
);
252 * Switching routine. Call the correct routine
253 * based on content type.
257 show_switch (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
260 switch (ct
->c_type
) {
262 return show_multi (ct
, alternate
, concatsw
, textonly
,
266 switch (ct
->c_subtype
) {
267 case MESSAGE_PARTIAL
:
268 return show_partial (ct
, alternate
);
270 case MESSAGE_EXTERNAL
:
271 return show_external (ct
, alternate
, concatsw
, textonly
,
275 return show_message_rfc822 (ct
, alternate
, fmt
);
278 * Treat unknown message types as equivalent to
279 * application/octet-stream for now
282 return show_content (ct
, alternate
, textonly
,
287 return show_text (ct
, alternate
, concatsw
, fmt
);
294 return show_content (ct
, alternate
, textonly
, inlineonly
, fmt
);
297 return 0; /* NOT REACHED */
302 * Generic method for displaying content
306 show_content (CT ct
, int alternate
, int textonly
, int inlineonly
,
310 CI ci
= &ct
->c_ctinfo
;
313 * If we're here, we are not a text type. So we don't need to check
317 if (textonly
|| (inlineonly
&& !is_inline(ct
))) {
318 output_marker(ct
, fmt
, 1);
322 /* Check for invo_name-show-type[/subtype] */
323 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
324 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
326 if ((cp
= ct
->c_showproc
))
327 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
329 /* complain if we are not a part of a multipart/alternative */
331 content_error (NULL
, ct
, "don't know how to display content");
338 * Parse the display string for displaying generic content
342 show_content_aux (CT ct
, int alternate
, char *cp
, char *cracked
, struct format
*fmt
)
345 int xstdin
= 0, xlist
= 0;
346 char *file
= NULL
, buffer
[BUFSIZ
];
348 if (!ct
->c_ceopenfnx
) {
350 content_error (NULL
, ct
, "don't know how to decode content");
355 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
357 if (ct
->c_showproc
&& !strcmp (ct
->c_showproc
, "true"))
360 if (! strcmp(invo_name
, "mhshow") &&
361 ct
->c_type
== CT_TEXT
&& ct
->c_subtype
== TEXT_PLAIN
) {
362 /* This has to be done after calling c_ceopenfnx, so
363 unfortunately the type checks are necessary without
364 some code rearrangement. And to make this really ugly,
365 only do it in mhshow, not mhfixmsg, mhn, or mhstore. */
366 if (convert_content_charset (ct
, &file
) == OK
) {
367 (*ct
->c_ceclosefnx
) (ct
);
368 if ((fd
= (*ct
->c_ceopenfnx
) (ct
, &file
)) == NOTOK
)
371 char *charset
= content_charset (ct
);
372 admonish (NULL
, "unable to convert character set%s%s to %s",
373 ct
->c_partno
? " of part " : "",
374 ct
->c_partno
? ct
->c_partno
: "",
381 strncpy (buffer
, cp
, sizeof(buffer
));
385 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
, buffer
,
386 sizeof(buffer
) - 1, 0)) {
387 admonish (NULL
, "Buffer overflow constructing show command!\n");
392 return show_content_aux2 (ct
, alternate
, cracked
, buffer
,
393 fd
, xlist
, xstdin
, fmt
);
398 * Routine to actually display the content
402 show_content_aux2 (CT ct
, int alternate
, char *cracked
, char *buffer
,
403 int fd
, int xlist
, int xstdin
, struct format
*fmt
)
409 if (debugsw
|| cracked
) {
412 fprintf (stderr
, "%s msg %s", cracked
? "storing" : "show",
415 fprintf (stderr
, " part %s", ct
->c_partno
);
417 fprintf (stderr
, " using command (cd %s; %s)\n", cracked
, buffer
);
419 fprintf (stderr
, " using command %s\n", buffer
);
423 output_marker(ct
, fmt
, 0);
427 * If the command is a zero-length string, just write the output on
431 if (buffer
[0] == '\0') {
432 char readbuf
[BUFSIZ
];
434 char lastchar
= '\n';
437 advise(NULL
, "Cannot use NULL command to display content-type "
438 "%s/%s", ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
);
442 while ((cc
= read(fd
, readbuf
, sizeof(readbuf
))) > 0) {
443 if ((ssize_t
) fwrite(readbuf
, sizeof(char), cc
, stdout
) < cc
) {
444 advise ("putline", "fwrite");
446 lastchar
= readbuf
[cc
- 1];
450 advise("read", "while reading text content");
455 * The MIME standards allow content to not have a trailing newline.
456 * But because we are (presumably) sending this to stdout, include
457 * a newline for text content if the final character was not a
458 * newline. Only do this for mhshow.
461 if (strcmp(invo_name
, "mhshow") == 0 && ct
->c_type
== CT_TEXT
&&
462 ct
->c_subtype
== TEXT_PLAIN
&& lastchar
!= '\n') {
471 vec
= argsplit(buffer
, &file
, &vecp
);
476 for (i
= 0; (child_id
= fork()) == NOTOK
&& i
< 5; i
++)
480 advise ("fork", "unable to");
481 (*ct
->c_ceclosefnx
) (ct
);
486 if (chdir (cracked
) < 0) {
487 advise (cracked
, "chdir");
494 fprintf (stderr
, "unable to exec ");
502 arglist_free(file
, vec
);
504 pidcheck ((status
= pidXwait (child_id
, NULL
)));
507 (*ct
->c_ceclosefnx
) (ct
);
508 return (alternate
? OK
: status
);
515 * show content of type "text"
519 show_text (CT ct
, int alternate
, int concatsw
, struct format
*fmt
)
521 char *cp
, buffer
[BUFSIZ
];
522 CI ci
= &ct
->c_ctinfo
;
524 /* Check for invo_name-show-type[/subtype] */
525 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
526 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
529 * Use default method if content is text/plain, or if
530 * if it is not a text part of a multipart/alternative
532 if (!alternate
|| ct
->c_subtype
== TEXT_PLAIN
) {
535 snprintf(buffer
, sizeof(buffer
), "%%lcat");
537 snprintf(buffer
, sizeof(buffer
), "%%l");
539 snprintf (buffer
, sizeof(buffer
), "%%l%s %%F", progsw
? progsw
:
540 moreproc
&& *moreproc
? moreproc
: DEFAULT_PAGER
);
541 cp
= (ct
->c_showproc
= add (buffer
, NULL
));
542 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
550 * show message body of type "multipart"
554 show_multi (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
558 CI ci
= &ct
->c_ctinfo
;
560 /* Check for invo_name-show-type[/subtype] */
561 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
562 return show_multi_aux (ct
, alternate
, cp
, fmt
);
564 if ((cp
= ct
->c_showproc
)) {
565 return show_multi_aux (ct
, alternate
, cp
, fmt
);
569 * Use default method to display this multipart content. Even
570 * unknown types are displayable, since they're treated as mixed
573 return show_multi_internal (ct
, alternate
, concatsw
, textonly
,
579 * show message body of subtypes of multipart that
580 * we understand directly (mixed, alternate, etc...)
584 show_multi_internal (CT ct
, int alternate
, int concatsw
, int textonly
,
585 int inlineonly
, struct format
*fmt
)
587 int alternating
, nowalternate
, result
;
588 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
597 nowalternate
= alternate
;
599 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
605 * alternate -> we are a part inside a multipart/alternative
606 * alternating -> we are a multipart/alternative
614 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
617 /* while looking for the right displayable alternative, we
618 * use a looser search criterion than we do after finding it.
619 * specifically, while still looking, part_ok() will match
620 * "parent" parts (e.g. "-part 2" where 2 is a high-level
621 * multipart). after finding it, we use part_exact() to only
622 * choose a part that was requested explicitly.
624 if ((part_exact(p
) && type_ok(p
, 1)) ||
625 (!mult_alt_done
&& part_ok (p
) && type_ok (p
, 1))) {
629 inneresult
= show_switch (p
, nowalternate
, concatsw
, textonly
,
631 switch (inneresult
) {
632 case NOTOK
: /* hard display error */
634 if (alternate
&& !alternating
) {
640 case DONE
: /* found no match on content type */
643 case OK
: /* display successful */
648 /* if we got success on a sub-part of
649 * multipart/alternative, we're done, unless
650 * there's a chance an explicit part should be
651 * matched later in the alternatives. */
654 } else if (alternate
) {
655 alternate
= nowalternate
= 0;
663 /* we're supposed to be displaying at least something from a
664 * multipart/alternative. if we've had parts to consider, and
665 * we've had no success, then we should complain. we shouldn't
666 * complain if none of the parts matched any -part or -type option.
668 if (alternating
&& request_matched
&& !display_success
) {
669 /* if we're ourselves an alternate. don't complain yet. */
671 content_error (NULL
, ct
, "don't know how to display any of the contents");
676 /* if no parts matched what was requested, there can't have been
677 * any display errors. we report DONE rather than OK. */
678 ret
= request_matched
? result
: DONE
;
684 * Parse display string for multipart content
685 * and use external program to display it.
689 show_multi_aux (CT ct
, int alternate
, char *cp
, struct format
*fmt
)
691 /* xstdin is only used in the call to parse_display_string():
692 its value is ignored in the function. */
693 int xstdin
= 0, xlist
= 0;
694 char *file
= NULL
, buffer
[BUFSIZ
];
695 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
699 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
702 if (!p
->c_ceopenfnx
) {
704 content_error (NULL
, p
, "don't know how to decode content");
708 if (p
->c_storage
== NULL
) {
709 if ((*p
->c_ceopenfnx
) (p
, &file
) == NOTOK
)
712 p
->c_storage
= add (file
, NULL
);
714 if (p
->c_showproc
&& !strcmp (p
->c_showproc
, "true"))
716 (*p
->c_ceclosefnx
) (p
);
720 if (parse_display_string (ct
, cp
, &xstdin
, &xlist
, file
,
721 buffer
, sizeof(buffer
) - 1, 1)) {
722 admonish (NULL
, "Buffer overflow constructing show command!\n");
726 return show_content_aux2 (ct
, alternate
, NULL
, buffer
, NOTOK
, xlist
, 0, fmt
);
731 * show content of type "message/rfc822"
735 show_message_rfc822 (CT ct
, int alternate
, struct format
*fmt
)
738 CI ci
= &ct
->c_ctinfo
;
740 /* Check for invo_name-show-type[/subtype] */
741 if ((cp
= context_find_by_type ("show", ci
->ci_type
, ci
->ci_subtype
)))
742 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
744 if ((cp
= ct
->c_showproc
))
745 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
747 /* default method for message/rfc822 */
748 if (ct
->c_subtype
== MESSAGE_RFC822
) {
749 cp
= (ct
->c_showproc
= add ("%pshow -file %F", NULL
));
750 return show_content_aux (ct
, alternate
, cp
, NULL
, fmt
);
753 /* complain if we are not a part of a multipart/alternative */
755 content_error (NULL
, ct
, "don't know how to display content");
762 * Show content of type "message/partial".
766 show_partial (CT ct
, int alternate
)
768 NMH_UNUSED (alternate
);
770 content_error (NULL
, ct
,
771 "in order to display this message, you must reassemble it");
777 * Show content of type "message/external".
779 * THE ERROR CHECKING IN THIS ONE IS NOT DONE YET.
783 show_external (CT ct
, int alternate
, int concatsw
, int textonly
, int inlineonly
,
786 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
787 CT p
= e
->eb_content
;
792 return show_switch (p
, alternate
, concatsw
, textonly
, inlineonly
, fmt
);
797 parse_display_string (CT ct
, char *cp
, int *xstdin
, int *xlist
,
798 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. */
946 "no closing brace for display string escape %s",
963 /* Did we actually insert something? */
965 /* Insert single quote if not inside quotes already */
966 if (!quoted
&& buflen
) {
968 memmove (pp
+ 1, pp
, len
+1);
974 /* Escape existing quotes */
975 while ((pp
= strchr (pp
, '\'')) && buflen
> 3) {
978 /* Quoted. Let this quote close that quoting.
979 Insert an escaped quote to replace it and
980 another quote to reopen quoting, which will be
982 memmove (pp
+ 2, pp
, len
);
989 /* Not quoted. This should not be reached with
990 the current code, but handle the condition
991 in case the code changes. Just escape the
993 memmove (pp
, pp
-1, len
+1);
999 /* If pp is still set, that means we ran out of space. */
1002 /* Close quoting. */
1003 if (quoted
&& buflen
) {
1004 /* See if we need to close the quote by looking
1005 for an odd number of unescaped close quotes in
1006 the remainder of the display string. */
1007 int found_quote
= 0, escaped
= 0;
1010 for (c
= cp
+1; *c
; ++c
) {
1012 escaped
= ! escaped
;
1018 found_quote
= ! found_quote
;
1023 if (! found_quote
) {
1043 (ct
->c_termproc
&& buflen
<= strlen(ct
->c_termproc
))) {
1044 /* content_error would provide a more useful error message
1045 * here, except that if we got overrun, it probably would
1051 /* use charset string to modify display method */
1052 if (ct
->c_termproc
) {
1055 strncpy (term
, buffer
, sizeof(term
));
1056 snprintf (buffer
, buflen
, ct
->c_termproc
, term
);
1064 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 int opened_input_file
= 0;
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 advise (NULL
, "Can't convert %s to %s", src_charset
, dest_charset
);
1092 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
1093 adios (NULL
, "unable to create temporary file in %s",
1096 dest
= add (tempfile
, NULL
);
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
= 1;
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 /* advise(NULL, "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 /* advise(NULL, "convert_charset: EILSEQ"); */
1178 advise (NULL
, "convert_charset: errno = %d", errno
);
1182 if (write (fd
, dest_buffer
, outbytes_before
- outbytes
)
1184 advise (dest
, "write");
1189 if (opened_input_file
) {
1195 iconv_close (conv_desc
);
1199 /* Replace the decoded file with the converted one. */
1200 if (ct
->c_cefile
.ce_file
) {
1201 if (ct
->c_cefile
.ce_unlink
) {
1202 (void) m_unlink (ct
->c_cefile
.ce_file
);
1204 free (ct
->c_cefile
.ce_file
);
1206 ct
->c_cefile
.ce_file
= dest
;
1207 ct
->c_cefile
.ce_unlink
= 1;
1211 /* Update ct->c_ctline. */
1213 char *ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
1214 ct
->c_ctinfo
.ci_subtype
, NULL
);
1217 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
1218 &ct
->c_ctinfo
.ci_last_pm
, "charset",
1220 outline
= output_params(strlen(TYPE_FIELD
) + 1 + strlen(ctline
),
1221 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
1223 ctline
= add(outline
, ctline
);
1227 free (ct
->c_ctline
);
1228 ct
->c_ctline
= ctline
;
1229 } /* else no CT line, which is odd */
1231 /* Update Content-Type header field. */
1232 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1233 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1234 char *ctline
= concat (ct
->c_ctline
, "\n", NULL
);
1242 (void) m_unlink (dest
);
1245 #else /* ! HAVE_ICONV */
1246 NMH_UNUSED (message_mods
);
1248 advise (NULL
, "Can't convert %s to %s without iconv", src_charset
,
1252 #endif /* ! HAVE_ICONV */
1261 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 *charset
= content_charset (ct
);
1270 if (! check_charset (charset
, strlen (charset
))) {
1273 char *charset
= getcpy (get_charset ());
1275 if (convert_charset (ct
, charset
, &unused
) == 0) {
1276 *file
= ct
->c_cefile
.ce_file
;
1282 #else /* ! HAVE_ICONV */
1285 #endif /* ! HAVE_ICONV */
1291 * Compile our format string and save any parameters we care about.
1294 #define DEFAULT_HEADER "[ Message %{folder}%<{folder}:%>%(msg) ]"
1295 #define DEFAULT_MARKER "[ part %{part} - %{content-type} - " \
1296 "%<{description}%{description}" \
1297 "%?{cdispo-filename}%{cdispo-filename}" \
1298 "%|%{ctype-name}%> " \
1299 "%(kilo(size))B %<(unseen)\\(suppressed\\)%> ]"
1301 static struct format
*
1302 compile_header(char *form
)
1306 struct comp
*comp
= NULL
;
1307 unsigned int bucket
;
1309 fmtstring
= new_fs(form
, NULL
, DEFAULT_HEADER
);
1311 (void) fmt_compile(fmtstring
, &fmt
, 1);
1314 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1315 if (strcasecmp(comp
->c_name
, "folder") == 0) {
1323 static struct format
*
1324 compile_marker(char *form
)
1328 struct comp
*comp
= NULL
;
1329 unsigned int bucket
;
1330 struct param_comp_list
*pc_entry
;
1332 fmtstring
= new_fs(form
, NULL
, DEFAULT_MARKER
);
1334 (void) fmt_compile(fmtstring
, &fmt
, 1);
1338 * Things we care about:
1340 * part - Part name (e.g., 1.1)
1341 * content-type - Content-Type
1342 * description - Content-Description
1343 * disposition - Content-Disposition (inline, attachment)
1344 * ctype-<param> - Content-Type parameter
1345 * cdispo-<param> - Content-Disposition parameter
1348 while ((comp
= fmt_nextcomp(comp
, &bucket
)) != NULL
) {
1349 if (strcasecmp(comp
->c_name
, "part") == 0) {
1351 } else if (strcasecmp(comp
->c_name
, "content-type") == 0) {
1353 } else if (strcasecmp(comp
->c_name
, "description") == 0) {
1354 description_comp
= comp
;
1355 } else if (strcasecmp(comp
->c_name
, "disposition") == 0) {
1357 } else if (strncasecmp(comp
->c_name
, "ctype-", 6) == 0 &&
1358 strlen(comp
->c_name
) > 6) {
1359 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1360 pc_entry
->param
= getcpy(comp
->c_name
+ 6);
1361 pc_entry
->comp
= comp
;
1362 pc_entry
->next
= ctype_pc_list
;
1363 ctype_pc_list
= pc_entry
;
1364 } else if (strncasecmp(comp
->c_name
, "cdispo-", 7) == 0 &&
1365 strlen(comp
->c_name
) > 7) {
1366 pc_entry
= mh_xmalloc(sizeof(*pc_entry
));
1367 pc_entry
->param
= getcpy(comp
->c_name
+ 7);
1368 pc_entry
->comp
= comp
;
1369 pc_entry
->next
= dispo_pc_list
;
1370 dispo_pc_list
= pc_entry
;
1378 * Output on stdout an appropriate marker for this content, using mh-format
1382 output_header(CT ct
, struct format
*fmt
)
1384 charstring_t outbuf
= charstring_create (BUFSIZ
);
1390 folder_comp
->c_text
= getcpy(folder
);
1392 if (ct
->c_file
&& *ct
->c_file
) {
1393 message
= strtol(ct
->c_file
, &endp
, 10);
1394 if (*endp
) message
= 0;
1398 /* it would be nice to populate dat[2], for %(size) here,
1399 * but it's not available. it might also be nice to know
1400 * if the message originally had any mime parts or not -- but
1401 * there's also no record of that. (except for MIME-version:)
1404 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1406 fputs(charstring_buffer (outbuf
), stdout
);
1407 charstring_free (outbuf
);
1413 output_marker(CT ct
, struct format
*fmt
, int hidden
)
1415 charstring_t outbuf
= charstring_create (BUFSIZ
);
1416 struct param_comp_list
*pcentry
;
1423 * Grab any items we care about.
1426 if (ctype_comp
&& ct
->c_ctinfo
.ci_type
) {
1427 ctype_comp
->c_text
= concat(ct
->c_ctinfo
.ci_type
, "/",
1428 ct
->c_ctinfo
.ci_subtype
, NULL
);
1431 if (part_comp
&& ct
->c_partno
) {
1432 part_comp
->c_text
= getcpy(ct
->c_partno
);
1435 if (description_comp
&& ct
->c_descr
) {
1436 description_comp
->c_text
= getcpy(ct
->c_descr
);
1439 if (dispo_comp
&& ct
->c_dispo_type
) {
1440 dispo_comp
->c_text
= getcpy(ct
->c_dispo_type
);
1443 for (pcentry
= ctype_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1444 pcentry
->comp
->c_text
= get_param(ct
->c_ctinfo
.ci_first_pm
,
1445 pcentry
->param
, '?', 0);
1448 for (pcentry
= dispo_pc_list
; pcentry
!= NULL
; pcentry
= pcentry
->next
) {
1449 pcentry
->comp
->c_text
= get_param(ct
->c_dispo_first
,
1450 pcentry
->param
, '?', 0);
1453 if (ct
->c_cesizefnx
)
1454 partsize
= (*ct
->c_cesizefnx
) (ct
);
1456 partsize
= ct
->c_end
- ct
->c_begin
;
1458 if (ct
->c_file
&& *ct
->c_file
) {
1459 message
= strtol(ct
->c_file
, &endp
, 10);
1460 if (*endp
) message
= 0;
1465 /* make the part's hidden aspect available by overloading the
1466 * %(unseen) function. make the part's size available via %(size).
1467 * see comments in h/fmt_scan.h.
1471 fmt_scan(fmt
, outbuf
, BUFSIZ
, dat
, NULL
);
1473 fputs(charstring_buffer (outbuf
), stdout
);
1474 charstring_free (outbuf
);
1480 * Reset (and free) any of the saved marker text
1484 free_markercomps(void)
1486 struct param_comp_list
*pc_entry
, *pc2
;
1491 description_comp
= NULL
;
1494 for (pc_entry
= ctype_pc_list
; pc_entry
!= NULL
; ) {
1495 free(pc_entry
->param
);
1496 pc2
= pc_entry
->next
;
1501 for (pc_entry
= dispo_pc_list
; pc_entry
!= NULL
; ) {
1502 free(pc_entry
->param
);
1503 pc2
= pc_entry
->next
;
1510 * Exit if the display process returned with a nonzero exit code, or terminated
1511 * with a SIGQUIT signal.
1515 pidcheck (int status
)
1517 if ((status
& 0xff00) == 0xff00 || (status
& 0x007f) != SIGQUIT
)