c5e438ec |
1 | /* |
2 | * PLink - a command-line (stdin/stdout) variant of PuTTY. |
3 | */ |
4 | |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
5673d44e |
7 | #include <errno.h> |
c5e438ec |
8 | #include <assert.h> |
9 | #include <stdarg.h> |
5673d44e |
10 | #include <signal.h> |
c5e438ec |
11 | #include <unistd.h> |
12 | #include <fcntl.h> |
13 | #include <termios.h> |
5a9eb105 |
14 | #include <pwd.h> |
15 | #include <sys/ioctl.h> |
c5e438ec |
16 | |
17 | /* More helpful version of the FD_SET macro, to also handle maxfd. */ |
18 | #define FD_SET_MAX(fd, max, set) do { \ |
19 | FD_SET(fd, &set); \ |
20 | if (max < fd + 1) max = fd + 1; \ |
21 | } while (0) |
22 | |
23 | #define PUTTY_DO_GLOBALS /* actually _define_ globals */ |
24 | #include "putty.h" |
25 | #include "storage.h" |
26 | #include "tree234.h" |
27 | |
28 | #define MAX_STDIN_BACKLOG 4096 |
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 | cleanup_exit(1); |
39 | } |
40 | void modalfatalbox(char *p, ...) |
41 | { |
42 | va_list ap; |
43 | fprintf(stderr, "FATAL ERROR: "); |
44 | va_start(ap, p); |
45 | vfprintf(stderr, p, ap); |
46 | va_end(ap); |
47 | fputc('\n', stderr); |
48 | cleanup_exit(1); |
49 | } |
50 | void connection_fatal(void *frontend, char *p, ...) |
51 | { |
52 | va_list ap; |
53 | fprintf(stderr, "FATAL ERROR: "); |
54 | va_start(ap, p); |
55 | vfprintf(stderr, p, ap); |
56 | va_end(ap); |
57 | fputc('\n', stderr); |
58 | cleanup_exit(1); |
59 | } |
60 | void cmdline_error(char *p, ...) |
61 | { |
62 | va_list ap; |
63 | fprintf(stderr, "plink: "); |
64 | va_start(ap, p); |
65 | vfprintf(stderr, p, ap); |
66 | va_end(ap); |
67 | fputc('\n', stderr); |
68 | exit(1); |
69 | } |
70 | |
71 | struct termios orig_termios; |
72 | |
73 | static Backend *back; |
74 | static void *backhandle; |
3ea863a3 |
75 | static Config cfg; |
c5e438ec |
76 | |
5a9eb105 |
77 | /* |
78 | * Default settings that are specific to pterm. |
79 | */ |
c85623f9 |
80 | char *platform_default_s(const char *name) |
5a9eb105 |
81 | { |
82 | if (!strcmp(name, "X11Display")) |
83 | return getenv("DISPLAY"); |
84 | if (!strcmp(name, "TermType")) |
85 | return getenv("TERM"); |
86 | if (!strcmp(name, "UserName")) { |
87 | /* |
88 | * Remote login username will default to the local username. |
89 | */ |
90 | struct passwd *p; |
91 | uid_t uid = getuid(); |
92 | char *user, *ret = NULL; |
93 | |
94 | /* |
95 | * First, find who we think we are using getlogin. If this |
96 | * agrees with our uid, we'll go along with it. This should |
97 | * allow sharing of uids between several login names whilst |
98 | * coping correctly with people who have su'ed. |
99 | */ |
100 | user = getlogin(); |
101 | setpwent(); |
102 | if (user) |
103 | p = getpwnam(user); |
104 | else |
105 | p = NULL; |
106 | if (p && p->pw_uid == uid) { |
107 | /* |
108 | * The result of getlogin() really does correspond to |
109 | * our uid. Fine. |
110 | */ |
111 | ret = user; |
112 | } else { |
113 | /* |
114 | * If that didn't work, for whatever reason, we'll do |
115 | * the simpler version: look up our uid in the password |
116 | * file and map it straight to a name. |
117 | */ |
118 | p = getpwuid(uid); |
119 | ret = p->pw_name; |
120 | } |
121 | endpwent(); |
122 | |
123 | return ret; |
124 | } |
125 | return NULL; |
126 | } |
127 | |
c85623f9 |
128 | int platform_default_i(const char *name, int def) |
5a9eb105 |
129 | { |
130 | if (!strcmp(name, "TermWidth") || |
131 | !strcmp(name, "TermHeight")) { |
132 | struct winsize size; |
133 | if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0) |
134 | return (!strcmp(name, "TermWidth") ? size.ws_col : size.ws_row); |
135 | } |
136 | return def; |
137 | } |
138 | |
9a30e26b |
139 | FontSpec platform_default_fontspec(const char *name) |
140 | { |
141 | FontSpec ret; |
142 | *ret.name = '\0'; |
143 | return ret; |
144 | } |
145 | |
146 | Filename platform_default_filename(const char *name) |
147 | { |
148 | Filename ret; |
149 | if (!strcmp(name, "LogFileName")) |
150 | strcpy(ret.path, "putty.log"); |
151 | else |
152 | *ret.path = '\0'; |
153 | return ret; |
154 | } |
155 | |
c85623f9 |
156 | char *x_get_default(const char *key) |
c5e438ec |
157 | { |
158 | return NULL; /* this is a stub */ |
159 | } |
160 | int term_ldisc(Terminal *term, int mode) |
161 | { |
162 | return FALSE; |
163 | } |
164 | void ldisc_update(void *frontend, int echo, int edit) |
165 | { |
166 | /* Update stdin read mode to reflect changes in line discipline. */ |
167 | struct termios mode; |
168 | |
169 | mode = orig_termios; |
170 | |
171 | if (echo) |
172 | mode.c_lflag |= ECHO; |
173 | else |
174 | mode.c_lflag &= ~ECHO; |
175 | |
176 | if (edit) |
177 | mode.c_lflag |= ISIG | ICANON; |
178 | else |
179 | mode.c_lflag &= ~(ISIG | ICANON); |
180 | |
181 | tcsetattr(0, TCSANOW, &mode); |
182 | } |
183 | |
184 | void cleanup_termios(void) |
185 | { |
186 | tcsetattr(0, TCSANOW, &orig_termios); |
187 | } |
188 | |
189 | bufchain stdout_data, stderr_data; |
190 | |
191 | void try_output(int is_stderr) |
192 | { |
193 | bufchain *chain = (is_stderr ? &stderr_data : &stdout_data); |
194 | int fd = (is_stderr ? 2 : 1); |
195 | void *senddata; |
196 | int sendlen, ret; |
197 | |
d69a46c0 |
198 | if (bufchain_size(chain) == 0) |
199 | return; |
200 | |
c5e438ec |
201 | bufchain_prefix(chain, &senddata, &sendlen); |
202 | ret = write(fd, senddata, sendlen); |
203 | if (ret > 0) |
204 | bufchain_consume(chain, ret); |
205 | else if (ret < 0) { |
206 | perror(is_stderr ? "stderr: write" : "stdout: write"); |
207 | exit(1); |
208 | } |
209 | } |
210 | |
211 | int from_backend(void *frontend_handle, int is_stderr, char *data, int len) |
212 | { |
213 | int osize, esize; |
214 | |
215 | assert(len > 0); |
216 | |
217 | if (is_stderr) { |
218 | bufchain_add(&stderr_data, data, len); |
219 | try_output(1); |
220 | } else { |
221 | bufchain_add(&stdout_data, data, len); |
222 | try_output(0); |
223 | } |
224 | |
225 | osize = bufchain_size(&stdout_data); |
226 | esize = bufchain_size(&stderr_data); |
227 | |
228 | return osize + esize; |
229 | } |
230 | |
5673d44e |
231 | int signalpipe[2]; |
232 | |
233 | void sigwinch(int signum) |
234 | { |
235 | write(signalpipe[1], "x", 1); |
236 | } |
237 | |
c5e438ec |
238 | /* |
239 | * Short description of parameters. |
240 | */ |
241 | static void usage(void) |
242 | { |
243 | printf("PuTTY Link: command-line connection utility\n"); |
244 | printf("%s\n", ver); |
245 | printf("Usage: plink [options] [user@]host [command]\n"); |
246 | printf(" (\"host\" can also be a PuTTY saved session name)\n"); |
247 | printf("Options:\n"); |
248 | printf(" -v show verbose messages\n"); |
249 | printf(" -load sessname Load settings from saved session\n"); |
250 | printf(" -ssh -telnet -rlogin -raw\n"); |
251 | printf(" force use of a particular protocol (default SSH)\n"); |
252 | printf(" -P port connect to specified port\n"); |
253 | printf(" -l user connect with specified username\n"); |
254 | printf(" -m file read remote command(s) from file\n"); |
255 | printf(" -batch disable all interactive prompts\n"); |
256 | printf("The following options only apply to SSH connections:\n"); |
257 | printf(" -pw passw login with specified password\n"); |
258 | printf(" -L listen-port:host:port Forward local port to " |
259 | "remote address\n"); |
260 | printf(" -R listen-port:host:port Forward remote port to" |
261 | " local address\n"); |
262 | printf(" -X -x enable / disable X11 forwarding\n"); |
263 | printf(" -A -a enable / disable agent forwarding\n"); |
264 | printf(" -t -T enable / disable pty allocation\n"); |
265 | printf(" -1 -2 force use of particular protocol version\n"); |
266 | printf(" -C enable compression\n"); |
267 | printf(" -i key private key file for authentication\n"); |
268 | exit(1); |
269 | } |
270 | |
271 | int main(int argc, char **argv) |
272 | { |
273 | int sending; |
274 | int portnumber = -1; |
275 | int *sklist; |
276 | int socket; |
277 | int i, skcount, sksize, socketstate; |
278 | int connopen; |
279 | int exitcode; |
86256dc6 |
280 | int errors; |
c5e438ec |
281 | void *ldisc; |
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; |
86256dc6 |
301 | errors = 0; |
c5e438ec |
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 == '-') { |
9b41d3a8 |
322 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
323 | 1, &cfg); |
c5e438ec |
324 | if (ret == -2) { |
325 | fprintf(stderr, |
326 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
327 | errors = 1; |
c5e438ec |
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; |
a0e5ed33 |
334 | } else if (!strcmp(p, "-o")) { |
86256dc6 |
335 | if (argc <= 1) { |
a0e5ed33 |
336 | fprintf(stderr, |
337 | "plink: option \"-o\" requires an argument\n"); |
86256dc6 |
338 | errors = 1; |
339 | } else { |
340 | --argc; |
341 | provide_xrm_string(*++argv); |
342 | } |
343 | } else { |
344 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
345 | errors = 1; |
c5e438ec |
346 | } |
347 | } else if (*p) { |
348 | if (!*cfg.host) { |
349 | char *q = p; |
a0e5ed33 |
350 | |
351 | do_defaults(NULL, &cfg); |
352 | |
c5e438ec |
353 | /* |
354 | * If the hostname starts with "telnet:", set the |
355 | * protocol to Telnet and process the string as a |
356 | * Telnet URL. |
357 | */ |
358 | if (!strncmp(q, "telnet:", 7)) { |
359 | char c; |
360 | |
361 | q += 7; |
362 | if (q[0] == '/' && q[1] == '/') |
363 | q += 2; |
364 | cfg.protocol = PROT_TELNET; |
365 | p = q; |
366 | while (*p && *p != ':' && *p != '/') |
367 | p++; |
368 | c = *p; |
369 | if (*p) |
370 | *p++ = '\0'; |
371 | if (c == ':') |
372 | cfg.port = atoi(p); |
373 | else |
374 | cfg.port = -1; |
375 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
376 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
377 | } else { |
378 | char *r; |
379 | /* |
380 | * Before we process the [user@]host string, we |
381 | * first check for the presence of a protocol |
382 | * prefix (a protocol name followed by ","). |
383 | */ |
384 | r = strchr(p, ','); |
385 | if (r) { |
386 | int i, j; |
387 | for (i = 0; backends[i].backend != NULL; i++) { |
388 | j = strlen(backends[i].name); |
389 | if (j == r - p && |
390 | !memcmp(backends[i].name, p, j)) { |
391 | default_protocol = cfg.protocol = |
392 | backends[i].protocol; |
393 | portnumber = |
394 | backends[i].backend->default_port; |
395 | p = r + 1; |
396 | break; |
397 | } |
398 | } |
399 | } |
400 | |
401 | /* |
402 | * Three cases. Either (a) there's a nonzero |
403 | * length string followed by an @, in which |
404 | * case that's user and the remainder is host. |
405 | * Or (b) there's only one string, not counting |
406 | * a potential initial @, and it exists in the |
407 | * saved-sessions database. Or (c) only one |
408 | * string and it _doesn't_ exist in the |
409 | * database. |
410 | */ |
411 | r = strrchr(p, '@'); |
412 | if (r == p) |
413 | p++, r = NULL; /* discount initial @ */ |
414 | if (r == NULL) { |
415 | /* |
416 | * One string. |
417 | */ |
418 | Config cfg2; |
419 | do_defaults(p, &cfg2); |
420 | if (cfg2.host[0] == '\0') { |
421 | /* No settings for this host; use defaults */ |
422 | strncpy(cfg.host, p, sizeof(cfg.host) - 1); |
423 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
424 | cfg.port = default_port; |
425 | } else { |
426 | cfg = cfg2; |
427 | cfg.remote_cmd_ptr = cfg.remote_cmd; |
428 | } |
429 | } else { |
430 | *r++ = '\0'; |
431 | strncpy(cfg.username, p, sizeof(cfg.username) - 1); |
432 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
433 | strncpy(cfg.host, r, sizeof(cfg.host) - 1); |
434 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
435 | cfg.port = default_port; |
436 | } |
437 | } |
438 | } else { |
439 | char *command; |
440 | int cmdlen, cmdsize; |
441 | cmdlen = cmdsize = 0; |
442 | command = NULL; |
443 | |
444 | while (argc) { |
445 | while (*p) { |
446 | if (cmdlen >= cmdsize) { |
447 | cmdsize = cmdlen + 512; |
448 | command = srealloc(command, cmdsize); |
449 | } |
450 | command[cmdlen++]=*p++; |
451 | } |
452 | if (cmdlen >= cmdsize) { |
453 | cmdsize = cmdlen + 512; |
454 | command = srealloc(command, cmdsize); |
455 | } |
456 | command[cmdlen++]=' '; /* always add trailing space */ |
457 | if (--argc) p = *++argv; |
458 | } |
459 | if (cmdlen) command[--cmdlen]='\0'; |
460 | /* change trailing blank to NUL */ |
461 | cfg.remote_cmd_ptr = command; |
462 | cfg.remote_cmd_ptr2 = NULL; |
463 | cfg.nopty = TRUE; /* command => no terminal */ |
464 | |
465 | break; /* done with cmdline */ |
466 | } |
467 | } |
468 | } |
469 | |
86256dc6 |
470 | if (errors) |
471 | return 1; |
472 | |
c5e438ec |
473 | if (!*cfg.host) { |
474 | usage(); |
475 | } |
476 | |
477 | /* |
478 | * Trim leading whitespace off the hostname if it's there. |
479 | */ |
480 | { |
481 | int space = strspn(cfg.host, " \t"); |
482 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
483 | } |
484 | |
485 | /* See if host is of the form user@host */ |
486 | if (cfg.host[0] != '\0') { |
487 | char *atsign = strchr(cfg.host, '@'); |
488 | /* Make sure we're not overflowing the user field */ |
489 | if (atsign) { |
490 | if (atsign - cfg.host < sizeof cfg.username) { |
491 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
492 | cfg.username[atsign - cfg.host] = '\0'; |
493 | } |
494 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
495 | } |
496 | } |
497 | |
498 | /* |
499 | * Perform command-line overrides on session configuration. |
500 | */ |
9b41d3a8 |
501 | cmdline_run_saved(&cfg); |
c5e438ec |
502 | |
503 | /* |
504 | * Trim a colon suffix off the hostname if it's there. |
505 | */ |
506 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
507 | |
508 | /* |
509 | * Remove any remaining whitespace from the hostname. |
510 | */ |
511 | { |
512 | int p1 = 0, p2 = 0; |
513 | while (cfg.host[p2] != '\0') { |
514 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
515 | cfg.host[p1] = cfg.host[p2]; |
516 | p1++; |
517 | } |
518 | p2++; |
519 | } |
520 | cfg.host[p1] = '\0'; |
521 | } |
522 | |
523 | if (!*cfg.remote_cmd_ptr) |
524 | flags |= FLAG_INTERACTIVE; |
525 | |
526 | /* |
527 | * Select protocol. This is farmed out into a table in a |
528 | * separate file to enable an ssh-free variant. |
529 | */ |
530 | { |
531 | int i; |
532 | back = NULL; |
533 | for (i = 0; backends[i].backend != NULL; i++) |
534 | if (backends[i].protocol == cfg.protocol) { |
535 | back = backends[i].backend; |
536 | break; |
537 | } |
538 | if (back == NULL) { |
539 | fprintf(stderr, |
540 | "Internal fault: Unsupported protocol found\n"); |
541 | return 1; |
542 | } |
543 | } |
544 | |
545 | /* |
546 | * Select port. |
547 | */ |
548 | if (portnumber != -1) |
549 | cfg.port = portnumber; |
550 | |
5673d44e |
551 | /* |
552 | * Set up the pipe we'll use to tell us about SIGWINCH. |
553 | */ |
554 | if (pipe(signalpipe) < 0) { |
555 | perror("pipe"); |
556 | exit(1); |
557 | } |
558 | putty_signal(SIGWINCH, sigwinch); |
559 | |
c5e438ec |
560 | sk_init(); |
561 | |
562 | /* |
563 | * Start up the connection. |
564 | */ |
c229ef97 |
565 | logctx = log_init(NULL, &cfg); |
c5e438ec |
566 | { |
567 | char *error; |
568 | char *realhost; |
569 | /* nodelay is only useful if stdin is a terminal device */ |
570 | int nodelay = cfg.tcp_nodelay && isatty(0); |
571 | |
86916870 |
572 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
c5e438ec |
573 | &realhost, nodelay); |
574 | if (error) { |
984992ef |
575 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
576 | return 1; |
577 | } |
c5e438ec |
578 | back->provide_logctx(backhandle, logctx); |
fe5634f6 |
579 | ldisc = ldisc_create(&cfg, NULL, back, backhandle, NULL); |
c5e438ec |
580 | sfree(realhost); |
581 | } |
582 | connopen = 1; |
583 | |
584 | /* |
585 | * Set up the initial console mode. We don't care if this call |
586 | * fails, because we know we aren't necessarily running in a |
587 | * console. |
588 | */ |
589 | tcgetattr(0, &orig_termios); |
590 | atexit(cleanup_termios); |
591 | ldisc_update(NULL, 1, 1); |
592 | sending = FALSE; |
593 | |
594 | while (1) { |
595 | fd_set rset, wset, xset; |
596 | int maxfd; |
597 | int rwx; |
598 | int ret; |
599 | |
600 | FD_ZERO(&rset); |
601 | FD_ZERO(&wset); |
602 | FD_ZERO(&xset); |
603 | maxfd = 0; |
604 | |
5673d44e |
605 | FD_SET_MAX(signalpipe[0], maxfd, rset); |
606 | |
c5e438ec |
607 | if (connopen && !sending && |
608 | back->socket(backhandle) != NULL && |
609 | back->sendok(backhandle) && |
610 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
611 | /* If we're OK to send, then try to read from stdin. */ |
612 | FD_SET_MAX(0, maxfd, rset); |
613 | } |
614 | |
615 | if (bufchain_size(&stdout_data) > 0) { |
616 | /* If we have data for stdout, try to write to stdout. */ |
617 | FD_SET_MAX(1, maxfd, wset); |
618 | } |
619 | |
620 | if (bufchain_size(&stderr_data) > 0) { |
621 | /* If we have data for stderr, try to write to stderr. */ |
622 | FD_SET_MAX(2, maxfd, wset); |
623 | } |
624 | |
625 | /* Count the currently active sockets. */ |
626 | i = 0; |
627 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
628 | socket = next_socket(&socketstate, &rwx)) i++; |
629 | |
630 | /* Expand the sklist buffer if necessary. */ |
631 | if (i > sksize) { |
632 | sksize = i + 16; |
633 | sklist = srealloc(sklist, sksize * sizeof(*sklist)); |
634 | } |
635 | |
636 | /* |
637 | * Add all currently open sockets to the select sets, and |
638 | * store them in sklist as well. |
639 | */ |
640 | skcount = 0; |
641 | for (socket = first_socket(&socketstate, &rwx); socket >= 0; |
642 | socket = next_socket(&socketstate, &rwx)) { |
643 | sklist[skcount++] = socket; |
644 | if (rwx & 1) |
645 | FD_SET_MAX(socket, maxfd, rset); |
646 | if (rwx & 2) |
647 | FD_SET_MAX(socket, maxfd, wset); |
648 | if (rwx & 4) |
649 | FD_SET_MAX(socket, maxfd, xset); |
650 | } |
651 | |
5673d44e |
652 | do { |
653 | ret = select(maxfd, &rset, &wset, &xset, NULL); |
654 | } while (ret < 0 && errno == EINTR); |
c5e438ec |
655 | |
656 | if (ret < 0) { |
657 | perror("select"); |
658 | exit(1); |
659 | } |
660 | |
661 | for (i = 0; i < skcount; i++) { |
662 | socket = sklist[i]; |
56e5b2db |
663 | /* |
664 | * We must process exceptional notifications before |
665 | * ordinary readability ones, or we may go straight |
666 | * past the urgent marker. |
667 | */ |
668 | if (FD_ISSET(socket, &xset)) |
669 | select_result(socket, 4); |
c5e438ec |
670 | if (FD_ISSET(socket, &rset)) |
671 | select_result(socket, 1); |
672 | if (FD_ISSET(socket, &wset)) |
673 | select_result(socket, 2); |
c5e438ec |
674 | } |
675 | |
5673d44e |
676 | if (FD_ISSET(signalpipe[0], &rset)) { |
677 | char c[1]; |
678 | struct winsize size; |
679 | read(signalpipe[0], c, 1); /* ignore its value; it'll be `x' */ |
680 | if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0) |
681 | back->size(backhandle, size.ws_col, size.ws_row); |
682 | } |
683 | |
c5e438ec |
684 | if (FD_ISSET(0, &rset)) { |
685 | char buf[4096]; |
686 | int ret; |
687 | |
688 | if (connopen && back->socket(backhandle) != NULL) { |
689 | ret = read(0, buf, sizeof(buf)); |
690 | if (ret < 0) { |
691 | perror("stdin: read"); |
692 | exit(1); |
693 | } else if (ret == 0) { |
694 | back->special(backhandle, TS_EOF); |
695 | sending = FALSE; /* send nothing further after this */ |
696 | } else { |
697 | back->send(backhandle, buf, ret); |
698 | } |
699 | } |
700 | } |
701 | |
702 | if (FD_ISSET(1, &wset)) { |
703 | try_output(0); |
704 | } |
705 | |
706 | if (FD_ISSET(2, &wset)) { |
707 | try_output(1); |
708 | } |
709 | |
710 | if ((!connopen || back->socket(backhandle) == NULL) && |
711 | bufchain_size(&stdout_data) == 0 && |
712 | bufchain_size(&stderr_data) == 0) |
713 | break; /* we closed the connection */ |
714 | } |
715 | exitcode = back->exitcode(backhandle); |
716 | if (exitcode < 0) { |
717 | fprintf(stderr, "Remote process exit code unavailable\n"); |
718 | exitcode = 1; /* this is an error condition */ |
719 | } |
d9c40fd6 |
720 | cleanup_exit(exitcode); |
721 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
722 | } |