Sha256: f9c1018617aed30e29e7fa4f913ea6b668d916fafdbb2cb64e64931b110281d5

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

module Ronin
  class Shell

    # Default shell prompt
    DEFAULT_PROMPT = '>'

    # Shell name to use
    attr_accessor :name

    # Shell prompt
    attr_accessor :prompt

    #
    # Creates a new Shell object with the given _options_. If a _block_ is
    # given it will be passed the newly created Shell object.
    #
    # _options_ may contain the following keys:
    # <tt>:name</tt>:: The name of the shell.
    # <tt>:prompt</tt>::The prompt to use for the shell.
    #
    def initialize(options={},&block)
      @name = options[:name]
      @prompt = (options[:prompt] || DEFAULT_PROMPT)

      block.call(self) if block
    end

    #
    # Creates and starts a new Shell object with the specified _options_.
    # If a _block_ is given, it will be passed the newly created Shell
    # object before it is started.
    #
    def self.start(options={},&block)
      self.new(options,&block).start
    end

    #
    # Starts the shell.
    #
    def start
      history_rollback = 0

      loop do
        line = Readline.readline("#{@name}#{@prompt} ")

        if line =~ /^\s*exit\s*$/
          exit_shell
          break
        else
          Readline::HISTORY << line
          history_rollback += 1

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

      history_rollback.times do
        Readline::HISTORY.pop
      end

      return nil
    end

    #
    # Default method that processes commands.
    #
    def process_command(command)
    end

    #
    # Default method that will be called when the shell is exited.
    #
    def exit_shell
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ronin-0.0.9 lib/ronin/shell.rb
ronin-0.1.0 lib/ronin/shell.rb
ronin-0.1.1 lib/ronin/shell.rb