]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: more argument parsing into ceph_argparse
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Mar 2011 23:02:11 +0000 (16:02 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Mon, 28 Mar 2011 23:14:47 +0000 (16:14 -0700)
Signed-off-by: Colin McCabe <colin.mccabe@dreamhost.com>
src/common/ceph_argparse.cc
src/common/ceph_argparse.h
src/common/config.cc
src/common/config.h

index 748fab0ac6f733ddd84dd0bfe06b1bf94268c443..35cf438f174f6306f9866da310f465839b7d466c 100644 (file)
 #undef generic_dout
 #undef dendl
 
+static bool cmd_is_char(const char *cmd)
+{
+  return ((cmd[0] == '-') &&
+    cmd[1] && !cmd[2]);
+}
+
+bool ceph_argparse_cmd_equals(const char *cmd, const char *opt, char char_opt,
+                             unsigned int *val_pos)
+{
+  unsigned int i;
+  unsigned int len = strlen(opt);
+
+  *val_pos = 0;
+
+  if (!*cmd)
+    return false;
+
+  if (char_opt && cmd_is_char(cmd))
+    return (char_opt == cmd[1]);
+
+  if ((cmd[0] != '-') || (cmd[1] != '-'))
+    return false;
+
+  for (i=0; i<len; i++) {
+    if ((opt[i] == '_') || (opt[i] == '-')) {
+      switch (cmd[i+2]) {
+      case '-':
+      case '_':
+        continue;
+      default:
+        break;
+      }
+    }
+
+    if (cmd[i+2] != opt[i])
+      return false;
+  }
+
+  if (cmd[i+2] == '=')
+    *val_pos = i+3;
+  else if (cmd[i+2])
+    return false;
+
+  return true;
+}
+
+bool ceph_argparse_cmdline_val(void *field, int type, const char *val)
+{
+  switch (type) {
+  case OPT_BOOL:
+    if (strcasecmp(val, "false") == 0)
+      *(bool *)field = false;
+    else if (strcasecmp(val, "true") == 0)
+      *(bool *)field = true;
+    else
+      *(bool *)field = (bool)atoi(val);
+    break;
+  case OPT_INT:
+    *(int *)field = atoi(val);
+    break;
+  case OPT_LONGLONG:
+    *(long long *)field = atoll(val);
+    break;
+  case OPT_STR:
+    if (val)
+      *(char **)field = strdup(val);
+    else
+      *(char **)field = NULL;
+    break;
+  case OPT_FLOAT:
+    *(float *)field = atof(val);
+    break;
+  case OPT_DOUBLE:
+    *(double *)field = strtod(val, NULL);
+    break;
+  case OPT_ADDR:
+    ((entity_addr_t *)field)->parse(val);
+    break;
+  default:
+    return false;
+  }
+
+  return true;
+}
+
 void env_override(char **ceph_var, const char * const env_var)
 {
   char *e = getenv(env_var);
index b1f464d5e516006dd633afad94a47dc8a997e1ef..b53a086791654fcab0032e67028e0c198d576ec2 100644 (file)
        void (*args_usage)() __attribute__((unused)) = usage_func; \
        bool __isarg __attribute__((unused))
 
+#define CONF_NEXT_VAL (val_pos ? &args[i][val_pos] : args[++i])
+
+#define CONF_SET_ARG_VAL(dest, type) \
+       ceph_argparse_cmdline_val(dest, type, CONF_NEXT_VAL)
+
+#define CONF_VAL args[i]
+
+#define CONF_SAFE_SET_ARG_VAL(dest, type) \
+       do { \
+          __isarg = i+1 < args.size(); \
+          if (__isarg && !val_pos && \
+              args[i+1][0] == '-' && args[i+1][1] != '\0') \
+              __isarg = false; \
+          if (type == OPT_BOOL) { \
+               if (val_pos) { \
+                       CONF_SET_ARG_VAL(dest, type); \
+               } else \
+                       ceph_argparse_cmdline_val(dest, type, "true"); \
+          } else if (__isarg || val_pos) { \
+               CONF_SET_ARG_VAL(dest, type); \
+         } else if (args_usage) \
+               args_usage(); \
+       } while (0)
+
+#define CONF_ARG_EQ(str_cmd, char_cmd) \
+       ceph_argparse_cmd_equals(args[i], str_cmd, char_cmd, &val_pos)
+
+extern bool ceph_argparse_cmdline_val(void *field, int type,
+                                     const char *val);
+extern bool ceph_argparse_cmd_equals(const char *cmd, const char *opt,
+                                    char char_opt, unsigned int *val_pos);
+
 /////////////////////// Types ///////////////////////
 class CephInitParameters
 {
index 8e7bad3e4c724ec49cd8723b9913aeabbd560511..4cada825d72bfff1410d72a26aec2d08311f1936 100644 (file)
@@ -399,45 +399,6 @@ struct config_option config_optionsp[] = {
 
 const int num_config_options = sizeof(config_optionsp) / sizeof(config_option);
 
-bool conf_set_conf_val(void *field, opt_type_t type, const char *val)
-{
-  switch (type) {
-  case OPT_BOOL:
-    if (strcasecmp(val, "false") == 0)
-      *(bool *)field = false;
-    else if (strcasecmp(val, "true") == 0)
-      *(bool *)field = true;
-    else
-      *(bool *)field = (bool)atoi(val);
-    break;
-  case OPT_INT:
-    *(int *)field = atoi(val);
-    break;
-  case OPT_LONGLONG:
-    *(long long *)field = atoll(val);
-    break;
-  case OPT_STR:
-    if (val)
-      *(char **)field = strdup(val);
-    else
-      *(char **)field = NULL;
-    break;
-  case OPT_FLOAT:
-    *(float *)field = atof(val);
-    break;
-  case OPT_DOUBLE:
-    *(double *)field = strtod(val, NULL);
-    break;
-  case OPT_ADDR:
-    ((entity_addr_t *)field)->parse(val);
-    break;
-  default:
-    return false;
-  }
-
-  return true;
-}
-
 bool conf_set_conf_val(void *field, opt_type_t type, const char *val, long long intval, double doubleval)
 {
   switch (type) {
@@ -508,51 +469,6 @@ static void set_conf_name(config_option *opt)
     opt->conf_name = (const char *)newconf;
 }
 
-static bool cmd_is_char(const char *cmd)
-{
-  return ((cmd[0] == '-') &&
-    cmd[1] && !cmd[2]);
-}
-
-bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos)
-{
-  unsigned int i;
-  unsigned int len = strlen(opt);
-
-  *val_pos = 0;
-
-  if (!*cmd)
-    return false;
-
-  if (char_opt && cmd_is_char(cmd))
-    return (char_opt == cmd[1]);
-
-  if ((cmd[0] != '-') || (cmd[1] != '-'))
-    return false;
-
-  for (i=0; i<len; i++) {
-    if ((opt[i] == '_') || (opt[i] == '-')) {
-      switch (cmd[i+2]) {
-      case '-':
-      case '_':
-        continue;
-      default:
-        break;
-      }
-    }
-
-    if (cmd[i+2] != opt[i])
-      return false;
-  }
-
-  if (cmd[i+2] == '=')
-    *val_pos = i+3;
-  else if (cmd[i+2])
-    return false;
-
-  return true;
-}
-
 static bool get_var(const char *str, int pos, char *var_name, int len, int *new_pos)
 {
   int bracket = (str[pos] == '{');
index 49802285df31ad43a8a7fece53b783497ef74a9b..8a2cb52985f70b38edd4737d412826d5cb9de0bb 100644 (file)
@@ -473,43 +473,9 @@ typedef enum {
 
 char *conf_post_process_val(const char *val);
 int conf_read_key(const char *alt_section, const char *key, opt_type_t type, void *out, void *def, bool free_old_val = false);
-bool conf_set_conf_val(void *field, opt_type_t type, const char *val);
-bool conf_cmd_equals(const char *cmd, const char *opt, char char_opt, unsigned int *val_pos);
 
 bool ceph_resolve_file_search(string& filename_list, string& result);
 
-#define CONF_NEXT_VAL (val_pos ? &args[i][val_pos] : args[++i])
-
-#define CONF_SET_ARG_VAL(dest, type) \
-       conf_set_conf_val(dest, type, CONF_NEXT_VAL)
-
-#define CONF_VAL args[i]
-
-#define CONF_SAFE_SET_ARG_VAL_USAGE(dest, type, show_usage) \
-       do { \
-          __isarg = i+1 < args.size(); \
-          if (__isarg && !val_pos && \
-              args[i+1][0] == '-' && args[i+1][1] != '\0') \
-              __isarg = false; \
-          if (type == OPT_BOOL) { \
-               if (val_pos) { \
-                       CONF_SET_ARG_VAL(dest, type); \
-               } else \
-                       conf_set_conf_val(dest, type, "true"); \
-          } else if (__isarg || val_pos) { \
-               CONF_SET_ARG_VAL(dest, type); \
-         } else if (show_usage && args_usage) \
-               args_usage(); \
-       } while (0)
-
-#define CONF_SAFE_SET_ARG_VAL(dest, type) CONF_SAFE_SET_ARG_VAL_USAGE(dest, type, true)
-
-#define CONF_SET_BOOL_ARG_VAL(dest) \
-       conf_set_conf_val(dest, OPT_BOOL, (val_pos ? &args[i][val_pos] : "true"))
-
-#define CONF_ARG_EQ(str_cmd, char_cmd) \
-       conf_cmd_equals(args[i], str_cmd, char_cmd, &val_pos)
-
 struct config_option {
   const char *section;
   const char *conf_name;