Ruby 2.0.0p247(2013-06-27revision41674)

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

console.c
Go to the documentation of this file.
1 /* -*- c-file-style: "ruby" -*- */
2 /*
3 * console IO module
4 */
5 #include "ruby.h"
6 #ifdef HAVE_RUBY_IO_H
7 #include "ruby/io.h"
8 #else
9 #include "rubyio.h"
10 #endif
11
12 #ifndef HAVE_RB_IO_T
13 typedef OpenFile rb_io_t;
14 #endif
15
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #ifdef HAVE_FCNTL_H
20 #include <fcntl.h>
21 #endif
22 #ifdef HAVE_SYS_IOCTL_H
23 #include <sys/ioctl.h>
24 #endif
25
26 #if defined HAVE_TERMIOS_H
27 # include <termios.h>
28 typedef struct termios conmode;
29
30 static int
31 setattr(int fd, conmode *t)
32 {
33 while (tcsetattr(fd, TCSAFLUSH, t)) {
34 if (errno != EINTR) return 0;
35 }
36 return 1;
37 }
38 # define getattr(fd, t) (tcgetattr(fd, t) == 0)
39 #elif defined HAVE_TERMIO_H
40 # include <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
45 # include <sgtty.h>
46 typedef struct sgttyb conmode;
47 # ifdef HAVE_STTY
48 # define setattr(fd, t) (stty(fd, t) == 0)
49 # else
50 # define setattr(fd, t) (ioctl((fd), TIOCSETP, (t)) == 0)
51 # endif
52 # ifdef HAVE_GTTY
53 # define getattr(fd, t) (gtty(fd, t) == 0)
54 # else
55 # define getattr(fd, t) (ioctl((fd), TIOCGETP, (t)) == 0)
56 # endif
57 #elif defined _WIN32
58 #include <winioctl.h>
59 typedef DWORD conmode;
60
61 #ifdef HAVE_RB_W32_MAP_ERRNO
62 #define LAST_ERROR rb_w32_map_errno(GetLastError())
63 #else
64 #define LAST_ERROR EBADF
65 #endif
66 #define SET_LAST_ERROR (errno = LAST_ERROR, 0)
67
68 static int
69 setattr(int fd, conmode *t)
70 {
71 int x = SetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), *t);
72 if (!x) errno = LAST_ERROR;
73 return x;
74 }
75
76 static int
77 getattr(int fd, conmode *t)
78 {
79 int x = GetConsoleMode((HANDLE)rb_w32_get_osfhandle(fd), t);
80 if (!x) errno = LAST_ERROR;
81 return x;
82 }
83 #endif
84 #ifndef SET_LAST_ERROR
85 #define SET_LAST_ERROR (0)
86 #endif
87
88 #ifndef InitVM
89 #define InitVM(ext) {void InitVM_##ext(void);InitVM_##ext();}
90 #endif
91
92 static ID id_getc, id_console;
93
94 typedef struct {
95 int vmin;
96 int vtime;
97 } rawmode_arg_t;
98
99 static rawmode_arg_t *
100 rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts)
101 {
102 rawmode_arg_t *optp = NULL;
103 VALUE vopts;
104 rb_scan_args(argc, argv, "0:", &vopts);
105 if (!NIL_P(vopts)) {
106 VALUE vmin = rb_hash_aref(vopts, ID2SYM(rb_intern("min")));
107 VALUE vtime = rb_hash_aref(vopts, ID2SYM(rb_intern("time")));
108 /* default values by `stty raw` */
109 opts->vmin = 1;
110 opts->vtime = 0;
111 if (!NIL_P(vmin)) {
112 opts->vmin = NUM2INT(vmin);
113 optp = opts;
114 }
115 if (!NIL_P(vtime)) {
116 VALUE v10 = INT2FIX(10);
117 vtime = rb_funcall3(vtime, '*', 1, &v10);
118 opts->vtime = NUM2INT(vtime);
119 optp = opts;
120 }
121 }
122 return optp;
123 }
124
125 static void
126 set_rawmode(conmode *t, void *arg)
127 {
128 #ifdef HAVE_CFMAKERAW
129 cfmakeraw(t);
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);
136 t->c_cflag |= CS8;
137 #elif defined HAVE_SGTTY_H
138 t->sg_flags &= ~ECHO;
139 t->sg_flags |= RAW;
140 #elif defined _WIN32
141 *t = 0;
142 #endif
143 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
144 if (arg) {
145 const rawmode_arg_t *r = arg;
146 if (r->vmin >= 0) t->c_cc[VMIN] = r->vmin;
147 if (r->vtime >= 0) t->c_cc[VTIME] = r->vtime;
148 }
149 #endif
150 }
151
152 static void
153 set_cookedmode(conmode *t, void *arg)
154 {
155 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
156 t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON);
157 t->c_oflag |= OPOST;
158 t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN);
159 #elif defined HAVE_SGTTY_H
160 t->sg_flags |= ECHO;
161 t->sg_flags &= ~RAW;
162 #elif defined _WIN32
163 *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT;
164 #endif
165 }
166
167 static void
168 set_noecho(conmode *t, void *arg)
169 {
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;
174 #elif defined _WIN32
175 *t &= ~ENABLE_ECHO_INPUT;
176 #endif
177 }
178
179 static void
180 set_echo(conmode *t, void *arg)
181 {
182 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
183 t->c_lflag |= (ECHO | ECHOE | ECHOK | ECHONL);
184 #elif defined HAVE_SGTTY_H
185 t->sg_flags |= ECHO;
186 #elif defined _WIN32
187 *t |= ENABLE_ECHO_INPUT;
188 #endif
189 }
190
191 static int
192 echo_p(conmode *t)
193 {
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;
198 #elif defined _WIN32
199 return (*t & ENABLE_ECHO_INPUT) != 0;
200 #endif
201 }
202
203 static int
204 set_ttymode(int fd, conmode *t, void (*setter)(conmode *, void *), void *arg)
205 {
206 conmode r;
207 if (!getattr(fd, t)) return 0;
208 r = *t;
209 setter(&r, arg);
210 return setattr(fd, &r);
211 }
212
213 #ifdef GetReadFile
214 #define GetReadFD(fptr) fileno(GetReadFile(fptr))
215 #else
216 #define GetReadFD(fptr) ((fptr)->fd)
217 #endif
218
219 #ifdef GetWriteFile
220 #define GetWriteFD(fptr) fileno(GetWriteFile(fptr))
221 #else
222 static inline int
223 get_write_fd(const rb_io_t *fptr)
224 {
225 VALUE wio = fptr->tied_io_for_writing;
226 rb_io_t *ofptr;
227 if (!wio) return fptr->fd;
228 GetOpenFile(wio, ofptr);
229 return ofptr->fd;
230 }
231 #define GetWriteFD(fptr) get_write_fd(fptr)
232 #endif
233
234 #define FD_PER_IO 2
235
236 static VALUE
237 ttymode(VALUE io, VALUE (*func)(VALUE), void (*setter)(conmode *, void *), void *arg)
238 {
239 rb_io_t *fptr;
240 int status = -1;
241 int error = 0;
242 int fd[FD_PER_IO];
243 conmode t[FD_PER_IO];
244 VALUE result = Qnil;
245
246 GetOpenFile(io, fptr);
247 fd[0] = GetReadFD(fptr);
248 if (fd[0] != -1) {
249 if (set_ttymode(fd[0], t+0, setter, arg)) {
250 status = 0;
251 }
252 else {
253 error = errno;
254 fd[0] = -1;
255 }
256 }
257 fd[1] = GetWriteFD(fptr);
258 if (fd[1] != -1 && fd[1] != fd[0]) {
259 if (set_ttymode(fd[1], t+1, setter, arg)) {
260 status = 0;
261 }
262 else {
263 error = errno;
264 fd[1] = -1;
265 }
266 }
267 if (status == 0) {
268 result = rb_protect(func, io, &status);
269 }
270 GetOpenFile(io, fptr);
271 if (fd[0] != -1 && fd[0] == GetReadFD(fptr)) {
272 if (!setattr(fd[0], t+0)) {
273 error = errno;
274 status = -1;
275 }
276 }
277 if (fd[1] != -1 && fd[1] != fd[0] && fd[1] == GetWriteFD(fptr)) {
278 if (!setattr(fd[1], t+1)) {
279 error = errno;
280 status = -1;
281 }
282 }
283 if (status) {
284 if (status == -1) {
285 errno = error;
286 rb_sys_fail(0);
287 }
288 rb_jump_tag(status);
289 }
290 return result;
291 }
292
293 /*
294 * call-seq:
295 * io.raw(min: nil, time: nil) {|io| }
296 *
297 * Yields +self+ within raw mode.
298 *
299 * STDIN.raw(&:gets)
300 *
301 * will read and return a line without echo back and line editing.
302 *
303 * You must require 'io/console' to use this method.
304 */
305 static VALUE
306 console_raw(int argc, VALUE *argv, VALUE io)
307 {
308 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
309 return ttymode(io, rb_yield, set_rawmode, optp);
310 }
311
312 /*
313 * call-seq:
314 * io.raw!(min: nil, time: nil)
315 *
316 * Enables raw mode.
317 *
318 * If the terminal mode needs to be back, use io.raw { ... }.
319 *
320 * You must require 'io/console' to use this method.
321 */
322 static VALUE
323 console_set_raw(int argc, VALUE *argv, VALUE io)
324 {
325 conmode t;
326 rb_io_t *fptr;
327 int fd;
328 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
329
330 GetOpenFile(io, fptr);
331 fd = GetReadFD(fptr);
332 if (!getattr(fd, &t)) rb_sys_fail(0);
333 set_rawmode(&t, optp);
334 if (!setattr(fd, &t)) rb_sys_fail(0);
335 return io;
336 }
337
338 /*
339 * call-seq:
340 * io.cooked {|io| }
341 *
342 * Yields +self+ within cooked mode.
343 *
344 * STDIN.cooked(&:gets)
345 *
346 * will read and return a line with echo back and line editing.
347 *
348 * You must require 'io/console' to use this method.
349 */
350 static VALUE
351 console_cooked(VALUE io)
352 {
353 return ttymode(io, rb_yield, set_cookedmode, NULL);
354 }
355
356 /*
357 * call-seq:
358 * io.cooked!
359 *
360 * Enables cooked mode.
361 *
362 * If the terminal mode needs to be back, use io.cooked { ... }.
363 *
364 * You must require 'io/console' to use this method.
365 */
366 static VALUE
367 console_set_cooked(VALUE io)
368 {
369 conmode t;
370 rb_io_t *fptr;
371 int fd;
372
373 GetOpenFile(io, fptr);
374 fd = GetReadFD(fptr);
375 if (!getattr(fd, &t)) rb_sys_fail(0);
376 set_cookedmode(&t, NULL);
377 if (!setattr(fd, &t)) rb_sys_fail(0);
378 return io;
379 }
380
381 static VALUE
382 getc_call(VALUE io)
383 {
384 return rb_funcall2(io, id_getc, 0, 0);
385 }
386
387 /*
388 * call-seq:
389 * io.getch(min: nil, time: nil) -> char
390 *
391 * Reads and returns a character in raw mode.
392 *
393 * You must require 'io/console' to use this method.
394 */
395 static VALUE
396 console_getch(int argc, VALUE *argv, VALUE io)
397 {
398 rawmode_arg_t opts, *optp = rawmode_opt(argc, argv, &opts);
399 return ttymode(io, getc_call, set_rawmode, optp);
400 }
401
402 /*
403 * call-seq:
404 * io.noecho {|io| }
405 *
406 * Yields +self+ with disabling echo back.
407 *
408 * STDIN.noecho(&:gets)
409 *
410 * will read and return a line without echo back.
411 *
412 * You must require 'io/console' to use this method.
413 */
414 static VALUE
415 console_noecho(VALUE io)
416 {
417 return ttymode(io, rb_yield, set_noecho, NULL);
418 }
419
420 /*
421 * call-seq:
422 * io.echo = flag
423 *
424 * Enables/disables echo back.
425 * On some platforms, all combinations of this flags and raw/cooked
426 * mode may not be valid.
427 *
428 * You must require 'io/console' to use this method.
429 */
430 static VALUE
431 console_set_echo(VALUE io, VALUE f)
432 {
433 conmode t;
434 rb_io_t *fptr;
435 int fd;
436
437 GetOpenFile(io, fptr);
438 fd = GetReadFD(fptr);
439 if (!getattr(fd, &t)) rb_sys_fail(0);
440 if (RTEST(f))
441 set_echo(&t, NULL);
442 else
443 set_noecho(&t, NULL);
444 if (!setattr(fd, &t)) rb_sys_fail(0);
445 return io;
446 }
447
448 /*
449 * call-seq:
450 * io.echo? -> true or false
451 *
452 * Returns +true+ if echo back is enabled.
453 *
454 * You must require 'io/console' to use this method.
455 */
456 static VALUE
457 console_echo_p(VALUE io)
458 {
459 conmode t;
460 rb_io_t *fptr;
461 int fd;
462
463 GetOpenFile(io, fptr);
464 fd = GetReadFD(fptr);
465 if (!getattr(fd, &t)) rb_sys_fail(0);
466 return echo_p(&t) ? Qtrue : Qfalse;
467 }
468
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
475 #elif defined _WIN32
476 typedef CONSOLE_SCREEN_BUFFER_INFO rb_console_size_t;
477 #define getwinsize(fd, buf) ( \
478 GetConsoleScreenBufferInfo((HANDLE)rb_w32_get_osfhandle(fd), (buf)) || \
479 SET_LAST_ERROR)
480 #define winsize_row(buf) ((buf)->srWindow.Bottom - (buf)->srWindow.Top + 1)
481 #define winsize_col(buf) (buf)->dwSize.X
482 #endif
483
484 #if defined TIOCGWINSZ || defined _WIN32
485 #define USE_CONSOLE_GETSIZE 1
486 #endif
487
488 #ifdef USE_CONSOLE_GETSIZE
489 /*
490 * call-seq:
491 * io.winsize -> [rows, columns]
492 *
493 * Returns console size.
494 *
495 * You must require 'io/console' to use this method.
496 */
497 static VALUE
498 console_winsize(VALUE io)
499 {
500 rb_io_t *fptr;
501 int fd;
502 rb_console_size_t ws;
503
504 GetOpenFile(io, fptr);
505 fd = GetWriteFD(fptr);
506 if (!getwinsize(fd, &ws)) rb_sys_fail(0);
507 return rb_assoc_new(INT2NUM(winsize_row(&ws)), INT2NUM(winsize_col(&ws)));
508 }
509
510 /*
511 * call-seq:
512 * io.winsize = [rows, columns]
513 *
514 * Tries to set console size. The effect depends on the platform and
515 * the running environment.
516 *
517 * You must require 'io/console' to use this method.
518 */
519 static VALUE
520 console_set_winsize(VALUE io, VALUE size)
521 {
522 rb_io_t *fptr;
523 rb_console_size_t ws;
524 #if defined _WIN32
525 HANDLE wh;
526 int newrow, newcol;
527 #endif
528 VALUE row, col, xpixel, ypixel;
529 #if defined TIOCSWINSZ
530 int fd;
531 #endif
532
533 GetOpenFile(io, fptr);
534 size = rb_Array(size);
535 rb_scan_args((int)RARRAY_LEN(size), RARRAY_PTR(size), "22",
536 &row, &col, &xpixel, &ypixel);
537 #if defined TIOCSWINSZ
538 fd = GetWriteFD(fptr);
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)
541 SET(row);
542 SET(col);
543 SET(xpixel);
544 SET(ypixel);
545 #undef SET
546 if (!setwinsize(fd, &ws)) rb_sys_fail(0);
547 #elif defined _WIN32
548 wh = (HANDLE)rb_w32_get_osfhandle(GetReadFD(fptr));
549 newrow = (SHORT)NUM2UINT(row);
550 newcol = (SHORT)NUM2UINT(col);
551 if (!getwinsize(GetReadFD(fptr), &ws)) {
552 rb_sys_fail("GetConsoleScreenBufferInfo");
553 }
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)) {
557 rb_sys_fail("SetConsoleScreenBufferInfo");
558 }
559 }
560 ws.srWindow.Left = 0;
561 ws.srWindow.Top = 0;
562 ws.srWindow.Right = newcol;
563 ws.srWindow.Bottom = newrow;
564 if (!(SetConsoleWindowInfo(wh, FALSE, &ws.srWindow) || SET_LAST_ERROR)) {
565 rb_sys_fail("SetConsoleWindowInfo");
566 }
567 #endif
568 return io;
569 }
570 #endif
571
572 /*
573 * call-seq:
574 * io.iflush
575 *
576 * Flushes input buffer in kernel.
577 *
578 * You must require 'io/console' to use this method.
579 */
580 static VALUE
581 console_iflush(VALUE io)
582 {
583 rb_io_t *fptr;
584 int fd;
585
586 GetOpenFile(io, fptr);
587 fd = GetReadFD(fptr);
588 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
589 if (tcflush(fd, TCIFLUSH)) rb_sys_fail(0);
590 #endif
591 return io;
592 }
593
594 /*
595 * call-seq:
596 * io.oflush
597 *
598 * Flushes output buffer in kernel.
599 *
600 * You must require 'io/console' to use this method.
601 */
602 static VALUE
603 console_oflush(VALUE io)
604 {
605 rb_io_t *fptr;
606 int fd;
607
608 GetOpenFile(io, fptr);
609 fd = GetWriteFD(fptr);
610 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
611 if (tcflush(fd, TCOFLUSH)) rb_sys_fail(0);
612 #endif
613 return io;
614 }
615
616 /*
617 * call-seq:
618 * io.ioflush
619 *
620 * Flushes input and output buffers in kernel.
621 *
622 * You must require 'io/console' to use this method.
623 */
624 static VALUE
625 console_ioflush(VALUE io)
626 {
627 rb_io_t *fptr;
628 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
629 int fd1, fd2;
630 #endif
631
632 GetOpenFile(io, fptr);
633 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H
634 fd1 = GetReadFD(fptr);
635 fd2 = GetWriteFD(fptr);
636 if (fd2 != -1 && fd1 != fd2) {
637 if (tcflush(fd1, TCIFLUSH)) rb_sys_fail(0);
638 if (tcflush(fd2, TCOFLUSH)) rb_sys_fail(0);
639 }
640 else {
641 if (tcflush(fd1, TCIOFLUSH)) rb_sys_fail(0);
642 }
643 #endif
644 return io;
645 }
646
647 /*
648 * call-seq:
649 * IO.console -> #<File:/dev/tty>
650 *
651 * Returns an File instance opened console.
652 *
653 * You must require 'io/console' to use this method.
654 */
655 static VALUE
656 console_dev(VALUE klass)
657 {
658 VALUE con = 0;
659 rb_io_t *fptr;
660
661 if (klass == rb_cIO) klass = rb_cFile;
662 if (rb_const_defined(klass, id_console)) {
663 con = rb_const_get(klass, id_console);
664 if (RB_TYPE_P(con, T_FILE)) {
665 if ((fptr = RFILE(con)->fptr) && GetReadFD(fptr) != -1)
666 return con;
667 }
668 rb_mod_remove_const(klass, ID2SYM(id_console));
669 }
670 {
671 VALUE args[2];
672 #if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H || defined HAVE_SGTTY_H
673 # define CONSOLE_DEVICE "/dev/tty"
674 #elif defined _WIN32
675 # define CONSOLE_DEVICE "con$"
676 # define CONSOLE_DEVICE_FOR_READING "conin$"
677 # define CONSOLE_DEVICE_FOR_WRITING "conout$"
678 #endif
679 #ifndef CONSOLE_DEVICE_FOR_READING
680 # define CONSOLE_DEVICE_FOR_READING CONSOLE_DEVICE
681 #endif
682 #ifdef CONSOLE_DEVICE_FOR_WRITING
683 VALUE out;
684 rb_io_t *ofptr;
685 #endif
686 int fd;
687
688 #ifdef CONSOLE_DEVICE_FOR_WRITING
689 fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_WRITING, O_WRONLY, 0);
690 if (fd < 0) return Qnil;
691 rb_update_max_fd(fd);
692 args[1] = INT2FIX(O_WRONLY);
693 args[0] = INT2NUM(fd);
694 out = rb_class_new_instance(2, args, klass);
695 #endif
696 fd = rb_cloexec_open(CONSOLE_DEVICE_FOR_READING, O_RDWR, 0);
697 if (fd < 0) {
698 #ifdef CONSOLE_DEVICE_FOR_WRITING
699 rb_io_close(out);
700 #endif
701 return Qnil;
702 }
703 rb_update_max_fd(fd);
704 args[1] = INT2FIX(O_RDWR);
705 args[0] = INT2NUM(fd);
706 con = rb_class_new_instance(2, args, klass);
707 GetOpenFile(con, fptr);
708 fptr->pathv = rb_obj_freeze(rb_str_new2(CONSOLE_DEVICE));
709 #ifdef CONSOLE_DEVICE_FOR_WRITING
710 GetOpenFile(out, ofptr);
711 # ifdef HAVE_RB_IO_GET_WRITE_IO
712 ofptr->pathv = fptr->pathv;
713 fptr->tied_io_for_writing = out;
714 # else
715 fptr->f2 = ofptr->f;
716 ofptr->f = 0;
717 # endif
718 ofptr->mode |= FMODE_SYNC;
719 #endif
720 fptr->mode |= FMODE_SYNC;
721 rb_const_set(klass, id_console, con);
722 }
723 return con;
724 }
725
726 static VALUE
727 io_getch(int argc, VALUE *argv, VALUE io)
728 {
729 return rb_funcall2(io, rb_intern("getc"), argc, argv);
730 }
731
732 /*
733 * IO console methods
734 */
735 void
736 Init_console(void)
737 {
738 id_getc = rb_intern("getc");
739 id_console = rb_intern("console");
740 InitVM(console);
741 }
742
743 void
744 InitVM_console(void)
745 {
746 rb_define_method(rb_cIO, "raw", console_raw, -1);
747 rb_define_method(rb_cIO, "raw!", console_set_raw, -1);
748 rb_define_method(rb_cIO, "cooked", console_cooked, 0);
749 rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0);
750 rb_define_method(rb_cIO, "getch", console_getch, -1);
751 rb_define_method(rb_cIO, "echo=", console_set_echo, 1);
752 rb_define_method(rb_cIO, "echo?", console_echo_p, 0);
753 rb_define_method(rb_cIO, "noecho", console_noecho, 0);
754 rb_define_method(rb_cIO, "winsize", console_winsize, 0);
755 rb_define_method(rb_cIO, "winsize=", console_set_winsize, 1);
756 rb_define_method(rb_cIO, "iflush", console_iflush, 0);
757 rb_define_method(rb_cIO, "oflush", console_oflush, 0);
758 rb_define_method(rb_cIO, "ioflush", console_ioflush, 0);
759 rb_define_singleton_method(rb_cIO, "console", console_dev, 0);
760 {
761 VALUE mReadable = rb_define_module_under(rb_cIO, "readable");
762 rb_define_method(mReadable, "getch", io_getch, -1);
763 }
764 }
765