OILS / frontend / option_def.py View on Github | oils.pub

422 lines, 214 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3
4from typing import List, Dict, Optional, Tuple, Any
5
6
7class Option(object):
8
9 def __init__(self,
10 index,
11 name,
12 short_flag=None,
13 builtin='shopt',
14 default=False,
15 implemented=True,
16 groups=None):
17 # type: (int, str, str, Optional[str], bool, bool, List[str]) -> None
18 self.index = index
19 self.name = name # e.g. 'errexit'
20 self.short_flag = short_flag # 'e' for -e
21
22 if short_flag:
23 self.builtin = 'set'
24 else:
25 # The 'interactive' option is the only one where builtin is None. It has
26 # a cell but you can't change it. Only the shell can.
27 self.builtin = builtin
28
29 self.default = default # default value is True in some cases
30 self.implemented = implemented
31 self.groups = groups or [] # list of groups
32
33 # for optview
34 self.is_parse = (name.startswith('parse_') or
35 name.startswith('strict_parse_') or
36 name == 'expand_aliases')
37 # interactive() is an accessor
38 self.is_exec = implemented and not self.is_parse
39
40
41class _OptionDef(object):
42 """Description of all shell options.
43
44 Similar to id_kind_def.IdSpec
45 """
46
47 def __init__(self):
48 # type: () -> None
49 self.opts = [] # type: List[Option]
50 self.index = 1 # start with 1
51 self.array_size = -1
52
53 def Add(self, *args, **kwargs):
54 # type: (Any, Any) -> None
55 self.opts.append(Option(self.index, *args, **kwargs))
56 self.index += 1
57
58 def DoneWithImplementedOptions(self):
59 # type: () -> None
60 self.array_size = self.index
61
62
63# Used by builtin
64_OTHER_SET_OPTIONS = [
65 # NOTE: set -i and +i is explicitly disallowed. Only osh -i or +i is valid
66 # https://unix.stackexchange.com/questions/339506/can-an-interactive-shell-become-non-interactive-or-vice-versa
67 ('n', 'noexec'),
68 ('x', 'xtrace'),
69 ('v', 'verbose'), # like xtrace, but prints unevaluated commands
70 ('f', 'noglob'),
71 ('C', 'noclobber'),
72 ('E', 'errtrace'),
73 (None, 'posix'), # controls special builtins
74 (None, 'vi'),
75 (None, 'emacs'),
76]
77
78_STRICT_OPTS = [
79 # $a{[@]::} is not allowed, you need ${a[@]::0} or ${a[@]::n}
80 'strict_parse_slice',
81
82 # These are RUNTIME strict options.
83 'strict_argv', # empty argv not allowed
84 'strict_arith', # string to integer conversions, e.g. x=foo; echo $(( x ))
85
86 # No implicit conversions between string and array.
87 # - foo="$@" not allowed because it decays. Should be foo=( "$@" ).
88 # - ${a} not ${a[0]} (not implemented)
89 # sane-array? compare arrays like [[ "$@" == "${a[@]}" ]], which is
90 # incompatible because bash coerces
91 # default: do not allow
92 'strict_array',
93 'strict_control_flow', # break/continue at top level is fatal
94 # 'return $empty' and return "" are NOT accepted
95 'strict_errexit', # errexit can't be disabled in compound commands
96 'strict_nameref', # trap invalid variable names
97 'strict_word_eval', # negative slices, unicode
98 'strict_tilde', # ~nonexistent is an error (like zsh)
99
100 # Not implemented
101 'strict_glob', # glob_.py GlobParser has warnings
102]
103
104# These will break some programs, but the fix should be simple.
105
106# command_sub_errexit makes 'local foo=$(false)' and echo $(false) fail.
107# By default, we have mimic bash's undesirable behavior of ignoring
108# these failures, since ash copied it, and Alpine's abuild relies on it.
109#
110# Note that inherit_errexit is a strict option.
111
112_UPGRADE_RUNTIME_OPTS = [
113 ('simple_word_eval', False), # No splitting; arity isn't data-dependent
114 # Don't reparse program data as globs
115 ('dashglob', True), # do globs return files starting with - ?
116
117 # TODO: Should these be in strict mode?
118 # The logic was that strict_errexit improves your bash programs, but these
119 # would lead you to remove error handling. But the same could be said for
120 # strict_array?
121 ('command_sub_errexit', False), # check after command sub
122 ('process_sub_fail', False), # like pipefail, but for <(sort foo.txt)
123 ('xtrace_rich', False), # Hierarchical trace with PIDs
124 ('xtrace_details', True), # Legacy set -x stuff
125
126 # Whether status 141 in pipelines is turned into 0
127 ('sigpipe_status_ok', False),
128
129 # create ENV at startup; read from it when starting processes
130 ('env_obj', False),
131 ('init_ysh_globals', False), # initialize ARGV
132
133 # Can create closures from loop variables, like JS / C# / Go
134 ('for_loop_frames', False),
135]
136
137# TODO: Add strict_arg_parse? For example, 'trap 1 2 3' shouldn't be
138# valid, because it has an extra argument. Builtins are inconsistent about
139# checking this.
140
141_YSH_RUNTIME_OPTS = [
142 ('no_exported', False), # don't initialize or use exported variables
143 ('no_init_globals', False), # don't initialize PWD, COMP_WORDBREAKS, etc.
144 ('simple_echo', False), # echo takes 0 or 1 arguments
145 ('simple_eval_builtin', False), # eval takes exactly 1 argument
146
147 # only file tests (no strings), remove [, status 2
148 ('simple_test_builtin', False),
149
150 # TODO: simple_trap
151
152 # Turn aliases off so we can statically parse. bash has it off
153 # non-interactively, sothis shouldn't break much.
154 ('expand_aliases', True),
155]
156
157# Stuff that doesn't break too many programs.
158_UPGRADE_PARSE_OPTS = [
159 'parse_at', # @array, @[expr]
160 'parse_proc', # proc p { ... }
161 'parse_func', # func f(x) { ... }
162 'parse_brace', # cd /bin { ... }
163 'parse_bracket', # assert [42 === x]
164
165 # bare assignment 'x = 42' is allowed in Hay { } blocks, but disallowed
166 # everywhere else. It's not a command 'x' with arg '='.
167 'parse_equals',
168 'parse_paren', # if (x > 0) ...
169 'parse_ysh_string', # r'' u'' b'' and multi-line versions
170 'parse_triple_quote', # for ''' and """
171]
172
173# Extra stuff that breaks too many programs.
174_YSH_PARSE_OPTS = [
175 ('parse_at_all', False), # @ starting any word, e.g. @[] @{} @@ @_ @-
176
177 # Legacy syntax that is removed. These options are distinct from strict_*
178 # because they don't help you avoid bugs in bash programs. They just makes
179 # the language more consistent.
180 ('parse_backslash', True),
181 ('parse_backticks', True),
182 ('parse_dollar', True),
183 ('parse_ignored', True),
184 ('parse_sh_arith', True), # disallow all shell arithmetic, $(( )) etc.
185 ('parse_dparen', True), # disallow bash's ((
186 ('parse_dbracket', True), # disallow bash's [[
187 ('parse_bare_word', True), # 'case bare' and 'for x in bare'
188]
189
190# No-ops for bash compatibility
191_NO_OPS = [
192 # Handled one by one
193 'progcomp',
194 'histappend', # stubbed out for issue #218
195 'hostcomplete', # complete words with '@' ?
196 'cmdhist', # multi-line commands in history
197
198 # Copied from https://www.gnu.org/software/bash/manual/bash.txt
199 # except 'compat*' because they were deemed too ugly
200 'assoc_expand_once',
201 'autocd',
202 'cdable_vars',
203 'cdspell',
204 'checkhash',
205 'checkjobs',
206 'checkwinsize',
207 'complete_fullquote', # Set by default
208 # If set, Bash quotes all shell metacharacters in filenames and
209 # directory names when performing completion. If not set, Bash
210 # removes metacharacters such as the dollar sign from the set of
211 # characters that will be quoted in completed filenames when
212 # these metacharacters appear in shell variable references in
213 # words to be completed. This means that dollar signs in
214 # variable names that expand to directories will not be quoted;
215 # however, any dollar signs appearing in filenames will not be
216 # quoted, either. This is active only when bash is using
217 # backslashes to quote completed filenames. This variable is
218 # set by default, which is the default Bash behavior in versions
219 # through 4.2.
220 'direxpand',
221 'dirspell',
222 'execfail',
223 'extquote',
224 'force_fignore',
225 'globasciiranges',
226 'globstar', # TODO: implement **
227 'gnu_errfmt',
228 'histreedit',
229 'histverify',
230 'huponexit',
231 'interactive_comments',
232 'lithist',
233 'localvar_inherit',
234 'localvar_unset',
235 'login_shell',
236 'mailwarn',
237 'no_empty_cmd_completion',
238 'nocaseglob',
239 'progcomp_alias',
240 'promptvars',
241 'restricted_shell',
242 'shift_verbose',
243 'sourcepath',
244 'xpg_echo',
245]
246
247
248def _Init(opt_def):
249 # type: (_OptionDef) -> None
250
251 opt_def.Add('errexit',
252 short_flag='e',
253 builtin='set',
254 groups=['ysh:upgrade', 'ysh:all'])
255 opt_def.Add('nounset',
256 short_flag='u',
257 builtin='set',
258 groups=['ysh:upgrade', 'ysh:all'])
259 opt_def.Add('pipefail', builtin='set', groups=['ysh:upgrade', 'ysh:all'])
260
261 opt_def.Add('inherit_errexit', groups=['ysh:upgrade', 'ysh:all'])
262 # Hm is this subsumed by simple_word_eval?
263 opt_def.Add('nullglob', groups=['ysh:upgrade', 'ysh:all'])
264 opt_def.Add('verbose_errexit', groups=['ysh:upgrade', 'ysh:all'])
265
266 # set -o noclobber, etc.
267 for short_flag, name in _OTHER_SET_OPTIONS:
268 opt_def.Add(name, short_flag=short_flag, builtin='set')
269
270 # The only one where builtin=None. Only the shell can change it.
271 opt_def.Add('interactive', builtin=None)
272
273 # bash --norc -c 'set -o' shows this is on by default
274 opt_def.Add('hashall', short_flag='h', builtin='set', default=True)
275
276 # This option is always on
277 opt_def.Add('lastpipe', default=True)
278
279 #
280 # shopt
281 # (bash uses $BASHOPTS rather than $SHELLOPTS)
282 #
283
284 # shopt options that aren't in any groups.
285 opt_def.Add('failglob')
286 opt_def.Add('extglob')
287 opt_def.Add('nocasematch')
288 opt_def.Add('dotglob')
289
290 opt_def.Add('extdebug') # for task files
291
292 # recursive parsing and evaluation - for compatibility, ble.sh, etc.
293 opt_def.Add('eval_unsafe_arith')
294
295 opt_def.Add('ignore_flags_not_impl')
296 opt_def.Add('ignore_shopt_not_impl')
297
298 # For implementing strict_errexit
299 # TODO: could be _no_command_sub / _no_process_sub, if we had to discourage
300 # "default True" options
301 opt_def.Add('_allow_command_sub', default=True)
302 opt_def.Add('_allow_process_sub', default=True)
303
304 # For implementing 'proc'
305 opt_def.Add('dynamic_scope', default=True)
306
307 # On in interactive shell
308 opt_def.Add('redefine_const', default=False)
309 opt_def.Add('redefine_source', default=False)
310
311 # For disabling strict_errexit while running traps. Because we run in the
312 # main loop, the value can be "off". Prefix with _ because it's undocumented
313 # and users shouldn't fiddle with it. We need a stack so this is a
314 # convenient place.
315 opt_def.Add('_running_trap')
316 opt_def.Add('_running_hay')
317
318 # For fixing lastpipe / job control / DEBUG trap interaction
319 opt_def.Add('_no_debug_trap')
320 # To implement ERR trap semantics - it's only run for the WHOLE pipeline,
321 # not each part (even the last part)
322 opt_def.Add('_no_err_trap')
323
324 # shopt -s strict_arith, etc.
325 for name in _STRICT_OPTS:
326 opt_def.Add(name, groups=['strict:all', 'ysh:all'])
327
328 #
329 # Options that enable YSH features
330 #
331
332 #opt_def.Add('no_exported') # TODO: move this
333 for name in _UPGRADE_PARSE_OPTS:
334 opt_def.Add(name, groups=['ysh:upgrade', 'ysh:all'])
335 # shopt -s simple_word_eval, etc.
336 for name, default in _UPGRADE_RUNTIME_OPTS:
337 opt_def.Add(name, default=default, groups=['ysh:upgrade', 'ysh:all'])
338
339 for name, default in _YSH_PARSE_OPTS:
340 opt_def.Add(name, default=default, groups=['ysh:all'])
341 for name, default in _YSH_RUNTIME_OPTS:
342 opt_def.Add(name, default=default, groups=['ysh:all'])
343
344 opt_def.DoneWithImplementedOptions()
345
346 # NO_OPS
347
348 # Stubs for shopt -s xpg_echo, etc.
349 for name in _NO_OPS:
350 opt_def.Add(name, implemented=False)
351
352
353def All():
354 # type: () -> List[Option]
355 """Return a list of options with metadata.
356
357 - Used by osh/builtin_pure.py to construct the arg spec.
358 - Used by frontend/lexer_gen.py to construct the lexer/matcher
359 """
360 return _OPTION_DEF.opts
361
362
363def ArraySize():
364 # type: () -> int
365 """Unused now, since we use opt_num::ARRAY_SIZE.
366
367 We could get rid of unimplemented options and shrink the array.
368 """
369 return _OPTION_DEF.array_size
370
371
372def OptionDict():
373 # type: () -> Dict[str, Tuple[int, bool]]
374 """Implemented options.
375
376 For the slow path in frontend/consts.py
377 """
378 d = {}
379 for opt in _OPTION_DEF.opts:
380 d[opt.name] = (opt.index, opt.implemented)
381 return d
382
383
384def ParseOptNames():
385 # type: () -> List[str]
386 """Used by core/optview*.py."""
387 return [opt.name for opt in _OPTION_DEF.opts if opt.is_parse]
388
389
390def ExecOptNames():
391 # type: () -> List[str]
392 """Used by core/optview*.py."""
393 return [opt.name for opt in _OPTION_DEF.opts if opt.is_exec]
394
395
396_OPTION_DEF = _OptionDef()
397
398_Init(_OPTION_DEF)
399
400# Sort by name because we print options.
401# TODO: for MEMBERSHIP queries, we could sort by the most common? errexit
402# first?
403_SORTED = sorted(_OPTION_DEF.opts, key=lambda opt: opt.name)
404
405PARSE_OPTION_NUMS = [opt.index for opt in _SORTED if opt.is_parse]
406
407# Sorted because 'shopt -o -p' should be sorted, etc.
408VISIBLE_SHOPT_NUMS = [
409 opt.index for opt in _SORTED if opt.builtin == 'shopt' and opt.implemented
410]
411
412YSH_UPGRADE = [opt.index for opt in _SORTED if 'ysh:upgrade' in opt.groups]
413YSH_ALL = [opt.index for opt in _SORTED if 'ysh:all' in opt.groups]
414STRICT_ALL = [opt.index for opt in _SORTED if 'strict:all' in opt.groups]
415DEFAULT_TRUE = [opt.index for opt in _SORTED if opt.default]
416#print([opt.name for opt in _SORTED if opt.default])
417
418META_OPTIONS = ['strict:all', 'ysh:upgrade',
419 'ysh:all'] # Passed to flag parser
420
421# For printing option names to stdout. Wrapped by frontend/consts.
422OPTION_NAMES = dict((opt.index, opt.name) for opt in _SORTED)