Adds input redirection for subprocess

This commit is contained in:
sloum 2022-05-14 16:01:39 -07:00
parent 0a4307f3f2
commit 5a9858e7ad
4 changed files with 29 additions and 4 deletions

View File

@ -444,7 +444,7 @@ Implemented:
<li><code>(chmod [filepath: string] [file-mode: number])</code>: <code>()</code></li>
<li><code>(chdir [filepath: string])</code>: <code>()</code></li>
<li><code>(env [[env-key: string]] [[env-value: string]])</code>: <code>list|string|()</code></li>
<li><code>(subprocess [list] [[output-redirection: IOHandle|#f]] [[error-redirection: IOHandle|#f]])</code>: <code>number</code> (Passing `#f` to output redirection allows you to do stdout while still redirecting stderr. The `#f` for stderr only exists for symetry)</li>
<li><code>(subprocess [list] [[output-redirection: IOHandle|#f]] [[error-redirection: IOHandle|#f]] [[input-redirection: IOHandle|#f]])</code>: <code>number</code> (Passing `#f` to output redirection allows you to do stdout while still redirecting stderr. The `#f` for stderr only exists for symetry)</li>
<li><code>(mkdir [path: string] [permissions: number] [[make-all: bool]])</code>: <code>()</code></li>
<li><code>(rm [path: string] [[make-all: bool]])</code>: <code>()</code></li>
<li><code>(mv [from-path: string] [to-path: string])</code>: <code>()</code></li>

27
lib.go
View File

@ -2245,7 +2245,18 @@ var stdLibrary = vars{
}
}
var o, e *IOHandle
var o, e, i *IOHandle
if len(a) > 3 {
i1, i1ok := a[3].(*IOHandle)
if !i1ok {
if b, ok := a[3].(bool); !ok || (ok && b) {
return exception("'subprocess' was given an input redirection that is not an IOHandle or the boolean '#f'")
}
} else if !i1.Open {
return exception("'subprocess' was given an input redirection IOHandle that is already closed")
}
i = i1
}
if len(a) > 2 {
e1, e1ok := a[2].(*IOHandle)
if !e1ok {
@ -2300,6 +2311,20 @@ var stdLibrary = vars{
return exception("'subprocess' was given an error redirection IOHandle of an unknown type")
}
}
if i != nil {
switch io := i.Obj.(type) {
case *os.File:
cmd.Stdin = io
case *net.Conn:
cmd.Stdin = (*io)
case *tls.Conn:
cmd.Stdin = io
case *strings.Builder:
cmd.Stdin = strings.NewReader(io.String())
default:
return exception("'subprocess' was given an input redirection IOHandle of an unknown type")
}
}
err := cmd.Run()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {

View File

@ -16,7 +16,7 @@ import (
ln "github.com/peterh/liner"
)
const version = "0.5.17"
const version = "0.5.18"
const globalLibPath = "/usr/local/lib/slope/modules/"

View File

@ -157,7 +157,7 @@ var usageStrings = map[string]string{
"string-ref": "(string-ref [string] [index: number]) => string\n\nReturns the character at the given index as a string. Like with lists indexes begin at 0",
"string-trim-space": "(string-trim-space [string]) => string\n\nRemoves any leading or trailing white-space from `string`",
"string-upper": "(string-upper [string]) => string\n\nConverts all characters in the given string to their uppercase form and returns the new uppercase string",
"subprocess": "(subprocess [list] [[output-redirection: IOHandle|#f]] [[error-redirection: IOHandle|#f]]) => number\n\nPassing `#f` to output redirection allows you to do stdout while still redirecting stderr. The `#f` for stderr only exists for symetry, and will not redirect stderr",
"subprocess": "(subprocess [list] [[output-redirection: IOHandle|#f]] [[error-redirection: IOHandle|#f]] [[input-redirection: IOHandle|#f]]) => number\n\nPassing `#f` to any of the redirections will have them use the std version of the redirection (stdout, stderr, or stdin). If you want to have something redirect to devnull you must explicitly pass it",
"symbol?": "(symbol? [value]) => bool\n\nChecks if the given value is a symbol",
"sys-args": "sys-args => list\n\nsys-args is the list of arguments that were invoked to run the currently running slope program. The first argument in the list is always the name of the program that was invoked, which will not include the slope interpreter itself (if invoked directly it will be removed from the list).",
"term-char-mode": "(term-char-mode) => ()\n\nChanges the terminal to char mode",