2003-07-23 05:26:53 -04:00
|
|
|
/**********************************************************************
|
|
|
|
|
|
|
|
io/wait.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Tue Aug 28 09:08:06 JST 2001
|
|
|
|
|
|
|
|
All the files in this distribution are covered under the Ruby's
|
|
|
|
license (see the file COPYING).
|
|
|
|
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "rubyio.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2005-07-17 00:47:38 -04:00
|
|
|
#if defined(FIONREAD_HEADER)
|
2003-07-23 05:26:53 -04:00
|
|
|
#include FIONREAD_HEADER
|
2005-07-17 23:23:47 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_RB_W32_IOCTLSOCKET
|
2005-07-17 00:47:38 -04:00
|
|
|
#define ioctl ioctlsocket
|
2005-07-17 23:23:47 -04:00
|
|
|
#define ioctl_arg u_long
|
|
|
|
#define ioctl_arg2num(i) ULONG2NUM(i)
|
|
|
|
#else
|
|
|
|
#define ioctl_arg int
|
|
|
|
#define ioctl_arg2num(i) INT2NUM(i)
|
2005-07-17 00:47:38 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_RB_W32_IS_SOCKET
|
2005-07-17 23:23:47 -04:00
|
|
|
#define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd)
|
2005-07-17 00:47:38 -04:00
|
|
|
#else
|
2005-07-17 23:23:47 -04:00
|
|
|
#define FIONREAD_POSSIBLE_P(fd) ((fd),Qtrue)
|
2005-07-17 00:47:38 -04:00
|
|
|
#endif
|
2003-07-23 05:26:53 -04:00
|
|
|
|
|
|
|
static VALUE io_ready_p _((VALUE io));
|
|
|
|
static VALUE io_wait _((int argc, VALUE *argv, VALUE io));
|
|
|
|
void Init_wait _((void));
|
|
|
|
|
|
|
|
EXTERN struct timeval rb_time_interval _((VALUE time));
|
|
|
|
|
|
|
|
/*
|
|
|
|
=begin
|
|
|
|
= IO wait methods.
|
|
|
|
=end
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
=begin
|
|
|
|
--- IO#ready?
|
|
|
|
returns non-nil if input available without blocking, or nil.
|
|
|
|
=end
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
io_ready_p(io)
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2005-07-17 23:23:47 -04:00
|
|
|
ioctl_arg n;
|
2003-07-23 05:26:53 -04:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
* rubyio.h, intern.h, io.c, file.c, process.c, ext/socket/socket.c,
ext/pty/pty.c, ext/io/wait/wait.c, ext/openssl/ossl_ssl.c:
Use own buffering mechanism instead of stdio.
* io.c, ext/stringio/stringio.c, test/ruby/ut_eof.rb:
EOF flag removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-12-06 03:40:30 -05:00
|
|
|
if (rb_io_read_pending(fptr)) return Qtrue;
|
2005-07-17 00:47:38 -04:00
|
|
|
if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
|
* rubyio.h, intern.h, io.c, file.c, process.c, ext/socket/socket.c,
ext/pty/pty.c, ext/io/wait/wait.c, ext/openssl/ossl_ssl.c:
Use own buffering mechanism instead of stdio.
* io.c, ext/stringio/stringio.c, test/ruby/ut_eof.rb:
EOF flag removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-12-06 03:40:30 -05:00
|
|
|
if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
|
2005-07-17 23:23:47 -04:00
|
|
|
if (n > 0) return ioctl_arg2num(n);
|
2003-07-23 05:26:53 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2005-06-03 10:23:17 -04:00
|
|
|
struct wait_readable_arg {
|
|
|
|
rb_fdset_t fds;
|
|
|
|
struct timeval *timeout;
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef HAVE_RB_FD_INIT
|
|
|
|
static VALUE
|
|
|
|
wait_readable(p)
|
|
|
|
VALUE p;
|
|
|
|
{
|
|
|
|
struct wait_readable_arg *arg = (struct wait_readable_arg *)p;
|
|
|
|
rb_fdset_t *fds = &arg->fds;
|
|
|
|
|
|
|
|
return (VALUE)rb_thread_select(rb_fd_max(fds), rb_fd_ptr(fds), NULL, NULL, arg->timeout);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-23 05:26:53 -04:00
|
|
|
/*
|
|
|
|
=begin
|
|
|
|
--- IO#wait([timeout])
|
|
|
|
waits until input available or timed out and returns self, or nil
|
|
|
|
when EOF reached.
|
|
|
|
=end
|
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
io_wait(argc, argv, io)
|
|
|
|
int argc;
|
|
|
|
VALUE *argv;
|
|
|
|
VALUE io;
|
|
|
|
{
|
|
|
|
OpenFile *fptr;
|
2005-06-03 10:23:17 -04:00
|
|
|
struct wait_readable_arg arg;
|
2005-07-17 23:23:47 -04:00
|
|
|
int fd, i;
|
|
|
|
ioctl_arg n;
|
2003-07-23 05:26:53 -04:00
|
|
|
VALUE timeout;
|
2005-06-03 10:23:17 -04:00
|
|
|
struct timeval timerec;
|
2003-07-23 05:26:53 -04:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
rb_io_check_readable(fptr);
|
|
|
|
rb_scan_args(argc, argv, "01", &timeout);
|
|
|
|
if (NIL_P(timeout)) {
|
2005-06-03 10:23:17 -04:00
|
|
|
arg.timeout = 0;
|
2003-07-23 05:26:53 -04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
timerec = rb_time_interval(timeout);
|
2005-06-03 10:23:17 -04:00
|
|
|
arg.timeout = &timerec;
|
2003-07-23 05:26:53 -04:00
|
|
|
}
|
|
|
|
|
* rubyio.h, intern.h, io.c, file.c, process.c, ext/socket/socket.c,
ext/pty/pty.c, ext/io/wait/wait.c, ext/openssl/ossl_ssl.c:
Use own buffering mechanism instead of stdio.
* io.c, ext/stringio/stringio.c, test/ruby/ut_eof.rb:
EOF flag removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-12-06 03:40:30 -05:00
|
|
|
if (rb_io_read_pending(fptr)) return Qtrue;
|
2005-07-17 00:47:38 -04:00
|
|
|
if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse;
|
* rubyio.h, intern.h, io.c, file.c, process.c, ext/socket/socket.c,
ext/pty/pty.c, ext/io/wait/wait.c, ext/openssl/ossl_ssl.c:
Use own buffering mechanism instead of stdio.
* io.c, ext/stringio/stringio.c, test/ruby/ut_eof.rb:
EOF flag removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-12-06 03:40:30 -05:00
|
|
|
fd = fptr->fd;
|
2005-06-03 10:23:17 -04:00
|
|
|
rb_fd_init(&arg.fds);
|
|
|
|
rb_fd_set(fd, &arg.fds);
|
|
|
|
#ifdef HAVE_RB_FD_INIT
|
2005-07-17 23:23:47 -04:00
|
|
|
i = (int)rb_ensure(wait_readable, (VALUE)&arg,
|
2005-06-03 10:23:17 -04:00
|
|
|
(VALUE (*)_((VALUE)))rb_fd_term, (VALUE)&arg.fds);
|
|
|
|
#else
|
2005-07-17 23:23:47 -04:00
|
|
|
i = rb_thread_select(fd + 1, rb_fd_ptr(&arg.fds), NULL, NULL, arg.timeout);
|
2005-06-03 10:23:17 -04:00
|
|
|
#endif
|
2005-07-17 23:23:47 -04:00
|
|
|
if (i < 0)
|
2003-07-23 05:26:53 -04:00
|
|
|
rb_sys_fail(0);
|
|
|
|
rb_io_check_closed(fptr);
|
* rubyio.h, intern.h, io.c, file.c, process.c, ext/socket/socket.c,
ext/pty/pty.c, ext/io/wait/wait.c, ext/openssl/ossl_ssl.c:
Use own buffering mechanism instead of stdio.
* io.c, ext/stringio/stringio.c, test/ruby/ut_eof.rb:
EOF flag removed.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7473 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-12-06 03:40:30 -05:00
|
|
|
if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0);
|
2003-07-23 05:26:53 -04:00
|
|
|
if (n > 0) return io;
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_wait()
|
|
|
|
{
|
|
|
|
rb_define_method(rb_cIO, "ready?", io_ready_p, 0);
|
|
|
|
rb_define_method(rb_cIO, "wait", io_wait, -1);
|
|
|
|
}
|