Add new test programs: ConinMode, ConoutMode, WriteConsole
This commit is contained in:
parent
6c994460a3
commit
4a2ff5da03
117
misc/ConinMode.cc
Executable file
117
misc/ConinMode.cc
Executable file
@ -0,0 +1,117 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static HANDLE getConin() {
|
||||
HANDLE conin = GetStdHandle(STD_INPUT_HANDLE);
|
||||
if (conin == INVALID_HANDLE_VALUE) {
|
||||
fprintf(stderr, "error: cannot get stdin\n");
|
||||
exit(1);
|
||||
}
|
||||
return conin;
|
||||
}
|
||||
|
||||
static DWORD getConsoleMode() {
|
||||
DWORD mode = 0;
|
||||
if (!GetConsoleMode(getConin(), &mode)) {
|
||||
fprintf(stderr, "error: GetConsoleMode failed (is stdin a console?)\n");
|
||||
exit(1);
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void setConsoleMode(DWORD mode) {
|
||||
if (!SetConsoleMode(getConin(), mode)) {
|
||||
fprintf(stderr, "error: SetConsoleMode failed (is stdin a console?)\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static long parseInt(const std::string &s) {
|
||||
errno = 0;
|
||||
char *endptr = nullptr;
|
||||
long result = strtol(s.c_str(), &endptr, 0);
|
||||
if (errno != 0 || !endptr || *endptr != '\0') {
|
||||
fprintf(stderr, "error: could not parse integral argument '%s'\n", s.c_str());
|
||||
exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
printf("Usage: ConinMode [verb] [options]\n");
|
||||
printf("Verbs:\n");
|
||||
printf(" [info] Dumps info about mode flags.\n");
|
||||
printf(" get Prints the mode DWORD.\n");
|
||||
printf(" set VALUE Sets the mode to VALUE, which can be decimal, hex, or octal.\n");
|
||||
printf(" set VALUE MASK\n");
|
||||
printf(" Same as `set VALUE`, but only alters the bits in MASK.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
DWORD value;
|
||||
} kInputFlags[] = {
|
||||
"ENABLE_PROCESSED_INPUT", ENABLE_PROCESSED_INPUT, // 0x0001
|
||||
"ENABLE_LINE_INPUT", ENABLE_LINE_INPUT, // 0x0002
|
||||
"ENABLE_ECHO_INPUT", ENABLE_ECHO_INPUT, // 0x0004
|
||||
"ENABLE_WINDOW_INPUT", ENABLE_WINDOW_INPUT, // 0x0008
|
||||
"ENABLE_MOUSE_INPUT", ENABLE_MOUSE_INPUT, // 0x0010
|
||||
"ENABLE_INSERT_MODE", ENABLE_INSERT_MODE, // 0x0020
|
||||
"ENABLE_QUICK_EDIT_MODE", ENABLE_QUICK_EDIT_MODE, // 0x0040
|
||||
"ENABLE_EXTENDED_FLAGS", ENABLE_EXTENDED_FLAGS, // 0x0080
|
||||
"ENABLE_VIRTUAL_TERMINAL_INPUT", 0x0200/*ENABLE_VIRTUAL_TERMINAL_INPUT*/, // 0x0200
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::vector<std::string> args;
|
||||
for (size_t i = 1; i < argc; ++i) {
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
if (args.empty() || args.size() == 1 && args[0] == "info") {
|
||||
DWORD mode = getConsoleMode();
|
||||
printf("mode: 0x%lx\n", mode);
|
||||
for (const auto &flag : kInputFlags) {
|
||||
printf("%-29s 0x%04lx %s\n", flag.name, flag.value, flag.value & mode ? "ON" : "off");
|
||||
mode &= ~flag.value;
|
||||
}
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
if (mode & (1u << i)) {
|
||||
printf("Unrecognized flag: %04x\n", (1u << i));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto verb = args[0];
|
||||
|
||||
if (verb == "set") {
|
||||
if (args.size() == 2) {
|
||||
const DWORD newMode = parseInt(args[1]);
|
||||
setConsoleMode(newMode);
|
||||
} else if (args.size() == 3) {
|
||||
const DWORD mode = parseInt(args[1]);
|
||||
const DWORD mask = parseInt(args[2]);
|
||||
const int newMode = (getConsoleMode() & ~mask) | (mode & mask);
|
||||
setConsoleMode(newMode);
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
} else if (verb == "get") {
|
||||
if (args.size() != 1) {
|
||||
usage();
|
||||
}
|
||||
printf("0x%lx\n", getConsoleMode());
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
113
misc/ConoutMode.cc
Executable file
113
misc/ConoutMode.cc
Executable file
@ -0,0 +1,113 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static HANDLE getConout() {
|
||||
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
if (conout == INVALID_HANDLE_VALUE) {
|
||||
fprintf(stderr, "error: cannot get stdout\n");
|
||||
exit(1);
|
||||
}
|
||||
return conout;
|
||||
}
|
||||
|
||||
static DWORD getConsoleMode() {
|
||||
DWORD mode = 0;
|
||||
if (!GetConsoleMode(getConout(), &mode)) {
|
||||
fprintf(stderr, "error: GetConsoleMode failed (is stdout a console?)\n");
|
||||
exit(1);
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
static void setConsoleMode(DWORD mode) {
|
||||
if (!SetConsoleMode(getConout(), mode)) {
|
||||
fprintf(stderr, "error: SetConsoleMode failed (is stdout a console?)\n");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static long parseInt(const std::string &s) {
|
||||
errno = 0;
|
||||
char *endptr = nullptr;
|
||||
long result = strtol(s.c_str(), &endptr, 0);
|
||||
if (errno != 0 || !endptr || *endptr != '\0') {
|
||||
fprintf(stderr, "error: could not parse integral argument '%s'\n", s.c_str());
|
||||
exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
printf("Usage: ConoutMode [verb] [options]\n");
|
||||
printf("Verbs:\n");
|
||||
printf(" [info] Dumps info about mode flags.\n");
|
||||
printf(" get Prints the mode DWORD.\n");
|
||||
printf(" set VALUE Sets the mode to VALUE, which can be decimal, hex, or octal.\n");
|
||||
printf(" set VALUE MASK\n");
|
||||
printf(" Same as `set VALUE`, but only alters the bits in MASK.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
struct {
|
||||
const char *name;
|
||||
DWORD value;
|
||||
} kOutputFlags[] = {
|
||||
"ENABLE_PROCESSED_OUTPUT", ENABLE_PROCESSED_OUTPUT, // 0x0001
|
||||
"ENABLE_WRAP_AT_EOL_OUTPUT", ENABLE_WRAP_AT_EOL_OUTPUT, // 0x0002
|
||||
"ENABLE_VIRTUAL_TERMINAL_PROCESSING", 0x0004/*ENABLE_VIRTUAL_TERMINAL_PROCESSING*/, // 0x0004
|
||||
"DISABLE_NEWLINE_AUTO_RETURN", 0x0008/*DISABLE_NEWLINE_AUTO_RETURN*/, // 0x0008
|
||||
"ENABLE_LVB_GRID_WORLDWIDE", 0x0010/*ENABLE_LVB_GRID_WORLDWIDE*/, //0x0010
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::vector<std::string> args;
|
||||
for (size_t i = 1; i < argc; ++i) {
|
||||
args.push_back(argv[i]);
|
||||
}
|
||||
|
||||
if (args.empty() || args.size() == 1 && args[0] == "info") {
|
||||
DWORD mode = getConsoleMode();
|
||||
printf("mode: 0x%lx\n", mode);
|
||||
for (const auto &flag : kOutputFlags) {
|
||||
printf("%-34s 0x%04lx %s\n", flag.name, flag.value, flag.value & mode ? "ON" : "off");
|
||||
mode &= ~flag.value;
|
||||
}
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
if (mode & (1u << i)) {
|
||||
printf("Unrecognized flag: %04x\n", (1u << i));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
const auto verb = args[0];
|
||||
|
||||
if (verb == "set") {
|
||||
if (args.size() == 2) {
|
||||
const DWORD newMode = parseInt(args[1]);
|
||||
setConsoleMode(newMode);
|
||||
} else if (args.size() == 3) {
|
||||
const DWORD mode = parseInt(args[1]);
|
||||
const DWORD mask = parseInt(args[2]);
|
||||
const int newMode = (getConsoleMode() & ~mask) | (mode & mask);
|
||||
setConsoleMode(newMode);
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
} else if (verb == "get") {
|
||||
if (args.size() != 1) {
|
||||
usage();
|
||||
}
|
||||
printf("0x%lx\n", getConsoleMode());
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
106
misc/WriteConsole.cc
Executable file
106
misc/WriteConsole.cc
Executable file
@ -0,0 +1,106 @@
|
||||
#include <windows.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static std::wstring mbsToWcs(const std::string &s) {
|
||||
const size_t len = mbstowcs(nullptr, s.c_str(), 0);
|
||||
if (len == static_cast<size_t>(-1)) {
|
||||
assert(false && "mbsToWcs: invalid string");
|
||||
}
|
||||
std::wstring ret;
|
||||
ret.resize(len);
|
||||
const size_t len2 = mbstowcs(&ret[0], s.c_str(), len);
|
||||
assert(len == len2);
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t parseHex(wchar_t ch, bool &invalid) {
|
||||
if (ch >= L'0' && ch <= L'9') {
|
||||
return ch - L'0';
|
||||
} else if (ch >= L'a' && ch <= L'f') {
|
||||
return ch - L'a' + 10;
|
||||
} else if (ch >= L'A' && ch <= L'F') {
|
||||
return ch - L'A' + 10;
|
||||
} else {
|
||||
invalid = true;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
std::vector<std::wstring> args;
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
args.push_back(mbsToWcs(argv[i]));
|
||||
}
|
||||
|
||||
std::wstring out;
|
||||
for (const auto &arg : args) {
|
||||
if (!out.empty()) {
|
||||
out.push_back(L' ');
|
||||
}
|
||||
for (size_t i = 0; i < arg.size(); ++i) {
|
||||
wchar_t ch = arg[i];
|
||||
wchar_t nch = i + 1 < arg.size() ? arg[i + 1] : L'\0';
|
||||
if (ch == L'\\') {
|
||||
switch (nch) {
|
||||
case L'a': ch = L'\a'; ++i; break;
|
||||
case L'b': ch = L'\b'; ++i; break;
|
||||
case L'e': ch = L'\x1b'; ++i; break;
|
||||
case L'f': ch = L'\f'; ++i; break;
|
||||
case L'n': ch = L'\n'; ++i; break;
|
||||
case L'r': ch = L'\r'; ++i; break;
|
||||
case L't': ch = L'\t'; ++i; break;
|
||||
case L'v': ch = L'\v'; ++i; break;
|
||||
case L'\\': ch = L'\\'; ++i; break;
|
||||
case L'\'': ch = L'\''; ++i; break;
|
||||
case L'\"': ch = L'\"'; ++i; break;
|
||||
case L'\?': ch = L'\?'; ++i; break;
|
||||
case L'x':
|
||||
if (i + 3 < arg.size()) {
|
||||
bool invalid = false;
|
||||
uint32_t d1 = parseHex(arg[i + 2], invalid);
|
||||
uint32_t d2 = parseHex(arg[i + 3], invalid);
|
||||
if (!invalid) {
|
||||
i += 3;
|
||||
ch = (d1 << 4) | d2;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case L'u':
|
||||
if (i + 5 < arg.size()) {
|
||||
bool invalid = false;
|
||||
uint32_t d1 = parseHex(arg[i + 2], invalid);
|
||||
uint32_t d2 = parseHex(arg[i + 3], invalid);
|
||||
uint32_t d3 = parseHex(arg[i + 4], invalid);
|
||||
uint32_t d4 = parseHex(arg[i + 5], invalid);
|
||||
if (!invalid) {
|
||||
i += 5;
|
||||
ch = (d1 << 24) | (d2 << 16) | (d3 << 8) | d4;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
out.push_back(ch);
|
||||
}
|
||||
}
|
||||
|
||||
DWORD actual = 0;
|
||||
if (!WriteConsoleW(
|
||||
GetStdHandle(STD_OUTPUT_HANDLE),
|
||||
out.c_str(),
|
||||
out.size(),
|
||||
&actual,
|
||||
nullptr)) {
|
||||
fprintf(stderr, "WriteConsole failed (is stdout a console?)\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user