*
* This code is Copyright (c) 2002 and 2013, by the authors of nmh.
* See the COPYRIGHT file in the root directory of the nmh
*
* This code is Copyright (c) 2002 and 2013, by the authors of nmh.
* See the COPYRIGHT file in the root directory of the nmh
-/* mhparse.c */
-extern int skip_mp_cte_check; /* flag to InitMultiPart */
-extern int suppress_bogus_mp_content_warning; /* flag to InitMultiPart */
-extern int bogus_mp_content; /* flag from InitMultiPart */
-/* flags to/from parse_header_attrs */
-extern int suppress_extraneous_trailing_semicolon_warning;
-
-/* mhoutsbr.c */
-int output_message (CT, char *);
-
-/* mhmisc.c */
-void flush_errors (void);
-
-/* mhfree.c */
-extern CT *cts;
-
-int mhfixmsgsbr (CT *, char *, const fix_transformations *, char *);
+static int mhfixmsgsbr (CT *, char *, const fix_transformations *,
+ FILE **, char *, FILE **);
static int get_multipart_boundary (CT, char **);
static int replace_boundary (CT, char *, char *);
static int fix_types (CT, svector_t, int *);
static int get_multipart_boundary (CT, char **);
static int replace_boundary (CT, char *, char *);
static int fix_types (CT, svector_t, int *);
static int content_encoding (CT, const char **);
static int strip_crs (CT, int *);
static void update_cte (CT);
static int content_encoding (CT, const char **);
static int strip_crs (CT, int *);
static void update_cte (CT);
static int less_restrictive (int, int);
static int convert_charsets (CT, char *, int *);
static int fix_always (CT, int *);
static int fix_filename_param (char *, char *, PM *, PM *);
static int fix_filename_encoding (CT);
static int less_restrictive (int, int);
static int convert_charsets (CT, char *, int *);
static int fix_always (CT, int *);
static int fix_filename_param (char *, char *, PM *, PM *);
static int fix_filename_encoding (CT);
-static int write_content (CT, const char *, char *, int, int);
+static int write_content (CT, const char *, char *, FILE *, int, int);
static void set_text_ctparams(CT, char *, int);
static int remove_file (const char *);
static void set_text_ctparams(CT, char *, int);
static int remove_file (const char *);
-static void report (char *, char *, char *, char *, ...);
+static void report (char *, char *, char *, char *, ...)
+ CHECK_PRINTF(4, 5);
adios (NULL, "missing argument to %s", argp[-2]);
}
if (! strncasecmp (cp, "multipart/", 10) ||
adios (NULL, "missing argument to %s", argp[-2]);
}
if (! strncasecmp (cp, "multipart/", 10) ||
if (fx.fixtypes == NULL) { fx.fixtypes = svector_create (10); }
svector_push_back (fx.fixtypes, cp);
continue;
if (fx.fixtypes == NULL) { fx.fixtypes = svector_create (10); }
svector_push_back (fx.fixtypes, cp);
continue;
* Read the standard profile setup
*/
if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
* Read the standard profile setup
*/
if ((fp = fopen (cp = etcpath ("mhn.defaults"), "r"))) {
- readconfig ((struct node **) 0, fp, cp, 0);
+ readconfig(NULL, fp, cp, 0);
- suppress_bogus_mp_content_warning = skip_mp_cte_check = 1;
- suppress_extraneous_trailing_semicolon_warning = 1;
+ suppress_bogus_mp_content_warning = skip_mp_cte_check = true;
+ suppress_extraneous_trailing_semicolon_warning = true;
if (! context_find ("path")) {
free (path ("./", TFOLDER));
if (! context_find ("path")) {
free (path ("./", TFOLDER));
+ if (outfile) {
+ /* Open the outfile now, so we don't have to risk opening it
+ after running out of fds. */
+ if (strcmp (outfile, "-") == 0) {
+ outfp = stdout;
+ } else if ((outfp = fopen (outfile, "w")) == NULL) {
+ adios (outfile, "unable to open for writing");
+ }
+ }
+
- /* If there's an outfile, pass the input message unchanged, so the message won't
- get dropped from a pipeline. */
+ /* If there's an outfile, pass the input message unchanged, so the
+ message won't get dropped from a pipeline. */
- /* Something went wrong. Output might be expected, such as if this were run
- as a filter. Just copy the input to the output. */
- if (copy_input_to_output (file, outfile) != OK) {
- advise (NULL, "unable to copy message to %s, it might be lost\n", outfile);
+ /* Something went wrong. Output might be expected, such as if
+ this were run as a filter. Just copy the input to the
+ output. */
+ if ((infp = fopen (file, "r")) == NULL) {
+ adios (file, "unable to open for reading");
+
+ if (copy_input_to_output (file, infp, outfile, outfp) != OK) {
+ inform("unable to copy message to %s, "
+ "it might be lost\n", outfile);
+ }
+
+ fclose (infp);
+ infp = NULL;
/* chdir so that error messages, esp. from MIME parser, just
refer to the message and not its path. */
/* chdir so that error messages, esp. from MIME parser, just
refer to the message and not its path. */
char *input_filename =
concat (maildir, "/", msgnam, NULL);
char *input_filename =
concat (maildir, "/", msgnam, NULL);
- if (copy_input_to_output (input_filename, outfile) != OK) {
- advise (NULL,
- "unable to copy message to %s, it might be lost\n",
- outfile);
+ if ((infp = fopen (input_filename, "r")) == NULL) {
+ adios (input_filename,
+ "unable to open for reading");
+
+ if (copy_input_to_output (input_filename, infp,
+ outfile, outfp) != OK) {
+ inform("unable to copy message to %s, "
+ "it might be lost\n", outfile);
+ }
+
+ fclose (infp);
+ infp = NULL;
- status += mhfixmsgsbr (ctp, maildir, &fx, outfile);
+ status =
+ mhfixmsgsbr (ctp, maildir, &fx, &infp, outfile, &outfp) == OK
+ ? 0
+ : 1;
mhfixmsgsbr (CT *ctp, char *maildir, const fix_transformations *fx,
mhfixmsgsbr (CT *ctp, char *maildir, const fix_transformations *fx,
/* Store input filename in case one of the transformations, i.e.,
fix_boundary(), rewrites to a tmp file. */
char *input_filename = maildir
/* Store input filename in case one of the transformations, i.e.,
fix_boundary(), rewrites to a tmp file. */
char *input_filename = maildir
+ /* Though the input file won't need to be opened if everything goes
+ well, do it here just in case there's a failure, and that failure is
+ running out of file descriptors. */
+ if ((*infp = fopen (input_filename, "r")) == NULL) {
+ adios (input_filename, "unable to open for reading");
+ }
+
- if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
+ /* outfp will be closed by the caller */
+ if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, outfp)) ==
+ NULL) {
reverse_alternative_parts (*ctp);
status = fix_always (*ctp, &message_mods);
reverse_alternative_parts (*ctp);
status = fix_always (*ctp, &message_mods);
- status = write_content (*ctp, input_filename, outfile, modify_inplace,
- message_mods);
+ status = write_content (*ctp, input_filename, outfile, *outfp,
+ modify_inplace, message_mods);
} else if (! modify_inplace) {
/* Something went wrong. Output might be expected, such
as if this were run as a filter. Just copy the input
to the output. */
} else if (! modify_inplace) {
/* Something went wrong. Output might be expected, such
as if this were run as a filter. Just copy the input
to the output. */
- if (copy_input_to_output (input_filename, outfile) != OK) {
- advise (NULL, "unable to copy message to %s, it might be lost\n", outfile);
+ if (copy_input_to_output (input_filename, *infp, outfile,
+ *outfp) != OK) {
+ inform("unable to copy message to %s, it might be lost\n",
+ outfile);
-copy_input_to_output (const char *input_filename, const char *output_filename) {
- int in = open (input_filename, O_RDONLY);
- int out = strcmp (output_filename, "-")
- ? open (output_filename, O_WRONLY | O_CREAT, m_gmprot ())
- : STDOUT_FILENO;
+copy_input_to_output (const char *input_filename, FILE *infp,
+ const char *output_filename, FILE *outfp) {
+ int in = fileno (infp);
+ int out = fileno (outfp);
- switch (state = m_getfld (&gstate, name, buf, &bufsz, fpin)) {
+ switch (state = m_getfld2(&gstate, name, buf, &bufsz)) {
/* if necessary, get rest of field */
while (state == FLDPLUS) {
bufsz = sizeof buf;
/* if necessary, get rest of field */
while (state == FLDPLUS) {
bufsz = sizeof buf;
- state = m_getfld (&gstate, name, buf, &bufsz, fpin);
+ state = m_getfld2(&gstate, name, buf, &bufsz);
new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
ct->c_ctinfo.ci_subtype, NULL);
new_ctline = concat(" ", ct->c_ctinfo.ci_type, "/",
ct->c_ctinfo.ci_subtype, NULL);
strlen(new_ctline) + 1,
ct->c_ctinfo.ci_first_pm, NULL, 0);
fprintf (fpout, "%s:%s%s\n", np, new_ctline,
strlen(new_ctline) + 1,
ct->c_ctinfo.ci_first_pm, NULL, 0);
fprintf (fpout, "%s:%s%s\n", np, new_ctline,
ct->c_ctinfo.ci_subtype = ct_subtype;
if (! replace_substring (&ct->c_ctline, type,
ct_type_subtype)) {
ct->c_ctinfo.ci_subtype = ct_subtype;
if (! replace_substring (&ct->c_ctline, type,
ct_type_subtype)) {
char *
remove_parameter (char *str, const char *name) {
/* It looks to me, based on the BNF in RFC 2045, than there can't
char *
remove_parameter (char *str, const char *name) {
/* It looks to me, based on the BNF in RFC 2045, than there can't
between the "=" and the parameter value. */
char *param_name = concat (name, "=", NULL);
char *cp;
between the "=" and the parameter value. */
char *param_name = concat (name, "=", NULL);
char *cp;
parent->c_ctinfo.ci_subtype = mh_xstrdup("alternative");
if (! replace_substring (&parent->c_ctline, "/related",
"/alternative")) {
parent->c_ctinfo.ci_subtype = mh_xstrdup("alternative");
if (! replace_substring (&parent->c_ctline, "/related",
"/alternative")) {
multipart/alternative. */
remove_parameter (hf->value, "type");
break;
multipart/alternative. */
remove_parameter (hf->value, "type");
break;
if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
- if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
+ if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, &file)) == NULL) {
adios (NULL, "unable to create temporary file in %s", get_temp_dir());
}
tmp_decoded = mh_xstrdup (tempfile);
/* The following call will load ct->c_cefile.ce_file with the tmp
filename of the decoded content. tmp_decoded will contain the
encoded output, get rid of that. */
adios (NULL, "unable to create temporary file in %s", get_temp_dir());
}
tmp_decoded = mh_xstrdup (tempfile);
/* The following call will load ct->c_cefile.ce_file with the tmp
filename of the decoded content. tmp_decoded will contain the
encoded output, get rid of that. */
/* Check for invo_name-format-type/subtype. */
if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
if (verbosw) {
/* Check for invo_name-format-type/subtype. */
if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
if (verbosw) {
"%s-format-%s/%s profile entry",
ct->c_file, invo_name, type, subtype);
}
return NOTOK;
}
if (strchr (cf, '>')) {
"%s-format-%s/%s profile entry",
ct->c_file, invo_name, type, subtype);
}
return NOTOK;
}
if (strchr (cf, '>')) {
"%s-format-%s/%s profile entry", cf, invo_name, type,
"%s-format-%s/%s profile entry", cf, invo_name, type,
snprintf (buffer2, sizeof buffer2, "%d", serial);
boundary =
concat (boundary_prefix,
snprintf (buffer2, sizeof buffer2, "%d", serial);
boundary =
concat (boundary_prefix,
if (hp == old->c_last_hf) {
if (hp == old->c_first_hf) {
old->c_last_hf = old->c_first_hf = NULL;
if (hp == old->c_last_hf) {
if (hp == old->c_first_hf) {
old->c_last_hf = old->c_first_hf = NULL;
report (NULL, ct->c_partno, ct->c_file,
"will not decode%s because it is binary (%s)",
ct->c_partno ? ""
report (NULL, ct->c_partno, ct->c_file,
"will not decode%s because it is binary (%s)",
ct->c_partno ? ""
report (NULL, ct->c_partno, ct->c_file,
"will not decode%s because it is 8bit",
ct->c_partno ? ""
report (NULL, ct->c_partno, ct->c_file,
"will not decode%s because it is 8bit",
ct->c_partno ? ""
++*message_mods;
if (verbosw) {
report (NULL, ct->c_partno, ct->c_file, "decode%s",
++*message_mods;
if (verbosw) {
report (NULL, ct->c_partno, ct->c_file, "decode%s",
fix_filename_param (char *name, char *value, PM *first_pm, PM *last_pm) {
int fixed = 0;
fix_filename_param (char *name, char *value, PM *first_pm, PM *last_pm) {
int fixed = 0;
replace_param (first_pm, last_pm, name, decoded, 0);
fixed = 1;
} else {
replace_param (first_pm, last_pm, name, decoded, 0);
fixed = 1;
} else {
- advise (NULL, "failed to decode %s parameter %s", name, value);
+ inform("failed to decode %s parameter %s", name, value);
-write_content (CT ct, const char *input_filename, char *outfile, int modify_inplace,
- int message_mods) {
+write_content (CT ct, const char *input_filename, char *outfile, FILE *outfp,
+ int modify_inplace, int message_mods) {
char *infile = input_filename
? mh_xstrdup (input_filename)
: mh_xstrdup (ct->c_file ? ct->c_file : "-");
char *infile = input_filename
? mh_xstrdup (input_filename)
: mh_xstrdup (ct->c_file ? ct->c_file : "-");
- admonish (NULL, "unable to remove input file %s, "
- "not modifying it", infile);
+ inform("unable to remove input file %s, "
+ "not modifying it, continuing...", infile);
if (verbosw) {
va_start (args, message);
fmt = concat (filename, partno ? " part " : ", ",
if (verbosw) {
va_start (args, message);
fmt = concat (filename, partno ? " part " : ", ",
- partno ? partno : "", partno ? ", " : "", message, NULL);
+ FENDNULL(partno), partno ? ", " : "", message, NULL);
advertise (what, NULL, fmt, args);
advertise (what, NULL, fmt, args);