Module: Expectr::Child
Overview
Public: The Expectr::Child Module defines the interface for interacting with child processes.
Defined Under Namespace
Modules: Errstr
Instance Method Summary collapse
-
#init_interface(args) ⇒ Object
Public: Initialize the Expectr interface, spawning a sub-process attaching to STDIN and STDOUT of the new process.
-
#kill!(signal = :TERM) ⇒ Object
Public: Send a signal to the running child process.
-
#output_loop ⇒ Object
Public: Read the output of the child process, force UTF-8 encoding, then append to the internal buffer and print to $stdout if appropriate.
-
#send(str) ⇒ Object
Public: Send input to the active child process.
-
#winsize ⇒ Object
Public: Return the PTY’s window size.
Methods included from Interface
Instance Method Details
#init_interface(args) ⇒ Object
Public: Initialize the Expectr interface, spawning a sub-process attaching to STDIN and STDOUT of the new process.
args - A Hash containing arguments to Expectr. Only :cmd is presently
used for this function.
:cmd - A String or File referencing the application to launch.
Returns nothing. Raises TypeError if args is anything other than String or File.
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/expectr/child.rb', line 20 def init_interface(args) cmd = args[:cmd] cmd = cmd.path if cmd.kind_of?(File) unless cmd.kind_of?(String) raise(TypeError, Errstr::STRING_FILE_EXPECTED) end @stdout,@stdin,@pid = PTY.spawn(cmd) @stdout.winsize = $stdout.winsize if $stdout.tty? end |
#kill!(signal = :TERM) ⇒ Object
Public: Send a signal to the running child process.
signal - Symbol, String or FixNum indicating the symbol to be sent to the
running process. (default: :TERM)
Returns a boolean indicating whether the process was successfully sent the signal. Raises ProcessError if the process is not running (@pid = 0).
40 41 42 43 44 45 |
# File 'lib/expectr/child.rb', line 40 def kill!(signal = :TERM) unless @pid > 0 raise(ProcessError, Errstr::PROCESS_NOT_RUNNING) end Process::kill(signal.to_sym, @pid) == 1 end |
#output_loop ⇒ Object
Public: Read the output of the child process, force UTF-8 encoding, then append to the internal buffer and print to $stdout if appropriate.
Returns nothing.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/expectr/child.rb', line 68 def output_loop while @pid > 0 unless select([@stdout], nil, nil, @timeout).nil? buf = '' begin @stdout.sysread(@buffer_size, buf) rescue Errno::EIO, EOFError #Application is not running @pid = 0 @thread.wakeup if @thread return end process_output(buf) end end end |
#send(str) ⇒ Object
Public: Send input to the active child process.
str - String to be sent.
Returns nothing. Raises Expectr::ProcessError if the process is not running (@pid = 0).
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/expectr/child.rb', line 53 def send(str) begin @stdin.syswrite str rescue Errno::EIO, EOFError # Application is not running @pid = 0 end unless @pid > 0 raise(Expectr::ProcessError, Errstr::PROCESS_GONE) end end |
#winsize ⇒ Object
Public: Return the PTY’s window size.
Returns a two-element Array (same as IO#winsize)
88 89 90 |
# File 'lib/expectr/child.rb', line 88 def winsize @stdout.winsize end |