From b55f4d2df21b966c3953264d8961f259814f4650 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Thu, 31 Jan 2019 08:13:31 +0100 Subject: vty: enable optional-multi-choice syntax: [(one|two)] Since very recently we sensibly handle commands like cmd ([one]|[two]|[three]) as optional multi-choice arguments. In addition, support the more obvious syntax of cmd [(one|two|three)] Internally, the tokens are mangled to [one] [two] and [three], which is how the rest of the code detects optional args, and makes sense in terms of UI: > cmd ? [one] [two] [three] (i.e. optional arguments are always shown in braces in '?' listings) Before this patch, commands defined with a syntax like [(one|two)], would lead to an assertion (shows as "multiple") during program startup. Change-Id: I952b3c00f97e2447f2308b0ec6f5f1714692b5b2 --- src/vty/command.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/vty/command.c b/src/vty/command.c index 9d02d696..2242e761 100644 --- a/src/vty/command.c +++ b/src/vty/command.c @@ -380,6 +380,7 @@ static char *cmd_desc_str(const char **string) static vector cmd_make_descvec(const char *string, const char *descstr) { int multiple = 0; + int optional_brace = 0; const char *sp; char *token; int len; @@ -401,6 +402,12 @@ static vector cmd_make_descvec(const char *string, const char *descstr) while (isspace((int)*cp) && *cp != '\0') cp++; + /* Explicitly detect optional multi-choice braces like [(one|two)]. */ + if (cp[0] == '[' && cp[1] == '(') { + optional_brace = 1; + cp++; + } + if (*cp == '(') { multiple = 1; cp++; @@ -408,6 +415,9 @@ static vector cmd_make_descvec(const char *string, const char *descstr) if (*cp == ')') { multiple = 0; cp++; + if (*cp == ']') + cp++; + optional_brace = 0; } if (*cp == '|') { OSMO_ASSERT(multiple); @@ -434,9 +444,17 @@ static vector cmd_make_descvec(const char *string, const char *descstr) len = cp - sp; - token = _talloc_zero(tall_vty_cmd_ctx, len + 1, "cmd_make_descvec"); - memcpy(token, sp, len); - *(token + len) = '\0'; + token = _talloc_zero(tall_vty_cmd_ctx, len + (optional_brace? 2 : 0) + 1, "cmd_make_descvec"); + if (optional_brace) { + /* Place each individual multi-choice token in its own square braces */ + token[0] = '['; + memcpy(token + 1, sp, len); + token[1 + len] = ']'; + token[2 + len] = '\0'; + } else { + memcpy(token, sp, len); + *(token + len) = '\0'; + } desc = talloc_zero(tall_vty_cmd_ctx, struct desc); desc->cmd = token; -- cgit v1.2.3