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 nonfatal(char *p
, ...)
71 fprintf(stderr
, "ERROR: ");
73 vfprintf(stderr
, p
, ap
);
82 void connection_fatal(void *frontend
, char *p
, ...)
87 fprintf(stderr
, "FATAL ERROR: ");
89 vfprintf(stderr
, p
, ap
);
99 void cmdline_error(char *p
, ...)
104 fprintf(stderr
, "plink: ");
106 vfprintf(stderr
, p
, ap
);
113 static int local_tty
= FALSE
; /* do we have a local tty? */
115 static Backend
*back
;
116 static void *backhandle
;
120 * Default settings that are specific to pterm.
122 char *platform_default_s(const char *name
)
124 if (!strcmp(name
, "TermType"))
125 return dupstr(getenv("TERM"));
126 if (!strcmp(name
, "UserName"))
127 return get_username();
128 if (!strcmp(name
, "SerialLine"))
129 return dupstr("/dev/ttyS0");
133 int platform_default_i(const char *name
, int def
)
138 FontSpec
*platform_default_fontspec(const char *name
)
140 return fontspec_new("");
143 Filename
*platform_default_filename(const char *name
)
145 if (!strcmp(name
, "LogFileName"))
146 return filename_from_str("putty.log");
148 return filename_from_str("");
151 char *x_get_default(const char *key
)
153 return NULL
; /* this is a stub */
155 int term_ldisc(Terminal
*term
, int mode
)
159 void ldisc_update(void *frontend
, int echo
, int edit
)
161 /* Update stdin read mode to reflect changes in line discipline. */
164 if (!local_tty
) return;
169 mode
.c_lflag
|= ECHO
;
171 mode
.c_lflag
&= ~ECHO
;
174 mode
.c_iflag
|= ICRNL
;
175 mode
.c_lflag
|= ISIG
| ICANON
;
176 mode
.c_oflag
|= OPOST
;
178 mode
.c_iflag
&= ~ICRNL
;
179 mode
.c_lflag
&= ~(ISIG
| ICANON
);
180 mode
.c_oflag
&= ~OPOST
;
181 /* Solaris sets these to unhelpful values */
183 mode
.c_cc
[VTIME
] = 0;
184 /* FIXME: perhaps what we do with IXON/IXOFF should be an
185 * argument to ldisc_update(), to allow implementation of SSH-2
186 * "xon-xoff" and Rlogin's equivalent? */
187 mode
.c_iflag
&= ~IXON
;
188 mode
.c_iflag
&= ~IXOFF
;
191 * Mark parity errors and (more important) BREAK on input. This
192 * is more complex than it need be because POSIX-2001 suggests
193 * that escaping of valid 0xff in the input stream is dependent on
194 * IGNPAR being clear even though marking of BREAK isn't. NetBSD
195 * 2.0 goes one worse and makes it dependent on INPCK too. We
196 * deal with this by forcing these flags into a useful state and
197 * then faking the state in which we found them in from_tty() if
198 * we get passed a parity or framing error.
200 mode
.c_iflag
= (mode
.c_iflag
| INPCK
| PARMRK
) & ~IGNPAR
;
202 tcsetattr(STDIN_FILENO
, TCSANOW
, &mode
);
205 /* Helper function to extract a special character from a termios. */
206 static char *get_ttychar(struct termios
*t
, int index
)
208 cc_t c
= t
->c_cc
[index
];
209 #if defined(_POSIX_VDISABLE)
210 if (c
== _POSIX_VDISABLE
)
211 return dupprintf("");
213 return dupprintf("^<%d>", c
);
216 char *get_ttymode(void *frontend
, const char *mode
)
219 * Propagate appropriate terminal modes from the local terminal,
222 if (!local_tty
) return NULL
;
224 #define GET_CHAR(ourname, uxname) \
226 if (strcmp(mode, ourname) == 0) \
227 return get_ttychar(&orig_termios, uxname); \
229 #define GET_BOOL(ourname, uxname, uxmemb, transform) \
231 if (strcmp(mode, ourname) == 0) { \
232 int b = (orig_termios.uxmemb & uxname) != 0; \
234 return dupprintf("%d", b); \
239 * Modes that want to be the same on all terminal devices involved.
241 /* All the special characters supported by SSH */
243 GET_CHAR("INTR", VINTR
);
246 GET_CHAR("QUIT", VQUIT
);
249 GET_CHAR("ERASE", VERASE
);
252 GET_CHAR("KILL", VKILL
);
255 GET_CHAR("EOF", VEOF
);
258 GET_CHAR("EOL", VEOL
);
261 GET_CHAR("EOL2", VEOL2
);
264 GET_CHAR("START", VSTART
);
267 GET_CHAR("STOP", VSTOP
);
270 GET_CHAR("SUSP", VSUSP
);
273 GET_CHAR("DSUSP", VDSUSP
);
275 #if defined(VREPRINT)
276 GET_CHAR("REPRINT", VREPRINT
);
279 GET_CHAR("WERASE", VWERASE
);
282 GET_CHAR("LNEXT", VLNEXT
);
285 GET_CHAR("FLUSH", VFLUSH
);
288 GET_CHAR("SWTCH", VSWTCH
);
291 GET_CHAR("STATUS", VSTATUS
);
293 #if defined(VDISCARD)
294 GET_CHAR("DISCARD", VDISCARD
);
296 /* Modes that "configure" other major modes. These should probably be
297 * considered as user preferences. */
298 /* Configuration of ICANON */
300 GET_BOOL("ECHOK", ECHOK
, c_lflag
, );
303 GET_BOOL("ECHOKE", ECHOKE
, c_lflag
, );
306 GET_BOOL("ECHOE", ECHOE
, c_lflag
, );
309 GET_BOOL("ECHONL", ECHONL
, c_lflag
, );
312 GET_BOOL("XCASE", XCASE
, c_lflag
, );
314 /* Configuration of ECHO */
316 GET_BOOL("ECHOCTL", ECHOCTL
, c_lflag
, );
318 /* Configuration of IXON/IXOFF */
320 GET_BOOL("IXANY", IXANY
, c_iflag
, );
322 /* Configuration of OPOST */
324 GET_BOOL("OLCUC", OLCUC
, c_oflag
, );
327 GET_BOOL("ONLCR", ONLCR
, c_oflag
, );
330 GET_BOOL("OCRNL", OCRNL
, c_oflag
, );
333 GET_BOOL("ONOCR", ONOCR
, c_oflag
, );
336 GET_BOOL("ONLRET", ONLRET
, c_oflag
, );
340 * Modes that want to be set in only one place, and that we have
344 GET_BOOL("ISIG", ISIG
, c_lflag
, );
347 GET_BOOL("ICANON", ICANON
, c_lflag
, );
350 GET_BOOL("ECHO", ECHO
, c_lflag
, );
353 GET_BOOL("IXON", IXON
, c_iflag
, );
356 GET_BOOL("IXOFF", IXOFF
, c_iflag
, );
359 GET_BOOL("OPOST", OPOST
, c_oflag
, );
363 * We do not propagate the following modes:
364 * - Parity/serial settings, which are a local affair and don't
365 * make sense propagated over SSH's 8-bit byte-stream.
366 * IGNPAR PARMRK INPCK CS7 CS8 PARENB PARODD
367 * - Things that want to be enabled in one place that we don't
372 * - Things I don't know what to do with. (FIXME)
373 * ISTRIP IMAXBEL NOFLSH TOSTOP IEXTEN
380 /* Fall through to here for unrecognised names, or ones that are
381 * unsupported on this platform */
385 void cleanup_termios(void)
388 tcsetattr(STDIN_FILENO
, TCSANOW
, &orig_termios
);
391 bufchain stdout_data
, stderr_data
;
392 enum { EOF_NO
, EOF_PENDING
, EOF_SENT
} outgoingeof
;
394 int try_output(int is_stderr
)
396 bufchain
*chain
= (is_stderr ?
&stderr_data
: &stdout_data
);
397 int fd
= (is_stderr ? STDERR_FILENO
: STDOUT_FILENO
);
399 int sendlen
, ret
, fl
;
401 if (bufchain_size(chain
) > 0) {
402 fl
= fcntl(fd
, F_GETFL
);
403 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
404 fcntl(fd
, F_SETFL
, fl
| O_NONBLOCK
);
406 bufchain_prefix(chain
, &senddata
, &sendlen
);
407 ret
= write(fd
, senddata
, sendlen
);
409 bufchain_consume(chain
, ret
);
410 } while (ret
== sendlen
&& bufchain_size(chain
) != 0);
411 if (fl
!= -1 && !(fl
& O_NONBLOCK
))
412 fcntl(fd
, F_SETFL
, fl
);
413 if (ret
< 0 && errno
!= EAGAIN
) {
414 perror(is_stderr ?
"stderr: write" : "stdout: write");
418 if (outgoingeof
== EOF_PENDING
&& bufchain_size(&stdout_data
) == 0) {
419 close(STDOUT_FILENO
);
420 outgoingeof
= EOF_SENT
;
422 return bufchain_size(&stdout_data
) + bufchain_size(&stderr_data
);
425 int from_backend(void *frontend_handle
, int is_stderr
,
426 const char *data
, int len
)
429 bufchain_add(&stderr_data
, data
, len
);
430 return try_output(TRUE
);
432 assert(outgoingeof
== EOF_NO
);
433 bufchain_add(&stdout_data
, data
, len
);
434 return try_output(FALSE
);
438 int from_backend_untrusted(void *frontend_handle
, const char *data
, int len
)
441 * No "untrusted" output should get here (the way the code is
442 * currently, it's all diverted by FLAG_STDERR).
444 assert(!"Unexpected call to from_backend_untrusted()");
445 return 0; /* not reached */
448 int from_backend_eof(void *frontend_handle
)
450 assert(outgoingeof
== EOF_NO
);
451 outgoingeof
= EOF_PENDING
;
453 return FALSE
; /* do not respond to incoming EOF with outgoing */
456 int get_userpass_input(prompts_t
*p
, unsigned char *in
, int inlen
)
459 ret
= cmdline_get_passwd_input(p
, in
, inlen
);
461 ret
= console_get_userpass_input(p
, in
, inlen
);
466 * Handle data from a local tty in PARMRK format.
468 static void from_tty(void *vbuf
, unsigned len
)
470 char *p
, *q
, *end
, *buf
= vbuf
;
471 static enum {NORMAL
, FF
, FF00
} state
= NORMAL
;
473 p
= buf
; end
= buf
+ len
;
481 q
= memchr(p
, '\xff', end
- p
);
482 if (q
== NULL
) q
= end
;
483 back
->send(backhandle
, p
, q
- p
);
489 back
->send(backhandle
, p
, 1);
492 } else if (*p
== '\0') {
499 back
->special(backhandle
, TS_BRK
);
502 * Pretend that PARMRK wasn't set. This involves
503 * faking what INPCK and IGNPAR would have done if
504 * we hadn't overridden them. Unfortunately, we
505 * can't do this entirely correctly because INPCK
506 * distinguishes between framing and parity
507 * errors, but PARMRK format represents both in
508 * the same way. We assume that parity errors are
509 * more common than framing errors, and hence
510 * treat all input errors as being subject to
513 if (orig_termios
.c_iflag
& INPCK
) {
514 /* If IGNPAR is set, we throw away the character. */
515 if (!(orig_termios
.c_iflag
& IGNPAR
)) {
516 /* PE/FE get passed on as NUL. */
518 back
->send(backhandle
, p
, 1);
521 /* INPCK not set. Assume we got a parity error. */
522 back
->send(backhandle
, p
, 1);
533 void sigwinch(int signum
)
535 if (write(signalpipe
[1], "x", 1) <= 0)
536 /* not much we can do about it */;
540 * In Plink our selects are synchronous, so these functions are
543 int uxsel_input_add(int fd
, int rwx
) { return 0; }
544 void uxsel_input_remove(int id
) { }
547 * Short description of parameters.
549 static void usage(void)
551 printf("PuTTY Link: command-line connection utility\n");
553 printf("Usage: plink [options] [user@]host [command]\n");
554 printf(" (\"host\" can also be a PuTTY saved session name)\n");
555 printf("Options:\n");
556 printf(" -V print version information and exit\n");
557 printf(" -pgpfp print PGP key fingerprints and exit\n");
558 printf(" -v show verbose messages\n");
559 printf(" -load sessname Load settings from saved session\n");
560 printf(" -ssh -telnet -rlogin -raw -serial\n");
561 printf(" force use of a particular protocol\n");
562 printf(" -P port connect to specified port\n");
563 printf(" -l user connect with specified username\n");
564 printf(" -batch disable all interactive prompts\n");
565 printf("The following options only apply to SSH connections:\n");
566 printf(" -pw passw login with specified password\n");
567 printf(" -D [listen-IP:]listen-port\n");
568 printf(" Dynamic SOCKS-based port forwarding\n");
569 printf(" -L [listen-IP:]listen-port:host:port\n");
570 printf(" Forward local port to remote address\n");
571 printf(" -R [listen-IP:]listen-port:host:port\n");
572 printf(" Forward remote port to local address\n");
573 printf(" -X -x enable / disable X11 forwarding\n");
574 printf(" -A -a enable / disable agent forwarding\n");
575 printf(" -t -T enable / disable pty allocation\n");
576 printf(" -1 -2 force use of particular protocol version\n");
577 printf(" -4 -6 force use of IPv4 or IPv6\n");
578 printf(" -C enable compression\n");
579 printf(" -i key private key file for authentication\n");
580 printf(" -noagent disable use of Pageant\n");
581 printf(" -agent enable use of Pageant\n");
582 printf(" -m file read remote command(s) from file\n");
583 printf(" -s remote command is an SSH subsystem (SSH-2 only)\n");
584 printf(" -N don't start a shell/command (SSH-2 only)\n");
585 printf(" -nc host:port\n");
586 printf(" open tunnel in place of session (SSH-2 only)\n");
587 printf(" -sercfg configuration-string (e.g. 19200,8,n,1,X)\n");
588 printf(" Specify the serial configuration (serial only)\n");
592 static void version(void)
594 printf("plink: %s\n", ver
);
598 void frontend_net_error_pending(void) {}
600 int main(int argc
, char **argv
)
606 int i
, fdcount
, fdsize
, fdstate
;
610 int use_subsystem
= 0;
611 int got_host
= FALSE
;
616 fdcount
= fdsize
= 0;
618 * Initialise port and protocol to sensible defaults. (These
619 * will be overridden by more or less anything.)
621 default_protocol
= PROT_SSH
;
624 bufchain_init(&stdout_data
);
625 bufchain_init(&stderr_data
);
626 outgoingeof
= EOF_NO
;
628 flags
= FLAG_STDERR
| FLAG_STDERR_TTY
;
632 * Process the command line.
635 do_defaults(NULL
, conf
);
636 loaded_session
= FALSE
;
637 default_protocol
= conf_get_int(conf
, CONF_protocol
);
638 default_port
= conf_get_int(conf
, CONF_port
);
642 * Override the default protocol if PLINK_PROTOCOL is set.
644 char *p
= getenv("PLINK_PROTOCOL");
646 const Backend
*b
= backend_from_name(p
);
648 default_protocol
= b
->protocol
;
649 default_port
= b
->default_port
;
650 conf_set_int(conf
, CONF_protocol
, default_protocol
);
651 conf_set_int(conf
, CONF_port
, default_port
);
658 int ret
= cmdline_process_param(p
, (argc
> 1 ? argv
[1] : NULL
),
662 "plink: option \"%s\" requires an argument\n", p
);
664 } else if (ret
== 2) {
666 } else if (ret
== 1) {
668 } else if (!strcmp(p
, "-batch")) {
669 console_batch_mode
= 1;
670 } else if (!strcmp(p
, "-s")) {
671 /* Save status to write to conf later. */
673 } else if (!strcmp(p
, "-V") || !strcmp(p
, "--version")) {
675 } else if (!strcmp(p
, "--help")) {
678 } else if (!strcmp(p
, "-pgpfp")) {
681 } else if (!strcmp(p
, "-o")) {
684 "plink: option \"-o\" requires an argument\n");
688 provide_xrm_string(*++argv
);
691 fprintf(stderr
, "plink: unknown option \"%s\"\n", p
);
695 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
699 * If the hostname starts with "telnet:", set the
700 * protocol to Telnet and process the string as a
703 if (!strncmp(q
, "telnet:", 7)) {
707 if (q
[0] == '/' && q
[1] == '/')
709 conf_set_int(conf
, CONF_protocol
, PROT_TELNET
);
711 while (*p
&& *p
!= ':' && *p
!= '/')
717 conf_set_int(conf
, CONF_port
, atoi(p
));
719 conf_set_int(conf
, CONF_port
, -1);
720 conf_set_str(conf
, CONF_host
, q
);
723 char *r
, *user
, *host
;
725 * Before we process the [user@]host string, we
726 * first check for the presence of a protocol
727 * prefix (a protocol name followed by ",").
733 b
= backend_from_name(p
);
735 default_protocol
= b
->protocol
;
736 conf_set_int(conf
, CONF_protocol
,
738 portnumber
= b
->default_port
;
744 * A nonzero length string followed by an @ is treated
745 * as a username. (We discount an _initial_ @.) The
746 * rest of the string (or the whole string if no @)
747 * is treated as a session name and/or hostname.
751 p
++, r
= NULL
; /* discount initial @ */
756 user
= NULL
, host
= p
;
760 * Now attempt to load a saved session with the
761 * same name as the hostname.
764 Conf
*conf2
= conf_new();
765 do_defaults(host
, conf2
);
766 if (loaded_session
|| !conf_launchable(conf2
)) {
767 /* No settings for this host; use defaults */
768 /* (or session was already loaded with -load) */
769 conf_set_str(conf
, CONF_host
, host
);
770 conf_set_int(conf
, CONF_port
, default_port
);
773 conf_copy_into(conf
, conf2
);
774 loaded_session
= TRUE
;
780 /* Patch in specified username. */
781 conf_set_str(conf
, CONF_username
, user
);
788 cmdlen
= cmdsize
= 0;
793 if (cmdlen
>= cmdsize
) {
794 cmdsize
= cmdlen
+ 512;
795 command
= sresize(command
, cmdsize
, char);
797 command
[cmdlen
++]=*p
++;
799 if (cmdlen
>= cmdsize
) {
800 cmdsize
= cmdlen
+ 512;
801 command
= sresize(command
, cmdsize
, char);
803 command
[cmdlen
++]=' '; /* always add trailing space */
804 if (--argc
) p
= *++argv
;
806 if (cmdlen
) command
[--cmdlen
]='\0';
807 /* change trailing blank to NUL */
808 conf_set_str(conf
, CONF_remote_cmd
, command
);
809 conf_set_str(conf
, CONF_remote_cmd2
, "");
810 conf_set_int(conf
, CONF_nopty
, TRUE
); /* command => no tty */
812 break; /* done with cmdline */
820 if (!conf_launchable(conf
) || !(got_host
|| loaded_session
)) {
825 * Muck about with the hostname in various ways.
828 char *hostbuf
= dupstr(conf_get_str(conf
, CONF_host
));
829 char *host
= hostbuf
;
833 * Trim leading whitespace.
835 host
+= strspn(host
, " \t");
838 * See if host is of the form user@host, and separate out
839 * the username if so.
841 if (host
[0] != '\0') {
842 char *atsign
= strrchr(host
, '@');
845 conf_set_str(conf
, CONF_username
, host
);
851 * Trim off a colon suffix if it's there.
853 host
[strcspn(host
, ":")] = '\0';
856 * Remove any remaining whitespace.
861 if (*q
!= ' ' && *q
!= '\t')
867 conf_set_str(conf
, CONF_host
, hostbuf
);
872 * Perform command-line overrides on session configuration.
874 cmdline_run_saved(conf
);
877 * Apply subsystem status.
880 conf_set_int(conf
, CONF_ssh_subsys
, TRUE
);
882 if (!*conf_get_str(conf
, CONF_remote_cmd
) &&
883 !*conf_get_str(conf
, CONF_remote_cmd2
) &&
884 !*conf_get_str(conf
, CONF_ssh_nc_host
))
885 flags
|= FLAG_INTERACTIVE
;
888 * Select protocol. This is farmed out into a table in a
889 * separate file to enable an ssh-free variant.
891 back
= backend_from_proto(conf_get_int(conf
, CONF_protocol
));
894 "Internal fault: Unsupported protocol found\n");
901 if (portnumber
!= -1)
902 conf_set_int(conf
, CONF_port
, portnumber
);
905 * Block SIGPIPE, so that we'll get EPIPE individually on
906 * particular network connections that go wrong.
908 putty_signal(SIGPIPE
, SIG_IGN
);
911 * Set up the pipe we'll use to tell us about SIGWINCH.
913 if (pipe(signalpipe
) < 0) {
917 putty_signal(SIGWINCH
, sigwinch
);
920 * Now that we've got the SIGWINCH handler installed, try to find
921 * out the initial terminal size.
923 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, &size
) >= 0) {
924 conf_set_int(conf
, CONF_width
, size
.ws_col
);
925 conf_set_int(conf
, CONF_height
, size
.ws_row
);
932 * Unix Plink doesn't provide any way to add forwardings after the
933 * connection is set up, so if there are none now, we can safely set
936 if (conf_get_int(conf
, CONF_protocol
) == PROT_SSH
&&
937 !conf_get_int(conf
, CONF_x11_forward
) &&
938 !conf_get_int(conf
, CONF_agentfwd
) &&
939 !conf_get_str_nthstrkey(conf
, CONF_portfwd
, 0))
940 conf_set_int(conf
, CONF_ssh_simple
, TRUE
);
943 * Start up the connection.
945 logctx
= log_init(NULL
, conf
);
946 console_provide_logctx(logctx
);
950 /* nodelay is only useful if stdin is a terminal device */
951 int nodelay
= conf_get_int(conf
, CONF_tcp_nodelay
) && isatty(0);
953 error
= back
->init(NULL
, &backhandle
, conf
,
954 conf_get_str(conf
, CONF_host
),
955 conf_get_int(conf
, CONF_port
),
957 conf_get_int(conf
, CONF_tcp_keepalives
));
959 fprintf(stderr
, "Unable to open connection:\n%s\n", error
);
962 back
->provide_logctx(backhandle
, logctx
);
963 ldisc_create(conf
, NULL
, back
, backhandle
, NULL
);
969 * Set up the initial console mode. We don't care if this call
970 * fails, because we know we aren't necessarily running in a
973 local_tty
= (tcgetattr(STDIN_FILENO
, &orig_termios
) == 0);
974 atexit(cleanup_termios
);
975 ldisc_update(NULL
, 1, 1);
977 now
= GETTICKCOUNT();
980 fd_set rset
, wset
, xset
;
990 FD_SET_MAX(signalpipe
[0], maxfd
, rset
);
992 if (connopen
&& !sending
&&
993 back
->connected(backhandle
) &&
994 back
->sendok(backhandle
) &&
995 back
->sendbuffer(backhandle
) < MAX_STDIN_BACKLOG
) {
996 /* If we're OK to send, then try to read from stdin. */
997 FD_SET_MAX(STDIN_FILENO
, maxfd
, rset
);
1000 if (bufchain_size(&stdout_data
) > 0) {
1001 /* If we have data for stdout, try to write to stdout. */
1002 FD_SET_MAX(STDOUT_FILENO
, maxfd
, wset
);
1005 if (bufchain_size(&stderr_data
) > 0) {
1006 /* If we have data for stderr, try to write to stderr. */
1007 FD_SET_MAX(STDERR_FILENO
, maxfd
, wset
);
1010 /* Count the currently active fds. */
1012 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
1013 fd
= next_fd(&fdstate
, &rwx
)) i
++;
1015 /* Expand the fdlist buffer if necessary. */
1018 fdlist
= sresize(fdlist
, fdsize
, int);
1022 * Add all currently open fds to the select sets, and store
1023 * them in fdlist as well.
1026 for (fd
= first_fd(&fdstate
, &rwx
); fd
>= 0;
1027 fd
= next_fd(&fdstate
, &rwx
)) {
1028 fdlist
[fdcount
++] = fd
;
1030 FD_SET_MAX(fd
, maxfd
, rset
);
1032 FD_SET_MAX(fd
, maxfd
, wset
);
1034 FD_SET_MAX(fd
, maxfd
, xset
);
1038 unsigned long next
, then
;
1040 struct timeval tv
, *ptv
;
1042 if (run_timers(now
, &next
)) {
1044 now
= GETTICKCOUNT();
1045 if (now
- then
> next
- then
)
1049 tv
.tv_sec
= ticks
/ 1000;
1050 tv
.tv_usec
= ticks
% 1000 * 1000;
1055 ret
= select(maxfd
, &rset
, &wset
, &xset
, ptv
);
1059 now
= GETTICKCOUNT();
1060 } while (ret
< 0 && errno
== EINTR
);
1067 for (i
= 0; i
< fdcount
; i
++) {
1070 * We must process exceptional notifications before
1071 * ordinary readability ones, or we may go straight
1072 * past the urgent marker.
1074 if (FD_ISSET(fd
, &xset
))
1075 select_result(fd
, 4);
1076 if (FD_ISSET(fd
, &rset
))
1077 select_result(fd
, 1);
1078 if (FD_ISSET(fd
, &wset
))
1079 select_result(fd
, 2);
1082 if (FD_ISSET(signalpipe
[0], &rset
)) {
1084 struct winsize size
;
1085 if (read(signalpipe
[0], c
, 1) <= 0)
1087 /* ignore its value; it'll be `x' */
1088 if (ioctl(STDIN_FILENO
, TIOCGWINSZ
, (void *)&size
) >= 0)
1089 back
->size(backhandle
, size
.ws_col
, size
.ws_row
);
1092 if (FD_ISSET(STDIN_FILENO
, &rset
)) {
1096 if (connopen
&& back
->connected(backhandle
)) {
1097 ret
= read(STDIN_FILENO
, buf
, sizeof(buf
));
1099 perror("stdin: read");
1101 } else if (ret
== 0) {
1102 back
->special(backhandle
, TS_EOF
);
1103 sending
= FALSE
; /* send nothing further after this */
1108 back
->send(backhandle
, buf
, ret
);
1113 if (FD_ISSET(STDOUT_FILENO
, &wset
)) {
1114 back
->unthrottle(backhandle
, try_output(FALSE
));
1117 if (FD_ISSET(STDERR_FILENO
, &wset
)) {
1118 back
->unthrottle(backhandle
, try_output(TRUE
));
1121 net_pending_errors();
1123 if ((!connopen
|| !back
->connected(backhandle
)) &&
1124 bufchain_size(&stdout_data
) == 0 &&
1125 bufchain_size(&stderr_data
) == 0)
1126 break; /* we closed the connection */
1128 exitcode
= back
->exitcode(backhandle
);
1130 fprintf(stderr
, "Remote process exit code unavailable\n");
1131 exitcode
= 1; /* this is an error condition */
1133 cleanup_exit(exitcode
);
1134 return exitcode
; /* shouldn't happen, but placates gcc */