blob: 4801a6c1d47bd9e0a8ada16089221c8237b777d5 [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""A lexical analyzer class for simple shell-like syntaxes."""
2
Tim Peters70c43782001-01-17 08:48:39 +00003# Module and documentation by Eric S. Raymond, 21 Dec 1998
Guido van Rossumeb4e11a2000-05-01 20:08:46 +00004# Input stacking and error message cleanup added by ESR, March 2000
Tim Peters70c43782001-01-17 08:48:39 +00005# push_source() and pop_source() made explicit by ESR, January 2001.
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +00006# Posix compliance, split(), string arguments, and
7# iterator interface by Gustavo Niemeyer, April 2003.
Vinay Sajipc1f974c2016-07-29 22:35:03 +01008# changes to tokenize more like Posix shells by Vinay Sajip, July 2016.
Guido van Rossum9c30c241998-12-22 05:19:29 +00009
Éric Araujo9bce3112011-07-27 18:29:31 +020010import os
11import re
Guido van Rossum73898c71999-05-03 18:14:16 +000012import sys
Raymond Hettinger756b3f32004-01-29 06:37:52 +000013from collections import deque
Guido van Rossum9c30c241998-12-22 05:19:29 +000014
Guido van Rossum68937b42007-05-18 00:51:22 +000015from io import StringIO
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000016
Bo Baylesca804952019-05-29 03:06:12 -050017__all__ = ["shlex", "split", "quote", "join"]
Skip Montanaro0de65802001-02-15 22:15:14 +000018
Guido van Rossum9c30c241998-12-22 05:19:29 +000019class shlex:
Tim Peters70c43782001-01-17 08:48:39 +000020 "A lexical analyzer class for simple shell-like syntaxes."
Vinay Sajipc1f974c2016-07-29 22:35:03 +010021 def __init__(self, instream=None, infile=None, posix=False,
22 punctuation_chars=False):
Guido van Rossum3172c5d2007-10-16 18:12:55 +000023 if isinstance(instream, str):
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000024 instream = StringIO(instream)
Raymond Hettingerf13eb552002-06-02 00:40:05 +000025 if instream is not None:
Guido van Rossum9c30c241998-12-22 05:19:29 +000026 self.instream = instream
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000027 self.infile = infile
Guido van Rossum9c30c241998-12-22 05:19:29 +000028 else:
29 self.instream = sys.stdin
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000030 self.infile = None
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000031 self.posix = posix
32 if posix:
33 self.eof = None
34 else:
35 self.eof = ''
Guido van Rossum9c30c241998-12-22 05:19:29 +000036 self.commenters = '#'
Fred Drakedbbf76b2000-07-09 16:44:26 +000037 self.wordchars = ('abcdfeghijklmnopqrstuvwxyz'
38 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_')
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000039 if self.posix:
Antoine Pitroud72402e2010-10-27 18:52:48 +000040 self.wordchars += ('ßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ'
41 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ')
Guido van Rossum9c30c241998-12-22 05:19:29 +000042 self.whitespace = ' \t\r\n'
Fred Drake24315232003-04-17 22:01:17 +000043 self.whitespace_split = False
Guido van Rossum9c30c241998-12-22 05:19:29 +000044 self.quotes = '\'"'
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000045 self.escape = '\\'
46 self.escapedquotes = '"'
Guido van Rossum9c30c241998-12-22 05:19:29 +000047 self.state = ' '
Raymond Hettinger756b3f32004-01-29 06:37:52 +000048 self.pushback = deque()
Guido van Rossum9c30c241998-12-22 05:19:29 +000049 self.lineno = 1
50 self.debug = 0
51 self.token = ''
Raymond Hettinger756b3f32004-01-29 06:37:52 +000052 self.filestack = deque()
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000053 self.source = None
Vinay Sajipc1f974c2016-07-29 22:35:03 +010054 if not punctuation_chars:
55 punctuation_chars = ''
56 elif punctuation_chars is True:
57 punctuation_chars = '();<>|&'
Alex972cf5c2019-09-11 14:04:04 +030058 self._punctuation_chars = punctuation_chars
Vinay Sajipc1f974c2016-07-29 22:35:03 +010059 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 Rossum9c30c241998-12-22 05:19:29 +000067
Alex972cf5c2019-09-11 14:04:04 +030068 @property
69 def punctuation_chars(self):
70 return self._punctuation_chars
71
Guido van Rossum9c30c241998-12-22 05:19:29 +000072 def push_token(self, tok):
73 "Push a token onto the stack popped by the get_token method"
Guido van Rossumeb4e11a2000-05-01 20:08:46 +000074 if self.debug >= 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000075 print("shlex: pushing token " + repr(tok))
Raymond Hettinger756b3f32004-01-29 06:37:52 +000076 self.pushback.appendleft(tok)
Guido van Rossum9c30c241998-12-22 05:19:29 +000077
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000078 def push_source(self, newstream, newfile=None):
79 "Push an input source onto the lexer's input source stack."
Guido van Rossum3172c5d2007-10-16 18:12:55 +000080 if isinstance(newstream, str):
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +000081 newstream = StringIO(newstream)
Raymond Hettinger756b3f32004-01-29 06:37:52 +000082 self.filestack.appendleft((self.infile, self.instream, self.lineno))
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000083 self.infile = newfile
84 self.instream = newstream
85 self.lineno = 1
86 if self.debug:
Raymond Hettingerf13eb552002-06-02 00:40:05 +000087 if newfile is not None:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000088 print('shlex: pushing to file %s' % (self.infile,))
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000089 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000090 print('shlex: pushing to stream %s' % (self.instream,))
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000091
92 def pop_source(self):
93 "Pop the input source stack."
94 self.instream.close()
Raymond Hettinger756b3f32004-01-29 06:37:52 +000095 (self.infile, self.instream, self.lineno) = self.filestack.popleft()
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000096 if self.debug:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000097 print('shlex: popping to %s, line %d' \
98 % (self.instream, self.lineno))
Eric S. Raymondbddbaf72001-01-16 15:19:13 +000099 self.state = ' '
100
Guido van Rossum9c30c241998-12-22 05:19:29 +0000101 def get_token(self):
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000102 "Get a token from the input stream (or from stack if it's nonempty)"
Guido van Rossum9c30c241998-12-22 05:19:29 +0000103 if self.pushback:
Raymond Hettinger756b3f32004-01-29 06:37:52 +0000104 tok = self.pushback.popleft()
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000105 if self.debug >= 1:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000106 print("shlex: popping token " + repr(tok))
Guido van Rossum9c30c241998-12-22 05:19:29 +0000107 return tok
Fred Drakedbbf76b2000-07-09 16:44:26 +0000108 # No pushback. Get a token.
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000109 raw = self.read_token()
110 # Handle inclusions
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000111 if self.source is not None:
112 while raw == self.source:
113 spec = self.sourcehook(self.read_token())
114 if spec:
115 (newfile, newstream) = spec
116 self.push_source(newstream, newfile)
117 raw = self.get_token()
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000118 # Maybe we got EOF instead?
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000119 while raw == self.eof:
Fred Drake24315232003-04-17 22:01:17 +0000120 if not self.filestack:
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000121 return self.eof
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000122 else:
Eric S. Raymondbddbaf72001-01-16 15:19:13 +0000123 self.pop_source()
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000124 raw = self.get_token()
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000125 # Neither inclusion nor EOF
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000126 if self.debug >= 1:
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000127 if raw != self.eof:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000128 print("shlex: token=" + repr(raw))
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000129 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 print("shlex: token=EOF")
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000131 return raw
132
133 def read_token(self):
Fred Drake24315232003-04-17 22:01:17 +0000134 quoted = False
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000135 escapedstate = ' '
Neal Norwitz10cf2182003-04-17 23:09:08 +0000136 while True:
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100137 if self.punctuation_chars and self._pushback_chars:
138 nextchar = self._pushback_chars.pop()
139 else:
140 nextchar = self.instream.read(1)
Guido van Rossum9c30c241998-12-22 05:19:29 +0000141 if nextchar == '\n':
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100142 self.lineno += 1
Guido van Rossum9c30c241998-12-22 05:19:29 +0000143 if self.debug >= 3:
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100144 print("shlex: in state %r I see character: %r" % (self.state,
145 nextchar))
Fred Drakedbbf76b2000-07-09 16:44:26 +0000146 if self.state is None:
Eric S. Raymondbddbaf72001-01-16 15:19:13 +0000147 self.token = '' # past end of file
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000148 break
Guido van Rossum9c30c241998-12-22 05:19:29 +0000149 elif self.state == ' ':
150 if not nextchar:
Eric S. Raymondbddbaf72001-01-16 15:19:13 +0000151 self.state = None # end of file
Guido van Rossum9c30c241998-12-22 05:19:29 +0000152 break
153 elif nextchar in self.whitespace:
154 if self.debug >= 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000155 print("shlex: I see whitespace in whitespace state")
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000156 if self.token or (self.posix and quoted):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000157 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000158 else:
159 continue
160 elif nextchar in self.commenters:
161 self.instream.readline()
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100162 self.lineno += 1
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000163 elif self.posix and nextchar in self.escape:
164 escapedstate = 'a'
165 self.state = nextchar
Guido van Rossum9c30c241998-12-22 05:19:29 +0000166 elif nextchar in self.wordchars:
167 self.token = nextchar
168 self.state = 'a'
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100169 elif nextchar in self.punctuation_chars:
170 self.token = nextchar
171 self.state = 'c'
Guido van Rossum9c30c241998-12-22 05:19:29 +0000172 elif nextchar in self.quotes:
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000173 if not self.posix:
174 self.token = nextchar
Guido van Rossum9c30c241998-12-22 05:19:29 +0000175 self.state = nextchar
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000176 elif self.whitespace_split:
177 self.token = nextchar
178 self.state = 'a'
Guido van Rossum9c30c241998-12-22 05:19:29 +0000179 else:
180 self.token = nextchar
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000181 if self.token or (self.posix and quoted):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000182 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000183 else:
184 continue
185 elif self.state in self.quotes:
Fred Drake24315232003-04-17 22:01:17 +0000186 quoted = True
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000187 if not nextchar: # end of file
Andrew M. Kuchling9d56cd12001-01-09 03:01:15 +0000188 if self.debug >= 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000189 print("shlex: I see EOF in quotes state")
Andrew M. Kuchling9d56cd12001-01-09 03:01:15 +0000190 # XXX what error should be raised here?
Collin Winterce36ad82007-08-30 01:19:48 +0000191 raise ValueError("No closing quotation")
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000192 if nextchar == self.state:
193 if not self.posix:
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100194 self.token += nextchar
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000195 self.state = ' '
196 break
197 else:
198 self.state = 'a'
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100199 elif (self.posix and nextchar in self.escape and self.state
200 in self.escapedquotes):
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000201 escapedstate = self.state
202 self.state = nextchar
203 else:
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100204 self.token += nextchar
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000205 elif self.state in self.escape:
206 if not nextchar: # end of file
207 if self.debug >= 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000208 print("shlex: I see EOF in escape state")
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000209 # XXX what error should be raised here?
Collin Winterce36ad82007-08-30 01:19:48 +0000210 raise ValueError("No escaped character")
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000211 # In posix shells, only the quote itself or the escape
212 # character may be escaped within quotes.
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100213 if (escapedstate in self.quotes and
214 nextchar != self.state and nextchar != escapedstate):
215 self.token += self.state
216 self.token += nextchar
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000217 self.state = escapedstate
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100218 elif self.state in ('a', 'c'):
Guido van Rossum9c30c241998-12-22 05:19:29 +0000219 if not nextchar:
Tim Peters70c43782001-01-17 08:48:39 +0000220 self.state = None # end of file
Guido van Rossum9c30c241998-12-22 05:19:29 +0000221 break
222 elif nextchar in self.whitespace:
223 if self.debug >= 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000224 print("shlex: I see whitespace in word state")
Guido van Rossum9c30c241998-12-22 05:19:29 +0000225 self.state = ' '
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000226 if self.token or (self.posix and quoted):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000227 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000228 else:
229 continue
230 elif nextchar in self.commenters:
231 self.instream.readline()
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100232 self.lineno += 1
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000233 if self.posix:
234 self.state = ' '
235 if self.token or (self.posix and quoted):
236 break # emit current token
237 else:
238 continue
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100239 elif self.state == 'c':
240 if nextchar in self.punctuation_chars:
241 self.token += nextchar
242 else:
243 if nextchar not in self.whitespace:
244 self._pushback_chars.append(nextchar)
245 self.state = ' '
246 break
Vinay Sajip61eda722017-01-15 10:06:52 +0000247 elif self.posix and nextchar in self.quotes:
248 self.state = nextchar
249 elif self.posix and nextchar in self.escape:
250 escapedstate = 'a'
251 self.state = nextchar
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100252 elif (nextchar in self.wordchars or nextchar in self.quotes
Evan56624a92019-06-02 05:09:22 +1000253 or (self.whitespace_split and
254 nextchar not in self.punctuation_chars)):
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100255 self.token += nextchar
Guido van Rossum9c30c241998-12-22 05:19:29 +0000256 else:
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100257 if self.punctuation_chars:
258 self._pushback_chars.append(nextchar)
259 else:
260 self.pushback.appendleft(nextchar)
Guido van Rossum9c30c241998-12-22 05:19:29 +0000261 if self.debug >= 2:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000262 print("shlex: I see punctuation in word state")
Guido van Rossumf247d751999-03-22 15:28:08 +0000263 self.state = ' '
Vinay Sajipc1f974c2016-07-29 22:35:03 +0100264 if self.token or (self.posix and quoted):
Fred Drakedbbf76b2000-07-09 16:44:26 +0000265 break # emit current token
Guido van Rossum9c30c241998-12-22 05:19:29 +0000266 else:
267 continue
Guido van Rossum9c30c241998-12-22 05:19:29 +0000268 result = self.token
269 self.token = ''
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000270 if self.posix and not quoted and result == '':
271 result = None
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000272 if self.debug > 1:
273 if result:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000274 print("shlex: raw token=" + repr(result))
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000275 else:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000276 print("shlex: raw token=EOF")
Guido van Rossum9c30c241998-12-22 05:19:29 +0000277 return result
278
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000279 def sourcehook(self, newfile):
280 "Hook called on a filename to be sourced."
281 if newfile[0] == '"':
282 newfile = newfile[1:-1]
Fred Drake52dc76c2000-07-03 09:56:23 +0000283 # This implements cpp-like semantics for relative-path inclusion.
Guido van Rossum3172c5d2007-10-16 18:12:55 +0000284 if isinstance(self.infile, str) and not os.path.isabs(newfile):
Fred Drake52dc76c2000-07-03 09:56:23 +0000285 newfile = os.path.join(os.path.dirname(self.infile), newfile)
Guido van Rossumeb4e11a2000-05-01 20:08:46 +0000286 return (newfile, open(newfile, "r"))
287
Guido van Rossum4b83ecb2000-05-01 20:14:12 +0000288 def error_leader(self, infile=None, lineno=None):
289 "Emit a C-compiler-like, Emacs-friendly error-message leader."
Raymond Hettingerf13eb552002-06-02 00:40:05 +0000290 if infile is None:
Guido van Rossum4b83ecb2000-05-01 20:14:12 +0000291 infile = self.infile
Raymond Hettingerf13eb552002-06-02 00:40:05 +0000292 if lineno is None:
Guido van Rossum4b83ecb2000-05-01 20:14:12 +0000293 lineno = self.lineno
294 return "\"%s\", line %d: " % (infile, lineno)
295
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000296 def __iter__(self):
297 return self
298
Georg Brandla18af4e2007-04-21 15:47:16 +0000299 def __next__(self):
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000300 token = self.get_token()
301 if token == self.eof:
302 raise StopIteration
303 return token
304
Guido van Rossume7ba4952007-06-06 23:52:48 +0000305def split(s, comments=False, posix=True):
MaT1g3R65c73822019-10-31 06:23:20 -0400306 """Split the string *s* using shell-like syntax."""
Zackery Spytz975ac322020-04-01 07:58:55 -0600307 if s is None:
308 import warnings
309 warnings.warn("Passing None for 's' to shlex.split() is deprecated.",
310 DeprecationWarning, stacklevel=2)
Guido van Rossume7ba4952007-06-06 23:52:48 +0000311 lex = shlex(s, posix=posix)
Gustavo Niemeyer48f3dcc2003-04-20 01:57:03 +0000312 lex.whitespace_split = True
313 if not comments:
314 lex.commenters = ''
Gustavo Niemeyer68d8cef2003-04-17 21:31:33 +0000315 return list(lex)
Guido van Rossum9c30c241998-12-22 05:19:29 +0000316
Éric Araujo9bce3112011-07-27 18:29:31 +0200317
Bo Baylesca804952019-05-29 03:06:12 -0500318def join(split_command):
319 """Return a shell-escaped string from *split_command*."""
320 return ' '.join(quote(arg) for arg in split_command)
321
322
Ezio Melotti67321cc2011-08-16 19:03:41 +0300323_find_unsafe = re.compile(r'[^\w@%+=:,./-]', re.ASCII).search
Éric Araujo9bce3112011-07-27 18:29:31 +0200324
325def quote(s):
326 """Return a shell-escaped version of the string *s*."""
327 if not s:
328 return "''"
329 if _find_unsafe(s) is None:
330 return s
331
332 # use single quotes, and put single quotes into double quotes
333 # the string $'b is then quoted as '$'"'"'b'
334 return "'" + s.replace("'", "'\"'\"'") + "'"
335
336
R David Murray838f2c42014-10-17 20:28:47 -0400337def _print_tokens(lexer):
Guido van Rossum9c30c241998-12-22 05:19:29 +0000338 while 1:
339 tt = lexer.get_token()
R David Murray838f2c42014-10-17 20:28:47 -0400340 if not tt:
Guido van Rossum9c30c241998-12-22 05:19:29 +0000341 break
R David Murray838f2c42014-10-17 20:28:47 -0400342 print("Token: " + repr(tt))
343
344if __name__ == '__main__':
345 if len(sys.argv) == 1:
346 _print_tokens(shlex())
347 else:
348 fn = sys.argv[1]
349 with open(fn) as f:
350 _print_tokens(shlex(f, fn))