| 1 |
#include <windows.h> |
| 2 |
|
| 3 |
#include <errno.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <stdlib.h> |
| 6 |
|
| 7 |
#include <string> |
| 8 |
#include <vector> |
| 9 |
|
| 10 |
static HANDLE getConout() { |
| 11 |
HANDLE conout = GetStdHandle(STD_OUTPUT_HANDLE); |
| 12 |
if (conout == INVALID_HANDLE_VALUE) { |
| 13 |
fprintf(stderr, "error: cannot get stdout\n"); |
| 14 |
exit(1); |
| 15 |
} |
| 16 |
return conout; |
| 17 |
} |
| 18 |
|
| 19 |
static DWORD getConsoleMode() { |
| 20 |
DWORD mode = 0; |
| 21 |
if (!GetConsoleMode(getConout(), &mode)) { |
| 22 |
fprintf(stderr, "error: GetConsoleMode failed (is stdout a console?)\n"); |
| 23 |
exit(1); |
| 24 |
} |
| 25 |
return mode; |
| 26 |
} |
| 27 |
|
| 28 |
static void setConsoleMode(DWORD mode) { |
| 29 |
if (!SetConsoleMode(getConout(), mode)) { |
| 30 |
fprintf(stderr, "error: SetConsoleMode failed (is stdout a console?)\n"); |
| 31 |
exit(1); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
static long parseInt(const std::string &s) { |
| 36 |
errno = 0; |
| 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()); |
| 41 |
exit(1); |
| 42 |
} |
| 43 |
return result; |
| 44 |
} |
| 45 |
|
| 46 |
static void usage() { |
| 47 |
printf("Usage: ConoutMode [verb] [options]\n"); |
| 48 |
printf("Verbs:\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"); |
| 54 |
exit(1); |
| 55 |
} |
| 56 |
|
| 57 |
struct { |
| 58 |
const char *name; |
| 59 |
DWORD value; |
| 60 |
} kOutputFlags[] = { |
| 61 |
"ENABLE_PROCESSED_OUTPUT", ENABLE_PROCESSED_OUTPUT, // 0x0001 |
| 62 |
"ENABLE_WRAP_AT_EOL_OUTPUT", ENABLE_WRAP_AT_EOL_OUTPUT, // 0x0002 |
| 63 |
"ENABLE_VIRTUAL_TERMINAL_PROCESSING", 0x0004/*ENABLE_VIRTUAL_TERMINAL_PROCESSING*/, // 0x0004 |
| 64 |
"DISABLE_NEWLINE_AUTO_RETURN", 0x0008/*DISABLE_NEWLINE_AUTO_RETURN*/, // 0x0008 |
| 65 |
"ENABLE_LVB_GRID_WORLDWIDE", 0x0010/*ENABLE_LVB_GRID_WORLDWIDE*/, //0x0010 |
| 66 |
}; |
| 67 |
|
| 68 |
int main(int argc, char *argv[]) { |
| 69 |
std::vector<std::string> args; |
| 70 |
for (size_t i = 1; i < argc; ++i) { |
| 71 |
args.push_back(argv[i]); |
| 72 |
} |
| 73 |
|
| 74 |
if (args.empty() || args.size() == 1 && args[0] == "info") { |
| 75 |
DWORD mode = getConsoleMode(); |
| 76 |
printf("mode: 0x%lx\n", mode); |
| 77 |
for (const auto &flag : kOutputFlags) { |
| 78 |
printf("%-34s 0x%04lx %s\n", flag.name, flag.value, flag.value & mode ? "ON" : "off"); |
| 79 |
mode &= ~flag.value; |
| 80 |
} |
| 81 |
for (int i = 0; i < 32; ++i) { |
| 82 |
if (mode & (1u << i)) { |
| 83 |
printf("Unrecognized flag: %04x\n", (1u << i)); |
| 84 |
} |
| 85 |
} |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
const auto verb = args[0]; |
| 90 |
|
| 91 |
if (verb == "set") { |
| 92 |
if (args.size() == 2) { |
| 93 |
const DWORD newMode = parseInt(args[1]); |
| 94 |
setConsoleMode(newMode); |
| 95 |
} else if (args.size() == 3) { |
| 96 |
const DWORD mode = parseInt(args[1]); |
| 97 |
const DWORD mask = parseInt(args[2]); |
| 98 |
const int newMode = (getConsoleMode() & ~mask) | (mode & mask); |
| 99 |
setConsoleMode(newMode); |
| 100 |
} else { |
| 101 |
usage(); |
| 102 |
} |
| 103 |
} else if (verb == "get") { |
| 104 |
if (args.size() != 1) { |
| 105 |
usage(); |
| 106 |
} |
| 107 |
printf("0x%lx\n", getConsoleMode()); |
| 108 |
} else { |
| 109 |
usage(); |
| 110 |
} |
| 111 |
|
| 112 |
return 0; |
| 113 |
} |