]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Expose commandline parser so that we can use it for --quiet,etc.
authorNick Mathewson <nickm@torproject.org>
Sun, 25 Aug 2013 16:49:16 +0000 (12:49 -0400)
committerNick Mathewson <nickm@torproject.org>
Fri, 13 Sep 2013 16:36:39 +0000 (12:36 -0400)
Fix for bug 9578.

changes/bug4647
src/or/config.c
src/or/config.h
src/or/main.c

index ed4c1ff319e8e3480e5f4ef661c59b2739afb36f..2ad08fec31140b3c81ee1a805963ef457106974c 100644 (file)
@@ -3,7 +3,8 @@
     - Use a single command-line parser for parsing torrc options on the
       command line and for finding special command-line options to avoid
       inconsistent behavior for torrc option arguments that have the same
-      names as command-line options. Fixes bugs 4647; bugfix on 0.0.9pre5.
+      names as command-line options. Fixes bugs 4647 and 9578; bugfix on
+      0.0.9pre5.
 
 
 
index de9c5eee1977811fe224bfe897b0fe8d2115c2e5..c1bbf7896fb30fb70e4de1d2daeb458eb9ce7f76 100644 (file)
@@ -1792,12 +1792,14 @@ options_act(const or_options_t *old_options)
 }
 
 /** Helper: Read a list of configuration options from the command line.  If
- * successful, put them in *<b>result</b>, put the commandline-only options in
- * *<b>cmdline_result</b>, and return 0; otherwise, return -1 and leave
- * *<b>result</b> and <b>cmdline_result</b> alone. */
-static int
-config_get_commandlines(int argc, char **argv, config_line_t **result,
-    config_line_t **cmdline_result)
+ * successful, or if ignore_errors is set, put them in *<b>result</b>, put the
+ * commandline-only options in *<b>cmdline_result</b>, and return 0;
+ * otherwise, return -1 and leave *<b>result</b> and <b>cmdline_result</b>
+ * alone. */
+int
+config_parse_commandline(int argc, char **argv, int ignore_errors,
+                         config_line_t **result,
+                         config_line_t **cmdline_result)
 {
   config_line_t *param = NULL;
 
@@ -1823,7 +1825,8 @@ config_get_commandlines(int argc, char **argv, config_line_t **result,
                !strcmp(argv[i],"--verify-config") ||
                !strcmp(argv[i],"--ignore-missing-torrc") ||
                !strcmp(argv[i],"--quiet") ||
-               !strcmp(argv[i],"--hush")) {
+               !strcmp(argv[i],"--hush") ||
+               !strcmp(argv[1],"--version")) {
       is_cmdline = 1;
       want_arg = 0;
     } else if (!strcmp(argv[i],"--nt-service") ||
@@ -1851,7 +1854,7 @@ config_get_commandlines(int argc, char **argv, config_line_t **result,
     }
 
     if (want_arg && i == argc-1) {
-      if (!strcmp(argv[i],"--hash-password")) {
+      if (!strcmp(argv[i],"--hash-password") || ignore_errors) {
         arg = strdup("");
       } else {
         log_warn(LD_CONFIG,"Command-line option '%s' with no value. Failing.",
@@ -3852,8 +3855,8 @@ options_init_from_torrc(int argc, char **argv)
   if (!global_cmdline_options) {
     /* Or we could redo the list every time we pass this place.
      * It does not really matter */
-    if (config_get_commandlines(argc, argv, &global_cmdline_options,
-        &cmdline_only_options) < 0) {
+    if (config_parse_commandline(argc, argv, 0, &global_cmdline_options,
+                                 &cmdline_only_options) < 0) {
       goto err;
     }
   }
index eb16e74610bd503d1ea3e2ec1fe17dbe86725750..0ed5a5b5f82e5628ee61ad295e9808e9f8df720b 100644 (file)
@@ -96,6 +96,10 @@ int init_cookie_authentication(const char *fname, const char *header,
 
 or_options_t *options_new(void);
 
+int config_parse_commandline(int argc, char **argv, int ignore_errors,
+                             config_line_t **result,
+                             config_line_t **cmdline_result);
+
 void config_register_addressmaps(const or_options_t *options);
 /* XXXX024 move to connection_edge.h */
 int addressmap_register_auto(const char *from, const char *to,
index 33e1c6437f57a820a2f37b2bed198d6c9a20a45c..e816a6667084f98a48d2a228f76f8bd3f721a36e 100644 (file)
@@ -21,6 +21,7 @@
 #include "circuituse.h"
 #include "command.h"
 #include "config.h"
+#include "confparse.h"
 #include "connection.h"
 #include "connection_edge.h"
 #include "connection_or.h"
@@ -2320,7 +2321,7 @@ int
 tor_init(int argc, char *argv[])
 {
   char buf[256];
-  int i, quiet = 0;
+  int quiet = 0;
   time_of_process_start = time(NULL);
   init_connection_lists();
   /* Have the log set up with our application name. */
@@ -2333,17 +2334,25 @@ tor_init(int argc, char *argv[])
   addressmap_init(); /* Init the client dns cache. Do it always, since it's
                       * cheap. */
 
+  {
   /* We search for the "quiet" option first, since it decides whether we
    * will log anything at all to the command line. */
-  for (i=1;i<argc;++i) {
-    if (!strcmp(argv[i], "--hush"))
-      quiet = 1;
-    if (!strcmp(argv[i], "--quiet"))
-      quiet = 2;
-    /* --version implies --quiet */
-    if (!strcmp(argv[i], "--version"))
-      quiet = 2;
+    config_line_t *opts = NULL, *cmdline_opts = NULL;
+    const config_line_t *cl;
+    (void) config_parse_commandline(argc, argv, 1, &opts, &cmdline_opts);
+    for (cl = cmdline_opts; cl; cl = cl->next) {
+      if (!strcmp(cl->key, "--hush"))
+        quiet = 1;
+      if (!strcmp(cl->key, "--quiet"))
+        quiet = 2;
+      /* --version implies --quiet */
+      if (!strcmp(cl->key, "--version"))
+        quiet = 2;
+    }
+    config_free_lines(opts);
+    config_free_lines(cmdline_opts);
   }
+
  /* give it somewhere to log to initially */
   switch (quiet) {
     case 2: