2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
10 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
13 void fatalbox (char *p, ...) {
15 fprintf(stderr, "FATAL ERROR: ", p);
17 vfprintf(stderr, p, ap);
23 void connection_fatal (char *p, ...) {
25 fprintf(stderr, "FATAL ERROR: ", p);
27 vfprintf(stderr, p, ap);
35 DWORD orig_console_mode;
37 void begin_session(void) {
39 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
41 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), orig_console_mode);
49 while (reap < inbuf_head) {
50 if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL))
51 return; /* give up in panic */
63 static int get_password(const char *prompt, char *str, int maxlen)
68 #if 0 /* this allows specifying a password some other way */
70 static int tried_once = 0;
75 strncpy(str, password, maxlen);
83 hin = GetStdHandle(STD_INPUT_HANDLE);
84 hout = GetStdHandle(STD_OUTPUT_HANDLE);
85 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
86 fprintf(stderr, "Cannot get standard input/output handles");
90 GetConsoleMode(hin, &savemode);
91 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
92 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
94 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
95 ReadFile(hin, str, maxlen-1, &i, NULL);
97 SetConsoleMode(hin, savemode);
99 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
102 WriteFile(hout, "\r\n", 2, &i, NULL);
107 int WINAPI stdin_read_thread(void *param) {
108 struct input_data *idata = (struct input_data *)param;
111 inhandle = GetStdHandle(STD_INPUT_HANDLE);
113 while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
114 &idata->len, NULL)) {
115 SetEvent(idata->event);
119 SetEvent(idata->event);
124 int main(int argc, char **argv) {
127 WSAEVENT netevent, stdinevent;
131 struct input_data idata;
134 ssh_get_password = get_password;
138 * Process the command line.
140 default_protocol = DEFAULT_PROTOCOL;
141 default_port = DEFAULT_PORT;
146 if (!strcmp(p, "-ssh")) {
147 default_protocol = cfg.protocol = PROT_SSH;
148 default_port = cfg.port = 22;
149 } else if (!strcmp(p, "-v")) {
150 flags |= FLAG_VERBOSE;
151 } else if (!strcmp(p, "-log")) {
152 logfile = "putty.log";
158 * If the hostname starts with "telnet:", set the
159 * protocol to Telnet and process the string as a
162 if (!strncmp(q, "telnet:", 7)) {
166 if (q[0] == '/' && q[1] == '/')
168 cfg.protocol = PROT_TELNET;
170 while (*p && *p != ':' && *p != '/') p++;
178 strncpy (cfg.host, q, sizeof(cfg.host)-1);
179 cfg.host[sizeof(cfg.host)-1] = '\0';
182 * Three cases. Either (a) there's a nonzero
183 * length string followed by an @, in which
184 * case that's user and the remainder is host.
185 * Or (b) there's only one string, not counting
186 * a potential initial @, and it exists in the
187 * saved-sessions database. Or (c) only one
188 * string and it _doesn't_ exist in the
191 char *r = strrchr(p, '@');
192 if (r == p) p++, r = NULL; /* discount initial @ */
198 if (cfg.host[0] == '\0') {
199 /* No settings for this host; use defaults */
200 strncpy(cfg.host, p, sizeof(cfg.host)-1);
201 cfg.host[sizeof(cfg.host)-1] = '\0';
206 strncpy(cfg.username, p, sizeof(cfg.username)-1);
207 cfg.username[sizeof(cfg.username)-1] = '\0';
208 strncpy(cfg.host, r, sizeof(cfg.host)-1);
209 cfg.host[sizeof(cfg.host)-1] = '\0';
214 int len = sizeof(cfg.remote_cmd) - 1;
215 char *cp = cfg.remote_cmd;
218 strncpy(cp, p, len); cp[len] = '\0';
219 len2 = strlen(cp); len -= len2; cp += len2;
223 strncpy(cp, *++argv, len); cp[len] = '\0';
224 len2 = strlen(cp); len -= len2; cp += len2;
226 cfg.nopty = TRUE; /* command => no terminal */
227 cfg.ldisc_term = TRUE; /* use stdin like a line buffer */
228 break; /* done with cmdline */
233 if (!*cfg.remote_cmd)
234 flags |= FLAG_INTERACTIVE;
237 * Select protocol. This is farmed out into a table in a
238 * separate file to enable an ssh-free variant.
243 for (i = 0; backends[i].backend != NULL; i++)
244 if (backends[i].protocol == cfg.protocol) {
245 back = backends[i].backend;
249 fprintf(stderr, "Internal fault: Unsupported protocol found\n");
255 * Initialise WinSock.
257 winsock_ver = MAKEWORD(2, 0);
258 if (WSAStartup(winsock_ver, &wsadata)) {
259 MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
260 MB_OK | MB_ICONEXCLAMATION);
263 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
264 MessageBox(NULL, "WinSock version is incompatible with 2.0",
265 "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
271 * Start up the connection.
277 error = back->init (NULL, cfg.host, cfg.port, &realhost);
279 fprintf(stderr, "Unable to open connection:\n%s", error);
284 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
285 stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
287 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_console_mode);
288 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
289 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
292 * Now we must send the back end oodles of stuff.
294 socket = back->socket();
296 * Turn off ECHO and LINE input modes. We don't care if this
297 * call fails, because we know we aren't necessarily running in
300 WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE);
301 handles[0] = netevent;
302 handles[1] = stdinevent;
306 n = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
308 WSANETWORKEVENTS things;
309 if (!WSAEnumNetworkEvents(socket, netevent, &things)) {
310 if (things.lNetworkEvents & FD_READ)
311 back->msg(0, FD_READ);
312 if (things.lNetworkEvents & FD_CLOSE) {
313 back->msg(0, FD_CLOSE);
318 if (!sending && back->sendok()) {
320 * Create a separate thread to read from stdin.
321 * This is a total pain, but I can't find another
324 * - an overlapped ReadFile or ReadFileEx just
325 * doesn't happen; we get failure from
326 * ReadFileEx, and ReadFile blocks despite being
327 * given an OVERLAPPED structure. Perhaps we
328 * can't do overlapped reads on consoles. WHY
331 * - WaitForMultipleObjects(netevent, console)
332 * doesn't work, because it signals the console
333 * when _anything_ happens, including mouse
334 * motions and other things that don't cause
335 * data to be readable - so we're back to
338 idata.event = stdinevent;
339 if (!CreateThread(NULL, 0, stdin_read_thread,
340 &idata, 0, &threadid)) {
341 fprintf(stderr, "Unable to create second thread\n");
348 back->send(idata.buffer, idata.len);
350 back->special(TS_EOF);