Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mruby
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
mruby
Commits
d2c1f4d1
Unverified
Commit
d2c1f4d1
authored
Aug 07, 2016
by
ksss
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable Fixnum option
parent
b462ef45
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
10 deletions
+31
-10
src/io.c
src/io.c
+30
-10
test/io.rb
test/io.rb
+1
-0
No files found.
src/io.c
View file @
d2c1f4d1
...
...
@@ -264,13 +264,32 @@ mrb_io_alloc(mrb_state *mrb)
#endif
#ifndef _WIN32
static
int
option_to_fd
(
mrb_state
*
mrb
,
mrb_value
obj
,
const
char
*
key
)
{
mrb_value
opt
=
mrb_funcall
(
mrb
,
obj
,
"[]"
,
1
,
mrb_symbol_value
(
mrb_intern_static
(
mrb
,
key
,
strlen
(
key
))));
if
(
mrb_nil_p
(
opt
))
{
return
-
1
;
}
switch
(
mrb_type
(
opt
))
{
case
MRB_TT_DATA
:
/* IO */
return
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt
));
case
MRB_TT_FIXNUM
:
return
mrb_fixnum
(
opt
);
default:
mrb_raise
(
mrb
,
E_ARGUMENT_ERROR
,
"wrong exec redirect action"
);
break
;
}
return
-
1
;
/* never reached */
}
mrb_value
mrb_io_s_popen
(
mrb_state
*
mrb
,
mrb_value
klass
)
{
mrb_value
cmd
,
io
,
result
;
mrb_value
mode
=
mrb_str_new_cstr
(
mrb
,
"r"
);
mrb_value
opt
=
mrb_hash_new
(
mrb
);
mrb_value
opt_in
,
opt_out
,
opt_err
;
struct
mrb_io
*
fptr
;
const
char
*
pname
;
...
...
@@ -279,6 +298,7 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
int
pw
[
2
]
=
{
-
1
,
-
1
};
int
doexec
;
int
saved_errno
;
int
opt_in
,
opt_out
,
opt_err
;
mrb_get_args
(
mrb
,
"S|SH"
,
&
cmd
,
&
mode
,
&
opt
);
io
=
mrb_obj_value
(
mrb_data_object_alloc
(
mrb
,
mrb_class_ptr
(
klass
),
NULL
,
&
mrb_io_type
));
...
...
@@ -287,6 +307,9 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
flags
=
mrb_io_modestr_to_flags
(
mrb
,
mrb_string_value_cstr
(
mrb
,
&
mode
));
doexec
=
(
strcmp
(
"-"
,
pname
)
!=
0
);
opt_in
=
option_to_fd
(
mrb
,
opt
,
"in"
);
opt_out
=
option_to_fd
(
mrb
,
opt
,
"out"
);
opt_err
=
option_to_fd
(
mrb
,
opt
,
"err"
);
if
(
flags
&
FMODE_READABLE
)
{
if
(
pipe
(
pr
)
==
-
1
)
{
...
...
@@ -316,17 +339,14 @@ mrb_io_s_popen(mrb_state *mrb, mrb_value klass)
result
=
mrb_nil_value
();
switch
(
pid
=
fork
())
{
case
0
:
/* child */
opt_in
=
mrb_funcall
(
mrb
,
opt
,
"[]"
,
1
,
mrb_symbol_value
(
mrb_intern_lit
(
mrb
,
"in"
)));
opt_out
=
mrb_funcall
(
mrb
,
opt
,
"[]"
,
1
,
mrb_symbol_value
(
mrb_intern_lit
(
mrb
,
"out"
)));
opt_err
=
mrb_funcall
(
mrb
,
opt
,
"[]"
,
1
,
mrb_symbol_value
(
mrb_intern_lit
(
mrb
,
"err"
)));
if
(
!
mrb_nil_p
(
opt_in
))
{
dup2
(
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt_in
)),
0
);
if
(
opt_in
!=
-
1
)
{
dup2
(
opt_in
,
0
);
}
if
(
!
mrb_nil_p
(
opt_out
)
)
{
dup2
(
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt_out
))
,
1
);
if
(
opt_out
!=
-
1
)
{
dup2
(
opt_out
,
1
);
}
if
(
!
mrb_nil_p
(
opt_err
)
)
{
dup2
(
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt_err
))
,
2
);
if
(
opt_err
!=
-
1
)
{
dup2
(
opt_err
,
2
);
}
if
(
flags
&
FMODE_READABLE
)
{
close
(
pr
[
0
]);
...
...
test/io.rb
View file @
d2c1f4d1
...
...
@@ -401,6 +401,7 @@ assert('IO.popen with in option') do
assert_equal
"hello"
,
IO
.
popen
(
"cat"
,
"r"
,
in:
r
)
{
|
i
|
i
.
read
}
assert_equal
""
,
r
.
read
end
assert_raise
(
ArgumentError
)
{
IO
.
popen
(
"hello"
,
"r"
,
in:
Object
.
new
)
}
rescue
NotImplementedError
=>
e
skip
e
.
message
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment