OILS / builtin / pure_osh.py View on Github | oils.pub

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