From 41452785c67731d30ab5ba7df85157b57f2bd436 Mon Sep 17 00:00:00 2001 From: Marek Zajac Date: Thu, 13 May 2021 07:53:50 +0200 Subject: [PATCH] [Console] Fix Windows code page support --- .../Console/Helper/QuestionHelper.php | 44 ++++++++++++++++--- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Console/Helper/QuestionHelper.php b/src/Symfony/Component/Console/Helper/QuestionHelper.php index f1a3c4b5d7..2f582d4585 100644 --- a/src/Symfony/Component/Console/Helper/QuestionHelper.php +++ b/src/Symfony/Component/Console/Helper/QuestionHelper.php @@ -108,11 +108,6 @@ class QuestionHelper extends Helper $inputStream = $this->inputStream ?: \STDIN; $autocomplete = $question->getAutocompleterCallback(); - if (\function_exists('sapi_windows_cp_set')) { - // Codepage used by cmd.exe on Windows to allow special characters (éàüñ). - @sapi_windows_cp_set(1252); - } - if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) { $ret = false; if ($question->isHidden()) { @@ -127,7 +122,9 @@ class QuestionHelper extends Helper } if (false === $ret) { + $cp = $this->setIOCodepage(); $ret = fgets($inputStream, 4096); + $ret = $this->resetIOCodepage($cp, $ret); if (false === $ret) { throw new MissingInputException('Aborted.'); } @@ -503,4 +500,41 @@ class QuestionHelper extends Helper return self::$stdinIsInteractive = 1 !== $status; } + + /** + * Sets console I/O to the host code page. + * + * @return int Previous code page in IBM/EBCDIC format + */ + private function setIOCodepage(): int + { + if (\function_exists('sapi_windows_cp_set')) { + $cp = sapi_windows_cp_get(); + sapi_windows_cp_set(sapi_windows_cp_get('oem')); + + return $cp; + } + + return 0; + } + + /** + * Sets console I/O to the specified code page and converts the user input. + * + * @param string|false $input + * + * @return string|false + */ + private function resetIOCodepage(int $cp, $input) + { + if (0 !== $cp) { + sapi_windows_cp_set($cp); + + if (false !== $input && '' !== $input) { + $input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input); + } + } + + return $input; + } }