]> diplodocus.org Git - nmh/commitdiff
Added context_find_by_type(), helper function to search first, if
authorDavid Levine <levinedl@acm.org>
Mon, 26 May 2014 14:17:34 +0000 (09:17 -0500)
committerDavid Levine <levinedl@acm.org>
Mon, 26 May 2014 14:17:34 +0000 (09:17 -0500)
subtype is non-NULL, for invoname-string-type/subtype and then
invoname-string-type.  Also gets rid of some fixed-size temp buffers.

h/prototypes.h
sbr/context_find.c
uip/mhbuildsbr.c
uip/mhfixmsg.c
uip/mhparse.c
uip/mhshowsbr.c
uip/mhstoresbr.c

index 00862d09f7364fbab2de83af2ba3c06bdd795c84..a21a7180ec7947bfaaafb524c4bff76c1052a149 100644 (file)
@@ -46,7 +46,8 @@ int client(char *, char *, char *, int, int);
 void closefds(int);
 char *concat (const char *, ...);
 int context_del (char *);
-char *context_find (char *);
+char *context_find (const char *);
+char *context_find_by_type (const char *, const char *, const char *);
 int context_foil (char *);
 void context_read (void);
 void context_replace (char *, char *);
index 7c809ec180d2bf69e2e5cbc89ad101b2214cd153..5cad0164144ac8b9497609d715a6466758cde790 100644 (file)
@@ -11,7 +11,7 @@
 
 
 char *
-context_find (char *str)
+context_find (const char *str)
 {
     struct node *np;
 
@@ -21,3 +21,33 @@ context_find (char *str)
 
     return NULL;
 }
+
+
+/*
+ * Helper function to search first, if subtype is non-NULL, for
+ * invoname-string-type/subtype and then invoname-string-type.
+ * If entry is found but is empty, returns NULL.
+ */
+char *
+context_find_by_type (const char *string, const char *type,
+                      const char *subtype) {
+    char *value = NULL;
+
+    if (subtype) {
+        char *cp;
+
+        cp = concat (invo_name, "-", string, "-", type, "/", subtype, NULL);
+        if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
+        free (cp);
+    }
+
+    if (value == NULL) {
+        char *cp;
+
+        cp = concat (invo_name, "-", string, "-", type, NULL);
+        if ((value = context_find (cp)) != NULL && *value == '\0') value = NULL;
+        free (cp);
+    }
+
+    return value;
+}
index 7e2f9f48dd3cd43962f78be31b80e9880505162d..19d31316adff6e56f1d304e32f5485b197430ce2 100644 (file)
@@ -827,14 +827,10 @@ use_forw:
         * No [file] argument, so check profile for
         * method to compose content.
         */
-       snprintf (buffer, sizeof(buffer), "%s-compose-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-       if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
-           snprintf (buffer, sizeof(buffer), "%s-compose-%s", invo_name, ci->ci_type);
-           if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
-               content_error (NULL, ct, "don't know how to compose content");
-               done (1);
-           }
+       cp = context_find_by_type ("compose", ci->ci_type, ci->ci_subtype);
+       if (cp == NULL) {
+           content_error (NULL, ct, "don't know how to compose content");
+           done (1);
        }
        ci->ci_magic = add (cp, NULL);
        return OK;
@@ -1863,8 +1859,7 @@ setup_attach_content(CT ct, char *filename)
     char *type, *simplename = r1bindex(filename, '/');
     struct str2init *s2i;
     PM pm;
-    char buffer[BUFSIZ], *cp;
-    int no_subtype = 0;
+    char *cp;
 
     if (! (type = mime_type(filename))) {
        adios(NULL, "Unable to determine MIME type of \"%s\"", filename);
@@ -1944,24 +1939,16 @@ setup_attach_content(CT ct, char *filename)
      * 'attachment'.
      */
 
-    snprintf (buffer, sizeof(buffer), "%s-disposition-%s/%s",
-              invo_name, ct->c_ctinfo.ci_type, ct->c_ctinfo.ci_subtype);
-    cp = context_find (buffer);
-    if (cp == NULL || *cp == '\0') {
-        no_subtype = 1;
-        snprintf (buffer, sizeof(buffer), "%s-disposition-%s", invo_name,
-                  ct->c_ctinfo.ci_type);
-        cp = context_find (buffer);
-    }
-    if (cp != NULL && *cp != '\0') {
-        if (strcasecmp (cp, "attachment")  &&
-            strcasecmp (cp, "inline")) {
+    cp = context_find_by_type ("disposition", ct->c_ctinfo.ci_type,
+                               ct->c_ctinfo.ci_subtype);
+    if (cp != NULL) {
+        if (strcasecmp (cp, "attachment")  &&  strcasecmp (cp, "inline")) {
             admonish (NULL, "configuration problem: %s-disposition-%s%s%s "
                       "specifies '%s' but only 'attachment' and 'inline' are "
                       "allowed", invo_name,
                       ct->c_ctinfo.ci_type,
-                      no_subtype ? "" : "/",
-                      no_subtype ? "" : ct->c_ctinfo.ci_subtype,
+                      ct->c_ctinfo.ci_subtype ? "/" : "",
+                      ct->c_ctinfo.ci_subtype ? ct->c_ctinfo.ci_subtype : "",
                       cp);
         }
     }
index c93a71fd6911c22286444ef329da1f4ab9c88f37..2a705114e0fe8cb95e3e5db7c31ec457c3f47d82 100644 (file)
@@ -1091,37 +1091,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. */
-    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, '>')) {
-            free (cp);
             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;
         }
     }
-    free (cp);
 
     cp = concat (cf, " >", file, NULL);
     status = show_content_aux (ct, 0, cp, NULL, NULL);
