OILS / builtin / meta_osh.py View on Github | oilshell.org

467 lines, 325 significant
1#!/usr/bin/env python2
2"""
3meta_osh.py - Builtins that call back into the interpreter.
4"""
5from __future__ import print_function
6
7from _devbuild.gen import arg_types
8from _devbuild.gen.runtime_asdl import cmd_value, CommandStatus
9from _devbuild.gen.syntax_asdl import source, loc
10from _devbuild.gen.value_asdl import value
11from core import alloc
12from core import dev
13from core import error
14from core import executor
15from core import main_loop
16from core import process
17from core.error import e_usage
18from core import pyutil # strerror
19from core import state
20from core import vm
21from data_lang import j8_lite
22from frontend import flag_util
23from frontend import consts
24from frontend import reader
25from frontend import typed_args
26from mycpp.mylib import log, print_stderr
27from pylib import os_path
28from osh import cmd_eval
29
30import posix_ as posix
31from posix_ import X_OK # translated directly to C macro
32
33_ = log
34
35from typing import Dict, List, Tuple, Optional, TYPE_CHECKING
36if TYPE_CHECKING:
37 from frontend import args
38 from frontend.parse_lib import ParseContext
39 from core import optview
40 from core import ui
41 from osh.cmd_eval import CommandEvaluator
42 from osh.cmd_parse import CommandParser
43
44
45class Eval(vm._Builtin):
46
47 def __init__(
48 self,
49 parse_ctx, # type: ParseContext
50 exec_opts, # type: optview.Exec
51 cmd_ev, # type: CommandEvaluator
52 tracer, # type: dev.Tracer
53 errfmt, # type: ui.ErrorFormatter
54 ):
55 # type: (...) -> None
56 self.parse_ctx = parse_ctx
57 self.arena = parse_ctx.arena
58 self.exec_opts = exec_opts
59 self.cmd_ev = cmd_ev
60 self.tracer = tracer
61 self.errfmt = errfmt
62
63 def Run(self, cmd_val):
64 # type: (cmd_value.Argv) -> int
65
66 if cmd_val.typed_args: # eval (myblock)
67 rd = typed_args.ReaderForProc(cmd_val)
68 block = rd.PosCommand()
69 rd.Done()
70 return self.cmd_ev.EvalCommand(block)
71
72 # There are no flags, but we need it to respect --
73 _, arg_r = flag_util.ParseCmdVal('eval', cmd_val)
74
75 if self.exec_opts.simple_eval_builtin():
76 code_str, eval_loc = arg_r.ReadRequired2('requires code string')
77 if not arg_r.AtEnd():
78 e_usage('requires exactly 1 argument', loc.Missing)
79 else:
80 code_str = ' '.join(arg_r.Rest())
81 # code_str could be EMPTY, so just use the first one
82 eval_loc = cmd_val.arg_locs[0]
83
84 line_reader = reader.StringLineReader(code_str, self.arena)
85 c_parser = self.parse_ctx.MakeOshParser(line_reader)
86
87 src = source.ArgvWord('eval', eval_loc)
88 with dev.ctx_Tracer(self.tracer, 'eval', None):
89 with alloc.ctx_SourceCode(self.arena, src):
90 return main_loop.Batch(self.cmd_ev,
91 c_parser,
92 self.errfmt,
93 cmd_flags=cmd_eval.RaiseControlFlow)
94
95
96class Source(vm._Builtin):
97
98 def __init__(
99 self,
100 parse_ctx, # type: ParseContext
101 search_path, # type: state.SearchPath
102 cmd_ev, # type: CommandEvaluator
103 fd_state, # type: process.FdState
104 tracer, # type: dev.Tracer
105 errfmt, # type: ui.ErrorFormatter
106 loader, # type: pyutil._ResourceLoader
107 ):
108 # type: (...) -> None
109 self.parse_ctx = parse_ctx
110 self.arena = parse_ctx.arena
111 self.search_path = search_path
112 self.cmd_ev = cmd_ev
113 self.fd_state = fd_state
114 self.tracer = tracer
115 self.errfmt = errfmt
116 self.loader = loader
117
118 self.mem = cmd_ev.mem
119
120 def Run(self, cmd_val):
121 # type: (cmd_value.Argv) -> int
122 attrs, arg_r = flag_util.ParseCmdVal('source', cmd_val)
123 arg = arg_types.source(attrs.attrs)
124
125 path = arg_r.Peek()
126 if path is None:
127 e_usage('missing required argument', loc.Missing)
128 arg_r.Next()
129
130 if arg.builtin:
131 try:
132 path = os_path.join("stdlib", path)
133 contents = self.loader.Get(path)
134 except (IOError, OSError):
135 self.errfmt.Print_(
136 'source --builtin %r failed: No such builtin file' % path,
137 blame_loc=cmd_val.arg_locs[2])
138 return 2
139
140 line_reader = reader.StringLineReader(contents, self.arena)
141 c_parser = self.parse_ctx.MakeOshParser(line_reader)
142 return self._Exec(cmd_val, arg_r, path, c_parser)
143
144 else:
145 # 'source' respects $PATH
146 resolved = self.search_path.LookupOne(path, exec_required=False)
147 if resolved is None:
148 resolved = path
149
150 try:
151 # Shell can't use descriptors 3-9
152 f = self.fd_state.Open(resolved)
153 except (IOError, OSError) as e:
154 self.errfmt.Print_('source %r failed: %s' %
155 (path, pyutil.strerror(e)),
156 blame_loc=cmd_val.arg_locs[1])
157 return 1
158
159 line_reader = reader.FileLineReader(f, self.arena)
160 c_parser = self.parse_ctx.MakeOshParser(line_reader)
161
162 with process.ctx_FileCloser(f):
163 return self._Exec(cmd_val, arg_r, path, c_parser)
164
165 def _Exec(self, cmd_val, arg_r, path, c_parser):
166 # type: (cmd_value.Argv, args.Reader, str, CommandParser) -> int
167 call_loc = cmd_val.arg_locs[0]
168
169 # A sourced module CAN have a new arguments array, but it always shares
170 # the same variable scope as the caller. The caller could be at either a
171 # global or a local scope.
172
173 # TODO: I wonder if we compose the enter/exit methods more easily.
174
175 with dev.ctx_Tracer(self.tracer, 'source', cmd_val.argv):
176 source_argv = arg_r.Rest()
177 with state.ctx_Source(self.mem, path, source_argv):
178 with state.ctx_ThisDir(self.mem, path):
179 src = source.SourcedFile(path, call_loc)
180 with alloc.ctx_SourceCode(self.arena, src):
181 try:
182 status = main_loop.Batch(
183 self.cmd_ev,
184 c_parser,
185 self.errfmt,
186 cmd_flags=cmd_eval.RaiseControlFlow)
187 except vm.IntControlFlow as e:
188 if e.IsReturn():
189 status = e.StatusCode()
190 else:
191 raise
192
193 return status
194
195
196def _PrintFreeForm(row):
197 # type: (Tuple[str, str, Optional[str]]) -> None
198 name, kind, resolved = row
199
200 if kind == 'file':
201 what = resolved
202 elif kind == 'alias':
203 what = ('an alias for %s' %
204 j8_lite.EncodeString(resolved, unquoted_ok=True))
205 else: # builtin, function, keyword
206 what = 'a shell %s' % kind
207
208 # TODO: Should also print haynode
209
210 print('%s is %s' % (name, what))
211
212 # if kind == 'function':
213 # bash is the only shell that prints the function
214
215
216def _PrintEntry(arg, row):
217 # type: (arg_types.type, Tuple[str, str, Optional[str]]) -> None
218
219 _, kind, resolved = row
220 assert kind is not None
221
222 if arg.t: # short string
223 print(kind)
224
225 elif arg.p:
226 #log('%s %s %s', name, kind, resolved)
227 if kind == 'file':
228 print(resolved)
229
230 else: # free-form text
231 _PrintFreeForm(row)
232
233
234class Command(vm._Builtin):
235 """'command ls' suppresses function lookup."""
236
237 def __init__(
238 self,
239 shell_ex, # type: vm._Executor
240 funcs, # type: Dict[str, value.Proc]
241 aliases, # type: Dict[str, str]
242 search_path, # type: state.SearchPath
243 ):
244 # type: (...) -> None
245 self.shell_ex = shell_ex
246 self.funcs = funcs
247 self.aliases = aliases
248 self.search_path = search_path
249
250 def Run(self, cmd_val):
251 # type: (cmd_value.Argv) -> int
252
253 # accept_typed_args=True because we invoke other builtins
254 attrs, arg_r = flag_util.ParseCmdVal('command',
255 cmd_val,
256 accept_typed_args=True)
257 arg = arg_types.command(attrs.attrs)
258
259 argv, locs = arg_r.Rest2()
260
261 if arg.v or arg.V:
262 status = 0
263 for argument in argv:
264 r = _ResolveName(argument, self.funcs, self.aliases,
265 self.search_path, False)
266 if len(r):
267 # command -v prints the name (-V is more detailed)
268 # Print it only once.
269 row = r[0]
270 name, _, _ = row
271 if arg.v:
272 print(name)
273 else:
274 _PrintFreeForm(row)
275 else:
276 # match bash behavior by printing to stderr
277 print_stderr('%s: not found' % argument)
278 status = 1 # nothing printed, but we fail
279
280 return status
281
282 cmd_val2 = cmd_value.Argv(argv, locs, cmd_val.typed_args,
283 cmd_val.pos_args, cmd_val.named_args)
284
285 # If we respected do_fork here instead of passing True, the case
286 # 'command date | wc -l' would take 2 processes instead of 3. But no other
287 # shell does that, and this rare case isn't worth the bookkeeping.
288 # See test/syscall
289 cmd_st = CommandStatus.CreateNull(alloc_lists=True)
290
291 run_flags = executor.DO_FORK | executor.NO_CALL_PROCS
292 if arg.p:
293 run_flags |= executor.USE_DEFAULT_PATH
294
295 return self.shell_ex.RunSimpleCommand(cmd_val2, cmd_st, run_flags)
296
297
298def _ShiftArgv(cmd_val):
299 # type: (cmd_value.Argv) -> cmd_value.Argv
300 return cmd_value.Argv(cmd_val.argv[1:], cmd_val.arg_locs[1:],
301 cmd_val.typed_args, cmd_val.pos_args,
302 cmd_val.named_args)
303
304
305class Builtin(vm._Builtin):
306
307 def __init__(self, shell_ex, errfmt):
308 # type: (vm._Executor, ui.ErrorFormatter) -> None
309 self.shell_ex = shell_ex
310 self.errfmt = errfmt
311
312 def Run(self, cmd_val):
313 # type: (cmd_value.Argv) -> int
314
315 if len(cmd_val.argv) == 1:
316 return 0 # this could be an error in strict mode?
317
318 name = cmd_val.argv[1]
319
320 # Run regular builtin or special builtin
321 to_run = consts.LookupNormalBuiltin(name)
322 if to_run == consts.NO_INDEX:
323 to_run = consts.LookupSpecialBuiltin(name)
324 if to_run == consts.NO_INDEX:
325 location = cmd_val.arg_locs[1]
326 if consts.LookupAssignBuiltin(name) != consts.NO_INDEX:
327 # NOTE: There's a similar restriction for 'command'
328 self.errfmt.Print_("Can't run assignment builtin recursively",
329 blame_loc=location)
330 else:
331 self.errfmt.Print_("%r isn't a shell builtin" % name,
332 blame_loc=location)
333 return 1
334
335 cmd_val2 = _ShiftArgv(cmd_val)
336 return self.shell_ex.RunBuiltin(to_run, cmd_val2)
337
338
339class RunProc(vm._Builtin):
340
341 def __init__(self, shell_ex, procs, errfmt):
342 # type: (vm._Executor, Dict[str, value.Proc], ui.ErrorFormatter) -> None
343 self.shell_ex = shell_ex
344 self.procs = procs
345 self.errfmt = errfmt
346
347 def Run(self, cmd_val):
348 # type: (cmd_value.Argv) -> int
349 _, arg_r = flag_util.ParseCmdVal('runproc',
350 cmd_val,
351 accept_typed_args=True)
352 argv, locs = arg_r.Rest2()
353
354 if len(argv) == 0:
355 raise error.Usage('requires arguments', loc.Missing)
356
357 name = argv[0]
358 if name not in self.procs:
359 self.errfmt.PrintMessage('runproc: no proc named %r' % name)
360 return 1
361
362 cmd_val2 = cmd_value.Argv(argv, locs, cmd_val.typed_args,
363 cmd_val.pos_args, cmd_val.named_args)
364
365 cmd_st = CommandStatus.CreateNull(alloc_lists=True)
366 return self.shell_ex.RunSimpleCommand(cmd_val2, cmd_st,
367 executor.DO_FORK)
368
369
370def _ResolveName(
371 name, # type: str
372 funcs, # type: Dict[str, value.Proc]
373 aliases, # type: Dict[str, str]
374 search_path, # type: state.SearchPath
375 do_all, # type: bool
376):
377 # type: (...) -> List[Tuple[str, str, Optional[str]]]
378
379 # MyPy tuple type
380 no_str = None # type: Optional[str]
381
382 results = [] # type: List[Tuple[str, str, Optional[str]]]
383
384 if name in funcs:
385 results.append((name, 'function', no_str))
386
387 if name in aliases:
388 results.append((name, 'alias', aliases[name]))
389
390 # See if it's a builtin
391 if consts.LookupNormalBuiltin(name) != 0:
392 results.append((name, 'builtin', no_str))
393 elif consts.LookupSpecialBuiltin(name) != 0:
394 results.append((name, 'builtin', no_str))
395 elif consts.LookupAssignBuiltin(name) != 0:
396 results.append((name, 'builtin', no_str))
397
398 # See if it's a keyword
399 if consts.IsControlFlow(name): # continue, etc.
400 results.append((name, 'keyword', no_str))
401 elif consts.IsKeyword(name):
402 results.append((name, 'keyword', no_str))
403
404 # See if it's external
405 for path in search_path.LookupReflect(name, do_all):
406 if posix.access(path, X_OK):
407 results.append((name, 'file', path))
408
409 return results
410
411
412class Type(vm._Builtin):
413
414 def __init__(
415 self,
416 funcs, # type: Dict[str, value.Proc]
417 aliases, # type: Dict[str, str]
418 search_path, # type: state.SearchPath
419 errfmt, # type: ui.ErrorFormatter
420 ):
421 # type: (...) -> None
422 self.funcs = funcs
423 self.aliases = aliases
424 self.search_path = search_path
425 self.errfmt = errfmt
426
427 def Run(self, cmd_val):
428 # type: (cmd_value.Argv) -> int
429 attrs, arg_r = flag_util.ParseCmdVal('type', cmd_val)
430 arg = arg_types.type(attrs.attrs)
431
432 if arg.f: # suppress function lookup
433 funcs = {} # type: Dict[str, value.Proc]
434 else:
435 funcs = self.funcs
436
437 status = 0
438 names = arg_r.Rest()
439
440 if arg.P: # -P should forces PATH search, regardless of builtin/alias/function/etc.
441 for name in names:
442 paths = self.search_path.LookupReflect(name, arg.a)
443 if len(paths):
444 for path in paths:
445 print(path)
446 else:
447 status = 1
448 return status
449
450 for argument in names:
451 r = _ResolveName(argument, funcs, self.aliases, self.search_path,
452 arg.a)
453 if arg.a:
454 for row in r:
455 _PrintEntry(arg, row)
456 else:
457 if len(r): # Just print the first one
458 _PrintEntry(arg, r[0])
459
460 # Error case
461 if len(r) == 0:
462 if not arg.t: # 'type -t' is silent in this case
463 # match bash behavior by printing to stderr
464 print_stderr('%s: not found' % argument)
465 status = 1 # nothing printed, but we fail
466
467 return status