2 * mhfixmsg.c -- rewrite a message with various tranformations
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", 0, DECODETEXTSW) \
18 X("nodecodetext", 0, NDECODETEXTSW) \
19 X("textcharset", 0, TEXTCHARSETSW) \
20 X("notextcharset", 0, NTEXTCHARSETSW) \
21 X("reformat", 0, REFORMATSW) \
22 X("noreformat", 0, NREFORMATSW) \
23 X("replacetextplain", 0, REPLACETEXTPLAINSW) \
24 X("noreplacetextplain", 0, NREPLACETEXTPLAINSW) \
25 X("fixboundary", 0, FIXBOUNDARYSW) \
26 X("nofixboundary", 0, NFIXBOUNDARYSW) \
27 X("fixcte", 0, FIXCTESW) \
28 X("nofixcte", 0, NFIXCTESW) \
29 X("file file", 0, FILESW) \
30 X("outfile file", 0, OUTFILESW) \
31 X("rmmproc program", 0, RPROCSW) \
32 X("normmproc", 0, NRPRCSW) \
33 X("verbose", 0, VERBSW) \
34 X("noverbose", 0, NVERBSW) \
35 X("version", 0, VERSIONSW) \
36 X("help", 0, HELPSW) \
38 #define X(sw, minchars, id) id,
39 DEFINE_SWITCH_ENUM(MHFIXMSG
);
42 #define X(sw, minchars, id) { sw, minchars, id },
43 DEFINE_SWITCH_ARRAY(MHFIXMSG
, switches
);
48 int debugsw
; /* Needed by mhparse.c. */
50 #define quitser pipeser
53 extern int skip_mp_cte_check
; /* flag to InitMultiPart */
54 extern int suppress_bogus_mp_content_warning
; /* flag to InitMultiPart */
55 extern int bogus_mp_content
; /* flag from InitMultiPart */
56 CT
parse_mime (char *);
57 void reverse_parts (CT
);
60 int output_message (CT
, char *);
63 int show_content_aux (CT
, int, int, char *, char *);
66 void flush_errors (void);
70 void freects_done (int) NORETURN
;
75 typedef struct fix_transformations
{
82 } fix_transformations
;
84 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
85 static void reverse_alternative_parts (CT
);
86 static int fix_boundary (CT
*, int *);
87 static int get_multipart_boundary (CT
, char **);
88 static int replace_boundary (CT
, char *, char *);
89 static int fix_multipart_cte (CT
, int *);
90 static int set_ce (CT
, int);
91 static int ensure_text_plain (CT
*, CT
, int *, int);
92 static CT
build_text_plain_part (CT
);
93 static CT
divide_part (CT
);
94 static void copy_ctinfo (CI
, CI
);
95 static int decode_part (CT
);
96 static int reformat_part (CT
, char *, char *, char *, int);
97 static int charset_encoding (CT
);
98 static CT
build_multipart_alt (CT
, CT
, int, int);
99 static int boundary_in_content (FILE **, char *, const char *);
100 static void transfer_noncontent_headers (CT
, CT
);
101 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
102 static int decode_text_parts (CT
, int, int *);
103 static int content_encoding (CT
, const char **);
104 static int strip_crs (CT
, int *);
105 static int convert_charsets (CT
, char *, int *);
106 static int write_content (CT
, char *, char *, int, int);
107 static int remove_file (char *);
108 static void report (char *, char *, char *, ...);
109 static void pipeser (int);
113 main (int argc
, char **argv
) {
115 char *cp
, *file
= NULL
, *folder
= NULL
;
116 char *maildir
, buf
[100], *outfile
= NULL
;
117 char **argp
, **arguments
;
118 struct msgs_array msgs
= { 0, 0, NULL
};
119 struct msgs
*mp
= NULL
;
124 fix_transformations fx
;
125 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
126 fx
.replacetextplain
= 0;
127 fx
.decodetext
= CE_8BIT
;
128 fx
.textcharset
= NULL
;
130 if (nmh_init(argv
[0], 1)) { return 1; }
134 arguments
= getarguments (invo_name
, argc
, argv
, 1);
140 while ((cp
= *argp
++)) {
142 switch (smatch (++cp
, switches
)) {
144 ambigsw (cp
, switches
);
147 adios (NULL
, "-%s unknown", cp
);
150 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
152 print_help (buf
, switches
, 1);
155 print_version(invo_name
);
159 if (! (cp
= *argp
++) || *cp
== '-')
160 adios (NULL
, "missing argument to %s", argp
[-2]);
161 if (! strcasecmp (cp
, "8bit")) {
162 fx
.decodetext
= CE_8BIT
;
163 } else if (! strcasecmp (cp
, "7bit")) {
164 fx
.decodetext
= CE_7BIT
;
166 adios (NULL
, "invalid argument to %s", argp
[-2]);
173 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
174 adios (NULL
, "missing argument to %s", argp
[-2]);
198 case REPLACETEXTPLAINSW
:
199 fx
.replacetextplain
= 1;
201 case NREPLACETEXTPLAINSW
:
202 fx
.replacetextplain
= 0;
205 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
206 adios (NULL
, "missing argument to %s", argp
[-2]);
207 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
210 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
211 adios (NULL
, "missing argument to %s", argp
[-2]);
212 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
215 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
216 adios (NULL
, "missing argument to %s", argp
[-2]);
229 if (*cp
== '+' || *cp
== '@') {
231 adios (NULL
, "only one folder at a time!");
233 folder
= pluspath (cp
);
236 /* Interpret a full path as a filename, not a message. */
237 file
= add (cp
, NULL
);
239 app_msgarg (&msgs
, cp
);
244 SIGNAL (SIGQUIT
, quitser
);
245 SIGNAL (SIGPIPE
, pipeser
);
248 * Read the standard profile setup
250 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
251 readconfig ((struct node
**) 0, fp
, cp
, 0);
255 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
257 if (! context_find ("path"))
258 free (path ("./", TFOLDER
));
260 if (file
&& msgs
.size
)
261 adios (NULL
, "cannot specify msg and file at same time!");
264 * check if message is coming from file
267 /* If file is stdin, create a tmp file name before parse_mime()
268 has a chance, because it might put in on a different
269 filesystem than the output file. Instead, put it in the
270 user's preferred tmp directory. */
273 if (! strcmp ("-", file
)) {
279 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
280 adios (NULL
, "unable to create temporary file in %s",
284 file
= add (cp
, NULL
);
285 cpydata (STDIN_FILENO
, fd
, "-", file
);
289 (void) m_unlink (file
);
290 adios (NULL
, "failed to write temporary file");
294 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
)))
295 adios (NULL
, "out of memory");
298 if ((ct
= parse_mime (file
))) *ctp
++ = ct
;
301 * message(s) are coming from a folder
306 app_msgarg(&msgs
, "cur");
308 folder
= getfolder (1);
309 maildir
= m_maildir (folder
);
311 if (chdir (maildir
) == NOTOK
)
312 adios (maildir
, "unable to change directory to");
314 /* read folder and create message structure */
315 if (! (mp
= folder_read (folder
, 1)))
316 adios (NULL
, "unable to read folder %s", folder
);
318 /* check for empty folder */
320 adios (NULL
, "no messages in %s", folder
);
322 /* parse all the message ranges/sequences and set SELECTED */
323 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
324 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
326 seq_setprev (mp
); /* set the previous-sequence */
328 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
)))
329 adios (NULL
, "out of memory");
332 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
333 if (is_selected(mp
, msgnum
)) {
336 msgnam
= m_name (msgnum
);
337 if ((ct
= parse_mime (msgnam
))) *ctp
++ = ct
;
341 seq_setcur (mp
, mp
->hghsel
); /* update current message */
342 seq_save (mp
); /* synchronize sequences */
343 context_replace (pfolder
, folder
);/* update current folder */
344 context_save (); /* save the context file */
348 for (ctp
= cts
; *ctp
; ++ctp
) {
349 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
352 (void) m_unlink (file
);
355 /* Just calling m_backup() unlinks the backup file. */
356 (void) m_backup (file
);
367 /* done is freects_done, which will clean up all of cts. */
374 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
375 /* Store input filename in case one of the transformations, i.e.,
376 fix_boundary(), rewrites to a tmp file. */
377 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
378 int modify_inplace
= 0;
379 int message_mods
= 0;
382 if (outfile
== NULL
) {
385 if ((*ctp
)->c_file
) {
387 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
388 adios (NULL
, "unable to create temporary file in %s",
391 outfile
= add (tempfile
, NULL
);
393 adios (NULL
, "missing both input and output filenames\n");
397 reverse_alternative_parts (*ctp
);
398 if (status
== OK
&& fx
->fixboundary
) {
399 status
= fix_boundary (ctp
, &message_mods
);
401 if (status
== OK
&& fx
->fixcte
) {
402 status
= fix_multipart_cte (*ctp
, &message_mods
);
404 if (status
== OK
&& fx
->reformat
) {
406 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
408 if (status
== OK
&& fx
->decodetext
) {
409 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
411 if (status
== OK
&& fx
->textcharset
!= NULL
) {
412 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
415 if (! (*ctp
)->c_umask
) {
416 /* Set the umask for the contents file. This currently
417 isn't used but just in case it is in the future. */
420 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
421 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
423 (*ctp
)->c_umask
= ~m_gmprot();
428 * Write the content to a file
431 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
433 } else if (! modify_inplace
) {
434 /* Something went wrong. Output might be expected, such
435 as if this were run as a filter. Just copy the input
437 int in
= open (input_filename
, O_RDONLY
);
438 int out
= strcmp (outfile
, "-")
439 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
442 if (in
!= -1 && out
!= -1) {
443 cpydata (in
, out
, input_filename
, outfile
);
452 if (modify_inplace
) {
453 if (status
!= OK
) (void) m_unlink (outfile
);
458 free (input_filename
);
464 /* parse_mime() arranges alternates in reverse (priority) order, so
465 reverse them back. This will put a text/plain part at the front of
466 a multipart/alternative part, for example, where it belongs. */
468 reverse_alternative_parts (CT ct
) {
469 if (ct
->c_type
== CT_MULTIPART
) {
470 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
473 if (ct
->c_subtype
== MULTI_ALTERNATE
) {
477 /* And call recursively on each part of a multipart. */
478 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
479 reverse_alternative_parts (part
->mp_part
);
486 fix_boundary (CT
*ct
, int *message_mods
) {
487 struct multipart
*mp
;
490 if (bogus_mp_content
) {
491 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
494 * 1) Get boundary at end of part.
495 * 2) Get boundary at beginning of part and compare to the end-of-part
497 * 3) Write out contents of ct to tmp file, replacing boundary in
498 * header with boundary from part. Set c_unlink to 1.
500 * 5) Call parse_mime() on the tmp file, replacing ct.
503 if (mp
&& mp
->mp_start
) {
506 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
509 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
510 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
511 char *filename
= add ((*ct
)->c_file
, NULL
);
514 if ((*ct
= parse_mime (fixed
))) {
519 report (NULL
, filename
,
520 "fix multipart boundary");
525 advise (NULL
, "unable to replace broken boundary");
529 advise (NULL
, "unable to create temporary file in %s",
534 free (part_boundary
);
544 get_multipart_boundary (CT ct
, char **part_boundary
) {
546 char *end_boundary
= NULL
;
547 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
548 ? (off_t
) (ct
->c_end
- sizeof buffer
)
549 : (off_t
) ct
->c_begin
;
553 /* This will fail if the boundary spans fread() calls. BUFSIZ should
554 be big enough, even if it's just 1024, to make that unlikely. */
556 /* free_content() will close ct->c_fp. */
557 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
558 advise (ct
->c_file
, "unable to open for reading");
562 /* Get boundary at end of multipart. */
563 while (begin
>= (off_t
) ct
->c_begin
) {
564 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
565 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
566 char *end
= buffer
+ bytes_read
- 1;
569 if ((cp
= rfind_str (buffer
, bytes_read
, "--"))) {
570 /* Trim off trailing "--" and anything beyond. */
572 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
573 if (strlen (end
) > 3 && *end
++ == '\n' &&
574 *end
++ == '-' && *end
++ == '-') {
575 end_boundary
= add (end
, NULL
);
582 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
583 begin
-= sizeof buffer
;
589 /* Get boundary at beginning of multipart. */
591 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
592 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
593 if (bytes_read
>= strlen (end_boundary
)) {
594 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
596 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
597 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
602 /* The start and end boundaries didn't match, or the
603 start boundary doesn't begin with "\n--" (or "--"
604 if at the beginning of buffer). Keep trying. */
613 *part_boundary
= end_boundary
;
615 *part_boundary
= NULL
;
623 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
625 replace_boundary (CT ct
, char *file
, char *boundary
) {
628 char buf
[BUFSIZ
], name
[NAMESZ
];
630 m_getfld_state_t gstate
= 0;
633 if (ct
->c_file
== NULL
) {
634 advise (NULL
, "missing input filename");
638 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
639 advise (ct
->c_file
, "unable to open for reading");
643 if ((fpout
= fopen (file
, "w")) == NULL
) {
645 advise (file
, "unable to open for writing");
649 for (compnum
= 1;;) {
650 int bufsz
= (int) sizeof buf
;
652 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
657 /* get copies of the buffers */
658 np
= add (name
, NULL
);
659 vp
= add (buf
, NULL
);
661 /* if necessary, get rest of field */
662 while (state
== FLDPLUS
) {
664 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
665 vp
= add (buf
, vp
); /* add to previous value */
668 if (strcasecmp (TYPE_FIELD
, np
)) {
669 fprintf (fpout
, "%s:%s", np
, vp
);
671 char *new_ctline
, *new_params
;
673 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
674 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
677 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
678 ct
->c_ctinfo
.ci_subtype
, NULL
);
679 new_params
= output_params(strlen(TYPE_FIELD
) +
680 strlen(new_ctline
) + 1,
681 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
682 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
683 new_params
? new_params
: "");
696 /* buf will have a terminating NULL, skip it. */
697 fwrite (buf
, 1, bufsz
-1, fpout
);
705 advise (NULL
, "message format error in component #%d", compnum
);
710 advise (NULL
, "getfld() returned %d", state
);
718 m_getfld_state_destroy (&gstate
);
727 fix_multipart_cte (CT ct
, int *message_mods
) {
730 if (ct
->c_type
== CT_MULTIPART
) {
734 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
735 ct
->c_encoding
!= CE_BINARY
) {
738 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
739 char *name
= hf
->name
;
740 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
744 if (! strncasecmp (name
, ENCODING_FIELD
,
745 strlen (ENCODING_FIELD
))) {
746 char *prefix
= "Nmh-REPLACED-INVALID-";
747 HF h
= mh_xmalloc (sizeof *h
);
749 h
->name
= add (hf
->name
, NULL
);
750 h
->hf_encoding
= hf
->hf_encoding
;
754 /* Retain old header but prefix its name. */
756 hf
->name
= concat (prefix
, h
->name
, NULL
);
760 char *encoding
= cpytrim (hf
->value
);
761 report (ct
->c_partno
, ct
->c_file
,
762 "replace Content-Transfer-Encoding of %s "
763 "with 8 bit", encoding
);
767 h
->value
= add (" 8bit\n", NULL
);
769 /* Don't need to warn for multiple C-T-E header
770 fields, parse_mime() already does that. But
771 if there are any, fix them all as necessary. */
776 set_ce (ct
, CE_8BIT
);
779 m
= (struct multipart
*) ct
->c_ctparams
;
780 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
781 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
793 set_ce (CT ct
, int encoding
) {
794 const char *ce
= ce_str (encoding
);
795 const struct str2init
*ctinit
= get_ce_method (ce
);
798 char *cte
= concat (" ", ce
, "\n", NULL
);
801 /* Decoded contents might be in ct->c_cefile.ce_file, if the
802 caller is decode_text_parts (). Save because we'll
804 struct cefile decoded_content_info
= ct
->c_cefile
;
806 ct
->c_encoding
= encoding
;
808 ct
->c_ctinitfnx
= ctinit
->si_init
;
809 /* This will assign ct->c_cefile with an all-0 struct, which
811 (*ctinit
->si_init
) (ct
);
812 /* After returning, the caller should set
813 ct->c_cefile.ce_file to the name of the file containing
816 /* Restore the cefile. */
817 ct
->c_cefile
= decoded_content_info
;
819 /* Update/add Content-Transfer-Encoding header field. */
820 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
821 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
828 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
831 /* Update c_celine. It's used only by mhlist -debug. */
833 ct
->c_celine
= add (cte
, NULL
);
842 /* Make sure each text part has a corresponding text/plain part. */
844 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
847 switch ((*ct
)->c_type
) {
849 int has_text_plain
= 0;
851 /* Nothing to do for text/plain. */
852 if ((*ct
)->c_subtype
== TEXT_PLAIN
) return OK
;
854 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
855 parent
->c_subtype
== MULTI_ALTERNATE
) {
856 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
857 struct part
*part
, *prev
;
858 int new_subpart_number
= 1;
860 /* See if there is a sibling text/plain. */
861 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
862 ++new_subpart_number
;
863 if (part
->mp_part
->c_type
== CT_TEXT
&&
864 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
865 if (replacetextplain
) {
866 struct part
*old_part
;
867 if (part
== mp
->mp_parts
) {
868 old_part
= mp
->mp_parts
;
869 mp
->mp_parts
= part
->mp_next
;
871 old_part
= prev
->mp_next
;
872 prev
->mp_next
= part
->mp_next
;
875 report (parent
->c_partno
, parent
->c_file
,
876 "remove text/plain part %s",
877 old_part
->mp_part
->c_partno
);
879 free_content (old_part
->mp_part
);
889 if (! has_text_plain
) {
890 /* Parent is a multipart/alternative. Insert a new
891 text/plain subpart. */
892 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
894 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
896 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
898 new_part
->mp_next
= mp
->mp_parts
;
899 mp
->mp_parts
= new_part
;
900 new_part
->mp_part
->c_partno
=
901 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
906 report (parent
->c_partno
, parent
->c_file
,
907 "insert text/plain part");
910 free_content (new_part
->mp_part
);
916 /* Slip new text/plain part into a new multipart/alternative. */
917 CT tp_part
= build_text_plain_part (*ct
);
920 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
923 struct multipart
*mp
=
924 (struct multipart
*) mp_alt
->c_ctparams
;
926 if (mp
&& mp
->mp_parts
) {
927 mp
->mp_parts
->mp_part
= tp_part
;
928 /* Make the new multipart/alternative the parent. */
933 report ((*ct
)->c_partno
, (*ct
)->c_file
,
934 "insert text/plain part");
937 free_content (tp_part
);
938 free_content (mp_alt
);
952 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
955 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
956 if ((*ct
)->c_type
== CT_MULTIPART
) {
957 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
965 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
968 e
= (struct exbody
*) (*ct
)->c_ctparams
;
969 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
980 build_text_plain_part (CT encoded_part
) {
981 CT tp_part
= divide_part (encoded_part
);
982 char *tmp_plain_file
= NULL
;
984 if (decode_part (tp_part
) == OK
) {
985 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
986 contains the decoded contents. And the decoding function, such
987 as openQuoted, will have set ...->ce_unlink to 1 so that it will
988 be unlinked by free_content (). */
991 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
992 advise (NULL
, "unable to create temporary file in %s",
995 tmp_plain_file
= add (tempfile
, NULL
);
996 if (reformat_part (tp_part
, tmp_plain_file
,
997 tp_part
->c_ctinfo
.ci_type
,
998 tp_part
->c_ctinfo
.ci_subtype
,
999 tp_part
->c_type
) == OK
) {
1004 free_content (tp_part
);
1005 (void) m_unlink (tmp_plain_file
);
1006 free (tmp_plain_file
);
1013 divide_part (CT ct
) {
1016 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
1017 adios (NULL
, "out of memory");
1019 /* Just copy over what is needed for decoding. c_vrsn and
1020 c_celine aren't necessary. */
1021 new_part
->c_file
= add (ct
->c_file
, NULL
);
1022 new_part
->c_begin
= ct
->c_begin
;
1023 new_part
->c_end
= ct
->c_end
;
1024 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1025 new_part
->c_type
= ct
->c_type
;
1026 new_part
->c_cefile
= ct
->c_cefile
;
1027 new_part
->c_encoding
= ct
->c_encoding
;
1028 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1029 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1030 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1031 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1033 /* c_ctline is used by reformat__part(), so it can preserve
1034 anything after the type/subtype. */
1035 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1042 copy_ctinfo (CI dest
, CI src
) {
1045 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1046 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1048 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1049 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1051 if (s_pm
->pm_charset
)
1052 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1054 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1057 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1058 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1063 decode_part (CT ct
) {
1068 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1069 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1071 tmp_decoded
= add (tempfile
, NULL
);
1072 /* The following call will load ct->c_cefile.ce_file with the tmp
1073 filename of the decoded content. tmp_decoded will contain the
1074 encoded output, get rid of that. */
1075 status
= output_message (ct
, tmp_decoded
);
1076 (void) m_unlink (tmp_decoded
);
1083 /* Some of the arguments aren't really needed now, but maybe will
1084 be in the future for other than text types. */
1086 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1087 int output_subtype
, output_encoding
;
1091 /* Hacky: this redirects the output from whatever command is used
1092 to show the part to a file. So, the user can't have any output
1093 redirection in that command.
1094 Could show_multi() in mhshowsbr.c avoid this? */
1096 /* Check for invo_name-format-type/subtype. */
1097 cp
= concat (invo_name
, "-format-", type
, "/", subtype
, NULL
);
1098 if ((cf
= context_find (cp
)) && *cf
!= '\0') {
1099 if (strchr (cf
, '>')) {
1101 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1102 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1109 /* Check for invo_name-format-type. */
1110 cp
= concat (invo_name
, "-format-", type
, NULL
);
1111 if (! (cf
= context_find (cp
)) || *cf
== '\0') {
1114 advise (NULL
, "Don't know how to convert %s, there is no "
1115 "%s-format-%s/%s profile entry",
1116 ct
->c_file
, invo_name
, type
, subtype
);
1121 if (strchr (cf
, '>')) {
1123 advise (NULL
, "'>' prohibited in \"%s\"", cf
);
1129 cp
= concat (cf
, " >", file
, NULL
);
1130 status
= show_content_aux (ct
, 1, 0, cp
, NULL
);
1133 /* Unlink decoded content tmp file and free its filename to avoid
1134 leaks. The file stream should already have been closed. */
1135 if (ct
->c_cefile
.ce_unlink
) {
1136 (void) m_unlink (ct
->c_cefile
.ce_file
);
1137 free (ct
->c_cefile
.ce_file
);
1138 ct
->c_cefile
.ce_file
= NULL
;
1139 ct
->c_cefile
.ce_unlink
= 0;
1142 if (c_type
== CT_TEXT
) {
1143 output_subtype
= TEXT_PLAIN
;
1145 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1148 output_encoding
= charset_encoding (ct
);
1150 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1151 ct
->c_cefile
.ce_file
= file
;
1152 ct
->c_cefile
.ce_unlink
= 1;
1154 ct
->c_cefile
.ce_unlink
= 0;
1162 /* Identifies 7bit or 8bit content based on charset. */
1164 charset_encoding (CT ct
) {
1165 /* norm_charmap() is case sensitive. */
1166 char *charset
= upcase (content_charset (ct
));
1168 strcmp (norm_charmap (charset
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1176 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1177 char *boundary_prefix
= "----=_nmh-multipart";
1178 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1179 char *boundary_indicator
= "; boundary=";
1180 char *typename
, *subtypename
, *name
;
1183 struct multipart
*m
;
1184 const struct str2init
*ctinit
;
1186 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1187 adios (NULL
, "out of memory");
1189 /* Set up the multipart/alternative part. These fields of *ct were
1190 initialized to 0 by calloc():
1191 c_fp, c_unlink, c_begin, c_end,
1192 c_vrsn, c_ctline, c_celine,
1193 c_id, c_descr, c_dispo, c_partno,
1194 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1195 c_cefile, c_encoding,
1196 c_digested, c_digest[16], c_ctexbody,
1197 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1198 c_umask, c_pid, c_rfc934,
1199 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1202 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1204 ct
->c_subtype
= subtype
;
1206 ctinit
= get_ct_init (ct
->c_type
);
1208 typename
= ct_type_str (type
);
1209 subtypename
= ct_subtype_str (type
, subtype
);
1213 int found_boundary
= 1;
1215 while (found_boundary
&& serial
< 1000000) {
1218 /* Ensure that the boundary doesn't appear in the decoded
1220 if (new_part
->c_cefile
.ce_file
) {
1221 if ((found_boundary
=
1222 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1223 new_part
->c_cefile
.ce_file
,
1229 /* Ensure that the boundary doesn't appear in the encoded
1231 if (! found_boundary
&& new_part
->c_file
) {
1232 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1239 if (found_boundary
) {
1240 /* Try a slightly different boundary. */
1245 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1247 concat (boundary_prefix
,
1248 first_alt
->c_partno
? first_alt
->c_partno
: "",
1249 "-", buffer2
, NULL
);
1253 if (found_boundary
) {
1254 advise (NULL
, "giving up trying to find a unique boundary");
1259 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1260 boundary
, "\"", NULL
);
1262 /* Load c_first_hf and c_last_hf. */
1263 transfer_noncontent_headers (first_alt
, ct
);
1264 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1267 /* Load c_partno. */
1268 if (first_alt
->c_partno
) {
1269 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1270 free (first_alt
->c_partno
);
1271 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1272 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1274 first_alt
->c_partno
= add ("1", NULL
);
1275 new_part
->c_partno
= add ("2", NULL
);
1279 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1280 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1283 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1284 "boundary", boundary
, 0);
1286 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1287 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1288 p
->mp_next
->mp_next
= NULL
;
1289 p
->mp_next
->mp_part
= first_alt
;
1291 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1293 adios (NULL
, "out of memory");
1294 m
->mp_start
= concat (boundary
, "\n", NULL
);
1295 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1297 ct
->c_ctparams
= (void *) m
;
1305 /* Check that the boundary does not appear in the content. */
1307 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1308 char buffer
[BUFSIZ
];
1310 int found_boundary
= 0;
1312 /* free_content() will close *fp if we fopen it here. */
1313 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1314 advise (file
, "unable to open %s for reading", file
);
1318 fseeko (*fp
, 0L, SEEK_SET
);
1319 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1320 if (find_str (buffer
, bytes_read
, boundary
)) {
1326 return found_boundary
;
1330 /* Remove all non-Content headers. */
1332 transfer_noncontent_headers (CT old
, CT
new) {
1335 hp_prev
= hp
= old
->c_first_hf
;
1339 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1340 if (hp
== old
->c_last_hf
) {
1341 if (hp
== old
->c_first_hf
) {
1342 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1344 hp_prev
->next
= NULL
;
1345 old
->c_last_hf
= hp_prev
;
1348 if (hp
== old
->c_first_hf
) {
1349 old
->c_first_hf
= next
;
1351 hp_prev
->next
= next
;
1355 /* Put node hp in the new CT. */
1356 if (new->c_first_hf
== NULL
) {
1357 new->c_first_hf
= hp
;
1359 new->c_last_hf
->next
= hp
;
1361 new->c_last_hf
= hp
;
1363 /* A Content- header, leave in old. */
1373 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1374 char *typename
= ct_type_str (type
);
1375 char *subtypename
= ct_subtype_str (type
, subtype
);
1376 /* E.g, " text/plain" */
1377 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1378 /* E.g, " text/plain\n" */
1379 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1380 int found_content_type
= 0;
1382 const char *cp
= NULL
;
1386 /* Update/add Content-Type header field. */
1387 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1388 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1389 found_content_type
= 1;
1391 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1392 ? concat (type_subtypename
, cp
, "\n", NULL
)
1393 : add (name_plus_nl
, NULL
);
1396 if (! found_content_type
) {
1397 add_header (ct
, add (TYPE_FIELD
, NULL
),
1398 (cp
= strchr (ct
->c_ctline
, ';'))
1399 ? concat (type_subtypename
, cp
, "\n", NULL
)
1400 : add (name_plus_nl
, NULL
));
1403 /* Some of these might not be used, but set them anyway. */
1405 ? concat (type_subtypename
, cp
, NULL
)
1406 : concat (type_subtypename
, NULL
);
1407 free (ct
->c_ctline
);
1408 ct
->c_ctline
= ctline
;
1409 /* Leave other ctinfo members as they were. */
1410 free (ct
->c_ctinfo
.ci_type
);
1411 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1412 free (ct
->c_ctinfo
.ci_subtype
);
1413 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1415 ct
->c_subtype
= subtype
;
1417 free (name_plus_nl
);
1418 free (type_subtypename
);
1420 status
= set_ce (ct
, encoding
);
1427 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1430 switch (ct
->c_type
) {
1432 switch (ct
->c_encoding
) {
1437 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1438 const char *reason
= NULL
;
1440 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1441 && encoding
!= CE_BINARY
) {
1442 /* The decoding isn't acceptable so discard it.
1443 Leave status as OK to allow other transformations. */
1445 report (ct
->c_partno
, ct
->c_file
,
1446 "will not decode%s because it is binary (%s)",
1448 : ct
->c_ctline
? ct
->c_ctline
1452 (void) m_unlink (ct
->c_cefile
.ce_file
);
1453 free (ct
->c_cefile
.ce_file
);
1454 ct
->c_cefile
.ce_file
= NULL
;
1455 } else if (ct
->c_encoding
== CE_QUOTED
&&
1456 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1457 /* The decoding isn't acceptable so discard it.
1458 Leave status as OK to allow other transformations. */
1460 report (ct
->c_partno
, ct
->c_file
,
1461 "will not decode%s because it is 8bit",
1463 : ct
->c_ctline
? ct
->c_ctline
1466 (void) m_unlink (ct
->c_cefile
.ce_file
);
1467 free (ct
->c_cefile
.ce_file
);
1468 ct
->c_cefile
.ce_file
= NULL
;
1471 if (ct_encoding
== CE_BINARY
)
1473 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1476 enc
= charset_encoding (ct
);
1477 if (set_ce (ct
, enc
) == OK
) {
1480 report (ct
->c_partno
, ct
->c_file
, "decode%s",
1481 ct
->c_ctline
? ct
->c_ctline
: "");
1483 strip_crs (ct
, message_mods
);
1495 strip_crs (ct
, message_mods
);
1503 case CT_MULTIPART
: {
1504 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1507 /* Should check to see if the body for this part is encoded?
1508 For now, it gets passed along as-is by InitMultiPart(). */
1509 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1510 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1516 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1519 e
= (struct exbody
*) ct
->c_ctparams
;
1520 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1532 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1533 if it has any NUL characters, a CR not followed by a LF, or lines
1534 greater than 998 characters in length. If binary, reason is set
1535 to a string explaining why. */
1537 content_encoding (CT ct
, const char **reason
) {
1538 CE ce
= &ct
->c_cefile
;
1539 int encoding
= CE_7BIT
;
1542 size_t line_len
= 0;
1543 char buffer
[BUFSIZ
];
1546 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1547 advise (ce
->ce_file
, "unable to open for reading");
1551 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1552 while (encoding
!= CE_BINARY
&&
1553 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1556 int last_char_was_cr
= 0;
1558 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1559 if (*cp
== '\0' || ++line_len
> 998 ||
1560 (*cp
!= '\n' && last_char_was_cr
)) {
1561 encoding
= CE_BINARY
;
1563 *reason
= "null character";
1564 } else if (line_len
> 998) {
1565 *reason
= "line length > 998";
1566 } else if (*cp
!= '\n' && last_char_was_cr
) {
1567 *reason
= "CR not followed by LF";
1569 /* Should not reach this. */
1573 } else if (*cp
== '\n') {
1575 } else if (! isascii ((unsigned char) *cp
)) {
1579 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1585 } /* else should never happen */
1592 strip_crs (CT ct
, int *message_mods
) {
1593 /* norm_charmap() is case sensitive. */
1594 char *charset
= upcase (content_charset (ct
));
1597 /* Only strip carriage returns if content is ASCII or another
1598 charset that has the same readily recognizable CR followed by a
1599 LF. We can include UTF-8 here because if the high-order bit of
1600 a UTF-8 byte is 0, then it must be a single-byte ASCII
1602 if (! strcmp (norm_charmap (charset
), "US-ASCII") ||
1603 ! strncmp (norm_charmap (charset
), "ISO-8859-", 9) ||
1604 ! strncmp (norm_charmap (charset
), "UTF-8", 5) ||
1605 ! strncmp (norm_charmap (charset
), "WINDOWS-12", 10)) {
1611 int opened_input_file
= 0;
1613 if (ct
->c_cefile
.ce_file
) {
1614 file
= &ct
->c_cefile
.ce_file
;
1615 fp
= &ct
->c_cefile
.ce_fp
;
1617 } else if (ct
->c_file
) {
1620 begin
= (size_t) ct
->c_begin
;
1621 end
= (size_t) ct
->c_end
;
1622 } /* else don't know where the content is */
1624 if (file
&& *file
&& fp
) {
1626 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1627 advise (*file
, "unable to open for reading");
1630 opened_input_file
= 1;
1636 char buffer
[BUFSIZ
];
1638 size_t bytes_to_read
=
1639 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1641 fseeko (*fp
, begin
, SEEK_SET
);
1642 while ((bytes_read
= fread (buffer
, 1,
1643 min (bytes_to_read
, sizeof buffer
),
1645 /* Look for CR followed by a LF. This is supposed to
1646 be text so there should be LF's. If not, don't
1647 modify the content. */
1650 int last_char_was_cr
= 0;
1652 if (end
> 0) bytes_to_read
-= bytes_read
;
1654 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1655 if (*cp
== '\n' && last_char_was_cr
) {
1660 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1666 char *stripped_content_file
;
1667 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1669 if (tempfile
== NULL
) {
1670 adios (NULL
, "unable to create temporary file in %s",
1673 stripped_content_file
= add (tempfile
, NULL
);
1675 /* Strip each CR before a LF from the content. */
1676 fseeko (*fp
, begin
, SEEK_SET
);
1677 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1681 int last_char_was_cr
= 0;
1683 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1685 last_char_was_cr
= 1;
1686 } else if (last_char_was_cr
) {
1687 if (*cp
!= '\n') write (fd
, "\r", 1);
1689 last_char_was_cr
= 0;
1692 last_char_was_cr
= 0;
1698 admonish (NULL
, "unable to write temporary file %s",
1699 stripped_content_file
);
1700 (void) m_unlink (stripped_content_file
);
1703 /* Replace the decoded file with the converted one. */
1704 if (ct
->c_cefile
.ce_file
) {
1705 if (ct
->c_cefile
.ce_unlink
) {
1706 (void) m_unlink (ct
->c_cefile
.ce_file
);
1708 free (ct
->c_cefile
.ce_file
);
1710 ct
->c_cefile
.ce_file
= stripped_content_file
;
1711 ct
->c_cefile
.ce_unlink
= 1;
1715 report (ct
->c_partno
,
1716 begin
== 0 && end
== 0 ? "" : *file
,
1722 if (opened_input_file
) {
1735 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1738 switch (ct
->c_type
) {
1740 if (ct
->c_subtype
== TEXT_PLAIN
) {
1741 status
= convert_charset (ct
, dest_charset
, message_mods
);
1742 if (verbosw
&& status
== OK
) {
1743 report (ct
->c_partno
, ct
->c_file
, "convert %s to %s",
1744 content_charset(ct
), dest_charset
);
1749 case CT_MULTIPART
: {
1750 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1753 /* Should check to see if the body for this part is encoded?
1754 For now, it gets passed along as-is by InitMultiPart(). */
1755 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1757 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1763 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1766 e
= (struct exbody
*) ct
->c_ctparams
;
1768 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1781 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1785 if (modify_inplace
) {
1786 if (message_mods
> 0) {
1787 if ((status
= output_message (ct
, outfile
)) == OK
) {
1788 char *infile
= input_filename
1789 ? add (input_filename
, NULL
)
1790 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1792 if (remove_file (infile
) == OK
) {
1793 if (rename (outfile
, infile
)) {
1794 /* Rename didn't work, possibly because of an
1795 attempt to rename across filesystems. Try
1796 brute force copy. */
1797 int old
= open (outfile
, O_RDONLY
);
1799 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1802 if (old
!= -1 && new != -1) {
1803 char buffer
[BUFSIZ
];
1805 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1807 if (write (new, buffer
, i
) != i
) {
1813 if (new != -1) close (new);
1814 if (old
!= -1) close (old
);
1815 (void) m_unlink (outfile
);
1818 /* The -file argument processing used path() to
1819 expand filename to absolute path. */
1820 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1822 admonish (NULL
, "unable to rename %s %s to %s",
1823 file
? "file" : "message", outfile
,
1829 admonish (NULL
, "unable to remove input file %s, "
1830 "not modifying it", infile
);
1831 (void) m_unlink (outfile
);
1840 /* No modifications and didn't need the tmp outfile. */
1841 (void) m_unlink (outfile
);
1844 /* Output is going to some file. Produce it whether or not
1845 there were modifications. */
1846 status
= output_message (ct
, outfile
);
1855 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1856 * use the standard MH backup file.
1859 remove_file (char *file
) {
1861 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1862 int status
= system (rmm_command
);
1865 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1867 /* This is OK for a non-message file, it still uses the
1868 BACKUP_PREFIX form. The backup file will be in the same
1869 directory as file. */
1870 return rename (file
, m_backup (file
));
1876 report (char *partno
, char *filename
, char *message
, ...) {
1881 va_start (args
, message
);
1882 fmt
= concat (filename
, partno
? " part " : ", ",
1883 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1885 advertise (NULL
, NULL
, fmt
, args
);
1898 fprintf (stderr
, "\n");