qutebrowser/qutebrowser/utils/split.py

142 lines
5.0 KiB
Python
Raw Normal View History

2014-11-03 21:27:07 +01:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2014 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Our own fork of shlex.split with some added and removed features."""
from io import StringIO
2014-11-04 20:06:58 +01:00
from qutebrowser.utils import log
2014-11-03 21:27:07 +01:00
class ShellLexer:
2014-11-03 21:43:34 +01:00
"""A lexical analyzer class for simple shell-like syntaxes.
Based on Python's shlex, but cleaned up, removed some features, and added
some features useful for qutebrowser.
Attributes:
FIXME
"""
def __init__(self, s):
self.instream = StringIO(s)
2014-11-03 21:27:07 +01:00
self.whitespace = ' \t\r\n'
self.quotes = '\'"'
self.escape = '\\'
self.escapedquotes = '"'
self.state = ' '
self.token = ''
def read_token(self):
2014-11-03 21:43:34 +01:00
"""Read a raw token from the input stream."""
2014-11-03 21:27:07 +01:00
quoted = False
escapedstate = ' '
while True:
nextchar = self.instream.read(1)
2014-11-04 20:06:58 +01:00
log.shlexer.vdebug("in state {!r} I see character: {!r}".format(
self.state, nextchar))
2014-11-03 21:27:07 +01:00
if self.state is None:
2014-11-04 20:09:12 +01:00
self.token = None # past end of file
2014-11-03 21:27:07 +01:00
break
elif self.state == ' ':
if not nextchar:
self.state = None # end of file
break
elif nextchar in self.whitespace:
2014-11-04 20:06:58 +01:00
log.shlexer.vdebug("I see whitespace in whitespace state")
2014-11-03 21:35:47 +01:00
if self.token or quoted:
2014-11-03 21:27:07 +01:00
break # emit current token
else:
continue
2014-11-03 21:35:47 +01:00
elif nextchar in self.escape:
2014-11-03 21:27:07 +01:00
escapedstate = 'a'
self.state = nextchar
elif nextchar in self.quotes:
self.state = nextchar
else:
self.token = nextchar
self.state = 'a'
2014-11-03 21:27:07 +01:00
elif self.state in self.quotes:
quoted = True
if not nextchar: # end of file
2014-11-04 20:06:58 +01:00
log.shlexer.vdebug("I see EOF in quotes state")
self.state = None
break
2014-11-03 21:27:07 +01:00
if nextchar == self.state:
2014-11-03 21:35:47 +01:00
self.state = 'a'
elif (nextchar in self.escape and
self.state in self.escapedquotes):
2014-11-03 21:27:07 +01:00
escapedstate = self.state
self.state = nextchar
else:
self.token = self.token + nextchar
elif self.state in self.escape:
if not nextchar: # end of file
2014-11-04 20:06:58 +01:00
log.shlexer.vdebug("I see EOF in escape state")
self.token += self.state
self.state = None
break
2014-11-03 21:27:07 +01:00
# In posix shells, only the quote itself or the escape
# character may be escaped within quotes.
2014-11-04 20:13:31 +01:00
if (escapedstate in self.quotes and nextchar != self.state and
nextchar != escapedstate):
2014-11-03 21:27:07 +01:00
self.token = self.token + self.state
self.token = self.token + nextchar
self.state = escapedstate
elif self.state == 'a':
if not nextchar:
self.state = None # end of file
break
elif nextchar in self.whitespace:
2014-11-04 20:06:58 +01:00
log.shlexer.vdebug("shlex: I see whitespace in word state")
2014-11-03 21:27:07 +01:00
self.state = ' '
2014-11-03 21:35:47 +01:00
if self.token or quoted:
2014-11-03 21:27:07 +01:00
break # emit current token
else:
continue
2014-11-03 21:35:47 +01:00
elif nextchar in self.quotes:
2014-11-03 21:27:07 +01:00
self.state = nextchar
2014-11-03 21:35:47 +01:00
elif nextchar in self.escape:
2014-11-03 21:27:07 +01:00
escapedstate = 'a'
self.state = nextchar
else:
self.token = self.token + nextchar
2014-11-03 21:27:07 +01:00
result = self.token
self.token = ''
2014-11-03 21:35:47 +01:00
if not quoted and result == '':
2014-11-03 21:27:07 +01:00
result = None
2014-11-04 20:09:12 +01:00
log.shlexer.debug("token={!r}".format(result))
2014-11-03 21:27:07 +01:00
return result
def __iter__(self):
return self
def __next__(self):
2014-11-04 20:08:14 +01:00
token = self.read_token()
2014-11-04 20:09:12 +01:00
if token is None:
2014-11-03 21:27:07 +01:00
raise StopIteration
return token
def split(s):
"""Split a string via ShellLexer."""
lexer = ShellLexer(s)
return list(lexer)