def testShellFuncExecution(self): ex = cmd_exec_test.InitExecutor() func_node = ast.FuncDef() c1 = ast.CompoundWord() t1 = ast.token(Id.Lit_Chars, 'f1') c1.parts.append(ast.LiteralPart(t1)) c2 = ast.CompoundWord() t2 = ast.token(Id.Lit_Chars, 'f2') c2.parts.append(ast.LiteralPart(t2)) a = ast.ArrayLiteralPart() a.words = [c1, c2] w = ast.CompoundWord() w.parts.append(a) # Set global COMPREPLY=(f1 f2) pair = ast.assign_pair(ast.LhsName('COMPREPLY'), assign_op_e.Equal, w) pair.spids.append(0) # dummy pairs = [pair] body_node = ast.Assignment(Id.Assign_None, [], pairs) func_node.name = 'myfunc' func_node.body = body_node a = completion.ShellFuncAction(ex, func_node) matches = list(a.Matches([], 0, 'f')) self.assertEqual(['f1 ', 'f2 '], matches)
def testPipeline2(self): Banner('ls | cut -d . -f 1 | head') p = process.Pipeline() p.Add(_ExtProc(['ls'])) p.Add(_ExtProc(['cut', '-d', '.', '-f', '1'])) p.Add(_ExtProc(['head'])) print(p.Run(_WAITER)) ex = InitExecutor() # Simulating subshell for each command w1 = ast.CompoundWord() w1.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'ls'))) node1 = ast.SimpleCommand() node1.words = [w1] w2 = ast.CompoundWord() w2.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'head'))) node2 = ast.SimpleCommand() node2.words = [w2] w3 = ast.CompoundWord() w3.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, 'sort'))) w4 = ast.CompoundWord() w4.parts.append(ast.LiteralPart(ast.token(Id.Lit_Chars, '--reverse'))) node3 = ast.SimpleCommand() node3.words = [w3, w4] p = process.Pipeline() p.Add(Process(process.SubProgramThunk(ex, node1))) p.Add(Process(process.SubProgramThunk(ex, node2))) p.Add(Process(process.SubProgramThunk(ex, node3))) print(p.Run(_WAITER))
def testVarOps(self): ev = InitEvaluator() # initializes x=xxx and y=yyy unset_sub = ast.BracedVarSub(ast.token(Id.VSub_Name, 'unset')) part_vals = [] ev._EvalWordPart(unset_sub, part_vals) print(part_vals) set_sub = ast.BracedVarSub(ast.token(Id.VSub_Name, 'x')) part_vals = [] ev._EvalWordPart(set_sub, part_vals) print(part_vals) # Now add some ops part = ast.LiteralPart(ast.token(Id.Lit_Chars, 'default')) arg_word = ast.CompoundWord([part]) test_op = ast.StringUnary(Id.VTest_ColonHyphen, arg_word) unset_sub.suffix_op = test_op set_sub.suffix_op = test_op part_vals = [] ev._EvalWordPart(unset_sub, part_vals) print(part_vals) part_vals = [] ev._EvalWordPart(set_sub, part_vals) print(part_vals)
def EvalPrompt(self, val): """Perform the two evaluations that bash does. Used by $PS1 and ${x@P}.""" if val.tag != value_e.Str: return DEFAULT_PS1 # no evaluation necessary try: tokens = self.tokens_cache[val.s] except KeyError: tokens = match.PS1_LEXER.Tokens(val.s) self.tokens_cache[val.s] = tokens # First replacements. TODO: Should we cache this too? ps1_str = self._ReplaceBackslashCodes(tokens) # The prompt is often constant, so we can avoid parsing it. # NOTE: This is copied from the PS4 logic in Tracer. try: ps1_word = self.parse_cache[ps1_str] except KeyError: w_parser = self.parse_ctx.MakeWordParserForPlugin( ps1_str, self.arena) try: ps1_word = w_parser.ReadPS() except Exception as e: error_str = '<ERROR: cannot parse PS1>' t = ast.token(Id.Lit_Chars, error_str, const.NO_INTEGER) ps1_word = ast.CompoundWord([ast.LiteralPart(t)]) self.parse_cache[ps1_str] = ps1_word # e.g. "${debian_chroot}\u" -> '\u' val2 = self.ex.word_ev.EvalWordToString(ps1_word) return val2.s
def EvalPrompt(self, val): """Perform the two evaluations that bash does. Used by $PS1 and ${x@P}.""" if val.tag != value_e.Str: return DEFAULT_PS1 # no evaluation necessary # Parse backslash escapes (cached) try: tokens = self.tokens_cache[val.s] except KeyError: tokens = list(match.PS1_LEXER.Tokens(val.s)) self.tokens_cache[val.s] = tokens # Replace values. ps1_str = self._ReplaceBackslashCodes(tokens) # Parse it like a double-quoted word (cached). # NOTE: This is copied from the PS4 logic in Tracer. try: ps1_word = self.parse_cache[ps1_str] except KeyError: w_parser = self.parse_ctx.MakeWordParserForPlugin( ps1_str, self.arena) try: ps1_word = w_parser.ReadPS() except Exception as e: error_str = '<ERROR: cannot parse PS1>' t = ast.token(Id.Lit_Chars, error_str, const.NO_INTEGER) ps1_word = ast.CompoundWord([ast.LiteralPart(t)]) self.parse_cache[ps1_str] = ps1_word # Evaluate, e.g. "${debian_chroot}\u" -> '\u' val2 = self.ex.word_ev.EvalWordToString(ps1_word) return val2.s
def _ReadPatSubVarOp(self, lex_mode): """ Match = ('/' | '#' | '%') WORD VarSub = ... | VarOf '/' Match '/' WORD """ do_all = False do_prefix = False do_suffix = False pat = self._ReadVarOpArg(lex_mode, eof_type=Id.Lit_Slash, empty_ok=False) if not pat: return None if len(pat.parts) == 1: ok, s, quoted = word.StaticEval(pat) if ok and s == '/' and not quoted: # Looks like ${a////c}, read again self._Next(lex_mode) self._Peek() p = ast.LiteralPart(self.cur_token) pat.parts.append(p) if len(pat.parts) == 0: self._BadToken("Pattern must not be empty: %r", token=self.cur_token) return None else: first_part = pat.parts[0] if first_part.tag == word_part_e.LiteralPart: lit_id = first_part.token.id if lit_id == Id.Lit_Slash: do_all = True pat.parts.pop(0) elif lit_id == Id.Lit_Pound: do_prefix = True pat.parts.pop(0) elif lit_id == Id.Lit_Percent: do_suffix = True pat.parts.pop(0) #self._Peek() if self.token_type == Id.Right_VarSub: # e.g. ${v/a} is the same as ${v/a/} -- empty replacement string return ast.PatSub(pat, None, do_all, do_prefix, do_suffix) elif self.token_type == Id.Lit_Slash: replace = self._ReadVarOpArg(lex_mode) # do not stop at / if not replace: return None self._Peek() if self.token_type == Id.Right_VarSub: return ast.PatSub(pat, replace, do_all, do_prefix, do_suffix) else: self._BadToken("Expected } after pat sub, got %s", self.cur_token) return None else: self._BadToken("Expected } after pat sub, got %s", self.cur_token) return None
def _ReadLikeDQ(self, left_dq_token, out_parts): """ Args: left_dq_token: A token if we are reading a double quoted part, or None if we're reading a here doc. out_parts: list of word_part to append to """ done = False while not done: self._Next(lex_mode_e.DQ) self._Peek() if self.token_kind == Kind.Lit: if self.token_type == Id.Lit_EscapedChar: part = ast.EscapedLiteralPart(self.cur_token) else: part = ast.LiteralPart(self.cur_token) out_parts.append(part) elif self.token_kind == Kind.Left: part = self._ReadDoubleQuotedLeftParts() out_parts.append(part) elif self.token_kind == Kind.VSub: part = ast.SimpleVarSub(self.cur_token) out_parts.append(part) elif self.token_kind == Kind.Right: assert self.token_type == Id.Right_DoubleQuote, self.token_type if left_dq_token: done = True else: # In a here doc, the right quote is literal! out_parts.append(ast.LiteralPart(self.cur_token)) elif self.token_kind == Kind.Eof: if left_dq_token: p_die( 'Unexpected EOF reading double-quoted string that began here', token=left_dq_token) else: # here docs will have an EOF in their token stream done = True else: raise AssertionError(self.cur_token)
def _MaybeReadHereDocs(self): for h in self.pending_here_docs: lines = [] #log('HERE %r' % h.here_end) while True: # If op is <<-, strip off all leading tabs (NOT spaces). # (in C++, just bump the start?) line_id, line = self.line_reader.GetLine() #print("LINE %r %r" % (line, h.here_end)) if not line: # EOF # An unterminated here doc is just a warning in bash. We make it # fatal because we want to be strict, and because it causes problems # reporting other errors. # Attribute it to the << in <<EOF for now. self.AddErrorContext('Unterminated here doc', span_id=h.spids[0]) return False # NOTE: Could do this runtime to preserve LST. if h.op_id == Id.Redir_DLessDash: line = line.lstrip('\t') if line.rstrip() == h.here_end: break lines.append((line_id, line)) parts = [] if h.do_expansion: # NOTE: We read all lines at once, instead of doing it line-by-line, # because of cases like this: # cat <<EOF # 1 $(echo 2 # echo 3) 4 # EOF from osh import parse_lib # Avoid circular import w_parser = parse_lib.MakeWordParserForHereDoc(lines, self.arena) word = w_parser.ReadHereDocBody() if not word: self.AddErrorContext( 'Error reading here doc body: %s', w_parser.Error()) return False h.body = word h.was_filled = True else: # Each line is a single span. TODO: Add span_id to token. tokens = [ ast.token(Id.Lit_Chars, line, const.NO_INTEGER) for _, line in lines] parts = [ast.LiteralPart(t) for t in tokens] h.body = ast.CompoundWord(parts) h.was_filled = True # No .clear() until Python 3.3. del self.pending_here_docs[:] return True
def _ReadPatSubVarOp(self, lex_mode): """ Match = ('/' | '#' | '%') WORD VarSub = ... | VarOf '/' Match '/' WORD """ pat = self._ReadVarOpArg(lex_mode, eof_type=Id.Lit_Slash, empty_ok=False) if len(pat.parts) == 1: ok, s, quoted = word.StaticEval(pat) if ok and s == '/' and not quoted: # Looks like ${a////c}, read again self._Next(lex_mode) self._Peek() p = ast.LiteralPart(self.cur_token) pat.parts.append(p) if len(pat.parts) == 0: p_die('Pattern in ${x/pat/replace} must not be empty', token=self.cur_token) replace_mode = Id.Undefined_Tok # Check for / # % modifier on pattern. first_part = pat.parts[0] if first_part.tag == word_part_e.LiteralPart: lit_id = first_part.token.id if lit_id in (Id.Lit_Slash, Id.Lit_Pound, Id.Lit_Percent): pat.parts.pop(0) replace_mode = lit_id # NOTE: If there is a modifier, the pattern can be empty, e.g. # ${s/#/foo} and ${a/%/foo}. if self.token_type == Id.Right_VarSub: # e.g. ${v/a} is the same as ${v/a/} -- empty replacement string return ast.PatSub(pat, None, replace_mode) if self.token_type == Id.Lit_Slash: replace = self._ReadVarOpArg(lex_mode) # do not stop at / self._Peek() if self.token_type != Id.Right_VarSub: # NOTE: I think this never happens. # We're either in the VS_ARG_UNQ or VS_ARG_DQ lex state, and everything # there is Lit_ or Left_, except for }. p_die("Expected } after replacement string, got %s", self.cur_token, token=self.cur_token) return ast.PatSub(pat, replace, replace_mode) # Happens with ${x//} and ${x///foo}, see test/parse-errors.sh p_die("Expected } after pat sub, got %r", self.cur_token.val, token=self.cur_token)
def testMultiLine(self): w_parser = InitWordParser("""\ ls foo # Multiple newlines and comments should be ignored ls bar """) print('--MULTI') w = w_parser.ReadWord(lex_mode_e.OUTER) parts = [ast.LiteralPart(ast.token(Id.Lit_Chars, 'ls'))] test_lib.AssertAsdlEqual(self, ast.CompoundWord(parts), w) w = w_parser.ReadWord(lex_mode_e.OUTER) parts = [ast.LiteralPart(ast.token(Id.Lit_Chars, 'foo'))] test_lib.AssertAsdlEqual(self, ast.CompoundWord(parts), w) w = w_parser.ReadWord(lex_mode_e.OUTER) t = ast.token(Id.Op_Newline, '\n') test_lib.AssertAsdlEqual(self, ast.TokenWord(t), w) w = w_parser.ReadWord(lex_mode_e.OUTER) parts = [ast.LiteralPart(ast.token(Id.Lit_Chars, 'ls'))] test_lib.AssertAsdlEqual(self, ast.CompoundWord(parts), w) w = w_parser.ReadWord(lex_mode_e.OUTER) parts = [ast.LiteralPart(ast.token(Id.Lit_Chars, 'bar'))] test_lib.AssertAsdlEqual(self, ast.CompoundWord(parts), w) w = w_parser.ReadWord(lex_mode_e.OUTER) t = ast.token(Id.Op_Newline, '\n') test_lib.AssertAsdlEqual(self, ast.TokenWord(t), w) w = w_parser.ReadWord(lex_mode_e.OUTER) t = ast.token(Id.Eof_Real, '') test_lib.AssertAsdlEqual(self, ast.TokenWord(t), w)
def _EvalPS4(self): """For set -x.""" val = self.mem.GetVar('PS4') assert val.tag == value_e.Str s = val.s if s: first_char, ps4 = s[0], s[1:] else: first_char, ps4 = '+', ' ' # default try: ps4_word = self.parse_cache[ps4] except KeyError: # We have to parse this at runtime. PS4 should usually remain constant. w_parser = parse_lib.MakeWordParserForPlugin(ps4, self.arena) # NOTE: Reading PS4 is just like reading a here doc line. "\n" is # allowed too. The OUTER mode would stop at spaces, and ReadWord # doesn't allow lex_mode_e.DQ. ps4_word = w_parser.ReadHereDocBody() if not ps4_word: error_str = '<ERROR: cannot parse PS4>' t = ast.token(Id.Lit_Chars, error_str, const.NO_INTEGER) ps4_word = ast.CompoundWord([ast.LiteralPart(t)]) self.parse_cache[ps4] = ps4_word #print(ps4_word) # TODO: Repeat first character according process stack depth. Where is # that stored? In the executor itself? It should be stored along with # the PID. Need some kind of ShellProcessState or something. # # We should come up with a better mechanism. Something like $PROC_INDENT # and $OIL_XTRACE_PREFIX. # TODO: Handle runtime errors! For example, you could PS4='$(( 1 / 0 ))' # <ERROR: cannot evaluate PS4> prefix = self.word_ev.EvalWordToString(ps4_word) return first_char, prefix.s
def _EvalPS4(self): """For set -x.""" val = self.mem.GetVar('PS4') assert val.tag == value_e.Str s = val.s if s: first_char, ps4 = s[0], s[1:] else: first_char, ps4 = '+', ' ' # default # NOTE: This cache is slightly broken because aliases are mutable! I think # thati s more or less harmless though. try: ps4_word = self.parse_cache[ps4] except KeyError: # We have to parse this at runtime. PS4 should usually remain constant. w_parser = self.parse_ctx.MakeWordParserForPlugin(ps4, self.arena) try: ps4_word = w_parser.ReadPS() except util.ParseError as e: error_str = '<ERROR: cannot parse PS4>' t = ast.token(Id.Lit_Chars, error_str, const.NO_INTEGER) ps4_word = ast.CompoundWord([ast.LiteralPart(t)]) self.parse_cache[ps4] = ps4_word #print(ps4_word) # TODO: Repeat first character according process stack depth. Where is # that stored? In the executor itself? It should be stored along with # the PID. Need some kind of ShellProcessState or something. # # We should come up with a better mechanism. Something like $PROC_INDENT # and $OIL_XTRACE_PREFIX. # TODO: Handle runtime errors! For example, you could PS4='$(( 1 / 0 ))' # <ERROR: cannot evaluate PS4> prefix = self.word_ev.EvalWordToString(ps4_word) return first_char, prefix.s
def _ReadCompoundWord(self, eof_type=Id.Undefined_Tok, lex_mode=lex_mode_e.OUTER, empty_ok=True): """ Precondition: Looking at the first token of the first word part Postcondition: Looking at the token after, e.g. space or operator NOTE: eof_type is necessary because / is a literal, i.e. Lit_Slash, but it could be an operator delimiting a compound word. Can we change lexer modes and remove this special case? """ #print('_ReadCompoundWord', lex_mode) word = ast.CompoundWord() num_parts = 0 done = False while not done: allow_done = empty_ok or num_parts != 0 self._Peek() #print('CW',self.cur_token) if allow_done and self.token_type == eof_type: done = True # e.g. for ${foo//pat/replace} # Keywords like "for" are treated like literals elif self.token_kind in (Kind.Lit, Kind.KW, Kind.Assign, Kind.ControlFlow, Kind.BoolUnary, Kind.BoolBinary): if self.token_type == Id.Lit_EscapedChar: part = ast.EscapedLiteralPart(self.cur_token) else: part = ast.LiteralPart(self.cur_token) #part.xspans.append(self.cur_token.span_id) word.parts.append(part) if self.token_type == Id.Lit_VarLike: #print('@', self.cursor) #print('@', self.cur_token) t = self.lexer.LookAhead(lex_mode_e.OUTER) if t.id == Id.Op_LParen: self.lexer.PushHint(Id.Op_RParen, Id.Right_ArrayLiteral) part2 = self._ReadArrayLiteralPart() if not part2: self.AddErrorContext( '_ReadArrayLiteralPart failed') return False word.parts.append(part2) elif self.token_kind == Kind.VSub: part = ast.SimpleVarSub(self.cur_token) word.parts.append(part) elif self.token_kind == Kind.ExtGlob: part = self._ReadExtGlobPart() if not part: return None word.parts.append(part) elif self.token_kind == Kind.Left: #print('_ReadLeftParts') part = self._ReadLeftParts() if not part: return None word.parts.append(part) # NOT done yet, will advance below elif self.token_kind == Kind.Right: # Still part of the word; will be done on the next iter. if self.token_type == Id.Right_DoubleQuote: pass elif self.token_type == Id.Right_CommandSub: pass elif self.token_type == Id.Right_Subshell: # LEXER HACK for (case x in x) ;; esac ) assert self.next_lex_mode is None # Rewind before it's used if self.lexer.MaybeUnreadOne(): self.lexer.PushHint(Id.Op_RParen, Id.Right_Subshell) self._Next(lex_mode) done = True else: done = True elif self.token_kind == Kind.Ignored: done = True else: # LEXER HACK for unbalanced case clause. 'case foo in esac' is valid, # so to test for ESAC, we can read ) before getting a chance to # PushHint(Id.Op_RParen, Id.Right_CasePat). So here we unread one # token and do it again. # We get Id.Op_RParen at top level: case x in x) ;; esac # We get Id.Eof_RParen inside ComSub: $(case x in x) ;; esac ) if self.token_type in (Id.Op_RParen, Id.Eof_RParen): assert self.next_lex_mode is None # Rewind before it's used if self.lexer.MaybeUnreadOne(): if self.token_type == Id.Eof_RParen: # Redo translation self.lexer.PushHint(Id.Op_RParen, Id.Eof_RParen) self._Next(lex_mode) done = True # anything we don't recognize means we're done if not done: self._Next(lex_mode) num_parts += 1 return word
def _ReadDoubleQuotedPart(self, eof_type=Id.Undefined_Tok, here_doc=False): """ Args: eof_type: for stopping at }, Id.Lit_RBrace here_doc: Whether we are reading in a here doc context Also ${foo%%a b c} # treat this as double quoted. until you hit """ quoted_part = ast.DoubleQuotedPart() left_spid = const.NO_INTEGER right_spid = const.NO_INTEGER # gets set later if self.cur_token is not None: # None in here doc case left_spid = self.cur_token.span_id done = False while not done: self._Next(lex_mode_e.DQ) self._Peek() #print(self.cur_token) if self.token_type == eof_type: # e.g. stop at } done = True continue elif self.token_kind == Kind.Lit: if self.token_type == Id.Lit_EscapedChar: part = ast.EscapedLiteralPart(self.cur_token) else: part = ast.LiteralPart(self.cur_token) quoted_part.parts.append(part) elif self.token_kind == Kind.Left: part = self._ReadDoubleQuotedLeftParts() if not part: return None quoted_part.parts.append(part) elif self.token_kind == Kind.VSub: part = ast.SimpleVarSub(self.cur_token) quoted_part.parts.append(part) elif self.token_kind == Kind.Right: assert self.token_type == Id.Right_DoubleQuote if here_doc: # Turn Id.Right_DoubleQuote into a literal part quoted_part.parts.append(ast.LiteralPart(self.cur_token)) else: done = True # assume Id.Right_DoubleQuote right_spid = self.cur_token.span_id elif self.token_kind == Kind.Eof: if here_doc: # here docs will have an EOF in their token stream done = True else: self.AddErrorContext( 'Unexpected EOF reading double-quoted string that began here', span_id=left_spid) return False else: raise AssertionError(self.cur_token) quoted_part.spids.extend((left_spid, right_spid)) return quoted_part
def TildeDetect(word): """Detect tilde expansion. If it needs to include a TildeSubPart, return a new word. Otherwise return None. NOTE: This algorithm would be a simpler if 1. We could assume some regex for user names. 2. We didn't need to do brace expansion first, like {~foo,~bar} OR - If Lit_Slash were special (it is in the VAROP states, but not OUTER state). We could introduce another lexer mode after you hit Lit_Tilde? So we have to scan all LiteralPart instances until they contain a '/'. http://unix.stackexchange.com/questions/157426/what-is-the-regex-to-validate-linux-users "It is usually recommended to only use usernames that begin with a lower case letter or an underscore, followed by lower case letters, digits, underscores, or dashes. They can end with a dollar sign. In regular expression terms: [a-z_][a-z0-9_-]*[$]? On Debian, the only constraints are that usernames must neither start with a dash ('-') nor contain a colon (':') or a whitespace (space: ' ', end of line: '\n', tabulation: '\t', etc.). Note that using a slash ('/') may break the default algorithm for the definition of the user's home directory. """ if not word.parts: return None part0 = word.parts[0] if _LiteralPartId(part0) != Id.Lit_Tilde: return None prefix = '' found_slash = False # search for the next / for i in range(1, len(word.parts)): # Not a literal part, and we did NOT find a slash. So there is no # TildeSub applied. This would be something like ~X$var, ~$var, # ~$(echo), etc.. The slash is necessary. if word.parts[i].tag != word_part_e.LiteralPart: return None val = word.parts[i].token.val p = val.find('/') if p == -1: # no slash yet prefix += val elif p >= 0: # e.g. for ~foo!bar/baz, extract "bar" # NOTE: requires downcast to LiteralPart pre, post = val[:p], val[p:] prefix += pre tilde_part = ast.TildeSubPart(prefix) # NOTE: no span_id here. It would be nicer to use a different algorithm # that didn't require this. t = ast.token(Id.Lit_Chars, post, const.NO_INTEGER) remainder_part = ast.LiteralPart(t) found_slash = True break w = ast.CompoundWord() if found_slash: w.parts.append(tilde_part) w.parts.append(remainder_part) j = i + 1 while j < len(word.parts): w.parts.append(word.parts[j]) j += 1 else: # The whole thing is a tilde sub, e.g. ~foo or ~foo!bar w.parts.append(ast.TildeSubPart(prefix)) return w