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