[tor-commits] [tor/master] Use commandline parser for other options

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


commit a1096fe1802ee3585dd6296b50085017e1928edf
Author: Nick Mathewson <nickm at torproject.org>
Date:   Sun Aug 25 12:59:38 2013 -0400

    Use commandline parser for other options
    
    These were previously allowed only in the initial position:
      --help, -h , --version, --digests, --list-torrc-options
---
 src/or/config.c    |   36 +++++++++++++++++++++---------------
 src/or/confparse.c |   15 +++++++++++++++
 src/or/confparse.h |    2 ++
 src/or/main.c      |    5 +++--
 4 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/src/or/config.c b/src/or/config.c
index c1bbf78..e6adcc4 100644
--- a/src/or/config.c
+++ b/src/or/config.c
@@ -1826,7 +1826,11 @@ config_parse_commandline(int argc, char **argv, int ignore_errors,
                !strcmp(argv[i],"--ignore-missing-torrc") ||
                !strcmp(argv[i],"--quiet") ||
                !strcmp(argv[i],"--hush") ||
-               !strcmp(argv[1],"--version")) {
+               !strcmp(argv[i],"--version") ||
+               !strcmp(argv[i],"-h") ||
+               !strcmp(argv[i],"--help") ||
+               !strcmp(argv[i],"--list-torrc-options") ||
+               !strcmp(argv[i],"--digests")) {
       is_cmdline = 1;
       want_arg = 0;
     } else if (!strcmp(argv[i],"--nt-service") ||
@@ -3830,37 +3834,39 @@ options_init_from_torrc(int argc, char **argv)
     argv = backup_argv;
     argc = backup_argc;
   }
-  if (argc > 1 && (!strcmp(argv[1], "-h") || !strcmp(argv[1],"--help"))) {
+
+  /* Go through command-line variables */
+  if (!global_cmdline_options) {
+    /* Or we could redo the list every time we pass this place.
+     * It does not really matter */
+    if (config_parse_commandline(argc, argv, 0, &global_cmdline_options,
+                                 &cmdline_only_options) < 0) {
+      goto err;
+    }
+  }
+
+  if (config_line_find(cmdline_only_options, "-h") ||
+      config_line_find(cmdline_only_options, "--help")) {
     print_usage();
     exit(0);
   }
-  if (argc > 1 && !strcmp(argv[1], "--list-torrc-options")) {
+  if (config_line_find(cmdline_only_options, "--list-torrc-options")) {
     /* For documenting validating whether we've documented everything. */
     list_torrc_options();
     exit(0);
   }
 
-  if (argc > 1 && (!strcmp(argv[1],"--version"))) {
+  if (config_line_find(cmdline_only_options, "--version")) {
     printf("Tor version %s.\n",get_version());
     exit(0);
   }
-  if (argc > 1 && (!strcmp(argv[1],"--digests"))) {
+  if (config_line_find(cmdline_only_options, "--digests")) {
     printf("Tor version %s.\n",get_version());
     printf("%s", libor_get_digests());
     printf("%s", tor_get_digests());
     exit(0);
   }
 
-  /* Go through command-line variables */
-  if (!global_cmdline_options) {
-    /* Or we could redo the list every time we pass this place.
-     * It does not really matter */
-    if (config_parse_commandline(argc, argv, 0, &global_cmdline_options,
-                                 &cmdline_only_options) < 0) {
-      goto err;
-    }
-  }
-
   command = CMD_RUN_TOR;
   for (p_index = cmdline_only_options; p_index; p_index = p_index->next) {
     if (!strcmp(p_index->key,"--list-fingerprint")) {
diff --git a/src/or/confparse.c b/src/or/confparse.c
index 4105579..12ab0e4 100644
--- a/src/or/confparse.c
+++ b/src/or/confparse.c
@@ -79,6 +79,21 @@ config_line_append(config_line_t **lst,
   (*lst) = newline;
 }
 
+/** Return the line in <b>lines</b> whose key is exactly <b>key</b>, or NULL
+ * if no such key exists. For handling commandline-only options only; other
+ * options should be looked up in the appropriate data structure. */
+const config_line_t *
+config_line_find(const config_line_t *lines,
+                 const char *key)
+{
+  const config_line_t *cl;
+  for (cl = lines; cl; cl = cl->next) {
+    if (!strcmp(cl->key, key))
+      return cl;
+  }
+  return NULL;
+}
+
 /** Helper: parse the config string and strdup into key/value
  * strings. Set *result to the list, or NULL if parsing the string
  * failed.  Return 0 on success, -1 on failure. Warn and ignore any
diff --git a/src/or/confparse.h b/src/or/confparse.h
index 924ee0d..a1b6277 100644
--- a/src/or/confparse.h
+++ b/src/or/confparse.h
@@ -103,6 +103,8 @@ void *config_new(const config_format_t *fmt);
 void config_line_append(config_line_t **lst,
                         const char *key, const char *val);
 config_line_t *config_lines_dup(const config_line_t *inp);
+const config_line_t *config_line_find(const config_line_t *lines,
+                                      const char *key);
 void config_free(const config_format_t *fmt, void *options);
 int config_lines_eq(config_line_t *a, config_line_t *b);
 int config_count_key(const config_line_t *a, const char *key);
diff --git a/src/or/main.c b/src/or/main.c
index e816a66..22519f0 100644
--- a/src/or/main.c
+++ b/src/or/main.c
@@ -2345,8 +2345,9 @@ tor_init(int argc, char *argv[])
         quiet = 1;
       if (!strcmp(cl->key, "--quiet"))
         quiet = 2;
-      /* --version implies --quiet */
-      if (!strcmp(cl->key, "--version"))
+      /* --version and --help imply --quiet */
+      if (!strcmp(cl->key, "--version") ||
+          !strcmp(cl->key, "-h") || !strcmp(cl->key, "--help"))
         quiet = 2;
     }
     config_free_lines(opts);





More information about the tor-commits mailing list