From: Gabriel Scherer Date: Sun, 10 Jun 2018 10:53:04 +0000 (+0200) Subject: New command-line option -k/--get-config prints the value of a config key X-Git-Tag: v3.5~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e67eb20f518a9bf85938ab9cbd36a7f321104014;p=thirdparty%2Fccache.git New command-line option -k/--get-config prints the value of a config key We print exactly the value of the configuration setting, rather than a full item pair "(origin) key = value" as --print-config does. This is useful for scripting. For example, $(ccache -k cache_dir) gives the cache directory path. --- diff --git a/src/ccache.c b/src/ccache.c index 0665582fd..1a9ba146f 100644 --- a/src/ccache.c +++ b/src/ccache.c @@ -66,6 +66,7 @@ static const char USAGE_TEXT[] = " -C, --clear clear the cache completely (except configuration)\n" " -F, --max-files=N set maximum number of files in cache to N (use 0 for\n" " no limit)\n" + " -k, --get-config=K get the value of the configuration key K\n" " -M, --max-size=SIZE set maximum size of cache to SIZE (use 0 for no\n" " limit); available suffixes: k, M, G, T (decimal) and\n" " Ki, Mi, Gi, Ti (binary); default suffix: G\n" @@ -3544,6 +3545,7 @@ ccache_main_options(int argc, char *argv[]) {"cleanup", no_argument, 0, 'c'}, {"clear", no_argument, 0, 'C'}, {"dump-manifest", required_argument, 0, DUMP_MANIFEST}, + {"get-config", required_argument, 0, 'k'}, {"hash-file", required_argument, 0, HASH_FILE}, {"help", no_argument, 0, 'h'}, {"max-files", required_argument, 0, 'F'}, @@ -3557,7 +3559,8 @@ ccache_main_options(int argc, char *argv[]) }; int c; - while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "cCk:hF:M:o:psVz", options, NULL)) + != -1) { switch (c) { case DUMP_MANIFEST: manifest_dump(optarg, stdout); @@ -3595,6 +3598,16 @@ ccache_main_options(int argc, char *argv[]) fputs(USAGE_TEXT, stdout); x_exit(0); + case 'k': // --get-config + { + initialize(); + char *errmsg; + if (!conf_print_value(conf, optarg, stdout, &errmsg)) { + fatal("%s", errmsg); + } + } + break; + case 'F': // --max-files { initialize(); diff --git a/src/conf.c b/src/conf.c index 2bf96bcb4..404d2d7f7 100644 --- a/src/conf.c +++ b/src/conf.c @@ -629,6 +629,22 @@ conf_set_value_in_file(const char *path, const char *key, const char *value, return true; } +bool +conf_print_value(struct conf *conf, const char *key, + FILE *file, char **errmsg) +{ + const struct conf_item *item = find_conf(key); + if (!item) { + *errmsg = format("unknown configuration option \"%s\"", key); + return false; + } + void *value = (char *)conf + item->offset; + char *str = (char *)item->formatter(value); + fprintf(file, "%s\n", str); + free(str); + return true; +} + static bool print_item(struct conf *conf, const char *key, void (*printer)(const char *descr, const char *origin, diff --git a/src/conf.h b/src/conf.h index b0ace1c29..9c9e28f42 100644 --- a/src/conf.h +++ b/src/conf.h @@ -45,6 +45,8 @@ struct conf *conf_create(void); void conf_free(struct conf *conf); bool conf_read(struct conf *conf, const char *path, char **errmsg); bool conf_update_from_environment(struct conf *conf, char **errmsg); +bool conf_print_value(struct conf *conf, const char *key, + FILE *file, char **errmsg); bool conf_set_value_in_file(const char *path, const char *key, const char *value, char **errmsg); bool conf_print_items(struct conf *conf, diff --git a/unittest/test_conf.c b/unittest/test_conf.c index 2028ddc8c..f3d63da98 100644 --- a/unittest/test_conf.c +++ b/unittest/test_conf.c @@ -383,6 +383,50 @@ TEST(conf_set_existing_value) CHECK_STR_EQ_FREE2("path = vanilla\nstats = chocolate\n", data); } +TEST(conf_print_existing_value) +{ + struct conf *conf = conf_create(); + conf->max_files = 42; + char *errmsg; + { + FILE *log = fopen("log", "w"); + CHECK(log); + CHECK(conf_print_value(conf, "max_files", log, &errmsg)); + fclose(log); + } + { + FILE *log = fopen("log", "r"); + CHECK(log); + char buf[100]; + CHECK(fgets(buf, 100, log)); + CHECK_STR_EQ("42\n", buf); + fclose(log); + } + conf_free(conf); +} + +TEST(conf_print_unknown_value) +{ + struct conf *conf = conf_create(); + char *errmsg; + { + FILE *log = fopen("log", "w"); + CHECK(log); + CHECK(!conf_print_value(conf, "foo", log, &errmsg)); + CHECK_STR_EQ_FREE2("unknown configuration option \"foo\"", + errmsg); + fclose(log); + } + { + FILE *log = fopen("log", "r"); + CHECK(log); + char buf[100]; + CHECK(!fgets(buf, 100, log)); + fclose(log); + } + conf_free(conf); +} + TEST(conf_print_items) { size_t i;