Allow partial command matches if unambiguous.

This commit is contained in:
Daniel Beer 2010-10-12 11:50:20 +13:00
parent 99c478d37f
commit 4f1502d198
1 changed files with 22 additions and 3 deletions

25
cmddb.c
View File

@ -245,18 +245,37 @@ const struct cmddb_record commands[] = {
int cmddb_get(const char *name, struct cmddb_record *ret) int cmddb_get(const char *name, struct cmddb_record *ret)
{ {
int len = strlen(name);
int i; int i;
const struct cmddb_record *found = NULL;
/* First look for an exact match */
for (i = 0; i < ARRAY_LEN(commands); i++) { for (i = 0; i < ARRAY_LEN(commands); i++) {
const struct cmddb_record *r = &commands[i]; const struct cmddb_record *r = &commands[i];
if (!strcasecmp(r->name, name)) { if (!strcasecmp(r->name, name)) {
memcpy(ret, r, sizeof(*ret)); found = r;
return 0; goto done;
} }
} }
return -1; /* Allow partial matches if unambiguous */
for (i = 0; i < ARRAY_LEN(commands); i++) {
const struct cmddb_record *r = &commands[i];
if (!strncasecmp(r->name, name, len)) {
if (found)
return -1;
found = r;
}
}
if (!found)
return -1;
done:
memcpy(ret, found, sizeof(*ret));
return 0;
} }
int cmddb_enum(cmddb_enum_func_t func, void *user_data) int cmddb_enum(cmddb_enum_func_t func, void *user_data)