Exemplo n.º 1
0
    def Run(self):
        # type: () -> None
        val = self.mem.GetValue('PROMPT_COMMAND')
        if val.tag_() != value_e.Str:
            return

        # PROMPT_COMMAND almost never changes, so we try to cache its parsing.
        # This avoids memory allocations.
        prompt_cmd = cast(value__Str, val).s
        node = self.parse_cache.get(prompt_cmd)
        if node is None:
            line_reader = reader.StringLineReader(prompt_cmd, self.arena)
            c_parser = self.parse_ctx.MakeOshParser(line_reader)

            # NOTE: This is similar to CommandEvaluator.ParseTrapCode().
            # TODO: Add spid
            with alloc.ctx_Location(self.arena,
                                    source.PromptCommand(runtime.NO_SPID)):
                try:
                    node = main_loop.ParseWholeFile(c_parser)
                except error.Parse as e:
                    ui.PrettyPrintError(e, self.arena)
                    return  # don't execute

            self.parse_cache[prompt_cmd] = node

        # Save this so PROMPT_COMMAND can't set $?
        with state.ctx_Status(self.mem):
            # Catches fatal execution error
            self.cmd_ev.ExecuteAndCatch(node)
Exemplo n.º 2
0
    def Run(self):
        # type: () -> None
        val = self.mem.GetVar('PROMPT_COMMAND')
        if val.tag != value_e.Str:
            return

        # PROMPT_COMMAND almost never changes, so we try to cache its parsing.
        # This avoids memory allocations.
        prompt_cmd = val.s
        try:
            node = self.parse_cache[prompt_cmd]
        except KeyError:
            line_reader = reader.StringLineReader(prompt_cmd, self.arena)
            c_parser = self.parse_ctx.MakeOshParser(line_reader)

            # NOTE: This is similar to Executor.ParseTrapCode().
            # TODO: Add spid
            self.arena.PushSource(source.PromptCommand(const.NO_INTEGER))
            try:
                try:
                    node = main_loop.ParseWholeFile(c_parser)
                except util.ParseError as e:
                    ui.PrettyPrintError(e, self.arena)
                    return  # don't execute
            finally:
                self.arena.PopSource()

            self.parse_cache[prompt_cmd] = node

        # Save this so PROMPT_COMMAND can't set $?
        self.mem.PushStatusFrame()
        try:
            # Catches fatal execution error
            self.ex.ExecuteAndCatch(node)
        finally:
            self.mem.PopStatusFrame()