From 8ff7cbecc6d30c690dd0857a7ce2ee6d498c6ca0 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 26 Jul 2011 21:05:50 +0200 Subject: [PATCH] config: Add -o/--set-option command-line option --- ccache.c | 21 ++++++++++++++++++++- conf.c | 7 +++++++ test/test_conf.c | 12 ++++++------ 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/ccache.c b/ccache.c index ec618ac26..386c7d4c8 100644 --- a/ccache.c +++ b/ccache.c @@ -61,6 +61,7 @@ static const char USAGE_TEXT[] = " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n" " limit; available suffixes: G, M and K; default\n" " suffix: G)\n" +" -o, --set-option=K=V set configuration option K to V\n" " -s, --show-stats show statistics summary\n" " -z, --zero-stats zero statistics counters\n" "\n" @@ -2097,13 +2098,14 @@ ccache_main_options(int argc, char *argv[]) {"help", no_argument, 0, 'h'}, {"max-files", required_argument, 0, 'F'}, {"max-size", required_argument, 0, 'M'}, + {"set-option", required_argument, 0, 'o'}, {"show-stats", no_argument, 0, 's'}, {"version", no_argument, 0, 'V'}, {"zero-stats", no_argument, 0, 'z'}, {0, 0, 0, 0} }; - while ((c = getopt_long(argc, argv, "cChF:M:sVz", options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "cChF:M:o:sVz", options, NULL)) != -1) { switch (c) { case DUMP_MANIFEST: manifest_dump(optarg, stdout); @@ -2165,6 +2167,23 @@ ccache_main_options(int argc, char *argv[]) } break; + case 'o': /* --set-option */ + { + char *errmsg, *key, *value, *p; + initialize(); + p = strchr(optarg, '='); + if (!p) { + fatal("missing equal sign in \"%s\"", optarg); + } + key = x_strndup(optarg, p - optarg); + value = p + 1; + if (!conf_set_value_in_file(primary_config_path, key, value, &errmsg)) { + fatal("%s", errmsg); + } + free(key); + } + break; + case 's': /* --show-stats */ initialize(); stats_summary(conf); diff --git a/conf.c b/conf.c index cd90fce5f..efbb55ebb 100644 --- a/conf.c +++ b/conf.c @@ -563,6 +563,13 @@ conf_set_value_in_file(const char *path, const char *key, const char *value, char *outpath; char buf[10000]; bool found; + const struct conf_item *item; + + item = find_conf(key); + if (!item) { + *errmsg = format("unknown configuration option \"%s\"", key); + return false; + } infile = fopen(path, "r"); if (!infile) { diff --git a/test/test_conf.c b/test/test_conf.c index 53872f346..b7fa37cf5 100644 --- a/test/test_conf.c +++ b/test/test_conf.c @@ -307,11 +307,11 @@ TEST(conf_set_new_value) char *errmsg; char *data; - create_file("ccache.conf", "flavour = vanilla\n"); - CHECK(conf_set_value_in_file("ccache.conf", "topping", "chocolate", &errmsg)); + create_file("ccache.conf", "path = vanilla\n"); + CHECK(conf_set_value_in_file("ccache.conf", "stats", "chocolate", &errmsg)); data = read_text_file("ccache.conf", 0); CHECK(data); - CHECK_STR_EQ_FREE2("flavour = vanilla\ntopping = chocolate\n", data); + CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data); } TEST(conf_set_existing_value) @@ -319,11 +319,11 @@ TEST(conf_set_existing_value) char *errmsg; char *data; - create_file("ccache.conf", "flavour = chocolate\ntopping = chocolate\n"); - CHECK(conf_set_value_in_file("ccache.conf", "flavour", "vanilla", &errmsg)); + create_file("ccache.conf", "path = chocolate\nstats = chocolate\n"); + CHECK(conf_set_value_in_file("ccache.conf", "path", "vanilla", &errmsg)); data = read_text_file("ccache.conf", 0); CHECK(data); - CHECK_STR_EQ_FREE2("flavour = vanilla\ntopping = chocolate\n", data); + CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data); } TEST_SUITE_END -- 2.47.3