package commands; import java.io.*; import kernel.*; /** * Prints help for specified command. * * @author Tomáš Podskalský */ public class man extends AbstractProcess { private static String[] commands = { "cat", "date", "echo", "login", "man", "ps", "pwd", "shell", "exit", "ls", "cd", "grep", "kill", "shutdown" }; public void realization() throws AppException { PipedOutputStream output = (this.getOut() != null) ? (PipedOutputStream) this .getOut() : null; ClassLoader loader = this.getClass().getClassLoader(); AbstractProcess process = null; try { if (getArgs().size() == 0) { output.write(this.getHelp().getBytes()); output.flush(); } else { for (String arg : getArgs()) { try { process = (AbstractProcess) loader.loadClass("commands." + arg) .newInstance(); } catch (Exception e) { process = null; } String s = new String("Příkaz:\n" + arg + "\n"); s = s + (process != null ? process.getHelp() : "\nPříkaz nenalezen."); s = s + "\n"; s = s + "------------------------------------------\n"; output.write(s.getBytes()); output.flush(); } } } catch (IOException ioe) { ioe.printStackTrace(); throw new AppException("Chyba I/O\n" + ioe.getMessage(), ioe); } catch (NullPointerException npe) { npe.printStackTrace(); throw new AppException("Output není definován\n" + npe.getMessage(), npe); } catch (Exception e) { e.printStackTrace(); throw new AppException("Neznámá Chyba.\n" + e.getMessage(), e); } } /** * Provides help for users. * */ public String getHelp() { return "\n\nDescription:\nPrints help for specified command.\n\nUsage:\n" + "man [command1 [command2 [...]]]\n\n" + "příkaz1, příkaz2, ...\tCommands that will be described. " + "If no agrument is given prints help for all available commands"; } }