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