From: "Ara.T.Howard" Date: 2005-06-18T06:26:06+09:00 Subject: Re: stubborn program using readline - Perl finally beaten On Sat, 18 Jun 2005, Wybo Dekker wrote: > On Sat, 18 Jun 2005, Ara.T.Howard wrote: > >> put another way - you want the program to ALWAYS read from EITHER a file or >> stdin and you ALWAYS want to interactively (eg. from a tty) run commands after >> reading the file - so why not just say that in the code? >> >> # >> # program which does the above >> # >> jib:~ > cat a.rb >> require 'readline' >> include Readline >> >> inpath = ARGV.shift >> infile = inpath ? open(inpath) : STDIN >> >> # count lines in stdin (the mail message): >> n=0; while infile.gets: n+=1 end >> puts "#{n} lines" >> >> # always get commands interactively >> STDIN.reopen '/dev/tty' unless STDIN.tty? >> readline("type return to quit: ") >> puts "quitting..." >> >> you could take other approaches with dup'ing and forking but this might work >> for you. > > This did it!! > The essential line is: STDIN.reopen '/dev/tty' unless STDIN.tty? > and I would say that this should actually be done in Readline at the first > call of readline. > I rewrote my Perl program showlines: i think it's a bit more complicated than that because then you could not do readline_program.rb < commands.txt which would be bad. i'd have to look into perl module to see what they do - but i suspect it would also give some suprising behaviour under certain conditions too... it's just too tough to imagine all the combinations of tty/stdin one might want in a program... but maybe not. cheers. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================