Commit 099ff5d911ea1b0c1453cb9f3db968e05f568202
Shell functions now support arguments and redirection
git-svn-id: http://jnode.svn.sourceforge.net/svnroot/jnode/trunk@5661 407389f7-7c16-0410-9eee-ad85acb55d7f
| |   |
| 1183 | 1183 | } |
| 1184 | 1184 | |
| 1185 | 1185 | /** |
| * Evaluate the redirections for this command against a set of streams, saving the context's existing IOs. |
| * |
| * @param redirects the redirection nodes to be evaluated |
| * @param streams the base CommandIOs for the redirection calculation. |
| * @throws ShellException |
| */ |
| void evaluateRedirectionsAndPushHolders(RedirectionNode[] redirects, CommandIO[] streams) |
| throws ShellException { |
| if (savedHolders == null) { |
| savedHolders = new ArrayList<CommandIOHolder[]>(1); |
| } |
| savedHolders.add(holders); |
| holders = new CommandIOHolder[streams.length]; |
| for (int i = 0; i < holders.length; i++) { |
| // Don't take ownership of the streams. |
| holders[i] = new CommandIOHolder(streams[i], false); |
| } |
| evaluateRedirections(redirects, holders); |
| } |
| |
| /** |
| 1186 | 1207 | * Close the context's current IO, restoring the previous ones. |
| 1187 | 1208 | * @throws ShellException |
| 1188 | 1209 | */ |
| … | … | |
| 1220 | 1220 | * @return the stream state after redirections |
| 1221 | 1221 | * @throws ShellException |
| 1222 | 1222 | */ |
| void evaluateRedirections( |
| RedirectionNode[] redirects, CommandIOHolder[] holders) throws ShellException { |
| void evaluateRedirections(RedirectionNode[] redirects, CommandIOHolder[] holders) |
| throws ShellException { |
| 1225 | 1225 | if (redirects == null) { |
| 1226 | 1226 | return; |
| 1227 | 1227 | } |
| |   |
| 322 | 322 | } else { |
| 323 | 323 | CommandNode body = context.getFunction(commandName); |
| 324 | 324 | if (body != null) { |
| // FIXME setup a new context, streams and args. |
| return body.execute(context); |
| context.evaluateRedirectionsAndPushHolders(body.getRedirects(), streams); |
| String[] savedArgs = context.getArgs(); |
| try { |
| context.setArgs(cmdLine.getArguments()); |
| return body.execute(context); |
| } finally { |
| context.popHolders(); |
| context.setArgs(savedArgs); |
| } |
| 327 | 334 | } |
| 328 | 335 | } |
| 329 | 336 | cmdLine.setStreams(streams); |
| 330 | 337 | return shell.invoke(cmdLine, sysProps, env); |
| 331 | 338 | } |
| 332 | 339 | |
| public BjorneContext createContext() throws ShellFailureException { |
| BjorneContext createContext() throws ShellFailureException { |
| 334 | 341 | return new BjorneContext(this); |
| 335 | 342 | } |
| 336 | 343 | |
| public CommandShell getShell() { |
| CommandShell getShell() { |
| 338 | 345 | return shell; |
| 339 | 346 | } |
| 340 | 347 | |
| public PrintStream resolvePrintStream(CommandIO commandIOIF) { |
| PrintStream resolvePrintStream(CommandIO commandIOIF) { |
| 342 | 349 | return shell.resolvePrintStream(commandIOIF); |
| 343 | 350 | } |
| 344 | 351 | |
| public InputStream resolveInputStream(CommandIO stream) { |
| InputStream resolveInputStream(CommandIO stream) { |
| 346 | 353 | return shell.resolveInputStream(stream); |
| 347 | 354 | } |
| 348 | 355 | |
| … | … | |
| 357 | 357 | return subshellCount++; |
| 358 | 358 | } |
| 359 | 359 | |
| public String getUniqueName() { |
| String getUniqueName() { |
| 361 | 361 | return getName() + "-" + getSubshellNumber(); |
| 362 | 362 | } |
| 363 | 363 | } |
| |   |
| 976 | 976 | } |
| 977 | 977 | foo |
| 978 | 978 | </script> |
| <output >hi |
| <output>hi |
| 980 | 980 | </output> |
| </testSpec> |
| <testSpec title="shell function 2" command="test" runMode="AS_SCRIPT" rc="0"> |
| <script>#!bjorne |
| foo() { |
| echo $1 $2 |
| } |
| foo hi there |
| foo bye mum >&2 |
| </script> |
| <output>hi there |
| </output> |
| <error>bye mum |
| </error> |
| 981 | 994 | </testSpec> |
| 982 | 995 | </testSet> |