From: Robert Klemme Date: 2008-10-17T18:49:30+09:00 Subject: Re: Damn you cmd.exe! On 17.10.2008 11:29, Adam Skobi wrote: > On Fri, Oct 17, 2008 at 10:39 AM, Robert Klemme > Native Windows Ruby. There are some quirkynesses with that implementation which I'm not familiar with. I use cygwin and it has served me well. >> 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. >>> >> Then what does "fails miserably" mean? Any error messages? > > Yes i do. It writes in my native language but it translates to something > along the lines: "The process tried to write to a non-existent stream" This works from cygwin: irb(main):012:0> IO.popen "cmd.exe", "r+" do |io| irb(main):013:1* t = Thread.new {io.each {|line| p line}} irb(main):014:1> io.puts "dir /w", "exit" irb(main):015:1> t.join irb(main):016:1> end "Microsoft Windows XP [Version 5.1.2600]\r\n" "(C) Copyright 1985-2001 Microsoft Corp.\r\n" "\r\n" "C:\\cygwin\\home\\Robert>dir /w\n" " Volume in Laufwerk C: hat keine Bezeichnung.\r\n" " Volumeseriennummer: 5C71-9E29\r\n" "\r\n" " Verzeichnis von C:\\cygwin\\home\\Robert\r\n" "\r\n" "[.] [..] .bashrc .bash_aliases .bash_history\r\n" ".bash_profile .inputrc .irbrc [bin] x.xml\r\n" " 7 Datei(en) 10.364 Bytes\r\n" " 3 Verzeichnis(se), 57.749.262.336 Bytes frei\r\n" "\r\n" "C:\\cygwin\\home\\Robert>exit\n" => # irb(main):017:0> >> Well, what is "normal" anyway? Not all operating systems are similar and >> just because Window's shell behaves differently does not make it abnormal. >> :-) > > You see, speaking simply, I want to have control on what 'goes in' and what > 'goes out'. I don't want to have a background thread reading everything from > cmd.exe as I wont be able to tell which command produced the output. I > haven't tested it thoroughly but I think it should look like in /bin/bash. > Simple stuff: > > shell.puts 'cd /home/me' > shell.puts 'pwd' > puts shell.gets # /home/me > > I am really bedazzled as to why it doesn't seem to work in win32. This solution is flawed as I have tried to explain earlier. Note that you will create deadlocks if you do not read the output stream properly (i.e. asynchronously). If you want to make sure you know which command produced which output you need to nevertheless read in the background and parse output for the prompt. Then you know when sub process output has finished and can attribute the output to commands properly. You also need proper synchronization for this as well. Note that you can also use File#expect. Insisting that the world should be different won't help because that has little effect on cmd.exe's implementation. You'll have to work with what you got. robert