ruby-changes:22875
From: nobu <ko1@a...>
Date: Tue, 6 Mar 2012 12:40:03 +0900 (JST)
Subject: [ruby-changes:22875] nobu:r34924 (ruby_1_9_3): merge revision(s) 33785:
nobu 2012-03-06 12:39:42 +0900 (Tue, 06 Mar 2012) New Revision: 34924 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=34924 Log: merge revision(s) 33785: * ext/io/console/console.c (console_cooked, console_set_cooked): new methods to reset cooked mode. [EXPERIMENTAL] Modified files: branches/ruby_1_9_3/ChangeLog branches/ruby_1_9_3/ext/io/console/console.c branches/ruby_1_9_3/test/io/console/test_io_console.rb branches/ruby_1_9_3/version.h Index: ruby_1_9_3/ChangeLog =================================================================== --- ruby_1_9_3/ChangeLog (revision 34923) +++ ruby_1_9_3/ChangeLog (revision 34924) @@ -1,3 +1,8 @@ +Tue Mar 6 12:39:27 2012 Nobuyoshi Nakada <nobu@r...> + + * ext/io/console/console.c (console_cooked, console_set_cooked): + new methods to reset cooked mode. [EXPERIMENTAL] + Tue Mar 6 12:31:47 2012 Nobuyoshi Nakada <nobu@r...> * ext/io/console/console.c (io_getch): default delegating method Index: ruby_1_9_3/ext/io/console/console.c =================================================================== --- ruby_1_9_3/ext/io/console/console.c (revision 34923) +++ ruby_1_9_3/ext/io/console/console.c (revision 34924) @@ -96,8 +96,7 @@ { #ifdef HAVE_CFMAKERAW cfmakeraw(t); -#else -#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H +#elif defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); t->c_oflag &= ~OPOST; t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); @@ -109,6 +108,20 @@ #elif defined _WIN32 *t = 0; #endif +} + +static void +set_cookedmode(conmode *t) +{ +#if defined HAVE_TERMIOS_H || defined HAVE_TERMIO_H + t->c_iflag |= (BRKINT|ISTRIP|ICRNL|IXON); + t->c_oflag |= OPOST; + t->c_lflag |= (ECHO|ECHOE|ECHOK|ECHONL|ICANON|ISIG|IEXTEN); +#elif defined HAVE_SGTTY_H + t->sg_flags |= ECHO; + t->sg_flags &= ~RAW; +#elif defined _WIN32 + *t |= ENABLE_ECHO_INPUT|ENABLE_LINE_INPUT|ENABLE_PROCESSED_INPUT; #endif } @@ -281,7 +294,46 @@ return io; } +/* + * call-seq: + * io.cooked {|io| } + * + * Yields +self+ within cooked mode. + * + * STDIN.cooked(&:gets) + * + * will read and return a line with echo back and line editing. + */ static VALUE +console_cooked(VALUE io) +{ + return ttymode(io, rb_yield, set_cookedmode); +} + +/* + * call-seq: + * io.cooked! + * + * Enables cooked mode. + * + * If the terminal mode needs to be back, use io.cooked { ... }. + */ +static VALUE +console_set_cooked(VALUE io) +{ + conmode t; + rb_io_t *fptr; + int fd; + + GetOpenFile(io, fptr); + fd = GetReadFD(fptr); + if (!getattr(fd, &t)) rb_sys_fail(0); + set_cookedmode(&t); + if (!setattr(fd, &t)) rb_sys_fail(0); + return io; +} + +static VALUE getc_call(VALUE io) { return rb_funcall2(io, id_getc, 0, 0); @@ -645,6 +697,8 @@ { rb_define_method(rb_cIO, "raw", console_raw, 0); rb_define_method(rb_cIO, "raw!", console_set_raw, 0); + rb_define_method(rb_cIO, "cooked", console_cooked, 0); + rb_define_method(rb_cIO, "cooked!", console_set_cooked, 0); rb_define_method(rb_cIO, "getch", console_getch, 0); rb_define_method(rb_cIO, "echo=", console_set_echo, 1); rb_define_method(rb_cIO, "echo?", console_echo_p, 0); Index: ruby_1_9_3/version.h =================================================================== --- ruby_1_9_3/version.h (revision 34923) +++ ruby_1_9_3/version.h (revision 34924) @@ -1,5 +1,5 @@ #define RUBY_VERSION "1.9.3" -#define RUBY_PATCHLEVEL 159 +#define RUBY_PATCHLEVEL 160 #define RUBY_RELEASE_DATE "2012-03-06" #define RUBY_RELEASE_YEAR 2012 Index: ruby_1_9_3/test/io/console/test_io_console.rb =================================================================== --- ruby_1_9_3/test/io/console/test_io_console.rb (revision 34923) +++ ruby_1_9_3/test/io/console/test_io_console.rb (revision 34924) @@ -20,6 +20,21 @@ } end + def test_cooked + helper {|m, s| + s.raw { + s.print "abc\n" + assert_equal("abc\n", m.gets) + s.cooked { + s.print "def\n" + assert_equal("def\r\n", m.gets) + } + } + s.print "ghi\n" + assert_equal("ghi\r\n", m.gets) + } + end + def test_echo helper {|m, s| assert(s.echo?) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/