From 5ce324f35f2b43a8cd28141dc8eb688b92e444c2 Mon Sep 17 00:00:00 2001 From: Thomas Keller Date: Thu, 24 Jun 2010 00:10:41 +0200 Subject: [PATCH] * clear the process environment and set LANG properly so we get english error codes and still keep utf8 compliant * rename _read to _readStdout and introduce a generic _readStderr which reads from the other pipe on failure --- src/IDF/Scm/Monotone.php | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/IDF/Scm/Monotone.php b/src/IDF/Scm/Monotone.php index 49c40b0..b9c0b9e 100644 --- a/src/IDF/Scm/Monotone.php +++ b/src/IDF/Scm/Monotone.php @@ -81,7 +81,10 @@ class IDF_Scm_Monotone_Stdio 2 => array("pipe", "w"), ); - $this->proc = proc_open($cmd, $descriptors, $this->pipes); + $env = array("LANG" => "en_US.UTF-8"); + + $this->proc = proc_open($cmd, $descriptors, $this->pipes, + null, $env); if (!is_resource($this->proc)) { @@ -153,9 +156,9 @@ class IDF_Scm_Monotone_Stdio $version = fgets($this->pipes[1]); if ($version === false) { - $err = fgets($this->pipes[2]); throw new IDF_Scm_Exception( - "Could not determine stdio version: $err" + "Could not determine stdio version, stderr is:\n". + $this->_readStderr() ); } @@ -214,13 +217,28 @@ class IDF_Scm_Monotone_Stdio $this->cmdnum++; } + /** + * Reads all output from stderr and returns it + * + * @return string + */ + private function _readStderr() + { + $err = ""; + while (($line = fgets($this->pipes[2])) !== false) + { + $err .= $line; + } + return empty($err) ? "" : $err; + } + /** * Reads the last output from the stdio process, parses and returns it * * @return string * @throws IDF_Scm_Exception */ - private function _read() + private function _readStdout() { $this->oob = array('w' => array(), 'p' => array(), @@ -242,10 +260,10 @@ class IDF_Scm_Monotone_Stdio $c = fgetc($this->pipes[1]); if ($c === false) { - $err = fgets($this->pipes[2]); throw new IDF_Scm_Exception( - "Could not read stdio: $err" - ); + "No data on stdin, stderr is:\n". + $this->_readStderr() + ); } if ($c == ':') @@ -320,7 +338,7 @@ class IDF_Scm_Monotone_Stdio public function exec(array $args, array $options = array()) { $this->_write($args, $options); - return $this->_read(); + return $this->_readStdout(); } /**