1 /** 
  2  * @fileoverview This file defines the implementation of our Sys namespace.
  3  */
  4 
  5 /**
  6  * @namespace Sys namespace used for things such as executing external commands.
  7  */
  8  
  9 var Sys = {
 10   
 11   /** 
 12    * This getter returns the current temp directory as returned by the XPCOM directory service.
 13    * @returns {String} The path to the temp directory
 14    */
 15   get tempDir() {
 16     return $Cc["@mozilla.org/file/directory_service;1"].getService($Ci.nsIProperties).get("TmpD", $Ci.nsIFile).path;
 17   },
 18   
 19   /** 
 20    * Runs the command given as the first parameter with the arguments given as the remaining
 21    * parameters and returns an object containing the exit status (as the exitStatus property)
 22    * and command output (as the output property).
 23    * @param {String[]} parameters First parameter is command name, rest are arguments to
 24    * invoke the command with.
 25    * @returns {Object} Object with 'exitStatus' and 'output' properties.
 26    */
 27   run: function() {
 28     var tempDir = Sys.tempDir;
 29     var popenHelper = File.join(XPCOMCoreConfig.getProperty('binRoot'), "popen_helper.sh");
 30     var file = new XPCBuiltins.nsILocalFile(popenHelper);
 31 
 32     var process = $Cc["@mozilla.org/process/util;1"].createInstance($Ci.nsIProcess);
 33     process.init(file);
 34     
 35     var args = Array.prototype.slice.call(arguments);
 36     var procArgs = [tempDir, "stdout"].concat(args);
 37     
 38     process.run(false, procArgs, procArgs.length);
 39     
 40     var outputPipe = File.join(tempDir, process.pid + ".stdout.pipe");
 41     var outputPipeFile = new XPCBuiltins.nsILocalFile(outputPipe);
 42     
 43     var output = null;
 44     while (!outputPipeFile.exists()) {
 45       null;
 46     };
 47     
 48     output = File.read(outputPipe);
 49     
 50     return({exitStatus: process.exitValue, output: output});
 51   }
 52 };
 53