]> diplodocus.org Git - nmh/blobdiff - uip/mhfixmsg.c
Removed "If this draft originated on the local host, then" phrase
[nmh] / uip / mhfixmsg.c
index c93a71fd6911c22286444ef329da1f4ab9c88f37..66b2eacab4e752a18bedadae81095a5cba777773 100644 (file)
@@ -53,7 +53,6 @@ int debugsw; /* Needed by 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 */
 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 */
-void reverse_parts (CT);
 
 /* mhoutsbr.c */
 int output_message (CT, char *);
 
 /* mhoutsbr.c */
 int output_message (CT, char *);
@@ -78,7 +77,6 @@ typedef struct fix_transformations {
 } fix_transformations;
 
 int mhfixmsgsbr (CT *, const fix_transformations *, char *);
 } fix_transformations;
 
 int mhfixmsgsbr (CT *, const fix_transformations *, char *);
-static void reverse_alternative_parts (CT);
 static int fix_boundary (CT *, int *);
 static int get_multipart_boundary (CT, char **);
 static int replace_boundary (CT, char *, char *);
 static int fix_boundary (CT *, int *);
 static int get_multipart_boundary (CT, char **);
 static int replace_boundary (CT, char *, char *);
@@ -287,11 +285,12 @@ main (int argc, char **argv) {
             }
         }
 
             }
         }
 
-        if (! (cts = (CT *) calloc ((size_t) 2, sizeof *cts)))
+        if (! (cts = (CT *) mh_xcalloc ((size_t) 2, sizeof *cts))) {
             adios (NULL, "out of memory");
             adios (NULL, "out of memory");
+        }
         ctp = cts;
 
         ctp = cts;
 
-        if ((ct = parse_mime (file))) *ctp++ = ct;
+        if ((ct = parse_mime (file))) { *ctp++ = ct; }
     } else {
         /*
          * message(s) are coming from a folder
     } else {
         /*
          * message(s) are coming from a folder
@@ -321,8 +320,10 @@ main (int argc, char **argv) {
                 done (1);
         seq_setprev (mp);       /* set the previous-sequence */
 
                 done (1);
         seq_setprev (mp);       /* set the previous-sequence */
 
-        if (! (cts = (CT *) calloc ((size_t) (mp->numsel + 1), sizeof *cts)))
+        if (! (cts =
+               (CT *) mh_xcalloc ((size_t) (mp->numsel + 1), sizeof *cts))) {
             adios (NULL, "out of memory");
             adios (NULL, "out of memory");
+        }
         ctp = cts;
 
         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
         ctp = cts;
 
         for (msgnum = mp->lowsel; msgnum <= mp->hghsel; msgnum++) {
@@ -330,7 +331,7 @@ main (int argc, char **argv) {
                 char *msgnam;
 
                 msgnam = m_name (msgnum);
                 char *msgnam;
 
                 msgnam = m_name (msgnum);
-                if ((ct = parse_mime (msgnam))) *ctp++ = ct;
+                if ((ct = parse_mime (msgnam))) { *ctp++ = ct; }
             }
         }
 
             }
         }
 
@@ -446,7 +447,7 @@ mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) {
     }
 
     if (modify_inplace) {
     }
 
     if (modify_inplace) {
-        if (status != OK) (void) m_unlink (outfile);
+        if (status != OK) { (void) m_unlink (outfile); }
         free (outfile);
         outfile = NULL;
     }
         free (outfile);
         outfile = NULL;
     }
@@ -457,27 +458,6 @@ mhfixmsgsbr (CT *ctp, const fix_transformations *fx, char *outfile) {
 }
 
 
 }
 
 
