On Thu, Aug 22, 2002 at 03:53:51AM +0900, Jeff Putsch wrote:
> The simple program I'm trying as a test is:
> 
>    #!/usr/sepp/bin/ruby
>    
>    require 'open3'
>    
>    def run_command
>      [stdin, out, err] = Open3.popen3("cat /etc/hosts")
>      while not (r=select([out,err])).nil?
>        yield r.inspect
>      end
>    end
>    
>    run_command { |l|
>     p l
>    }

Sorry, I botched the simple program. Here are two simple examples
illustrating my confusion:

  #!/usr/sepp/bin/ruby

  def run_command
    f = IO.popen("cat /etc/hosts") 

    while not (r=select([f])).nil?
      p r
      p r[0][0].gets.to_s
    end

  end

  run_command

and

  #!/usr/sepp/bin/ruby

  require 'open3'

  def run_command
    (stdin, out, err) = Open3.popen3("cat /etc/hosts") 

    while not (r=select([out,err])).nil?
      p r
      p "out: "+r[0][0].gets.to_s
      p "err: "+r[0][1].gets.to_s
    end

  end

  run_command

In both cases select returns that data is ALWAYS ready. The first example 
gives this output:

  [[#<IO:0x4a848>], [], []]
  "#\n"
  [[#<IO:0x4a848>], [], []]
  "# Internet host table\n"
  [[#<IO:0x4a848>], [], []]
  "#\n"
  [[#<IO:0x4a848>], [], []]
  "127.0.0.1\tlocalhost \n"
  [[#<IO:0x4a848>], [], []]
  "172.17.100.124\tblue.mxim.com blue\n"
  [[#<IO:0x4a848>], [], []]
  "\tparis.mxim.com           loghost\n"
  [[#<IO:0x4a848>], [], []]
  "172.17.100.5\tparis.mxim.com          loghost\n"
  [[#<IO:0x4a848>], [], []]
  ""
  [[#<IO:0x4a848>], [], []]
  ""
  [[#<IO:0x4a848>], [], []]
  ""

The second is similar, but shows the "err" never has any data.

So why is select doing this? What am I doing wrong?

Once I get select working, I'd like to be using the Open3.popen3() version
and detecting when the input data is done (a time out may not work), then
check the return code of the program called by Open3.popen3().

Any advice and/or pointers will be greatly appreciated.

Thanks,

Jeff.
-- 
Jeff Putsch                       Email: putsch / mxim.com
Maxim Integrated Products        Office: (503)547-2037
High Frequency CAD Engineering