<p>Corey Farrell has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/7339">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">CLI: Finish conversion of completion handling to vectors.<br><br>Change-Id: Ib81318f4ee52a5e73b003316e13fe9be1dd897a1<br>---<br>M main/asterisk.c<br>1 file changed, 22 insertions(+), 59 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/39/7339/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">diff --git a/main/asterisk.c b/main/asterisk.c<br>index 840fd14..2c86d1e 100644<br>--- a/main/asterisk.c<br>+++ b/main/asterisk.c<br>@@ -3009,10 +3009,9 @@<br>    return ast_str_buffer(prompt);<br> }<br> <br>-static char **ast_el_strtoarr(char *buf)<br>+static struct ast_vector_string *ast_el_strtoarr(char *buf)<br> {<br>    char *retstr;<br>-        char **match_list;<br>    struct ast_vector_string *vec = ast_calloc(1, sizeof(*vec));<br> <br>       if (!vec) {<br>@@ -3024,8 +3023,14 @@<br>                   break;<br>                }<br> <br>+         /* Older daemons sent duplicates. */<br>+         if (AST_VECTOR_GET_CMP(vec, retstr, strcasecmp)) {<br>+                   continue;<br>+            }<br>+<br>          retstr = ast_strdup(retstr);<br>-         if (!retstr || AST_VECTOR_APPEND(vec, retstr)) {<br>+             /* Older daemons sent unsorted. */<br>+           if (!retstr || AST_VECTOR_ADD_SORTED(vec, retstr, strcasecmp)) {<br>                      ast_free(retstr);<br>                     goto vector_cleanup;<br>          }<br>@@ -3035,15 +3040,7 @@<br>             goto vector_cleanup;<br>  }<br> <br>- if (AST_VECTOR_APPEND(vec, NULL)) {<br>-          /* We failed to NULL terminate the elements */<br>-               goto vector_cleanup;<br>- }<br>-<br>- match_list = AST_VECTOR_STEAL_ELEMENTS(vec);<br>- AST_VECTOR_PTR_FREE(vec);<br>-<br>- return match_list;<br>+   return vec;<br> <br> vector_cleanup:<br>      AST_VECTOR_CALLBACK_VOID(vec, ast_free);<br>@@ -3052,17 +3049,7 @@<br>      return NULL;<br> }<br> <br>-static int ast_el_sort_compare(const void *i1, const void *i2)<br>-{<br>-     char *s1, *s2;<br>-<br>-    s1 = ((char **)i1)[0];<br>-       s2 = ((char **)i2)[0];<br>-<br>-    return strcasecmp(s1, s2);<br>-}<br>-<br>-static void ast_cli_display_match_list(char **matches, int max)<br>+static void ast_cli_display_match_list(struct ast_vector_string *matches, int max)<br> {<br>  int idx = 1;<br>  /* find out how many entries can be put on one line, with two spaces between strings */<br>@@ -3075,14 +3062,9 @@<br>       for (;;) {<br>            int numoutputline;<br> <br>-                for (numoutputline = 0; numoutputline < limit && matches[idx]; idx++) {<br>-                   /* Don't print dupes */<br>-                  if ( (matches[idx+1] != NULL && strcmp(matches[idx], matches[idx+1]) == 0 ) ) {<br>-                              continue;<br>-                    }<br>-<br>+         for (numoutputline = 0; numoutputline < limit && idx < AST_VECTOR_SIZE(matches); idx++) {<br>                       numoutputline++;<br>-                     fprintf(stdout, "%-*s  ", max, matches[idx]);<br>+                      fprintf(stdout, "%-*s  ", max, AST_VECTOR_GET(matches, idx));<br>               }<br> <br>          if (!numoutputline) {<br>@@ -3098,8 +3080,7 @@<br> {<br>      int len = 0;<br>  char *ptr;<br>-   int nummatches = 0;<br>-  char **matches;<br>+      struct ast_vector_string *matches;<br>    int retval = CC_ERROR;<br>        char savechr;<br>         int res;<br>@@ -3173,44 +3154,28 @@<br>             matches = ast_el_strtoarr(mbuf);<br>              ast_free(mbuf);<br>       } else {<br>-             matches = ast_cli_completion_matches((char *)lf->buffer,ptr);<br>+             matches = ast_cli_completion_vector((char *)lf->buffer, ptr);<br>      }<br> <br>  if (matches) {<br>                int i;<br>                int maxlen, match_len;<br>+               const char *best_match = AST_VECTOR_GET(matches, 0);<br> <br>-              while (matches[nummatches + 1]) {<br>-                    nummatches++;<br>-                }<br>-<br>-         if (ast_opt_remote && nummatches > 1) {<br>-                   qsort(&matches[0], (size_t)(nummatches), sizeof(char *), ast_el_sort_compare);<br>-                   nummatches = 1;<br>-                      i = 1;<br>-                       while (matches[i + 1]) {<br>-                             if (strcasecmp(matches[i], matches[i + 1])) {<br>-                                        /* don't count duplicates. */<br>-                                    nummatches++;<br>-                                }<br>-                            i++;<br>-                 }<br>-            }<br>-<br>-         if (matches[0][0] != '\0') {<br>+         if (!ast_strlen_zero(best_match)) {<br>                   el_deletestr(editline, (int) len);<br>-                   el_insertstr(editline, matches[0]);<br>+                  el_insertstr(editline, best_match);<br>                   retval = CC_REFRESH;<br>          }<br> <br>-         if (nummatches == 1) {<br>+               if (AST_VECTOR_SIZE(matches) == 2) {<br>                  /* Found an exact match */<br>                    el_insertstr(editline, " ");<br>                        retval = CC_REFRESH;<br>          } else {<br>                      /* Must be more than one match */<br>-                    for (i = 1, maxlen = 0; matches[i]; i++) {<br>-                           match_len = strlen(matches[i]);<br>+                      for (i = 1, maxlen = 0; i < AST_VECTOR_SIZE(matches); i++) {<br>+                              match_len = strlen(AST_VECTOR_GET(matches, i));<br>                               if (match_len > maxlen) {<br>                                  maxlen = match_len;<br>                           }<br>@@ -3220,10 +3185,8 @@<br>                     ast_cli_display_match_list(matches, maxlen);<br>                  retval = CC_REDISPLAY;<br>                }<br>-            for (i = 0; matches[i]; i++) {<br>-                       ast_free(matches[i]);<br>-                }<br>-            ast_free(matches);<br>+           AST_VECTOR_CALLBACK_VOID(matches, ast_free);<br>+         AST_VECTOR_PTR_FREE(matches);<br>         }<br> <br>  *((char *) lf->cursor) = savechr;<br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/7339">change 7339</a>. To unsubscribe, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/7339"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 15 </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>
<div style="display:none"> Gerrit-Change-Id: Ib81318f4ee52a5e73b003316e13fe9be1dd897a1 </div>
<div style="display:none"> Gerrit-Change-Number: 7339 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: Corey Farrell <git@cfware.com> </div>