ext/io/console/console.c

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

Generated on 19 Jul 2016 for Ruby by  doxygen 1.4.7