dart-sdk/runtime/bin/stdio_win.cc
Zach Anderson f407419d0a [vm] Reland: Prefix HOST_OS_* and TARGET_OS_* with DART_
This relands https://dart-review.googlesource.com/c/sdk/+/205633
but without renaming TARGET_OS_IPHONE to DART_TARGET_OS_IPHONE.
It also changes uses of TARGET_OS_IOS to
DART_TARGET_OS_MACOS_IOS to be consistent with the rest of the
VM.

TargetConditionals.h for XCode 13 defines several
TARGET_OS_* preprocessor symbols that confuse the
Dart build. There is probably a more targeted fix
for this, but renaming the symbols that Dart uses
will also prevent this problem if more symbols
are added to the platform headers in the future.

See: https://github.com/dart-lang/sdk/issues/46499

TEST=It builds.

Change-Id: Ie775c19dd23cfdf5f65e5ebc6ee4ec3a561676fa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/205860
Commit-Queue: Zach Anderson <zra@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
2021-07-02 19:06:45 +00:00

139 lines
3.3 KiB
C++

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#include "platform/globals.h"
#if defined(DART_HOST_OS_WINDOWS)
#include "bin/stdio.h"
// These are not always defined in the header files. See:
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx
#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
#endif
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
namespace dart {
namespace bin {
bool Stdin::ReadByte(intptr_t fd, int* byte) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
uint8_t buffer[1];
DWORD read = 0;
BOOL success = ReadFile(h, buffer, 1, &read, NULL);
if (!success && (GetLastError() != ERROR_BROKEN_PIPE)) {
return false;
}
*byte = (read == 1) ? buffer[0] : -1;
return true;
}
bool Stdin::GetEchoMode(intptr_t fd, bool* enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
*enabled = ((mode & ENABLE_ECHO_INPUT) != 0);
return true;
}
bool Stdin::SetEchoMode(intptr_t fd, bool enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
if (enabled) {
mode |= ENABLE_ECHO_INPUT;
} else {
mode &= ~ENABLE_ECHO_INPUT;
}
return SetConsoleMode(h, mode);
}
bool Stdin::GetLineMode(intptr_t fd, bool* enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
*enabled = (mode & ENABLE_LINE_INPUT) != 0;
return true;
}
bool Stdin::SetLineMode(intptr_t fd, bool enabled) {
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
return false;
}
if (enabled) {
mode |= ENABLE_LINE_INPUT;
} else {
mode &= ~ENABLE_LINE_INPUT;
}
return SetConsoleMode(h, mode);
}
bool Stdin::AnsiSupported(intptr_t fd, bool* supported) {
ASSERT(supported != NULL);
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
if (h == INVALID_HANDLE_VALUE) {
*supported = false;
return true;
}
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
*supported = false;
return true;
}
*supported = (mode & ENABLE_VIRTUAL_TERMINAL_INPUT) != 0;
return true;
}
bool Stdout::GetTerminalSize(intptr_t fd, int size[2]) {
HANDLE h;
if (fd == 1) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
} else {
h = GetStdHandle(STD_ERROR_HANDLE);
}
CONSOLE_SCREEN_BUFFER_INFO info;
if (!GetConsoleScreenBufferInfo(h, &info)) {
return false;
}
size[0] = info.srWindow.Right - info.srWindow.Left + 1;
size[1] = info.srWindow.Bottom - info.srWindow.Top + 1;
return true;
}
bool Stdout::AnsiSupported(intptr_t fd, bool* supported) {
ASSERT(supported != NULL);
HANDLE h;
if (fd == 1) {
h = GetStdHandle(STD_OUTPUT_HANDLE);
} else {
h = GetStdHandle(STD_ERROR_HANDLE);
}
if (h == INVALID_HANDLE_VALUE) {
*supported = false;
return true;
}
DWORD mode;
if (!GetConsoleMode(h, &mode)) {
*supported = false;
return true;
}
*supported = (mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0;
return true;
}
} // namespace bin
} // namespace dart
#endif // defined(DART_HOST_OS_WINDOWS)