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
)
122 FontSpec
*platform_default_fontspec(const char *name
)
124 return fontspec_new("");
127 Filename
*platform_default_filename(const char *name
)
129 if (!strcmp(name
, "LogFileName"))
130 return filename_from_str("putty.log");
132 return filename_from_str("");
135 char *x_get_default(const char *key
)
137 return NULL
; /* this is a stub */
139 int term_ldisc(Terminal
*term
, int mode
)
143 void ldisc_update(void *frontend
, int echo
, int edit
)
145 /* Update stdin read mode to reflect changes in line discipline. */
148 if (!local_tty
) return;
153 mode
.c_lflag
|= ECHO
;
155 mode
.c_lflag
&= ~ECHO
;
158 mode
.c_iflag
|= ICRNL
;
159 mode
.c_lflag
|= ISIG
| ICANON
;
160 mode
.c_oflag
|= OPOST
;
162 mode
.c_iflag
&= ~ICRNL
;
163 mode
.c_lflag
&= ~(ISIG
| ICANON
);
164 mode
.c_oflag
&= ~OPOST
;
165 /* Solaris sets these to unhelpful values */
167 mode
.c_cc
[VTIME
] = 0;
168 /* FIXME: perhaps what we do with IXON/IXOFF should be an
169 * argument to ldisc_update(), to allow implementation of SSH-2
170 * "xon-xoff" and Rlogin's equivalent? */
171 mode
.c_iflag
&= ~IXON
;
172 mode
.c_iflag
&= ~IXOFF
;
175 * Mark parity errors and (more important) BREAK on input. This
176 * is more complex than it need be because POSIX-2001 suggests
177 * that escaping of valid 0xff in the input stream is dependent on
178 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
179 * 2.0 goes one worse and makes it dependent on INPCK too. We
180 * deal with this by forcing these flags into a useful state and
181 * then faking the state in which we found them in from_tty() if
182 * we get passed a parity or framing error.
184 mode
.c_iflag
= (mode
.c_iflag
| INPCK
| PARMRK
) & ~IGNPAR
;
186 tcsetattr(STDIN_FILENO
, TCSANOW
, &mode
);
189 /* Helper function to extract a special character from a termios. */
190 static char *get_ttychar(struct termios
*t
, int index
)
192 cc_t c
= t
->c_cc
[index
];
193 #if defined(_POSIX_VDISABLE)
194 if (c
== _POSIX_VDISABLE
)
195 return dupprintf("");
197 return dupprintf("^<%d>", c
);
200 char *get_ttymode(void *frontend
, const char *mode
)
203 * Propagate appropriate terminal modes from the local terminal,
206 if (!local_tty
) return NULL
;
208 #define GET_CHAR(ourname, uxname) \
210 if (strcmp(mode, ourname) == 0) \
211 return get_ttychar(&orig_termios, uxname); \
213 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
215 if (strcmp(mode, ourname) == 0) { \
216 int b = (orig_termios.uxmemb & uxname) != 0; \
218 return dupprintf("%d", b); \
223 * Modes that want to be the same on all terminal devices involved.
225 /* All the special characters supported by SSH */
227 GET_CHAR("INTR", VINTR
);
230 GET_CHAR("QUIT", VQUIT
);
233 GET_CHAR("ERASE", VERASE
);
236 GET_CHAR("KILL", VKILL
);
239 GET_CHAR("EOF", VEOF
);
242 GET_CHAR("EOL", VEOL
);
245 GET_CHAR("EOL2", VEOL2
);
248 GET_CHAR("START", VSTART
);
251 GET_CHAR("STOP", VSTOP
);
254 GET_CHAR("SUSP", VSUSP
);
257 GET_CHAR("DSUSP", VDSUSP
);
259 #if defined(VREPRINT)
260 GET_CHAR("REPRINT", VREPRINT
);
263 GET_CHAR("WERASE", VWERASE
);
266 GET_CHAR("LNEXT", VLNEXT
);
269 GET_CHAR("FLUSH", VFLUSH
);
272 GET_CHAR("SWTCH", VSWTCH
);
275 GET_CHAR("STATUS", VSTATUS
);
277 #if defined(VDISCARD)
278 GET_CHAR("DISCARD", VDISCARD
);
280 /* Modes that "configure" other major modes. These should probably be
281 * considered as user preferences. */
282 /* Configuration of ICANON */
284 GET_BOOL("ECHOK", ECHOK
, c_lflag
, );
287 GET_BOOL("ECHOKE", ECHOKE
, c_lflag
, );
290 GET_BOOL("ECHOE", ECHOE
, c_lflag
, );
293 GET_BOOL("ECHONL", ECHONL
, c_lflag
, );
296 GET_BOOL("XCASE", XCASE
, c_lflag
, );
298 /* Configuration of ECHO */
300 GET_BOOL("ECHOCTL", ECHOCTL
, c_lflag
, );
302 /* Configuration of IXON/IXOFF */
304 GET_BOOL("IXANY", IXANY
, c_iflag
, );
306 /* Configuration of OPOST */
308 GET_BOOL("OLCUC", OLCUC
, c_oflag
, );
311 GET_BOOL("ONLCR", ONLCR
, c_oflag
, );
314 GET_BOOL("OCRNL", OCRNL
, c_oflag
, );
317 GET_BOOL("ONOCR", ONOCR
, c_oflag
, );
320 GET_BOOL("ONLRET", ONLRET
, c_oflag
, );
324 * Modes that want to be set in only one place, and that we have
328 GET_BOOL("ISIG", ISIG
, c_lflag
, );
331 GET_BOOL("ICANON", ICANON
, c_lflag
, );
334 GET_BOOL("ECHO", ECHO
, c_lflag
, );
337 GET_BOOL("IXON", IXON
, c_iflag
, );
340 GET_BOOL("IXOFF", IXOFF
, c_iflag
, );
343 GET_BOOL("OPOST", OPOST
, c_oflag
, );
347 * We do not propagate the following modes:
348 * - Parity/serial settings, which are a local affair and don't
349 * make sense propagated over SSH's 8-bit byte-stream.
350 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
351 * - Things that want to be enabled in one place that we don't
356 * - Things I don't know what to do with. (FIXME)
357 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
364 /* Fall through to here for unrecognised names, or ones that are
365 * unsupported on this platform */
369 void cleanup_termios(void)
372 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_termios
);
375 bufchain stdout_data
, stderr_data
;
376 enum { EOF_NO
, EOF_PENDING
, EOF_SENT
} outgoingeof
;
378 int try_output(int is_stderr
)
380 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
381 int fd
= (is_stderr ? STDERR_FILENO
: STDOUT_FILENO
);
383 int sendlen
, ret
, fl
;
385 if (bufchain_size(chain
) > 0) {
386 fl
= fcntl(fd
, F_GETFL
);
387 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
388 fcntl(fd
, F_SETFL
, fl
| O_NONBLOCK
);
390 bufchain_prefix(chain
, &senddata
, &sendlen
);
391 ret
= write(fd
, senddata
, sendlen
);
393 bufchain_consume(chain
, ret
);
394 } while (ret
== sendlen
&& bufchain_size(chain
) != 0);
395 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
396 fcntl(fd
, F_SETFL
, fl
);
397 if (ret
< 0 && errno
!= EAGAIN
) {
398 perror(is_stderr ?
"stderr: write" : "stdout: write");
402 if (outgoingeof
== EOF_PENDING
&& bufchain_size(&stdout_data
) == 0) {
403 close(STDOUT_FILENO
);
404 outgoingeof
= EOF_SENT
;
406 return bufchain_size(&stdout_data
) + bufchain_size(&stderr_data
);
409 int from_backend(void *frontend_handle
, int is_stderr
,
410 const char *data
, int len
)
413 bufchain_add(&stderr_data
, data
, len
);
414 return try_output(TRUE
);
416 assert(outgoingeof
== EOF_NO
);
417 bufchain_add(&stdout_data
, data
, len
);
418 return try_output(FALSE
);
422 int from_backend_untrusted(void *frontend_handle
, const char *data
, int len
)
425 * No "untrusted" output should get here (the way the code is
426 * currently, it's all diverted by FLAG_STDERR).
428 assert(!"Unexpected call to from_backend_untrusted()");
429 return 0; /* not reached */
432 int from_backend_eof(void *frontend_handle
)
434 assert(outgoingeof
== EOF_NO
);
435 outgoingeof
= EOF_PENDING
;
437 return FALSE
; /* do not respond to incoming EOF with outgoing */
440 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
443 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
445 ret
= console_get_userpass_input(p
, in
, inlen
);
450 * Handle data from a local tty in PARMRK format.
452 static void from_tty(void *vbuf
, unsigned len
)
454 char *p
, *q
, *end
, *buf
= vbuf
;
455 static enum {NORMAL
, FF
, FF00
} state
= NORMAL
;
457 p
= buf
; end
= buf
+ len
;
465 q
= memchr(p
, '\xff', end
- p
);
466 if (q
== NULL
) q
= end
;
467 back
->send(backhandle
, p
, q
- p
);
473 back
->send(backhandle
, p
, 1);
476 } else if (*p
== '\0') {
483 back
->special(backhandle
, TS_BRK
);
486 * Pretend that PARMRK wasn't set. This involves
487 * faking what INPCK and IGNPAR would have done if
488 * we hadn't overridden them. Unfortunately, we
489 * can't do this entirely correctly because INPCK
490 * distinguishes between framing and parity
491 * errors, but PARMRK format represents both in
492 * the same way. We assume that parity errors are
493 * more common than framing errors, and hence
494 * treat all input errors as being subject to
497 if (orig_termios
.c_iflag
& INPCK
) {
498 /* If IGNPAR is set, we throw away the character. */
499 if (!(orig_termios
.c_iflag
& IGNPAR
)) {
500 /* PE/FE get passed on as NUL. */
502 back
->send(backhandle
, p
, 1);
505 /* INPCK not set. Assume we got a parity error. */
506 back
->send(backhandle
, p
, 1);
517 void sigwinch(int signum
)
519 if (write(signalpipe
[1], "x", 1) <= 0)
520 /* not much we can do about it */;
524 * In Plink our selects are synchronous, so these functions are
527 int uxsel_input_add(int fd
, int rwx
) { return 0; }
528 void uxsel_input_remove(int id
) { }
531 * Short description of parameters.
533 static void usage(void)
535 printf("PuTTY Link: command-line connection utility\n");
537 printf("Usage: plink [options] [user@]host [command]\n");
538 printf(" (\"host\" can also be a PuTTY saved session name)\n");
539 printf("Options:\n");
540 printf(" -V print version information and exit\n");
541 printf(" -pgpfp print PGP key fingerprints and exit\n");
542 printf(" -v show verbose messages\n");
543 printf(" -load sessname Load settings from saved session\n");
544 printf(" -ssh -telnet -rlogin -raw -serial\n");
545 printf(" force use of a particular protocol\n");
546 printf(" -P port connect to specified port\n");
547 printf(" -l user connect with specified username\n");
548 printf(" -batch disable all interactive prompts\n");
549 printf("The following options only apply to SSH connections:\n");
550 printf(" -pw passw login with specified password\n");
551 printf(" -D [listen-IP:]listen-port\n");
552 printf(" Dynamic SOCKS-based port forwarding\n");
553 printf(" -L [listen-IP:]listen-port:host:port\n");
554 printf(" Forward local port to remote address\n");
555 printf(" -R [listen-IP:]listen-port:host:port\n");
556 printf(" Forward remote port to local address\n");
557 printf(" -X -x enable / disable X11 forwarding\n");
558 printf(" -A -a enable / disable agent forwarding\n");
559 printf(" -t -T enable / disable pty allocation\n");
560 printf(" -1 -2 force use of particular protocol version\n");
561 printf(" -4 -6 force use of IPv4 or IPv6\n");
562 printf(" -C enable compression\n");
563 printf(" -i key private key file for authentication\n");
564 printf(" -noagent disable use of Pageant\n");
565 printf(" -agent enable use of Pageant\n");
566 printf(" -m file read remote command(s) from file\n");
567 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
568 printf(" -N don't start a shell/command (SSH-2 only)\n");
569 printf(" -nc host:port\n");
570 printf(" open tunnel in place of session (SSH-2 only)\n");
571 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
572 printf(" Specify the serial configuration (serial only)\n");
576 static void version(void)
578 printf("plink: %s\n", ver
);
582 void frontend_net_error_pending(void) {}
584 int main(int argc
, char **argv
)
590 int i
, fdcount
, fdsize
, fdstate
;
594 int use_subsystem
= 0;
595 int got_host
= FALSE
;
600 fdcount
= fdsize
= 0;
602 * Initialise port and protocol to sensible defaults. (These
603 * will be overridden by more or less anything.)
605 default_protocol
= PROT_SSH
;
608 bufchain_init(&stdout_data
);
609 bufchain_init(&stderr_data
);
610 outgoingeof
= EOF_NO
;
612 flags
= FLAG_STDERR
| FLAG_STDERR_TTY
;
616 * Process the command line.
619 do_defaults(NULL
, conf
);
620 loaded_session
= FALSE
;
621 default_protocol
= conf_get_int(conf
, CONF_protocol
);
622 default_port
= conf_get_int(conf
, CONF_port
);
626 * Override the default protocol if PLINK_PROTOCOL is set.
628 char *p
= getenv("PLINK_PROTOCOL");
630 const Backend
*b
= backend_from_name(p
);
632 default_protocol
= b
->protocol
;
633 default_port
= b
->default_port
;
634 conf_set_int(conf
, CONF_protocol
, default_protocol
);
635 conf_set_int(conf
, CONF_port
, default_port
);
642 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
646 "plink: option \"%s\" requires an argument\n", p
);
648 } else if (ret
== 2) {
650 } else if (ret
== 1) {
652 } else if (!strcmp(p
, "-batch")) {
653 console_batch_mode
= 1;
654 } else if (!strcmp(p
, "-s")) {
655 /* Save status to write to conf later. */
657 } else if (!strcmp(p
, "-V")) {
659 } else if (!strcmp(p
, "-pgpfp")) {
662 } else if (!strcmp(p
, "-o")) {
665 "plink: option \"-o\" requires an argument\n");
669 provide_xrm_string(*++argv
);
672 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
676 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
680 * If the hostname starts with "telnet:", set the
681 * protocol to Telnet and process the string as a
684 if (!strncmp(q
, "telnet:", 7)) {
688 if (q
[0] == '/' && q
[1] == '/')
690 conf_set_int(conf
, CONF_protocol
, PROT_TELNET
);
692 while (*p
&& *p
!= ':' && *p
!= '/')
698 conf_set_int(conf
, CONF_port
, atoi(p
));
700 conf_set_int(conf
, CONF_port
, -1);
701 conf_set_str(conf
, CONF_host
, q
);
704 char *r
, *user
, *host
;
706 * Before we process the [user@]host string, we
707 * first check for the presence of a protocol
708 * prefix (a protocol name followed by ",").
714 b
= backend_from_name(p
);
716 default_protocol
= b
->protocol
;
717 conf_set_int(conf
, CONF_protocol
,
719 portnumber
= b
->default_port
;
725 * A nonzero length string followed by an @ is treated
726 * as a username. (We discount an _initial_ @.) The
727 * rest of the string (or the whole string if no @)
728 * is treated as a session name and/or hostname.
732 p
++, r
= NULL
; /* discount initial @ */
737 user
= NULL
, host
= p
;
741 * Now attempt to load a saved session with the
742 * same name as the hostname.
745 Conf
*conf2
= conf_new();
746 do_defaults(host
, conf2
);
747 if (loaded_session
|| !conf_launchable(conf2
)) {
748 /* No settings for this host; use defaults */
749 /* (or session was already loaded with -load) */
750 conf_set_str(conf
, CONF_host
, host
);
751 conf_set_int(conf
, CONF_port
, default_port
);
754 conf_copy_into(conf
, conf2
);
755 loaded_session
= TRUE
;
761 /* Patch in specified username. */
762 conf_set_str(conf
, CONF_username
, user
);
769 cmdlen
= cmdsize
= 0;
774 if (cmdlen
>= cmdsize
) {
775 cmdsize
= cmdlen
+ 512;
776 command
= sresize(command
, cmdsize
, char);
778 command
[cmdlen
++]=*p
++;
780 if (cmdlen
>= cmdsize
) {
781 cmdsize
= cmdlen
+ 512;
782 command
= sresize(command
, cmdsize
, char);
784 command
[cmdlen
++]=' '; /* always add trailing space */
785 if (--argc
) p
= *++argv
;
787 if (cmdlen
) command
[--cmdlen
]='\0';
788 /* change trailing blank to NUL */
789 conf_set_str(conf
, CONF_remote_cmd
, command
);
790 conf_set_str(conf
, CONF_remote_cmd2
, "");
791 conf_set_int(conf
, CONF_nopty
, TRUE
); /* command => no tty */
793 break; /* done with cmdline */
801 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
806 * Muck about with the hostname in various ways.
809 char *hostbuf
= dupstr(conf_get_str(conf
, CONF_host
));
810 char *host
= hostbuf
;
814 * Trim leading whitespace.
816 host
+= strspn(host
, " \t");
819 * See if host is of the form user@host, and separate out
820 * the username if so.
822 if (host
[0] != '\0') {
823 char *atsign
= strrchr(host
, '@');
826 conf_set_str(conf
, CONF_username
, host
);
832 * Trim off a colon suffix if it's there.
834 host
[strcspn(host
, ":")] = '\0';
837 * Remove any remaining whitespace.
842 if (*q
!= ' ' && *q
!= '\t')
848 conf_set_str(conf
, CONF_host
, hostbuf
);
853 * Perform command-line overrides on session configuration.
855 cmdline_run_saved(conf
);
858 * Apply subsystem status.
861 conf_set_int(conf
, CONF_ssh_subsys
, TRUE
);
863 if (!*conf_get_str(conf
, CONF_remote_cmd
) &&
864 !*conf_get_str(conf
, CONF_remote_cmd2
) &&
865 !*conf_get_str(conf
, CONF_ssh_nc_host
))
866 flags
|= FLAG_INTERACTIVE
;
869 * Select protocol. This is farmed out into a table in a
870 * separate file to enable an ssh-free variant.
872 back
= backend_from_proto(conf_get_int(conf
, CONF_protocol
));
875 "Internal fault: Unsupported protocol found\n");
882 if (portnumber
!= -1)
883 conf_set_int(conf
, CONF_port
, portnumber
);
886 * Block SIGPIPE, so that we'll get EPIPE individually on
887 * particular network connections that go wrong.
889 putty_signal(SIGPIPE
, SIG_IGN
);
892 * Set up the pipe we'll use to tell us about SIGWINCH.
894 if (pipe(signalpipe
) < 0) {
898 putty_signal(SIGWINCH
, sigwinch
);
901 * Now that we've got the SIGWINCH handler installed, try to find
902 * out the initial terminal size.
904 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &size
) >= 0) {
905 conf_set_int(conf
, CONF_width
, size
.ws_col
);
906 conf_set_int(conf
, CONF_height
, size
.ws_row
);
913 * Unix Plink doesn't provide any way to add forwardings after the
914 * connection is set up, so if there are none now, we can safely set
917 if (conf_get_int(conf
, CONF_protocol
) == PROT_SSH
&&
918 !conf_get_int(conf
, CONF_x11_forward
) &&
919 !conf_get_int(conf
, CONF_agentfwd
) &&
920 !conf_get_str_nthstrkey(conf
, CONF_portfwd
, 0))
921 conf_set_int(conf
, CONF_ssh_simple
, TRUE
);
924 * Start up the connection.
926 logctx
= log_init(NULL
, conf
);
927 console_provide_logctx(logctx
);
931 /* nodelay is only useful if stdin is a terminal device */
932 int nodelay
= conf_get_int(conf
, CONF_tcp_nodelay
) && isatty(0);
934 error
= back
->init(NULL
, &backhandle
, conf
,
935 conf_get_str(conf
, CONF_host
),
936 conf_get_int(conf
, CONF_port
),
938 conf_get_int(conf
, CONF_tcp_keepalives
));
940 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
943 back
->provide_logctx(backhandle
, logctx
);
944 ldisc_create(conf
, NULL
, back
, backhandle
, NULL
);
950 * Set up the initial console mode. We don't care if this call
951 * fails, because we know we aren't necessarily running in a
954 local_tty
= (tcgetattr(STDIN_FILENO
, &orig_termios
) == 0);
955 atexit(cleanup_termios
);
956 ldisc_update(NULL
, 1, 1);
958 now
= GETTICKCOUNT();
961 fd_set rset
, wset
, xset
;
971 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
973 if (connopen
&& !sending
&&
974 back
->connected(backhandle
) &&
975 back
->sendok(backhandle
) &&
976 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
977 /* If we're OK to send, then try to read from stdin. */
978 FD_SET_MAX(STDIN_FILENO
, maxfd
, rset
);
981 if (bufchain_size(&stdout_data
) > 0) {
982 /* If we have data for stdout, try to write to stdout. */
983 FD_SET_MAX(STDOUT_FILENO
, maxfd
, wset
);
986 if (bufchain_size(&stderr_data
) > 0) {
987 /* If we have data for stderr, try to write to stderr. */
988 FD_SET_MAX(STDERR_FILENO
, maxfd
, wset
);
991 /* Count the currently active fds. */
993 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
994 fd
= next_fd(&fdstate
, &rwx
)) i
++;
996 /* Expand the fdlist buffer if necessary. */
999 fdlist
= sresize(fdlist
, fdsize
, int);
1003 * Add all currently open fds to the select sets, and store
1004 * them in fdlist as well.
1007 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
1008 fd
= next_fd(&fdstate
, &rwx
)) {
1009 fdlist
[fdcount
++] = fd
;
1011 FD_SET_MAX(fd
, maxfd
, rset
);
1013 FD_SET_MAX(fd
, maxfd
, wset
);
1015 FD_SET_MAX(fd
, maxfd
, xset
);
1020 struct timeval tv
, *ptv
;
1022 if (run_timers(now
, &next
)) {
1023 ticks
= next
- GETTICKCOUNT();
1024 if (ticks
< 0) ticks
= 0; /* just in case */
1025 tv
.tv_sec
= ticks
/ 1000;
1026 tv
.tv_usec
= ticks
% 1000 * 1000;
1031 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
1035 now
= GETTICKCOUNT();
1036 } while (ret
< 0 && errno
== EINTR
);
1043 for (i
= 0; i
< fdcount
; i
++) {
1046 * We must process exceptional notifications before
1047 * ordinary readability ones, or we may go straight
1048 * past the urgent marker.
1050 if (FD_ISSET(fd
, &xset
))
1051 select_result(fd
, 4);
1052 if (FD_ISSET(fd
, &rset
))
1053 select_result(fd
, 1);
1054 if (FD_ISSET(fd
, &wset
))
1055 select_result(fd
, 2);
1058 if (FD_ISSET(signalpipe
[0], &rset
)) {
1060 struct winsize size
;
1061 if (read(signalpipe
[0], c
, 1) <= 0)
1063 /* ignore its value; it'll be `x' */
1064 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, (void *)&size
) >= 0)
1065 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
1068 if (FD_ISSET(STDIN_FILENO
, &rset
)) {
1072 if (connopen
&& back
->connected(backhandle
)) {
1073 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
1075 perror("stdin: read");
1077 } else if (ret
== 0) {
1078 back
->special(backhandle
, TS_EOF
);
1079 sending
= FALSE
; /* send nothing further after this */
1084 back
->send(backhandle
, buf
, ret
);
1089 if (FD_ISSET(STDOUT_FILENO
, &wset
)) {
1090 back
->unthrottle(backhandle
, try_output(FALSE
));
1093 if (FD_ISSET(STDERR_FILENO
, &wset
)) {
1094 back
->unthrottle(backhandle
, try_output(TRUE
));
1097 net_pending_errors();
1099 if ((!connopen
|| !back
->connected(backhandle
)) &&
1100 bufchain_size(&stdout_data
) == 0 &&
1101 bufchain_size(&stderr_data
) == 0)
1102 break; /* we closed the connection */
1104 exitcode
= back
->exitcode(backhandle
);
1106 fprintf(stderr
, "Remote process exit code unavailable\n");
1107 exitcode
= 1; /* this is an error condition */
1109 cleanup_exit(exitcode
);
1110 return exitcode
; /* shouldn't happen, but placates gcc */