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