*
* 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;
if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
adios (NULL, "missing argument to %s", argp[-2]);
}
if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
adios (NULL, "missing argument to %s", argp[-2]);
}
continue;
case OUTFILESW:
if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
adios (NULL, "missing argument to %s", argp[-2]);
}
continue;
case OUTFILESW:
if (! (cp = *argp++) || (*cp == '-' && cp[1])) {
adios (NULL, "missing argument to %s", argp[-2]);
}
* 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");
+ }
+ }
+
cpydata (STDIN_FILENO, fd, "-", file);
}
cpydata (STDIN_FILENO, fd, "-", file);
}
- /* 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
? concat (maildir, "/", (*ctp)->c_file, NULL)
/* Store input filename in case one of the transformations, i.e.,
fix_boundary(), rewrites to a tmp file. */
char *input_filename = maildir
? concat (maildir, "/", (*ctp)->c_file, NULL)
+ /* 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);
if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
if (replace_boundary (*ct, fixed, part_boundary) == OK) {
if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
if (replace_boundary (*ct, fixed, part_boundary) == OK) {
if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
if (strlen (end) > 3 && *end++ == '\n' &&
*end++ == '-' && *end++ == '-') {
if ((end = rfind_str (buffer, cp - buffer, "\n"))) {
if (strlen (end) > 3 && *end++ == '\n' &&
*end++ == '-' && *end++ == '-') {
- 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;
/* Don't need to warn for multiple C-T-E header
fields, parse_mime() already does that. But
/* Don't need to warn for multiple C-T-E header
fields, parse_mime() already does that. But
- add_header (ct, add (ENCODING_FIELD, NULL), cte);
+ add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
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 (reformat_part (tp_part, tmp_plain_file,
tp_part->c_ctinfo.ci_type,
tp_part->c_ctinfo.ci_subtype,
if (reformat_part (tp_part, tmp_plain_file,
tp_part->c_ctinfo.ci_type,
tp_part->c_ctinfo.ci_subtype,
new_part->c_begin = ct->c_begin;
new_part->c_end = ct->c_end;
copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
new_part->c_begin = ct->c_begin;
new_part->c_end = ct->c_end;
copy_ctinfo (&new_part->c_ctinfo, &ct->c_ctinfo);
/* c_ctline is used by reformat__part(), so it can preserve
anything after the type/subtype. */
/* c_ctline is used by reformat__part(), so it can preserve
anything after the type/subtype. */
- dest->ci_type = src->ci_type ? add (src->ci_type, NULL) : NULL;
- dest->ci_subtype = src->ci_subtype ? add (src->ci_subtype, NULL) : NULL;
+ dest->ci_type = src->ci_type ? mh_xstrdup (src->ci_type) : NULL;
+ dest->ci_subtype = src->ci_subtype ? mh_xstrdup (src->ci_subtype) : NULL;
for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
for (s_pm = src->ci_first_pm; s_pm; s_pm = s_pm->pm_next) {
d_pm = add_param(&dest->ci_first_pm, &dest->ci_last_pm, s_pm->pm_name,
- dest->ci_comment = src->ci_comment ? add (src->ci_comment, NULL) : NULL;
- dest->ci_magic = src->ci_magic ? add (src->ci_magic, NULL) : NULL;
+ dest->ci_comment = src->ci_comment ? mh_xstrdup (src->ci_comment) : NULL;
+ dest->ci_magic = src->ci_magic ? mh_xstrdup (src->ci_magic) : NULL;
- if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, NULL)) == NULL) {
+ if ((tempfile = m_mktemp2 (NULL, invo_name, NULL, &file)) == NULL) {
/* 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. */
/* 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,
c_showproc, c_termproc, c_storeproc, c_storage, c_folder
*/
c_showproc, c_termproc, c_storeproc, c_storage, c_folder
*/
snprintf (buffer2, sizeof buffer2, "%d", serial);
boundary =
concat (boundary_prefix,
snprintf (buffer2, sizeof buffer2, "%d", serial);
boundary =
concat (boundary_prefix,
/* Load c_first_hf and c_last_hf. */
transfer_noncontent_headers (first_alt, ct);
/* Load c_first_hf and c_last_hf. */
transfer_noncontent_headers (first_alt, ct);
- add_header (ct, add (TYPE_FIELD, NULL), concat (name, "\n", NULL));
+ add_header (ct, mh_xstrdup (TYPE_FIELD), concat (name, "\n", NULL));
free (first_alt->c_partno);
first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
new_part->c_partno = concat (ct->c_partno, ".2", NULL);
} else {
free (first_alt->c_partno);
first_alt->c_partno = concat (ct->c_partno, ".1", NULL);
new_part->c_partno = concat (ct->c_partno, ".2", NULL);
} else {
- first_alt->c_partno = add ("1", NULL);
- new_part->c_partno = add ("2", NULL);
+ first_alt->c_partno = mh_xstrdup ("1");
+ new_part->c_partno = mh_xstrdup ("2");
- ct->c_ctinfo.ci_type = add (typename, NULL);
- ct->c_ctinfo.ci_subtype = add (subtypename, NULL);
+ ct->c_ctinfo.ci_type = mh_xstrdup (typename);
+ ct->c_ctinfo.ci_subtype = mh_xstrdup (subtypename);
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;
free (hf->value);
hf->value = (cp = strchr (ct->c_ctline, ';'))
? concat (type_subtypename, cp, "\n", NULL)
free (hf->value);
hf->value = (cp = strchr (ct->c_ctline, ';'))
? concat (type_subtypename, cp, "\n", NULL)
(cp = strchr (ct->c_ctline, ';'))
? concat (type_subtypename, cp, "\n", NULL)
(cp = strchr (ct->c_ctline, ';'))
? concat (type_subtypename, cp, "\n", 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",
/* Strip each CR before a LF from the content. */
fseeko (*fp, begin, SEEK_SET);
/* Strip each CR before a LF from the content. */
fseeko (*fp, begin, SEEK_SET);
- add_header (ct, add (ENCODING_FIELD, NULL), cte);
+ add_header (ct, mh_xstrdup (ENCODING_FIELD), cte);
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) {
- ? add (input_filename, NULL)
- : add (ct->c_file ? ct->c_file : "-", NULL);
+ ? mh_xstrdup (input_filename)
+ : mh_xstrdup (ct->c_file ? ct->c_file : "-");
if (remove_file (infile) == OK) {
if (rename (outfile, infile)) {
if (remove_file (infile) == OK) {
if (rename (outfile, infile)) {
- 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);