@@ -47,16 +47,48 @@ pub struct RunProgramResult {
47
47
handle : * ( ) ,
48
48
}
49
49
50
- struct ProgRepr {
51
- pid : pid_t ,
52
- handle : * ( ) ,
53
- in_fd : c_int ,
54
- out_file : * libc:: FILE ,
55
- err_file : * libc:: FILE ,
56
- finished : bool ,
50
+ /// A value representing a child process
51
+ pub struct Program {
52
+ priv pid: pid_t ,
53
+ priv handle : * ( ) ,
54
+ priv in_fd : c_int ,
55
+ priv out_file : * libc:: FILE ,
56
+ priv err_file : * libc:: FILE ,
57
+ priv finished : bool ,
58
+ }
59
+
60
+ impl Drop for Program {
61
+ fn finalize ( & self ) {
62
+ // FIXME #4943: transmute is bad.
63
+ let mut_self: & mut Program = unsafe { cast:: transmute ( self ) } ;
64
+
65
+ mut_self. finish ( ) ;
66
+ mut_self. close_outputs ( ) ;
67
+ free_handle ( self . handle ) ;
68
+ }
57
69
}
58
70
59
- impl ProgRepr {
71
+ pub impl Program {
72
+
73
+ /// Returns the process id of the program
74
+ fn get_id ( & mut self ) -> pid_t { self . pid }
75
+
76
+ /// Returns an io::Writer that can be used to write to stdin
77
+ fn input ( & mut self ) -> @io:: Writer {
78
+ io:: fd_writer ( self . in_fd , false )
79
+ }
80
+
81
+ /// Returns an io::Reader that can be used to read from stdout
82
+ fn output ( & mut self ) -> @io:: Reader {
83
+ io:: FILE_reader ( self . out_file , false )
84
+ }
85
+
86
+ /// Returns an io::Reader that can be used to read from stderr
87
+ fn err ( & mut self ) -> @io:: Reader {
88
+ io:: FILE_reader ( self . err_file , false )
89
+ }
90
+
91
+ /// Closes the handle to the child processes standard input
60
92
fn close_input ( & mut self ) {
61
93
let invalid_fd = -1i32 ;
62
94
if self . in_fd != invalid_fd {
@@ -67,21 +99,25 @@ impl ProgRepr {
67
99
}
68
100
}
69
101
70
- fn close_outputs ( & mut self ) {
102
+ priv fn close_outputs ( & mut self ) {
71
103
unsafe {
72
104
fclose_and_null ( & mut self . out_file ) ;
73
105
fclose_and_null ( & mut self . err_file ) ;
74
106
}
75
107
}
76
108
109
+ /**
110
+ * Waits for the child process to terminate. Closes the handle
111
+ * to stdin if necessary.
112
+ */
77
113
fn finish ( & mut self ) -> int {
78
114
if self . finished { return 0 ; }
79
115
self . finished = true ;
80
116
self . close_input ( ) ;
81
117
return waitpid ( self . pid ) ;
82
118
}
83
119
84
- fn destroy ( & mut self , force : bool ) {
120
+ priv fn destroy_internal ( & mut self , force : bool ) {
85
121
killpid ( self . pid , force) ;
86
122
self . finish ( ) ;
87
123
self . close_outputs ( ) ;
@@ -107,57 +143,6 @@ impl ProgRepr {
107
143
}
108
144
}
109
145
}
110
- }
111
-
112
- /// A value representing a child process
113
- pub struct Program {
114
- priv r: ProgRepr ,
115
- }
116
-
117
- impl Drop for Program {
118
- fn finalize ( & self ) {
119
- // FIXME #4943: transmute is bad.
120
- let selfr: & mut ProgRepr = unsafe { cast:: transmute ( & self . r ) } ;
121
-
122
- selfr. finish ( ) ;
123
- selfr. close_outputs ( ) ;
124
- free_handle ( self . r . handle ) ;
125
- }
126
- }
127
-
128
- pub impl Program {
129
- priv fn new ( r : ProgRepr ) -> Program {
130
- Program {
131
- r : r
132
- }
133
- }
134
-
135
- /// Returns the process id of the program
136
- fn get_id ( & mut self ) -> pid_t { self . r . pid }
137
-
138
- /// Returns an io::Writer that can be used to write to stdin
139
- fn input ( & mut self ) -> @io:: Writer {
140
- io:: fd_writer ( self . r . in_fd , false )
141
- }
142
-
143
- /// Returns an io::Reader that can be used to read from stdout
144
- fn output ( & mut self ) -> @io:: Reader {
145
- io:: FILE_reader ( self . r . out_file , false )
146
- }
147
-
148
- /// Returns an io::Reader that can be used to read from stderr
149
- fn err ( & mut self ) -> @io:: Reader {
150
- io:: FILE_reader ( self . r . err_file , false )
151
- }
152
-
153
- /// Closes the handle to the child processes standard input
154
- fn close_input ( & mut self ) { self . r . close_input ( ) ; }
155
-
156
- /**
157
- * Waits for the child process to terminate. Closes the handle
158
- * to stdin if necessary.
159
- */
160
- fn finish ( & mut self ) -> int { self . r . finish ( ) }
161
146
162
147
/**
163
148
* Terminate the program, giving it a chance to clean itself up if
@@ -166,7 +151,7 @@ pub impl Program {
166
151
* On Posix OSs SIGTERM will be sent to the process. On Win32
167
152
* TerminateProcess(..) will be called.
168
153
*/
169
- fn destroy ( & mut self ) { self . r . destroy ( false ) ; }
154
+ fn destroy ( & mut self ) { self . destroy_internal ( false ) ; }
170
155
171
156
/**
172
157
* Terminate the program as soon as possible without giving it a
@@ -175,7 +160,7 @@ pub impl Program {
175
160
* On Posix OSs SIGKILL will be sent to the process. On Win32
176
161
* TerminateProcess(..) will be called.
177
162
*/
178
- fn force_destroy ( & mut self ) { self . r . destroy ( true ) ; }
163
+ fn force_destroy ( & mut self ) { self . destroy_internal ( true ) ; }
179
164
}
180
165
181
166
@@ -366,16 +351,14 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {
366
351
libc:: close ( pipe_err. out ) ;
367
352
}
368
353
369
- let repr = ProgRepr {
354
+ Program {
370
355
pid : res. pid ,
371
356
handle : res. handle ,
372
357
in_fd : pipe_input. out ,
373
358
out_file : os:: fdopen ( pipe_output. in ) ,
374
359
err_file : os:: fdopen ( pipe_err. in ) ,
375
360
finished : false ,
376
- } ;
377
-
378
- Program :: new ( repr)
361
+ }
379
362
}
380
363
381
364
fn read_all ( rd : @io:: Reader ) -> ~str {
0 commit comments