diff --git a/mshell/Evaluator.go b/mshell/Evaluator.go index c62a6f8..efa10e2 100644 --- a/mshell/Evaluator.go +++ b/mshell/Evaluator.go @@ -900,22 +900,27 @@ MainLoop: return result } - asList, ok := top.(*MShellList) - if ok { - if asList.StdoutBehavior == STDOUT_LINES { - newMShellList := &MShellList{Items: []MShellObject{}, StandardInputFile: "", StandardOutputFile: "", StdoutBehavior: STDOUT_NONE} - var scanner *bufio.Scanner - scanner = bufio.NewScanner(strings.NewReader(stdout)) - for scanner.Scan() { - newMShellList.Items = append(newMShellList.Items, &MShellString{scanner.Text()}) - } - stack.Push(newMShellList) - } else if asList.StdoutBehavior == STDOUT_STRIPPED { - stripped := strings.TrimSpace(stdout) - stack.Push(&MShellString{stripped}) - } else if asList.StdoutBehavior == STDOUT_COMPLETE { - stack.Push(&MShellString{stdout}) + var stdoutBehavior StdoutBehavior + switch top.(type) { + case *MShellList: + stdoutBehavior = top.(*MShellList).StdoutBehavior + case *MShellPipe: + stdoutBehavior = top.(*MShellPipe).StdoutBehavior + } + + if stdoutBehavior == STDOUT_LINES { + newMShellList := &MShellList{Items: []MShellObject{}, StandardInputFile: "", StandardOutputFile: "", StdoutBehavior: STDOUT_NONE} + var scanner *bufio.Scanner + scanner = bufio.NewScanner(strings.NewReader(stdout)) + for scanner.Scan() { + newMShellList.Items = append(newMShellList.Items, &MShellString{scanner.Text()}) } + stack.Push(newMShellList) + } else if stdoutBehavior == STDOUT_STRIPPED { + stripped := strings.TrimSpace(stdout) + stack.Push(&MShellString{stripped}) + } else if stdoutBehavior == STDOUT_COMPLETE { + stack.Push(&MShellString{stdout}) } // Push the exit code onto the stack if a question was used to execute @@ -1477,7 +1482,7 @@ MainLoop: return FailWithMessage(fmt.Sprintf("%d:%d: Cannot pipe a %s.\n", t.Line, t.Column, obj1.TypeName())) } - stack.Push(&MShellPipe{*list, STDOUT_NONE}) + stack.Push(&MShellPipe{*list, list.StdoutBehavior}) } else if t.Type == READ { var reader io.Reader // Check if what we are reading from is seekable. If so, we can do a buffered read and reset the position. diff --git a/tests/pipeline.msh b/tests/pipeline.msh index bcb7658..2a33445 100644 --- a/tests/pipeline.msh +++ b/tests/pipeline.msh @@ -3,3 +3,20 @@ [sed "s/Earth/World/"] [grep World] ] |; + +# FILE:pipeline.out +[ + [printf "hello\\nEarth!\\n"] + [sed "s/Earth/World/"] "pipeline.out" > +] | ; + +[ + [printf "hello\\nEarth!\\n"] + [sed "s/Earth/World/"] +] | os ; wl + +# Swapping order of | and os shouldn't matter +[ + [printf "hello\\nEarth!\\n"] + [sed "s/Earth/World/"] +] os | ; wl diff --git a/tests/pipeline.msh.stdout b/tests/pipeline.msh.stdout index 496c875..6cdfbdf 100644 --- a/tests/pipeline.msh.stdout +++ b/tests/pipeline.msh.stdout @@ -1 +1,5 @@ World! +hello +World! +hello +World! diff --git a/tests/pipeline.out b/tests/pipeline.out new file mode 100644 index 0000000..2043f18 --- /dev/null +++ b/tests/pipeline.out @@ -0,0 +1,2 @@ +hello +World! diff --git a/tests/pipeline.out.expected b/tests/pipeline.out.expected new file mode 100644 index 0000000..2043f18 --- /dev/null +++ b/tests/pipeline.out.expected @@ -0,0 +1,2 @@ +hello +World!