Avoid destroying the CLI line when moving the cursor backward and trying to autocomplete.

When moving the cursor backward and pressing TAB to autocomplete, a NULL is put
in the line and we are loosing what we have already wrote after the actual
cursor position.

(closes issue #14373)
Reported by: eliel
Patches:
      asterisk.c.patch uploaded by eliel (license 64)
      Tested by: lmadsen



git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/1.4@184188 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Eliel C. Sardanons
2009-03-25 14:12:54 +00:00
parent 4a209dbe40
commit a81abfbd02

View File

@@ -2121,11 +2121,12 @@ static char *cli_complete(EditLine *el, int ch)
int nummatches = 0; int nummatches = 0;
char **matches; char **matches;
int retval = CC_ERROR; int retval = CC_ERROR;
char buf[2048]; char buf[2048], savechr;
int res; int res;
LineInfo *lf = (LineInfo *)el_line(el); LineInfo *lf = (LineInfo *)el_line(el);
savechr = *(char *)lf->cursor;
*(char *)lf->cursor = '\0'; *(char *)lf->cursor = '\0';
ptr = (char *)lf->cursor; ptr = (char *)lf->cursor;
if (ptr) { if (ptr) {
@@ -2151,8 +2152,10 @@ static char *cli_complete(EditLine *el, int ch)
char *mbuf; char *mbuf;
int mlen = 0, maxmbuf = 2048; int mlen = 0, maxmbuf = 2048;
/* Start with a 2048 byte buffer */ /* Start with a 2048 byte buffer */
if (!(mbuf = ast_malloc(maxmbuf))) if (!(mbuf = ast_malloc(maxmbuf))) {
lf->cursor[0] = savechr;
return (char *)(CC_ERROR); return (char *)(CC_ERROR);
}
snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr); snprintf(buf, sizeof(buf),"_COMMAND MATCHESARRAY \"%s\" \"%s\"", lf->buffer, ptr);
fdsend(ast_consock, buf); fdsend(ast_consock, buf);
res = 0; res = 0;
@@ -2161,8 +2164,10 @@ static char *cli_complete(EditLine *el, int ch)
if (mlen + 1024 > maxmbuf) { if (mlen + 1024 > maxmbuf) {
/* Every step increment buffer 1024 bytes */ /* Every step increment buffer 1024 bytes */
maxmbuf += 1024; maxmbuf += 1024;
if (!(mbuf = ast_realloc(mbuf, maxmbuf))) if (!(mbuf = ast_realloc(mbuf, maxmbuf))) {
lf->cursor[0] = savechr;
return (char *)(CC_ERROR); return (char *)(CC_ERROR);
}
} }
/* Only read 1024 bytes at a time */ /* Only read 1024 bytes at a time */
res = read(ast_consock, mbuf + mlen, 1024); res = read(ast_consock, mbuf + mlen, 1024);
@@ -2222,6 +2227,8 @@ static char *cli_complete(EditLine *el, int ch)
free(matches); free(matches);
} }
lf->cursor[0] = savechr;
return (char *)(long)retval; return (char *)(long)retval;
} }