2 * PLink - a command-line (stdin/stdout) variant of PuTTY.
15 #include <sys/ioctl.h>
17 #ifndef HAVE_NO_SYS_SELECT_H
18 #include <sys/select.h>
21 #define PUTTY_DO_GLOBALS /* actually _define_ globals */
26 #define MAX_STDIN_BACKLOG 4096
30 static struct termios orig_termios;
32 void fatalbox(char *p, ...)
37 fprintf(stderr, "FATAL ERROR: ");
39 vfprintf(stderr, p, ap);
49 void modalfatalbox(char *p, ...)
54 fprintf(stderr, "FATAL ERROR: ");
56 vfprintf(stderr, p, ap);
66 void connection_fatal(void *frontend, char *p, ...)
71 fprintf(stderr, "FATAL ERROR: ");
73 vfprintf(stderr, p, ap);
83 void cmdline_error(char *p, ...)
88 fprintf(stderr, "plink: ");
90 vfprintf(stderr, p, ap);
97 static int local_tty = FALSE; /* do we have a local tty? */
100 static void *backhandle;
104 * Default settings that are specific to pterm.
106 char *platform_default_s(const char *name)
108 if (!strcmp(name, "TermType"))
109 return dupstr(getenv("TERM"));
110 if (!strcmp(name, "UserName"))
111 return get_username();
112 if (!strcmp(name, "SerialLine"))
113 return dupstr("/dev/ttyS0");
117 int platform_default_i(const char *name, int def)
119 if (!strcmp(name, "TermWidth") ||
120 !strcmp(name, "TermHeight")) {
122 if (ioctl(STDIN_FILENO, TIOCGWINSZ, (void *)&size) >= 0)
123 return (!strcmp(name, "TermWidth") ? size.ws_col : size.ws_row);
128 FontSpec *platform_default_fontspec(const char *name)
130 return fontspec_new("");
133 Filename *platform_default_filename(const char *name)
135 if (!strcmp(name, "LogFileName"))
136 return filename_from_str("putty.log");
138 return filename_from_str("");
141 char *x_get_default(const char *key)
143 return NULL; /* this is a stub */
145 int term_ldisc(Terminal *term, int mode)
149 void ldisc_update(void *frontend, int echo, int edit)
151 /* Update stdin read mode to reflect changes in line discipline. */
154 if (!local_tty) return;
159 mode.c_lflag |= ECHO;
161 mode.c_lflag &= ~ECHO;
164 mode.c_iflag |= ICRNL;
165 mode.c_lflag |= ISIG | ICANON;
166 mode.c_oflag |= OPOST;
168 mode.c_iflag &= ~ICRNL;
169 mode.c_lflag &= ~(ISIG | ICANON);
170 mode.c_oflag &= ~OPOST;
171 /* Solaris sets these to unhelpful values */
173 mode.c_cc[VTIME] = 0;
174 /* FIXME: perhaps what we do with IXON/IXOFF should be an
175 * argument to ldisc_update(), to allow implementation of SSH-2
176 * "xon-xoff" and Rlogin's equivalent? */
177 mode.c_iflag &= ~IXON;
178 mode.c_iflag &= ~IXOFF;
181 * Mark parity errors and (more important) BREAK on input. This
182 * is more complex than it need be because POSIX-2001 suggests
183 * that escaping of valid 0xff in the input stream is dependent on
184 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
185 * 2.0 goes one worse and makes it dependent on INPCK too. We
186 * deal with this by forcing these flags into a useful state and
187 * then faking the state in which we found them in from_tty() if
188 * we get passed a parity or framing error.
190 mode.c_iflag = (mode.c_iflag | INPCK | PARMRK) & ~IGNPAR;
192 tcsetattr(STDIN_FILENO, TCSANOW, &mode);
195 /* Helper function to extract a special character from a termios. */
196 static char *get_ttychar(struct termios *t, int index)
198 cc_t c = t->c_cc[index];
199 #if defined(_POSIX_VDISABLE)
200 if (c == _POSIX_VDISABLE)
201 return dupprintf("");
203 return dupprintf("^<%d>", c);
206 char *get_ttymode(void *frontend, const char *mode)
209 * Propagate appropriate terminal modes from the local terminal,
212 if (!local_tty) return NULL;
214 #define GET_CHAR(ourname, uxname) \
216 if (strcmp(mode, ourname) == 0) \
217 return get_ttychar(&orig_termios, uxname); \
219 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
221 if (strcmp(mode, ourname) == 0) { \
222 int b = (orig_termios.uxmemb & uxname) != 0; \
224 return dupprintf("%d", b); \
229 * Modes that want to be the same on all terminal devices involved.
231 /* All the special characters supported by SSH */
233 GET_CHAR("INTR", VINTR);
236 GET_CHAR("QUIT", VQUIT);
239 GET_CHAR("ERASE", VERASE);
242 GET_CHAR("KILL", VKILL);
245 GET_CHAR("EOF", VEOF);
248 GET_CHAR("EOL", VEOL);
251 GET_CHAR("EOL2", VEOL2);
254 GET_CHAR("START", VSTART);
257 GET_CHAR("STOP", VSTOP);
260 GET_CHAR("SUSP", VSUSP);
263 GET_CHAR("DSUSP", VDSUSP);
265 #if defined(VREPRINT)
266 GET_CHAR("REPRINT", VREPRINT);
269 GET_CHAR("WERASE", VWERASE);
272 GET_CHAR("LNEXT", VLNEXT);
275 GET_CHAR("FLUSH", VFLUSH);
278 GET_CHAR("SWTCH", VSWTCH);
281 GET_CHAR("STATUS", VSTATUS);
283 #if defined(VDISCARD)
284 GET_CHAR("DISCARD", VDISCARD);
286 /* Modes that "configure" other major modes. These should probably be
287 * considered as user preferences. */
288 /* Configuration of ICANON */
290 GET_BOOL("ECHOK", ECHOK, c_lflag, );
293 GET_BOOL("ECHOKE", ECHOKE, c_lflag, );
296 GET_BOOL("ECHOE", ECHOE, c_lflag, );
299 GET_BOOL("ECHONL", ECHONL, c_lflag, );
302 GET_BOOL("XCASE", XCASE, c_lflag, );
304 /* Configuration of ECHO */
306 GET_BOOL("ECHOCTL", ECHOCTL, c_lflag, );
308 /* Configuration of IXON/IXOFF */
310 GET_BOOL("IXANY", IXANY, c_iflag, );
312 /* Configuration of OPOST */
314 GET_BOOL("OLCUC", OLCUC, c_oflag, );
317 GET_BOOL("ONLCR", ONLCR, c_oflag, );
320 GET_BOOL("OCRNL", OCRNL, c_oflag, );
323 GET_BOOL("ONOCR", ONOCR, c_oflag, );
326 GET_BOOL("ONLRET", ONLRET, c_oflag, );
330 * Modes that want to be set in only one place, and that we have
334 GET_BOOL("ISIG", ISIG, c_lflag, );
337 GET_BOOL("ICANON", ICANON, c_lflag, );
340 GET_BOOL("ECHO", ECHO, c_lflag, );
343 GET_BOOL("IXON", IXON, c_iflag, );
346 GET_BOOL("IXOFF", IXOFF, c_iflag, );
349 GET_BOOL("OPOST", OPOST, c_oflag, );
353 * We do not propagate the following modes:
354 * - Parity/serial settings, which are a local affair and don't
355 * make sense propagated over SSH's 8-bit byte-stream.
356 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
357 * - Things that want to be enabled in one place that we don't
362 * - Things I don't know what to do with. (FIXME)
363 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
370 /* Fall through to here for unrecognised names, or ones that are
371 * unsupported on this platform */
375 void cleanup_termios(void)
378 tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios);
381 bufchain stdout_data, stderr_data;
382 enum { EOF_NO, EOF_PENDING, EOF_SENT } outgoingeof;
384 int try_output(int is_stderr)
386 bufchain *chain = (is_stderr ? &stderr_data : &stdout_data);
387 int fd = (is_stderr ? STDERR_FILENO : STDOUT_FILENO);
389 int sendlen, ret, fl;
391 if (bufchain_size(chain) > 0) {
392 fl = fcntl(fd, F_GETFL);
393 if (fl != -1 && !(fl & O_NONBLOCK))
394 fcntl(fd, F_SETFL, fl | O_NONBLOCK);
396 bufchain_prefix(chain, &senddata, &sendlen);
397 ret = write(fd, senddata, sendlen);
399 bufchain_consume(chain, ret);
400 } while (ret == sendlen && bufchain_size(chain) != 0);
401 if (fl != -1 && !(fl & O_NONBLOCK))
402 fcntl(fd, F_SETFL, fl);
403 if (ret < 0 && errno != EAGAIN) {
404 perror(is_stderr ? "stderr: write" : "stdout: write");
408 if (outgoingeof == EOF_PENDING && bufchain_size(&stdout_data) == 0) {
409 close(STDOUT_FILENO);
410 outgoingeof = EOF_SENT;
412 return bufchain_size(&stdout_data) + bufchain_size(&stderr_data);
415 int from_backend(void *frontend_handle, int is_stderr,
416 const char *data, int len)
419 bufchain_add(&stderr_data, data, len);
420 return try_output(TRUE);
422 assert(outgoingeof == EOF_NO);
423 bufchain_add(&stdout_data, data, len);
424 return try_output(FALSE);
428 int from_backend_untrusted(void *frontend_handle, const char *data, int len)
431 * No "untrusted" output should get here (the way the code is
432 * currently, it's all diverted by FLAG_STDERR).
434 assert(!"Unexpected call to from_backend_untrusted()");
435 return 0; /* not reached */
438 int from_backend_eof(void *frontend_handle)
440 assert(outgoingeof == EOF_NO);
441 outgoingeof = EOF_PENDING;
443 return FALSE; /* do not respond to incoming EOF with outgoing */
446 int get_userpass_input(prompts_t *p, unsigned char *in, int inlen)
449 ret = cmdline_get_passwd_input(p, in, inlen);
451 ret = console_get_userpass_input(p, in, inlen);
456 * Handle data from a local tty in PARMRK format.
458 static void from_tty(void *vbuf, unsigned len)
460 char *p, *q, *end, *buf = vbuf;
461 static enum {NORMAL, FF, FF00} state = NORMAL;
463 p = buf; end = buf + len;
471 q = memchr(p, '\xff', end - p);
472 if (q == NULL) q = end;
473 back->send(backhandle, p, q - p);
479 back->send(backhandle, p, 1);
482 } else if (*p == '\0') {
489 back->special(backhandle, TS_BRK);
492 * Pretend that PARMRK wasn't set. This involves
493 * faking what INPCK and IGNPAR would have done if
494 * we hadn't overridden them. Unfortunately, we
495 * can't do this entirely correctly because INPCK
496 * distinguishes between framing and parity
497 * errors, but PARMRK format represents both in
498 * the same way. We assume that parity errors are
499 * more common than framing errors, and hence
500 * treat all input errors as being subject to
503 if (orig_termios.c_iflag & INPCK) {
504 /* If IGNPAR is set, we throw away the character. */
505 if (!(orig_termios.c_iflag & IGNPAR)) {
506 /* PE/FE get passed on as NUL. */
508 back->send(backhandle, p, 1);
511 /* INPCK not set. Assume we got a parity error. */
512 back->send(backhandle, p, 1);
523 void sigwinch(int signum)
525 if (write(signalpipe[1], "x", 1) <= 0)
526 /* not much we can do about it */;
530 * In Plink our selects are synchronous, so these functions are
533 int uxsel_input_add(int fd, int rwx) { return 0; }
534 void uxsel_input_remove(int id) { }
537 * Short description of parameters.
539 static void usage(void)
541 printf("PuTTY Link: command-line connection utility\n");
543 printf("Usage: plink [options] [user@]host [command]\n");
544 printf(" (\"host\" can also be a PuTTY saved session name)\n");
545 printf("Options:\n");
546 printf(" -V print version information and exit\n");
547 printf(" -pgpfp print PGP key fingerprints and exit\n");
548 printf(" -v show verbose messages\n");
549 printf(" -load sessname Load settings from saved session\n");
550 printf(" -ssh -telnet -rlogin -raw -serial\n");
551 printf(" force use of a particular protocol\n");
552 printf(" -P port connect to specified port\n");
553 printf(" -l user connect with specified username\n");
554 printf(" -batch disable all interactive prompts\n");
555 printf("The following options only apply to SSH connections:\n");
556 printf(" -pw passw login with specified password\n");
557 printf(" -D [listen-IP:]listen-port\n");
558 printf(" Dynamic SOCKS-based port forwarding\n");
559 printf(" -L [listen-IP:]listen-port:host:port\n");
560 printf(" Forward local port to remote address\n");
561 printf(" -R [listen-IP:]listen-port:host:port\n");
562 printf(" Forward remote port to local address\n");
563 printf(" -X -x enable / disable X11 forwarding\n");
564 printf(" -A -a enable / disable agent forwarding\n");
565 printf(" -t -T enable / disable pty allocation\n");
566 printf(" -1 -2 force use of particular protocol version\n");
567 printf(" -4 -6 force use of IPv4 or IPv6\n");
568 printf(" -C enable compression\n");
569 printf(" -i key private key file for authentication\n");
570 printf(" -noagent disable use of Pageant\n");
571 printf(" -agent enable use of Pageant\n");
572 printf(" -m file read remote command(s) from file\n");
573 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
574 printf(" -N don't start a shell/command (SSH-2 only)\n");
575 printf(" -nc host:port\n");
576 printf(" open tunnel in place of session (SSH-2 only)\n");
577 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
578 printf(" Specify the serial configuration (serial only)\n");
582 static void version(void)
584 printf("plink: %s\n", ver);
588 int main(int argc, char **argv)
594 int i, fdcount, fdsize, fdstate;
598 int use_subsystem = 0;
599 int got_host = FALSE;
603 fdcount = fdsize = 0;
605 * Initialise port and protocol to sensible defaults. (These
606 * will be overridden by more or less anything.)
608 default_protocol = PROT_SSH;
611 bufchain_init(&stdout_data);
612 bufchain_init(&stderr_data);
613 outgoingeof = EOF_NO;
615 flags = FLAG_STDERR | FLAG_STDERR_TTY;
619 * Process the command line.
622 do_defaults(NULL, conf);
623 loaded_session = FALSE;
624 default_protocol = conf_get_int(conf, CONF_protocol);
625 default_port = conf_get_int(conf, CONF_port);
629 * Override the default protocol if PLINK_PROTOCOL is set.
631 char *p = getenv("PLINK_PROTOCOL");
633 const Backend *b = backend_from_name(p);
635 default_protocol = b->protocol;
636 default_port = b->default_port;
637 conf_set_int(conf, CONF_protocol, default_protocol);
638 conf_set_int(conf, CONF_port, default_port);
645 int ret = cmdline_process_param(p, (argc > 1 ? argv[1] : NULL),
649 "plink: option \"%s\" requires an argument\n", p);
651 } else if (ret == 2) {
653 } else if (ret == 1) {
655 } else if (!strcmp(p, "-batch")) {
656 console_batch_mode = 1;
657 } else if (!strcmp(p, "-s")) {
658 /* Save status to write to conf later. */
660 } else if (!strcmp(p, "-V")) {
662 } else if (!strcmp(p, "-pgpfp")) {
665 } else if (!strcmp(p, "-o")) {
668 "plink: option \"-o\" requires an argument\n");
672 provide_xrm_string(*++argv);
675 fprintf(stderr, "plink: unknown option \"%s\"\n", p);
679 if (!conf_launchable(conf) || !(got_host || loaded_session)) {
683 * If the hostname starts with "telnet:", set the
684 * protocol to Telnet and process the string as a
687 if (!strncmp(q, "telnet:", 7)) {
691 if (q[0] == '/' && q[1] == '/')
693 conf_set_int(conf, CONF_protocol, PROT_TELNET);
695 while (*p && *p != ':' && *p != '/')
701 conf_set_int(conf, CONF_port, atoi(p));
703 conf_set_int(conf, CONF_port, -1);
704 conf_set_str(conf, CONF_host, q);
707 char *r, *user, *host;
709 * Before we process the [user@]host string, we
710 * first check for the presence of a protocol
711 * prefix (a protocol name followed by ",").
717 b = backend_from_name(p);
719 default_protocol = b->protocol;
720 conf_set_int(conf, CONF_protocol,
722 portnumber = b->default_port;
728 * A nonzero length string followed by an @ is treated
729 * as a username. (We discount an _initial_ @.) The
730 * rest of the string (or the whole string if no @)
731 * is treated as a session name and/or hostname.
735 p++, r = NULL; /* discount initial @ */
740 user = NULL, host = p;
744 * Now attempt to load a saved session with the
745 * same name as the hostname.
748 Conf *conf2 = conf_new();
749 do_defaults(host, conf2);
750 if (loaded_session || !conf_launchable(conf2)) {
751 /* No settings for this host; use defaults */
752 /* (or session was already loaded with -load) */
753 conf_set_str(conf, CONF_host, host);
754 conf_set_int(conf, CONF_port, default_port);
757 conf_copy_into(conf, conf2);
758 loaded_session = TRUE;
764 /* Patch in specified username. */
765 conf_set_str(conf, CONF_username, user);
772 cmdlen = cmdsize = 0;
777 if (cmdlen >= cmdsize) {
778 cmdsize = cmdlen + 512;
779 command = sresize(command, cmdsize, char);
781 command[cmdlen++]=*p++;
783 if (cmdlen >= cmdsize) {
784 cmdsize = cmdlen + 512;
785 command = sresize(command, cmdsize, char);
787 command[cmdlen++]=' '; /* always add trailing space */
788 if (--argc) p = *++argv;
790 if (cmdlen) command[--cmdlen]='\0';
791 /* change trailing blank to NUL */
792 conf_set_str(conf, CONF_remote_cmd, command);
793 conf_set_str(conf, CONF_remote_cmd2, "");
794 conf_set_int(conf, CONF_nopty, TRUE); /* command => no tty */
796 break; /* done with cmdline */
804 if (!conf_launchable(conf) || !(got_host || loaded_session)) {
809 * Muck about with the hostname in various ways.
812 char *hostbuf = dupstr(conf_get_str(conf, CONF_host));
813 char *host = hostbuf;
817 * Trim leading whitespace.
819 host += strspn(host, " \t");
822 * See if host is of the form user@host, and separate out
823 * the username if so.
825 if (host[0] != '\0') {
826 char *atsign = strrchr(host, '@');
829 conf_set_str(conf, CONF_username, host);
835 * Trim off a colon suffix if it's there.
837 host[strcspn(host, ":")] = '\0';
840 * Remove any remaining whitespace.
845 if (*q != ' ' && *q != '\t')
851 conf_set_str(conf, CONF_host, hostbuf);
856 * Perform command-line overrides on session configuration.
858 cmdline_run_saved(conf);
861 * Apply subsystem status.
864 conf_set_int(conf, CONF_ssh_subsys, TRUE);
866 if (!*conf_get_str(conf, CONF_remote_cmd) &&
867 !*conf_get_str(conf, CONF_remote_cmd2) &&
868 !*conf_get_str(conf, CONF_ssh_nc_host))
869 flags |= FLAG_INTERACTIVE;
872 * Select protocol. This is farmed out into a table in a
873 * separate file to enable an ssh-free variant.
875 back = backend_from_proto(conf_get_int(conf, CONF_protocol));
878 "Internal fault: Unsupported protocol found\n");
885 if (portnumber != -1)
886 conf_set_int(conf, CONF_port, portnumber);
889 * Set up the pipe we'll use to tell us about SIGWINCH.
891 if (pipe(signalpipe) < 0) {
895 putty_signal(SIGWINCH, sigwinch);
901 * Unix Plink doesn't provide any way to add forwardings after the
902 * connection is set up, so if there are none now, we can safely set
905 if (conf_get_int(conf, CONF_protocol) == PROT_SSH &&
906 !conf_get_int(conf, CONF_x11_forward) &&
907 !conf_get_int(conf, CONF_agentfwd) &&
908 !conf_get_str_nthstrkey(conf, CONF_portfwd, 0))
909 conf_set_int(conf, CONF_ssh_simple, TRUE);
912 * Start up the connection.
914 logctx = log_init(NULL, conf);
915 console_provide_logctx(logctx);
919 /* nodelay is only useful if stdin is a terminal device */
920 int nodelay = conf_get_int(conf, CONF_tcp_nodelay) && isatty(0);
922 error = back->init(NULL, &backhandle, conf,
923 conf_get_str(conf, CONF_host),
924 conf_get_int(conf, CONF_port),
926 conf_get_int(conf, CONF_tcp_keepalives));
928 fprintf(stderr, "Unable to open connection:\n%s\n", error);
931 back->provide_logctx(backhandle, logctx);
932 ldisc_create(conf, NULL, back, backhandle, NULL);
938 * Set up the initial console mode. We don't care if this call
939 * fails, because we know we aren't necessarily running in a
942 local_tty = (tcgetattr(STDIN_FILENO, &orig_termios) == 0);
943 atexit(cleanup_termios);
944 ldisc_update(NULL, 1, 1);
946 now = GETTICKCOUNT();
949 fd_set rset, wset, xset;
959 FD_SET_MAX(signalpipe[0], maxfd, rset);
961 if (connopen && !sending &&
962 back->connected(backhandle) &&
963 back->sendok(backhandle) &&
964 back->sendbuffer(backhandle) < MAX_STDIN_BACKLOG) {
965 /* If we're OK to send, then try to read from stdin. */
966 FD_SET_MAX(STDIN_FILENO, maxfd, rset);
969 if (bufchain_size(&stdout_data) > 0) {
970 /* If we have data for stdout, try to write to stdout. */
971 FD_SET_MAX(STDOUT_FILENO, maxfd, wset);
974 if (bufchain_size(&stderr_data) > 0) {
975 /* If we have data for stderr, try to write to stderr. */
976 FD_SET_MAX(STDERR_FILENO, maxfd, wset);
979 /* Count the currently active fds. */
981 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
982 fd = next_fd(&fdstate, &rwx)) i++;
984 /* Expand the fdlist buffer if necessary. */
987 fdlist = sresize(fdlist, fdsize, int);
991 * Add all currently open fds to the select sets, and store
992 * them in fdlist as well.
995 for (fd = first_fd(&fdstate, &rwx); fd >= 0;
996 fd = next_fd(&fdstate, &rwx)) {
997 fdlist[fdcount++] = fd;
999 FD_SET_MAX(fd, maxfd, rset);
1001 FD_SET_MAX(fd, maxfd, wset);
1003 FD_SET_MAX(fd, maxfd, xset);
1008 struct timeval tv, *ptv;
1010 if (run_timers(now, &next)) {
1011 ticks = next - GETTICKCOUNT();
1012 if (ticks < 0) ticks = 0; /* just in case */
1013 tv.tv_sec = ticks / 1000;
1014 tv.tv_usec = ticks % 1000 * 1000;
1019 ret = select(maxfd, &rset, &wset, &xset, ptv);
1023 long newnow = GETTICKCOUNT();
1025 * Check to see whether the system clock has
1026 * changed massively during the select.
1028 if (newnow - now < 0 || newnow - now > next - now) {
1030 * If so, look at the elapsed time in the
1031 * select and use it to compute a new
1034 long othernow = now + tv.tv_sec * 1000 + tv.tv_usec / 1000;
1035 /* So we'd like GETTICKCOUNT to have returned othernow,
1036 * but instead it return newnow. Hence ... */
1037 tickcount_offset += othernow - newnow;
1043 } while (ret < 0 && errno == EINTR);
1050 for (i = 0; i < fdcount; i++) {
1053 * We must process exceptional notifications before
1054 * ordinary readability ones, or we may go straight
1055 * past the urgent marker.
1057 if (FD_ISSET(fd, &xset))
1058 select_result(fd, 4);
1059 if (FD_ISSET(fd, &rset))
1060 select_result(fd, 1);
1061 if (FD_ISSET(fd, &wset))
1062 select_result(fd, 2);
1065 if (FD_ISSET(signalpipe[0], &rset)) {
1067 struct winsize size;
1068 if (read(signalpipe[0], c, 1) <= 0)
1070 /* ignore its value; it'll be `x' */
1071 if (ioctl(0, TIOCGWINSZ, (void *)&size) >= 0)
1072 back->size(backhandle, size.ws_col, size.ws_row);
1075 if (FD_ISSET(STDIN_FILENO, &rset)) {
1079 if (connopen && back->connected(backhandle)) {
1080 ret = read(STDIN_FILENO, buf, sizeof(buf));
1082 perror("stdin: read");
1084 } else if (ret == 0) {
1085 back->special(backhandle, TS_EOF);
1086 sending = FALSE; /* send nothing further after this */
1091 back->send(backhandle, buf, ret);
1096 if (FD_ISSET(STDOUT_FILENO, &wset)) {
1097 back->unthrottle(backhandle, try_output(FALSE));
1100 if (FD_ISSET(STDERR_FILENO, &wset)) {
1101 back->unthrottle(backhandle, try_output(TRUE));
1104 if ((!connopen || !back->connected(backhandle)) &&
1105 bufchain_size(&stdout_data) == 0 &&
1106 bufchain_size(&stderr_data) == 0)
1107 break; /* we closed the connection */
1109 exitcode = back->exitcode(backhandle);
1111 fprintf(stderr, "Remote process exit code unavailable\n");
1112 exitcode = 1; /* this is an error condition */
1114 cleanup_exit(exitcode);
1115 return exitcode; /* shouldn't happen, but placates gcc */