Re: Parse error with consecutive case statements inside $()

2016-04-01 Thread Piotr Grzybowski
Hello!

 I firmly believe that the attached patch fixes the problem at hand; what
it breaks I have no idea. Kindly requesting a review.

cheers,
pg

P.S.
 it also fixes others of the similar type, e.g.:

x=$(case 1 in 1) echo 1;; esac; case 2 in 2) echo 2;; esac;)
echo $x

P.P.S.
 vim :syntax notices the original parsing error, probably assuming ')' in
case value closes command subsitution. That, however, is beyond my ability
to fix.


On Tue, Mar 29, 2016 at 7:43 PM, Christian Franke <
christian.fra...@t-online.de> wrote:

> Configuration Information [Automatically generated, do not change]:
> Machine: i686
> OS: cygwin
> Compiler: gcc
> Compilation CFLAGS:  -DPROGRAM='bash.exe' -DCONF_HOSTTYPE='i686'
> -DCONF_OSTYPE='cygwin' -DCONF_MACHTYPE='i686-pc-cygwin' -DCONF_VENDOR='pc'
> -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H
> -DRECYCLES_PIDS   -I. -I/usr/src/bash-4.3.42-4.i686/src/bash-4.3
> -I/usr/src/bash-4.3.42-4.i686/src/bash-4.3/include
> -I/usr/src/bash-4.3.42-4.i686/src/bash-4.3/lib  -DWORDEXP_OPTION -ggdb -O2
> -pipe -Wimplicit-function-declaration
> -fdebug-prefix-map=/usr/src/bash-4.3.42-4.i686/build=/usr/src/debug/bash-4.3.42-4
> -fdebug-prefix-map=/usr/src/bash-4.3.42-4.i686/src/bash-4.3=/usr/src/debug/bash-4.3.42-4
> uname output: CYGWIN_NT-6.1-WOW Alien2 2.4.1(0.293/5/3) 2016-01-24 11:24
> i686 Cygwin
> Machine Type: i686-pc-cygwin
>
> Bash Version: 4.3
> Patch Level: 42
> Release Status: release
>
> Description:
> If consecutive case statements are inside of $(...), the parser
> misinterprets first ')' from second case statement as the end of command
> substitution.
>
> The possibly related patch bash43-042 is already included in this version.
>
>
> Repeat-By:
>
> $ cat bug.sh
> x=$(
>   case 1 in
> 1) echo 1
>   esac
>   case 2 in
> 2) echo 2
>   esac
> )
> echo "$x"
>
>
> $ bash -xv bug.sh
> x=$(
>   case 1 in
> 1) echo 1
>   esac
>   case 2 in
> 2) echo 2
> bug.sh: command substitution: line 13: syntax error: unexpected end of file
> bug.sh: line 7: syntax error near unexpected token `esac'
> bug.sh: line 7: ` esac'
>
>
> $ dash bug.sh
> 1
> 2
>
>
> Workarounds:
> - append semicolon behind first 'esac', or
> - insert any command line between the case statements, or
> - use `...` instead of $(...)
>
>
> --
> Christian Franke
>
>
>


fix-case-inside-command-substitution.patch
Description: Binary data


Re: Parse error with consecutive case statements inside $()

2016-04-01 Thread Chet Ramey
On 4/1/16 4:21 AM, Piotr Grzybowski wrote:
> Hello!
> 
>  I firmly believe that the attached patch fixes the problem at hand; what
> it breaks I have no idea. Kindly requesting a review.

It breaks the test suite.

It tries to do a little bit too much: it should not add the character to
the return buffer and go on to the next one if later code needs to do
something with it (e.g., if it's a `)').

Here's a version of the patch that does the trick.

Chet

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRUc...@case.eduhttp://cnswww.cns.cwru.edu/~chet/
*** ../bash-4.4-rc1/parse.y	2016-01-15 08:52:07.0 -0500
--- parse.y	2016-04-01 08:03:53.0 -0400
***
*** 3877,3880 
--- 3880,3884 
  		{
  		  tflags |= LEX_INCASE;
+ 		  tflags &= ~LEX_RESWDOK;
  /*itrace("parse_comsub:%d: found `case', lex_incase -> 1 lex_reswdok -> 0", line_number);*/
  		}
***
*** 3882,3888 
  		{
  		  tflags &= ~LEX_INCASE;
! /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 0", line_number);*/
  		}
- 	  tflags &= ~LEX_RESWDOK;
  	}
  	  else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))
--- 3886,3898 
  		{
  		  tflags &= ~LEX_INCASE;
! /*itrace("parse_comsub:%d: found `esac', lex_incase -> 0 lex_reswdok -> 1", line_number);*/
! 		  tflags |= LEX_RESWDOK;
! 		  lex_rwlen = 0;
! 		}
! 	  else
! 		{
! 		  tflags &= ~LEX_RESWDOK;
! /*itrace("parse_comsub:%d: found `%.4s', lex_reswdok -> 0", line_number, ret+retind-4);*/
  		}
  	}
  	  else if MBTEST((tflags & LEX_CKCOMMENT) && ch == '#' && (lex_rwlen == 0 || ((tflags & LEX_INWORD) && lex_wlen == 0)))