* io.c (struct argf): packed ARGF stuffs.

* ruby.c (proc_options): use ruby_set_inplace_mode().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15663 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2008-03-01 08:59:04 +00:00
parent cce9eb092b
commit b8a733d0b8
3 changed files with 270 additions and 134 deletions

View File

@ -1,3 +1,9 @@
Sat Mar 1 17:59:01 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* io.c (struct argf): packed ARGF stuffs.
* ruby.c (proc_options): use ruby_set_inplace_mode().
Sat Mar 1 17:51:34 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> Sat Mar 1 17:51:34 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* lib/test/unit/collector/dir.rb (recursive_collect): do not always * lib/test/unit/collector/dir.rb (recursive_collect): do not always

371
io.c
View File

@ -125,14 +125,20 @@ static VALUE argf;
static ID id_write, id_read, id_getc, id_flush, id_encode; static ID id_write, id_read, id_getc, id_flush, id_encode;
extern char *ruby_inplace_mode;
struct timeval rb_time_interval(VALUE); struct timeval rb_time_interval(VALUE);
static VALUE filename, current_file; struct argf {
static int gets_lineno; VALUE filename, current_file;
static int init_p = 0, next_p = 0; int gets_lineno;
static VALUE lineno = INT2FIX(0); int init_p, next_p;
VALUE lineno;
VALUE argv;
char *inplace;
int binmode;
rb_encoding *enc, *enc2;
};
#define ARGF (*(struct argf *)DATA_PTR(argf))
#ifdef _STDIO_USES_IOSTREAM /* GNU libc */ #ifdef _STDIO_USES_IOSTREAM /* GNU libc */
# ifdef _IO_fpos_t # ifdef _IO_fpos_t
@ -235,7 +241,7 @@ rb_io_check_closed(rb_io_t *fptr)
static int io_fflush(rb_io_t *); static int io_fflush(rb_io_t *);
static VALUE VALUE
rb_io_get_io(VALUE io) rb_io_get_io(VALUE io)
{ {
return rb_convert_type(io, T_FILE, "IO", "to_io"); return rb_convert_type(io, T_FILE, "IO", "to_io");
@ -691,9 +697,8 @@ io_fwrite(VALUE str, rb_io_t *fptr)
fptr->wbuf_capa = 8192; fptr->wbuf_capa = 8192;
fptr->wbuf = ALLOC_N(char, fptr->wbuf_capa); fptr->wbuf = ALLOC_N(char, fptr->wbuf_capa);
} }
if ((fptr->mode & FMODE_SYNC) || if ((fptr->mode & (FMODE_SYNC|FMODE_TTY)) ||
(fptr->wbuf && fptr->wbuf_capa <= fptr->wbuf_len + len) || (fptr->wbuf && fptr->wbuf_capa <= fptr->wbuf_len + len)) {
(fptr->mode & FMODE_TTY)) {
/* xxx: use writev to avoid double write if available */ /* xxx: use writev to avoid double write if available */
if (fptr->wbuf_len && fptr->wbuf_len+len <= fptr->wbuf_capa) { if (fptr->wbuf_len && fptr->wbuf_len+len <= fptr->wbuf_capa) {
if (fptr->wbuf_capa < fptr->wbuf_off+fptr->wbuf_len+len) { if (fptr->wbuf_capa < fptr->wbuf_off+fptr->wbuf_len+len) {
@ -990,8 +995,8 @@ rb_io_rewind(VALUE io)
GetOpenFile(io, fptr); GetOpenFile(io, fptr);
if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path); if (io_seek(fptr, 0L, 0) < 0) rb_sys_fail(fptr->path);
if (io == current_file) { if (io == ARGF.current_file) {
gets_lineno -= fptr->lineno; ARGF.gets_lineno -= fptr->lineno;
} }
fptr->lineno = 0; fptr->lineno = 0;
@ -1841,7 +1846,7 @@ rb_io_getline_fast(rb_io_t *fptr)
str = io_enc_str(str, fptr); str = io_enc_str(str, fptr);
if (!fptr->enc2) ENC_CODERANGE_SET(str, cr); if (!fptr->enc2) ENC_CODERANGE_SET(str, cr);
fptr->lineno++; fptr->lineno++;
lineno = INT2FIX(fptr->lineno); ARGF.lineno = INT2FIX(fptr->lineno);
return str; return str;
} }
@ -1972,7 +1977,7 @@ rb_io_getline_1(VALUE rs, long limit, VALUE io)
if (!NIL_P(str)) { if (!NIL_P(str)) {
if (!nolimit) { if (!nolimit) {
fptr->lineno++; fptr->lineno++;
lineno = INT2FIX(fptr->lineno); ARGF.lineno = INT2FIX(fptr->lineno);
} }
} }
@ -2088,27 +2093,6 @@ rb_io_set_lineno(VALUE io, VALUE lineno)
return lineno; return lineno;
} }
static void
lineno_setter(VALUE val, ID id, VALUE *var)
{
gets_lineno = NUM2INT(val);
*var = INT2FIX(gets_lineno);
}
static VALUE
argf_set_lineno(VALUE argf, VALUE val)
{
gets_lineno = NUM2INT(val);
lineno = INT2FIX(gets_lineno);
return Qnil;
}
static VALUE
argf_lineno(void)
{
return lineno;
}
/* /*
* call-seq: * call-seq:
* ios.readline(sep=$/) => string * ios.readline(sep=$/) => string
@ -4727,7 +4711,8 @@ prep_stdio(FILE *f, int mode, VALUE klass, const char *path)
return io; return io;
} }
FILE *rb_io_stdio_file(rb_io_t *fptr) FILE *
rb_io_stdio_file(rb_io_t *fptr)
{ {
if (!fptr->stdio_file) { if (!fptr->stdio_file) {
fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_flags_mode(fptr->mode)); fptr->stdio_file = rb_fdopen(fptr->fd, rb_io_flags_mode(fptr->mode));
@ -4903,22 +4888,85 @@ rb_io_s_for_fd(int argc, VALUE *argv, VALUE klass)
return io; return io;
} }
static int argf_binmode = 0; static void
static rb_encoding *argf_enc, *argf_enc2; argf_mark(void *ptr)
{
struct argf *p = ptr;
rb_gc_mark(p->filename);
rb_gc_mark(p->current_file);
rb_gc_mark(p->lineno);
rb_gc_mark(p->argv);
}
static void
argf_free(void *ptr)
{
struct argf *p = ptr;
free(p->inplace);
}
static VALUE static VALUE
argf_forward(int argc, VALUE *argv) argf_alloc(VALUE klass)
{
struct argf *p;
VALUE argf = Data_Make_Struct(klass, struct argf, argf_mark, argf_free, p);
p->filename = Qnil;
p->current_file = Qnil;
p->lineno = Qnil;
p->argv = Qnil;
return argf;
}
#define filename ARGF.filename
#define current_file ARGF.current_file
#define gets_lineno ARGF.gets_lineno
#define init_p ARGF.init_p
#define next_p ARGF.next_p
#define lineno ARGF.lineno
#define ruby_inplace_mode ARGF.inplace
#define argf_binmode ARGF.binmode
#define argf_enc ARGF.enc
#define argf_enc2 ARGF.enc2
#define rb_argv ARGF.argv
static VALUE
argf_initialize(VALUE argf, VALUE argv)
{
rb_argv = argv;
return argf;
}
static VALUE
argf_set_lineno(VALUE argf, VALUE val)
{
gets_lineno = NUM2INT(val);
lineno = INT2FIX(gets_lineno);
return Qnil;
}
static VALUE
argf_lineno(VALUE argf)
{
return lineno;
}
static VALUE
argf_forward(int argc, VALUE *argv, VALUE argf)
{ {
return rb_funcall3(current_file, rb_frame_this_func(), argc, argv); return rb_funcall3(current_file, rb_frame_this_func(), argc, argv);
} }
#define next_argv() argf_next_argv(argf)
#define ARGF_GENERIC_INPUT_P() \
(current_file == rb_stdin && TYPE(current_file) != T_FILE)
#define ARGF_FORWARD(argc, argv) do {\ #define ARGF_FORWARD(argc, argv) do {\
if (current_file == rb_stdin && TYPE(current_file) != T_FILE)\ if (ARGF_GENERIC_INPUT_P())\
return argf_forward(argc, argv);\ return argf_forward(argc, argv, argf);\
} while (0) } while (0)
#define NEXT_ARGF_FORWARD(argc, argv) do {\ #define NEXT_ARGF_FORWARD(argc, argv) do {\
if (!next_argv()) return Qnil;\ if (!next_argv()) return Qnil;\
ARGF_FORWARD(argc, argv);\ ARGF_FORWARD(argc, argv);\
} while (0) } while (0)
static void static void
@ -4928,9 +4976,8 @@ argf_close(VALUE file)
} }
static int static int
next_argv(void) argf_next_argv(VALUE argf)
{ {
extern VALUE rb_argv;
char *fn; char *fn;
rb_io_t *fptr; rb_io_t *fptr;
int stdout_binmode = 0; int stdout_binmode = 0;
@ -4942,7 +4989,7 @@ next_argv(void)
} }
if (init_p == 0) { if (init_p == 0) {
if (RARRAY_LEN(rb_argv) > 0) { if (!NIL_P(rb_argv) && RARRAY_LEN(rb_argv) > 0) {
next_p = 1; next_p = 1;
} }
else { else {
@ -5062,7 +5109,7 @@ argf_getline(int argc, VALUE *argv)
retry: retry:
if (!next_argv()) return Qnil; if (!next_argv()) return Qnil;
if (current_file == rb_stdin && TYPE(current_file) != T_FILE) { if (ARGF_GENERIC_INPUT_P()) {
line = rb_funcall3(current_file, rb_intern("gets"), argc, argv); line = rb_funcall3(current_file, rb_intern("gets"), argc, argv);
} }
else { else {
@ -5085,6 +5132,22 @@ argf_getline(int argc, VALUE *argv)
return line; return line;
} }
static VALUE
argf_lineno_getter(ID id, VALUE *var)
{
VALUE argf = *var;
return lineno;
}
static void
argf_lineno_setter(VALUE val, ID id, VALUE *var)
{
VALUE argf = *var;
int n = NUM2INT(val);
gets_lineno = n;
lineno = INT2FIX(n);
}
/* /*
* call-seq: * call-seq:
* gets(sep=$/) => string or nil * gets(sep=$/) => string or nil
@ -6101,19 +6164,19 @@ rb_io_set_encoding(int argc, VALUE *argv, VALUE io)
} }
static VALUE static VALUE
argf_external_encoding(void) argf_external_encoding(VALUE argf)
{ {
return rb_io_external_encoding(current_file); return rb_io_external_encoding(current_file);
} }
static VALUE static VALUE
argf_internal_encoding(void) argf_internal_encoding(VALUE argf)
{ {
return rb_io_internal_encoding(current_file); return rb_io_internal_encoding(current_file);
} }
static VALUE static VALUE
argf_set_encoding(int argc, VALUE *argv, VALUE io) argf_set_encoding(int argc, VALUE *argv, VALUE argf)
{ {
rb_io_t *fptr; rb_io_t *fptr;
@ -6124,7 +6187,7 @@ argf_set_encoding(int argc, VALUE *argv, VALUE io)
GetOpenFile(current_file, fptr); GetOpenFile(current_file, fptr);
argf_enc = fptr->enc; argf_enc = fptr->enc;
argf_enc2 = fptr->enc2; argf_enc2 = fptr->enc2;
return io; return argf;
} }
static VALUE static VALUE
@ -6199,7 +6262,7 @@ argf_eof(void)
} }
static VALUE static VALUE
argf_read(int argc, VALUE *argv) argf_read(int argc, VALUE *argv, VALUE argf)
{ {
VALUE tmp, str, length; VALUE tmp, str, length;
long len = 0; long len = 0;
@ -6218,8 +6281,8 @@ argf_read(int argc, VALUE *argv)
if (!next_argv()) { if (!next_argv()) {
return str; return str;
} }
if (current_file == rb_stdin && TYPE(current_file) != T_FILE) { if (ARGF_GENERIC_INPUT_P()) {
tmp = argf_forward(argc, argv); tmp = argf_forward(argc, argv, argf);
} }
else { else {
tmp = io_read(argc, argv, current_file); tmp = io_read(argc, argv, current_file);
@ -6246,18 +6309,19 @@ argf_read(int argc, VALUE *argv)
struct argf_call_arg { struct argf_call_arg {
int argc; int argc;
VALUE *argv; VALUE *argv;
VALUE argf;
}; };
static VALUE static VALUE
argf_forward_call(VALUE arg) argf_forward_call(VALUE arg)
{ {
struct argf_call_arg *p = (struct argf_call_arg *)arg; struct argf_call_arg *p = (struct argf_call_arg *)arg;
argf_forward(p->argc, p->argv); argf_forward(p->argc, p->argv, p->argf);
return Qnil; return Qnil;
} }
static VALUE static VALUE
argf_readpartial(int argc, VALUE *argv) argf_readpartial(int argc, VALUE *argv, VALUE argf)
{ {
VALUE tmp, str, length; VALUE tmp, str, length;
@ -6271,10 +6335,11 @@ argf_readpartial(int argc, VALUE *argv)
rb_str_resize(str, 0); rb_str_resize(str, 0);
rb_eof_error(); rb_eof_error();
} }
if (current_file == rb_stdin && TYPE(current_file) != T_FILE) { if (ARGF_GENERIC_INPUT_P()) {
struct argf_call_arg arg; struct argf_call_arg arg;
arg.argc = argc; arg.argc = argc;
arg.argv = argv; arg.argv = argv;
arg.argf = argf;
tmp = rb_rescue2(argf_forward_call, (VALUE)&arg, tmp = rb_rescue2(argf_forward_call, (VALUE)&arg,
RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0); RUBY_METHOD_FUNC(0), Qnil, rb_eEOFError, (VALUE)0);
} }
@ -6303,7 +6368,7 @@ argf_getc(void)
retry: retry:
if (!next_argv()) return Qnil; if (!next_argv()) return Qnil;
if (current_file == rb_stdin && TYPE(current_file) != T_FILE) { if (ARGF_GENERIC_INPUT_P()) {
ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0); ch = rb_funcall3(current_file, rb_intern("getc"), 0, 0);
} }
else { else {
@ -6388,9 +6453,9 @@ argf_each_line(int argc, VALUE *argv, VALUE self)
} }
static VALUE static VALUE
argf_each_byte(VALUE self) argf_each_byte(VALUE argf)
{ {
RETURN_ENUMERATOR(self, 0, 0); RETURN_ENUMERATOR(argf, 0, 0);
for (;;) { for (;;) {
if (!next_argv()) return Qnil; if (!next_argv()) return Qnil;
rb_block_call(current_file, rb_intern("each_byte"), 0, 0, rb_yield, 0); rb_block_call(current_file, rb_intern("each_byte"), 0, 0, rb_yield, 0);
@ -6399,21 +6464,27 @@ argf_each_byte(VALUE self)
} }
static VALUE static VALUE
argf_filename(void) argf_filename(VALUE argf)
{ {
next_argv(); next_argv();
return filename; return filename;
} }
static VALUE static VALUE
argf_file(void) argf_filename_getter(ID id, VALUE *var)
{
return argf_filename(*var);
}
static VALUE
argf_file(VALUE argf)
{ {
next_argv(); next_argv();
return current_file; return current_file;
} }
static VALUE static VALUE
argf_binmode_m(void) argf_binmode_m(VALUE argf)
{ {
argf_binmode = 1; argf_binmode = 1;
next_argv(); next_argv();
@ -6423,7 +6494,7 @@ argf_binmode_m(void)
} }
static VALUE static VALUE
argf_skip(void) argf_skip(VALUE argf)
{ {
if (next_p != -1) { if (next_p != -1) {
argf_close(current_file); argf_close(current_file);
@ -6433,7 +6504,7 @@ argf_skip(void)
} }
static VALUE static VALUE
argf_close_m(void) argf_close_m(VALUE argf)
{ {
next_argv(); next_argv();
argf_close(current_file); argf_close(current_file);
@ -6445,7 +6516,7 @@ argf_close_m(void)
} }
static VALUE static VALUE
argf_closed(void) argf_closed(VALUE argf)
{ {
next_argv(); next_argv();
ARGF_FORWARD(0, 0); ARGF_FORWARD(0, 0);
@ -6453,32 +6524,80 @@ argf_closed(void)
} }
static VALUE static VALUE
argf_to_s(void) argf_to_s(VALUE argf)
{ {
return rb_str_new2("ARGF"); return rb_str_new2("ARGF");
} }
static VALUE static VALUE
opt_i_get(void) argf_inplace_mode_get(VALUE argf)
{ {
if (!ruby_inplace_mode) return Qnil; if (!ruby_inplace_mode) return Qnil;
return rb_str_new2(ruby_inplace_mode); return rb_str_new2(ruby_inplace_mode);
} }
static void static VALUE
opt_i_set(VALUE val) opt_i_get(ID id, VALUE *var)
{
return argf_inplace_mode_get(*var);
}
static VALUE
argf_inplace_mode_set(VALUE argf, VALUE val)
{ {
if (!RTEST(val)) { if (!RTEST(val)) {
if (ruby_inplace_mode) free(ruby_inplace_mode); if (ruby_inplace_mode) free(ruby_inplace_mode);
ruby_inplace_mode = 0; ruby_inplace_mode = 0;
return;
} }
StringValue(val); else {
StringValue(val);
if (ruby_inplace_mode) free(ruby_inplace_mode);
ruby_inplace_mode = 0;
ruby_inplace_mode = strdup(RSTRING_PTR(val));
}
return argf;
}
static void
opt_i_set(VALUE val, ID id, VALUE *var)
{
argf_inplace_mode_set(*var, val);
}
const char *
ruby_get_inplace_mode(void)
{
return ruby_inplace_mode;
}
void
ruby_set_inplace_mode(const char *suffix)
{
if (ruby_inplace_mode) free(ruby_inplace_mode); if (ruby_inplace_mode) free(ruby_inplace_mode);
ruby_inplace_mode = 0; ruby_inplace_mode = 0;
ruby_inplace_mode = strdup(StringValueCStr(val)); if (suffix) ruby_inplace_mode = strdup(suffix);
} }
static VALUE
argf_argv(VALUE argf)
{
return rb_argv;
}
static VALUE
argf_argv_getter(ID id, VALUE *var)
{
return argf_argv(*var);
}
VALUE
rb_get_argv(void)
{
return rb_argv;
}
#undef rb_argv
/* /*
* Class <code>IO</code> is the basis for all input and output in Ruby. * Class <code>IO</code> is the basis for all input and output in Ruby.
* An I/O stream may be <em>duplexed</em> (that is, bidirectional), and * An I/O stream may be <em>duplexed</em> (that is, bidirectional), and
@ -6558,6 +6677,7 @@ opt_i_set(VALUE val)
void void
Init_IO(void) Init_IO(void)
{ {
VALUE rb_cARGF;
#ifdef __CYGWIN__ #ifdef __CYGWIN__
#include <sys/cygwin.h> #include <sys/cygwin.h>
static struct __cygwin_perfile pf[] = static struct __cygwin_perfile pf[] =
@ -6627,7 +6747,6 @@ Init_IO(void)
rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter); rb_define_hooked_variable("$-0", &rb_rs, 0, rb_str_setter);
rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter); rb_define_hooked_variable("$\\", &rb_output_rs, 0, rb_str_setter);
rb_define_hooked_variable("$.", &lineno, 0, lineno_setter);
rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set); rb_define_virtual_variable("$_", rb_lastline_get, rb_lastline_set);
rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1); rb_define_method(rb_cIO, "initialize_copy", rb_io_init_copy, 1);
@ -6722,58 +6841,68 @@ Init_IO(void)
rb_define_global_const("STDOUT", rb_stdout); rb_define_global_const("STDOUT", rb_stdout);
rb_define_global_const("STDERR", rb_stderr); rb_define_global_const("STDERR", rb_stderr);
argf = argf_alloc(rb_cObject);
argf_initialize(argf, rb_ary_new());
rb_cARGF = rb_singleton_class(argf);
rb_include_module(rb_cARGF, rb_mEnumerable);
rb_define_method(rb_cARGF, "initialize", argf_initialize, 1);
rb_define_method(rb_cARGF, "to_s", argf_to_s, 0);
rb_define_method(rb_cARGF, "argv", argf_argv, 0);
rb_define_method(rb_cARGF, "fileno", argf_fileno, 0);
rb_define_method(rb_cARGF, "to_i", argf_fileno, 0);
rb_define_method(rb_cARGF, "to_io", argf_to_io, 0);
rb_define_method(rb_cARGF, "each", argf_each_line, -1);
rb_define_method(rb_cARGF, "each_line", argf_each_line, -1);
rb_define_method(rb_cARGF, "each_byte", argf_each_byte, 0);
rb_define_method(rb_cARGF, "read", argf_read, -1);
rb_define_method(rb_cARGF, "readpartial", argf_readpartial, -1);
rb_define_method(rb_cARGF, "readlines", rb_f_readlines, -1);
rb_define_method(rb_cARGF, "to_a", rb_f_readlines, -1);
rb_define_method(rb_cARGF, "gets", rb_f_gets, -1);
rb_define_method(rb_cARGF, "readline", rb_f_readline, -1);
rb_define_method(rb_cARGF, "getc", argf_getc, 0);
rb_define_method(rb_cARGF, "getbyte", argf_getbyte, 0);
rb_define_method(rb_cARGF, "readchar", argf_readchar, 0);
rb_define_method(rb_cARGF, "readbyte", argf_readbyte, 0);
rb_define_method(rb_cARGF, "tell", argf_tell, 0);
rb_define_method(rb_cARGF, "seek", argf_seek_m, -1);
rb_define_method(rb_cARGF, "rewind", argf_rewind, 0);
rb_define_method(rb_cARGF, "pos", argf_tell, 0);
rb_define_method(rb_cARGF, "pos=", argf_set_pos, 1);
rb_define_method(rb_cARGF, "eof", argf_eof, 0);
rb_define_method(rb_cARGF, "eof?", argf_eof, 0);
rb_define_method(rb_cARGF, "binmode", argf_binmode_m, 0);
rb_define_method(rb_cARGF, "filename", argf_filename, 0);
rb_define_method(rb_cARGF, "path", argf_filename, 0);
rb_define_method(rb_cARGF, "file", argf_file, 0);
rb_define_method(rb_cARGF, "skip", argf_skip, 0);
rb_define_method(rb_cARGF, "close", argf_close_m, 0);
rb_define_method(rb_cARGF, "closed?", argf_closed, 0);
rb_define_method(rb_cARGF, "lineno", argf_lineno, 0);
rb_define_method(rb_cARGF, "lineno=", argf_set_lineno, 1);
rb_define_method(rb_cARGF, "inplace_mode", argf_inplace_mode_get, 0);
rb_define_method(rb_cARGF, "inplace_mode=", argf_inplace_mode_set, 1);
rb_define_method(rb_cARGF, "external_encoding", argf_external_encoding, 0);
rb_define_method(rb_cARGF, "internal_encoding", argf_internal_encoding, 0);
rb_define_method(rb_cARGF, "set_encoding", argf_set_encoding, -1);
rb_define_readonly_variable("$<", &argf); rb_define_readonly_variable("$<", &argf);
argf = rb_obj_alloc(rb_cObject);
rb_extend_object(argf, rb_mEnumerable);
rb_define_global_const("ARGF", argf); rb_define_global_const("ARGF", argf);
rb_define_singleton_method(argf, "to_s", argf_to_s, 0); rb_define_hooked_variable("$.", &argf, argf_lineno_getter, argf_lineno_setter);
rb_define_hooked_variable("$FILENAME", &argf, argf_filename_getter, 0);
rb_define_singleton_method(argf, "fileno", argf_fileno, 0);
rb_define_singleton_method(argf, "to_i", argf_fileno, 0);
rb_define_singleton_method(argf, "to_io", argf_to_io, 0);
rb_define_singleton_method(argf, "each", argf_each_line, -1);
rb_define_singleton_method(argf, "each_line", argf_each_line, -1);
rb_define_singleton_method(argf, "each_byte", argf_each_byte, 0);
rb_define_singleton_method(argf, "read", argf_read, -1);
rb_define_singleton_method(argf, "readpartial", argf_readpartial, -1);
rb_define_singleton_method(argf, "readlines", rb_f_readlines, -1);
rb_define_singleton_method(argf, "to_a", rb_f_readlines, -1);
rb_define_singleton_method(argf, "gets", rb_f_gets, -1);
rb_define_singleton_method(argf, "readline", rb_f_readline, -1);
rb_define_singleton_method(argf, "getc", argf_getc, 0);
rb_define_singleton_method(argf, "getbyte", argf_getbyte, 0);
rb_define_singleton_method(argf, "readchar", argf_readchar, 0);
rb_define_singleton_method(argf, "readbyte", argf_readbyte, 0);
rb_define_singleton_method(argf, "tell", argf_tell, 0);
rb_define_singleton_method(argf, "seek", argf_seek_m, -1);
rb_define_singleton_method(argf, "rewind", argf_rewind, 0);
rb_define_singleton_method(argf, "pos", argf_tell, 0);
rb_define_singleton_method(argf, "pos=", argf_set_pos, 1);
rb_define_singleton_method(argf, "eof", argf_eof, 0);
rb_define_singleton_method(argf, "eof?", argf_eof, 0);
rb_define_singleton_method(argf, "binmode", argf_binmode_m, 0);
rb_define_singleton_method(argf, "filename", argf_filename, 0);
rb_define_singleton_method(argf, "path", argf_filename, 0);
rb_define_singleton_method(argf, "file", argf_file, 0);
rb_define_singleton_method(argf, "skip", argf_skip, 0);
rb_define_singleton_method(argf, "close", argf_close_m, 0);
rb_define_singleton_method(argf, "closed?", argf_closed, 0);
rb_define_singleton_method(argf, "lineno", argf_lineno, 0);
rb_define_singleton_method(argf, "lineno=", argf_set_lineno, 1);
rb_define_singleton_method(argf, "external_encoding", argf_external_encoding, 0);
rb_define_singleton_method(argf, "internal_encoding", argf_internal_encoding, 0);
rb_define_singleton_method(argf, "set_encoding", argf_set_encoding, -1);
rb_global_variable(&current_file);
rb_define_readonly_variable("$FILENAME", &filename);
filename = rb_str_new2("-"); filename = rb_str_new2("-");
rb_define_virtual_variable("$-i", opt_i_get, opt_i_set); rb_define_hooked_variable("$-i", &argf, opt_i_get, opt_i_set);
rb_define_hooked_variable("$*", &argf, argf_argv_getter, 0);
#if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__) #if defined (_WIN32) || defined(DJGPP) || defined(__CYGWIN__) || defined(__human68k__)
atexit(pipe_atexit); atexit(pipe_atexit);

27
ruby.c
View File

@ -63,7 +63,11 @@ VALUE ruby_verbose = Qfalse;
VALUE rb_parser_get_yydebug(VALUE); VALUE rb_parser_get_yydebug(VALUE);
VALUE rb_parser_set_yydebug(VALUE, VALUE); VALUE rb_parser_set_yydebug(VALUE, VALUE);
char *ruby_inplace_mode = 0; const char *ruby_get_inplace_mode(void);
void ruby_set_inplace_mode(const char *);
extern VALUE rb_get_argv(void);
#define rb_argv rb_get_argv()
#define DISABLE_BIT(bit) (1U << disable_##bit) #define DISABLE_BIT(bit) (1U << disable_##bit)
enum disable_flag_bits { enum disable_flag_bits {
@ -461,9 +465,10 @@ process_sflag(struct cmdline_options *opt)
if (opt->sflag) { if (opt->sflag) {
long n; long n;
VALUE *args; VALUE *args;
VALUE argv = rb_argv;
n = RARRAY_LEN(rb_argv); n = RARRAY_LEN(argv);
args = RARRAY_PTR(rb_argv); args = RARRAY_PTR(argv);
while (n > 0) { while (n > 0) {
VALUE v = *args++; VALUE v = *args++;
char *s = StringValuePtr(v); char *s = StringValuePtr(v);
@ -510,9 +515,9 @@ process_sflag(struct cmdline_options *opt)
} }
rb_gv_set(s, v); rb_gv_set(s, v);
} }
n = RARRAY_LEN(rb_argv) - n; n = RARRAY_LEN(argv) - n;
while (n--) { while (n--) {
rb_ary_shift(rb_argv); rb_ary_shift(argv);
} }
} }
opt->sflag = 0; opt->sflag = 0;
@ -720,9 +725,7 @@ proc_options(int argc, char **argv, struct cmdline_options *opt)
case 'i': case 'i':
forbid_setid("-i"); forbid_setid("-i");
if (ruby_inplace_mode) ruby_set_inplace_mode(s + 1);
free(ruby_inplace_mode);
ruby_inplace_mode = strdup(s + 1);
break; break;
case 'x': case 'x':
@ -1272,7 +1275,6 @@ rb_load_file(const char *fname)
} }
VALUE rb_progname; VALUE rb_progname;
VALUE rb_argv;
VALUE rb_argv0; VALUE rb_argv0;
#if !defined(PSTAT_SETCMD) && !defined(HAVE_SETPROCTITLE) #if !defined(PSTAT_SETCMD) && !defined(HAVE_SETPROCTITLE)
@ -1436,8 +1438,6 @@ ruby_prog_init(void)
rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0); rb_define_hooked_variable("$0", &rb_progname, 0, set_arg0);
rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0); rb_define_hooked_variable("$PROGRAM_NAME", &rb_progname, 0, set_arg0);
rb_define_readonly_variable("$*", &rb_argv);
rb_argv = rb_ary_new();
rb_define_global_const("ARGV", rb_argv); rb_define_global_const("ARGV", rb_argv);
rb_global_variable(&rb_argv0); rb_global_variable(&rb_argv0);
@ -1455,6 +1455,7 @@ void
ruby_set_argv(int argc, char **argv) ruby_set_argv(int argc, char **argv)
{ {
int i; int i;
VALUE av = rb_argv;
#if defined(USE_DLN_A_OUT) #if defined(USE_DLN_A_OUT)
if (origarg.argv) if (origarg.argv)
@ -1462,12 +1463,12 @@ ruby_set_argv(int argc, char **argv)
else else
dln_argv0 = argv[0]; dln_argv0 = argv[0];
#endif #endif
rb_ary_clear(rb_argv); rb_ary_clear(av);
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
VALUE arg = rb_tainted_str_new2(argv[i]); VALUE arg = rb_tainted_str_new2(argv[i]);
OBJ_FREEZE(arg); OBJ_FREEZE(arg);
rb_ary_push(rb_argv, arg); rb_ary_push(av, arg);
} }
} }