Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

win32.c: make reading UTF-8 characters from the console possible #18702

Merged
merged 1 commit into from
Apr 13, 2021

Conversation

xenu
Copy link
Member

@xenu xenu commented Apr 9, 2021

Due to a bug in Windows, ReadFile() and ReadConsoleA() (and thus
_read()), return zeros instead of non-ASCII characters when the console
codepage is set to 65001. See this ticket for more details:
microsoft/terminal#4551

This commit works around that bug by using ReadConsoleW() inside
win32_read() when the passed fd points to the console and the console
codepage is set to 65001.

Fixes #18701

@xenu xenu requested review from tonycoz and steve-m-hay April 9, 2021 02:46
Due to a bug in Windows, ReadFile() and ReadConsoleA() (and thus
_read()), return zeros instead of non-ASCII characters when the console
codepage is set to 65001. See this ticket for more details:
microsoft/terminal#4551

This commit works around that bug by using ReadConsoleW() inside
win32_read() when the passed fd points to the console and the console
codepage is set to 65001.

Fixes Perl#18701
@xenu
Copy link
Member Author

xenu commented Apr 10, 2021

I forgot about Ctrl-Z handling. Unlike ReadFile(), ReadConsole() doesn't do that automatically. It's fixed now.

Copy link
Contributor

@tonycoz tonycoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

My only concern is that we'll be calling win32_read() once for every unicode character input, and I think that will propagate up through to pp_sysread's read() implementation, pp_sysread will be calling PerlIO_read() for each character input.

I don't see a way to avoid that without making this much more complex.

This will only be a concern for pastes to the console though.

@xenu
Copy link
Member Author

xenu commented Apr 12, 2021

win32_read_console() acts just like the standard read(), if you call it with a buffer of length 100, it will try to return 100 bytes. Unless, of course, ENABLE_LINE_INPUT is disabled, but that's a non-default configuration.

The internal algorithm of win32_read_console() is indeed extremely ineffective, but that should not propagate. It won't cause its callers to return after reading just a single character.

@xenu xenu merged commit dace60f into Perl:blead Apr 13, 2021
@xenu xenu deleted the xenu/win32-read-65001 branch April 13, 2021 23:51
@asfernandes
Copy link

Hi @xenu !

We are having the same problem in FirebirdSQL/firebird#7202

Looks like your solution fix our problem.

We use a license similar to MPL. So it's not compatible with GPL, but I'm not sure about Artistic license.

Do you allow us to write our code basing in your solution with appropriate credits as comments in our code?

@xenu
Copy link
Member Author

xenu commented Jun 15, 2022

@asfernandes Sure. You have my permission to use the code from this PR under the terms of the whatever license Firebird is using right now.

@asfernandes
Copy link

@asfernandes Sure. You have my permission to use the code from this PR under the terms of the whatever license Firebird is using right now.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Reading non-ASCII characters from the console on Windows doesn't work when codepage is 65001
3 participants