[Asterisk-code-review] CLI: Refactor ast cli display match list. (asterisk[master])

Jenkins2 asteriskteam at digium.com
Mon Nov 27 13:23:52 CST 2017


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/7266 )

Change subject: CLI: Refactor ast_cli_display_match_list.
......................................................................

CLI: Refactor ast_cli_display_match_list.

* Stop estimating line count, just print until we run out of matches.
* Stop freeing entries, the caller does that anyways.
* Stop calculating / returning numoutput, it was ignored.

Change-Id: I7f92afa8bea92241a95227587367424c8c32a5cb
---
M main/asterisk.c
1 file changed, 15 insertions(+), 29 deletions(-)

Approvals:
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/asterisk.c b/main/asterisk.c
index 77046f2..2908b46 100644
--- a/main/asterisk.c
+++ b/main/asterisk.c
@@ -3071,51 +3071,37 @@
 	return strcasecmp(s1, s2);
 }
 
-static int ast_cli_display_match_list(char **matches, int len, int max)
+static void ast_cli_display_match_list(char **matches, int len, int max)
 {
-	int i, idx, limit, count;
-	int screenwidth = 0;
-	int numoutput = 0, numoutputline = 0;
-
-	screenwidth = ast_get_termcols(STDOUT_FILENO);
-
+	int idx = 1;
 	/* find out how many entries can be put on one line, with two spaces between strings */
-	limit = screenwidth / (max + 2);
-	if (limit == 0)
+	int limit = ast_get_termcols(STDOUT_FILENO) / (max + 2);
+
+	if (limit == 0) {
 		limit = 1;
-
-	/* how many lines of output */
-	count = len / limit;
-	if (count * limit < len)
-		count++;
-
-	idx = 1;
+	}
 
 	qsort(&matches[0], (size_t)(len), sizeof(char *), ast_el_sort_compare);
 
-	for (; count > 0; count--) {
-		numoutputline = 0;
-		for (i = 0; i < limit && matches[idx]; i++, idx++) {
+	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 ) ) {
-				i--;
-				ast_free(matches[idx]);
-				matches[idx] = NULL;
 				continue;
 			}
 
-			numoutput++;
 			numoutputline++;
 			fprintf(stdout, "%-*s  ", max, matches[idx]);
-			ast_free(matches[idx]);
-			matches[idx] = NULL;
 		}
-		if (numoutputline > 0)
-			fprintf(stdout, "\n");
-	}
 
-	return numoutput;
+		if (!numoutputline) {
+			break;
+		}
+
+		fprintf(stdout, "\n");
+	}
 }
 
 

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

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: I7f92afa8bea92241a95227587367424c8c32a5cb
Gerrit-Change-Number: 7266
Gerrit-PatchSet: 2
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20171127/1d6f3a1c/attachment-0001.html>


More information about the asterisk-code-review mailing list