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);
42 while (reap < inbuf_head) {
43 if (!WriteFile(outhandle, inbuf+reap, inbuf_head-reap, &ret, NULL))
44 return; /* give up in panic */
56 static int get_password(const char *prompt, char *str, int maxlen)
61 #if 0 /* this allows specifying a password some other way */
63 static int tried_once = 0;
68 strncpy(str, password, maxlen);
76 hin = GetStdHandle(STD_INPUT_HANDLE);
77 hout = GetStdHandle(STD_OUTPUT_HANDLE);
78 if (hin == INVALID_HANDLE_VALUE || hout == INVALID_HANDLE_VALUE) {
79 fprintf(stderr, "Cannot get standard input/output handles");
83 GetConsoleMode(hin, &savemode);
84 SetConsoleMode(hin, (savemode & (~ENABLE_ECHO_INPUT)) |
85 ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT);
87 WriteFile(hout, prompt, strlen(prompt), &i, NULL);
88 ReadFile(hin, str, maxlen-1, &i, NULL);
90 SetConsoleMode(hin, savemode);
92 if ((int)i > maxlen) i = maxlen-1; else i = i - 2;
95 WriteFile(hout, "\r\n", 2, &i, NULL);
100 int WINAPI stdin_read_thread(void *param) {
101 struct input_data *idata = (struct input_data *)param;
104 inhandle = GetStdHandle(STD_INPUT_HANDLE);
106 while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
107 &idata->len, NULL)) {
108 SetEvent(idata->event);
112 SetEvent(idata->event);
117 int main(int argc, char **argv) {
120 WSAEVENT netevent, stdinevent;
124 struct input_data idata;
127 ssh_get_password = get_password;
131 * Process the command line.
133 default_protocol = DEFAULT_PROTOCOL;
134 default_port = DEFAULT_PORT;
139 if (!strcmp(p, "-ssh")) {
140 default_protocol = cfg.protocol = PROT_SSH;
141 default_port = cfg.port = 22;
142 } else if (!strcmp(p, "-v")) {
143 flags |= FLAG_VERBOSE;
144 } else if (!strcmp(p, "-log")) {
145 logfile = "putty.log";
151 * If the hostname starts with "telnet:", set the
152 * protocol to Telnet and process the string as a
155 if (!strncmp(q, "telnet:", 7)) {
159 if (q[0] == '/' && q[1] == '/')
161 cfg.protocol = PROT_TELNET;
163 while (*p && *p != ':' && *p != '/') p++;
171 strncpy (cfg.host, q, sizeof(cfg.host)-1);
172 cfg.host[sizeof(cfg.host)-1] = '\0';
175 * Three cases. Either (a) there's a nonzero
176 * length string followed by an @, in which
177 * case that's user and the remainder is host.
178 * Or (b) there's only one string, not counting
179 * a potential initial @, and it exists in the
180 * saved-sessions database. Or (c) only one
181 * string and it _doesn't_ exist in the
184 char *r = strrchr(p, '@');
185 if (r == p) p++, r = NULL; /* discount initial @ */
191 if (cfg.host[0] == '\0') {
192 /* No settings for this host; use defaults */
193 strncpy(cfg.host, p, sizeof(cfg.host)-1);
194 cfg.host[sizeof(cfg.host)-1] = '\0';
199 strncpy(cfg.username, p, sizeof(cfg.username)-1);
200 cfg.username[sizeof(cfg.username)-1] = '\0';
201 strncpy(cfg.host, r, sizeof(cfg.host)-1);
202 cfg.host[sizeof(cfg.host)-1] = '\0';
207 int len = sizeof(cfg.remote_cmd) - 1;
208 char *cp = cfg.remote_cmd;
211 strncpy(cp, p, len); cp[len] = '\0';
212 len2 = strlen(cp); len -= len2; cp += len2;
216 strncpy(cp, *++argv, len); cp[len] = '\0';
217 len2 = strlen(cp); len -= len2; cp += len2;
219 cfg.nopty = TRUE; /* command => no terminal */
220 cfg.ldisc_term = TRUE; /* use stdin like a line buffer */
221 break; /* done with cmdline */
226 if (!*cfg.remote_cmd)
227 flags |= FLAG_INTERACTIVE;
230 * Select protocol. This is farmed out into a table in a
231 * separate file to enable an ssh-free variant.
236 for (i = 0; backends[i].backend != NULL; i++)
237 if (backends[i].protocol == cfg.protocol) {
238 back = backends[i].backend;
242 fprintf(stderr, "Internal fault: Unsupported protocol found\n");
248 * Initialise WinSock.
250 winsock_ver = MAKEWORD(2, 0);
251 if (WSAStartup(winsock_ver, &wsadata)) {
252 MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
253 MB_OK | MB_ICONEXCLAMATION);
256 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
257 MessageBox(NULL, "WinSock version is incompatible with 2.0",
258 "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
264 * Start up the connection.
270 error = back->init (NULL, cfg.host, cfg.port, &realhost);
272 fprintf(stderr, "Unable to open connection:\n%s", error);
277 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
278 stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
281 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT);
282 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
285 * Now we must send the back end oodles of stuff.
287 socket = back->socket();
289 * Turn off ECHO and LINE input modes. We don't care if this
290 * call fails, because we know we aren't necessarily running in
293 WSAEventSelect(socket, netevent, FD_READ | FD_CLOSE);
294 handles[0] = netevent;
295 handles[1] = stdinevent;
299 n = WaitForMultipleObjects(2, handles, FALSE, INFINITE);
301 WSANETWORKEVENTS things;
302 if (!WSAEnumNetworkEvents(socket, netevent, &things)) {
303 if (things.lNetworkEvents & FD_READ)
304 back->msg(0, FD_READ);
305 if (things.lNetworkEvents & FD_CLOSE) {
306 back->msg(0, FD_CLOSE);
311 if (!sending && back->sendok()) {
313 * Create a separate thread to read from stdin.
314 * This is a total pain, but I can't find another
317 * - an overlapped ReadFile or ReadFileEx just
318 * doesn't happen; we get failure from
319 * ReadFileEx, and ReadFile blocks despite being
320 * given an OVERLAPPED structure. Perhaps we
321 * can't do overlapped reads on consoles. WHY
324 * - WaitForMultipleObjects(netevent, console)
325 * doesn't work, because it signals the console
326 * when _anything_ happens, including mouse
327 * motions and other things that don't cause
328 * data to be readable - so we're back to
331 idata.event = stdinevent;
332 if (!CreateThread(NULL, 0, stdin_read_thread,
333 &idata, 0, &threadid)) {
334 fprintf(stderr, "Unable to create second thread\n");
341 back->send(idata.buffer, idata.len);
343 back->special(TS_EOF);