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-29 23:01:38 +00:00
|
|
|
@@rl_thread = nil
|
|
|
|
@@rl_pipes = nil
|
|
|
|
@@rl_prompt = ''
|
|
|
|
@@rl_history = true
|
|
|
|
|
|
|
|
#
|
|
|
|
# Enable readline mode by setting rl_usestdio to false
|
|
|
|
# Address the following items before switching:
|
|
|
|
#
|
|
|
|
# XXX: Find a way to emulate ^Z when in readline mode
|
|
|
|
# XXX: Create histories/tab completion for shells
|
|
|
|
# XXX: Capture remote shell prompt and use for readline
|
|
|
|
#
|
|
|
|
@@rl_usestdio = true
|
2006-07-29 20:03:05 +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)
|
|
|
|
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
|
2006-07-31 20:00:20 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Whether or not the input medium supports readline.
|
|
|
|
#
|
|
|
|
def supports_readline
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
#
|
|
|
|
# Start the readline thread
|
|
|
|
#
|
|
|
|
def readline_start
|
2006-07-29 23:01:38 +00:00
|
|
|
return if @@rl_usestdio
|
2006-07-27 22:28:19 +00:00
|
|
|
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-29 23:01:38 +00:00
|
|
|
return $stdin.sysread(len) if @@rl_usestdio
|
|
|
|
|
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()
|
2006-07-29 23:01:38 +00:00
|
|
|
return $stdin.gets() if @@rl_usestdio
|
|
|
|
|
2006-07-27 22:28:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Prompt-based getline using readline.
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
def pgets
|
2007-04-04 02:38:15 +00:00
|
|
|
|
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
|
|
|
|
|
2007-04-04 02:38:15 +00:00
|
|
|
output.prompting
|
|
|
|
line = ::Readline.readline(prompt, true)
|
|
|
|
output.prompting(false)
|
|
|
|
|
|
|
|
if line
|
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-29 23:01:38 +00:00
|
|
|
return $stdin if @@rl_usestdio
|
|
|
|
|
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
|