Talking with processes

D

Dario Linsky

Hi,

I am trying to open an external process that normally reads and writes from
stdin/to stdout. For opening a pipe to this process I am using IO.popen.

Reading each line from the pipe is no problem until it comes to a point where
the external process wants to get some data from stdin and it waits for input.

Now when I write something into the IO stream I need to call IO#close_write for
further reading. But since the pipe is closed now, I cannot write to it any
more.

For example:
-- snip --
pipe = IO.popen("ruby -r debug script.rb", "w+")
if pipe
while line = pipe.gets
line.chomp!
puts line
if line =~ /^script\.rb:\d+\:.+$/ # Regexp to find special lines
# Here I want to tell the debugger what to do and
# get further output to evaluate.
pipe.puts "next"
pipe.close_write
end
end
-- snap --

Is there any way to reopen the pipe at the position where I close it or is there
any way to avoid using IO#close_write so I can write more commands to the
debugger?

Greetings, Dario
 
A

Ara.T.Howard

Hi,

I am trying to open an external process that normally reads and writes from
stdin/to stdout. For opening a pipe to this process I am using IO.popen.

Reading each line from the pipe is no problem until it comes to a point where
the external process wants to get some data from stdin and it waits for input.

Now when I write something into the IO stream I need to call IO#close_write for
further reading. But since the pipe is closed now, I cannot write to it any
more.

For example:
-- snip --
pipe = IO.popen("ruby -r debug script.rb", "w+")
if pipe
while line = pipe.gets
line.chomp!
puts line
if line =~ /^script\.rb:\d+\:.+$/ # Regexp to find special lines
# Here I want to tell the debugger what to do and
# get further output to evaluate.
pipe.puts "next"
pipe.close_write
end
end
-- snap --

Is there any way to reopen the pipe at the position where I close it or is there
any way to avoid using IO#close_write so I can write more commands to the
debugger?

Greetings, Dario


try this:

http://raa.ruby-lang.org/list.rhtml?name=session

it will do that, and more. what you describe is EXACTLY why i wrote this,
when you need to send muliple commands to an external process and de-multiplex
the responses (output). you also get stderr.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
G

Guillaume Marcais

Does your lib work on Windows? Could it work with win32_popen?

Guillaume.

Hi,

I am trying to open an external process that normally reads and writes from
stdin/to stdout. For opening a pipe to this process I am using IO.popen.

Reading each line from the pipe is no problem until it comes to a point where
the external process wants to get some data from stdin and it waits for input.

Now when I write something into the IO stream I need to call IO#close_write for
further reading. But since the pipe is closed now, I cannot write to it any
more.

For example:
-- snip --
pipe = IO.popen("ruby -r debug script.rb", "w+")
if pipe
while line = pipe.gets
line.chomp!
puts line
if line =~ /^script\.rb:\d+\:.+$/ # Regexp to find special lines
# Here I want to tell the debugger what to do and
# get further output to evaluate.
pipe.puts "next"
pipe.close_write
end
end
-- snap --

Is there any way to reopen the pipe at the position where I close it or is there
any way to avoid using IO#close_write so I can write more commands to the
debugger?

Greetings, Dario


try this:

http://raa.ruby-lang.org/list.rhtml?name=session

it will do that, and more. what you describe is EXACTLY why i wrote this,
when you need to send muliple commands to an external process and de-multiplex
the responses (output). you also get stderr.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
A

Ara.T.Howard

Does your lib work on Windows? Could it work with win32_popen?

i would happy to try to make it do so, but currently it does not...

does win32_popen return fd's for stdin, stdout, AND stderr?

i don't have access to a window's machine but the source is pretty straight
forward - basically all you need to do is look for the call to open3 and put
something else in there... other than that it should work.

-a
Guillaume.

Hi,

I am trying to open an external process that normally reads and writes from
stdin/to stdout. For opening a pipe to this process I am using IO.popen.

Reading each line from the pipe is no problem until it comes to a point where
the external process wants to get some data from stdin and it waits for input.

Now when I write something into the IO stream I need to call IO#close_write for
further reading. But since the pipe is closed now, I cannot write to it any
more.

For example:
-- snip --
pipe = IO.popen("ruby -r debug script.rb", "w+")
if pipe
while line = pipe.gets
line.chomp!
puts line
if line =~ /^script\.rb:\d+\:.+$/ # Regexp to find special lines
# Here I want to tell the debugger what to do and
# get further output to evaluate.
pipe.puts "next"
pipe.close_write
end
end
-- snap --

Is there any way to reopen the pipe at the position where I close it or is there
any way to avoid using IO#close_write so I can write more commands to the
debugger?

Greetings, Dario


try this:

http://raa.ruby-lang.org/list.rhtml?name=session

it will do that, and more. what you describe is EXACTLY why i wrote this,
when you need to send muliple commands to an external process and de-multiplex
the responses (output). you also get stderr.

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================

--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top