sample/cbreak.rb


DEFINITIONS

This source file includes following functions.


   1  # ioctl example works on Sun
   2  
   3  CBREAK = 0x00000002
   4  ECHO = 0x00000008
   5  TIOCGETP = 0x40067408
   6  TIOCSETP = 0x80067409
   7  
   8  def cbreak ()
   9    set_cbreak(TRUE)
  10  end
  11  
  12  def cooked ()
  13    set_cbreak(FALSE)
  14  end
  15  
  16  def set_cbreak (on)
  17    tty = "\0" * 256
  18    STDIN.ioctl(TIOCGETP, tty)
  19    ttys = tty.unpack("C4 S")
  20    if on
  21      ttys[4] |= CBREAK
  22      ttys[4] &= ~ECHO
  23    else
  24      ttys[4] &= ~CBREAK
  25      ttys[4] |= ECHO
  26    end
  27    tty = ttys.pack("C4 S")
  28    STDIN.ioctl(TIOCSETP, tty)
  29  end
  30  cbreak();
  31  
  32  print("this is no-echo line: ");
  33  readline().print
  34  cooked();
  35  print("this is echo line: ");
  36  readline()