Updated usage messages for command-line utilities to reflect new options.
[u/mdw/putty] / plink.c
CommitLineData
12dc4ec0 1/*
2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
3 */
4
4d331a77 5#ifndef AUTO_WINSOCK
12dc4ec0 6#include <winsock2.h>
4d331a77 7#endif
12dc4ec0 8#include <windows.h>
9#include <stdio.h>
49bad831 10#include <stdlib.h>
2b0c045b 11#include <assert.h>
12dc4ec0 12#include <stdarg.h>
13
32874aea 14#define PUTTY_DO_GLOBALS /* actually _define_ globals */
12dc4ec0 15#include "putty.h"
a9422f39 16#include "storage.h"
8df7a775 17#include "tree234.h"
12dc4ec0 18
5471d09a 19#define MAX_STDIN_BACKLOG 4096
20
32874aea 21void fatalbox(char *p, ...)
22{
12dc4ec0 23 va_list ap;
49bad831 24 fprintf(stderr, "FATAL ERROR: ");
12dc4ec0 25 va_start(ap, p);
26 vfprintf(stderr, p, ap);
27 va_end(ap);
28 fputc('\n', stderr);
29 WSACleanup();
93b581bd 30 cleanup_exit(1);
12dc4ec0 31}
32874aea 32void connection_fatal(char *p, ...)
33{
8d5de777 34 va_list ap;
49bad831 35 fprintf(stderr, "FATAL ERROR: ");
8d5de777 36 va_start(ap, p);
37 vfprintf(stderr, p, ap);
38 va_end(ap);
39 fputc('\n', stderr);
40 WSACleanup();
93b581bd 41 cleanup_exit(1);
8d5de777 42}
c0a81592 43void cmdline_error(char *p, ...)
44{
45 va_list ap;
46 fprintf(stderr, "plink: ");
47 va_start(ap, p);
48 vfprintf(stderr, p, ap);
49 va_end(ap);
50 fputc('\n', stderr);
51 exit(1);
52}
12dc4ec0 53
d8426c54 54static char *password = NULL;
55
0965bee0 56HANDLE inhandle, outhandle, errhandle;
6f34e365 57DWORD orig_console_mode;
58
8df7a775 59WSAEVENT netevent;
60
32874aea 61int term_ldisc(int mode)
62{
63 return FALSE;
64}
65void ldisc_update(int echo, int edit)
66{
0965bee0 67 /* Update stdin read mode to reflect changes in line discipline. */
68 DWORD mode;
69
70 mode = ENABLE_PROCESSED_INPUT;
71 if (echo)
32874aea 72 mode = mode | ENABLE_ECHO_INPUT;
0965bee0 73 else
32874aea 74 mode = mode & ~ENABLE_ECHO_INPUT;
0965bee0 75 if (edit)
32874aea 76 mode = mode | ENABLE_LINE_INPUT;
0965bee0 77 else
32874aea 78 mode = mode & ~ENABLE_LINE_INPUT;
0965bee0 79 SetConsoleMode(inhandle, mode);
80}
81
5471d09a 82struct input_data {
83 DWORD len;
84 char buffer[4096];
85 HANDLE event, eventback;
86};
87
32874aea 88static DWORD WINAPI stdin_read_thread(void *param)
89{
90 struct input_data *idata = (struct input_data *) param;
12dc4ec0 91 HANDLE inhandle;
92
93 inhandle = GetStdHandle(STD_INPUT_HANDLE);
94
95 while (ReadFile(inhandle, idata->buffer, sizeof(idata->buffer),
32874aea 96 &idata->len, NULL) && idata->len > 0) {
97 SetEvent(idata->event);
98 WaitForSingleObject(idata->eventback, INFINITE);
12dc4ec0 99 }
100
101 idata->len = 0;
102 SetEvent(idata->event);
103
104 return 0;
105}
106
5471d09a 107struct output_data {
108 DWORD len, lenwritten;
109 int writeret;
110 char *buffer;
111 int is_stderr, done;
112 HANDLE event, eventback;
113 int busy;
114};
115
116static DWORD WINAPI stdout_write_thread(void *param)
117{
118 struct output_data *odata = (struct output_data *) param;
119 HANDLE outhandle, errhandle;
120
121 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
122 errhandle = GetStdHandle(STD_ERROR_HANDLE);
123
124 while (1) {
125 WaitForSingleObject(odata->eventback, INFINITE);
126 if (odata->done)
127 break;
128 odata->writeret =
129 WriteFile(odata->is_stderr ? errhandle : outhandle,
130 odata->buffer, odata->len, &odata->lenwritten, NULL);
131 SetEvent(odata->event);
132 }
133
134 return 0;
135}
136
137bufchain stdout_data, stderr_data;
138struct output_data odata, edata;
139
140void try_output(int is_stderr)
141{
142 struct output_data *data = (is_stderr ? &edata : &odata);
143 void *senddata;
144 int sendlen;
145
146 if (!data->busy) {
147 bufchain_prefix(is_stderr ? &stderr_data : &stdout_data,
148 &senddata, &sendlen);
149 data->buffer = senddata;
150 data->len = sendlen;
151 SetEvent(data->eventback);
152 data->busy = 1;
153 }
154}
155
156int from_backend(int is_stderr, char *data, int len)
157{
5471d09a 158 HANDLE h = (is_stderr ? errhandle : outhandle);
5471d09a 159 int osize, esize;
160
2b0c045b 161 assert(len > 0);
162
5471d09a 163 if (is_stderr) {
164 bufchain_add(&stderr_data, data, len);
165 try_output(1);
166 } else {
167 bufchain_add(&stdout_data, data, len);
168 try_output(0);
169 }
170
171 osize = bufchain_size(&stdout_data);
172 esize = bufchain_size(&stderr_data);
173
174 return osize + esize;
175}
176
d8426c54 177/*
178 * Short description of parameters.
179 */
180static void usage(void)
181{
182 printf("PuTTY Link: command-line connection utility\n");
183 printf("%s\n", ver);
184 printf("Usage: plink [options] [user@]host [command]\n");
e672967c 185 printf(" (\"host\" can also be a PuTTY saved session name)\n");
d8426c54 186 printf("Options:\n");
187 printf(" -v show verbose messages\n");
e2a197cf 188 printf(" -load sessname Load settings from saved session\n");
189 printf(" -ssh -telnet -rlogin -raw\n");
190 printf(" force use of a particular protocol (default SSH)\n");
d8426c54 191 printf(" -P port connect to specified port\n");
e2a197cf 192 printf(" -l user connect with specified username\n");
96621a84 193 printf(" -m file read remote command(s) from file\n");
e2a197cf 194 printf(" -batch disable all interactive prompts\n");
195 printf("The following options only apply to SSH connections:\n");
196 printf(" -pw passw login with specified password\n");
e7aabca4 197 printf(" -L listen-port:host:port Forward local port to "
198 "remote address\n");
199 printf(" -R listen-port:host:port Forward remote port to"
200 " local address\n");
e2a197cf 201 printf(" -X -x enable / disable X11 forwarding\n");
202 printf(" -A -a enable / disable agent forwarding\n");
203 printf(" -t -T enable / disable pty allocation\n");
204 printf(" -1 -2 force use of particular protocol version\n");
205 printf(" -C enable compression\n");
206 printf(" -i key private key file for authentication\n");
d8426c54 207 exit(1);
208}
209
32874aea 210char *do_select(SOCKET skt, int startup)
211{
8df7a775 212 int events;
213 if (startup) {
3ad9d396 214 events = (FD_CONNECT | FD_READ | FD_WRITE |
215 FD_OOB | FD_CLOSE | FD_ACCEPT);
8df7a775 216 } else {
217 events = 0;
218 }
32874aea 219 if (WSAEventSelect(skt, netevent, events) == SOCKET_ERROR) {
220 switch (WSAGetLastError()) {
221 case WSAENETDOWN:
222 return "Network is down";
223 default:
224 return "WSAAsyncSelect(): unknown error";
225 }
8df7a775 226 }
227 return NULL;
228}
229
32874aea 230int main(int argc, char **argv)
231{
12dc4ec0 232 WSADATA wsadata;
233 WORD winsock_ver;
5471d09a 234 WSAEVENT stdinevent, stdoutevent, stderrevent;
235 HANDLE handles[4];
236 DWORD in_threadid, out_threadid, err_threadid;
12dc4ec0 237 struct input_data idata;
5471d09a 238 int reading;
12dc4ec0 239 int sending;
d8426c54 240 int portnumber = -1;
8df7a775 241 SOCKET *sklist;
242 int skcount, sksize;
243 int connopen;
d8d6c7e5 244 int exitcode;
12dc4ec0 245
ff2ae367 246 ssh_get_line = console_get_line;
67779be7 247
32874aea 248 sklist = NULL;
249 skcount = sksize = 0;
c9bdcd96 250 /*
251 * Initialise port and protocol to sensible defaults. (These
252 * will be overridden by more or less anything.)
253 */
254 default_protocol = PROT_SSH;
255 default_port = 22;
8df7a775 256
67779be7 257 flags = FLAG_STDERR;
12dc4ec0 258 /*
259 * Process the command line.
260 */
a9422f39 261 do_defaults(NULL, &cfg);
e7a7383f 262 default_protocol = cfg.protocol;
263 default_port = cfg.port;
8cb9c947 264 {
32874aea 265 /*
266 * Override the default protocol if PLINK_PROTOCOL is set.
267 */
268 char *p = getenv("PLINK_PROTOCOL");
269 int i;
270 if (p) {
271 for (i = 0; backends[i].backend != NULL; i++) {
272 if (!strcmp(backends[i].name, p)) {
273 default_protocol = cfg.protocol = backends[i].protocol;
274 default_port = cfg.port =
275 backends[i].backend->default_port;
276 break;
277 }
278 }
279 }
8cb9c947 280 }
12dc4ec0 281 while (--argc) {
32874aea 282 char *p = *++argv;
283 if (*p == '-') {
c0a81592 284 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), 1);
285 if (ret == -2) {
286 fprintf(stderr,
287 "plink: option \"%s\" requires an argument\n", p);
288 } else if (ret == 2) {
289 --argc, ++argv;
290 } else if (ret == 1) {
291 continue;
ff2ae367 292 } else if (!strcmp(p, "-batch")) {
c0a81592 293 console_batch_mode = 1;
12dc4ec0 294 } else if (!strcmp(p, "-log")) {
32874aea 295 logfile = "putty.log";
32874aea 296 }
12dc4ec0 297 } else if (*p) {
32874aea 298 if (!*cfg.host) {
299 char *q = p;
300 /*
301 * If the hostname starts with "telnet:", set the
302 * protocol to Telnet and process the string as a
303 * Telnet URL.
304 */
305 if (!strncmp(q, "telnet:", 7)) {
306 char c;
307
308 q += 7;
309 if (q[0] == '/' && q[1] == '/')
310 q += 2;
311 cfg.protocol = PROT_TELNET;
312 p = q;
313 while (*p && *p != ':' && *p != '/')
314 p++;
315 c = *p;
316 if (*p)
317 *p++ = '\0';
318 if (c == ':')
319 cfg.port = atoi(p);
320 else
321 cfg.port = -1;
322 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
323 cfg.host[sizeof(cfg.host) - 1] = '\0';
324 } else {
325 char *r;
326 /*
327 * Before we process the [user@]host string, we
328 * first check for the presence of a protocol
329 * prefix (a protocol name followed by ",").
330 */
331 r = strchr(p, ',');
332 if (r) {
333 int i, j;
334 for (i = 0; backends[i].backend != NULL; i++) {
335 j = strlen(backends[i].name);
336 if (j == r - p &&
337 !memcmp(backends[i].name, p, j)) {
338 default_protocol = cfg.protocol =
339 backends[i].protocol;
340 portnumber =
341 backends[i].backend->default_port;
342 p = r + 1;
343 break;
344 }
345 }
346 }
347
348 /*
349 * Three cases. Either (a) there's a nonzero
350 * length string followed by an @, in which
351 * case that's user and the remainder is host.
352 * Or (b) there's only one string, not counting
353 * a potential initial @, and it exists in the
354 * saved-sessions database. Or (c) only one
355 * string and it _doesn't_ exist in the
356 * database.
357 */
358 r = strrchr(p, '@');
359 if (r == p)
360 p++, r = NULL; /* discount initial @ */
361 if (r == NULL) {
362 /*
363 * One string.
364 */
365 Config cfg2;
366 do_defaults(p, &cfg2);
367 if (cfg2.host[0] == '\0') {
368 /* No settings for this host; use defaults */
369 strncpy(cfg.host, p, sizeof(cfg.host) - 1);
370 cfg.host[sizeof(cfg.host) - 1] = '\0';
371 cfg.port = default_port;
372 } else {
373 cfg = cfg2;
374 cfg.remote_cmd_ptr = cfg.remote_cmd;
375 }
376 } else {
377 *r++ = '\0';
378 strncpy(cfg.username, p, sizeof(cfg.username) - 1);
379 cfg.username[sizeof(cfg.username) - 1] = '\0';
380 strncpy(cfg.host, r, sizeof(cfg.host) - 1);
381 cfg.host[sizeof(cfg.host) - 1] = '\0';
382 cfg.port = default_port;
383 }
384 }
385 } else {
385528da 386 char *command;
387 int cmdlen, cmdsize;
388 cmdlen = cmdsize = 0;
389 command = NULL;
390
391 while (argc) {
392 while (*p) {
393 if (cmdlen >= cmdsize) {
394 cmdsize = cmdlen + 512;
395 command = srealloc(command, cmdsize);
396 }
397 command[cmdlen++]=*p++;
398 }
399 if (cmdlen >= cmdsize) {
400 cmdsize = cmdlen + 512;
401 command = srealloc(command, cmdsize);
402 }
403 command[cmdlen++]=' '; /* always add trailing space */
404 if (--argc) p = *++argv;
32874aea 405 }
385528da 406 if (cmdlen) command[--cmdlen]='\0';
407 /* change trailing blank to NUL */
408 cfg.remote_cmd_ptr = command;
409 cfg.remote_cmd_ptr2 = NULL;
32874aea 410 cfg.nopty = TRUE; /* command => no terminal */
385528da 411
32874aea 412 break; /* done with cmdline */
413 }
12dc4ec0 414 }
415 }
416
d8426c54 417 if (!*cfg.host) {
32874aea 418 usage();
d8426c54 419 }
d8426c54 420
449925a6 421 /*
422 * Trim leading whitespace off the hostname if it's there.
423 */
424 {
425 int space = strspn(cfg.host, " \t");
426 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
427 }
428
429 /* See if host is of the form user@host */
430 if (cfg.host[0] != '\0') {
431 char *atsign = strchr(cfg.host, '@');
432 /* Make sure we're not overflowing the user field */
433 if (atsign) {
434 if (atsign - cfg.host < sizeof cfg.username) {
435 strncpy(cfg.username, cfg.host, atsign - cfg.host);
436 cfg.username[atsign - cfg.host] = '\0';
437 }
438 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
439 }
440 }
441
442 /*
c0a81592 443 * Perform command-line overrides on session configuration.
444 */
445 cmdline_run_saved();
446
447 /*
449925a6 448 * Trim a colon suffix off the hostname if it's there.
449 */
450 cfg.host[strcspn(cfg.host, ":")] = '\0';
451
96621a84 452 if (!*cfg.remote_cmd_ptr)
32874aea 453 flags |= FLAG_INTERACTIVE;
67779be7 454
12dc4ec0 455 /*
456 * Select protocol. This is farmed out into a table in a
457 * separate file to enable an ssh-free variant.
458 */
459 {
32874aea 460 int i;
461 back = NULL;
462 for (i = 0; backends[i].backend != NULL; i++)
463 if (backends[i].protocol == cfg.protocol) {
464 back = backends[i].backend;
465 break;
466 }
467 if (back == NULL) {
468 fprintf(stderr,
469 "Internal fault: Unsupported protocol found\n");
470 return 1;
471 }
12dc4ec0 472 }
473
474 /*
8cb9c947 475 * Select port.
476 */
477 if (portnumber != -1)
32874aea 478 cfg.port = portnumber;
8cb9c947 479
480 /*
12dc4ec0 481 * Initialise WinSock.
482 */
483 winsock_ver = MAKEWORD(2, 0);
484 if (WSAStartup(winsock_ver, &wsadata)) {
485 MessageBox(NULL, "Unable to initialise WinSock", "WinSock Error",
486 MB_OK | MB_ICONEXCLAMATION);
487 return 1;
488 }
489 if (LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 0) {
490 MessageBox(NULL, "WinSock version is incompatible with 2.0",
491 "WinSock Error", MB_OK | MB_ICONEXCLAMATION);
492 WSACleanup();
493 return 1;
494 }
8df7a775 495 sk_init();
12dc4ec0 496
497 /*
498 * Start up the connection.
499 */
8df7a775 500 netevent = CreateEvent(NULL, FALSE, FALSE, NULL);
12dc4ec0 501 {
502 char *error;
503 char *realhost;
2184a5d9 504 /* nodelay is only useful if stdin is a character device (console) */
505 int nodelay = cfg.tcp_nodelay &&
506 (GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_CHAR);
12dc4ec0 507
2184a5d9 508 error = back->init(cfg.host, cfg.port, &realhost, nodelay);
12dc4ec0 509 if (error) {
510 fprintf(stderr, "Unable to open connection:\n%s", error);
511 return 1;
512 }
6e1ebb76 513 sfree(realhost);
12dc4ec0 514 }
8df7a775 515 connopen = 1;
12dc4ec0 516
12dc4ec0 517 stdinevent = CreateEvent(NULL, FALSE, FALSE, NULL);
5471d09a 518 stdoutevent = CreateEvent(NULL, FALSE, FALSE, NULL);
519 stderrevent = CreateEvent(NULL, FALSE, FALSE, NULL);
12dc4ec0 520
0965bee0 521 inhandle = GetStdHandle(STD_INPUT_HANDLE);
12dc4ec0 522 outhandle = GetStdHandle(STD_OUTPUT_HANDLE);
fe50e814 523 errhandle = GetStdHandle(STD_ERROR_HANDLE);
0965bee0 524 GetConsoleMode(inhandle, &orig_console_mode);
525 SetConsoleMode(inhandle, ENABLE_PROCESSED_INPUT);
12dc4ec0 526
527 /*
12dc4ec0 528 * Turn off ECHO and LINE input modes. We don't care if this
529 * call fails, because we know we aren't necessarily running in
530 * a console.
531 */
12dc4ec0 532 handles[0] = netevent;
533 handles[1] = stdinevent;
5471d09a 534 handles[2] = stdoutevent;
535 handles[3] = stderrevent;
12dc4ec0 536 sending = FALSE;
5471d09a 537
538 /*
539 * Create spare threads to write to stdout and stderr, so we
540 * can arrange asynchronous writes.
541 */
542 odata.event = stdoutevent;
543 odata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
544 odata.is_stderr = 0;
545 odata.busy = odata.done = 0;
546 if (!CreateThread(NULL, 0, stdout_write_thread,
547 &odata, 0, &out_threadid)) {
548 fprintf(stderr, "Unable to create output thread\n");
93b581bd 549 cleanup_exit(1);
5471d09a 550 }
551 edata.event = stderrevent;
552 edata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
553 edata.is_stderr = 1;
554 edata.busy = edata.done = 0;
555 if (!CreateThread(NULL, 0, stdout_write_thread,
556 &edata, 0, &err_threadid)) {
557 fprintf(stderr, "Unable to create error output thread\n");
93b581bd 558 cleanup_exit(1);
5471d09a 559 }
560
12dc4ec0 561 while (1) {
32874aea 562 int n;
563
564 if (!sending && back->sendok()) {
565 /*
566 * Create a separate thread to read from stdin. This is
567 * a total pain, but I can't find another way to do it:
568 *
569 * - an overlapped ReadFile or ReadFileEx just doesn't
570 * happen; we get failure from ReadFileEx, and
571 * ReadFile blocks despite being given an OVERLAPPED
572 * structure. Perhaps we can't do overlapped reads
573 * on consoles. WHY THE HELL NOT?
574 *
575 * - WaitForMultipleObjects(netevent, console) doesn't
576 * work, because it signals the console when
577 * _anything_ happens, including mouse motions and
578 * other things that don't cause data to be readable
579 * - so we're back to ReadFile blocking.
580 */
581 idata.event = stdinevent;
582 idata.eventback = CreateEvent(NULL, FALSE, FALSE, NULL);
583 if (!CreateThread(NULL, 0, stdin_read_thread,
5471d09a 584 &idata, 0, &in_threadid)) {
585 fprintf(stderr, "Unable to create input thread\n");
93b581bd 586 cleanup_exit(1);
32874aea 587 }
588 sending = TRUE;
589 }
590
5471d09a 591 n = WaitForMultipleObjects(4, handles, FALSE, INFINITE);
32874aea 592 if (n == 0) {
593 WSANETWORKEVENTS things;
8df7a775 594 SOCKET socket;
d2371c81 595 extern SOCKET first_socket(int *), next_socket(int *);
8df7a775 596 extern int select_result(WPARAM, LPARAM);
32874aea 597 int i, socketstate;
598
599 /*
600 * We must not call select_result() for any socket
601 * until we have finished enumerating within the tree.
602 * This is because select_result() may close the socket
603 * and modify the tree.
604 */
605 /* Count the active sockets. */
606 i = 0;
607 for (socket = first_socket(&socketstate);
608 socket != INVALID_SOCKET;
609 socket = next_socket(&socketstate)) i++;
610
611 /* Expand the buffer if necessary. */
612 if (i > sksize) {
613 sksize = i + 16;
614 sklist = srealloc(sklist, sksize * sizeof(*sklist));
615 }
616
617 /* Retrieve the sockets into sklist. */
618 skcount = 0;
619 for (socket = first_socket(&socketstate);
620 socket != INVALID_SOCKET;
d2371c81 621 socket = next_socket(&socketstate)) {
32874aea 622 sklist[skcount++] = socket;
623 }
624
625 /* Now we're done enumerating; go through the list. */
626 for (i = 0; i < skcount; i++) {
627 WPARAM wp;
628 socket = sklist[i];
629 wp = (WPARAM) socket;
ffb959c7 630 if (!WSAEnumNetworkEvents(socket, NULL, &things)) {
64cdd21b 631 static const struct { int bit, mask; } eventtypes[] = {
632 {FD_CONNECT_BIT, FD_CONNECT},
633 {FD_READ_BIT, FD_READ},
634 {FD_CLOSE_BIT, FD_CLOSE},
635 {FD_OOB_BIT, FD_OOB},
636 {FD_WRITE_BIT, FD_WRITE},
637 {FD_ACCEPT_BIT, FD_ACCEPT},
638 };
639 int e;
640
32874aea 641 noise_ultralight(socket);
642 noise_ultralight(things.lNetworkEvents);
d74d141c 643
64cdd21b 644 for (e = 0; e < lenof(eventtypes); e++)
645 if (things.lNetworkEvents & eventtypes[e].mask) {
646 LPARAM lp;
647 int err = things.iErrorCode[eventtypes[e].bit];
648 lp = WSAMAKESELECTREPLY(eventtypes[e].mask, err);
649 connopen &= select_result(wp, lp);
650 }
8df7a775 651 }
652 }
32874aea 653 } else if (n == 1) {
5471d09a 654 reading = 0;
32874aea 655 noise_ultralight(idata.len);
42856df4 656 if (connopen && back->socket() != NULL) {
657 if (idata.len > 0) {
658 back->send(idata.buffer, idata.len);
659 } else {
660 back->special(TS_EOF);
661 }
32874aea 662 }
5471d09a 663 } else if (n == 2) {
664 odata.busy = 0;
665 if (!odata.writeret) {
666 fprintf(stderr, "Unable to write to standard output\n");
93b581bd 667 cleanup_exit(0);
5471d09a 668 }
669 bufchain_consume(&stdout_data, odata.lenwritten);
670 if (bufchain_size(&stdout_data) > 0)
671 try_output(0);
42856df4 672 if (connopen && back->socket() != NULL) {
673 back->unthrottle(bufchain_size(&stdout_data) +
674 bufchain_size(&stderr_data));
675 }
5471d09a 676 } else if (n == 3) {
677 edata.busy = 0;
678 if (!edata.writeret) {
679 fprintf(stderr, "Unable to write to standard output\n");
93b581bd 680 cleanup_exit(0);
5471d09a 681 }
682 bufchain_consume(&stderr_data, edata.lenwritten);
683 if (bufchain_size(&stderr_data) > 0)
684 try_output(1);
42856df4 685 if (connopen && back->socket() != NULL) {
686 back->unthrottle(bufchain_size(&stdout_data) +
687 bufchain_size(&stderr_data));
688 }
5471d09a 689 }
690 if (!reading && back->sendbuffer() < MAX_STDIN_BACKLOG) {
32874aea 691 SetEvent(idata.eventback);
5471d09a 692 reading = 1;
32874aea 693 }
42856df4 694 if ((!connopen || back->socket() == NULL) &&
695 bufchain_size(&stdout_data) == 0 &&
696 bufchain_size(&stderr_data) == 0)
32874aea 697 break; /* we closed the connection */
12dc4ec0 698 }
699 WSACleanup();
d8d6c7e5 700 exitcode = back->exitcode();
701 if (exitcode < 0) {
702 fprintf(stderr, "Remote process exit code unavailable\n");
703 exitcode = 1; /* this is an error condition */
704 }
705 return exitcode;
12dc4ec0 706}