]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
config: Add -o/--set-option command-line option
authorJoel Rosdahl <joel@rosdahl.net>
Tue, 26 Jul 2011 19:05:50 +0000 (21:05 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Tue, 26 Jul 2011 20:11:45 +0000 (22:11 +0200)
ccache.c
conf.c
test/test_conf.c

index ec618ac268e9f855f9078f8b8923ec37530a17b1..386c7d4c8826acbfe43c0f39ceed44f8466f27e6 100644 (file)
--- 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 cd90fce5ff703b44faf0f0898bb6a06972faef9e..efbb55ebb267239c49ab141361d159f445f2b534 100644 (file)
--- 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) {
index 53872f346f67318f90a4dda2a747be38dae6dadc..b7fa37cf548bf2fd277676568fc3e60b9465f6a4 100644 (file)
@@ -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