[Asterisk-code-review] CLI: Remove calls to ast cli generator. (asterisk[master])

Corey Farrell asteriskteam at digium.com
Thu Nov 9 14:58:33 CST 2017


Corey Farrell has uploaded this change for review. ( https://gerrit.asterisk.org/7167


Change subject: CLI: Remove calls to ast_cli_generator.
......................................................................

CLI: Remove calls to ast_cli_generator.

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
---
M include/asterisk/cli.h
M main/cli.c
M tests/test_substitution.c
3 files changed, 25 insertions(+), 16 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/67/7167/1

diff --git a/include/asterisk/cli.h b/include/asterisk/cli.h
index 3ed88eb..51f077f 100644
--- a/include/asterisk/cli.h
+++ b/include/asterisk/cli.h
@@ -287,6 +287,9 @@
  * 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);
 
diff --git a/main/cli.c b/main/cli.c
index b7626d4..65421ed 100644
--- a/main/cli.c
+++ b/main/cli.c
@@ -2473,18 +2473,17 @@
 /*! \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;
+	int matches;
+	struct ast_vector_string *vec = ast_cli_completion_vector(text, word);
 
-	while ((buf = ast_cli_generator(text, word, i++))) {
-		if (!oldbuf || strcmp(buf,oldbuf))
-			matches++;
-		if (oldbuf)
-			ast_free(oldbuf);
-		oldbuf = buf;
+	if (!vec) {
+		return 0;
 	}
-	if (oldbuf)
-		ast_free(oldbuf);
+
+	matches = AST_VECTOR_SIZE(vec) - 1;
+	AST_VECTOR_CALLBACK_VOID(vec, ast_free);
+	AST_VECTOR_PTR_FREE(vec);
+
 	return matches;
 }
 
diff --git a/tests/test_substitution.c b/tests/test_substitution.c
index 3a1dc1f..0c201be 100644
--- a/tests/test_substitution.c
+++ b/tests/test_substitution.c
@@ -43,6 +43,7 @@
 #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)
@@ -225,6 +226,7 @@
 	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:
@@ -302,11 +304,12 @@
 #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; 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") &&
@@ -321,10 +324,14 @@
 				}
 			}
 		}
-		ast_free(cmd);
 	}
 
+	if (funcs) {
+		AST_VECTOR_CALLBACK_VOID(funcs, ast_free);
+		AST_VECTOR_PTR_FREE(funcs);
+	}
 	ast_hangup(c);
+
 	return res;
 }
 

-- 
To view, visit https://gerrit.asterisk.org/7167
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8cd6b93905363cf7a33a2d2b0e2a8f8446d9f248
Gerrit-Change-Number: 7167
Gerrit-PatchSet: 1
Gerrit-Owner: Corey Farrell <git at cfware.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171109/f8587e00/attachment-0001.html>


More information about the asterisk-code-review mailing list