24 #ifdef HAVE_SYS_IOCTL_H
25 #include <sys/ioctl.h>
29 #define RB_TYPE_P(obj, type) (TYPE(obj) == type)
32 #if defined HAVE_TERMIOS_H
34 typedef struct termios conmode;
37 setattr(
int fd, conmode *
t)
39 while (tcsetattr(fd, TCSAFLUSH, t)) {
40 if (
errno != EINTR)
return 0;
44 # define getattr(fd, t) (tcgetattr(fd, t) == 0)
45 #elif defined HAVE_TERMIO_H
47 typedef struct termio conmode;
48 # define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0)
49 # define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0)
50 #elif defined HAVE_SGTTY_H
52 typedef struct sgttyb conmode;
54 # define setattr(fd, t) (stty(fd, t) == 0)
56 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0)
59 # define getattr(fd, t) (gtty(fd, t) == 0)
61 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0)
65 typedef DWORD conmode;
67 #ifdef HAVE_RB_W32_MAP_ERRNO
68 #define LAST_ERROR rb_w32_map_errno(GetLastError())
70 #define LAST_ERROR EBADF
72 #define SET_LAST_ERROR (errno = LAST_ERROR, 0)
75 setattr(
int fd, conmode *t)
78 if (!x)
errno = LAST_ERROR;
83 getattr(
int fd, conmode *t)
86 if (!x)
errno = LAST_ERROR;
90 #ifndef SET_LAST_ERROR
91 #define SET_LAST_ERROR (0)
95 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();}
110 #ifdef HAVE_RB_SCAN_ARGS_OPTIONAL_HASH
115 vopts = argv[--
argc];
117 # ifdef HAVE_RB_CHECK_HASH_TYPE
150 #ifdef HAVE_CFMAKERAW
152 t->c_lflag &= ~(ECHOE|ECHOK);
153 #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
154 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
155 t->c_oflag &= ~OPOST;
156 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
157 t->c_cflag &= ~(CSIZE|PARENB);
159 #elif defined HAVE_SGTTY_H
160 t->sg_flags &= ~ECHO;
165 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
168 if (r->
vmin >= 0) t->c_cc[VMIN] = r->
vmin;
177 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
178 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON);
180 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
181 #elif defined HAVE_SGTTY_H
185 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT;
192 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
193 t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
194 #elif defined HAVE_SGTTY_H
195 t->sg_flags &= ~ECHO;
197 *t &= ~ENABLE_ECHO_INPUT;
204 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
205 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
206 #elif defined HAVE_SGTTY_H
209 *t |= ENABLE_ECHO_INPUT;
216 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
217 return (t->c_lflag & (ECHO | ECHONL)) != 0;
218 #elif defined HAVE_SGTTY_H
219 return (t->sg_flags & ECHO) != 0;
221 return (*t & ENABLE_ECHO_INPUT) != 0;
229 if (!getattr(fd, t))
return 0;
232 return setattr(fd, &r);
236 #define GetReadFD(fptr) fileno(GetReadFile(fptr))
238 #define GetReadFD(fptr) ((fptr)->fd)
242 #define GetWriteFD(fptr) fileno(GetWriteFile(fptr))
249 if (!wio)
return fptr->
fd;
253 #define GetWriteFD(fptr) get_write_fd(fptr)
280 if (fd[1] != -1 && fd[1] != fd[0]) {
293 if (fd[0] != -1 && fd[0] ==
GetReadFD(fptr)) {
294 if (!setattr(fd[0], t+0)) {
299 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] ==
GetWriteFD(fptr)) {
300 if (!setattr(fd[1], t+1)) {
491 #if defined TIOCGWINSZ
492 typedef struct winsize rb_console_size_t;
493 #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0)
494 #define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0)
495 #define winsize_row(buf) (buf)->ws_row
496 #define winsize_col(buf) (buf)->ws_col
498 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
499 #define getwinsize(fd, buf) ( \
500 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \
502 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1)
503 #define winsize_col(buf) (buf)->dwSize.X
506 #if defined TIOCGWINSZ || defined _WIN32
507 #define USE_CONSOLE_GETSIZE 1
510 #ifdef USE_CONSOLE_GETSIZE
520 console_winsize(
VALUE io)
524 rb_console_size_t ws;
545 rb_console_size_t ws;
550 VALUE row, col, xpixel, ypixel;
551 #if defined TIOCSWINSZ
558 &row, &col, &xpixel, &ypixel);
559 #if defined TIOCSWINSZ
561 ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
562 #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m)
576 if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) ||
577 (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) {
578 if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) ||
SET_LAST_ERROR)) {
582 ws.srWindow.Left = 0;
584 ws.srWindow.Right = newcol;
585 ws.srWindow.Bottom = newrow;
610 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
633 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
652 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
657 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
660 if (fd2 != -1 && fd1 != fd2) {
696 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
697 # define CONSOLE_DEVICE "/dev/tty"
699 # define CONSOLE_DEVICE "con$"
700 # define CONSOLE_DEVICE_FOR_READING "conin$"
701 # define CONSOLE_DEVICE_FOR_WRITING "conout$"
703 #ifndef CONSOLE_DEVICE_FOR_READING
704 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
706 #ifdef CONSOLE_DEVICE_FOR_WRITING
712 #ifdef CONSOLE_DEVICE_FOR_WRITING
714 if (fd < 0)
return Qnil;
722 #ifdef CONSOLE_DEVICE_FOR_WRITING
732 #ifdef HAVE_RUBY_IO_H
737 #ifdef CONSOLE_DEVICE_FOR_WRITING
739 # ifdef HAVE_RB_IO_GET_WRITE_IO
#define RB_TYPE_P(obj, type)
static int get_write_fd(const rb_io_t *fptr)
#define SET(a, b, c, d, k, s, Ti)
rb_funcall2(argv[0], id_yield, argc-1, argv+1)
VALUE rb_class_new_instance(int, VALUE *, VALUE)
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)
static void set_cookedmode(conmode *t, void *arg)
SSL_METHOD *(* func)(void)
VALUE rb_const_get(VALUE, ID)
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_obj_freeze(VALUE)
void rb_update_max_fd(int fd)
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
static int echo_p(conmode *t)
#define GetOpenFile(obj, fp)
void InitVM_console(void)
static VALUE console_ioflush(VALUE io)
#define CONSOLE_DEVICE_FOR_READING
char * ruby_strdup(const char *)
static VALUE console_echo_p(VALUE io)
VALUE rb_check_hash_type(VALUE)
static VALUE console_set_echo(VALUE io, VALUE f)
static VALUE console_set_cooked(VALUE io)
static VALUE getc_call(VALUE io)
int rb_const_defined(VALUE, ID)
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)
static VALUE console_raw(int argc, VALUE *argv, VALUE io)
VALUE rb_define_module_under(VALUE outer, const char *name)
RUBY_EXTERN VALUE rb_cFile
VALUE rb_const_remove(VALUE, ID)
static VALUE console_iflush(VALUE io)
static VALUE console_set_raw(int argc, VALUE *argv, VALUE io)
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
VALUE rb_hash_aref(VALUE, 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)
static void set_noecho(conmode *t, void *arg)