]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
CLI: Remove calls to ast_cli_generator.
authorCorey Farrell <git@cfware.com>
Thu, 9 Nov 2017 06:39:13 +0000 (01:39 -0500)
committerCorey Farrell <git@cfware.com>
Tue, 21 Nov 2017 14:47:38 +0000 (09:47 -0500)
The ability to add to localized storage cannot be supported by
ast_cli_generator.  The only calls to ast_cli_generator should be by
functions that need to proxy the CLI generator, for example 'cli check
permissions' or 'core show help'.

* ast_cli_generatornummatches now retrieves the vector of matches and
  reports the number of elements (not including 'best' match).
* test_substitution retrieves and iterates the vector.

Change-Id: I8cd6b93905363cf7a33a2d2b0e2a8f8446d9f248

include/asterisk/cli.h
main/cli.c
tests/test_substitution.c

index 7bf473c61bf7f3d3cc74ea2dd7b40c3df829662f..0a05a4c112a1377405c95862a130fdba30244f67 100644 (file)
@@ -282,6 +282,9 @@ int ast_cli_unregister_multiple(struct ast_cli_entry *e, int len);
  * Useful for readline, that's about it
  * \retval 0 on success
  * \retval -1 on failure
+ *
+ * Only call this function to proxy the CLI generator to
+ * another.
  */
 char *ast_cli_generator(const char *, const char *, int);
 
index f3411860c2c13ec58e4ddf3db1704359552ff3df..94ea959568d0adba2c0d6f1921a491f25b57152c 100644 (file)
@@ -2463,18 +2463,17 @@ static char *parse_args(const char *s, int *argc, const char *argv[], int max, i
 /*! \brief Return the number of unique matches for the generator */
 int ast_cli_generatornummatches(const char *text, const char *word)
 {
-       int matches = 0, i = 0;
-       char *buf = NULL, *oldbuf = NULL;
-
-       while ((buf = ast_cli_generator(text, word, i++))) {
-               if (!oldbuf || strcmp(buf,oldbuf))
-                       matches++;
-               if (oldbuf)
-                       ast_free(oldbuf);
-               oldbuf = buf;
-       }
-       if (oldbuf)
-               ast_free(oldbuf);
+       int matches;
+       struct ast_vector_string *vec = ast_cli_completion_vector(text, word);
+
+       if (!vec) {
+               return 0;
+       }
+
+       matches = AST_VECTOR_SIZE(vec) - 1;
+       AST_VECTOR_CALLBACK_VOID(vec, ast_free);
+       AST_VECTOR_PTR_FREE(vec);
+
        return matches;
 }
 
index 2635430a45e3a42edf4f379f3ab4dc069a4cb8fc..96ca29c7402f52eac235e596335739a502ec20ab 100644 (file)
@@ -45,6 +45,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 #include "asterisk/stringfields.h"
 #include "asterisk/threadstorage.h"
 #include "asterisk/test.h"
+#include "asterisk/vector.h"
 
 static enum ast_test_result_state test_chan_integer(struct ast_test *test,
                struct ast_channel *c, int *ifield, const char *expression)
@@ -227,6 +228,7 @@ AST_TEST_DEFINE(test_substitution)
        struct ast_channel *c;
        int i;
        enum ast_test_result_state res = AST_TEST_PASS;
+       struct ast_vector_string *funcs;
 
        switch (cmd) {
        case TEST_INIT:
@@ -304,11 +306,12 @@ AST_TEST_DEFINE(test_substitution)
 #undef TEST
 
        /* For testing dialplan functions */
-       for (i = 0; ; i++) {
-               char *cmd = ast_cli_generator("core show function", "", i);
-               if (cmd == NULL) {
-                       break;
-               }
+       funcs = ast_cli_completion_vector("core show function", "");
+
+       /* Skip "best match" element 0 */
+       for (i = 1; funcs && i < AST_VECTOR_SIZE(funcs); i++) {
+               char *cmd = AST_VECTOR_GET(funcs, i);
+
                if (strcmp(cmd, "CHANNEL") && strcmp(cmd, "CALLERID") && strncmp(cmd, "CURL", 4) &&
                                strncmp(cmd, "AES", 3) && strncmp(cmd, "BASE64", 6) &&
                                strcmp(cmd, "CDR") && strcmp(cmd, "ENV") && strcmp(cmd, "GLOBAL") &&
@@ -323,10 +326,14 @@ AST_TEST_DEFINE(test_substitution)
                                }
                        }
                }
-               ast_free(cmd);
        }
 
+       if (funcs) {
+               AST_VECTOR_CALLBACK_VOID(funcs, ast_free);
+               AST_VECTOR_PTR_FREE(funcs);
+       }
        ast_hangup(c);
+
        return res;
 }