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
b462ef45
Unverified
Commit
b462ef45
authored
Aug 07, 2016
by
ksss
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable option :in, :out, :err
parent
93d481d4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
2 deletions
+52
-2
mrblib/io.rb
mrblib/io.rb
+2
-2
src/io.c
src/io.c
+13
-0
test/io.rb
test/io.rb
+37
-0
No files found.
mrblib/io.rb
View file @
b462ef45
...
...
@@ -26,11 +26,11 @@ class IO
end
end
def
self
.
popen
(
command
,
mode
=
'r'
,
&
block
)
def
self
.
popen
(
command
,
mode
=
'r'
,
opts
=
{},
&
block
)
if
!
self
.
respond_to?
(
:_popen
)
raise
NotImplementedError
,
"popen is not supported on this platform"
end
io
=
self
.
_popen
(
command
,
mode
)
io
=
self
.
_popen
(
command
,
mode
,
opts
)
return
io
unless
block
begin
...
...
src/io.c
View file @
b462ef45
...
...
@@ -270,6 +270,7 @@ 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
;
...
...
@@ -315,6 +316,18 @@ 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
(
!
mrb_nil_p
(
opt_out
))
{
dup2
(
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt_out
)),
1
);
}
if
(
!
mrb_nil_p
(
opt_err
))
{
dup2
(
mrb_fixnum
(
mrb_io_fileno
(
mrb
,
opt_err
)),
2
);
}
if
(
flags
&
FMODE_READABLE
)
{
close
(
pr
[
0
]);
if
(
pr
[
1
]
!=
1
)
{
...
...
test/io.rb
View file @
b462ef45
...
...
@@ -393,6 +393,43 @@ assert('IO.popen') do
end
end
assert
(
'IO.popen with in option'
)
do
begin
IO
.
pipe
do
|
r
,
w
|
w
.
write
'hello'
w
.
close
assert_equal
"hello"
,
IO
.
popen
(
"cat"
,
"r"
,
in:
r
)
{
|
i
|
i
.
read
}
assert_equal
""
,
r
.
read
end
rescue
NotImplementedError
=>
e
skip
e
.
message
end
end
assert
(
'IO.popen with out option'
)
do
begin
IO
.
pipe
do
|
r
,
w
|
IO
.
popen
(
"echo 'hello'"
,
"w"
,
out:
w
)
{}
w
.
close
assert_equal
"hello
\n
"
,
r
.
read
end
rescue
NotImplementedError
=>
e
skip
e
.
message
end
end
assert
(
'IO.popen with err option'
)
do
begin
IO
.
pipe
do
|
r
,
w
|
assert_equal
""
,
IO
.
popen
(
"echo 'hello' 1>&2"
,
"r"
,
err:
w
)
{
|
i
|
i
.
read
}
w
.
close
assert_equal
"hello
\n
"
,
r
.
read
end
rescue
NotImplementedError
=>
e
skip
e
.
message
end
end
assert
(
'IO.read'
)
do
# empty file
fd
=
IO
.
sysopen
$mrbtest_io_wfname
,
"w"
...
...
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