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

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