Esempio n. 1
0
def InitWordEvaluator():
    arena = MakeArena('<InitWordEvaluator>')
    mem = state.Mem('', [], arena, [])
    state.InitMem(mem, {})

    opt_array = [False] * option_i.ARRAY_SIZE
    errexit = state._ErrExit()
    parse_opts = optview.Parse(opt_array)
    exec_opts = optview.Exec(opt_array, errexit)
    mem.exec_opts = exec_opts  # circular dep

    exec_deps = cmd_exec.Deps()
    exec_deps.trap_nodes = []

    splitter = split.SplitContext(mem)
    errfmt = ui.ErrorFormatter(arena)

    ev = word_eval.CompletionWordEvaluator(mem, exec_opts, splitter, errfmt)
    return ev
Esempio n. 2
0
def InitCommandEvaluator(parse_ctx=None, comp_lookup=None, arena=None, mem=None,
                 aliases=None, ext_prog=None):
  opt0_array = state.InitOpts()
  opt_stacks = [None] * option_i.ARRAY_SIZE
  if parse_ctx:
    arena = parse_ctx.arena
    parse_opts = parse_ctx.parse_opts
  else:
    parse_ctx = InitParseContext()

  mem = mem or state.Mem('', [], arena, [])
  exec_opts = optview.Exec(opt0_array, opt_stacks)
  mutable_opts = state.MutableOpts(mem, opt0_array, opt_stacks, None)
  mem.exec_opts = exec_opts
  state.InitMem(mem, {}, '0.1')
  mutable_opts.Init()

  # No 'readline' in the tests.

  errfmt = ui.ErrorFormatter(arena)
  job_state = process.JobState()
  fd_state = process.FdState(errfmt, job_state)
  aliases = {} if aliases is None else aliases
  procs = {}

  compopt_state = completion.OptionState()
  comp_lookup = comp_lookup or completion.Lookup()

  readline = None  # simulate not having it

  new_var = builtin_assign.NewVar(mem, procs, errfmt)
  assign_builtins = {
      builtin_i.declare: new_var,
      builtin_i.typeset: new_var,
      builtin_i.local: new_var,

      builtin_i.export_: builtin_assign.Export(mem, errfmt),
      builtin_i.readonly: builtin_assign.Readonly(mem, errfmt),
  }
  builtins = {  # Lookup
      builtin_i.echo: builtin_pure.Echo(exec_opts),
      builtin_i.shift: builtin_assign.Shift(mem),

      builtin_i.history: builtin_lib.History(readline, mylib.Stdout()),

      builtin_i.compopt: builtin_comp.CompOpt(compopt_state, errfmt),
      builtin_i.compadjust: builtin_comp.CompAdjust(mem),

      builtin_i.alias: builtin_pure.Alias(aliases, errfmt),
      builtin_i.unalias: builtin_pure.UnAlias(aliases, errfmt),
  }

  debug_f = util.DebugFile(sys.stderr)
  cmd_deps = cmd_eval.Deps()
  cmd_deps.mutable_opts = mutable_opts
  cmd_deps.trap_nodes = []

  search_path = state.SearchPath(mem)
  waiter = process.Waiter(job_state, exec_opts)

  ext_prog = \
      ext_prog or process.ExternalProgram('', fd_state, errfmt, debug_f)

  cmd_deps.dumper = dev.CrashDumper('')
  cmd_deps.debug_f = debug_f

  splitter = split.SplitContext(mem)

  arith_ev = sh_expr_eval.ArithEvaluator(mem, exec_opts, parse_ctx, errfmt)
  bool_ev = sh_expr_eval.BoolEvaluator(mem, exec_opts, parse_ctx, errfmt)
  expr_ev = expr_eval.OilEvaluator(mem, procs, splitter, errfmt)
  word_ev = word_eval.NormalWordEvaluator(mem, exec_opts, mutable_opts,
                                          splitter, errfmt)
  cmd_ev = cmd_eval.CommandEvaluator(mem, exec_opts, errfmt, procs,
                                     assign_builtins, arena, cmd_deps)

  shell_ex = executor.ShellExecutor(
      mem, exec_opts, mutable_opts, procs, builtins, search_path,
      ext_prog, waiter, job_state, fd_state, errfmt)

  assert cmd_ev.mutable_opts is not None, cmd_ev
  prompt_ev = prompt.Evaluator('osh', parse_ctx, mem)

  tracer = dev.Tracer(parse_ctx, exec_opts, mutable_opts, mem, word_ev,
                      debug_f)

  vm.InitCircularDeps(arith_ev, bool_ev, expr_ev, word_ev, cmd_ev, shell_ex,
                      prompt_ev, tracer)

  spec_builder = builtin_comp.SpecBuilder(cmd_ev, parse_ctx, word_ev, splitter,
                                          comp_lookup)
  # Add some builtins that depend on the executor!
  complete_builtin = builtin_comp.Complete(spec_builder, comp_lookup)
  builtins[builtin_i.complete] = complete_builtin
  builtins[builtin_i.compgen] = builtin_comp.CompGen(spec_builder)

  return cmd_ev