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

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