#include "parse_obj_list.h"
/* This file is an example of extension of libcmdline. It provides an
- * example of objects stored in a list. */
+ * example of named objects stored in a list, supporting the
+ * completion on objects name */
-struct cmdline_token_ops token_obj_list_ops = {
- .parse = parse_obj_list,
- .complete_get_nb = complete_get_nb_obj_list,
- .complete_get_elt = complete_get_elt_obj_list,
- .get_help = get_help_obj_list,
-};
-
-int
+static int
parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
unsigned ressize)
{
continue;
break;
}
- if (!o) /* not found */
+ if (o == NULL) /* not found */
return -1;
/* store the address of object in structure */
if (res)
*(struct object **)res = o;
- return token_len;
+ return 0;
}
-int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
+static int
+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;
struct object *o;
- int ret = 0;
- SLIST_FOREACH(o, tkd->list, next) {
- ret ++;
- }
- return ret;
+ o = SLIST_FIRST(tkd->list);
+ *opaque = (void *)o;
+ if (o == NULL)
+ return -1; /* no completion */
+ return 0;
}
-int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk, int idx,
- char *dstbuf, unsigned int size)
+static int
+complete_obj_list_iterate(cmdline_parse_token_hdr_t *tk, void **opaque,
+ char *dstbuf, unsigned int size)
{
- struct token_obj_list *tk2 = (struct token_obj_list *)tk;
- struct token_obj_list_data *tkd = &tk2->obj_list_data;
struct object *o;
- unsigned int i = 0, len;
+ int len;
- SLIST_FOREACH(o, tkd->list, next) {
- if (i++ == idx)
- break;
- }
- if (!o)
+ o = *opaque;
+ if (o == NULL)
return -1;
- len = strlen(o->name) + 1;
- if (len > size)
+ len = snprintf(dstbuf, size, "%s", o->name);
+ if (len < 0 || len >= size)
return -1;
strcpy(dstbuf, o->name);
+ o = SLIST_NEXT(o, next);
+ *opaque = o;
return 0;
}
-int get_help_obj_list(cmdline_parse_token_hdr_t *tk, char *dstbuf, unsigned int size)
+static int
+cmdline_help_obj_list(cmdline_parse_token_hdr_t *tk, char *dstbuf,
+ unsigned int size)
{
snprintf(dstbuf, size, "Obj-List");
return 0;
}
+
+struct cmdline_token_ops token_obj_list_ops = {
+ .parse = parse_obj_list,
+ .complete_start = complete_obj_list_start,
+ .complete_iterate = complete_obj_list_iterate,
+ .complete_end = NULL,
+ .help = cmdline_help_obj_list,
+};