Add gexpect

It will be needed in functional tests
This commit is contained in:
Krzesimir Nowak
2015-04-13 15:23:48 +02:00
committed by Alban Crequy
parent 860369f8d1
commit 754a3409bc
40 changed files with 1488 additions and 0 deletions
+13
View File
@@ -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"
+7
View File
@@ -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.
+53
View File
@@ -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/"
@@ -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()
}
@@ -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")
}
+277
View File
@@ -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
}
+122
View File
@@ -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")
}
+19
View File
@@ -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.
+36
View File
@@ -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.
+29
View File
@@ -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)
}
}
+3
View File
@@ -0,0 +1,3 @@
// Shellquote provides utilities for joining/splitting strings using sh's
// word-splitting rules.
package shellquote
+102
View File
@@ -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('\'')
}
}
+28
View File
@@ -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"},
}
+144
View File
@@ -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
}
@@ -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},
}
+4
View File
@@ -0,0 +1,4 @@
[568].out
_go*
_test*
_obj
+23
View File
@@ -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.
+36
View File
@@ -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)
}
```
+16
View File
@@ -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()
}
+11
View File
@@ -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
}
+39
View File
@@ -0,0 +1,39 @@
// +build darwin dragonfly freebsd netbsd openbsd
package pty
// from <sys/ioccom.h>
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)
}
+19
View File
@@ -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
+60
View File
@@ -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)
}
+73
View File
@@ -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")
}
+46
View File
@@ -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)))
}
+11
View File
@@ -0,0 +1,11 @@
// +build !linux,!darwin,!freebsd
package pty
import (
"os"
)
func open() (pty, tty *os.File, err error) {
return nil, nil, ErrUnsupported
}
+28
View File
@@ -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
}
+10
View File
@@ -0,0 +1,10 @@
// +build ignore
package pty
import "C"
type (
_C_int C.int
_C_uint C.uint
)
+15
View File
@@ -0,0 +1,15 @@
// +build ignore
package pty
/*
#include <sys/param.h>
#include <sys/filio.h>
*/
import "C"
const (
_C_SPECNAMELEN = C.SPECNAMELEN /* max length of devicename */
)
type fiodgnameArg C.struct_fiodgname_arg
+35
View File
@@ -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
}
+9
View File
@@ -0,0 +1,9 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types.go
package pty
type (
_C_int int32
_C_uint uint32
)
+9
View File
@@ -0,0 +1,9 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types.go
package pty
type (
_C_int int32
_C_uint uint32
)
+9
View File
@@ -0,0 +1,9 @@
// Created by cgo -godefs - DO NOT EDIT
// cgo -godefs types.go
package pty
type (
_C_int int32
_C_uint uint32
)
+13
View File
@@ -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
}
+14
View File
@@ -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
}
+13
View File
@@ -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
}
+11
View File
@@ -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
)
+11
View File
@@ -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
)
+11
View File
@@ -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
)
+1
View File
@@ -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"
)