From: Olivier Matz Date: Fri, 24 Dec 2010 12:13:25 +0000 (+0100) Subject: genconf: enhance short and long display of confnodes X-Git-Url: http://git.droids-corp.org/?p=libcmdline.git;a=commitdiff_plain;h=d8c2f3c983f787ff68e9ef8737804b86dba0a7cf genconf: enhance short and long display of confnodes Signed-off-by: Olivier Matz --- diff --git a/src/genconf/confnode.c b/src/genconf/confnode.c index 6a85953..22aa068 100644 --- a/src/genconf/confnode.c +++ b/src/genconf/confnode.c @@ -439,7 +439,7 @@ void confnode_display_short(const struct confnode *n) if (n->ops->display_short) n->ops->display_short(n); else - printf("------ %s\n", n->prompt); + printf("------ %s\n", n->prompt); } /* Print a detailed view of the node. */ @@ -447,6 +447,7 @@ void confnode_display_long(const struct confnode *n) { const char *nodetype; char value[MAX_VALUE_SIZE]; + char default_value[MAX_VALUE_SIZE]; const char *quote = ""; if (n->ops->display_long) { @@ -454,7 +455,6 @@ void confnode_display_long(const struct confnode *n) return; } - value[0] = '\0'; /* XXX get_value */ nodetype = confnode_get_type_str(n); printf("%s <%s>\n", nodetype, n->name); printf(" path "); @@ -462,9 +462,23 @@ void confnode_display_long(const struct confnode *n) if (n->flags & CONFNODE_F_QUOTE_VALUE) quote = "\""; + + /* display real value */ + if (confnode_get_value(n, value, sizeof(value)) < 0) + snprintf(value, sizeof(value), "invalid"); printf(" value %s%s%s\n", quote, value, quote); - printf(" wanted %s%s%s\n", quote, n->value, quote); - printf(" default %s%s%s\n", quote, n->default_value, quote); + + /* display user value */ + if (!strcmp(n->value, "")) + printf(" user NONE\n"); + else + printf(" user %s%s%s\n", quote, n->value, quote); + + /* display default value */ + confnode_get_default_strvalue(n, default_value, MAX_VALUE_SIZE); + printf(" default %s%s%s\n", quote, default_value, quote); + + /* check and display deps */ confnode_check_deps(n, 1); } diff --git a/src/genconf/confnode_choice.c b/src/genconf/confnode_choice.c index 892e634..20e1efb 100644 --- a/src/genconf/confnode_choice.c +++ b/src/genconf/confnode_choice.c @@ -199,7 +199,7 @@ static void confnode_choice_display_short(const struct confnode *n) { char buf[BUFSIZ]; - printf("choice %s ", n->name); + printf("choice -> %s ", n->name); if (confnode_get_value(n, buf, sizeof(buf)) >= 0) { if (strlen(buf) > MAX_DISP_SIZE) { buf[MAX_DISP_SIZE-3] = '.'; diff --git a/src/genconf/confnode_choiceconfig.c b/src/genconf/confnode_choiceconfig.c index 7d70d27..7814804 100644 --- a/src/genconf/confnode_choiceconfig.c +++ b/src/genconf/confnode_choiceconfig.c @@ -163,7 +163,7 @@ static void confnode_choiceconfig_display_short(const struct confnode *n) c = '*'; } - printf("[%c] %s: %s\n", c, n->name, n->prompt); + printf("[%c] %s: %s\n", c, n->name, n->prompt); } /* register the node type */ diff --git a/src/genconf/confnode_config.c b/src/genconf/confnode_config.c index 7817ebf..51ed951 100644 --- a/src/genconf/confnode_config.c +++ b/src/genconf/confnode_config.c @@ -165,7 +165,7 @@ static void confnode_config_display_short(const struct confnode *n) c = '*'; } - printf("[%c] %s: %s\n", c, n->name, n->prompt); + printf("[%c] %s: %s\n", c, n->name, n->prompt); } /* register the node type */ diff --git a/src/genconf/confnode_intconfig.c b/src/genconf/confnode_intconfig.c index 9c31664..7d759cc 100644 --- a/src/genconf/confnode_intconfig.c +++ b/src/genconf/confnode_intconfig.c @@ -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 */ diff --git a/src/genconf/confnode_menu.c b/src/genconf/confnode_menu.c index 4eafa4b..b6a7c97 100644 --- a/src/genconf/confnode_menu.c +++ b/src/genconf/confnode_menu.c @@ -131,7 +131,7 @@ static const char *confnode_menu_get_type_str(const struct confnode *n) /* Print a one-line summary of the node. Used in case of 'ls'. */ static void confnode_menu_display_short(const struct confnode *n) { - printf(" -> %s: %s\n", n->name, n->prompt); + printf(" -> %s: %s\n", n->name, n->prompt); } /* Print a detailed view of the node. */ @@ -141,7 +141,6 @@ static void confnode_menu_display_long(const struct confnode *n) printf(" path "); conf_display_path(n); printf(" \"%s\"\n", n->prompt); - printf(" no value\n"); } /* register the node type */ diff --git a/src/genconf/confnode_menuconfig.c b/src/genconf/confnode_menuconfig.c index 15a5d51..545d1b6 100644 --- a/src/genconf/confnode_menuconfig.c +++ b/src/genconf/confnode_menuconfig.c @@ -188,7 +188,7 @@ static void confnode_menuconfig_display_short(const struct confnode *n) c = '*'; } - printf("[%c] -> %s: %s\n", c, n->name, n->prompt); + printf("[%c] -> %s: %s\n", c, n->name, n->prompt); } /* register the node type */ diff --git a/src/genconf/confnode_strconfig.c b/src/genconf/confnode_strconfig.c index c6dbcd2..40975c8 100644 --- a/src/genconf/confnode_strconfig.c +++ b/src/genconf/confnode_strconfig.c @@ -150,8 +150,24 @@ static const char *confnode_strconfig_get_type_str(const struct confnode *n) /* Print a one-line summary of the node. Used in case of 'ls'. */ static void confnode_strconfig_display_short(const struct confnode *n) { - /* just display prompt */ - printf(" %s\n", n->prompt); +#define STRBUFLEN 10 + char buf[STRBUFLEN]; + + memset(buf, 0, sizeof(buf)); + + buf[0] = '"'; + confnode_get_value(n, &buf[1], STRBUFLEN-1); + if (strlen(buf) == STRBUFLEN-1) { /* buffer full */ + buf[STRBUFLEN-4] = '.'; + buf[STRBUFLEN-3] = '.'; + buf[STRBUFLEN-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 */