tags:

views:

66

answers:

1

I am writing a terminal emulator in ruby using the PTY library. /dev/tty0 is a device file connected to a keyboard. I am spawning the shell like this:

shell = PTY.spawn 'env TERM=ansi COLUMNS=63 LINES=21 sh -i < /dev/tty0'

It mostly works, but when a subprocess is started in the shell, shell[0] is not outputting the keyboard input to that subprocess. For example: When I send "cat\nasdf" through shell[1], "cat" comes back through shell[0] but "asdf" does not. Why is this happening, and how can I fix it?

Edit:
Here is my code. ChumbyScreen is an external module controlling the screen of the embedded device I am writing this for (it is called "Chumby"). The write method puts a character on the screen.

require 'pty'

def handle_escape(io)
  actions = 'ABCDEFGHJKSTfmnsulh'
  str, action = '', nil
  loop do
    c = io.read(1)
    if actions.include? c
      action = c
      break
    else
      str += c
    end
  end
  case action
  when 'J'
    ChumbyScreen.x = 0
  end
end

system '[ -e /dev/tty0 ] || mknod /dev/tty0 c 4 0'
shell = PTY.spawn 'env TERM=ansi COLUMNS=63 LINES=21 sh -i < /dev/tty0'

loop do
  c = shell[0].read(1)
  if c == "\e"
    c2 = shell[0].read(1)
    if c2 == '['
      handle_escape shell[0]
      next
    else
      c += c2
    end
  end
  ChumbyScreen.write c
end

After reading shodanex's answer, I tried this:

require 'pty'

def handle_escape(io)
  actions = 'ABCDEFGHJKSTfmnsulh'
  str, action = '', nil
  loop do
    c = io.read(1)
    if actions.include? c
      action = c
      break
    else
      str += c
    end
  end
  case action
  when 'J'
    ChumbyScreen.x = 0
  end
end

system '[ -e /dev/tty0 ] || mknod /dev/tty0 c 4 0'
shell = PTY.spawn 'env TERM=ansi COLUMNS=63 LINES=21 sh -i'

Thread.new do
  k = open '/dev/tty0', File::RDONLY
  loop do
    shell[1].write k.read(1)
  end
end.priority = 1

loop do
  c = shell[0].read(1)
  if c == "\e"
    c2 = shell[0].read(1)
    if c2 == '['
      handle_escape shell[0]
      next
    else
      c += c2
    end
  end
  ChumbyScreen.write c
end

It works, but characters I have typed do not show up until I press enter. It must be line buffered somehow - how do I get past this? Also Control-C and Control-D do nothing. I need them to send an eof and terminate a process.

+1  A: 

The tty input mode defaults to line input, so you won't see anything until you output a newline.

I suggest using strace to debug this kind of behaviour. This way you can see the system call, and for example see if you are blocked on a read waiting for more input and so on.

When you do not use '< /dev/tty0', it does work, right ? Basically, what you want is echoing of character. If you do the following :

shell = PTY.spawn 'env TERM=ansi COLUMNS=63 LINES=21 sh -i'
shell[1].puts("cat\nasdf")
s = shell[0].read(16)
puts s

And you strace the process using :

strace -ff -o test.log -e trace=read,write ./testr.rb

In the output you will see asdf twice. But if you look at the strace code, you see that the cat subprocess only writes asdf once, and it's parent process, ie the shell, never writes asdf.

So why is there two 'asdf' output ? Because the tty layer is doing local echo. So that when you type something in shell it goes to the pty, and the pty driver :

  • Write it to the slave side of the pseudo tty
  • Echoes it to the master side.

So what happens when you do sh -i </dev/tty0 ? The character coming from the keyboard are echoed to /dev/tty0, not to the output of the shell.

The shell is not doing any echo, the tty layer is, so what you want to do is the folowing (take it as pseudo code, I am not competent in ruby) :

# Emulate terminal behavior with pty
shell = PTY.spawn 'env TERM=ansi COLUMNS=63 LINES=21 sh -i'
keyboard = open(/dev/tty0)

input = keyboard.read()
shell[1].write(input)
puts shell[0].read(...)

Now, if you want something interactive, you will need to configure /dev/tty0 in raw mode, and use select to know when you can read without blocking, and when there is data available for output.

To configure tty in raw mode, you can try to use

stty -F /dev/tty0 -cooked
shodanex
But in working terminal emulators, when you run `cat` and type something and press enter, you can see the thing that you typed two times, one from keyboard input and one from `cat`. On mine, you can only see the output of `cat`.
Adrian
Your `stty` solution worked, thanks!!
Adrian