Class: R2Pipe

Inherits:
Object
  • Object
show all
Defined in:
lib/r2pipe.rb

Overview

R2Pipe is an easy way to communicate with an r2 core through ruby

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ R2Pipe

Returns a new instance of R2Pipe.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/r2pipe.rb', line 11

def initialize(file = nil)
  @file = file
  if file.nil?
    fd_in, fd_out = getfds
    @read = IO.new(fd_in, 'r')
    @write = IO.new(fd_out, 'w')
    @pid = -1
  else
    execute file
  end
end

Instance Method Details

#cmd(str) ⇒ Object



23
24
25
26
27
# File 'lib/r2pipe.rb', line 23

def cmd(str)
  @write.print "#{str}\n"
  @write.flush
  @read.gets("\0")[0..-2]
end

#cmdj(str) ⇒ Object



29
30
31
# File 'lib/r2pipe.rb', line 29

def cmdj(str)
  json(cmd(str))
end

#json(str) ⇒ Object



42
43
44
45
46
# File 'lib/r2pipe.rb', line 42

def json(str)
  return if str.nil?

  JSON.parse str.sub("\n", '').sub("\r", '')
end

#quitObject



33
34
35
36
37
38
39
40
# File 'lib/r2pipe.rb', line 33

def quit
  cmd('q!')
  @read.close
  @write.close
  return if @pid == -1

  ::Process.wait @pid
end