]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cconf: restructure, use common infrastructure
authorYehuda Sadeh <yehuda@hq.newdream.net>
Thu, 12 Mar 2009 17:48:24 +0000 (10:48 -0700)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Thu, 12 Mar 2009 17:48:24 +0000 (10:48 -0700)
src/cconf.cc
src/config.cc
src/config.h

index a4de351e201fb6491ea6d682d3c295a8d83a0c0d..18a7035f33df1622644304c7f0db809c5d2c7320 100644 (file)
@@ -11,7 +11,7 @@ using namespace std;
 
 #include "config.h"
 #include "common/ConfUtils.h"
-
+#include "common/common_init.h"
 
 const char *id = NULL, *type = NULL;
 char *name, *alt_name;
@@ -24,38 +24,34 @@ static void usage()
 
 int main(int argc, const char **argv) 
 {
-  const char *fname = NULL;
   const char *key = NULL, *defval = NULL;
   const char *list_sections = 0;
   char *val;
   int param = 0;
-  deque<const char*> args;
+  vector<const char*> args, nargs;
   deque<const char *> sections;
-  argv_to_deq(argc, argv, args);
-  env_to_deq(args);
+  argv_to_vec(argc, argv, args);
+  env_to_vec(args);
 
   if (args.size() < 2)
     usage();
 
   for (unsigned i=0; i<args.size(); i++) {
-    if (strcmp(args[i], "-c") == 0) {
-      if (i < args.size() - 1)
-        fname = args[++i];
-      else
-       usage();
-    } else if (strcmp(args[i], "-t") == 0) {
-      if (param == 0)
-          param++;
+    if (strcmp(args[i], "-t") == 0) {
       if (i < args.size() - 1)
         type = args[++i];
       else
        usage();
-    } else if (strcmp(args[i], "-i") == 0) {
-      if (i < args.size() - 1)
-        id = args[++i];
-      else
-       usage();
-    } else if (strcmp(args[i], "-l") == 0 ||
+    } else {
+       nargs.push_back(args[i]);
+    }
+  }
+  args.swap(nargs);
+
+  common_init(args, type);
+
+  for (unsigned i=0; i<args.size(); i++) {
+      if (strcmp(args[i], "-l") == 0 ||
               strcmp(args[i], "--list_sections") == 0) {
       if (i < args.size() - 1)
        list_sections = args[++i];
@@ -87,16 +83,13 @@ int main(int argc, const char **argv)
   if (!list_sections && (param < 1 || param > 3))
     usage();
 
-  if (!fname)
-    usage();
+  ConfFile *cf = conf_get_conf_file();
 
-  ConfFile cf(fname);
-  cf.set_post_process_func(conf_post_process_val);
-  parse_config_file(&cf, true, type, id);
+  assert(cf);
 
   if (list_sections) {
-    for (std::list<ConfSection*>::const_iterator p = cf.get_section_list().begin();
-        p != cf.get_section_list().end();
+    for (std::list<ConfSection*>::const_iterator p = cf->get_section_list().begin();
+        p != cf->get_section_list().end();
         p++) {
       if (strncmp(list_sections, (*p)->get_name().c_str(), strlen(list_sections)) == 0)
        cout << (*p)->get_name() << std::endl;
@@ -104,31 +97,8 @@ int main(int argc, const char **argv)
     return 0;
   }
 
-  if (id) {
-       name = (char *)malloc(strlen(type) + strlen(id) + 2);
-       sprintf(name, "%s.%s", type, id);
-       alt_name = (char *)malloc(strlen(type) + strlen(id) + 1);
-       sprintf(alt_name, "%s%s", type, id);
-  } else {
-       name = (char *)type;
-  }
-
-  g_conf.name = name;
-  g_conf.id = (char *)id;
-  g_conf.type = (char *)type;
-
-  if (type)
-    sections.push_front(type);
-
-  if (alt_name)
-    sections.push_front(alt_name);
-  if (name)
-    sections.push_front(name);
-
-  sections.push_back("global");
-
   for (unsigned i=0; i<sections.size(); i++) {
-    cf.read(sections[i], key, (char **)&val, NULL);
+    cf->read(sections[i], key, (char **)&val, NULL);
 
     if (val) {
       cout << val << std::endl;
index 6d176e8adae85568bd735ef59cfbccfd6f4bc79f..8090458221fcb76d546b58eaca156602235d94db 100644 (file)
@@ -40,6 +40,8 @@ atomic_t buffer_total_alloc;
 
 static bool show_config = false;
 
+static ConfFile *cf = NULL;
+
 /*
 struct foobar {
   foobar() { cerr << "config.cc init" << std::endl; }
@@ -937,21 +939,26 @@ void parse_startup_config_options(std::vector<const char*>& args, const char *mo
   args.swap(nargs);
   nargs.clear();
 
-  g_conf.type = strdup(module_type);
+  if (module_type) {
+    g_conf.type = strdup(module_type);
 
-  if (g_conf.id) {
-       g_conf.name = (char *)malloc(strlen(module_type) + strlen(g_conf.id) + 2);
+    if (g_conf.id) {
+       g_conf.name = (char *)malloc(strlen(module_type) + strlen(g_conf.id) + 2);
        sprintf(g_conf.name, "%s.%s", g_conf.type, g_conf.id);
-  } else {
+    } else {
        g_conf.name = g_conf.type;
+    }
   }
 
-  ConfFile cf(g_conf.conf);
+  if (cf)
+       delete cf;
 
-  parse_config_file(&cf, true, g_conf.type, g_conf.id);
+  cf = new ConfFile(g_conf.conf);
+
+  parse_config_file(cf, true, g_conf.type, g_conf.id);
 
   if (show_config) {
-    cf.dump();
+    cf->dump();
     exit(0);
   }
 }
@@ -993,6 +1000,11 @@ void generic_client_usage()
   exit(1);
 }
 
+ConfFile *conf_get_conf_file()
+{
+  return cf;
+}
+
 void parse_config_options(std::vector<const char*>& args)
 {
   int opt_len = sizeof(config_optionsp)/sizeof(config_option);
index 259f67e3a7fe9f3d332ee70a07a7f9c7f3bff1b8..e80b4c1bac651796886ba36112ec9df83cc64a1a 100644 (file)
@@ -373,6 +373,7 @@ void generic_server_usage();
 void generic_client_usage();
 
 class ConfFile;
+ConfFile *conf_get_conf_file();
 
 void parse_config_file(ConfFile *cf, bool auto_update, const char *module_type, const char *module_name);
 char *conf_post_process_val(const char *val);