package nxm.sys.intr;

import nxm.sys.lib.Foreign;
import nxm.sys.lib.Intrinsic;

/**
   sends an operating system command to the OS

  @author Jeff Schoen
  @author Neon Ngo 2011-07-21 Added support to capture standard output and error to results
                              via /STDOUT=&lt;out_resname&gt; and /STDERR=&lt;err_resname&gt;, but they
                              must come right after this command (otherwise they are picked up
                              and used to run as part of OS command line).
*/
public class Os extends Intrinsic {

  /** default constructor */
  public Os () {}

  public int process () {
    String cmdline = MA.getRawArgs();
    String resname_stdout = MA.getU("/STDOUT", null); // since 3.0.0 for BUG 1357
    String resname_stderr = MA.getU("/STDERR", null); // since 3.0.0 for BUG 1357
    String resname_exit   = MA.getU("/EXITSTATUS", null); // since 3.5.0

    int exitStatus = Foreign.runInternal(M, cmdline, false, resname_stdout, resname_stderr);

    if (resname_exit != null) { // since 3.5.0
      M.results.put(resname_exit, exitStatus);
    }
    return FINISH;
  }

}
