From 5dc0759533a6150b70c88bc01d04034c45144562 Mon Sep 17 00:00:00 2001 From: surdeus Date: Sun, 4 Jun 2023 21:42:15 +0300 Subject: [PATCH] Trying to implement the damn commands at "run". --- src/tool/run/anko.go | 71 ++++++++++++++++++++++++++++++++++++++++++ src/tool/run/test.anko | 3 ++ 2 files changed, 74 insertions(+) create mode 100644 src/tool/run/test.anko diff --git a/src/tool/run/anko.go b/src/tool/run/anko.go index 84fbebe..05ace09 100644 --- a/src/tool/run/anko.go +++ b/src/tool/run/anko.go @@ -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) } diff --git a/src/tool/run/test.anko b/src/tool/run/test.anko new file mode 100644 index 0000000..2979faa --- /dev/null +++ b/src/tool/run/test.anko @@ -0,0 +1,3 @@ + +rcmd("dir") +