From df9816e5e10b0552c7e29650ee21a79654af67ef Mon Sep 17 00:00:00 2001 From: Olivier Matz Date: Fri, 18 Feb 2011 18:33:44 +0100 Subject: [PATCH] cmdline: add the incomplete token string as an argument of iter_start() Signed-off-by: Olivier Matz --- src/extension_example/parse_obj_list.c | 4 +++- src/genconf/parse_confnode.c | 4 +++- src/lib/cmdline_parse.c | 18 ++++++++++++++++-- src/lib/cmdline_parse.h | 16 +++++++++------- src/lib/cmdline_parse_string.c | 4 +++- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/src/extension_example/parse_obj_list.c b/src/extension_example/parse_obj_list.c index d561519..9869e09 100644 --- a/src/extension_example/parse_obj_list.c +++ b/src/extension_example/parse_obj_list.c @@ -70,7 +70,9 @@ parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, } static int -complete_obj_list_start(cmdline_parse_token_hdr_t *tk, void **opaque) +complete_obj_list_start(cmdline_parse_token_hdr_t *tk, + __attribute__((unused)) const char *tokstr, + void **opaque) { struct token_obj_list *tk2 = (struct token_obj_list *)tk; struct token_obj_list_data *tkd = &tk2->obj_list_data; diff --git a/src/genconf/parse_confnode.c b/src/genconf/parse_confnode.c index f3dcda6..033e45d 100644 --- a/src/genconf/parse_confnode.c +++ b/src/genconf/parse_confnode.c @@ -187,7 +187,9 @@ static struct confnode *get_next_node(struct confnode *start, } static int -complete_conf_node_start(cmdline_parse_token_hdr_t *tk, void **opaque) +complete_conf_node_start(cmdline_parse_token_hdr_t *tk, + __attribute__((unused)) const char *tokstr, + void **opaque) { struct token_conf_node *tk2 = (struct token_conf_node *)tk; struct token_conf_node_data *tkd = &tk2->conf_node_data; diff --git a/src/lib/cmdline_parse.c b/src/lib/cmdline_parse.c index 8e73e65..c056833 100644 --- a/src/lib/cmdline_parse.c +++ b/src/lib/cmdline_parse.c @@ -502,8 +502,15 @@ int cmdline_help(cmdline_parse_ctx_t *ctx, const char *buf, else iterate = 1; + /* store the incomplete token in tmpbuf */ + n = preparse.comp_tok_len + 1; + if (n > sizeof(tmpbuf)) + n = sizeof(tmpbuf); + snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf); + if (iterate == 1 && - token->ops->complete_start(token, &preparse.opaque) < 0) { + token->ops->complete_start(token, tmpbuf, + &preparse.opaque) < 0) { /* cancel iteration, complete_start() returned * a negative value, meaning no completion */ iterate = 0; @@ -605,8 +612,15 @@ cmdline_complete(cmdline_parse_ctx_t *ctx, const char *buf, token->ops->complete_iterate == NULL) continue; + /* store the incomplete token in tmpbuf */ + n = preparse.comp_tok_len + 1; + if (n > sizeof(tmpbuf)) + n = sizeof(tmpbuf); + snprintf(tmpbuf, n, "%s", preparse.comp_tok_buf); + /* non completable */ - if (token->ops->complete_start(token, &preparse.opaque) < 0) { + if (token->ops->complete_start(token, tmpbuf, + &preparse.opaque) < 0) { if (token->ops->complete_end != NULL) token->ops->complete_end(token, &preparse.opaque); diff --git a/src/lib/cmdline_parse.h b/src/lib/cmdline_parse.h index 5112ab5..6facb9e 100644 --- a/src/lib/cmdline_parse.h +++ b/src/lib/cmdline_parse.h @@ -91,12 +91,13 @@ typedef struct cmdline_token_hdr cmdline_parse_token_hdr_t; * error. * * complete_start() prepares a completion operation. The first - * argument is the token to complete. The second argument is an opaque - * pointer that will be given to complete_iterate() function. It can - * be used to store private data for this completion. For each - * complete_start() call, the user must call complete_end() at the end - * of iterations (if defined). Return a negative value if completion - * is not possible, or 0 on success. + * argument is the token to complete. The second argument is the token + * to complete, and the third arg is an opaque pointer that will be + * given to complete_iterate() function. It can be used to store + * private data for this completion. For each complete_start() call, + * the user must call complete_end() at the end of iterations (if + * defined). Return a negative value if completion is not possible, or + * 0 on success. * * complete_iterate() copy in dstbuf (the size is specified in the * parameter) the next possible completion for this token. Return 0 on @@ -115,7 +116,8 @@ struct cmdline_token_ops { int (*parse)(cmdline_parse_token_hdr_t *, const char *, void *, unsigned int); /** prepare a completion on this token */ - int (*complete_start)(cmdline_parse_token_hdr_t *, void **); + int (*complete_start)(cmdline_parse_token_hdr_t *, const char *, + void **); /** fill dstbuf for this token (token, opaque, dstbuf, size) */ int (*complete_iterate)(cmdline_parse_token_hdr_t *, void **, char *, unsigned int); diff --git a/src/lib/cmdline_parse_string.c b/src/lib/cmdline_parse_string.c index 1b2c146..b99a278 100644 --- a/src/lib/cmdline_parse_string.c +++ b/src/lib/cmdline_parse_string.c @@ -150,7 +150,9 @@ cmdline_parse_string(cmdline_parse_token_hdr_t *tk, const char *buf, void *res, } static int -cmdline_complete_string_start(cmdline_parse_token_hdr_t *tk, void **opaque) +cmdline_complete_string_start(cmdline_parse_token_hdr_t *tk, + __attribute__((unused)) const char *tokstr, + void **opaque) { struct cmdline_token_string *tk2 = (struct cmdline_token_string *)tk; struct cmdline_token_string_data *sd = &tk2->string_data;; -- 2.20.1