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"); |
c9a13be6 |
211 | printf(" -V print version information\n"); |
c5e438ec |
212 | printf(" -v show verbose messages\n"); |
213 | printf(" -load sessname Load settings from saved session\n"); |
214 | printf(" -ssh -telnet -rlogin -raw\n"); |
afd4d0d2 |
215 | printf(" force use of a particular protocol\n"); |
c5e438ec |
216 | printf(" -P port connect to specified port\n"); |
217 | printf(" -l user connect with specified username\n"); |
218 | printf(" -m file read remote command(s) from file\n"); |
219 | printf(" -batch disable all interactive prompts\n"); |
220 | printf("The following options only apply to SSH connections:\n"); |
221 | printf(" -pw passw login with specified password\n"); |
dbe6c525 |
222 | printf(" -D [listen-IP:]listen-port\n"); |
223 | printf(" Dynamic SOCKS-based port forwarding\n"); |
224 | printf(" -L [listen-IP:]listen-port:host:port\n"); |
225 | printf(" Forward local port to remote address\n"); |
226 | printf(" -R [listen-IP:]listen-port:host:port\n"); |
227 | printf(" Forward remote port to local address\n"); |
c5e438ec |
228 | printf(" -X -x enable / disable X11 forwarding\n"); |
229 | printf(" -A -a enable / disable agent forwarding\n"); |
230 | printf(" -t -T enable / disable pty allocation\n"); |
231 | printf(" -1 -2 force use of particular protocol version\n"); |
232 | printf(" -C enable compression\n"); |
233 | printf(" -i key private key file for authentication\n"); |
09bdfcbb |
234 | printf(" -s remote command is an SSH subsystem (SSH-2 only)\n"); |
dc108ebc |
235 | exit(1); |
236 | } |
237 | |
238 | static void version(void) |
239 | { |
240 | printf("plink: %s\n", ver); |
c5e438ec |
241 | exit(1); |
242 | } |
243 | |
244 | int main(int argc, char **argv) |
245 | { |
246 | int sending; |
247 | int portnumber = -1; |
0ff9ea38 |
248 | int *fdlist; |
249 | int fd; |
250 | int i, fdcount, fdsize, fdstate; |
c5e438ec |
251 | int connopen; |
252 | int exitcode; |
86256dc6 |
253 | int errors; |
09bdfcbb |
254 | int use_subsystem = 0; |
b51259f6 |
255 | void *ldisc, *logctx; |
c5e438ec |
256 | |
257 | ssh_get_line = console_get_line; |
258 | |
0ff9ea38 |
259 | fdlist = NULL; |
260 | fdcount = fdsize = 0; |
c5e438ec |
261 | /* |
262 | * Initialise port and protocol to sensible defaults. (These |
263 | * will be overridden by more or less anything.) |
264 | */ |
265 | default_protocol = PROT_SSH; |
266 | default_port = 22; |
267 | |
268 | flags = FLAG_STDERR; |
269 | /* |
270 | * Process the command line. |
271 | */ |
272 | do_defaults(NULL, &cfg); |
18e62ad8 |
273 | loaded_session = FALSE; |
c5e438ec |
274 | default_protocol = cfg.protocol; |
275 | default_port = cfg.port; |
86256dc6 |
276 | errors = 0; |
c5e438ec |
277 | { |
278 | /* |
279 | * Override the default protocol if PLINK_PROTOCOL is set. |
280 | */ |
281 | char *p = getenv("PLINK_PROTOCOL"); |
282 | int i; |
283 | if (p) { |
284 | for (i = 0; backends[i].backend != NULL; i++) { |
285 | if (!strcmp(backends[i].name, p)) { |
286 | default_protocol = cfg.protocol = backends[i].protocol; |
287 | default_port = cfg.port = |
288 | backends[i].backend->default_port; |
289 | break; |
290 | } |
291 | } |
292 | } |
293 | } |
294 | while (--argc) { |
295 | char *p = *++argv; |
296 | if (*p == '-') { |
9b41d3a8 |
297 | int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL), |
298 | 1, &cfg); |
c5e438ec |
299 | if (ret == -2) { |
300 | fprintf(stderr, |
301 | "plink: option \"%s\" requires an argument\n", p); |
86256dc6 |
302 | errors = 1; |
c5e438ec |
303 | } else if (ret == 2) { |
304 | --argc, ++argv; |
305 | } else if (ret == 1) { |
306 | continue; |
307 | } else if (!strcmp(p, "-batch")) { |
308 | console_batch_mode = 1; |
09bdfcbb |
309 | } else if (!strcmp(p, "-s")) { |
310 | /* Save status to write to cfg later. */ |
311 | use_subsystem = 1; |
dc108ebc |
312 | } else if (!strcmp(p, "-V")) { |
313 | version(); |
a0e5ed33 |
314 | } else if (!strcmp(p, "-o")) { |
86256dc6 |
315 | if (argc <= 1) { |
a0e5ed33 |
316 | fprintf(stderr, |
317 | "plink: option \"-o\" requires an argument\n"); |
86256dc6 |
318 | errors = 1; |
319 | } else { |
320 | --argc; |
321 | provide_xrm_string(*++argv); |
322 | } |
323 | } else { |
324 | fprintf(stderr, "plink: unknown option \"%s\"\n", p); |
325 | errors = 1; |
c5e438ec |
326 | } |
327 | } else if (*p) { |
328 | if (!*cfg.host) { |
329 | char *q = p; |
a0e5ed33 |
330 | |
331 | do_defaults(NULL, &cfg); |
332 | |
c5e438ec |
333 | /* |
334 | * If the hostname starts with "telnet:", set the |
335 | * protocol to Telnet and process the string as a |
336 | * Telnet URL. |
337 | */ |
338 | if (!strncmp(q, "telnet:", 7)) { |
339 | char c; |
340 | |
341 | q += 7; |
342 | if (q[0] == '/' && q[1] == '/') |
343 | q += 2; |
344 | cfg.protocol = PROT_TELNET; |
345 | p = q; |
346 | while (*p && *p != ':' && *p != '/') |
347 | p++; |
348 | c = *p; |
349 | if (*p) |
350 | *p++ = '\0'; |
351 | if (c == ':') |
352 | cfg.port = atoi(p); |
353 | else |
354 | cfg.port = -1; |
355 | strncpy(cfg.host, q, sizeof(cfg.host) - 1); |
356 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
357 | } else { |
3608528b |
358 | char *r, *user, *host; |
c5e438ec |
359 | /* |
360 | * Before we process the [user@]host string, we |
361 | * first check for the presence of a protocol |
362 | * prefix (a protocol name followed by ","). |
363 | */ |
364 | r = strchr(p, ','); |
365 | if (r) { |
366 | int i, j; |
367 | for (i = 0; backends[i].backend != NULL; i++) { |
368 | j = strlen(backends[i].name); |
369 | if (j == r - p && |
370 | !memcmp(backends[i].name, p, j)) { |
371 | default_protocol = cfg.protocol = |
372 | backends[i].protocol; |
373 | portnumber = |
374 | backends[i].backend->default_port; |
375 | p = r + 1; |
376 | break; |
377 | } |
378 | } |
379 | } |
380 | |
381 | /* |
3608528b |
382 | * A nonzero length string followed by an @ is treated |
383 | * as a username. (We discount an _initial_ @.) The |
384 | * rest of the string (or the whole string if no @) |
385 | * is treated as a session name and/or hostname. |
c5e438ec |
386 | */ |
387 | r = strrchr(p, '@'); |
388 | if (r == p) |
389 | p++, r = NULL; /* discount initial @ */ |
3608528b |
390 | if (r) { |
391 | *r++ = '\0'; |
392 | user = p, host = r; |
393 | } else { |
394 | user = NULL, host = p; |
395 | } |
396 | |
397 | /* |
398 | * Now attempt to load a saved session with the |
399 | * same name as the hostname. |
400 | */ |
401 | { |
c5e438ec |
402 | Config cfg2; |
3608528b |
403 | do_defaults(host, &cfg2); |
18e62ad8 |
404 | if (loaded_session || cfg2.host[0] == '\0') { |
c5e438ec |
405 | /* No settings for this host; use defaults */ |
18e62ad8 |
406 | /* (or session was already loaded with -load) */ |
3608528b |
407 | strncpy(cfg.host, host, sizeof(cfg.host) - 1); |
c5e438ec |
408 | cfg.host[sizeof(cfg.host) - 1] = '\0'; |
409 | cfg.port = default_port; |
410 | } else { |
411 | cfg = cfg2; |
18e62ad8 |
412 | /* Ick: patch up internal pointer after copy */ |
c5e438ec |
413 | cfg.remote_cmd_ptr = cfg.remote_cmd; |
414 | } |
3608528b |
415 | } |
416 | |
417 | if (user) { |
418 | /* Patch in specified username. */ |
419 | strncpy(cfg.username, user, |
420 | sizeof(cfg.username) - 1); |
c5e438ec |
421 | cfg.username[sizeof(cfg.username) - 1] = '\0'; |
c5e438ec |
422 | } |
3608528b |
423 | |
c5e438ec |
424 | } |
425 | } else { |
426 | char *command; |
427 | int cmdlen, cmdsize; |
428 | cmdlen = cmdsize = 0; |
429 | command = NULL; |
430 | |
431 | while (argc) { |
432 | while (*p) { |
433 | if (cmdlen >= cmdsize) { |
434 | cmdsize = cmdlen + 512; |
3d88e64d |
435 | command = sresize(command, cmdsize, char); |
c5e438ec |
436 | } |
437 | command[cmdlen++]=*p++; |
438 | } |
439 | if (cmdlen >= cmdsize) { |
440 | cmdsize = cmdlen + 512; |
3d88e64d |
441 | command = sresize(command, cmdsize, char); |
c5e438ec |
442 | } |
443 | command[cmdlen++]=' '; /* always add trailing space */ |
444 | if (--argc) p = *++argv; |
445 | } |
446 | if (cmdlen) command[--cmdlen]='\0'; |
447 | /* change trailing blank to NUL */ |
448 | cfg.remote_cmd_ptr = command; |
449 | cfg.remote_cmd_ptr2 = NULL; |
450 | cfg.nopty = TRUE; /* command => no terminal */ |
451 | |
452 | break; /* done with cmdline */ |
453 | } |
454 | } |
455 | } |
456 | |
86256dc6 |
457 | if (errors) |
458 | return 1; |
459 | |
c5e438ec |
460 | if (!*cfg.host) { |
461 | usage(); |
462 | } |
463 | |
464 | /* |
465 | * Trim leading whitespace off the hostname if it's there. |
466 | */ |
467 | { |
468 | int space = strspn(cfg.host, " \t"); |
469 | memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space); |
470 | } |
471 | |
472 | /* See if host is of the form user@host */ |
473 | if (cfg.host[0] != '\0') { |
474 | char *atsign = strchr(cfg.host, '@'); |
475 | /* Make sure we're not overflowing the user field */ |
476 | if (atsign) { |
477 | if (atsign - cfg.host < sizeof cfg.username) { |
478 | strncpy(cfg.username, cfg.host, atsign - cfg.host); |
479 | cfg.username[atsign - cfg.host] = '\0'; |
480 | } |
481 | memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1)); |
482 | } |
483 | } |
484 | |
485 | /* |
486 | * Perform command-line overrides on session configuration. |
487 | */ |
9b41d3a8 |
488 | cmdline_run_saved(&cfg); |
c5e438ec |
489 | |
490 | /* |
09bdfcbb |
491 | * Apply subsystem status. |
492 | */ |
493 | if (use_subsystem) |
494 | cfg.ssh_subsys = TRUE; |
495 | |
496 | /* |
c5e438ec |
497 | * Trim a colon suffix off the hostname if it's there. |
498 | */ |
499 | cfg.host[strcspn(cfg.host, ":")] = '\0'; |
500 | |
501 | /* |
502 | * Remove any remaining whitespace from the hostname. |
503 | */ |
504 | { |
505 | int p1 = 0, p2 = 0; |
506 | while (cfg.host[p2] != '\0') { |
507 | if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') { |
508 | cfg.host[p1] = cfg.host[p2]; |
509 | p1++; |
510 | } |
511 | p2++; |
512 | } |
513 | cfg.host[p1] = '\0'; |
514 | } |
515 | |
516 | if (!*cfg.remote_cmd_ptr) |
517 | flags |= FLAG_INTERACTIVE; |
518 | |
519 | /* |
520 | * Select protocol. This is farmed out into a table in a |
521 | * separate file to enable an ssh-free variant. |
522 | */ |
523 | { |
524 | int i; |
525 | back = NULL; |
526 | for (i = 0; backends[i].backend != NULL; i++) |
527 | if (backends[i].protocol == cfg.protocol) { |
528 | back = backends[i].backend; |
529 | break; |
530 | } |
531 | if (back == NULL) { |
532 | fprintf(stderr, |
533 | "Internal fault: Unsupported protocol found\n"); |
534 | return 1; |
535 | } |
536 | } |
537 | |
538 | /* |
539 | * Select port. |
540 | */ |
541 | if (portnumber != -1) |
542 | cfg.port = portnumber; |
543 | |
5673d44e |
544 | /* |
545 | * Set up the pipe we'll use to tell us about SIGWINCH. |
546 | */ |
547 | if (pipe(signalpipe) < 0) { |
548 | perror("pipe"); |
549 | exit(1); |
550 | } |
551 | putty_signal(SIGWINCH, sigwinch); |
552 | |
c5e438ec |
553 | sk_init(); |
0ff9ea38 |
554 | uxsel_init(); |
c5e438ec |
555 | |
556 | /* |
557 | * Start up the connection. |
558 | */ |
c229ef97 |
559 | logctx = log_init(NULL, &cfg); |
b51259f6 |
560 | console_provide_logctx(logctx); |
c5e438ec |
561 | { |
cbe2d68f |
562 | const char *error; |
c5e438ec |
563 | char *realhost; |
564 | /* nodelay is only useful if stdin is a terminal device */ |
565 | int nodelay = cfg.tcp_nodelay && isatty(0); |
566 | |
86916870 |
567 | error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port, |
79bf227b |
568 | &realhost, nodelay, cfg.tcp_keepalives); |
c5e438ec |
569 | if (error) { |
984992ef |
570 | fprintf(stderr, "Unable to open connection:\n%s\n", error); |
c5e438ec |
571 | return 1; |
572 | } |
c5e438ec |
573 | back->provide_logctx(backhandle, logctx); |
fe5634f6 |
574 | ldisc = ldisc_create(&cfg, NULL, back, backhandle, NULL); |
c5e438ec |
575 | sfree(realhost); |
576 | } |
577 | connopen = 1; |
578 | |
579 | /* |
580 | * Set up the initial console mode. We don't care if this call |
581 | * fails, because we know we aren't necessarily running in a |
582 | * console. |
583 | */ |
584 | tcgetattr(0, &orig_termios); |
585 | atexit(cleanup_termios); |
586 | ldisc_update(NULL, 1, 1); |
587 | sending = FALSE; |
588 | |
589 | while (1) { |
590 | fd_set rset, wset, xset; |
591 | int maxfd; |
592 | int rwx; |
593 | int ret; |
594 | |
595 | FD_ZERO(&rset); |
596 | FD_ZERO(&wset); |
597 | FD_ZERO(&xset); |
598 | maxfd = 0; |
599 | |
5673d44e |
600 | FD_SET_MAX(signalpipe[0], maxfd, rset); |
601 | |
c5e438ec |
602 | if (connopen && !sending && |
603 | back->socket(backhandle) != NULL && |
604 | back->sendok(backhandle) && |
605 | back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) { |
606 | /* If we're OK to send, then try to read from stdin. */ |
607 | FD_SET_MAX(0, maxfd, rset); |
608 | } |
609 | |
610 | if (bufchain_size(&stdout_data) > 0) { |
611 | /* If we have data for stdout, try to write to stdout. */ |
612 | FD_SET_MAX(1, maxfd, wset); |
613 | } |
614 | |
615 | if (bufchain_size(&stderr_data) > 0) { |
616 | /* If we have data for stderr, try to write to stderr. */ |
617 | FD_SET_MAX(2, maxfd, wset); |
618 | } |
619 | |
0ff9ea38 |
620 | /* Count the currently active fds. */ |
c5e438ec |
621 | i = 0; |
0ff9ea38 |
622 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
623 | fd = next_fd(&fdstate, &rwx)) i++; |
c5e438ec |
624 | |
0ff9ea38 |
625 | /* Expand the fdlist buffer if necessary. */ |
626 | if (i > fdsize) { |
627 | fdsize = i + 16; |
628 | fdlist = sresize(fdlist, fdsize, int); |
c5e438ec |
629 | } |
630 | |
631 | /* |
0ff9ea38 |
632 | * Add all currently open fds to the select sets, and store |
633 | * them in fdlist as well. |
c5e438ec |
634 | */ |
0ff9ea38 |
635 | fdcount = 0; |
636 | for (fd = first_fd(&fdstate, &rwx); fd >= 0; |
637 | fd = next_fd(&fdstate, &rwx)) { |
638 | fdlist[fdcount++] = fd; |
c5e438ec |
639 | if (rwx & 1) |
0ff9ea38 |
640 | FD_SET_MAX(fd, maxfd, rset); |
c5e438ec |
641 | if (rwx & 2) |
0ff9ea38 |
642 | FD_SET_MAX(fd, maxfd, wset); |
c5e438ec |
643 | if (rwx & 4) |
0ff9ea38 |
644 | FD_SET_MAX(fd, maxfd, xset); |
c5e438ec |
645 | } |
646 | |
5673d44e |
647 | do { |
648 | ret = select(maxfd, &rset, &wset, &xset, NULL); |
649 | } while (ret < 0 && errno == EINTR); |
c5e438ec |
650 | |
651 | if (ret < 0) { |
652 | perror("select"); |
653 | exit(1); |
654 | } |
655 | |
0ff9ea38 |
656 | for (i = 0; i < fdcount; i++) { |
657 | fd = fdlist[i]; |
56e5b2db |
658 | /* |
659 | * We must process exceptional notifications before |
660 | * ordinary readability ones, or we may go straight |
661 | * past the urgent marker. |
662 | */ |
0ff9ea38 |
663 | if (FD_ISSET(fd, &xset)) |
664 | select_result(fd, 4); |
665 | if (FD_ISSET(fd, &rset)) |
666 | select_result(fd, 1); |
667 | if (FD_ISSET(fd, &wset)) |
668 | select_result(fd, 2); |
c5e438ec |
669 | } |
670 | |
5673d44e |
671 | if (FD_ISSET(signalpipe[0], &rset)) { |
672 | char c[1]; |
673 | struct winsize size; |
674 | read(signalpipe[0], c, 1); /* ignore its value; it'll be `x' */ |
675 | if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0) |
676 | back->size(backhandle, size.ws_col, size.ws_row); |
677 | } |
678 | |
c5e438ec |
679 | if (FD_ISSET(0, &rset)) { |
680 | char buf[4096]; |
681 | int ret; |
682 | |
683 | if (connopen && back->socket(backhandle) != NULL) { |
684 | ret = read(0, buf, sizeof(buf)); |
685 | if (ret < 0) { |
686 | perror("stdin: read"); |
687 | exit(1); |
688 | } else if (ret == 0) { |
689 | back->special(backhandle, TS_EOF); |
690 | sending = FALSE; /* send nothing further after this */ |
691 | } else { |
692 | back->send(backhandle, buf, ret); |
693 | } |
694 | } |
695 | } |
696 | |
697 | if (FD_ISSET(1, &wset)) { |
698 | try_output(0); |
699 | } |
700 | |
701 | if (FD_ISSET(2, &wset)) { |
702 | try_output(1); |
703 | } |
704 | |
705 | if ((!connopen || back->socket(backhandle) == NULL) && |
706 | bufchain_size(&stdout_data) == 0 && |
707 | bufchain_size(&stderr_data) == 0) |
708 | break; /* we closed the connection */ |
709 | } |
710 | exitcode = back->exitcode(backhandle); |
711 | if (exitcode < 0) { |
712 | fprintf(stderr, "Remote process exit code unavailable\n"); |
713 | exitcode = 1; /* this is an error condition */ |
714 | } |
d9c40fd6 |
715 | cleanup_exit(exitcode); |
716 | return exitcode; /* shouldn't happen, but placates gcc */ |
c5e438ec |
717 | } |