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

588 lines, 372 significant
1#!/usr/bin/env python2
2"""
3pure_osh.py - Builtins that don't do any I/O.
4
5If the OSH interpreter were embedded in another program, these builtins can be
6safely used, e.g. without worrying about modifying the file system.
7
8NOTE: There can be spew on stdout, e.g. for shopt -p and so forth.
9"""
10from __future__ import print_function
11
12from _devbuild.gen import arg_types
13from _devbuild.gen.syntax_asdl import loc
14from _devbuild.gen.types_asdl import opt_group_i
15
16from core import error
17from core.error import e_usage
18from core import state
19from display import ui
20from core import vm
21from data_lang import j8_lite
22from frontend import args
23from frontend import consts
24from frontend import flag_util
25from frontend import match
26from frontend import typed_args
27from mycpp import mylib
28from mycpp.mylib import print_stderr, log
29
30from typing import List, Dict, Tuple, Optional, TYPE_CHECKING
31if TYPE_CHECKING:
32 from _devbuild.gen.runtime_asdl import cmd_value
33 from core import optview
34 from core.state import MutableOpts, Mem
35 from core import executor
36 from osh.cmd_eval import CommandEvaluator
37
38_ = log
39
40
41class Boolean(vm._Builtin):
42 """For :, true, false."""
43
44 def __init__(self, status):
45 # type: (int) -> None
46 self.status = status
47
48 def Run(self, cmd_val):
49 # type: (cmd_value.Argv) -> int
50
51 # These ignore regular args, but shouldn't accept typed args.
52 typed_args.DoesNotAccept(cmd_val.proc_args)
53 return self.status
54
55
56class Alias(vm._Builtin):
57
58 def __init__(self, aliases, errfmt):
59 # type: (Dict[str, str], ui.ErrorFormatter) -> None
60 self.aliases = aliases
61 self.errfmt = errfmt
62
63 def Run(self, cmd_val):
64 # type: (cmd_value.Argv) -> int
65 _, arg_r = flag_util.ParseCmdVal('alias', cmd_val)
66 argv = arg_r.Rest()
67
68 if len(argv) == 0:
69 for name in sorted(self.aliases):
70 alias_exp = self.aliases[name]
71 # This is somewhat like bash, except we use %r for ''.
72 print('alias %s=%r' % (name, alias_exp))
73 return 0
74
75 status = 0
76 for i, arg in enumerate(argv):
77 name, alias_exp = mylib.split_once(arg, '=')
78 if alias_exp is None: # if we get a plain word without, print alias
79 alias_exp = self.aliases.get(name)
80 if alias_exp is None:
81 self.errfmt.Print_('No alias named %r' % name,
82 blame_loc=cmd_val.arg_locs[i])
83 status = 1
84 else:
85 print('alias %s=%r' % (name, alias_exp))
86 else:
87 self.aliases[name] = alias_exp
88
89 #print(argv)
90 #log('AFTER ALIAS %s', aliases)
91 return status
92
93
94class UnAlias(vm._Builtin):
95
96 def __init__(self, aliases, errfmt):
97 # type: (Dict[str, str], ui.ErrorFormatter) -> None
98 self.aliases = aliases
99 self.errfmt = errfmt
100
101 def Run(self, cmd_val):
102 # type: (cmd_value.Argv) -> int
103 attrs, arg_r = flag_util.ParseCmdVal('unalias', cmd_val)
104 arg = arg_types.unalias(attrs.attrs)
105
106 if arg.a:
107 self.aliases.clear()
108 return 0
109
110 argv = arg_r.Rest()
111
112 if len(argv) == 0:
113 e_usage('requires an argument', loc.Missing)
114
115 status = 0
116 for i, name in enumerate(argv):
117 if name in self.aliases:
118 mylib.dict_erase(self.aliases, name)
119 else:
120 self.errfmt.Print_('No alias named %r' % name,
121 blame_loc=cmd_val.arg_locs[i])
122 status = 1
123 return status
124
125
126def SetOptionsFromFlags(exec_opts, opt_changes, shopt_changes):
127 # type: (MutableOpts, List[Tuple[str, bool]], List[Tuple[str, bool]]) -> None
128 """Used by core/shell.py."""
129
130 # We can set ANY option with -o. -O is too annoying to type.
131 for opt_name, b in opt_changes:
132 exec_opts.SetAnyOption(opt_name, b)
133
134 for opt_name, b in shopt_changes:
135 exec_opts.SetAnyOption(opt_name, b)
136
137
138def ShowOptions(mutable_opts, opt_names):
139 # type: (state.MutableOpts, List[str]) -> bool
140 """Show traditional options, for 'set -o' and 'shopt -p -o'."""
141 # TODO: Maybe sort them differently?
142
143 if len(opt_names) == 0: # if none, supplied, show all
144 opt_names = [consts.OptionName(i) for i in consts.SET_OPTION_NUMS]
145
146 any_false = False
147 for opt_name in opt_names:
148 opt_num = state._SetOptionNum(opt_name)
149 b = mutable_opts.Get(opt_num)
150 if not b:
151 any_false = True
152 print('set %so %s' % ('-' if b else '+', opt_name))
153 return any_false
154
155
156def _ShowShoptOptions(mutable_opts, opt_nums):
157 # type: (state.MutableOpts, List[int]) -> bool
158 """For 'shopt -p'."""
159
160 if len(opt_nums) == 0:
161 # If none supplied, show all
162 # Note: the way to show BOTH shopt and set options should be a
163 # __shopt__ Dict
164 opt_nums.extend(consts.VISIBLE_SHOPT_NUMS)
165
166 any_false = False
167 for opt_num in opt_nums:
168 b = mutable_opts.Get(opt_num)
169 if not b:
170 any_false = True
171 print('shopt -%s %s' % ('s' if b else 'u', consts.OptionName(opt_num)))
172 return any_false
173
174
175class Set(vm._Builtin):
176
177 def __init__(self, exec_opts, mem):
178 # type: (MutableOpts, Mem) -> None
179 self.exec_opts = exec_opts
180 self.mem = mem
181
182 def Run(self, cmd_val):
183 # type: (cmd_value.Argv) -> int
184
185 # TODO:
186 # - How to integrate this with auto-completion? Have to handle '+'.
187
188 if len(cmd_val.argv) == 1:
189 # 'set' without args shows visible variable names and values. According
190 # to POSIX:
191 # - the names should be sorted, and
192 # - the code should be suitable for re-input to the shell. We have a
193 # spec test for this.
194 # Also:
195 # - autoconf also wants them to fit on ONE LINE.
196 # http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
197 mapping = self.mem.GetAllVars()
198 for name in sorted(mapping):
199 str_val = mapping[name]
200 code_str = '%s=%s' % (name, j8_lite.MaybeShellEncode(str_val))
201 print(code_str)
202 return 0
203
204 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
205 arg_r.Next() # skip 'set'
206 arg = flag_util.ParseMore('set', arg_r)
207
208 # 'set -o' shows options. This is actually used by autoconf-generated
209 # scripts!
210 if arg.show_options:
211 ShowOptions(self.exec_opts, [])
212 return 0
213
214 # Note: set -o nullglob is not valid. The 'shopt' builtin is preferred in
215 # YSH, and we want code to be consistent.
216 for opt_name, b in arg.opt_changes:
217 self.exec_opts.SetOldOption(opt_name, b)
218
219 for opt_name, b in arg.shopt_changes:
220 self.exec_opts.SetAnyOption(opt_name, b)
221
222 # Hm do we need saw_double_dash?
223 if arg.saw_double_dash or not arg_r.AtEnd():
224 self.mem.SetArgv(arg_r.Rest())
225 return 0
226
227
228class Shopt(vm._Builtin):
229
230 def __init__(
231 self,
232 exec_opts, # type: optview.Exec
233 mutable_opts, # type: MutableOpts
234 cmd_ev, # type: CommandEvaluator
235 mem, # type: state.Mem
236 environ, # type: Dict[str, str]
237 ):
238 # type: (...) -> None
239 self.exec_opts = exec_opts
240 self.mutable_opts = mutable_opts
241 self.cmd_ev = cmd_ev
242 self.mem = mem
243 self.environ = environ
244
245 def _PrintOptions(self, use_set_opts, opt_names):
246 # type: (bool, List[str]) -> int
247 if use_set_opts:
248 any_false = ShowOptions(self.mutable_opts, opt_names)
249
250 if len(opt_names):
251 # bash behavior: behave like -q if options are set
252 return 1 if any_false else 0
253 else:
254 return 0
255 else:
256 # Respect option groups like ysh:upgrade
257 any_single_names = False
258 opt_nums = [] # type: List[int]
259 for opt_name in opt_names:
260 opt_group = consts.OptionGroupNum(opt_name)
261 if opt_group == opt_group_i.YshUpgrade:
262 opt_nums.extend(consts.YSH_UPGRADE)
263 elif opt_group == opt_group_i.YshAll:
264 opt_nums.extend(consts.YSH_ALL)
265 elif opt_group == opt_group_i.StrictAll:
266 opt_nums.extend(consts.STRICT_ALL)
267
268 else:
269 index = consts.OptionNum(opt_name)
270 # Minor incompatibility with bash: we validate everything
271 # before printing.
272 if index == 0:
273 if self.exec_opts.ignore_shopt_not_impl():
274 index = consts.UnimplOptionNum(opt_name)
275 if index == 0:
276 e_usage('got invalid option %r' % opt_name,
277 loc.Missing)
278 opt_nums.append(index)
279 any_single_names = True
280
281 any_false = _ShowShoptOptions(self.mutable_opts, opt_nums)
282
283 if any_single_names:
284 # bash behavior: behave like -q if options are set
285 return 1 if any_false else 0
286 else:
287 return 0
288
289 def Run(self, cmd_val):
290 # type: (cmd_value.Argv) -> int
291 attrs, arg_r = flag_util.ParseCmdVal('shopt',
292 cmd_val,
293 accept_typed_args=True)
294
295 arg = arg_types.shopt(attrs.attrs)
296 opt_names = arg_r.Rest()
297
298 if arg.q: # query values
299 for name in opt_names:
300 index = consts.OptionNum(name)
301 if index == 0:
302 if self.exec_opts.ignore_shopt_not_impl():
303 index = consts.UnimplOptionNum(name)
304 if index == 0:
305 return 2 # bash gives 1 for invalid option; 2 is better
306
307 if not self.mutable_opts.opt0_array[index]:
308 return 1 # at least one option is not true
309
310 return 0 # all options are true
311
312 if arg.s:
313 b = True
314 elif arg.u:
315 b = False
316 elif arg.p: # explicit -p
317 return self._PrintOptions(arg.o, opt_names)
318 else: # otherwise -p is implicit
319 return self._PrintOptions(arg.o, opt_names)
320
321 # shopt --set x { my-block }
322 cmd_frag = typed_args.OptionalBlockAsFrag(cmd_val)
323 if cmd_frag:
324 opt_nums = [] # type: List[int]
325 for opt_name in opt_names:
326 # TODO: could consolidate with checks in core/state.py and option
327 # lexer?
328 opt_group = consts.OptionGroupNum(opt_name)
329 if opt_group == opt_group_i.YshUpgrade:
330 opt_nums.extend(consts.YSH_UPGRADE)
331 if b:
332 self.mem.MaybeInitEnvDict(self.environ)
333 continue
334
335 if opt_group == opt_group_i.YshAll:
336 opt_nums.extend(consts.YSH_ALL)
337 if b:
338 self.mem.MaybeInitEnvDict(self.environ)
339 continue
340
341 if opt_group == opt_group_i.StrictAll:
342 opt_nums.extend(consts.STRICT_ALL)
343 continue
344
345 index = consts.OptionNum(opt_name)
346 if index == 0:
347 if self.exec_opts.ignore_shopt_not_impl():
348 index = consts.UnimplOptionNum(opt_name)
349 if index == 0:
350 # TODO: location info
351 e_usage('got invalid option %r' % opt_name,
352 loc.Missing)
353 opt_nums.append(index)
354
355 with state.ctx_Option(self.mutable_opts, opt_nums, b):
356 unused = self.cmd_ev.EvalCommandFrag(cmd_frag)
357 return 0 # cd also returns 0
358
359 # Otherwise, set options.
360 ignore_shopt_not_impl = self.exec_opts.ignore_shopt_not_impl()
361 for opt_name in opt_names:
362 # We allow set -o options here
363 self.mutable_opts.SetAnyOption(opt_name, b, ignore_shopt_not_impl)
364
365 return 0
366
367
368class Hash(vm._Builtin):
369
370 def __init__(self, search_path):
371 # type: (executor.SearchPath) -> None
372 self.search_path = search_path
373
374 def Run(self, cmd_val):
375 # type: (cmd_value.Argv) -> int
376 attrs, arg_r = flag_util.ParseCmdVal('hash', cmd_val)
377 arg = arg_types.hash(attrs.attrs)
378
379 rest = arg_r.Rest()
380 if arg.r:
381 if len(rest):
382 e_usage('got extra arguments after -r', loc.Missing)
383 self.search_path.ClearCache()
384 return 0
385
386 status = 0
387 if len(rest):
388 for cmd in rest: # enter in cache
389 full_path = self.search_path.CachedLookup(cmd)
390 if full_path is None:
391 print_stderr('hash: %r not found' % cmd)
392 status = 1
393 else: # print cache
394 commands = self.search_path.CachedCommands()
395 commands.sort()
396 for cmd in commands:
397 print(cmd)
398
399 return status
400
401
402def _ParseOptSpec(spec_str):
403 # type: (str) -> Dict[str, bool]
404 spec = {} # type: Dict[str, bool]
405 i = 0
406 n = len(spec_str)
407 while True:
408 if i >= n:
409 break
410 ch = spec_str[i]
411 spec[ch] = False
412 i += 1
413 if i >= n:
414 break
415 # If the next character is :, change the value to True.
416 if spec_str[i] == ':':
417 spec[ch] = True
418 i += 1
419 return spec
420
421
422class GetOptsState(object):
423 """State persisted across invocations.
424
425 This would be simpler in GetOpts.
426 """
427
428 def __init__(self, mem, errfmt):
429 # type: (Mem, ui.ErrorFormatter) -> None
430 self.mem = mem
431 self.errfmt = errfmt
432 self._optind = -1
433 self.flag_pos = 1 # position within the arg, public var
434
435 def _OptInd(self):
436 # type: () -> int
437 """Returns OPTIND that's >= 1, or -1 if it's invalid."""
438 # Note: OPTIND could be value.Int?
439 try:
440 result = state.GetInteger(self.mem, 'OPTIND')
441 except error.Runtime as e:
442 self.errfmt.Print_(e.UserErrorString())
443 result = -1
444 return result
445
446 def GetArg(self, argv):
447 # type: (List[str]) -> Optional[str]
448 """Get the value of argv at OPTIND.
449
450 Returns None if it's out of range.
451 """
452
453 #log('_optind %d flag_pos %d', self._optind, self.flag_pos)
454
455 optind = self._OptInd()
456 if optind == -1:
457 return None
458 self._optind = optind # save for later
459
460 i = optind - 1 # 1-based index
461 #log('argv %s i %d', argv, i)
462 if 0 <= i and i < len(argv):
463 return argv[i]
464 else:
465 return None
466
467 def IncIndex(self):
468 # type: () -> None
469 """Increment OPTIND."""
470 # Note: bash-completion uses a *local* OPTIND ! Not global.
471 assert self._optind != -1
472 state.BuiltinSetString(self.mem, 'OPTIND', str(self._optind + 1))
473 self.flag_pos = 1
474
475 def SetArg(self, optarg):
476 # type: (str) -> None
477 """Set OPTARG."""
478 state.BuiltinSetString(self.mem, 'OPTARG', optarg)
479
480 def Fail(self):
481 # type: () -> None
482 """On failure, reset OPTARG."""
483 state.BuiltinSetString(self.mem, 'OPTARG', '')
484
485
486def _GetOpts(
487 spec, # type: Dict[str, bool]
488 argv, # type: List[str]
489 my_state, # type: GetOptsState
490 errfmt, # type: ui.ErrorFormatter
491):
492 # type: (...) -> Tuple[int, str]
493 current = my_state.GetArg(argv)
494 #log('current %s', current)
495
496 if current is None: # out of range, etc.
497 my_state.Fail()
498 return 1, '?'
499
500 if not current.startswith('-') or current == '-':
501 my_state.Fail()
502 return 1, '?'
503
504 flag_char = current[my_state.flag_pos]
505
506 if my_state.flag_pos < len(current) - 1:
507 my_state.flag_pos += 1 # don't move past this arg yet
508 more_chars = True
509 else:
510 my_state.IncIndex()
511 my_state.flag_pos = 1
512 more_chars = False
513
514 if flag_char not in spec: # Invalid flag
515 return 0, '?'
516
517 if spec[flag_char]: # does it need an argument?
518 if more_chars:
519 optarg = current[my_state.flag_pos:]
520 else:
521 optarg = my_state.GetArg(argv)
522 if optarg is None:
523 my_state.Fail()
524 # TODO: Add location info
525 errfmt.Print_('getopts: option %r requires an argument.' %
526 current)
527 tmp = [j8_lite.MaybeShellEncode(a) for a in argv]
528 print_stderr('(getopts argv: %s)' % ' '.join(tmp))
529
530 # Hm doesn't cause status 1?
531 return 0, '?'
532 my_state.IncIndex()
533 my_state.SetArg(optarg)
534 else:
535 my_state.SetArg('')
536
537 return 0, flag_char
538
539
540class GetOpts(vm._Builtin):
541 """
542 Vars used:
543 OPTERR: disable printing of error messages
544 Vars set:
545 The variable named by the second arg
546 OPTIND - initialized to 1 at startup
547 OPTARG - argument
548 """
549
550 def __init__(self, mem, errfmt):
551 # type: (Mem, ui.ErrorFormatter) -> None
552 self.mem = mem
553 self.errfmt = errfmt
554
555 # TODO: state could just be in this object
556 self.my_state = GetOptsState(mem, errfmt)
557 self.spec_cache = {} # type: Dict[str, Dict[str, bool]]
558
559 def Run(self, cmd_val):
560 # type: (cmd_value.Argv) -> int
561 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
562 arg_r.Next()
563
564 # NOTE: If first char is a colon, error reporting is different. Alpine
565 # might not use that?
566 spec_str = arg_r.ReadRequired('requires an argspec')
567
568 var_name, var_loc = arg_r.ReadRequired2(
569 'requires the name of a variable to set')
570
571 spec = self.spec_cache.get(spec_str)
572 if spec is None:
573 spec = _ParseOptSpec(spec_str)
574 self.spec_cache[spec_str] = spec
575
576 user_argv = self.mem.GetArgv() if arg_r.AtEnd() else arg_r.Rest()
577 #log('user_argv %s', user_argv)
578 status, flag_char = _GetOpts(spec, user_argv, self.my_state,
579 self.errfmt)
580
581 if match.IsValidVarName(var_name):
582 state.BuiltinSetString(self.mem, var_name, flag_char)
583 else:
584 # NOTE: The builtin has PARTIALLY set state. This happens in all shells
585 # except mksh.
586 raise error.Usage('got invalid variable name %r' % var_name,
587 var_loc)
588 return status