package commands; import java.io.*; import java.util.*; import java_cup.runtime.ComplexSymbolFactory; import java_cup.runtime.SymbolFactory; import kernel.*; import gui.*; /** * Starts a new shell. * * @author Jan Horký * @version 0.0 * @since 10.11.2008 */ public class shell extends AbstractProcess { private ShellObservable observable; private TextConsoleWindow console; private Pipe stdOutPipe; private Pipe stdInPipe; /** * Realizes the SHELL command. * * @throws AppException * Exception caused by an error in the virtual machines manager. */ public void realization() throws AppException { try { this.stdOutPipe = new Pipe(); this.stdInPipe = new Pipe(); this.console = new TextConsoleWindow(this); observable = new ShellObservable(this.console); this.setErrOut(this.initErrDeamon()); } catch (Exception e) { e.printStackTrace(); throw new AppException("Unexpected error\n" + e.getMessage(), e); } } /** * Kills the shell. */ public void kill() { observable.change(); observable.notifyObservers(Constants.EXIT_SHELL); super.kill(); } public void enableStdIn() { observable.change(); observable.notifyObservers(Constants.ENABLE_STDIN); } public void disableStdIn() { observable.change(); observable.notifyObservers(Constants.DISABLE_STDIN); } public void notifyDirChange() { observable.change(); observable.notifyObservers(Constants.DIR_CHANGE); } public void execCommand(String command) throws Exception { SymbolFactory sf = new ComplexSymbolFactory(); Object parser = null; parser = new Parser(new LexicalAnalyzer(new StringReader(command), sf), sf) .parse().value; CommandList cl = new CommandList(); cl = (CommandList) parser; this.getKernel().initProcesses(this.getID(), cl); } public synchronized Pipe getStdOutPipe() { return this.stdOutPipe; } public synchronized Pipe getStdInPipe() { return this.stdInPipe; } public synchronized void procesEnd() { try { this.stdInPipe = new Pipe(); this.stdOutPipe = new Pipe(); this.console.startStdOutDeamon(); console.writePrompt(); } catch (IOException e) { e.printStackTrace(); } } public synchronized OutputStream initErrDeamon() throws IOException { Pipe errPipe = new Pipe(); ErrOutCustomer e = new ErrOutCustomer(this.console, errPipe.getPipeIn()); while (!e.isAlive()) ; return errPipe.getPipeOut(); } /** * Provides help for users. * */ public String getHelp() { return "\n\nDescription:\nStarts a new shell.\n\n"; } private class ShellObservable extends Observable { private ShellObservable(Observer observer) { this.addObserver(observer); } private void change() { this.setChanged(); } } }