-/* parse_mime() arranges alternates in reverse (priority) order, so
-   reverse them back.  This will put a text/plain part at the front of
-   a multipart/alternative part, for example, where it belongs. */
-static void
-reverse_alternative_parts (CT ct) {
-    if (ct->c_type == CT_MULTIPART) {
-        struct multipart *m = (struct multipart *) ct->c_ctparams;
-        struct part *part;
-
-        if (ct->c_subtype == MULTI_ALTERNATE) {
-            reverse_parts (ct);
-        }
-
-        /* And call recursively on each part of a multipart. */
-        for (part = m->mp_parts; part; part = part->mp_next) {
-            reverse_alternative_parts (part->mp_part);
-        }
-    }
-}
-
-
 static int
 fix_boundary (CT *ct, int *message_mods) {
     struct multipart *mp;
 static int
 fix_boundary (CT *ct, int *message_mods) {
     struct multipart *mp;
@@ -505,9 +485,11 @@ fix_boundary (CT *ct, int *message_mods) {
                 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
                     if (replace_boundary (*ct, fixed, part_boundary) == OK) {
                         char *filename = add ((*ct)->c_file, NULL);
                 if ((fixed = m_mktemp2 (NULL, invo_name, NULL, &(*ct)->c_fp))) {
                     if (replace_boundary (*ct, fixed, part_boundary) == OK) {
                         char *filename = add ((*ct)->c_file, NULL);
+                        CT fixed_ct;
 
                         free_content (*ct);
 
                         free_content (*ct);
-                        if ((*ct = parse_mime (fixed))) {
+                        if ((fixed_ct = parse_mime (fixed))) {
+                            *ct = fixed_ct;
                             (*ct)->c_unlink = 1;
 
                             ++*message_mods;
                             (*ct)->c_unlink = 1;
 
                             ++*message_mods;
@@ -515,6 +497,9 @@ fix_boundary (CT *ct, int *message_mods) {
                                 report (NULL, NULL, filename,
                                         "fix multipart boundary");
                             }
                                 report (NULL, NULL, filename,
                                         "fix multipart boundary");
                             }
+                        } else {
+                            advise (NULL, "unable to parse fixed part");
+                            status = NOTOK;
                         }
                         free (filename);
                     } else {
                         }
                         free (filename);
                     } else {
@@ -691,7 +676,9 @@ replace_boundary (CT ct, char *file, char *boundary) {
         case BODY:
             fputs ("\n", fpout);
             /* buf will have a terminating NULL, skip it. */
         case BODY:
             fputs ("\n", fpout);
             /* buf will have a terminating NULL, skip it. */
-            fwrite (buf, 1, bufsz-1, fpout);
+            if ((int) fwrite (buf, 1, bufsz-1, fpout) < bufsz-1) {
+                advise (file, "fwrite");
+            }
             continue;
 
         case FILEEOF:
             continue;
 
         case FILEEOF:
@@ -846,7 +833,7 @@ ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) {
         int has_text_plain = 0;
 
         /* Nothing to do for text/plain. */
         int has_text_plain = 0;
 
         /* Nothing to do for text/plain. */
-        if ((*ct)->c_subtype == TEXT_PLAIN) return OK;
+        if ((*ct)->c_subtype == TEXT_PLAIN) { return OK; }
 
         if (parent  &&  parent->c_type == CT_MULTIPART  &&
             parent->c_subtype == MULTI_ALTERNATE) {
 
         if (parent  &&  parent->c_type == CT_MULTIPART  &&
             parent->c_subtype == MULTI_ALTERNATE) {
@@ -960,9 +947,8 @@ ensure_text_plain (CT *ct, CT parent, int *message_mods, int replacetextplain) {
 
     case CT_MESSAGE:
         if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
 
     case CT_MESSAGE:
         if ((*ct)->c_subtype == MESSAGE_EXTERNAL) {
-            struct exbody *e;
+            struct exbody *e = (struct exbody *) (*ct)->c_ctparams;
 
 
-            e = (struct exbody *) (*ct)->c_ctparams;
             status = ensure_text_plain (&e->eb_content, *ct, message_mods,
                                         replacetextplain);
         }
             status = ensure_text_plain (&e->eb_content, *ct, message_mods,
                                         replacetextplain);
         }
@@ -1010,7 +996,7 @@ static CT
 divide_part (CT ct) {
     CT new_part;
 
 divide_part (CT ct) {
     CT new_part;
 
-    if ((new_part = (CT) calloc (1, sizeof *new_part)) == NULL)
+    if ((new_part = (CT) mh_xcalloc (1, sizeof *new_part)) == NULL)
         adios (NULL, "out of memory");
 
     /* Just copy over what is needed for decoding.  c_vrsn and
         adios (NULL, "out of memory");
 
     /* Just copy over what is needed for decoding.  c_vrsn and
@@ -1091,37 +1077,22 @@ reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
        Could show_multi() in mhshowsbr.c avoid this? */
 
     /* Check for invo_name-format-type/subtype. */
        Could show_multi() in mhshowsbr.c avoid this? */
 
     /* Check for invo_name-format-type/subtype. */
-    cp = concat (invo_name, "-format-", type, "/", subtype, NULL);
-    if ((cf = context_find (cp))  &&  *cf != '\0') {
+    if ((cf = context_find_by_type ("format", type, subtype)) == NULL) {
+        if (verbosw) {
+            advise (NULL, "Don't know how to convert %s, there is no "
+                    "%s-format-%s/%s profile entry",
+                    ct->c_file, invo_name, type, subtype);
+        }
+        return NOTOK;
+    } else {
         if (strchr (cf, '>')) {
         if (strchr (cf, '>')) {
-            free (cp);
             advise (NULL, "'>' prohibited in \"%s\",\nplease fix your "
                     "%s-format-%s/%s profile entry", cf, invo_name, type,
             advise (NULL, "'>' prohibited in \"%s\",\nplease fix your "
                     "%s-format-%s/%s profile entry", cf, invo_name, type,
-                    subtype);
-            return NOTOK;
-        }
-    } else {
-        free (cp);
-
-        /* Check for invo_name-format-type. */
-        cp = concat (invo_name, "-format-", type, NULL);
-        if (! (cf = context_find (cp))  ||  *cf == '\0') {
-            free (cp);
-            if (verbosw) {
-                advise (NULL, "Don't know how to convert %s, there is no "
-                        "%s-format-%s/%s profile entry",
-                        ct->c_file, invo_name, type, subtype);
-            }
-            return NOTOK;
-        }
+                    subtype ? subtype : "");
 
 
-        if (strchr (cf, '>')) {
-            free (cp);
-            advise (NULL, "'>' prohibited in \"%s\"", cf);
             return NOTOK;
         }
     }
             return NOTOK;
         }
     }
-    free (cp);
 
     cp = concat (cf, " >", file, NULL);
     status = show_content_aux (ct, 0, cp, NULL, NULL);
 
     cp = concat (cf, " >", file, NULL);
     status = show_content_aux (ct, 0, cp, NULL, NULL);
@@ -1159,8 +1130,10 @@ reformat_part (CT ct, char *file, char *type, char *subtype, int c_type) {
 /* Identifies 7bit or 8bit content based on charset. */
 static int
 charset_encoding (CT ct) {
 /* Identifies 7bit or 8bit content based on charset. */
 static int
 charset_encoding (CT ct) {
-    int encoding =
-        strcasecmp (content_charset (ct), "US-ASCII")  ?  CE_8BIT  :  CE_7BIT;
+    char *ct_charset = content_charset (ct);
+    int encoding = strcasecmp (ct_charset, "US-ASCII")  ?  CE_8BIT  :  CE_7BIT;
+
+    free (ct_charset);
 
     return encoding;
 }
 
     return encoding;
 }
@@ -1177,11 +1150,11 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
     struct multipart *m;
     const struct str2init *ctinit;
 
     struct multipart *m;
     const struct str2init *ctinit;
 
-    if ((ct = (CT) calloc (1, sizeof *ct)) == NULL)
+    if ((ct = (CT) mh_xcalloc (1, sizeof *ct)) == NULL)
         adios (NULL, "out of memory");
 
     /* Set up the multipart/alternative part.  These fields of *ct were
         adios (NULL, "out of memory");
 
     /* Set up the multipart/alternative part.  These fields of *ct were
-       initialized to 0 by calloc():
+       initialized to 0 by mh_xcalloc():
        c_fp, c_unlink, c_begin, c_end,
        c_vrsn, c_ctline, c_celine,
        c_id, c_descr, c_dispo, c_partno,
        c_fp, c_unlink, c_begin, c_end,
        c_vrsn, c_ctline, c_celine,
        c_id, c_descr, c_dispo, c_partno,
@@ -1285,13 +1258,13 @@ build_multipart_alt (CT first_alt, CT new_part, int type, int subtype) {
     p->mp_next->mp_next = NULL;
     p->mp_next->mp_part = first_alt;
 
     p->mp_next->mp_next = NULL;
     p->mp_next->mp_part = first_alt;
 
-    if ((m = (struct multipart *) calloc (1, sizeof (struct multipart))) ==
+    if ((m = (struct multipart *) mh_xcalloc (1, sizeof (struct multipart))) ==
         NULL)
         adios (NULL, "out of memory");
     m->mp_start = concat (boundary, "\n", NULL);
     m->mp_stop = concat (boundary, "--\n", NULL);
     m->mp_parts = p;
         NULL)
         adios (NULL, "out of memory");
     m->mp_start = concat (boundary, "\n", NULL);
     m->mp_stop = concat (boundary, "--\n", NULL);
     m->mp_parts = p;
-    ct->c_ctparams = (void *) m;
+    ct->c_ctparams = m;
 
     free (boundary);
 
 
     free (boundary);
 
@@ -1511,9 +1484,8 @@ decode_text_parts (CT ct, int encoding, int *message_mods) {
 
     case CT_MESSAGE:
         if (ct->c_subtype == MESSAGE_EXTERNAL) {
 
     case CT_MESSAGE:
         if (ct->c_subtype == MESSAGE_EXTERNAL) {
-            struct exbody *e;
+            struct exbody *e = (struct exbody *) ct->c_ctparams;
 
 
-            e = (struct exbody *) ct->c_ctparams;
             status = decode_text_parts (e->eb_content, encoding, message_mods);
         }
         break;
             status = decode_text_parts (e->eb_content, encoding, message_mods);
         }
         break;
@@ -1645,7 +1617,7 @@ strip_crs (CT ct, int *message_mods) {
                 size_t i;
                 int last_char_was_cr = 0;
 
                 size_t i;
                 int last_char_was_cr = 0;
 
-                if (end > 0) bytes_to_read -= bytes_read;
+                if (end > 0) { bytes_to_read -= bytes_read; }
 
                 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
                     if (*cp == '\n'  &&  last_char_was_cr) {
 
                 for (i = 0, cp = buffer; i < bytes_read; ++i, ++cp) {
                     if (*cp == '\n'  &&  last_char_was_cr) {
@@ -1680,11 +1652,19 @@ strip_crs (CT ct, int *message_mods) {
                         if (*cp == '\r') {
                             last_char_was_cr = 1;
                         } else if (last_char_was_cr) {
                         if (*cp == '\r') {
                             last_char_was_cr = 1;
                         } else if (last_char_was_cr) {
-                            if (*cp != '\n') write (fd, "\r", 1);
-                            write (fd, cp, 1);
+                            if (*cp != '\n') {
+                                if (write (fd, "\r", 1) < 0) {
+                                    advise (tempfile, "CR write");
+                                }
+                            }
+                            if (write (fd, cp, 1) < 0) {
+                                advise (tempfile, "write");
+                            }
                             last_char_was_cr = 0;
                         } else {
                             last_char_was_cr = 0;
                         } else {
-                            write (fd, cp, 1);
+                            if (write (fd, cp, 1) < 0) {
+                                advise (tempfile, "write");
+                            }
                             last_char_was_cr = 0;
                         }
                     }
                             last_char_was_cr = 0;
                         }
                     }
@@ -1722,6 +1702,8 @@ strip_crs (CT ct, int *message_mods) {
         }
     }
 
         }
     }
 
+    free (charset);
+
     return status;
 }
 
     return status;
 }
 
@@ -1736,14 +1718,18 @@ convert_charsets (CT ct, char *dest_charset, int *message_mods) {
             status = convert_charset (ct, dest_charset, message_mods);
             if (status == OK) {
                 if (verbosw) {
             status = convert_charset (ct, dest_charset, message_mods);
             if (status == OK) {
                 if (verbosw) {
+                    char *ct_charset = content_charset (ct);
+
                     report (NULL, ct->c_partno, ct->c_file,
                     report (NULL, ct->c_partno, ct->c_file,
-                            "convert %s to %s",
-                            content_charset(ct), dest_charset);
+                            "convert %s to %s", ct_charset, dest_charset);
+                    free (ct_charset);
                 }
             } else {
                 }
             } else {
+                char *ct_charset = content_charset (ct);
+
                 report ("iconv", ct->c_partno, ct->c_file,
                 report ("iconv", ct->c_partno, ct->c_file,
-                        "failed to convert %s to %s",
-                        content_charset(ct), dest_charset);
+                        "failed to convert %s to %s", ct_charset, dest_charset);
+                free (ct_charset);
             }
         }
         break;
             }
         }
         break;
@@ -1763,9 +1749,8 @@ convert_charsets (CT ct, char *dest_charset, int *message_mods) {
 
     case CT_MESSAGE:
         if (ct->c_subtype == MESSAGE_EXTERNAL) {
 
     case CT_MESSAGE:
         if (ct->c_subtype == MESSAGE_EXTERNAL) {
-            struct exbody *e;
+            struct exbody *e = (struct exbody *) ct->c_ctparams;
 
 
-            e = (struct exbody *) ct->c_ctparams;
             status =
                 convert_charsets (e->eb_content, dest_charset, message_mods);
         }
             status =
                 convert_charsets (e->eb_content, dest_charset, message_mods);
         }
@@ -1812,8 +1797,8 @@ write_content (CT ct, char *input_filename, char *outfile, int modify_inplace,
                                 }
                             }
                         }
                                 }
                             }
                         }
-                        if (new != -1) close (new);
-                        if (old != -1) close (old);
+                        if (new != -1) { close (new); }
+                        if (old != -1) { close (old); }
                         (void) m_unlink (outfile);
 
                         if (i < 0) {
                         (void) m_unlink (outfile);
 
                         if (i < 0) {