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