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