~bzr-pqm/bzr/bzr.dev

5646.1.2 by Martin Pool
Add brief user documentation of command line splitting
1
# Copyright (C) 2010-2011 Canonical Ltd
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
2
#
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.
7
#
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.
12
#
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
16
5646.1.2 by Martin Pool
Add brief user documentation of command line splitting
17
"""Unicode-compatible command-line splitter for all platforms.
18
19
The user-visible behaviour of this module is described in
20
configuring_bazaar.txt.
21
"""
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
22
23
import re
24
25
26
_whitespace_match = re.compile(u'\s', re.UNICODE).match
27
28
29
class _PushbackSequence(object):
30
    def __init__(self, orig):
31
        self._iter = iter(orig)
32
        self._pushback_buffer = []
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
33
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
34
    def next(self):
35
        if len(self._pushback_buffer) > 0:
36
            return self._pushback_buffer.pop()
37
        else:
38
            return self._iter.next()
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
39
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
40
    def pushback(self, char):
41
        self._pushback_buffer.append(char)
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
42
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
43
    def __iter__(self):
44
        return self
45
46
47
class _Whitespace(object):
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
48
    def process(self, next_char, context):
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
49
        if _whitespace_match(next_char):
50
            if len(context.token) > 0:
51
                return None
52
            else:
53
                return self
4913.5.22 by Gordon Tyler
Tweaked quote handling.
54
        elif next_char in context.allowed_quote_chars:
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
55
            context.quoted = True
56
            return _Quotes(next_char, self)
57
        elif next_char == u'\\':
58
            return _Backslash(self)
59
        else:
60
            context.token.append(next_char)
61
            return _Word()
62
63
64
class _Quotes(object):
65
    def __init__(self, quote_char, exit_state):
66
        self.quote_char = quote_char
67
        self.exit_state = exit_state
68
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
69
    def process(self, next_char, context):
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
70
        if next_char == u'\\':
71
            return _Backslash(self)
72
        elif next_char == self.quote_char:
73
            return self.exit_state
74
        else:
75
            context.token.append(next_char)
76
            return self
77
78
79
class _Backslash(object):
80
    # See http://msdn.microsoft.com/en-us/library/bb776391(VS.85).aspx
81
    def __init__(self, exit_state):
82
        self.exit_state = exit_state
83
        self.count = 1
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
84
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
85
    def process(self, next_char, context):
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
86
        if next_char == u'\\':
87
            self.count += 1
88
            return self
4913.5.22 by Gordon Tyler
Tweaked quote handling.
89
        elif next_char in context.allowed_quote_chars:
90
            # 2N backslashes followed by a quote are N backslashes
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
91
            context.token.append(u'\\' * (self.count/2))
4913.5.22 by Gordon Tyler
Tweaked quote handling.
92
            # 2N+1 backslashes follwed by a quote are N backslashes followed by
93
            # the quote which should not be processed as the start or end of
94
            # the quoted arg
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
95
            if self.count % 2 == 1:
4913.5.22 by Gordon Tyler
Tweaked quote handling.
96
                # odd number of \ escapes the quote
97
                context.token.append(next_char)
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
98
            else:
4913.5.22 by Gordon Tyler
Tweaked quote handling.
99
                # let exit_state handle next_char
100
                context.seq.pushback(next_char)
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
101
            self.count = 0
102
            return self.exit_state
103
        else:
4913.5.22 by Gordon Tyler
Tweaked quote handling.
104
            # N backslashes not followed by a quote are just N backslashes
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
105
            if self.count > 0:
106
                context.token.append(u'\\' * self.count)
107
                self.count = 0
4913.5.22 by Gordon Tyler
Tweaked quote handling.
108
            # let exit_state handle next_char
109
            context.seq.pushback(next_char)
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
110
            return self.exit_state
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
111
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
112
    def finish(self, context):
113
        if self.count > 0:
114
            context.token.append(u'\\' * self.count)
115
116
117
class _Word(object):
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
118
    def process(self, next_char, context):
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
119
        if _whitespace_match(next_char):
120
            return None
4913.5.22 by Gordon Tyler
Tweaked quote handling.
121
        elif next_char in context.allowed_quote_chars:
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
122
            return _Quotes(next_char, self)
123
        elif next_char == u'\\':
124
            return _Backslash(self)
125
        else:
126
            context.token.append(next_char)
127
            return self
128
129
4913.5.23 by Gordon Tyler
Renamed cmdline.Parser to Splitter to better match its usage.
130
class Splitter(object):
4913.5.24 by Gordon Tyler
Added cmdline.split function, which replaces commands.shlex_split_unicode.
131
    def __init__(self, command_line, single_quotes_allowed):
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
132
        self.seq = _PushbackSequence(command_line)
4913.5.22 by Gordon Tyler
Tweaked quote handling.
133
        self.allowed_quote_chars = u'"'
134
        if single_quotes_allowed:
135
            self.allowed_quote_chars += u"'"
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
136
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
137
    def __iter__(self):
138
        return self
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
139
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
140
    def next(self):
141
        quoted, token = self._get_token()
142
        if token is None:
143
            raise StopIteration
144
        return quoted, token
5050.1.3 by Vincent Ladeuil
Delete spurious spaces.
145
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
146
    def _get_token(self):
147
        self.quoted = False
148
        self.token = []
149
        state = _Whitespace()
4913.5.21 by Gordon Tyler
Refactored interface between Parser and states.
150
        for next_char in self.seq:
151
            state = state.process(next_char, self)
4913.5.19 by Gordon Tyler
Moved UnicodeShlex, etc. to a new module, bzrlib.cmdline, and renamed it to Parser.
152
            if state is None:
153
                break
154
        if not state is None and not getattr(state, 'finish', None) is None:
155
            state.finish(self)
156
        result = u''.join(self.token)
157
        if not self.quoted and result == '':
158
            result = None
159
        return self.quoted, result
4913.5.24 by Gordon Tyler
Added cmdline.split function, which replaces commands.shlex_split_unicode.
160
4913.5.27 by Gordon Tyler
Code formatting.
161
4913.5.24 by Gordon Tyler
Added cmdline.split function, which replaces commands.shlex_split_unicode.
162
def split(unsplit, single_quotes_allowed=True):
163
    splitter = Splitter(unsplit, single_quotes_allowed=single_quotes_allowed)
164
    return [arg for quoted, arg in splitter]