installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / misc / ConinMode.cc
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 getConin() {
11     HANDLE conin = GetStdHandle(STD_INPUT_HANDLE);
12     if (conin == INVALID_HANDLE_VALUE) {
13         fprintf(stderr, "error: cannot get stdin\n");
14         exit(1);
15     }
16     return conin;
17 }
18
19 static DWORD getConsoleMode() {
20     DWORD mode = 0;
21     if (!GetConsoleMode(getConin(), &mode)) {
22         fprintf(stderr, "error: GetConsoleMode failed (is stdin a console?)\n");
23         exit(1);
24     }
25     return mode;
26 }
27
28 static void setConsoleMode(DWORD mode) {
29     if (!SetConsoleMode(getConin(), mode)) {
30         fprintf(stderr, "error: SetConsoleMode failed (is stdin 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: ConinMode [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 } kInputFlags[] = {
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
70 };
71
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]);
76     }
77
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");
83             mode &= ~flag.value;
84         }
85         for (int i = 0; i < 32; ++i) {
86             if (mode & (1u << i)) {
87                 printf("Unrecognized flag: %04x\n", (1u << i));
88             }
89         }
90         return 0;
91     }
92
93     const auto verb = args[0];
94
95     if (verb == "set") {
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);
104         } else {
105             usage();
106         }
107     } else if (verb == "get") {
108         if (args.size() != 1) {
109             usage();
110         }
111         printf("0x%lx\n", getConsoleMode());
112     } else {
113         usage();
114     }
115
116     return 0;
117 }