summaryrefslogtreecommitdiff
path: root/libgo/go/exec
diff options
context:
space:
mode:
authorupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
committerupstream source tree <ports@midipix.org>2015-03-15 20:14:05 -0400
commit554fd8c5195424bdbcabf5de30fdc183aba391bd (patch)
tree976dc5ab7fddf506dadce60ae936f43f58787092 /libgo/go/exec
downloadcbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.bz2
cbb-gcc-4.6.4-554fd8c5195424bdbcabf5de30fdc183aba391bd.tar.xz
obtained gcc-4.6.4.tar.bz2 from upstream website;upstream
verified gcc-4.6.4.tar.bz2.sig; imported gcc-4.6.4 source tree from verified upstream tarball. downloading a git-generated archive based on the 'upstream' tag should provide you with a source tree that is binary identical to the one extracted from the above tarball. if you have obtained the source via the command 'git clone', however, do note that line-endings of files in your working directory might differ from line-endings of the respective files in the upstream repository.
Diffstat (limited to 'libgo/go/exec')
-rw-r--r--libgo/go/exec/exec.go183
-rw-r--r--libgo/go/exec/exec_test.go120
-rw-r--r--libgo/go/exec/lp_unix.go45
-rw-r--r--libgo/go/exec/lp_windows.go66
4 files changed, 414 insertions, 0 deletions
diff --git a/libgo/go/exec/exec.go b/libgo/go/exec/exec.go
new file mode 100644
index 000000000..ba9bd2472
--- /dev/null
+++ b/libgo/go/exec/exec.go
@@ -0,0 +1,183 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The exec package runs external commands.
+package exec
+
+import (
+ "os"
+)
+
+// Arguments to Run.
+const (
+ DevNull = iota
+ PassThrough
+ Pipe
+ MergeWithStdout
+)
+
+// A Cmd represents a running command.
+// Stdin, Stdout, and Stderr are Files representing pipes
+// connected to the running command's standard input, output, and error,
+// or else nil, depending on the arguments to Run.
+// Pid is the running command's operating system process ID.
+type Cmd struct {
+ Stdin *os.File
+ Stdout *os.File
+ Stderr *os.File
+ Pid int
+}
+
+// Given mode (DevNull, etc), return file for child
+// and file to record in Cmd structure.
+func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
+ switch mode {
+ case DevNull:
+ rw := os.O_WRONLY
+ if fd == 0 {
+ rw = os.O_RDONLY
+ }
+ f, err := os.Open(os.DevNull, rw, 0)
+ return f, nil, err
+ case PassThrough:
+ switch fd {
+ case 0:
+ return os.Stdin, nil, nil
+ case 1:
+ return os.Stdout, nil, nil
+ case 2:
+ return os.Stderr, nil, nil
+ }
+ case Pipe:
+ r, w, err := os.Pipe()
+ if err != nil {
+ return nil, nil, err
+ }
+ if fd == 0 {
+ return r, w, nil
+ }
+ return w, r, nil
+ }
+ return nil, nil, os.EINVAL
+}
+
+// Run starts the named binary running with
+// arguments argv and environment envv.
+// It returns a pointer to a new Cmd representing
+// the command or an error.
+//
+// The parameters stdin, stdout, and stderr
+// specify how to handle standard input, output, and error.
+// The choices are DevNull (connect to /dev/null),
+// PassThrough (connect to the current process's standard stream),
+// Pipe (connect to an operating system pipe), and
+// MergeWithStdout (only for standard error; use the same
+// file descriptor as was used for standard output).
+// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
+// of the returned Cmd is the other end of the pipe.
+// Otherwise the field in Cmd is nil.
+func Run(name string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
+ p = new(Cmd)
+ var fd [3]*os.File
+
+ if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
+ goto Error
+ }
+ if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
+ goto Error
+ }
+ if stderr == MergeWithStdout {
+ fd[2] = fd[1]
+ } else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
+ goto Error
+ }
+
+ // Run command.
+ p.Pid, err = os.ForkExec(name, argv, envv, dir, fd[0:])
+ if err != nil {
+ goto Error
+ }
+ if fd[0] != os.Stdin {
+ fd[0].Close()
+ }
+ if fd[1] != os.Stdout {
+ fd[1].Close()
+ }
+ if fd[2] != os.Stderr && fd[2] != fd[1] {
+ fd[2].Close()
+ }
+ return p, nil
+
+Error:
+ if fd[0] != os.Stdin && fd[0] != nil {
+ fd[0].Close()
+ }
+ if fd[1] != os.Stdout && fd[1] != nil {
+ fd[1].Close()
+ }
+ if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
+ fd[2].Close()
+ }
+ if p.Stdin != nil {
+ p.Stdin.Close()
+ }
+ if p.Stdout != nil {
+ p.Stdout.Close()
+ }
+ if p.Stderr != nil {
+ p.Stderr.Close()
+ }
+ return nil, err
+}
+
+// Wait waits for the running command p,
+// returning the Waitmsg returned by os.Wait and an error.
+// The options are passed through to os.Wait.
+// Setting options to 0 waits for p to exit;
+// other options cause Wait to return for other
+// process events; see package os for details.
+func (p *Cmd) Wait(options int) (*os.Waitmsg, os.Error) {
+ if p.Pid <= 0 {
+ return nil, os.ErrorString("exec: invalid use of Cmd.Wait")
+ }
+ w, err := os.Wait(p.Pid, options)
+ if w != nil && (w.Exited() || w.Signaled()) {
+ p.Pid = -1
+ }
+ return w, err
+}
+
+// Close waits for the running command p to exit,
+// if it hasn't already, and then closes the non-nil file descriptors
+// p.Stdin, p.Stdout, and p.Stderr.
+func (p *Cmd) Close() os.Error {
+ if p.Pid > 0 {
+ // Loop on interrupt, but
+ // ignore other errors -- maybe
+ // caller has already waited for pid.
+ _, err := p.Wait(0)
+ for err == os.EINTR {
+ _, err = p.Wait(0)
+ }
+ }
+
+ // Close the FDs that are still open.
+ var err os.Error
+ if p.Stdin != nil && p.Stdin.Fd() >= 0 {
+ if err1 := p.Stdin.Close(); err1 != nil {
+ err = err1
+ }
+ }
+ if p.Stdout != nil && p.Stdout.Fd() >= 0 {
+ if err1 := p.Stdout.Close(); err1 != nil && err != nil {
+ err = err1
+ }
+ }
+ if p.Stderr != nil && p.Stderr != p.Stdout && p.Stderr.Fd() >= 0 {
+ if err1 := p.Stderr.Close(); err1 != nil && err != nil {
+ err = err1
+ }
+ }
+ return err
+}
diff --git a/libgo/go/exec/exec_test.go b/libgo/go/exec/exec_test.go
new file mode 100644
index 000000000..3a3d3b1a5
--- /dev/null
+++ b/libgo/go/exec/exec_test.go
@@ -0,0 +1,120 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package exec
+
+import (
+ "io"
+ "io/ioutil"
+ "testing"
+ "os"
+ "runtime"
+)
+
+func run(argv []string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
+ if runtime.GOOS == "windows" {
+ argv = append([]string{"cmd", "/c"}, argv...)
+ }
+ exe, err := LookPath(argv[0])
+ if err != nil {
+ return nil, err
+ }
+ p, err = Run(exe, argv, nil, "", stdin, stdout, stderr)
+ return p, err
+}
+
+func TestRunCat(t *testing.T) {
+ cmd, err := run([]string{"cat"}, Pipe, Pipe, DevNull)
+ if err != nil {
+ t.Fatal("run:", err)
+ }
+ io.WriteString(cmd.Stdin, "hello, world\n")
+ cmd.Stdin.Close()
+ buf, err := ioutil.ReadAll(cmd.Stdout)
+ if err != nil {
+ t.Fatal("read:", err)
+ }
+ if string(buf) != "hello, world\n" {
+ t.Fatalf("read: got %q", buf)
+ }
+ if err = cmd.Close(); err != nil {
+ t.Fatal("close:", err)
+ }
+}
+
+func TestRunEcho(t *testing.T) {
+ cmd, err := run([]string{"sh", "-c", "echo hello world"},
+ DevNull, Pipe, DevNull)
+ if err != nil {
+ t.Fatal("run:", err)
+ }
+ buf, err := ioutil.ReadAll(cmd.Stdout)
+ if err != nil {
+ t.Fatal("read:", err)
+ }
+ if string(buf) != "hello world\n" {
+ t.Fatalf("read: got %q", buf)
+ }
+ if err = cmd.Close(); err != nil {
+ t.Fatal("close:", err)
+ }
+}
+
+func TestStderr(t *testing.T) {
+ cmd, err := run([]string{"sh", "-c", "echo hello world 1>&2"},
+ DevNull, DevNull, Pipe)
+ if err != nil {
+ t.Fatal("run:", err)
+ }
+ buf, err := ioutil.ReadAll(cmd.Stderr)
+ if err != nil {
+ t.Fatal("read:", err)
+ }
+ if string(buf) != "hello world\n" {
+ t.Fatalf("read: got %q", buf)
+ }
+ if err = cmd.Close(); err != nil {
+ t.Fatal("close:", err)
+ }
+}
+
+func TestMergeWithStdout(t *testing.T) {
+ cmd, err := run([]string{"sh", "-c", "echo hello world 1>&2"},
+ DevNull, Pipe, MergeWithStdout)
+ if err != nil {
+ t.Fatal("run:", err)
+ }
+ buf, err := ioutil.ReadAll(cmd.Stdout)
+ if err != nil {
+ t.Fatal("read:", err)
+ }
+ if string(buf) != "hello world\n" {
+ t.Fatalf("read: got %q", buf)
+ }
+ if err = cmd.Close(); err != nil {
+ t.Fatal("close:", err)
+ }
+}
+
+func TestAddEnvVar(t *testing.T) {
+ err := os.Setenv("NEWVAR", "hello world")
+ if err != nil {
+ t.Fatal("setenv:", err)
+ }
+ cmd, err := run([]string{"sh", "-c", "echo $NEWVAR"},
+ DevNull, Pipe, DevNull)
+ if err != nil {
+ t.Fatal("run:", err)
+ }
+ buf, err := ioutil.ReadAll(cmd.Stdout)
+ if err != nil {
+ t.Fatal("read:", err)
+ }
+ if string(buf) != "hello world\n" {
+ t.Fatalf("read: got %q", buf)
+ }
+ if err = cmd.Close(); err != nil {
+ t.Fatal("close:", err)
+ }
+}
diff --git a/libgo/go/exec/lp_unix.go b/libgo/go/exec/lp_unix.go
new file mode 100644
index 000000000..292e24fcc
--- /dev/null
+++ b/libgo/go/exec/lp_unix.go
@@ -0,0 +1,45 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package exec
+
+import (
+ "os"
+ "strings"
+)
+
+func canExec(file string) bool {
+ d, err := os.Stat(file)
+ if err != nil {
+ return false
+ }
+ return d.IsRegular() && d.Permission()&0111 != 0
+}
+
+// LookPath searches for an executable binary named file
+// in the directories named by the PATH environment variable.
+// If file contains a slash, it is tried directly and the PATH is not consulted.
+func LookPath(file string) (string, os.Error) {
+ // NOTE(rsc): I wish we could use the Plan 9 behavior here
+ // (only bypass the path if file begins with / or ./ or ../)
+ // but that would not match all the Unix shells.
+
+ if strings.Contains(file, "/") {
+ if canExec(file) {
+ return file, nil
+ }
+ return "", &os.PathError{"lookpath", file, os.ENOENT}
+ }
+ pathenv := os.Getenv("PATH")
+ for _, dir := range strings.Split(pathenv, ":", -1) {
+ if dir == "" {
+ // Unix shell semantics: path element "" means "."
+ dir = "."
+ }
+ if canExec(dir + "/" + file) {
+ return dir + "/" + file, nil
+ }
+ }
+ return "", &os.PathError{"lookpath", file, os.ENOENT}
+}
diff --git a/libgo/go/exec/lp_windows.go b/libgo/go/exec/lp_windows.go
new file mode 100644
index 000000000..7b56afa85
--- /dev/null
+++ b/libgo/go/exec/lp_windows.go
@@ -0,0 +1,66 @@
+// Copyright 2010 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package exec
+
+import (
+ "os"
+ "strings"
+)
+
+func chkStat(file string) bool {
+ d, err := os.Stat(file)
+ if err != nil {
+ return false
+ }
+ return d.IsRegular()
+}
+
+func canExec(file string, exts []string) (string, bool) {
+ if len(exts) == 0 {
+ return file, chkStat(file)
+ }
+ f := strings.ToLower(file)
+ for _, e := range exts {
+ if strings.HasSuffix(f, e) {
+ return file, chkStat(file)
+ }
+ }
+ for _, e := range exts {
+ if f := file + e; chkStat(f) {
+ return f, true
+ }
+ }
+ return ``, false
+}
+
+func LookPath(file string) (string, os.Error) {
+ exts := []string{}
+ if x := os.Getenv(`PATHEXT`); x != `` {
+ exts = strings.Split(strings.ToLower(x), `;`, -1)
+ for i, e := range exts {
+ if e == `` || e[0] != '.' {
+ exts[i] = `.` + e
+ }
+ }
+ }
+ if strings.Contains(file, `\`) || strings.Contains(file, `/`) {
+ if f, ok := canExec(file, exts); ok {
+ return f, nil
+ }
+ return ``, &os.PathError{"lookpath", file, os.ENOENT}
+ }
+ if pathenv := os.Getenv(`PATH`); pathenv == `` {
+ if f, ok := canExec(`.\`+file, exts); ok {
+ return f, nil
+ }
+ } else {
+ for _, dir := range strings.Split(pathenv, `;`, -1) {
+ if f, ok := canExec(dir+`\`+file, exts); ok {
+ return f, nil
+ }
+ }
+ }
+ return ``, &os.PathError{"lookpath", file, os.ENOENT}
+}