module PTY

创建和管理伪终端(PTY)。另请参阅 en.wikipedia.org/wiki/Pseudo_terminal

PTY 允许您使用 ::open 分配新终端,或使用 ::spawn 启动一个带有特定命令的新终端。

示例

在此示例中,我们将更改 factor 命令的缓冲类型,假设 factor 使用 stdio 作为 stdout 缓冲。

如果使用 IO.pipe 而不是 PTY.open,则此代码将死锁,因为 factor 的 stdout 是全缓冲的。

# start by requiring the standard library PTY
require 'pty'

master, slave = PTY.open
read, write = IO.pipe
pid = spawn("factor", :in=>read, :out=>slave)
read.close     # we dont need the read
slave.close    # or the slave

# pipe "42" to the factor command
write.puts "42"
# output the response from factor
p master.gets #=> "42: 2 3 7\n"

# pipe "144" to factor and print out the response
write.puts "144"
p master.gets #=> "144: 2 2 2 2 3 3\n"
write.close # close the pipe

# The result of read operation when pty slave is closed is platform
# dependent.
ret = begin
        master.gets     # FreeBSD returns nil.
      rescue Errno::EIO # GNU/Linux raises EIO.
        nil
      end
p ret #=> nil

许可证

© Copyright 1998 by Akinori Ito.

此软件可出于此目的自由再分发,全部或部分,前提是所有该软件的副本以及其应用程序和衍生品中都包含此完整的版权声明。

此软件按“原样”提供,不附带任何明示或暗示的保证,包括但不限于对特定用途的适用性、适销性或使用此软件所得结果的保证。