Пример #1
0
  def Run(self, cmd_val):
    arg, arg_r = flag_spec.ParseOilCmdVal('write', cmd_val)
    #print(arg)

    if arg.unicode == 'raw':
      bit8_display = qsn.BIT8_UTF8
    elif arg.unicode == 'u':
      bit8_display = qsn.BIT8_U_ESCAPE
    elif arg.unicode == 'x':
      bit8_display = qsn.BIT8_X_ESCAPE
    else:
      raise AssertionError()

    i = 0
    while not arg_r.AtEnd():
      if i != 0:
        sys.stdout.write(arg.sep)
      s = arg_r.Peek()

      if arg.qsn:
        s = qsn.maybe_encode(s, bit8_display)

      sys.stdout.write(s)

      arg_r.Next()
      i += 1

    if arg.n:
      pass
    elif arg.end:
      sys.stdout.write(arg.end)

    return 0
Пример #2
0
    def Run(self, cmd_val):
        # type: (cmd_value__Argv) -> int

        # TODO: Also hard usage error here too?
        attrs, arg_r = flag_spec.ParseOilCmdVal('run', cmd_val)
        arg = arg_types.run(attrs.attrs)

        if arg_r.Peek() is None:
            # HARD ERROR, not e_usage(), because errexit is often disabled!
            e_die("'run' expected a command to run", status=2)

        argv, spids = arg_r.Rest2()
        cmd_val2 = cmd_value.Argv(argv, spids, cmd_val.block)

        # Set in the 'except' block, e.g. if 'myfunc' failed
        failure_spid = runtime.NO_SPID
        try:
            # Temporarily turn ON errexit, and blame the 'run' spid.  Note that
            # 'if run myproc' disables it and then enables it!
            with state.ctx_ErrExit(self.mutable_opts, True,
                                   cmd_val.arg_spids[0]):
                # Pass do_fork=True.  Slight annoyance: the real value is a field of
                # command.Simple().  See _NoForkLast() in CommandEvaluator We have an
                # extra fork (miss out on an optimization) of code like ( status ls )
                # or forkwait { status ls }, but that is NOT idiomatic code.  status is
                # for functions.
                status = self.shell_ex.RunSimpleCommand(cmd_val2, True)
                #log('st %d', status)
        except error.ErrExit as e:  # from functino call
            #log('e %d', e.exit_status)
            status = e.exit_status
            failure_spid = e.span_id

        # Do this before -allow-status-01
        if arg.status_ok is not None:
            status = _AdjustStatus(arg.status_ok, status)

        if arg.allow_status_01 and status not in (0, 1):
            if failure_spid != runtime.NO_SPID:
                self.errfmt.Print_('(original failure)', span_id=failure_spid)
                self.errfmt.StderrLine('')

            raise error.ErrExit('fatal: status %d when --allow-status-01' %
                                status,
                                span_id=spids[0],
                                status=status)

        if arg.assign_status is not None:
            var_name = arg.assign_status
            if var_name.startswith(':'):
                var_name = var_name[1:]

            state.SetRefString(self.mem, var_name, str(status))
            return 0  # don't fail

        return status
Пример #3
0
    def Run(self, cmd_val):
        # type: (cmd_value__Argv) -> int
        attrs, arg_r = flag_spec.ParseOilCmdVal('forkwait', cmd_val)
        arg, span_id = arg_r.Peek2()
        if arg is not None:
            e_usage('got unexpected argument %r' % arg, span_id=span_id)

        if cmd_val.block is None:
            e_usage('expected a block')

        return self.shell_ex.RunSubshell(cmd_val.block)
Пример #4
0
    def Run(self, cmd_val):
        arg, arg_r = flag_spec.ParseOilCmdVal('push', cmd_val)

        var_name, var_spid = arg_r.ReadRequired2('requires a variable name')

        if var_name.startswith(':'):  # optional : sigil
            var_name = var_name[1:]

        if not match.IsValidVarName(var_name):
            raise error.Usage('got invalid variable name %r' % var_name,
                              span_id=var_spid)

        val = self.mem.GetValue(var_name)
        # TODO: value.Obj too
        if val.tag != value_e.MaybeStrArray:
            self.errfmt.Print("%r isn't an array", var_name, span_id=var_spid)
            return 1

        val.strs.extend(arg_r.Rest())
        return 0
Пример #5
0
  def Run(self, cmd_val):
    arg, arg_r = flag_spec.ParseOilCmdVal('repr', cmd_val)

    action, action_spid = arg_r.ReadRequired2(
        'expected an action (proc, .cell, etc.)')

    # Actions that print unstable formats start with '.'
    if action == '.cell':
      argv, spids = arg_r.Rest2()

      status = 0
      for i, name in enumerate(argv):
        if name.startswith(':'):
          name = name[1:]

        if not match.IsValidVarName(name):
          raise error.Usage('got invalid variable name %r' % name,
                            span_id=spids[i])

        cell = self.mem.GetCell(name)
        if cell is None:
          self.errfmt.Print("Couldn't find a variable named %r" % name,
                            span_id=spids[i])
          status = 1
        else:
          sys.stdout.write('%s = ' % name)
          cell.PrettyPrint()  # may be color
          sys.stdout.write('\n')

    elif action == 'proc':
      names, spids = arg_r.Rest2()
      if len(names):
        for i, name in enumerate(names):
          node = self.procs.get(name)
          if node is None:
            self.errfmt.Print_('Invalid proc %r' % name, span_id=spids[i])
            return 1
      else:
        names = sorted(self.procs)

      # QTSV header
      print('proc_name\tdoc_comment')
      for name in names:
        node = self.procs[name]  # must exist
        body = node.body

        # TODO: not just command__ShFunction, but command__Proc!
        doc = ''
        if body.tag_() == command_e.BraceGroup:
          if body.doc_token:
            span_id = body.doc_token.span_id
            span = self.arena.GetLineSpan(span_id)
            line = self.arena.GetLine(span.line_id)
            # 1 to remove leading space
            doc = line[span.col+1 : span.col + span.length]

        # No limits on proc names
        print('%s\t%s' % (qsn.maybe_encode(name), qsn.maybe_encode(doc)))

      status = 0

    else:
      e_usage('got invalid action %r' % action, span_id=action_spid)

    return status