Skip to content

Commit

Permalink
Align history manager to IRBs multiple backend library approach
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Mar 26, 2024
1 parent 70e27f8 commit e2814d6
Showing 1 changed file with 16 additions and 48 deletions.
64 changes: 16 additions & 48 deletions lib/rex/ui/text/shell/history_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,52 +116,30 @@ def clear_reline

def load_history_file(context)
history_file = context[:history_file]
case context[:input_library]
when :readline
return unless readline_available?

clear_readline
if File.exist?(history_file)
File.readlines(history_file).each do |e|
::Readline::HISTORY << safe_undump(e.chomp)
end
end
when :reline
return unless reline_available?

clear_reline
if File.exist?(history_file)
File.readlines(history_file).each do |e|
::Reline::HISTORY << safe_undump(e.chomp)
history = context[:input_library] == :reline ? ::Reline::HISTORY : ::Readline::HISTORY

if File.exist?(history_file)
File.open(history_file, 'r') do |f|
f.each do |line|
chomped_line = line.chomp
if context[:input_library] == :reline && history.last&.end_with?("\\")
history.last.delete_suffix!("\\")
history.last << "\n" << chomped_line
else
history << chomped_line
end
end
end
end
end

def store_history_file(context)
cmds = []
history_file = context[:history_file]
history = context[:input_library] == :reline ? ::Reline::HISTORY : ::Readline::HISTORY

case context[:input_library]
when :readline
return unless readline_available?

history_diff = ::Readline::HISTORY.length < MAX_HISTORY ? ::Readline::HISTORY.length : MAX_HISTORY
history_diff.times do
entry = ::Readline::HISTORY.pop.dump
cmds.push(entry) unless entry.nil?
end
when :reline
return unless reline_available?

history_diff = ::Reline::HISTORY.length < MAX_HISTORY ? ::Reline::HISTORY.length : MAX_HISTORY
history_diff.times do
entry = ::Reline::HISTORY.pop.dump
cmds.push(entry) unless entry.nil?
end
end
history_to_save = history.map { |line| line.scrub.split("\n").join("\\\n") }

write_history_file(history_file, cmds)
write_history_file(history_file, history_to_save)
end

def switch_context(new_context, old_context=nil)
Expand Down Expand Up @@ -190,7 +168,7 @@ def write_history_file(history_file, cmds)
cmds = event[:cmds]

File.open(history_file, 'wb+') do |f|
f.puts(cmds.reverse)
f.puts(cmds)
end

rescue => e
Expand All @@ -205,16 +183,6 @@ def write_history_file(history_file, cmds)
@write_queue << event
@remaining_work << event
end

# @param [String] dumped A string that has been previously dumped
# @return [String] A string that is undumped if possible or the input if it can't be undumped.
def safe_undump(dumped)
begin
dumped.undump
rescue ::RuntimeError => _e
dumped
end
end
end

end
Expand Down

0 comments on commit e2814d6

Please sign in to comment.