installed pty
[VSoRC/.git] / node_modules / node-pty / deps / winpty / src / agent / main.cc
1 // Copyright (c) 2011-2015 Ryan Prichard
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 // IN THE SOFTWARE.
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <wchar.h>
25
26 #include "../shared/StringUtil.h"
27 #include "../shared/WindowsVersion.h"
28 #include "../shared/WinptyAssert.h"
29 #include "../shared/WinptyVersion.h"
30
31 #include "Agent.h"
32 #include "AgentCreateDesktop.h"
33 #include "DebugShowInput.h"
34
35 const char USAGE[] =
36 "Usage: %ls controlPipeName flags mouseMode cols rows\n"
37 "Usage: %ls controlPipeName --create-desktop\n"
38 "\n"
39 "Ordinarily, this program is launched by winpty.dll and is not directly\n"
40 "useful to winpty users.  However, it also has options intended for\n"
41 "debugging winpty.\n"
42 "\n"
43 "Usage: %ls [options]\n"
44 "\n"
45 "Options:\n"
46 "  --show-input [--with-mouse] [--escape-input]\n"
47 "                   Dump INPUT_RECORDs from the console input buffer\n"
48 "                   --with-mouse: Include MOUSE_INPUT_RECORDs in the dump\n"
49 "                       output\n"
50 "                   --escape-input: Direct the new Windows 10 console to use\n"
51 "                       escape sequences for input\n"
52 "  --version        Print the winpty version\n";
53
54 static uint64_t winpty_atoi64(const char *str) {
55     return strtoll(str, NULL, 10);
56 }
57
58 int main() {
59     dumpWindowsVersion();
60     dumpVersionToTrace();
61
62     // Technically, we should free the CommandLineToArgvW return value using
63     // a single call to LocalFree, but the call will never actually happen in
64     // the normal case.
65     int argc = 0;
66     wchar_t *cmdline = GetCommandLineW();
67     ASSERT(cmdline != nullptr && "GetCommandLineW returned NULL");
68     wchar_t **argv = CommandLineToArgvW(cmdline, &argc);
69     ASSERT(argv != nullptr && "CommandLineToArgvW returned NULL");
70
71     if (argc == 2 && !wcscmp(argv[1], L"--version")) {
72         dumpVersionToStdout();
73         return 0;
74     }
75
76     if (argc >= 2 && !wcscmp(argv[1], L"--show-input")) {
77         bool withMouse = false;
78         bool escapeInput = false;
79         for (int i = 2; i < argc; ++i) {
80             if (!wcscmp(argv[i], L"--with-mouse")) {
81                 withMouse = true;
82             } else if (!wcscmp(argv[i], L"--escape-input")) {
83                 escapeInput = true;
84             } else {
85                 fprintf(stderr, "Unrecognized --show-input option: %ls\n",
86                     argv[i]);
87                 return 1;
88             }
89         }
90         debugShowInput(withMouse, escapeInput);
91         return 0;
92     }
93
94     if (argc == 3 && !wcscmp(argv[2], L"--create-desktop")) {
95         handleCreateDesktop(argv[1]);
96         return 0;
97     }
98
99     if (argc != 6) {
100         fprintf(stderr, USAGE, argv[0], argv[0], argv[0]);
101         return 1;
102     }
103
104     Agent agent(argv[1],
105                 winpty_atoi64(utf8FromWide(argv[2]).c_str()),
106                 atoi(utf8FromWide(argv[3]).c_str()),
107                 atoi(utf8FromWide(argv[4]).c_str()),
108                 atoi(utf8FromWide(argv[5]).c_str()));
109     agent.run();
110
111     // The Agent destructor shouldn't return, but if it does, exit
112     // unsuccessfully.
113     return 1;
114 }