From cc536c7f45e656989f987f6eea5139a76beb4419 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 13 Nov 2020 08:58:06 +0100 Subject: [PATCH] Fix/normalize tty handling --- src/Composer/Console/Application.php | 7 +------ src/Composer/Util/Platform.php | 22 ++++++++++++++++++++++ src/Composer/Util/ProcessExecutor.php | 10 +++------- 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/Composer/Console/Application.php b/src/Composer/Console/Application.php index 77ac06377..e92b45863 100644 --- a/src/Composer/Console/Application.php +++ b/src/Composer/Console/Application.php @@ -129,12 +129,7 @@ class Application extends BaseApplication { $this->disablePluginsByDefault = $input->hasParameterOption('--no-plugins'); - if ( - getenv('COMPOSER_NO_INTERACTION') - /* @see \Composer\Util\ProcessExecutor::executeTty - tty test */ - || (function_exists('stream_isatty') && !stream_isatty(STDOUT)) - || (function_exists('posix_isatty') && !posix_isatty(STDOUT)) - ) { + if (getenv('COMPOSER_NO_INTERACTION') || !Platform::isTty(STDIN)) { $input->setInteractive(false); } diff --git a/src/Composer/Util/Platform.php b/src/Composer/Util/Platform.php index 4dc9af07b..a582a6fad 100644 --- a/src/Composer/Util/Platform.php +++ b/src/Composer/Util/Platform.php @@ -89,4 +89,26 @@ class Platform return \strlen($str); } + + public static function isTty($fd = null) + { + if ($fd === null) { + $fd = STDOUT; + } + + // modern cross-platform function, includes the fstat + // fallback so if it is present we trust it + if (function_exists('stream_isatty')) { + return stream_isatty($fd); + } + + // only trusting this if it is positive, otherwise prefer fstat fallback + if (function_exists('posix_isatty') && posix_isatty($fd)) { + return true; + } + + $stat = @fstat($fd); + // Check if formatted mode is S_IFCHR + return $stat ? 0020000 === ($stat['mode'] & 0170000) : false; + } } diff --git a/src/Composer/Util/ProcessExecutor.php b/src/Composer/Util/ProcessExecutor.php index 1f55f6144..930974b06 100644 --- a/src/Composer/Util/ProcessExecutor.php +++ b/src/Composer/Util/ProcessExecutor.php @@ -77,15 +77,11 @@ class ProcessExecutor */ public function executeTty($command, $cwd = null) { - if ( - /* @see \Composer\Console\Application::doRun - tty test */ - (function_exists('stream_isatty') && !stream_isatty(STDOUT)) - || (function_exists('posix_isatty') && !posix_isatty(STDOUT)) - ) { - return $this->doExecute($command, $cwd, false); + if (Platform::isTty()) { + return $this->doExecute($command, $cwd, true); } - return $this->doExecute($command, $cwd, true); + return $this->doExecute($command, $cwd, false); } private function doExecute($command, $cwd, $tty, &$output = null)