Skip to content

Commit 6d7579d

Browse files
committed
Support breaking commands into multiple lines
1 parent 6721b79 commit 6d7579d

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

lib/rex/ui/text/shell.rb

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil)
4646

4747
# Initialize the prompt
4848
self.init_prompt = prompt
49+
self.cont_prompt = ' > '
4950
self.prompt_char = prompt_char
5051

5152
self.histfile = histfile
@@ -185,17 +186,14 @@ def run(&block)
185186
self.init_prompt = input.prompt
186187
end
187188

188-
output.input = input
189-
line = input.pgets()
190-
output.input = nil
191-
log_output(input.prompt)
189+
line = get_input_line
192190

193191
# If a block was passed in, pass the line to it. If it returns true,
194192
# break out of the shell loop.
195193
if (block)
196194
break if (line == nil or block.call(line))
197195
elsif(input.eof? or line == nil)
198-
# If you have sessions active, this will give you a shot to exit gravefully
196+
# If you have sessions active, this will give you a shot to exit gracefully
199197
# If you really are ambitious, 2 eofs will kick this out
200198
self.stop_count += 1
201199
next if(self.stop_count > 1)
@@ -350,6 +348,37 @@ def print(msg='')
350348

351349
protected
352350

351+
#
352+
# Get a single line of input, following continuation directives as necessary.
353+
#
354+
def get_input_line
355+
line = "\\\n"
356+
prompt_needs_reset = false
357+
358+
while line =~ /(^|[^\\])\\\s*$/
359+
# Strip \ and all the trailing whitespace
360+
line.sub!(/\\\s*/, '')
361+
362+
if line.length > 0
363+
# Using update_prompt will overwrite the primary prompt
364+
input.prompt = output.update_prompt(self.cont_prompt)
365+
prompt_needs_reset = true
366+
end
367+
368+
output.input = input
369+
line << input.pgets()
370+
output.input = nil
371+
log_output(input.prompt)
372+
end
373+
374+
if prompt_needs_reset
375+
# The continuation prompt was used so reset the prompt
376+
update_prompt
377+
end
378+
379+
line
380+
end
381+
353382
#
354383
# Parse a line into an array of arguments.
355384
#
@@ -389,7 +418,7 @@ def log_output(buf)
389418
end
390419

391420
attr_writer :input, :output # :nodoc:
392-
attr_accessor :stop_flag, :init_prompt # :nodoc:
421+
attr_accessor :stop_flag, :init_prompt, :cont_prompt # :nodoc:
393422
attr_accessor :prompt # :nodoc:
394423
attr_accessor :prompt_char, :tab_complete_proc # :nodoc:
395424
attr_accessor :histfile # :nodoc:

0 commit comments

Comments
 (0)