2005-07-14 22:45:10 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
begin
|
|
|
|
require 'readline'
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class implements standard input using readline against
|
|
|
|
# standard input. It supports tab completion.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Input::Readline < Rex::Ui::Text::Input
|
|
|
|
include ::Readline
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
@@rl_thread = nil
|
|
|
|
@@rl_pipes = nil
|
|
|
|
@@rl_prompt = ''
|
2006-07-29 20:03:05 +00:00
|
|
|
@@rl_history = true;
|
|
|
|
|
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)
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|
2005-12-07 03:40:09 +00:00
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Start the readline thread
|
|
|
|
#
|
|
|
|
def readline_start
|
|
|
|
return if @@rl_thread
|
|
|
|
@@rl_pipes = Rex::Compat.pipe
|
|
|
|
@@rl_thread = ::Thread.new do
|
|
|
|
begin
|
|
|
|
while (line = ::Readline.readline(@@rl_prompt, @@rl_history))
|
|
|
|
@@rl_pipes[1].write(line+"\n")
|
|
|
|
@@rl_pipes[1].flush
|
|
|
|
end
|
|
|
|
rescue ::Exception => e
|
|
|
|
$stderr.puts "ERROR: readline thread: #{e.to_s} #{e.backtrace.to_s}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stop the readline thread
|
|
|
|
#
|
|
|
|
def readline_stop
|
|
|
|
# Stop the reader thread
|
|
|
|
if (@@rl_thread)
|
|
|
|
begin
|
|
|
|
@@rl_thread.kill
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
@@rl_thread = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
# Close the pipes
|
|
|
|
if (@@rl_pipes)
|
|
|
|
begin
|
|
|
|
@rl_pipes[0].close
|
|
|
|
@rl_pipes[1].close
|
|
|
|
rescue ::Exception
|
|
|
|
end
|
|
|
|
@@rl_pipes = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Status of the readline thread
|
|
|
|
#
|
|
|
|
def readline_status
|
|
|
|
@@rl_thread ? true : false
|
|
|
|
end
|
|
|
|
|
2005-12-07 03:40:09 +00:00
|
|
|
#
|
|
|
|
# Calls sysread on the standard input handle.
|
|
|
|
#
|
|
|
|
def sysread(len = 1)
|
2006-07-27 22:28:19 +00:00
|
|
|
if (! readline_status)
|
|
|
|
$stderr.puts "ERROR: sysread() called outside of thread mode: " + caller(1).to_s
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
@@rl_pipes[0].sysread(len)
|
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
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
# Fake gets using readline
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2006-07-27 22:28:19 +00:00
|
|
|
def gets()
|
|
|
|
if (! readline_status)
|
|
|
|
$stderr.puts "ERROR: gets() called outside of thread mode: " + caller(1).to_s
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
@@rl_pipes[0].gets
|
2005-07-16 07:32:11 +00:00
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Print a prompt and flush standard output.
|
|
|
|
#
|
|
|
|
def prompt(prompt)
|
|
|
|
_print_prompt(prompt)
|
|
|
|
return gets()
|
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Prompt-based getline using readline.
|
2006-07-27 22:28:19 +00:00
|
|
|
# XXX: Incompatible with thread mode
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def pgets
|
2006-07-27 22:28:19 +00:00
|
|
|
if (readline_status)
|
|
|
|
$stderr.puts "ERROR: pgets called inside of thread mode: " + caller(1).to_s
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
if ((line = ::Readline.readline(prompt, true)))
|
2005-07-14 22:45:10 +00:00
|
|
|
HISTORY.pop if (line.empty?)
|
|
|
|
return line + "\n"
|
|
|
|
else
|
|
|
|
eof = true
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
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
|
2006-07-27 22:28:19 +00:00
|
|
|
if (! readline_status)
|
|
|
|
$stderr.puts "fd called outside of thread mode: " + caller(1).to_s
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
|
|
|
@@rl_pipes[0]
|
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
|
|
|
|
end
|