Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Improve printing of boolean values with --cfg-help.
[simgrid.git] / src / xbt / config.c
index 2b65a73..0c00bcd 100644 (file)
@@ -33,7 +33,7 @@ typedef struct {
   /* Allowed type of the variable */
   e_xbt_cfgelm_type_t type;
   int min, max;
-  int isdefault:1;
+  unsigned isdefault:1;
 
   /* Callbacks */
   xbt_cfg_cb_t cb_set;
@@ -45,7 +45,15 @@ typedef struct {
 } s_xbt_cfgelm_t, *xbt_cfgelm_t;
 
 static const char *xbt_cfgelm_type_name[xbt_cfgelm_type_count] =
-    { "int", "double", "string", "peer", "any" };
+    { "int", "double", "string", "boolean", "peer", "any" };
+
+const struct xbt_boolean_couple xbt_cfgelm_boolean_values[] = {
+  { "yes",    "no"},
+  {  "on",   "off"},
+  {"true", "false"},
+  {   "1",     "0"},
+  {  NULL,    NULL}
+};
 
 /* Internal stuff used in cache to free a variable */
 static void xbt_cfgelm_free(void *data);
@@ -58,7 +66,7 @@ static xbt_cfgelm_t xbt_cfgelm_get(xbt_cfg_t cfg, const char *name,
 
 /** @brief Constructor
  *
- * Initialise an config set
+ * Initialise a config set
  */
 
 
@@ -154,6 +162,13 @@ void xbt_cfg_dump(const char *name, const char *indent, xbt_cfg_t cfg)
       }
       break;
 
+    case xbt_cfgelm_boolean:
+      for (i = 0; i < size; i++) {
+        ival = xbt_dynar_get_as(variable->content, i, int);
+        printf("%s    %d\n", indent, ival);
+      }
+      break;
+
     case xbt_cfgelm_peer:
       for (i = 0; i < size; i++) {
         hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
@@ -253,6 +268,12 @@ xbt_cfg_register(xbt_cfg_t * cfg,
       xbt_dynar_push(res->content, default_value);
     break;
 
+  case xbt_cfgelm_boolean:
+    res->content = xbt_dynar_new(sizeof(int), NULL);
+    if (default_value)
+      xbt_dynar_push(res->content, default_value);
+    break;
+
   case xbt_cfgelm_peer:
     res->content = xbt_dynar_new(sizeof(xbt_peer_t), xbt_peer_free_voidp);
     if (default_value)
@@ -370,48 +391,48 @@ void xbt_cfg_help(xbt_cfg_t cfg)
       else
         printf("Arity: min:%d to max:%d; ", variable->min, variable->max);
     }
-    printf("Current value: ");
     size = xbt_dynar_length(variable->content);
+    printf("Current value%s: ", (size <= 1 ? "" : "s"));
 
-    switch (variable->type) {
-      int ival;
-      char *sval;
-      double dval;
-      xbt_peer_t hval;
+    if (size != 1)
+      printf(size == 0 ? "n/a\n" : "{ ");
+    for (i = 0; i < size; i++) {
+      const char *sep = (size == 1 ? "\n" : (i < size - 1 ? ", " : " }\n"));
 
-    case xbt_cfgelm_int:
-      for (i = 0; i < size; i++) {
-        ival = xbt_dynar_get_as(variable->content, i, int);
-        printf("%s%d\n", (i == 0 ? "" : "              "), ival);
-      }
-      break;
+      switch (variable->type) {
+      case xbt_cfgelm_int:
+        printf("%d%s", xbt_dynar_get_as(variable->content, i, int), sep);
+        break;
 
-    case xbt_cfgelm_double:
-      for (i = 0; i < size; i++) {
-        dval = xbt_dynar_get_as(variable->content, i, double);
-        printf("%s%f\n", (i == 0 ? "" : "              "), dval);
-      }
-      break;
+      case xbt_cfgelm_double:
+        printf("%f%s", xbt_dynar_get_as(variable->content, i, double), sep);
+        break;
 
-    case xbt_cfgelm_string:
-      for (i = 0; i < size; i++) {
-        sval = xbt_dynar_get_as(variable->content, i, char *);
-        printf("%s'%s'\n", (i == 0 ? "" : "              "), sval);
+      case xbt_cfgelm_string:
+        printf("'%s'%s", xbt_dynar_get_as(variable->content, i, char *), sep);
+        break;
+
+      case xbt_cfgelm_boolean: {
+        int b = xbt_dynar_get_as(variable->content, i, int);
+        const char *bs = b ? xbt_cfgelm_boolean_values[0].true_val
+                           : xbt_cfgelm_boolean_values[0].false_val;
+        if (b == 0 || b == 1)
+          printf("'%s'%s", bs, sep);
+        else
+          printf("'%s/%d'%s", bs, b, sep);
+        break;
       }
-      break;
 
-    case xbt_cfgelm_peer:
-      for (i = 0; i < size; i++) {
-        hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
-        printf("%s%s:%d\n", (i == 0 ? "" : "              "), hval->name,
-               hval->port);
+      case xbt_cfgelm_peer: {
+        xbt_peer_t hval = xbt_dynar_get_as(variable->content, i, xbt_peer_t);
+        printf("%s:%d%s", hval->name, hval->port, sep);
+        break;
       }
-      break;
 
-    default:
-      printf("Invalid type!!\n");
+      default:
+        printf("Invalid type!!%s", sep);
+      }
     }
-
   }
 
   xbt_dynar_free(&names);
@@ -490,7 +511,7 @@ e_xbt_cfgelm_type_t xbt_cfg_get_type(xbt_cfg_t cfg, const char *name)
            "Can't get the type of '%s' since this variable does not exist",
            name);
 
-  XBT_INFO("type in variable = %d", (int)variable->type);
+  XBT_DEBUG("type in variable = %d", (int)variable->type);
 
   return variable->type;
 }
@@ -548,6 +569,11 @@ void xbt_cfg_set_vargs(xbt_cfg_t cfg, const char *name, va_list pa)
     xbt_cfg_set_double(cfg, name, d);
     break;
 
+  case xbt_cfgelm_boolean:
+    str = va_arg(pa, char *);
+    xbt_cfg_set_boolean(cfg, name, str);
+    break;
+
   default:
     xbt_die("Config element variable %s not valid (type=%d)", name, (int)type);
   }
@@ -581,21 +607,14 @@ void xbt_cfg_set(xbt_cfg_t cfg, const char *name, ...)
  * @todo This is a crude manual parser, it should be a proper lexer.
  */
 
-void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
-{
-  xbt_ex_t e;
-
-  int i;
-  double d;
-  char *str;
+void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
 
-  volatile xbt_cfgelm_t variable = NULL;
   char *optionlist_cpy;
   char *option, *name, *val;
 
   int len;
 
-  XBT_IN("");
+  XBT_IN();
   if (!options || !strlen(options)) {   /* nothing to do */
     return;
   }
@@ -645,95 +664,107 @@ void xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options)
 
     val = strchr(name, ':');
     if (!val) {
-      free(optionlist_cpy);
+      /* don't free(optionlist_cpy) here, 'name' points inside it */
       xbt_die("Option '%s' badly formated. Should be of the form 'name:value'",
               name);
     }
     *(val++) = '\0';
 
-    if (strncmp(name, "contexts/", strlen("contexts/")))
+    if (strncmp(name, "contexts/", strlen("contexts/")) && strncmp(name, "path", strlen("path")))
       XBT_INFO("Configuration change: Set '%s' to '%s'", name, val);
 
     TRY {
-      variable = xbt_dict_get((xbt_dict_t) cfg, name);
-    }
-    CATCH(e) {
-      if (e.category == not_found_error) {
-        xbt_ex_free(e);
-        TRY {
-          THROWF(not_found_error, 0,
-                 "No registered variable corresponding to '%s'.", name);
-        }
-        TRY_CLEANUP {
-          /* name points into optionlist_cpy, it cannot be freed before */
-          free(optionlist_cpy);
-        }
-        CATCH_ANONYMOUS {
-          RETHROW;
-        }
-      }
+      xbt_cfg_set_as_string(cfg,name,val);
+    } CATCH_ANONYMOUS {
       free(optionlist_cpy);
       RETHROW;
     }
+  }
+  free(optionlist_cpy);
+}
 
