// CommandShellChild.java
//
// You may use and distribute this example. This program is 
// provided WITHOUT WARRANTY either expressed or implied.
//
// Copyright Henrik Björkman 1998
//
// Description: See usage below.
//
// History:
// 0.0 Created by Henrik Björkman 1998-06-09
//


import java.io.*;
import java.awt.*;



/**
 * 
 */
public class CommandShellChild extends Thread
{
  static void error(String str)
  {
    System.err.println("CommandShellChild: " + str);
    System.exit(0);
  }

  static void debug(String str)
  {
    System.err.println(str);
  }


  public CommandShellChild(String command, TextArea out)
  {
    //command="c:\\temp\\test.bat";
    debug("CommandShellChild "+command);

    try
    {
      debug("ok");

      Process p=Runtime.getRuntime().exec(command);

      debug("start readers");
      Thread i=new ReadToTextArea(p.getInputStream(),out);
      Thread e=new ReadToTextArea(p.getErrorStream(),out);

      debug("now wait for");
      p.waitFor();

      synchronized (this)
      {
        debug("now wait 1s");
        this.wait(1000);
      }

      debug("stop readers");
      i.stop();
      e.stop();

      debug("destroy");
      p.destroy();

    }
    catch(InterruptedException e)
    {
      error(""+e);
    }
    catch(IOException e) 
    {
      error("" + e);
    }

  }

}


