/**
  * Set runtime information.
  */
 static function init($port = 8089)
 {
     // Allow override of default port
     VCLIManager::$port = $port;
     // Provide session support
     if (session_id() === "") {
         session_id('virtualcli');
         session_start();
     }
     // Provide unique security key
     if (!isset($_SESSION['vcli_security_key'])) {
         $_SESSION['vcli_security_key'] = uniqid() . dechex(rand(0, 32000));
     }
     VCLIManager::$security_key = $_SESSION['vcli_security_key'];
     // Determine platform
     $uname = strtolower(php_uname());
     if (strpos($uname, "darwin") !== false) {
         VCLIManager::$platform = 'darwin';
         // OS X
     } else {
         if (strpos($uname, "win") !== false) {
             VCLIManager::$platform = 'win32';
             // Windows
         } else {
             if (strpos($uname, "linux") !== false) {
                 VCLIManager::$platform = 'linux';
                 // Linux
             } else {
                 VCLIManager::$platform = 'unsupported';
                 // Unsupported
             }
         }
     }
     // Ensure vcli native binary is in memory
     $process_id = false;
     $cmd = '"' . __DIR__ . "/Builds - vcli.xojo_xml_project/";
     if (VCLIManager::$platform === 'win32') {
         exec("tasklist.exe", $ps);
         foreach ($ps as $p) {
             if (false !== strpos($p, "vcli.exe")) {
                 $p = new GString($p);
                 $process_id = intval($p->delLeftMost("vcli.exe")->trim()->getLeftMost(" ")->__toString());
                 break;
             }
         }
         $cmd .= 'Windows\\vcli\\vcli.exe" --port ' . VCLIManager::$port . ' --security_key ' . VCLIManager::$security_key;
         $cmd = str_replace('/', '\\', $cmd);
         $cmd = 'start /b "vcli" ' . $cmd;
     } else {
         $process_id = exec("ps -ax | awk '/[v]cli\\/vcli/{print \$1}'") | false;
         if (!$process_id) {
             $process_id = exec("ps -ax | awk '/[v]cli.debug\\/vcli.debug/{print \$1}'") | false;
         }
         if (VCLIManager::$platform === 'darwin') {
             $cmd .= 'Mac OS X (Intel)/vcli/vcli" --port ' . VCLIManager::$port . ' --security_key ';
             $cmd .= VCLIManager::$security_key . ' > /dev/null 2>&1 &';
         } else {
             $cmd .= 'Linux/vcli/vcli" --port ' . VCLIManager::$port . ' --security_key ' . VCLIManager::$security_key;
             $cmd .= ' > /dev/null 2>&1 &';
         }
     }
     // Launch vcli instance
     if (!$process_id) {
         if (VCLIManager::$platform === 'win32') {
             pclose(popen($cmd, "r"));
         } else {
             exec($cmd);
         }
     }
     // Wait up to 15 seconds to see if socket is online
     $connected = @fsockopen("127.0.0.1", VCLIManager::$port);
     $timeup = 15;
     while (false === $connected && $timeup > 0) {
         $connected = @fsockopen("127.0.0.1", VCLIManager::$port);
         $timeup = $timeup - 1;
         sleep(1);
     }
     @fclose($connected);
 }
 /**
  * Get the current results from the virtual commandline interface. Non-blocking. Specify an optional command_id to
  * retrieve a command's specific results or none (-1 default) to return all results of the current given session.
  *
  * @param int $command_id Optional id of a specific command to get the results for or -1 (default) to return all
  *
  * @return string The given results.
  */
 public function get_results($command_id = -1)
 {
     $args = array('wait_for' => $command_id, 'console_id' => $this->id, 'action' => 'result');
     $results = VCLIManager::send($args);
     // Filter out password and ~~~done~~~ echos if present
     $lines = explode(Chr(10), $results);
     $results = "";
     $prev = "";
     foreach ($lines as $line) {
         // Hide passwords in response to prompts
         if (substr(strtolower($prev), 0, 8) === "password") {
             $line = "********" . $this->eol;
         }
         // Hide done waiting mechanism
         if (false !== strpos($line, "~~~done~~~")) {
             $line = str_replace($this->concat_char . "echo ~~~done~~~", "", $line);
             $line = str_replace("~~~done~~~", "", $line);
         }
         // Eat echo'd command on Windows
         if (VCLIManager::$platform === 'win32') {
             if ($line === substr($prev, -strlen($line)) && strlen($prev) > strlen($line)) {
                 continue;
             }
         }
         // Eat invisible [?1034h
         if (substr($line, 0, 8) === '[?1034h') {
             $line = substr($line, 8);
         }
         $prev = $line;
         $results .= $line . Chr(10);
     }
     // Parse out the percent complete from the initial response
     $results = new GString($results);
     $this->completed = intval($results->getLeftMost("|\n"));
     return $results->delLeftMost("|\n")->__toString();
 }
 /**
  * Uncomment the line containing the key that was found from a prior find
  * method call.
  */
 function uncomment()
 {
     if (false === $this->last_index) {
         return;
     }
     if ($this->is_commented()) {
         $line = new GString($this->block[$this->last_index]);
         $comment = $this->comment_chars;
         if (false === strpos($this->block[$this->last_index], $comment)) {
             $comment = new GString($comment);
             // accomodate missing whitespace
             $comment = $comment->trim()->__toString();
         }
         $this->block[$this->last_index] = $line->delLeftMost($comment)->__toString();
     }
 }