diff options
author | Xiaoming Jiang <jiangxiaoming@outlook.com> | 2022-05-02 15:07:31 +0000 |
---|---|---|
committer | Florin Coras <florin.coras@gmail.com> | 2022-05-06 17:46:14 +0000 |
commit | da052b62bd2158944342735ae4cfdc65de268d6b (patch) | |
tree | 8c60b8d6502ed413415dc6e3356a8547c0338cfb /src/vlib | |
parent | 0530d09f9e9c0dab6bea32cf15a09fd885a6cbb7 (diff) |
session: fix session cli maybe parse wrong args if executed in files
Type: fix
Signed-off-by: Xiaoming Jiang <jiangxiaoming@outlook.com>
Change-Id: Id19a52df4f237cf5d85d305fdc279ab7df2d6f4b
Diffstat (limited to 'src/vlib')
-rw-r--r-- | src/vlib/cli.c | 40 | ||||
-rw-r--r-- | src/vlib/cli.h | 1 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/vlib/cli.c b/src/vlib/cli.c index dcabe8dfbaf..01409b8b677 100644 --- a/src/vlib/cli.c +++ b/src/vlib/cli.c @@ -159,6 +159,46 @@ done: return match; } +/* Get current command args */ +uword +unformat_vlib_cli_args (unformat_input_t *i, va_list *va) +{ + unformat_input_t *result = va_arg (*va, unformat_input_t *); + u8 *line; + uword last_c; + u32 index = i->index; + + if (unformat_is_eof (i)) + { + unformat_init (result, 0, 0); + return 0; + } + + /* try to find last non-space character */ + do + { + ASSERT (index > 0); + last_c = i->buffer[--index]; + } + while (last_c == ' '); + + if (last_c == '\t' || last_c == '\n' || last_c == '\r' || last_c == '\f' || + last_c == '}') + { + /* current command has no args */ + unformat_init (result, 0, 0); + return 0; + } + + if (!unformat_user (i, unformat_line, &line)) + { + unformat_init (result, 0, 0); + return 0; + } + unformat_init_vector (result, line); + return 1; +} + /* Looks for string based sub-input formatted { SUB-INPUT }. */ uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args) diff --git a/src/vlib/cli.h b/src/vlib/cli.h index 83b4eab1851..32e46f9e0da 100644 --- a/src/vlib/cli.h +++ b/src/vlib/cli.h @@ -204,6 +204,7 @@ clib_error_t *vlib_cli_register (struct vlib_main_t *vm, clib_error_t *vlib_cli_register_parse_rule (struct vlib_main_t *vm, vlib_cli_parse_rule_t * c); +uword unformat_vlib_cli_args (unformat_input_t *i, va_list *va); uword unformat_vlib_cli_sub_input (unformat_input_t * i, va_list * args); /* Return an vector of strings consisting of possible auto-completions |