10 static HANDLE getConin() {
11 HANDLE conin = GetStdHandle(STD_INPUT_HANDLE);
12 if (conin == INVALID_HANDLE_VALUE) {
13 fprintf(stderr, "error: cannot get stdin\n");
19 static DWORD getConsoleMode() {
21 if (!GetConsoleMode(getConin(), &mode)) {
22 fprintf(stderr, "error: GetConsoleMode failed (is stdin a console?)\n");
28 static void setConsoleMode(DWORD mode) {
29 if (!SetConsoleMode(getConin(), mode)) {
30 fprintf(stderr, "error: SetConsoleMode failed (is stdin a console?)\n");
35 static long parseInt(const std::string &s) {
37 char *endptr = nullptr;
38 long result = strtol(s.c_str(), &endptr, 0);
39 if (errno != 0 || !endptr || *endptr != '\0') {
40 fprintf(stderr, "error: could not parse integral argument '%s'\n", s.c_str());
47 printf("Usage: ConinMode [verb] [options]\n");
49 printf(" [info] Dumps info about mode flags.\n");
50 printf(" get Prints the mode DWORD.\n");
51 printf(" set VALUE Sets the mode to VALUE, which can be decimal, hex, or octal.\n");
52 printf(" set VALUE MASK\n");
53 printf(" Same as `set VALUE`, but only alters the bits in MASK.\n");
61 "ENABLE_PROCESSED_INPUT", ENABLE_PROCESSED_INPUT, // 0x0001
62 "ENABLE_LINE_INPUT", ENABLE_LINE_INPUT, // 0x0002
63 "ENABLE_ECHO_INPUT", ENABLE_ECHO_INPUT, // 0x0004
64 "ENABLE_WINDOW_INPUT", ENABLE_WINDOW_INPUT, // 0x0008
65 "ENABLE_MOUSE_INPUT", ENABLE_MOUSE_INPUT, // 0x0010
66 "ENABLE_INSERT_MODE", ENABLE_INSERT_MODE, // 0x0020
67 "ENABLE_QUICK_EDIT_MODE", ENABLE_QUICK_EDIT_MODE, // 0x0040
68 "ENABLE_EXTENDED_FLAGS", ENABLE_EXTENDED_FLAGS, // 0x0080
69 "ENABLE_VIRTUAL_TERMINAL_INPUT", 0x0200/*ENABLE_VIRTUAL_TERMINAL_INPUT*/, // 0x0200
72 int main(int argc, char *argv[]) {
73 std::vector<std::string> args;
74 for (size_t i = 1; i < argc; ++i) {
75 args.push_back(argv[i]);
78 if (args.empty() || args.size() == 1 && args[0] == "info") {
79 DWORD mode = getConsoleMode();
80 printf("mode: 0x%lx\n", mode);
81 for (const auto &flag : kInputFlags) {
82 printf("%-29s 0x%04lx %s\n", flag.name, flag.value, flag.value & mode ? "ON" : "off");
85 for (int i = 0; i < 32; ++i) {
86 if (mode & (1u << i)) {
87 printf("Unrecognized flag: %04x\n", (1u << i));
93 const auto verb = args[0];
96 if (args.size() == 2) {
97 const DWORD newMode = parseInt(args[1]);
98 setConsoleMode(newMode);
99 } else if (args.size() == 3) {
100 const DWORD mode = parseInt(args[1]);
101 const DWORD mask = parseInt(args[2]);
102 const int newMode = (getConsoleMode() & ~mask) | (mode & mask);
103 setConsoleMode(newMode);
107 } else if (verb == "get") {
108 if (args.size() != 1) {
111 printf("0x%lx\n", getConsoleMode());