Class: Ronin::UI::Shell

Inherits:
Object show all
Includes:
Output::Helpers
Defined in:
lib/ronin/ui/shell.rb

Overview

Spawns a ReadLine powered interactive Shell.

Constant Summary collapse

DEFAULT_PROMPT =

Default shell prompt

'>'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Output::Helpers

#format_message, #print_debug, #print_error, #print_info, #print_warning, #printf, #putc, #puts, #write

Constructor Details

#initialize(options = {}) {|shell, line| ... } ⇒ Shell

Creates a new shell.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :name (String) — default: ''

    The shell-name to use before the prompt.

  • :prompt (String) — default: DEFAULT_PROMPT

    The prompt to use for the shell.

Yields:

  • (shell, line)

    The block that will be passed every command entered.

Yield Parameters:

  • shell (Shell)

    The shell to use for output.

  • line (String)

    The command entered into the shell.

Since:

  • 0.3.0



72
73
74
75
76
77
78
79
80
# File 'lib/ronin/ui/shell.rb', line 72

def initialize(options={},&block)
  @name     = options.fetch(:name,'')
  @prompt   = options.fetch(:prompt,DEFAULT_PROMPT)

  @commands = Set[:help, :exit]
  @commands += protected_methods.map { |name| name.to_sym }

  @input_handler = block
end

Instance Attribute Details

#commandsObject (readonly)

The commands available for the shell



45
46
47
# File 'lib/ronin/ui/shell.rb', line 45

def commands
  @commands
end

#nameObject

The shell name



39
40
41
# File 'lib/ronin/ui/shell.rb', line 39

def name
  @name
end

#promptObject

The shell prompt



42
43
44
# File 'lib/ronin/ui/shell.rb', line 42

def prompt
  @prompt
end

Class Method Details

.start(*arguments) {|shell, line| ... } ⇒ nil

Creates a new Shell object and starts it.

Examples:

Shell.start(:prompt => '$') { |shell,line| system(line) }

Parameters:

Yields:

  • (shell, line)

    The block that will be passed every command entered.

Yield Parameters:

  • shell (Shell)

    The shell to use for output.

  • line (String)

    The command entered into the shell.

Returns:

  • (nil)


102
103
104
# File 'lib/ronin/ui/shell.rb', line 102

def self.start(*arguments,&block)
  new(*arguments,&block).start
end

Instance Method Details

#call(line) ⇒ Object

Handles input for the shell.

Parameters:

  • line (String)

    A line of input received by the shell.

Since:

  • 0.3.0



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ronin/ui/shell.rb', line 148

def call(line)
  if @input_handler
    @input_handler.call(self,line)
  else
    arguments = line.split(/\s+/)
    command   = arguments.shift

    # ignore empty lines
    return false unless command

    command = command.to_sym

    # no explicitly calling handler
    return false if command == :handler

    unless @commands.include?(command)
      print_error "Invalid command: #{command}"
      return false
    end

    return send(command,*arguments)
  end
end

#exitObject (protected)

Method which is called before exiting the shell.

Since:

  • 0.3.0



179
180
# File 'lib/ronin/ui/shell.rb', line 179

def exit
end

#helpObject (protected)

Prints the available commands and their arguments.

Since:

  • 0.3.0



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/ronin/ui/shell.rb', line 196

def help
  puts "Available commands:"
  puts

  @commands.sort.each do |name|
    command_method = method(name)
    arguments = command_method.parameters.map do |param|
      case param[0]
      when :opt
        "[#{param[1]}]"
      when :rest
        "[#{param[1]} ...]"
      else
        param[1]
      end
    end

    puts "  #{name} #{arguments.join(' ')}"
  end
end

#quitObject (protected)

See Also:

Since:

  • 0.3.0



187
188
189
# File 'lib/ronin/ui/shell.rb', line 187

def quit
  exit
end

#startObject

Starts the shell.

Since:

  • 0.3.0



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/ronin/ui/shell.rb', line 111

def start
  history_rollback = 0

  loop do
    unless (raw_line = Readline.readline("#{name}#{prompt} "))
      break # user exited the shell
    end

    line = raw_line.strip

    if (line == 'exit' || line == 'quit')
      exit
      break
    elsif !(line.empty?)
      Readline::HISTORY << raw_line
      history_rollback += 1

      begin
        handler(line)
      rescue => e
        print_error "#{e.class.name}: #{e.message}"
      end
    end
  end

  history_rollback.times { Readline::HISTORY.pop }
  return nil
end