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