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

558 lines, 379 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3
4from _devbuild.gen import arg_types
5from _devbuild.gen.option_asdl import builtin_i
6from _devbuild.gen.runtime_asdl import (
7 scope_e,
8 cmd_value,
9 AssignArg,
10)
11from _devbuild.gen.value_asdl import (value, value_e, value_t, LeftName)
12from _devbuild.gen.syntax_asdl import loc, loc_t, word_t
13
14from core import bash_impl
15from core import error
16from core.error import e_usage
17from core import state
18from core import vm
19from frontend import flag_util
20from frontend import args
21from mycpp.mylib import log, NewDict
22from osh import cmd_eval
23from osh import sh_expr_eval
24from data_lang import j8_lite
25
26from typing import cast, Dict, List, Optional, TYPE_CHECKING
27if TYPE_CHECKING:
28 from core.state import Mem
29 from core import optview
30 from display import ui
31 from frontend.args import _Attributes
32
33_ = log
34
35_OTHER = 0
36_READONLY = 1
37_EXPORT = 2
38
39
40def _PrintVariables(mem, cmd_val, attrs, print_flags, builtin=_OTHER):
41 # type: (Mem, cmd_value.Assign, _Attributes, bool, int) -> int
42 """
43 Args:
44 print_flags: whether to print flags
45 builtin: is it the readonly or export builtin?
46 """
47 flag = attrs.attrs
48
49 # Turn dynamic vars to static.
50 tmp_g = flag.get('g')
51 tmp_a = flag.get('a')
52 tmp_A = flag.get('A')
53
54 flag_g = (cast(value.Bool, tmp_g).b
55 if tmp_g and tmp_g.tag() == value_e.Bool else False)
56 flag_a = (cast(value.Bool, tmp_a).b
57 if tmp_a and tmp_a.tag() == value_e.Bool else False)
58 flag_A = (cast(value.Bool, tmp_A).b
59 if tmp_A and tmp_A.tag() == value_e.Bool else False)
60
61 tmp_n = flag.get('n')
62 tmp_r = flag.get('r')
63 tmp_x = flag.get('x')
64
65 #log('FLAG %r', flag)
66
67 # SUBTLE: export -n vs. declare -n. flag vs. OPTION.
68 # flags are value.Bool, while options are Undef or Str.
69 # '+', '-', or None
70 flag_n = (cast(value.Str, tmp_n).s if tmp_n and tmp_n.tag() == value_e.Str
71 else None) # type: Optional[str]
72 flag_r = (cast(value.Str, tmp_r).s if tmp_r and tmp_r.tag() == value_e.Str
73 else None) # type: Optional[str]
74 flag_x = (cast(value.Str, tmp_x).s if tmp_x and tmp_x.tag() == value_e.Str
75 else None) # type: Optional[str]
76
77 if cmd_val.builtin_id == builtin_i.local:
78 if flag_g and not mem.IsGlobalScope():
79 return 1
80 which_scopes = scope_e.LocalOnly
81 elif flag_g:
82 which_scopes = scope_e.GlobalOnly
83 else:
84 which_scopes = mem.ScopesForReading() # reading
85
86 if len(cmd_val.pairs) == 0:
87 print_all = True
88 cells = mem.GetAllCells(which_scopes)
89 names = sorted(cells) # type: List[str]
90 else:
91 print_all = False
92 names = []
93 cells = {}
94 for pair in cmd_val.pairs:
95 name = pair.var_name
96 if pair.rval and pair.rval.tag() == value_e.Str:
97 # Invalid: declare -p foo=bar
98 # Add a sentinel so we skip it, but know to exit with status 1.
99 s = cast(value.Str, pair.rval).s
100 invalid = "%s=%s" % (name, s)
101 names.append(invalid)
102 cells[invalid] = None
103 else:
104 names.append(name)
105 cells[name] = mem.GetCell(name, which_scopes)
106
107 count = 0
108 for name in names:
109 cell = cells[name]
110 if cell is None:
111 continue # Invalid
112 val = cell.val
113 #log('name %r %s', name, val)
114
115 if val.tag() == value_e.Undef:
116 continue
117 if builtin == _READONLY and not cell.readonly:
118 continue
119 if builtin == _EXPORT and not cell.exported:
120 continue
121
122 if flag_n == '-' and not cell.nameref:
123 continue
124 if flag_n == '+' and cell.nameref:
125 continue
126 if flag_r == '-' and not cell.readonly:
127 continue
128 if flag_r == '+' and cell.readonly:
129 continue
130 if flag_x == '-' and not cell.exported:
131 continue
132 if flag_x == '+' and cell.exported:
133 continue
134
135 if flag_a and val.tag() not in (value_e.BashArray,
136 value_e.SparseArray):
137 continue
138 if flag_A and val.tag() != value_e.BashAssoc:
139 continue
140
141 decl = [] # type: List[str]
142 if print_flags:
143 flags = [] # type: List[str]
144 if cell.nameref:
145 flags.append('n')
146 if cell.readonly:
147 flags.append('r')
148 if cell.exported:
149 flags.append('x')
150 if val.tag() in (value_e.BashArray, value_e.SparseArray):
151 flags.append('a')
152 elif val.tag() == value_e.BashAssoc:
153 flags.append('A')
154 if len(flags) == 0:
155 flags.append('-')
156
157 decl.extend(["declare -", ''.join(flags), " ", name])
158 else:
159 decl.append(name)
160
161 if val.tag() == value_e.Str:
162 str_val = cast(value.Str, val)
163 decl.extend(["=", j8_lite.MaybeShellEncode(str_val.s)])
164
165 elif val.tag() == value_e.BashArray:
166 array_val = cast(value.BashArray, val)
167 decl.extend(
168 ["=",
169 bash_impl.BashArray_ToStrForShellPrint(array_val, name)])
170
171 elif val.tag() == value_e.BashAssoc:
172 assoc_val = cast(value.BashAssoc, val)
173 decl.extend(
174 ["=", bash_impl.BashAssoc_ToStrForShellPrint(assoc_val)])
175
176 elif val.tag() == value_e.SparseArray:
177 sparse_val = cast(value.SparseArray, val)
178 decl.extend(
179 ["=",
180 bash_impl.SparseArray_ToStrForShellPrint(sparse_val)])
181
182 else:
183 pass # note: other types silently ignored
184
185 print(''.join(decl))
186 count += 1
187
188 if print_all or count == len(names):
189 return 0
190 else:
191 return 1
192
193
194def _ExportReadonly(mem, pair, flags):
195 # type: (Mem, AssignArg, int) -> None
196 """For 'export' and 'readonly' to respect += and flags.
197
198 Like 'setvar' (scope_e.LocalOnly), unless dynamic scope is on. That is, it
199 respects shopt --unset dynamic_scope.
200
201 Used for assignment builtins, (( a = b )), {fd}>out, ${x=}, etc.
202 """
203 which_scopes = mem.ScopesForWriting()
204
205 lval = LeftName(pair.var_name, pair.blame_word)
206 if pair.plus_eq:
207 old_val = sh_expr_eval.OldValue(lval, mem, None) # ignore set -u
208 # When 'export e+=', then rval is value.Str('')
209 # When 'export foo', the pair.plus_eq flag is false.
210 assert pair.rval is not None
211 val = cmd_eval.PlusEquals(old_val, pair.rval)
212 else:
213 # NOTE: when rval is None, only flags are changed
214 val = pair.rval
215
216 mem.SetNamed(lval, val, which_scopes, flags=flags)
217
218
219class Export(vm._AssignBuiltin):
220
221 def __init__(self, mem, errfmt):
222 # type: (Mem, ui.ErrorFormatter) -> None
223 self.mem = mem
224 self.errfmt = errfmt
225
226 def Run(self, cmd_val):
227 # type: (cmd_value.Assign) -> int
228 if self.mem.exec_opts.no_exported():
229 self.errfmt.Print_(
230 'export builtin is disabled in YSH (shopt --set no_exported)',
231 cmd_val.arg_locs[0])
232 return 1
233
234 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
235 arg_r.Next()
236 attrs = flag_util.Parse('export_', arg_r)
237 arg = arg_types.export_(attrs.attrs)
238 #arg = attrs
239
240 if arg.f:
241 e_usage(
242 "doesn't accept -f because it's dangerous. "
243 "(The code can usually be restructured with 'source')",
244 loc.Missing)
245
246 if arg.p or len(cmd_val.pairs) == 0:
247 return _PrintVariables(self.mem,
248 cmd_val,
249 attrs,
250 True,
251 builtin=_EXPORT)
252
253 if arg.n:
254 for pair in cmd_val.pairs:
255 if pair.rval is not None:
256 e_usage("doesn't accept RHS with -n",
257 loc.Word(pair.blame_word))
258
259 # NOTE: we don't care if it wasn't found, like bash.
260 self.mem.ClearFlag(pair.var_name, state.ClearExport)
261 else:
262 for pair in cmd_val.pairs:
263 _ExportReadonly(self.mem, pair, state.SetExport)
264
265 return 0
266
267
268def _ReconcileTypes(rval, flag_a, flag_A, blame_word):
269 # type: (Optional[value_t], bool, bool, word_t) -> value_t
270 """Check that -a and -A flags are consistent with RHS.
271
272 Special case: () is allowed to mean empty indexed array or empty assoc array
273 if the context is clear.
274
275 Shared between NewVar and Readonly.
276 """
277 if flag_a and rval is not None and rval.tag() != value_e.BashArray:
278 e_usage("Got -a but RHS isn't an array", loc.Word(blame_word))
279
280 if flag_A and rval:
281 # Special case: declare -A A=() is OK. The () is changed to mean an empty
282 # associative array.
283 if rval.tag() == value_e.BashArray:
284 array_val = cast(value.BashArray, rval)
285 if len(array_val.strs) == 0:
286 # mycpp limitation: NewDict() needs to be typed
287 tmp = NewDict() # type: Dict[str, str]
288 return value.BashAssoc(tmp)
289 #return value.BashArray([])
290
291 if rval.tag() != value_e.BashAssoc:
292 e_usage("Got -A but RHS isn't an associative array",
293 loc.Word(blame_word))
294
295 return rval
296
297
298class Readonly(vm._AssignBuiltin):
299
300 def __init__(self, mem, errfmt):
301 # type: (Mem, ui.ErrorFormatter) -> None
302 self.mem = mem
303 self.errfmt = errfmt
304
305 def Run(self, cmd_val):
306 # type: (cmd_value.Assign) -> int
307 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
308 arg_r.Next()
309 attrs = flag_util.Parse('readonly', arg_r)
310 arg = arg_types.readonly(attrs.attrs)
311
312 if arg.p or len(cmd_val.pairs) == 0:
313 return _PrintVariables(self.mem,
314 cmd_val,
315 attrs,
316 True,
317 builtin=_READONLY)
318
319 for pair in cmd_val.pairs:
320 if pair.rval is None:
321 if arg.a:
322 rval = value.BashArray([]) # type: value_t
323 elif arg.A:
324 rval = value.BashAssoc({})
325 else:
326 rval = None
327 else:
328 rval = pair.rval
329
330 rval = _ReconcileTypes(rval, arg.a, arg.A, pair.blame_word)
331
332 # NOTE:
333 # - when rval is None, only flags are changed
334 # - dynamic scope because flags on locals can be changed, etc.
335 _ExportReadonly(self.mem, pair, state.SetReadOnly)
336
337 return 0
338
339
340class NewVar(vm._AssignBuiltin):
341 """declare/typeset/local."""
342
343 def __init__(self, mem, procs, exec_opts, errfmt):
344 # type: (Mem, state.Procs, optview.Exec, ui.ErrorFormatter) -> None
345 self.mem = mem
346 self.procs = procs
347 self.exec_opts = exec_opts
348 self.errfmt = errfmt
349
350 def _PrintFuncs(self, names):
351 # type: (List[str]) -> int
352 status = 0
353 for name in names:
354 if self.procs.GetShellFunc(name):
355 print(name)
356 # TODO: Could print LST for -f, or render LST. Bash does this. 'trap'
357 # could use that too.
358 else:
359 status = 1
360 return status
361
362 def Run(self, cmd_val):
363 # type: (cmd_value.Assign) -> int
364 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
365 arg_r.Next()
366 attrs = flag_util.Parse('new_var', arg_r)
367 arg = arg_types.new_var(attrs.attrs)
368
369 status = 0
370
371 if arg.f:
372 names = arg_r.Rest()
373 if len(names):
374 # This is only used for a STATUS QUERY now. We only show the name,
375 # not the body.
376 status = self._PrintFuncs(names)
377 else:
378 # Disallow this since it would be incompatible.
379 e_usage('with -f expects function names', loc.Missing)
380 return status
381
382 if arg.F:
383 names = arg_r.Rest()
384 if len(names):
385 status = self._PrintFuncs(names)
386 else:
387 # bash quirk: with no names, they're printed in a different format!
388 for func_name in self.procs.ShellFuncNames():
389 print('declare -f %s' % (func_name))
390 return status
391
392 if arg.p: # Lookup and print variables.
393 return _PrintVariables(self.mem, cmd_val, attrs, True)
394 elif len(cmd_val.pairs) == 0:
395 return _PrintVariables(self.mem, cmd_val, attrs, False)
396
397 if not self.exec_opts.ignore_flags_not_impl():
398 if arg.i:
399 e_usage(
400 "doesn't implement flag -i (shopt --set ignore_flags_not_impl)",
401 loc.Missing)
402
403 if arg.l or arg.u:
404 # Just print a warning! The program may still run.
405 self.errfmt.Print_(
406 "Warning: OSH doesn't implement flags -l or -u (shopt --set ignore_flags_not_impl)",
407 loc.Missing)
408
409 #
410 # Set variables
411 #
412
413 if cmd_val.builtin_id == builtin_i.local:
414 which_scopes = scope_e.LocalOnly
415 else: # declare/typeset
416 if arg.g:
417 which_scopes = scope_e.GlobalOnly
418 else:
419 which_scopes = scope_e.LocalOnly
420
421 flags = 0
422 if arg.x == '-':
423 flags |= state.SetExport
424 if arg.r == '-':
425 flags |= state.SetReadOnly
426 if arg.n == '-':
427 flags |= state.SetNameref
428
429 if arg.x == '+':
430 flags |= state.ClearExport
431 if arg.r == '+':
432 flags |= state.ClearReadOnly
433 if arg.n == '+':
434 flags |= state.ClearNameref
435
436 for pair in cmd_val.pairs:
437 rval = pair.rval
438 # declare -a foo=(a b); declare -a foo; should not reset to empty array
439 if rval is None and (arg.a or arg.A):
440 old_val = self.mem.GetValue(pair.var_name)
441 if arg.a:
442 if old_val.tag() not in (value_e.BashArray,
443 value_e.SparseArray):
444 rval = value.BashArray([])
445 elif arg.A:
446 if old_val.tag() != value_e.BashAssoc:
447 # mycpp limitation: NewDict() needs to be typed
448 tmp = NewDict() # type: Dict[str, str]
449 rval = value.BashAssoc(tmp)
450
451 lval = LeftName(pair.var_name, pair.blame_word)
452
453 if pair.plus_eq:
454 old_val = sh_expr_eval.OldValue(lval, self.mem,
455 None) # ignore set -u
456 # When 'typeset e+=', then rval is value.Str('')
457 # When 'typeset foo', the pair.plus_eq flag is false.
458 assert pair.rval is not None
459 rval = cmd_eval.PlusEquals(old_val, pair.rval)
460 else:
461 rval = _ReconcileTypes(rval, arg.a, arg.A, pair.blame_word)
462
463 self.mem.SetNamed(lval, rval, which_scopes, flags=flags)
464
465 return status
466
467
468# TODO:
469# - It would make more sense to treat no args as an error (bash doesn't.)
470# - Should we have strict builtins? Or just make it stricter?
471# - Typed args: unset (mylist[0]) is like Python's del
472# - It has the same word as 'setvar', which makes sense
473
474
475class Unset(vm._Builtin):
476
477 def __init__(
478 self,
479 mem, # type: state.Mem
480 procs, # type: state.Procs
481 unsafe_arith, # type: sh_expr_eval.UnsafeArith
482 errfmt, # type: ui.ErrorFormatter
483 ):
484 # type: (...) -> None
485 self.mem = mem
486 self.procs = procs
487 self.unsafe_arith = unsafe_arith
488 self.errfmt = errfmt
489
490 def _UnsetVar(self, arg, location, proc_fallback):
491 # type: (str, loc_t, bool) -> bool
492 """
493 Returns:
494 bool: whether the 'unset' builtin should succeed with code 0.
495 """
496 lval = self.unsafe_arith.ParseLValue(arg, location)
497
498 #log('unsafe lval %s', lval)
499 found = False
500 try:
501 found = self.mem.Unset(lval, scope_e.Shopt)
502 except error.Runtime as e:
503 # note: in bash, myreadonly=X fails, but declare myreadonly=X doesn't
504 # fail because it's a builtin. So I guess the same is true of 'unset'.
505 msg = e.UserErrorString()
506 self.errfmt.Print_(msg, blame_loc=location)
507 return False
508
509 if proc_fallback and not found:
510 self.procs.EraseShellFunc(arg)
511
512 return True
513
514 def Run(self, cmd_val):
515 # type: (cmd_value.Argv) -> int
516 attrs, arg_r = flag_util.ParseCmdVal('unset', cmd_val)
517 arg = arg_types.unset(attrs.attrs)
518
519 argv, arg_locs = arg_r.Rest2()
520 for i, name in enumerate(argv):
521 location = arg_locs[i]
522
523 if arg.f:
524 self.procs.EraseShellFunc(name)
525
526 elif arg.v:
527 if not self._UnsetVar(name, location, False):
528 return 1
529
530 else:
531 # proc_fallback: Try to delete var first, then func.
532 if not self._UnsetVar(name, location, True):
533 return 1
534
535 return 0
536
537
538class Shift(vm._Builtin):
539
540 def __init__(self, mem):
541 # type: (Mem) -> None
542 self.mem = mem
543
544 def Run(self, cmd_val):
545 # type: (cmd_value.Argv) -> int
546 num_args = len(cmd_val.argv) - 1
547 if num_args == 0:
548 n = 1
549 elif num_args == 1:
550 arg = cmd_val.argv[1]
551 try:
552 n = int(arg)
553 except ValueError:
554 e_usage("Invalid shift argument %r" % arg, loc.Missing)
555 else:
556 e_usage('got too many arguments', loc.Missing)
557
558 return self.mem.Shift(n)