index f61edeb652830109158a41c499e478235ceac59d..6e16a5aa613b9ecd66a36d13c2f3daad56decc77 100644 (file)
@@ -1711,15 +1711,7 @@ openBase64 (CT ct, char **file)
 
     /* sbeck@cise.ufl.edu -- handle suffixes */
     ci = &ct->c_ctinfo;
-    snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
-              invo_name, ci->ci_type, ci->ci_subtype);
-    cp = context_find (buffer);
-    if (cp == NULL || *cp == '\0') {
-        snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
-                  ci->ci_type);
-        cp = context_find (buffer);
-    }
-    if (cp != NULL && *cp != '\0') {
+    if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
        if (ce->ce_unlink) {
            /* Create temporary file with filename extension. */
            if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
@@ -1921,7 +1913,6 @@ openQuoted (CT ct, char **file)
 {
     int        cc, digested, len, quoted, own_ct_fp = 0;
     char *cp, *ep;
-    char buffer[BUFSIZ];
     char *bufp = NULL;
     size_t buflen;
     ssize_t gotlen;
@@ -1953,15 +1944,7 @@ openQuoted (CT ct, char **file)
 
     /* sbeck@cise.ufl.edu -- handle suffixes */
     ci = &ct->c_ctinfo;
-    snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
-              invo_name, ci->ci_type, ci->ci_subtype);
-    cp = context_find (buffer);
-    if (cp == NULL || *cp == '\0') {
-        snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
-                  ci->ci_type);
-        cp = context_find (buffer);
-    }
-    if (cp != NULL && *cp != '\0') {
+    if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
        if (ce->ce_unlink) {
            /* Create temporary file with filename extension. */
            if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
@@ -2179,15 +2162,7 @@ open7Bit (CT ct, char **file)
 
     /* sbeck@cise.ufl.edu -- handle suffixes */
     ci = &ct->c_ctinfo;
-    snprintf (buffer, sizeof(buffer), "%s-suffix-%s/%s",
-              invo_name, ci->ci_type, ci->ci_subtype);
-    cp = context_find (buffer);
-    if (cp == NULL || *cp == '\0') {
-        snprintf (buffer, sizeof(buffer), "%s-suffix-%s", invo_name,
-                  ci->ci_type);
-        cp = context_find (buffer);
-    }
-    if (cp != NULL && *cp != '\0') {
+    if ((cp = context_find_by_type ("suffix", ci->ci_type, ci->ci_subtype))) {
        if (ce->ce_unlink) {
            /* Create temporary file with filename extension. */
            if ((ce->ce_file = m_mktemps(invo_name, cp, NULL, NULL)) == NULL) {
index 1f3b6f16a59dda3d591c0b514093e1e7c8e9e57a..4a51dd1cdaabcbb39f825c82678210b5c30fa815 100644 (file)
@@ -292,7 +292,7 @@ static int
 show_content (CT ct, int alternate, int textonly, int inlineonly,
              struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
     /*
@@ -305,15 +305,8 @@ show_content (CT ct, int alternate, int textonly, int inlineonly,
        return OK;
     }
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     if ((cp = ct->c_showproc))
@@ -505,15 +498,8 @@ show_text (CT ct, int alternate, int concatsw, struct format *fmt)
     char *cp, buffer[BUFSIZ];
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     /*
@@ -545,18 +531,11 @@ static int
 show_multi (CT ct, int alternate, int concatsw, int textonly, int inlineonly,
            struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_multi_aux (ct, alternate, cp, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_multi_aux (ct, alternate, cp, fmt);
 
     if ((cp = ct->c_showproc))
@@ -705,18 +684,11 @@ show_multi_aux (CT ct, int alternate, char *cp, struct format *fmt)
 static int
 show_message_rfc822 (CT ct, int alternate, struct format *fmt)
 {
-    char *cp, buffer[BUFSIZ];
+    char *cp;
     CI ci = &ct->c_ctinfo;
 
-    /* Check for invo_name-show-type/subtype */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-    if ((cp = context_find (buffer)) && *cp != '\0')
-       return show_content_aux (ct, alternate, cp, NULL, fmt);
-
-    /* Check for invo_name-show-type */
-    snprintf (buffer, sizeof(buffer), "%s-show-%s", invo_name, ci->ci_type);
-    if ((cp = context_find (buffer)) && *cp != '\0')
+    /* Check for invo_name-show-type[/subtype] */
+    if ((cp = context_find_by_type ("show", ci->ci_type, ci->ci_subtype)))
        return show_content_aux (ct, alternate, cp, NULL, fmt);
 
     if ((cp = ct->c_showproc))
index 6fb9d85684b7614d05807ea8266b8dd36c9f7228..dc7a0f0f7fa0de0f74ebdd04fa4f5f45123266b9 100644 (file)
@@ -566,13 +566,9 @@ store_content (CT ct, CT p, mhstoreinfo_t info)
     if ((cp = ct->c_storeproc) == NULL || *cp == '\0') {
        CI ci = &ct->c_ctinfo;
 
-       snprintf (buffer, sizeof(buffer), "%s-store-%s/%s",
-               invo_name, ci->ci_type, ci->ci_subtype);
-       if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
-           snprintf (buffer, sizeof(buffer), "%s-store-%s", invo_name, ci->ci_type);
-           if ((cp = context_find (buffer)) == NULL || *cp == '\0') {
-               cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
-           }
+       cp = context_find_by_type ("store", ci->ci_type, ci->ci_subtype);
+       if (cp == NULL) {
+           cp = ct->c_type == CT_MESSAGE ? "+" : "%m%P.%s";
        }
     }