Module: Ronin::UI::Console::Commands Private

Defined in:
lib/ronin/ui/console/commands.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Allows for executing shell commands prefixed by a !.

Since:

  • 1.2.0

Constant Summary collapse

EXECUTABLES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Names and statuses of executables.

Since:

  • 1.2.0

Hash.new do |hash,key|
  hash[key] = Env.paths.any? do |dir|
    path = dir.join(key)

    (path.file? && path.executable?)
  end
end
BLACKLIST =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Blacklist of known commands that conflict with Ruby keywords.

Since:

  • 1.2.0

Set[
  '[', 'ap', 'begin', 'case', 'class', 'def', 'fail', 'false',
  'for', 'if', 'lambda', 'load', 'loop', 'module', 'p', 'pp',
  'print', 'proc', 'puts', 'raise', 'require', 'true', 'undef',
  'unless', 'until', 'warn', 'while'
]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cd(*arguments) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Equivalent of the cd command, using Dir.chdir.

Parameters:

  • arguments (Array<String>)

    The arguments of the command.

Returns:

  • (Boolean)

    Specifies whether the directory change was successful.

Since:

  • 1.2.0


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ronin/ui/console/commands.rb', line 86

def Commands.cd(*arguments)
  old_pwd = Dir.pwd

  new_cwd = if arguments.empty?
              Env.home
            elsif arguments.first == '-'
              unless ENV['OLDPWD']
                print_warning 'cd: OLDPWD not set'
                return false
              end

              ENV['OLDPWD']
            else
              arguments.first
            end

  Dir.chdir(new_cwd)
  ENV['OLDPWD'] = old_pwd
  return true
end

.edit(*arguments) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Edits a path and re-loads the code.

Parameters:

  • path (Array<String>)

    The path of the file to re-load.

Returns:

  • (Boolean)

    Specifies whether the code was successfully re-loaded.

Since:

  • 1.2.0


132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/ronin/ui/console/commands.rb', line 132

def Commands.edit(*arguments)
  path = arguments.first

  if Env.editor
    path ||= Tempfile.new(['ronin-console', '.rb']).path

    system(Env.editor,path) && load(path)
  else
    print_error "Please set the EDITOR env variable"
    return false
  end
end

.export(*arguments) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Equivalent of the export or set commands.

Parameters:

  • arguments (Array<String>)

    The arguments of the command.

Returns:

  • (true)

Since:

  • 1.2.0


115
116
117
118
119
120
121
# File 'lib/ronin/ui/console/commands.rb', line 115

def Commands.export(*arguments)
  arguments.each do |pair|
    name, value = pair.split('=',2)

    ENV[name] = value
  end
end

Instance Method Details

#executable?(name) ⇒ Boolean (protected)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Determines if an executable exists on the system.

Parameters:

  • name (String)

    The program name or path.

Returns:

  • (Boolean)

    Specifies whether the executable exists.

Since:

  • 1.2.0


156
157
158
# File 'lib/ronin/ui/console/commands.rb', line 156

def executable?(name)
  (File.file?(name) && File.executable?(name)) || EXECUTABLES[name]
end

#loop_eval(input) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Dynamically execute shell commands, instead of Ruby.

Parameters:

  • input (String)

    The input from the console.

Since:

  • 1.2.0


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ronin/ui/console/commands.rb', line 58

def loop_eval(input)
  if input[0,1] == '!'
    command = input[1..-1]
    name, arguments = command.split(' ')

    unless BLACKLIST.include?(name)
      if Commands.singleton_class.method_defined?(name)
        arguments ||= []

        return Commands.send(name,*arguments)
      elsif executable?(name)
        return system(command)
      end
    end
  end

  super(input)
end