14 #ifdef HAVE_SYS_IOCTL_H 15 #include <sys/ioctl.h> 17 #ifndef RARRAY_CONST_PTR 18 # define RARRAY_CONST_PTR(ary) RARRAY_PTR(ary) 20 #ifndef HAVE_RB_FUNCALLV 21 # define rb_funcallv rb_funcall2 24 #if defined HAVE_TERMIOS_H 26 typedef struct termios conmode;
29 setattr(
int fd, conmode *t)
31 while (tcsetattr(fd, TCSAFLUSH, t)) {
32 if (
errno != EINTR)
return 0;
36 # define getattr(fd, t) (tcgetattr(fd, t) == 0) 37 #elif defined HAVE_TERMIO_H 39 typedef struct termio conmode;
40 # define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0) 41 # define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0) 42 #elif defined HAVE_SGTTY_H 44 typedef struct sgttyb conmode;
46 # define setattr(fd, t) (stty(fd, t) == 0) 48 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0) 51 # define getattr(fd, t) (gtty(fd, t) == 0) 53 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0) 57 typedef DWORD conmode;
59 #define LAST_ERROR rb_w32_map_errno(GetLastError()) 60 #define SET_LAST_ERROR (errno = LAST_ERROR, 0) 63 setattr(
int fd, conmode *t)
66 if (!x)
errno = LAST_ERROR;
71 getattr(
int fd, conmode *t)
74 if (!x)
errno = LAST_ERROR;
78 #ifndef SET_LAST_ERROR 79 #define SET_LAST_ERROR (0) 82 static ID id_getc, id_console, id_close, id_min, id_time;
87 #ifndef HAVE_RB_F_SEND 88 static ID id___send__;
106 #ifndef HAVE_RB_SYM2STR 107 # define rb_sym2str(sym) rb_id2str(SYM2ID(sym)) 142 set_rawmode(conmode *t,
void *arg)
144 #ifdef HAVE_CFMAKERAW 146 t->c_lflag &= ~(ECHOE|ECHOK);
147 #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 148 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
149 t->c_oflag &= ~OPOST;
150 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
151 t->c_cflag &= ~(CSIZE|PARENB);
153 #elif defined HAVE_SGTTY_H 154 t->sg_flags &= ~ECHO;
159 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 162 if (r->
vmin >= 0) t->c_cc[VMIN] = r->
vmin;
169 set_cookedmode(conmode *t,
void *arg)
171 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 172 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON);
174 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
175 #elif defined HAVE_SGTTY_H 179 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT;
184 set_noecho(conmode *t,
void *arg)
186 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 187 t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
188 #elif defined HAVE_SGTTY_H 189 t->sg_flags &= ~ECHO;
191 *t &= ~ENABLE_ECHO_INPUT;
196 set_echo(conmode *t,
void *arg)
198 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 199 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
200 #elif defined HAVE_SGTTY_H 203 *t |= ENABLE_ECHO_INPUT;
210 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 211 return (t->c_lflag & (ECHO | ECHONL)) != 0;
212 #elif defined HAVE_SGTTY_H 213 return (t->sg_flags & ECHO) != 0;
215 return (*t & ENABLE_ECHO_INPUT) != 0;
220 set_ttymode(
int fd, conmode *t,
void (*setter)(conmode *,
void *),
void *arg)
223 if (!getattr(fd, t))
return 0;
226 return setattr(fd, &r);
229 #define GetReadFD(fptr) ((fptr)->fd) 232 get_write_fd(
const rb_io_t *fptr)
236 if (!wio)
return fptr->
fd;
240 #define GetWriteFD(fptr) get_write_fd(fptr) 245 ttymode(
VALUE io,
VALUE (*func)(
VALUE),
void (*setter)(conmode *,
void *),
void *arg)
257 if (set_ttymode(fd[0], t+0, setter, arg)) {
266 if (fd[1] != -1 && fd[1] != fd[0]) {
267 if (set_ttymode(fd[1], t+1, setter, arg)) {
279 if (fd[0] != -1 && fd[0] ==
GetReadFD(fptr)) {
280 if (!setattr(fd[0], t+0)) {
285 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] ==
GetWriteFD(fptr)) {
286 if (!setattr(fd[1], t+1)) {
316 return ttymode(io,
rb_yield, set_rawmode, optp);
330 console_set_raw(
int argc,
VALUE *argv,
VALUE io)
340 set_rawmode(&t, optp);
358 console_cooked(
VALUE io)
374 console_set_cooked(
VALUE io)
383 set_cookedmode(&t,
NULL);
403 console_getch(
int argc,
VALUE *argv,
VALUE io)
406 return ttymode(io, getc_call, set_rawmode, optp);
422 console_noecho(
VALUE io)
450 set_noecho(&t,
NULL);
464 console_echo_p(
VALUE io)
476 #if defined TIOCGWINSZ 477 typedef struct winsize rb_console_size_t;
478 #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0) 479 #define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0) 480 #define winsize_row(buf) (buf)->ws_row 481 #define winsize_col(buf) (buf)->ws_col 483 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
484 #define getwinsize(fd, buf) ( \ 485 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \ 487 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1) 488 #define winsize_col(buf) (buf)->dwSize.X 491 #if defined TIOCGWINSZ || defined _WIN32 492 #define USE_CONSOLE_GETSIZE 1 495 #ifdef USE_CONSOLE_GETSIZE 505 console_winsize(
VALUE io)
509 rb_console_size_t ws;
530 rb_console_size_t ws;
536 VALUE row, col, xpixel, ypixel;
543 if ((sizelen =
RARRAY_LEN(size)) != 2 && sizelen != 4) {
545 "wrong number of arguments (given %ld, expected 2 or 4)",
549 row = sz[0], col = sz[1], xpixel = ypixel =
Qnil;
550 if (sizelen == 4) xpixel = sz[2], ypixel = sz[3];
552 #if defined TIOCSWINSZ 553 ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0;
554 #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) 563 #define SET(m) new##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) 569 if (!GetConsoleScreenBufferInfo(wh, &ws)) {
572 ws.dwSize.X = newcol;
573 ret = SetConsoleScreenBufferSize(wh, ws.dwSize);
574 ws.srWindow.Left = 0;
576 ws.srWindow.Right = newcol-1;
577 ws.srWindow.Bottom = newrow-1;
578 if (!SetConsoleWindowInfo(wh,
TRUE, &ws.srWindow)) {
582 if (!ret && !SetConsoleScreenBufferSize(wh, ws.dwSize)) {
586 if (!SetConsoleWindowInfo(wh,
TRUE, &ws.srWindow)) {
603 console_iflush(
VALUE io)
610 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 626 console_oflush(
VALUE io)
633 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 649 console_ioflush(
VALUE io)
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) {
672 console_beep(
VALUE io)
683 if (write(fd,
"\a", 1) < 0)
712 rb_console_size_t ws;
730 #include "win32_vk.inc" 741 const struct vktable *t;
751 if (!t || (vk = (
short)t->vk) == -1) {
758 # define console_goto rb_f_notimplement 759 # define console_cursor_pos rb_f_notimplement 760 # define console_cursor_set rb_f_notimplement 761 # define console_key_pressed_p rb_f_notimplement 778 console_dev(
int argc,
VALUE *argv,
VALUE klass)
798 if (sym ==
ID2SYM(id_close) && argc == 1) {
809 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H 810 # define CONSOLE_DEVICE "/dev/tty" 812 # define CONSOLE_DEVICE "con$" 813 # define CONSOLE_DEVICE_FOR_READING "conin$" 814 # define CONSOLE_DEVICE_FOR_WRITING "conout$" 816 #ifndef CONSOLE_DEVICE_FOR_READING 817 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE 819 #ifdef CONSOLE_DEVICE_FOR_WRITING 825 #ifdef CONSOLE_DEVICE_FOR_WRITING 827 if (fd < 0)
return Qnil;
835 #ifdef CONSOLE_DEVICE_FOR_WRITING 846 #ifdef CONSOLE_DEVICE_FOR_WRITING 856 return rb_f_send(argc, argv, con);
873 #if ENABLE_IO_GETPASS 881 getpass_call(
VALUE io)
889 if (argc > 0 && !
NIL_P(argv[0])) {
916 console_getpass(
int argc,
VALUE *argv,
VALUE io)
923 prompt(argc, argv, wio);
924 str =
rb_ensure(getpass_call, io, puts_call, wio);
925 return str_chomp(str);
940 prompt(argc, argv, io);
955 #if ENABLE_IO_GETPASS 962 #ifndef HAVE_RB_F_SEND 989 #if ENABLE_IO_GETPASS 996 #if ENABLE_IO_GETPASS #define SET(a, b, c, d, k, s, Ti)
ID rb_check_id(volatile VALUE *)
Returns ID for the given name if it is interned already, or 0.
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *pstate)
Protects a function call from potential global escapes from the function.
void rb_syserr_fail(int e, const char *mesg)
#define console_cursor_set
void rb_update_max_fd(int fd)
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
void rb_raise(VALUE exc, const char *fmt,...)
void rb_jump_tag(int tag)
Continues the exception caught by rb_protect() and rb_eval_string_protect().
SOCKET rb_w32_get_osfhandle(int)
RUBY_EXTERN VALUE rb_stdin
int rb_const_defined(VALUE, ID)
VALUE rb_io_write(VALUE, VALUE)
VALUE rb_ensure(VALUE(*b_proc)(ANYARGS), VALUE data1, VALUE(*e_proc)(ANYARGS), VALUE data2)
An equivalent to ensure clause.
#define GetOpenFile(obj, fp)
void InitVM_console(void)
#define RB_TYPE_P(obj, type)
VALUE rb_io_get_write_io(VALUE io)
#define CONSOLE_DEVICE_FOR_READING
#define console_key_pressed_p
VALUE rb_Array(VALUE)
Equivalent to Kernel#Array in Ruby.
void rb_sys_fail(const char *mesg)
VALUE rb_const_get(VALUE, ID)
#define RARRAY_CONST_PTR(a)
VALUE rb_obj_freeze(VALUE)
call-seq: obj.freeze -> obj
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)
VALUE rb_define_module_under(VALUE outer, const char *name)
#define StringValueCStr(v)
RUBY_EXTERN VALUE rb_default_rs
RUBY_EXTERN VALUE rb_stderr
#define UNLIMITED_ARGUMENTS
#define RARRAY_AREF(a, i)
VALUE rb_hash_aref(VALUE hash, VALUE key)
VALUE rb_convert_type(VALUE, int, const char *, const char *)
Converts an object into another type.
VALUE rb_class_new_instance(int, const VALUE *, VALUE)
Allocates and initializes an instance of klass.
void rb_check_safe_obj(VALUE)
#define console_cursor_pos
#define StringValuePtr(v)
VALUE rb_const_remove(VALUE, ID)
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)