Trying to implement the damn commands at "run".

This commit is contained in:
Andrey Parhomenko 2023-06-04 21:42:15 +03:00
parent 5251f0fbf4
commit 5dc0759533
2 changed files with 74 additions and 0 deletions

View file

@ -17,8 +17,15 @@ import (
"github.com/surdeus/goblin/src/tool/run/parser"
"github.com/surdeus/goblin/src/tool/run/vm"
"github.com/surdeus/gomtool/src/mtool"
"os/exec"
)
type Cmd struct {
input io.Reader
*exec.Cmd
output io.Writer
}
const version = "0.1.8"
var (
@ -66,7 +73,71 @@ func parseFlags() {
func setupEnv() {
e = env.NewEnv()
cmd := func(args ...string) *Cmd {
if len(args) < 1 {
panic("too few arguments")
}
cmd := exec.Command(args[0], args...)
return &Cmd{
Cmd: cmd,
input: os.Stdin,
output: os.Stdout,
}
}
e.Define("args", args)
e.Define("cmd", cmd)
e.Define("rcmd", func(args ...string) bool {
rcmd := cmd(args...)
input := rcmd.input
output := rcmd.output
stdin, err := rcmd.StdinPipe()
if err != nil {
panic(err)
}
stdout, err := rcmd.StdoutPipe()
if err != nil {
panic(err)
}
err = rcmd.Start()
if err != nil {
panic(err)
}
ibuf := make([]byte, 512)
obuf := make([]byte, 512)
for {
fmt.Println("in")
ni, err := input.Read(ibuf)
stdin.Write(ibuf[:ni])
if err == io.EOF {
break
} else if err != nil {
fmt.Println("check")
panic(err)
}
no, err := stdout.Read(obuf)
output.Write(obuf[:no])
if err == io.EOF {
break
} else if err != nil {
panic(err)
}
}
err = rcmd.Wait()
if err != nil {
fmt.Println("shit", err)
}
return err == nil
})
core.Import(e)
}

3
src/tool/run/test.anko Normal file
View file

@ -0,0 +1,3 @@
rcmd("dir")