2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
\r
13 #include <termios.h>
\r
15 #include <sys/ioctl.h>
\r
16 #include <sys/time.h>
\r
17 #ifndef HAVE_NO_SYS_SELECT_H
\r
18 #include <sys/select.h>
\r
21 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
\r
23 #include "storage.h"
\r
24 #include "tree234.h"
\r
26 #define MAX_STDIN_BACKLOG 4096
\r
30 static struct termios orig_termios;
\r
32 void fatalbox(char *p, ...)
\r
37 fprintf(stderr, "FATAL ERROR: ");
\r
39 vfprintf(stderr, p, ap);
\r
41 fputc('\n', stderr);
\r
49 void modalfatalbox(char *p, ...)
\r
54 fprintf(stderr, "FATAL ERROR: ");
\r
56 vfprintf(stderr, p, ap);
\r
58 fputc('\n', stderr);
\r
66 void connection_fatal(void *frontend, char *p, ...)
\r
71 fprintf(stderr, "FATAL ERROR: ");
\r
73 vfprintf(stderr, p, ap);
\r
75 fputc('\n', stderr);
\r
83 void cmdline_error(char *p, ...)
\r
88 fprintf(stderr, "plink: ");
\r
90 vfprintf(stderr, p, ap);
\r
92 fputc('\n', stderr);
\r
97 static int local_tty = FALSE; /* do we have a local tty? */
\r
99 static Backend *back;
\r
100 static void *backhandle;
\r
104 * Default settings that are specific to pterm.
\r
106 char *platform_default_s(const char *name)
\r
108 if (!strcmp(name, "TermType"))
\r
109 return dupstr(getenv("TERM"));
\r
110 if (!strcmp(name, "UserName"))
\r
111 return get_username();
\r
112 if (!strcmp(name, "SerialLine"))
\r
113 return dupstr("/dev/ttyS0");
\r
117 int platform_default_i(const char *name, int def)
\r
119 if (!strcmp(name, "TermWidth") ||
\r
120 !strcmp(name, "TermHeight")) {
\r
121 struct winsize size;
\r
122 if (ioctl(STDIN_FILENO, TIOCGWINSZ, (void *)&size) >= 0)
\r
123 return (!strcmp(name, "TermWidth") ? size.ws_col : size.ws_row);
\r
128 FontSpec platform_default_fontspec(const char *name)
\r
135 Filename platform_default_filename(const char *name)
\r
138 if (!strcmp(name, "LogFileName"))
\r
139 strcpy(ret.path, "putty.log");
\r
145 char *x_get_default(const char *key)
\r
147 return NULL; /* this is a stub */
\r
149 int term_ldisc(Terminal *term, int mode)
\r
153 void ldisc_update(void *frontend, int echo, int edit)
\r
155 /* Update stdin read mode to reflect changes in line discipline. */
\r
156 struct termios mode;
\r
158 if (!local_tty) return;
\r
160 mode = orig_termios;
\r
163 mode.c_lflag |= ECHO;
\r
165 mode.c_lflag &= ~ECHO;
\r
168 mode.c_iflag |= ICRNL;
\r
169 mode.c_lflag |= ISIG | ICANON;
\r
170 mode.c_oflag |= OPOST;
\r
172 mode.c_iflag &= ~ICRNL;
\r
173 mode.c_lflag &= ~(ISIG | ICANON);
\r
174 mode.c_oflag &= ~OPOST;
\r
175 /* Solaris sets these to unhelpful values */
\r
176 mode.c_cc[VMIN] = 1;
\r
177 mode.c_cc[VTIME] = 0;
\r
178 /* FIXME: perhaps what we do with IXON/IXOFF should be an
\r
179 * argument to ldisc_update(), to allow implementation of SSH-2
\r
180 * "xon-xoff" and Rlogin's equivalent? */
\r
181 mode.c_iflag &= ~IXON;
\r
182 mode.c_iflag &= ~IXOFF;
\r
185 * Mark parity errors and (more important) BREAK on input. This
\r
186 * is more complex than it need be because POSIX-2001 suggests
\r
187 * that escaping of valid 0xff in the input stream is dependent on
\r
188 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
\r
189 * 2.0 goes one worse and makes it dependent on INPCK too. We
\r
190 * deal with this by forcing these flags into a useful state and
\r
191 * then faking the state in which we found them in from_tty() if
\r
192 * we get passed a parity or framing error.
\r
194 mode.c_iflag = (mode.c_iflag | INPCK | PARMRK) & ~IGNPAR;
\r
196 tcsetattr(STDIN_FILENO, TCSANOW, &mode);
\r
199 /* Helper function to extract a special character from a termios. */
\r
200 static char *get_ttychar(struct termios *t, int index)
\r
202 cc_t c = t->c_cc[index];
\r
203 #if defined(_POSIX_VDISABLE)
\r
204 if (c == _POSIX_VDISABLE)
\r
205 return dupprintf("");
\r
207 return dupprintf("^<%d>", c);
\r
210 char *get_ttymode(void *frontend, const char *mode)
\r
213 * Propagate appropriate terminal modes from the local terminal,
\r
216 if (!local_tty) return NULL;
\r
218 #define GET_CHAR(ourname, uxname) \
\r
220 if (strcmp(mode, ourname) == 0) \
\r
221 return get_ttychar(&orig_termios, uxname); \
\r
223 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
\r
225 if (strcmp(mode, ourname) == 0) { \
\r
226 int b = (orig_termios.uxmemb & uxname) != 0; \
\r
228 return dupprintf("%d", b); \
\r
233 * Modes that want to be the same on all terminal devices involved.
\r
235 /* All the special characters supported by SSH */
\r
237 GET_CHAR("INTR", VINTR);
\r
240 GET_CHAR("QUIT", VQUIT);
\r
242 #if defined(VERASE)
\r
243 GET_CHAR("ERASE", VERASE);
\r
246 GET_CHAR("KILL", VKILL);
\r
249 GET_CHAR("EOF", VEOF);
\r
252 GET_CHAR("EOL", VEOL);
\r
255 GET_CHAR("EOL2", VEOL2);
\r
257 #if defined(VSTART)
\r
258 GET_CHAR("START", VSTART);
\r
261 GET_CHAR("STOP", VSTOP);
\r
264 GET_CHAR("SUSP", VSUSP);
\r
266 #if defined(VDSUSP)
\r
267 GET_CHAR("DSUSP", VDSUSP);
\r
269 #if defined(VREPRINT)
\r
270 GET_CHAR("REPRINT", VREPRINT);
\r
272 #if defined(VWERASE)
\r
273 GET_CHAR("WERASE", VWERASE);
\r
275 #if defined(VLNEXT)
\r
276 GET_CHAR("LNEXT", VLNEXT);
\r
278 #if defined(VFLUSH)
\r
279 GET_CHAR("FLUSH", VFLUSH);
\r
281 #if defined(VSWTCH)
\r
282 GET_CHAR("SWTCH", VSWTCH);
\r
284 #if defined(VSTATUS)
\r
285 GET_CHAR("STATUS", VSTATUS);
\r
287 #if defined(VDISCARD)
\r
288 GET_CHAR("DISCARD", VDISCARD);
\r
290 /* Modes that "configure" other major modes. These should probably be
\r
291 * considered as user preferences. */
\r
292 /* Configuration of ICANON */
\r
294 GET_BOOL("ECHOK", ECHOK, c_lflag, );
\r
296 #if defined(ECHOKE)
\r
297 GET_BOOL("ECHOKE", ECHOKE, c_lflag, );
\r
300 GET_BOOL("ECHOE", ECHOE, c_lflag, );
\r
302 #if defined(ECHONL)
\r
303 GET_BOOL("ECHONL", ECHONL, c_lflag, );
\r
306 GET_BOOL("XCASE", XCASE, c_lflag, );
\r
308 /* Configuration of ECHO */
\r
309 #if defined(ECHOCTL)
\r
310 GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, );
\r
312 /* Configuration of IXON/IXOFF */
\r
314 GET_BOOL("IXANY", IXANY, c_iflag, );
\r
316 /* Configuration of OPOST */
\r
318 GET_BOOL("OLCUC", OLCUC, c_oflag, );
\r
321 GET_BOOL("ONLCR", ONLCR, c_oflag, );
\r
324 GET_BOOL("OCRNL", OCRNL, c_oflag, );
\r
327 GET_BOOL("ONOCR", ONOCR, c_oflag, );
\r
329 #if defined(ONLRET)
\r
330 GET_BOOL("ONLRET", ONLRET, c_oflag, );
\r
334 * Modes that want to be set in only one place, and that we have
\r
335 * squashed locally.
\r
338 GET_BOOL("ISIG", ISIG, c_lflag, );
\r
340 #if defined(ICANON)
\r
341 GET_BOOL("ICANON", ICANON, c_lflag, );
\r
344 GET_BOOL("ECHO", ECHO, c_lflag, );
\r
347 GET_BOOL("IXON", IXON, c_iflag, );
\r
350 GET_BOOL("IXOFF", IXOFF, c_iflag, );
\r
353 GET_BOOL("OPOST", OPOST, c_oflag, );
\r
357 * We do not propagate the following modes:
\r
358 * - Parity/serial settings, which are a local affair and don't
\r
359 * make sense propagated over SSH's 8-bit byte-stream.
\r
360 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
\r
361 * - Things that want to be enabled in one place that we don't
\r
366 * - Things I don't know what to do with. (FIXME)
\r
367 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
\r
368 * INLCR IGNCR ICRNL
\r
374 /* Fall through to here for unrecognised names, or ones that are
\r
375 * unsupported on this platform */
\r
379 void cleanup_termios(void)
\r
382 tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
\r
385 bufchain stdout_data, stderr_data;
\r
387 int try_output(int is_stderr)
\r
389 bufchain *chain = (is_stderr ? &stderr_data : &stdout_data);
\r
390 int fd = (is_stderr ? STDERR_FILENO : STDOUT_FILENO);
\r
392 int sendlen, ret, fl;
\r
394 if (bufchain_size(chain) == 0)
\r
395 return bufchain_size(&stdout_data) + bufchain_size(&stderr_data);
\r
397 fl = fcntl(fd, F_GETFL);
\r
398 if (fl != -1 && !(fl & O_NONBLOCK))
\r
399 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
\r
401 bufchain_prefix(chain, &senddata, &sendlen);
\r
402 ret = write(fd, senddata, sendlen);
\r
404 bufchain_consume(chain, ret);
\r
405 } while (ret == sendlen && bufchain_size(chain) != 0);
\r
406 if (fl != -1 && !(fl & O_NONBLOCK))
\r
407 fcntl(fd, F_SETFL, fl);
\r
408 if (ret < 0 && errno != EAGAIN) {
\r
409 perror(is_stderr ? "stderr: write" : "stdout: write");
\r
412 return bufchain_size(&stdout_data) + bufchain_size(&stderr_data);
\r
415 int from_backend(void *frontend_handle, int is_stderr,
\r
416 const char *data, int len)
\r
419 bufchain_add(&stderr_data, data, len);
\r
420 return try_output(TRUE);
\r
422 bufchain_add(&stdout_data, data, len);
\r
423 return try_output(FALSE);
\r
427 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
\r
430 * No "untrusted" output should get here (the way the code is
\r
431 * currently, it's all diverted by FLAG_STDERR).
\r
433 assert(!"Unexpected call to from_backend_untrusted()");
\r
434 return 0; /* not reached */
\r
437 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
\r
440 ret = cmdline_get_passwd_input(p, in, inlen);
\r
442 ret = console_get_userpass_input(p, in, inlen);
\r
447 * Handle data from a local tty in PARMRK format.
\r
449 static void from_tty(void *vbuf, unsigned len)
\r
451 char *p, *q, *end, *buf = vbuf;
\r
452 static enum {NORMAL, FF, FF00} state = NORMAL;
\r
454 p = buf; end = buf + len;
\r
458 if (*p == '\xff') {
\r
462 q = memchr(p, '\xff', end - p);
\r
463 if (q == NULL) q = end;
\r
464 back->send(backhandle, p, q - p);
\r
469 if (*p == '\xff') {
\r
470 back->send(backhandle, p, 1);
\r
473 } else if (*p == '\0') {
\r
480 back->special(backhandle, TS_BRK);
\r
483 * Pretend that PARMRK wasn't set. This involves
\r
484 * faking what INPCK and IGNPAR would have done if
\r
485 * we hadn't overridden them. Unfortunately, we
\r
486 * can't do this entirely correctly because INPCK
\r
487 * distinguishes between framing and parity
\r
488 * errors, but PARMRK format represents both in
\r
489 * the same way. We assume that parity errors are
\r
490 * more common than framing errors, and hence
\r
491 * treat all input errors as being subject to
\r
494 if (orig_termios.c_iflag & INPCK) {
\r
495 /* If IGNPAR is set, we throw away the character. */
\r
496 if (!(orig_termios.c_iflag & IGNPAR)) {
\r
497 /* PE/FE get passed on as NUL. */
\r
499 back->send(backhandle, p, 1);
\r
502 /* INPCK not set. Assume we got a parity error. */
\r
503 back->send(backhandle, p, 1);
\r
514 void sigwinch(int signum)
\r
516 if (write(signalpipe[1], "x", 1) <= 0)
\r
517 /* not much we can do about it */;
\r
521 * In Plink our selects are synchronous, so these functions are
\r
524 int uxsel_input_add(int fd, int rwx) { return 0; }
\r
525 void uxsel_input_remove(int id) { }
\r
528 * Short description of parameters.
\r
530 static void usage(void)
\r
532 printf("PuTTY Link: command-line connection utility\n");
\r
533 printf("%s\n", ver);
\r
534 printf("Usage: plink [options] [user@]host [command]\n");
\r
535 printf(" (\"host\" can also be a PuTTY saved session name)\n");
\r
536 printf("Options:\n");
\r
537 printf(" -V print version information and exit\n");
\r
538 printf(" -pgpfp print PGP key fingerprints and exit\n");
\r
539 printf(" -v show verbose messages\n");
\r
540 printf(" -load sessname Load settings from saved session\n");
\r
541 printf(" -ssh -telnet -rlogin -raw -serial\n");
\r
542 printf(" force use of a particular protocol\n");
\r
543 printf(" -P port connect to specified port\n");
\r
544 printf(" -l user connect with specified username\n");
\r
545 printf(" -batch disable all interactive prompts\n");
\r
546 printf("The following options only apply to SSH connections:\n");
\r
547 printf(" -pw passw login with specified password\n");
\r
548 printf(" -D [listen-IP:]listen-port\n");
\r
549 printf(" Dynamic SOCKS-based port forwarding\n");
\r
550 printf(" -L [listen-IP:]listen-port:host:port\n");
\r
551 printf(" Forward local port to remote address\n");
\r
552 printf(" -R [listen-IP:]listen-port:host:port\n");
\r
553 printf(" Forward remote port to local address\n");
\r
554 printf(" -X -x enable / disable X11 forwarding\n");
\r
555 printf(" -A -a enable / disable agent forwarding\n");
\r
556 printf(" -t -T enable / disable pty allocation\n");
\r
557 printf(" -1 -2 force use of particular protocol version\n");
\r
558 printf(" -4 -6 force use of IPv4 or IPv6\n");
\r
559 printf(" -C enable compression\n");
\r
560 printf(" -i key private key file for authentication\n");
\r
561 printf(" -noagent disable use of Pageant\n");
\r
562 printf(" -agent enable use of Pageant\n");
\r
563 printf(" -m file read remote command(s) from file\n");
\r
564 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
\r
565 printf(" -N don't start a shell/command (SSH-2 only)\n");
\r
566 printf(" -nc host:port\n");
\r
567 printf(" open tunnel in place of session (SSH-2 only)\n");
\r
568 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
\r
569 printf(" Specify the serial configuration (serial only)\n");
\r
573 static void version(void)
\r
575 printf("plink: %s\n", ver);
\r
579 int main(int argc, char **argv)
\r
582 int portnumber = -1;
\r
585 int i, fdcount, fdsize, fdstate;
\r
589 int use_subsystem = 0;
\r
590 int got_host = FALSE;
\r
594 fdcount = fdsize = 0;
\r
596 * Initialise port and protocol to sensible defaults. (These
\r
597 * will be overridden by more or less anything.)
\r
599 default_protocol = PROT_SSH;
\r
602 flags = FLAG_STDERR | FLAG_STDERR_TTY;
\r
606 * Process the command line.
\r
608 do_defaults(NULL, &cfg);
\r
609 loaded_session = FALSE;
\r
610 default_protocol = cfg.protocol;
\r
611 default_port = cfg.port;
\r
615 * Override the default protocol if PLINK_PROTOCOL is set.
\r
617 char *p = getenv("PLINK_PROTOCOL");
\r
619 const Backend *b = backend_from_name(p);
\r
621 default_protocol = cfg.protocol = b->protocol;
\r
622 default_port = cfg.port = b->default_port;
\r
629 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
\r
633 "plink: option \"%s\" requires an argument\n", p);
\r
635 } else if (ret == 2) {
\r
637 } else if (ret == 1) {
\r
639 } else if (!strcmp(p, "-batch")) {
\r
640 console_batch_mode = 1;
\r
641 } else if (!strcmp(p, "-s")) {
\r
642 /* Save status to write to cfg later. */
\r
644 } else if (!strcmp(p, "-V")) {
\r
646 } else if (!strcmp(p, "-pgpfp")) {
\r
647 pgp_fingerprints();
\r
649 } else if (!strcmp(p, "-o")) {
\r
652 "plink: option \"-o\" requires an argument\n");
\r
656 provide_xrm_string(*++argv);
\r
659 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
\r
663 if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {
\r
667 * If the hostname starts with "telnet:", set the
\r
668 * protocol to Telnet and process the string as a
\r
671 if (!strncmp(q, "telnet:", 7)) {
\r
675 if (q[0] == '/' && q[1] == '/')
\r
677 cfg.protocol = PROT_TELNET;
\r
679 while (*p && *p != ':' && *p != '/')
\r
685 cfg.port = atoi(p);
\r
688 strncpy(cfg.host, q, sizeof(cfg.host) - 1);
\r
689 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
692 char *r, *user, *host;
\r
694 * Before we process the [user@]host string, we
\r
695 * first check for the presence of a protocol
\r
696 * prefix (a protocol name followed by ",").
\r
698 r = strchr(p, ',');
\r
702 b = backend_from_name(p);
\r
704 default_protocol = cfg.protocol = b->protocol;
\r
705 portnumber = b->default_port;
\r
711 * A nonzero length string followed by an @ is treated
\r
712 * as a username. (We discount an _initial_ @.) The
\r
713 * rest of the string (or the whole string if no @)
\r
714 * is treated as a session name and/or hostname.
\r
716 r = strrchr(p, '@');
\r
718 p++, r = NULL; /* discount initial @ */
\r
721 user = p, host = r;
\r
723 user = NULL, host = p;
\r
727 * Now attempt to load a saved session with the
\r
728 * same name as the hostname.
\r
732 do_defaults(host, &cfg2);
\r
733 if (loaded_session || !cfg_launchable(&cfg2)) {
\r
734 /* No settings for this host; use defaults */
\r
735 /* (or session was already loaded with -load) */
\r
736 strncpy(cfg.host, host, sizeof(cfg.host) - 1);
\r
737 cfg.host[sizeof(cfg.host) - 1] = '\0';
\r
738 cfg.port = default_port;
\r
742 loaded_session = TRUE;
\r
747 /* Patch in specified username. */
\r
748 strncpy(cfg.username, user,
\r
749 sizeof(cfg.username) - 1);
\r
750 cfg.username[sizeof(cfg.username) - 1] = '\0';
\r
756 int cmdlen, cmdsize;
\r
757 cmdlen = cmdsize = 0;
\r
762 if (cmdlen >= cmdsize) {
\r
763 cmdsize = cmdlen + 512;
\r
764 command = sresize(command, cmdsize, char);
\r
766 command[cmdlen++]=*p++;
\r
768 if (cmdlen >= cmdsize) {
\r
769 cmdsize = cmdlen + 512;
\r
770 command = sresize(command, cmdsize, char);
\r
772 command[cmdlen++]=' '; /* always add trailing space */
\r
773 if (--argc) p = *++argv;
\r
775 if (cmdlen) command[--cmdlen]='\0';
\r
776 /* change trailing blank to NUL */
\r
777 cfg.remote_cmd_ptr = command;
\r
778 cfg.remote_cmd_ptr2 = NULL;
\r
779 cfg.nopty = TRUE; /* command => no terminal */
\r
781 break; /* done with cmdline */
\r
789 if (!cfg_launchable(&cfg) || !(got_host || loaded_session)) {
\r
794 * Trim leading whitespace off the hostname if it's there.
\r
797 int space = strspn(cfg.host, " \t");
\r
798 memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
\r
801 /* See if host is of the form user@host */
\r
802 if (cfg.host[0] != '\0') {
\r
803 char *atsign = strrchr(cfg.host, '@');
\r
804 /* Make sure we're not overflowing the user field */
\r
806 if (atsign - cfg.host < sizeof cfg.username) {
\r
807 strncpy(cfg.username, cfg.host, atsign - cfg.host);
\r
808 cfg.username[atsign - cfg.host] = '\0';
\r
810 memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
\r
815 * Perform command-line overrides on session configuration.
\r
817 cmdline_run_saved(&cfg);
\r
820 * Apply subsystem status.
\r
823 cfg.ssh_subsys = TRUE;
\r
826 * Trim a colon suffix off the hostname if it's there.
\r
828 cfg.host[strcspn(cfg.host, ":")] = '\0';
\r
831 * Remove any remaining whitespace from the hostname.
\r
834 int p1 = 0, p2 = 0;
\r
835 while (cfg.host[p2] != '\0') {
\r
836 if (cfg.host[p2] != ' ' && cfg.host[p2] != '\t') {
\r
837 cfg.host[p1] = cfg.host[p2];
\r
842 cfg.host[p1] = '\0';
\r
845 if (!cfg.remote_cmd_ptr && !*cfg.remote_cmd && !*cfg.ssh_nc_host)
\r
846 flags |= FLAG_INTERACTIVE;
\r
849 * Select protocol. This is farmed out into a table in a
\r
850 * separate file to enable an ssh-free variant.
\r
852 back = backend_from_proto(cfg.protocol);
\r
853 if (back == NULL) {
\r
855 "Internal fault: Unsupported protocol found\n");
\r
862 if (portnumber != -1)
\r
863 cfg.port = portnumber;
\r
866 * Set up the pipe we'll use to tell us about SIGWINCH.
\r
868 if (pipe(signalpipe) < 0) {
\r
872 putty_signal(SIGWINCH, sigwinch);
\r
878 * Unix Plink doesn't provide any way to add forwardings after the
\r
879 * connection is set up, so if there are none now, we can safely set
\r
880 * the "simple" flag.
\r
882 if (cfg.protocol == PROT_SSH && !cfg.x11_forward && !cfg.agentfwd &&
\r
883 cfg.portfwd[0] == '\0' && cfg.portfwd[1] == '\0')
\r
884 cfg.ssh_simple = TRUE;
\r
886 * Start up the connection.
\r
888 logctx = log_init(NULL, &cfg);
\r
889 console_provide_logctx(logctx);
\r
893 /* nodelay is only useful if stdin is a terminal device */
\r
894 int nodelay = cfg.tcp_nodelay && isatty(0);
\r
896 error = back->init(NULL, &backhandle, &cfg, cfg.host, cfg.port,
\r
897 &realhost, nodelay, cfg.tcp_keepalives);
\r
899 fprintf(stderr, "Unable to open connection:\n%s\n", error);
\r
902 back->provide_logctx(backhandle, logctx);
\r
903 ldisc_create(&cfg, NULL, back, backhandle, NULL);
\r
909 * Set up the initial console mode. We don't care if this call
\r
910 * fails, because we know we aren't necessarily running in a
\r
913 local_tty = (tcgetattr(STDIN_FILENO, &orig_termios) == 0);
\r
914 atexit(cleanup_termios);
\r
915 ldisc_update(NULL, 1, 1);
\r
917 now = GETTICKCOUNT();
\r
920 fd_set rset, wset, xset;
\r
930 FD_SET_MAX(signalpipe[0], maxfd, rset);
\r
932 if (connopen && !sending &&
\r
933 back->connected(backhandle) &&
\r
934 back->sendok(backhandle) &&
\r
935 back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
\r
936 /* If we're OK to send, then try to read from stdin. */
\r
937 FD_SET_MAX(STDIN_FILENO, maxfd, rset);
\r
940 if (bufchain_size(&stdout_data) > 0) {
\r
941 /* If we have data for stdout, try to write to stdout. */
\r
942 FD_SET_MAX(STDOUT_FILENO, maxfd, wset);
\r
945 if (bufchain_size(&stderr_data) > 0) {
\r
946 /* If we have data for stderr, try to write to stderr. */
\r
947 FD_SET_MAX(STDERR_FILENO, maxfd, wset);
\r
950 /* Count the currently active fds. */
\r
952 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
\r
953 fd = next_fd(&fdstate, &rwx)) i++;
\r
955 /* Expand the fdlist buffer if necessary. */
\r
958 fdlist = sresize(fdlist, fdsize, int);
\r
962 * Add all currently open fds to the select sets, and store
\r
963 * them in fdlist as well.
\r
966 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
\r
967 fd = next_fd(&fdstate, &rwx)) {
\r
968 fdlist[fdcount++] = fd;
\r
970 FD_SET_MAX(fd, maxfd, rset);
\r
972 FD_SET_MAX(fd, maxfd, wset);
\r
974 FD_SET_MAX(fd, maxfd, xset);
\r
979 struct timeval tv, *ptv;
\r
981 if (run_timers(now, &next)) {
\r
982 ticks = next - GETTICKCOUNT();
\r
983 if (ticks < 0) ticks = 0; /* just in case */
\r
984 tv.tv_sec = ticks / 1000;
\r
985 tv.tv_usec = ticks % 1000 * 1000;
\r
990 ret = select(maxfd, &rset, &wset, &xset, ptv);
\r
994 long newnow = GETTICKCOUNT();
\r
996 * Check to see whether the system clock has
\r
997 * changed massively during the select.
\r
999 if (newnow - now < 0 || newnow - now > next - now) {
\r
1001 * If so, look at the elapsed time in the
\r
1002 * select and use it to compute a new
\r
1003 * tickcount_offset.
\r
1005 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
\r
1006 /* So we'd like GETTICKCOUNT to have returned othernow,
\r
1007 * but instead it return newnow. Hence ... */
\r
1008 tickcount_offset += othernow - newnow;
\r
1014 } while (ret < 0 && errno == EINTR);
\r
1021 for (i = 0; i < fdcount; i++) {
\r
1024 * We must process exceptional notifications before
\r
1025 * ordinary readability ones, or we may go straight
\r
1026 * past the urgent marker.
\r
1028 if (FD_ISSET(fd, &xset))
\r
1029 select_result(fd, 4);
\r
1030 if (FD_ISSET(fd, &rset))
\r
1031 select_result(fd, 1);
\r
1032 if (FD_ISSET(fd, &wset))
\r
1033 select_result(fd, 2);
\r
1036 if (FD_ISSET(signalpipe[0], &rset)) {
\r
1038 struct winsize size;
\r
1039 if (read(signalpipe[0], c, 1) <= 0)
\r
1040 /* ignore error */;
\r
1041 /* ignore its value; it'll be `x' */
\r
1042 if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0)
\r
1043 back->size(backhandle, size.ws_col, size.ws_row);
\r
1046 if (FD_ISSET(STDIN_FILENO, &rset)) {
\r
1050 if (connopen && back->connected(backhandle)) {
\r
1051 ret = read(STDIN_FILENO, buf, sizeof(buf));
\r
1053 perror("stdin: read");
\r
1055 } else if (ret == 0) {
\r
1056 back->special(backhandle, TS_EOF);
\r
1057 sending = FALSE; /* send nothing further after this */
\r
1060 from_tty(buf, ret);
\r
1062 back->send(backhandle, buf, ret);
\r
1067 if (FD_ISSET(STDOUT_FILENO, &wset)) {
\r
1068 back->unthrottle(backhandle, try_output(FALSE));
\r
1071 if (FD_ISSET(STDERR_FILENO, &wset)) {
\r
1072 back->unthrottle(backhandle, try_output(TRUE));
\r
1075 if ((!connopen || !back->connected(backhandle)) &&
\r
1076 bufchain_size(&stdout_data) == 0 &&
\r
1077 bufchain_size(&stderr_data) == 0)
\r
1078 break; /* we closed the connection */
\r
1080 exitcode = back->exitcode(backhandle);
\r
1081 if (exitcode < 0) {
\r
1082 fprintf(stderr, "Remote process exit code unavailable\n");
\r
1083 exitcode = 1; /* this is an error condition */
\r
1085 cleanup_exit(exitcode);
\r
1086 return exitcode; /* shouldn't happen, but placates gcc */
\r