[tor-commits] [tor/master] Add a --dump-config option to help testing option parsing.

nickm at torproject.org nickm at torproject.org
Fri Sep 13 16:41:20 UTC 2013


commit aac4f30d234491d8325b55918647242bb7c40537
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Sep 3 10:30:50 2013 -0400

    Add a --dump-config option to help testing option parsing.
---
 changes/bug4647    |    7 +++++++
 src/or/config.c    |   31 +++++++++++++++++++++++++++----
 src/or/confparse.c |    4 ++--
 src/or/confparse.h |    2 +-
 src/or/control.c   |    2 +-
 src/or/main.c      |   39 ++++++++++++++++++++++++++++++++++++---
 src/or/or.h        |    2 +-
 7 files changed, 75 insertions(+), 12 deletions(-)

diff --git a/changes/bug4647 b/changes/bug4647
index 162a69c..f756a7d 100644
--- a/changes/bug4647
+++ b/changes/bug4647
@@ -9,3 +9,10 @@
     - No longer allow 'tor --hash-password' with no arguments. Fixes bug
       9573; bugfix on 0.0.9pre5.
 
+  o Minor features:
+
+    - Support a --dump-config optoin to dump some or all of the configured
+      options. Mainly useful for debugging the command-line option parsing
+      code.
+
+
diff --git a/src/or/config.c b/src/or/config.c
index 40bdf91..2a86dac 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1801,6 +1801,7 @@ static const struct {
   { "-f",                     1 },
   { "--defaults-torrc",       1 },
   { "--hash-password",        1 },
+  { "--dump-config",          1 },
   { "--list-fingerprint",     0 },
   { "--verify-config",        0 },
   { "--ignore-missing-torrc", 0 },
@@ -2268,10 +2269,29 @@ options_init(or_options_t *options)
  * include options that are the same as Tor's defaults.
  */
 char *
-options_dump(const or_options_t *options, int minimal)
+options_dump(const or_options_t *options, int how_to_dump)
 {
-  return config_dump(&options_format, global_default_options,
-                     options, minimal, 0);
+  const or_options_t *use_defaults;
+  int minimal;
+  switch (how_to_dump) {
+    case OPTIONS_DUMP_MINIMAL:
+      use_defaults = global_default_options;
+      minimal = 1;
+      break;
+    case OPTIONS_DUMP_DEFAULTS:
+      use_defaults = NULL;
+      minimal = 1;
+      break;
+    case OPTIONS_DUMP_ALL:
+      use_defaults = NULL;
+      minimal = 0;
+      break;
+    default:
+      log_warn(LD_BUG, "Bogus value for how_to_dump==%d", how_to_dump);
+      return NULL;
+  }
+
+  return config_dump(&options_format, use_defaults, options, minimal, 0);
 }
 
 /** Return 0 if every element of sl is a string holding a decimal
@@ -3894,6 +3914,9 @@ options_init_from_torrc(int argc, char **argv)
     } else if (!strcmp(p_index->key, "--hash-password")) {
       command = CMD_HASH_PASSWORD;
       command_arg = p_index->value;
+    } else if (!strcmp(p_index->key, "--dump-config")) {
+      command = CMD_DUMP_CONFIG;
+      command_arg = p_index->value;
     } else if (!strcmp(p_index->key, "--verify-config")) {
       command = CMD_VERIFY_CONFIG;
     }
@@ -6119,7 +6142,7 @@ write_configuration_file(const char *fname, const or_options_t *options)
       return -1;
   }
 
-  if (!(new_conf = options_dump(options, 1))) {
+  if (!(new_conf = options_dump(options, OPTIONS_DUMP_MINIMAL))) {
     log_warn(LD_BUG, "Couldn't get configuration string");
     goto err;
   }
diff --git a/src/or/confparse.c b/src/or/confparse.c
index 12ab0e4..f2ada4f 100644
--- a/src/or/confparse.c
+++ b/src/or/confparse.c
@@ -1074,8 +1074,8 @@ config_dump(const config_format_t *fmt, const void *default_options,
 
   /* XXX use a 1 here so we don't add a new log line while dumping */
   if (default_options == NULL) {
-    if (fmt->validate_fn(NULL, defaults_tmp, 1, &msg) < 0) {
-      log_err(LD_BUG, "Failed to validate default config.");
+    if (fmt->validate_fn(NULL, defaults_tmp, defaults_tmp, 1, &msg) < 0) {
+      log_err(LD_BUG, "Failed to validate default config: %s", msg);
       tor_free(msg);
       tor_assert(0);
     }
diff --git a/src/or/confparse.h b/src/or/confparse.h
index a1b6277..2cd6c49 100644
--- a/src/or/confparse.h
+++ b/src/or/confparse.h
@@ -71,7 +71,7 @@ typedef struct config_var_description_t {
 /** Type of a callback to validate whether a given configuration is
  * well-formed and consistent. See options_trial_assign() for documentation
  * of arguments. */
-typedef int (*validate_fn_t)(void*,void*,int,char**);
+typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
 
 /** Information on the keys, value types, key-to-struct-member mappings,
  * variable descriptions, validation functions, and abbreviations for a
diff --git a/src/or/control.c b/src/or/control.c
index 7034605..e97c18d 100644
--- a/src/or/control.c
+++ b/src/or/control.c
@@ -1406,7 +1406,7 @@ getinfo_helper_misc(control_connection_t *conn, const char *question,
   } else if (!strcmp(question, "config-defaults-file")) {
     *answer = tor_strdup(get_torrc_fname(1));
   } else if (!strcmp(question, "config-text")) {
-    *answer = options_dump(get_options(), 1);
+    *answer = options_dump(get_options(), OPTIONS_DUMP_MINIMAL);
   } else if (!strcmp(question, "info/names")) {
     *answer = list_getinfo_options();
   } else if (!strcmp(question, "dormant")) {
diff --git a/src/or/main.c b/src/or/main.c
index 598d099..48d8155 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -2343,10 +2343,12 @@ tor_init(int argc, char *argv[])
     for (cl = cmdline_opts; cl; cl = cl->next) {
       if (!strcmp(cl->key, "--hush"))
         quiet = 1;
-      if (!strcmp(cl->key, "--quiet"))
+      if (!strcmp(cl->key, "--quiet") ||
+          !strcmp(cl->key, "--dump-config"))
         quiet = 2;
-      /* --version, --digests, and --help imply --quiet */
+      /* --version, --digests, and --help imply --husth */
       if (!strcmp(cl->key, "--version") || !strcmp(cl->key, "--digests") ||
+          !strcmp(cl->key, "--list-torrc-options") ||
           !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help"))
         quiet = 1;
     }
@@ -2597,7 +2599,7 @@ do_list_fingerprint(void)
   const char *nickname = get_options()->Nickname;
   if (!server_mode(get_options())) {
     log_err(LD_GENERAL,
-            "Clients don't have long-term identity keys. Exiting.\n");
+            "Clients don't have long-term identity keys. Exiting.");
     return -1;
   }
   tor_assert(nickname);
@@ -2635,6 +2637,34 @@ do_hash_password(void)
   printf("16:%s\n",output);
 }
 
+/** Entry point for configuration dumping: write the configuration to
+ * stdout. */
+static int
+do_dump_config(void)
+{
+  const or_options_t *options = get_options();
+  const char *arg = options->command_arg;
+  int how;
+  char *opts;
+  if (!strcmp(arg, "short")) {
+    how = OPTIONS_DUMP_MINIMAL;
+  } else if (!strcmp(arg, "non-builtin")) {
+    how = OPTIONS_DUMP_DEFAULTS;
+  } else if (!strcmp(arg, "full")) {
+    how = OPTIONS_DUMP_ALL;
+  } else {
+    printf("%s is not a recognized argument to --dump-config. "
+           "Please select 'short', 'non-builtin', or 'full'", arg);
+    return -1;
+  }
+
+  opts = options_dump(options, how);
+  printf("%s", opts);
+  tor_free(opts);
+
+  return 0;
+}
+
 #if defined (WINCE)
 int
 find_flashcard_path(PWCHAR path, size_t size)
@@ -2752,6 +2782,9 @@ tor_main(int argc, char *argv[])
     printf("Configuration was valid\n");
     result = 0;
     break;
+  case CMD_DUMP_CONFIG:
+    result = do_dump_config();
+    break;
   case CMD_RUN_UNITTESTS: /* only set by test.c */
   default:
     log_warn(LD_BUG,"Illegal command number %d: internal error.",
diff --git a/src/or/or.h b/src/or/or.h
index 43e0212..bd038f7 100644
--- a/src/or/or.h
+++ b/src/or/or.h
@@ -3398,7 +3398,7 @@ typedef struct {
   /** What should the tor process actually do? */
   enum {
     CMD_RUN_TOR=0, CMD_LIST_FINGERPRINT, CMD_HASH_PASSWORD,
-    CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS
+    CMD_VERIFY_CONFIG, CMD_RUN_UNITTESTS, CMD_DUMP_CONFIG
   } command;
   char *command_arg; /**< Argument for command-line option. */
 





More information about the tor-commits mailing list