[or-cvs] r13942: options_init_from_torrc(): move code that looks for torrc in (tor/trunk/src/or)

weasel at seul.org weasel at seul.org
Mon Mar 10 12:41:29 UTC 2008


Author: weasel
Date: 2008-03-10 08:41:29 -0400 (Mon, 10 Mar 2008)
New Revision: 13942

Modified:
   tor/trunk/src/or/config.c
Log:
options_init_from_torrc(): move code that looks for torrc into its own function

Part of options_init_from_torrc()'s job was looking for -f flags (to specify
an alternate config file) on the command line, complaining if more than one
is given or the given does not exist.  If none is given then use the compiled-in
default location, accepting if it does not exist.  This logic has been moved
into its own function in an attemped to make options_init_from_torrc() easier
to deal with.


Modified: tor/trunk/src/or/config.c
===================================================================
--- tor/trunk/src/or/config.c	2008-03-10 12:41:26 UTC (rev 13941)
+++ tor/trunk/src/or/config.c	2008-03-10 12:41:29 UTC (rev 13942)
@@ -3523,6 +3523,57 @@
   return r;
 }
 
+/** Learn config file name from command line arguments, or use the default */
+char *
+find_torrc_filename(int argc, char **argv,
+                    int *using_default_torrc, int *ignore_missing_torrc)
+{
+  char *fname=NULL;
+  int i;
+
+  for (i = 1; i < argc; ++i) {
+    if (i < argc-1 && !strcmp(argv[i],"-f")) {
+      if (fname) {
+        log(LOG_WARN, LD_CONFIG, "Duplicate -f options on command line.");
+        tor_free(fname);
+      }
+#ifdef MS_WINDOWS
+      /* XXX one day we might want to extend expand_filename to work
+       * under Windows as well. */
+      fname = tor_strdup(argv[i+1]);
+#else
+      fname = expand_filename(argv[i+1]);
+#endif
+      *using_default_torrc = 0;
+      ++i;
+    } else if (!strcmp(argv[i],"--ignore-missing-torrc")) {
+      *ignore_missing_torrc = 1;
+    }
+  }
+
+  if (*using_default_torrc) {
+    /* didn't find one, try CONFDIR */
+    const char *dflt = get_default_conf_file();
+    if (dflt && file_status(dflt) == FN_FILE) {
+      fname = tor_strdup(dflt);
+    } else {
+#ifndef MS_WINDOWS
+      char *fn;
+      fn = expand_filename("~/.torrc");
+      if (fn && file_status(fn) == FN_FILE) {
+        fname = fn;
+      } else {
+        tor_free(fn);
+        fname = tor_strdup(dflt);
+      }
+#else
+      fname = tor_strdup(dflt);
+#endif
+    }
+  }
+  return fname;
+}
+
 /** Read a configuration file into <b>options</b>, finding the configuration
  * file location based on the command line.  After loading the options,
  * validate them for consistency, then take actions based on them.
@@ -3534,8 +3585,8 @@
   config_line_t *cl;
   char *cf=NULL, *fname=NULL, *errmsg=NULL;
   int i, retval;
-  int using_default_torrc;
-  int ignore_missing_torrc;
+  int using_default_torrc = 1;
+  int ignore_missing_torrc = 0;
   static char **backup_argv;
   static int backup_argc;
 
@@ -3583,49 +3634,8 @@
     }
   }
 
-  /* learn config file name */
-  fname = NULL;
-  using_default_torrc = 1;
-  ignore_missing_torrc = 0;
-  for (i = 1; i < argc; ++i) {
-    if (i < argc-1 && !strcmp(argv[i],"-f")) {
-      if (fname) {
-        log(LOG_WARN, LD_CONFIG, "Duplicate -f options on command line.");
-        tor_free(fname);
-      }
-#ifdef MS_WINDOWS
-      /* XXX one day we might want to extend expand_filename to work
-       * under Windows as well. */
-      fname = tor_strdup(argv[i+1]);
-#else
-      fname = expand_filename(argv[i+1]);
-#endif
-      using_default_torrc = 0;
-      ++i;
-    } else if (!strcmp(argv[i],"--ignore-missing-torrc")) {
-      ignore_missing_torrc = 1;
-    }
-  }
-  if (using_default_torrc) {
-    /* didn't find one, try CONFDIR */
-    const char *dflt = get_default_conf_file();
-    if (dflt && file_status(dflt) == FN_FILE) {
-      fname = tor_strdup(dflt);
-    } else {
-#ifndef MS_WINDOWS
-      char *fn;
-      fn = expand_filename("~/.torrc");
-      if (fn && file_status(fn) == FN_FILE) {
-        fname = fn;
-      } else {
-        tor_free(fn);
-        fname = tor_strdup(dflt);
-      }
-#else
-      fname = tor_strdup(dflt);
-#endif
-    }
-  }
+  fname = find_torrc_filename(argc, argv,
+                              &using_default_torrc, &ignore_missing_torrc);
   tor_assert(fname);
   log(LOG_DEBUG, LD_CONFIG, "Opening config file \"%s\"", fname);
 



More information about the tor-commits mailing list