[Asterisk-code-review] CLI: Finish conversion of completion handling to vectors. (asterisk[15])

Corey Farrell asteriskteam at digium.com
Tue Nov 21 08:55:25 CST 2017


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


Change subject: CLI: Finish conversion of completion handling to vectors.
......................................................................

CLI: Finish conversion of completion handling to vectors.

Change-Id: Ib81318f4ee52a5e73b003316e13fe9be1dd897a1
---
M main/asterisk.c
1 file changed, 22 insertions(+), 59 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/39/7339/1

diff --git a/main/asterisk.c b/main/asterisk.c
index 840fd14..2c86d1e 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -3009,10 +3009,9 @@
 	return ast_str_buffer(prompt);
 }
 
-static char **ast_el_strtoarr(char *buf)
+static struct ast_vector_string *ast_el_strtoarr(char *buf)
 {
 	char *retstr;
-	char **match_list;
 	struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));
 
 	if (!vec) {
@@ -3024,8 +3023,14 @@
 			break;
 		}
 
+		/* Older daemons sent duplicates. */
+		if (AST_VECTOR_GET_CMP(vec, retstr, strcasecmp)) {
+			continue;
+		}
+
 		retstr = ast_strdup(retstr);
-		if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {
+		/* Older daemons sent unsorted. */
+		if (!retstr || AST_VECTOR_ADD_SORTED(vec, retstr, strcasecmp)) {
 			ast_free(retstr);
 			goto vector_cleanup;
 		}
@@ -3035,15 +3040,7 @@
 		goto vector_cleanup;
 	}
 
-	if (AST_VECTOR_APPEND(vec, NULL)) {
-		/* We failed to NULL terminate the elements */
-		goto vector_cleanup;
-	}
-
-	match_list = AST_VECTOR_STEAL_ELEMENTS(vec);
-	AST_VECTOR_PTR_FREE(vec);
-
-	return match_list;
+	return vec;
 
 vector_cleanup:
 	AST_VECTOR_CALLBACK_VOID(vec, ast_free);
@@ -3052,17 +3049,7 @@
 	return NULL;
 }
 
-static int ast_el_sort_compare(const void *i1, const void *i2)
-{
-	char *s1, *s2;
-
-	s1 = ((char **)i1)[0];
-	s2 = ((char **)i2)[0];
-
-	return strcasecmp(s1, s2);
-}
-
-static void ast_cli_display_match_list(char **matches, int max)
+static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)
 {
 	int idx = 1;
 	/* find out how many entries can be put on one line, with two spaces between strings */
@@ -3075,14 +3062,9 @@
 	for (;;) {
 		int numoutputline;
 
-		for (numoutputline = 0; numoutputline < limit && matches[idx]; idx++) {
-			/* Don't print dupes */
-			if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {
-				continue;
-			}
-
+		for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {
 			numoutputline++;
-			fprintf(stdout, "%-*s  ", max, matches[idx]);
+			fprintf(stdout, "%-*s  ", max, AST_VECTOR_GET(matches, idx));
 		}
 
 		if (!numoutputline) {
@@ -3098,8 +3080,7 @@
 {
 	int len = 0;
 	char *ptr;
-	int nummatches = 0;
-	char **matches;
+	struct ast_vector_string *matches;
 	int retval = CC_ERROR;
 	char savechr;
 	int res;
@@ -3173,44 +3154,28 @@
 		matches = ast_el_strtoarr(mbuf);
 		ast_free(mbuf);
 	} else {
-		matches = ast_cli_completion_matches((char *)lf->buffer,ptr);
+		matches = ast_cli_completion_vector((char *)lf->buffer, ptr);
 	}
 
 	if (matches) {
 		int i;
 		int maxlen, match_len;
+		const char *best_match = AST_VECTOR_GET(matches, 0);
 
-		while (matches[nummatches + 1]) {
-			nummatches++;
-		}
-
-		if (ast_opt_remote && nummatches > 1) {
-			qsort(&matches[0], (size_t)(nummatches), sizeof(char *), ast_el_sort_compare);
-			nummatches = 1;
-			i = 1;
-			while (matches[i + 1]) {
-				if (strcasecmp(matches[i], matches[i + 1])) {
-					/* don't count duplicates. */
-					nummatches++;
-				}
-				i++;
-			}
-		}
-
-		if (matches[0][0] != '\0') {
+		if (!ast_strlen_zero(best_match)) {
 			el_deletestr(editline, (int) len);
-			el_insertstr(editline, matches[0]);
+			el_insertstr(editline, best_match);
 			retval = CC_REFRESH;
 		}
 
-		if (nummatches == 1) {
+		if (AST_VECTOR_SIZE(matches) == 2) {
 			/* Found an exact match */
 			el_insertstr(editline, " ");
 			retval = CC_REFRESH;
 		} else {
 			/* Must be more than one match */
-			for (i = 1, maxlen = 0; matches[i]; i++) {
-				match_len = strlen(matches[i]);
+			for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {
+				match_len = strlen(AST_VECTOR_GET(matches, i));
 				if (match_len > maxlen) {
 					maxlen = match_len;
 				}
@@ -3220,10 +3185,8 @@
 			ast_cli_display_match_list(matches, maxlen);
 			retval = CC_REDISPLAY;
 		}
-		for (i = 0; matches[i]; i++) {
-			ast_free(matches[i]);
-		}
-		ast_free(matches);
+		AST_VECTOR_CALLBACK_VOID(matches, ast_free);
+		AST_VECTOR_PTR_FREE(matches);
 	}
 
 	*((char *) lf->cursor) = savechr;

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

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib81318f4ee52a5e73b003316e13fe9be1dd897a1
Gerrit-Change-Number: 7339
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/20171121/c1271f18/attachment.html>


More information about the asterisk-code-review mailing list