From: Alan T. DeKok Date: Tue, 24 Jul 2018 17:52:54 +0000 (-0400) Subject: move compare to common API X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=43dfb8c2ff45301ffdfdc7f8b1a5b45c6b7798c8;p=thirdparty%2Ffreeradius-server.git move compare to common API --- diff --git a/src/lib/server/command.c b/src/lib/server/command.c index 68f1fcdba39..1eab71b2344 100644 --- a/src/lib/server/command.c +++ b/src/lib/server/command.c @@ -2670,3 +2670,26 @@ int fr_command_print_help(FILE *fp, fr_cmd_t *head, char const *text) return 0; } + +/* See if partial string matches a full string. + * + * @param word the partial word to match + * @param name the name which "word" might match + * @return + * - false if they do not match + * - true if "word" is a prefix of "name" + * + */ +bool fr_command_strncmp(const char *word, const char *name) +{ + char const *p, *q; + + if (!*word) return true; + + SKIP_NAME(name); + + /* + * If we're done P (partial or full), that's a match. + */ + return (*p == '\0'); +} diff --git a/src/lib/server/command.h b/src/lib/server/command.h index 51e5581ccd1..deb4c581abe 100644 --- a/src/lib/server/command.h +++ b/src/lib/server/command.h @@ -90,6 +90,7 @@ void fr_command_info_init(TALLOC_CTX *ctx, fr_cmd_info_t *info); int fr_command_complete(fr_cmd_t *head, char const *text, int start, int max_expansions, char const **expansions); int fr_command_print_help(FILE *fp, fr_cmd_t *head, char const *text); +bool fr_command_strncmp(const char *text, const char *name) CC_HINT(nonnull); #ifdef __cplusplus } diff --git a/src/lib/server/module.c b/src/lib/server/module.c index 313889b46be..0647360fb3e 100644 --- a/src/lib/server/module.c +++ b/src/lib/server/module.c @@ -756,29 +756,14 @@ static int _module_tab_expand(void *instance, void *ctx) { module_instance_t *mi = talloc_get_type_abort(instance, module_instance_t); module_tab_expand_t *mt = ctx; - char const *p, *q; if (mt->count >= mt->max_expansions) return 1; - if (!*mt->text) { - matched: + if (fr_command_strncmp(mt->text, mi->name)) { mt->expansions[mt->count] = strdup(mi->name); mt->count++; - return 0; - } - - /* - * @todo - put this in a helper function. - */ - p = mt->text; - q = mi->name; - while (*p && *q && (*p == *q)) { - p++; - q++; } - if (!*p) goto matched; - return 0; }