Ruby 1.9.3p327(2012-11-10revision37606)
|
00001 /* -*- c-file-style: "ruby" -*- */ 00002 /* 00003 * console IO module 00004 */ 00005 #include "ruby.h" 00006 #ifdef HAVE_RUBY_IO_H 00007 #include "ruby/io.h" 00008 #else 00009 #include "rubyio.h" 00010 #endif 00011 00012 #ifndef HAVE_RB_IO_T 00013 typedef OpenFile rb_io_t; 00014 #endif 00015 00016 #ifdef HAVE_UNISTD_H 00017 #include <unistd.h> 00018 #endif 00019 #ifdef HAVE_FCNTL_H 00020 #include <fcntl.h> 00021 #endif 00022 #ifdef HAVE_SYS_IOCTL_H 00023 #include <sys/ioctl.h> 00024 #endif 00025 00026 #if defined HAVE_TERMIOS_H 00027 # include <termios.h> 00028 typedef struct termios conmode; 00029 00030 static int 00031 setattr(int fd, conmode *t) 00032 { 00033 while (tcsetattr(fd, TCSAFLUSH, t)) { 00034 if (errno != EINTR) return 0; 00035 } 00036 return 1; 00037 } 00038 # define getattr(fd, t) (tcgetattr(fd, t) == 0) 00039 #elif defined HAVE_TERMIO_H 00040 # include <termio.h> 00041 typedef struct termio conmode; 00042 # define setattr(fd, t) (ioctl(fd, TCSETAF, t) == 0) 00043 # define getattr(fd, t) (ioctl(fd, TCGETA, t) == 0) 00044 #elif defined HAVE_SGTTY_H 00045 # include <sgtty.h> 00046 typedef struct sgttyb conmode; 00047 # ifdef HAVE_STTY 00048 # define setattr(fd, t) (stty(fd, t) == 0) 00049 # else 00050 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0) 00051 # endif 00052 # ifdef HAVE_GTTY 00053 # define getattr(fd, t) (gtty(fd, t) == 0) 00054 # else 00055 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0) 00056 # endif 00057 #elif defined _WIN32 00058 #include <winioctl.h> 00059 typedef DWORD conmode; 00060 00061 #ifdef HAVE_RB_W32_MAP_ERRNO 00062 #define LAST_ERROR rb_w32_map_errno(GetLastError()) 00063 #else 00064 #define LAST_ERROR EBADF 00065 #endif 00066 #define SET_LAST_ERROR (errno = LAST_ERROR, 0) 00067 00068 static int 00069 setattr(int fd, conmode *t) 00070 { 00071 int x = SetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), *t); 00072 if (!x) errno = LAST_ERROR; 00073 return x; 00074 } 00075 00076 static int 00077 getattr(int fd, conmode *t) 00078 { 00079 int x = GetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), t); 00080 if (!x) errno = LAST_ERROR; 00081 return x; 00082 } 00083 #endif 00084 #ifndef SET_LAST_ERROR 00085 #define SET_LAST_ERROR (0) 00086 #endif 00087 00088 #ifndef InitVM 00089 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();} 00090 #endif 00091 00092 static ID id_getc, id_console; 00093 00094 typedef struct { 00095 int vmin; 00096 int vtime; 00097 } rawmode_arg_t; 00098 00099 static rawmode_arg_t * 00100 rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts) 00101 { 00102 rawmode_arg_t *optp = NULL; 00103 VALUE vopts; 00104 rb_scan_args(argc, argv, "0:", &vopts); 00105 if (!NIL_P(vopts)) { 00106 VALUE vmin = rb_hash_aref(vopts, ID2SYM(rb_intern("min"))); 00107 VALUE vtime = rb_hash_aref(vopts, ID2SYM(rb_intern("time"))); 00108 VALUE v10 = INT2FIX(10); 00109 if (!NIL_P(vmin)) { 00110 vmin = rb_funcall3(vmin, '*', 1, &v10); 00111 opts->vmin = NUM2INT(vmin); 00112 optp = opts; 00113 } 00114 if (!NIL_P(vtime)) { 00115 vtime = rb_funcall3(vtime, '*', 1, &v10); 00116 opts->vtime = NUM2INT(vtime); 00117 optp = opts; 00118 } 00119 } 00120 return optp; 00121 } 00122 00123 static void 00124 set_rawmode(conmode *t, void *arg) 00125 { 00126 #ifdef HAVE_CFMAKERAW 00127 cfmakeraw(t); 00128 t->c_lflag &= ~(ECHOE|ECHOK); 00129 #elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00130 t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); 00131 t->c_oflag &= ~OPOST; 00132 t->c_lflag &= ~(ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN); 00133 t->c_cflag &= ~(CSIZE|PARENB); 00134 t->c_cflag |= CS8; 00135 #elif defined HAVE_SGTTY_H 00136 t->sg_flags &= ~ECHO; 00137 t->sg_flags |= RAW; 00138 #elif defined _WIN32 00139 *t = 0; 00140 #endif 00141 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00142 if (arg) { 00143 const rawmode_arg_t *r = arg; 00144 if (r->vmin >= 0) t->c_cc[VMIN] = r->vmin; 00145 if (r->vtime >= 0) t->c_cc[VTIME] = r->vtime; 00146 } 00147 #endif 00148 } 00149 00150 static void 00151 set_cookedmode(conmode *t, void *arg) 00152 { 00153 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00154 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON); 00155 t->c_oflag |= OPOST; 00156 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN); 00157 #elif defined HAVE_SGTTY_H 00158 t->sg_flags |= ECHO; 00159 t->sg_flags &= ~RAW; 00160 #elif defined _WIN32 00161 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT; 00162 #endif 00163 } 00164 00165 static void 00166 set_noecho(conmode *t, void *arg) 00167 { 00168 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00169 t->c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL); 00170 #elif defined HAVE_SGTTY_H 00171 t->sg_flags &= ~ECHO; 00172 #elif defined _WIN32 00173 *t &= ~ENABLE_ECHO_INPUT; 00174 #endif 00175 } 00176 00177 static void 00178 set_echo(conmode *t, void *arg) 00179 { 00180 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00181 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL); 00182 #elif defined HAVE_SGTTY_H 00183 t->sg_flags |= ECHO; 00184 #elif defined _WIN32 00185 *t |= ENABLE_ECHO_INPUT; 00186 #endif 00187 } 00188 00189 static int 00190 echo_p(conmode *t) 00191 { 00192 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00193 return (t->c_lflag & (ECHO | ECHONL)) != 0; 00194 #elif defined HAVE_SGTTY_H 00195 return (t->sg_flags & ECHO) != 0; 00196 #elif defined _WIN32 00197 return (*t & ENABLE_ECHO_INPUT) != 0; 00198 #endif 00199 } 00200 00201 static int 00202 set_ttymode(int fd, conmode *t, void (*setter)(conmode *, void *), void *arg) 00203 { 00204 conmode r; 00205 if (!getattr(fd, t)) return 0; 00206 r = *t; 00207 setter(&r, arg); 00208 return setattr(fd, &r); 00209 } 00210 00211 #ifdef GetReadFile 00212 #define GetReadFD(fptr) fileno(GetReadFile(fptr)) 00213 #else 00214 #define GetReadFD(fptr) ((fptr)->fd) 00215 #endif 00216 00217 #ifdef GetWriteFile 00218 #define GetWriteFD(fptr) fileno(GetWriteFile(fptr)) 00219 #else 00220 static inline int 00221 get_write_fd(const rb_io_t *fptr) 00222 { 00223 VALUE wio = fptr->tied_io_for_writing; 00224 rb_io_t *ofptr; 00225 if (!wio) return fptr->fd; 00226 GetOpenFile(wio, ofptr); 00227 return ofptr->fd; 00228 } 00229 #define GetWriteFD(fptr) get_write_fd(fptr) 00230 #endif 00231 00232 #define FD_PER_IO 2 00233 00234 static VALUE 00235 ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg) 00236 { 00237 rb_io_t *fptr; 00238 int status = -1; 00239 int error = 0; 00240 int fd[FD_PER_IO]; 00241 conmode t[FD_PER_IO]; 00242 VALUE result = Qnil; 00243 00244 GetOpenFile(io, fptr); 00245 fd[0] = GetReadFD(fptr); 00246 if (fd[0] != -1) { 00247 if (set_ttymode(fd[0], t+0, setter, arg)) { 00248 status = 0; 00249 } 00250 else { 00251 error = errno; 00252 fd[0] = -1; 00253 } 00254 } 00255 fd[1] = GetWriteFD(fptr); 00256 if (fd[1] != -1 && fd[1] != fd[0]) { 00257 if (set_ttymode(fd[1], t+1, setter, arg)) { 00258 status = 0; 00259 } 00260 else { 00261 error = errno; 00262 fd[1] = -1; 00263 } 00264 } 00265 if (status == 0) { 00266 result = rb_protect(func, io, &status); 00267 } 00268 GetOpenFile(io, fptr); 00269 if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) { 00270 if (!setattr(fd[0], t+0)) { 00271 error = errno; 00272 status = -1; 00273 } 00274 } 00275 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) { 00276 if (!setattr(fd[1], t+1)) { 00277 error = errno; 00278 status = -1; 00279 } 00280 } 00281 if (status) { 00282 if (status == -1) { 00283 errno = error; 00284 rb_sys_fail(0); 00285 } 00286 rb_jump_tag(status); 00287 } 00288 return result; 00289 } 00290 00291 /* 00292 * call-seq: 00293 * io.raw(min: nil, time: nil) {|io| } 00294 * 00295 * Yields +self+ within raw mode. 00296 * 00297 * STDIN.raw(&:gets) 00298 * 00299 * will read and return a line without echo back and line editing. 00300 * 00301 * You must require 'io/console' to use this method. 00302 */ 00303 static VALUE 00304 console_raw(int argc, VALUE *argv, VALUE io) 00305 { 00306 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); 00307 return ttymode(io, rb_yield, set_rawmode, optp); 00308 } 00309 00310 /* 00311 * call-seq: 00312 * io.raw!(min: nil, time: nil) 00313 * 00314 * Enables raw mode. 00315 * 00316 * If the terminal mode needs to be back, use io.raw { ... }. 00317 * 00318 * You must require 'io/console' to use this method. 00319 */ 00320 static VALUE 00321 console_set_raw(int argc, VALUE *argv, VALUE io) 00322 { 00323 conmode t; 00324 rb_io_t *fptr; 00325 int fd; 00326 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); 00327 00328 GetOpenFile(io, fptr); 00329 fd = GetReadFD(fptr); 00330 if (!getattr(fd, &t)) rb_sys_fail(0); 00331 set_rawmode(&t, optp); 00332 if (!setattr(fd, &t)) rb_sys_fail(0); 00333 return io; 00334 } 00335 00336 /* 00337 * call-seq: 00338 * io.cooked {|io| } 00339 * 00340 * Yields +self+ within cooked mode. 00341 * 00342 * STDIN.cooked(&:gets) 00343 * 00344 * will read and return a line with echo back and line editing. 00345 * 00346 * You must require 'io/console' to use this method. 00347 */ 00348 static VALUE 00349 console_cooked(VALUE io) 00350 { 00351 return ttymode(io, rb_yield, set_cookedmode, NULL); 00352 } 00353 00354 /* 00355 * call-seq: 00356 * io.cooked! 00357 * 00358 * Enables cooked mode. 00359 * 00360 * If the terminal mode needs to be back, use io.cooked { ... }. 00361 * 00362 * You must require 'io/console' to use this method. 00363 */ 00364 static VALUE 00365 console_set_cooked(VALUE io) 00366 { 00367 conmode t; 00368 rb_io_t *fptr; 00369 int fd; 00370 00371 GetOpenFile(io, fptr); 00372 fd = GetReadFD(fptr); 00373 if (!getattr(fd, &t)) rb_sys_fail(0); 00374 set_cookedmode(&t, NULL); 00375 if (!setattr(fd, &t)) rb_sys_fail(0); 00376 return io; 00377 } 00378 00379 static VALUE 00380 getc_call(VALUE io) 00381 { 00382 return rb_funcall2(io, id_getc, 0, 0); 00383 } 00384 00385 /* 00386 * call-seq: 00387 * io.getch(min: nil, time: nil) -> char 00388 * 00389 * Reads and returns a character in raw mode. 00390 * 00391 * You must require 'io/console' to use this method. 00392 */ 00393 static VALUE 00394 console_getch(int argc, VALUE *argv, VALUE io) 00395 { 00396 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts); 00397 return ttymode(io, getc_call, set_rawmode, optp); 00398 } 00399 00400 /* 00401 * call-seq: 00402 * io.noecho {|io| } 00403 * 00404 * Yields +self+ with disabling echo back. 00405 * 00406 * STDIN.noecho(&:gets) 00407 * 00408 * will read and return a line without echo back. 00409 * 00410 * You must require 'io/console' to use this method. 00411 */ 00412 static VALUE 00413 console_noecho(VALUE io) 00414 { 00415 return ttymode(io, rb_yield, set_noecho, NULL); 00416 } 00417 00418 /* 00419 * call-seq: 00420 * io.echo = flag 00421 * 00422 * Enables/disables echo back. 00423 * On some platforms, all combinations of this flags and raw/cooked 00424 * mode may not be valid. 00425 * 00426 * You must require 'io/console' to use this method. 00427 */ 00428 static VALUE 00429 console_set_echo(VALUE io, VALUE f) 00430 { 00431 conmode t; 00432 rb_io_t *fptr; 00433 int fd; 00434 00435 GetOpenFile(io, fptr); 00436 fd = GetReadFD(fptr); 00437 if (!getattr(fd, &t)) rb_sys_fail(0); 00438 if (RTEST(f)) 00439 set_echo(&t, NULL); 00440 else 00441 set_noecho(&t, NULL); 00442 if (!setattr(fd, &t)) rb_sys_fail(0); 00443 return io; 00444 } 00445 00446 /* 00447 * call-seq: 00448 * io.echo? -> true or false 00449 * 00450 * Returns +true+ if echo back is enabled. 00451 * 00452 * You must require 'io/console' to use this method. 00453 */ 00454 static VALUE 00455 console_echo_p(VALUE io) 00456 { 00457 conmode t; 00458 rb_io_t *fptr; 00459 int fd; 00460 00461 GetOpenFile(io, fptr); 00462 fd = GetReadFD(fptr); 00463 if (!getattr(fd, &t)) rb_sys_fail(0); 00464 return echo_p(&t) ? Qtrue : Qfalse; 00465 } 00466 00467 #if defined TIOCGWINSZ 00468 typedef struct winsize rb_console_size_t; 00469 #define getwinsize(fd, buf) (ioctl((fd), TIOCGWINSZ, (buf)) == 0) 00470 #define setwinsize(fd, buf) (ioctl((fd), TIOCSWINSZ, (buf)) == 0) 00471 #define winsize_row(buf) (buf)->ws_row 00472 #define winsize_col(buf) (buf)->ws_col 00473 #elif defined _WIN32 00474 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t; 00475 #define getwinsize(fd, buf) ( \ 00476 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \ 00477 SET_LAST_ERROR) 00478 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1) 00479 #define winsize_col(buf) (buf)->dwSize.X 00480 #endif 00481 00482 #if defined TIOCGWINSZ || defined _WIN32 00483 #define USE_CONSOLE_GETSIZE 1 00484 #endif 00485 00486 #ifdef USE_CONSOLE_GETSIZE 00487 /* 00488 * call-seq: 00489 * io.winsize -> [rows, columns] 00490 * 00491 * Returns console size. 00492 * 00493 * You must require 'io/console' to use this method. 00494 */ 00495 static VALUE 00496 console_winsize(VALUE io) 00497 { 00498 rb_io_t *fptr; 00499 int fd; 00500 rb_console_size_t ws; 00501 00502 GetOpenFile(io, fptr); 00503 fd = GetWriteFD(fptr); 00504 if (!getwinsize(fd, &ws)) rb_sys_fail(0); 00505 return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws))); 00506 } 00507 00508 /* 00509 * call-seq: 00510 * io.winsize = [rows, columns] 00511 * 00512 * Tries to set console size. The effect depends on the platform and 00513 * the running environment. 00514 * 00515 * You must require 'io/console' to use this method. 00516 */ 00517 static VALUE 00518 console_set_winsize(VALUE io, VALUE size) 00519 { 00520 rb_io_t *fptr; 00521 rb_console_size_t ws; 00522 #if defined _WIN32 00523 HANDLE wh; 00524 int newrow, newcol; 00525 #endif 00526 VALUE row, col, xpixel, ypixel; 00527 #if defined TIOCSWINSZ 00528 int fd; 00529 #endif 00530 00531 GetOpenFile(io, fptr); 00532 size = rb_Array(size); 00533 rb_scan_args((int)RARRAY_LEN(size), RARRAY_PTR(size), "22", 00534 &row, &col, &xpixel, &ypixel); 00535 #if defined TIOCSWINSZ 00536 fd = GetWriteFD(fptr); 00537 ws.ws_row = ws.ws_col = ws.ws_xpixel = ws.ws_ypixel = 0; 00538 #define SET(m) ws.ws_##m = NIL_P(m) ? 0 : (unsigned short)NUM2UINT(m) 00539 SET(row); 00540 SET(col); 00541 SET(xpixel); 00542 SET(ypixel); 00543 #undef SET 00544 if (!setwinsize(fd, &ws)) rb_sys_fail(0); 00545 #elif defined _WIN32 00546 wh = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr)); 00547 newrow = (SHORT)NUM2UINT(row); 00548 newcol = (SHORT)NUM2UINT(col); 00549 if (!getwinsize(GetReadFD(fptr), &ws)) { 00550 rb_sys_fail("GetConsoleScreenBufferInfo"); 00551 } 00552 if ((ws.dwSize.X < newcol && (ws.dwSize.X = newcol, 1)) || 00553 (ws.dwSize.Y < newrow && (ws.dwSize.Y = newrow, 1))) { 00554 if (!(SetConsoleScreenBufferSize(wh, ws.dwSize) || SET_LAST_ERROR)) { 00555 rb_sys_fail("SetConsoleScreenBufferInfo"); 00556 } 00557 } 00558 ws.srWindow.Left = 0; 00559 ws.srWindow.Top = 0; 00560 ws.srWindow.Right = newcol; 00561 ws.srWindow.Bottom = newrow; 00562 if (!(SetConsoleWindowInfo(wh, FALSE, &ws.srWindow) || SET_LAST_ERROR)) { 00563 rb_sys_fail("SetConsoleWindowInfo"); 00564 } 00565 #endif 00566 return io; 00567 } 00568 #endif 00569 00570 /* 00571 * call-seq: 00572 * io.iflush 00573 * 00574 * Flushes input buffer in kernel. 00575 * 00576 * You must require 'io/console' to use this method. 00577 */ 00578 static VALUE 00579 console_iflush(VALUE io) 00580 { 00581 rb_io_t *fptr; 00582 int fd; 00583 00584 GetOpenFile(io, fptr); 00585 fd = GetReadFD(fptr); 00586 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00587 if (tcflush(fd, TCIFLUSH)) rb_sys_fail(0); 00588 #endif 00589 return io; 00590 } 00591 00592 /* 00593 * call-seq: 00594 * io.oflush 00595 * 00596 * Flushes output buffer in kernel. 00597 * 00598 * You must require 'io/console' to use this method. 00599 */ 00600 static VALUE 00601 console_oflush(VALUE io) 00602 { 00603 rb_io_t *fptr; 00604 int fd; 00605 00606 GetOpenFile(io, fptr); 00607 fd = GetWriteFD(fptr); 00608 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00609 if (tcflush(fd, TCOFLUSH)) rb_sys_fail(0); 00610 #endif 00611 return io; 00612 } 00613 00614 /* 00615 * call-seq: 00616 * io.ioflush 00617 * 00618 * Flushes input and output buffers in kernel. 00619 * 00620 * You must require 'io/console' to use this method. 00621 */ 00622 static VALUE 00623 console_ioflush(VALUE io) 00624 { 00625 rb_io_t *fptr; 00626 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00627 int fd1, fd2; 00628 #endif 00629 00630 GetOpenFile(io, fptr); 00631 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H 00632 fd1 = GetReadFD(fptr); 00633 fd2 = GetWriteFD(fptr); 00634 if (fd2 != -1 && fd1 != fd2) { 00635 if (tcflush(fd1, TCIFLUSH)) rb_sys_fail(0); 00636 if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0); 00637 } 00638 else { 00639 if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0); 00640 } 00641 #endif 00642 return io; 00643 } 00644 00645 /* 00646 * call-seq: 00647 * IO.console -> #<File:/dev/tty> 00648 * 00649 * Returns an File instance opened console. 00650 * 00651 * You must require 'io/console' to use this method. 00652 */ 00653 static VALUE 00654 console_dev(VALUE klass) 00655 { 00656 VALUE con = 0; 00657 rb_io_t *fptr; 00658 00659 if (klass == rb_cIO) klass = rb_cFile; 00660 if (rb_const_defined(klass, id_console)) { 00661 con = rb_const_get(klass, id_console); 00662 if (TYPE(con) == T_FILE) { 00663 if ((fptr = RFILE(con)->fptr) && GetReadFD(fptr) != -1) 00664 return con; 00665 } 00666 rb_mod_remove_const(klass, ID2SYM(id_console)); 00667 } 00668 { 00669 VALUE args[2]; 00670 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H 00671 # define CONSOLE_DEVICE "/dev/tty" 00672 #elif defined _WIN32 00673 # define CONSOLE_DEVICE "con$" 00674 # define CONSOLE_DEVICE_FOR_READING "conin$" 00675 # define CONSOLE_DEVICE_FOR_WRITING "conout$" 00676 #endif 00677 #ifndef CONSOLE_DEVICE_FOR_READING 00678 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE 00679 #endif 00680 #ifdef CONSOLE_DEVICE_FOR_WRITING 00681 VALUE out; 00682 rb_io_t *ofptr; 00683 #endif 00684 int fd; 00685 00686 #ifdef CONSOLE_DEVICE_FOR_WRITING 00687 fd = open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY); 00688 if (fd < 0) return Qnil; 00689 rb_update_max_fd(fd); 00690 args[1] = INT2FIX(O_WRONLY); 00691 args[0] = INT2NUM(fd); 00692 out = rb_class_new_instance(2, args, klass); 00693 #endif 00694 fd = open(CONSOLE_DEVICE_FOR_READING, O_RDWR); 00695 if (fd < 0) { 00696 #ifdef CONSOLE_DEVICE_FOR_WRITING 00697 rb_io_close(out); 00698 #endif 00699 return Qnil; 00700 } 00701 rb_update_max_fd(fd); 00702 args[1] = INT2FIX(O_RDWR); 00703 args[0] = INT2NUM(fd); 00704 con = rb_class_new_instance(2, args, klass); 00705 GetOpenFile(con, fptr); 00706 fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE)); 00707 #ifdef CONSOLE_DEVICE_FOR_WRITING 00708 GetOpenFile(out, ofptr); 00709 # ifdef HAVE_RB_IO_GET_WRITE_IO 00710 ofptr->pathv = fptr->pathv; 00711 fptr->tied_io_for_writing = out; 00712 # else 00713 fptr->f2 = ofptr->f; 00714 ofptr->f = 0; 00715 # endif 00716 ofptr->mode |= FMODE_SYNC; 00717 #endif 00718 fptr->mode |= FMODE_SYNC; 00719 rb_const_set(klass, id_console, con); 00720 } 00721 return con; 00722 } 00723 00724 static VALUE 00725 io_getch(int argc, VALUE *argv, VALUE io) 00726 { 00727 return rb_funcall2(io, rb_intern("getc"), argc, argv); 00728 } 00729 00730 /* 00731 * IO console methods 00732 */ 00733 void 00734 Init_console(void) 00735 { 00736 id_getc = rb_intern("getc"); 00737 id_console = rb_intern("console"); 00738 InitVM(console); 00739 } 00740 00741 void 00742 InitVM_console(void) 00743 { 00744 rb_define_method(rb_cIO, "raw", console_raw, -1); 00745 rb_define_method(rb_cIO, "raw!", console_set_raw, -1); 00746 rb_define_method(rb_cIO, "cooked", console_cooked, 0); 00747 rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0); 00748 rb_define_method(rb_cIO, "getch", console_getch, -1); 00749 rb_define_method(rb_cIO, "echo=", console_set_echo, 1); 00750 rb_define_method(rb_cIO, "echo?", console_echo_p, 0); 00751 rb_define_method(rb_cIO, "noecho", console_noecho, 0); 00752 rb_define_method(rb_cIO, "winsize", console_winsize, 0); 00753 rb_define_method(rb_cIO, "winsize=", console_set_winsize, 1); 00754 rb_define_method(rb_cIO, "iflush", console_iflush, 0); 00755 rb_define_method(rb_cIO, "oflush", console_oflush, 0); 00756 rb_define_method(rb_cIO, "ioflush", console_ioflush, 0); 00757 rb_define_singleton_method(rb_cIO, "console", console_dev, 0); 00758 { 00759 VALUE mReadable = rb_define_module_under(rb_cIO, "readable"); 00760 rb_define_method(mReadable, "getch", io_getch, -1); 00761 } 00762 } 00763