ba9bd2472a4b17e13951596c2032a802c407c084
[platform/upstream/gcc.git] / libgo / go / exec / exec.go
1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // The exec package runs external commands.
6 package exec
7
8 import (
9         "os"
10 )
11
12 // Arguments to Run.
13 const (
14         DevNull = iota
15         PassThrough
16         Pipe
17         MergeWithStdout
18 )
19
20 // A Cmd represents a running command.
21 // Stdin, Stdout, and Stderr are Files representing pipes
22 // connected to the running command's standard input, output, and error,
23 // or else nil, depending on the arguments to Run.
24 // Pid is the running command's operating system process ID.
25 type Cmd struct {
26         Stdin  *os.File
27         Stdout *os.File
28         Stderr *os.File
29         Pid    int
30 }
31
32 // Given mode (DevNull, etc), return file for child
33 // and file to record in Cmd structure.
34 func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
35         switch mode {
36         case DevNull:
37                 rw := os.O_WRONLY
38                 if fd == 0 {
39                         rw = os.O_RDONLY
40                 }
41                 f, err := os.Open(os.DevNull, rw, 0)
42                 return f, nil, err
43         case PassThrough:
44                 switch fd {
45                 case 0:
46                         return os.Stdin, nil, nil
47                 case 1:
48                         return os.Stdout, nil, nil
49                 case 2:
50                         return os.Stderr, nil, nil
51                 }
52         case Pipe:
53                 r, w, err := os.Pipe()
54                 if err != nil {
55                         return nil, nil, err
56                 }
57                 if fd == 0 {
58                         return r, w, nil
59                 }
60                 return w, r, nil
61         }
62         return nil, nil, os.EINVAL
63 }
64
65 // Run starts the named binary running with
66 // arguments argv and environment envv.
67 // It returns a pointer to a new Cmd representing
68 // the command or an error.
69 //
70 // The parameters stdin, stdout, and stderr
71 // specify how to handle standard input, output, and error.
72 // The choices are DevNull (connect to /dev/null),
73 // PassThrough (connect to the current process's standard stream),
74 // Pipe (connect to an operating system pipe), and
75 // MergeWithStdout (only for standard error; use the same
76 // file descriptor as was used for standard output).
77 // If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
78 // of the returned Cmd is the other end of the pipe.
79 // Otherwise the field in Cmd is nil.
80 func Run(name string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
81         p = new(Cmd)
82         var fd [3]*os.File
83
84         if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
85                 goto Error
86         }
87         if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
88                 goto Error
89         }
90         if stderr == MergeWithStdout {
91                 fd[2] = fd[1]
92         } else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
93                 goto Error
94         }
95
96         // Run command.
97         p.Pid, err = os.ForkExec(name, argv, envv, dir, fd[0:])
98         if err != nil {
99                 goto Error
100         }
101         if fd[0] != os.Stdin {
102                 fd[0].Close()
103         }
104         if fd[1] != os.Stdout {
105                 fd[1].Close()
106         }
107         if fd[2] != os.Stderr && fd[2] != fd[1] {
108                 fd[2].Close()
109         }
110         return p, nil
111
112 Error:
113         if fd[0] != os.Stdin && fd[0] != nil {
114                 fd[0].Close()
115         }
116         if fd[1] != os.Stdout && fd[1] != nil {
117                 fd[1].Close()
118         }
119         if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
120                 fd[2].Close()
121         }
122         if p.Stdin != nil {
123                 p.Stdin.Close()
124         }
125         if p.Stdout != nil {
126                 p.Stdout.Close()
127         }
128         if p.Stderr != nil {
129                 p.Stderr.Close()
130         }
131         return nil, err
132 }
133
134 // Wait waits for the running command p,
135 // returning the Waitmsg returned by os.Wait and an error.
136 // The options are passed through to os.Wait.
137 // Setting options to 0 waits for p to exit;
138 // other options cause Wait to return for other
139 // process events; see package os for details.
140 func (p *Cmd) Wait(options int) (*os.Waitmsg, os.Error) {
141         if p.Pid <= 0 {
142                 return nil, os.ErrorString("exec: invalid use of Cmd.Wait")
143         }
144         w, err := os.Wait(p.Pid, options)
145         if w != nil && (w.Exited() || w.Signaled()) {
146                 p.Pid = -1
147         }
148         return w, err
149 }
150
151 // Close waits for the running command p to exit,
152 // if it hasn't already, and then closes the non-nil file descriptors
153 // p.Stdin, p.Stdout, and p.Stderr.
154 func (p *Cmd) Close() os.Error {
155         if p.Pid > 0 {
156                 // Loop on interrupt, but
157                 // ignore other errors -- maybe
158                 // caller has already waited for pid.
159                 _, err := p.Wait(0)
160                 for err == os.EINTR {
161                         _, err = p.Wait(0)
162                 }
163         }
164
165         // Close the FDs that are still open.
166         var err os.Error
167         if p.Stdin != nil && p.Stdin.Fd() >= 0 {
168                 if err1 := p.Stdin.Close(); err1 != nil {
169                         err = err1
170                 }
171         }
172         if p.Stdout != nil && p.Stdout.Fd() >= 0 {
173                 if err1 := p.Stdout.Close(); err1 != nil && err != nil {
174                         err = err1
175                 }
176         }
177         if p.Stderr != nil && p.Stderr != p.Stdout && p.Stderr.Fd() >= 0 {
178                 if err1 := p.Stderr.Close(); err1 != nil && err != nil {
179                         err = err1
180                 }
181         }
182         return err
183 }