Ruby  2.0.0p648(2015-12-16revision53162)
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 
93 
94 typedef struct {
95  int vmin;
96  int vtime;
98 
99 static rawmode_arg_t *
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
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
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
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
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
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
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
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
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
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);
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
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
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
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
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  }
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_RDWR, 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
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
728 {
729  return rb_funcall2(io, rb_intern("getc"), argc, argv);
730 }
731 
732 /*
733  * IO console methods
734  */
735 void
737 {
738  id_getc = rb_intern("getc");
739  id_console = rb_intern("console");
740  InitVM(console);
741 }
742 
743 void
745 {
746  rb_define_method(rb_cIO, "raw", console_raw, -1);
748  rb_define_method(rb_cIO, "cooked", console_cooked, 0);
750  rb_define_method(rb_cIO, "getch", console_getch, -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);
760  {
761  VALUE mReadable = rb_define_module_under(rb_cIO, "readable");
762  rb_define_method(mReadable, "getch", io_getch, -1);
763  }
764 }
static int get_write_fd(const rb_io_t *fptr)
Definition: console.c:223
#define SET(a, b, c, d, k, s, Ti)
#define GetReadFD(fptr)
Definition: console.c:216
#define RARRAY_LEN(a)
Definition: ruby.h:899
#define FALSE
Definition: nkf.h:174
#define INT2NUM(x)
Definition: ruby.h:1178
void rb_update_max_fd(int fd)
Definition: io.c:164
#define FD_PER_IO
Definition: console.c:234
#define NUM2INT(x)
Definition: ruby.h:622
void Init_console(void)
Definition: console.c:736
#define NUM2UINT(x)
Definition: ruby.h:623
static rawmode_arg_t * rawmode_opt(int argc, VALUE *argv, rawmode_arg_t *opts)
Definition: console.c:100
void rb_define_singleton_method(VALUE obj, const char *name, VALUE(*func)(ANYARGS), int argc)
Defines a singleton method for obj.
Definition: class.c:1497
static void set_echo(conmode *t, void *arg)
Definition: console.c:180
int rb_cloexec_open(const char *pathname, int flags, mode_t mode)
Definition: io.c:209
#define InitVM(ext)
Definition: ruby.h:1651
static void set_cookedmode(conmode *t, void *arg)
Definition: console.c:153
struct rb_io_t rb_io_t
#define Qtrue
Definition: ruby.h:434
Definition: io.h:63
static VALUE ttymode(VALUE io, VALUE(*func)(VALUE), void(*setter)(conmode *, void *), void *arg)
Definition: console.c:237
static VALUE console_getch(int argc, VALUE *argv, VALUE io)
Definition: console.c:396
SOCKET rb_w32_get_osfhandle(int)
Definition: win32.c:972
VALUE rb_cFile
Definition: file.c:138
SSL_METHOD *(* func)(void)
Definition: ossl_ssl.c:108
#define GetWriteFD(fptr)
Definition: console.c:231
VALUE rb_protect(VALUE(*proc)(VALUE), VALUE data, int *state)
Definition: eval.c:771
static int echo_p(conmode *t)
Definition: console.c:192
static ID id_getc
Definition: console.c:92
int rb_const_defined(VALUE, ID)
Definition: variable.c:2103
static ID id_console
Definition: console.c:92
#define RFILE(obj)
Definition: ruby.h:1107
#define GetOpenFile(obj, fp)
Definition: io.h:120
void InitVM_console(void)
Definition: console.c:744
int mode
Definition: io.h:66
int args
Definition: win32ole.c:785
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
VALUE rb_Array(VALUE)
Definition: object.c:2820
static VALUE console_ioflush(VALUE io)
Definition: console.c:625
VALUE rb_class_new_instance(int, VALUE *, VALUE)
Definition: object.c:1794
IUnknown DWORD
Definition: win32ole.c:149
#define CONSOLE_DEVICE_FOR_READING
#define NIL_P(v)
Definition: ruby.h:446
int fd
Definition: io.h:64
static VALUE console_echo_p(VALUE io)
Definition: console.c:457
int argc
Definition: ruby.c:130
#define Qfalse
Definition: ruby.h:433
#define SET_LAST_ERROR
Definition: console.c:85
static VALUE console_set_echo(VALUE io, VALUE f)
Definition: console.c:431
static VALUE console_set_cooked(VALUE io)
Definition: console.c:367
VALUE rb_const_get(VALUE, ID)
Definition: variable.c:1876
RUBY_EXTERN VALUE rb_cIO
Definition: ruby.h:1442
VALUE rb_yield(VALUE)
Definition: vm_eval.c:933
int errno
VALUE rb_funcall2(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:804
static VALUE getc_call(VALUE io)
Definition: console.c:382
int rb_scan_args(int argc, const VALUE *argv, const char *fmt,...)
Definition: class.c:1570
VALUE rb_assoc_new(VALUE car, VALUE cdr)
Definition: array.c:545
unsigned long ID
Definition: ruby.h:105
VALUE tied_io_for_writing
Definition: io.h:74
#define Qnil
Definition: ruby.h:435
void rb_const_set(VALUE, ID, VALUE)
Definition: variable.c:2141
static int set_ttymode(int fd, conmode *t, void(*setter)(conmode *, void *), void *arg)
Definition: console.c:204
VALUE rb_io_close(VALUE)
Definition: io.c:4233
unsigned long VALUE
Definition: ruby.h:104
static VALUE result
Definition: nkf.c:40
static VALUE console_cooked(VALUE io)
Definition: console.c:351
static VALUE console_noecho(VALUE io)
Definition: console.c:415
void rb_sys_fail(const char *mesg)
Definition: error.c:1907
void rb_jump_tag(int tag)
Definition: eval.c:666
VALUE rb_funcall3(VALUE, ID, int, const VALUE *)
Calls a method.
Definition: vm_eval.c:819
static VALUE console_raw(int argc, VALUE *argv, VALUE io)
Definition: console.c:306
VALUE rb_define_module_under(VALUE outer, const char *name)
Definition: class.c:626
int size
Definition: encoding.c:52
#define f
#define INT2FIX(i)
Definition: ruby.h:241
VALUE rb_hash_aref(VALUE hash, VALUE key)
Definition: hash.c:570
#define RARRAY_PTR(a)
Definition: ruby.h:904
VALUE pathv
Definition: io.h:69
#define RTEST(v)
Definition: ruby.h:445
VALUE rb_mod_remove_const(VALUE, VALUE)
Definition: variable.c:1916
#define T_FILE
Definition: ruby.h:496
static VALUE console_iflush(VALUE io)
Definition: console.c:581
static VALUE console_set_raw(int argc, VALUE *argv, VALUE io)
Definition: console.c:323
#define ID2SYM(x)
Definition: ruby.h:363
#define FMODE_SYNC
Definition: io.h:107
VALUE rb_obj_freeze(VALUE)
Definition: object.c:1012
static VALUE console_dev(VALUE klass)
Definition: console.c:656
#define rb_intern(str)
static VALUE io_getch(int argc, VALUE *argv, VALUE io)
Definition: console.c:727
#define NULL
Definition: _sdbm.c:102
static VALUE console_oflush(VALUE io)
Definition: console.c:603
static void set_rawmode(conmode *t, void *arg)
Definition: console.c:126
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
VALUE rb_str_new2(const char *)
static void set_noecho(conmode *t, void *arg)
Definition: console.c:168
char ** argv
Definition: ruby.c:131