[Asterisk-code-review] main/bridge: Use ast cli completion add. (asterisk[15])

Corey Farrell asteriskteam at digium.com
Sat Mar 17 19:44:46 CDT 2018


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


Change subject: main/bridge: Use ast_cli_completion_add.
......................................................................

main/bridge: Use ast_cli_completion_add.

Change-Id: I3775a696d6a57139fdf09651ecb786bcf1774509
---
M main/bridge.c
1 file changed, 39 insertions(+), 63 deletions(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/70/8570/1

diff --git a/main/bridge.c b/main/bridge.c
index 88d9e54..44a171d 100644
--- a/main/bridge.c
+++ b/main/bridge.c
@@ -5009,46 +5009,29 @@
 	return ao2_find(bridges, bridge_id, OBJ_SEARCH_KEY);
 }
 
-struct bridge_complete {
-	/*! Nth match to return. */
-	int state;
-	/*! Which match currently on. */
-	int which;
-};
-
-static int complete_bridge_live_search(void *obj, void *arg, void *data, int flags)
+static int complete_bridge_live_search(void *obj, void *arg, int flags)
 {
-	struct bridge_complete *search = data;
+	struct ast_bridge *bridge = obj;
 
-	if (++search->which > search->state) {
-		return CMP_MATCH;
+	if (ast_cli_completion_add(ast_strdup(bridge->uniqueid))) {
+		return CMP_STOP;
 	}
+
 	return 0;
 }
 
-static char *complete_bridge_live(const char *word, int state)
+static char *complete_bridge_live(const char *word)
 {
-	char *ret;
-	struct ast_bridge *bridge;
-	struct bridge_complete search = {
-		.state = state,
-		};
+	ao2_callback(bridges, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
+		complete_bridge_live_search, (char *) word);
 
-	bridge = ao2_callback_data(bridges, ast_strlen_zero(word) ? 0 : OBJ_PARTIAL_KEY,
-		complete_bridge_live_search, (char *) word, &search);
-	if (!bridge) {
-		return NULL;
-	}
-	ret = ast_strdup(bridge->uniqueid);
-	ao2_ref(bridge, -1);
-	return ret;
+	return NULL;
 }
 
-static char *complete_bridge_stasis(const char *word, int state)
+static char *complete_bridge_stasis(const char *word)
 {
-	char *ret = NULL;
-	int wordlen = strlen(word), which = 0;
-	RAII_VAR(struct ao2_container *, cached_bridges, NULL, ao2_cleanup);
+	int wordlen = strlen(word);
+	struct ao2_container *cached_bridges;
 	struct ao2_iterator iter;
 	struct stasis_message *msg;
 
@@ -5061,15 +5044,17 @@
 	for (; (msg = ao2_iterator_next(&iter)); ao2_ref(msg, -1)) {
 		struct ast_bridge_snapshot *snapshot = stasis_message_data(msg);
 
-		if (!strncasecmp(word, snapshot->uniqueid, wordlen) && (++which > state)) {
-			ret = ast_strdup(snapshot->uniqueid);
-			ao2_ref(msg, -1);
-			break;
+		if (!strncasecmp(word, snapshot->uniqueid, wordlen)) {
+			if (ast_cli_completion_add(ast_strdup(snapshot->uniqueid))) {
+				ao2_ref(msg, -1);
+				break;
+			}
 		}
 	}
 	ao2_iterator_destroy(&iter);
+	ao2_ref(cached_bridges, -1);
 
-	return ret;
+	return NULL;
 }
 
 static char *handle_bridge_show_all(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
@@ -5150,7 +5135,7 @@
 		return NULL;
 	case CLI_GENERATE:
 		if (a->pos == 2) {
-			return complete_bridge_stasis(a->word, a->n);
+			return complete_bridge_stasis(a->word);
 		}
 		return NULL;
 	}
@@ -5189,7 +5174,7 @@
 		return NULL;
 	case CLI_GENERATE:
 		if (a->pos == 2) {
-			return complete_bridge_live(a->word, a->n);
+			return complete_bridge_live(a->word);
 		}
 		return NULL;
 	}
@@ -5211,11 +5196,10 @@
 }
 #endif
 
