OILS / core / shell.py View on Github | oils.pub

1324 lines, 849 significant
1"""shell.py -- assemble the shell interpreter"""
2from __future__ import print_function
3
4from errno import ENOENT
5import time as time_
6
7from _devbuild.gen import arg_types
8from _devbuild.gen.option_asdl import option_i, builtin_i
9from _devbuild.gen.syntax_asdl import (loc, source, source_t, IntParamBox,
10 debug_frame, debug_frame_t)
11from _devbuild.gen.value_asdl import (value, value_e, value_t, value_str, Obj)
12from core import alloc
13from core import comp_ui
14from core import dev
15from core import error
16from core import executor
17from core import completion
18from core import main_loop
19from core import optview
20from core import process
21from core import pyutil
22from core import sh_init
23from core import state
24from display import ui
25from core import util
26from core import vm
27
28from frontend import args
29from frontend import flag_def # side effect: flags are defined!
30
31unused1 = flag_def
32from frontend import flag_util
33from frontend import reader
34from frontend import parse_lib
35
36from builtin import assign_osh
37from builtin import bracket_osh
38from builtin import completion_osh
39from builtin import completion_ysh
40from builtin import dirs_osh
41from builtin import error_ysh
42from builtin import hay_ysh
43from builtin import io_osh
44from builtin import io_ysh
45from builtin import json_ysh
46from builtin import meta_oils
47from builtin import misc_osh
48from builtin import module_ysh
49from builtin import printf_osh
50from builtin import private_ysh
51from builtin import process_osh
52from builtin import pure_osh
53from builtin import pure_ysh
54from builtin import readline_osh
55from builtin import read_osh
56from builtin import trap_osh
57from builtin import umask_osh
58
59from builtin import func_eggex
60from builtin import func_hay
61from builtin import func_misc
62from builtin import func_reflect
63
64from builtin import method_dict
65from builtin import method_io
66from builtin import method_list
67from builtin import method_other
68from builtin import method_str
69from builtin import method_type
70
71from osh import cmd_eval
72from osh import glob_
73from osh import history
74from osh import prompt
75from osh import sh_expr_eval
76from osh import split
77from osh import word_eval
78
79from mycpp import iolib
80from mycpp import mops
81from mycpp import mylib
82from mycpp.mylib import NewDict, print_stderr, log, str_switch
83from pylib import os_path
84from tools import deps
85from tools import lint
86from tools import fmt
87from tools import ysh_ify
88from ysh import expr_eval
89
90unused2 = log
91
92import libc
93import posix_ as posix
94
95from typing import List, Dict, Optional, TYPE_CHECKING
96if TYPE_CHECKING:
97 from frontend.py_readline import Readline
98
99if mylib.PYTHON:
100 try:
101 from _devbuild.gen import help_meta # type: ignore
102 except ImportError:
103 help_meta = None
104
105
106def _InitDefaultCompletions(cmd_ev, complete_builtin, comp_lookup):
107 # type: (cmd_eval.CommandEvaluator, completion_osh.Complete, completion.Lookup) -> None
108
109 # register builtins and words
110 complete_builtin.Run(cmd_eval.MakeBuiltinArgv(['-E', '-A', 'command']))
111 # register path completion
112 # Add -o filenames? Or should that be automatic?
113 complete_builtin.Run(cmd_eval.MakeBuiltinArgv(['-D', '-A', 'file']))
114
115
116def _CompletionDemo(comp_lookup):
117 # type: (completion.Lookup) -> None
118
119 # Something for fun, to show off. Also: test that you don't repeatedly hit
120 # the file system / network / coprocess.
121 A1 = completion.TestAction(['foo.py', 'foo', 'bar.py'], 0.0)
122 l = [] # type: List[str]
123 for i in xrange(0, 5):
124 l.append('m%d' % i)
125
126 A2 = completion.TestAction(l, 0.1)
127 C1 = completion.UserSpec([A1, A2], [], [], completion.DefaultPredicate(),
128 '', '')
129 comp_lookup.RegisterName('slowc', {}, C1)
130
131
132def SourceStartupFile(
133 fd_state, # type: process.FdState
134 rc_path, # type: str
135 lang, # type: str
136 parse_ctx, # type: parse_lib.ParseContext
137 cmd_ev, # type: cmd_eval.CommandEvaluator
138 errfmt, # type: ui.ErrorFormatter
139):
140 # type: (...) -> None
141
142 # Right now this is called when the shell is interactive. (Maybe it should
143 # be called on login_shel too.)
144 #
145 # Terms:
146 # - interactive shell: Roughly speaking, no args or -c, and isatty() is true
147 # for stdin and stdout.
148 # - login shell: Started from the top level, e.g. from init or ssh.
149 #
150 # We're not going to copy everything bash does because it's too complex, but
151 # for reference:
152 # https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files
153 # Bash also has --login.
154
155 try:
156 f = fd_state.Open(rc_path)
157 except (IOError, OSError) as e:
158 # TODO: Could warn about nonexistent explicit --rcfile?
159 if e.errno != ENOENT:
160 raise # Goes to top level. Handle this better?
161 return
162
163 arena = parse_ctx.arena
164 rc_line_reader = reader.FileLineReader(f, arena)
165 rc_c_parser = parse_ctx.MakeOshParser(rc_line_reader)
166
167 with alloc.ctx_SourceCode(arena, source.MainFile(rc_path)):
168 # Note: bash keep going after parse error in startup file. Should we
169 # have a strict mode for this?
170 unused = main_loop.Batch(cmd_ev, rc_c_parser, errfmt)
171
172 f.close()
173
174
175class ShellOptHook(state.OptHook):
176
177 def __init__(self, readline):
178 # type: (Optional[Readline]) -> None
179 self.readline = readline
180
181 def OnChange(self, opt0_array, opt_name, b):
182 # type: (List[bool], str, bool) -> bool
183 """This method is called whenever an option is changed.
184
185 Returns success or failure.
186 """
187 if opt_name == 'vi' or opt_name == 'emacs':
188 # TODO: Replace with a hook? Just like setting LANG= can have a hook.
189 if self.readline:
190 self.readline.parse_and_bind("set editing-mode " + opt_name)
191 else:
192 print_stderr(
193 "Warning: Can't set option %r because shell wasn't compiled with GNU readline"
194 % opt_name)
195 return False
196
197 # Invert: they are mutually exclusive!
198 if opt_name == 'vi':
199 opt0_array[option_i.emacs] = not b
200 elif opt_name == 'emacs':
201 opt0_array[option_i.vi] = not b
202
203 return True
204
205
206def _AddBuiltinFunc(mem, name, func):
207 # type: (state.Mem, str, vm._Callable) -> None
208 assert isinstance(func, vm._Callable), func
209 mem.AddBuiltin(name, value.BuiltinFunc(func))
210
211
212def InitAssignmentBuiltins(
213 mem, # type: state.Mem
214 procs, # type: state.Procs
215 exec_opts, # type: optview.Exec
216 arith_ev, # type: sh_expr_eval.ArithEvaluator
217 errfmt, # type: ui.ErrorFormatter
218):
219 # type: (...) -> Dict[int, vm._AssignBuiltin]
220
221 assign_b = {} # type: Dict[int, vm._AssignBuiltin]
222
223 new_var = assign_osh.NewVar(mem, procs, exec_opts, arith_ev, errfmt)
224 assign_b[builtin_i.declare] = new_var
225 assign_b[builtin_i.typeset] = new_var
226 assign_b[builtin_i.local] = new_var
227
228 assign_b[builtin_i.export_] = assign_osh.Export(mem, arith_ev, errfmt)
229 assign_b[builtin_i.readonly] = assign_osh.Readonly(mem, arith_ev, errfmt)
230
231 return assign_b
232
233
234def Main(
235 lang, # type: str
236 arg_r, # type: args.Reader
237 environ, # type: Dict[str, str]
238 login_shell, # type: bool
239 loader, # type: pyutil._ResourceLoader
240 readline, # type: Optional[Readline]
241 bash_compat=True, # type: bool
242):
243 # type: (...) -> int
244 """The full shell lifecycle. Used by bin/osh and bin/ysh.
245
246 Args:
247 lang: 'osh' or 'ysh'
248 login_shell: Was - on argv[0]?
249 loader: to get help, version, grammar, etc.
250 readline: optional GNU readline
251 """
252 # Differences between osh and ysh:
253 # - oshrc vs yshrc
254 # - shopt -s ysh:all
255 # - Prompt
256 # - --help
257
258 argv0 = arg_r.Peek()
259 assert argv0 is not None
260 arg_r.Next()
261
262 assert lang in ('osh', 'ysh'), lang
263
264 try:
265 attrs = flag_util.ParseMore('main', arg_r, sh_dash_c=True)
266 except error.Usage as e:
267 print_stderr('%s usage error: %s' % (lang, e.msg))
268 return 2
269 flag = arg_types.main(attrs.attrs)
270
271 arena = alloc.Arena()
272 errfmt = ui.ErrorFormatter()
273
274 if flag.help:
275 util.HelpFlag(loader, '%s-usage' % lang, mylib.Stdout())
276 return 0
277 if flag.version:
278 util.VersionFlag(loader, mylib.Stdout())
279 return 0
280
281 if flag.tool == 'cat-em':
282 paths = arg_r.Rest()
283
284 status = 0
285 for p in paths:
286 try:
287 contents = loader.Get(p)
288 print(contents)
289 except (OSError, IOError):
290 print_stderr("cat-em: %r not found" % p)
291 status = 1
292 return status
293
294 script_name = arg_r.Peek() # type: Optional[str]
295 arg_r.Next()
296
297 if script_name is None:
298 dollar0 = argv0
299 # placeholder for -c or stdin (depending on flag.c)
300 frame0 = debug_frame.Dummy # type: debug_frame_t
301 else:
302 dollar0 = script_name
303 frame0 = debug_frame.MainFile(script_name)
304
305 debug_stack = [frame0]
306
307 argv = arg_r.Rest()
308 env_dict = NewDict() # type: Dict[str, value_t]
309 defaults = NewDict() # type: Dict[str, value_t]
310 mem = state.Mem(dollar0,
311 argv,
312 arena,
313 debug_stack,
314 env_dict,
315 defaults=defaults)
316
317 opt_hook = ShellOptHook(readline)
318 # Note: only MutableOpts needs mem, so it's not a true circular dep.
319 parse_opts, exec_opts, mutable_opts = state.MakeOpts(
320 mem, environ, opt_hook)
321 mem.exec_opts = exec_opts # circular dep
322
323 # Set these BEFORE processing flags, so they can be overridden.
324 if lang == 'ysh':
325 mutable_opts.SetAnyOption('ysh:all', True)
326
327 pure_osh.SetOptionsFromFlags(mutable_opts, attrs.opt_changes,
328 attrs.shopt_changes)
329
330 version_str = pyutil.GetVersion(loader)
331 sh_init.InitBuiltins(mem, version_str, defaults)
332 sh_init.InitDefaultVars(mem, argv)
333 if bash_compat:
334 # 2025-09: bash 5.3 is the latest version; can increase this with
335 # future Oils releases
336 state.SetGlobalString(mem, 'BASH_VERSION', '5.3')
337 # major minor patch build release-status machine-type
338 state.SetGlobalArray(mem, 'BASH_VERSINFO',
339 ['5', '3', '0', '0', 'release', 'unknown'])
340
341 sh_init.CopyVarsFromEnv(exec_opts, environ, mem)
342
343 # PATH PWD, etc. must be set after CopyVarsFromEnv()
344 # Also mutate options from SHELLOPTS, if set
345 sh_init.InitVarsAfterEnv(mem, mutable_opts)
346
347 if attrs.show_options: # special case: sh -o
348 pure_osh.ShowOptions(mutable_opts, [])
349 return 0
350
351 # feedback between runtime and parser
352 aliases = NewDict() # type: Dict[str, str]
353
354 ysh_grammar = pyutil.LoadYshGrammar(loader)
355
356 if flag.do_lossless and not exec_opts.noexec():
357 raise error.Usage('--one-pass-parse requires noexec (-n)', loc.Missing)
358
359 # Tools always use one pass parse
360 # Note: osh --tool syntax-tree is like osh -n --one-pass-parse
361 do_lossless = True if len(flag.tool) else flag.do_lossless
362
363 parse_ctx = parse_lib.ParseContext(arena,
364 parse_opts,
365 aliases,
366 ysh_grammar,
367 do_lossless=do_lossless)
368
369 # Three ParseContext instances SHARE aliases.
370 comp_arena = alloc.Arena()
371 comp_arena.PushSource(source.Unused('completion'))
372 trail1 = parse_lib.Trail()
373 # do_lossless needs to be turned on to complete inside backticks. TODO:
374 # fix the issue where ` gets erased because it's not part of
375 # set_completer_delims().
376 comp_ctx = parse_lib.ParseContext(comp_arena,
377 parse_opts,
378 aliases,
379 ysh_grammar,
380 do_lossless=True)
381 comp_ctx.Init_Trail(trail1)
382
383 hist_arena = alloc.Arena()
384 hist_arena.PushSource(source.Unused('history'))
385 trail2 = parse_lib.Trail()
386 hist_ctx = parse_lib.ParseContext(hist_arena, parse_opts, aliases,
387 ysh_grammar)
388 hist_ctx.Init_Trail(trail2)
389
390 # Deps helps manages dependencies. These dependencies are circular:
391 # - cmd_ev and word_ev, arith_ev -- for command sub, arith sub
392 # - arith_ev and word_ev -- for $(( ${a} )) and $x$(( 1 ))
393 # - cmd_ev and builtins (which execute code, like eval)
394 # - prompt_ev needs word_ev for $PS1, which needs prompt_ev for @P
395 cmd_deps = cmd_eval.Deps()
396 cmd_deps.mutable_opts = mutable_opts
397
398 job_control = process.JobControl()
399 job_list = process.JobList()
400 fd_state = process.FdState(errfmt, job_control, job_list, mem, None, None,
401 exec_opts)
402
403 my_pid = posix.getpid()
404
405 debug_path = ''
406 debug_dir = environ.get('OILS_DEBUG_DIR')
407 if flag.debug_file is not None:
408 # --debug-file takes precedence over OSH_DEBUG_DIR
409 debug_path = flag.debug_file
410 elif debug_dir is not None:
411 debug_path = os_path.join(debug_dir, '%d-osh.log' % my_pid)
412
413 if len(debug_path):
414 # This will be created as an empty file if it doesn't exist, or it could be
415 # a pipe.
416 try:
417 debug_f = util.DebugFile(
418 fd_state.OpenForWrite(debug_path)) # type: util._DebugFile
419 except (IOError, OSError) as e:
420 print_stderr("%s: Couldn't open %r: %s" %
421 (lang, debug_path, posix.strerror(e.errno)))
422 return 2
423 else:
424 debug_f = util.NullDebugFile()
425
426 if flag.xtrace_to_debug_file:
427 trace_f = debug_f
428 else:
429 trace_f = util.DebugFile(mylib.Stderr())
430
431 trace_dir = environ.get('OILS_TRACE_DIR', '')
432 dumps = environ.get('OILS_TRACE_DUMPS', '')
433 streams = environ.get('OILS_TRACE_STREAMS', '')
434 multi_trace = dev.MultiTracer(my_pid, trace_dir, dumps, streams, fd_state)
435
436 tracer = dev.Tracer(parse_ctx, exec_opts, mutable_opts, mem, trace_f,
437 multi_trace)
438 fd_state.tracer = tracer # circular dep
439
440 signal_safe = iolib.InitSignalSafe()
441 trap_state = trap_osh.TrapState(signal_safe)
442
443 waiter = process.Waiter(job_list, exec_opts, signal_safe, tracer)
444 fd_state.waiter = waiter
445
446 cmd_deps.debug_f = debug_f
447
448 cflow_builtin = cmd_eval.ControlFlowBuiltin(mem, exec_opts, tracer, errfmt)
449 cmd_deps.cflow_builtin = cflow_builtin
450
451 now = time_.time()
452 iso_stamp = time_.strftime("%Y-%m-%d %H:%M:%S", time_.localtime(now))
453
454 argv_buf = mylib.BufWriter()
455 dev.PrintShellArgv(arg_r.argv, argv_buf)
456
457 debug_f.writeln('%s [%d] Oils started with argv %s' %
458 (iso_stamp, my_pid, argv_buf.getvalue()))
459 if len(debug_path):
460 debug_f.writeln('Writing logs to %r' % debug_path)
461
462 interp = environ.get('OILS_HIJACK_SHEBANG', '')
463 search_path = executor.SearchPath(mem, exec_opts)
464 ext_prog = process.ExternalProgram(interp, fd_state, errfmt, debug_f)
465
466 splitter = split.SplitContext(mem)
467 # TODO: This is instantiation is duplicated in osh/word_eval.py
468 globber = glob_.Globber(exec_opts, mem)
469
470 # This could just be OILS_TRACE_DUMPS='crash:argv0'
471 crash_dump_dir = environ.get('OILS_CRASH_DUMP_DIR', '')
472 cmd_deps.dumper = dev.CrashDumper(crash_dump_dir, fd_state)
473
474 comp_lookup = completion.Lookup()
475
476 # Various Global State objects to work around readline interfaces
477 compopt_state = completion.OptionState()
478
479 comp_ui_state = comp_ui.State()
480 prompt_state = comp_ui.PromptState()
481
482 # The login program is supposed to set $HOME
483 # https://superuser.com/questions/271925/where-is-the-home-environment-variable-set
484 # state.InitMem(mem) must happen first
485 tilde_ev = word_eval.TildeEvaluator(mem, exec_opts)
486 home_dir = tilde_ev.GetMyHomeDir()
487 if home_dir is None:
488 # TODO: print errno from getpwuid()
489 print_stderr("%s: Failed to get home dir from $HOME or getpwuid()" %
490 lang)
491 return 1
492
493 sh_files = sh_init.ShellFiles(lang, home_dir, mem, flag)
494
495 #
496 # Executor and Evaluators (are circularly dependent)
497 #
498
499 # Global proc namespace. Funcs are defined in the common variable
500 # namespace.
501 procs = state.Procs(mem) # type: state.Procs
502
503 builtins = {} # type: Dict[int, vm._Builtin]
504
505 # e.g. s.startswith()
506 methods = {} # type: Dict[int, Dict[str, vm._Callable]]
507
508 hay_state = hay_ysh.HayState()
509
510 shell_ex = executor.ShellExecutor(mem, exec_opts, mutable_opts, procs,
511 hay_state, builtins, tracer, errfmt,
512 search_path, ext_prog, waiter,
513 job_control, job_list, fd_state,
514 trap_state)
515
516 pure_ex = executor.PureExecutor(mem, exec_opts, mutable_opts, procs,
517 hay_state, builtins, tracer, errfmt)
518
519 arith_ev = sh_expr_eval.ArithEvaluator(mem, exec_opts, mutable_opts,
520 parse_ctx, errfmt)
521 bool_ev = sh_expr_eval.BoolEvaluator(mem, exec_opts, mutable_opts,
522 parse_ctx, errfmt)
523 expr_ev = expr_eval.ExprEvaluator(mem, mutable_opts, methods, splitter,
524 errfmt)
525 word_ev = word_eval.NormalWordEvaluator(mem, exec_opts, mutable_opts,
526 tilde_ev, splitter, errfmt)
527
528 assign_b = InitAssignmentBuiltins(mem, procs, exec_opts, arith_ev, errfmt)
529 cmd_ev = cmd_eval.CommandEvaluator(mem, exec_opts, errfmt, procs, assign_b,
530 arena, cmd_deps, trap_state,
531 signal_safe)
532
533 # PromptEvaluator rendering is needed in non-interactive shells for @P.
534 prompt_ev = prompt.Evaluator(lang, version_str, parse_ctx, mem)
535
536 io_methods = NewDict() # type: Dict[str, value_t]
537 io_methods['promptVal'] = value.BuiltinFunc(method_io.PromptVal(prompt_ev))
538
539 # The M/ prefix means it's io->eval()
540 io_methods['M/eval'] = value.BuiltinFunc(
541 method_io.Eval(mem, cmd_ev, None, method_io.EVAL_NULL))
542 io_methods['M/evalExpr'] = value.BuiltinFunc(
543 method_io.EvalExpr(expr_ev, None, None))
544
545 # Identical to command sub
546 io_methods['captureStdout'] = value.BuiltinFunc(
547 method_io.CaptureStdout(mem, shell_ex))
548 # Like captureStdout but capture stderr, too
549 io_methods['captureAll'] = value.BuiltinFunc(
550 method_io.CaptureAll(mem, shell_ex))
551
552 # TODO: remove these 2 deprecated methods
553 io_methods['M/evalToDict'] = value.BuiltinFunc(
554 method_io.Eval(mem, cmd_ev, None, method_io.EVAL_DICT))
555 io_methods['M/evalInFrame'] = value.BuiltinFunc(
556 method_io.EvalInFrame(mem, cmd_ev))
557
558 io_methods['time'] = value.BuiltinFunc(method_io.Time())
559 io_methods['strftime'] = value.BuiltinFunc(method_io.Strftime())
560 io_methods['glob'] = value.BuiltinFunc(
561 method_io.Glob(globber, is_method=True))
562 io_methods['libcGlob'] = value.BuiltinFunc(method_io.LibcGlob(globber))
563
564 io_props = {'stdin': value.Stdin} # type: Dict[str, value_t]
565 io_obj = Obj(Obj(None, io_methods), io_props)
566
567 vm_methods = NewDict() # type: Dict[str, value_t]
568 # These are methods, not free functions, because they reflect VM state
569 vm_methods['getFrame'] = value.BuiltinFunc(func_reflect.GetFrame(mem))
570 vm_methods['getDebugStack'] = value.BuiltinFunc(
571 func_reflect.GetDebugStack(mem))
572 vm_methods['id'] = value.BuiltinFunc(func_reflect.Id())
573
574 vm_props = NewDict() # type: Dict[str, value_t]
575 vm_obj = Obj(Obj(None, vm_methods), vm_props)
576
577 # Add basic type objects for flag parser
578 # flag -v --verbose (Bool, help='foo')
579 #
580 # TODO:
581 # - Add other types like Dict, CommandFlag
582 # - Obj(first, rest)
583 # - List() Dict() Obj() can do shallow copy with __call__
584
585 # - type(x) should return these Obj, or perhaps typeObj(x)
586 # - __str__ method for echo $[type(x)] ?
587
588 # TODO: List and Dict could be the only ones with __index__?
589 i_func = method_type.Index__()
590 type_m = NewDict() # type: Dict[str, value_t]
591 type_m['__index__'] = value.BuiltinFunc(i_func)
592 type_obj_methods = Obj(None, type_m)
593
594 # Note: Func[Int -> Int] is something we should do?
595 for tag in [
596 value_e.Bool,
597 value_e.Int,
598 value_e.Float,
599 value_e.Str,
600 value_e.List,
601 value_e.Dict,
602 ]:
603 type_name = value_str(tag, dot=False)
604 #log('%s %s' , type_name, tag)
605 type_obj = Obj(type_obj_methods, {'name': value.Str(type_name)})
606 mem.AddBuiltin(type_name, type_obj)
607
608 # Initialize Obj
609 tag = value_e.Obj
610 type_name = value_str(tag, dot=False)
611
612 # TODO: change Obj.new to __call__
613 type_props = NewDict() # type: Dict[str, value_t]
614 type_props['name'] = value.Str(type_name)
615 type_props['new'] = value.BuiltinFunc(func_misc.Obj_call())
616 type_obj = Obj(type_obj_methods, type_props)
617
618 mem.AddBuiltin(type_name, type_obj)
619
620 # Wire up circular dependencies.
621 vm.InitCircularDeps(arith_ev, bool_ev, expr_ev, word_ev, cmd_ev, shell_ex,
622 pure_ex, prompt_ev, io_obj, tracer)
623
624 unsafe_arith = sh_expr_eval.UnsafeArith(mem, exec_opts, mutable_opts,
625 parse_ctx, arith_ev, errfmt)
626 vm.InitUnsafeArith(mem, word_ev, unsafe_arith)
627
628 #
629 # Initialize Built-in Procs
630 #
631
632 b = builtins # short alias for initialization
633
634 if mylib.PYTHON:
635 if help_meta:
636 help_data = help_meta.TopicMetadata()
637 else:
638 help_data = NewDict() # minimal build
639 else:
640 help_data = help_meta.TopicMetadata()
641 b[builtin_i.help] = misc_osh.Help(lang, loader, help_data, errfmt)
642
643 # Control flow
644 b[builtin_i.break_] = cflow_builtin
645 b[builtin_i.continue_] = cflow_builtin
646 b[builtin_i.return_] = cflow_builtin
647 b[builtin_i.exit] = cflow_builtin
648
649 # Interpreter state
650 b[builtin_i.set] = pure_osh.Set(mutable_opts, mem)
651 b[builtin_i.shopt] = pure_osh.Shopt(exec_opts, mutable_opts, cmd_ev, mem,
652 environ)
653
654 b[builtin_i.hash] = pure_osh.Hash(search_path) # not really pure
655 b[builtin_i.trap] = trap_osh.Trap(trap_state, parse_ctx, exec_opts, tracer,
656 errfmt)
657
658 b[builtin_i.shvar] = pure_ysh.Shvar(mem, search_path, cmd_ev)
659 b[builtin_i.ctx] = pure_ysh.Ctx(mem, cmd_ev)
660 b[builtin_i.push_registers] = pure_ysh.PushRegisters(mem, cmd_ev)
661
662 # Hay
663 b[builtin_i.hay] = hay_ysh.Hay(hay_state, mutable_opts, mem, cmd_ev)
664 b[builtin_i.haynode] = hay_ysh.HayNode_(hay_state, mem, cmd_ev)
665
666 # Interpreter introspection
667 b[builtin_i.type] = meta_oils.Type(procs, aliases, search_path, errfmt)
668 b[builtin_i.builtin] = meta_oils.Builtin(shell_ex, errfmt)
669 b[builtin_i.command] = meta_oils.Command(shell_ex, procs, aliases,
670 search_path)
671 # Part of YSH, but similar to builtin/command
672 b[builtin_i.invoke] = meta_oils.Invoke(shell_ex, procs, aliases,
673 search_path, errfmt)
674 # Note: runproc is like invoke --proc, could be deprecated?
675 b[builtin_i.runproc] = meta_oils.RunProc(shell_ex, procs, errfmt)
676
677 # allows setting ENV and PATH
678 #b[builtin_i.extern_] = meta_oils.Extern(shell_ex, procs, errfmt)
679
680 # Meta builtins
681 module_invoke = module_ysh.ModuleInvoke(cmd_ev, tracer, errfmt)
682 b[builtin_i.use] = meta_oils.ShellFile(parse_ctx,
683 search_path,
684 cmd_ev,
685 fd_state,
686 tracer,
687 errfmt,
688 loader,
689 module_invoke=module_invoke)
690 source_builtin = meta_oils.ShellFile(parse_ctx, search_path, cmd_ev,
691 fd_state, tracer, errfmt, loader)
692 b[builtin_i.source] = source_builtin
693 b[builtin_i.dot] = source_builtin
694 eval_builtin = meta_oils.Eval(parse_ctx, exec_opts, cmd_ev, tracer, errfmt,
695 mem)
696 b[builtin_i.eval] = eval_builtin
697
698 # Module builtins
699 guards = NewDict() # type: Dict[str, bool]
700 b[builtin_i.source_guard] = module_ysh.SourceGuard(guards, exec_opts,
701 errfmt)
702 b[builtin_i.is_main] = module_ysh.IsMain(mem)
703
704 # Errors
705 b[builtin_i.error] = error_ysh.Error()
706 b[builtin_i.failed] = error_ysh.Failed(mem)
707 b[builtin_i.boolstatus] = error_ysh.BoolStatus(shell_ex, errfmt)
708 b[builtin_i.try_] = error_ysh.Try(mutable_opts, mem, cmd_ev, shell_ex,
709 errfmt)
710 b[builtin_i.assert_] = error_ysh.Assert(expr_ev, errfmt)
711
712 # Pure builtins
713 true_ = pure_osh.Boolean(0)
714 b[builtin_i.colon] = true_ # a "special" builtin
715 b[builtin_i.true_] = true_
716 b[builtin_i.false_] = pure_osh.Boolean(1)
717
718 b[builtin_i.alias] = pure_osh.Alias(aliases, errfmt)
719 b[builtin_i.unalias] = pure_osh.UnAlias(aliases, errfmt)
720
721 b[builtin_i.getopts] = pure_osh.GetOpts(mem, errfmt)
722
723 b[builtin_i.shift] = assign_osh.Shift(mem)
724 b[builtin_i.unset] = assign_osh.Unset(mem, procs, unsafe_arith, errfmt)
725
726 b[builtin_i.append] = pure_ysh.Append(mem, errfmt)
727
728 # test / [ differ by need_right_bracket
729 b[builtin_i.test] = bracket_osh.Test(False, exec_opts, mem, errfmt)
730 b[builtin_i.bracket] = bracket_osh.Test(True, exec_opts, mem, errfmt)
731
732 # Output
733 b[builtin_i.echo] = io_osh.Echo(exec_opts)
734 b[builtin_i.printf] = printf_osh.Printf(mem, parse_ctx, unsafe_arith,
735 errfmt)
736 b[builtin_i.write] = io_ysh.Write(mem, errfmt)
737 redir_builtin = io_ysh.RunBlock(mem, cmd_ev) # used only for redirects
738 b[builtin_i.redir] = redir_builtin
739 b[builtin_i.fopen] = redir_builtin # alias for backward compatibility
740
741 # (pp output format isn't stable)
742 b[builtin_i.pp] = io_ysh.Pp(expr_ev, mem, errfmt, procs, arena)
743
744 cat = private_ysh.Cat(errfmt)
745 b[builtin_i.cat] = cat
746 b[builtin_i.read] = read_osh.Read(splitter, mem, parse_ctx, cmd_ev, errfmt)
747
748 # PRIVATE builtins
749 b[builtin_i.sleep] = private_ysh.Sleep(cmd_ev, signal_safe)
750 b[builtin_i.rm] = private_ysh.Rm(errfmt)
751
752 mapfile = io_osh.MapFile(mem, errfmt, cmd_ev)
753 b[builtin_i.mapfile] = mapfile
754 b[builtin_i.readarray] = mapfile
755
756 # Dirs
757 dir_stack = dirs_osh.DirStack()
758 cd_builtin = dirs_osh.Cd(mem, dir_stack, cmd_ev, errfmt)
759 b[builtin_i.cd] = cd_builtin
760 b[builtin_i.chdir] = cd_builtin # alias that dash, mksh, zsh have
761 b[builtin_i.pushd] = dirs_osh.Pushd(mem, dir_stack, errfmt)
762 b[builtin_i.popd] = dirs_osh.Popd(mem, dir_stack, errfmt)
763 b[builtin_i.dirs] = dirs_osh.Dirs(mem, dir_stack, errfmt)
764 b[builtin_i.pwd] = dirs_osh.Pwd(mem, errfmt)
765
766 b[builtin_i.times] = misc_osh.Times()
767
768 b[builtin_i.json] = json_ysh.Json(mem, errfmt, False)
769 b[builtin_i.json8] = json_ysh.Json(mem, errfmt, True)
770
771 ### Process builtins
772 b[builtin_i.exec_] = process_osh.Exec(mem, ext_prog, fd_state, search_path,
773 errfmt)
774 b[builtin_i.umask] = umask_osh.Umask()
775 b[builtin_i.ulimit] = process_osh.Ulimit()
776 b[builtin_i.wait] = process_osh.Wait(waiter, job_list, mem, tracer, errfmt)
777
778 b[builtin_i.jobs] = process_osh.Jobs(job_list)
779 b[builtin_i.fg] = process_osh.Fg(job_control, job_list, waiter)
780 b[builtin_i.kill] = process_osh.Kill(job_list)
781 b[builtin_i.bg] = process_osh.Bg(job_list)
782
783 # Could be in process_ysh
784 b[builtin_i.fork] = process_osh.Fork(shell_ex)
785 b[builtin_i.forkwait] = process_osh.ForkWait(shell_ex)
786
787 # Interactive builtins depend on readline
788 bindx_cb = readline_osh.BindXCallback(eval_builtin, mem, errfmt)
789 b[builtin_i.bind] = readline_osh.Bind(readline, errfmt, bindx_cb)
790 b[builtin_i.history] = readline_osh.History(readline, sh_files, errfmt,
791 mylib.Stdout())
792 b[builtin_i.fc] = readline_osh.Fc(exec_opts, readline, mylib.Stdout())
793
794 # Completion
795 spec_builder = completion_osh.SpecBuilder(cmd_ev, parse_ctx, word_ev,
796 splitter, comp_lookup, help_data,
797 errfmt)
798 complete_builtin = completion_osh.Complete(spec_builder, comp_lookup)
799 b[builtin_i.complete] = complete_builtin
800 b[builtin_i.compgen] = completion_osh.CompGen(spec_builder)
801 b[builtin_i.compopt] = completion_osh.CompOpt(compopt_state, errfmt)
802 b[builtin_i.compadjust] = completion_osh.CompAdjust(mem)
803
804 comp_ev = word_eval.CompletionWordEvaluator(mem, exec_opts, mutable_opts,
805 tilde_ev, splitter, errfmt)
806
807 comp_ev.arith_ev = arith_ev
808 comp_ev.expr_ev = expr_ev
809 comp_ev.prompt_ev = prompt_ev
810 comp_ev.CheckCircularDeps()
811
812 root_comp = completion.RootCompleter(comp_ev, mem, comp_lookup,
813 compopt_state, comp_ui_state,
814 comp_ctx, debug_f)
815 b[builtin_i.compexport] = completion_ysh.CompExport(root_comp)
816
817 #
818 # Initialize Builtin-in Methods
819 #
820
821 methods[value_e.Str] = {
822 'startsWith': method_str.HasAffix(method_str.START),
823 'endsWith': method_str.HasAffix(method_str.END),
824 'trim': method_str.Trim(method_str.START | method_str.END),
825 'trimStart': method_str.Trim(method_str.START),
826 'trimEnd': method_str.Trim(method_str.END),
827 'upper': method_str.Upper(),
828 'lower': method_str.Lower(),
829 'split': method_str.Split(),
830 'lines': method_str.Lines(),
831
832 # finds a substring, optional named parameters specifying the slice
833 # of the string to search in - [start:end]
834 'find': method_str.Find(method_str.START),
835 'findLast': method_str.Find(method_str.END),
836 'contains': method_str.Contains(),
837
838 # replace substring, OR an eggex
839 # takes count=3, the max number of replacements to do.
840 'replace': method_str.Replace(mem, expr_ev),
841
842 # Like Python's re.search, except we put it on the string object
843 # It's more consistent with Str->find(substring, pos=0)
844 # It returns value.Match() rather than an integer
845 'search': method_str.SearchMatch(method_str.SEARCH),
846
847 # like Python's re.match()
848 'leftMatch': method_str.SearchMatch(method_str.LEFT_MATCH),
849
850 # like Python's re.fullmatch(), not sure if we really need it
851 'fullMatch': None,
852 }
853 methods[value_e.Dict] = {
854 # keys() values() get() are FREE functions, not methods
855 # I think items() isn't as necessary because dicts are ordered? YSH
856 # code shouldn't use the List of Lists representation.
857 'M/erase': method_dict.Erase(),
858 'M/clear': method_dict.Clear(),
859 'M/inc': method_dict.Inc(),
860 'M/append': method_dict.Append(),
861 'M/update': method_dict.Update(),
862
863 # DEPRECATED - use free functions
864 'get': method_dict.Get(),
865 'keys': method_dict.Keys(),
866 'values': method_dict.Values(),
867 }
868 methods[value_e.List] = {
869 'M/reverse': method_list.Reverse(),
870 'M/append': method_list.Append(),
871 'M/clear': method_list.Clear(),
872 'M/extend': method_list.Extend(),
873 'M/pop': method_list.Pop(),
874 'M/insert': method_list.Insert(),
875 'M/remove': method_list.Remove(),
876 'indexOf': method_list.IndexOf(), # return first index of value, or -1
877 # Python list() has index(), which raises ValueError
878 # But this is consistent with Str->find(), and doesn't
879 # use exceptions
880 'lastIndexOf': method_list.LastIndexOf(),
881 'join': func_misc.Join(), # both a method and a func
882 }
883
884 methods[value_e.Match] = {
885 'group': func_eggex.MatchMethod(func_eggex.G, expr_ev),
886 'start': func_eggex.MatchMethod(func_eggex.S, None),
887 'end': func_eggex.MatchMethod(func_eggex.E, None),
888 }
889
890 methods[value_e.Place] = {
891 # __mut_setValue()
892
893 # instead of setplace keyword
894 'M/setValue': method_other.SetValue(mem),
895 }
896
897 methods[value_e.Command] = {
898 # var x = ^(echo hi)
899 # p { echo hi }
900 # Export source code and location
901 # Useful for test frameworks, built systems and so forth
902 'sourceCode': method_other.SourceCode(),
903 }
904
905 methods[value_e.Proc] = {
906 'docComment': method_other.DocComment(),
907 }
908
909 methods[value_e.DebugFrame] = {
910 'toString': func_reflect.DebugFrameToString(),
911 }
912
913 #
914 # Initialize Built-in Funcs
915 #
916
917 # Pure functions
918 _AddBuiltinFunc(mem, 'eval',
919 method_io.Eval(mem, cmd_ev, pure_ex, method_io.EVAL_NULL))
920 _AddBuiltinFunc(mem, 'evalExpr',
921 method_io.EvalExpr(expr_ev, pure_ex, cmd_ev))
922
923 parse_hay = func_hay.ParseHay(fd_state, parse_ctx, mem, errfmt)
924 eval_hay = func_hay.EvalHay(hay_state, mutable_opts, mem, cmd_ev)
925 hay_func = func_hay.HayFunc(hay_state)
926
927 _AddBuiltinFunc(mem, 'parseHay', parse_hay)
928 _AddBuiltinFunc(mem, 'evalHay', eval_hay)
929 _AddBuiltinFunc(mem, '_hay', hay_func)
930
931 _AddBuiltinFunc(mem, 'len', func_misc.Len())
932 _AddBuiltinFunc(mem, 'type', func_misc.Type())
933
934 g = func_eggex.MatchFunc(func_eggex.G, expr_ev, mem)
935 _AddBuiltinFunc(mem, '_group', g)
936 _AddBuiltinFunc(mem, '_match',
937 g) # TODO: remove this backward compat alias
938 _AddBuiltinFunc(mem, '_start',
939 func_eggex.MatchFunc(func_eggex.S, None, mem))
940 _AddBuiltinFunc(mem, '_end', func_eggex.MatchFunc(func_eggex.E, None, mem))
941
942 # TODO: should this be parseCommandStr() vs. parseFile() for Hay?
943 _AddBuiltinFunc(mem, 'parseCommand',
944 func_reflect.ParseCommand(parse_ctx, mem, errfmt))
945 _AddBuiltinFunc(mem, 'parseExpr',
946 func_reflect.ParseExpr(parse_ctx, errfmt))
947
948 _AddBuiltinFunc(mem, 'shvarGet', func_reflect.Shvar_get(mem))
949 _AddBuiltinFunc(mem, 'getVar', func_reflect.GetVar(mem))
950 _AddBuiltinFunc(mem, 'setVar', func_reflect.SetVar(mem))
951
952 # TODO: implement bindFrame() to turn CommandFrag -> Command
953 # Then parseCommand() and parseHay() will not depend on mem; they will not
954 # bind a frame yet
955 #
956 # what about newFrame() and globalFrame()?
957 _AddBuiltinFunc(mem, 'bindFrame', func_reflect.BindFrame())
958
959 _AddBuiltinFunc(mem, 'Object', func_misc.Object())
960
961 _AddBuiltinFunc(mem, 'rest', func_misc.Prototype())
962 _AddBuiltinFunc(mem, 'first', func_misc.PropView())
963
964 # TODO: remove these aliases
965 _AddBuiltinFunc(mem, 'prototype', func_misc.Prototype())
966 _AddBuiltinFunc(mem, 'propView', func_misc.PropView())
967
968 # type conversions
969 _AddBuiltinFunc(mem, 'bool', func_misc.Bool())
970 _AddBuiltinFunc(mem, 'int', func_misc.Int())
971 _AddBuiltinFunc(mem, 'float', func_misc.Float())
972 _AddBuiltinFunc(mem, 'str', func_misc.Str_())
973 _AddBuiltinFunc(mem, 'list', func_misc.List_())
974 _AddBuiltinFunc(mem, 'dict', func_misc.DictFunc())
975
976 # Dict functions
977 _AddBuiltinFunc(mem, 'get', method_dict.Get())
978 _AddBuiltinFunc(mem, 'keys', method_dict.Keys())
979 _AddBuiltinFunc(mem, 'values', method_dict.Values())
980
981 _AddBuiltinFunc(mem, 'runes', func_misc.Runes())
982 _AddBuiltinFunc(mem, 'encodeRunes', func_misc.EncodeRunes())
983 _AddBuiltinFunc(mem, 'bytes', func_misc.Bytes())
984 _AddBuiltinFunc(mem, 'encodeBytes', func_misc.EncodeBytes())
985
986 # Str
987 _AddBuiltinFunc(mem, 'strcmp', func_misc.StrCmp())
988 # TODO: This should be Python style splitting
989 _AddBuiltinFunc(mem, 'split', func_misc.Split(splitter))
990 _AddBuiltinFunc(mem, 'shSplit', func_misc.Split(splitter))
991
992 # Float
993 _AddBuiltinFunc(mem, 'floatsEqual', func_misc.FloatsEqual())
994
995 # List
996 _AddBuiltinFunc(mem, 'join', func_misc.Join())
997 _AddBuiltinFunc(mem, 'maybe', func_misc.Maybe())
998 # TODO: deprecate in favor of io.glob()
999 _AddBuiltinFunc(mem, 'glob', method_io.Glob(globber))
1000
1001 # Serialize
1002 _AddBuiltinFunc(mem, 'toJson8', func_misc.ToJson8(True))
1003 _AddBuiltinFunc(mem, 'toJson', func_misc.ToJson8(False))
1004
1005 _AddBuiltinFunc(mem, 'fromJson8', func_misc.FromJson8(True))
1006 _AddBuiltinFunc(mem, 'fromJson', func_misc.FromJson8(False))
1007
1008 mem.AddBuiltin('io', io_obj)
1009 mem.AddBuiltin('vm', vm_obj)
1010
1011 # Special case for testing
1012 mem.AddBuiltin('module-invoke', value.BuiltinProc(module_invoke))
1013
1014 # First, process --eval flags. In interactive mode, this comes before --rcfile.
1015 # (It could be used for the headless shell. Although terminals have a
1016 # bootstrap process.)
1017
1018 for path, is_pure in attrs.eval_flags:
1019 ex = pure_ex if is_pure else None
1020 with vm.ctx_MaybePure(ex, cmd_ev):
1021 try:
1022 ok, status = main_loop.EvalFile(path, fd_state, parse_ctx,
1023 cmd_ev, lang)
1024 except util.HardExit as e:
1025 # Doesn't seem like we need this, and verbose_errexit isn't the right option
1026 #if exec_opts.verbose_errexit():
1027 # print-stderr('oils: --eval exit')
1028 return e.status
1029
1030 # I/O error opening file, parse error. Message was already printed.
1031 if not ok:
1032 return 1
1033
1034 # YSH will stop on errors. OSH keep going, a bit like 'source'.
1035 if status != 0 and exec_opts.errexit():
1036 return status
1037
1038 #
1039 # Is the shell interactive?
1040 #
1041
1042 # History evaluation is a no-op if readline is None.
1043 hist_ev = history.Evaluator(readline, hist_ctx, debug_f)
1044
1045 if flag.c is not None:
1046 src = source.CFlag # type: source_t
1047 line_reader = reader.StringLineReader(flag.c,
1048 arena) # type: reader._Reader
1049 if flag.i: # -c and -i can be combined
1050 mutable_opts.set_interactive()
1051
1052 elif flag.i: # force interactive
1053 src = source.Stdin(' -i')
1054 line_reader = reader.InteractiveLineReader(arena, prompt_ev, hist_ev,
1055 readline, prompt_state)
1056 mutable_opts.set_interactive()
1057
1058 else:
1059 if script_name is None:
1060 if flag.headless:
1061 src = source.Headless
1062 line_reader = None # unused!
1063 # Not setting '-i' flag for now. Some people's bashrc may want it?
1064 else:
1065 stdin_ = mylib.Stdin()
1066 # --tool never starts a prompt
1067 if len(flag.tool) == 0 and stdin_.isatty():
1068 src = source.Interactive
1069 line_reader = reader.InteractiveLineReader(
1070 arena, prompt_ev, hist_ev, readline, prompt_state)
1071 mutable_opts.set_interactive()
1072 else:
1073 src = source.Stdin('')
1074 line_reader = reader.FileLineReader(stdin_, arena)
1075 else:
1076 src = source.MainFile(script_name)
1077 try:
1078 f = fd_state.Open(script_name)
1079 except (IOError, OSError) as e:
1080 print_stderr("%s: Couldn't open %r: %s" %
1081 (lang, script_name, posix.strerror(e.errno)))
1082 return 1
1083 line_reader = reader.FileLineReader(f, arena)
1084
1085 # Pretend it came from somewhere else
1086 if flag.location_str is not None:
1087 src = source.Synthetic(flag.location_str)
1088 assert line_reader is not None
1089 location_start_line = mops.BigTruncate(flag.location_start_line)
1090 if location_start_line != -1:
1091 line_reader.SetLineOffset(location_start_line)
1092
1093 arena.PushSource(src)
1094
1095 # Calculate ~/.config/oils/oshrc or yshrc. Used for both -i and --headless
1096 # We avoid cluttering the user's home directory. Some users may want to ln
1097 # -s ~/.config/oils/oshrc ~/oshrc or ~/.oshrc.
1098
1099 # https://unix.stackexchange.com/questions/24347/why-do-some-applications-use-config-appname-for-their-config-data-while-other
1100
1101 config_dir = '.config/oils'
1102 rc_paths = [] # type: List[str]
1103 if flag.headless or exec_opts.interactive():
1104 if flag.norc:
1105 # bash doesn't have this warning, but it's useful
1106 if flag.rcfile is not None:
1107 print_stderr('%s warning: --rcfile ignored with --norc' % lang)
1108 if flag.rcdir is not None:
1109 print_stderr('%s warning: --rcdir ignored with --norc' % lang)
1110 else:
1111 # User's rcfile comes FIRST. Later we can add an 'after-rcdir' hook
1112 rc_path = flag.rcfile
1113 if rc_path is None:
1114 rc_paths.append(
1115 os_path.join(home_dir, '%s/%src' % (config_dir, lang)))
1116 else:
1117 rc_paths.append(rc_path)
1118
1119 # Load all files in ~/.config/oils/oshrc.d or oilrc.d
1120 # This way "installers" can avoid mutating oshrc directly
1121
1122 rc_dir = flag.rcdir
1123 if rc_dir is None:
1124 rc_dir = os_path.join(home_dir,
1125 '%s/%src.d' % (config_dir, lang))
1126
1127 rc_paths.extend(libc.glob(os_path.join(rc_dir, '*'), 0))
1128
1129 # Initialize even in non-interactive shell, for 'compexport'
1130 _InitDefaultCompletions(cmd_ev, complete_builtin, comp_lookup)
1131
1132 if flag.headless:
1133 sh_init.InitInteractive(mem, sh_files, lang)
1134 mutable_opts.set_redefine_const()
1135 mutable_opts.set_redefine_source()
1136
1137 # NOTE: rc files loaded AFTER _InitDefaultCompletions.
1138 for rc_path in rc_paths:
1139 with state.ctx_ThisDir(mem, rc_path):
1140 try:
1141 SourceStartupFile(fd_state, rc_path, lang, parse_ctx,
1142 cmd_ev, errfmt)
1143 except util.HardExit as e:
1144 return e.status
1145
1146 loop = main_loop.Headless(cmd_ev, parse_ctx, errfmt)
1147 try:
1148 # TODO: What other exceptions happen here?
1149 status = loop.Loop()
1150 except util.HardExit as e:
1151 status = e.status
1152
1153 # Same logic as interactive shell
1154 mut_status = IntParamBox(status)
1155 cmd_ev.RunTrapsOnExit(mut_status)
1156 status = mut_status.i
1157
1158 return status
1159
1160 # Note: headless mode above doesn't use c_parser
1161 assert line_reader is not None
1162 c_parser = parse_ctx.MakeOshParser(line_reader)
1163
1164 if exec_opts.interactive():
1165 sh_init.InitInteractive(mem, sh_files, lang)
1166 # bash: 'set -o emacs' is the default only in the interactive shell
1167 mutable_opts.set_emacs()
1168 mutable_opts.set_redefine_const()
1169 mutable_opts.set_redefine_source()
1170
1171 # NOTE: rc files loaded AFTER _InitDefaultCompletions.
1172 for rc_path in rc_paths:
1173 with state.ctx_ThisDir(mem, rc_path):
1174 try:
1175 SourceStartupFile(fd_state, rc_path, lang, parse_ctx,
1176 cmd_ev, errfmt)
1177 except util.HardExit as e:
1178 return e.status
1179
1180 completion_display = state.MaybeString(mem, 'OILS_COMP_UI')
1181 if completion_display is None:
1182 completion_display = flag.completion_display
1183
1184 if readline:
1185 if completion_display == 'nice':
1186 display = comp_ui.NiceDisplay(
1187 comp_ui_state, prompt_state, debug_f, readline,
1188 signal_safe) # type: comp_ui._IDisplay
1189 else:
1190 display = comp_ui.MinimalDisplay(comp_ui_state, prompt_state,
1191 debug_f, signal_safe)
1192
1193 comp_ui.InitReadline(readline, sh_files.HistoryFile(), root_comp,
1194 display, debug_f)
1195
1196 if flag.completion_demo:
1197 _CompletionDemo(comp_lookup)
1198
1199 else: # Without readline module
1200 display = comp_ui.MinimalDisplay(comp_ui_state, prompt_state,
1201 debug_f, signal_safe)
1202
1203 process.InitInteractiveShell(signal_safe) # Set signal handlers
1204 # The interactive shell leads a process group which controls the terminal.
1205 # It MUST give up the terminal afterward, otherwise we get SIGTTIN /
1206 # SIGTTOU bugs.
1207 with process.ctx_TerminalControl(job_control, errfmt):
1208
1209 assert line_reader is not None
1210 line_reader.Reset() # After sourcing startup file, render $PS1
1211
1212 prompt_plugin = prompt.UserPlugin(mem, parse_ctx, cmd_ev, errfmt)
1213 try:
1214 status = main_loop.Interactive(flag, cmd_ev, c_parser, display,
1215 prompt_plugin, waiter, errfmt)
1216 except util.HardExit as e:
1217 status = e.status
1218
1219 mut_status = IntParamBox(status)
1220 cmd_ev.RunTrapsOnExit(mut_status)
1221 status = mut_status.i
1222
1223 if readline:
1224 hist_file = sh_files.HistoryFile()
1225 if hist_file is not None:
1226 try:
1227 readline.write_history_file(hist_file)
1228 except (IOError, OSError):
1229 pass
1230
1231 return status
1232
1233 if flag.rcfile is not None: # bash doesn't have this warning, but it's useful
1234 print_stderr('%s warning: --rcfile ignored in non-interactive shell' %
1235 lang)
1236 if flag.rcdir is not None:
1237 print_stderr('%s warning: --rcdir ignored in non-interactive shell' %
1238 lang)
1239
1240 #
1241 # Tools that use the OSH/YSH parsing mode, etc.
1242 #
1243
1244 # flag.tool is '' if nothing is passed
1245 # osh --tool syntax-tree is equivalent to osh -n --one-pass-parse
1246 tool_name = 'syntax-tree' if exec_opts.noexec() else flag.tool
1247
1248 if len(tool_name):
1249 # Don't save tokens because it's slow
1250 if tool_name != 'syntax-tree':
1251 arena.SaveTokens()
1252
1253 try:
1254 node = main_loop.ParseWholeFile(c_parser)
1255 except error.Parse as e:
1256 errfmt.PrettyPrintError(e)
1257 return 2
1258
1259 with str_switch(tool_name) as case:
1260 # like osh -n
1261 if case('syntax-tree'):
1262 ui.PrintAst(node, flag)
1263
1264 elif case('tokens'):
1265 ysh_ify.PrintTokens(arena)
1266
1267 # OSH or YSH lint
1268 elif case('lint'):
1269 lint.Lint(arena, node)
1270
1271 # formatter: start with indenting
1272 elif case('fmt'):
1273 fmt.Format(arena, node)
1274
1275 # rough translator
1276 elif case('ysh-ify'):
1277 ysh_ify.Ysh_ify(arena, node)
1278
1279 # dependency resolver
1280 elif case('deps'):
1281 if mylib.PYTHON:
1282 deps.Deps(node)
1283
1284 # Test runner? Or this 'byo test'
1285 elif case('test'):
1286 raise AssertionError('TODO')
1287
1288 # TESTING ONLY: for test/lossless.sh
1289 elif case('lossless-cat'):
1290 ysh_ify.LosslessCat(arena)
1291
1292 # TESTING ONLY: one-off
1293 elif case('find-lhs-array'):
1294 # For analyzing whether we need a[x + 1]=foo
1295 # This is similar to "lint"
1296 ysh_ify.TreeFind(arena, node, errfmt)
1297
1298 else:
1299 raise AssertionError(tool_name) # flag parser validated it
1300
1301 return 0
1302
1303 #
1304 # Batch mode: shell script or -c
1305 #
1306
1307 with state.ctx_ThisDir(mem, script_name):
1308 try:
1309 status = main_loop.Batch(cmd_ev,
1310 c_parser,
1311 errfmt,
1312 cmd_flags=cmd_eval.IsMainProgram)
1313 except util.HardExit as e:
1314 status = e.status
1315 except KeyboardInterrupt:
1316 # The interactive shell handles this in main_loop.Interactive
1317 status = 130 # 128 + 2
1318 mut_status = IntParamBox(status)
1319 cmd_ev.RunTrapsOnExit(mut_status)
1320
1321 multi_trace.WriteDumps()
1322
1323 # NOTE: We haven't closed the file opened with fd_state.Open
1324 return mut_status.i