Commit 6a72dee00201809c8c6584b2d0744dc5bbf62b1d
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
| |   |
| 1029 | 1029 | } |
| 1030 | 1030 | |
| 1031 | 1031 | private CharSequence dollarParenParenExpand(CharIterator ci) throws ShellException { |
| // Different shells handle $(( ... )) differently, but dash seems to do what |
| // the POSIX spec seems to say. In the first phase, we look for the matching '))' |
| // keeping track of nested parentheses and performing any $ expansions. Double |
| // quotes should be treated as literal. |
| 1036 | 1032 | StringBuilder sb = new StringBuilder(); |
| 1037 | 1033 | int nesting = 0; |
| 1038 | 1034 | boolean done = false; |
| … | … | |
| 1050 | 1050 | sb.append(')'); |
| 1051 | 1051 | } |
| 1052 | 1052 | break; |
| case '$': |
| sb.append(dollarExpand(ci, '\000')); |
| break; |
| 1056 | 1053 | case -1: |
| 1057 | 1054 | throw new ShellSyntaxException("Unmatched \"((\" (double left parenthesis)"); |
| 1058 | 1055 | default: |
| 1059 | 1056 | sb.append((char) ch); |
| 1060 | 1057 | } |
| ci.nextCh(); |
| 1061 | 1059 | } while (!done); |
| return new BjorneArithmeticEvaluator(this).evaluateExpression(sb); |
| CharSequence tmp = dollarBacktickExpand(new CharIterator(sb), ' |
| return new BjorneArithmeticEvaluator(this).evaluateExpression(tmp); |
| 1063 | 1062 | } |
| 1064 | 1063 | |
| 1065 | 1064 | int execute(CommandLine command, CommandIO[] streams, boolean isBuiltin) throws ShellException { |
| |   |
| 98 | 98 | 1 1 |
| 99 | 99 | </output> |
| 100 | 100 | </testSpec> |
| <testSpec title="$((...))" command="test" runMode="AS_SCRIPT" rc="0"> |
| <script>#!bjorne |
| A=1 |
| echo $A |
| B="$((A + 1))" |
| echo $B |
| C="$(($B + 1))" |
| echo $C |
| </script> |
| <output>1 |
| 2 |
| 3 |
| </output> |
| </testSpec> |
| 101 | 115 | <testSpec title="if ... then ... fi" command="test" runMode="AS_SCRIPT" rc="0"> |
| 102 | 116 | <script>#!bjorne |
| 103 | 117 | if true ; then echo HI ; fi |