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.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/r2pipe.rb', line 8

def initialize(file = nil)
  @file = file
  if file == nil
    fdIn = ENV['R2PIPE_IN'].to_i
    fdOut = ENV['R2PIPE_OUT'].to_i
    if fdIn < 1 or fdOut < 1
      raise 'Cannot find R2PIPE_IN and R2PIPE_OUT environment variables'
    end
    @read = IO.new(fdIn, 'r')
    @write = IO.new(fdOut, 'w')
    @pid = -1
  else
    exec = "r2 -q0 #{Shellwords.shellescape file} 2>/dev/null"
    PTY.spawn(exec) do |read, write, pid|
      @read = read
      @write = write
      @pid = pid
      @read.gets("\0")
    end
  end
end

Instance Method Details

#cmd(str) ⇒ Object



30
31
32
33
34
# File 'lib/r2pipe.rb', line 30

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

#cmdj(str) ⇒ Object



36
37
38
# File 'lib/r2pipe.rb', line 36

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

#json(str) ⇒ Object



49
50
51
52
53
# File 'lib/r2pipe.rb', line 49

def json(str)
  if !str.nil?
    JSON.parse str.sub("\n", '').sub("\r", '')
  end
end

#quitObject



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

def quit
  cmd('q!')
  @read.close
  @write.close
  if @pid != -1
    ::Process.wait @pid
  end
end