-    TRY {
-      switch (variable->type) {
-      case xbt_cfgelm_string:
-        xbt_cfg_set_string(cfg, name, val);     /* throws */
-        break;
+/** @brief Set the value of a variable, using the string representation of that value
+ *
+ * @arg cfg config set to modify
+ * @arg key name of the variable to modify
+ * @arg value string representation of the value to set
+ *
+ * @return the first char after the parsed value in val
+ */
 
-      case xbt_cfgelm_int:
-        i = strtol(val, &val, 0);
-        if (val == NULL) {
-          free(optionlist_cpy);
-          xbt_die("Value of option %s not valid. Should be an integer", name);
-        }
+void *xbt_cfg_set_as_string(xbt_cfg_t cfg, const char *key, const char *value) {
+  xbt_ex_t e;
 
-        xbt_cfg_set_int(cfg, name, i);  /* throws */
-        break;
+  char *ret;
+  volatile xbt_cfgelm_t variable = NULL;
+  int i;
+  double d;
+  char *str, *val;
 
-      case xbt_cfgelm_double:
-        d = strtod(val, &val);
-        if (val == NULL) {
-          free(optionlist_cpy);
-          xbt_die("Value of option %s not valid. Should be a double", name);
-        }
 
-        xbt_cfg_set_double(cfg, name, d);       /* throws */
-        break;
+  TRY {
+    variable = xbt_dict_get((xbt_dict_t) cfg, key);
+  }
+  CATCH(e) {
+    if (e.category == not_found_error) {
+      xbt_ex_free(e);
+      THROWF(not_found_error, 0,
+          "No registered variable corresponding to '%s'.", key);
+    }
+    RETHROW;
+  }
 
-      case xbt_cfgelm_peer:
-        str = val;
-        val = strchr(val, ':');
-        if (!val) {
-          free(optionlist_cpy);
-          xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
-                  name);
-        }
-
-        *(val++) = '\0';
-        i = strtol(val, &val, 0);
-        if (val == NULL) {
-          free(optionlist_cpy);
-          xbt_die("Value of option %s not valid. Should be an peer (machine:port)",
-                  name);
-        }
-
-        xbt_cfg_set_peer(cfg, name, str, i);    /* throws */
-        break;
+  switch (variable->type) {
+  case xbt_cfgelm_string:
+    xbt_cfg_set_string(cfg, key, value);     /* throws */
+    break;
 
-      default:
-        THROWF(unknown_error, 0, "Type of config element %s is not valid.",
-               name);
-      }
+  case xbt_cfgelm_int:
+    i = strtol(value, &ret, 0);
+    if (ret == value) {
+      xbt_die("Value of option %s not valid. Should be an integer", key);
     }
-    CATCH_ANONYMOUS {
-      free(optionlist_cpy);
-      RETHROW;
+
+    xbt_cfg_set_int(cfg, key, i);  /* throws */
+    break;
+
+  case xbt_cfgelm_double:
+    d = strtod(value, &ret);
+    if (ret == value) {
+      xbt_die("Value of option %s not valid. Should be a double", key);
     }
+
+    xbt_cfg_set_double(cfg, key, d);       /* throws */
+    break;
+
+  case xbt_cfgelm_boolean:
+    xbt_cfg_set_boolean(cfg, key, value);  /* throws */
+    break;
+
+  case xbt_cfgelm_peer:
+    val = xbt_strdup(value);
+    str = val;
+    val = strchr(val, ':');
+    if (!val) {
+      xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
+    }
+
+    *(val++) = '\0';
+    i = strtol(val, &ret, 0);
+    if (ret == val) {
+      xbt_die("Value of option %s not valid. Should be an peer (machine:port)", key);
+    }
+
+    xbt_cfg_set_peer(cfg, key, str, i);    /* throws */
+    free(val);
+    break;
+
+  default:
+    THROWF(unknown_error, 0, "Type of config element %s is not valid.", key);
+    break;
   }
-  free(optionlist_cpy);
 
+  return ret;
 }
 
 /** @brief Set an integer value to \a name within \a cfg if it wasn't changed yet
@@ -794,6 +825,26 @@ void xbt_cfg_setdefault_string(xbt_cfg_t cfg, const char *name,
          name, val);
 }
 
+
+/** @brief Set an boolean value to \a name within \a cfg if it wasn't changed yet
+ *
+ * This is useful to change the default value of a variable while allowing
+ * users to override it with command line arguments
+ */
+void xbt_cfg_setdefault_boolean(xbt_cfg_t cfg, const char *name, const char *val)
+{
+  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
+
+  if (variable->isdefault){
+    xbt_cfg_set_boolean(cfg, name, val);
+    variable->isdefault = 1;
+  }
+   else
+    XBT_DEBUG
+        ("Do not override configuration variable '%s' with value '%s' because it was already set.",
+         name, val);
+}
+
 /** @brief Set a peer value to \a name within \a cfg if it wasn't changed yet
  *
  * This is useful to change the default value of a variable while allowing
@@ -927,6 +978,55 @@ void xbt_cfg_set_string(xbt_cfg_t cfg, const char *name, const char *val)
   variable->isdefault = 0;
 }
 
+/** @brief Set or add a boolean value to \a name within \a cfg
+ *
+ * \arg cfg the config set
+ * \arg name the name of the variable
+ * \arg val the value of the variable
+ */
+void xbt_cfg_set_boolean(xbt_cfg_t cfg, const char *name, const char *val)
+{
+  xbt_cfgelm_t variable;
+  int i, bval;
+
+  XBT_VERB("Configuration setting: %s=%s", name, val);
+  variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
+
+  for (i = 0; xbt_cfgelm_boolean_values[i].true_val != NULL; i++) {
+       if (strcmp(val, xbt_cfgelm_boolean_values[i].true_val) == 0){
+         bval = 1;
+         break;
+       }
+       if (strcmp(val, xbt_cfgelm_boolean_values[i].false_val) == 0){
+         bval = 0;
+         break;
+       }
+  }
+  if (xbt_cfgelm_boolean_values[i].true_val == NULL) {
+    xbt_die("Value of option '%s' not valid. Should be a boolean (yes,no,on,off,true,false,0,1)", val);
+  }
+
+  if (variable->max == 1) {
+    if (variable->cb_rm && !xbt_dynar_is_empty(variable->content))
+      variable->cb_rm(name, 0);
+
+    xbt_dynar_set(variable->content, 0, &bval);
+  } else {
+    if (variable->max
+        && xbt_dynar_length(variable->content) ==
+        (unsigned long) variable->max)
+      THROWF(mismatch_error, 0,
+             "Cannot add value %s to the config element %s since it's already full (size=%d)",
+             val, name, variable->max);
+
+    xbt_dynar_push(variable->content, &bval);
+  }
+
+  if (variable->cb_set)
+    variable->cb_set(name, xbt_dynar_length(variable->content) - 1);
+  variable->isdefault = 0;
+}
+
 /** @brief Set or add an peer value to \a name within \a cfg
  *
  * \arg cfg the config set
@@ -1071,6 +1171,40 @@ void xbt_cfg_rm_string(xbt_cfg_t cfg, const char *name, const char *val)
          val, name);
 }
 
+/** @brief Remove the provided \e val boolean value from a variable
+ *
+ * \arg cfg the config set
+ * \arg name the name of the variable
+ * \arg val the value to be removed
+ */
+void xbt_cfg_rm_boolean(xbt_cfg_t cfg, const char *name, int val)
+{
+
+  xbt_cfgelm_t variable;
+  unsigned int cpt;
+  int seen;
+
+  variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
+
+  if (xbt_dynar_length(variable->content) == variable->min)
+    THROWF(mismatch_error, 0,
+           "Cannot remove value %d from the config element %s since it's already at its minimal size (=%d)",
+           val, name, variable->min);
+
+  xbt_dynar_foreach(variable->content, cpt, seen) {
+    if (seen == val) {
+      if (variable->cb_rm)
+        variable->cb_rm(name, cpt);
+      xbt_dynar_cursor_rm(variable->content, &cpt);
+      return;
+    }
+  }
+
+  THROWF(not_found_error, 0,
+         "Can't remove the value %d of config element %s: value not found.",
+         val, name);
+}
+
 /** @brief Remove the provided \e val peer value from a variable
  *
  * \arg cfg the config set
@@ -1252,6 +1386,31 @@ char *xbt_cfg_get_string(xbt_cfg_t cfg, const char *name)
   return xbt_dynar_get_as(variable->content, 0, char *);
 }
 
+/** @brief Retrieve a boolean value of a variable (get a warning if not uniq)
+ *
+ * \arg cfg the config set
+ * \arg name the name of the variable
+ * \arg val the wanted value
+ *
+ * Returns the first value from the config set under the given name.
+ * If there is more than one value, it will issue a warning. Consider using
+ * xbt_cfg_get_dynar() instead.
+ *
+ * \warning the returned value is the actual content of the config set
+ */
+int xbt_cfg_get_boolean(xbt_cfg_t cfg, const char *name)
+{
+  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
+
+  if (xbt_dynar_length(variable->content) > 1) {
+    XBT_WARN
+        ("You asked for the first value of the config element '%s', but there is %lu values",
+         name, xbt_dynar_length(variable->content));
+  }
+
+  return xbt_dynar_get_as(variable->content, 0, int);
+}
+
 /** @brief Retrieve an peer value of a variable (get a warning if not uniq)
  *
  * \arg cfg the config set
@@ -1341,6 +1500,14 @@ char *xbt_cfg_get_string_at(xbt_cfg_t cfg, const char *name, int pos)
   return xbt_dynar_get_as(variable->content, pos, char *);
 }
 
+/** @brief Retrieve one of the boolean value of a variable */
+int xbt_cfg_get_boolean_at(xbt_cfg_t cfg, const char *name, int pos)
+{
+
+  xbt_cfgelm_t variable = xbt_cfgelm_get(cfg, name, xbt_cfgelm_boolean);
+  return xbt_dynar_get_as(variable->content, pos, int);
+}
+
 /** @brief Retrieve one of the peer value of a variable */
 void
 xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
@@ -1359,8 +1526,7 @@ xbt_cfg_get_peer_at(xbt_cfg_t cfg, const char *name, int pos,
 #include "xbt.h"
 #include "xbt/ex.h"
 
-XBT_LOG_EXTERNAL_CATEGORY(xbt_cfg);
-XBT_LOG_DEFAULT_CATEGORY(xbt_cfg);
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_cfg);
 
 XBT_TEST_SUITE("config", "Configuration support");