2010-05-08 00:05:39 -04:00
|
|
|
/* -*- c-file-style: "ruby" -*- */
|
|
|
|
/*
|
|
|
|
* console IO module
|
|
|
|
*/
|
|
|
|
#include "ruby.h"
|
|
|
|
#include "ruby/io.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined HAVE_TERMIOS_H
|
|
|
|
# include <termios.h>
|
|
|
|
typedef struct termios conmode;
|
2010-12-01 17:08:41 -05:00
|
|
|
|
|
|
|
static int
|
|
|
|
setattr(int fd, conmode *t)
|
|
|
|
{
|
|
|
|
while (tcsetattr(fd, TCSAFLUSH, t)) {
|
|
|
|
if (errno != EINTR) return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2010-05-08 00:05:39 -04:00
|
|
|
# define getattr(fd, t) (tcgetattr(fd, t) == 0)
|
|
|
|
#elif defined HAVE_TERMIO_H
|
|
|
|
# include <termio.h>
|
|
|
|
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 <sgtty.h>
|
|
|
|
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 <winioctl.h>
|
|
|
|
typedef DWORD conmode;
|
|
|
|
|
|
|
|
#define LAST_ERROR rb_w32_map_errno(GetLastError())
|
|
|
|
#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
|
|
|
|
|
2015-02-15 22:43:20 -05:00
|
|
|
static ID id_getc, id_console, id_close, id_min, id_time;
|
2010-05-08 00:05:39 -04:00
|
|
|
|
2015-02-15 22:43:18 -05:00
|
|
|
#ifndef HAVE_RB_F_SEND
|
|
|
|
static ID id___send__;
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
rb_f_send(int argc, VALUE *argv, VALUE recv)
|
|
|
|
{
|
|
|
|
VALUE sym = argv[0];
|
|
|
|
ID vid = rb_check_id(&sym);
|
|
|
|
if (vid) {
|
|
|
|
--argc;
|
|
|
|
++argv;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
vid = id___send__;
|
|
|
|
}
|
|
|
|
return rb_funcallv(recv, vid, argc, argv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 03:18:18 -05:00
|
|
|
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)) {
|
2015-02-15 22:43:20 -05:00
|
|
|
VALUE vmin = rb_hash_aref(vopts, ID2SYM(id_min));
|
|
|
|
VALUE vtime = rb_hash_aref(vopts, ID2SYM(id_time));
|
2013-02-04 23:27:57 -05:00
|
|
|
/* default values by `stty raw` */
|
|
|
|
opts->vmin = 1;
|
2013-02-04 01:10:12 -05:00
|
|
|
opts->vtime = 0;
|
2011-11-18 03:18:18 -05:00
|
|
|
if (!NIL_P(vmin)) {
|
|
|
|
opts->vmin = NUM2INT(vmin);
|
|
|
|
optp = opts;
|
|
|
|
}
|
|
|
|
if (!NIL_P(vtime)) {
|
2013-02-03 02:54:18 -05:00
|
|
|
VALUE v10 = INT2FIX(10);
|
2011-11-18 03:18:18 -05:00
|
|
|
vtime = rb_funcall3(vtime, '*', 1, &v10);
|
|
|
|
opts->vtime = NUM2INT(vtime);
|
|
|
|
optp = opts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return optp;
|
|
|
|
}
|
|
|
|
|
2010-05-08 00:05:39 -04:00
|
|
|
static void
|
2011-11-18 03:18:18 -05:00
|
|
|
set_rawmode(conmode *t, void *arg)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_CFMAKERAW
|
|
|
|
cfmakeraw(t);
|
2012-03-05 20:11:47 -05:00
|
|
|
t->c_lflag &= ~(ECHOE|ECHOK);
|
2011-11-18 02:12:14 -05:00
|
|
|
#elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
|
2010-05-08 00:05:39 -04:00
|
|
|
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
|
|
|
|
t->c_oflag &= ~OPOST;
|
2012-03-05 20:11:47 -05:00
|
|
|
t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
|
2010-05-08 00:05:39 -04:00
|
|
|
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
|
2011-11-18 03:18:18 -05:00
|
|
|
#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
|
2011-11-18 02:12:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-18 03:18:18 -05:00
|
|
|
set_cookedmode(conmode *t, void *arg)
|
2011-11-18 02:12:14 -05:00
|
|
|
{
|
|
|
|
#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;
|
2010-05-08 00:05:39 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-11-18 03:18:18 -05:00
|
|
|
set_noecho(conmode *t, void *arg)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
#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
|
2011-11-18 03:18:18 -05:00
|
|
|
set_echo(conmode *t, void *arg)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
#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
|
2012-03-05 20:11:47 -05:00
|
|
|
return (t->c_lflag & (ECHO | ECHONL)) != 0;
|
2010-05-08 00:05:39 -04:00
|
|
|
#elif defined HAVE_SGTTY_H
|
|
|
|
return (t->sg_flags & ECHO) != 0;
|
|
|
|
#elif defined _WIN32
|
|
|
|
return (*t & ENABLE_ECHO_INPUT) != 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-11-18 03:18:18 -05:00
|
|
|
set_ttymode(int fd, conmode *t, void (*setter)(conmode *, void *), void *arg)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
conmode r;
|
|
|
|
if (!getattr(fd, t)) return 0;
|
|
|
|
r = *t;
|
2011-11-18 03:18:18 -05:00
|
|
|
setter(&r, arg);
|
2010-05-08 00:05:39 -04:00
|
|
|
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;
|
2010-05-13 05:45:29 -04:00
|
|
|
if (!wio) return fptr->fd;
|
2010-05-08 00:05:39 -04:00
|
|
|
GetOpenFile(wio, ofptr);
|
|
|
|
return ofptr->fd;
|
|
|
|
}
|
|
|
|
#define GetWriteFD(fptr) get_write_fd(fptr)
|
|
|
|
|
|
|
|
#define FD_PER_IO 2
|
|
|
|
|
|
|
|
static VALUE
|
2011-11-18 03:18:18 -05:00
|
|
|
ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
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) {
|
2011-11-18 03:18:18 -05:00
|
|
|
if (set_ttymode(fd[0], t+0, setter, arg)) {
|
2010-05-08 00:05:39 -04:00
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = errno;
|
|
|
|
fd[0] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fd[1] = GetWriteFD(fptr);
|
|
|
|
if (fd[1] != -1 && fd[1] != fd[0]) {
|
2011-11-18 03:18:18 -05:00
|
|
|
if (set_ttymode(fd[1], t+1, setter, arg)) {
|
2010-05-08 00:05:39 -04:00
|
|
|
status = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
error = errno;
|
|
|
|
fd[1] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (status == 0) {
|
|
|
|
result = rb_protect(func, io, &status);
|
|
|
|
}
|
2010-05-09 11:35:43 -04:00
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) {
|
2010-05-08 00:05:39 -04:00
|
|
|
if (!setattr(fd[0], t+0)) {
|
|
|
|
error = errno;
|
|
|
|
status = -1;
|
|
|
|
}
|
|
|
|
}
|
2010-05-09 11:35:43 -04:00
|
|
|
if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) {
|
2010-05-08 00:05:39 -04:00
|
|
|
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:
|
2011-11-18 03:18:18 -05:00
|
|
|
* io.raw(min: nil, time: nil) {|io| }
|
2010-05-08 00:05:39 -04:00
|
|
|
*
|
|
|
|
* Yields +self+ within raw mode.
|
|
|
|
*
|
|
|
|
* STDIN.raw(&:gets)
|
|
|
|
*
|
2012-03-05 20:11:47 -05:00
|
|
|
* will read and return a line without echo back and line editing.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2011-11-18 03:18:18 -05:00
|
|
|
console_raw(int argc, VALUE *argv, VALUE io)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
2011-11-18 03:18:18 -05:00
|
|
|
rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
|
|
|
|
return ttymode(io, rb_yield, set_rawmode, optp);
|
2010-05-08 00:05:39 -04:00
|
|
|
}
|
|
|
|
|
2010-05-09 11:54:17 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-11-18 03:18:18 -05:00
|
|
|
* io.raw!(min: nil, time: nil)
|
2010-05-09 11:54:17 -04:00
|
|
|
*
|
|
|
|
* Enables raw mode.
|
2010-05-25 10:26:04 -04:00
|
|
|
*
|
|
|
|
* If the terminal mode needs to be back, use io.raw { ... }.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-09 11:54:17 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2011-11-18 03:18:18 -05:00
|
|
|
console_set_raw(int argc, VALUE *argv, VALUE io)
|
2010-05-09 11:54:17 -04:00
|
|
|
{
|
|
|
|
conmode t;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
2011-11-18 03:18:18 -05:00
|
|
|
rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
|
2010-05-09 11:54:17 -04:00
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = GetReadFD(fptr);
|
|
|
|
if (!getattr(fd, &t)) rb_sys_fail(0);
|
2011-11-18 03:18:18 -05:00
|
|
|
set_rawmode(&t, optp);
|
2010-05-09 11:54:17 -04:00
|
|
|
if (!setattr(fd, &t)) rb_sys_fail(0);
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2011-11-18 02:12:14 -05:00
|
|
|
/*
|
|
|
|
* 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.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2011-11-18 02:12:14 -05:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
console_cooked(VALUE io)
|
|
|
|
{
|
2011-11-18 03:18:18 -05:00
|
|
|
return ttymode(io, rb_yield, set_cookedmode, NULL);
|
2011-11-18 02:12:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.cooked!
|
|
|
|
*
|
|
|
|
* Enables cooked mode.
|
|
|
|
*
|
|
|
|
* If the terminal mode needs to be back, use io.cooked { ... }.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2011-11-18 02:12:14 -05:00
|
|
|
*/
|
|
|
|
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);
|
2011-11-18 03:18:18 -05:00
|
|
|
set_cookedmode(&t, NULL);
|
2011-11-18 02:12:14 -05:00
|
|
|
if (!setattr(fd, &t)) rb_sys_fail(0);
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2010-05-08 00:05:39 -04:00
|
|
|
static VALUE
|
|
|
|
getc_call(VALUE io)
|
|
|
|
{
|
|
|
|
return rb_funcall2(io, id_getc, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2011-11-18 03:18:18 -05:00
|
|
|
* io.getch(min: nil, time: nil) -> char
|
2010-05-08 00:05:39 -04:00
|
|
|
*
|
|
|
|
* Reads and returns a character in raw mode.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2011-11-18 03:18:18 -05:00
|
|
|
console_getch(int argc, VALUE *argv, VALUE io)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
2011-11-18 03:18:18 -05:00
|
|
|
rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
|
|
|
|
return ttymode(io, getc_call, set_rawmode, optp);
|
2010-05-08 00:05:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.noecho {|io| }
|
|
|
|
*
|
|
|
|
* Yields +self+ with disabling echo back.
|
|
|
|
*
|
|
|
|
* STDIN.noecho(&:gets)
|
|
|
|
*
|
|
|
|
* will read and return a line without echo back.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
console_noecho(VALUE io)
|
|
|
|
{
|
2011-11-18 03:18:18 -05:00
|
|
|
return ttymode(io, rb_yield, set_noecho, NULL);
|
2010-05-08 00:05:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.echo = flag
|
|
|
|
*
|
|
|
|
* Enables/disables echo back.
|
2012-03-05 20:11:47 -05:00
|
|
|
* On some platforms, all combinations of this flags and raw/cooked
|
|
|
|
* mode may not be valid.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
console_set_echo(VALUE io, VALUE f)
|
|
|
|
{
|
|
|
|
conmode t;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2010-05-13 04:56:11 -04:00
|
|
|
fd = GetReadFD(fptr);
|
2010-05-08 00:05:39 -04:00
|
|
|
if (!getattr(fd, &t)) rb_sys_fail(0);
|
|
|
|
if (RTEST(f))
|
2011-11-18 03:18:18 -05:00
|
|
|
set_echo(&t, NULL);
|
2010-05-08 00:05:39 -04:00
|
|
|
else
|
2011-11-18 03:18:18 -05:00
|
|
|
set_noecho(&t, NULL);
|
2010-05-08 00:05:39 -04:00
|
|
|
if (!setattr(fd, &t)) rb_sys_fail(0);
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.echo? -> true or false
|
|
|
|
*
|
|
|
|
* Returns +true+ if echo back is enabled.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
console_echo_p(VALUE io)
|
|
|
|
{
|
|
|
|
conmode t;
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2010-05-13 04:56:11 -04:00
|
|
|
fd = GetReadFD(fptr);
|
2010-05-08 00:05:39 -04:00
|
|
|
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)
|
2011-01-31 18:01:46 -05:00
|
|
|
#define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0)
|
2010-05-08 00:05:39 -04:00
|
|
|
#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)
|
2010-07-06 08:47:18 -04:00
|
|
|
#define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1)
|
2010-05-08 00:05:39 -04:00
|
|
|
#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.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
|
|
|
console_winsize(VALUE io)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
rb_console_size_t ws;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
2011-01-31 17:56:07 -05:00
|
|
|
fd = GetWriteFD(fptr);
|
2010-05-08 00:05:39 -04:00
|
|
|
if (!getwinsize(fd, &ws)) rb_sys_fail(0);
|
|
|
|
return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
|
|
|
|
}
|
2011-01-31 18:01:46 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.winsize = [rows, columns]
|
|
|
|
*
|
|
|
|
* Tries to set console size. The effect depends on the platform and
|
|
|
|
* the running environment.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2011-01-31 18:01:46 -05:00
|
|
|
*/
|
|
|
|
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;
|
2015-11-04 02:27:10 -05:00
|
|
|
const VALUE *sz;
|
2011-01-31 18:01:46 -05:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
size = rb_Array(size);
|
2015-11-04 02:27:10 -05:00
|
|
|
rb_check_arity(RARRAY_LENINT(size), 2, 4);
|
|
|
|
sz = RARRAY_CONST_PTR(size);
|
|
|
|
row = sz[0], col = sz[1], xpixel = sz[2], ypixel = sz[3];
|
2011-01-31 18:01:46 -05:00
|
|
|
fd = GetWriteFD(fptr);
|
2015-03-21 02:01:29 -04:00
|
|
|
#if defined TIOCSWINSZ
|
2011-01-31 18:01:46 -05:00
|
|
|
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
|
2015-03-21 02:01:29 -04:00
|
|
|
wh = (HANDLE)rb_w32_get_osfhandle(fd);
|
2015-11-06 19:25:50 -05:00
|
|
|
#define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
|
|
|
|
SET(row);
|
|
|
|
SET(col);
|
|
|
|
#undef SET
|
|
|
|
if (!NIL_P(xpixel)) (void)NUM2UINT(xpixel);
|
|
|
|
if (!NIL_P(ypixel)) (void)NUM2UINT(ypixel);
|
2015-03-21 02:01:29 -04:00
|
|
|
if (!GetConsoleScreenBufferInfo(wh, &ws)) {
|
|
|
|
rb_syserr_fail(LAST_ERROR, "GetConsoleScreenBufferInfo");
|
2011-01-31 18:01:46 -05:00
|
|
|
}
|
|
|
|
if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) ||
|
|
|
|
(ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) {
|
2015-03-21 02:01:29 -04:00
|
|
|
if (!SetConsoleScreenBufferSize(wh, ws.dwSize)) {
|
|
|
|
rb_syserr_fail(LAST_ERROR, "SetConsoleScreenBufferInfo");
|
2011-01-31 18:01:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.srWindow.Left = 0;
|
|
|
|
ws.srWindow.Top = 0;
|
|
|
|
ws.srWindow.Right = newcol;
|
|
|
|
ws.srWindow.Bottom = newrow;
|
2015-03-21 02:01:29 -04:00
|
|
|
if (!SetConsoleWindowInfo(wh, FALSE, &ws.srWindow)) {
|
|
|
|
rb_syserr_fail(LAST_ERROR, "SetConsoleWindowInfo");
|
2011-01-31 18:01:46 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return io;
|
|
|
|
}
|
2010-05-08 00:05:39 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.iflush
|
|
|
|
*
|
|
|
|
* Flushes input buffer in kernel.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
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
|
2013-03-16 04:05:45 -04:00
|
|
|
(void)fd;
|
2010-05-08 00:05:39 -04:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.oflush
|
|
|
|
*
|
|
|
|
* Flushes output buffer in kernel.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
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
|
2013-03-16 04:05:45 -04:00
|
|
|
(void)fd;
|
2010-05-08 00:05:39 -04:00
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.ioflush
|
|
|
|
*
|
|
|
|
* Flushes input and output buffers in kernel.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-03 14:11:25 -04:00
|
|
|
static VALUE
|
|
|
|
console_beep(VALUE io)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = GetWriteFD(fptr);
|
|
|
|
#ifdef _WIN32
|
|
|
|
(void)fd;
|
|
|
|
MessageBeep(0);
|
|
|
|
#else
|
|
|
|
if (write(fd, "\a", 1) < 0)
|
|
|
|
rb_sys_fail(0);
|
|
|
|
#endif
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
2015-05-03 14:11:40 -04:00
|
|
|
#if defined _WIN32
|
|
|
|
static VALUE
|
|
|
|
console_goto(VALUE io, VALUE x, VALUE y)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
COORD pos;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = GetWriteFD(fptr);
|
|
|
|
pos.X = NUM2UINT(x);
|
|
|
|
pos.Y = NUM2UINT(y);
|
|
|
|
if (!SetConsoleCursorPosition((HANDLE)rb_w32_get_osfhandle(fd), pos)) {
|
|
|
|
rb_syserr_fail(LAST_ERROR, 0);
|
|
|
|
}
|
|
|
|
return io;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
console_cursor_pos(VALUE io)
|
|
|
|
{
|
|
|
|
rb_io_t *fptr;
|
|
|
|
int fd;
|
|
|
|
rb_console_size_t ws;
|
|
|
|
|
|
|
|
GetOpenFile(io, fptr);
|
|
|
|
fd = GetWriteFD(fptr);
|
|
|
|
if (!GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), &ws)) {
|
|
|
|
rb_syserr_fail(LAST_ERROR, 0);
|
|
|
|
}
|
|
|
|
return rb_assoc_new(UINT2NUM(ws.dwCursorPosition.X), UINT2NUM(ws.dwCursorPosition.Y));
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
console_cursor_set(VALUE io, VALUE cpos)
|
|
|
|
{
|
|
|
|
cpos = rb_convert_type(cpos, T_ARRAY, "Array", "to_ary");
|
|
|
|
if (RARRAY_LEN(cpos) != 2) rb_raise(rb_eArgError, "expected 2D coordinate");
|
|
|
|
return console_goto(io, RARRAY_AREF(cpos, 0), RARRAY_AREF(cpos, 1));
|
|
|
|
}
|
2015-05-03 14:13:10 -04:00
|
|
|
|
2015-05-07 02:35:40 -04:00
|
|
|
#include "win32_vk.inc"
|
2015-05-03 14:13:10 -04:00
|
|
|
|
|
|
|
static VALUE
|
|
|
|
console_key_pressed_p(VALUE io, VALUE k)
|
|
|
|
{
|
|
|
|
int vk = -1;
|
|
|
|
|
|
|
|
if (FIXNUM_P(k)) {
|
|
|
|
vk = NUM2UINT(k);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
const struct vktable *t;
|
|
|
|
if (SYMBOL_P(k)) {
|
|
|
|
k = rb_sym2str(k);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
StringValueCStr(k);
|
|
|
|
}
|
|
|
|
t = console_win32_vk(RSTRING_PTR(k), RSTRING_LEN(k));
|
2015-07-21 06:52:19 -04:00
|
|
|
if (!t || (vk = (short)t->vk) == -1) {
|
|
|
|
rb_raise(rb_eArgError, "unknown virtual key code: %"PRIsVALUE, k);
|
|
|
|
}
|
2015-05-03 14:13:10 -04:00
|
|
|
}
|
|
|
|
return GetKeyState(vk) & 0x80 ? Qtrue : Qfalse;
|
|
|
|
}
|
2015-05-03 14:11:40 -04:00
|
|
|
#else
|
|
|
|
# define console_goto rb_f_notimplement
|
|
|
|
# define console_cursor_pos rb_f_notimplement
|
|
|
|
# define console_cursor_set rb_f_notimplement
|
2015-05-03 14:13:10 -04:00
|
|
|
# define console_key_pressed_p rb_f_notimplement
|
2015-05-03 14:11:40 -04:00
|
|
|
#endif
|
|
|
|
|
2010-05-08 00:05:39 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* IO.console -> #<File:/dev/tty>
|
2014-12-25 02:14:12 -05:00
|
|
|
* IO.console(sym, *args)
|
2010-05-08 00:05:39 -04:00
|
|
|
*
|
|
|
|
* Returns an File instance opened console.
|
2011-12-06 20:49:40 -05:00
|
|
|
*
|
2014-12-25 02:14:12 -05:00
|
|
|
* 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.
|
|
|
|
*
|
2011-12-06 20:49:40 -05:00
|
|
|
* You must require 'io/console' to use this method.
|
2010-05-08 00:05:39 -04:00
|
|
|
*/
|
|
|
|
static VALUE
|
2014-12-25 02:14:12 -05:00
|
|
|
console_dev(int argc, VALUE *argv, VALUE klass)
|
2010-05-08 00:05:39 -04:00
|
|
|
{
|
|
|
|
VALUE con = 0;
|
|
|
|
rb_io_t *fptr;
|
2014-12-25 02:14:12 -05:00
|
|
|
VALUE sym = 0;
|
2010-05-08 00:05:39 -04:00
|
|
|
|
2015-02-15 22:27:02 -05:00
|
|
|
rb_check_arity(argc, 0, UNLIMITED_ARGUMENTS);
|
2014-12-25 02:14:12 -05:00
|
|
|
if (argc) {
|
|
|
|
Check_Type(sym = argv[0], T_SYMBOL);
|
|
|
|
}
|
2010-05-08 00:05:39 -04:00
|
|
|
if (klass == rb_cIO) klass = rb_cFile;
|
|
|
|
if (rb_const_defined(klass, id_console)) {
|
|
|
|
con = rb_const_get(klass, id_console);
|
2014-12-25 02:14:12 -05:00
|
|
|
if (!RB_TYPE_P(con, T_FILE) ||
|
|
|
|
(!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1)) {
|
|
|
|
rb_const_remove(klass, id_console);
|
|
|
|
con = 0;
|
2010-05-08 00:05:39 -04:00
|
|
|
}
|
|
|
|
}
|
2014-12-25 02:14:12 -05:00
|
|
|
if (sym) {
|
2015-02-15 22:43:18 -05:00
|
|
|
if (sym == ID2SYM(id_close) && argc == 1) {
|
2014-12-25 02:14:12 -05:00
|
|
|
if (con) {
|
|
|
|
rb_io_close(con);
|
|
|
|
rb_const_remove(klass, id_console);
|
|
|
|
con = 0;
|
|
|
|
}
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!con) {
|
2010-05-08 00:05:39 -04:00
|
|
|
VALUE args[2];
|
|
|
|
#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
|
2011-06-13 10:06:35 -04:00
|
|
|
# define CONSOLE_DEVICE "/dev/tty"
|
2010-05-08 00:05:39 -04:00
|
|
|
#elif defined _WIN32
|
2011-06-13 10:06:35 -04:00
|
|
|
# define CONSOLE_DEVICE "con$"
|
|
|
|
# define CONSOLE_DEVICE_FOR_READING "conin$"
|
|
|
|
# define CONSOLE_DEVICE_FOR_WRITING "conout$"
|
2010-05-08 00:05:39 -04:00
|
|
|
#endif
|
2011-06-13 10:06:35 -04:00
|
|
|
#ifndef CONSOLE_DEVICE_FOR_READING
|
|
|
|
# define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
|
2010-05-08 00:05:39 -04:00
|
|
|
#endif
|
2011-06-13 10:06:35 -04:00
|
|
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
2010-05-08 00:05:39 -04:00
|
|
|
VALUE out;
|
|
|
|
rb_io_t *ofptr;
|
|
|
|
#endif
|
2011-07-29 11:49:36 -04:00
|
|
|
int fd;
|
2010-05-08 00:05:39 -04:00
|
|
|
|
2011-06-13 10:06:35 -04:00
|
|
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
2014-02-22 05:11:25 -05:00
|
|
|
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_RDWR, 0);
|
2011-06-13 10:28:53 -04:00
|
|
|
if (fd < 0) return Qnil;
|
2011-10-29 00:01:54 -04:00
|
|
|
rb_update_max_fd(fd);
|
2011-06-13 10:28:53 -04:00
|
|
|
args[1] = INT2FIX(O_WRONLY);
|
|
|
|
args[0] = INT2NUM(fd);
|
2010-05-08 00:05:39 -04:00
|
|
|
out = rb_class_new_instance(2, args, klass);
|
|
|
|
#endif
|
2011-10-29 00:01:54 -04:00
|
|
|
fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0);
|
2011-06-13 10:28:53 -04:00
|
|
|
if (fd < 0) {
|
2011-06-13 10:06:35 -04:00
|
|
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
2011-06-13 10:28:53 -04:00
|
|
|
rb_io_close(out);
|
|
|
|
#endif
|
|
|
|
return Qnil;
|
|
|
|
}
|
2011-10-29 00:01:54 -04:00
|
|
|
rb_update_max_fd(fd);
|
2011-06-13 10:28:53 -04:00
|
|
|
args[1] = INT2FIX(O_RDWR);
|
|
|
|
args[0] = INT2NUM(fd);
|
|
|
|
con = rb_class_new_instance(2, args, klass);
|
2010-05-08 00:05:39 -04:00
|
|
|
GetOpenFile(con, fptr);
|
2011-06-13 10:28:53 -04:00
|
|
|
fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
|
|
|
|
#ifdef CONSOLE_DEVICE_FOR_WRITING
|
2010-05-08 00:05:39 -04:00
|
|
|
GetOpenFile(out, ofptr);
|
2011-06-13 10:28:53 -04:00
|
|
|
ofptr->pathv = fptr->pathv;
|
2010-05-08 00:05:39 -04:00
|
|
|
fptr->tied_io_for_writing = out;
|
2011-06-13 10:51:25 -04:00
|
|
|
ofptr->mode |= FMODE_SYNC;
|
2010-05-08 00:05:39 -04:00
|
|
|
#endif
|
2011-06-13 10:33:36 -04:00
|
|
|
fptr->mode |= FMODE_SYNC;
|
2010-05-08 00:05:39 -04:00
|
|
|
rb_const_set(klass, id_console, con);
|
|
|
|
}
|
2014-12-25 02:14:12 -05:00
|
|
|
if (sym) {
|
2015-02-15 22:43:18 -05:00
|
|
|
return rb_f_send(argc, argv, con);
|
2014-12-25 02:14:12 -05:00
|
|
|
}
|
2010-05-08 00:05:39 -04:00
|
|
|
return con;
|
|
|
|
}
|
|
|
|
|
2013-05-24 02:54:25 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* io.getch(min: nil, time: nil) -> char
|
|
|
|
*
|
|
|
|
* See IO#getch.
|
|
|
|
*/
|
2012-01-25 13:24:09 -05:00
|
|
|
static VALUE
|
|
|
|
io_getch(int argc, VALUE *argv, VALUE io)
|
|
|
|
{
|
2015-02-15 22:43:20 -05:00
|
|
|
return rb_funcall2(io, id_getc, argc, argv);
|
2012-01-25 13:24:09 -05:00
|
|
|
}
|
|
|
|
|
2010-05-08 00:05:39 -04:00
|
|
|
/*
|
|
|
|
* IO console methods
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Init_console(void)
|
|
|
|
{
|
2015-02-15 22:43:18 -05:00
|
|
|
#undef rb_intern
|
2010-05-08 00:05:39 -04:00
|
|
|
id_getc = rb_intern("getc");
|
|
|
|
id_console = rb_intern("console");
|
2014-12-25 02:14:12 -05:00
|
|
|
id_close = rb_intern("close");
|
2015-02-15 22:43:20 -05:00
|
|
|
id_min = rb_intern("min");
|
|
|
|
id_time = rb_intern("time");
|
2015-02-15 22:43:18 -05:00
|
|
|
#ifndef HAVE_RB_F_SEND
|
|
|
|
id___send__ = rb_intern("__send__");
|
|
|
|
#endif
|
2010-05-08 00:05:39 -04:00
|
|
|
InitVM(console);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
InitVM_console(void)
|
|
|
|
{
|
2011-11-18 03:18:18 -05:00
|
|
|
rb_define_method(rb_cIO, "raw", console_raw, -1);
|
|
|
|
rb_define_method(rb_cIO, "raw!", console_set_raw, -1);
|
2011-11-18 02:12:14 -05:00
|
|
|
rb_define_method(rb_cIO, "cooked", console_cooked, 0);
|
|
|
|
rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0);
|
2011-11-18 03:18:18 -05:00
|
|
|
rb_define_method(rb_cIO, "getch", console_getch, -1);
|
2010-05-08 00:05:39 -04:00
|
|
|
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);
|
2011-01-31 18:01:46 -05:00
|
|
|
rb_define_method(rb_cIO, "winsize=", console_set_winsize, 1);
|
2010-05-08 00:05:39 -04:00
|
|
|
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);
|
2015-05-03 14:11:25 -04:00
|
|
|
rb_define_method(rb_cIO, "beep", console_beep, 0);
|
2015-05-03 14:11:40 -04:00
|
|
|
rb_define_method(rb_cIO, "goto", console_goto, 2);
|
|
|
|
rb_define_method(rb_cIO, "cursor", console_cursor_pos, 0);
|
|
|
|
rb_define_method(rb_cIO, "cursor=", console_cursor_set, 1);
|
2015-05-03 14:13:10 -04:00
|
|
|
rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
|
2014-12-25 02:14:12 -05:00
|
|
|
rb_define_singleton_method(rb_cIO, "console", console_dev, -1);
|
2012-01-25 13:24:09 -05:00
|
|
|
{
|
2013-05-24 02:54:17 -04:00
|
|
|
VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
|
2012-01-25 13:24:09 -05:00
|
|
|
rb_define_method(mReadable, "getch", io_getch, -1);
|
|
|
|
}
|
2010-05-08 00:05:39 -04:00
|
|
|
}
|