aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-06 06:25:10 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-12-06 06:25:10 +0000
commit4024f1c13c52b6d1af999e0f6586a17a976e31ce (patch)
tree22571b758c572185fb2dd9fe2b25ad7528cb29ac
parent3cd9983c299a3deb9883599317ea2eb0bf51cb6f (diff)
downloadruby-4024f1c13c52b6d1af999e0f6586a17a976e31ce.tar.gz
console.c: console_getpass
* ext/io/console/console.c (console_getpass): add IO#getpass method. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52902 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--ext/io/console/console.c84
-rw-r--r--test/io/console/test_io_console.rb14
3 files changed, 102 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index 888a5eaa1b..575f121a1e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sun Dec 6 15:25:06 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * ext/io/console/console.c (console_getpass): add IO#getpass
+ method.
+
Sun Dec 6 08:39:05 2015 SHIBATA Hiroshi <hsbt@ruby-lang.org>
* ext/json/json.gemspec: bump version to json 1.8.3. CRuby already contained
diff --git a/ext/io/console/console.c b/ext/io/console/console.c
index dc494c2ddd..629a7fbd6d 100644
--- a/ext/io/console/console.c
+++ b/ext/io/console/console.c
@@ -73,7 +73,7 @@ getattr(int fd, conmode *t)
#define SET_LAST_ERROR (0)
#endif
-static ID id_getc, id_console, id_close, id_min, id_time;
+static ID id_getc, id_gets, id_console, id_close, id_min, id_time;
#ifndef HAVE_RB_F_SEND
static ID id___send__;
@@ -845,6 +845,85 @@ io_getch(int argc, VALUE *argv, VALUE io)
return rb_funcall2(io, id_getc, argc, argv);
}
+static VALUE
+puts_call(VALUE io)
+{
+ return rb_io_write(io, rb_default_rs);
+}
+
+static VALUE
+getpass_call(VALUE io)
+{
+ return ttymode(io, rb_io_gets, set_noecho, NULL);
+}
+
+static VALUE
+write_io(VALUE io)
+{
+ VALUE wio = rb_io_get_write_io(io);
+ if (wio == io && io == rb_stdin) wio = rb_stdout;
+ return wio;
+}
+
+static void
+prompt(int argc, VALUE *argv, VALUE io)
+{
+ if (argc > 0 && !NIL_P(argv[0])) {
+ VALUE str = argv[0];
+ StringValueCStr(str);
+ rb_check_safe_obj(str);
+ rb_io_write(io, str);
+ }
+}
+
+static VALUE
+str_chomp(VALUE str)
+{
+ if (!NIL_P(str)) {
+ str = rb_funcallv(str, rb_intern("chomp!"), 0, 0);
+ }
+ return str;
+}
+
+/*
+ * call-seq:
+ * io.getpass(prompt=nil) -> string
+ *
+ * Reads and returns a line without echo back.
+ * Prints +prompt+ unless it is +nil+.
+ *
+ * You must require 'io/console' to use this method.
+ */
+static VALUE
+console_getpass(int argc, VALUE *argv, VALUE io)
+{
+ VALUE str, wio;
+
+ rb_check_arity(argc, 0, 1);
+ wio = write_io(io);
+ prompt(argc, argv, wio);
+ str = rb_ensure(getpass_call, io, puts_call, wio);
+ return str_chomp(str);
+}
+
+/*
+ * call-seq:
+ * io.getpass(prompt=nil) -> string
+ *
+ * See IO#getpass.
+ */
+static VALUE
+io_getpass(int argc, VALUE *argv, VALUE io)
+{
+ VALUE str;
+
+ rb_check_arity(argc, 0, 1);
+ prompt(argc, argv, io);
+ str = str_chomp(rb_funcallv(io, id_gets, 0, 0));
+ puts_call(io);
+ return str;
+}
+
/*
* IO console methods
*/
@@ -853,6 +932,7 @@ Init_console(void)
{
#undef rb_intern
id_getc = rb_intern("getc");
+ id_gets = rb_intern("gets");
id_console = rb_intern("console");
id_close = rb_intern("close");
id_min = rb_intern("min");
@@ -884,9 +964,11 @@ InitVM_console(void)
rb_define_method(rb_cIO, "cursor", console_cursor_pos, 0);
rb_define_method(rb_cIO, "cursor=", console_cursor_set, 1);
rb_define_method(rb_cIO, "pressed?", console_key_pressed_p, 1);
+ rb_define_method(rb_cIO, "getpass", console_getpass, -1);
rb_define_singleton_method(rb_cIO, "console", console_dev, -1);
{
VALUE mReadable = rb_define_module_under(rb_cIO, "generic_readable");
rb_define_method(mReadable, "getch", io_getch, -1);
+ rb_define_method(mReadable, "getpass", io_getpass, -1);
}
}
diff --git a/test/io/console/test_io_console.rb b/test/io/console/test_io_console.rb
index c69eec757e..dc489166e8 100644
--- a/test/io/console/test_io_console.rb
+++ b/test/io/console/test_io_console.rb
@@ -180,6 +180,20 @@ class TestIO_Console < Test::Unit::TestCase
}
end
+ def test_getpass
+ th = nil
+ helper {|m, s|
+ th = Thread.start {
+ sleep 0.1
+ s.print "asdf\n"
+ }
+ assert_equal("asdf", m.getpass("> "))
+ assert_equal("\n", s.readpartial(2))
+ }
+ ensure
+ th.join
+ end
+
def test_iflush
helper {|m, s|
m.print "a"