parser, executor: implement named file-descriptor redirections ({var}>file) - #276
Merged
Conversation
The {varname} redirection form was unsupported -- {fd} was taken as an
ordinary word, so exec {fd}>file failed with "exec: {fd}: not found".
Add Redirect.fd_var; recognize an unquoted {name} glued to a redirection
operator in both parse_simple and parse_redirect_list; and in apply_redirect
allocate a fresh high, close-on-exec descriptor, open the redirection on it,
and store its number in the variable. The descriptor is recorded as an
originally-closed fd so a normal command closes it afterwards while exec keeps
it (the variable retains the number). {var}>&- / {var}<&- close the descriptor
the variable currently names; here-documents and here-strings are supported
too. declare -f reconstruction prints the {name} prefix.
Handles <, >, >>, <>, >&N, <&N, >&-, <&-, and <<EOF forms, on simple and
compound commands, with exec (permanent) or a command (temporary). Reduces the
vredir test-suite file 125 -> 33; the remainder is bash's exact
cannot-assign/ambiguous-redirect error text, array-element fd targets
({fd[0]}), and the specific high-descriptor numbers, all separate.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #275.
Root cause
The
{varname}file-descriptor redirection form was unsupported —{fd}was taken as an ordinary word (exec {fd}>file→exec: {fd}: not found).Implementation
Redirect.fd_var.{name}glued to a redirection operator (in bothparse_simpleandparse_redirect_list), so a{name}before a redirection is the fd-variable specifier rather than a command word.apply_redirectallocates a fresh high, close-on-exec descriptor, opens the redirection on it, and stores its number in the variable. It is recorded as an originally-closed fd, so a normal command closes it afterwards whileexeckeeps it open (the variable keeps the number).{var}>&-/{var}<&-close the descriptor the variable currently names.declare -freconstruction emits the{name}prefix.Supports
<,>,>>,<>,>&N,<&N,>&-,<&-, and<<EOF, on simple and compound commands, withexec(permanent) or a command (temporary).Verification
exec {fd}>file; echo hi >&$fd; exec {fd}>&-; cat file→hi;{fd}<filereads;{fd}<>file,{fd}>&1, heredoc{v}<<EOF, group{ ...; } {fd}<file— all match bash 5.3; the variable holds an integer ≥ 10.{fd} >file, quoted"{fd}", and brace expansion{a,b}>fileare unaffected.Known limitations (the residual vredir diffs)
cannot assign fd to variable/ambiguous redirect/Bad file descriptorerror text for unassignable (nameref/readonly) or invalid fd-vars is not reproduced; because those cases now run instead of failing to parse,namerefticks up slightly (248 → 258) — a byte-diff artifact, not broken behavior.{fd[0]}<&0) and bash's specific high-descriptor numbering are not implemented.