Fix for parse_util_token_extent doing the wrong thing inside a command

substitution. Fixes https://github.com/fish-shell/fish-shell/issues/833
This commit is contained in:
ridiculousfish 2013-09-21 16:38:57 -07:00
parent 44bd405ed3
commit 97ea61a407
2 changed files with 18 additions and 19 deletions

View file

@ -411,35 +411,34 @@ void parse_util_token_extent(const wchar_t *buff,
const wchar_t **prev_begin, const wchar_t **prev_begin,
const wchar_t **prev_end) const wchar_t **prev_end)
{ {
const wchar_t *begin, *end;
long pos;
const wchar_t *a = NULL, *b = NULL, *pa = NULL, *pb = NULL; const wchar_t *a = NULL, *b = NULL, *pa = NULL, *pb = NULL;
CHECK(buff,); CHECK(buff,);
assert(cursor_pos >= 0); assert(cursor_pos >= 0);
parse_util_cmdsubst_extent(buff, cursor_pos, &begin, &end); const wchar_t *cmdsubst_begin, *cmdsubst_end;
parse_util_cmdsubst_extent(buff, cursor_pos, &cmdsubst_begin, &cmdsubst_end);
if (!end || !begin) if (!cmdsubst_end || !cmdsubst_begin)
{ {
return; return;
} }
pos = cursor_pos - (begin - buff); /* pos is equivalent to cursor_pos within the range of the command substitution {begin, end} */
long offset_within_cmdsubst = cursor_pos - (cmdsubst_begin - buff);
a = buff + pos; a = cmdsubst_begin + offset_within_cmdsubst;
b = a; b = a;
pa = buff + pos; pa = cmdsubst_begin + offset_within_cmdsubst;
pb = pa; pb = pa;
assert(begin >= buff); assert(cmdsubst_begin >= buff);
assert(begin <= (buff+wcslen(buff))); assert(cmdsubst_begin <= (buff+wcslen(buff)));
assert(end >= begin); assert(cmdsubst_end >= cmdsubst_begin);
assert(end <= (buff+wcslen(buff))); assert(cmdsubst_end <= (buff+wcslen(buff)));
const wcstring buffcpy = wcstring(begin, end-begin); const wcstring buffcpy = wcstring(cmdsubst_begin, cmdsubst_end-cmdsubst_begin);
tokenizer_t tok(buffcpy.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS); tokenizer_t tok(buffcpy.c_str(), TOK_ACCEPT_UNFINISHED | TOK_SQUASH_ERRORS);
for (; tok_has_next(&tok); tok_next(&tok)) for (; tok_has_next(&tok); tok_next(&tok))
@ -460,9 +459,9 @@ void parse_util_token_extent(const wchar_t *buff,
cursor is between two tokens, so we set it to a zero element cursor is between two tokens, so we set it to a zero element
string and break string and break
*/ */
if (tok_begin > pos) if (tok_begin > offset_within_cmdsubst)
{ {
a = b = (wchar_t *)buff + pos; a = b = cmdsubst_begin + offset_within_cmdsubst;
break; break;
} }
@ -470,9 +469,9 @@ void parse_util_token_extent(const wchar_t *buff,
If cursor is inside the token, this is the token we are If cursor is inside the token, this is the token we are
looking for. If so, set a and b and break looking for. If so, set a and b and break
*/ */
if ((tok_last_type(&tok) == TOK_STRING) && (tok_end >= pos)) if ((tok_last_type(&tok) == TOK_STRING) && (tok_end >= offset_within_cmdsubst))
{ {
a = begin + tok_get_pos(&tok); a = cmdsubst_begin + tok_get_pos(&tok);
b = a + wcslen(tok_last(&tok)); b = a + wcslen(tok_last(&tok));
break; break;
} }
@ -482,7 +481,7 @@ void parse_util_token_extent(const wchar_t *buff,
*/ */
if (tok_last_type(&tok) == TOK_STRING) if (tok_last_type(&tok) == TOK_STRING)
{ {
pa = begin + tok_get_pos(&tok); pa = cmdsubst_begin + tok_get_pos(&tok);
pb = pa + wcslen(tok_last(&tok)); pb = pa + wcslen(tok_last(&tok));
} }
} }

View file

@ -2170,7 +2170,7 @@ static void reader_replace_current_token(const wchar_t *new_token)
/* Find current token */ /* Find current token */
const wchar_t *buff = data->command_line.c_str(); const wchar_t *buff = data->command_line.c_str();
parse_util_token_extent((wchar_t *)buff, data->buff_pos, &begin, &end, 0, 0); parse_util_token_extent(buff, data->buff_pos, &begin, &end, 0, 0);
if (!begin || !end) if (!begin || !end)
return; return;