lineedit: clean up tab completion code (variable reuse, comments)

Noted bugs in behavior.
Added debugging machinery.
Decoupled variables reused for unrelated purposes: apparently,
when not forced to use liveness analysis, gcc fares better.

function                                             old     new   delta
complete_cmd_dir_file                                699     705      +6
collapse_pos                                          75      79      +4
build_match_prefix                                   892     838     -54
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 10/-54)            Total: -44 bytes

Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
Denys Vlasenko 2010-09-03 12:53:15 +02:00
parent 57ea9b488b
commit 2679e3c8cc

View File

@ -716,6 +716,7 @@ static NOINLINE void complete_cmd_dir_file(char *command, int type)
char **paths = path1; char **paths = path1;
int npaths; int npaths;
int i; int i;
unsigned pf_len;
char *pfind; char *pfind;
char *dirbuf = NULL; char *dirbuf = NULL;
@ -738,6 +739,7 @@ static NOINLINE void complete_cmd_dir_file(char *command, int type)
#endif #endif
path1[0] = dirbuf; path1[0] = dirbuf;
} }
pf_len = strlen(pfind);
for (i = 0; i < npaths; i++) { for (i = 0; i < npaths; i++) {
DIR *dir; DIR *dir;
@ -756,7 +758,7 @@ static NOINLINE void complete_cmd_dir_file(char *command, int type)
if (!pfind[0] && DOT_OR_DOTDOT(name_found)) if (!pfind[0] && DOT_OR_DOTDOT(name_found))
continue; continue;
/* match? */ /* match? */
if (strncmp(name_found, pfind, strlen(pfind)) != 0) if (strncmp(name_found, pfind, pf_len) != 0)
continue; /* no */ continue; /* no */
found = concat_path_file(paths[i], name_found); found = concat_path_file(paths[i], name_found);
@ -812,21 +814,31 @@ static NOINLINE void complete_cmd_dir_file(char *command, int type)
#define QUOT (UCHAR_MAX+1) #define QUOT (UCHAR_MAX+1)
#define int_buf (S.find_match__int_buf) #define int_buf (S.find_match__int_buf)
#define pos_buf (S.find_match__pos_buf) #define pos_buf (S.find_match__pos_buf)
#define dbg_bmp 0
static void collapse_pos(int beg, int end) static void collapse_pos(int beg, int end)
{ {
/* beg must be <= end */ /* beg must be <= end */
if (beg == end)
return;
memmove(int_buf+beg, int_buf+end, (MAX_LINELEN+1-end) * sizeof(int_buf[0])); memmove(int_buf+beg, int_buf+end, (MAX_LINELEN+1-end) * sizeof(int_buf[0]));
memmove(pos_buf+beg, pos_buf+end, (MAX_LINELEN+1-end) * sizeof(pos_buf[0])); memmove(pos_buf+beg, pos_buf+end, (MAX_LINELEN+1-end) * sizeof(pos_buf[0]));
if (dbg_bmp) {
int i;
for (i = 0; int_buf[i]; i++)
bb_putchar((unsigned char)int_buf[i]);
bb_putchar('\n');
}
} }
static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes) static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
{ {
int i, j; int i, j;
int command_mode; int command_mode;
int c, c2;
/* Were local, but it used too much stack */ /* Were local, but it used too much stack */
/* int16_t int_buf[MAX_LINELEN + 1]; */ /* int16_t int_buf[MAX_LINELEN + 1]; */
/* int16_t pos_buf[MAX_LINELEN + 1]; */ /* int16_t pos_buf[MAX_LINELEN + 1]; */
if (dbg_bmp) printf("\n%s\n", matchBuf);
for (i = 0;; i++) { for (i = 0;; i++) {
int_buf[i] = (unsigned char)matchBuf[i]; int_buf[i] = (unsigned char)matchBuf[i];
if (int_buf[i] == 0) { if (int_buf[i] == 0) {
@ -845,20 +857,21 @@ static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
} }
} }
/* Quote-mark "chars" and 'chars' */ /* Quote-mark "chars" and 'chars' */
c2 = 0; {
for (i = 0; int_buf[i]; i++) { int in_quote = 0;
c = int_buf[i]; for (i = 0; int_buf[i]; i++) {
if (c == '\'' || c == '"') { int cur = int_buf[i];
if (c2 == 0) if (cur == '\'' || cur == '"') {
c2 = c; if (!in_quote)
else { in_quote = cur;
if (c == c2) else if (cur == in_quote)
c2 = 0; in_quote = 0;
else else
int_buf[i] |= QUOT; int_buf[i] |= QUOT;
} else if (in_quote && cur != '$') {
int_buf[i] |= QUOT;
} }
} else if (c2 != 0 && c != '$') }
int_buf[i] |= QUOT;
} }
/* Remove everything up to command delimiters: /* Remove everything up to command delimiters:
@ -866,64 +879,61 @@ static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
* but careful with '>&' '<&' '>|' * but careful with '>&' '<&' '>|'
*/ */
for (i = 0; int_buf[i]; i++) { for (i = 0; int_buf[i]; i++) {
int n; int cur = int_buf[i];
c = int_buf[i]; if (cur == ';' || cur == '&' || cur == '|') {
c2 = int_buf[i + 1]; int prev = i ? int_buf[i - 1] : 0;
j = i ? int_buf[i - 1] : -1; if (cur == '&' && (prev == '>' || prev == '<')) {
n = 0; continue;
if (c == ';' || c == '&' || c == '|') { } else if (cur == '|' && prev == '>') {
n = 1 + (c == c2); continue;
if (c == '&') { }
if (j == '>' || j == '<') collapse_pos(0, i + 1 + (cur == int_buf[i + 1]));
n = 0; i = -1; /* back to square 1 */
} else if (c == '|' && j == '>')
n = 0;
}
if (n) {
collapse_pos(0, i + n);
i = -1; /* hack increment */
} }
} }
/* Remove all `cmd` */ /* Remove all `cmd` */
//BUG: `cmd` should count as a word: `cmd` c<tab> should search for files c*, not commands c*
for (i = 0; int_buf[i]; i++) { for (i = 0; int_buf[i]; i++) {
if (int_buf[i] == '`') { if (int_buf[i] == '`') {
for (j = i + 1; int_buf[j]; j++) { for (j = i + 1; int_buf[j]; j++) {
if (int_buf[j] == '`') { if (int_buf[j] == '`') {
collapse_pos(i, j + 1); collapse_pos(i, j + 1);
j = 0; goto next;
break;
} }
} }
if (j) { /* No closing ` - command mode, remove all up to ` */
/* No closing ` - command mode, remove all up to ` */ collapse_pos(0, i + 1);
collapse_pos(0, i + 1); break;
break; next:
}
i--; /* hack increment */ i--; /* hack increment */
} }
} }
/* Remove (command...(command...)...) and {command...{command...}...} */ /* Remove (command...(command...)...) and {command...{command...}...} */
c = 0; /* "recursive" level */ {
c2 = 0; int paren_lvl = 0;
for (i = 0; int_buf[i]; i++) { int curly_lvl = 0;
if (int_buf[i] == '(' || int_buf[i] == '{') { for (i = 0; int_buf[i]; i++) {
if (int_buf[i] == '(') if (int_buf[i] == '(' || int_buf[i] == '{') {
c++; if (int_buf[i] == '(')
else paren_lvl++;
c2++; else
collapse_pos(0, i + 1); curly_lvl++;
i = -1; /* hack increment */ collapse_pos(0, i + 1);
i = -1; /* hack increment */
}
} }
} for (i = 0; pos_buf[i] >= 0 && (paren_lvl > 0 || curly_lvl > 0); i++) {
for (i = 0; pos_buf[i] >= 0 && (c > 0 || c2 > 0); i++) { if ((int_buf[i] == ')' && paren_lvl > 0)
if ((int_buf[i] == ')' && c > 0) || (int_buf[i] == '}' && c2 > 0)) { || (int_buf[i] == '}' && curly_lvl > 0)
if (int_buf[i] == ')') ) {
c--; if (int_buf[i] == ')')
else paren_lvl--;
c2--; else
collapse_pos(0, i + 1); curly_lvl--;
i = -1; /* hack increment */ collapse_pos(0, i + 1);
i = -1; /* hack increment */
}
} }
} }
@ -931,8 +941,7 @@ static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
for (i = 0; int_buf[i]; i++) for (i = 0; int_buf[i]; i++)
if (int_buf[i] != ' ') if (int_buf[i] != ' ')
break; break;
if (i) collapse_pos(0, i);
collapse_pos(0, i);
/* Determine completion mode */ /* Determine completion mode */
command_mode = FIND_EXE_ONLY; command_mode = FIND_EXE_ONLY;
@ -942,6 +951,7 @@ static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
&& command_mode == FIND_EXE_ONLY && command_mode == FIND_EXE_ONLY
&& matchBuf[pos_buf[0]] == 'c' && matchBuf[pos_buf[0]] == 'c'
&& matchBuf[pos_buf[1]] == 'd' && matchBuf[pos_buf[1]] == 'd'
//BUG: must check "cd ", not "cd"
) { ) {
command_mode = FIND_DIR_ONLY; command_mode = FIND_DIR_ONLY;
} else { } else {
@ -950,36 +960,42 @@ static NOINLINE int build_match_prefix(char *matchBuf, int *len_with_quotes)
} }
} }
} }
if (dbg_bmp) printf("command_mode(0:exe/1:dir/2:file):%d\n", command_mode);
/* Remove everything except last word */ /* Remove everything except last word */
for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */ for (i = 0; int_buf[i]; i++) /* quasi-strlen(int_buf) */
continue; continue;
for (--i; i >= 0; i--) { for (--i; i >= 0; i--) {
c = int_buf[i]; int cur = int_buf[i];
if (c == ' ' || c == '<' || c == '>' || c == '|' || c == '&') { if (cur == ' ' || cur == '<' || cur == '>' || cur == '|' || cur == '&') {
collapse_pos(0, i + 1); collapse_pos(0, i + 1);
break; break;
} }
} }
/* Skip all leading unquoted ' or " */ /* Skip all leading unquoted ' or " */
//BUG: bash doesn't do this
for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++) for (i = 0; int_buf[i] == '\'' || int_buf[i] == '"'; i++)
continue; continue;
/* collapse quote or unquote // or /~ */ /* Skip quoted or unquoted // or /~ */
while ((int_buf[i] & ~QUOT) == '/' //BUG: bash doesn't do this
&& ((int_buf[i+1] & ~QUOT) == '/' || (int_buf[i+1] & ~QUOT) == '~') while ((char)int_buf[i] == '/'
&& ((char)int_buf[i+1] == '/' || (char)int_buf[i+1] == '~')
) { ) {
i++; i++;
} }
/* set only match and destroy quotes */ /* set only match and destroy quotes */
j = 0; {
for (c = 0; pos_buf[i] >= 0; i++) { int pos = 0;
matchBuf[c++] = matchBuf[pos_buf[i]]; for (j = 0; pos_buf[i] >= 0; i++) {
j = pos_buf[i] + 1; matchBuf[j++] = matchBuf[pos_buf[i]];
pos = pos_buf[i] + 1;
}
matchBuf[j] = '\0';
/* old length matchBuf with quotes symbols */
*len_with_quotes = pos ? pos - pos_buf[0] : 0;
if (dbg_bmp) printf("len_with_quotes:%d\n", *len_with_quotes);
} }
matchBuf[c] = '\0';
/* old length matchBuf with quotes symbols */
*len_with_quotes = j ? j - pos_buf[0] : 0;
return command_mode; return command_mode;
} }