From a67f1a9534322914367d6eb17eb01badd87e7f79 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 27 Jun 2010 14:31:19 +0000 Subject: * include/ruby/io.h, io.c: reverted r21709. * ruby.c (load_file_internal): nothing to read if EOF reached while reading shebang. [ruby-core:30910] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@28451 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 7 +++++++ include/ruby/io.h | 1 - io.c | 21 +++------------------ ruby.c | 12 +++++++++--- test/ruby/test_rubyoptions.rb | 24 ++++++++++++++++++++++++ 5 files changed, 43 insertions(+), 22 deletions(-) diff --git a/ChangeLog b/ChangeLog index 37c7eeb114..573002ad94 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Sun Jun 27 23:31:17 2010 Nobuyoshi Nakada + + * include/ruby/io.h, io.c: reverted r21709. + + * ruby.c (load_file_internal): nothing to read if EOF reached + while reading shebang. [ruby-core:30910] + Sun Jun 27 13:25:07 2010 Tanaka Akira * io.c (simple_sendfile): don't try to send data more than SSIZE_MAX diff --git a/include/ruby/io.h b/include/ruby/io.h index e05a0f52ca..f0f83fc329 100644 --- a/include/ruby/io.h +++ b/include/ruby/io.h @@ -92,7 +92,6 @@ typedef struct rb_io_t { #define FMODE_WSPLIT_INITIALIZED 0x00000400 #define FMODE_TRUNC 0x00000800 #define FMODE_TEXTMODE 0x00001000 -#define FMODE_EOF 0x00002000 /* #define FMODE_PREP 0x00010000 */ #define FMODE_SETENC_BY_BOM 0x00100000 diff --git a/io.c b/io.c index 48634d306b..05b2d45832 100644 --- a/io.c +++ b/io.c @@ -398,9 +398,7 @@ flush_before_seek(rb_io_t *fptr) return fptr; } -#define io_set_eof(fptr) (void)(((fptr)->mode & FMODE_TTY) && ((fptr)->mode |= FMODE_EOF)) -#define io_unset_eof(fptr) (fptr->mode &= ~FMODE_EOF) -#define io_seek(fptr, ofs, whence) (errno = 0, io_unset_eof(fptr), lseek(flush_before_seek(fptr)->fd, ofs, whence)) +#define io_seek(fptr, ofs, whence) (errno = 0, lseek(flush_before_seek(fptr)->fd, ofs, whence)) #define io_tell(fptr) lseek(flush_before_seek(fptr)->fd, 0, SEEK_CUR) #ifndef SEEK_CUR @@ -1203,9 +1201,6 @@ io_fillbuf(rb_io_t *fptr) { ssize_t r; - if (fptr->mode & FMODE_EOF) { - return -1; - } if (fptr->rbuf == NULL) { fptr->rbuf_off = 0; fptr->rbuf_len = 0; @@ -1224,10 +1219,8 @@ io_fillbuf(rb_io_t *fptr) } fptr->rbuf_off = 0; fptr->rbuf_len = (int)r; /* r should be <= rbuf_capa */ - if (r == 0) { - io_set_eof(fptr); + if (r == 0) return -1; /* EOF */ - } } return 0; } @@ -1533,10 +1526,7 @@ io_fread(VALUE str, long offset, rb_io_t *fptr) while (n > 0) { again: c = rb_read_internal(fptr->fd, RSTRING_PTR(str)+offset, n); - if (c == 0) { - io_set_eof(fptr); - break; - } + if (c == 0) break; if (c < 0) { if (rb_io_wait_readable(fptr->fd)) goto again; @@ -1870,9 +1860,6 @@ io_getpartial(int argc, VALUE *argv, VALUE io, int nonblock) rb_mod_sys_fail(rb_mWaitReadable, "read would block"); rb_sys_fail_path(fptr->pathv); } - else if (n == 0) { - io_set_eof(fptr); - } } rb_str_resize(str, n); @@ -3154,7 +3141,6 @@ rb_io_ungetbyte(VALUE io, VALUE b) GetOpenFile(io, fptr); rb_io_check_byte_readable(fptr); - io_unset_eof(fptr); if (NIL_P(b)) return Qnil; if (FIXNUM_P(b)) { char cc = FIX2INT(b); @@ -3191,7 +3177,6 @@ rb_io_ungetc(VALUE io, VALUE c) GetOpenFile(io, fptr); rb_io_check_char_readable(fptr); - io_unset_eof(fptr); if (NIL_P(c)) return Qnil; if (FIXNUM_P(c)) { int cc = FIX2INT(c); diff --git a/ruby.c b/ruby.c index 4dc0003e27..1a445ea6cd 100644 --- a/ruby.c +++ b/ruby.c @@ -1598,6 +1598,10 @@ load_file_internal(VALUE arg) else if (!NIL_P(c)) { rb_io_ungetbyte(f, c); } + else { + if (f != rb_stdin) rb_io_close(f); + f = Qnil; + } rb_vm_set_progname(rb_progname = opt->script_name); require_libraries(&opt->req_list); /* Why here? unnatural */ } @@ -1610,6 +1614,11 @@ load_file_internal(VALUE arg) else { enc = rb_usascii_encoding(); } + if (NIL_P(f)) { + f = rb_str_new(0, 0); + rb_enc_associate(f, enc); + return (VALUE)rb_parser_compile_string(parser, fname, f, line_start); + } rb_funcall(f, set_encoding, 2, rb_enc_from_encoding(enc), rb_str_new_cstr("-")); tree = rb_parser_compile_file(parser, fname, f, line_start); rb_funcall(f, set_encoding, 1, rb_parser_encoding(parser)); @@ -1619,9 +1628,6 @@ load_file_internal(VALUE arg) else if (f != rb_stdin) { rb_io_close(f); } - else { - rb_io_ungetbyte(f, Qnil); - } return (VALUE)tree; } diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index 5befa465a3..faa7c5a9df 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -421,4 +421,28 @@ class TestRubyOptions < Test::Unit::TestCase assert_in_out_err(["-we", "a=1"], "", [], ["-e:1: warning: assigned but unused variable - a"], feature3446) assert_in_out_err(["-we", "1.times do\n a=1\nend"], "", [], ["-e:2: warning: assigned but unused variable - a"], feature3446) end + + def test_script_from_stdin + begin + require 'pty' + require 'io/console' + rescue LoadError + return + end + require 'timeout' + result = nil + PTY.spawn(EnvUtil.rubybin) do |s, m| + m.print("\C-d") + assert_nothing_raised('[ruby-dev:37798]') do + Timeout.timeout(3) {s.read} + end + end + PTY.spawn(EnvUtil.rubybin) do |s, m| + m.print("$stdin.read; p $stdin.gets\n\C-d") + m.print("abc\n\C-d") + m.print("zzz\n") + result = s.read + end + assert_match(/zzz\r\n"zzz\\n"\r\n\z/, result, '[ruby-core:30910]') + end end -- cgit v1.2.3