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