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

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