From 754a3409bcbefc2b400a95c46cde1786dc1d1ef5 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Mon, 13 Apr 2015 15:23:48 +0200 Subject: [PATCH] Add gexpect It will be needed in functional tests --- Godeps/Godeps.json | 13 + .../github.com/ThomasRooney/gexpect/LICENCE | 7 + .../github.com/ThomasRooney/gexpect/README.md | 53 ++++ .../ThomasRooney/gexpect/examples/python.go | 22 ++ .../ThomasRooney/gexpect/examples/screen.go | 53 ++++ .../ThomasRooney/gexpect/gexpect.go | 277 ++++++++++++++++++ .../ThomasRooney/gexpect/gexpect_test.go | 122 ++++++++ .../github.com/kballard/go-shellquote/LICENSE | 19 ++ .../github.com/kballard/go-shellquote/README | 36 +++ .../kballard/go-shellquote/both_test.go | 29 ++ .../github.com/kballard/go-shellquote/doc.go | 3 + .../kballard/go-shellquote/quote.go | 102 +++++++ .../kballard/go-shellquote/quote_test.go | 28 ++ .../kballard/go-shellquote/unquote.go | 144 +++++++++ .../kballard/go-shellquote/unquote_test.go | 53 ++++ .../src/github.com/kr/pty/.gitignore | 4 + .../_workspace/src/github.com/kr/pty/License | 23 ++ .../src/github.com/kr/pty/README.md | 36 +++ .../_workspace/src/github.com/kr/pty/doc.go | 16 + .../_workspace/src/github.com/kr/pty/ioctl.go | 11 + .../src/github.com/kr/pty/ioctl_bsd.go | 39 +++ .../src/github.com/kr/pty/mktypes.bash | 19 ++ .../src/github.com/kr/pty/pty_darwin.go | 60 ++++ .../src/github.com/kr/pty/pty_freebsd.go | 73 +++++ .../src/github.com/kr/pty/pty_linux.go | 46 +++ .../src/github.com/kr/pty/pty_unsupported.go | 11 + .../_workspace/src/github.com/kr/pty/run.go | 28 ++ .../_workspace/src/github.com/kr/pty/types.go | 10 + .../src/github.com/kr/pty/types_freebsd.go | 15 + .../_workspace/src/github.com/kr/pty/util.go | 35 +++ .../src/github.com/kr/pty/ztypes_386.go | 9 + .../src/github.com/kr/pty/ztypes_amd64.go | 9 + .../src/github.com/kr/pty/ztypes_arm.go | 9 + .../github.com/kr/pty/ztypes_freebsd_386.go | 13 + .../github.com/kr/pty/ztypes_freebsd_amd64.go | 14 + .../github.com/kr/pty/ztypes_freebsd_arm.go | 13 + .../src/github.com/kr/pty/ztypes_ppc64.go | 11 + .../src/github.com/kr/pty/ztypes_ppc64le.go | 11 + .../src/github.com/kr/pty/ztypes_s390x.go | 11 + stage1/dummy.go | 1 + 40 files changed, 1488 insertions(+) create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/LICENCE create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/README.md create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/python.go create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/screen.go create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect.go create mode 100644 Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect_test.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/LICENSE create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/README create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/both_test.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/doc.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/quote.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/quote_test.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote.go create mode 100644 Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote_test.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/.gitignore create mode 100644 Godeps/_workspace/src/github.com/kr/pty/License create mode 100644 Godeps/_workspace/src/github.com/kr/pty/README.md create mode 100644 Godeps/_workspace/src/github.com/kr/pty/doc.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ioctl.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ioctl_bsd.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/mktypes.bash create mode 100644 Godeps/_workspace/src/github.com/kr/pty/pty_darwin.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/pty_freebsd.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/pty_linux.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/pty_unsupported.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/run.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/types.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/types_freebsd.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/util.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_386.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_amd64.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_arm.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_386.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_amd64.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_arm.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64le.go create mode 100644 Godeps/_workspace/src/github.com/kr/pty/ztypes_s390x.go diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 6624a6b..32dcb7a 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -15,6 +15,10 @@ "Comment": "v0.12.0-270-g53ec66c", "Rev": "53ec66caf4e952a1384ec93b9f0cde37616e4caf" }, + { + "ImportPath": "github.com/ThomasRooney/gexpect", + "Rev": "025428b8021ad3cef9602997658d9eca6c145d4e" + }, { "ImportPath": "github.com/appc/docker2aci/lib", "Rev": "4e4333c116ee15c74ea06ec036c1bc4fad651628" @@ -110,6 +114,15 @@ "ImportPath": "github.com/gorilla/mux", "Rev": "e444e69cbd2e2e3e0749a2f3c717cec491552bbf" }, + { + "ImportPath": "github.com/kballard/go-shellquote", + "Rev": "e5c918b80c17694cbc49aab32a759f9a40067f5d" + }, + { + "ImportPath": "github.com/kr/pty", + "Comment": "release.r56-25-g05017fc", + "Rev": "05017fcccf23c823bfdea560dcc958a136e54fb7" + }, { "ImportPath": "github.com/mitchellh/ioprogress", "Rev": "b0a0b6773359ef5016d5ecc4522150e43a189a53" diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/LICENCE b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/LICENCE new file mode 100644 index 0000000..50adb0f --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/LICENCE @@ -0,0 +1,7 @@ +Copyright (C) 2014 Thomas Rooney + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/README.md b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/README.md new file mode 100644 index 0000000..784608e --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/README.md @@ -0,0 +1,53 @@ +## Gexpect + +Gexpect is a pure golang expect-like module. + +It makes it simple and safe to control other terminal applications. + +It provides pexpect-like syntax for golang + + child, err := gexpect.Spawn("python") + if err != nil { + panic(err) + } + child.Expect(">>>") + child.SendLine("print 'Hello World'") + child.Interact() + child.Close() + +It's fast, with its 'expect' function working off a variant of Knuth-Morris-Pratt on Standard Output/Error streams + +It also provides interface functions that make it much simpler to work with subprocesses + + child.Spawn("/bin/sh -c 'echo \"my complicated command\" | tee log | cat > log2'") + + child.ReadLine() // ReadLine() (string, error) + + child.ReadUntil(' ') // ReadUntil(delim byte) ([]byte, error) + + child.SendLine("/bin/sh -c 'echo Hello World | tee foo'") // SendLine(command string) (error) + + child.Wait() // Wait() (error) + + sender, reciever := child.AsyncInteractChannels() // AsyncInteractChannels() (chan string, chan string) + sender <- "echo Hello World\n" // Send to stdin + + line, open := <- reciever // Recieve a line from stdout/stderr + // When the subprocess stops (e.g. with child.Close()) , receiver is closed + if open { + fmt.Printf("Received %s", line)] + } + + +Free, MIT open source licenced, etc etc. + +Check gexpect_test.go and the examples folder for full examples + +### Golang Dependencies + + "github.com/kballard/go-shellquote" + "github.com/kr/pty" + +# Credits + + KMP Algorithm: "http://blog.databigbang.com/searching-for-substrings-in-streams-a-slight-modification-of-the-knuth-morris-pratt-algorithm-in-haxe/" \ No newline at end of file diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/python.go b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/python.go new file mode 100644 index 0000000..4e7d0c9 --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/python.go @@ -0,0 +1,22 @@ +package main + +import "github.com/coreos/rkt/Godeps/_workspace/src/github.com/ThomasRooney/gexpect" +import "fmt" + +func main() { + fmt.Printf("Starting python.. \n") + child, err := gexpect.Spawn("python") + if err != nil { + panic(err) + } + fmt.Printf("Expecting >>>.. \n") + child.Expect(">>>") + fmt.Printf("print 'Hello World'..\n") + child.SendLine("print 'Hello World'") + child.Expect(">>>") + + fmt.Printf("Interacting.. \n") + child.Interact() + fmt.Printf("Done \n") + child.Close() +} diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/screen.go b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/screen.go new file mode 100644 index 0000000..c6b014a --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/examples/screen.go @@ -0,0 +1,53 @@ +package main + +import "github.com/coreos/rkt/Godeps/_workspace/src/github.com/ThomasRooney/gexpect" +import "fmt" +import "strings" + +func main() { + waitChan := make(chan string) + + fmt.Printf("Starting screen.. \n") + + child, err := gexpect.Spawn("screen") + if err != nil { + panic(err) + } + + sender, reciever := child.AsyncInteractChannels() + go func() { + waitString := "" + count := 0 + for { + select { + case waitString = <-waitChan: + count++ + case msg, open := <-reciever: + if !open { + return + } + fmt.Printf("Recieved: %s\n", msg) + + if strings.Contains(msg, waitString) { + if count >= 1 { + waitChan <- msg + count -= 1 + } + } + } + } + }() + wait := func(str string) { + waitChan <- str + <-waitChan + } + fmt.Printf("Waiting until started.. \n") + wait(" ") + fmt.Printf("Sending Enter.. \n") + sender <- "\n" + wait("$") + fmt.Printf("Sending echo.. \n") + sender <- "echo Hello World\n" + wait("Hello World") + fmt.Printf("Received echo. \n") +} diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect.go b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect.go new file mode 100644 index 0000000..9092632 --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect.go @@ -0,0 +1,277 @@ +package gexpect + +import ( + "errors" + shell "github.com/coreos/rkt/Godeps/_workspace/src/github.com/kballard/go-shellquote" + "github.com/coreos/rkt/Godeps/_workspace/src/github.com/kr/pty" + "io" + "os" + "os/exec" + "regexp" + "time" +) + +type ExpectSubprocess struct { + Cmd *exec.Cmd + f *os.File +} + +func SpawnAtDirectory(command string, directory string) (*ExpectSubprocess, error) { + expect, err := _spawn(command) + if err != nil { + return nil, err + } + expect.Cmd.Dir = directory + return _start(expect) +} + +func Command(command string) (*ExpectSubprocess, error) { + expect, err := _spawn(command) + if err != nil { + return nil, err + } + return expect, nil +} + +func (expect *ExpectSubprocess) Start() error { + _, err := _start(expect) + return err +} + +func Spawn(command string) (*ExpectSubprocess, error) { + expect, err := _spawn(command) + if err != nil { + return nil, err + } + return _start(expect) +} + +func (expect *ExpectSubprocess) Close() error { + return expect.Cmd.Process.Kill() +} + +func (expect *ExpectSubprocess) AsyncInteractChannels() (send chan string, receive chan string) { + receive = make(chan string) + send = make(chan string) + + go func() { + for { + str, err := expect.ReadLine() + if err != nil { + close(receive) + return + } + receive <- str + } + }() + + go func() { + for { + select { + case sendCommand, exists := <-send: + { + if !exists { + return + } + err := expect.Send(sendCommand) + if err != nil { + receive <- "gexpect Error: " + err.Error() + return + } + } + } + } + }() + + return +} + +// This quite possibly won't work as we're operating on an incomplete stream. It might work if all the input is within one +// Flush, but that can't be relied upon. I need to find a nice, safe way to apply a regex to a stream of partial content, given we +// don't not knowing how long our input is, and thus can't buffer it. Until that point, please just use Expect, or use the channel +// to parse the stream yourself. +func (expect *ExpectSubprocess) ExpectRegex(regexSearchString string) (e error) { + var size = len(regexSearchString) + + if size < 255 { + size = 255 + } + + chunk := make([]byte, size) + + for { + n, err := expect.f.Read(chunk) + + if err != nil { + return err + } + success, err := regexp.Match(regexSearchString, chunk[:n]) + if err != nil { + return err + } + if success { + return nil + } + } +} + +func buildKMPTable(searchString string) []int { + pos := 2 + cnd := 0 + length := len(searchString) + + var table []int + if length < 2 { + length = 2 + } + + table = make([]int, length) + table[0] = -1 + table[1] = 0 + + for pos < len(searchString) { + if searchString[pos-1] == searchString[cnd] { + cnd += 1 + table[pos] = cnd + pos += 1 + } else if cnd > 0 { + cnd = table[cnd] + } else { + table[pos] = 0 + pos += 1 + } + } + return table +} + +func (expect *ExpectSubprocess) ExpectTimeout(searchString string, timeout time.Duration) (e error) { + result := make(chan error) + go func() { + result <- expect.Expect(searchString) + }() + select { + case e = <-result: + case <-time.After(timeout): + e = errors.New("Expect timed out.") + } + return e +} + +func (expect *ExpectSubprocess) Expect(searchString string) (e error) { + chunk := make([]byte, len(searchString)*2) + target := len(searchString) + m := 0 + i := 0 + // Build KMP Table + table := buildKMPTable(searchString) + + for { + n, err := expect.f.Read(chunk) + + if err != nil { + return err + } + offset := m + i + for m+i-offset < n { + if searchString[i] == chunk[m+i-offset] { + i += 1 + if i == target { + return nil + } + } else { + m += i - table[i] + if table[i] > -1 { + i = table[i] + } else { + i = 0 + } + } + } + } +} + +func (expect *ExpectSubprocess) Send(command string) error { + _, err := io.WriteString(expect.f, command) + return err +} + +func (expect *ExpectSubprocess) SendLine(command string) error { + _, err := io.WriteString(expect.f, command+"\r\n") + return err +} + +func (expect *ExpectSubprocess) Interact() { + defer expect.Cmd.Wait() + go io.Copy(os.Stdout, expect.f) + go io.Copy(os.Stderr, expect.f) + go io.Copy(expect.f, os.Stdin) +} + +func (expect *ExpectSubprocess) ReadUntil(delim byte) ([]byte, error) { + join := make([]byte, 1, 512) + chunk := make([]byte, 255) + + for { + + n, err := expect.f.Read(chunk) + + if err != nil { + return join, err + } + + for i := 0; i < n; i++ { + if chunk[i] == delim { + return join, nil + } else { + join = append(join, chunk[i]) + } + } + } +} + +func (expect *ExpectSubprocess) Wait() error { + return expect.Cmd.Wait() +} + +func (expect *ExpectSubprocess) ReadLine() (string, error) { + str, err := expect.ReadUntil('\n') + if err != nil { + return "", err + } + return string(str), nil +} + +func _start(expect *ExpectSubprocess) (*ExpectSubprocess, error) { + f, err := pty.Start(expect.Cmd) + if err != nil { + return nil, err + } + expect.f = f + + return expect, nil +} + +func _spawn(command string) (*ExpectSubprocess, error) { + wrapper := new(ExpectSubprocess) + + splitArgs, err := shell.Split(command) + if err != nil { + return nil, err + } + numArguments := len(splitArgs) - 1 + if numArguments < 0 { + return nil, errors.New("gexpect: No command given to spawn") + } + path, err := exec.LookPath(splitArgs[0]) + if err != nil { + return nil, err + } + + if numArguments >= 1 { + wrapper.Cmd = exec.Command(path, splitArgs[1:]...) + } else { + wrapper.Cmd = exec.Command(path) + } + + return wrapper, nil +} diff --git a/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect_test.go b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect_test.go new file mode 100644 index 0000000..7c40cb8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/ThomasRooney/gexpect/gexpect_test.go @@ -0,0 +1,122 @@ +package gexpect + +import ( + "log" + "strings" + "testing" +) + +func TestHelloWorld(*testing.T) { + log.Printf("Testing Hello World... ") + child, err := Spawn("echo \"Hello World\"") + if err != nil { + panic(err) + } + err = child.Expect("Hello World") + if err != nil { + panic(err) + } + log.Printf("Success\n") +} + +func TestHelloWorldFailureCase(*testing.T) { + log.Printf("Testing Hello World Failure case... ") + child, err := Spawn("echo \"Hello World\"") + if err != nil { + panic(err) + } + err = child.Expect("YOU WILL NEVER FIND ME") + if err != nil { + log.Printf("Success\n") + return + } + panic("Expected an error for TestHelloWorldFailureCase") +} + +func TestBiChannel(*testing.T) { + log.Printf("Testing BiChannel screen... ") + child, err := Spawn("screen") + if err != nil { + panic(err) + } + sender, reciever := child.AsyncInteractChannels() + wait := func(str string) { + for { + msg, open := <-reciever + if !open { + return + } + if strings.Contains(msg, str) { + return + } + } + } + sender <- "\n" + sender <- "echo Hello World\n" + wait("Hello World") + sender <- "times\n" + wait("s") + sender <- "^D\n" + log.Printf("Success\n") + +} + +func TestExpectRegex(*testing.T) { + log.Printf("Testing ExpectRegex... ") + + child, err := Spawn("/bin/sh times") + if err != nil { + panic(err) + } + child.ExpectRegex("Name") + log.Printf("Success\n") + +} + +func TestCommandStart(*testing.T) { + log.Printf("Testing Command... ") + + // Doing this allows you to modify the cmd struct prior to execution, for example to add environment variables + child, err := Command("echo 'Hello World'") + if err != nil { + panic(err) + } + child.Start() + child.Expect("Hello World") + log.Printf("Success\n") +} + +func TestExpectFtp(*testing.T) { + log.Printf("Testing Ftp... ") + + child, err := Spawn("ftp ftp.openbsd.org") + if err != nil { + panic(err) + } + child.Expect("Name") + child.SendLine("anonymous") + child.Expect("Password") + child.SendLine("pexpect@sourceforge.net") + child.Expect("ftp> ") + child.SendLine("cd /pub/OpenBSD/3.7/packages/i386") + child.Expect("ftp> ") + child.SendLine("bin") + child.Expect("ftp> ") + child.SendLine("prompt") + child.Expect("ftp> ") + child.SendLine("pwd") + child.Expect("ftp> ") + log.Printf("Success\n") +} + +func TestInteractPing(*testing.T) { + log.Printf("Testing Ping interact... \n") + + child, err := Spawn("ping -c8 8.8.8.8") + if err != nil { + panic(err) + } + child.Interact() + log.Printf("Success\n") + +} diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/LICENSE b/Godeps/_workspace/src/github.com/kballard/go-shellquote/LICENSE new file mode 100644 index 0000000..a6d7731 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2014 Kevin Ballard + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the "Software"), +to deal in the Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, sublicense, +and/or sell copies of the Software, and to permit persons to whom the +Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE +OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/README b/Godeps/_workspace/src/github.com/kballard/go-shellquote/README new file mode 100644 index 0000000..4d34e87 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/README @@ -0,0 +1,36 @@ +PACKAGE + +package shellquote + import "github.com/kballard/go-shellquote" + + Shellquote provides utilities for joining/splitting strings using sh's + word-splitting rules. + +VARIABLES + +var ( + UnterminatedSingleQuoteError = errors.New("Unterminated single-quoted string") + UnterminatedDoubleQuoteError = errors.New("Unterminated double-quoted string") + UnterminatedEscapeError = errors.New("Unterminated backslash-escape") +) + + +FUNCTIONS + +func Join(args ...string) string + Join quotes each argument and joins them with a space. If passed to + /bin/sh, the resulting string will be split back into the original + arguments. + +func Split(input string) (words []string, err error) + Split splits a string according to /bin/sh's word-splitting rules. It + supports backslash-escapes, single-quotes, and double-quotes. Notably it + does not support the $'' style of quoting. It also doesn't attempt to + perform any other sort of expansion, including brace expansion, shell + expansion, or pathname expansion. + + If the given input has an unterminated quoted string or ends in a + backslash-escape, one of UnterminatedSingleQuoteError, + UnterminatedDoubleQuoteError, or UnterminatedEscapeError is returned. + + diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/both_test.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/both_test.go new file mode 100644 index 0000000..9cba3c8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/both_test.go @@ -0,0 +1,29 @@ +package shellquote + +import ( + "reflect" + "testing" + "testing/quick" +) + +// this is called bothtest because it tests Split and Join together + +func TestJoinSplit(t *testing.T) { + f := func(strs []string) bool { + // Join, then split, the input + combined := Join(strs...) + split, err := Split(combined) + if err != nil { + t.Logf("Error splitting %#v: %v", combined, err) + return false + } + if !reflect.DeepEqual(strs, split) { + t.Logf("Input %q did not match output %q", strs, split) + return false + } + return true + } + if err := quick.Check(f, nil); err != nil { + t.Error(err) + } +} diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/doc.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/doc.go new file mode 100644 index 0000000..9445fa4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/doc.go @@ -0,0 +1,3 @@ +// Shellquote provides utilities for joining/splitting strings using sh's +// word-splitting rules. +package shellquote diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote.go new file mode 100644 index 0000000..f6cacee --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote.go @@ -0,0 +1,102 @@ +package shellquote + +import ( + "bytes" + "strings" + "unicode/utf8" +) + +// Join quotes each argument and joins them with a space. +// If passed to /bin/sh, the resulting string will be split back into the +// original arguments. +func Join(args ...string) string { + var buf bytes.Buffer + for i, arg := range args { + if i != 0 { + buf.WriteByte(' ') + } + quote(arg, &buf) + } + return buf.String() +} + +const ( + specialChars = "\\'\"`${[|&;<>()*?!" + extraSpecialChars = " \t\n" + prefixChars = "~" +) + +func quote(word string, buf *bytes.Buffer) { + // We want to try to produce a "nice" output. As such, we will + // backslash-escape most characters, but if we encounter a space, or if we + // encounter an extra-special char (which doesn't work with + // backslash-escaping) we switch over to quoting the whole word. We do this + // with a space because it's typically easier for people to read multi-word + // arguments when quoted with a space rather than with ugly backslashes + // everywhere. + origLen := buf.Len() + + if len(word) == 0 { + // oops, no content + buf.WriteString("''") + return + } + + cur, prev := word, word + atStart := true + for len(cur) > 0 { + c, l := utf8.DecodeRuneInString(cur) + cur = cur[l:] + if strings.ContainsRune(specialChars, c) || (atStart && strings.ContainsRune(prefixChars, c)) { + // copy the non-special chars up to this point + if len(cur) < len(prev) { + buf.WriteString(word[0 : len(prev)-len(cur)-l]) + } + buf.WriteByte('\\') + buf.WriteRune(c) + prev = cur + } else if strings.ContainsRune(extraSpecialChars, c) { + // start over in quote mode + buf.Truncate(origLen) + goto quote + } + atStart = false + } + if len(prev) > 0 { + buf.WriteString(prev) + } + return + +quote: + // quote mode + // Use single-quotes, but if we find a single-quote in the word, we need + // to terminate the string, emit an escaped quote, and start the string up + // again + inQuote := false + for len(word) > 0 { + i := strings.IndexRune(word, '\'') + if i == -1 { + break + } + if i > 0 { + if !inQuote { + buf.WriteByte('\'') + inQuote = true + } + buf.WriteString(word[0:i]) + word = word[i+1:] + } + if inQuote { + buf.WriteByte('\'') + inQuote = false + } + buf.WriteString("\\'") + } + if len(word) > 0 { + if !inQuote { + buf.WriteByte('\'') + } + buf.WriteString(word) + buf.WriteByte('\'') + } +} diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote_test.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote_test.go new file mode 100644 index 0000000..a4d2d82 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/quote_test.go @@ -0,0 +1,28 @@ +package shellquote + +import ( + "testing" +) + +func TestSimpleJoin(t *testing.T) { + for _, elem := range simpleJoinTest { + output := Join(elem.input...) + if output != elem.output { + t.Errorf("Input %q, got %q, expected %q", elem.input, output, elem.output) + } + } +} + +var simpleJoinTest = []struct { + input []string + output string +}{ + {[]string{"test"}, "test"}, + {[]string{"hello goodbye"}, "'hello goodbye'"}, + {[]string{"hello", "goodbye"}, "hello goodbye"}, + {[]string{"don't you know the dewey decimal system?"}, "'don'\\''t you know the dewey decimal system?'"}, + {[]string{"don't", "you", "know", "the", "dewey", "decimal", "system?"}, "don\\'t you know the dewey decimal system\\?"}, + {[]string{"~user", "u~ser", " ~user", "!~user"}, "\\~user u~ser ' ~user' \\!~user"}, + {[]string{"foo*", "M{ovies,usic}", "ab[cd]", "%3"}, "foo\\* M\\{ovies,usic} ab\\[cd] %3"}, + {[]string{"one", "", "three"}, "one '' three"}, +} diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote.go new file mode 100644 index 0000000..ba3a0f2 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote.go @@ -0,0 +1,144 @@ +package shellquote + +import ( + "bytes" + "errors" + "strings" + "unicode/utf8" +) + +var ( + UnterminatedSingleQuoteError = errors.New("Unterminated single-quoted string") + UnterminatedDoubleQuoteError = errors.New("Unterminated double-quoted string") + UnterminatedEscapeError = errors.New("Unterminated backslash-escape") +) + +var ( + splitChars = " \n\t" + singleChar = '\'' + doubleChar = '"' + escapeChar = '\\' + doubleEscapeChars = "$`\"\n\\" +) + +// Split splits a string according to /bin/sh's word-splitting rules. It +// supports backslash-escapes, single-quotes, and double-quotes. Notably it does +// not support the $'' style of quoting. It also doesn't attempt to perform any +// other sort of expansion, including brace expansion, shell expansion, or +// pathname expansion. +// +// If the given input has an unterminated quoted string or ends in a +// backslash-escape, one of UnterminatedSingleQuoteError, +// UnterminatedDoubleQuoteError, or UnterminatedEscapeError is returned. +func Split(input string) (words []string, err error) { + var buf bytes.Buffer + words = make([]string, 0) + + for len(input) > 0 { + // skip any splitChars at the start + c, l := utf8.DecodeRuneInString(input) + if strings.ContainsRune(splitChars, c) { + input = input[l:] + continue + } + + var word string + word, input, err = splitWord(input, &buf) + if err != nil { + return + } + words = append(words, word) + } + return +} + +func splitWord(input string, buf *bytes.Buffer) (word string, remainder string, err error) { + buf.Reset() + +raw: + { + cur := input + for len(cur) > 0 { + c, l := utf8.DecodeRuneInString(cur) + cur = cur[l:] + if c == singleChar { + buf.WriteString(input[0 : len(input)-len(cur)-l]) + input = cur + goto single + } else if c == doubleChar { + buf.WriteString(input[0 : len(input)-len(cur)-l]) + input = cur + goto double + } else if c == escapeChar { + buf.WriteString(input[0 : len(input)-len(cur)-l]) + input = cur + goto escape + } else if strings.ContainsRune(splitChars, c) { + buf.WriteString(input[0 : len(input)-len(cur)-l]) + return buf.String(), cur, nil + } + } + if len(input) > 0 { + buf.WriteString(input) + input = "" + } + goto done + } + +escape: + { + if len(input) == 0 { + return "", "", UnterminatedEscapeError + } + c, l := utf8.DecodeRuneInString(input) + if c == '\n' { + // a backslash-escaped newline is elided from the output entirely + } else { + buf.WriteString(input[:l]) + } + input = input[l:] + } + goto raw + +single: + { + i := strings.IndexRune(input, singleChar) + if i == -1 { + return "", "", UnterminatedSingleQuoteError + } + buf.WriteString(input[0:i]) + input = input[i+1:] + goto raw + } + +double: + { + cur := input + for len(cur) > 0 { + c, l := utf8.DecodeRuneInString(cur) + cur = cur[l:] + if c == doubleChar { + buf.WriteString(input[0 : len(input)-len(cur)-l]) + input = cur + goto raw + } else if c == escapeChar { + // bash only supports certain escapes in double-quoted strings + c2, l2 := utf8.DecodeRuneInString(cur) + cur = cur[l2:] + if strings.ContainsRune(doubleEscapeChars, c2) { + buf.WriteString(input[0 : len(input)-len(cur)-l-l2]) + if c2 == '\n' { + // newline is special, skip the backslash entirely + } else { + buf.WriteRune(c2) + } + input = cur + } + } + } + return "", "", UnterminatedDoubleQuoteError + } + +done: + return buf.String(), input, nil +} diff --git a/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote_test.go b/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote_test.go new file mode 100644 index 0000000..1444a9f --- /dev/null +++ b/Godeps/_workspace/src/github.com/kballard/go-shellquote/unquote_test.go @@ -0,0 +1,53 @@ +package shellquote + +import ( + "reflect" + "testing" +) + +func TestSimpleSplit(t *testing.T) { + for _, elem := range simpleSplitTest { + output, err := Split(elem.input) + if err != nil { + t.Errorf("Input %q, got error %#v", elem.input, err) + } else if !reflect.DeepEqual(output, elem.output) { + t.Errorf("Input %q, got %q, expected %q", elem.input, output, elem.output) + } + } +} + +func TestErrorSplit(t *testing.T) { + for _, elem := range errorSplitTest { + _, err := Split(elem.input) + if err != elem.error { + t.Errorf("Input %q, got error %#v, expected error %#v", elem.input, err, elem.error) + } + } +} + +var simpleSplitTest = []struct { + input string + output []string +}{ + {"hello", []string{"hello"}}, + {"hello goodbye", []string{"hello", "goodbye"}}, + {"hello goodbye", []string{"hello", "goodbye"}}, + {"glob* test?", []string{"glob*", "test?"}}, + {"don\\'t you know the dewey decimal system\\?", []string{"don't", "you", "know", "the", "dewey", "decimal", "system?"}}, + {"'don'\\''t you know the dewey decimal system?'", []string{"don't you know the dewey decimal system?"}}, + {"one '' two", []string{"one", "", "two"}}, + {"text with\\\na backslash-escaped newline", []string{"text", "witha", "backslash-escaped", "newline"}}, + {"text \"with\na\" quoted newline", []string{"text", "with\na", "quoted", "newline"}}, + {"\"quoted\\d\\\\\\\" text with\\\na backslash-escaped newline\"", []string{"quoted\\d\\\" text witha backslash-escaped newline"}}, + {"foo\"bar\"baz", []string{"foobarbaz"}}, +} + +var errorSplitTest = []struct { + input string + error error +}{ + {"don't worry", UnterminatedSingleQuoteError}, + {"'test'\\''ing", UnterminatedSingleQuoteError}, + {"\"foo'bar", UnterminatedDoubleQuoteError}, + {"foo\\", UnterminatedEscapeError}, +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/.gitignore b/Godeps/_workspace/src/github.com/kr/pty/.gitignore new file mode 100644 index 0000000..1f0a99f --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/.gitignore @@ -0,0 +1,4 @@ +[568].out +_go* +_test* +_obj diff --git a/Godeps/_workspace/src/github.com/kr/pty/License b/Godeps/_workspace/src/github.com/kr/pty/License new file mode 100644 index 0000000..6b7558b --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/License @@ -0,0 +1,23 @@ +Copyright (c) 2011 Keith Rarick + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without limitation +the rights to use, copy, modify, merge, publish, distribute, +sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall +be included in all copies or substantial portions of the +Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY +KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE +WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS +OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/kr/pty/README.md b/Godeps/_workspace/src/github.com/kr/pty/README.md new file mode 100644 index 0000000..7b7900c --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/README.md @@ -0,0 +1,36 @@ +# pty + +Pty is a Go package for using unix pseudo-terminals. + +## Install + + go get github.com/kr/pty + +## Example + +```go +package main + +import ( + "github.com/kr/pty" + "io" + "os" + "os/exec" +) + +func main() { + c := exec.Command("grep", "--color=auto", "bar") + f, err := pty.Start(c) + if err != nil { + panic(err) + } + + go func() { + f.Write([]byte("foo\n")) + f.Write([]byte("bar\n")) + f.Write([]byte("baz\n")) + f.Write([]byte{4}) // EOT + }() + io.Copy(os.Stdout, f) +} +``` diff --git a/Godeps/_workspace/src/github.com/kr/pty/doc.go b/Godeps/_workspace/src/github.com/kr/pty/doc.go new file mode 100644 index 0000000..190cfbe --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/doc.go @@ -0,0 +1,16 @@ +// Package pty provides functions for working with Unix terminals. +package pty + +import ( + "errors" + "os" +) + +// ErrUnsupported is returned if a function is not +// available on the current platform. +var ErrUnsupported = errors.New("unsupported") + +// Opens a pty and its corresponding tty. +func Open() (pty, tty *os.File, err error) { + return open() +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ioctl.go b/Godeps/_workspace/src/github.com/kr/pty/ioctl.go new file mode 100644 index 0000000..5b856e8 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ioctl.go @@ -0,0 +1,11 @@ +package pty + +import "syscall" + +func ioctl(fd, cmd, ptr uintptr) error { + _, _, e := syscall.Syscall(syscall.SYS_IOCTL, fd, cmd, ptr) + if e != 0 { + return e + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ioctl_bsd.go b/Godeps/_workspace/src/github.com/kr/pty/ioctl_bsd.go new file mode 100644 index 0000000..73b12c5 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ioctl_bsd.go @@ -0,0 +1,39 @@ +// +build darwin dragonfly freebsd netbsd openbsd + +package pty + +// from +const ( + _IOC_VOID uintptr = 0x20000000 + _IOC_OUT uintptr = 0x40000000 + _IOC_IN uintptr = 0x80000000 + _IOC_IN_OUT uintptr = _IOC_OUT | _IOC_IN + _IOC_DIRMASK = _IOC_VOID | _IOC_OUT | _IOC_IN + + _IOC_PARAM_SHIFT = 13 + _IOC_PARAM_MASK = (1 << _IOC_PARAM_SHIFT) - 1 +) + +func _IOC_PARM_LEN(ioctl uintptr) uintptr { + return (ioctl >> 16) & _IOC_PARAM_MASK +} + +func _IOC(inout uintptr, group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return inout | (param_len&_IOC_PARAM_MASK)<<16 | uintptr(group)<<8 | ioctl_num +} + +func _IO(group byte, ioctl_num uintptr) uintptr { + return _IOC(_IOC_VOID, group, ioctl_num, 0) +} + +func _IOR(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_OUT, group, ioctl_num, param_len) +} + +func _IOW(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_IN, group, ioctl_num, param_len) +} + +func _IOWR(group byte, ioctl_num uintptr, param_len uintptr) uintptr { + return _IOC(_IOC_IN_OUT, group, ioctl_num, param_len) +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/mktypes.bash b/Godeps/_workspace/src/github.com/kr/pty/mktypes.bash new file mode 100644 index 0000000..9952c88 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/mktypes.bash @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +GOOSARCH="${GOOS}_${GOARCH}" +case "$GOOSARCH" in +_* | *_ | _) + echo 'undefined $GOOS_$GOARCH:' "$GOOSARCH" 1>&2 + exit 1 + ;; +esac + +GODEFS="go tool cgo -godefs" + +$GODEFS types.go |gofmt > ztypes_$GOARCH.go + +case $GOOS in +freebsd) + $GODEFS types_$GOOS.go |gofmt > ztypes_$GOOSARCH.go + ;; +esac diff --git a/Godeps/_workspace/src/github.com/kr/pty/pty_darwin.go b/Godeps/_workspace/src/github.com/kr/pty/pty_darwin.go new file mode 100644 index 0000000..4f4d5ca --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/pty_darwin.go @@ -0,0 +1,60 @@ +package pty + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +func open() (pty, tty *os.File, err error) { + p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + err = grantpt(p) + if err != nil { + return nil, nil, err + } + + err = unlockpt(p) + if err != nil { + return nil, nil, err + } + + t, err := os.OpenFile(sname, os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func ptsname(f *os.File) (string, error) { + n := make([]byte, _IOC_PARM_LEN(syscall.TIOCPTYGNAME)) + + err := ioctl(f.Fd(), syscall.TIOCPTYGNAME, uintptr(unsafe.Pointer(&n[0]))) + if err != nil { + return "", err + } + + for i, c := range n { + if c == 0 { + return string(n[:i]), nil + } + } + return "", errors.New("TIOCPTYGNAME string not NUL-terminated") +} + +func grantpt(f *os.File) error { + return ioctl(f.Fd(), syscall.TIOCPTYGRANT, 0) +} + +func unlockpt(f *os.File) error { + return ioctl(f.Fd(), syscall.TIOCPTYUNLK, 0) +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/pty_freebsd.go b/Godeps/_workspace/src/github.com/kr/pty/pty_freebsd.go new file mode 100644 index 0000000..b341bab --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/pty_freebsd.go @@ -0,0 +1,73 @@ +package pty + +import ( + "errors" + "os" + "syscall" + "unsafe" +) + +func posix_openpt(oflag int) (fd int, err error) { + r0, _, e1 := syscall.Syscall(syscall.SYS_POSIX_OPENPT, uintptr(oflag), 0, 0) + fd = int(r0) + if e1 != 0 { + err = e1 + } + return +} + +func open() (pty, tty *os.File, err error) { + fd, err := posix_openpt(syscall.O_RDWR | syscall.O_CLOEXEC) + if err != nil { + return nil, nil, err + } + + p := os.NewFile(uintptr(fd), "/dev/pts") + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + t, err := os.OpenFile("/dev/"+sname, os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func isptmaster(fd uintptr) (bool, error) { + err := ioctl(fd, syscall.TIOCPTMASTER, 0) + return err == nil, err +} + +var ( + emptyFiodgnameArg fiodgnameArg + ioctl_FIODGNAME = _IOW('f', 120, unsafe.Sizeof(emptyFiodgnameArg)) +) + +func ptsname(f *os.File) (string, error) { + master, err := isptmaster(f.Fd()) + if err != nil { + return "", err + } + if !master { + return "", syscall.EINVAL + } + + const n = _C_SPECNAMELEN + 1 + var ( + buf = make([]byte, n) + arg = fiodgnameArg{Len: n, Buf: (*byte)(unsafe.Pointer(&buf[0]))} + ) + err = ioctl(f.Fd(), ioctl_FIODGNAME, uintptr(unsafe.Pointer(&arg))) + if err != nil { + return "", err + } + + for i, c := range buf { + if c == 0 { + return string(buf[:i]), nil + } + } + return "", errors.New("FIODGNAME string not NUL-terminated") +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/pty_linux.go b/Godeps/_workspace/src/github.com/kr/pty/pty_linux.go new file mode 100644 index 0000000..cb901a2 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/pty_linux.go @@ -0,0 +1,46 @@ +package pty + +import ( + "os" + "strconv" + "syscall" + "unsafe" +) + +func open() (pty, tty *os.File, err error) { + p, err := os.OpenFile("/dev/ptmx", os.O_RDWR, 0) + if err != nil { + return nil, nil, err + } + + sname, err := ptsname(p) + if err != nil { + return nil, nil, err + } + + err = unlockpt(p) + if err != nil { + return nil, nil, err + } + + t, err := os.OpenFile(sname, os.O_RDWR|syscall.O_NOCTTY, 0) + if err != nil { + return nil, nil, err + } + return p, t, nil +} + +func ptsname(f *os.File) (string, error) { + var n _C_uint + err := ioctl(f.Fd(), syscall.TIOCGPTN, uintptr(unsafe.Pointer(&n))) + if err != nil { + return "", err + } + return "/dev/pts/" + strconv.Itoa(int(n)), nil +} + +func unlockpt(f *os.File) error { + var u _C_int + // use TIOCSPTLCK with a zero valued arg to clear the slave pty lock + return ioctl(f.Fd(), syscall.TIOCSPTLCK, uintptr(unsafe.Pointer(&u))) +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/pty_unsupported.go b/Godeps/_workspace/src/github.com/kr/pty/pty_unsupported.go new file mode 100644 index 0000000..898c730 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/pty_unsupported.go @@ -0,0 +1,11 @@ +// +build !linux,!darwin,!freebsd + +package pty + +import ( + "os" +) + +func open() (pty, tty *os.File, err error) { + return nil, nil, ErrUnsupported +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/run.go b/Godeps/_workspace/src/github.com/kr/pty/run.go new file mode 100644 index 0000000..f0678d2 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/run.go @@ -0,0 +1,28 @@ +package pty + +import ( + "os" + "os/exec" + "syscall" +) + +// Start assigns a pseudo-terminal tty os.File to c.Stdin, c.Stdout, +// and c.Stderr, calls c.Start, and returns the File of the tty's +// corresponding pty. +func Start(c *exec.Cmd) (pty *os.File, err error) { + pty, tty, err := Open() + if err != nil { + return nil, err + } + defer tty.Close() + c.Stdout = tty + c.Stdin = tty + c.Stderr = tty + c.SysProcAttr = &syscall.SysProcAttr{Setctty: true, Setsid: true} + err = c.Start() + if err != nil { + pty.Close() + return nil, err + } + return pty, err +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/types.go b/Godeps/_workspace/src/github.com/kr/pty/types.go new file mode 100644 index 0000000..5aecb6b --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/types.go @@ -0,0 +1,10 @@ +// +build ignore + +package pty + +import "C" + +type ( + _C_int C.int + _C_uint C.uint +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/types_freebsd.go b/Godeps/_workspace/src/github.com/kr/pty/types_freebsd.go new file mode 100644 index 0000000..ce3eb95 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/types_freebsd.go @@ -0,0 +1,15 @@ +// +build ignore + +package pty + +/* +#include +#include +*/ +import "C" + +const ( + _C_SPECNAMELEN = C.SPECNAMELEN /* max length of devicename */ +) + +type fiodgnameArg C.struct_fiodgname_arg diff --git a/Godeps/_workspace/src/github.com/kr/pty/util.go b/Godeps/_workspace/src/github.com/kr/pty/util.go new file mode 100644 index 0000000..67c52d0 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/util.go @@ -0,0 +1,35 @@ +package pty + +import ( + "os" + "syscall" + "unsafe" +) + +// Getsize returns the number of rows (lines) and cols (positions +// in each line) in terminal t. +func Getsize(t *os.File) (rows, cols int, err error) { + var ws winsize + err = windowrect(&ws, t.Fd()) + return int(ws.ws_row), int(ws.ws_col), err +} + +type winsize struct { + ws_row uint16 + ws_col uint16 + ws_xpixel uint16 + ws_ypixel uint16 +} + +func windowrect(ws *winsize, fd uintptr) error { + _, _, errno := syscall.Syscall( + syscall.SYS_IOCTL, + fd, + syscall.TIOCGWINSZ, + uintptr(unsafe.Pointer(ws)), + ) + if errno != 0 { + return syscall.Errno(errno) + } + return nil +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_386.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_386.go new file mode 100644 index 0000000..ff0b8fd --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_386.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_amd64.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_amd64.go new file mode 100644 index 0000000..ff0b8fd --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_amd64.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_arm.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_arm.go new file mode 100644 index 0000000..ff0b8fd --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_arm.go @@ -0,0 +1,9 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_386.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_386.go new file mode 100644 index 0000000..d997537 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_386.go @@ -0,0 +1,13 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Buf *byte +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_amd64.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_amd64.go new file mode 100644 index 0000000..5fa102f --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_amd64.go @@ -0,0 +1,14 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Pad_cgo_0 [4]byte + Buf *byte +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_arm.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_arm.go new file mode 100644 index 0000000..d997537 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_freebsd_arm.go @@ -0,0 +1,13 @@ +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types_freebsd.go + +package pty + +const ( + _C_SPECNAMELEN = 0x3f +) + +type fiodgnameArg struct { + Len int32 + Buf *byte +} diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64.go new file mode 100644 index 0000000..4e1af84 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64.go @@ -0,0 +1,11 @@ +// +build ppc64 + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64le.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64le.go new file mode 100644 index 0000000..e6780f4 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_ppc64le.go @@ -0,0 +1,11 @@ +// +build ppc64le + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/Godeps/_workspace/src/github.com/kr/pty/ztypes_s390x.go b/Godeps/_workspace/src/github.com/kr/pty/ztypes_s390x.go new file mode 100644 index 0000000..a7452b6 --- /dev/null +++ b/Godeps/_workspace/src/github.com/kr/pty/ztypes_s390x.go @@ -0,0 +1,11 @@ +// +build s390x + +// Created by cgo -godefs - DO NOT EDIT +// cgo -godefs types.go + +package pty + +type ( + _C_int int32 + _C_uint uint32 +) diff --git a/stage1/dummy.go b/stage1/dummy.go index f59d995..2aa6546 100644 --- a/stage1/dummy.go +++ b/stage1/dummy.go @@ -3,6 +3,7 @@ package dummy // This is a dummy package to ensure that Godep vendors // actool, which is used in building the stage1 ACI import ( + _ "github.com/coreos/rkt/Godeps/_workspace/src/github.com/ThomasRooney/gexpect" _ "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/aci" _ "github.com/coreos/rkt/Godeps/_workspace/src/github.com/appc/spec/actool" )