lib/ronin/ui/console.rb in ronin-0.3.0 vs lib/ronin/ui/console.rb in ronin-1.0.0.pre1
- old
+ new
@@ -1,9 +1,9 @@
#
# Ronin - A Ruby platform for exploit development and security research.
#
-# Copyright (c) 2006-2009 Hal Brodigan (postmodern.mod3 at gmail.com)
+# Copyright (c) 2006-2010 Hal Brodigan (postmodern.mod3 at gmail.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
@@ -16,197 +16,192 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
+require 'ronin/ui/output/helpers'
+require 'ronin/repository'
require 'ronin/config'
+require 'ronin/class_methods'
-require 'irb'
+require 'ripl'
+require 'ripl/completion'
+require 'ripl/multi_line'
+require 'ripl/auto_indent'
module Ronin
module UI
- module Console
- # Default prompt style.
- PROMPT = :SIMPLE
+ class Console
- # Default indentation mode.
- INDENT = true
+ # The history file for the Console session
+ HISTORY_FILE = File.join(Config::PATH,'console.log')
- # Default backtrace depth.
- BACKTRACE_DEPTH = 5
+ @@color = true
+ @@short_errors = true
+ @@auto_load = []
+ @@setup_blocks = []
- # Default completion mode.
- COMPLETION = true
-
#
- # @return [String]
- # The default Console prompt style.
+ # Determines whether colorized output will be enabled.
#
- def Console.prompt
- @@ronin_console_prompt ||= PROMPT
- end
-
+ # @return [Boolean]
+ # Specifies whether colorized output will be enabled.
#
- # Sets the default Console prompt style.
+ # @since 1.0.0
#
- # @param [String] style
- # The new Console prompt style to use.
- #
- def Console.prompt=(style)
- @@ronin_console_prompt = style
+ def Console.color?
+ @@color
end
#
- # @return [Boolean]
- # The default Console indent setting.
+ # Enables or disables colorized output.
#
- def Console.indent
- @@ronin_console_indent ||= INDENT
- end
-
- #
- # Sets the default Console indent setting.
- #
# @param [Boolean] mode
- # The new default Console indent setting.
+ # The new colorized output mode.
#
- # @example
- # Console.indent = false
- # # => false
+ # @return [Boolean]
+ # The colorized output mode.
#
- def Console.indent=(mode)
- @@ronin_console_indent = mode
- end
-
+ # @since 1.0.0
#
- # @return [Integer]
- # The default Console back-trace depth.
- #
- def Console.backtrace_depth
- @@ronin_console_backtrace_depth ||= BACKTRACE_DEPTH
+ def Console.color=(mode)
+ @@color = mode
end
#
- # Sets the default Console back-trace depth.
+ # Determines whenter one-line errors will be printed, instead of full
+ # backtraces.
#
- # @param [Integer] depth
- # The new default Console back-trace depth.
- #
- def Console.backtrace_depth=(depth)
- @@ronin_console_backtrace_depth = depth
- end
-
- #
# @return [Boolean]
- # The default Console tab-completion setting.
+ # The Console short-errors setting.
#
- def Console.completion
- @@ronin_console_completion ||= COMPLETION
+ # @since 1.0.0
+ #
+ def Console.short_errors?
+ @@short_errors
end
#
- # Sets the default Console tab-completion setting.
+ # Enables or disables the printing of one-lin errors.
#
# @param [Boolean] mode
- # The new default Console tab-completion setting.
+ # The new Console short-errors setting.
#
- # @example
- # Console.completion = false
- # # => false
+ # @return [Boolean]
+ # The Console short-errors setting.
#
- def Console.completion=(mode)
- @@ronin_console_completion = mode
+ # @since 1.0.0
+ #
+ def Console.short_errors=(mode)
+ @@short_errors = mode
end
#
+ # The list of files to load before starting the Console.
+ #
# @return [Array]
# The files to require when the Console starts.
#
def Console.auto_load
- @@ronin_console_auto_load ||= []
+ @@auto_load
end
#
# Adds a block to be ran from within the Console after it is
# started.
#
# @yield []
# The block to be ran from within the Console.
#
def Console.setup(&block)
- Console.setup_blocks << block if block
+ @@setup_blocks << block if block
end
#
# Starts a Console.
#
- # @param [String] script
- # The path to a script to run within the Console.
+ # @param [Hash{Symbol => Object}] variables
+ # Instance variable names and values to set within the console.
#
# @yield []
# The block to be ran within the Console, after it has been setup.
#
+ # @return [Console]
+ # The instance context the Console ran within.
+ #
# @example
# Console.start
+ # # >>
#
# @example
- # console.start { @var = 'hello' }
+ # Console.start(:var => 'hello')
+ # # >> @var
+ # # # => "hello"
#
- def Console.start(script=nil,&block)
- IRB.setup(script)
+ # @example
+ # Console.start { @var = 'hello' }
+ # # >> @var
+ # # # => "hello"
+ #
+ def Console.start(variables={},&block)
+ require 'ripl/color_result' if @@color
+ require 'ripl/short_errors' if @@short_errors
- # configure IRB
- IRB.conf[:IRB_NAME] = 'ronin'
- IRB.conf[:PROMPT_MODE] = Console.prompt
- IRB.conf[:AUTO_INDENT] = Console.indent
- IRB.conf[:BACK_TRACE_LIMIT] = Console.backtrace_depth
+ require 'ronin'
+ require 'pp'
- irb = IRB::Irb.new(nil,script)
+ # activates all installed or added repositories
+ Repository.activate!
- # configure the IRB context
- irb.context.main.instance_eval do
- require 'ronin'
- require 'ronin/ui/output'
+ # append the current directory to $LOAD_PATH for Ruby 1.9.
+ $LOAD_PATH << '.' unless $LOAD_PATH.include?('.')
- # include the output helpers
- include Ronin::UI::Output::Helpers
+ # require any of the auto-load paths
+ @@auto_load.each { |path| require path }
- # setup irb completion
- require 'irb/completion' if Ronin::UI::Console.completion
+ context = class << self.new; self; end
- # require any of the auto-load paths
- Ronin::UI::Console.auto_load.each do |path|
- require path
- end
-
- include Ronin
+ # populate instance variables
+ variables.each do |name,value|
+ context.instance_variable_set("@#{name}".to_sym,value)
end
# run any setup-blocks
- Console.setup_blocks.each do |setup_block|
- irb.context.main.instance_eval(&setup_block)
+ @@setup_blocks.each do |setup_block|
+ context.instance_eval(&setup_block)
end
# run the supplied configuration block is given
- irb.context.main.instance_eval(&block) if block
+ context.instance_eval(&block) if block
- IRB.conf[:MAIN_CONTEXT] = irb.context
+ # Start the Ripl console
+ Ripl.start(
+ :argv => [],
+ :name => 'ronin',
+ :binding => context.instance_eval { binding },
+ :history => HISTORY_FILE
+ )
- trap('SIGINT') { irb.signal_handle }
- catch(:IRB_EXIT) { irb.eval_input }
-
- putc "\n"
- return nil
+ return context
end
- protected
+ class << self
+ include Ronin
+ include Ronin::ClassMethods
+ include Ronin::UI::Output::Helpers
- #
- # @return [Array]
- # The blocks to be ran from within the Console after it is started.
- #
- def Console.setup_blocks
- @@console_setup_blocks ||= []
+ #
+ # Inspects the console.
+ #
+ # @return [String]
+ # The inspected console.
+ #
+ # @since 1.0.0
+ #
+ def inspect
+ "#<Ronin::UI::Console>"
+ end
end
+
end
end
end