]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
config: Handle CCACHE_NO* environment variables
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 16 Jul 2011 15:11:36 +0000 (17:11 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 20 Jul 2011 21:31:32 +0000 (23:31 +0200)
conf.c
test/test_conf.c

diff --git a/conf.c b/conf.c
index 95cfa2f5e009b74caa6d1751a6ef36d6db29781a..0fdab63db82085a57f4d9722a3ad469ff7a18986 100644 (file)
--- a/conf.c
+++ b/conf.c
@@ -283,7 +283,7 @@ find_env_to_conf(const char *name)
 
 static bool
 handle_conf_setting(struct conf *conf, const char *key, const char *value,
-                    char **errmsg, bool from_env_variable)
+                    char **errmsg, bool from_env_variable, bool negate_boolean)
 {
        const struct conf_item *item;
 
@@ -299,7 +299,7 @@ handle_conf_setting(struct conf *conf, const char *key, const char *value,
                 * true.
                 */
                bool *value = (bool *)((void *)conf + item->offset);
-               *value = true;
+               *value = !negate_boolean;
                return true;
        }
 
@@ -355,7 +355,7 @@ parse_line(struct conf *conf, const char *line, char **errmsg)
 
 #undef SKIP_WS
 
-       result = handle_conf_setting(conf, key, value, errmsg, false);
+       result = handle_conf_setting(conf, key, value, errmsg, false, false);
 
        free(key);
        free(value);
@@ -507,6 +507,8 @@ conf_update_from_environment(struct conf *conf, char **errmsg)
        char *key;
        char *errmsg2;
        const struct env_to_conf_item *env_to_conf_item;
+       bool negate;
+       size_t key_start;
 
        for (p = environ; *p; ++p) {
                if (!str_startswith(*p, "CCACHE_")) {
@@ -517,7 +519,15 @@ conf_update_from_environment(struct conf *conf, char **errmsg)
                        continue;
                }
 
-               key = x_strndup(*p + 7, q - *p - 7);
+               if (str_startswith(*p + 7, "NO")) {
+                       negate = true;
+                       key_start = 9;
+               } else {
+                       negate = false;
+                       key_start = 7;
+               }
+               key = x_strndup(*p + key_start, q - *p - key_start);
+
                ++q; /* Now points to the value. */
 
                env_to_conf_item = find_env_to_conf(key);
@@ -527,7 +537,7 @@ conf_update_from_environment(struct conf *conf, char **errmsg)
                }
 
                if (!handle_conf_setting(
-                           conf, env_to_conf_item->conf_name, q, &errmsg2, true)) {
+                           conf, env_to_conf_item->conf_name, q, &errmsg2, true, negate)) {
                        *errmsg = format("%s: %s", key, errmsg2);
                        free(errmsg2);
                        free(key);
index 330e237c2f1da14b8b41c69a5d4f57d6c234d0bb..e687687d280516350254ef8b308a946619ffa1ee 100644 (file)
@@ -285,4 +285,21 @@ TEST(verify_dir_levels)
        conf_free(conf);
 }
 
+TEST(conf_update_from_environment)
+{
+       struct conf *conf = conf_create();
+       char *errmsg;
+
+       putenv("CCACHE_COMPRESS=1");
+       CHECK(conf_update_from_environment(conf, &errmsg));
+       CHECK(conf->compression);
+
+       unsetenv("CCACHE_COMPRESS");
+       putenv("CCACHE_NOCOMPRESS=1");
+       CHECK(conf_update_from_environment(conf, &errmsg));
+       CHECK(!conf->compression);
+
+       conf_free(conf);
+}
+
 TEST_SUITE_END