Repository navigation
Keep letters outside ASCII in a Windows program's arguments - #516
Draft
dimensionscape wants to merge 1 commit into
Draft
dimensionscape wants to merge 1 commit into
dimensionscape wants to merge 1 commit into
Conversation
On Windows the C runtime hands main its arguments in the ANSI code page, and __get_args reads them as UTF-8. A letter of the code page, as the e acute of code page 1252, came out of Sys.args() as U+FFFD, and a letter the code page lacks was already a "?". For a character it lacks Windows writes the nearest it has, so a full width quote (U+FF02) became a quote and ended the argument: "a<U+FF02> b" and "c" arrived as "a" and "b c". main now passes its arguments through _hxcpp_windows_utf8_argv before anything reads them: - a command line of ASCII is returned as it is; - where the ANSI command line still holds every character, the arguments stay the runtime's and are written as UTF-8, so its splitting is kept, and its wildcard expansion in a program linked for that; - where it does not, the wide command line is split here by the rules the runtime documents. Nothing expands wildcards there, so a program linked with setargv.obj gets none on such a command line. _hxcpp_argv stays an array of UTF-8, so the cppia host still moves it past its own options, and a script it runs gets the same arguments. The WinMain entry of an MSVC build with -D no_console is unchanged: it has no arguments of main's and reads the wide command line already, with a splitter of its own. ArgsWide starts itself with such arguments, through Process and, for the splitting rules, as command lines written out whole. CppiaHostArgs passes two to a script. The splitting was also compared with the C runtime's own on 3,700 command lines of quotes, backslashes, tabs and spaces, with no difference.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Windows,
Sys.args()in a console program loses letters outside ASCII. On a machine whose ANSI code page is 1252:caféarrives ascafand U+FFFD;日本arrives as??;αβarrives asaand U+FFFD;a, U+FF02 (a full width quote), space,bandcarrive asaandb c.A script run by the cppia host,
Cppia.exe, gets the same.Cause
The C runtime splits the ANSI copy of the command line and hands
mainbytes of the ANSI code page.mainstores them in_hxcpp_argv, and__get_argsreads_hxcpp_argvas UTF-8. A lone byte above 0x7F is not UTF-8, hence U+FFFD. A letter the code page lacks was replaced before the program started, by?or by the nearest character the code page has. For U+FF02 that is a quote, which the runtime then takes as one.A program built with
-D no_consoleon MSVC enters throughWinMain, has no arguments ofmain's, and reads the wide command line already. The letters arrive there before and after this change.The change
mainpasses its arguments through_hxcpp_windows_utf8_argv, insrc/hx/StdLibs.cpp, before it stores them:setargv.obj.setargv.objgets*.txtas it was typed when another argument holds a letter the code page lacks._hxcpp_argvstays an array of UTF-8 strings and__get_argsis not changed, so a host that sets_hxcpp_argvitself is read as before, and the cppia host still moves it past its own options.Tests
test/regression/ArgsWidestarts itself with arguments and compares the code unitsSys.args()gives the child with what was sent: ASCII, a letter of the code page, letters of three other scripts, a character above U+FFFF, such letters beside a space, a quote, a backslash and an empty argument, an argument that starts with#, and two characters whose nearest ANSI character is a quote and a space. On Windows it also writes six command lines out whole, for the splitting rulesProcessnever produces: two quotes inside quotes, backslashes before a quote and before a letter, a tab, and a quote left open. Before the change 18 of its 19 lines are false.CppiaHostArgsgets one more line, a script that seescaféand日本after its name. It is false before the change.Run on 64-bit Windows with MSVC and Haxe 4.3.7, ANSI code page 1252:
ArgsWideas a 64-bit and as a 32-bit program, andCppiaHostArgs.EnvironmentWide,ProgramPathNamesProgram, the 13Process*, the 5Utf8*and the 73Cppia*. They ran before one comment inStdLibs.cppwas reworded, andArgsWideandCppiaHostArgsran again after.#,*and letters, with the program's own name varied too. Each was sent once as ASCII, where the arguments are the runtime's, and once with a letter the code page lacks, where they are split here. None differed.ArgsWidefail: two quotes inside quotes no longer one quote, backslashes before a quote not halved, the runtime's arguments not written as UTF-8, and the wide command line never split.café dirand日本, and from one named with an emoji: its arguments arrive.setargv.obj, in a folder that holdsone.txtandtwo.txt.*.txtand*.txt cafégive both file names, as onmain, andcafénow arrives.*.txt 日gives*.txtand日. Onmainit gave the two file names and no third argument.ArgsWidebuilt with-D no_console: 18 of its 19 lines are true onmainand here, every line about letters among them. The false one is two quotes inside quotes: that entry has a splitter of its own, which this does not change.Not run here: Linux, macOS, MinGW, another ANSI code page, the rest of
test/regression, and the other suites.The
Changes.mdentry sits at the same place as the one in #515, so whichever merges second needs a rebase.