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

573 lines, 358 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):
230 # type: (optview.Exec, MutableOpts, CommandEvaluator) -> None
231 self.exec_opts = exec_opts
232 self.mutable_opts = mutable_opts
233 self.cmd_ev = cmd_ev
234
235 def _PrintOptions(self, use_set_opts, opt_names):
236 # type: (bool, List[str]) -> int
237 if use_set_opts:
238 any_false = ShowOptions(self.mutable_opts, opt_names)
239
240 if len(opt_names):
241 # bash behavior: behave like -q if options are set
242 return 1 if any_false else 0
243 else:
244 return 0
245 else:
246 # Respect option groups like ysh:upgrade
247 any_single_names = False
248 opt_nums = [] # type: List[int]
249 for opt_name in opt_names:
250 opt_group = consts.OptionGroupNum(opt_name)
251 if opt_group == opt_group_i.YshUpgrade:
252 opt_nums.extend(consts.YSH_UPGRADE)
253 elif opt_group == opt_group_i.YshAll:
254 opt_nums.extend(consts.YSH_ALL)
255 elif opt_group == opt_group_i.StrictAll:
256 opt_nums.extend(consts.STRICT_ALL)
257 else:
258 index = consts.OptionNum(opt_name)
259 # Minor incompatibility with bash: we validate everything
260 # before printing.
261 if index == 0:
262 if self.exec_opts.ignore_shopt_not_impl():
263 index = consts.UnimplOptionNum(opt_name)
264 if index == 0:
265 e_usage('got invalid option %r' % opt_name,
266 loc.Missing)
267 opt_nums.append(index)
268 any_single_names = True
269
270 any_false = _ShowShoptOptions(self.mutable_opts, opt_nums)
271
272 if any_single_names:
273 # bash behavior: behave like -q if options are set
274 return 1 if any_false else 0
275 else:
276 return 0
277
278 def Run(self, cmd_val):
279 # type: (cmd_value.Argv) -> int
280 attrs, arg_r = flag_util.ParseCmdVal('shopt',
281 cmd_val,
282 accept_typed_args=True)
283
284 arg = arg_types.shopt(attrs.attrs)
285 opt_names = arg_r.Rest()
286
287 if arg.q: # query values
288 for name in opt_names:
289 index = consts.OptionNum(name)
290 if index == 0:
291 if self.exec_opts.ignore_shopt_not_impl():
292 index = consts.UnimplOptionNum(name)
293 if index == 0:
294 return 2 # bash gives 1 for invalid option; 2 is better
295
296 if not self.mutable_opts.opt0_array[index]:
297 return 1 # at least one option is not true
298
299 return 0 # all options are true
300
301 if arg.s:
302 b = True
303 elif arg.u:
304 b = False
305 elif arg.p: # explicit -p
306 return self._PrintOptions(arg.o, opt_names)
307 else: # otherwise -p is implicit
308 return self._PrintOptions(arg.o, opt_names)
309
310 # shopt --set x { my-block }
311 cmd_frag = typed_args.OptionalBlockAsFrag(cmd_val)
312 if cmd_frag:
313 opt_nums = [] # type: List[int]
314 for opt_name in opt_names:
315 # TODO: could consolidate with checks in core/state.py and option
316 # lexer?
317 opt_group = consts.OptionGroupNum(opt_name)
318 if opt_group == opt_group_i.YshUpgrade:
319 opt_nums.extend(consts.YSH_UPGRADE)
320 continue
321
322 if opt_group == opt_group_i.YshAll:
323 opt_nums.extend(consts.YSH_ALL)
324 continue
325
326 if opt_group == opt_group_i.StrictAll:
327 opt_nums.extend(consts.STRICT_ALL)
328 continue
329
330 index = consts.OptionNum(opt_name)
331 if index == 0:
332 if self.exec_opts.ignore_shopt_not_impl():
333 index = consts.UnimplOptionNum(opt_name)
334 if index == 0:
335 # TODO: location info
336 e_usage('got invalid option %r' % opt_name,
337 loc.Missing)
338 opt_nums.append(index)
339
340 with state.ctx_Option(self.mutable_opts, opt_nums, b):
341 unused = self.cmd_ev.EvalCommandFrag(cmd_frag)
342 return 0 # cd also returns 0
343
344 # Otherwise, set options.
345 ignore_shopt_not_impl = self.exec_opts.ignore_shopt_not_impl()
346 for opt_name in opt_names:
347 # We allow set -o options here
348 self.mutable_opts.SetAnyOption(opt_name, b, ignore_shopt_not_impl)
349
350 return 0
351
352
353class Hash(vm._Builtin):
354
355 def __init__(self, search_path):
356 # type: (SearchPath) -> None
357 self.search_path = search_path
358
359 def Run(self, cmd_val):
360 # type: (cmd_value.Argv) -> int
361 attrs, arg_r = flag_util.ParseCmdVal('hash', cmd_val)
362 arg = arg_types.hash(attrs.attrs)
363
364 rest = arg_r.Rest()
365 if arg.r:
366 if len(rest):
367 e_usage('got extra arguments after -r', loc.Missing)
368 self.search_path.ClearCache()
369 return 0
370
371 status = 0
372 if len(rest):
373 for cmd in rest: # enter in cache
374 full_path = self.search_path.CachedLookup(cmd)
375 if full_path is None:
376 print_stderr('hash: %r not found' % cmd)
377 status = 1
378 else: # print cache
379 commands = self.search_path.CachedCommands()
380 commands.sort()
381 for cmd in commands:
382 print(cmd)
383
384 return status
385
386
387def _ParseOptSpec(spec_str):
388 # type: (str) -> Dict[str, bool]
389 spec = {} # type: Dict[str, bool]
390 i = 0
391 n = len(spec_str)
392 while True:
393 if i >= n:
394 break
395 ch = spec_str[i]
396 spec[ch] = False
397 i += 1
398 if i >= n:
399 break
400 # If the next character is :, change the value to True.
401 if spec_str[i] == ':':
402 spec[ch] = True
403 i += 1
404 return spec
405
406
407class GetOptsState(object):
408 """State persisted across invocations.
409
410 This would be simpler in GetOpts.
411 """
412
413 def __init__(self, mem, errfmt):
414 # type: (Mem, ui.ErrorFormatter) -> None
415 self.mem = mem
416 self.errfmt = errfmt
417 self._optind = -1
418 self.flag_pos = 1 # position within the arg, public var
419
420 def _OptInd(self):
421 # type: () -> int
422 """Returns OPTIND that's >= 1, or -1 if it's invalid."""
423 # Note: OPTIND could be value.Int?
424 try:
425 result = state.GetInteger(self.mem, 'OPTIND')
426 except error.Runtime as e:
427 self.errfmt.Print_(e.UserErrorString())
428 result = -1
429 return result
430
431 def GetArg(self, argv):
432 # type: (List[str]) -> Optional[str]
433 """Get the value of argv at OPTIND.
434
435 Returns None if it's out of range.
436 """
437
438 #log('_optind %d flag_pos %d', self._optind, self.flag_pos)
439
440 optind = self._OptInd()
441 if optind == -1:
442 return None
443 self._optind = optind # save for later
444
445 i = optind - 1 # 1-based index
446 #log('argv %s i %d', argv, i)
447 if 0 <= i and i < len(argv):
448 return argv[i]
449 else:
450 return None
451
452 def IncIndex(self):
453 # type: () -> None
454 """Increment OPTIND."""
455 # Note: bash-completion uses a *local* OPTIND ! Not global.
456 assert self._optind != -1
457 state.BuiltinSetString(self.mem, 'OPTIND', str(self._optind + 1))
458 self.flag_pos = 1
459
460 def SetArg(self, optarg):
461 # type: (str) -> None
462 """Set OPTARG."""
463 state.BuiltinSetString(self.mem, 'OPTARG', optarg)
464
465 def Fail(self):
466 # type: () -> None
467 """On failure, reset OPTARG."""
468 state.BuiltinSetString(self.mem, 'OPTARG', '')
469
470
471def _GetOpts(
472 spec, # type: Dict[str, bool]
473 argv, # type: List[str]
474 my_state, # type: GetOptsState
475 errfmt, # type: ui.ErrorFormatter
476):
477 # type: (...) -> Tuple[int, str]
478 current = my_state.GetArg(argv)
479 #log('current %s', current)
480
481 if current is None: # out of range, etc.
482 my_state.Fail()
483 return 1, '?'
484
485 if not current.startswith('-') or current == '-':
486 my_state.Fail()
487 return 1, '?'
488
489 flag_char = current[my_state.flag_pos]
490
491 if my_state.flag_pos < len(current) - 1:
492 my_state.flag_pos += 1 # don't move past this arg yet
493 more_chars = True
494 else:
495 my_state.IncIndex()
496 my_state.flag_pos = 1
497 more_chars = False
498
499 if flag_char not in spec: # Invalid flag
500 return 0, '?'
501
502 if spec[flag_char]: # does it need an argument?
503 if more_chars:
504 optarg = current[my_state.flag_pos:]
505 else:
506 optarg = my_state.GetArg(argv)
507 if optarg is None:
508 my_state.Fail()
509 # TODO: Add location info
510 errfmt.Print_('getopts: option %r requires an argument.' %
511 current)
512 tmp = [j8_lite.MaybeShellEncode(a) for a in argv]
513 print_stderr('(getopts argv: %s)' % ' '.join(tmp))
514
515 # Hm doesn't cause status 1?
516 return 0, '?'
517 my_state.IncIndex()
518 my_state.SetArg(optarg)
519 else:
520 my_state.SetArg('')
521
522 return 0, flag_char
523
524
525class GetOpts(vm._Builtin):
526 """
527 Vars used:
528 OPTERR: disable printing of error messages
529 Vars set:
530 The variable named by the second arg
531 OPTIND - initialized to 1 at startup
532 OPTARG - argument
533 """
534
535 def __init__(self, mem, errfmt):
536 # type: (Mem, ui.ErrorFormatter) -> None
537 self.mem = mem
538 self.errfmt = errfmt
539
540 # TODO: state could just be in this object
541 self.my_state = GetOptsState(mem, errfmt)
542 self.spec_cache = {} # type: Dict[str, Dict[str, bool]]
543
544 def Run(self, cmd_val):
545 # type: (cmd_value.Argv) -> int
546 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
547 arg_r.Next()
548
549 # NOTE: If first char is a colon, error reporting is different. Alpine
550 # might not use that?
551 spec_str = arg_r.ReadRequired('requires an argspec')
552
553 var_name, var_loc = arg_r.ReadRequired2(
554 'requires the name of a variable to set')
555
556 spec = self.spec_cache.get(spec_str)
557 if spec is None:
558 spec = _ParseOptSpec(spec_str)
559 self.spec_cache[spec_str] = spec
560
561 user_argv = self.mem.GetArgv() if arg_r.AtEnd() else arg_r.Rest()
562 #log('user_argv %s', user_argv)
563 status, flag_char = _GetOpts(spec, user_argv, self.my_state,
564 self.errfmt)
565
566 if match.IsValidVarName(var_name):
567 state.BuiltinSetString(self.mem, var_name, flag_char)
568 else:
569 # NOTE: The builtin has PARTIALLY set state. This happens in all shells
570 # except mksh.
571 raise error.Usage('got invalid variable name %r' % var_name,
572 var_loc)
573 return status