mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This adjusts the Windows console to switch the codepage to UTF-8. This is important as the default codepage (CP437) does not allow for UTF-8 output, but expects ASCII. However, strings in Swift are assumed to be UTF-8, which means that there is now a conversion mismatch. Because the console mode persists beyond the duration of the application as it is state local to the console and not the C runtime, we should restore the state of the console before termination. We do this by registering a termination handler via `atexit`. This means that an abnormal termination (e.g. via `fatalError`) will irrevocably alter the state of the console (interestingly enough, `chcp` will still report the original console codepage even though the console will internally be set to UTF-8). Fixes: SR-13807
78 lines
2.0 KiB
C++
78 lines
2.0 KiB
C++
//===--- LibcShims.cpp ----------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#if defined(__APPLE__)
|
|
#define _REENTRANT
|
|
#include <math.h>
|
|
#endif
|
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
|
#include <io.h>
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#include <Windows.h>
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <sys/types.h>
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__)
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <type_traits>
|
|
|
|
#include "../SwiftShims/LibcShims.h"
|
|
|
|
#if defined(_WIN32)
|
|
static void __attribute__((__constructor__))
|
|
_swift_stdlib_configure_console_mode(void) {
|
|
static UINT uiPrevConsoleCP = GetConsoleOutputCP();
|
|
atexit([]() { SetConsoleOutputCP(uiPrevConsoleCP); });
|
|
SetConsoleOutputCP(CP_UTF8);
|
|
}
|
|
#endif
|
|
|
|
SWIFT_RUNTIME_STDLIB_INTERNAL
|
|
__swift_size_t _swift_stdlib_fwrite_stdout(const void *ptr,
|
|
__swift_size_t size,
|
|
__swift_size_t nitems) {
|
|
return fwrite(ptr, size, nitems, stdout);
|
|
}
|
|
|
|
SWIFT_RUNTIME_STDLIB_SPI
|
|
__swift_ssize_t
|
|
_swift_stdlib_read(int fd, void *buf, __swift_size_t nbyte) {
|
|
#if defined(_WIN32)
|
|
return _read(fd, buf, nbyte);
|
|
#else
|
|
return read(fd, buf, nbyte);
|
|
#endif
|
|
}
|
|
|
|
SWIFT_RUNTIME_STDLIB_SPI
|
|
__swift_ssize_t
|
|
_swift_stdlib_write(int fd, const void *buf, __swift_size_t nbyte) {
|
|
#if defined(_WIN32)
|
|
return _write(fd, buf, nbyte);
|
|
#else
|
|
return write(fd, buf, nbyte);
|
|
#endif
|
|
}
|
|
|
|
SWIFT_RUNTIME_STDLIB_SPI
|
|
int _swift_stdlib_close(int fd) {
|
|
#if defined(_WIN32)
|
|
return _close(fd);
|
|
#else
|
|
return close(fd);
|
|
#endif
|
|
}
|