From 069283e349b2e8dd324446aa5c30253a0993f1a3 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 28 Apr 2014 00:20:57 +0200 Subject: =?UTF-8?q?Don=E2=80=99t=20escape=20backslashes=20unless=20we=20ar?= =?UTF-8?q?e=20inside=20a=20quoted=20string=20and=20the=20next=20char=20is?= =?UTF-8?q?=20a=20quote?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sadly, we can’t doctest stuff with backslashes because it drives doctest crazy. --- src/common.py | 2 ++ src/poezio_shlex.py | 21 ++++----------------- 2 files changed, 6 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/common.py b/src/common.py index e9fe05c8..20700258 100644 --- a/src/common.py +++ b/src/common.py @@ -319,6 +319,8 @@ def shell_split(st): ['toto', 'titi'] >>> shell_split('toto ""') ['toto', ''] + >>> shell_split('to"to titi "a" b') + ['to"to', 'titi', 'a', 'b'] >>> shell_split('"toto titi" toto ""') ['toto titi', 'toto', ''] >>> shell_split('toto "titi') diff --git a/src/poezio_shlex.py b/src/poezio_shlex.py index 086b0707..032baeee 100644 --- a/src/poezio_shlex.py +++ b/src/poezio_shlex.py @@ -102,6 +102,7 @@ class shlex(object): escapedstate = ' ' token_start = 0 token_end = -1 + # read one char from the stream at once while True: nextchar = self.instream.read(1) if nextchar == '\n': @@ -126,13 +127,6 @@ class shlex(object): break # emit current token else: continue - elif nextchar in self.commenters: - self.instream.readline() - self.lineno = self.lineno + 1 - elif self.posix and nextchar in self.escape: - token_start = self.instream.tell() - 1 - escapedstate = 'a' - self.state = nextchar elif nextchar in self.wordchars: token_start = self.instream.tell() - 1 self.token = nextchar @@ -181,10 +175,8 @@ class shlex(object): # XXX what error should be raised here? token_end = self.instream.tell() break - # In posix shells, only the quote itself or the escape - # character may be escaped within quotes. - if escapedstate in self.quotes and \ - nextchar != self.state and nextchar != escapedstate: + # only the quote may be escaped + if escapedstate in self.quotes and nextchar != escapedstate: self.token = self.token + self.state self.token = self.token + nextchar self.state = escapedstate @@ -202,13 +194,8 @@ class shlex(object): break # emit current token else: continue - elif self.posix and nextchar in self.quotes: - self.state = nextchar - elif self.posix and nextchar in self.escape: - escapedstate = 'a' - self.state = nextchar elif nextchar in self.wordchars or nextchar in self.quotes \ - or self.whitespace_split: + or self.whitespace_split: self.token = self.token + nextchar else: self.pushback.appendleft(nextchar) -- cgit v1.2.3