From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/ext/io/console/Makefile | 284 ++++++++++ jni/ruby/ext/io/console/console.c | 774 ++++++++++++++++++++++++++++ jni/ruby/ext/io/console/depend | 4 + jni/ruby/ext/io/console/extconf.h | 6 + jni/ruby/ext/io/console/extconf.rb | 21 + jni/ruby/ext/io/console/io-console.gemspec | 21 + jni/ruby/ext/io/console/lib/console/size.rb | 22 + jni/ruby/ext/io/nonblock/Makefile | 273 ++++++++++ jni/ruby/ext/io/nonblock/depend | 4 + jni/ruby/ext/io/nonblock/extconf.h | 3 + jni/ruby/ext/io/nonblock/extconf.rb | 8 + jni/ruby/ext/io/nonblock/nonblock.c | 138 +++++ jni/ruby/ext/io/wait/Makefile | 273 ++++++++++ jni/ruby/ext/io/wait/depend | 4 + jni/ruby/ext/io/wait/extconf.h | 5 + jni/ruby/ext/io/wait/extconf.rb | 18 + jni/ruby/ext/io/wait/wait.c | 187 +++++++ 17 files changed, 2045 insertions(+) create mode 100644 jni/ruby/ext/io/console/Makefile create mode 100644 jni/ruby/ext/io/console/console.c create mode 100644 jni/ruby/ext/io/console/depend create mode 100644 jni/ruby/ext/io/console/extconf.h create mode 100644 jni/ruby/ext/io/console/extconf.rb create mode 100644 jni/ruby/ext/io/console/io-console.gemspec create mode 100644 jni/ruby/ext/io/console/lib/console/size.rb create mode 100644 jni/ruby/ext/io/nonblock/Makefile create mode 100644 jni/ruby/ext/io/nonblock/depend create mode 100644 jni/ruby/ext/io/nonblock/extconf.h create mode 100644 jni/ruby/ext/io/nonblock/extconf.rb create mode 100644 jni/ruby/ext/io/nonblock/nonblock.c create mode 100644 jni/ruby/ext/io/wait/Makefile create mode 100644 jni/ruby/ext/io/wait/depend create mode 100644 jni/ruby/ext/io/wait/extconf.h create mode 100644 jni/ruby/ext/io/wait/extconf.rb create mode 100644 jni/ruby/ext/io/wait/wait.c (limited to 'jni/ruby/ext/io') diff --git a/jni/ruby/ext/io/console/Makefile b/jni/ruby/ext/io/console/Makefile new file mode 100644 index 0000000..fad3282 --- /dev/null +++ b/jni/ruby/ext/io/console/Makefile @@ -0,0 +1,284 @@ + +SHELL = /bin/sh + +# V=0 quiet, V=1 verbose. other values don't work. +V = 0 +Q1 = $(V:1=) +Q = $(Q1:0=@) +ECHO1 = $(V:1=@:) +ECHO = $(ECHO1:0=@echo) +NULLCMD = : + +#### Start of system configuration section. #### +top_srcdir = $(topdir)/. +srcdir = $(top_srcdir)/ext/io/console +topdir = ../../.. +hdrdir = $(top_srcdir)/include +arch_hdrdir = $(extout)/include/$(arch) +PATH_SEPARATOR = : +VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby +RUBYLIB = +RUBYOPT = - +prefix = $(DESTDIR)/usr/local +rubysitearchprefix = $(rubylibprefix)/$(sitearch) +rubyarchprefix = $(rubylibprefix)/$(arch) +rubylibprefix = $(libdir)/$(RUBY_BASE_NAME) +exec_prefix = $(prefix) +vendorarchhdrdir = $(vendorhdrdir)/$(sitearch) +sitearchhdrdir = $(sitehdrdir)/$(sitearch) +rubyarchhdrdir = $(rubyhdrdir)/$(arch) +vendorhdrdir = $(rubyhdrdir)/vendor_ruby +sitehdrdir = $(rubyhdrdir)/site_ruby +rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME) +vendorarchdir = $(vendorlibdir)/$(sitearch) +vendorlibdir = $(vendordir)/$(ruby_version) +vendordir = $(rubylibprefix)/vendor_ruby +sitearchdir = $(sitelibdir)/$(sitearch) +sitelibdir = $(sitedir)/$(ruby_version) +sitedir = $(rubylibprefix)/site_ruby +rubyarchdir = $(rubylibdir)/$(arch) +rubylibdir = $(rubylibprefix)/$(ruby_version) +sitearchincludedir = $(includedir)/$(sitearch) +archincludedir = $(includedir)/$(arch) +sitearchlibdir = $(libdir)/$(sitearch) +archlibdir = $(libdir)/$(arch) +ridir = $(datarootdir)/$(RI_BASE_NAME) +mandir = $(datarootdir)/man +localedir = $(datarootdir)/locale +libdir = $(exec_prefix)/lib +psdir = $(docdir) +pdfdir = $(docdir) +dvidir = $(docdir) +htmldir = $(docdir) +infodir = $(datarootdir)/info +docdir = $(datarootdir)/doc/$(PACKAGE) +oldincludedir = $(DESTDIR)/usr/include +includedir = $(prefix)/include +localstatedir = $(prefix)/var +sharedstatedir = $(prefix)/com +sysconfdir = $(prefix)/etc +datadir = $(datarootdir) +datarootdir = $(prefix)/share +libexecdir = $(exec_prefix)/libexec +sbindir = $(exec_prefix)/sbin +bindir = $(exec_prefix)/bin +archdir = $(rubyarchdir) + + +CC = gcc +CXX = g++ +LIBRUBY = $(LIBRUBY_SO) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME) +LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static +empty = +OUTFLAG = -o $(empty) +COUTFLAG = -o $(empty) + +RUBY_EXTCONF_H = extconf.h +cflags = $(optflags) $(debugflags) $(warnflags) +optflags = -O3 -fno-fast-math +debugflags = -ggdb3 +warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat +CCDLFLAGS = -fPIC +CFLAGS = $(CCDLFLAGS) $(cflags) -fPIC $(ARCH_FLAG) +INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir) +DEFS = +CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags) +CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG) +ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic +dldflags = +ARCH_FLAG = +DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG) +LDSHARED = $(CC) -shared +LDSHAREDXX = $(CXX) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = $(RUBY_BASE_NAME) +RUBY_SO_NAME = ruby +RUBYW_INSTALL_NAME = +RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version) +RUBYW_BASE_NAME = rubyw +RUBY_BASE_NAME = ruby + +arch = x86_64-linux +sitearch = $(arch) +ruby_version = 2.2.0 +ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(top_srcdir)/lib' -I'$(extout)/$(arch)' -I'$(extout)/common' +RUBY = $(ruby) +ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H) + +RM = rm -f +RM_RF = $(RUBY) -run -e rm -- -rf +RMDIRS = rmdir --ignore-fail-on-non-empty -p +MAKEDIRS = /bin/mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp +TOUCH = exit > + +#### End of system configuration section. #### + +preload = + +libpath = . $(topdir) +LIBPATH = -L. -L$(topdir) +DEFFILE = + +CLEANFILES = mkmf.log +DISTCLEANFILES = +DISTCLEANDIRS = + +extout = $(topdir)/.ext +extout_prefix = $(extout)$(target_prefix)/ +target_prefix = /io +LOCAL_LIBS = +LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc +ORIG_SRCS = console.c +SRCS = $(ORIG_SRCS) +OBJS = console.o +HDRS = $(srcdir)/extconf.h +TARGET = console +TARGET_NAME = console +TARGET_ENTRY = Init_$(TARGET_NAME) +DLLIB = $(TARGET).so +EXTSTATIC = +STATIC_LIB = $(TARGET).a + +TIMESTAMP_DIR = $(extout)/.timestamp +BINDIR = $(extout)/bin +RUBYCOMMONDIR = $(extout)/common +RUBYLIBDIR = $(RUBYCOMMONDIR)$(target_prefix) +RUBYARCHDIR = $(extout)/$(arch)$(target_prefix) +HDRDIR = $(extout)/include/ruby$(target_prefix) +ARCHHDRDIR = $(extout)/include/$(arch)/ruby$(target_prefix) + +TARGET_SO = $(RUBYARCHDIR)/$(DLLIB) +CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so +CLEANOBJS = *.o *.bak + +all: install +static: all +.PHONY: all install static install-so install-rb +.PHONY: clean clean-so clean-static clean-rb + +clean-static:: +clean-rb-default:: +clean-rb:: +clean-so:: +clean: clean-so clean-static clean-rb-default clean-rb + -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time + +distclean-rb-default:: +distclean-rb:: +distclean-so:: +distclean-static:: +distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb + -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log + -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR)/$(DLLIB) +clean-so:: + -$(Q)$(RM) $(RUBYARCHDIR)/$(DLLIB) + -$(Q)$(RMDIRS) $(RUBYARCHDIR) 2> /dev/null || true +clean-static:: + -$(Q)$(RM) $(STATIC_LIB) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +pre-install-rb-default: $(TIMESTAMP_DIR)/.RUBYLIBDIR.-.io.-.console.time +install-rb-default: $(RUBYLIBDIR)/console/size.rb +$(RUBYLIBDIR)/console/size.rb: $(srcdir)/lib/console/size.rb $(TIMESTAMP_DIR)/.RUBYLIBDIR.-.io.-.console.time + $(Q) $(COPY) $(srcdir)/lib/console/size.rb $(@D) +clean-rb-default:: + -$(Q)$(RM) $(RUBYLIBDIR)/console/size.rb +pre-install-rb-default: + $(ECHO) installing default console libraries +clean-rb-default:: + -$(Q)$(RMDIRS) $(RUBYLIBDIR)/console 2> /dev/null || true +$(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time: + $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR) + $(Q) $(TOUCH) $@ +$(TIMESTAMP_DIR)/.RUBYLIBDIR.-.io.-.console.time: + $(Q) $(MAKEDIRS) $(@D) $(RUBYLIBDIR)/console + $(Q) $(TOUCH) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S + +.cc.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cc.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.mm.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.mm.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cxx.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cxx.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cpp.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cpp.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.c.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.c.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +.m.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.m.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +$(RUBYARCHDIR)/$(DLLIB): $(OBJS) Makefile $(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time + $(ECHO) linking shared-object io/$(DLLIB) + -$(Q)$(RM) $(@) + $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS) + +$(STATIC_LIB): $(OBJS) + -$(Q)$(RM) $(@) + $(ECHO) linking static-library $(@) + $(Q) $(AR) cru $@ $(OBJS) + -$(Q)ranlib $(@) 2> /dev/null || true + +### +$(OBJS): $(RUBY_EXTCONF_H) + +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/console/console.c b/jni/ruby/ext/io/console/console.c new file mode 100644 index 0000000..e846450 --- /dev/null +++ b/jni/ruby/ext/io/console/console.c @@ -0,0 +1,774 @@ +/* -*- c-file-style: "ruby" -*- */ +/* + * console IO module + */ +#include "ruby.h" +#include "ruby/io.h" + +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_FCNTL_H +#include +#endif +#ifdef HAVE_SYS_IOCTL_H +#include +#endif + +#if defined HAVE_TERMIOS_H +# include +typedef struct termios conmode; + +static int +setattr(int fd, conmode *t) +{ + while (tcsetattr(fd, TCSAFLUSH, t)) { + if (errno != EINTR) return 0; + } + return 1; +} +# define getattr(fd, t) (tcgetattr(fd, t) == 0) +#elif defined HAVE_TERMIO_H +# include +typedef struct termio conmode; +# define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0) +# define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0) +#elif defined HAVE_SGTTY_H +# include +typedef struct sgttyb conmode; +# ifdef HAVE_STTY +# define setattr(fd, t) (stty(fd, t) == 0) +# else +# define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0) +# endif +# ifdef HAVE_GTTY +# define getattr(fd, t) (gtty(fd, t) == 0) +# else +# define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0) +# endif +#elif defined _WIN32 +#include +typedef DWORD conmode; + +#ifdef HAVE_RB_W32_MAP_ERRNO +#define LAST_ERROR rb_w32_map_errno(GetLastError()) +#else +#define LAST_ERROR EBADF +#endif +#define SET_LAST_ERROR (errno = LAST_ERROR, 0) + +static int +setattr(int fd, conmode *t) +{ + int x = SetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), *t); + if (!x) errno = LAST_ERROR; + return x; +} + +static int +getattr(int fd, conmode *t) +{ + int x = GetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), t); + if (!x) errno = LAST_ERROR; + return x; +} +#endif +#ifndef SET_LAST_ERROR +#define SET_LAST_ERROR (0) +#endif + +static ID id_getc, id_console, id_close; + +typedef struct { + int vmin; + int vtime; +} rawmode_arg_t; + +static rawmode_arg_t * +rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts) +{ + rawmode_arg_t *optp = NULL; + VALUE vopts; + rb_scan_args(argc, argv, "0:", &vopts); + if (!NIL_P(vopts)) { + VALUE vmin = rb_hash_aref(vopts, ID2SYM(rb_intern("min"))); + VALUE vtime = rb_hash_aref(vopts, ID2SYM(rb_intern("time"))); + /* default values by `stty raw` */ + opts->vmin = 1; + opts->vtime = 0; + if (!NIL_P(vmin)) { + opts->vmin = NUM2INT(vmin); + optp = opts; + } + if (!NIL_P(vtime)) { + VALUE v10 = INT2FIX(10); + vtime = rb_funcall3(vtime, '*', 1, &v10); + opts->vtime = NUM2INT(vtime); + optp = opts; + } + } + return optp; +} + +static void +set_rawmode(conmode *t, void *arg) +{ +#ifdef HAVE_CFMAKERAW + cfmakeraw(t); + t->c_lflag &= ~(ECHOE|ECHOK); +#elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); + t->c_oflag &= ~OPOST; + t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN); + t->c_cflag &= ~(CSIZE|PARENB); + t->c_cflag |= CS8; +#elif defined HAVE_SGTTY_H + t->sg_flags &= ~ECHO; + t->sg_flags |= RAW; +#elif defined _WIN32 + *t = 0; +#endif +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + if (arg) { + const rawmode_arg_t *r = arg; + if (r->vmin >= 0) t->c_cc[VMIN] = r->vmin; + if (r->vtime >= 0) t->c_cc[VTIME] = r->vtime; + } +#endif +} + +static void +set_cookedmode(conmode *t, void *arg) +{ +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON); + t->c_oflag |= OPOST; + t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN); +#elif defined HAVE_SGTTY_H + t->sg_flags |= ECHO; + t->sg_flags &= ~RAW; +#elif defined _WIN32 + *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT; +#endif +} + +static void +set_noecho(conmode *t, void *arg) +{ +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); +#elif defined HAVE_SGTTY_H + t->sg_flags &= ~ECHO; +#elif defined _WIN32 + *t &= ~ENABLE_ECHO_INPUT; +#endif +} + +static void +set_echo(conmode *t, void *arg) +{ +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL); +#elif defined HAVE_SGTTY_H + t->sg_flags |= ECHO; +#elif defined _WIN32 + *t |= ENABLE_ECHO_INPUT; +#endif +} + +static int +echo_p(conmode *t) +{ +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + return (t->c_lflag & (ECHO | ECHONL)) != 0; +#elif defined HAVE_SGTTY_H + return (t->sg_flags & ECHO) != 0; +#elif defined _WIN32 + return (*t & ENABLE_ECHO_INPUT) != 0; +#endif +} + +static int +set_ttymode(int fd, conmode *t, void (*setter)(conmode *, void *), void *arg) +{ + conmode r; + if (!getattr(fd, t)) return 0; + r = *t; + setter(&r, arg); + return setattr(fd, &r); +} + +#define GetReadFD(fptr) ((fptr)->fd) + +static inline int +get_write_fd(const rb_io_t *fptr) +{ + VALUE wio = fptr->tied_io_for_writing; + rb_io_t *ofptr; + if (!wio) return fptr->fd; + GetOpenFile(wio, ofptr); + return ofptr->fd; +} +#define GetWriteFD(fptr) get_write_fd(fptr) + +#define FD_PER_IO 2 + +static VALUE +ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg) +{ + rb_io_t *fptr; + int status = -1; + int error = 0; + int fd[FD_PER_IO]; + conmode t[FD_PER_IO]; + VALUE result = Qnil; + + GetOpenFile(io, fptr); + fd[0] = GetReadFD(fptr); + if (fd[0] != -1) { + if (set_ttymode(fd[0], t+0, setter, arg)) { + status = 0; + } + else { + error = errno; + fd[0] = -1; + } + } + fd[1] = GetWriteFD(fptr); + if (fd[1] != -1 && fd[1] != fd[0]) { + if (set_ttymode(fd[1], t+1, setter, arg)) { + status = 0; + } + else { + error = errno; + fd[1] = -1; + } + } + if (status == 0) { + result = rb_protect(func, io, &status); + } + GetOpenFile(io, fptr); + if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) { + if (!setattr(fd[0], t+0)) { + error = errno; + status = -1; + } + } + if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) { + if (!setattr(fd[1], t+1)) { + error = errno; + status = -1; + } + } + if (status) { + if (status == -1) { + errno = error; + rb_sys_fail(0); + } + rb_jump_tag(status); + } + return result; +} + +/* + * call-seq: + * io.raw(min: nil, time: nil) {|io| } + * + * Yields +self+ within raw mode. + * + * STDIN.raw(&:gets) + * + * will read and return a line without echo back and line editing. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_raw(int argc, VALUE *argv, VALUE io) +{ + rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); + return ttymode(io, rb_yield, set_rawmode, optp); +} + +/* + * call-seq: + * io.raw!(min: nil, time: nil) + * + * Enables raw mode. + * + * If the terminal mode needs to be back, use io.raw { ... }. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_set_raw(int argc, VALUE *argv, VALUE io) +{ + conmode t; + rb_io_t *fptr; + int fd; + rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); + if (!getattr(fd, &t)) rb_sys_fail(0); + set_rawmode(&t, optp); + if (!setattr(fd, &t)) rb_sys_fail(0); + return io; +} + +/* + * call-seq: + * io.cooked {|io| } + * + * Yields +self+ within cooked mode. + * + * STDIN.cooked(&:gets) + * + * will read and return a line with echo back and line editing. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_cooked(VALUE io) +{ + return ttymode(io, rb_yield, set_cookedmode, NULL); +} + +/* + * call-seq: + * io.cooked! + * + * Enables cooked mode. + * + * If the terminal mode needs to be back, use io.cooked { ... }. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_set_cooked(VALUE io) +{ + conmode t; + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); + if (!getattr(fd, &t)) rb_sys_fail(0); + set_cookedmode(&t, NULL); + if (!setattr(fd, &t)) rb_sys_fail(0); + return io; +} + +static VALUE +getc_call(VALUE io) +{ + return rb_funcall2(io, id_getc, 0, 0); +} + +/* + * call-seq: + * io.getch(min: nil, time: nil) -> char + * + * Reads and returns a character in raw mode. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_getch(int argc, VALUE *argv, VALUE io) +{ + rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); + return ttymode(io, getc_call, set_rawmode, optp); +} + +/* + * call-seq: + * io.noecho {|io| } + * + * Yields +self+ with disabling echo back. + * + * STDIN.noecho(&:gets) + * + * will read and return a line without echo back. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_noecho(VALUE io) +{ + return ttymode(io, rb_yield, set_noecho, NULL); +} + +/* + * call-seq: + * io.echo = flag + * + * Enables/disables echo back. + * On some platforms, all combinations of this flags and raw/cooked + * mode may not be valid. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_set_echo(VALUE io, VALUE f) +{ + conmode t; + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); + if (!getattr(fd, &t)) rb_sys_fail(0); + if (RTEST(f)) + set_echo(&t, NULL); + else + set_noecho(&t, NULL); + if (!setattr(fd, &t)) rb_sys_fail(0); + return io; +} + +/* + * call-seq: + * io.echo? -> true or false + * + * Returns +true+ if echo back is enabled. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_echo_p(VALUE io) +{ + conmode t; + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); + if (!getattr(fd, &t)) rb_sys_fail(0); + return echo_p(&t) ? Qtrue : Qfalse; +} + +#if defined TIOCGWINSZ +typedef struct winsize rb_console_size_t; +#define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0) +#define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0) +#define winsize_row(buf) (buf)->ws_row +#define winsize_col(buf) (buf)->ws_col +#elif defined _WIN32 +typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t; +#define getwinsize(fd, buf) ( \ + GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \ + SET_LAST_ERROR) +#define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1) +#define winsize_col(buf) (buf)->dwSize.X +#endif + +#if defined TIOCGWINSZ || defined _WIN32 +#define USE_CONSOLE_GETSIZE 1 +#endif + +#ifdef USE_CONSOLE_GETSIZE +/* + * call-seq: + * io.winsize -> [rows, columns] + * + * Returns console size. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_winsize(VALUE io) +{ + rb_io_t *fptr; + int fd; + rb_console_size_t ws; + + GetOpenFile(io, fptr); + fd = GetWriteFD(fptr); + if (!getwinsize(fd, &ws)) rb_sys_fail(0); + return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws))); +} + +/* + * call-seq: + * io.winsize = [rows, columns] + * + * Tries to set console size. The effect depends on the platform and + * the running environment. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_set_winsize(VALUE io, VALUE size) +{ + rb_io_t *fptr; + rb_console_size_t ws; +#if defined _WIN32 + HANDLE wh; + int newrow, newcol; +#endif + VALUE row, col, xpixel, ypixel; +#if defined TIOCSWINSZ + int fd; +#endif + + GetOpenFile(io, fptr); + size = rb_Array(size); + rb_scan_args((int)RARRAY_LEN(size), RARRAY_PTR(size), "22", + &row, &col, &xpixel, &ypixel); +#if defined TIOCSWINSZ + fd = GetWriteFD(fptr); + ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0; +#define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) + SET(row); + SET(col); + SET(xpixel); + SET(ypixel); +#undef SET + if (!setwinsize(fd, &ws)) rb_sys_fail(0); +#elif defined _WIN32 + wh = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr)); + newrow = (SHORT)NUM2UINT(row); + newcol = (SHORT)NUM2UINT(col); + if (!getwinsize(GetReadFD(fptr), &ws)) { + rb_sys_fail("GetConsoleScreenBufferInfo"); + } + if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || + (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { + if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) || SET_LAST_ERROR)) { + rb_sys_fail("SetConsoleScreenBufferInfo"); + } + } + ws.srWindow.Left = 0; + ws.srWindow.Top = 0; + ws.srWindow.Right = newcol; + ws.srWindow.Bottom = newrow; + if (!(SetConsoleWindowInfo(wh, FALSE, &ws.srWindow) || SET_LAST_ERROR)) { + rb_sys_fail("SetConsoleWindowInfo"); + } +#endif + return io; +} +#endif + +/* + * call-seq: + * io.iflush + * + * Flushes input buffer in kernel. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_iflush(VALUE io) +{ + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + if (tcflush(fd, TCIFLUSH)) rb_sys_fail(0); +#endif + (void)fd; + return io; +} + +/* + * call-seq: + * io.oflush + * + * Flushes output buffer in kernel. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_oflush(VALUE io) +{ + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetWriteFD(fptr); +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + if (tcflush(fd, TCOFLUSH)) rb_sys_fail(0); +#endif + (void)fd; + return io; +} + +/* + * call-seq: + * io.ioflush + * + * Flushes input and output buffers in kernel. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_ioflush(VALUE io) +{ + rb_io_t *fptr; +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + int fd1, fd2; +#endif + + GetOpenFile(io, fptr); +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + fd1 = GetReadFD(fptr); + fd2 = GetWriteFD(fptr); + if (fd2 != -1 && fd1 != fd2) { + if (tcflush(fd1, TCIFLUSH)) rb_sys_fail(0); + if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0); + } + else { + if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0); + } +#endif + return io; +} + +/* + * call-seq: + * IO.console -> # + * IO.console(sym, *args) + * + * Returns an File instance opened console. + * + * If +sym+ is given, it will be sent to the opened console with + * +args+ and the result will be returned instead of the console IO + * itself. + * + * You must require 'io/console' to use this method. + */ +static VALUE +console_dev(int argc, VALUE *argv, VALUE klass) +{ + VALUE con = 0; + rb_io_t *fptr; + VALUE sym = 0; + + rb_check_arity(argc, 0, 1); + if (argc) { + Check_Type(sym = argv[0], T_SYMBOL); + --argc; + ++argv; + } + if (klass == rb_cIO) klass = rb_cFile; + if (rb_const_defined(klass, id_console)) { + con = rb_const_get(klass, id_console); + if (!RB_TYPE_P(con, T_FILE) || + (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1)) { + rb_const_remove(klass, id_console); + con = 0; + } + } + if (sym) { + if (sym == ID2SYM(id_close) && !argc) { + if (con) { + rb_io_close(con); + rb_const_remove(klass, id_console); + con = 0; + } + return Qnil; + } + } + if (!con) { + VALUE args[2]; +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H +# define CONSOLE_DEVICE "/dev/tty" +#elif defined _WIN32 +# define CONSOLE_DEVICE "con$" +# define CONSOLE_DEVICE_FOR_READING "conin$" +# define CONSOLE_DEVICE_FOR_WRITING "conout$" +#endif +#ifndef CONSOLE_DEVICE_FOR_READING +# define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE +#endif +#ifdef CONSOLE_DEVICE_FOR_WRITING + VALUE out; + rb_io_t *ofptr; +#endif + int fd; + +#ifdef CONSOLE_DEVICE_FOR_WRITING + fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0); + if (fd < 0) return Qnil; + rb_update_max_fd(fd); + args[1] = INT2FIX(O_WRONLY); + args[0] = INT2NUM(fd); + out = rb_class_new_instance(2, args, klass); +#endif + fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0); + if (fd < 0) { +#ifdef CONSOLE_DEVICE_FOR_WRITING + rb_io_close(out); +#endif + return Qnil; + } + rb_update_max_fd(fd); + args[1] = INT2FIX(O_RDWR); + args[0] = INT2NUM(fd); + con = rb_class_new_instance(2, args, klass); + GetOpenFile(con, fptr); + fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE)); +#ifdef CONSOLE_DEVICE_FOR_WRITING + GetOpenFile(out, ofptr); + ofptr->pathv = fptr->pathv; + fptr->tied_io_for_writing = out; + ofptr->mode |= FMODE_SYNC; +#endif + fptr->mode |= FMODE_SYNC; + rb_const_set(klass, id_console, con); + } + if (sym) { + /* TODO: avoid inadvertent pindown */ + return rb_funcall(con, SYM2ID(sym), argc, argv); + } + return con; +} + +/* + * call-seq: + * io.getch(min: nil, time: nil) -> char + * + * See IO#getch. + */ +static VALUE +io_getch(int argc, VALUE *argv, VALUE io) +{ + return rb_funcall2(io, rb_intern("getc"), argc, argv); +} + +/* + * IO console methods + */ +void +Init_console(void) +{ + id_getc = rb_intern("getc"); + id_console = rb_intern("console"); + id_close = rb_intern("close"); + InitVM(console); +} + +void +InitVM_console(void) +{ + rb_define_method(rb_cIO, "raw", console_raw, -1); + rb_define_method(rb_cIO, "raw!", console_set_raw, -1); + rb_define_method(rb_cIO, "cooked", console_cooked, 0); + rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0); + rb_define_method(rb_cIO, "getch", console_getch, -1); + rb_define_method(rb_cIO, "echo=", console_set_echo, 1); + rb_define_method(rb_cIO, "echo?", console_echo_p, 0); + rb_define_method(rb_cIO, "noecho", console_noecho, 0); + rb_define_method(rb_cIO, "winsize", console_winsize, 0); + rb_define_method(rb_cIO, "winsize=", console_set_winsize, 1); + rb_define_method(rb_cIO, "iflush", console_iflush, 0); + rb_define_method(rb_cIO, "oflush", console_oflush, 0); + rb_define_method(rb_cIO, "ioflush", console_ioflush, 0); + rb_define_singleton_method(rb_cIO, "console", console_dev, -1); + { + VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable"); + rb_define_method(mReadable, "getch", io_getch, -1); + } +} diff --git a/jni/ruby/ext/io/console/depend b/jni/ruby/ext/io/console/depend new file mode 100644 index 0000000..e786dc7 --- /dev/null +++ b/jni/ruby/ext/io/console/depend @@ -0,0 +1,4 @@ +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/console/extconf.h b/jni/ruby/ext/io/console/extconf.h new file mode 100644 index 0000000..693d523 --- /dev/null +++ b/jni/ruby/ext/io/console/extconf.h @@ -0,0 +1,6 @@ +#ifndef EXTCONF_H +#define EXTCONF_H +#define HAVE_TERMIOS_H 1 +#define HAVE_CFMAKERAW 1 +#define HAVE_SYS_IOCTL_H 1 +#endif diff --git a/jni/ruby/ext/io/console/extconf.rb b/jni/ruby/ext/io/console/extconf.rb new file mode 100644 index 0000000..eb431b0 --- /dev/null +++ b/jni/ruby/ext/io/console/extconf.rb @@ -0,0 +1,21 @@ +require 'mkmf' + +ok = true +hdr = nil +case +when macro_defined?("_WIN32", "") + # rb_w32_map_errno: 1.8.7 +when hdr = %w"termios.h termio.h".find {|h| have_header(h)} + have_func("cfmakeraw", hdr) +when have_header(hdr = "sgtty.h") + %w"stty gtty".each {|f| have_func(f, hdr)} +else + ok = false +end +if ok + have_header("sys/ioctl.h") + # rb_check_hash_type: 1.9.3 + # rb_io_get_write_io: 1.9.1 + # rb_cloexec_open: 2.0.0 + create_makefile("io/console") +end diff --git a/jni/ruby/ext/io/console/io-console.gemspec b/jni/ruby/ext/io/console/io-console.gemspec new file mode 100644 index 0000000..79a2843 --- /dev/null +++ b/jni/ruby/ext/io/console/io-console.gemspec @@ -0,0 +1,21 @@ +# -*- ruby -*- +_VERSION = "0.4.3" +date = %w$Date:: 2015-07-01 04:17:07 +0900#$[1] + +Gem::Specification.new do |s| + s.name = "io-console" + s.version = _VERSION + s.date = date + s.summary = "Console interface" + s.email = "nobu@ruby-lang.org" + s.description = "add console capabilities to IO instances." + s.required_ruby_version = ">= 2.0.0" + s.homepage = "http://www.ruby-lang.org" + s.authors = ["Nobu Nakada"] + s.require_path = %[lib] + s.files = %w[console.c extconf.rb lib/console/size.rb] + s.extensions = %w[extconf.rb] + s.licenses = "ruby" + s.cert_chain = %w[certs/nobu.pem] + s.signing_key = File.expand_path("~/.ssh/gem-private_key.pem") if $0 =~ /gem\z/ +end diff --git a/jni/ruby/ext/io/console/lib/console/size.rb b/jni/ruby/ext/io/console/lib/console/size.rb new file mode 100644 index 0000000..519bc3b --- /dev/null +++ b/jni/ruby/ext/io/console/lib/console/size.rb @@ -0,0 +1,22 @@ +# fallback to console window size +def IO.default_console_size + [ + ENV["LINES"].to_i.nonzero? || 25, + ENV["COLUMNS"].to_i.nonzero? || 80, + ] +end + +begin + require 'io/console' +rescue LoadError + class IO + alias console_size default_console_size + end +else + # returns console window size + def IO.console_size + console.winsize + rescue NoMethodError + default_console_size + end +end diff --git a/jni/ruby/ext/io/nonblock/Makefile b/jni/ruby/ext/io/nonblock/Makefile new file mode 100644 index 0000000..1b79b89 --- /dev/null +++ b/jni/ruby/ext/io/nonblock/Makefile @@ -0,0 +1,273 @@ + +SHELL = /bin/sh + +# V=0 quiet, V=1 verbose. other values don't work. +V = 0 +Q1 = $(V:1=) +Q = $(Q1:0=@) +ECHO1 = $(V:1=@:) +ECHO = $(ECHO1:0=@echo) +NULLCMD = : + +#### Start of system configuration section. #### +top_srcdir = $(topdir)/. +srcdir = $(top_srcdir)/ext/io/nonblock +topdir = ../../.. +hdrdir = $(top_srcdir)/include +arch_hdrdir = $(extout)/include/$(arch) +PATH_SEPARATOR = : +VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby +RUBYLIB = +RUBYOPT = - +prefix = $(DESTDIR)/usr/local +rubysitearchprefix = $(rubylibprefix)/$(sitearch) +rubyarchprefix = $(rubylibprefix)/$(arch) +rubylibprefix = $(libdir)/$(RUBY_BASE_NAME) +exec_prefix = $(prefix) +vendorarchhdrdir = $(vendorhdrdir)/$(sitearch) +sitearchhdrdir = $(sitehdrdir)/$(sitearch) +rubyarchhdrdir = $(rubyhdrdir)/$(arch) +vendorhdrdir = $(rubyhdrdir)/vendor_ruby +sitehdrdir = $(rubyhdrdir)/site_ruby +rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME) +vendorarchdir = $(vendorlibdir)/$(sitearch) +vendorlibdir = $(vendordir)/$(ruby_version) +vendordir = $(rubylibprefix)/vendor_ruby +sitearchdir = $(sitelibdir)/$(sitearch) +sitelibdir = $(sitedir)/$(ruby_version) +sitedir = $(rubylibprefix)/site_ruby +rubyarchdir = $(rubylibdir)/$(arch) +rubylibdir = $(rubylibprefix)/$(ruby_version) +sitearchincludedir = $(includedir)/$(sitearch) +archincludedir = $(includedir)/$(arch) +sitearchlibdir = $(libdir)/$(sitearch) +archlibdir = $(libdir)/$(arch) +ridir = $(datarootdir)/$(RI_BASE_NAME) +mandir = $(datarootdir)/man +localedir = $(datarootdir)/locale +libdir = $(exec_prefix)/lib +psdir = $(docdir) +pdfdir = $(docdir) +dvidir = $(docdir) +htmldir = $(docdir) +infodir = $(datarootdir)/info +docdir = $(datarootdir)/doc/$(PACKAGE) +oldincludedir = $(DESTDIR)/usr/include +includedir = $(prefix)/include +localstatedir = $(prefix)/var +sharedstatedir = $(prefix)/com +sysconfdir = $(prefix)/etc +datadir = $(datarootdir) +datarootdir = $(prefix)/share +libexecdir = $(exec_prefix)/libexec +sbindir = $(exec_prefix)/sbin +bindir = $(exec_prefix)/bin +archdir = $(rubyarchdir) + + +CC = gcc +CXX = g++ +LIBRUBY = $(LIBRUBY_SO) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME) +LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static +empty = +OUTFLAG = -o $(empty) +COUTFLAG = -o $(empty) + +RUBY_EXTCONF_H = extconf.h +cflags = $(optflags) $(debugflags) $(warnflags) +optflags = -O3 -fno-fast-math +debugflags = -ggdb3 +warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat +CCDLFLAGS = -fPIC +CFLAGS = $(CCDLFLAGS) $(cflags) -fPIC $(ARCH_FLAG) +INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir) +DEFS = +CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags) +CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG) +ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic +dldflags = +ARCH_FLAG = +DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG) +LDSHARED = $(CC) -shared +LDSHAREDXX = $(CXX) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = $(RUBY_BASE_NAME) +RUBY_SO_NAME = ruby +RUBYW_INSTALL_NAME = +RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version) +RUBYW_BASE_NAME = rubyw +RUBY_BASE_NAME = ruby + +arch = x86_64-linux +sitearch = $(arch) +ruby_version = 2.2.0 +ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(top_srcdir)/lib' -I'$(extout)/$(arch)' -I'$(extout)/common' +RUBY = $(ruby) +ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H) + +RM = rm -f +RM_RF = $(RUBY) -run -e rm -- -rf +RMDIRS = rmdir --ignore-fail-on-non-empty -p +MAKEDIRS = /bin/mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp +TOUCH = exit > + +#### End of system configuration section. #### + +preload = + +libpath = . $(topdir) +LIBPATH = -L. -L$(topdir) +DEFFILE = + +CLEANFILES = mkmf.log +DISTCLEANFILES = +DISTCLEANDIRS = + +extout = $(topdir)/.ext +extout_prefix = $(extout)$(target_prefix)/ +target_prefix = /io +LOCAL_LIBS = +LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc +ORIG_SRCS = nonblock.c +SRCS = $(ORIG_SRCS) +OBJS = nonblock.o +HDRS = $(srcdir)/extconf.h +TARGET = nonblock +TARGET_NAME = nonblock +TARGET_ENTRY = Init_$(TARGET_NAME) +DLLIB = $(TARGET).so +EXTSTATIC = +STATIC_LIB = $(TARGET).a + +TIMESTAMP_DIR = $(extout)/.timestamp +BINDIR = $(extout)/bin +RUBYCOMMONDIR = $(extout)/common +RUBYLIBDIR = $(RUBYCOMMONDIR)$(target_prefix) +RUBYARCHDIR = $(extout)/$(arch)$(target_prefix) +HDRDIR = $(extout)/include/ruby$(target_prefix) +ARCHHDRDIR = $(extout)/include/$(arch)/ruby$(target_prefix) + +TARGET_SO = $(RUBYARCHDIR)/$(DLLIB) +CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so +CLEANOBJS = *.o *.bak + +all: install +static: all +.PHONY: all install static install-so install-rb +.PHONY: clean clean-so clean-static clean-rb + +clean-static:: +clean-rb-default:: +clean-rb:: +clean-so:: +clean: clean-so clean-static clean-rb-default clean-rb + -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time + +distclean-rb-default:: +distclean-rb:: +distclean-so:: +distclean-static:: +distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb + -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log + -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR)/$(DLLIB) +clean-so:: + -$(Q)$(RM) $(RUBYARCHDIR)/$(DLLIB) + -$(Q)$(RMDIRS) $(RUBYARCHDIR) 2> /dev/null || true +clean-static:: + -$(Q)$(RM) $(STATIC_LIB) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +pre-install-rb-default: + @$(NULLCMD) +$(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time: + $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR) + $(Q) $(TOUCH) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S + +.cc.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cc.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.mm.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.mm.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cxx.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cxx.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cpp.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cpp.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.c.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.c.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +.m.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.m.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +$(RUBYARCHDIR)/$(DLLIB): $(OBJS) Makefile $(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time + $(ECHO) linking shared-object io/$(DLLIB) + -$(Q)$(RM) $(@) + $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS) + +$(STATIC_LIB): $(OBJS) + -$(Q)$(RM) $(@) + $(ECHO) linking static-library $(@) + $(Q) $(AR) cru $@ $(OBJS) + -$(Q)ranlib $(@) 2> /dev/null || true + +### +$(OBJS): $(RUBY_EXTCONF_H) + +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/nonblock/depend b/jni/ruby/ext/io/nonblock/depend new file mode 100644 index 0000000..e786dc7 --- /dev/null +++ b/jni/ruby/ext/io/nonblock/depend @@ -0,0 +1,4 @@ +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/nonblock/extconf.h b/jni/ruby/ext/io/nonblock/extconf.h new file mode 100644 index 0000000..cda0cc8 --- /dev/null +++ b/jni/ruby/ext/io/nonblock/extconf.h @@ -0,0 +1,3 @@ +#ifndef EXTCONF_H +#define EXTCONF_H +#endif diff --git a/jni/ruby/ext/io/nonblock/extconf.rb b/jni/ruby/ext/io/nonblock/extconf.rb new file mode 100644 index 0000000..aecdc16 --- /dev/null +++ b/jni/ruby/ext/io/nonblock/extconf.rb @@ -0,0 +1,8 @@ +require 'mkmf' +target = "io/nonblock" + +hdr = %w"fcntl.h" +if have_macro("O_NONBLOCK", hdr) and + (have_macro("F_GETFL", hdr) or have_macro("F_SETFL", hdr)) + create_makefile(target) +end diff --git a/jni/ruby/ext/io/nonblock/nonblock.c b/jni/ruby/ext/io/nonblock/nonblock.c new file mode 100644 index 0000000..792b6f0 --- /dev/null +++ b/jni/ruby/ext/io/nonblock/nonblock.c @@ -0,0 +1,138 @@ +/********************************************************************** + + io/wait.c - + + $Author: usa $ + created at: Tue Jul 14 21:53:18 2009 + + All the files in this distribution are covered under the Ruby's + license (see the file COPYING). + +**********************************************************************/ + +#include "ruby.h" +#include "ruby/io.h" +#ifdef HAVE_UNISTD_H +#include +#endif +#include + +#ifdef F_GETFL +static int +io_nonblock_mode(int fd) +{ + int f = fcntl(fd, F_GETFL); + if (f == -1) rb_sys_fail(0); + return f; +} +#else +#define io_nonblock_mode(fd) ((void)(fd), 0) +#endif + +#ifdef F_GETFL +/* + * call-seq: + * io.nonblock? -> boolean + * + * Returns +true+ if an IO object is in non-blocking mode. + */ +static VALUE +rb_io_nonblock_p(VALUE io) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (io_nonblock_mode(fptr->fd) & O_NONBLOCK) + return Qtrue; + return Qfalse; +} +#else +#define rb_io_nonblock_p rb_f_notimplement +#endif + +#ifdef F_SETFL +static void +io_nonblock_set(int fd, int f, int nb) +{ + if (nb) { + if ((f & O_NONBLOCK) != 0) + return; + f |= O_NONBLOCK; + } + else { + if ((f & O_NONBLOCK) == 0) + return; + f &= ~O_NONBLOCK; + } + if (fcntl(fd, F_SETFL, f) == -1) + rb_sys_fail(0); +} + +/* + * call-seq: + * io.nonblock = boolean -> boolean + * + * Enables non-blocking mode on a stream when set to + * +true+, and blocking mode when set to +false+. + */ +static VALUE +rb_io_nonblock_set(VALUE io, VALUE nb) +{ + rb_io_t *fptr; + GetOpenFile(io, fptr); + if (RTEST(nb)) + rb_io_set_nonblock(fptr); + else + io_nonblock_set(fptr->fd, io_nonblock_mode(fptr->fd), RTEST(nb)); + return io; +} + +static VALUE +io_nonblock_restore(VALUE arg) +{ + int *restore = (int *)arg; + if (fcntl(restore[0], F_SETFL, restore[1]) == -1) + rb_sys_fail(0); + return Qnil; +} + +/* + * call-seq: + * io.nonblock {|io| } -> io + * io.nonblock(boolean) {|io| } -> io + * + * Yields +self+ in non-blocking mode. + * + * When +false+ is given as an argument, +self+ is yielded in blocking mode. + * The original mode is restored after the block is executed. + */ +static VALUE +rb_io_nonblock_block(int argc, VALUE *argv, VALUE io) +{ + int nb = 1; + rb_io_t *fptr; + int f, restore[2]; + + GetOpenFile(io, fptr); + if (argc > 0) { + VALUE v; + rb_scan_args(argc, argv, "01", &v); + nb = RTEST(v); + } + f = io_nonblock_mode(fptr->fd); + restore[0] = fptr->fd; + restore[1] = f; + io_nonblock_set(fptr->fd, f, nb); + return rb_ensure(rb_yield, io, io_nonblock_restore, (VALUE)restore); +} +#else +#define rb_io_nonblock_set rb_f_notimplement +#define rb_io_nonblock_block rb_f_notimplement +#endif + +void +Init_nonblock(void) +{ + rb_define_method(rb_cIO, "nonblock?", rb_io_nonblock_p, 0); + rb_define_method(rb_cIO, "nonblock=", rb_io_nonblock_set, 1); + rb_define_method(rb_cIO, "nonblock", rb_io_nonblock_block, -1); +} diff --git a/jni/ruby/ext/io/wait/Makefile b/jni/ruby/ext/io/wait/Makefile new file mode 100644 index 0000000..b1bf69a --- /dev/null +++ b/jni/ruby/ext/io/wait/Makefile @@ -0,0 +1,273 @@ + +SHELL = /bin/sh + +# V=0 quiet, V=1 verbose. other values don't work. +V = 0 +Q1 = $(V:1=) +Q = $(Q1:0=@) +ECHO1 = $(V:1=@:) +ECHO = $(ECHO1:0=@echo) +NULLCMD = : + +#### Start of system configuration section. #### +top_srcdir = $(topdir)/. +srcdir = $(top_srcdir)/ext/io/wait +topdir = ../../.. +hdrdir = $(top_srcdir)/include +arch_hdrdir = $(extout)/include/$(arch) +PATH_SEPARATOR = : +VPATH = $(srcdir):$(arch_hdrdir)/ruby:$(hdrdir)/ruby +RUBYLIB = +RUBYOPT = - +prefix = $(DESTDIR)/usr/local +rubysitearchprefix = $(rubylibprefix)/$(sitearch) +rubyarchprefix = $(rubylibprefix)/$(arch) +rubylibprefix = $(libdir)/$(RUBY_BASE_NAME) +exec_prefix = $(prefix) +vendorarchhdrdir = $(vendorhdrdir)/$(sitearch) +sitearchhdrdir = $(sitehdrdir)/$(sitearch) +rubyarchhdrdir = $(rubyhdrdir)/$(arch) +vendorhdrdir = $(rubyhdrdir)/vendor_ruby +sitehdrdir = $(rubyhdrdir)/site_ruby +rubyhdrdir = $(includedir)/$(RUBY_VERSION_NAME) +vendorarchdir = $(vendorlibdir)/$(sitearch) +vendorlibdir = $(vendordir)/$(ruby_version) +vendordir = $(rubylibprefix)/vendor_ruby +sitearchdir = $(sitelibdir)/$(sitearch) +sitelibdir = $(sitedir)/$(ruby_version) +sitedir = $(rubylibprefix)/site_ruby +rubyarchdir = $(rubylibdir)/$(arch) +rubylibdir = $(rubylibprefix)/$(ruby_version) +sitearchincludedir = $(includedir)/$(sitearch) +archincludedir = $(includedir)/$(arch) +sitearchlibdir = $(libdir)/$(sitearch) +archlibdir = $(libdir)/$(arch) +ridir = $(datarootdir)/$(RI_BASE_NAME) +mandir = $(datarootdir)/man +localedir = $(datarootdir)/locale +libdir = $(exec_prefix)/lib +psdir = $(docdir) +pdfdir = $(docdir) +dvidir = $(docdir) +htmldir = $(docdir) +infodir = $(datarootdir)/info +docdir = $(datarootdir)/doc/$(PACKAGE) +oldincludedir = $(DESTDIR)/usr/include +includedir = $(prefix)/include +localstatedir = $(prefix)/var +sharedstatedir = $(prefix)/com +sysconfdir = $(prefix)/etc +datadir = $(datarootdir) +datarootdir = $(prefix)/share +libexecdir = $(exec_prefix)/libexec +sbindir = $(exec_prefix)/sbin +bindir = $(exec_prefix)/bin +archdir = $(rubyarchdir) + + +CC = gcc +CXX = g++ +LIBRUBY = $(LIBRUBY_SO) +LIBRUBY_A = lib$(RUBY_SO_NAME)-static.a +LIBRUBYARG_SHARED = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME) +LIBRUBYARG_STATIC = -Wl,-R$(libdir) -L$(libdir) -l$(RUBY_SO_NAME)-static +empty = +OUTFLAG = -o $(empty) +COUTFLAG = -o $(empty) + +RUBY_EXTCONF_H = extconf.h +cflags = $(optflags) $(debugflags) $(warnflags) +optflags = -O3 -fno-fast-math +debugflags = -ggdb3 +warnflags = -Wall -Wextra -Wno-unused-parameter -Wno-parentheses -Wno-long-long -Wno-missing-field-initializers -Wunused-variable -Wpointer-arith -Wwrite-strings -Wdeclaration-after-statement -Wimplicit-function-declaration -Wdeprecated-declarations -Wno-packed-bitfield-compat +CCDLFLAGS = -fPIC +CFLAGS = $(CCDLFLAGS) $(cflags) -fPIC $(ARCH_FLAG) +INCFLAGS = -I. -I$(arch_hdrdir) -I$(hdrdir) -I$(srcdir) +DEFS = +CPPFLAGS = -DRUBY_EXTCONF_H=\"$(RUBY_EXTCONF_H)\" $(DEFS) $(cppflags) +CXXFLAGS = $(CCDLFLAGS) $(cxxflags) $(ARCH_FLAG) +ldflags = -L. -fstack-protector -rdynamic -Wl,-export-dynamic +dldflags = +ARCH_FLAG = +DLDFLAGS = $(ldflags) $(dldflags) $(ARCH_FLAG) +LDSHARED = $(CC) -shared +LDSHAREDXX = $(CXX) -shared +AR = ar +EXEEXT = + +RUBY_INSTALL_NAME = $(RUBY_BASE_NAME) +RUBY_SO_NAME = ruby +RUBYW_INSTALL_NAME = +RUBY_VERSION_NAME = $(RUBY_BASE_NAME)-$(ruby_version) +RUBYW_BASE_NAME = rubyw +RUBY_BASE_NAME = ruby + +arch = x86_64-linux +sitearch = $(arch) +ruby_version = 2.2.0 +ruby = $(topdir)/miniruby -I'$(topdir)' -I'$(top_srcdir)/lib' -I'$(extout)/$(arch)' -I'$(extout)/common' +RUBY = $(ruby) +ruby_headers = $(hdrdir)/ruby.h $(hdrdir)/ruby/ruby.h $(hdrdir)/ruby/defines.h $(hdrdir)/ruby/missing.h $(hdrdir)/ruby/intern.h $(hdrdir)/ruby/st.h $(hdrdir)/ruby/subst.h $(arch_hdrdir)/ruby/config.h $(RUBY_EXTCONF_H) + +RM = rm -f +RM_RF = $(RUBY) -run -e rm -- -rf +RMDIRS = rmdir --ignore-fail-on-non-empty -p +MAKEDIRS = /bin/mkdir -p +INSTALL = /usr/bin/install -c +INSTALL_PROG = $(INSTALL) -m 0755 +INSTALL_DATA = $(INSTALL) -m 644 +COPY = cp +TOUCH = exit > + +#### End of system configuration section. #### + +preload = + +libpath = . $(topdir) +LIBPATH = -L. -L$(topdir) +DEFFILE = + +CLEANFILES = mkmf.log +DISTCLEANFILES = +DISTCLEANDIRS = + +extout = $(topdir)/.ext +extout_prefix = $(extout)$(target_prefix)/ +target_prefix = /io +LOCAL_LIBS = +LIBS = $(LIBRUBYARG_SHARED) -lpthread -lgmp -ldl -lcrypt -lm -lc +ORIG_SRCS = wait.c +SRCS = $(ORIG_SRCS) +OBJS = wait.o +HDRS = $(srcdir)/extconf.h +TARGET = wait +TARGET_NAME = wait +TARGET_ENTRY = Init_$(TARGET_NAME) +DLLIB = $(TARGET).so +EXTSTATIC = +STATIC_LIB = $(TARGET).a + +TIMESTAMP_DIR = $(extout)/.timestamp +BINDIR = $(extout)/bin +RUBYCOMMONDIR = $(extout)/common +RUBYLIBDIR = $(RUBYCOMMONDIR)$(target_prefix) +RUBYARCHDIR = $(extout)/$(arch)$(target_prefix) +HDRDIR = $(extout)/include/ruby$(target_prefix) +ARCHHDRDIR = $(extout)/include/$(arch)/ruby$(target_prefix) + +TARGET_SO = $(RUBYARCHDIR)/$(DLLIB) +CLEANLIBS = $(RUBYARCHDIR)/$(TARGET).so +CLEANOBJS = *.o *.bak + +all: install +static: all +.PHONY: all install static install-so install-rb +.PHONY: clean clean-so clean-static clean-rb + +clean-static:: +clean-rb-default:: +clean-rb:: +clean-so:: +clean: clean-so clean-static clean-rb-default clean-rb + -$(Q)$(RM) $(CLEANLIBS) $(CLEANOBJS) $(CLEANFILES) .*.time + +distclean-rb-default:: +distclean-rb:: +distclean-so:: +distclean-static:: +distclean: clean distclean-so distclean-static distclean-rb-default distclean-rb + -$(Q)$(RM) Makefile $(RUBY_EXTCONF_H) conftest.* mkmf.log + -$(Q)$(RM) core ruby$(EXEEXT) *~ $(DISTCLEANFILES) + -$(Q)$(RMDIRS) $(DISTCLEANDIRS) 2> /dev/null || true + +realclean: distclean +install: install-so install-rb + +install-so: $(RUBYARCHDIR)/$(DLLIB) +clean-so:: + -$(Q)$(RM) $(RUBYARCHDIR)/$(DLLIB) + -$(Q)$(RMDIRS) $(RUBYARCHDIR) 2> /dev/null || true +clean-static:: + -$(Q)$(RM) $(STATIC_LIB) +install-rb: pre-install-rb install-rb-default +install-rb-default: pre-install-rb-default +pre-install-rb: Makefile +pre-install-rb-default: Makefile +pre-install-rb-default: + @$(NULLCMD) +$(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time: + $(Q) $(MAKEDIRS) $(@D) $(RUBYARCHDIR) + $(Q) $(TOUCH) $@ + +site-install: site-install-so site-install-rb +site-install-so: install-so +site-install-rb: install-rb + +.SUFFIXES: .c .m .cc .mm .cxx .cpp .o .S + +.cc.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cc.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.mm.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.mm.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cxx.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cxx.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.cpp.o: + $(ECHO) compiling $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -c $< + +.cpp.S: + $(ECHO) translating $(<) + $(Q) $(CXX) $(INCFLAGS) $(CPPFLAGS) $(CXXFLAGS) $(COUTFLAG)$@ -S $< + +.c.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.c.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +.m.o: + $(ECHO) compiling $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -c $< + +.m.S: + $(ECHO) translating $(<) + $(Q) $(CC) $(INCFLAGS) $(CPPFLAGS) $(CFLAGS) $(COUTFLAG)$@ -S $< + +$(RUBYARCHDIR)/$(DLLIB): $(OBJS) Makefile $(TIMESTAMP_DIR)/.RUBYARCHDIR.-.io.time + $(ECHO) linking shared-object io/$(DLLIB) + -$(Q)$(RM) $(@) + $(Q) $(LDSHARED) -o $@ $(OBJS) $(LIBPATH) $(DLDFLAGS) $(LOCAL_LIBS) $(LIBS) + +$(STATIC_LIB): $(OBJS) + -$(Q)$(RM) $(@) + $(ECHO) linking static-library $(@) + $(Q) $(AR) cru $@ $(OBJS) + -$(Q)ranlib $(@) 2> /dev/null || true + +### +$(OBJS): $(RUBY_EXTCONF_H) + +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/wait/depend b/jni/ruby/ext/io/wait/depend new file mode 100644 index 0000000..e786dc7 --- /dev/null +++ b/jni/ruby/ext/io/wait/depend @@ -0,0 +1,4 @@ +$(OBJS): $(HDRS) $(ruby_headers) \ + $(hdrdir)/ruby/io.h \ + $(hdrdir)/ruby/encoding.h \ + $(hdrdir)/ruby/oniguruma.h diff --git a/jni/ruby/ext/io/wait/extconf.h b/jni/ruby/ext/io/wait/extconf.h new file mode 100644 index 0000000..27a457f --- /dev/null +++ b/jni/ruby/ext/io/wait/extconf.h @@ -0,0 +1,5 @@ +#ifndef EXTCONF_H +#define EXTCONF_H +#define HAVE_SYS_IOCTL_H 1 +#define FIONREAD_HEADER +#endif diff --git a/jni/ruby/ext/io/wait/extconf.rb b/jni/ruby/ext/io/wait/extconf.rb new file mode 100644 index 0000000..eed3543 --- /dev/null +++ b/jni/ruby/ext/io/wait/extconf.rb @@ -0,0 +1,18 @@ +require 'mkmf' +target = "io/wait" + +unless macro_defined?("DOSISH", "#include ") + have_header(ioctl_h = "sys/ioctl.h") or ioctl_h = nil + fionread = %w[sys/ioctl.h sys/filio.h sys/socket.h].find do |h| + have_macro("FIONREAD", [h, ioctl_h].compact) + end + if fionread + $defs << "-DFIONREAD_HEADER=\"<#{fionread}>\"" + create_makefile(target) + end +else + if have_func("rb_w32_ioctlsocket", "ruby.h") + have_func("rb_w32_is_socket", "ruby.h") + create_makefile(target) + end +end diff --git a/jni/ruby/ext/io/wait/wait.c b/jni/ruby/ext/io/wait/wait.c new file mode 100644 index 0000000..8104397 --- /dev/null +++ b/jni/ruby/ext/io/wait/wait.c @@ -0,0 +1,187 @@ +/********************************************************************** + + io/wait.c - + + $Author: nagachika $ + 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 "ruby/io.h" + +#include +#if defined(HAVE_UNISTD_H) && (defined(__sun)) +#include +#endif +#if defined(HAVE_SYS_IOCTL_H) +#include +#endif +#if defined(FIONREAD_HEADER) +#include FIONREAD_HEADER +#endif + +#ifdef HAVE_RB_W32_IOCTLSOCKET +#define ioctl ioctlsocket +#define ioctl_arg u_long +#define ioctl_arg2num(i) ULONG2NUM(i) +#else +#define ioctl_arg int +#define ioctl_arg2num(i) INT2NUM(i) +#endif + +#ifdef HAVE_RB_W32_IS_SOCKET +#define FIONREAD_POSSIBLE_P(fd) rb_w32_is_socket(fd) +#else +#define FIONREAD_POSSIBLE_P(fd) ((void)(fd),Qtrue) +#endif + +static VALUE io_ready_p _((VALUE io)); +static VALUE io_wait_readable _((int argc, VALUE *argv, VALUE io)); +static VALUE io_wait_writable _((int argc, VALUE *argv, VALUE io)); +void Init_wait _((void)); + +/* + * call-seq: + * io.nread -> int + * + * Returns number of bytes that can be read without blocking. + * Returns zero if no information available. + */ + +static VALUE +io_nread(VALUE io) +{ + rb_io_t *fptr; + int len; + ioctl_arg n; + + GetOpenFile(io, fptr); + rb_io_check_readable(fptr); + len = rb_io_read_pending(fptr); + if (len > 0) return INT2FIX(len); + if (!FIONREAD_POSSIBLE_P(fptr->fd)) return INT2FIX(0); + if (ioctl(fptr->fd, FIONREAD, &n)) return INT2FIX(0); + if (n > 0) return ioctl_arg2num(n); + return INT2FIX(0); +} + +/* + * call-seq: + * io.ready? -> true, false or nil + * + * Returns true if input available without blocking, or false. + * Returns nil if no information available. + */ + +static VALUE +io_ready_p(VALUE io) +{ + rb_io_t *fptr; + ioctl_arg n; + + GetOpenFile(io, fptr); + rb_io_check_readable(fptr); + if (rb_io_read_pending(fptr)) return Qtrue; + if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qnil; + if (ioctl(fptr->fd, FIONREAD, &n)) return Qnil; + if (n > 0) return Qtrue; + return Qfalse; +} + +/* + * call-seq: + * io.wait -> IO, true, false or nil + * io.wait(timeout) -> IO, true, false or nil + * io.wait_readable -> IO, true, false or nil + * io.wait_readable(timeout) -> IO, true, false or nil + * + * Waits until input is available or times out and returns self or nil when + * EOF is reached. + */ + +static VALUE +io_wait_readable(int argc, VALUE *argv, VALUE io) +{ + rb_io_t *fptr; + int i; + ioctl_arg n; + VALUE timeout; + struct timeval timerec; + struct timeval *tv; + + GetOpenFile(io, fptr); + rb_io_check_readable(fptr); + rb_scan_args(argc, argv, "01", &timeout); + if (NIL_P(timeout)) { + tv = NULL; + } + else { + timerec = rb_time_interval(timeout); + tv = &timerec; + } + + if (rb_io_read_pending(fptr)) return Qtrue; + if (!FIONREAD_POSSIBLE_P(fptr->fd)) return Qfalse; + i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_IN, tv); + if (i < 0) + rb_sys_fail(0); + rb_io_check_closed(fptr); + if (ioctl(fptr->fd, FIONREAD, &n)) rb_sys_fail(0); + if (n > 0) return io; + return Qnil; +} + +/* + * call-seq: + * io.wait_writable -> IO + * io.wait_writable(timeout) -> IO or nil + * + * Waits until IO writable is available or times out and returns self or + * nil when EOF is reached. + */ +static VALUE +io_wait_writable(int argc, VALUE *argv, VALUE io) +{ + rb_io_t *fptr; + int i; + VALUE timeout; + struct timeval timerec; + struct timeval *tv; + + GetOpenFile(io, fptr); + rb_io_check_writable(fptr); + rb_scan_args(argc, argv, "01", &timeout); + if (NIL_P(timeout)) { + tv = NULL; + } + else { + timerec = rb_time_interval(timeout); + tv = &timerec; + } + + i = rb_wait_for_single_fd(fptr->fd, RB_WAITFD_OUT, tv); + if (i < 0) + rb_sys_fail(0); + rb_io_check_closed(fptr); + if (i & RB_WAITFD_OUT) + return io; + return Qnil; +} + +/* + * IO wait methods + */ + +void +Init_wait(void) +{ + rb_define_method(rb_cIO, "nread", io_nread, 0); + rb_define_method(rb_cIO, "ready?", io_ready_p, 0); + rb_define_method(rb_cIO, "wait", io_wait_readable, -1); + rb_define_method(rb_cIO, "wait_readable", io_wait_readable, -1); + rb_define_method(rb_cIO, "wait_writable", io_wait_writable, -1); +} -- cgit v1.2.3-70-g09d2