// CommandExec.java
// 
// By Jeff Schmitt
//    Towson University
//    February, 1998
//
// Execute a native command
// with access to its input and output streams
//

import java.io.*;

public class CommandExec {
   Process p;
   PrintWriter os;    // output to the unix command
   BufferedReader is; // input from the unix command

   // constructor
   CommandExec(String command) {
      try {
         // Start running command
         Process p  = Runtime.getRuntime().exec(command);

         // Attach to the input of the command
         os = new PrintWriter(
             new OutputStreamWriter(p.getOutputStream()));
         is = new BufferedReader(
             new InputStreamReader(p.getInputStream()));
      } catch (Exception e) {
      }
   }

   // close and return the exit value (abnormal result is != 0)
   public int close(boolean waitfor) {
      try {
         os.close();
         is.close();
         if (waitfor) {
            return(p.waitFor());
         } 
         return(p.exitValue());
      } catch (Exception e) {
      }
      return 0;
   }

   // print a line of input data to the command
   public void println(String line) {
      os.println(line);
   }

   // read a line of output data from the command
   public String readLine() {
      try {
         return (is.readLine());
      } catch (Exception e) {
      }
      return null;
   }
}
