OILS / frontend / option_def.py View on Github | oilshell.org

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