Skip to content

Commit 53e47b3

Browse files
bug #41113 [Console] Fix Windows code page support (orkan)
This PR was submitted for the 5.x branch but it was squashed and merged into the 5.2 branch instead. Discussion ---------- [Console] Fix Windows code page support | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #37385, Fix #35842, Fix #36324, Fix #37495, Fix #37278 | License | MIT Corrects previous fixes that dealt with the mojibake problem on Windows where an OEM code page was applied to an input string and then messed with PHP.internal_encoding setting used by the script. This caused strings with different encodings to be displayed on the console output. Commits ------- be68682 [Console] Fix Windows code page support
2 parents 897f287 + be68682 commit 53e47b3

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

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

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ private function doAsk(OutputInterface $output, Question $question)
110110
$inputStream = $this->inputStream ?: \STDIN;
111111
$autocomplete = $question->getAutocompleterCallback();
112112

113-
if (\function_exists('sapi_windows_cp_set')) {
114-
// Codepage used by cmd.exe on Windows to allow special characters (éàüñ).
115-
@sapi_windows_cp_set(1252);
116-
}
117-
118113
if (null === $autocomplete || !self::$stty || !Terminal::hasSttyAvailable()) {
119114
$ret = false;
120115
if ($question->isHidden()) {
@@ -514,7 +509,10 @@ private function isInteractiveInput($inputStream): bool
514509
private function readInput($inputStream, Question $question)
515510
{
516511
if (!$question->isMultiline()) {
517-
return fgets($inputStream, 4096);
512+
$cp = $this->setIOCodepage();
513+
$ret = fgets($inputStream, 4096);
514+
515+
return $this->resetIOCodepage($cp, $ret);
518516
}
519517

520518
$multiLineStreamReader = $this->cloneInputStream($inputStream);
@@ -523,14 +521,45 @@ private function readInput($inputStream, Question $question)
523521
}
524522

525523
$ret = '';
524+
$cp = $this->setIOCodepage();
526525
while (false !== ($char = fgetc($multiLineStreamReader))) {
527526
if (\PHP_EOL === "{$ret}{$char}") {
528527
break;
529528
}
530529
$ret .= $char;
531530
}
532531

533-
return $ret;
532+
return $this->resetIOCodepage($cp, $ret);
533+
}
534+
535+
/**
536+
* Set console I/O to the host code page.
537+
*
538+
* @return int Previous code page in IBM/EBCDIC format
539+
*/
540+
private function setIOCodepage(): int
541+
{
542+
if (\function_exists('sapi_windows_cp_set')) {
543+
$cp = sapi_windows_cp_get();
544+
sapi_windows_cp_set(sapi_windows_cp_get('oem'));
545+
546+
return $cp;
547+
}
548+
549+
return 0;
550+
}
551+
552+
/**
553+
* Set console I/O to the specified code page and convert the user input.
554+
*/
555+
private function resetIOCodepage(int $cp, string $input): string
556+
{
557+
if (\function_exists('sapi_windows_cp_set') && 0 < $cp) {
558+
sapi_windows_cp_set($cp);
559+
$input = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), $cp, $input);
560+
}
561+
562+
return $input;
534563
}
535564

536565
/**

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy