Module: PTY

Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/samples/pty.rb

Defined Under Namespace

Modules: LibC

Class Method Summary collapse

Class Method Details

.getpty(*args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/samples/pty.rb', line 34

def self.getpty(*args)
  mfdp = Buffer.new :int
  name = Buffer.new 1024
  #
  # All the execv setup is done in the parent, since doing anything other than
  # execv in the child after fork is really flakey
  #
  exec_cmd, exec_args = build_args(args)
  pid = LibC.forkpty(mfdp, name, nil, nil)
  raise "forkpty failed: #{LibC.strerror(FFI.errno)}" if pid < 0
  if pid == 0
    LibC.execvp(exec_cmd, exec_args)
    exit 1
  end
  masterfd = mfdp.get_int(0)
  rfp = FFI::IO.for_fd(masterfd, "r")
  wfp = FFI::IO.for_fd(LibC.dup(masterfd), "w")
  if block_given?
    yield rfp, wfp, pid
    rfp.close unless rfp.closed?
    wfp.close unless wfp.closed?
  else
    [ rfp, wfp, pid ]
  end
end

.spawn(*args, &block) ⇒ Object



59
60
61
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/ffi-1.15.5/samples/pty.rb', line 59

def self.spawn(*args, &block)
  self.getpty("/bin/sh", "-c", args[0], &block)
end