2005-07-14 22:45:10 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class implements standard input using readline against
|
|
|
|
# standard input. It supports tab completion.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Input::Readline < Rex::Ui::Text::Input
|
2008-06-27 19:57:28 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Initializes the readline-aware Input instance for text.
|
|
|
|
#
|
2005-07-14 22:45:10 +00:00
|
|
|
def initialize(tab_complete_proc = nil)
|
2009-10-02 14:17:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
if(not Object.const_defined?('Readline'))
|
|
|
|
begin
|
|
|
|
require 'readline'
|
|
|
|
rescue ::LoadError
|
|
|
|
require 'readline_compat'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self.extend(::Readline)
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
if (tab_complete_proc)
|
2005-11-23 22:12:49 +00:00
|
|
|
::Readline.basic_word_break_characters = "\x00"
|
2005-07-14 22:45:10 +00:00
|
|
|
::Readline.completion_proc = tab_complete_proc
|
2007-09-22 19:53:39 +00:00
|
|
|
@rl_saved_proc = tab_complete_proc
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
2006-07-31 20:00:20 +00:00
|
|
|
|
2007-09-22 19:53:39 +00:00
|
|
|
#
|
|
|
|
# Reattach the original completion proc
|
|
|
|
#
|
|
|
|
def reset_tab_completion(tab_complete_proc = nil)
|
|
|
|
::Readline.basic_word_break_characters = "\x00"
|
|
|
|
::Readline.completion_proc = tab_complete_proc || @rl_saved_proc
|
|
|
|
end
|
2008-06-04 22:56:25 +00:00
|
|
|
|
2006-07-31 20:00:20 +00:00
|
|
|
#
|
|
|
|
# Whether or not the input medium supports readline.
|
|
|
|
#
|
|
|
|
def supports_readline
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2005-12-07 03:40:09 +00:00
|
|
|
#
|
|
|
|
# Calls sysread on the standard input handle.
|
|
|
|
#
|
|
|
|
def sysread(len = 1)
|
2008-10-01 17:32:55 +00:00
|
|
|
begin
|
|
|
|
self.fd.sysread(len)
|
|
|
|
rescue ::Errno::EINTR
|
|
|
|
retry
|
|
|
|
end
|
2005-12-07 03:40:09 +00:00
|
|
|
end
|
2005-11-23 22:12:49 +00:00
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2008-06-04 22:56:25 +00:00
|
|
|
# Read a line from stdin
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
def gets()
|
2008-10-01 17:32:55 +00:00
|
|
|
begin
|
|
|
|
self.fd.gets()
|
|
|
|
rescue ::Errno::EINTR
|
|
|
|
retry
|
|
|
|
end
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2008-11-09 08:44:36 +00:00
|
|
|
# Stick readline into a low-priority thread so that the scheduler doesn't slow
|
|
|
|
# down other background threads. This is important when there are many active
|
|
|
|
# background jobs, such as when the user is running Karmetasploit
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def pgets
|
2008-06-27 19:57:28 +00:00
|
|
|
|
2008-11-09 08:44:36 +00:00
|
|
|
line = nil
|
|
|
|
orig = Thread.current.priority
|
|
|
|
|
|
|
|
begin
|
|
|
|
Thread.current.priority = -20
|
2008-06-05 00:13:33 +00:00
|
|
|
output.prompting
|
2008-06-05 00:15:40 +00:00
|
|
|
line = ::Readline.readline(prompt, true)
|
2009-10-02 16:17:27 +00:00
|
|
|
::Readline::HISTORY.pop if (line and line.empty?)
|
2008-11-09 08:44:36 +00:00
|
|
|
ensure
|
2009-06-02 23:36:58 +00:00
|
|
|
Thread.current.priority = orig || 0
|
2008-06-05 00:13:33 +00:00
|
|
|
end
|
|
|
|
|
2008-11-09 08:44:36 +00:00
|
|
|
line
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
# Returns the output pipe handle
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def fd
|
2008-06-04 22:56:25 +00:00
|
|
|
$stdin
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
2006-07-27 22:28:19 +00:00
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
# Indicates that this input medium as a shell builtin, no need
|
|
|
|
# to extend.
|
|
|
|
#
|
|
|
|
def intrinsic_shell?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The prompt that is to be displayed.
|
|
|
|
#
|
|
|
|
attr_accessor :prompt
|
|
|
|
#
|
|
|
|
# The output handle to use when displaying the prompt.
|
|
|
|
#
|
|
|
|
attr_accessor :output
|
2005-07-14 22:45:10 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2008-11-09 08:44:36 +00:00
|
|
|
end
|