Commit 6a72dee00201809c8c6584b2d0744dc5bbf62b1d

  • avatar
  • crawley <crawley @407389f7-7c16-0410…ad85acb55d7f.>
  • Fri Aug 21 15:24:37 CEST 2009
Arithmetic Expression evaluation using $((...)) now works ... modulo
that the POSIX spec is so unclear that it is difficult to figure
out exactly what is correct in edge-cases.


git-svn-id: http://jnode.svn.sourceforge.net/svnroot/jnode/trunk@5658 407389f7-7c16-0410-9eee-ad85acb55d7f
  
10291029 }
10301030
10311031 private CharSequence dollarParenParenExpand(CharIterator ci) throws ShellException {
1032 // Different shells handle $(( ... )) differently, but dash seems to do what
1033 // the POSIX spec seems to say. In the first phase, we look for the matching '))'
1034 // keeping track of nested parentheses and performing any $ expansions. Double
1035 // quotes should be treated as literal.
10361032 StringBuilder sb = new StringBuilder();
10371033 int nesting = 0;
10381034 boolean done = false;
10501050 sb.append(')');
10511051 }
10521052 break;
1053 case '$':
1054 sb.append(dollarExpand(ci, '\000'));
1055 break;
10561053 case -1:
10571054 throw new ShellSyntaxException("Unmatched \"((\" (double left parenthesis)");
10581055 default:
10591056 sb.append((char) ch);
10601057 }
1058 ci.nextCh();
10611059 } while (!done);
1062 return new BjorneArithmeticEvaluator(this).evaluateExpression(sb);
1060 CharSequence tmp = dollarBacktickExpand(new CharIterator(sb), '
1061 return new BjorneArithmeticEvaluator(this).evaluateExpression(tmp);
10631062 }
10641063
10651064 int execute(CommandLine command, CommandIO[] streams, boolean isBuiltin) throws ShellException {
  
98981 1
9999</output>
100100 </testSpec>
101 <testSpec title="$((...))" command="test" runMode="AS_SCRIPT" rc="0">
102 <script>#!bjorne
103A=1
104echo $A
105B="$((A + 1))"
106echo $B
107C="$(($B + 1))"
108echo $C
109</script>
110 <output>1
1112
1123
113</output>
114 </testSpec>
101115 <testSpec title="if ... then ... fi" command="test" runMode="AS_SCRIPT" rc="0">
102116 <script>#!bjorne
103117if true ; then echo HI ; fi