A
Adam Skobi
[Note: parts of this message were removed to make it a legal post.]
Hi,
As an exercise in metaprogramming I decided to write a DSL shell. One of the
requirements was that it should be working on both - win32 and unix
platforms. I quickly encountered some problems with cmd.exe.
My first understanding was that I open a stream to cmd.exe and write/read it
synchronously. In linux it would look like this
IO.popen( "/bin/bash", "r+" ) do |cmd|
cmd.puts 'pwd'
puts cmd.gets
end
Everything's dandy. It works like a charm. When I try to create an
equivalent in win32:
IO.popen( "cmd.exe", "r+" ) do |cmd|
cmd.puts 'cd'
puts cmd.gets
end
This thing fails miserably. I can't really pinpoint what is the root cause
of it. I have found here on comp.lang.ruby two solutions. First one:
IO.popen( "cmd.exe", "r+" ) do |cmd|
Thread.new(cmd) do |io|
while line = io.gets
puts line
end
end
cmd.puts "dir"
cmd.puts "cd \\"
cmd.puts "dir"
sleep 3
end
It works but is sucks badly on many levels as you can see. Other one:
IO.popen("cmd.exe /c #{single_command}") do | pipe |
pipe.each_line { |line| puts line }
end
This thing is not so bad but there is a small problem. I want my DSL user to
be able to write such simple commands as
cd C:\
dir
The problem is although the both command will run correctly, the change of
current directory will be lost between the commands. I can think of some
crazy workarounds but I was wondering if there is a way for the cmd.exe to
behave normally. Any help would be appreciated.
Hi,
As an exercise in metaprogramming I decided to write a DSL shell. One of the
requirements was that it should be working on both - win32 and unix
platforms. I quickly encountered some problems with cmd.exe.
My first understanding was that I open a stream to cmd.exe and write/read it
synchronously. In linux it would look like this
IO.popen( "/bin/bash", "r+" ) do |cmd|
cmd.puts 'pwd'
puts cmd.gets
end
Everything's dandy. It works like a charm. When I try to create an
equivalent in win32:
IO.popen( "cmd.exe", "r+" ) do |cmd|
cmd.puts 'cd'
puts cmd.gets
end
This thing fails miserably. I can't really pinpoint what is the root cause
of it. I have found here on comp.lang.ruby two solutions. First one:
IO.popen( "cmd.exe", "r+" ) do |cmd|
Thread.new(cmd) do |io|
while line = io.gets
puts line
end
end
cmd.puts "dir"
cmd.puts "cd \\"
cmd.puts "dir"
sleep 3
end
It works but is sucks badly on many levels as you can see. Other one:
IO.popen("cmd.exe /c #{single_command}") do | pipe |
pipe.each_line { |line| puts line }
end
This thing is not so bad but there is a small problem. I want my DSL user to
be able to write such simple commands as
cd C:\
dir
The problem is although the both command will run correctly, the change of
current directory will be lost between the commands. I can think of some
crazy workarounds but I was wondering if there is a way for the cmd.exe to
behave normally. Any help would be appreciated.