2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
12 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
15 void fatalbox (char *p, ...) {
17 fprintf(stderr, "FATAL ERROR: ", p);
19 vfprintf(stderr, p, ap);
25 void connection_fatal (char *p, ...) {
27 fprintf(stderr, "FATAL ERROR: ", p);
29 vfprintf(stderr, p, ap);
37 DWORD orig_console_mode;
39 void begin_session(void) {
41 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
43 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), orig_console_mode);
51 while (reap < inbuf_head) {
52 if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL))
53 return; /* give up in panic */
65 static int get_password(const char *prompt, char *str, int maxlen)
70 #if 0 /* this allows specifying a password some other way */
72 static int tried_once = 0;
77 strncpy(str, password, maxlen);
85 hin = GetStdHandle(STD_INPUT_HANDLE);
86 hout = GetStdHandle(STD_OUTPUT_HANDLE);
87 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
88 fprintf(stderr, "Cannot get standard input/output handles");
92 GetConsoleMode(hin, &savemode);
93 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
94 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
96 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
97 ReadFile(hin, str, maxlen-1, &i, NULL);
99 SetConsoleMode(hin, savemode);
101 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
104 WriteFile(hout, "\r\n", 2, &i, NULL);
109 int WINAPI stdin_read_thread(void *param) {
110 struct input_data *idata = (struct input_data *)param;
113 inhandle = GetStdHandle(STD_INPUT_HANDLE);
115 while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
116 &idata->len, NULL)) {
117 SetEvent(idata->event);
121 SetEvent(idata->event);
126 int main(int argc, char **argv) {
129 WSAEVENT netevent, stdinevent;
133 struct input_data idata;
136 ssh_get_password = get_password;
140 * Process the command line.
142 default_protocol = DEFAULT_PROTOCOL;
143 default_port = DEFAULT_PORT;
148 if (!strcmp(p, "-ssh")) {
149 default_protocol = cfg.protocol = PROT_SSH;
150 default_port = cfg.port = 22;
151 } else if (!strcmp(p, "-v")) {
152 flags |= FLAG_VERBOSE;
153 } else if (!strcmp(p, "-log")) {
154 logfile = "putty.log";
160 * If the hostname starts with "telnet:", set the
161 * protocol to Telnet and process the string as a
164 if (!strncmp(q, "telnet:", 7)) {
168 if (q[0] == '/' && q[1] == '/')
170 cfg.protocol = PROT_TELNET;
172 while (*p && *p != ':' && *p != '/') p++;
180 strncpy (cfg.host, q, sizeof(cfg.host)-1);
181 cfg.host[sizeof(cfg.host)-1] = '\0';
184 * Three cases. Either (a) there's a nonzero
185 * length string followed by an @, in which
186 * case that's user and the remainder is host.
187 * Or (b) there's only one string, not counting
188 * a potential initial @, and it exists in the
189 * saved-sessions database. Or (c) only one
190 * string and it _doesn't_ exist in the
193 char *r = strrchr(p, '@');
194 if (r == p) p++, r = NULL; /* discount initial @ */
200 if (cfg.host[0] == '\0') {
201 /* No settings for this host; use defaults */
202 strncpy(cfg.host, p, sizeof(cfg.host)-1);
203 cfg.host[sizeof(cfg.host)-1] = '\0';
208 strncpy(cfg.username, p, sizeof(cfg.username)-1);
209 cfg.username[sizeof(cfg.username)-1] = '\0';
210 strncpy(cfg.host, r, sizeof(cfg.host)-1);
211 cfg.host[sizeof(cfg.host)-1] = '\0';
216 int len = sizeof(cfg.remote_cmd) - 1;
217 char *cp = cfg.remote_cmd;
220 strncpy(cp, p, len); cp[len] = '\0';
221 len2 = strlen(cp); len -= len2; cp += len2;
225 strncpy(cp, *++argv, len); cp[len] = '\0';
226 len2 = strlen(cp); len -= len2; cp += len2;
228 cfg.nopty = TRUE; /* command => no terminal */
229 cfg.ldisc_term = TRUE; /* use stdin like a line buffer */
230 break; /* done with cmdline */
235 if (!*cfg.remote_cmd)
236 flags |= FLAG_INTERACTIVE;
239 * Select protocol. This is farmed out into a table in a
240 * separate file to enable an ssh-free variant.
245 for (i = 0; backends[i].backend != NULL; i++)
246 if (backends[i].protocol == cfg.protocol) {
247 back = backends[i].backend;
251 fprintf(stderr, "Internal fault: Unsupported protocol found\n");
257 * Initialise WinSock.
259 winsock_ver = MAKEWORD(2, 0);
260 if (WSAStartup(winsock_ver, &wsadata)) {
261 MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
262 MB_OK | MB_ICONEXCLAMATION);
265 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
266 MessageBox(NULL, "WinSock version is incompatible with 2.0",
267 "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
273 * Start up the connection.
279 error = back->init (NULL, cfg.host, cfg.port, &realhost);
281 fprintf(stderr, "Unable to open connection:\n%s", error);
286 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
287 stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
289 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &orig_console_mode);
290 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
291 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
294 * Now we must send the back end oodles of stuff.
296 socket = back->socket();
298 * Turn off ECHO and LINE input modes. We don't care if this
299 * call fails, because we know we aren't necessarily running in
302 WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE);
303 handles[0] = netevent;
304 handles[1] = stdinevent;
308 n = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
310 WSANETWORKEVENTS things;
311 if (!WSAEnumNetworkEvents(socket, netevent, &things)) {
312 if (things.lNetworkEvents & FD_READ)
313 back->msg(0, FD_READ);
314 if (things.lNetworkEvents & FD_CLOSE) {
315 back->msg(0, FD_CLOSE);
320 if (!sending && back->sendok()) {
322 * Create a separate thread to read from stdin.
323 * This is a total pain, but I can't find another
326 * - an overlapped ReadFile or ReadFileEx just
327 * doesn't happen; we get failure from
328 * ReadFileEx, and ReadFile blocks despite being
329 * given an OVERLAPPED structure. Perhaps we
330 * can't do overlapped reads on consoles. WHY
333 * - WaitForMultipleObjects(netevent, console)
334 * doesn't work, because it signals the console
335 * when _anything_ happens, including mouse
336 * motions and other things that don't cause
337 * data to be readable - so we're back to
340 idata.event = stdinevent;
341 if (!CreateThread(NULL, 0, stdin_read_thread,
342 &idata, 0, &threadid)) {
343 fprintf(stderr, "Unable to create second thread\n");
350 back->send(idata.buffer, idata.len);
352 back->special(TS_EOF);