add subshells, here-documents and redirections/pipes for control structures
- control structures can have redirections (while read l; do ...; done
< file), which apply to all commands in them; they run in the shell
- in pipes and in the background they run in a subshell (cmd | while
read l; do ...; done, for ...; done &, a && b &)
- ( list ) subshells; ( and ) are operators now
- here-documents <<EOF, <<-EOF, <<'EOF' and here-strings <<<word
- redirections of source apply to the commands of the file
- fix non-ASCII characters in quotes being mangled ('für')
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
443367f66f
commit
f66cf538c0
7 files changed
+1172
-221
No files matched your search
@@ -191,14 +191,14 @@ func tokenize(input string) ([]token, error) {
|
||||
if quote == '"' && !strings.ContainsRune("$\"\\`", rune(ch)) {
|
||||
literal("\\")
|
||||
}
|
||||
literal(string(ch))
|
||||
literal(input[i : i+1])
|
||||
inWord = true
|
||||
escaped = false
|
||||
case quote == '\'':
|
||||
if ch == '\'' {
|
||||
quote = 0
|
||||
} else {
|
||||
literal(string(ch))
|
||||
literal(input[i : i+1])
|
||||
}
|
||||
case ch == '\\':
|
||||
startRaw(i)
|
||||
@@ -263,7 +263,7 @@ func tokenize(input string) ([]token, error) {
|
||||
if ch == '"' {
|
||||
quote = 0
|
||||
} else {
|
||||
literal(string(ch))
|
||||
literal(input[i : i+1])
|
||||
}
|
||||
case ch == '"' || ch == '\'':
|
||||
startRaw(i)
|
||||
@@ -306,6 +306,10 @@ func tokenize(input string) ([]token, error) {
|
||||
}
|
||||
t := token{op: string(ch), fd: fd}
|
||||
switch {
|
||||
case strings.HasPrefix(input[i:], "<<<"):
|
||||
// <<<word: the word (and a newline) is the input
|
||||
t.op = "<<<"
|
||||
i += 2
|
||||
case ch == '>' && i+1 < len(input) && input[i+1] == '>':
|
||||
t.op = ">>"
|
||||
i++
|
||||
@@ -1418,27 +1422,8 @@ func execInput(input string) error {
|
||||
// execCommand executes a pipeline of simple commands with redirections, in
|
||||
// the background if background is set.
|
||||
func execCommand(input string, background bool) error {
|
||||
// Brace expansion comes first, so that its results are expanded further
|
||||
substStatus = 0
|
||||
input, err := expandBraceWords(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Split the input into words and operators, expanding variables and ~ on
|
||||
// the way.
|
||||
tokens, err := tokenize(input)
|
||||
var (
|
||||
synErr syntaxError
|
||||
statusErr statusError
|
||||
)
|
||||
if errors.As(err, &synErr) || errors.As(err, &statusErr) {
|
||||
return err
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid input: %w", err)
|
||||
}
|
||||
stages, err := parsePipeline(tokens)
|
||||
stages, err := expandStages(input)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1456,19 +1441,62 @@ func execCommand(input string, background bool) error {
|
||||
return runPipeline(stages, strings.TrimSpace(input), background)
|
||||
}
|
||||
|
||||
// redirect is a redirection of a simple command, e.g. 2>>file or 2>&1.
|
||||
type redirect struct {
|
||||
op string // <, >, >>, &>, &>>, >& or <&
|
||||
fd int // the redirected file descriptor
|
||||
dup int // the target file descriptor of >& and <&
|
||||
file string // the file name for all other operators
|
||||
// expandStages expands the text of a pipeline of simple commands and splits
|
||||
// it into its stages.
|
||||
func expandStages(input string) ([]stage, error) {
|
||||
// Brace expansion comes first, so that its results are expanded further
|
||||
input, err := expandBraceWords(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Split the input into words and operators, expanding variables and ~ on
|
||||
// the way.
|
||||
tokens, err := tokenize(input)
|
||||
var (
|
||||
synErr syntaxError
|
||||
statusErr statusError
|
||||
)
|
||||
if errors.As(err, &synErr) || errors.As(err, &statusErr) {
|
||||
return nil, err
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid input: %w", err)
|
||||
}
|
||||
return parsePipeline(tokens)
|
||||
}
|
||||
|
||||
// stage is a simple command of a pipeline.
|
||||
// openRedirections expands and opens the redirections of a compound command
|
||||
// and returns the resulting stdin, stdout and stderr, and the files to close
|
||||
// afterwards.
|
||||
func openRedirections(text string) ([3]*os.File, []*os.File, error) {
|
||||
fds := [3]*os.File{stdio(0), stdio(1), stdio(2)}
|
||||
stages, err := expandStages(text)
|
||||
if err != nil {
|
||||
return fds, nil, err
|
||||
}
|
||||
if len(stages) != 1 || len(stages[0].args) > 0 || len(stages[0].assigns) > 0 {
|
||||
return fds, nil, syntaxError(text)
|
||||
}
|
||||
opened, err := applyRedirects(&fds, stages[0].redirects)
|
||||
return fds, opened, err
|
||||
}
|
||||
|
||||
// redirect is a redirection of a simple command, e.g. 2>>file or 2>&1.
|
||||
type redirect struct {
|
||||
op string // <, >, >>, &>, &>>, >&, <& or <<<
|
||||
fd int // the redirected file descriptor
|
||||
dup int // the target file descriptor of >& and <&
|
||||
file string // the file name for all other operators (the text for <<<)
|
||||
}
|
||||
|
||||
// stage is a simple command of a pipeline, or a script that runs in a
|
||||
// subshell (a compound command in a pipeline or in the background).
|
||||
type stage struct {
|
||||
assigns []string // name=value before the command name
|
||||
args []string
|
||||
redirects []redirect
|
||||
script string
|
||||
}
|
||||
|
||||
// parsePipeline groups tokens into the stages of a pipeline.
|
||||
@@ -1505,6 +1533,11 @@ func parsePipeline(tokens []token) ([]stage, error) {
|
||||
return nil, syntaxError(next.op)
|
||||
}
|
||||
i++
|
||||
if t.op == "<<<" {
|
||||
// A here-string is not expanded as a pattern
|
||||
cur.redirects = append(cur.redirects, redirect{op: t.op, fd: t.fd, file: tokens[i].word})
|
||||
continue
|
||||
}
|
||||
files := expandWord(tokens[i])
|
||||
if len(files) > 1 {
|
||||
return nil, fmt.Errorf("%s: ambiguous redirect", sanitize(tokens[i].word))
|
||||
@@ -1621,18 +1654,22 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
err = statusError(substStatus)
|
||||
}
|
||||
}
|
||||
if err != nil || len(st.args) == 0 {
|
||||
if err != nil || len(st.args) == 0 && st.script == "" {
|
||||
// A command without arguments only creates or truncates the files
|
||||
closeOwned()
|
||||
return finishedProc(err)
|
||||
}
|
||||
|
||||
fn, isFunc := functions[st.args[0]]
|
||||
var fn *funcDef
|
||||
isFunc := false
|
||||
if st.script == "" {
|
||||
fn, isFunc = functions[st.args[0]]
|
||||
}
|
||||
if isFunc && !inPipeline && !background {
|
||||
defer closeOwned()
|
||||
return finishedProc(callFunction(fn, st.args, fds, st.assigns))
|
||||
}
|
||||
if builtin, ok := builtins[st.args[0]]; !isFunc && ok {
|
||||
if builtin, ok := builtins[firstArg(st.args)]; !isFunc && st.script == "" && ok {
|
||||
switch {
|
||||
case inPipeline && builtin.changesState:
|
||||
closeOwned()
|
||||
@@ -1641,10 +1678,11 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
closeOwned()
|
||||
return finishedProc(fmt.Errorf("%s: cannot be run in the background", st.args[0]))
|
||||
}
|
||||
shellStderr := stdio(2)
|
||||
run := func() error {
|
||||
defer closeOwned()
|
||||
err := runBuiltin(st.args, fds[0], fds[1])
|
||||
if err != nil && fds[2] != stdio(2) {
|
||||
if err != nil && fds[2] != shellStderr {
|
||||
// Report the error where stderr is redirected to
|
||||
fmt.Fprintln(fds[2], "gosh:", err)
|
||||
return statusError(1)
|
||||
@@ -1652,8 +1690,12 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
return err
|
||||
}
|
||||
if !inPipeline && !background {
|
||||
// name=value before a builtin applies only while it runs
|
||||
// name=value before a builtin applies only while it runs, its
|
||||
// redirections also to the commands it runs (source file > out)
|
||||
defer setTemporarily(st.assigns)()
|
||||
saved := ioOverride
|
||||
ioOverride = fds
|
||||
defer func() { ioOverride = saved }()
|
||||
return finishedProc(run())
|
||||
}
|
||||
p := &proc{done: make(chan error, 1)}
|
||||
@@ -1662,7 +1704,13 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
if isFunc {
|
||||
if st.script != "" {
|
||||
cmd, err = subshellCommand(st.script, positional)
|
||||
if err != nil {
|
||||
closeOwned()
|
||||
return finishedProc(err)
|
||||
}
|
||||
} else if isFunc {
|
||||
// A function in a pipeline or in the background runs in a subshell
|
||||
cmd, err = subshellCommand(`"$@"`, st.args)
|
||||
if err != nil {
|
||||
@@ -1695,7 +1743,7 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
// The child has its own copies of the files now
|
||||
closeOwned()
|
||||
if errors.Is(err, exec.ErrNotFound) {
|
||||
err = fmt.Errorf("%s: %w", sanitize(st.args[0]), errCommandNotFound)
|
||||
err = fmt.Errorf("%s: %w", sanitize(firstArg(st.args)), errCommandNotFound)
|
||||
}
|
||||
if err != nil {
|
||||
return finishedProc(err)
|
||||
@@ -1710,6 +1758,13 @@ func startStage(st stage, stdin, stdout *os.File, j *job, inPipeline, background
|
||||
return &proc{cmd: cmd}
|
||||
}
|
||||
|
||||
func firstArg(args []string) string {
|
||||
if len(args) == 0 {
|
||||
return ""
|
||||
}
|
||||
return args[0]
|
||||
}
|
||||
|
||||
// setTemporarily sets the variables of the assignments and returns a
|
||||
// function that restores the previous values.
|
||||
func setTemporarily(assigns []string) func() {
|
||||
@@ -1758,6 +1813,16 @@ func applyRedirects(fds *[3]*os.File, redirects []redirect) ([]*os.File, error)
|
||||
continue
|
||||
}
|
||||
|
||||
if r.op == "<<<" {
|
||||
f, err := hereFile(r.file + "\n")
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
opened = append(opened, f)
|
||||
fds[r.fd] = f
|
||||
continue
|
||||
}
|
||||
|
||||
flags := os.O_WRONLY | os.O_CREATE | os.O_TRUNC
|
||||
switch r.op {
|
||||
case "<":
|
||||
@@ -1783,6 +1848,26 @@ func applyRedirects(fds *[3]*os.File, redirects []redirect) ([]*os.File, error)
|
||||
return opened, nil
|
||||
}
|
||||
|
||||
// hereFile returns a file to read content from: a temporary file that is
|
||||
// already removed (like bash does for here-documents), so that large inputs
|
||||
// work and nothing is left behind.
|
||||
func hereFile(content string) (*os.File, error) {
|
||||
f, err := os.CreateTemp("", "gosh-here-*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
os.Remove(f.Name())
|
||||
if _, err := f.WriteString(content); err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
f.Close()
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// builtins lists the built-in commands. Commands that change the state of
|
||||
// the shell cannot be used in a pipeline, since there they would run
|
||||
// concurrently (sh runs them in a subshell where they have no effect).
|
||||
@@ -2021,12 +2106,16 @@ Features:
|
||||
a || b (b only if a failed); $? is the exit status of the last command
|
||||
- Control structures: if cmd; then ...; elif cmd; then ...; else ...; fi,
|
||||
for x in words; do ...; done, while cmd; do ...; done, until ...,
|
||||
case word in pattern|pattern) ...;; esac, { cmd; cmd; } groups;
|
||||
! cmd negates the status; they can span several lines (prompt "> ")
|
||||
case word in pattern|pattern) ...;; esac, { cmd; cmd; } groups,
|
||||
( cmd; cmd ) subshells; ! cmd negates the status; they can span
|
||||
several lines (prompt "> "), have redirections (done < file) and can
|
||||
be used in pipes (| while read l; do ...; done) and with &
|
||||
- Functions: name() { ...; } (or function name { ...; }); the arguments
|
||||
are $1, $2, ...; in pipes, & and $(...) they run in a subshell
|
||||
- Pipes (a | b) and redirections: < file, > file, >> file, 2> file,
|
||||
2>> file, 2>&1, >&2, &> file (stdout and stderr), &>> file
|
||||
2>> file, 2>&1, >&2, &> file (stdout and stderr), &>> file,
|
||||
<<EOF here-documents (<<-EOF strips tabs, <<'EOF' expands nothing),
|
||||
<<<word here-strings
|
||||
- Job control: command & runs in the background, Ctrl+Z stops the
|
||||
foreground job; jobs, fg, bg and wait manage the jobs
|
||||
- Aliases replace the first word of a command in interactive shells
|
||||
|
||||
Reference in new issue
Block a user