--- shlex.py.old 2003-04-16 13:58:42.000000000 -0300 +++ shlex.py 2003-04-16 16:06:00.000000000 -0300 @@ -3,21 +3,33 @@ # Module and documentation by Eric S. Raymond, 21 Dec 1998 # Input stacking and error message cleanup added by ESR, March 2000 # push_source() and pop_source() made explicit by ESR, January 2001. +# Posix compliance, split_args(), string arguments, and +# iterator interface by Gustavo Niemeyer, April 2003. import os.path import sys -__all__ = ["shlex"] +from types import StringTypes + +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO + +__all__ = ["shlex", "split_args"] class shlex: "A lexical analyzer class for simple shell-like syntaxes." - def __init__(self, instream=None, infile=None): + def __init__(self, instream=None, infile=None, posix=0): + if type(instream) in StringTypes: + instream = StringIO(instream) if instream is not None: self.instream = instream self.infile = infile else: self.instream = sys.stdin self.infile = None + self.posix = posix self.commenters = '#' self.wordchars = ('abcdfeghijklmnopqrstuvwxyz' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_') @@ -42,6 +54,8 @@ def push_source(self, newstream, newfile=None): "Push an input source onto the lexer's input source stack." + if type(newstream) in StringTypes: + newstream = StringIO(newstream) self.filestack.insert(0, (self.infile, self.instream, self.lineno)) self.infile = newfile self.instream = newstream @@ -62,7 +76,7 @@ % (self.instream, self.lineno) self.state = ' ' - def get_token(self): + def get_token(self, posix=None): "Get a token from the input stream (or from stack if it's nonempty)" if self.pushback: tok = self.pushback[0] @@ -71,10 +85,10 @@ print "shlex: popping token " + `tok` return tok # No pushback. Get a token. - raw = self.read_token() + raw = self.read_token(posix) # Handle inclusions while raw == self.source: - spec = self.sourcehook(self.read_token()) + spec = self.sourcehook(self.read_token(posix)) if spec: (newfile, newstream) = spec self.push_source(newstream, newfile) @@ -94,8 +108,9 @@ print "shlex: token=EOF" return raw - def read_token(self): - "Read a token from the input stream (no pushback or inclusions)" + def read_token(self, posix=None): + if posix is None: + posix = self.posix while 1: nextchar = self.instream.read(1) if nextchar == '\n': @@ -124,7 +139,8 @@ self.token = nextchar self.state = 'a' elif nextchar in self.quotes: - self.token = nextchar + if not posix: + self.token = nextchar self.state = nextchar else: self.token = nextchar @@ -133,15 +149,20 @@ else: continue elif self.state in self.quotes: - self.token = self.token + nextchar - if nextchar == self.state: - self.state = ' ' - break - elif not nextchar: # end of file + if not nextchar: # end of file if self.debug >= 2: print "shlex: I see EOF in quotes state" # XXX what error should be raised here? raise ValueError, "No closing quotation" + if nextchar == self.state: + if not posix: + self.token = self.token + nextchar + self.state = ' ' + break + else: + self.state = 'a' + else: + self.token = self.token + nextchar elif self.state == 'a': if not nextchar: self.state = None # end of file @@ -157,6 +178,8 @@ elif nextchar in self.commenters: self.instream.readline() self.lineno = self.lineno + 1 + elif posix and nextchar in self.quotes: + self.state = nextchar elif nextchar in self.wordchars or nextchar in self.quotes: self.token = self.token + nextchar else: @@ -182,7 +205,7 @@ if newfile[0] == '"': newfile = newfile[1:-1] # This implements cpp-like semantics for relative-path inclusion. - if type(self.infile) == type("") and not os.path.isabs(newfile): + if type(self.infile) in StringTypes and not os.path.isabs(newfile): newfile = os.path.join(os.path.dirname(self.infile), newfile) return (newfile, open(newfile, "r")) @@ -194,6 +217,19 @@ lineno = self.lineno return "\"%s\", line %d: " % (infile, lineno) + def __iter__(self): + return self + + def next(self): + token = self.get_token() + if not token: + raise StopIteration + return token + +def split_args(s): + lex = shlex(s, posix=1) + lex.wordchars += "~`!@#$%^&*()-+={}[]:;<>,.?/|" + return list(lex) if __name__ == '__main__': if len(sys.argv) == 1: