Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1115889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:13:22+00:00 2026-05-17T03:13:22+00:00

I am writing a terminal emulator in ruby using the PTY library. /dev/tty0 is

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T03:13:23+00:00Added an answer on May 17, 2026 at 3:13 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a bit of code to run a shell script using process that
I am writing a simple shell script which requires a root privilege. Using the
I'm writing a C program that prints something on terminal using ncurses. It should
I'm writing a program in C++ using the Qt library. There is a symbolic
I'm currently writing a 2-player chess game for the terminal and I'd like to
I'm writing a tic tac toe program that plays throuh the terminal/console After Player
I am writing a Python program which runs a virtual terminal. Currently I am
I'm writing a Python program for Linux and the program basically uses the terminal
Writing a python program, and I came up with this error while using the
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.