1
# Copyright (C) 2010-2011 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""Unicode-compatible command-line splitter for all platforms.
19
The user-visible behaviour of this module is described in
20
configuring_bazaar.txt.
23
from __future__ import absolute_import
28
_whitespace_match = re.compile(u'\\s', re.UNICODE).match
31
class _PushbackSequence(object):
32
def __init__(self, orig):
33
self._iter = iter(orig)
34
self._pushback_buffer = []
37
if len(self._pushback_buffer) > 0:
38
return self._pushback_buffer.pop()
40
return next(self._iter)
44
def pushback(self, char):
45
self._pushback_buffer.append(char)
51
class _Whitespace(object):
52
def process(self, next_char, context):
53
if _whitespace_match(next_char):
54
if len(context.token) > 0:
58
elif next_char in context.allowed_quote_chars:
60
return _Quotes(next_char, self)
61
elif next_char == u'\\':
62
return _Backslash(self)
64
context.token.append(next_char)
68
class _Quotes(object):
69
def __init__(self, quote_char, exit_state):
70
self.quote_char = quote_char
71
self.exit_state = exit_state
73
def process(self, next_char, context):
74
if next_char == u'\\':
75
return _Backslash(self)
76
elif next_char == self.quote_char:
77
context.token.append(u'')
78
return self.exit_state
80
context.token.append(next_char)
84
class _Backslash(object):
85
# See http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx
86
def __init__(self, exit_state):
87
self.exit_state = exit_state
90
def process(self, next_char, context):
91
if next_char == u'\\':
94
elif next_char in context.allowed_quote_chars:
95
# 2N backslashes followed by a quote are N backslashes
96
context.token.append(u'\\' * (self.count // 2))
97
# 2N+1 backslashes follwed by a quote are N backslashes followed by
98
# the quote which should not be processed as the start or end of
100
if self.count % 2 == 1:
101
# odd number of \ escapes the quote
102
context.token.append(next_char)
104
# let exit_state handle next_char
105
context.seq.pushback(next_char)
107
return self.exit_state
109
# N backslashes not followed by a quote are just N backslashes
111
context.token.append(u'\\' * self.count)
113
# let exit_state handle next_char
114
context.seq.pushback(next_char)
115
return self.exit_state
117
def finish(self, context):
119
context.token.append(u'\\' * self.count)
123
def process(self, next_char, context):
124
if _whitespace_match(next_char):
126
elif next_char in context.allowed_quote_chars:
127
return _Quotes(next_char, self)
128
elif next_char == u'\\':
129
return _Backslash(self)
131
context.token.append(next_char)
135
class Splitter(object):
136
def __init__(self, command_line, single_quotes_allowed):
137
self.seq = _PushbackSequence(command_line)
138
self.allowed_quote_chars = u'"'
139
if single_quotes_allowed:
140
self.allowed_quote_chars += u"'"
146
quoted, token = self._get_token()
153
def _get_token(self):
156
state = _Whitespace()
157
for next_char in self.seq:
158
state = state.process(next_char, self)
161
if state is not None and not getattr(state, 'finish', None) is None:
163
result = u''.join(self.token)
164
if not self.quoted and result == '':
166
return self.quoted, result
169
def split(unsplit, single_quotes_allowed=True):
170
splitter = Splitter(unsplit, single_quotes_allowed=single_quotes_allowed)
171
return [arg for quoted, arg in splitter]