Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """A lexical analyzer class for simple shell-like syntaxes.""" |
| 2 | |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 3 | # Module and documentation by Eric S. Raymond, 21 Dec 1998 |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 4 | # Input stacking and error message cleanup added by ESR, March 2000 |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 5 | # push_source() and pop_source() made explicit by ESR, January 2001. |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 6 | # Posix compliance, split(), string arguments, and |
| 7 | # iterator interface by Gustavo Niemeyer, April 2003. |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 8 | # changes to tokenize more like Posix shells by Vinay Sajip, July 2016. |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 9 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 10 | import os |
| 11 | import re |
Guido van Rossum | 73898c7 | 1999-05-03 18:14:16 +0000 | [diff] [blame] | 12 | import sys |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 13 | from collections import deque |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 68937b4 | 2007-05-18 00:51:22 +0000 | [diff] [blame] | 15 | from io import StringIO |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 16 | |
Bo Bayles | ca80495 | 2019-05-29 03:06:12 -0500 | [diff] [blame] | 17 | __all__ = ["shlex", "split", "quote", "join"] |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 19 | class shlex: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 20 | "A lexical analyzer class for simple shell-like syntaxes." |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 21 | def __init__(self, instream=None, infile=None, posix=False, |
| 22 | punctuation_chars=False): |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 23 | if isinstance(instream, str): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 24 | instream = StringIO(instream) |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 25 | if instream is not None: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 26 | self.instream = instream |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 27 | self.infile = infile |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 28 | else: |
| 29 | self.instream = sys.stdin |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 30 | self.infile = None |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 31 | self.posix = posix |
| 32 | if posix: |
| 33 | self.eof = None |
| 34 | else: |
| 35 | self.eof = '' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 36 | self.commenters = '#' |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 37 | self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' |
| 38 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 39 | if self.posix: |
Antoine Pitrou | d72402e | 2010-10-27 18:52:48 +0000 | [diff] [blame] | 40 | self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ' |
| 41 | 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ') |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 42 | self.whitespace = ' \t\r\n' |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 43 | self.whitespace_split = False |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 44 | self.quotes = '\'"' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 45 | self.escape = '\\' |
| 46 | self.escapedquotes = '"' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 47 | self.state = ' ' |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 48 | self.pushback = deque() |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 49 | self.lineno = 1 |
| 50 | self.debug = 0 |
| 51 | self.token = '' |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 52 | self.filestack = deque() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 53 | self.source = None |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 54 | if not punctuation_chars: |
| 55 | punctuation_chars = '' |
| 56 | elif punctuation_chars is True: |
| 57 | punctuation_chars = '();<>|&' |
| 58 | self.punctuation_chars = punctuation_chars |
| 59 | if punctuation_chars: |
| 60 | # _pushback_chars is a push back queue used by lookahead logic |
| 61 | self._pushback_chars = deque() |
| 62 | # these chars added because allowed in file names, args, wildcards |
| 63 | self.wordchars += '~-./*?=' |
| 64 | #remove any punctuation chars from wordchars |
| 65 | t = self.wordchars.maketrans(dict.fromkeys(punctuation_chars)) |
| 66 | self.wordchars = self.wordchars.translate(t) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 67 | |
| 68 | def push_token(self, tok): |
| 69 | "Push a token onto the stack popped by the get_token method" |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 70 | if self.debug >= 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 71 | print("shlex: pushing token " + repr(tok)) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 72 | self.pushback.appendleft(tok) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 73 | |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 74 | def push_source(self, newstream, newfile=None): |
| 75 | "Push an input source onto the lexer's input source stack." |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 76 | if isinstance(newstream, str): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 77 | newstream = StringIO(newstream) |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 78 | self.filestack.appendleft((self.infile, self.instream, self.lineno)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 79 | self.infile = newfile |
| 80 | self.instream = newstream |
| 81 | self.lineno = 1 |
| 82 | if self.debug: |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 83 | if newfile is not None: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 84 | print('shlex: pushing to file %s' % (self.infile,)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 85 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 86 | print('shlex: pushing to stream %s' % (self.instream,)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 87 | |
| 88 | def pop_source(self): |
| 89 | "Pop the input source stack." |
| 90 | self.instream.close() |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 91 | (self.infile, self.instream, self.lineno) = self.filestack.popleft() |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 92 | if self.debug: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 93 | print('shlex: popping to %s, line %d' \ |
| 94 | % (self.instream, self.lineno)) |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 95 | self.state = ' ' |
| 96 | |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 97 | def get_token(self): |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 98 | "Get a token from the input stream (or from stack if it's nonempty)" |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 99 | if self.pushback: |
Raymond Hettinger | 756b3f3 | 2004-01-29 06:37:52 +0000 | [diff] [blame] | 100 | tok = self.pushback.popleft() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 101 | if self.debug >= 1: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 102 | print("shlex: popping token " + repr(tok)) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 103 | return tok |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 104 | # No pushback. Get a token. |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 105 | raw = self.read_token() |
| 106 | # Handle inclusions |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 107 | if self.source is not None: |
| 108 | while raw == self.source: |
| 109 | spec = self.sourcehook(self.read_token()) |
| 110 | if spec: |
| 111 | (newfile, newstream) = spec |
| 112 | self.push_source(newstream, newfile) |
| 113 | raw = self.get_token() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 114 | # Maybe we got EOF instead? |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 115 | while raw == self.eof: |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 116 | if not self.filestack: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 117 | return self.eof |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 118 | else: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 119 | self.pop_source() |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 120 | raw = self.get_token() |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 121 | # Neither inclusion nor EOF |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 122 | if self.debug >= 1: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 123 | if raw != self.eof: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 124 | print("shlex: token=" + repr(raw)) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 125 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 126 | print("shlex: token=EOF") |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 127 | return raw |
| 128 | |
| 129 | def read_token(self): |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 130 | quoted = False |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 131 | escapedstate = ' ' |
Neal Norwitz | 10cf218 | 2003-04-17 23:09:08 +0000 | [diff] [blame] | 132 | while True: |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 133 | if self.punctuation_chars and self._pushback_chars: |
| 134 | nextchar = self._pushback_chars.pop() |
| 135 | else: |
| 136 | nextchar = self.instream.read(1) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 137 | if nextchar == '\n': |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 138 | self.lineno += 1 |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 139 | if self.debug >= 3: |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 140 | print("shlex: in state %r I see character: %r" % (self.state, |
| 141 | nextchar)) |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 142 | if self.state is None: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 143 | self.token = '' # past end of file |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 144 | break |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 145 | elif self.state == ' ': |
| 146 | if not nextchar: |
Eric S. Raymond | bddbaf7 | 2001-01-16 15:19:13 +0000 | [diff] [blame] | 147 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 148 | break |
| 149 | elif nextchar in self.whitespace: |
| 150 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 151 | print("shlex: I see whitespace in whitespace state") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 152 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 153 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 154 | else: |
| 155 | continue |
| 156 | elif nextchar in self.commenters: |
| 157 | self.instream.readline() |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 158 | self.lineno += 1 |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 159 | elif self.posix and nextchar in self.escape: |
| 160 | escapedstate = 'a' |
| 161 | self.state = nextchar |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 162 | elif nextchar in self.wordchars: |
| 163 | self.token = nextchar |
| 164 | self.state = 'a' |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 165 | elif nextchar in self.punctuation_chars: |
| 166 | self.token = nextchar |
| 167 | self.state = 'c' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 168 | elif nextchar in self.quotes: |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 169 | if not self.posix: |
| 170 | self.token = nextchar |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 171 | self.state = nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 172 | elif self.whitespace_split: |
| 173 | self.token = nextchar |
| 174 | self.state = 'a' |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 175 | else: |
| 176 | self.token = nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 177 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 178 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 179 | else: |
| 180 | continue |
| 181 | elif self.state in self.quotes: |
Fred Drake | 2431523 | 2003-04-17 22:01:17 +0000 | [diff] [blame] | 182 | quoted = True |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 183 | if not nextchar: # end of file |
Andrew M. Kuchling | 9d56cd1 | 2001-01-09 03:01:15 +0000 | [diff] [blame] | 184 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 185 | print("shlex: I see EOF in quotes state") |
Andrew M. Kuchling | 9d56cd1 | 2001-01-09 03:01:15 +0000 | [diff] [blame] | 186 | # XXX what error should be raised here? |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 187 | raise ValueError("No closing quotation") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 188 | if nextchar == self.state: |
| 189 | if not self.posix: |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 190 | self.token += nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 191 | self.state = ' ' |
| 192 | break |
| 193 | else: |
| 194 | self.state = 'a' |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 195 | elif (self.posix and nextchar in self.escape and self.state |
| 196 | in self.escapedquotes): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 197 | escapedstate = self.state |
| 198 | self.state = nextchar |
| 199 | else: |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 200 | self.token += nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 201 | elif self.state in self.escape: |
| 202 | if not nextchar: # end of file |
| 203 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 204 | print("shlex: I see EOF in escape state") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 205 | # XXX what error should be raised here? |
Collin Winter | ce36ad8 | 2007-08-30 01:19:48 +0000 | [diff] [blame] | 206 | raise ValueError("No escaped character") |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 207 | # In posix shells, only the quote itself or the escape |
| 208 | # character may be escaped within quotes. |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 209 | if (escapedstate in self.quotes and |
| 210 | nextchar != self.state and nextchar != escapedstate): |
| 211 | self.token += self.state |
| 212 | self.token += nextchar |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 213 | self.state = escapedstate |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 214 | elif self.state in ('a', 'c'): |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 215 | if not nextchar: |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 216 | self.state = None # end of file |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 217 | break |
| 218 | elif nextchar in self.whitespace: |
| 219 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 220 | print("shlex: I see whitespace in word state") |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 221 | self.state = ' ' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 222 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 223 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 224 | else: |
| 225 | continue |
| 226 | elif nextchar in self.commenters: |
| 227 | self.instream.readline() |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 228 | self.lineno += 1 |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 229 | if self.posix: |
| 230 | self.state = ' ' |
| 231 | if self.token or (self.posix and quoted): |
| 232 | break # emit current token |
| 233 | else: |
| 234 | continue |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 235 | elif self.state == 'c': |
| 236 | if nextchar in self.punctuation_chars: |
| 237 | self.token += nextchar |
| 238 | else: |
| 239 | if nextchar not in self.whitespace: |
| 240 | self._pushback_chars.append(nextchar) |
| 241 | self.state = ' ' |
| 242 | break |
Vinay Sajip | 61eda72 | 2017-01-15 10:06:52 +0000 | [diff] [blame] | 243 | elif self.posix and nextchar in self.quotes: |
| 244 | self.state = nextchar |
| 245 | elif self.posix and nextchar in self.escape: |
| 246 | escapedstate = 'a' |
| 247 | self.state = nextchar |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 248 | elif (nextchar in self.wordchars or nextchar in self.quotes |
Evan | 56624a9 | 2019-06-02 05:09:22 +1000 | [diff] [blame^] | 249 | or (self.whitespace_split and |
| 250 | nextchar not in self.punctuation_chars)): |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 251 | self.token += nextchar |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 252 | else: |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 253 | if self.punctuation_chars: |
| 254 | self._pushback_chars.append(nextchar) |
| 255 | else: |
| 256 | self.pushback.appendleft(nextchar) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 257 | if self.debug >= 2: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 258 | print("shlex: I see punctuation in word state") |
Guido van Rossum | f247d75 | 1999-03-22 15:28:08 +0000 | [diff] [blame] | 259 | self.state = ' ' |
Vinay Sajip | c1f974c | 2016-07-29 22:35:03 +0100 | [diff] [blame] | 260 | if self.token or (self.posix and quoted): |
Fred Drake | dbbf76b | 2000-07-09 16:44:26 +0000 | [diff] [blame] | 261 | break # emit current token |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 262 | else: |
| 263 | continue |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 264 | result = self.token |
| 265 | self.token = '' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 266 | if self.posix and not quoted and result == '': |
| 267 | result = None |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 268 | if self.debug > 1: |
| 269 | if result: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 270 | print("shlex: raw token=" + repr(result)) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 271 | else: |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 272 | print("shlex: raw token=EOF") |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 273 | return result |
| 274 | |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 275 | def sourcehook(self, newfile): |
| 276 | "Hook called on a filename to be sourced." |
| 277 | if newfile[0] == '"': |
| 278 | newfile = newfile[1:-1] |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 279 | # This implements cpp-like semantics for relative-path inclusion. |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 280 | if isinstance(self.infile, str) and not os.path.isabs(newfile): |
Fred Drake | 52dc76c | 2000-07-03 09:56:23 +0000 | [diff] [blame] | 281 | newfile = os.path.join(os.path.dirname(self.infile), newfile) |
Guido van Rossum | eb4e11a | 2000-05-01 20:08:46 +0000 | [diff] [blame] | 282 | return (newfile, open(newfile, "r")) |
| 283 | |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 284 | def error_leader(self, infile=None, lineno=None): |
| 285 | "Emit a C-compiler-like, Emacs-friendly error-message leader." |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 286 | if infile is None: |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 287 | infile = self.infile |
Raymond Hettinger | f13eb55 | 2002-06-02 00:40:05 +0000 | [diff] [blame] | 288 | if lineno is None: |
Guido van Rossum | 4b83ecb | 2000-05-01 20:14:12 +0000 | [diff] [blame] | 289 | lineno = self.lineno |
| 290 | return "\"%s\", line %d: " % (infile, lineno) |
| 291 | |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 292 | def __iter__(self): |
| 293 | return self |
| 294 | |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 295 | def __next__(self): |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 296 | token = self.get_token() |
| 297 | if token == self.eof: |
| 298 | raise StopIteration |
| 299 | return token |
| 300 | |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 301 | def split(s, comments=False, posix=True): |
| 302 | lex = shlex(s, posix=posix) |
Gustavo Niemeyer | 48f3dcc | 2003-04-20 01:57:03 +0000 | [diff] [blame] | 303 | lex.whitespace_split = True |
| 304 | if not comments: |
| 305 | lex.commenters = '' |
Gustavo Niemeyer | 68d8cef | 2003-04-17 21:31:33 +0000 | [diff] [blame] | 306 | return list(lex) |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 307 | |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 308 | |
Bo Bayles | ca80495 | 2019-05-29 03:06:12 -0500 | [diff] [blame] | 309 | def join(split_command): |
| 310 | """Return a shell-escaped string from *split_command*.""" |
| 311 | return ' '.join(quote(arg) for arg in split_command) |
| 312 | |
| 313 | |
Ezio Melotti | 67321cc | 2011-08-16 19:03:41 +0300 | [diff] [blame] | 314 | _find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search |
Éric Araujo | 9bce311 | 2011-07-27 18:29:31 +0200 | [diff] [blame] | 315 | |
| 316 | def quote(s): |
| 317 | """Return a shell-escaped version of the string *s*.""" |
| 318 | if not s: |
| 319 | return "''" |
| 320 | if _find_unsafe(s) is None: |
| 321 | return s |
| 322 | |
| 323 | # use single quotes, and put single quotes into double quotes |
| 324 | # the string $'b is then quoted as '$'"'"'b' |
| 325 | return "'" + s.replace("'", "'\"'\"'") + "'" |
| 326 | |
| 327 | |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 328 | def _print_tokens(lexer): |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 329 | while 1: |
| 330 | tt = lexer.get_token() |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 331 | if not tt: |
Guido van Rossum | 9c30c24 | 1998-12-22 05:19:29 +0000 | [diff] [blame] | 332 | break |
R David Murray | 838f2c4 | 2014-10-17 20:28:47 -0400 | [diff] [blame] | 333 | print("Token: " + repr(tt)) |
| 334 | |
| 335 | if __name__ == '__main__': |
| 336 | if len(sys.argv) == 1: |
| 337 | _print_tokens(shlex()) |
| 338 | else: |
| 339 | fn = sys.argv[1] |
| 340 | with open(fn) as f: |
| 341 | _print_tokens(shlex(f, fn)) |