diff options
author | Harald Welte <laforge@gnumonks.org> | 2012-08-17 12:17:38 +0200 |
---|---|---|
committer | Harald Welte <laforge@gnumonks.org> | 2012-08-20 18:43:09 +0200 |
commit | fab0ae9b43ad5c44d3d0402424b152a531b609c4 (patch) | |
tree | 7f3a066f153168c243bb4a22fe281c8a8421cbbf /src | |
parent | 08b286277b1d1c16ce3a615c5de07cefcbd6acf6 (diff) |
import vty_cmd_string_from_valstr() from osmo-bts
This function is able to generate a VTY help string based on an
array of 'struct value_string'.
Diffstat (limited to 'src')
-rw-r--r-- | src/vty/utils.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/vty/utils.c b/src/vty/utils.c index b3223edf..abf0ac49 100644 --- a/src/vty/utils.c +++ b/src/vty/utils.c @@ -22,11 +22,13 @@ #include <stdint.h> #include <inttypes.h> +#include <string.h> #include <osmocom/core/linuxlist.h> #include <osmocom/core/talloc.h> #include <osmocom/core/timer.h> #include <osmocom/core/rate_ctr.h> +#include <osmocom/core/utils.h> #include <osmocom/vty/vty.h> @@ -60,4 +62,56 @@ void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, }; } +/*! \brief Generate a VTY command string from value_string */ +char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals, + const char *prefix, const char *sep, + const char *end, int do_lower) +{ + int len = 0, offset = 0, ret, rem; + int size = strlen(prefix); + const struct value_string *vs; + char *str; + + for (vs = vals; vs->value || vs->str; vs++) + size += strlen(vs->str) + 1; + + rem = size; + str = talloc_zero_size(ctx, size); + if (!str) + return NULL; + + ret = snprintf(str + offset, rem, prefix); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); + + for (vs = vals; vs->value || vs->str; vs++) { + if (vs->str) { + int j, name_len = strlen(vs->str)+1; + char name[name_len]; + + for (j = 0; j < name_len; j++) + name[j] = do_lower ? + tolower(vs->str[j]) : vs->str[j]; + + name[name_len-1] = '\0'; + ret = snprintf(str + offset, rem, "%s%s", name, sep); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); + } + } + offset--; /* to remove the trailing | */ + rem++; + + ret = snprintf(str + offset, rem, end); + if (ret < 0) + goto err; + OSMO_SNPRINTF_RET(ret, rem, offset, len); +err: + str[size-1] = '\0'; + return str; +} + + /*! @} */ |