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

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