* io.c (check_pipe_command): extracted from rb_f_open and rb_io_open.

(rb_f_open): use check_pipe_command.
  (rb_io_open): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@18784 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2008-08-23 01:45:49 +00:00
parent 341abd3f88
commit d2cb86c532
2 changed files with 27 additions and 7 deletions

View File

@ -1,3 +1,9 @@
Sat Aug 23 10:42:52 2008 Tanaka Akira <akr@fsij.org>
* io.c (check_pipe_command): extracted from rb_f_open and rb_io_open.
(rb_f_open): use check_pipe_command.
(rb_io_open): ditto.
Sat Aug 23 10:13:00 2008 Tanaka Akira <akr@fsij.org> Sat Aug 23 10:13:00 2008 Tanaka Akira <akr@fsij.org>
* io.c (read_all): fptr->enc2 is 0 if no conversion. * io.c (read_all): fptr->enc2 is 0 if no conversion.

28
io.c
View File

@ -4673,6 +4673,22 @@ rb_io_s_sysopen(int argc, VALUE *argv)
return INT2NUM(fd); return INT2NUM(fd);
} }
static VALUE
check_pipe_command(VALUE filename_or_command)
{
char *s = RSTRING_PTR(filename_or_command);
long l = RSTRING_LEN(filename_or_command);
char *e = s + l;
int chlen;
if (rb_enc_ascget(s, e, &chlen, rb_enc_get(filename_or_command)) == '|') {
VALUE cmd = rb_str_new(s+chlen, l-chlen);
OBJ_INFECT(cmd, filename_or_command);
return cmd;
}
return Qnil;
}
/* /*
* call-seq: * call-seq:
* open(path [, mode_enc [, perm]] ) => io or nil * open(path [, mode_enc [, perm]] ) => io or nil
@ -4798,10 +4814,9 @@ rb_f_open(int argc, VALUE *argv)
redirect = Qtrue; redirect = Qtrue;
} }
else { else {
char *str = StringValuePtr(tmp); VALUE cmd = check_pipe_command(tmp);
if (str && str[0] == '|') { if (!NIL_P(cmd)) {
argv[0] = rb_str_new(str+1, RSTRING_LEN(tmp)-1); argv[0] = cmd;
OBJ_INFECT(argv[0], tmp);
return rb_io_s_popen(argc, argv, rb_cIO); return rb_io_s_popen(argc, argv, rb_cIO);
} }
} }
@ -4821,13 +4836,12 @@ rb_f_open(int argc, VALUE *argv)
static VALUE static VALUE
rb_io_open(VALUE filename, VALUE mode, VALUE opt) rb_io_open(VALUE filename, VALUE mode, VALUE opt)
{ {
char *fname = RSTRING_PTR(filename); VALUE cmd;
int modenum, flags; int modenum, flags;
convconfig_t convconfig; convconfig_t convconfig;
rb_io_extract_modeenc(&mode, opt, &modenum, &flags, &convconfig); rb_io_extract_modeenc(&mode, opt, &modenum, &flags, &convconfig);
if (fname[0] == '|') { if (!NIL_P(cmd = check_pipe_command(filename))) {
VALUE cmd = rb_str_new2(fname+1);
return pipe_open_s(cmd, rb_io_modenum_mode(modenum), flags, &convconfig); return pipe_open_s(cmd, rb_io_modenum_mode(modenum), flags, &convconfig);
} }
else { else {