///////////////////////////////////////////////////// // Interpretador de Comandos Java- ShellJava // // Professor: Ismael H F Santos ///////////////////////////////////////////////////// import java.io.*; import java.util.*; // Shell LinComandEnum for ShellJava /* import FileViewer; import FileCopy; import FileLister; import Grep; import matrixProdTh; import somaMatrixProdTh; import Pausa; import Espera; */ class ShellJava implements Runnable { // Variaveis da Classe private static Thread [] t; // Variaveis de Thread private Comand cmd; // Construtor para cada Thread ShellJava( Comand c ) { cmd = c; }; // Métodos Privados // Métodos Publicos public void run() { // Executa comandos em Background if ( cmd.classe.equalsIgnoreCase("FileViewer") ) { FileViewer.main( cmd.param ); } else if ( cmd.classe.equalsIgnoreCase("FileCopy") ) { FileCopy.main( cmd.param ); } else if ( cmd.classe.equalsIgnoreCase("FileLister") ) { FileLister.main( cmd.param ); } else if ( cmd.classe.equalsIgnoreCase("Grep") ) { Grep.main( cmd.param ); } else if ( cmd.classe.equalsIgnoreCase("matrixProdTh") ) { matrixProdTh.main( cmd.param ); } else if ( cmd.classe.equalsIgnoreCase("somaMatrixProdTh") ) { somaMatrixProdTh.main( cmd.param ); } } public static void main ( String args[] ) { DataInputStream in = new DataInputStream( System.in ); while ( true ) { System.out.println("% "); System.out.flush(); String lin; try { lin = in.readLine(); } catch ( IOException e ) { ; } LinComandEnum linCmd = new LinComandEnum(lin.trim()); while ( linCmd.hasMoreComands() ) { try { ComandWraper cmdW = linCmd.nextComand(); } catch ( NoSuchElementException e ) { } t = new Thread[cmdW.cmd.length()]; for ( int i=0; i < cmdW.cmd.length() ; ++i ) { t[i] = new Thread( new ShellJava(cmdW.cmd[i]) ); t[i].start(); } if ( ! cmdW.backGround ) for ( int i=0; i < cmdW.cmd.length(); ++i ) t[i].join(); } } } } // // Comandos e respectivos parametros // class ComandWraper { Comand [] cmd; // Vetor de Comandos boolean backGround; // Execucaoem Background ! ComandWraper( Comand [] cmd, boolean back ) { this.cmd = cmd; backGround = back; } } class Comand { String classe; String [] param; Comand( String cmdStr ) { int i = cmdStr.indexOf("&"); // limita String até o & if ( i != -1 ) cmdStr = cmdStr.substring(0, i); StringTokenizer t = new StringTokenizer(cmdStr," "); int nParams = t.countTokens() - 1; classe = (t.nextToken()).trim(); if ( nParams == 0 ) { param = null; } else { param = new String[nParams]; for ( int i=0; t.hasMoreTokens(); ++i ) { param[i] = t.nextToken(); } } } } // // Decompoe linha de comando em comandos a serem enumerados // (interface Enumeration) para serem executados pelo Shell // class LinComandEnum implements Enumeration { private String [] comandStr; private int lastComand = -1; LinComandEnum( String lin ) { StringTokenizer t = new StringTokenizer(lin,";"); int n = t.countTokens(); if ( n == 0 ) { comandStr = new String[1]; comandStr[0] = lin; } else { comandStr = new String[n]; for ( int i=0; t.hasMoreTokens(); ++i ) { comandStr[i] = t.nextToken(); } } } public boolean hasMoreComands() { return lastComand < comandStr.length(); } public ComandWraper nextComand() throws NoSuchElementException { if ( lastComand == comandStr.length() ) throw new NoSuchElementException(); // Coleciona comandos em background !!! int j = lastComand; while ( ++j < comandStr.length() ) { if ( comandStr[j].indexOf("&") == -1 ) break; } int nComands = j - 1 - lastComand; if ( nComands == 0 ) { Comand [] cmd = new Comand[1]; cmd[0] = new Comand(comandStr[j]); } else { Comand [] cmd = new Comand[nComands]; for ( int i=lastComand+1; i < j; ++i ) { cmd[i] = new Comand(comandStr[i]); } } lastComand = j; return new ComandWraper(cmd, (nComands==0?false:true) ); } public boolean hasMoreElements() { return hasMoreComands(); } public Object nextElement() throws NoSuchElementException { return (Object)nextComand(); } }