8000 [Console] Fix Windows code page support · symfony/symfony@4145278 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4145278

Browse files
committed
[Console] Fix Windows code page support
1 parent 21e9894 commit 4145278

File tree

1 file changed

+39
-5
lines changed

1 file changed

+39
-5
lines changed

src/Symfony/Component/Console/Helper/QuestionHelper.php

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ private function doAsk(OutputInterface $output, Question $question)
108108
$inputStream = $this->inputStream ?: \STDIN;
109109
$autocomplete = $question->getAutocompleterCallback();
110110

111-
if (\function_exists('sapi_windows_cp_set')) {
112-
// Codepage used by cmd.exe on Windows to allow special characters (éàüñ).
113-
@sapi_windows_cp_set(1252);
114-
}
115-
116111
if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) {
117112
$ret = false;
118113
if ($question->isHidden()) {
@@ -127,7 +122,9 @@ private function doAsk(OutputInterface $output, Question $question)
127122
}
128123

129124
if (false === $ret) {
125+
$cp = $this->setIOCodepage();
130126
$ret = fgets($inputStream, 4096);
127+
$ret = $this->resetIOCodepage($cp, $ret);
131128
if (false === $ret) {
132129
throw new MissingInputException('Aborted.');
133130
}
@@ -503,4 +500,41 @@ private function isInteractiveInput($inputStream): bool
503500

504501
return self::$stdinIsInteractive = 1 !== $status;
505502
}
503+
504+
/**
505+
* Sets console I/O to the host code page.
506+
*
507+
* @return int Previous code page in IBM/EBCDIC format
508+
*/
509+
private function setIOCodepage(): int
510+
{
511+
if (\function_exists('sapi_windows_cp_set')) {
512+
$cp = sapi_windows_cp_get();
513+
sapi_windows_cp_set(sapi_windows_cp_get('oem'));
514+
515+
return $cp;
516+
}
517+
518+
return 0;
519+
}
520+
521+
/**
522+
* Sets console I/O to the specified code page and converts the user input.
523+
*
524+
* @param string|false $input
525+
*
526+
* @return string|false
527+
*/
528+
private function resetIOCodepage(int $cp, $input)
529+
{
530+
if (0 !== $cp) {
531+
sapi_windows_cp_set($cp);
532+
533+
if (false !== $input && '' !== $input) {
534+
$input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input);
535+
}
536+
}
537+
538+
return $input;
539+
}
506540
}

0 commit comments

Comments
 (0)
0