1

I have two versions of a method, I encounter the same problem with both.

version 1

def read_char
  $stdin.raw!
  input = $stdin.getc.chr
  if input == "\e" then
     input << $stdin.read_nonblock(3) rescue nil
     input << $stdin.read_nonblock(2) rescue nil
  end
ensure
  $stdin.cooked!
  return input
 end

version 2

def read_char
  system("stty raw -echo") 
  input = $stdin.getc.chr
  if input == "\e" then
    input << $stdin.read_nonblock(3) rescue nil
    input << $stdin.read_nonblock(2) rescue nil
  end
ensure
  system("stty -raw echo")  
  return input
end

I have the same problem with both. Specifically they modify the behavior of the Kernel#system command.

As an example:

system("irb")
c=read_char
system("irb")

It is easy to see the behavior of system is changed and in the second version the inout is not drawn from the tty.

Note that this does not happen when you press any key, just a key that gets translated to an escape sequence, such as the cursor keys.

So how is this repaired?

PS

Someone suggested that I run system("strace -o logfile irb") in the second spot and check the read statements. Instead I straced both irbs and compared. The read statements were the same for all but the last one, and they were all to so's. The final read looked different:

1st system: read(0, "1\n", 1024)                    = 2
2nd system: read(0, 0x7ff805a53000, 1024)           = -1 EAGAIN (Resource temporarily unavailable)

0x7ff805a53000 is the address of a memory mapped region.

PPS

I am doing this on a linux system.

Community
  • 1
  • 1

0 Answers0