Class: Ronin::UI::Shell
- 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
-
#commands ⇒ Object
readonly
The commands available for the shell.
-
#name ⇒ Object
The shell name.
-
#prompt ⇒ Object
The shell prompt.
Class Method Summary collapse
-
.start(*arguments) {|shell, line| ... } ⇒ nil
Creates a new Shell object and starts it.
Instance Method Summary collapse
-
#call(line) ⇒ Object
Handles input for the shell.
-
#exit ⇒ Object
protected
Method which is called before exiting the shell.
-
#help ⇒ Object
protected
Prints the available commands and their arguments.
-
#initialize(options = {}) {|shell, line| ... } ⇒ Shell
constructor
Creates a new shell.
- #quit ⇒ Object protected
-
#start ⇒ Object
Starts the shell.
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.
72 73 74 75 76 77 78 79 80 |
# File 'lib/ronin/ui/shell.rb', line 72 def initialize(={},&block) @name = .fetch(:name,'') @prompt = .fetch(:prompt,DEFAULT_PROMPT) @commands = Set[:help, :exit] @commands += protected_methods.map { |name| name.to_sym } @input_handler = block end |
Instance Attribute Details
#commands ⇒ Object (readonly)
The commands available for the shell
45 46 47 |
# File 'lib/ronin/ui/shell.rb', line 45 def commands @commands end |
#prompt ⇒ Object
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.
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.
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 |
#exit ⇒ Object (protected)
Method which is called before exiting the shell.
179 180 |
# File 'lib/ronin/ui/shell.rb', line 179 def exit end |
#help ⇒ Object (protected)
Prints the available commands and their arguments.
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 |
#start ⇒ Object
Starts the shell.
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.}" end end end history_rollback.times { Readline::HISTORY.pop } return nil end |