]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
config: read configuration file before parsing command line
authorYehuda Sadeh <yehuda@hq.newdream.net>
Fri, 23 Jan 2009 20:40:16 +0000 (12:40 -0800)
committerYehuda Sadeh <yehuda@hq.newdream.net>
Fri, 23 Jan 2009 20:40:16 +0000 (12:40 -0800)
also does not write configuration unless specified

src/common/ConfUtils.cc
src/config.cc
src/config.h

index 55979846b98a5f8de1fbd653032fc3018ed7ead5..2b300c8c78a886d9f68ff40c6b64c6087c64eec9 100644 (file)
@@ -25,6 +25,7 @@ struct ltstr
 
 static const char *_def_delim=" \t\n\r";
 static const char *_eq_delim="= \t\n\r";
+static const char *_eq_nospace_delim="=\t\n\r";
 static const char *_eol_delim="\n\r";
 /* static const char *_pr_delim="[] \t\n\r"; */
 
@@ -40,7 +41,7 @@ static int is_delim(char c, const char *delim)
        return 0;
 }
 
-char *get_next_tok(char *str, const char *delim, int alloc, char **p)
+static char *get_next_tok(char *str, const char *delim, int alloc, char **p)
 {
        int i=0;
        char *out;
@@ -77,9 +78,110 @@ char *get_next_tok(char *str, const char *delim, int alloc, char **p)
        return out;
 }
 
+static char *get_next_name(char *str, int alloc, char **p)
+{
+       int i=0;
+       char *out;
+
+       while (*str && is_delim(*str, _def_delim)) {
+               str++;
+       }
+
+       if (*str == '"') {
+               while (str[i] && !is_delim(str[i], _eol_delim)) {
+                       i++;
+                       if (str[i] == '"') {
+                               i++;
+                               break;
+                       }
+               }
+       } else {
+               while (str[i] && !is_delim(str[i], _eq_nospace_delim)) {
+                       i++;
+               }
+
+               while (str[i] && is_delim(str[i], _def_delim)) {
+                       i--;
+               }
+       }
+
+       if (alloc) {
+               out = (char *)malloc(i+1);
+               memcpy(out, str, i);
+               out[i] = '\0';
+       } else {
+               out = str;
+       }
+
+       if (p)
+               *p = &str[i];
+
+       return out;
+}
+
+static char *str_trim(char *str)
+{
+       char *head = str;
+       char *tail;
+
+       while (*head && is_delim(*head, _def_delim))
+               ++head;
+
+       head = strdup(head);
+
+       if (!(*head))
+               goto done;
+
+       tail = &head[strlen(head)-1];
+
+       while (*tail && is_delim(*tail, _def_delim))
+               --tail;
+
+       *(tail + 1) = '\0';
+
+done:
+       return head;
+}
+
+/*
+ * normalizes a var name, removes extra spaces; e.g., 'foo  bar' -> 'foo bar'
+ */
+static char *normalize_name(char *name)
+{
+       char *newname, *p;
+       int i, len;
+       int last_delim = 0, had_delim = 0, had_non_delim = 0;
+
+       len = strlen(name);
+       newname = (char *)malloc(len + 1);
+
+       p = newname;
+
+       for (i=0; i < len; i++) {
+               int now_delim = is_delim(name[i], _def_delim);
+
+               if (!now_delim) {
+
+                       if (had_delim && had_non_delim)
+                               *p++ = ' ';
+
+                       *p++ = name[i];
+
+                       had_non_delim = 1;
+                       had_delim = 0;
+               } else {
+                       had_delim = 1;
+               }
+
+               last_delim = now_delim;
+       }
+
+       return newname;
+}
+
 #define MAX_LINE 256
 
-char *get_next_delim(char *str, const char *delim, int alloc, char **p)
+static char *get_next_delim(char *str, const char *delim, int alloc, char **p)
 {
        int i=0;
        char *out;
@@ -151,6 +253,7 @@ int parse_line(char *line, ConfLine *parsed)
        char *p = NULL;
        char *eq;
        int ret = 0;
+       char *var_name;
 
        memset(parsed, 0, sizeof(ConfLine));
 
@@ -160,6 +263,7 @@ int parse_line(char *line, ConfLine *parsed)
                goto out;
 
        switch (*p) {
+               case ';':
                case '#':
                        parsed->set_suffix(p);
                        goto out;
@@ -807,9 +911,21 @@ int main(int argc, char *argv[])
        char *str_val;
        float fval;
        bool bval;
+       char *s;
+
+
+       s = "  hello    world   =   kaka  ";
+
+       printf("s'%s'\n", s);
+       s = get_next_name(s, 1, NULL);
+
+       printf("str_trim='%s'\n", s);
+
        cf.parse();
        cf.dump();
 
+
+
        cf.set_auto_update(true);
 
        cf.read("core", "repositoryformatversion", &val, 12);
index 71393364e6e99a35f92634581b3f8ca69b74cfc9..8c776b1408db5e6e499818c605086581f7cfcbe4 100644 (file)
@@ -205,6 +205,7 @@ md_config_t g_conf = {
   dout_sym_dir: "out",    // if daemonize == true
 
   conf_file: "ceph.conf",
+  dump_conf: false,
   
   fake_clock: false,
   fakemessenger_serialize: true,
@@ -610,11 +611,11 @@ void sighup_handler(int signum)
 #define CF_READ_STR(section, var, inout) \
   cf.read(section, var, (char **)&g_conf.inout, (char *)g_conf.inout)
 
-void parse_config_file(const char *fname)
+void parse_config_file(const char *fname, bool dump_conf)
 {
   ConfFile cf(fname);
 
-  cf.set_auto_update(true);
+  cf.set_auto_update(dump_conf);
 
   cf.parse();
 
@@ -843,13 +844,23 @@ void parse_config_file(const char *fname)
   CF_READ("bdstore", "bdbstore_transactional", bdbstore_transactional);
 #endif
 
-  cf.flush();
+  if (dump_conf)
+    cf.flush();
 }
 
 void parse_config_options(std::vector<const char*>& args, bool open)
 {
   std::vector<const char*> nargs;
 
+  for (unsigned i=0; i<args.size(); i++) {
+    if (strcmp(args[i], "--conf_file") == 0) 
+      g_conf.conf_file = args[++i];
+    else if (strcmp(args[i], "--dump_conf") == 0) 
+      g_conf.dump_conf = true;
+  }
+
+  parse_config_file(g_conf.conf_file, g_conf.dump_conf);
+
   for (unsigned i=0; i<args.size(); i++) {
     if (strcmp(args[i],"--bind") == 0) 
       assert(parse_ip_port(args[++i], g_my_addr));
@@ -1409,8 +1420,6 @@ void parse_config_options(std::vector<const char*>& args, bool open)
     }
   }
 
-  parse_config_file(g_conf.conf_file);
-
   // open log file?
   if (open)
     _dout_open_log();
index a2fccafee5daca5f47c229816be1541ac19399f9..f1cf712b0a35e7c4987221f21b8e481b2b6d12bc 100644 (file)
@@ -65,6 +65,7 @@ struct md_config_t {
   const char *dout_sym_dir;
 
   const char *conf_file;
+  bool dump_conf;
 
   bool fake_clock;
   bool fakemessenger_serialize;