Commit 099ff5d911ea1b0c1453cb9f3db968e05f568202

  • avatar
  • crawley <crawley @407389f7-7c16-0410…ad85acb55d7f.>
  • Mon Aug 24 16:11:14 CEST 2009
Shell functions now support arguments and redirection


git-svn-id: http://jnode.svn.sourceforge.net/svnroot/jnode/trunk@5661 407389f7-7c16-0410-9eee-ad85acb55d7f
  
11831183 }
11841184
11851185 /**
1186 * Evaluate the redirections for this command against a set of streams, saving the context's existing IOs.
1187 *
1188 * @param redirects the redirection nodes to be evaluated
1189 * @param streams the base CommandIOs for the redirection calculation.
1190 * @throws ShellException
1191 */
1192 void evaluateRedirectionsAndPushHolders(RedirectionNode[] redirects, CommandIO[] streams)
1193 throws ShellException {
1194 if (savedHolders == null) {
1195 savedHolders = new ArrayList<CommandIOHolder[]>(1);
1196 }
1197 savedHolders.add(holders);
1198 holders = new CommandIOHolder[streams.length];
1199 for (int i = 0; i < holders.length; i++) {
1200 // Don't take ownership of the streams.
1201 holders[i] = new CommandIOHolder(streams[i], false);
1202 }
1203 evaluateRedirections(redirects, holders);
1204 }
1205
1206 /**
11861207 * Close the context's current IO, restoring the previous ones.
11871208 * @throws ShellException
11881209 */
12201220 * @return the stream state after redirections
12211221 * @throws ShellException
12221222 */
1223 void evaluateRedirections(
1224 RedirectionNode[] redirects, CommandIOHolder[] holders) throws ShellException {
1223 void evaluateRedirections(RedirectionNode[] redirects, CommandIOHolder[] holders)
1224 throws ShellException {
12251225 if (redirects == null) {
12261226 return;
12271227 }
  
322322 } else {
323323 CommandNode body = context.getFunction(commandName);
324324 if (body != null) {
325 // FIXME setup a new context, streams and args.
326 return body.execute(context);
325 context.evaluateRedirectionsAndPushHolders(body.getRedirects(), streams);
326 String[] savedArgs = context.getArgs();
327 try {
328 context.setArgs(cmdLine.getArguments());
329 return body.execute(context);
330 } finally {
331 context.popHolders();
332 context.setArgs(savedArgs);
333 }
327334 }
328335 }
329336 cmdLine.setStreams(streams);
330337 return shell.invoke(cmdLine, sysProps, env);
331338 }
332339
333 public BjorneContext createContext() throws ShellFailureException {
340 BjorneContext createContext() throws ShellFailureException {
334341 return new BjorneContext(this);
335342 }
336343
337 public CommandShell getShell() {
344 CommandShell getShell() {
338345 return shell;
339346 }
340347
341 public PrintStream resolvePrintStream(CommandIO commandIOIF) {
348 PrintStream resolvePrintStream(CommandIO commandIOIF) {
342349 return shell.resolvePrintStream(commandIOIF);
343350 }
344351
345 public InputStream resolveInputStream(CommandIO stream) {
352 InputStream resolveInputStream(CommandIO stream) {
346353 return shell.resolveInputStream(stream);
347354 }
348355
357357 return subshellCount++;
358358 }
359359
360 public String getUniqueName() {
360 String getUniqueName() {
361361 return getName() + "-" + getSubshellNumber();
362362 }
363363}
  
976976 }
977977 foo
978978 </script>
979 <output >hi
979 <output>hi
980980</output>
981 </testSpec>
982 <testSpec title="shell function 2" command="test" runMode="AS_SCRIPT" rc="0">
983 <script>#!bjorne
984 foo() {
985 echo $1 $2
986 }
987 foo hi there
988 foo bye mum &gt;&amp;2
989 </script>
990 <output>hi there
991</output>
992 <error>bye mum
993</error>
981994 </testSpec>
982995</testSet>