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
)
136 if (!strcmp(name
, "LogFileName"))
137 strcpy(ret
.path
, "putty.log");
143 char *x_get_default(const char *key
)
145 return NULL
; /* this is a stub */
147 int term_ldisc(Terminal
*term
, int mode
)
151 void ldisc_update(void *frontend
, int echo
, int edit
)
153 /* Update stdin read mode to reflect changes in line discipline. */
156 if (!local_tty
) return;
161 mode
.c_lflag
|= ECHO
;
163 mode
.c_lflag
&= ~ECHO
;
166 mode
.c_iflag
|= ICRNL
;
167 mode
.c_lflag
|= ISIG
| ICANON
;
168 mode
.c_oflag
|= OPOST
;
170 mode
.c_iflag
&= ~ICRNL
;
171 mode
.c_lflag
&= ~(ISIG
| ICANON
);
172 mode
.c_oflag
&= ~OPOST
;
173 /* Solaris sets these to unhelpful values */
175 mode
.c_cc
[VTIME
] = 0;
176 /* FIXME: perhaps what we do with IXON/IXOFF should be an
177 * argument to ldisc_update(), to allow implementation of SSH-2
178 * "xon-xoff" and Rlogin's equivalent? */
179 mode
.c_iflag
&= ~IXON
;
180 mode
.c_iflag
&= ~IXOFF
;
183 * Mark parity errors and (more important) BREAK on input. This
184 * is more complex than it need be because POSIX-2001 suggests
185 * that escaping of valid 0xff in the input stream is dependent on
186 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
187 * 2.0 goes one worse and makes it dependent on INPCK too. We
188 * deal with this by forcing these flags into a useful state and
189 * then faking the state in which we found them in from_tty() if
190 * we get passed a parity or framing error.
192 mode
.c_iflag
= (mode
.c_iflag
| INPCK
| PARMRK
) & ~IGNPAR
;
194 tcsetattr(STDIN_FILENO
, TCSANOW
, &mode
);
197 /* Helper function to extract a special character from a termios. */
198 static char *get_ttychar(struct termios
*t
, int index
)
200 cc_t c
= t
->c_cc
[index
];
201 #if defined(_POSIX_VDISABLE)
202 if (c
== _POSIX_VDISABLE
)
203 return dupprintf("");
205 return dupprintf("^<%d>", c
);
208 char *get_ttymode(void *frontend
, const char *mode
)
211 * Propagate appropriate terminal modes from the local terminal,
214 if (!local_tty
) return NULL
;
216 #define GET_CHAR(ourname, uxname) \
218 if (strcmp(mode, ourname) == 0) \
219 return get_ttychar(&orig_termios, uxname); \
221 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
223 if (strcmp(mode, ourname) == 0) { \
224 int b = (orig_termios.uxmemb & uxname) != 0; \
226 return dupprintf("%d", b); \
231 * Modes that want to be the same on all terminal devices involved.
233 /* All the special characters supported by SSH */
235 GET_CHAR("INTR", VINTR
);
238 GET_CHAR("QUIT", VQUIT
);
241 GET_CHAR("ERASE", VERASE
);
244 GET_CHAR("KILL", VKILL
);
247 GET_CHAR("EOF", VEOF
);
250 GET_CHAR("EOL", VEOL
);
253 GET_CHAR("EOL2", VEOL2
);
256 GET_CHAR("START", VSTART
);
259 GET_CHAR("STOP", VSTOP
);
262 GET_CHAR("SUSP", VSUSP
);
265 GET_CHAR("DSUSP", VDSUSP
);
267 #if defined(VREPRINT)
268 GET_CHAR("REPRINT", VREPRINT
);
271 GET_CHAR("WERASE", VWERASE
);
274 GET_CHAR("LNEXT", VLNEXT
);
277 GET_CHAR("FLUSH", VFLUSH
);
280 GET_CHAR("SWTCH", VSWTCH
);
283 GET_CHAR("STATUS", VSTATUS
);
285 #if defined(VDISCARD)
286 GET_CHAR("DISCARD", VDISCARD
);
288 /* Modes that "configure" other major modes. These should probably be
289 * considered as user preferences. */
290 /* Configuration of ICANON */
292 GET_BOOL("ECHOK", ECHOK
, c_lflag
, );
295 GET_BOOL("ECHOKE", ECHOKE
, c_lflag
, );
298 GET_BOOL("ECHOE", ECHOE
, c_lflag
, );
301 GET_BOOL("ECHONL", ECHONL
, c_lflag
, );
304 GET_BOOL("XCASE", XCASE
, c_lflag
, );
306 /* Configuration of ECHO */
308 GET_BOOL("ECHOCTL", ECHOCTL
, c_lflag
, );
310 /* Configuration of IXON/IXOFF */
312 GET_BOOL("IXANY", IXANY
, c_iflag
, );
314 /* Configuration of OPOST */
316 GET_BOOL("OLCUC", OLCUC
, c_oflag
, );
319 GET_BOOL("ONLCR", ONLCR
, c_oflag
, );
322 GET_BOOL("OCRNL", OCRNL
, c_oflag
, );
325 GET_BOOL("ONOCR", ONOCR
, c_oflag
, );
328 GET_BOOL("ONLRET", ONLRET
, c_oflag
, );
332 * Modes that want to be set in only one place, and that we have
336 GET_BOOL("ISIG", ISIG
, c_lflag
, );
339 GET_BOOL("ICANON", ICANON
, c_lflag
, );
342 GET_BOOL("ECHO", ECHO
, c_lflag
, );
345 GET_BOOL("IXON", IXON
, c_iflag
, );
348 GET_BOOL("IXOFF", IXOFF
, c_iflag
, );
351 GET_BOOL("OPOST", OPOST
, c_oflag
, );
355 * We do not propagate the following modes:
356 * - Parity/serial settings, which are a local affair and don't
357 * make sense propagated over SSH's 8-bit byte-stream.
358 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
359 * - Things that want to be enabled in one place that we don't
364 * - Things I don't know what to do with. (FIXME)
365 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
372 /* Fall through to here for unrecognised names, or ones that are
373 * unsupported on this platform */
377 void cleanup_termios(void)
380 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_termios
);
383 bufchain stdout_data
, stderr_data
;
384 enum { EOF_NO
, EOF_PENDING
, EOF_SENT
} outgoingeof
;
386 int try_output(int is_stderr
)
388 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
389 int fd
= (is_stderr ? STDERR_FILENO
: STDOUT_FILENO
);
391 int sendlen
, ret
, fl
;
393 if (bufchain_size(chain
) > 0) {
394 fl
= fcntl(fd
, F_GETFL
);
395 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
396 fcntl(fd
, F_SETFL
, fl
| O_NONBLOCK
);
398 bufchain_prefix(chain
, &senddata
, &sendlen
);
399 ret
= write(fd
, senddata
, sendlen
);
401 bufchain_consume(chain
, ret
);
402 } while (ret
== sendlen
&& bufchain_size(chain
) != 0);
403 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
404 fcntl(fd
, F_SETFL
, fl
);
405 if (ret
< 0 && errno
!= EAGAIN
) {
406 perror(is_stderr ?
"stderr: write" : "stdout: write");
410 if (outgoingeof
== EOF_PENDING
&& bufchain_size(&stdout_data
) == 0) {
411 close(STDOUT_FILENO
);
412 outgoingeof
= EOF_SENT
;
414 return bufchain_size(&stdout_data
) + bufchain_size(&stderr_data
);
417 int from_backend(void *frontend_handle
, int is_stderr
,
418 const char *data
, int len
)
421 bufchain_add(&stderr_data
, data
, len
);
422 return try_output(TRUE
);
424 assert(outgoingeof
== EOF_NO
);
425 bufchain_add(&stdout_data
, data
, len
);
426 return try_output(FALSE
);
430 int from_backend_untrusted(void *frontend_handle
, const char *data
, int len
)
433 * No "untrusted" output should get here (the way the code is
434 * currently, it's all diverted by FLAG_STDERR).
436 assert(!"Unexpected call to from_backend_untrusted()");
437 return 0; /* not reached */
440 int from_backend_eof(void *frontend_handle
)
442 assert(outgoingeof
== EOF_NO
);
443 outgoingeof
= EOF_PENDING
;
445 return FALSE
; /* do not respond to incoming EOF with outgoing */
448 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
451 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
453 ret
= console_get_userpass_input(p
, in
, inlen
);
458 * Handle data from a local tty in PARMRK format.
460 static void from_tty(void *vbuf
, unsigned len
)
462 char *p
, *q
, *end
, *buf
= vbuf
;
463 static enum {NORMAL
, FF
, FF00
} state
= NORMAL
;
465 p
= buf
; end
= buf
+ len
;
473 q
= memchr(p
, '\xff', end
- p
);
474 if (q
== NULL
) q
= end
;
475 back
->send(backhandle
, p
, q
- p
);
481 back
->send(backhandle
, p
, 1);
484 } else if (*p
== '\0') {
491 back
->special(backhandle
, TS_BRK
);
494 * Pretend that PARMRK wasn't set. This involves
495 * faking what INPCK and IGNPAR would have done if
496 * we hadn't overridden them. Unfortunately, we
497 * can't do this entirely correctly because INPCK
498 * distinguishes between framing and parity
499 * errors, but PARMRK format represents both in
500 * the same way. We assume that parity errors are
501 * more common than framing errors, and hence
502 * treat all input errors as being subject to
505 if (orig_termios
.c_iflag
& INPCK
) {
506 /* If IGNPAR is set, we throw away the character. */
507 if (!(orig_termios
.c_iflag
& IGNPAR
)) {
508 /* PE/FE get passed on as NUL. */
510 back
->send(backhandle
, p
, 1);
513 /* INPCK not set. Assume we got a parity error. */
514 back
->send(backhandle
, p
, 1);
525 void sigwinch(int signum
)
527 if (write(signalpipe
[1], "x", 1) <= 0)
528 /* not much we can do about it */;
532 * In Plink our selects are synchronous, so these functions are
535 int uxsel_input_add(int fd
, int rwx
) { return 0; }
536 void uxsel_input_remove(int id
) { }
539 * Short description of parameters.
541 static void usage(void)
543 printf("PuTTY Link: command-line connection utility\n");
545 printf("Usage: plink [options] [user@]host [command]\n");
546 printf(" (\"host\" can also be a PuTTY saved session name)\n");
547 printf("Options:\n");
548 printf(" -V print version information and exit\n");
549 printf(" -pgpfp print PGP key fingerprints and exit\n");
550 printf(" -v show verbose messages\n");
551 printf(" -load sessname Load settings from saved session\n");
552 printf(" -ssh -telnet -rlogin -raw -serial\n");
553 printf(" force use of a particular protocol\n");
554 printf(" -P port connect to specified port\n");
555 printf(" -l user connect with specified username\n");
556 printf(" -batch disable all interactive prompts\n");
557 printf("The following options only apply to SSH connections:\n");
558 printf(" -pw passw login with specified password\n");
559 printf(" -D [listen-IP:]listen-port\n");
560 printf(" Dynamic SOCKS-based port forwarding\n");
561 printf(" -L [listen-IP:]listen-port:host:port\n");
562 printf(" Forward local port to remote address\n");
563 printf(" -R [listen-IP:]listen-port:host:port\n");
564 printf(" Forward remote port to local address\n");
565 printf(" -X -x enable / disable X11 forwarding\n");
566 printf(" -A -a enable / disable agent forwarding\n");
567 printf(" -t -T enable / disable pty allocation\n");
568 printf(" -1 -2 force use of particular protocol version\n");
569 printf(" -4 -6 force use of IPv4 or IPv6\n");
570 printf(" -C enable compression\n");
571 printf(" -i key private key file for authentication\n");
572 printf(" -noagent disable use of Pageant\n");
573 printf(" -agent enable use of Pageant\n");
574 printf(" -m file read remote command(s) from file\n");
575 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
576 printf(" -N don't start a shell/command (SSH-2 only)\n");
577 printf(" -nc host:port\n");
578 printf(" open tunnel in place of session (SSH-2 only)\n");
579 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
580 printf(" Specify the serial configuration (serial only)\n");
584 static void version(void)
586 printf("plink: %s\n", ver
);
590 int main(int argc
, char **argv
)
596 int i
, fdcount
, fdsize
, fdstate
;
600 int use_subsystem
= 0;
601 int got_host
= FALSE
;
605 fdcount
= fdsize
= 0;
607 * Initialise port and protocol to sensible defaults. (These
608 * will be overridden by more or less anything.)
610 default_protocol
= PROT_SSH
;
613 bufchain_init(&stdout_data
);
614 bufchain_init(&stderr_data
);
615 outgoingeof
= EOF_NO
;
617 flags
= FLAG_STDERR
| FLAG_STDERR_TTY
;
621 * Process the command line.
624 do_defaults(NULL
, conf
);
625 loaded_session
= FALSE
;
626 default_protocol
= conf_get_int(conf
, CONF_protocol
);
627 default_port
= conf_get_int(conf
, CONF_port
);
631 * Override the default protocol if PLINK_PROTOCOL is set.
633 char *p
= getenv("PLINK_PROTOCOL");
635 const Backend
*b
= backend_from_name(p
);
637 default_protocol
= b
->protocol
;
638 default_port
= b
->default_port
;
639 conf_set_int(conf
, CONF_protocol
, default_protocol
);
640 conf_set_int(conf
, CONF_port
, default_port
);
647 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
651 "plink: option \"%s\" requires an argument\n", p
);
653 } else if (ret
== 2) {
655 } else if (ret
== 1) {
657 } else if (!strcmp(p
, "-batch")) {
658 console_batch_mode
= 1;
659 } else if (!strcmp(p
, "-s")) {
660 /* Save status to write to conf later. */
662 } else if (!strcmp(p
, "-V")) {
664 } else if (!strcmp(p
, "-pgpfp")) {
667 } else if (!strcmp(p
, "-o")) {
670 "plink: option \"-o\" requires an argument\n");
674 provide_xrm_string(*++argv
);
677 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
681 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
685 * If the hostname starts with "telnet:", set the
686 * protocol to Telnet and process the string as a
689 if (!strncmp(q
, "telnet:", 7)) {
693 if (q
[0] == '/' && q
[1] == '/')
695 conf_set_int(conf
, CONF_protocol
, PROT_TELNET
);
697 while (*p
&& *p
!= ':' && *p
!= '/')
703 conf_set_int(conf
, CONF_port
, atoi(p
));
705 conf_set_int(conf
, CONF_port
, -1);
706 conf_set_str(conf
, CONF_host
, q
);
709 char *r
, *user
, *host
;
711 * Before we process the [user@]host string, we
712 * first check for the presence of a protocol
713 * prefix (a protocol name followed by ",").
719 b
= backend_from_name(p
);
721 default_protocol
= b
->protocol
;
722 conf_set_int(conf
, CONF_protocol
,
724 portnumber
= b
->default_port
;
730 * A nonzero length string followed by an @ is treated
731 * as a username. (We discount an _initial_ @.) The
732 * rest of the string (or the whole string if no @)
733 * is treated as a session name and/or hostname.
737 p
++, r
= NULL
; /* discount initial @ */
742 user
= NULL
, host
= p
;
746 * Now attempt to load a saved session with the
747 * same name as the hostname.
750 Conf
*conf2
= conf_new();
751 do_defaults(host
, conf2
);
752 if (loaded_session
|| !conf_launchable(conf2
)) {
753 /* No settings for this host; use defaults */
754 /* (or session was already loaded with -load) */
755 conf_set_str(conf
, CONF_host
, host
);
756 conf_set_int(conf
, CONF_port
, default_port
);
759 conf_copy_into(conf
, conf2
);
760 loaded_session
= TRUE
;
766 /* Patch in specified username. */
767 conf_set_str(conf
, CONF_username
, user
);
774 cmdlen
= cmdsize
= 0;
779 if (cmdlen
>= cmdsize
) {
780 cmdsize
= cmdlen
+ 512;
781 command
= sresize(command
, cmdsize
, char);
783 command
[cmdlen
++]=*p
++;
785 if (cmdlen
>= cmdsize
) {
786 cmdsize
= cmdlen
+ 512;
787 command
= sresize(command
, cmdsize
, char);
789 command
[cmdlen
++]=' '; /* always add trailing space */
790 if (--argc
) p
= *++argv
;
792 if (cmdlen
) command
[--cmdlen
]='\0';
793 /* change trailing blank to NUL */
794 conf_set_str(conf
, CONF_remote_cmd
, command
);
795 conf_set_str(conf
, CONF_remote_cmd2
, "");
796 conf_set_int(conf
, CONF_nopty
, TRUE
); /* command => no tty */
798 break; /* done with cmdline */
806 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
811 * Muck about with the hostname in various ways.
814 char *hostbuf
= dupstr(conf_get_str(conf
, CONF_host
));
815 char *host
= hostbuf
;
819 * Trim leading whitespace.
821 host
+= strspn(host
, " \t");
824 * See if host is of the form user@host, and separate out
825 * the username if so.
827 if (host
[0] != '\0') {
828 char *atsign
= strrchr(host
, '@');
831 conf_set_str(conf
, CONF_username
, host
);
837 * Trim off a colon suffix if it's there.
839 host
[strcspn(host
, ":")] = '\0';
842 * Remove any remaining whitespace.
847 if (*q
!= ' ' && *q
!= '\t')
853 conf_set_str(conf
, CONF_host
, hostbuf
);
858 * Perform command-line overrides on session configuration.
860 cmdline_run_saved(conf
);
863 * Apply subsystem status.
866 conf_set_int(conf
, CONF_ssh_subsys
, TRUE
);
868 if (!*conf_get_str(conf
, CONF_remote_cmd
) &&
869 !*conf_get_str(conf
, CONF_remote_cmd2
) &&
870 !*conf_get_str(conf
, CONF_ssh_nc_host
))
871 flags
|= FLAG_INTERACTIVE
;
874 * Select protocol. This is farmed out into a table in a
875 * separate file to enable an ssh-free variant.
877 back
= backend_from_proto(conf_get_int(conf
, CONF_protocol
));
880 "Internal fault: Unsupported protocol found\n");
887 if (portnumber
!= -1)
888 conf_set_int(conf
, CONF_port
, portnumber
);
891 * Set up the pipe we'll use to tell us about SIGWINCH.
893 if (pipe(signalpipe
) < 0) {
897 putty_signal(SIGWINCH
, sigwinch
);
903 * Unix Plink doesn't provide any way to add forwardings after the
904 * connection is set up, so if there are none now, we can safely set
907 if (conf_get_int(conf
, CONF_protocol
) == PROT_SSH
&&
908 !conf_get_int(conf
, CONF_x11_forward
) &&
909 !conf_get_int(conf
, CONF_agentfwd
) &&
910 !conf_get_str_nthstrkey(conf
, CONF_portfwd
, 0))
911 conf_set_int(conf
, CONF_ssh_simple
, TRUE
);
914 * Start up the connection.
916 logctx
= log_init(NULL
, conf
);
917 console_provide_logctx(logctx
);
921 /* nodelay is only useful if stdin is a terminal device */
922 int nodelay
= conf_get_int(conf
, CONF_tcp_nodelay
) && isatty(0);
924 error
= back
->init(NULL
, &backhandle
, conf
,
925 conf_get_str(conf
, CONF_host
),
926 conf_get_int(conf
, CONF_port
),
928 conf_get_int(conf
, CONF_tcp_keepalives
));
930 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
933 back
->provide_logctx(backhandle
, logctx
);
934 ldisc_create(conf
, NULL
, back
, backhandle
, NULL
);
940 * Set up the initial console mode. We don't care if this call
941 * fails, because we know we aren't necessarily running in a
944 local_tty
= (tcgetattr(STDIN_FILENO
, &orig_termios
) == 0);
945 atexit(cleanup_termios
);
946 ldisc_update(NULL
, 1, 1);
948 now
= GETTICKCOUNT();
951 fd_set rset
, wset
, xset
;
961 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
963 if (connopen
&& !sending
&&
964 back
->connected(backhandle
) &&
965 back
->sendok(backhandle
) &&
966 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
967 /* If we're OK to send, then try to read from stdin. */
968 FD_SET_MAX(STDIN_FILENO
, maxfd
, rset
);
971 if (bufchain_size(&stdout_data
) > 0) {
972 /* If we have data for stdout, try to write to stdout. */
973 FD_SET_MAX(STDOUT_FILENO
, maxfd
, wset
);
976 if (bufchain_size(&stderr_data
) > 0) {
977 /* If we have data for stderr, try to write to stderr. */
978 FD_SET_MAX(STDERR_FILENO
, maxfd
, wset
);
981 /* Count the currently active fds. */
983 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
984 fd
= next_fd(&fdstate
, &rwx
)) i
++;
986 /* Expand the fdlist buffer if necessary. */
989 fdlist
= sresize(fdlist
, fdsize
, int);
993 * Add all currently open fds to the select sets, and store
994 * them in fdlist as well.
997 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
998 fd
= next_fd(&fdstate
, &rwx
)) {
999 fdlist
[fdcount
++] = fd
;
1001 FD_SET_MAX(fd
, maxfd
, rset
);
1003 FD_SET_MAX(fd
, maxfd
, wset
);
1005 FD_SET_MAX(fd
, maxfd
, xset
);
1010 struct timeval tv
, *ptv
;
1012 if (run_timers(now
, &next
)) {
1013 ticks
= next
- GETTICKCOUNT();
1014 if (ticks
< 0) ticks
= 0; /* just in case */
1015 tv
.tv_sec
= ticks
/ 1000;
1016 tv
.tv_usec
= ticks
% 1000 * 1000;
1021 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
1025 long newnow
= GETTICKCOUNT();
1027 * Check to see whether the system clock has
1028 * changed massively during the select.
1030 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
1032 * If so, look at the elapsed time in the
1033 * select and use it to compute a new
1036 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1037 /* So we'd like GETTICKCOUNT to have returned othernow,
1038 * but instead it return newnow. Hence ... */
1039 tickcount_offset
+= othernow
- newnow
;
1045 } while (ret
< 0 && errno
== EINTR
);
1052 for (i
= 0; i
< fdcount
; i
++) {
1055 * We must process exceptional notifications before
1056 * ordinary readability ones, or we may go straight
1057 * past the urgent marker.
1059 if (FD_ISSET(fd
, &xset
))
1060 select_result(fd
, 4);
1061 if (FD_ISSET(fd
, &rset
))
1062 select_result(fd
, 1);
1063 if (FD_ISSET(fd
, &wset
))
1064 select_result(fd
, 2);
1067 if (FD_ISSET(signalpipe
[0], &rset
)) {
1069 struct winsize size
;
1070 if (read(signalpipe
[0], c
, 1) <= 0)
1072 /* ignore its value; it'll be `x' */
1073 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
1074 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
1077 if (FD_ISSET(STDIN_FILENO
, &rset
)) {
1081 if (connopen
&& back
->connected(backhandle
)) {
1082 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
1084 perror("stdin: read");
1086 } else if (ret
== 0) {
1087 back
->special(backhandle
, TS_EOF
);
1088 sending
= FALSE
; /* send nothing further after this */
1093 back
->send(backhandle
, buf
, ret
);
1098 if (FD_ISSET(STDOUT_FILENO
, &wset
)) {
1099 back
->unthrottle(backhandle
, try_output(FALSE
));
1102 if (FD_ISSET(STDERR_FILENO
, &wset
)) {
1103 back
->unthrottle(backhandle
, try_output(TRUE
));
1106 if ((!connopen
|| !back
->connected(backhandle
)) &&
1107 bufchain_size(&stdout_data
) == 0 &&
1108 bufchain_size(&stderr_data
) == 0)
1109 break; /* we closed the connection */
1111 exitcode
= back
->exitcode(backhandle
);
1113 fprintf(stderr
, "Remote process exit code unavailable\n");
1114 exitcode
= 1; /* this is an error condition */
1116 cleanup_exit(exitcode
);
1117 return exitcode
; /* shouldn't happen, but placates gcc */