前田です。

今Linuxでしか使わないプログラムを書いてまして、sttyを呼ぶのが何と
なく嫌だったので、代りにioctlを使おうと思ったのですが、あまりに面
倒だったのでこんなクラスを書いてみました。
# というか、ほとんど定数定義してるだけですが。

    tbuf = TermIO.new(io)
    tbuf.iflag = 0
    tbuf.oflag = 0
    tbuf.lflag &= ~TermIO::ISIG
    tbuf.lflag &= ~TermIO::ICANON
    tbuf.lflag &= ~TermIO::XCASE
    tbuf.cflag |= TermIO::CS8
    tbuf.cflag &= ~TermIO::CRTSCTS
    tbuf.set(io)

移植性ゼロです(きっぱり)。
# PLATFORMを見るようにすれば少しはマシになるかもしれないけど、自
# 分ではやりたくないなあ:-p

class TermIO
  TERMIO_SIZE = 10
  TERMIO_FORMAT = "S4C2"

  TCGETA 	= 0x5405
  TCSETA 	= 0x5406
  TCSETSW	= 0x5403
  TCSETSF	= 0x5404
  
  # c_cc characters
  VINTR		= 0
  VQUIT		= 1
  VERASE	= 2
  VKILL		= 3
  VEOF		= 4
  VTIME		= 5
  VMIN		= 6
  VSWTC		= 7
  VSTART	= 8
  VSTOP		= 9
  VSUSP		= 10
  VEOL		= 11
  VREPRINT	= 12
  VDISCARD	= 13
  VWERASE	= 14
  VLNEXT	= 15
  VEOL2		= 16
  
  # c_iflag bits
  IGNBRK	= 0000001
  BRKINT	= 0000002
  IGNPAR	= 0000004
  PARMRK	= 0000010
  INPCK		= 0000020
  ISTRIP	= 0000040
  INLCR		= 0000100
  IGNCR		= 0000200
  ICRNL		= 0000400
  IUCLC		= 0001000
  IXON		= 0002000
  IXANY		= 0004000
  IXOFF		= 0010000
  IMAXBEL	= 0020000

  # c_oflag bits
  OPOST		= 0000001
  OLCUC		= 0000002
  ONLCR		= 0000004
  OCRNL		= 0000010
  ONOCR		= 0000020
  ONLRET	= 0000040
  OFILL		= 0000100
  OFDEL		= 0000200
  NLDLY		= 0000400
  NL0		= 0000000
  NL1		= 0000400
  CRDLY		= 0003000
  CR0		= 0000000
  CR1		= 0001000
  CR2		= 0002000
  CR3		= 0003000
  TABDLY	= 0014000
  TAB0		= 0000000
  TAB1		= 0004000
  TAB2		= 0010000
  TAB3		= 0014000
  XTABS		= 0014000
  BSDLY		= 0020000
  BS0		= 0000000
  BS1		= 0020000
  VTDLY		= 0040000
  VT0		= 0000000
  VT1		= 0040000
  FFDLY		= 0100000
  FF0		= 0000000
  FF1		= 0100000

  # c_cflag bit meaning
  CBAUD		= 0010017
  B0		= 0000000
  B50		= 0000001
  B75		= 0000002
  B110		= 0000003
  B134		= 0000004
  B150		= 0000005
  B200		= 0000006
  B300		= 0000007
  B600		= 0000010
  B1200		= 0000011
  B1800		= 0000012
  B2400		= 0000013
  B4800		= 0000014
  B9600		= 0000015
  B19200	= 0000016
  B38400	= 0000017
  EXTA		= B19200
  EXTB		= B38400
  CSIZE		= 0000060
  CS5		= 0000000
  CS6		= 0000020
  CS7		= 0000040
  CS8		= 0000060
  CSTOPB	= 0000100
  CREAD		= 0000200
  PARENB	= 0000400
  PARODD	= 0001000
  HUPCL		= 0002000
  CLOCAL	= 0004000
  CBAUDEX	= 0010000
  B57600	= 0010001
  B115200	= 0010002
  B230400	= 0010003
  B460800	= 0010004
  B500000	= 0010005
  B576000	= 0010006
  B921600	= 0010007
  B1000000	= 0010010
  B1152000	= 0010011
  B1500000	= 0010012
  B2000000	= 0010013
  B2500000	= 0010014
  B3000000	= 0010015
  B3500000	= 0010016
  B4000000	= 0010017
  CIBAUD	= 002003600000
  CMSPAR	= 010000000000
  CRTSCTS	= 020000000000
  
  # c_lflag bits
  ISIG		= 0000001
  ICANON	= 0000002
  XCASE		= 0000004
  ECHO		= 0000010
  ECHOE		= 0000020
  ECHOK		= 0000040
  ECHONL	= 0000100
  NOFLSH	= 0000200
  TOSTOP	= 0000400
  ECHOCTL	= 0001000
  ECHOPRT	= 0002000
  ECHOKE	= 0004000
  FLUSHO	= 0010000
  PENDIN	= 0040000
  IEXTEN	= 0100000

  def initialize(io)
    s = "\0" * TERMIO_SIZE
    io.ioctl(TCGETA, s)
    @array = s.unpack(TERMIO_FORMAT)
  end

  def to_s
    return @array.pack(TERMIO_FORMAT)
  end

  def set(io)
    io.ioctl(TCSETA, to_s)
  end

  def setsw(io)
    io.ioctl(TCSETSW, to_s)
  end

  def setsf(io)
    io.ioctl(TCSETSF, to_s)
  end

  def iflag
    return @array[0]
  end

  def iflag=(val)
    @array[0] = val
  end

  def oflag
    return @array[1]
  end

  def oflag=(val)
    @array[1] = val
  end

  def cflag
    return @array[2]
  end

  def cflag=(val)
    @array[2] = val
  end

  def lflag
    return @array[3]
  end

  def lflag=(val)
    @array[3] = val
  end

  def line
    return @array[4]
  end

  def line=(val)
    @array[4] = val
  end

  def cc
    return @array[5]
  end

  def cc=(val)
    @array[5] = val
  end
end

-- 
前田 修吾