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

576 lines, 393 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.mylib import log
21from osh import cmd_eval
22from osh import sh_expr_eval
23from data_lang import j8_lite
24
25from typing import cast, Optional, List, TYPE_CHECKING
26if TYPE_CHECKING:
27 from core.state import Mem
28 from core import optview
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, exec_opts, errfmt):
365 # type: (Mem, state.Procs, optview.Exec, ui.ErrorFormatter) -> None
366 self.mem = mem
367 self.procs = procs
368 self.exec_opts = exec_opts
369 self.errfmt = errfmt
370
371 def _PrintFuncs(self, names):
372 # type: (List[str]) -> int
373 status = 0
374 for name in names:
375 if self.procs.Get(name):
376 print(name)
377 # TODO: Could print LST for -f, or render LST. Bash does this. 'trap'
378 # could use that too.
379 else:
380 status = 1
381 return status
382
383 def Run(self, cmd_val):
384 # type: (cmd_value.Assign) -> int
385 arg_r = args.Reader(cmd_val.argv, locs=cmd_val.arg_locs)
386 arg_r.Next()
387 attrs = flag_util.Parse('new_var', arg_r)
388 arg = arg_types.new_var(attrs.attrs)
389
390 status = 0
391
392 if arg.f:
393 names = arg_r.Rest()
394 if len(names):
395 # This is only used for a STATUS QUERY now. We only show the name,
396 # not the body.
397 status = self._PrintFuncs(names)
398 else:
399 # Disallow this since it would be incompatible.
400 e_usage('with -f expects function names', loc.Missing)
401 return status
402
403 if arg.F:
404 names = arg_r.Rest()
405 if len(names):
406 status = self._PrintFuncs(names)
407 else:
408 # bash quirk: with no names, they're printed in a different format!
409 for func_name in self.procs.GetNames():
410 print('declare -f %s' % (func_name))
411 return status
412
413 if arg.p: # Lookup and print variables.
414 return _PrintVariables(self.mem, cmd_val, attrs, True)
415 elif len(cmd_val.pairs) == 0:
416 return _PrintVariables(self.mem, cmd_val, attrs, False)
417
418 if not self.exec_opts.ignore_flags_not_impl():
419 if arg.i:
420 e_usage(
421 "doesn't implement flag -i (shopt --set ignore_flags_not_impl)",
422 loc.Missing)
423
424 if arg.l or arg.u:
425 # Just print a warning! The program may still run.
426 self.errfmt.Print_(
427 "Warning: OSH doesn't implement flags -l or -u (shopt --set ignore_flags_not_impl)",
428 loc.Missing)
429
430 #
431 # Set variables
432 #
433
434 if cmd_val.builtin_id == builtin_i.local:
435 which_scopes = scope_e.LocalOnly
436 else: # declare/typeset
437 if arg.g:
438 which_scopes = scope_e.GlobalOnly
439 else:
440 which_scopes = scope_e.LocalOnly
441
442 flags = 0
443 if arg.x == '-':
444 flags |= state.SetExport
445 if arg.r == '-':
446 flags |= state.SetReadOnly
447 if arg.n == '-':
448 flags |= state.SetNameref
449
450 if arg.x == '+':
451 flags |= state.ClearExport
452 if arg.r == '+':
453 flags |= state.ClearReadOnly
454 if arg.n == '+':
455 flags |= state.ClearNameref
456
457 for pair in cmd_val.pairs:
458 rval = pair.rval
459 # declare -a foo=(a b); declare -a foo; should not reset to empty array
460 if rval is None and (arg.a or arg.A):
461 old_val = self.mem.GetValue(pair.var_name)
462 if arg.a:
463 if old_val.tag() != value_e.BashArray:
464 rval = value.BashArray([])
465 elif arg.A:
466 if old_val.tag() != value_e.BashAssoc:
467 rval = value.BashAssoc({})
468
469 lval = LeftName(pair.var_name, pair.blame_word)
470
471 if pair.plus_eq:
472 old_val = sh_expr_eval.OldValue(lval, self.mem,
473 None) # ignore set -u
474 # When 'typeset e+=', then rval is value.Str('')
475 # When 'typeset foo', the pair.plus_eq flag is false.
476 assert pair.rval is not None
477 rval = cmd_eval.PlusEquals(old_val, pair.rval)
478 else:
479 rval = _ReconcileTypes(rval, arg.a, arg.A, pair.blame_word)
480
481 self.mem.SetNamed(lval, rval, which_scopes, flags=flags)
482
483 return status
484
485
486# TODO:
487# - It would make more sense to treat no args as an error (bash doesn't.)
488# - Should we have strict builtins? Or just make it stricter?
489# - Typed args: unset (mylist[0]) is like Python's del
490# - It has the same word as 'setvar', which makes sense
491
492
493class Unset(vm._Builtin):
494
495 def __init__(
496 self,
497 mem, # type: state.Mem
498 procs, # type: state.Procs
499 unsafe_arith, # type: sh_expr_eval.UnsafeArith
500 errfmt, # type: ui.ErrorFormatter
501 ):
502 # type: (...) -> None
503 self.mem = mem
504 self.procs = procs
505 self.unsafe_arith = unsafe_arith
506 self.errfmt = errfmt
507
508 def _UnsetVar(self, arg, location, proc_fallback):
509 # type: (str, loc_t, bool) -> bool
510 """
511 Returns:
512 bool: whether the 'unset' builtin should succeed with code 0.
513 """
514 lval = self.unsafe_arith.ParseLValue(arg, location)
515
516 #log('unsafe lval %s', lval)
517 found = False
518 try:
519 found = self.mem.Unset(lval, scope_e.Shopt)
520 except error.Runtime as e:
521 # note: in bash, myreadonly=X fails, but declare myreadonly=X doesn't
522 # fail because it's a builtin. So I guess the same is true of 'unset'.
523 msg = e.UserErrorString()
524 self.errfmt.Print_(msg, blame_loc=location)
525 return False
526
527 if proc_fallback and not found:
528 self.procs.Del(arg)
529
530 return True
531
532 def Run(self, cmd_val):
533 # type: (cmd_value.Argv) -> int
534 attrs, arg_r = flag_util.ParseCmdVal('unset', cmd_val)
535 arg = arg_types.unset(attrs.attrs)
536
537 argv, arg_locs = arg_r.Rest2()
538 for i, name in enumerate(argv):
539 location = arg_locs[i]
540
541 if arg.f:
542 self.procs.Del(name)
543
544 elif arg.v:
545 if not self._UnsetVar(name, location, False):
546 return 1
547
548 else:
549 # proc_fallback: Try to delete var first, then func.
550 if not self._UnsetVar(name, location, True):
551 return 1
552
553 return 0
554
555
556class Shift(vm._Builtin):
557
558 def __init__(self, mem):
559 # type: (Mem) -> None
560 self.mem = mem
561
562 def Run(self, cmd_val):
563 # type: (cmd_value.Argv) -> int
564 num_args = len(cmd_val.argv) - 1
565 if num_args == 0:
566 n = 1
567 elif num_args == 1:
568 arg = cmd_val.argv[1]
569 try:
570 n = int(arg)
571 except ValueError:
572 e_usage("Invalid shift argument %r" % arg, loc.Missing)
573 else:
574 e_usage('got too many arguments', loc.Missing)
575
576 return self.mem.Shift(n)