hush: fix more obscure ${var%...} cases

function                                             old     new   delta
add_till_closing_paren                               313     359     +46
builtin_exit                                          48      47      -1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko
2010-05-22 00:26:06 +02:00
parent 7436950a75
commit a6ad397ea9
5 changed files with 45 additions and 24 deletions

View File

@ -45,6 +45,8 @@
* follow IFS rules more precisely, including update semantics
* builtins mandated by standards we don't support:
* [un]alias, command, fc, getopts, newgrp, readonly, times
* make complex ${var%...} constructs support optional
* make here documents optional
*
* Bash compat TODO:
* redirection of stdout+stderr: &> and >&
@ -5887,36 +5889,36 @@ static void add_till_backquote(o_string *dest, struct in_str *input)
* echo $(echo 'TEST)' BEST) TEST) BEST
* echo $(echo \(\(TEST\) BEST) ((TEST) BEST
*
* BUG: enter: echo $(( `printf '(\x28 1'` + `echo 2))` ))
* on the command line, press Enter. You get > prompt which is impossible
* to exit with ^C.
* Also adapted to eat ${var%...} constructs, since ... part
* can contain arbitrary constructs, just like $(cmd).
*/
#define DOUBLE_CLOSE_CHAR_FLAG 0x80
static void add_till_closing_paren(o_string *dest, struct in_str *input, char end_ch)
{
int count = 0;
char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG;
end_ch &= (DOUBLE_CLOSE_CHAR_FLAG-1);
while (1) {
int ch = i_getch(input);
if (ch == EOF) {
syntax_error_unterm_ch(')');
syntax_error_unterm_ch(end_ch);
/*xfunc_die(); - redundant */
}
if (ch == '(' || ch == '{')
count++;
if (ch == ')' || ch == '}') {
count--;
if (count < 0 && ch == end_ch) {
if (!dbl)
break;
if (i_peek(input) == ')') {
i_getch(input);
break;
}
if (ch == end_ch) {
if (!dbl)
break;
/* we look for closing )) of $((EXPR)) */
if (i_peek(input) == end_ch) {
i_getch(input); /* eat second ')' */
break;
}
}
o_addchr(dest, ch);
if (ch == '(' || ch == '{') {
ch = (ch == '(' ? ')' : '}');
add_till_closing_paren(dest, input, ch);
o_addchr(dest, ch);
continue;
}
if (ch == '\'') {
add_till_single_quote(dest, input);
o_addchr(dest, ch);
@ -5927,6 +5929,11 @@ static void add_till_closing_paren(o_string *dest, struct in_str *input, char en
o_addchr(dest, ch);
continue;
}
if (ch == '`') {
add_till_backquote(dest, input);
o_addchr(dest, ch);
continue;
}
if (ch == '\\') {
/* \x. Copy verbatim. Important for \(, \) */
ch = i_getch(input);