@@ -108,11 +108,6 @@ private function doAsk(OutputInterface $output, Question $question)
108
108
$ inputStream = $ this ->inputStream ?: \STDIN ;
109
109
$ autocomplete = $ question ->getAutocompleterCallback ();
110
110
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
-
116
111
if (null === $ autocomplete || !self ::$ stty || !Terminal::hasSttyAvailable ()) {
117
112
$ ret = false ;
118
113
if ($ question ->isHidden ()) {
@@ -127,7 +122,9 @@ private function doAsk(OutputInterface $output, Question $question)
127
122
}
128
123
129
124
if (false === $ ret ) {
125
+ $ cp = $ this ->setIOCodepage ();
130
126
$ ret = fgets ($ inputStream , 4096 );
127
+ $ ret = $ this ->resetIOCodepage ($ cp , $ ret );
131
128
if (false === $ ret ) {
132
129
throw new MissingInputException ('Aborted. ' );
133
130
}
@@ -503,4 +500,41 @@ private function isInteractiveInput($inputStream): bool
503
500
504
501
return self ::$ stdinIsInteractive = 1 !== $ status ;
505
502
}
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
+ }
506
540
}
0 commit comments