From 2104af9774aa568030f3eaac57d88459486d0740 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Tue, 26 Jul 2011 22:10:31 +0200 Subject: [PATCH] config: Add -p/--print-config option --- ccache.c | 19 ++++++++++++++++++- conf.c | 44 ++++++++++++++++++++++++++------------------ test/test_conf.c | 9 ++++----- 3 files changed, 48 insertions(+), 24 deletions(-) diff --git a/ccache.c b/ccache.c index 386c7d4c8..920a7d97a 100644 --- a/ccache.c +++ b/ccache.c @@ -62,6 +62,7 @@ static const char USAGE_TEXT[] = " limit; available suffixes: G, M and K; default\n" " suffix: G)\n" " -o, --set-option=K=V set configuration option K to V\n" +" -p, --print-config print current configuration options\n" " -s, --show-stats show statistics summary\n" " -z, --zero-stats zero statistics counters\n" "\n" @@ -2081,6 +2082,16 @@ ccache(int argc, char *argv[]) failed(); } +static void +conf_printer(const char *s, void *context) +{ + if (context == NULL) { + cc_log("%s\n", s); + } else { + fprintf(context, "%s\n", s); + } +} + /* the main program when not doing a compile */ static int ccache_main_options(int argc, char *argv[]) @@ -2099,13 +2110,14 @@ ccache_main_options(int argc, char *argv[]) {"max-files", required_argument, 0, 'F'}, {"max-size", required_argument, 0, 'M'}, {"set-option", required_argument, 0, 'o'}, + {"print-config", no_argument, 0, 'p'}, {"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:o:sVz", options, NULL)) != -1) { + while ((c = getopt_long(argc, argv, "cChF:M:o:psVz", options, NULL)) != -1) { switch (c) { case DUMP_MANIFEST: manifest_dump(optarg, stdout); @@ -2184,6 +2196,11 @@ ccache_main_options(int argc, char *argv[]) } break; + case 'p': /* --print-config */ + initialize(); + conf_print_items(conf, conf_printer, stdout); + break; + case 's': /* --show-stats */ initialize(); stats_summary(conf); diff --git a/conf.c b/conf.c index 5cd73928a..a79001bb0 100644 --- a/conf.c +++ b/conf.c @@ -60,21 +60,6 @@ parse_env_string(const char *str, void *result, char **errmsg) return *value; } -static bool -parse_octal(const char *str, void *result, char **errmsg) -{ - unsigned *value = (unsigned *)result; - char *endptr; - errno = 0; - *value = strtoul(str, &endptr, 8); - if (errno == 0 && *str != '\0' && *endptr == '\0') { - return true; - } else { - *errmsg = format("not an octal integer: \"%s\"", str); - return false; - } -} - static bool parse_size(const char *str, void *result, char **errmsg) { @@ -129,6 +114,25 @@ parse_string(const char *str, void *result, char **errmsg) return true; } +static bool +parse_umask(const char *str, void *result, char **errmsg) +{ + unsigned *value = (unsigned *)result; + char *endptr; + if (str_eq(str, "")) { + *value = UINT_MAX; + return true; + } + errno = 0; + *value = strtoul(str, &endptr, 8); + if (errno == 0 && *str != '\0' && *endptr == '\0') { + return true; + } else { + *errmsg = format("not an octal integer: \"%s\"", str); + return false; + } +} + static bool parse_unsigned(const char *str, void *result, char **errmsg) { @@ -206,7 +210,7 @@ static const struct conf_item conf_items[] = { ITEM(sloppiness, sloppiness), ITEM(stats, bool), ITEM(temporary_dir, env_string), - ITEM(umask, octal), + ITEM(umask, umask), ITEM(unify, bool) }; @@ -710,8 +714,12 @@ bool conf_print_items(struct conf *conf, reformat(&s, "temporary_dir = %s", conf->temporary_dir); printer(s, context); - reformat(&s, "umask = %03o", conf->umask); - printer(s, context); + if (conf->umask == UINT_MAX) { + printer("umask = ", context); + } else { + reformat(&s, "umask = %03o", conf->umask); + printer(s, context); + } reformat(&s, "unify = %s", conf->unify ? "true" : "false"); printer(s, context); diff --git a/test/test_conf.c b/test/test_conf.c index 2ad6d80b7..f2f4e5ae7 100644 --- a/test/test_conf.c +++ b/test/test_conf.c @@ -210,14 +210,13 @@ TEST(conf_read_invalid_env_string) conf_free(conf); } -TEST(conf_read_invalid_octal) +TEST(conf_read_empty_umask) { struct conf *conf = conf_create(); char *errmsg; - create_file("ccache.conf", "umask = 890x"); - CHECK(!conf_read(conf, "ccache.conf", &errmsg)); - CHECK_STR_EQ_FREE2("ccache.conf:1: not an octal integer: \"890x\"", - errmsg); + create_file("ccache.conf", "umask = "); + CHECK(conf_read(conf, "ccache.conf", &errmsg)); + CHECK_INT_EQ(conf->umask, UINT_MAX); conf_free(conf); } -- 2.47.3