On 6/16/05, Wybo Dekker <wybo / servalys.nl> wrote:
> Hi,
> 
> I've been on the list a few days ago with a readline-problem, for which
> there doesn't seem to be a quick fix. I'm now trying a dirty fix... but
> this seems to be a very stubborn problem.
> 
> Here is my original test program
> 
>         # rltest - test for readline
>         # run me like this: (i.e. operating on a file)
>         #
>         # ruby rltest rltest
>         #
>         # and all is fine. But run me like so: (i.e. operating on stdin)
>         #
>         # ruby rltest < rltest
>         #
>         # and see that I successfully count my own lines,
>         # then also print the "type return to quit" prompt,
>         # but finally seem to be looping in the readline function, and
>         # can only be stopped with ctrl-C,
>         # ( not even with ctrl-D - I'm not looking at the terminal)
> 
>         require 'readline'
>         include Readline
> 
>         # count lines in stdin (the mail message):
>         n=0; while gets: n+=1 end
>         puts "#{n} lines"
> 
>         readline("type return to quit: ")
>         puts "quitting..."
> 
> I tried to get around this problem by testing if input is from stdin,
> and if so, making a temporary copy of stdin and re-running the program
> on that copied file:
> 
>         file = ARGV.shift || ''
>         if file == ''
>            require 'tempfile'
>            f = Tempfile.new('vpp')
>            name = f.path
>            puts "Making temporary file: #{name}"
>            n = 0
>            while gets
>               f.puts
>               n += 1
>            end
>            f.close
>            puts "Wrote #{n} lines"
>            system($0, name)
>         else
>            require 'readline'
>            include Readline
>            puts "reading #{file}"
>            # count lines in stdin (the mail message):
>            n = 0
>            open(file).each do
>               n+=1
>            end
>            puts "Saw #{n} lines"
> 
>            readline("type return to quit: ")
>            puts "quitting..."
>         end
> 
> Running this with: ruby rltest rltest says:
> 
>         reading rltest
>         Saw 30 lines
>         type return to quit:
>         quitting...
> 
> But to my astonishment, running with: ruby rltest < rltest stays in a
> loop, like the original test program:
> 
>         Making temporary file: /data/tmp/vpp19009.0
>         Wrote 30 lines
>         reading /data/tmp/vpp19009.0
>         Saw 30 lines
>         type return to quit:
>         ./rltest:28:in `readline': Interrupt
>                 from ./rltest:28
> 
> Can anybody explain this?
> 
> --
> Wybo
> 
> 

Theory: readline reads from STDIN correct? using rltest < rltest means
that STDIN gets EOF, which means STDIN is closed which means readlien
can't read anything from it. I don't think you can have your cake and
eat it too in this case. If you want the interactive prompt your going
to have pass the file name on the command line instead of using IO
redirection. I'm a bit confused as to the purpose of this.