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");
19 static DWORD getConsoleMode() {
21 if (!GetConsoleMode(getConout(), &mode)) {
22 fprintf(stderr, "error: GetConsoleMode failed (is stdout a console?)\n");
28 static void setConsoleMode(DWORD mode) {
29 if (!SetConsoleMode(getConout(), mode)) {
30 fprintf(stderr, "error: SetConsoleMode failed (is stdout 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: ConoutMode [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_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
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]);
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");
81 for (int i = 0; i < 32; ++i) {
82 if (mode & (1u << i)) {
83 printf("Unrecognized flag: %04x\n", (1u << i));
89 const auto verb = args[0];
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);
103 } else if (verb == "get") {
104 if (args.size() != 1) {
107 printf("0x%lx\n", getConsoleMode());