-static char *complete_bridge_participant(const char *bridge_name, const char *line, const char *word, int pos, int state)
+static char *complete_bridge_participant(const char *bridge_name, const char *word)
 {
 	struct ast_bridge *bridge;
 	struct ast_bridge_channel *bridge_channel;
-	int which;
 	int wordlen;
 
 	bridge = ast_bridge_find_by_id(bridge_name);
@@ -5223,19 +5207,17 @@
 		return NULL;
 	}
 
-	{
-		SCOPED_LOCK(bridge_lock, bridge, ast_bridge_lock, ast_bridge_unlock);
+	wordlen = strlen(word);
 
-		which = 0;
-		wordlen = strlen(word);
-		AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
-			if (!strncasecmp(ast_channel_name(bridge_channel->chan), word, wordlen)
-				&& ++which > state) {
-				ao2_ref(bridge, -1);
-				return ast_strdup(ast_channel_name(bridge_channel->chan));
+	ast_bridge_lock(bridge);
+	AST_LIST_TRAVERSE(&bridge->channels, bridge_channel, entry) {
+		if (!strncasecmp(ast_channel_name(bridge_channel->chan), word, wordlen)) {
+			if (ast_cli_completion_add(ast_strdup(ast_channel_name(bridge_channel->chan)))) {
+				break;
 			}
 		}
 	}
+	ast_bridge_unlock(bridge);
 
 	ao2_ref(bridge, -1);
 
@@ -5245,7 +5227,6 @@
 static char *handle_bridge_kick_channel(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
 	static const char * const completions[] = { "all", NULL };
-	char *complete;
 	struct ast_bridge *bridge;
 
 	switch (cmd) {
@@ -5259,14 +5240,11 @@
 		return NULL;
 	case CLI_GENERATE:
 		if (a->pos == 2) {
-			return complete_bridge_live(a->word, a->n);
+			return complete_bridge_live(a->word);
 		}
 		if (a->pos == 3) {
-			complete = ast_cli_complete(a->word, completions, a->n);
-			if (!complete) {
-				complete = complete_bridge_participant(a->argv[2], a->line, a->word, a->pos, a->n - 1);
-			}
-			return complete;
+			ast_cli_complete(a->word, completions, -1);
+			return complete_bridge_participant(a->argv[2], a->word);
 		}
 		return NULL;
 	}
@@ -5367,24 +5345,22 @@
 #undef FORMAT
 }
 
-static char *complete_bridge_technology(const char *word, int state)
+static char *complete_bridge_technology(const char *word)
 {
 	struct ast_bridge_technology *cur;
-	char *res;
-	int which;
 	int wordlen;
 
-	which = 0;
 	wordlen = strlen(word);
 	AST_RWLIST_RDLOCK(&bridge_technologies);
 	AST_RWLIST_TRAVERSE(&bridge_technologies, cur, entry) {
-		if (!strncasecmp(cur->name, word, wordlen) && ++which > state) {
-			res = ast_strdup(cur->name);
-			AST_RWLIST_UNLOCK(&bridge_technologies);
-			return res;
+		if (!strncasecmp(cur->name, word, wordlen)) {
+			if (ast_cli_completion_add(ast_strdup(cur->name))) {
+				break;
+			}
 		}
 	}
 	AST_RWLIST_UNLOCK(&bridge_technologies);
+
 	return NULL;
 }
 
@@ -5403,7 +5379,7 @@
 		return NULL;
 	case CLI_GENERATE:
 		if (a->pos == 3) {
-			return complete_bridge_technology(a->word, a->n);
+			return complete_bridge_technology(a->word);
 		}
 		return NULL;
 	}

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

Gerrit-Project: asterisk
Gerrit-Branch: 15
Gerrit-MessageType: newchange
Gerrit-Change-Id: I3775a696d6a57139fdf09651ecb786bcf1774509
Gerrit-Change-Number: 8570
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/20180317/a26d4602/attachment.html>


More information about the asterisk-code-review mailing list