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."""
|
|
|
|
|
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):
|
2014-11-04 20:41:29 +01:00
|
|
|
self.iterator = iter(s)
|
2014-11-05 21:44:52 +01:00
|
|
|
self.whitespace = ' \t\r'
|
2014-11-03 21:27:07 +01:00
|
|
|
self.quotes = '\'"'
|
|
|
|
self.escape = '\\'
|
|
|
|
self.escapedquotes = '"'
|
2014-11-05 07:41:17 +01:00
|
|
|
self.keep = False
|
2014-11-03 21:27:07 +01:00
|
|
|
|
|
|
|
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 = ' '
|
2014-11-05 23:47:48 +01:00
|
|
|
token = ''
|
|
|
|
state = ' '
|
2014-11-03 21:27:07 +01:00
|
|
|
while True:
|
2014-11-04 20:41:29 +01:00
|
|
|
try:
|
|
|
|
nextchar = next(self.iterator)
|
|
|
|
except StopIteration:
|
|
|
|
nextchar = None
|
2014-11-04 20:06:58 +01:00
|
|
|
log.shlexer.vdebug("in state {!r} I see character: {!r}".format(
|
2014-11-05 23:47:48 +01:00
|
|
|
state, nextchar))
|
2014-11-05 23:48:57 +01:00
|
|
|
if state == ' ':
|
2014-11-04 20:41:29 +01:00
|
|
|
if nextchar is None:
|
2014-11-03 21:27:07 +01:00
|
|
|
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-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
if token or quoted:
|
2014-11-04 21:06:43 +01:00
|
|
|
# emit current token
|
|
|
|
break
|
2014-11-03 21:27:07 +01:00
|
|
|
else:
|
|
|
|
continue
|
2014-11-03 21:35:47 +01:00
|
|
|
elif nextchar in self.escape:
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
escapedstate = 'a'
|
2014-11-05 23:47:48 +01:00
|
|
|
state = nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
elif nextchar in self.quotes:
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
state = nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
else:
|
2014-11-05 23:47:48 +01:00
|
|
|
token = nextchar
|
|
|
|
state = 'a'
|
|
|
|
elif state in self.quotes:
|
2014-11-03 21:27:07 +01:00
|
|
|
quoted = True
|
2014-11-04 21:06:43 +01:00
|
|
|
if nextchar is None:
|
2014-11-04 20:06:58 +01:00
|
|
|
log.shlexer.vdebug("I see EOF in quotes state")
|
2014-11-04 20:24:42 +01:00
|
|
|
break
|
2014-11-05 23:47:48 +01:00
|
|
|
if nextchar == state:
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
state = 'a'
|
2014-11-03 21:35:47 +01:00
|
|
|
elif (nextchar in self.escape and
|
2014-11-05 23:47:48 +01:00
|
|
|
state in self.escapedquotes):
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
escapedstate = state
|
|
|
|
state = nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
else:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
elif state in self.escape:
|
2014-11-04 21:06:43 +01:00
|
|
|
if nextchar is None:
|
2014-11-04 20:06:58 +01:00
|
|
|
log.shlexer.vdebug("I see EOF in escape state")
|
2014-11-05 07:41:17 +01:00
|
|
|
if not self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += state
|
2014-11-04 20:24:42 +01:00
|
|
|
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-05 23:47:48 +01:00
|
|
|
if (escapedstate in self.quotes and nextchar != state and
|
2014-11-05 07:41:17 +01:00
|
|
|
nextchar != escapedstate and not self.keep):
|
2014-11-05 23:47:48 +01:00
|
|
|
token += state
|
|
|
|
token += nextchar
|
|
|
|
state = escapedstate
|
|
|
|
elif state == 'a':
|
2014-11-04 20:41:29 +01:00
|
|
|
if nextchar is None:
|
2014-11-03 21:27:07 +01:00
|
|
|
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-05 23:47:48 +01:00
|
|
|
state = ' '
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
if 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-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
state = nextchar
|
2014-11-03 21:35:47 +01:00
|
|
|
elif nextchar in self.escape:
|
2014-11-05 07:41:17 +01:00
|
|
|
if self.keep:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
escapedstate = 'a'
|
2014-11-05 23:47:48 +01:00
|
|
|
state = nextchar
|
2014-11-03 21:27:07 +01:00
|
|
|
else:
|
2014-11-05 23:47:48 +01:00
|
|
|
token += nextchar
|
|
|
|
if not quoted and token == '':
|
|
|
|
token = None
|
|
|
|
log.shlexer.vdebug("token={!r}".format(token))
|
|
|
|
return token
|
2014-11-03 21:27:07 +01:00
|
|
|
|
|
|
|
def __iter__(self):
|
2014-11-05 23:47:48 +01:00
|
|
|
while True:
|
|
|
|
token = self.read_token()
|
|
|
|
if token is None:
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
yield token
|
2014-11-03 21:27:07 +01:00
|
|
|
|
|
|
|
|
2014-11-05 07:41:17 +01:00
|
|
|
def split(s, keep=False):
|
|
|
|
"""Split a string via ShellLexer.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
keep: Whether to keep are special chars in the split output.
|
|
|
|
"""
|
2014-11-04 20:24:42 +01:00
|
|
|
lexer = ShellLexer(s)
|
2014-11-05 07:41:17 +01:00
|
|
|
lexer.keep = keep
|
2014-11-05 21:42:27 +01:00
|
|
|
tokens = list(lexer)
|
|
|
|
out = []
|
|
|
|
if tokens[0].isspace():
|
|
|
|
out.append(tokens[0] + tokens[1])
|
|
|
|
tokens = tokens[2:]
|
|
|
|
for t in tokens:
|
|
|
|
if t.isspace():
|
|
|
|
out[-1] += t
|
|
|
|
else:
|
|
|
|
out.append(t)
|
|
|
|
return out
|