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