lineedit: allocate matchBuf only temporarily: saves MAX_LINELEN bytes
function old new delta input_tab 1027 1041 +14 build_match_prefix 590 562 -28 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 14/-28) Total: -14 bytes Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
This commit is contained in:
parent
ba0e103a66
commit
76939e7b72
@ -150,11 +150,6 @@ struct lineedit_statics {
|
|||||||
#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
|
#if ENABLE_FEATURE_EDITING_ASK_TERMINAL
|
||||||
smallint sent_ESC_br6n;
|
smallint sent_ESC_br6n;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Formerly these were big buffers on stack: */
|
|
||||||
#if ENABLE_FEATURE_TAB_COMPLETION
|
|
||||||
char input_tab__matchBuf[MAX_LINELEN];
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* See lineedit_ptr_hack.c */
|
/* See lineedit_ptr_hack.c */
|
||||||
@ -810,17 +805,17 @@ static NOINLINE unsigned complete_cmd_dir_file(const char *command, int type)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* build_match_prefix:
|
/* build_match_prefix:
|
||||||
* On entry, matchBuf contains everything up to cursor at the moment <tab>
|
* On entry, match_buf contains everything up to cursor at the moment <tab>
|
||||||
* was pressed. This function looks at it, figures out what part of it
|
* was pressed. This function looks at it, figures out what part of it
|
||||||
* constitutes the command/file/directory prefix to use for completion,
|
* constitutes the command/file/directory prefix to use for completion,
|
||||||
* and rewrites matchBuf to contain only that part.
|
* and rewrites match_buf to contain only that part.
|
||||||
*/
|
*/
|
||||||
|
#define dbg_bmp 0
|
||||||
/* Helpers: */
|
/* Helpers: */
|
||||||
/* QUOT is used on elements of int_buf[], which are bytes,
|
/* QUOT is used on elements of int_buf[], which are bytes,
|
||||||
* not Unicode chars. Therefore it works correctly even in Unicode mode.
|
* not Unicode chars. Therefore it works correctly even in Unicode mode.
|
||||||
*/
|
*/
|
||||||
#define QUOT (UCHAR_MAX+1)
|
#define QUOT (UCHAR_MAX+1)
|
||||||
#define dbg_bmp 0
|
|
||||||
static void remove_chunk(int16_t *int_buf, int beg, int end)
|
static void remove_chunk(int16_t *int_buf, int beg, int end)
|
||||||
{
|
{
|
||||||
/* beg must be <= end */
|
/* beg must be <= end */
|
||||||
@ -837,25 +832,29 @@ static void remove_chunk(int16_t *int_buf, int beg, int end)
|
|||||||
bb_putchar('\n');
|
bb_putchar('\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
static NOINLINE int build_match_prefix(char *matchBuf)
|
/* Caller ensures that match_buf points to a malloced buffer
|
||||||
|
* big enough to hold strlen(match_buf)*2 + 2
|
||||||
|
*/
|
||||||
|
static NOINLINE int build_match_prefix(char *match_buf)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j;
|
||||||
int command_mode;
|
int command_mode;
|
||||||
int16_t *int_buf;
|
int16_t *int_buf = (int16_t*)match_buf;
|
||||||
|
|
||||||
if (dbg_bmp) printf("\n%s\n", matchBuf);
|
if (dbg_bmp) printf("\n%s\n", match_buf);
|
||||||
|
|
||||||
int_buf = xmalloc(sizeof(int_buf[0]) * (strlen(matchBuf) + 1));
|
/* Copy in reverse order, since they overlap */
|
||||||
i = 0;
|
i = strlen(match_buf);
|
||||||
while ((int_buf[i] = (unsigned char)matchBuf[i]) != '\0')
|
do {
|
||||||
i++;
|
int_buf[i] = (unsigned char)match_buf[i];
|
||||||
|
i--;
|
||||||
|
} while (i >= 0);
|
||||||
|
|
||||||
/* Mark every \c as "quoted c" */
|
/* Mark every \c as "quoted c" */
|
||||||
for (i = j = 0; matchBuf[i]; i++, j++) {
|
for (i = 0; int_buf[i]; i++) {
|
||||||
if (matchBuf[i] == '\\') {
|
if (int_buf[i] == '\\') {
|
||||||
remove_chunk(int_buf, j, j + 1);
|
remove_chunk(int_buf, i, i + 1);
|
||||||
int_buf[j] |= QUOT;
|
int_buf[i] |= QUOT;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Quote-mark "chars" and 'chars', drop delimiters */
|
/* Quote-mark "chars" and 'chars', drop delimiters */
|
||||||
@ -964,13 +963,12 @@ static NOINLINE int build_match_prefix(char *matchBuf)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Store only match prefix */
|
/* Convert back to string of _chars_ */
|
||||||
i = 0;
|
i = 0;
|
||||||
while ((matchBuf[i] = int_buf[i]) != '\0')
|
while ((match_buf[i] = int_buf[i]) != '\0')
|
||||||
i++;
|
i++;
|
||||||
free(int_buf);
|
|
||||||
|
|
||||||
if (dbg_bmp) printf("final matchBuf:'%s'\n", matchBuf);
|
if (dbg_bmp) printf("final match_buf:'%s'\n", match_buf);
|
||||||
|
|
||||||
return command_mode;
|
return command_mode;
|
||||||
}
|
}
|
||||||
@ -1037,9 +1035,8 @@ static char *add_quote_for_spec_chars(char *found)
|
|||||||
static NOINLINE void input_tab(smallint *lastWasTab)
|
static NOINLINE void input_tab(smallint *lastWasTab)
|
||||||
{
|
{
|
||||||
char *chosen_match;
|
char *chosen_match;
|
||||||
|
char *match_buf;
|
||||||
size_t len_found;
|
size_t len_found;
|
||||||
/* char matchBuf[MAX_LINELEN]; */
|
|
||||||
#define matchBuf (S.input_tab__matchBuf)
|
|
||||||
/* Length of string used for matching */
|
/* Length of string used for matching */
|
||||||
unsigned match_pfx_len = match_pfx_len;
|
unsigned match_pfx_len = match_pfx_len;
|
||||||
int find_type;
|
int find_type;
|
||||||
@ -1065,21 +1062,26 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
}
|
}
|
||||||
|
|
||||||
*lastWasTab = 1;
|
*lastWasTab = 1;
|
||||||
|
chosen_match = NULL;
|
||||||
|
|
||||||
/* Make a local copy of the string --
|
/* Make a local copy of the string up to the position of the cursor.
|
||||||
* up to the position of the cursor */
|
* build_match_prefix will expand it into int16_t's, need to allocate
|
||||||
|
* twice as much as the string_len+1.
|
||||||
|
* (we then also (ab)use this extra space later - see (**))
|
||||||
|
*/
|
||||||
|
match_buf = xmalloc(MAX_LINELEN * sizeof(int16_t));
|
||||||
#if !ENABLE_UNICODE_SUPPORT
|
#if !ENABLE_UNICODE_SUPPORT
|
||||||
save_string(matchBuf, cursor + 1);
|
save_string(match_buf, cursor + 1); /* +1 for NUL */
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
CHAR_T wc = command_ps[cursor];
|
CHAR_T wc = command_ps[cursor];
|
||||||
command_ps[cursor] = BB_NUL;
|
command_ps[cursor] = BB_NUL;
|
||||||
save_string(matchBuf, MAX_LINELEN);
|
save_string(match_buf, MAX_LINELEN);
|
||||||
command_ps[cursor] = wc;
|
command_ps[cursor] = wc;
|
||||||
cursor_mb = strlen(matchBuf);
|
cursor_mb = strlen(match_buf);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
find_type = build_match_prefix(matchBuf);
|
find_type = build_match_prefix(match_buf);
|
||||||
|
|
||||||
/* Free up any memory already allocated */
|
/* Free up any memory already allocated */
|
||||||
free_tab_completion_data();
|
free_tab_completion_data();
|
||||||
@ -1088,12 +1090,12 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
/* If the word starts with `~' and there is no slash in the word,
|
/* If the word starts with `~' and there is no slash in the word,
|
||||||
* then try completing this word as a username. */
|
* then try completing this word as a username. */
|
||||||
if (state->flags & USERNAME_COMPLETION)
|
if (state->flags & USERNAME_COMPLETION)
|
||||||
if (matchBuf[0] == '~' && strchr(matchBuf, '/') == NULL)
|
if (match_buf[0] == '~' && strchr(match_buf, '/') == NULL)
|
||||||
match_pfx_len = complete_username(matchBuf);
|
match_pfx_len = complete_username(match_buf);
|
||||||
#endif
|
#endif
|
||||||
/* Try to match a command in $PATH, or a directory, or a file */
|
/* Try to match a command in $PATH, or a directory, or a file */
|
||||||
if (!matches)
|
if (!matches)
|
||||||
match_pfx_len = complete_cmd_dir_file(matchBuf, find_type);
|
match_pfx_len = complete_cmd_dir_file(match_buf, find_type);
|
||||||
/* Remove duplicates */
|
/* Remove duplicates */
|
||||||
if (matches) {
|
if (matches) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
@ -1117,7 +1119,7 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
char *cp;
|
char *cp;
|
||||||
beep();
|
beep();
|
||||||
if (!matches)
|
if (!matches)
|
||||||
return; /* no matches at all */
|
goto ret; /* no matches at all */
|
||||||
/* Find common prefix */
|
/* Find common prefix */
|
||||||
chosen_match = xstrdup(matches[0]);
|
chosen_match = xstrdup(matches[0]);
|
||||||
for (cp = chosen_match; *cp; cp++) {
|
for (cp = chosen_match; *cp; cp++) {
|
||||||
@ -1130,8 +1132,7 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
}
|
}
|
||||||
stop:
|
stop:
|
||||||
if (cp == chosen_match) { /* have unique prefix? */
|
if (cp == chosen_match) { /* have unique prefix? */
|
||||||
free(chosen_match); /* no */
|
goto ret; /* no */
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
*cp = '\0';
|
*cp = '\0';
|
||||||
cp = add_quote_for_spec_chars(chosen_match);
|
cp = add_quote_for_spec_chars(chosen_match);
|
||||||
@ -1158,9 +1159,9 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
|
if ((int)(len_found - match_pfx_len + command_len) < S.maxsize) {
|
||||||
int pos;
|
int pos;
|
||||||
/* save tail */
|
/* save tail */
|
||||||
strcpy(matchBuf, &command_ps[cursor]);
|
strcpy(match_buf, &command_ps[cursor]);
|
||||||
/* add match and tail */
|
/* add match and tail */
|
||||||
sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, matchBuf);
|
sprintf(&command_ps[cursor], "%s%s", chosen_match + match_pfx_len, match_buf);
|
||||||
command_len = strlen(command_ps);
|
command_len = strlen(command_ps);
|
||||||
/* new pos */
|
/* new pos */
|
||||||
pos = cursor + len_found - match_pfx_len;
|
pos = cursor + len_found - match_pfx_len;
|
||||||
@ -1169,19 +1170,20 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
char command[MAX_LINELEN];
|
/* Use 2nd half of match_buf as scratch space - see (**) */
|
||||||
int len = save_string(command, sizeof(command));
|
char *command = match_buf + MAX_LINELEN;
|
||||||
|
int len = save_string(command, MAX_LINELEN);
|
||||||
/* Have space to place the match? */
|
/* Have space to place the match? */
|
||||||
/* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
|
/* cursor_mb + (len_found - match_pfx_len) + (len - cursor_mb) */
|
||||||
if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
|
if ((int)(len_found - match_pfx_len + len) < MAX_LINELEN) {
|
||||||
int pos;
|
int pos;
|
||||||
/* save tail */
|
/* save tail */
|
||||||
strcpy(matchBuf, &command[cursor_mb]);
|
strcpy(match_buf, &command[cursor_mb]);
|
||||||
/* where do we want to have cursor after all? */
|
/* where do we want to have cursor after all? */
|
||||||
strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
|
strcpy(&command[cursor_mb], chosen_match + match_pfx_len);
|
||||||
len = load_string(command, S.maxsize);
|
len = load_string(command, S.maxsize);
|
||||||
/* add match and tail */
|
/* add match and tail */
|
||||||
sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, matchBuf);
|
sprintf(&command[cursor_mb], "%s%s", chosen_match + match_pfx_len, match_buf);
|
||||||
command_len = load_string(command, S.maxsize);
|
command_len = load_string(command, S.maxsize);
|
||||||
/* write out the matched command */
|
/* write out the matched command */
|
||||||
/* paranoia: load_string can return 0 on conv error,
|
/* paranoia: load_string can return 0 on conv error,
|
||||||
@ -1191,8 +1193,9 @@ static NOINLINE void input_tab(smallint *lastWasTab)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
ret:
|
||||||
free(chosen_match);
|
free(chosen_match);
|
||||||
#undef matchBuf
|
free(match_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* FEATURE_TAB_COMPLETION */
|
#endif /* FEATURE_TAB_COMPLETION */
|
||||||
|
Loading…
Reference in New Issue
Block a user