@@ -110,11 +110,6 @@ private function doAsk(OutputInterface $output, Question $question)
110
110
$ inputStream = $ this ->inputStream ?: \STDIN ;
111
111
$ autocomplete = $ question ->getAutocompleterCallback ();
112
112
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
-
118
113
if (null === $ autocomplete || !self ::$ stty || !Terminal::hasSttyAvailable ()) {
119
114
$ ret = false ;
120
115
if ($ question ->isHidden ()) {
@@ -514,7 +509,10 @@ private function isInteractiveInput($inputStream): bool
514
509
private function readInput ($ inputStream , Question $ question )
515
510
{
516
511
if (!$ question ->isMultiline ()) {
517
- return fgets ($ inputStream , 4096 );
512
+ $ cp = $ this ->setIOCodepage ();
513
+ $ ret = fgets ($ inputStream , 4096 );
514
+
515
+ return $ this ->resetIOCodepage ($ cp , $ ret );
518
516
}
519
517
520
518
$ multiLineStreamReader = $ this ->cloneInputStream ($ inputStream );
@@ -523,14 +521,45 @@ private function readInput($inputStream, Question $question)
523
521
}
524
522
525
523
$ ret = '' ;
524
+ $ cp = $ this ->setIOCodepage ();
526
525
while (false !== ($ char = fgetc ($ multiLineStreamReader ))) {
527
526
if (\PHP_EOL === "{$ ret }{$ char }" ) {
528
527
break ;
529
528
}
530
529
$ ret .= $ char ;
531
530
}
532
531
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 ;
534
563
}
535
564
536
565
/**
0 commit comments