22 #ifdef HAVE_SYS_IOCTL_H
23 #include <sys/ioctl.h>
26 #if defined HAVE_TERMIOS_H
28 typedef struct termios conmode;
31 setattr(
int fd, conmode *t)
33 while (tcsetattr(fd, TCSAFLUSH, t)) {
34 if (
errno != EINTR)
return 0;
38 # define getattr(fd, t) (tcgetattr(fd, t) == 0)
39 #elif defined HAVE_TERMIO_H
41 typedef struct termio conmode;
42 # define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0)
43 # define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0)
44 #elif defined HAVE_SGTTY_H
46 typedef struct sgttyb conmode;
48 # define setattr(fd, t) (stty(fd, t) == 0)
50 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0)
53 # define getattr(fd, t) (gtty(fd, t) == 0)
55 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0)
59 typedef DWORD conmode;
61 #ifdef HAVE_RB_W32_MAP_ERRNO
62 #define LAST_ERROR rb_w32_map_errno(GetLastError())
64 #define LAST_ERROR EBADF
66 #define SET_LAST_ERROR (errno = LAST_ERROR, 0)
69 setattr(
int fd, conmode *t)
72 if (!x)
errno = LAST_ERROR;
77 getattr(
int fd, conmode *t)
80 if (!x)
errno = LAST_ERROR;
84 #ifndef SET_LAST_ERROR
85 #define SET_LAST_ERROR (0)
89 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();}
128 #ifdef HAVE_CFMAKERAW
130 t->c_lflag &= ~(ECHOE|ECHOK);
131 #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
132 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
133 t->c_oflag &= ~OPOST;
134 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
135 t->c_cflag &= ~(CSIZE|PARENB);
137 #elif defined HAVE_SGTTY_H
138 t->sg_flags &= ~ECHO;
143 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
146 if (r->
vmin >= 0) t->c_cc[VMIN] = r->
vmin;
155 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
156 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON);
158 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
159 #elif defined HAVE_SGTTY_H
163 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT;
170 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
171 t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
172 #elif defined HAVE_SGTTY_H
173 t->sg_flags &= ~ECHO;
175 *t &= ~ENABLE_ECHO_INPUT;
182 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
183 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
184 #elif defined HAVE_SGTTY_H
187 *t |= ENABLE_ECHO_INPUT;
194 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
195 return (t->c_lflag & (ECHO | ECHONL)) != 0;
196 #elif defined HAVE_SGTTY_H
197 return (t->sg_flags & ECHO) != 0;
199 return (*t & ENABLE_ECHO_INPUT) != 0;
204 set_ttymode(
int fd, conmode *t,
void (*setter)(conmode *,
void *),
void *arg)
207 if (!getattr(fd, t))
return 0;
210 return setattr(fd, &r);
214 #define GetReadFD(fptr) fileno(GetReadFile(fptr))
216 #define GetReadFD(fptr) ((fptr)->fd)
220 #define GetWriteFD(fptr) fileno(GetWriteFile(fptr))
227 if (!wio)
return fptr->
fd;
231 #define GetWriteFD(fptr) get_write_fd(fptr)
258 if (fd[1] != -1 && fd[1] != fd[0]) {
271 if (fd[0] != -1 && fd[0] ==
GetReadFD(fptr)) {
272 if (!setattr(fd[0], t+0)) {
277 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] ==
GetWriteFD(fptr)) {
278 if (!setattr(fd[1], t+1)) {
469 #if defined TIOCGWINSZ
470 typedef struct winsize rb_console_size_t;
471 #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0)
472 #define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0)
473 #define winsize_row(buf) (buf)->ws_row
474 #define winsize_col(buf) (buf)->ws_col
476 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
477 #define getwinsize(fd, buf) ( \
478 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \
480 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1)
481 #define winsize_col(buf) (buf)->dwSize.X
484 #if defined TIOCGWINSZ || defined _WIN32
485 #define USE_CONSOLE_GETSIZE 1
488 #ifdef USE_CONSOLE_GETSIZE
498 console_winsize(
VALUE io)
502 rb_console_size_t ws;
523 rb_console_size_t ws;
528 VALUE row, col, xpixel, ypixel;
529 #if defined TIOCSWINSZ
536 &row, &col, &xpixel, &ypixel);
537 #if defined TIOCSWINSZ
539 ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
540 #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
554 if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) ||
555 (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) {
556 if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) ||
SET_LAST_ERROR)) {
560 ws.srWindow.Left = 0;
562 ws.srWindow.Right = newcol;
563 ws.srWindow.Bottom = newrow;
588 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
610 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
628 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
633 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
636 if (fd2 != -1 && fd1 != fd2) {
672 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
673 # define CONSOLE_DEVICE "/dev/tty"
675 # define CONSOLE_DEVICE "con$"
676 # define CONSOLE_DEVICE_FOR_READING "conin$"
677 # define CONSOLE_DEVICE_FOR_WRITING "conout$"
679 #ifndef CONSOLE_DEVICE_FOR_READING
680 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
682 #ifdef CONSOLE_DEVICE_FOR_WRITING
688 #ifdef CONSOLE_DEVICE_FOR_WRITING
690 if (fd < 0)
return Qnil;
698 #ifdef CONSOLE_DEVICE_FOR_WRITING
709 #ifdef CONSOLE_DEVICE_FOR_WRITING
711 # ifdef HAVE_RB_IO_GET_WRITE_IO
static int get_write_fd(const rb_io_t *fptr)
#define SET(a, b, c, d, k, s, Ti)
void rb_update_max_fd(int fd)
static rawmode_arg_t * rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
static void set_echo(conmode *t, void *arg)
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
static void set_cookedmode(conmode *t, void *arg)
static VALUE ttymode(VALUE io, VALUE(*func)(VALUE), void(*setter)(conmode *, void *), void *arg)
static VALUE console_getch(int argc, VALUE *argv, VALUE io)
SOCKET rb_w32_get_osfhandle(int)
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
static int echo_p(conmode *t)
int rb_const_defined(VALUE, ID)
#define GetOpenFile(obj, fp)
void InitVM_console(void)
#define RB_TYPE_P(obj, type)
static VALUE console_ioflush(VALUE io)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
#define CONSOLE_DEVICE_FOR_READING
static VALUE console_echo_p(VALUE io)
static VALUE console_set_echo(VALUE io, VALUE f)
static VALUE console_set_cooked(VALUE io)
VALUE rb_const_get(VALUE, ID)
SSL_METHOD *(* func)(void)
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
static VALUE getc_call(VALUE io)
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
VALUE rb_assoc_new(VALUE car, VALUE cdr)
VALUE tied_io_for_writing
void rb_const_set(VALUE, ID, VALUE)
static int set_ttymode(int fd, conmode *t, void(*setter)(conmode *, void *), void *arg)
static VALUE console_cooked(VALUE io)
static VALUE console_noecho(VALUE io)
void rb_sys_fail(const char *mesg)
void rb_jump_tag(int tag)
VALUE rb_funcall3(VALUE, ID, int, const VALUE *)
Calls a method.
static VALUE console_raw(int argc, VALUE *argv, VALUE io)
VALUE rb_define_module_under(VALUE outer, const char *name)
VALUE rb_hash_aref(VALUE hash, VALUE key)
VALUE rb_mod_remove_const(VALUE, VALUE)
static VALUE console_iflush(VALUE io)
static VALUE console_set_raw(int argc, VALUE *argv, VALUE io)
VALUE rb_obj_freeze(VALUE)
static VALUE console_dev(VALUE klass)
static VALUE io_getch(int argc, VALUE *argv, VALUE io)
static VALUE console_oflush(VALUE io)
static void set_rawmode(conmode *t, void *arg)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
VALUE rb_str_new2(const char *)
static void set_noecho(conmode *t, void *arg)