2 * mhfixmsg.c -- rewrite a message with various transformations
4 * This code is Copyright (c) 2002 and 2013, by the authors of nmh.
5 * See the COPYRIGHT file in the root directory of the nmh
6 * distribution for complete copyright information.
11 #include <h/mhparse.h>
13 #include <h/signals.h>
16 #define MHFIXMSG_SWITCHES \
17 X("decodetext 8bit|7bit|binary", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("decodetypes", 0, DECODETYPESW) \
20 X("crlflinebreaks", 0, CRLFLINEBREAKSSW) \
21 X("nocrlflinebreaks", 0, NCRLFLINEBREAKSSW) \
22 X("textcharset", 0, TEXTCHARSETSW) \
23 X("notextcharset", 0, NTEXTCHARSETSW) \
24 X("reformat", 0, REFORMATSW) \
25 X("noreformat", 0, NREFORMATSW) \
26 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
27 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
28 X("fixboundary", 0, FIXBOUNDARYSW) \
29 X("nofixboundary", 0, NFIXBOUNDARYSW) \
30 X("fixcte", 0, FIXCTESW) \
31 X("nofixcte", 0, NFIXCTESW) \
32 X("fixtype mimetype", 0, FIXTYPESW) \
33 X("file file", 0, FILESW) \
34 X("outfile file", 0, OUTFILESW) \
35 X("rmmproc program", 0, RPROCSW) \
36 X("normmproc", 0, NRPRCSW) \
37 X("changecur", 0, CHGSW) \
38 X("nochangecur", 0, NCHGSW) \
39 X("verbose", 0, VERBSW) \
40 X("noverbose", 0, NVERBSW) \
41 X("version", 0, VERSIONSW) \
42 X("help", 0, HELPSW) \
44 #define X(sw, minchars, id) id,
45 DEFINE_SWITCH_ENUM(MHFIXMSG
);
48 #define X(sw, minchars, id) { sw, minchars, id },
49 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
54 int debugsw
; /* Needed by mhparse.c. */
56 #define quitser pipeser
59 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
60 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
61 extern int bogus_mp_content
; /* flag from InitMultiPart */
62 /* flags to/from parse_header_attrs */
63 extern int suppress_extraneous_trailing_semicolon_warning
;
64 extern int extraneous_trailing_semicolon
;
67 int output_message (CT
, char *);
70 void flush_errors (void);
74 void freects_done (int) NORETURN
;
79 typedef struct fix_transformations
{
87 /* Whether to use CRLF linebreaks, per RFC 2046 Sec. 4.1.1, par.1. */
90 } fix_transformations
;
92 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
93 static int fix_boundary (CT
*, int *);
94 static int copy_input_to_output (const char *, const char *);
95 static int get_multipart_boundary (CT
, char **);
96 static int replace_boundary (CT
, char *, char *);
97 static int fix_types (CT
, svector_t
, int *);
98 static char *replace_substring (char **, const char *, const char *);
99 static char *remove_parameter (char *, const char *);
100 static int fix_multipart_cte (CT
, int *);
101 static int set_ce (CT
, int);
102 static int ensure_text_plain (CT
*, CT
, int *, int);
103 static int find_textplain_sibling (CT
, int, int *);
104 static int insert_new_text_plain_part (CT
, int, CT
);
105 static CT
build_text_plain_part (CT
);
106 static int insert_into_new_mp_alt (CT
*, int *);
107 static CT
divide_part (CT
);
108 static void copy_ctinfo (CI
, CI
);
109 static int decode_part (CT
);
110 static int reformat_part (CT
, char *, char *, char *, int);
111 static int charset_encoding (CT
);
112 static CT
build_multipart_alt (CT
, CT
, int, int);
113 static int boundary_in_content (FILE **, char *, const char *);
114 static void transfer_noncontent_headers (CT
, CT
);
115 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
116 static int decode_text_parts (CT
, int, const char *, int *);
117 static int should_decode(const char *, const char *, const char *);
118 static int content_encoding (CT
, const char **);
119 static int strip_crs (CT
, int *);
120 static int convert_charsets (CT
, char *, int *);
121 static int fix_always (CT
, int *);
122 static int fix_filename_param (char *, char *, PM
*, PM
*);
123 static int fix_filename_encoding (CT
);
124 static int write_content (CT
, const char *, char *, int, int);
125 static void set_text_ctparams(CT
, char *, int);
126 static int remove_file (const char *);
127 static void report (char *, char *, char *, char *, ...);
128 static void pipeser (int);
132 main (int argc
, char **argv
) {
134 char *cp
, *file
= NULL
, *folder
= NULL
;
135 char *maildir
, buf
[100], *outfile
= NULL
;
136 char **argp
, **arguments
;
137 struct msgs_array msgs
= { 0, 0, NULL
};
138 struct msgs
*mp
= NULL
;
144 fix_transformations fx
;
145 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
147 fx
.replacetextplain
= 0;
148 fx
.decodetext
= CE_8BIT
;
149 fx
.decodetypes
= "text,application/ics"; /* Default, per man page. */
150 fx
.lf_line_endings
= 0;
151 fx
.textcharset
= NULL
;
153 if (nmh_init(argv
[0], 1)) { return 1; }
157 arguments
= getarguments (invo_name
, argc
, argv
, 1);
163 while ((cp
= *argp
++)) {
165 switch (smatch (++cp
, switches
)) {
167 ambigsw (cp
, switches
);
170 adios (NULL
, "-%s unknown", cp
);
173 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
175 print_help (buf
, switches
, 1);
178 print_version(invo_name
);
182 if (! (cp
= *argp
++) || *cp
== '-') {
183 adios (NULL
, "missing argument to %s", argp
[-2]);
185 if (! strcasecmp (cp
, "8bit")) {
186 fx
.decodetext
= CE_8BIT
;
187 } else if (! strcasecmp (cp
, "7bit")) {
188 fx
.decodetext
= CE_7BIT
;
189 } else if (! strcasecmp (cp
, "binary")) {
190 fx
.decodetext
= CE_BINARY
;
192 adios (NULL
, "invalid argument to %s", argp
[-2]);
199 if (! (cp
= *argp
++) || *cp
== '-') {
200 adios (NULL
, "missing argument to %s", argp
[-2]);
204 case CRLFLINEBREAKSSW
:
205 fx
.lf_line_endings
= 0;
207 case NCRLFLINEBREAKSSW
:
208 fx
.lf_line_endings
= 1;
211 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
212 adios (NULL
, "missing argument to %s", argp
[-2]);
232 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
233 adios (NULL
, "missing argument to %s", argp
[-2]);
235 if (! strncasecmp (cp
, "multipart/", 10) ||
236 ! strncasecmp (cp
, "message/", 8)) {
237 adios (NULL
, "-fixtype %s not allowed", cp
);
238 } else if (! strchr (cp
, '/')) {
239 adios (NULL
, "-fixtype requires type/subtype");
241 if (fx
.fixtypes
== NULL
) { fx
.fixtypes
= svector_create (10); }
242 svector_push_back (fx
.fixtypes
, cp
);
250 case REPLACETEXTPLAINSW
:
251 fx
.replacetextplain
= 1;
253 case NREPLACETEXTPLAINSW
:
254 fx
.replacetextplain
= 0;
257 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
258 adios (NULL
, "missing argument to %s", argp
[-2]);
260 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
263 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1])) {
264 adios (NULL
, "missing argument to %s", argp
[-2]);
266 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
269 if (!(rmmproc
= *argp
++) || *rmmproc
== '-') {
270 adios (NULL
, "missing argument to %s", argp
[-2]);
290 if (*cp
== '+' || *cp
== '@') {
292 adios (NULL
, "only one folder at a time!");
294 folder
= pluspath (cp
);
298 /* Interpret a full path as a filename, not a message. */
299 file
= add (cp
, NULL
);
301 app_msgarg (&msgs
, cp
);
306 SIGNAL (SIGQUIT
, quitser
);
307 SIGNAL (SIGPIPE
, pipeser
);
310 * Read the standard profile setup
312 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
313 readconfig ((struct node
**) 0, fp
, cp
, 0);
317 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
318 suppress_extraneous_trailing_semicolon_warning
= 1;
320 if (! context_find ("path")) {
321 free (path ("./", TFOLDER
));
324 if (file
&& msgs
.size
) {
325 adios (NULL
, "cannot specify msg and file at same time!");
329 * check if message is coming from file
332 /* If file is stdin, create a tmp file name before parse_mime()
333 has a chance, because it might put in on a different
334 filesystem than the output file. Instead, put it in the
335 user's preferred tmp directory. */
338 if (! strcmp ("-", file
)) {
344 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
345 adios (NULL
, "unable to create temporary file in %s",
349 file
= add (cp
, NULL
);
350 cpydata (STDIN_FILENO
, fd
, "-", file
);
354 (void) m_unlink (file
);
355 adios (NULL
, "failed to write temporary file");
359 if (! (cts
= (CT
*) mh_xcalloc ((size_t) 2, sizeof *cts
))) {
360 adios (NULL
, "out of memory");
364 if ((ct
= parse_mime (file
))) {
365 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
368 advise (NULL
, "unable to parse message from file %s", file
);
371 /* If there's an outfile, pass the input message unchanged, so the message won't
372 get dropped from a pipeline. */
374 /* Something went wrong. Output might be expected, such as if this were run
375 as a filter. Just copy the input to the output. */
376 if (copy_input_to_output (file
, outfile
) != OK
) {
377 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
383 * message(s) are coming from a folder
388 app_msgarg(&msgs
, "cur");
391 folder
= getfolder (1);
393 maildir
= m_maildir (folder
);
395 if (chdir (maildir
) == NOTOK
) {
396 adios (maildir
, "unable to change directory to");
399 /* read folder and create message structure */
400 if (! (mp
= folder_read (folder
, 1))) {
401 adios (NULL
, "unable to read folder %s", folder
);
404 /* check for empty folder */
405 if (mp
->nummsg
== 0) {
406 adios (NULL
, "no messages in %s", folder
);
409 /* parse all the message ranges/sequences and set SELECTED */
410 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
411 if (! m_convert (mp
, msgs
.msgs
[msgnum
])) {
414 seq_setprev (mp
); /* set the previous-sequence */
417 (CT
*) mh_xcalloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
418 adios (NULL
, "out of memory");
422 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
423 if (is_selected(mp
, msgnum
)) {
426 msgnam
= m_name (msgnum
);
427 if ((ct
= parse_mime (msgnam
))) {
428 set_text_ctparams(ct
, fx
.decodetypes
, fx
.lf_line_endings
);
431 advise (NULL
, "unable to parse message %s", msgnam
);
434 /* If there's an outfile, pass the input message unchanged, so the message won't
435 get dropped from a pipeline. */
437 /* Something went wrong. Output might be expected, such as if this were run
438 as a filter. Just copy the input to the output. */
439 const char *input_filename
= path (msgnam
, TFILE
);
441 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
442 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
450 seq_setcur (mp
, mp
->hghsel
); /* update current message */
452 seq_save (mp
); /* synchronize sequences */
453 context_replace (pfolder
, folder
);/* update current folder */
454 context_save (); /* save the context file */
458 for (ctp
= cts
; *ctp
; ++ctp
) {
459 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
462 (void) m_unlink (file
);
465 /* Just calling m_backup() unlinks the backup file. */
466 (void) m_backup (file
);
474 if (fx
.fixtypes
!= NULL
) { svector_free (fx
.fixtypes
); }
480 /* done is freects_done, which will clean up all of cts. */
487 * Apply transformations to one message.
490 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
491 /* Store input filename in case one of the transformations, i.e.,
492 fix_boundary(), rewrites to a tmp file. */
493 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
494 int modify_inplace
= 0;
495 int message_mods
= 0;
498 if (outfile
== NULL
) {
501 if ((*ctp
)->c_file
) {
503 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
504 adios (NULL
, "unable to create temporary file in %s",
507 outfile
= add (tempfile
, NULL
);
509 adios (NULL
, "missing both input and output filenames\n");
513 reverse_alternative_parts (*ctp
);
514 status
= fix_always (*ctp
, &message_mods
);
515 if (status
== OK
&& fx
->fixboundary
) {
516 status
= fix_boundary (ctp
, &message_mods
);
518 if (status
== OK
&& fx
->fixtypes
!= NULL
) {
519 status
= fix_types (*ctp
, fx
->fixtypes
, &message_mods
);
521 if (status
== OK
&& fx
->fixcte
) {
522 status
= fix_multipart_cte (*ctp
, &message_mods
);
524 if (status
== OK
&& fx
->reformat
) {
526 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
528 if (status
== OK
&& fx
->decodetext
) {
529 status
= decode_text_parts (*ctp
, fx
->decodetext
, fx
->decodetypes
, &message_mods
);
531 if (status
== OK
&& fx
->textcharset
!= NULL
) {
532 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
535 if (status
== OK
&& ! (*ctp
)->c_umask
) {
536 /* Set the umask for the contents file. This currently
537 isn't used but just in case it is in the future. */
540 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
541 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
543 (*ctp
)->c_umask
= ~m_gmprot();
548 * Write the content to a file
551 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
553 } else if (! modify_inplace
) {
554 /* Something went wrong. Output might be expected, such
555 as if this were run as a filter. Just copy the input
557 if (copy_input_to_output (input_filename
, outfile
) != OK
) {
558 advise (NULL
, "unable to copy message to %s, it might be lost\n", outfile
);
562 if (modify_inplace
) {
563 if (status
!= OK
) { (void) m_unlink (outfile
); }
568 free (input_filename
);
575 * Copy input message to output. Assumes not modifying in place, so this
576 * might be running as part of a pipeline.
579 copy_input_to_output (const char *input_filename
, const char *output_filename
) {
580 int in
= open (input_filename
, O_RDONLY
);
581 int out
= strcmp (output_filename
, "-")
582 ? open (output_filename
, O_WRONLY
| O_CREAT
, m_gmprot ())
586 if (in
!= -1 && out
!= -1) {
587 cpydata (in
, out
, input_filename
, output_filename
);
600 * Fix mismatched outer level boundary.
603 fix_boundary (CT
*ct
, int *message_mods
) {
604 struct multipart
*mp
;
607 if (ct
&& (*ct
)->c_type
== CT_MULTIPART
&& bogus_mp_content
) {
608 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
611 * 1) Get boundary at end of part.
612 * 2) Get boundary at beginning of part and compare to the end-of-part
614 * 3) Write out contents of ct to tmp file, replacing boundary in
615 * header with boundary from part. Set c_unlink to 1.
617 * 5) Call parse_mime() on the tmp file, replacing ct.
620 if (mp
&& mp
->mp_start
) {
623 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
626 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
627 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
628 char *filename
= add ((*ct
)->c_file
, NULL
);
632 if ((fixed_ct
= parse_mime (fixed
))) {
638 report (NULL
, NULL
, filename
,
639 "fix multipart boundary");
643 advise (NULL
, "unable to parse fixed part");
648 advise (NULL
, "unable to replace broken boundary");
652 advise (NULL
, "unable to create temporary file in %s",
657 free (part_boundary
);
659 /* Couldn't fix the boundary. Report failure so that mhfixmsg
660 doesn't modify the message. */
664 /* No multipart struct, even though the content type is
665 CT_MULTIPART. Report failure so that mhfixmsg doesn't modify
676 * Find boundary at end of multipart.
679 get_multipart_boundary (CT ct
, char **part_boundary
) {
681 char *end_boundary
= NULL
;
682 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
683 ? (off_t
) (ct
->c_end
- sizeof buffer
)
684 : (off_t
) ct
->c_begin
;
688 /* This will fail if the boundary spans fread() calls. BUFSIZ should
689 be big enough, even if it's just 1024, to make that unlikely. */
691 /* free_content() will close ct->c_fp. */
692 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
693 advise (ct
->c_file
, "unable to open for reading");
697 /* Get boundary at end of multipart. */
698 while (begin
>= (off_t
) ct
->c_begin
) {
699 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
700 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
701 char *cp
= rfind_str (buffer
, bytes_read
, "--");
706 /* Trim off trailing "--" and anything beyond. */
708 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
709 if (strlen (end
) > 3 && *end
++ == '\n' &&
710 *end
++ == '-' && *end
++ == '-') {
711 end_boundary
= add (end
, NULL
);
718 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
719 begin
-= sizeof buffer
;
725 /* Get boundary at beginning of multipart. */
727 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
728 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
729 if (bytes_read
>= strlen (end_boundary
)) {
730 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
732 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
733 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
738 /* The start and end boundaries didn't match, or the
739 start boundary doesn't begin with "\n--" (or "--"
740 if at the beginning of buffer). Keep trying. */
749 *part_boundary
= end_boundary
;
751 *part_boundary
= NULL
;
760 * Open and copy ct->c_file to file, replacing the multipart boundary.
763 replace_boundary (CT ct
, char *file
, char *boundary
) {
766 char buf
[BUFSIZ
], name
[NAMESZ
];
768 m_getfld_state_t gstate
= 0;
771 if (ct
->c_file
== NULL
) {
772 advise (NULL
, "missing input filename");
776 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
777 advise (ct
->c_file
, "unable to open for reading");
781 if ((fpout
= fopen (file
, "w")) == NULL
) {
783 advise (file
, "unable to open for writing");
787 for (compnum
= 1;;) {
788 int bufsz
= (int) sizeof buf
;
790 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
795 /* get copies of the buffers */
796 np
= add (name
, NULL
);
797 vp
= add (buf
, NULL
);
799 /* if necessary, get rest of field */
800 while (state
== FLDPLUS
) {
802 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
803 vp
= add (buf
, vp
); /* add to previous value */
806 if (strcasecmp (TYPE_FIELD
, np
)) {
807 fprintf (fpout
, "%s:%s", np
, vp
);
809 char *new_ctline
, *new_params
;
811 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
812 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
815 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
816 ct
->c_ctinfo
.ci_subtype
, NULL
);
817 new_params
= output_params(strlen(TYPE_FIELD
) +
818 strlen(new_ctline
) + 1,
819 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
820 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
821 new_params
? new_params
: "");
835 /* buf will have a terminating NULL, skip it. */
836 if ((int) fwrite (buf
, 1, bufsz
-1, fpout
) < bufsz
-1) {
837 advise (file
, "fwrite");
846 advise (NULL
, "message format error in component #%d", compnum
);
851 advise (NULL
, "getfld() returned %d", state
);
859 m_getfld_state_destroy (&gstate
);
868 * Fix Content-Type header to reflect the content of its part.
871 fix_types (CT ct
, svector_t fixtypes
, int *message_mods
) {
874 switch (ct
->c_type
) {
876 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
879 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
880 status
= fix_types (part
->mp_part
, fixtypes
, message_mods
);
886 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
887 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
889 status
= fix_types (e
->eb_content
, fixtypes
, message_mods
);
896 if (ct
->c_ctinfo
.ci_type
&& ct
->c_ctinfo
.ci_subtype
) {
897 for (typep
= svector_strs (fixtypes
);
898 typep
&& (type
= *typep
);
901 concat (ct
->c_ctinfo
.ci_type
, "/", ct
->c_ctinfo
.ci_subtype
,
904 if (! strcasecmp (type
, type_subtype
) &&
905 decode_part (ct
) == OK
&&
906 ct
->c_cefile
.ce_file
!= NULL
) {
907 char *ct_type_subtype
= mime_type (ct
->c_cefile
.ce_file
);
910 if ((cp
= strchr (ct_type_subtype
, ';'))) {
911 /* Truncate to remove any parameter list from
912 mime_type () result. */
916 if (strcasecmp (type
, ct_type_subtype
)) {
917 char *ct_type
, *ct_subtype
;
920 /* The Content-Type header does not match the
921 content, so update these struct Content
924 * c_ctinfo.ci_type, c_ctinfo.ci_subtype
927 /* Extract type and subtype from type/subtype. */
928 ct_type
= getcpy (ct_type_subtype
);
929 if ((cp
= strchr (ct_type
, '/'))) {
931 ct_subtype
= getcpy (++cp
);
933 advise (NULL
, "missing / in MIME type of %s %s",
934 ct
->c_file
, ct
->c_partno
);
939 ct
->c_type
= ct_str_type (ct_type
);
940 ct
->c_subtype
= ct_str_subtype (ct
->c_type
, ct_subtype
);
942 free (ct
->c_ctinfo
.ci_type
);
943 ct
->c_ctinfo
.ci_type
= ct_type
;
944 free (ct
->c_ctinfo
.ci_subtype
);
945 ct
->c_ctinfo
.ci_subtype
= ct_subtype
;
946 if (! replace_substring (&ct
->c_ctline
, type
,
948 advise (NULL
, "did not find %s in %s",
952 /* Update Content-Type header field. */
953 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
954 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
955 if (replace_substring (&hf
->value
, type
,
959 report (NULL
, ct
->c_partno
, ct
->c_file
,
960 "change Content-Type in header "
962 type
, ct_type_subtype
);
966 advise (NULL
, "did not find %s in %s",
972 free (ct_type_subtype
);
984 * Replace a substring, allocating space to hold the new one.
987 replace_substring (char **str
, const char *old
, const char *new) {
990 if ((cp
= strstr (*str
, old
))) {
991 char *remainder
= cp
+ strlen (old
);
992 char *prefix
, *new_str
;
995 prefix
= getcpy (*str
);
996 *(prefix
+ (cp
- *str
)) = '\0';
997 new_str
= concat (prefix
, new, remainder
, NULL
);
1000 new_str
= concat (new, remainder
, NULL
);
1005 return *str
= new_str
;
1013 * Remove a name=value parameter, given just its name, from a header value.
1016 remove_parameter (char *str
, const char *name
) {
1017 /* It looks to me, based on the BNF in RFC 2045, than there can't
1018 be whitespace betwwen the parameter name and the "=", or
1019 between the "=" and the parameter value. */
1020 char *param_name
= concat (name
, "=", NULL
);
1023 if ((cp
= strstr (str
, param_name
))) {
1027 /* Remove any leading spaces, before the parameter name. */
1029 start
> str
&& isspace ((unsigned char) *(start
-1));
1033 /* Remove a leading semicolon. */
1034 if (start
> str
&& *(start
-1) == ';') { --start
; }
1036 end
= cp
+ strlen (name
) + 1;
1038 /* Skip past the quoted value, and then the final quote. */
1039 for (++end
; *end
&& *end
!= '"'; ++end
) { continue; }
1042 /* Skip past the value. */
1043 for (++end
; *end
&& ! isspace ((unsigned char) *end
); ++end
) {}
1046 /* Count how many characters need to be moved. Include
1047 trailing null, which is accounted for by the
1048 initialization of count to 1. */
1049 for (cp
= end
; *cp
; ++cp
) { ++count
; }
1050 (void) memmove (start
, end
, count
);
1060 * Fix Content-Transfer-Encoding.
1063 fix_multipart_cte (CT ct
, int *message_mods
) {
1066 if (ct
->c_type
== CT_MULTIPART
) {
1067 struct multipart
*m
;
1070 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
1071 ct
->c_encoding
!= CE_BINARY
) {
1074 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1075 char *name
= hf
->name
;
1076 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
1080 if (! strncasecmp (name
, ENCODING_FIELD
,
1081 strlen (ENCODING_FIELD
))) {
1082 char *prefix
= "Nmh-REPLACED-INVALID-";
1083 HF h
= mh_xmalloc (sizeof *h
);
1085 h
->name
= add (hf
->name
, NULL
);
1086 h
->hf_encoding
= hf
->hf_encoding
;
1090 /* Retain old header but prefix its name. */
1092 hf
->name
= concat (prefix
, h
->name
, NULL
);
1096 char *encoding
= cpytrim (hf
->value
);
1097 report (NULL
, ct
->c_partno
, ct
->c_file
,
1098 "replace Content-Transfer-Encoding of %s "
1099 "with 8 bit", encoding
);
1103 h
->value
= add (" 8bit\n", NULL
);
1105 /* Don't need to warn for multiple C-T-E header
1106 fields, parse_mime() already does that. But
1107 if there are any, fix them all as necessary. */
1112 set_ce (ct
, CE_8BIT
);
1115 m
= (struct multipart
*) ct
->c_ctparams
;
1116 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
1117 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
1129 * Set content encoding.
1132 set_ce (CT ct
, int encoding
) {
1133 const char *ce
= ce_str (encoding
);
1134 const struct str2init
*ctinit
= get_ce_method (ce
);
1137 char *cte
= concat (" ", ce
, "\n", NULL
);
1140 /* Decoded contents might be in ct->c_cefile.ce_file, if the
1141 caller is decode_text_parts (). Save because we'll
1143 struct cefile decoded_content_info
= ct
->c_cefile
;
1145 ct
->c_encoding
= encoding
;
1147 ct
->c_ctinitfnx
= ctinit
->si_init
;
1148 /* This will assign ct->c_cefile with an all-0 struct, which
1150 (*ctinit
->si_init
) (ct
);
1151 /* After returning, the caller should set
1152 ct->c_cefile.ce_file to the name of the file containing
1155 /* Restore the cefile. */
1156 ct
->c_cefile
= decoded_content_info
;
1158 /* Update/add Content-Transfer-Encoding header field. */
1159 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1160 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
1167 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
1170 /* Update c_celine. It's used only by mhlist -debug. */
1171 free (ct
->c_celine
);
1172 ct
->c_celine
= add (cte
, NULL
);
1182 * Make sure each text part has a corresponding text/plain part.
1185 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
1188 switch ((*ct
)->c_type
) {
1190 /* Nothing to do for text/plain. */
1191 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
1193 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1194 parent
->c_subtype
== MULTI_ALTERNATE
) {
1195 int new_subpart_number
= 1;
1196 int has_text_plain
=
1197 find_textplain_sibling (parent
, replacetextplain
,
1198 &new_subpart_number
);
1200 if (! has_text_plain
) {
1201 /* Parent is a multipart/alternative. Insert a new
1202 text/plain subpart. */
1203 const int inserted
=
1204 insert_new_text_plain_part (*ct
, new_subpart_number
,
1209 report (NULL
, parent
->c_partno
, parent
->c_file
,
1210 "insert text/plain part");
1216 } else if (parent
&& parent
->c_type
== CT_MULTIPART
&&
1217 parent
->c_subtype
== MULTI_RELATED
) {
1218 char *type_subtype
=
1219 concat ((*ct
)->c_ctinfo
.ci_type
, "/",
1220 (*ct
)->c_ctinfo
.ci_subtype
, NULL
);
1221 const char *parent_type
=
1222 get_param (parent
->c_ctinfo
.ci_first_pm
, "type", '?', 1);
1223 int new_subpart_number
= 1;
1224 int has_text_plain
= 0;
1226 /* Have to do string comparison on the subtype because we
1227 don't enumerate all of them in c_subtype values.
1228 parent_type will be NULL if the multipart/related part
1229 doesn't have a type parameter. The type parameter must
1230 be specified according to RFC 2387 Sec. 3.1 but not all
1232 if (parent_type
&& strcasecmp (type_subtype
, parent_type
) == 0) {
1233 /* The type of this part matches the root type of the
1234 parent multipart/related. Look to see if there's
1235 text/plain sibling. */
1237 find_textplain_sibling (parent
, replacetextplain
,
1238 &new_subpart_number
);
1241 free (type_subtype
);
1243 if (! has_text_plain
) {
1244 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1248 for (part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1249 if (*ct
!= part
->mp_part
) {
1255 /* Parent is a multipart/related. Insert a new
1256 text/plain subpart in a new multipart/alternative. */
1257 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1258 /* Not an error if text/plain couldn't be added. */
1261 /* There are no siblings, so insert a new text/plain
1262 subpart, and change the parent type from
1263 multipart/related to multipart/alternative. */
1264 const int inserted
=
1265 insert_new_text_plain_part (*ct
, new_subpart_number
,
1271 parent
->c_subtype
= MULTI_ALTERNATE
;
1272 parent
->c_ctinfo
.ci_subtype
= getcpy ("alternative");
1273 if (! replace_substring (&parent
->c_ctline
, "/related",
1276 "did not find multipart/related in %s",
1280 /* Update Content-Type header field. */
1281 for (hf
= parent
->c_first_hf
; hf
; hf
= hf
->next
) {
1282 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1283 if (replace_substring (&hf
->value
, "/related",
1287 report (NULL
, parent
->c_partno
,
1289 "insert text/plain part");
1292 /* Remove, e.g., type="text/html" from
1293 multipart/alternative. */
1294 remove_parameter (hf
->value
, "type");
1297 advise (NULL
, "did not find multipart/"
1298 "related in header %s",
1304 /* Not an error if text/plain couldn't be inserted. */
1309 if (insert_into_new_mp_alt (ct
, message_mods
)) {
1316 case CT_MULTIPART
: {
1317 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
1320 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1321 if ((*ct
)->c_type
== CT_MULTIPART
) {
1322 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
1330 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
1331 struct exbody
*e
= (struct exbody
*) (*ct
)->c_ctparams
;
1333 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
1344 * See if there is a sibling text/plain, and return its subpart number.
1347 find_textplain_sibling (CT parent
, int replacetextplain
,
1348 int *new_subpart_number
) {
1349 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1350 struct part
*part
, *prev
;
1351 int has_text_plain
= 0;
1353 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
1354 ++*new_subpart_number
;
1355 if (part
->mp_part
->c_type
== CT_TEXT
&&
1356 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
1357 if (replacetextplain
) {
1358 struct part
*old_part
;
1359 if (part
== mp
->mp_parts
) {
1360 old_part
= mp
->mp_parts
;
1361 mp
->mp_parts
= part
->mp_next
;
1363 old_part
= prev
->mp_next
;
1364 prev
->mp_next
= part
->mp_next
;
1367 report (NULL
, parent
->c_partno
, parent
->c_file
,
1368 "remove text/plain part %s",
1369 old_part
->mp_part
->c_partno
);
1371 free_content (old_part
->mp_part
);
1381 return has_text_plain
;
1386 * Insert a new text/plain part.
1389 insert_new_text_plain_part (CT ct
, int new_subpart_number
, CT parent
) {
1390 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
1391 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
1393 if ((new_part
->mp_part
= build_text_plain_part (ct
))) {
1395 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
1397 new_part
->mp_next
= mp
->mp_parts
;
1398 mp
->mp_parts
= new_part
;
1399 new_part
->mp_part
->c_partno
=
1400 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
1405 free_content (new_part
->mp_part
);
1414 * Create a text/plain part to go along with non-plain sibling part.
1417 build_text_plain_part (CT encoded_part
) {
1418 CT tp_part
= divide_part (encoded_part
);
1419 char *tmp_plain_file
= NULL
;
1421 if (decode_part (tp_part
) == OK
) {
1422 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
1423 contains the decoded contents. And the decoding function, such
1424 as openQuoted, will have set ...->ce_unlink to 1 so that it will
1425 be unlinked by free_content (). */
1428 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1429 advise (NULL
, "unable to create temporary file in %s",
1432 tmp_plain_file
= add (tempfile
, NULL
);
1433 if (reformat_part (tp_part
, tmp_plain_file
,
1434 tp_part
->c_ctinfo
.ci_type
,
1435 tp_part
->c_ctinfo
.ci_subtype
,
1436 tp_part
->c_type
) == OK
) {
1442 free_content (tp_part
);
1443 if (tmp_plain_file
) { (void) m_unlink (tmp_plain_file
); }
1444 free (tmp_plain_file
);
1451 * Slip new text/plain part into a new multipart/alternative.
1454 insert_into_new_mp_alt (CT
*ct
, int *message_mods
) {
1455 CT tp_part
= build_text_plain_part (*ct
);
1459 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
1462 struct multipart
*mp
= (struct multipart
*) mp_alt
->c_ctparams
;
1464 if (mp
&& mp
->mp_parts
) {
1465 mp
->mp_parts
->mp_part
= tp_part
;
1466 /* Make the new multipart/alternative the parent. */
1471 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
1472 "insert text/plain part");
1475 free_content (tp_part
);
1476 free_content (mp_alt
);
1483 /* Not an error if text/plain couldn't be built. */
1491 * Clone a MIME part.
1494 divide_part (CT ct
) {
1497 if ((new_part
= (CT
) mh_xcalloc (1, sizeof *new_part
)) == NULL
)
1498 adios (NULL
, "out of memory");
1500 /* Just copy over what is needed for decoding. c_vrsn and
1501 c_celine aren't necessary. */
1502 new_part
->c_file
= add (ct
->c_file
, NULL
);
1503 new_part
->c_begin
= ct
->c_begin
;
1504 new_part
->c_end
= ct
->c_end
;
1505 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1506 new_part
->c_type
= ct
->c_type
;
1507 new_part
->c_cefile
= ct
->c_cefile
;
1508 new_part
->c_encoding
= ct
->c_encoding
;
1509 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1510 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1511 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1512 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1514 /* c_ctline is used by reformat__part(), so it can preserve
1515 anything after the type/subtype. */
1516 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1523 * Copy the content info from one part to another.
1526 copy_ctinfo (CI dest
, CI src
) {
1529 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1530 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1532 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1533 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1535 if (s_pm
->pm_charset
) {
1536 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1538 if (s_pm
->pm_lang
) {
1539 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1543 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1544 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1552 decode_part (CT ct
) {
1557 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1558 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1560 tmp_decoded
= add (tempfile
, NULL
);
1561 /* The following call will load ct->c_cefile.ce_file with the tmp
1562 filename of the decoded content. tmp_decoded will contain the
1563 encoded output, get rid of that. */
1564 status
= output_message (ct
, tmp_decoded
);
1565 (void) m_unlink (tmp_decoded
);
1573 * Reformat content as plain text.
1574 * Some of the arguments aren't really needed now, but maybe will
1575 * be in the future for other than text types.
1578 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1579 int output_subtype
, output_encoding
;
1583 /* Hacky: this redirects the output from whatever command is used
1584 to show the part to a file. So, the user can't have any output
1585 redirection in that command.
1586 Could show_multi() in mhshowsbr.c avoid this? */
1588 /* Check for invo_name-format-type/subtype. */
1589 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1591 advise (NULL
, "Don't know how to convert %s, there is no "
1592 "%s-format-%s/%s profile entry",
1593 ct
->c_file
, invo_name
, type
, subtype
);
1597 if (strchr (cf
, '>')) {
1598 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1599 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1600 subtype
? subtype
: "");
1606 cp
= concat (cf
, " >", file
, NULL
);
1607 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1610 /* Unlink decoded content tmp file and free its filename to avoid
1611 leaks. The file stream should already have been closed. */
1612 if (ct
->c_cefile
.ce_unlink
) {
1613 (void) m_unlink (ct
->c_cefile
.ce_file
);
1614 free (ct
->c_cefile
.ce_file
);
1615 ct
->c_cefile
.ce_file
= NULL
;
1616 ct
->c_cefile
.ce_unlink
= 0;
1619 if (c_type
== CT_TEXT
) {
1620 output_subtype
= TEXT_PLAIN
;
1622 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1625 output_encoding
= charset_encoding (ct
);
1627 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1628 ct
->c_cefile
.ce_file
= file
;
1629 ct
->c_cefile
.ce_unlink
= 1;
1631 ct
->c_cefile
.ce_unlink
= 0;
1640 * Identifies 7bit or 8bit content based on charset.
1643 charset_encoding (CT ct
) {
1644 char *ct_charset
= content_charset (ct
);
1645 int encoding
= strcasecmp (ct_charset
, "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1654 * Fill in a multipart/alternative part.
1657 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1658 char *boundary_prefix
= "----=_nmh-multipart";
1659 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1660 char *boundary_indicator
= "; boundary=";
1661 char *typename
, *subtypename
, *name
;
1664 struct multipart
*m
;
1665 const struct str2init
*ctinit
;
1667 if ((ct
= (CT
) mh_xcalloc (1, sizeof *ct
)) == NULL
)
1668 adios (NULL
, "out of memory");
1670 /* Set up the multipart/alternative part. These fields of *ct were
1671 initialized to 0 by mh_xcalloc():
1672 c_fp, c_unlink, c_begin, c_end,
1673 c_vrsn, c_ctline, c_celine,
1674 c_id, c_descr, c_dispo, c_partno,
1675 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1676 c_cefile, c_encoding,
1677 c_digested, c_digest[16], c_ctexbody,
1678 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1680 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1683 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1685 ct
->c_subtype
= subtype
;
1687 ctinit
= get_ct_init (ct
->c_type
);
1689 typename
= ct_type_str (type
);
1690 subtypename
= ct_subtype_str (type
, subtype
);
1694 int found_boundary
= 1;
1696 while (found_boundary
&& serial
< 1000000) {
1699 /* Ensure that the boundary doesn't appear in the decoded
1701 if (new_part
->c_cefile
.ce_file
) {
1702 if ((found_boundary
=
1703 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1704 new_part
->c_cefile
.ce_file
,
1711 /* Ensure that the boundary doesn't appear in the encoded
1713 if (! found_boundary
&& new_part
->c_file
) {
1714 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1722 if (found_boundary
) {
1723 /* Try a slightly different boundary. */
1728 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1730 concat (boundary_prefix
,
1731 first_alt
->c_partno
? first_alt
->c_partno
: "",
1732 "-", buffer2
, NULL
);
1736 if (found_boundary
) {
1737 advise (NULL
, "giving up trying to find a unique boundary");
1743 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1744 boundary
, "\"", NULL
);
1746 /* Load c_first_hf and c_last_hf. */
1747 transfer_noncontent_headers (first_alt
, ct
);
1748 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1751 /* Load c_partno. */
1752 if (first_alt
->c_partno
) {
1753 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1754 free (first_alt
->c_partno
);
1755 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1756 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1758 first_alt
->c_partno
= add ("1", NULL
);
1759 new_part
->c_partno
= add ("2", NULL
);
1763 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1764 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1767 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1768 "boundary", boundary
, 0);
1770 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1771 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1772 p
->mp_next
->mp_next
= NULL
;
1773 p
->mp_next
->mp_part
= first_alt
;
1775 if ((m
= (struct multipart
*) mh_xcalloc (1, sizeof (struct multipart
))) ==
1777 adios (NULL
, "out of memory");
1778 m
->mp_start
= concat (boundary
, "\n", NULL
);
1779 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1790 * Check that the boundary does not appear in the content.
1793 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1794 char buffer
[BUFSIZ
];
1796 int found_boundary
= 0;
1798 /* free_content() will close *fp if we fopen it here. */
1799 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1800 advise (file
, "unable to open %s for reading", file
);
1804 fseeko (*fp
, 0L, SEEK_SET
);
1805 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1806 if (find_str (buffer
, bytes_read
, boundary
)) {
1812 return found_boundary
;
1817 * Remove all non-Content headers.
1820 transfer_noncontent_headers (CT old
, CT
new) {
1823 hp_prev
= hp
= old
->c_first_hf
;
1827 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1828 if (hp
== old
->c_last_hf
) {
1829 if (hp
== old
->c_first_hf
) {
1830 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1832 hp_prev
->next
= NULL
;
1833 old
->c_last_hf
= hp_prev
;
1836 if (hp
== old
->c_first_hf
) {
1837 old
->c_first_hf
= next
;
1839 hp_prev
->next
= next
;
1843 /* Put node hp in the new CT. */
1844 if (new->c_first_hf
== NULL
) {
1845 new->c_first_hf
= hp
;
1847 new->c_last_hf
->next
= hp
;
1849 new->c_last_hf
= hp
;
1851 /* A Content- header, leave in old. */
1864 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1865 char *typename
= ct_type_str (type
);
1866 char *subtypename
= ct_subtype_str (type
, subtype
);
1867 /* E.g, " text/plain" */
1868 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1869 /* E.g, " text/plain\n" */
1870 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1871 int found_content_type
= 0;
1873 const char *cp
= NULL
;
1877 /* Update/add Content-Type header field. */
1878 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1879 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1880 found_content_type
= 1;
1882 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1883 ? concat (type_subtypename
, cp
, "\n", NULL
)
1884 : add (name_plus_nl
, NULL
);
1887 if (! found_content_type
) {
1888 add_header (ct
, add (TYPE_FIELD
, NULL
),
1889 (cp
= strchr (ct
->c_ctline
, ';'))
1890 ? concat (type_subtypename
, cp
, "\n", NULL
)
1891 : add (name_plus_nl
, NULL
));
1894 /* Some of these might not be used, but set them anyway. */
1896 ? concat (type_subtypename
, cp
, NULL
)
1897 : concat (type_subtypename
, NULL
);
1898 free (ct
->c_ctline
);
1899 ct
->c_ctline
= ctline
;
1900 /* Leave other ctinfo members as they were. */
1901 free (ct
->c_ctinfo
.ci_type
);
1902 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1903 free (ct
->c_ctinfo
.ci_subtype
);
1904 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1906 ct
->c_subtype
= subtype
;
1908 free (name_plus_nl
);
1909 free (type_subtypename
);
1911 status
= set_ce (ct
, encoding
);
1918 * It's not necessary to update the charset parameter of a Content-Type
1919 * header for a text part. According to RFC 2045 Sec. 6.4, the body
1920 * (content) was originally in the specified charset, "and will be in
1921 * that character set again after decoding."
1924 decode_text_parts (CT ct
, int encoding
, const char *decodetypes
, int *message_mods
) {
1926 int lf_line_endings
= 0;
1928 switch (ct
->c_type
) {
1929 case CT_MULTIPART
: {
1930 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1933 /* Should check to see if the body for this part is encoded?
1934 For now, it gets passed along as-is by InitMultiPart(). */
1935 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1936 status
= decode_text_parts (part
->mp_part
, encoding
, decodetypes
, message_mods
);
1942 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1943 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
1945 status
= decode_text_parts (e
->eb_content
, encoding
, decodetypes
, message_mods
);
1950 if (! should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
1955 ct
->c_ctparams
&& ((struct text
*) ct
->c_ctparams
)->lf_line_endings
;
1957 switch (ct
->c_encoding
) {
1962 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1963 const char *reason
= NULL
;
1965 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1966 && encoding
!= CE_BINARY
) {
1967 /* The decoding isn't acceptable so discard it.
1968 Leave status as OK to allow other transformations. */
1970 report (NULL
, ct
->c_partno
, ct
->c_file
,
1971 "will not decode%s because it is binary (%s)",
1973 : ct
->c_ctline
? ct
->c_ctline
1977 (void) m_unlink (ct
->c_cefile
.ce_file
);
1978 free (ct
->c_cefile
.ce_file
);
1979 ct
->c_cefile
.ce_file
= NULL
;
1980 } else if (ct
->c_encoding
== CE_QUOTED
&&
1981 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1982 /* The decoding isn't acceptable so discard it.
1983 Leave status as OK to allow other transformations. */
1985 report (NULL
, ct
->c_partno
, ct
->c_file
,
1986 "will not decode%s because it is 8bit",
1988 : ct
->c_ctline
? ct
->c_ctline
1991 (void) m_unlink (ct
->c_cefile
.ce_file
);
1992 free (ct
->c_cefile
.ce_file
);
1993 ct
->c_cefile
.ce_file
= NULL
;
1996 if (ct_encoding
== CE_BINARY
) {
1998 } else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
2001 enc
= charset_encoding (ct
);
2003 if (set_ce (ct
, enc
) == OK
) {
2006 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
2007 ct
->c_ctline
? ct
->c_ctline
: "");
2009 if (lf_line_endings
) {
2010 strip_crs (ct
, message_mods
);
2023 if (lf_line_endings
) {
2024 strip_crs (ct
, message_mods
);
2039 * Determine if the part with type[/subtype] should be decoded, according to
2040 * decodetypes (which came from the -decodetypes switch).
2043 should_decode(const char *decodetypes
, const char *type
, const char *subtype
) {
2044 /* Quick search for matching type[/subtype] in decodetypes: bracket
2045 decodetypes with commas, then search for ,type, and ,type/subtype, in
2048 int found_match
= 0;
2049 char *delimited_decodetypes
= concat(",", decodetypes
, ",", NULL
);
2050 char *delimited_type
= concat(",", type
, ",", NULL
);
2052 if (nmh_strcasestr(delimited_decodetypes
, delimited_type
)) {
2054 } else if (subtype
!= NULL
) {
2055 char *delimited_type_subtype
=
2056 concat(",", type
, "/", subtype
, ",", NULL
);
2058 if (nmh_strcasestr(delimited_decodetypes
, delimited_type_subtype
)) {
2061 free(delimited_type_subtype
);
2064 free(delimited_type
);
2065 free(delimited_decodetypes
);
2072 * See if the decoded content is 7bit, 8bit, or binary. It's binary
2073 * if it has any NUL characters, a CR not followed by a LF, or lines
2074 * greater than 998 characters in length. If binary, reason is set
2075 * to a string explaining why.
2078 content_encoding (CT ct
, const char **reason
) {
2079 CE ce
= &ct
->c_cefile
;
2080 int encoding
= CE_7BIT
;
2083 size_t line_len
= 0;
2084 char buffer
[BUFSIZ
];
2087 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
2088 advise (ce
->ce_file
, "unable to open for reading");
2092 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
2093 while (encoding
!= CE_BINARY
&&
2094 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
2097 int last_char_was_cr
= 0;
2099 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
2100 if (*cp
== '\0' || ++line_len
> 998 ||
2101 (*cp
!= '\n' && last_char_was_cr
)) {
2102 encoding
= CE_BINARY
;
2104 *reason
= "null character";
2105 } else if (line_len
> 998) {
2106 *reason
= "line length > 998";
2107 } else if (*cp
!= '\n' && last_char_was_cr
) {
2108 *reason
= "CR not followed by LF";
2110 /* Should not reach this. */
2114 } else if (*cp
== '\n') {
2116 } else if (! isascii ((unsigned char) *cp
)) {
2120 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2126 } /* else should never happen */
2133 * Strip carriage returns from content.
2136 strip_crs (CT ct
, int *message_mods
) {
2137 char *charset
= content_charset (ct
);
2140 /* Only strip carriage returns if content is ASCII or another
2141 charset that has the same readily recognizable CR followed by a
2142 LF. We can include UTF-8 here because if the high-order bit of
2143 a UTF-8 byte is 0, then it must be a single-byte ASCII
2145 if (! strcasecmp (charset
, "US-ASCII") ||
2146 ! strcasecmp (charset
, "UTF-8") ||
2147 ! strncasecmp (charset
, "ISO-8859-", 9) ||
2148 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
2154 int opened_input_file
= 0;
2156 if (ct
->c_cefile
.ce_file
) {
2157 file
= &ct
->c_cefile
.ce_file
;
2158 fp
= &ct
->c_cefile
.ce_fp
;
2160 } else if (ct
->c_file
) {
2163 begin
= (size_t) ct
->c_begin
;
2164 end
= (size_t) ct
->c_end
;
2165 } /* else don't know where the content is */
2167 if (file
&& *file
&& fp
) {
2169 if ((*fp
= fopen (*file
, "r")) == NULL
) {
2170 advise (*file
, "unable to open for reading");
2173 opened_input_file
= 1;
2179 char buffer
[BUFSIZ
];
2181 size_t bytes_to_read
=
2182 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
2184 fseeko (*fp
, begin
, SEEK_SET
);
2185 while ((bytes_read
= fread (buffer
, 1,
2186 min (bytes_to_read
, sizeof buffer
),
2188 /* Look for CR followed by a LF. This is supposed to
2189 be text so there should be LF's. If not, don't
2190 modify the content. */
2193 int last_char_was_cr
= 0;
2195 if (end
> 0) { bytes_to_read
-= bytes_read
; }
2197 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2198 if (*cp
== '\n' && last_char_was_cr
) {
2203 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
2209 char *stripped_content_file
;
2210 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
2212 if (tempfile
== NULL
) {
2213 adios (NULL
, "unable to create temporary file in %s",
2216 stripped_content_file
= add (tempfile
, NULL
);
2218 /* Strip each CR before a LF from the content. */
2219 fseeko (*fp
, begin
, SEEK_SET
);
2220 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
2224 int last_char_was_cr
= 0;
2226 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
2228 last_char_was_cr
= 1;
2229 } else if (last_char_was_cr
) {
2231 if (write (fd
, "\r", 1) < 0) {
2232 advise (tempfile
, "CR write");
2235 if (write (fd
, cp
, 1) < 0) {
2236 advise (tempfile
, "write");
2238 last_char_was_cr
= 0;
2240 if (write (fd
, cp
, 1) < 0) {
2241 advise (tempfile
, "write");
2243 last_char_was_cr
= 0;
2249 admonish (NULL
, "unable to write temporary file %s",
2250 stripped_content_file
);
2251 (void) m_unlink (stripped_content_file
);
2254 /* Replace the decoded file with the converted one. */
2255 if (ct
->c_cefile
.ce_file
) {
2256 if (ct
->c_cefile
.ce_unlink
) {
2257 (void) m_unlink (ct
->c_cefile
.ce_file
);
2259 free (ct
->c_cefile
.ce_file
);
2261 ct
->c_cefile
.ce_file
= stripped_content_file
;
2262 ct
->c_cefile
.ce_unlink
= 1;
2266 report (NULL
, ct
->c_partno
,
2267 begin
== 0 && end
== 0 ? "" : *file
,
2273 if (opened_input_file
) {
2287 * Convert character set of each part.
2290 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
2293 switch (ct
->c_type
) {
2295 if (ct
->c_subtype
== TEXT_PLAIN
) {
2296 status
= convert_charset (ct
, dest_charset
, message_mods
);
2299 char *ct_charset
= content_charset (ct
);
2301 report (NULL
, ct
->c_partno
, ct
->c_file
,
2302 "convert %s to %s", ct_charset
, dest_charset
);
2306 char *ct_charset
= content_charset (ct
);
2308 report ("iconv", ct
->c_partno
, ct
->c_file
,
2309 "failed to convert %s to %s", ct_charset
, dest_charset
);
2315 case CT_MULTIPART
: {
2316 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2319 /* Should check to see if the body for this part is encoded?
2320 For now, it gets passed along as-is by InitMultiPart(). */
2321 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2323 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
2329 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2330 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2333 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
2346 * Fix various problems that aren't handled elsewhere. These
2347 * are fixed unconditionally: there are no switches to disable
2348 * them. Currently, "problems" are these:
2349 * 1) remove extraneous semicolon at the end of a header parameter list
2350 * 2) replace RFC 2047 encoding with RFC 2231 encoding of name and
2351 * filename parameters in Content-Type and Content-Disposition
2352 * headers, respectively.
2355 fix_always (CT ct
, int *message_mods
) {
2358 switch (ct
->c_type
) {
2359 case CT_MULTIPART
: {
2360 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2363 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
2364 status
= fix_always (part
->mp_part
, message_mods
);
2370 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2371 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2373 status
= fix_always (e
->eb_content
, message_mods
);
2380 if (ct
->c_first_hf
) {
2381 fix_filename_encoding (ct
);
2384 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
2385 size_t len
= strlen (hf
->value
);
2387 if (strcasecmp (hf
->name
, TYPE_FIELD
) != 0 &&
2388 strcasecmp (hf
->name
, DISPO_FIELD
) != 0) {
2389 /* Only do this for Content-Type and
2390 Content-Disposition fields because those are the
2391 only headers that parse_mime() warns about. */
2395 /* whitespace following a trailing ';' will be nuked as well */
2396 if (hf
->value
[len
- 1] == '\n') {
2397 while (isspace((unsigned char)(hf
->value
[len
- 2]))) {
2398 if (len
-- == 0) { break; }
2402 if (hf
->value
[len
- 2] == ';') {
2403 /* Remove trailing ';' from parameter value. */
2404 hf
->value
[len
- 2] = '\n';
2405 hf
->value
[len
- 1] = '\0';
2407 /* Also, if Content-Type parameter, remove trailing ';'
2408 from ct->c_ctline. This probably isn't necessary
2410 if (strcasecmp(hf
->name
, TYPE_FIELD
) == 0 && ct
->c_ctline
) {
2411 size_t l
= strlen(ct
->c_ctline
) - 1;
2412 while (isspace((unsigned char)(ct
->c_ctline
[l
])) ||
2413 ct
->c_ctline
[l
] == ';') {
2414 ct
->c_ctline
[l
--] = '\0';
2415 if (l
== 0) { break; }
2421 report (NULL
, ct
->c_partno
, ct
->c_file
,
2422 "remove trailing ; from %s parameter value",
2434 * Factor out common code for loops in fix_filename_encoding().
2437 fix_filename_param (char *name
, char *value
, PM
*first_pm
, PM
*last_pm
) {
2441 if (((value_len
= strlen (value
)) > 0) &&
2442 strncmp (value
, "=?", 2) == 0 &&
2443 strncmp (&value
[value_len
- 2], "?=", 2) == 0) {
2444 /* Looks like an RFC 2047 encoded parameter. */
2445 char decoded
[PATH_MAX
+ 1];
2447 if (decode_rfc2047 (value
, decoded
, sizeof decoded
)) {
2448 /* Encode using RFC 2231. */
2449 replace_param (first_pm
, last_pm
, name
, decoded
, 0);
2452 advise (NULL
, "failed to decode %s parameter %s", name
, value
);
2461 * Replace RFC 2047 encoding with RFC 2231 encoding of name and
2462 * filename parameters in Content-Type and Content-Disposition
2463 * headers, respectively.
2466 fix_filename_encoding (CT ct
) {
2471 for (pm
= ct
->c_ctinfo
.ci_first_pm
; pm
; pm
= pm
->pm_next
) {
2472 if (pm
->pm_name
&& pm
->pm_value
&&
2473 strcasecmp (pm
->pm_name
, "name") == 0) {
2474 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2475 &ct
->c_ctinfo
.ci_first_pm
,
2476 &ct
->c_ctinfo
.ci_last_pm
);
2480 for (pm
= ct
->c_dispo_first
; pm
; pm
= pm
->pm_next
) {
2481 if (pm
->pm_name
&& pm
->pm_value
&&
2482 strcasecmp (pm
->pm_name
, "filename") == 0) {
2483 fixed
= fix_filename_param (pm
->pm_name
, pm
->pm_value
,
2489 /* Fix hf values to correspond. */
2490 for (hf
= ct
->c_first_hf
; fixed
&& hf
; hf
= hf
->next
) {
2491 enum { OTHER
, TYPE_HEADER
, DISPO_HEADER
} field
= OTHER
;
2493 if (strcasecmp (hf
->name
, TYPE_FIELD
) == 0) {
2494 field
= TYPE_HEADER
;
2495 } else if (strcasecmp (hf
->name
, DISPO_FIELD
) == 0) {
2496 field
= DISPO_HEADER
;
2499 if (field
!= OTHER
) {
2500 const char *const semicolon_loc
= strchr (hf
->value
, ';');
2502 if (semicolon_loc
) {
2504 strlen (hf
->name
) + 1 + semicolon_loc
- hf
->value
;
2505 const char *const params
=
2507 field
== TYPE_HEADER
2508 ? ct
->c_ctinfo
.ci_first_pm
2509 : ct
->c_dispo_first
,
2511 const char *const new_params
= concat (params
, "\n", NULL
);
2513 replace_substring (&hf
->value
, semicolon_loc
, new_params
);
2514 free ((char *) new_params
);
2515 free ((char *) params
);
2517 advise (NULL
, "did not find semicolon in %s:%s\n",
2518 hf
->name
, hf
->value
);
2528 * Output content in input file to output file.
2531 write_content (CT ct
, const char *input_filename
, char *outfile
, int modify_inplace
,
2535 if (modify_inplace
) {
2536 if (message_mods
> 0) {
2537 if ((status
= output_message (ct
, outfile
)) == OK
) {
2538 char *infile
= input_filename
2539 ? add (input_filename
, NULL
)
2540 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
2542 if (remove_file (infile
) == OK
) {
2543 if (rename (outfile
, infile
)) {
2544 /* Rename didn't work, possibly because of an
2545 attempt to rename across filesystems. Try
2546 brute force copy. */
2547 int old
= open (outfile
, O_RDONLY
);
2549 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
2552 if (old
!= -1 && new != -1) {
2553 char buffer
[BUFSIZ
];
2555 while ((i
= read (old
, buffer
, sizeof buffer
)) >
2557 if (write (new, buffer
, i
) != i
) {
2563 if (new != -1) { close (new); }
2564 if (old
!= -1) { close (old
); }
2565 (void) m_unlink (outfile
);
2568 /* The -file argument processing used path() to
2569 expand filename to absolute path. */
2570 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
2572 admonish (NULL
, "unable to rename %s %s to %s",
2573 file
? "file" : "message", outfile
,
2579 admonish (NULL
, "unable to remove input file %s, "
2580 "not modifying it", infile
);
2581 (void) m_unlink (outfile
);
2590 /* No modifications and didn't need the tmp outfile. */
2591 (void) m_unlink (outfile
);
2594 /* Output is going to some file. Produce it whether or not
2595 there were modifications. */
2596 status
= output_message (ct
, outfile
);
2605 * parse_mime() does not set lf_line_endings in struct text, so use this
2606 * function to do it. It touches the parts the decodetypes identifies.
2609 set_text_ctparams(CT ct
, char *decodetypes
, int lf_line_endings
) {
2610 switch (ct
->c_type
) {
2611 case CT_MULTIPART
: {
2612 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
2615 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
2616 set_text_ctparams(part
->mp_part
, decodetypes
, lf_line_endings
);
2622 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
2623 struct exbody
*e
= (struct exbody
*) ct
->c_ctparams
;
2625 set_text_ctparams(e
->eb_content
, decodetypes
, lf_line_endings
);
2630 if (should_decode(decodetypes
, ct
->c_ctinfo
.ci_type
, ct
->c_ctinfo
.ci_subtype
)) {
2631 if (ct
->c_ctparams
== NULL
) {
2632 if ((ct
->c_ctparams
= (struct text
*) mh_xcalloc (1, sizeof (struct text
))) == NULL
) {
2633 adios (NULL
, "out of memory");
2636 ((struct text
*) ct
->c_ctparams
)->lf_line_endings
= lf_line_endings
;
2643 * If "rmmproc" is defined, call that to remove the file. Otherwise,
2644 * use the standard MH backup file.
2647 remove_file (const char *file
) {
2649 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
2650 int status
= system (rmm_command
);
2653 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
2655 /* This is OK for a non-message file, it still uses the
2656 BACKUP_PREFIX form. The backup file will be in the same
2657 directory as file. */
2658 return rename (file
, m_backup (file
));
2664 * Output formatted message to user.
2667 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
2672 va_start (args
, message
);
2673 fmt
= concat (filename
, partno
? " part " : ", ",
2674 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
2676 advertise (what
, NULL
, fmt
, args
);
2689 fprintf (stderr
, "\n");