genconf: enhance short and long display of confnodes
[libcmdline.git] / src / genconf / confnode_intconfig.c
index 9c31664..7d759cc 100644 (file)
@@ -40,6 +40,8 @@
 #include "confnode.h"
 
 static int confnode_intconfig_new(struct confnode *n, const struct line *line);
+static int confnode_intconfig_strvalue_to_boolvalue(const struct confnode *n,
+                                                   const char *strvalue);
 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
                                              const char *strvalue);
 static int confnode_intconfig_get_user_strvalue(const struct confnode *n, char *buf,
@@ -58,7 +60,7 @@ static struct confnode_type confnode_intconfig = {
                .add_attr = NULL, /* XXX range, displayhex */
                .close_dir = NULL,
                .dotconfig_write = NULL,
-               .strvalue_to_boolvalue = NULL,
+               .strvalue_to_boolvalue = confnode_intconfig_strvalue_to_boolvalue,
                .set_user_strvalue = confnode_intconfig_set_user_strvalue,
                .get_user_strvalue = confnode_intconfig_get_user_strvalue,
                .set_default_strvalue = confnode_intconfig_set_default_strvalue,
@@ -88,6 +90,22 @@ static int confnode_intconfig_new(struct confnode *n, const struct line *line)
        return 0;
 }
 
+/* Convert string value into a boolean value (mainly used for
+ * dependancies). Return -1 on error, else return the boolean value (0
+ * or 1). */
+static int confnode_intconfig_strvalue_to_boolvalue(const struct confnode *n,
+                                                   const char *strvalue)
+{
+       char *end = NULL;
+       int val;
+
+       /* XXX support hex and bin */
+       val = strtoul(strvalue, &end, 0);
+       if ((strvalue[0] == '\0') || (end == NULL) || (*end != '\0'))
+               return -1;
+       return val;
+}
+
 /* Set the string value of the node n. Return 0 on success, or -1 if
  * the value cannot be set. */
 static int confnode_intconfig_set_user_strvalue(struct confnode *n,
@@ -154,9 +172,25 @@ static const char *confnode_intconfig_get_type_str(const struct confnode *n)
 /* Print a one-line summary of the node. Used in case of 'ls'. */
 static void confnode_intconfig_display_short(const struct confnode *n)
 {
-       int val;
-       val = confnode_get_value(n, NULL, 0);
-       printf("[%4d] %s: %s\n", val, n->name, n->prompt);
+#define INTBUFLEN 10
+       char buf[INTBUFLEN];
+
+       memset(buf, 0, sizeof(buf));
+
+       buf[0] = '[';
+       confnode_get_value(n, buf, 0);
+       confnode_get_value(n, &buf[1], INTBUFLEN-1);
+       if (strlen(buf) == INTBUFLEN-1) { /* buffer full */
+               buf[INTBUFLEN-4] = '.';
+               buf[INTBUFLEN-3] = '.';
+               buf[INTBUFLEN-2] = '\0';
+       }
+       strcat(buf, "]");
+
+       while (strlen(buf) < sizeof(buf) - 1)
+               strcat(buf, " ");
+
+       printf("%s %s: %s\n", buf, n->name, n->prompt);
 }
 
 /* register the node type */