From 8311d2a2abd95cb83e5662ab79708417e7abe549 Mon Sep 17 00:00:00 2001 From: Mitchell Paulus Date: Tue, 12 Nov 2024 11:15:06 -0600 Subject: [PATCH] Add stdin function --- mshell-go/Evaluator.go | 8 ++++++++ mshell/tests/stdin_keyword.msh | 1 + mshell/tests/stdin_keyword.msh.stdout | 1 + mshell/tests/test_file_go.sh | 4 +++- 4 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 mshell/tests/stdin_keyword.msh create mode 100644 mshell/tests/stdin_keyword.msh.stdout diff --git a/mshell-go/Evaluator.go b/mshell-go/Evaluator.go index e64315e..c411df8 100644 --- a/mshell-go/Evaluator.go +++ b/mshell-go/Evaluator.go @@ -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 { diff --git a/mshell/tests/stdin_keyword.msh b/mshell/tests/stdin_keyword.msh new file mode 100644 index 0000000..1c8d187 --- /dev/null +++ b/mshell/tests/stdin_keyword.msh @@ -0,0 +1 @@ +stdin lines "||" join wl diff --git a/mshell/tests/stdin_keyword.msh.stdout b/mshell/tests/stdin_keyword.msh.stdout new file mode 100644 index 0000000..ba61f7b --- /dev/null +++ b/mshell/tests/stdin_keyword.msh.stdout @@ -0,0 +1 @@ +Hello,||World! diff --git a/mshell/tests/test_file_go.sh b/mshell/tests/test_file_go.sh index 6fecb61..369af9d 100755 --- a/mshell/tests/test_file_go.sh +++ b/mshell/tests/test_file_go.sh @@ -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