1
1
#include " node.h"
2
- #include " process .h"
2
+ #include " child_process .h"
3
3
4
4
#include < assert.h>
5
5
#include < stdlib.h>
@@ -13,65 +13,65 @@ using namespace node;
13
13
14
14
#define PID_SYMBOL String::NewSymbol (" pid" )
15
15
16
- Persistent<FunctionTemplate> Process ::constructor_template;
16
+ Persistent<FunctionTemplate> ChildProcess ::constructor_template;
17
17
18
18
void
19
- Process ::Initialize (Handle<Object> target)
19
+ ChildProcess ::Initialize (Handle<Object> target)
20
20
{
21
21
HandleScope scope;
22
22
23
- Local<FunctionTemplate> t = FunctionTemplate::New (Process ::New);
23
+ Local<FunctionTemplate> t = FunctionTemplate::New (ChildProcess ::New);
24
24
constructor_template = Persistent<FunctionTemplate>::New (t);
25
25
constructor_template->Inherit (EventEmitter::constructor_template);
26
26
constructor_template->InstanceTemplate ()->SetInternalFieldCount (1 );
27
27
28
- NODE_SET_PROTOTYPE_METHOD (constructor_template, " spawn" , Process ::Spawn);
29
- NODE_SET_PROTOTYPE_METHOD (constructor_template, " write" , Process ::Write);
30
- NODE_SET_PROTOTYPE_METHOD (constructor_template, " close" , Process ::Close);
31
- NODE_SET_PROTOTYPE_METHOD (constructor_template, " kill" , Process ::Kill);
28
+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " spawn" , ChildProcess ::Spawn);
29
+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " write" , ChildProcess ::Write);
30
+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " close" , ChildProcess ::Close);
31
+ NODE_SET_PROTOTYPE_METHOD (constructor_template, " kill" , ChildProcess ::Kill);
32
32
33
- target->Set (String::NewSymbol (" Process " ), constructor_template->GetFunction ());
33
+ target->Set (String::NewSymbol (" ChildProcess " ), constructor_template->GetFunction ());
34
34
}
35
35
36
36
Handle<Value>
37
- Process ::New (const Arguments& args)
37
+ ChildProcess ::New (const Arguments& args)
38
38
{
39
39
HandleScope scope;
40
40
41
- Process *p = new Process ();
41
+ ChildProcess *p = new ChildProcess ();
42
42
p->Wrap (args.Holder ());
43
43
44
44
return args.This ();
45
45
}
46
46
47
47
Handle<Value>
48
- Process ::Spawn (const Arguments& args)
48
+ ChildProcess ::Spawn (const Arguments& args)
49
49
{
50
50
if (args.Length () == 0 || !args[0 ]->IsString ()) {
51
51
return ThrowException (String::New (" Bad argument." ));
52
52
}
53
53
54
54
HandleScope scope;
55
- Process *process = ObjectWrap::Unwrap<Process >(args.Holder ());
55
+ ChildProcess *child = ObjectWrap::Unwrap<ChildProcess >(args.Holder ());
56
56
57
57
String::Utf8Value command (args[0 ]->ToString ());
58
58
59
- int r = process ->Spawn (*command);
59
+ int r = child ->Spawn (*command);
60
60
if (r != 0 ) {
61
61
return ThrowException (String::New (" Error spawning" ));
62
62
}
63
63
64
- process ->handle_ ->Set (PID_SYMBOL, Integer::New (process ->pid_ ));
64
+ child ->handle_ ->Set (PID_SYMBOL, Integer::New (child ->pid_ ));
65
65
66
66
return Undefined ();
67
67
}
68
68
69
69
Handle<Value>
70
- Process ::Write (const Arguments& args)
70
+ ChildProcess ::Write (const Arguments& args)
71
71
{
72
72
HandleScope scope;
73
- Process *process = ObjectWrap::Unwrap<Process >(args.Holder ());
74
- assert (process );
73
+ ChildProcess *child = ObjectWrap::Unwrap<ChildProcess >(args.Holder ());
74
+ assert (child );
75
75
76
76
ssize_t len;
77
77
@@ -109,69 +109,69 @@ Process::Write (const Arguments& args)
109
109
}
110
110
}
111
111
112
- return process ->Write (buf, len) == 0 ? True () : False ();
112
+ return child ->Write (buf, len) == 0 ? True () : False ();
113
113
}
114
114
115
115
Handle<Value>
116
- Process ::Kill (const Arguments& args)
116
+ ChildProcess ::Kill (const Arguments& args)
117
117
{
118
118
HandleScope scope;
119
- Process *process = ObjectWrap::Unwrap<Process >(args.Holder ());
120
- assert (process );
119
+ ChildProcess *child = ObjectWrap::Unwrap<ChildProcess >(args.Holder ());
120
+ assert (child );
121
121
122
122
int sig = SIGTERM;
123
123
if (args[0 ]->IsInt32 ()) sig = args[0 ]->Int32Value ();
124
124
125
- if (process ->Kill (sig) != 0 ) {
126
- return ThrowException (String::New (" Process already dead" ));
125
+ if (child ->Kill (sig) != 0 ) {
126
+ return ThrowException (String::New (" ChildProcess already dead" ));
127
127
}
128
128
129
129
return Undefined ();
130
130
}
131
131
132
132
Handle<Value>
133
- Process ::Close (const Arguments& args)
133
+ ChildProcess ::Close (const Arguments& args)
134
134
{
135
135
HandleScope scope;
136
- Process *process = ObjectWrap::Unwrap<Process >(args.Holder ());
137
- assert (process );
138
- return process ->Close () == 0 ? True () : False ();
136
+ ChildProcess *child = ObjectWrap::Unwrap<ChildProcess >(args.Holder ());
137
+ assert (child );
138
+ return child ->Close () == 0 ? True () : False ();
139
139
}
140
140
141
141
void
142
- Process ::reader_closed (evcom_reader *r)
142
+ ChildProcess ::reader_closed (evcom_reader *r)
143
143
{
144
- Process *process = static_cast <Process *> (r->data );
145
- if (r == &process ->stdout_reader_ ) {
146
- process ->stdout_fd_ = -1 ;
144
+ ChildProcess *child = static_cast <ChildProcess *> (r->data );
145
+ if (r == &child ->stdout_reader_ ) {
146
+ child ->stdout_fd_ = -1 ;
147
147
} else {
148
- assert (r == &process ->stderr_reader_ );
149
- process ->stderr_fd_ = -1 ;
148
+ assert (r == &child ->stderr_reader_ );
149
+ child ->stderr_fd_ = -1 ;
150
150
}
151
151
evcom_reader_detach (r);
152
- process ->MaybeShutdown ();
152
+ child ->MaybeShutdown ();
153
153
}
154
154
155
155
void
156
- Process ::stdin_closed (evcom_writer *w)
156
+ ChildProcess ::stdin_closed (evcom_writer *w)
157
157
{
158
- Process *process = static_cast <Process *> (w->data );
159
- assert (w == &process ->stdin_writer_ );
160
- process ->stdin_fd_ = -1 ;
158
+ ChildProcess *child = static_cast <ChildProcess *> (w->data );
159
+ assert (w == &child ->stdin_writer_ );
160
+ child ->stdin_fd_ = -1 ;
161
161
evcom_writer_detach (w);
162
- process ->MaybeShutdown ();
162
+ child ->MaybeShutdown ();
163
163
}
164
164
165
165
void
166
- Process ::on_read (evcom_reader *r, const void *buf, size_t len)
166
+ ChildProcess ::on_read (evcom_reader *r, const void *buf, size_t len)
167
167
{
168
- Process *process = static_cast <Process *> (r->data );
168
+ ChildProcess *child = static_cast <ChildProcess *> (r->data );
169
169
HandleScope scope;
170
170
171
- bool isSTDOUT = (r == &process ->stdout_reader_ );
171
+ bool isSTDOUT = (r == &child ->stdout_reader_ );
172
172
Local<Value> argv[1 ];
173
173
174
- enum encoding encoding = isSTDOUT ? process ->stdout_encoding_ : process ->stderr_encoding_ ;
174
+ enum encoding encoding = isSTDOUT ? child ->stdout_encoding_ : child ->stderr_encoding_ ;
175
175
176
176
if (len == 0 ) {
177
177
argv[0 ] = Local<Value>::New (Null ());
@@ -190,11 +190,11 @@ Process::on_read (evcom_reader *r, const void *buf, size_t len)
190
190
argv[0 ] = String::New ((const char *)buf, len);
191
191
}
192
192
193
- process ->Emit (isSTDOUT ? " output" : " error" , 1 , argv);
194
- process ->MaybeShutdown ();
193
+ child ->Emit (isSTDOUT ? " output" : " error" , 1 , argv);
194
+ child ->MaybeShutdown ();
195
195
}
196
196
197
- Process::Process ()
197
+ ChildProcess::ChildProcess ()
198
198
: EventEmitter()
199
199
{
200
200
evcom_reader_init (&stdout_reader_);
@@ -211,7 +211,7 @@ Process::Process ()
211
211
stdin_writer_.data = this ;
212
212
stdin_writer_.on_close = stdin_closed;
213
213
214
- ev_init (&child_watcher_, Process ::OnCHLD);
214
+ ev_init (&child_watcher_, ChildProcess ::OnCHLD);
215
215
child_watcher_.data = this ;
216
216
217
217
stdout_fd_ = -1 ;
@@ -227,13 +227,13 @@ Process::Process ()
227
227
pid_ = 0 ;
228
228
}
229
229
230
- Process ::~Process ()
230
+ ChildProcess ::~ChildProcess ()
231
231
{
232
232
Shutdown ();
233
233
}
234
234
235
235
void
236
- Process ::Shutdown ()
236
+ ChildProcess ::Shutdown ()
237
237
{
238
238
if (stdin_fd_ >= 0 ) {
239
239
evcom_writer_close (&stdin_writer_);
@@ -269,7 +269,7 @@ SetNonBlocking (int fd)
269
269
}
270
270
271
271
int
272
- Process ::Spawn (const char *command)
272
+ ChildProcess ::Spawn (const char *command)
273
273
{
274
274
assert (pid_ == 0 );
275
275
assert (stdout_fd_ == -1 );
@@ -345,48 +345,48 @@ Process::Spawn (const char *command)
345
345
}
346
346
347
347
void
348
- Process ::OnCHLD (EV_P_ ev_child *watcher, int revents)
348
+ ChildProcess ::OnCHLD (EV_P_ ev_child *watcher, int revents)
349
349
{
350
350
ev_child_stop (EV_A_ watcher);
351
- Process *process = static_cast <Process *>(watcher->data );
351
+ ChildProcess *child = static_cast <ChildProcess *>(watcher->data );
352
352
353
353
assert (revents == EV_CHILD);
354
- assert (process ->pid_ == watcher->rpid );
355
- assert (&process ->child_watcher_ == watcher);
354
+ assert (child ->pid_ == watcher->rpid );
355
+ assert (&child ->child_watcher_ == watcher);
356
356
357
- process ->got_chld_ = true ;
358
- process ->exit_code_ = watcher->rstatus ;
357
+ child ->got_chld_ = true ;
358
+ child ->exit_code_ = watcher->rstatus ;
359
359
360
- if (process ->stdin_fd_ >= 0 ) evcom_writer_close (&process ->stdin_writer_ );
360
+ if (child ->stdin_fd_ >= 0 ) evcom_writer_close (&child ->stdin_writer_ );
361
361
362
- process ->MaybeShutdown ();
362
+ child ->MaybeShutdown ();
363
363
}
364
364
365
365
int
366
- Process ::Write (const char *str, size_t len)
366
+ ChildProcess ::Write (const char *str, size_t len)
367
367
{
368
368
if (stdin_fd_ < 0 || got_chld_) return -1 ;
369
369
evcom_writer_write (&stdin_writer_, str, len);
370
370
return 0 ;
371
371
}
372
372
373
373
int
374
- Process ::Close (void )
374
+ ChildProcess ::Close (void )
375
375
{
376
376
if (stdin_fd_ < 0 || got_chld_) return -1 ;
377
377
evcom_writer_close (EV_DEFAULT_UC_ &stdin_writer_);
378
378
return 0 ;
379
379
}
380
380
381
381
int
382
- Process ::Kill (int sig)
382
+ ChildProcess ::Kill (int sig)
383
383
{
384
384
if (got_chld_ || pid_ == 0 ) return -1 ;
385
385
return kill (pid_, sig);
386
386
}
387
387
388
388
void
389
- Process ::MaybeShutdown (void )
389
+ ChildProcess ::MaybeShutdown (void )
390
390
{
391
391
if (stdout_fd_ < 0 && stderr_fd_ < 0 && got_chld_) {
392
392
HandleScope scope;
0 commit comments