From d13bbc5812c5322ae64d3b135ac2c6b6e4d3f60e Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sat, 16 Jul 2011 17:11:36 +0200 Subject: [PATCH] config: Handle CCACHE_NO* environment variables --- conf.c | 20 +++++++++++++++----- test/test_conf.c | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/conf.c b/conf.c index 95cfa2f5e..0fdab63db 100644 --- 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); diff --git a/test/test_conf.c b/test/test_conf.c index 330e237c2..e687687d2 100644 --- a/test/test_conf.c +++ b/test/test_conf.c @@ -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 -- 2.47.3