mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-08 01:12:59 +08:00
Fixed recursive brace expansion
https://github.com/fish-shell/fish-shell/issues/399
This commit is contained in:
parent
87510ac77d
commit
e9d216bc84
43
expand.cpp
43
expand.cpp
|
@ -1201,28 +1201,26 @@ static int expand_variables_internal(parser_t &parser, wchar_t * const in, std::
|
||||||
*/
|
*/
|
||||||
static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, std::vector<completion_t> &out)
|
static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, std::vector<completion_t> &out)
|
||||||
{
|
{
|
||||||
const wchar_t *pos;
|
|
||||||
bool syntax_error = false;
|
bool syntax_error = false;
|
||||||
int bracket_count=0;
|
int bracket_count=0;
|
||||||
|
|
||||||
const wchar_t *bracket_begin=0, *bracket_end=0;
|
const wchar_t *bracket_begin = NULL, *bracket_end = NULL;
|
||||||
const wchar_t *last_sep=0;
|
const wchar_t *last_sep = NULL;
|
||||||
|
|
||||||
const wchar_t *item_begin;
|
const wchar_t *item_begin;
|
||||||
size_t len1, len2, tot_len;
|
size_t length_preceding_brackets, length_following_brackets, tot_len;
|
||||||
|
|
||||||
const wchar_t * const in = instr.c_str();
|
const wchar_t * const in = instr.c_str();
|
||||||
|
|
||||||
for (pos=in;
|
/* Locate the first non-nested bracket pair */
|
||||||
(*pos) && !syntax_error;
|
for (const wchar_t *pos = in; (*pos) && !syntax_error; pos++)
|
||||||
pos++)
|
|
||||||
{
|
{
|
||||||
switch (*pos)
|
switch (*pos)
|
||||||
{
|
{
|
||||||
case BRACKET_BEGIN:
|
case BRACKET_BEGIN:
|
||||||
{
|
{
|
||||||
bracket_begin = pos;
|
if (bracket_count == 0)
|
||||||
|
bracket_begin = pos;
|
||||||
bracket_count++;
|
bracket_count++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -1230,16 +1228,16 @@ static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, s
|
||||||
case BRACKET_END:
|
case BRACKET_END:
|
||||||
{
|
{
|
||||||
bracket_count--;
|
bracket_count--;
|
||||||
if (bracket_end < bracket_begin)
|
|
||||||
{
|
|
||||||
bracket_end = pos;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bracket_count < 0)
|
if (bracket_count < 0)
|
||||||
{
|
{
|
||||||
syntax_error = true;
|
syntax_error = true;
|
||||||
}
|
}
|
||||||
break;
|
else if (bracket_count == 0)
|
||||||
|
{
|
||||||
|
bracket_end = pos;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
case BRACKET_SEP:
|
case BRACKET_SEP:
|
||||||
{
|
{
|
||||||
|
@ -1257,6 +1255,7 @@ static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, s
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/* The user hasn't typed an end bracket yet; make one up and append it, then expand that. */
|
||||||
wcstring mod;
|
wcstring mod;
|
||||||
if (last_sep)
|
if (last_sep)
|
||||||
{
|
{
|
||||||
|
@ -1282,17 +1281,17 @@ static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, s
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bracket_begin == 0)
|
if (bracket_begin == NULL)
|
||||||
{
|
{
|
||||||
append_completion(out, in);
|
append_completion(out, instr);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
len1 = (bracket_begin-in);
|
length_preceding_brackets = (bracket_begin-in);
|
||||||
len2 = wcslen(bracket_end)-1;
|
length_following_brackets = wcslen(bracket_end)-1;
|
||||||
tot_len = len1+len2;
|
tot_len = length_preceding_brackets+length_following_brackets;
|
||||||
item_begin = bracket_begin+1;
|
item_begin = bracket_begin+1;
|
||||||
for (pos=(bracket_begin+1); 1; pos++)
|
for (const wchar_t *pos =(bracket_begin+1); true; pos++)
|
||||||
{
|
{
|
||||||
if (bracket_count == 0)
|
if (bracket_count == 0)
|
||||||
{
|
{
|
||||||
|
@ -1303,7 +1302,7 @@ static int expand_brackets(parser_t &parser, const wcstring &instr, int flags, s
|
||||||
|
|
||||||
wcstring whole_item;
|
wcstring whole_item;
|
||||||
whole_item.reserve(tot_len + item_len + 2);
|
whole_item.reserve(tot_len + item_len + 2);
|
||||||
whole_item.append(in, len1);
|
whole_item.append(in, length_preceding_brackets);
|
||||||
whole_item.append(item_begin, item_len);
|
whole_item.append(item_begin, item_len);
|
||||||
whole_item.append(bracket_end + 1);
|
whole_item.append(bracket_end + 1);
|
||||||
expand_brackets(parser, whole_item, flags, out);
|
expand_brackets(parser, whole_item, flags, out);
|
||||||
|
|
|
@ -10,6 +10,11 @@ for i in 1 2 #Comment on same line as command
|
||||||
end;
|
end;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Bracket expansion
|
||||||
|
echo x-{1}
|
||||||
|
echo x-{1,2}
|
||||||
|
echo foo-{1,2{3,4}}
|
||||||
|
|
||||||
# Simple alias tests
|
# Simple alias tests
|
||||||
|
|
||||||
function foo
|
function foo
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
1b
|
1b
|
||||||
2a
|
2a
|
||||||
2b
|
2b
|
||||||
|
x-1
|
||||||
|
x-1 x-2
|
||||||
|
foo-1 foo-23 foo-24
|
||||||
Test 2 pass
|
Test 2 pass
|
||||||
Test pass
|
Test pass
|
||||||
Test 3 pass
|
Test 3 pass
|
||||||
|
|
Loading…
Reference in New Issue
Block a user