Skip to content

Commit

Permalink
monster commit, cleaning up whitespace (2xSP indentation, no trailing…
Browse files Browse the repository at this point in the history
… space)
  • Loading branch information
rsms committed Dec 31, 2010
1 parent ba0d15a commit ca145dc
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 89 deletions.
20 changes: 10 additions & 10 deletions HDProcess.h
Expand Up @@ -12,7 +12,7 @@ typedef void (^HDProcessBlock)(HDProcess *process);
* Subprocess facility based on Grand Central Dispatch.
*
* Events emitted:
*
*
* - "exit" (HDProcess *self) -- the process exited
* - "start" (HDProcess *self) -- the process started
*
Expand All @@ -23,14 +23,14 @@ typedef void (^HDProcessBlock)(HDProcess *process);
NSArray *arguments_;
NSString *workingDirectory_;
NSDictionary *environment_;

dispatch_source_t procSource_;

HDStream *stdinStream_;
HDStream *stdoutStream_;
HDStream *stderrStream_;
NSMutableArray *channels_;

dispatch_queue_t dispatchQueue_;
pid_t pid_;
int exitStatus_;
Expand Down Expand Up @@ -161,11 +161,11 @@ typedef void (^HDProcessBlock)(HDProcess *process);
* var net = require("net");
* var stdin = new net.Stream(0, 'unix');
* stdin.on('fd', function (fd) {
* var stream = new net.Stream(fd, "unix");
* stream.resume();
* stream.on('data', function (message) {
* stream.write('pong '+message.toString('utf8'));
* });
* var stream = new net.Stream(fd, "unix");
* stream.resume();
* stream.on('data', function (message) {
* stream.write('pong '+message.toString('utf8'));
* });
* });
* stdin.resume();
*
Expand All @@ -179,7 +179,7 @@ typedef void (^HDProcessBlock)(HDProcess *process);
* if (length == 0) return; // EOS
* printf("channel.onData -> %*s\n", length, (const char*)bytes);
* }];
*
*
* The returned HDStream is autoreleased, but will remain valid until
* the channel is closed (A reference is managed by its dispatch queue). Note
* that the returned stream pair is in a suspended state and need to receive a
Expand Down
56 changes: 28 additions & 28 deletions HDProcess.m
Expand Up @@ -49,12 +49,12 @@ static BOOL _fd_socketpipe(int fd[2]) {

static void _proc_handle_ev(HDProcess *self) {
unsigned long flags = dispatch_source_get_data(self->procSource_);
/*#define DPFLAG(X) ((flags & DISPATCH_PROC_##X) ? #X" " : "")
/*#define DPFLAG(X) ((flags & DISPATCH_PROC_##X) ? #X" " : "")
fprintf(stderr, "process %d: flags: %lx %s%s%s%s\n",
self.pid, flags,
DPFLAG(EXIT), DPFLAG(FORK), DPFLAG(EXEC), DPFLAG(SIGNAL));*/
if (flags & DISPATCH_PROC_EXIT) {
waitpid(self->pid_, &(self->exitStatus_), WNOHANG);
waitpid(self->pid_, &(self->exitStatus_), WNOHANG);

// cancel proc source
dispatch_source_cancel(self->procSource_);
Expand Down Expand Up @@ -103,10 +103,10 @@ @implementation HDProcess
+ (HDProcess*)start:(NSString*)program, ... {
HDProcess *proc = [[HDProcess alloc] init];
proc.program = program;
va_list valist;
va_start(valist, program);
va_list valist;
va_start(valist, program);
[proc setVariableArguments:valist];
va_end(valist);
va_end(valist);
return proc;
}

Expand Down Expand Up @@ -150,7 +150,7 @@ - (void)dealloc {
assert(pid_ == -1);

dispatch_release(dispatchQueue_); // no effect if its a global shared queue

[channels_ release];

[stdinStream_ release];
Expand Down Expand Up @@ -215,23 +215,23 @@ - (void)start {
[NSException raise:NSInvalidArgumentException
format:@"\"program\" has not been set"];
}

// check that we are not running
if (self.isRunning) {
[NSException raise:NSInternalInconsistencyException
format:@"already running"];
}

// reset exist status
exitStatus_ = -1;

// pipes
int stdin_pipe[2], stdout_pipe[2], stderr_pipe[2];
if (pipe(stdout_pipe) < 0 || pipe(stderr_pipe) < 0) {
[NSException raise:NSInternalInconsistencyException
format:@"pipe(): %s", strerror(errno)];
}

// stdin (unix socket if usesSocketStdin_ is true, used for FD delegation)
if (hasSocketpair_) {
if (!_fd_socketpipe(stdin_pipe)) {
Expand All @@ -245,35 +245,35 @@ - (void)start {
format:@"pipe(): %s", strerror(errno)];
}
}

// set close-on-exec flag
_fd_set_closeonexec(stdin_pipe[0]); _fd_set_closeonexec(stdin_pipe[1]);
_fd_set_closeonexec(stdout_pipe[0]); _fd_set_closeonexec(stdout_pipe[1]);
_fd_set_closeonexec(stderr_pipe[0]); _fd_set_closeonexec(stderr_pipe[1]);

// save environ in the case that we get it clobbered by the child process.
char **saved_env = environ;

// vfork & execvp
pid_ = fork();
if (pid_ == -1) {
[NSException raise:NSInternalInconsistencyException format:@"vfork()"];
} else if (pid_ == 0) {
// child

// close parent end of pipes and assign our stdio to our end
close(stdin_pipe[1]); // close write end
dup2(stdin_pipe[0], STDIN_FILENO);
close(stdout_pipe[0]); // close read end
dup2(stdout_pipe[1], STDOUT_FILENO);
close(stderr_pipe[0]); // close read end
dup2(stderr_pipe[1], STDERR_FILENO);

// chdir
if (workingDirectory_ && chdir([workingDirectory_ UTF8String]) != 0) {
_exit(127);
}

// set environment
if (environment_) {
NSUInteger envlen = [environment_ count];
Expand All @@ -299,7 +299,7 @@ - (void)start {

// executable
const char *file = [program_ UTF8String];

// args
char *_argv[2] = {NULL, NULL};
char **argv = (char**)&_argv;
Expand All @@ -316,19 +316,19 @@ - (void)start {
} else {
_argv[0] = strdup(file);
}

// switch process image
execvp(file, argv);

// if we get here execvp failed
_exit(127);
}

// [parent]

// restore environment
environ = saved_env;

// create and start process watcher
procSource_ = dispatch_source_create(DISPATCH_SOURCE_TYPE_PROC, pid_
,DISPATCH_PROC_EXIT
Expand All @@ -340,35 +340,35 @@ - (void)start {
(dispatch_function_t)&_proc_handle_ev);
dispatch_set_context(procSource_, [self retain]); // released by ^
dispatch_resume(procSource_);

// close other end of pipes
close(stdin_pipe[0]);// _fd_set_nonblock(stdin_pipe[1]);
close(stdout_pipe[1]);// _fd_set_nonblock(stdout_pipe[0]);
close(stderr_pipe[1]);// _fd_set_nonblock(stderr_pipe[0]);

// explicitly set dispatchQueue_
stdinStream_.dispatchQueue = dispatchQueue_;
stdoutStream_.dispatchQueue = dispatchQueue_;
stderrStream_.dispatchQueue = dispatchQueue_;

// setup and resume stdin, stdout and stderr streams
HDStream *stdinStream = [stdinStream_
copyWithFileDescriptor:stdin_pipe[1] disableReading:YES disableWriting:NO];
HDStream *stdoutStream = [stdoutStream_
copyWithFileDescriptor:stdout_pipe[0] disableReading:NO disableWriting:YES];
HDStream *stderrStream = [stderrStream_
copyWithFileDescriptor:stderr_pipe[0] disableReading:NO disableWriting:YES];

// swap instances
id old = stdinStream_; stdinStream_ = stdinStream; [old release];
old = stdoutStream_; stdoutStream_ = stdoutStream; [old release];
old = stderrStream_; stderrStream_ = stderrStream; [old release];

// make sure they are all in an unsuspended state
[stdinStream_ resume];
[stdoutStream_ resume];
[stderrStream_ resume];

// dequeue any queued input
if (queuedInput_) {
for (id entry in queuedInput_) {
Expand Down Expand Up @@ -406,7 +406,7 @@ - (HDNamedStream*)createChannel:(NSString*)name {
HDNamedStream *stream =
[HDNamedStream streamWithFileDescriptor:fds[0] name:name];
stream.dispatchQueue = dispatchQueue_;

// register object
if (!channels_) {
channels_ = [[NSMutableArray alloc] initWithObjects:stream, nil];
Expand All @@ -427,7 +427,7 @@ - (HDNamedStream*)createChannel:(NSString*)name {
// send the other part of the FD pair to the process
[stdinStream_ writeFileDescriptor:fds[1] name:name];
}

return stream;
}

Expand Down
2 changes: 1 addition & 1 deletion HDStream.h
Expand Up @@ -21,7 +21,7 @@ typedef void (^HDStreamBlock)(const void *bytes, size_t length);
dispatch_queue_t dispatchQueue_;
uint32_t flags_;
int fd_;

dispatch_source_t readSource_;
dispatch_source_t writeSource_;
HDStreamBlock onData_;
Expand Down

0 comments on commit ca145dc

Please sign in to comment.