[Olsr-dev] [PATCH v1 23/43] conf: move loadConfig function here from main

Ferry Huberts (spam-protected)
Wed Nov 11 17:21:56 CET 2015


From: Ferry Huberts <(spam-protected)>

And refactor it to not invoke olsr_exit

Signed-off-by: Ferry Huberts <(spam-protected)>
---
 src/cfgparser/olsrd_conf.c | 90 ++++++++++++++++++++++++++++++++++++++++++++
 src/cfgparser/olsrd_conf.h |  2 +
 src/main.c                 | 93 ++--------------------------------------------
 3 files changed, 96 insertions(+), 89 deletions(-)

diff --git a/src/cfgparser/olsrd_conf.c b/src/cfgparser/olsrd_conf.c
index c97a6b0..3cf31c4 100644
--- a/src/cfgparser/olsrd_conf.c
+++ b/src/cfgparser/olsrd_conf.c
@@ -58,6 +58,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <sys/stat.h>
 #ifdef __linux__
 #include <linux/types.h>
 #include <linux/rtnetlink.h>
@@ -139,6 +140,95 @@ olsr_startup_sleep(int seconds __attribute__((unused))) {
 
 #endif /* MAKEBIN */
 
+/**
+ * loads a config file
+ * @return <0 if load failed, 0 otherwise
+ */
+static int
+olsrmain_load_config(char *file) {
+  struct stat statbuf;
+
+  if (stat(file, &statbuf) < 0) {
+    fprintf(stderr, "Could not find specified config file %s!\n%s\n\n",
+        file, strerror(errno));
+    return -1;
+  }
+
+  if (olsrd_parse_cnf(file) < 0) {
+    fprintf(stderr, "Error while reading config file %s!\n", file);
+    return -1;
+  }
+  return 0;
+}
+
+/*
+ * Set configfile name and
+ * check if a configfile name was given as parameter
+ */
+bool loadConfig(int *argc, char *argv[], char * conf_file_name, int conf_file_name_size) {
+  bool loadedConfig = false;
+  int i;
+
+  /* setup the default olsrd configuration file name in conf_file_name */
+#ifdef _WIN32
+  size_t len = 0;
+
+#ifndef WINCE
+  /* get the current directory */
+  GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
+#else /* WINCE */
+  conf_file_name[0] = '\0';
+#endif /* WINCE */
+
+  len = strlen(conf_file_name);
+  if (!len || (conf_file_name[len - 1] != '\\')) {
+    conf_file_name[len++] = '\\';
+  }
+
+  strscpy(conf_file_name + len, "olsrd.conf", sizeof(conf_file_name) - len);
+#else /* _WIN32 */
+  strscpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, conf_file_name_size);
+#endif /* _WIN32 */
+
+  /* get the default configuration */
+  olsr_cnf = olsrd_get_default_cnf(strdup(conf_file_name));
+
+  /* scan for -f configFile arguments */
+  for (i = 1; i < (*argc - 1);) {
+    if (strcmp(argv[i], "-f") == 0) {
+      /* setup the provided olsrd configuration file name in conf_file_name */
+      strscpy(conf_file_name, argv[i + 1], conf_file_name_size);
+
+      /* remove -f confgFile arguments from argc and argv */
+      if ((i + 2) < *argc) {
+        memmove(&argv[i], &argv[i + 2], sizeof(*argv) * (*argc - i - 1));
+      }
+      *argc -= 2;
+
+      /* load the config from the file */
+      if (olsrmain_load_config(conf_file_name) < 0) {
+        return false;
+      }
+
+      loadedConfig = true;
+    } else {
+      i++;
+    }
+  }
+
+  /* set up configuration prior to processing command-line options */
+  if (!loadedConfig && olsrmain_load_config(conf_file_name) == 0) {
+    loadedConfig = true;
+  }
+
+  if (!loadedConfig) {
+    olsrd_free_cnf(olsr_cnf);
+    olsr_cnf = olsrd_get_default_cnf(strdup(conf_file_name));
+  }
+
+  return true;
+}
+
 int
 olsrd_parse_cnf(const char *filename)
 {
diff --git a/src/cfgparser/olsrd_conf.h b/src/cfgparser/olsrd_conf.h
index dec02d7..ba9af54 100644
--- a/src/cfgparser/olsrd_conf.h
+++ b/src/cfgparser/olsrd_conf.h
@@ -59,6 +59,8 @@ struct conf_token {
 #define DEFAULT_STR(val) \
 (((!defcnf) && ((*((uint8_t *)(&cnfi->val)))==0))?" (d)":"")
 
+bool loadConfig(int *argc, char *argv[], char * conf_file_name, int conf_file_name_size);
+
 void set_default_cnf(struct olsrd_config *, char * configuration_file);
 
 void set_derived_cnf(struct olsrd_config * olsr_cnf);
diff --git a/src/main.c b/src/main.c
index 078eb0d..70d225c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -154,93 +154,6 @@ static void writePidFile(void) {
 }
 
 /**
- * loads a config file
- * @return <0 if load failed, 0 otherwise
- */
-static int
-olsrmain_load_config(char *file) {
-  struct stat statbuf;
-
-  if (stat(file, &statbuf) < 0) {
-    fprintf(stderr, "Could not find specified config file %s!\n%s\n\n",
-        file, strerror(errno));
-    return -1;
-  }
-
-  if (olsrd_parse_cnf(file) < 0) {
-    fprintf(stderr, "Error while reading config file %s!\n", file);
-    return -1;
-  }
-  return 0;
-}
-
-/*
- * Set configfile name and
- * check if a configfile name was given as parameter
- */
-static void loadConfig(int *argc, char *argv[], char * conf_file_name, int conf_file_name_size) {
-  bool loadedConfig = false;
-  int i;
-
-  /* setup the default olsrd configuration file name in conf_file_name */
-#ifdef _WIN32
-  size_t len = 0;
-
-#ifndef WINCE
-  /* get the current directory */
-  GetWindowsDirectory(conf_file_name, FILENAME_MAX - 11);
-#else /* WINCE */
-  conf_file_name[0] = '\0';
-#endif /* WINCE */
-
-  len = strlen(conf_file_name);
-  if (!len || (conf_file_name[len - 1] != '\\')) {
-    conf_file_name[len++] = '\\';
-  }
-
-  strscpy(conf_file_name + len, "olsrd.conf", sizeof(conf_file_name) - len);
-#else /* _WIN32 */
-  strscpy(conf_file_name, OLSRD_GLOBAL_CONF_FILE, conf_file_name_size);
-#endif /* _WIN32 */
-
-  /* get the default configuration */
-  olsr_cnf = olsrd_get_default_cnf(strdup(conf_file_name));
-
-  /* scan for -f configFile arguments */
-  for (i = 1; i < (*argc - 1);) {
-    if (strcmp(argv[i], "-f") == 0) {
-      /* setup the provided olsrd configuration file name in conf_file_name */
-      strscpy(conf_file_name, argv[i + 1], conf_file_name_size);
-
-      /* remove -f confgFile arguments from argc and argv */
-      if ((i + 2) < *argc) {
-        memmove(&argv[i], &argv[i + 2], sizeof(*argv) * (*argc - i - 1));
-      }
-      *argc -= 2;
-
-      /* load the config from the file */
-      if (olsrmain_load_config(conf_file_name) < 0) {
-        olsr_exit(NULL, EXIT_FAILURE);
-      }
-
-      loadedConfig = true;
-    } else {
-      i++;
-    }
-  }
-
-  /* set up configuration prior to processing command-line options */
-  if (!loadedConfig && olsrmain_load_config(conf_file_name) == 0) {
-    loadedConfig = true;
-  }
-
-  if (!loadedConfig) {
-    olsrd_free_cnf(olsr_cnf);
-    olsr_cnf = olsrd_get_default_cnf(strdup(conf_file_name));
-  }
-}
-
-/**
  * Main entrypoint
  */
 
@@ -317,7 +230,9 @@ int main(int argc, char *argv[]) {
   olsr_openlog("olsrd");
 
   /* load the configuration */
-  loadConfig(&argcLocal, argv, conf_file_name, sizeof(conf_file_name));
+  if (!loadConfig(&argcLocal, argv, conf_file_name, sizeof(conf_file_name))) {
+    olsr_exit(NULL, EXIT_FAILURE);
+  }
 
   /* process arguments */
   {
@@ -328,7 +243,7 @@ int main(int argc, char *argv[]) {
     }
 
     /* Process olsrd options */
-  if (olsr_process_arguments(argcLocal, argv, olsr_cnf, default_ifcnf) < 0) {
+    if (olsr_process_arguments(argcLocal, argv, olsr_cnf, default_ifcnf) < 0) {
       print_usage(true);
       free(default_ifcnf);
       olsr_exit(NULL, EXIT_FAILURE);
-- 
2.5.0




More information about the Olsr-dev mailing list