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
)
135 Filename
platform_default_filename(const char *name
)
138 if (!strcmp(name
, "LogFileName"))
139 strcpy(ret
.path
, "putty.log");
145 char *x_get_default(const char *key
)
147 return NULL
; /* this is a stub */
149 int term_ldisc(Terminal
*term
, int mode
)
153 void ldisc_update(void *frontend
, int echo
, int edit
)
155 /* Update stdin read mode to reflect changes in line discipline. */
158 if (!local_tty
) return;
163 mode
.c_lflag
|= ECHO
;
165 mode
.c_lflag
&= ~ECHO
;
168 mode
.c_iflag
|= ICRNL
;
169 mode
.c_lflag
|= ISIG
| ICANON
;
170 mode
.c_oflag
|= OPOST
;
172 mode
.c_iflag
&= ~ICRNL
;
173 mode
.c_lflag
&= ~(ISIG
| ICANON
);
174 mode
.c_oflag
&= ~OPOST
;
175 /* Solaris sets these to unhelpful values */
177 mode
.c_cc
[VTIME
] = 0;
178 /* FIXME: perhaps what we do with IXON/IXOFF should be an
179 * argument to ldisc_update(), to allow implementation of SSH-2
180 * "xon-xoff" and Rlogin's equivalent? */
181 mode
.c_iflag
&= ~IXON
;
182 mode
.c_iflag
&= ~IXOFF
;
185 * Mark parity errors and (more important) BREAK on input. This
186 * is more complex than it need be because POSIX-2001 suggests
187 * that escaping of valid 0xff in the input stream is dependent on
188 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
189 * 2.0 goes one worse and makes it dependent on INPCK too. We
190 * deal with this by forcing these flags into a useful state and
191 * then faking the state in which we found them in from_tty() if
192 * we get passed a parity or framing error.
194 mode
.c_iflag
= (mode
.c_iflag
| INPCK
| PARMRK
) & ~IGNPAR
;
196 tcsetattr(STDIN_FILENO
, TCSANOW
, &mode
);
199 /* Helper function to extract a special character from a termios. */
200 static char *get_ttychar(struct termios
*t
, int index
)
202 cc_t c
= t
->c_cc
[index
];
203 #if defined(_POSIX_VDISABLE)
204 if (c
== _POSIX_VDISABLE
)
205 return dupprintf("");
207 return dupprintf("^<%d>", c
);
210 char *get_ttymode(void *frontend
, const char *mode
)
213 * Propagate appropriate terminal modes from the local terminal,
216 if (!local_tty
) return NULL
;
218 #define GET_CHAR(ourname, uxname) \
220 if (strcmp(mode, ourname) == 0) \
221 return get_ttychar(&orig_termios, uxname); \
223 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
225 if (strcmp(mode, ourname) == 0) { \
226 int b = (orig_termios.uxmemb & uxname) != 0; \
228 return dupprintf("%d", b); \
233 * Modes that want to be the same on all terminal devices involved.
235 /* All the special characters supported by SSH */
237 GET_CHAR("INTR", VINTR
);
240 GET_CHAR("QUIT", VQUIT
);
243 GET_CHAR("ERASE", VERASE
);
246 GET_CHAR("KILL", VKILL
);
249 GET_CHAR("EOF", VEOF
);
252 GET_CHAR("EOL", VEOL
);
255 GET_CHAR("EOL2", VEOL2
);
258 GET_CHAR("START", VSTART
);
261 GET_CHAR("STOP", VSTOP
);
264 GET_CHAR("SUSP", VSUSP
);
267 GET_CHAR("DSUSP", VDSUSP
);
269 #if defined(VREPRINT)
270 GET_CHAR("REPRINT", VREPRINT
);
273 GET_CHAR("WERASE", VWERASE
);
276 GET_CHAR("LNEXT", VLNEXT
);
279 GET_CHAR("FLUSH", VFLUSH
);
282 GET_CHAR("SWTCH", VSWTCH
);
285 GET_CHAR("STATUS", VSTATUS
);
287 #if defined(VDISCARD)
288 GET_CHAR("DISCARD", VDISCARD
);
290 /* Modes that "configure" other major modes. These should probably be
291 * considered as user preferences. */
292 /* Configuration of ICANON */
294 GET_BOOL("ECHOK", ECHOK
, c_lflag
, );
297 GET_BOOL("ECHOKE", ECHOKE
, c_lflag
, );
300 GET_BOOL("ECHOE", ECHOE
, c_lflag
, );
303 GET_BOOL("ECHONL", ECHONL
, c_lflag
, );
306 GET_BOOL("XCASE", XCASE
, c_lflag
, );
308 /* Configuration of ECHO */
310 GET_BOOL("ECHOCTL", ECHOCTL
, c_lflag
, );
312 /* Configuration of IXON/IXOFF */
314 GET_BOOL("IXANY", IXANY
, c_iflag
, );
316 /* Configuration of OPOST */
318 GET_BOOL("OLCUC", OLCUC
, c_oflag
, );
321 GET_BOOL("ONLCR", ONLCR
, c_oflag
, );
324 GET_BOOL("OCRNL", OCRNL
, c_oflag
, );
327 GET_BOOL("ONOCR", ONOCR
, c_oflag
, );
330 GET_BOOL("ONLRET", ONLRET
, c_oflag
, );
334 * Modes that want to be set in only one place, and that we have
338 GET_BOOL("ISIG", ISIG
, c_lflag
, );
341 GET_BOOL("ICANON", ICANON
, c_lflag
, );
344 GET_BOOL("ECHO", ECHO
, c_lflag
, );
347 GET_BOOL("IXON", IXON
, c_iflag
, );
350 GET_BOOL("IXOFF", IXOFF
, c_iflag
, );
353 GET_BOOL("OPOST", OPOST
, c_oflag
, );
357 * We do not propagate the following modes:
358 * - Parity/serial settings, which are a local affair and don't
359 * make sense propagated over SSH's 8-bit byte-stream.
360 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
361 * - Things that want to be enabled in one place that we don't
366 * - Things I don't know what to do with. (FIXME)
367 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
374 /* Fall through to here for unrecognised names, or ones that are
375 * unsupported on this platform */
379 void cleanup_termios(void)
382 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_termios
);
385 bufchain stdout_data
, stderr_data
;
387 int try_output(int is_stderr
)
389 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
390 int fd
= (is_stderr ? STDERR_FILENO
: STDOUT_FILENO
);
392 int sendlen
, ret
, fl
;
394 if (bufchain_size(chain
) == 0)
395 return bufchain_size(&stdout_data
) + bufchain_size(&stderr_data
);
397 fl
= fcntl(fd
, F_GETFL
);
398 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
399 fcntl(fd
, F_SETFL
, fl
| O_NONBLOCK
);
401 bufchain_prefix(chain
, &senddata
, &sendlen
);
402 ret
= write(fd
, senddata
, sendlen
);
404 bufchain_consume(chain
, ret
);
405 } while (ret
== sendlen
&& bufchain_size(chain
) != 0);
406 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
407 fcntl(fd
, F_SETFL
, fl
);
408 if (ret
< 0 && errno
!= EAGAIN
) {
409 perror(is_stderr ?
"stderr: write" : "stdout: write");
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 bufchain_add(&stdout_data
, data
, len
);
423 return try_output(FALSE
);
427 int from_backend_untrusted(void *frontend_handle
, const char *data
, int len
)
430 * No "untrusted" output should get here (the way the code is
431 * currently, it's all diverted by FLAG_STDERR).
433 assert(!"Unexpected call to from_backend_untrusted()");
434 return 0; /* not reached */
437 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
440 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
442 ret
= console_get_userpass_input(p
, in
, inlen
);
447 * Handle data from a local tty in PARMRK format.
449 static void from_tty(void *vbuf
, unsigned len
)
451 char *p
, *q
, *end
, *buf
= vbuf
;
452 static enum {NORMAL
, FF
, FF00
} state
= NORMAL
;
454 p
= buf
; end
= buf
+ len
;
462 q
= memchr(p
, '\xff', end
- p
);
463 if (q
== NULL
) q
= end
;
464 back
->send(backhandle
, p
, q
- p
);
470 back
->send(backhandle
, p
, 1);
473 } else if (*p
== '\0') {
480 back
->special(backhandle
, TS_BRK
);
483 * Pretend that PARMRK wasn't set. This involves
484 * faking what INPCK and IGNPAR would have done if
485 * we hadn't overridden them. Unfortunately, we
486 * can't do this entirely correctly because INPCK
487 * distinguishes between framing and parity
488 * errors, but PARMRK format represents both in
489 * the same way. We assume that parity errors are
490 * more common than framing errors, and hence
491 * treat all input errors as being subject to
494 if (orig_termios
.c_iflag
& INPCK
) {
495 /* If IGNPAR is set, we throw away the character. */
496 if (!(orig_termios
.c_iflag
& IGNPAR
)) {
497 /* PE/FE get passed on as NUL. */
499 back
->send(backhandle
, p
, 1);
502 /* INPCK not set. Assume we got a parity error. */
503 back
->send(backhandle
, p
, 1);
514 void sigwinch(int signum
)
516 if (write(signalpipe
[1], "x", 1) <= 0)
517 /* not much we can do about it */;
521 * In Plink our selects are synchronous, so these functions are
524 int uxsel_input_add(int fd
, int rwx
) { return 0; }
525 void uxsel_input_remove(int id
) { }
528 * Short description of parameters.
530 static void usage(void)
532 printf("PuTTY Link: command-line connection utility\n");
534 printf("Usage: plink [options] [user@]host [command]\n");
535 printf(" (\"host\" can also be a PuTTY saved session name)\n");
536 printf("Options:\n");
537 printf(" -V print version information and exit\n");
538 printf(" -pgpfp print PGP key fingerprints and exit\n");
539 printf(" -v show verbose messages\n");
540 printf(" -load sessname Load settings from saved session\n");
541 printf(" -ssh -telnet -rlogin -raw -serial\n");
542 printf(" force use of a particular protocol\n");
543 printf(" -P port connect to specified port\n");
544 printf(" -l user connect with specified username\n");
545 printf(" -batch disable all interactive prompts\n");
546 printf("The following options only apply to SSH connections:\n");
547 printf(" -pw passw login with specified password\n");
548 printf(" -D [listen-IP:]listen-port\n");
549 printf(" Dynamic SOCKS-based port forwarding\n");
550 printf(" -L [listen-IP:]listen-port:host:port\n");
551 printf(" Forward local port to remote address\n");
552 printf(" -R [listen-IP:]listen-port:host:port\n");
553 printf(" Forward remote port to local address\n");
554 printf(" -X -x enable / disable X11 forwarding\n");
555 printf(" -A -a enable / disable agent forwarding\n");
556 printf(" -t -T enable / disable pty allocation\n");
557 printf(" -1 -2 force use of particular protocol version\n");
558 printf(" -4 -6 force use of IPv4 or IPv6\n");
559 printf(" -C enable compression\n");
560 printf(" -i key private key file for authentication\n");
561 printf(" -noagent disable use of Pageant\n");
562 printf(" -agent enable use of Pageant\n");
563 printf(" -m file read remote command(s) from file\n");
564 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
565 printf(" -N don't start a shell/command (SSH-2 only)\n");
566 printf(" -nc host:port\n");
567 printf(" open tunnel in place of session (SSH-2 only)\n");
568 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
569 printf(" Specify the serial configuration (serial only)\n");
573 static void version(void)
575 printf("plink: %s\n", ver
);
579 int main(int argc
, char **argv
)
585 int i
, fdcount
, fdsize
, fdstate
;
589 int use_subsystem
= 0;
590 int got_host
= FALSE
;
595 fdcount
= fdsize
= 0;
597 * Initialise port and protocol to sensible defaults. (These
598 * will be overridden by more or less anything.)
600 default_protocol
= PROT_SSH
;
603 flags
= FLAG_STDERR
| FLAG_STDERR_TTY
;
607 * Process the command line.
609 do_defaults(NULL
, &cfg
);
610 loaded_session
= FALSE
;
611 default_protocol
= cfg
.protocol
;
612 default_port
= cfg
.port
;
616 * Override the default protocol if PLINK_PROTOCOL is set.
618 char *p
= getenv("PLINK_PROTOCOL");
620 const Backend
*b
= backend_from_name(p
);
622 default_protocol
= cfg
.protocol
= b
->protocol
;
623 default_port
= cfg
.port
= b
->default_port
;
630 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
634 "plink: option \"%s\" requires an argument\n", p
);
636 } else if (ret
== 2) {
638 } else if (ret
== 1) {
640 } else if (!strcmp(p
, "-batch")) {
641 console_batch_mode
= 1;
642 } else if (!strcmp(p
, "-s")) {
643 /* Save status to write to cfg later. */
645 } else if (!strcmp(p
, "-V")) {
647 } else if (!strcmp(p
, "-pgpfp")) {
650 } else if (!strcmp(p
, "-o")) {
653 "plink: option \"-o\" requires an argument\n");
657 provide_xrm_string(*++argv
);
660 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
664 if (!cfg_launchable(&cfg
) || !(got_host
|| loaded_session
)) {
668 * If the hostname starts with "telnet:", set the
669 * protocol to Telnet and process the string as a
672 if (!strncmp(q
, "telnet:", 7)) {
676 if (q
[0] == '/' && q
[1] == '/')
678 cfg
.protocol
= PROT_TELNET
;
680 while (*p
&& *p
!= ':' && *p
!= '/')
689 strncpy(cfg
.host
, q
, sizeof(cfg
.host
) - 1);
690 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
693 char *r
, *user
, *host
;
695 * Before we process the [user@]host string, we
696 * first check for the presence of a protocol
697 * prefix (a protocol name followed by ",").
703 b
= backend_from_name(p
);
705 default_protocol
= cfg
.protocol
= b
->protocol
;
706 portnumber
= b
->default_port
;
712 * A nonzero length string followed by an @ is treated
713 * as a username. (We discount an _initial_ @.) The
714 * rest of the string (or the whole string if no @)
715 * is treated as a session name and/or hostname.
719 p
++, r
= NULL
; /* discount initial @ */
724 user
= NULL
, host
= p
;
728 * Now attempt to load a saved session with the
729 * same name as the hostname.
733 do_defaults(host
, &cfg2
);
734 if (loaded_session
|| !cfg_launchable(&cfg2
)) {
735 /* No settings for this host; use defaults */
736 /* (or session was already loaded with -load) */
737 strncpy(cfg
.host
, host
, sizeof(cfg
.host
) - 1);
738 cfg
.host
[sizeof(cfg
.host
) - 1] = '\0';
739 cfg
.port
= default_port
;
743 loaded_session
= TRUE
;
748 /* Patch in specified username. */
749 strncpy(cfg
.username
, user
,
750 sizeof(cfg
.username
) - 1);
751 cfg
.username
[sizeof(cfg
.username
) - 1] = '\0';
758 cmdlen
= cmdsize
= 0;
763 if (cmdlen
>= cmdsize
) {
764 cmdsize
= cmdlen
+ 512;
765 command
= sresize(command
, cmdsize
, char);
767 command
[cmdlen
++]=*p
++;
769 if (cmdlen
>= cmdsize
) {
770 cmdsize
= cmdlen
+ 512;
771 command
= sresize(command
, cmdsize
, char);
773 command
[cmdlen
++]=' '; /* always add trailing space */
774 if (--argc
) p
= *++argv
;
776 if (cmdlen
) command
[--cmdlen
]='\0';
777 /* change trailing blank to NUL */
778 cfg
.remote_cmd_ptr
= command
;
779 cfg
.remote_cmd_ptr2
= NULL
;
780 cfg
.nopty
= TRUE
; /* command => no terminal */
782 break; /* done with cmdline */
790 if (!cfg_launchable(&cfg
) || !(got_host
|| loaded_session
)) {
795 * Trim leading whitespace off the hostname if it's there.
798 int space
= strspn(cfg
.host
, " \t");
799 memmove(cfg
.host
, cfg
.host
+space
, 1+strlen(cfg
.host
)-space
);
802 /* See if host is of the form user@host */
803 if (cfg
.host
[0] != '\0') {
804 char *atsign
= strrchr(cfg
.host
, '@');
805 /* Make sure we're not overflowing the user field */
807 if (atsign
- cfg
.host
< sizeof cfg
.username
) {
808 strncpy(cfg
.username
, cfg
.host
, atsign
- cfg
.host
);
809 cfg
.username
[atsign
- cfg
.host
] = '\0';
811 memmove(cfg
.host
, atsign
+ 1, 1 + strlen(atsign
+ 1));
816 * Perform command-line overrides on session configuration.
818 cmdline_run_saved(&cfg
);
821 * Apply subsystem status.
824 cfg
.ssh_subsys
= TRUE
;
827 * Trim a colon suffix off the hostname if it's there.
829 cfg
.host
[strcspn(cfg
.host
, ":")] = '\0';
832 * Remove any remaining whitespace from the hostname.
836 while (cfg
.host
[p2
] != '\0') {
837 if (cfg
.host
[p2
] != ' ' && cfg
.host
[p2
] != '\t') {
838 cfg
.host
[p1
] = cfg
.host
[p2
];
846 if (!cfg
.remote_cmd_ptr
&& !*cfg
.remote_cmd
&& !*cfg
.ssh_nc_host
)
847 flags
|= FLAG_INTERACTIVE
;
850 * Select protocol. This is farmed out into a table in a
851 * separate file to enable an ssh-free variant.
853 back
= backend_from_proto(cfg
.protocol
);
856 "Internal fault: Unsupported protocol found\n");
863 if (portnumber
!= -1)
864 cfg
.port
= portnumber
;
867 * Set up the pipe we'll use to tell us about SIGWINCH.
869 if (pipe(signalpipe
) < 0) {
873 putty_signal(SIGWINCH
, sigwinch
);
879 * Unix Plink doesn't provide any way to add forwardings after the
880 * connection is set up, so if there are none now, we can safely set
883 if (cfg
.protocol
== PROT_SSH
&& !cfg
.x11_forward
&& !cfg
.agentfwd
&&
884 cfg
.portfwd
[0] == '\0' && cfg
.portfwd
[1] == '\0')
885 cfg
.ssh_simple
= TRUE
;
887 * Start up the connection.
889 logctx
= log_init(NULL
, &cfg
);
890 console_provide_logctx(logctx
);
894 /* nodelay is only useful if stdin is a terminal device */
895 int nodelay
= cfg
.tcp_nodelay
&& isatty(0);
897 error
= back
->init(NULL
, &backhandle
, &cfg
, cfg
.host
, cfg
.port
,
898 &realhost
, nodelay
, cfg
.tcp_keepalives
);
900 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
903 back
->provide_logctx(backhandle
, logctx
);
904 ldisc
= ldisc_create(&cfg
, NULL
, back
, backhandle
, NULL
);
910 * Set up the initial console mode. We don't care if this call
911 * fails, because we know we aren't necessarily running in a
914 local_tty
= (tcgetattr(STDIN_FILENO
, &orig_termios
) == 0);
915 atexit(cleanup_termios
);
916 ldisc_update(NULL
, 1, 1);
918 now
= GETTICKCOUNT();
921 fd_set rset
, wset
, xset
;
931 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
933 if (connopen
&& !sending
&&
934 back
->connected(backhandle
) &&
935 back
->sendok(backhandle
) &&
936 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
937 /* If we're OK to send, then try to read from stdin. */
938 FD_SET_MAX(STDIN_FILENO
, maxfd
, rset
);
941 if (bufchain_size(&stdout_data
) > 0) {
942 /* If we have data for stdout, try to write to stdout. */
943 FD_SET_MAX(STDOUT_FILENO
, maxfd
, wset
);
946 if (bufchain_size(&stderr_data
) > 0) {
947 /* If we have data for stderr, try to write to stderr. */
948 FD_SET_MAX(STDERR_FILENO
, maxfd
, wset
);
951 /* Count the currently active fds. */
953 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
954 fd
= next_fd(&fdstate
, &rwx
)) i
++;
956 /* Expand the fdlist buffer if necessary. */
959 fdlist
= sresize(fdlist
, fdsize
, int);
963 * Add all currently open fds to the select sets, and store
964 * them in fdlist as well.
967 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
968 fd
= next_fd(&fdstate
, &rwx
)) {
969 fdlist
[fdcount
++] = fd
;
971 FD_SET_MAX(fd
, maxfd
, rset
);
973 FD_SET_MAX(fd
, maxfd
, wset
);
975 FD_SET_MAX(fd
, maxfd
, xset
);
980 struct timeval tv
, *ptv
;
982 if (run_timers(now
, &next
)) {
983 ticks
= next
- GETTICKCOUNT();
984 if (ticks
< 0) ticks
= 0; /* just in case */
985 tv
.tv_sec
= ticks
/ 1000;
986 tv
.tv_usec
= ticks
% 1000 * 1000;
991 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
995 long newnow
= GETTICKCOUNT();
997 * Check to see whether the system clock has
998 * changed massively during the select.
1000 if (newnow
- now
< 0 || newnow
- now
> next
- now
) {
1002 * If so, look at the elapsed time in the
1003 * select and use it to compute a new
1006 long othernow
= now
+ tv
.tv_sec
* 1000 + tv
.tv_usec
/ 1000;
1007 /* So we'd like GETTICKCOUNT to have returned othernow,
1008 * but instead it return newnow. Hence ... */
1009 tickcount_offset
+= othernow
- newnow
;
1015 } while (ret
< 0 && errno
== EINTR
);
1022 for (i
= 0; i
< fdcount
; i
++) {
1025 * We must process exceptional notifications before
1026 * ordinary readability ones, or we may go straight
1027 * past the urgent marker.
1029 if (FD_ISSET(fd
, &xset
))
1030 select_result(fd
, 4);
1031 if (FD_ISSET(fd
, &rset
))
1032 select_result(fd
, 1);
1033 if (FD_ISSET(fd
, &wset
))
1034 select_result(fd
, 2);
1037 if (FD_ISSET(signalpipe
[0], &rset
)) {
1039 struct winsize size
;
1040 if (read(signalpipe
[0], c
, 1) <= 0)
1042 /* ignore its value; it'll be `x' */
1043 if (ioctl(0, TIOCGWINSZ
, (void *)&size
) >= 0)
1044 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
1047 if (FD_ISSET(STDIN_FILENO
, &rset
)) {
1051 if (connopen
&& back
->connected(backhandle
)) {
1052 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
1054 perror("stdin: read");
1056 } else if (ret
== 0) {
1057 back
->special(backhandle
, TS_EOF
);
1058 sending
= FALSE
; /* send nothing further after this */
1063 back
->send(backhandle
, buf
, ret
);
1068 if (FD_ISSET(STDOUT_FILENO
, &wset
)) {
1069 back
->unthrottle(backhandle
, try_output(FALSE
));
1072 if (FD_ISSET(STDERR_FILENO
, &wset
)) {
1073 back
->unthrottle(backhandle
, try_output(TRUE
));
1076 if ((!connopen
|| !back
->connected(backhandle
)) &&
1077 bufchain_size(&stdout_data
) == 0 &&
1078 bufchain_size(&stderr_data
) == 0)
1079 break; /* we closed the connection */
1081 exitcode
= back
->exitcode(backhandle
);
1083 fprintf(stderr
, "Remote process exit code unavailable\n");
1084 exitcode
= 1; /* this is an error condition */
1086 cleanup_exit(exitcode
);
1087 return exitcode
; /* shouldn't happen, but placates gcc */