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

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