Skip to content

Commit e2814d6

Browse files
committed
Align history manager to IRBs multiple backend library approach
1 parent 70e27f8 commit e2814d6

File tree

1 file changed

+16
-48
lines changed

1 file changed

+16
-48
lines changed

lib/rex/ui/text/shell/history_manager.rb

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -116,52 +116,30 @@ def clear_reline
116116

117117
def load_history_file(context)
118118
history_file = context[:history_file]
119-
case context[:input_library]
120-
when :readline
121-
return unless readline_available?
122-
123-
clear_readline
124-
if File.exist?(history_file)
125-
File.readlines(history_file).each do |e|
126-
::Readline::HISTORY << safe_undump(e.chomp)
127-
end
128-
end
129-
when :reline
130-
return unless reline_available?
131-
132-
clear_reline
133-
if File.exist?(history_file)
134-
File.readlines(history_file).each do |e|
135-
::Reline::HISTORY << safe_undump(e.chomp)
119+
history = context[:input_library] == :reline ? ::Reline::HISTORY : ::Readline::HISTORY
120+
121+
if File.exist?(history_file)
122+
File.open(history_file, 'r') do |f|
123+
f.each do |line|
124+
chomped_line = line.chomp
125+
if context[:input_library] == :reline && history.last&.end_with?("\\")
126+
history.last.delete_suffix!("\\")
127+
history.last << "\n" << chomped_line
128+
else
129+
history << chomped_line
130+
end
136131
end
137132
end
138133
end
139134
end
140135

141136
def store_history_file(context)
142-
cmds = []
143137
history_file = context[:history_file]
138+
history = context[:input_library] == :reline ? ::Reline::HISTORY : ::Readline::HISTORY
144139

145-
case context[:input_library]
146-
when :readline
147-
return unless readline_available?
148-
149-
history_diff = ::Readline::HISTORY.length < MAX_HISTORY ? ::Readline::HISTORY.length : MAX_HISTORY
150-
history_diff.times do
151-
entry = ::Readline::HISTORY.pop.dump
152-
cmds.push(entry) unless entry.nil?
153-
end
154-
when :reline
155-
return unless reline_available?
156-
157-
history_diff = ::Reline::HISTORY.length < MAX_HISTORY ? ::Reline::HISTORY.length : MAX_HISTORY
158-
history_diff.times do
159-
entry = ::Reline::HISTORY.pop.dump
160-
cmds.push(entry) unless entry.nil?
161-
end
162-
end
140+
history_to_save = history.map { |line| line.scrub.split("\n").join("\\\n") }
163141

164-
write_history_file(history_file, cmds)
142+
write_history_file(history_file, history_to_save)
165143
end
166144

167145
def switch_context(new_context, old_context=nil)
@@ -190,7 +168,7 @@ def write_history_file(history_file, cmds)
190168
cmds = event[:cmds]
191169

192170
File.open(history_file, 'wb+') do |f|
193-
f.puts(cmds.reverse)
171+
f.puts(cmds)
194172
end
195173

196174
rescue => e
@@ -205,16 +183,6 @@ def write_history_file(history_file, cmds)
205183
@write_queue << event
206184
@remaining_work << event
207185
end
208-
209-
# @param [String] dumped A string that has been previously dumped
210-
# @return [String] A string that is undumped if possible or the input if it can't be undumped.
211-
def safe_undump(dumped)
212-
begin
213-
dumped.undump
214-
rescue ::RuntimeError => _e
215-
dumped
216-
end
217-
end
218186
end
219187

220188
end

0 commit comments

Comments
 (0)