Skip to content

Commit

Permalink
ignore quoted separators when splitting command lines
Browse files Browse the repository at this point in the history
Fixes #1454:
Can't attach a file if filename contains a semicolon
  • Loading branch information
pacien authored and pazz committed May 12, 2020
1 parent 8e96d50 commit d9533f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
16 changes: 4 additions & 12 deletions alot/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@
import magic


def split_commandline(s, comments=False, posix=True):
def split_commandline(s):
"""
splits semi-colon separated commandlines
splits semi-colon separated commandlines, ignoring quoted separators
"""
# shlex seems to remove unescaped quotes and backslashes
s = s.replace('\\', '\\\\')
s = s.replace('\'', '\\\'')
s = s.replace('\"', '\\\"')
lex = shlex.shlex(s, posix=posix)
lex.whitespace_split = True
lex.whitespace = ';'
if not comments:
lex.commenters = ''
return list(lex)
splitter = r'''((?:[^;"']|"(\\\\|\\"|[^"])*"|'(\\\\|\\'|[^'])*')+)'''
return re.split(splitter, s)[1::4]


def split_commandstring(cmdstring):
Expand Down
17 changes: 14 additions & 3 deletions tests/test_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,20 @@ def test_single(self):
expected = [base]
self._test(base, expected)

def test_unicode(self):
base = 'echo "foo";sleep 1'
expected = ['echo "foo"', 'sleep 1']
def test_quoted_separator(self):
base = '''echo "foo; bar";sleep 1 ; echo "foo; \\"bar; baz";''' \
''' echo "foo; bar \\\\" "baz"; test 'hi' 'hi';''' \
''' word; two words; empty '' '''
expected = [
'echo "foo; bar"',
'sleep 1 ',
' echo "foo; \\"bar; baz"',
' echo "foo; bar \\\\" "baz"',
" test 'hi' 'hi'",
' word',
' two words',
" empty '' "
]
self._test(base, expected)


Expand Down

0 comments on commit d9533f1

Please sign in to comment.