}
} else {
try {
fd = Integer.parseInt(io.getText());
} catch (NumberFormatException ex) {
throw new ShellFailureException("Invalid &fd number");
}
}
// If necessary, grow the fd table.
if (fd >= holders.length) {
CommandIOHolder[] tmp = new CommandIOHolder[fd + 1];
System.arraycopy(holders, 0, tmp, 0, fd + 1);
holders = tmp;
}
CommandIOHolder stream;
CommandInput in;
CommandOutput out;
switch (redir.getRedirectionType()) {
case REDIR_DLESS:
case REDIR_DLESSDASH:
String here = redir.getHereDocument();
if (redir.isHereDocumentExpandable()) {
here = dollarBacktickExpand(here).toString();
}
in = new CommandInput(new StringReader(here));
stream = new CommandIOHolder(in, true);
break;
case REDIR_GREAT:
try {
File file = new File(redir.getArg().getText());
if (isNoClobber() && file.exists()) {
throw new ShellException("File already exists");
}
out = new CommandOutput(new FileOutputStream(file));
stream = new CommandIOHolder(out, true);
} catch (IOException ex) {
throw new ShellException("Cannot open output file", ex);
}
break;
case REDIR_CLOBBER:
case REDIR_DGREAT:
try {
FileOutputStream tmp = new FileOutputStream(redir.getArg().getText(),
redir.getRedirectionType() == REDIR_DGREAT);
stream = new CommandIOHolder(new CommandOutput(tmp), true);
} catch (IOException ex) {
throw new ShellException("Cannot open output file", ex);
}
break;
case REDIR_LESS:
try {
File file = new File(redir.getArg().getText());
in = new CommandInput(new FileInputStream(file));
stream = new CommandIOHolder(in, true);
} catch (IOException ex) {
throw new ShellException("Cannot open input file", ex);
}
break;
case REDIR_LESSAND:
try {
int fromFd = Integer.parseInt(redir.getArg().getText());
stream = (fromFd >= holders.length) ? null :
new CommandIOHolder(holders[fromFd]);
} catch (NumberFormatException ex) {
throw new ShellException("Invalid fd after <&");
}
break;
case REDIR_GREATAND:
try {
int fromFd = Integer.parseInt(redir.getArg().getText());
stream = (fromFd >= holders.length) ? null :
new CommandIOHolder(holders[fromFd]);
} catch (NumberFormatException ex) {
throw new ShellException("Invalid fd after >&");
}
break;
case REDIR_LESSGREAT:
throw new UnsupportedOperationException("<>");
default:
throw new ShellFailureException("unknown redirection type");
}
holders[fd] = stream;
}
ok = true;
} finally {