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 */
58 int output_message (CT
, char *);
61 void flush_errors (void);
65 void freects_done (int) NORETURN
;
70 typedef struct fix_transformations
{
77 } fix_transformations
;
79 int mhfixmsgsbr (CT
*, const fix_transformations
*, char *);
80 static int fix_boundary (CT
*, int *);
81 static int get_multipart_boundary (CT
, char **);
82 static int replace_boundary (CT
, char *, char *);
83 static int fix_multipart_cte (CT
, int *);
84 static int set_ce (CT
, int);
85 static int ensure_text_plain (CT
*, CT
, int *, int);
86 static CT
build_text_plain_part (CT
);
87 static CT
divide_part (CT
);
88 static void copy_ctinfo (CI
, CI
);
89 static int decode_part (CT
);
90 static int reformat_part (CT
, char *, char *, char *, int);
91 static int charset_encoding (CT
);
92 static CT
build_multipart_alt (CT
, CT
, int, int);
93 static int boundary_in_content (FILE **, char *, const char *);
94 static void transfer_noncontent_headers (CT
, CT
);
95 static int set_ct_type (CT
, int type
, int subtype
, int encoding
);
96 static int decode_text_parts (CT
, int, int *);
97 static int content_encoding (CT
, const char **);
98 static int strip_crs (CT
, int *);
99 static int convert_charsets (CT
, char *, int *);
100 static int write_content (CT
, char *, char *, int, int);
101 static int remove_file (char *);
102 static void report (char *, char *, char *, char *, ...);
103 static void pipeser (int);
107 main (int argc
, char **argv
) {
109 char *cp
, *file
= NULL
, *folder
= NULL
;
110 char *maildir
, buf
[100], *outfile
= NULL
;
111 char **argp
, **arguments
;
112 struct msgs_array msgs
= { 0, 0, NULL
};
113 struct msgs
*mp
= NULL
;
118 fix_transformations fx
;
119 fx
.reformat
= fx
.fixcte
= fx
.fixboundary
= 1;
120 fx
.replacetextplain
= 0;
121 fx
.decodetext
= CE_8BIT
;
122 fx
.textcharset
= NULL
;
124 if (nmh_init(argv
[0], 1)) { return 1; }
128 arguments
= getarguments (invo_name
, argc
, argv
, 1);
134 while ((cp
= *argp
++)) {
136 switch (smatch (++cp
, switches
)) {
138 ambigsw (cp
, switches
);
141 adios (NULL
, "-%s unknown", cp
);
144 snprintf (buf
, sizeof buf
, "%s [+folder] [msgs] [switches]",
146 print_help (buf
, switches
, 1);
149 print_version(invo_name
);
153 if (! (cp
= *argp
++) || *cp
== '-')
154 adios (NULL
, "missing argument to %s", argp
[-2]);
155 if (! strcasecmp (cp
, "8bit")) {
156 fx
.decodetext
= CE_8BIT
;
157 } else if (! strcasecmp (cp
, "7bit")) {
158 fx
.decodetext
= CE_7BIT
;
160 adios (NULL
, "invalid argument to %s", argp
[-2]);
167 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
168 adios (NULL
, "missing argument to %s", argp
[-2]);
192 case REPLACETEXTPLAINSW
:
193 fx
.replacetextplain
= 1;
195 case NREPLACETEXTPLAINSW
:
196 fx
.replacetextplain
= 0;
199 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
200 adios (NULL
, "missing argument to %s", argp
[-2]);
201 file
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
204 if (! (cp
= *argp
++) || (*cp
== '-' && cp
[1]))
205 adios (NULL
, "missing argument to %s", argp
[-2]);
206 outfile
= *cp
== '-' ? add (cp
, NULL
) : path (cp
, TFILE
);
209 if (!(rmmproc
= *argp
++) || *rmmproc
== '-')
210 adios (NULL
, "missing argument to %s", argp
[-2]);
223 if (*cp
== '+' || *cp
== '@') {
225 adios (NULL
, "only one folder at a time!");
227 folder
= pluspath (cp
);
230 /* Interpret a full path as a filename, not a message. */
231 file
= add (cp
, NULL
);
233 app_msgarg (&msgs
, cp
);
238 SIGNAL (SIGQUIT
, quitser
);
239 SIGNAL (SIGPIPE
, pipeser
);
242 * Read the standard profile setup
244 if ((fp
= fopen (cp
= etcpath ("mhn.defaults"), "r"))) {
245 readconfig ((struct node
**) 0, fp
, cp
, 0);
249 suppress_bogus_mp_content_warning
= skip_mp_cte_check
= 1;
251 if (! context_find ("path"))
252 free (path ("./", TFOLDER
));
254 if (file
&& msgs
.size
)
255 adios (NULL
, "cannot specify msg and file at same time!");
258 * check if message is coming from file
261 /* If file is stdin, create a tmp file name before parse_mime()
262 has a chance, because it might put in on a different
263 filesystem than the output file. Instead, put it in the
264 user's preferred tmp directory. */
267 if (! strcmp ("-", file
)) {
273 if ((cp
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
)) == NULL
) {
274 adios (NULL
, "unable to create temporary file in %s",
278 file
= add (cp
, NULL
);
279 cpydata (STDIN_FILENO
, fd
, "-", file
);
283 (void) m_unlink (file
);
284 adios (NULL
, "failed to write temporary file");
288 if (! (cts
= (CT
*) calloc ((size_t) 2, sizeof *cts
))) {
289 adios (NULL
, "out of memory");
293 if ((ct
= parse_mime (file
))) { *ctp
++ = ct
; }
296 * message(s) are coming from a folder
301 app_msgarg(&msgs
, "cur");
303 folder
= getfolder (1);
304 maildir
= m_maildir (folder
);
306 if (chdir (maildir
) == NOTOK
)
307 adios (maildir
, "unable to change directory to");
309 /* read folder and create message structure */
310 if (! (mp
= folder_read (folder
, 1)))
311 adios (NULL
, "unable to read folder %s", folder
);
313 /* check for empty folder */
315 adios (NULL
, "no messages in %s", folder
);
317 /* parse all the message ranges/sequences and set SELECTED */
318 for (msgnum
= 0; msgnum
< msgs
.size
; msgnum
++)
319 if (! m_convert (mp
, msgs
.msgs
[msgnum
]))
321 seq_setprev (mp
); /* set the previous-sequence */
323 if (! (cts
= (CT
*) calloc ((size_t) (mp
->numsel
+ 1), sizeof *cts
))) {
324 adios (NULL
, "out of memory");
328 for (msgnum
= mp
->lowsel
; msgnum
<= mp
->hghsel
; msgnum
++) {
329 if (is_selected(mp
, msgnum
)) {
332 msgnam
= m_name (msgnum
);
333 if ((ct
= parse_mime (msgnam
))) { *ctp
++ = ct
; }
337 seq_setcur (mp
, mp
->hghsel
); /* update current message */
338 seq_save (mp
); /* synchronize sequences */
339 context_replace (pfolder
, folder
);/* update current folder */
340 context_save (); /* save the context file */
344 for (ctp
= cts
; *ctp
; ++ctp
) {
345 status
+= mhfixmsgsbr (ctp
, &fx
, outfile
);
348 (void) m_unlink (file
);
351 /* Just calling m_backup() unlinks the backup file. */
352 (void) m_backup (file
);
363 /* done is freects_done, which will clean up all of cts. */
370 mhfixmsgsbr (CT
*ctp
, const fix_transformations
*fx
, char *outfile
) {
371 /* Store input filename in case one of the transformations, i.e.,
372 fix_boundary(), rewrites to a tmp file. */
373 char *input_filename
= add ((*ctp
)->c_file
, NULL
);
374 int modify_inplace
= 0;
375 int message_mods
= 0;
378 if (outfile
== NULL
) {
381 if ((*ctp
)->c_file
) {
383 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
384 adios (NULL
, "unable to create temporary file in %s",
387 outfile
= add (tempfile
, NULL
);
389 adios (NULL
, "missing both input and output filenames\n");
393 reverse_alternative_parts (*ctp
);
394 if (status
== OK
&& fx
->fixboundary
) {
395 status
= fix_boundary (ctp
, &message_mods
);
397 if (status
== OK
&& fx
->fixcte
) {
398 status
= fix_multipart_cte (*ctp
, &message_mods
);
400 if (status
== OK
&& fx
->reformat
) {
402 ensure_text_plain (ctp
, NULL
, &message_mods
, fx
->replacetextplain
);
404 if (status
== OK
&& fx
->decodetext
) {
405 status
= decode_text_parts (*ctp
, fx
->decodetext
, &message_mods
);
407 if (status
== OK
&& fx
->textcharset
!= NULL
) {
408 status
= convert_charsets (*ctp
, fx
->textcharset
, &message_mods
);
411 if (! (*ctp
)->c_umask
) {
412 /* Set the umask for the contents file. This currently
413 isn't used but just in case it is in the future. */
416 if (stat ((*ctp
)->c_file
, &st
) != NOTOK
) {
417 (*ctp
)->c_umask
= ~(st
.st_mode
& 0777);
419 (*ctp
)->c_umask
= ~m_gmprot();
424 * Write the content to a file
427 status
= write_content (*ctp
, input_filename
, outfile
, modify_inplace
,
429 } else if (! modify_inplace
) {
430 /* Something went wrong. Output might be expected, such
431 as if this were run as a filter. Just copy the input
433 int in
= open (input_filename
, O_RDONLY
);
434 int out
= strcmp (outfile
, "-")
435 ? open (outfile
, O_WRONLY
| O_CREAT
, m_gmprot ())
438 if (in
!= -1 && out
!= -1) {
439 cpydata (in
, out
, input_filename
, outfile
);
448 if (modify_inplace
) {
449 if (status
!= OK
) { (void) m_unlink (outfile
); }
454 free (input_filename
);
461 fix_boundary (CT
*ct
, int *message_mods
) {
462 struct multipart
*mp
;
465 if (bogus_mp_content
) {
466 mp
= (struct multipart
*) (*ct
)->c_ctparams
;
469 * 1) Get boundary at end of part.
470 * 2) Get boundary at beginning of part and compare to the end-of-part
472 * 3) Write out contents of ct to tmp file, replacing boundary in
473 * header with boundary from part. Set c_unlink to 1.
475 * 5) Call parse_mime() on the tmp file, replacing ct.
478 if (mp
&& mp
->mp_start
) {
481 if (get_multipart_boundary (*ct
, &part_boundary
) == OK
) {
484 if ((fixed
= m_mktemp2 (NULL
, invo_name
, NULL
, &(*ct
)->c_fp
))) {
485 if (replace_boundary (*ct
, fixed
, part_boundary
) == OK
) {
486 char *filename
= add ((*ct
)->c_file
, NULL
);
489 if ((*ct
= parse_mime (fixed
))) {
494 report (NULL
, NULL
, filename
,
495 "fix multipart boundary");
500 advise (NULL
, "unable to replace broken boundary");
504 advise (NULL
, "unable to create temporary file in %s",
509 free (part_boundary
);
519 get_multipart_boundary (CT ct
, char **part_boundary
) {
521 char *end_boundary
= NULL
;
522 off_t begin
= (off_t
) ct
->c_end
> (off_t
) (ct
->c_begin
+ sizeof buffer
)
523 ? (off_t
) (ct
->c_end
- sizeof buffer
)
524 : (off_t
) ct
->c_begin
;
528 /* This will fail if the boundary spans fread() calls. BUFSIZ should
529 be big enough, even if it's just 1024, to make that unlikely. */
531 /* free_content() will close ct->c_fp. */
532 if (! ct
->c_fp
&& (ct
->c_fp
= fopen (ct
->c_file
, "r")) == NULL
) {
533 advise (ct
->c_file
, "unable to open for reading");
537 /* Get boundary at end of multipart. */
538 while (begin
>= (off_t
) ct
->c_begin
) {
539 fseeko (ct
->c_fp
, begin
, SEEK_SET
);
540 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
541 char *cp
= rfind_str (buffer
, bytes_read
, "--");
546 /* Trim off trailing "--" and anything beyond. */
548 if ((end
= rfind_str (buffer
, cp
- buffer
, "\n"))) {
549 if (strlen (end
) > 3 && *end
++ == '\n' &&
550 *end
++ == '-' && *end
++ == '-') {
551 end_boundary
= add (end
, NULL
);
558 if (! end_boundary
&& begin
> (off_t
) (ct
->c_begin
+ sizeof buffer
)) {
559 begin
-= sizeof buffer
;
565 /* Get boundary at beginning of multipart. */
567 fseeko (ct
->c_fp
, ct
->c_begin
, SEEK_SET
);
568 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, ct
->c_fp
)) > 0) {
569 if (bytes_read
>= strlen (end_boundary
)) {
570 char *cp
= find_str (buffer
, bytes_read
, end_boundary
);
572 if (cp
&& cp
- buffer
>= 2 && *--cp
== '-' &&
573 *--cp
== '-' && (cp
> buffer
&& *--cp
== '\n')) {
578 /* The start and end boundaries didn't match, or the
579 start boundary doesn't begin with "\n--" (or "--"
580 if at the beginning of buffer). Keep trying. */
589 *part_boundary
= end_boundary
;
591 *part_boundary
= NULL
;
599 /* Open and copy ct->c_file to file, replacing the multipart boundary. */
601 replace_boundary (CT ct
, char *file
, char *boundary
) {
604 char buf
[BUFSIZ
], name
[NAMESZ
];
606 m_getfld_state_t gstate
= 0;
609 if (ct
->c_file
== NULL
) {
610 advise (NULL
, "missing input filename");
614 if ((fpin
= fopen (ct
->c_file
, "r")) == NULL
) {
615 advise (ct
->c_file
, "unable to open for reading");
619 if ((fpout
= fopen (file
, "w")) == NULL
) {
621 advise (file
, "unable to open for writing");
625 for (compnum
= 1;;) {
626 int bufsz
= (int) sizeof buf
;
628 switch (state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
)) {
633 /* get copies of the buffers */
634 np
= add (name
, NULL
);
635 vp
= add (buf
, NULL
);
637 /* if necessary, get rest of field */
638 while (state
== FLDPLUS
) {
640 state
= m_getfld (&gstate
, name
, buf
, &bufsz
, fpin
);
641 vp
= add (buf
, vp
); /* add to previous value */
644 if (strcasecmp (TYPE_FIELD
, np
)) {
645 fprintf (fpout
, "%s:%s", np
, vp
);
647 char *new_ctline
, *new_params
;
649 replace_param(&ct
->c_ctinfo
.ci_first_pm
,
650 &ct
->c_ctinfo
.ci_last_pm
, "boundary",
653 new_ctline
= concat(" ", ct
->c_ctinfo
.ci_type
, "/",
654 ct
->c_ctinfo
.ci_subtype
, NULL
);
655 new_params
= output_params(strlen(TYPE_FIELD
) +
656 strlen(new_ctline
) + 1,
657 ct
->c_ctinfo
.ci_first_pm
, NULL
, 0);
658 fprintf (fpout
, "%s:%s%s\n", np
, new_ctline
,
659 new_params
? new_params
: "");
672 /* buf will have a terminating NULL, skip it. */
673 fwrite (buf
, 1, bufsz
-1, fpout
);
681 advise (NULL
, "message format error in component #%d", compnum
);
686 advise (NULL
, "getfld() returned %d", state
);
694 m_getfld_state_destroy (&gstate
);
703 fix_multipart_cte (CT ct
, int *message_mods
) {
706 if (ct
->c_type
== CT_MULTIPART
) {
710 if (ct
->c_encoding
!= CE_7BIT
&& ct
->c_encoding
!= CE_8BIT
&&
711 ct
->c_encoding
!= CE_BINARY
) {
714 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
715 char *name
= hf
->name
;
716 for (; *name
&& isspace ((unsigned char) *name
); ++name
) {
720 if (! strncasecmp (name
, ENCODING_FIELD
,
721 strlen (ENCODING_FIELD
))) {
722 char *prefix
= "Nmh-REPLACED-INVALID-";
723 HF h
= mh_xmalloc (sizeof *h
);
725 h
->name
= add (hf
->name
, NULL
);
726 h
->hf_encoding
= hf
->hf_encoding
;
730 /* Retain old header but prefix its name. */
732 hf
->name
= concat (prefix
, h
->name
, NULL
);
736 char *encoding
= cpytrim (hf
->value
);
737 report (NULL
, ct
->c_partno
, ct
->c_file
,
738 "replace Content-Transfer-Encoding of %s "
739 "with 8 bit", encoding
);
743 h
->value
= add (" 8bit\n", NULL
);
745 /* Don't need to warn for multiple C-T-E header
746 fields, parse_mime() already does that. But
747 if there are any, fix them all as necessary. */
752 set_ce (ct
, CE_8BIT
);
755 m
= (struct multipart
*) ct
->c_ctparams
;
756 for (part
= m
->mp_parts
; part
; part
= part
->mp_next
) {
757 if (fix_multipart_cte (part
->mp_part
, message_mods
) != OK
) {
769 set_ce (CT ct
, int encoding
) {
770 const char *ce
= ce_str (encoding
);
771 const struct str2init
*ctinit
= get_ce_method (ce
);
774 char *cte
= concat (" ", ce
, "\n", NULL
);
777 /* Decoded contents might be in ct->c_cefile.ce_file, if the
778 caller is decode_text_parts (). Save because we'll
780 struct cefile decoded_content_info
= ct
->c_cefile
;
782 ct
->c_encoding
= encoding
;
784 ct
->c_ctinitfnx
= ctinit
->si_init
;
785 /* This will assign ct->c_cefile with an all-0 struct, which
787 (*ctinit
->si_init
) (ct
);
788 /* After returning, the caller should set
789 ct->c_cefile.ce_file to the name of the file containing
792 /* Restore the cefile. */
793 ct
->c_cefile
= decoded_content_info
;
795 /* Update/add Content-Transfer-Encoding header field. */
796 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
797 if (! strcasecmp (ENCODING_FIELD
, hf
->name
)) {
804 add_header (ct
, add (ENCODING_FIELD
, NULL
), cte
);
807 /* Update c_celine. It's used only by mhlist -debug. */
809 ct
->c_celine
= add (cte
, NULL
);
818 /* Make sure each text part has a corresponding text/plain part. */
820 ensure_text_plain (CT
*ct
, CT parent
, int *message_mods
, int replacetextplain
) {
823 switch ((*ct
)->c_type
) {
825 int has_text_plain
= 0;
827 /* Nothing to do for text/plain. */
828 if ((*ct
)->c_subtype
== TEXT_PLAIN
) { return OK
; }
830 if (parent
&& parent
->c_type
== CT_MULTIPART
&&
831 parent
->c_subtype
== MULTI_ALTERNATE
) {
832 struct multipart
*mp
= (struct multipart
*) parent
->c_ctparams
;
833 struct part
*part
, *prev
;
834 int new_subpart_number
= 1;
836 /* See if there is a sibling text/plain. */
837 for (prev
= part
= mp
->mp_parts
; part
; part
= part
->mp_next
) {
838 ++new_subpart_number
;
839 if (part
->mp_part
->c_type
== CT_TEXT
&&
840 part
->mp_part
->c_subtype
== TEXT_PLAIN
) {
841 if (replacetextplain
) {
842 struct part
*old_part
;
843 if (part
== mp
->mp_parts
) {
844 old_part
= mp
->mp_parts
;
845 mp
->mp_parts
= part
->mp_next
;
847 old_part
= prev
->mp_next
;
848 prev
->mp_next
= part
->mp_next
;
851 report (NULL
, parent
->c_partno
, parent
->c_file
,
852 "remove text/plain part %s",
853 old_part
->mp_part
->c_partno
);
855 free_content (old_part
->mp_part
);
865 if (! has_text_plain
) {
866 /* Parent is a multipart/alternative. Insert a new
867 text/plain subpart. */
868 struct part
*new_part
= mh_xmalloc (sizeof *new_part
);
870 if ((new_part
->mp_part
= build_text_plain_part (*ct
))) {
872 snprintf (buffer
, sizeof buffer
, "%d", new_subpart_number
);
874 new_part
->mp_next
= mp
->mp_parts
;
875 mp
->mp_parts
= new_part
;
876 new_part
->mp_part
->c_partno
=
877 concat (parent
->c_partno
? parent
->c_partno
: "1", ".",
882 report (NULL
, parent
->c_partno
, parent
->c_file
,
883 "insert text/plain part");
886 free_content (new_part
->mp_part
);
892 /* Slip new text/plain part into a new multipart/alternative. */
893 CT tp_part
= build_text_plain_part (*ct
);
896 CT mp_alt
= build_multipart_alt (*ct
, tp_part
, CT_MULTIPART
,
899 struct multipart
*mp
=
900 (struct multipart
*) mp_alt
->c_ctparams
;
902 if (mp
&& mp
->mp_parts
) {
903 mp
->mp_parts
->mp_part
= tp_part
;
904 /* Make the new multipart/alternative the parent. */
909 report (NULL
, (*ct
)->c_partno
, (*ct
)->c_file
,
910 "insert text/plain part");
913 free_content (tp_part
);
914 free_content (mp_alt
);
928 struct multipart
*mp
= (struct multipart
*) (*ct
)->c_ctparams
;
931 for (part
= mp
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
932 if ((*ct
)->c_type
== CT_MULTIPART
) {
933 status
= ensure_text_plain (&part
->mp_part
, *ct
, message_mods
,
941 if ((*ct
)->c_subtype
== MESSAGE_EXTERNAL
) {
944 e
= (struct exbody
*) (*ct
)->c_ctparams
;
945 status
= ensure_text_plain (&e
->eb_content
, *ct
, message_mods
,
956 build_text_plain_part (CT encoded_part
) {
957 CT tp_part
= divide_part (encoded_part
);
958 char *tmp_plain_file
= NULL
;
960 if (decode_part (tp_part
) == OK
) {
961 /* Now, tp_part->c_cefile.ce_file is the name of the tmp file that
962 contains the decoded contents. And the decoding function, such
963 as openQuoted, will have set ...->ce_unlink to 1 so that it will
964 be unlinked by free_content (). */
967 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
968 advise (NULL
, "unable to create temporary file in %s",
971 tmp_plain_file
= add (tempfile
, NULL
);
972 if (reformat_part (tp_part
, tmp_plain_file
,
973 tp_part
->c_ctinfo
.ci_type
,
974 tp_part
->c_ctinfo
.ci_subtype
,
975 tp_part
->c_type
) == OK
) {
980 free_content (tp_part
);
981 (void) m_unlink (tmp_plain_file
);
982 free (tmp_plain_file
);
989 divide_part (CT ct
) {
992 if ((new_part
= (CT
) calloc (1, sizeof *new_part
)) == NULL
)
993 adios (NULL
, "out of memory");
995 /* Just copy over what is needed for decoding. c_vrsn and
996 c_celine aren't necessary. */
997 new_part
->c_file
= add (ct
->c_file
, NULL
);
998 new_part
->c_begin
= ct
->c_begin
;
999 new_part
->c_end
= ct
->c_end
;
1000 copy_ctinfo (&new_part
->c_ctinfo
, &ct
->c_ctinfo
);
1001 new_part
->c_type
= ct
->c_type
;
1002 new_part
->c_cefile
= ct
->c_cefile
;
1003 new_part
->c_encoding
= ct
->c_encoding
;
1004 new_part
->c_ctinitfnx
= ct
->c_ctinitfnx
;
1005 new_part
->c_ceopenfnx
= ct
->c_ceopenfnx
;
1006 new_part
->c_ceclosefnx
= ct
->c_ceclosefnx
;
1007 new_part
->c_cesizefnx
= ct
->c_cesizefnx
;
1009 /* c_ctline is used by reformat__part(), so it can preserve
1010 anything after the type/subtype. */
1011 new_part
->c_ctline
= add (ct
->c_ctline
, NULL
);
1018 copy_ctinfo (CI dest
, CI src
) {
1021 dest
->ci_type
= src
->ci_type
? add (src
->ci_type
, NULL
) : NULL
;
1022 dest
->ci_subtype
= src
->ci_subtype
? add (src
->ci_subtype
, NULL
) : NULL
;
1024 for (s_pm
= src
->ci_first_pm
; s_pm
; s_pm
= s_pm
->pm_next
) {
1025 d_pm
= add_param(&dest
->ci_first_pm
, &dest
->ci_last_pm
, s_pm
->pm_name
,
1027 if (s_pm
->pm_charset
)
1028 d_pm
->pm_charset
= getcpy(s_pm
->pm_charset
);
1030 d_pm
->pm_lang
= getcpy(s_pm
->pm_lang
);
1033 dest
->ci_comment
= src
->ci_comment
? add (src
->ci_comment
, NULL
) : NULL
;
1034 dest
->ci_magic
= src
->ci_magic
? add (src
->ci_magic
, NULL
) : NULL
;
1039 decode_part (CT ct
) {
1044 if ((tempfile
= m_mktemp2 (NULL
, invo_name
, NULL
, NULL
)) == NULL
) {
1045 adios (NULL
, "unable to create temporary file in %s", get_temp_dir());
1047 tmp_decoded
= add (tempfile
, NULL
);
1048 /* The following call will load ct->c_cefile.ce_file with the tmp
1049 filename of the decoded content. tmp_decoded will contain the
1050 encoded output, get rid of that. */
1051 status
= output_message (ct
, tmp_decoded
);
1052 (void) m_unlink (tmp_decoded
);
1059 /* Some of the arguments aren't really needed now, but maybe will
1060 be in the future for other than text types. */
1062 reformat_part (CT ct
, char *file
, char *type
, char *subtype
, int c_type
) {
1063 int output_subtype
, output_encoding
;
1067 /* Hacky: this redirects the output from whatever command is used
1068 to show the part to a file. So, the user can't have any output
1069 redirection in that command.
1070 Could show_multi() in mhshowsbr.c avoid this? */
1072 /* Check for invo_name-format-type/subtype. */
1073 if ((cf
= context_find_by_type ("format", type
, subtype
)) == NULL
) {
1075 advise (NULL
, "Don't know how to convert %s, there is no "
1076 "%s-format-%s/%s profile entry",
1077 ct
->c_file
, invo_name
, type
, subtype
);
1081 if (strchr (cf
, '>')) {
1082 advise (NULL
, "'>' prohibited in \"%s\",\nplease fix your "
1083 "%s-format-%s/%s profile entry", cf
, invo_name
, type
,
1084 subtype
? subtype
: "");
1090 cp
= concat (cf
, " >", file
, NULL
);
1091 status
= show_content_aux (ct
, 0, cp
, NULL
, NULL
);
1094 /* Unlink decoded content tmp file and free its filename to avoid
1095 leaks. The file stream should already have been closed. */
1096 if (ct
->c_cefile
.ce_unlink
) {
1097 (void) m_unlink (ct
->c_cefile
.ce_file
);
1098 free (ct
->c_cefile
.ce_file
);
1099 ct
->c_cefile
.ce_file
= NULL
;
1100 ct
->c_cefile
.ce_unlink
= 0;
1103 if (c_type
== CT_TEXT
) {
1104 output_subtype
= TEXT_PLAIN
;
1106 /* Set subtype to 0, which is always an UNKNOWN subtype. */
1109 output_encoding
= charset_encoding (ct
);
1111 if (set_ct_type (ct
, c_type
, output_subtype
, output_encoding
) == OK
) {
1112 ct
->c_cefile
.ce_file
= file
;
1113 ct
->c_cefile
.ce_unlink
= 1;
1115 ct
->c_cefile
.ce_unlink
= 0;
1123 /* Identifies 7bit or 8bit content based on charset. */
1125 charset_encoding (CT ct
) {
1127 strcasecmp (content_charset (ct
), "US-ASCII") ? CE_8BIT
: CE_7BIT
;
1134 build_multipart_alt (CT first_alt
, CT new_part
, int type
, int subtype
) {
1135 char *boundary_prefix
= "----=_nmh-multipart";
1136 char *boundary
= concat (boundary_prefix
, first_alt
->c_partno
, NULL
);
1137 char *boundary_indicator
= "; boundary=";
1138 char *typename
, *subtypename
, *name
;
1141 struct multipart
*m
;
1142 const struct str2init
*ctinit
;
1144 if ((ct
= (CT
) calloc (1, sizeof *ct
)) == NULL
)
1145 adios (NULL
, "out of memory");
1147 /* Set up the multipart/alternative part. These fields of *ct were
1148 initialized to 0 by calloc():
1149 c_fp, c_unlink, c_begin, c_end,
1150 c_vrsn, c_ctline, c_celine,
1151 c_id, c_descr, c_dispo, c_partno,
1152 c_ctinfo.ci_comment, c_ctinfo.ci_magic,
1153 c_cefile, c_encoding,
1154 c_digested, c_digest[16], c_ctexbody,
1155 c_ctinitfnx, c_ceopenfnx, c_ceclosefnx, c_cesizefnx,
1157 c_showproc, c_termproc, c_storeproc, c_storage, c_folder
1160 ct
->c_file
= add (first_alt
->c_file
, NULL
);
1162 ct
->c_subtype
= subtype
;
1164 ctinit
= get_ct_init (ct
->c_type
);
1166 typename
= ct_type_str (type
);
1167 subtypename
= ct_subtype_str (type
, subtype
);
1171 int found_boundary
= 1;
1173 while (found_boundary
&& serial
< 1000000) {
1176 /* Ensure that the boundary doesn't appear in the decoded
1178 if (new_part
->c_cefile
.ce_file
) {
1179 if ((found_boundary
=
1180 boundary_in_content (&new_part
->c_cefile
.ce_fp
,
1181 new_part
->c_cefile
.ce_file
,
1188 /* Ensure that the boundary doesn't appear in the encoded
1190 if (! found_boundary
&& new_part
->c_file
) {
1191 if ((found_boundary
= boundary_in_content (&new_part
->c_fp
,
1199 if (found_boundary
) {
1200 /* Try a slightly different boundary. */
1205 snprintf (buffer2
, sizeof buffer2
, "%d", serial
);
1207 concat (boundary_prefix
,
1208 first_alt
->c_partno
? first_alt
->c_partno
: "",
1209 "-", buffer2
, NULL
);
1213 if (found_boundary
) {
1214 advise (NULL
, "giving up trying to find a unique boundary");
1220 name
= concat (" ", typename
, "/", subtypename
, boundary_indicator
, "\"",
1221 boundary
, "\"", NULL
);
1223 /* Load c_first_hf and c_last_hf. */
1224 transfer_noncontent_headers (first_alt
, ct
);
1225 add_header (ct
, add (TYPE_FIELD
, NULL
), concat (name
, "\n", NULL
));
1228 /* Load c_partno. */
1229 if (first_alt
->c_partno
) {
1230 ct
->c_partno
= add (first_alt
->c_partno
, NULL
);
1231 free (first_alt
->c_partno
);
1232 first_alt
->c_partno
= concat (ct
->c_partno
, ".1", NULL
);
1233 new_part
->c_partno
= concat (ct
->c_partno
, ".2", NULL
);
1235 first_alt
->c_partno
= add ("1", NULL
);
1236 new_part
->c_partno
= add ("2", NULL
);
1240 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1241 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1244 add_param(&ct
->c_ctinfo
.ci_first_pm
, &ct
->c_ctinfo
.ci_last_pm
,
1245 "boundary", boundary
, 0);
1247 p
= (struct part
*) mh_xmalloc (sizeof *p
);
1248 p
->mp_next
= (struct part
*) mh_xmalloc (sizeof *p
->mp_next
);
1249 p
->mp_next
->mp_next
= NULL
;
1250 p
->mp_next
->mp_part
= first_alt
;
1252 if ((m
= (struct multipart
*) calloc (1, sizeof (struct multipart
))) ==
1254 adios (NULL
, "out of memory");
1255 m
->mp_start
= concat (boundary
, "\n", NULL
);
1256 m
->mp_stop
= concat (boundary
, "--\n", NULL
);
1258 ct
->c_ctparams
= (void *) m
;
1266 /* Check that the boundary does not appear in the content. */
1268 boundary_in_content (FILE **fp
, char *file
, const char *boundary
) {
1269 char buffer
[BUFSIZ
];
1271 int found_boundary
= 0;
1273 /* free_content() will close *fp if we fopen it here. */
1274 if (! *fp
&& (*fp
= fopen (file
, "r")) == NULL
) {
1275 advise (file
, "unable to open %s for reading", file
);
1279 fseeko (*fp
, 0L, SEEK_SET
);
1280 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) > 0) {
1281 if (find_str (buffer
, bytes_read
, boundary
)) {
1287 return found_boundary
;
1291 /* Remove all non-Content headers. */
1293 transfer_noncontent_headers (CT old
, CT
new) {
1296 hp_prev
= hp
= old
->c_first_hf
;
1300 if (strncasecmp (XXX_FIELD_PRF
, hp
->name
, strlen (XXX_FIELD_PRF
))) {
1301 if (hp
== old
->c_last_hf
) {
1302 if (hp
== old
->c_first_hf
) {
1303 old
->c_last_hf
= old
->c_first_hf
= NULL
;
1305 hp_prev
->next
= NULL
;
1306 old
->c_last_hf
= hp_prev
;
1309 if (hp
== old
->c_first_hf
) {
1310 old
->c_first_hf
= next
;
1312 hp_prev
->next
= next
;
1316 /* Put node hp in the new CT. */
1317 if (new->c_first_hf
== NULL
) {
1318 new->c_first_hf
= hp
;
1320 new->c_last_hf
->next
= hp
;
1322 new->c_last_hf
= hp
;
1324 /* A Content- header, leave in old. */
1334 set_ct_type (CT ct
, int type
, int subtype
, int encoding
) {
1335 char *typename
= ct_type_str (type
);
1336 char *subtypename
= ct_subtype_str (type
, subtype
);
1337 /* E.g, " text/plain" */
1338 char *type_subtypename
= concat (" ", typename
, "/", subtypename
, NULL
);
1339 /* E.g, " text/plain\n" */
1340 char *name_plus_nl
= concat (type_subtypename
, "\n", NULL
);
1341 int found_content_type
= 0;
1343 const char *cp
= NULL
;
1347 /* Update/add Content-Type header field. */
1348 for (hf
= ct
->c_first_hf
; hf
; hf
= hf
->next
) {
1349 if (! strcasecmp (TYPE_FIELD
, hf
->name
)) {
1350 found_content_type
= 1;
1352 hf
->value
= (cp
= strchr (ct
->c_ctline
, ';'))
1353 ? concat (type_subtypename
, cp
, "\n", NULL
)
1354 : add (name_plus_nl
, NULL
);
1357 if (! found_content_type
) {
1358 add_header (ct
, add (TYPE_FIELD
, NULL
),
1359 (cp
= strchr (ct
->c_ctline
, ';'))
1360 ? concat (type_subtypename
, cp
, "\n", NULL
)
1361 : add (name_plus_nl
, NULL
));
1364 /* Some of these might not be used, but set them anyway. */
1366 ? concat (type_subtypename
, cp
, NULL
)
1367 : concat (type_subtypename
, NULL
);
1368 free (ct
->c_ctline
);
1369 ct
->c_ctline
= ctline
;
1370 /* Leave other ctinfo members as they were. */
1371 free (ct
->c_ctinfo
.ci_type
);
1372 ct
->c_ctinfo
.ci_type
= add (typename
, NULL
);
1373 free (ct
->c_ctinfo
.ci_subtype
);
1374 ct
->c_ctinfo
.ci_subtype
= add (subtypename
, NULL
);
1376 ct
->c_subtype
= subtype
;
1378 free (name_plus_nl
);
1379 free (type_subtypename
);
1381 status
= set_ce (ct
, encoding
);
1388 decode_text_parts (CT ct
, int encoding
, int *message_mods
) {
1391 switch (ct
->c_type
) {
1393 switch (ct
->c_encoding
) {
1398 if (decode_part (ct
) == OK
&& ct
->c_cefile
.ce_file
) {
1399 const char *reason
= NULL
;
1401 if ((ct_encoding
= content_encoding (ct
, &reason
)) == CE_BINARY
1402 && encoding
!= CE_BINARY
) {
1403 /* The decoding isn't acceptable so discard it.
1404 Leave status as OK to allow other transformations. */
1406 report (NULL
, ct
->c_partno
, ct
->c_file
,
1407 "will not decode%s because it is binary (%s)",
1409 : ct
->c_ctline
? ct
->c_ctline
1413 (void) m_unlink (ct
->c_cefile
.ce_file
);
1414 free (ct
->c_cefile
.ce_file
);
1415 ct
->c_cefile
.ce_file
= NULL
;
1416 } else if (ct
->c_encoding
== CE_QUOTED
&&
1417 ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
) {
1418 /* The decoding isn't acceptable so discard it.
1419 Leave status as OK to allow other transformations. */
1421 report (NULL
, ct
->c_partno
, ct
->c_file
,
1422 "will not decode%s because it is 8bit",
1424 : ct
->c_ctline
? ct
->c_ctline
1427 (void) m_unlink (ct
->c_cefile
.ce_file
);
1428 free (ct
->c_cefile
.ce_file
);
1429 ct
->c_cefile
.ce_file
= NULL
;
1432 if (ct_encoding
== CE_BINARY
)
1434 else if (ct_encoding
== CE_8BIT
&& encoding
== CE_7BIT
)
1437 enc
= charset_encoding (ct
);
1438 if (set_ce (ct
, enc
) == OK
) {
1441 report (NULL
, ct
->c_partno
, ct
->c_file
, "decode%s",
1442 ct
->c_ctline
? ct
->c_ctline
: "");
1444 strip_crs (ct
, message_mods
);
1456 strip_crs (ct
, message_mods
);
1464 case CT_MULTIPART
: {
1465 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1468 /* Should check to see if the body for this part is encoded?
1469 For now, it gets passed along as-is by InitMultiPart(). */
1470 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1471 status
= decode_text_parts (part
->mp_part
, encoding
, message_mods
);
1477 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1480 e
= (struct exbody
*) ct
->c_ctparams
;
1481 status
= decode_text_parts (e
->eb_content
, encoding
, message_mods
);
1493 /* See if the decoded content is 7bit, 8bit, or binary. It's binary
1494 if it has any NUL characters, a CR not followed by a LF, or lines
1495 greater than 998 characters in length. If binary, reason is set
1496 to a string explaining why. */
1498 content_encoding (CT ct
, const char **reason
) {
1499 CE ce
= &ct
->c_cefile
;
1500 int encoding
= CE_7BIT
;
1503 size_t line_len
= 0;
1504 char buffer
[BUFSIZ
];
1507 if (! ce
->ce_fp
&& (ce
->ce_fp
= fopen (ce
->ce_file
, "r")) == NULL
) {
1508 advise (ce
->ce_file
, "unable to open for reading");
1512 fseeko (ce
->ce_fp
, 0L, SEEK_SET
);
1513 while (encoding
!= CE_BINARY
&&
1514 (inbytes
= fread (buffer
, 1, sizeof buffer
, ce
->ce_fp
)) > 0) {
1517 int last_char_was_cr
= 0;
1519 for (i
= 0, cp
= buffer
; i
< inbytes
; ++i
, ++cp
) {
1520 if (*cp
== '\0' || ++line_len
> 998 ||
1521 (*cp
!= '\n' && last_char_was_cr
)) {
1522 encoding
= CE_BINARY
;
1524 *reason
= "null character";
1525 } else if (line_len
> 998) {
1526 *reason
= "line length > 998";
1527 } else if (*cp
!= '\n' && last_char_was_cr
) {
1528 *reason
= "CR not followed by LF";
1530 /* Should not reach this. */
1534 } else if (*cp
== '\n') {
1536 } else if (! isascii ((unsigned char) *cp
)) {
1540 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1546 } /* else should never happen */
1553 strip_crs (CT ct
, int *message_mods
) {
1554 char *charset
= content_charset (ct
);
1557 /* Only strip carriage returns if content is ASCII or another
1558 charset that has the same readily recognizable CR followed by a
1559 LF. We can include UTF-8 here because if the high-order bit of
1560 a UTF-8 byte is 0, then it must be a single-byte ASCII
1562 if (! strcasecmp (charset
, "US-ASCII") ||
1563 ! strcasecmp (charset
, "UTF-8") ||
1564 ! strncasecmp (charset
, "ISO-8859-", 9) ||
1565 ! strncasecmp (charset
, "WINDOWS-12", 10)) {
1571 int opened_input_file
= 0;
1573 if (ct
->c_cefile
.ce_file
) {
1574 file
= &ct
->c_cefile
.ce_file
;
1575 fp
= &ct
->c_cefile
.ce_fp
;
1577 } else if (ct
->c_file
) {
1580 begin
= (size_t) ct
->c_begin
;
1581 end
= (size_t) ct
->c_end
;
1582 } /* else don't know where the content is */
1584 if (file
&& *file
&& fp
) {
1586 if ((*fp
= fopen (*file
, "r")) == NULL
) {
1587 advise (*file
, "unable to open for reading");
1590 opened_input_file
= 1;
1596 char buffer
[BUFSIZ
];
1598 size_t bytes_to_read
=
1599 end
> 0 && end
> begin
? end
- begin
: sizeof buffer
;
1601 fseeko (*fp
, begin
, SEEK_SET
);
1602 while ((bytes_read
= fread (buffer
, 1,
1603 min (bytes_to_read
, sizeof buffer
),
1605 /* Look for CR followed by a LF. This is supposed to
1606 be text so there should be LF's. If not, don't
1607 modify the content. */
1610 int last_char_was_cr
= 0;
1612 if (end
> 0) { bytes_to_read
-= bytes_read
; }
1614 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1615 if (*cp
== '\n' && last_char_was_cr
) {
1620 last_char_was_cr
= *cp
== '\r' ? 1 : 0;
1626 char *stripped_content_file
;
1627 char *tempfile
= m_mktemp2 (NULL
, invo_name
, &fd
, NULL
);
1629 if (tempfile
== NULL
) {
1630 adios (NULL
, "unable to create temporary file in %s",
1633 stripped_content_file
= add (tempfile
, NULL
);
1635 /* Strip each CR before a LF from the content. */
1636 fseeko (*fp
, begin
, SEEK_SET
);
1637 while ((bytes_read
= fread (buffer
, 1, sizeof buffer
, *fp
)) >
1641 int last_char_was_cr
= 0;
1643 for (i
= 0, cp
= buffer
; i
< bytes_read
; ++i
, ++cp
) {
1645 last_char_was_cr
= 1;
1646 } else if (last_char_was_cr
) {
1648 if (write (fd
, "\r", 1) < 0) {
1649 advise (tempfile
, "CR write");
1652 if (write (fd
, cp
, 1) < 0) {
1653 advise (tempfile
, "write");
1655 last_char_was_cr
= 0;
1657 if (write (fd
, cp
, 1) < 0) {
1658 advise (tempfile
, "write");
1660 last_char_was_cr
= 0;
1666 admonish (NULL
, "unable to write temporary file %s",
1667 stripped_content_file
);
1668 (void) m_unlink (stripped_content_file
);
1671 /* Replace the decoded file with the converted one. */
1672 if (ct
->c_cefile
.ce_file
) {
1673 if (ct
->c_cefile
.ce_unlink
) {
1674 (void) m_unlink (ct
->c_cefile
.ce_file
);
1676 free (ct
->c_cefile
.ce_file
);
1678 ct
->c_cefile
.ce_file
= stripped_content_file
;
1679 ct
->c_cefile
.ce_unlink
= 1;
1683 report (NULL
, ct
->c_partno
,
1684 begin
== 0 && end
== 0 ? "" : *file
,
1690 if (opened_input_file
) {
1702 convert_charsets (CT ct
, char *dest_charset
, int *message_mods
) {
1705 switch (ct
->c_type
) {
1707 if (ct
->c_subtype
== TEXT_PLAIN
) {
1708 status
= convert_charset (ct
, dest_charset
, message_mods
);
1711 report (NULL
, ct
->c_partno
, ct
->c_file
,
1713 content_charset(ct
), dest_charset
);
1716 report ("iconv", ct
->c_partno
, ct
->c_file
,
1717 "failed to convert %s to %s",
1718 content_charset(ct
), dest_charset
);
1723 case CT_MULTIPART
: {
1724 struct multipart
*m
= (struct multipart
*) ct
->c_ctparams
;
1727 /* Should check to see if the body for this part is encoded?
1728 For now, it gets passed along as-is by InitMultiPart(). */
1729 for (part
= m
->mp_parts
; status
== OK
&& part
; part
= part
->mp_next
) {
1731 convert_charsets (part
->mp_part
, dest_charset
, message_mods
);
1737 if (ct
->c_subtype
== MESSAGE_EXTERNAL
) {
1740 e
= (struct exbody
*) ct
->c_ctparams
;
1742 convert_charsets (e
->eb_content
, dest_charset
, message_mods
);
1755 write_content (CT ct
, char *input_filename
, char *outfile
, int modify_inplace
,
1759 if (modify_inplace
) {
1760 if (message_mods
> 0) {
1761 if ((status
= output_message (ct
, outfile
)) == OK
) {
1762 char *infile
= input_filename
1763 ? add (input_filename
, NULL
)
1764 : add (ct
->c_file
? ct
->c_file
: "-", NULL
);
1766 if (remove_file (infile
) == OK
) {
1767 if (rename (outfile
, infile
)) {
1768 /* Rename didn't work, possibly because of an
1769 attempt to rename across filesystems. Try
1770 brute force copy. */
1771 int old
= open (outfile
, O_RDONLY
);
1773 open (infile
, O_WRONLY
| O_CREAT
, m_gmprot ());
1776 if (old
!= -1 && new != -1) {
1777 char buffer
[BUFSIZ
];
1779 while ((i
= read (old
, buffer
, sizeof buffer
)) >
1781 if (write (new, buffer
, i
) != i
) {
1787 if (new != -1) { close (new); }
1788 if (old
!= -1) { close (old
); }
1789 (void) m_unlink (outfile
);
1792 /* The -file argument processing used path() to
1793 expand filename to absolute path. */
1794 int file
= ct
->c_file
&& ct
->c_file
[0] == '/';
1796 admonish (NULL
, "unable to rename %s %s to %s",
1797 file
? "file" : "message", outfile
,
1803 admonish (NULL
, "unable to remove input file %s, "
1804 "not modifying it", infile
);
1805 (void) m_unlink (outfile
);
1814 /* No modifications and didn't need the tmp outfile. */
1815 (void) m_unlink (outfile
);
1818 /* Output is going to some file. Produce it whether or not
1819 there were modifications. */
1820 status
= output_message (ct
, outfile
);
1829 * If "rmmproc" is defined, call that to remove the file. Otherwise,
1830 * use the standard MH backup file.
1833 remove_file (char *file
) {
1835 char *rmm_command
= concat (rmmproc
, " ", file
, NULL
);
1836 int status
= system (rmm_command
);
1839 return WIFEXITED (status
) ? WEXITSTATUS (status
) : NOTOK
;
1841 /* This is OK for a non-message file, it still uses the
1842 BACKUP_PREFIX form. The backup file will be in the same
1843 directory as file. */
1844 return rename (file
, m_backup (file
));
1850 report (char *what
, char *partno
, char *filename
, char *message
, ...) {
1855 va_start (args
, message
);
1856 fmt
= concat (filename
, partno
? " part " : ", ",
1857 partno
? partno
: "", partno
? ", " : "", message
, NULL
);
1859 advertise (what
, NULL
, fmt
, args
);
1872 fprintf (stderr
, "\n");