Skip to content

Commit

Permalink
Add stdin function
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchpaulus committed Nov 12, 2024
1 parent 384f825 commit 8311d2a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
8 changes: 8 additions & 0 deletions mshell-go/Evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,14 @@ func (state *EvalState) Evaluate(objects []MShellParseItem, stack *MShellStack,
newList.Items = append(newList.Items, &MShellString { file })
}
stack.Push(newList)
} else if t.Lexeme == "stdin" {
// Dump all of current stdin onto the stack as a string
var buffer bytes.Buffer
_, err := buffer.ReadFrom(context.StandardInput)
if err != nil {
return FailWithMessage(fmt.Sprintf("%d:%d: Error reading from stdin: %s\n", t.Line, t.Column, err.Error()))
}
stack.Push(&MShellString { buffer.String() })
} else if t.Lexeme == "append" {
obj1, err := stack.Pop()
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions mshell/tests/stdin_keyword.msh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stdin lines "||" join wl
1 change: 1 addition & 0 deletions mshell/tests/stdin_keyword.msh.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello,||World!
4 changes: 3 additions & 1 deletion mshell/tests/test_file_go.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ TMP_ERR="$(mktemp)"

if printf %s "$1" | grep -q 'positional'; then
mshell-go "$1" Hello World > "$TMP_FILE" 2>"$TMP_ERR"
elif test "$(basename $1)" = "args.msh"; then
elif test "$(basename "$1")" = "args.msh"; then
mshell-go "$1" Hello World > "$TMP_FILE" 2>"$TMP_ERR"
elif test "$(basename "$1")" = "stdin_keyword.msh"; then
mshell-go stdin_keyword.msh < stdin_for_test.txt > "$TMP_FILE" 2>"$TMP_ERR"
else
mshell-go < "$1" > "$TMP_FILE" 2>"$TMP_ERR"
fi
Expand Down

0 comments on commit 8311d2a

Please sign in to comment.