1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Parse key sequence more strictly

This commit is contained in:
Nobuyoshi Nakada 2019-07-04 18:54:50 +09:00
parent 12e06d32f5
commit 265b9a0edf
No known key found for this signature in database
GPG key ID: 4BC7D6DF58D8DF60
2 changed files with 11 additions and 4 deletions

View file

@ -5,6 +5,8 @@ class Reline::Config
DEFAULT_PATH = '~/.inputrc' DEFAULT_PATH = '~/.inputrc'
KEYSEQ_PATTERN = /\\C-[A-Za-z_]|\\M-[0-9A-Za-z_]|\\C-M-[A-Za-z_]|\\M-C-[A-Za-z_]|\\e|\\[\\\"\'abdfnrtv]|\\\d{1,3}|\\x\h{1,2}|./
class InvalidInputrc < RuntimeError class InvalidInputrc < RuntimeError
attr_accessor :file, :lineno attr_accessor :file, :lineno
end end
@ -137,9 +139,10 @@ class Reline::Config
next next
end end
if line =~ /\s*(.*)\s*:\s*(.*)\s*$/ if line =~ /\s*("#{KEYSEQ_PATTERN}+")\s*:\s*(.*)\s*$/
key, func_name = $1, $2 key, func_name = $1, $2
keystroke, func = bind_key(key, func_name) keystroke, func = bind_key(key, func_name)
next unless keystroke
@additional_key_bindings[keystroke] = func @additional_key_bindings[keystroke] = func
end end
end end
@ -227,7 +230,7 @@ class Reline::Config
end end
def bind_key(key, func_name) def bind_key(key, func_name)
if key =~ /"(.*)"/ if key =~ /\A"(.*)"\z/
keyseq = parse_keyseq($1) keyseq = parse_keyseq($1)
else else
keyseq = nil keyseq = nil
@ -277,9 +280,8 @@ class Reline::Config
def parse_keyseq(str) def parse_keyseq(str)
# TODO: Control- and Meta- # TODO: Control- and Meta-
ret = [] ret = []
while str =~ /(\\C-[A-Za-z_]|\\M-[0-9A-Za-z_]|\\C-M-[A-Za-z_]|\\M-C-[A-Za-z_]|\\e|\\\\|\\"|\\'|\\a|\\b|\\d|\\f|\\n|\\r|\\t|\\v|\\\d{1,3}|\\x\h{1,2}|.)/ str.scan(KEYSEQ_PATTERN) do
ret << key_notation_to_code($&) ret << key_notation_to_code($&)
str = $'
end end
ret ret
end end

View file

@ -33,6 +33,11 @@ class Reline::Config::Test < Reline::TestCase
assert_not_include @config.key_bindings, nil assert_not_include @config.key_bindings, nil
end end
def test_invalid_keystroke
@config.read_lines(["a: error\n"])
assert_not_include @config.key_bindings, nil
end
def test_bind_key def test_bind_key
assert_equal ['input'.bytes, 'abcde'.bytes], @config.bind_key('"input"', '"abcde"') assert_equal ['input'.bytes, 'abcde'.bytes], @config.bind_key('"input"', '"abcde"')
end end