Skip to content

std.start: initialize Windows console output CP to UTF-8 on exe startup #14411

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/std/start.zig
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,14 @@ extern "kernel32" fn ExitProcess(exit_code: u32) callconv(.C) noreturn;

////////////////////////////////////////////////////////////////////////////////

/// Contains any logic that should be run on exe startup to bully Windows
/// into being a sane environment
inline fn setupWindows() void {
if (std.options.windows_force_utf8_codepage) {
_ = std.os.windows.kernel32.SetConsoleOutputCP(65001); // use UTF-8 codepage
}
Copy link
Contributor

@expikr expikr Nov 21, 2023

Choose a reason for hiding this comment

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

Seems that this would be on the same level of prescription as enabling ansi escape codes here, could sneak in something like

Suggested change
}
}
if (std.options.windows_force_virtual_terminal) {
const ENABLE_VIRTUAL_TERMINAL_PROCESSING = 4;
var stdout_mode: u32 = undefined;
_ = std.os.windows.kernel32.GetConsoleMode(std.io.getStdOut().handle, &stdout_mode);
stdout_mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
_ = std.os.windows.kernel32.SetConsoleMode(std.io.getStdOut().handle, stdout_mode);
}

}

fn _DllMainCRTStartup(
hinstDLL: std.os.windows.HINSTANCE,
fdwReason: std.os.windows.DWORD,
Expand Down Expand Up @@ -382,6 +390,7 @@ fn WinStartup() callconv(std.os.windows.WINAPI) noreturn {
_ = @import("start_windows_tls.zig");
}

setupWindows();
std.debug.maybeEnableSegfaultHandler();

std.os.windows.kernel32.ExitProcess(initEventLoopAndCallMain());
Expand All @@ -393,6 +402,7 @@ fn wWinMainCRTStartup() callconv(std.os.windows.WINAPI) noreturn {
_ = @import("start_windows_tls.zig");
}

setupWindows();
std.debug.maybeEnableSegfaultHandler();

const result: std.os.windows.INT = initEventLoopAndCallWinMain();
Expand Down Expand Up @@ -495,6 +505,9 @@ fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
std.os.argv = argv[0..argc];
std.os.environ = envp;

if (builtin.os.tag == .windows) {
setupWindows();
}
std.debug.maybeEnableSegfaultHandler();

return initEventLoopAndCallMain();
Expand Down
5 changes: 5 additions & 0 deletions lib/std/std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ pub const options = struct {
options_override.crypto_always_getrandom
else
false;

pub const windows_force_utf8_codepage: bool = if (@hasDecl(options_override, "windows_force_utf8_codepage"))
options_override.windows_force_utf8_codepage
else
true;
};

// This forces the start.zig file to be imported, and the comptime logic inside that
Expand Down