package icum.consola;

//import com.sun.corba.se.spi.orbutil.fsm.Input;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 *
 * @author miguel
 */
public class clsCmdExec implements Consola{
    private StringBuffer lineas=null;

    private int exitVal;
    
       
    public clsCmdExec(){
        lineas=new StringBuffer("Ninguna instruccion ejecutada todavía");
    }
        
    public clsCmdExec(String comando){
        this.ejecutar(comando);
    }
        
    public String ejecutar(String comando){
        Process p=null;
        BufferedReader input=null;
        BufferedReader error=null;
        
        lineas=new StringBuffer();
              
        try {
            String linea;
            p = Runtime.getRuntime().exec("cmd.exe /c"+comando);
            input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            error = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            
            int i=0;
            while ((linea = input.readLine()) != null) {
                lineas.append(linea);
                lineas.append("\n");
                i++;
            }
            input.close();
            
            while ((linea = error.readLine()) != null) {
                System.out.println(linea);
            }
            error.close();
            
            exitVal = p.waitFor();
        p.destroy();
        input.close();
        error.close();
        }catch (Exception err) {
            err.printStackTrace();
        }
        return lineas.toString();
    }
        
    public String ultimaSalida(){
        return lineas.toString();
    }
    
    public int ultimoValorSalida(){
          
    	return exitVal;
    }

    public void finalize() throws Throwable{
    	super.finalize();
    	
    }
    
}
