mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-31 16:50:58 +00:00
Tidied indentation etc
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@3354 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
1ede15edbc
commit
f275a3055c
1 changed files with 229 additions and 171 deletions
140
Source/NSTask.m
140
Source/NSTask.m
|
@ -49,6 +49,14 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
@implementation NSTask
|
||||
|
||||
+ (void) initialize
|
||||
{
|
||||
if (self == [NSTask class])
|
||||
{
|
||||
signal(SIGCHLD, SIG_IGN);
|
||||
}
|
||||
}
|
||||
|
||||
+ (NSTask*)launchedTaskWithLaunchPath:(NSString*)path arguments: (NSArray*)args
|
||||
{
|
||||
NSTask* task = [NSTask new];
|
||||
|
@ -83,7 +91,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (NSString*) currentDirectoryPath
|
||||
{
|
||||
if (currentDirectoryPath == nil) {
|
||||
if (currentDirectoryPath == nil)
|
||||
{
|
||||
[self setCurrentDirectoryPath:
|
||||
[[NSFileManager defaultManager] currentDirectoryPath]];
|
||||
}
|
||||
|
@ -92,7 +101,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (NSDictionary*) environment
|
||||
{
|
||||
if (environment == nil) {
|
||||
if (environment == nil)
|
||||
{
|
||||
[self setEnvironment: [[NSProcessInfo processInfo] environment]];
|
||||
}
|
||||
return environment;
|
||||
|
@ -105,7 +115,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (NSFileHandle*) standardError
|
||||
{
|
||||
if (standardError == nil) {
|
||||
if (standardError == nil)
|
||||
{
|
||||
[self setStandardError: [NSFileHandle fileHandleWithStandardError]];
|
||||
}
|
||||
return standardError;
|
||||
|
@ -113,7 +124,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (NSFileHandle*) standardInput
|
||||
{
|
||||
if (standardInput == nil) {
|
||||
if (standardInput == nil)
|
||||
{
|
||||
[self setStandardInput: [NSFileHandle fileHandleWithStandardInput]];
|
||||
}
|
||||
return standardInput;
|
||||
|
@ -121,7 +133,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (NSFileHandle*) standardOutput
|
||||
{
|
||||
if (standardOutput == nil) {
|
||||
if (standardOutput == nil)
|
||||
{
|
||||
[self setStandardOutput: [NSFileHandle fileHandleWithStandardOutput]];
|
||||
}
|
||||
return standardOutput;
|
||||
|
@ -133,7 +146,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setArguments: (NSArray*)args
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -144,7 +158,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setCurrentDirectoryPath: (NSString*)path
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -155,7 +170,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setEnvironment: (NSDictionary*)env
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -166,7 +182,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setLaunchPath: (NSString*)path
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -177,7 +194,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setStandardError: (NSFileHandle*)hdl
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -188,7 +206,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setStandardInput: (NSFileHandle*)hdl
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -199,7 +218,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) setStandardOutput: (NSFileHandle*)hdl
|
||||
{
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has been launched"];
|
||||
}
|
||||
|
@ -214,24 +234,34 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (BOOL) isRunning
|
||||
{
|
||||
if (hasLaunched == NO) return NO;
|
||||
if (hasCollected == NO) {
|
||||
if (hasLaunched == NO)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
if (hasCollected == NO)
|
||||
{
|
||||
[self _collectChild];
|
||||
}
|
||||
if (hasTerminated == YES) return NO;
|
||||
if (hasTerminated == YES)
|
||||
{
|
||||
return NO;
|
||||
}
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (int) terminationStatus
|
||||
{
|
||||
if (hasLaunched == NO) {
|
||||
if (hasLaunched == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has not yet launched"];
|
||||
}
|
||||
if (hasCollected == NO) {
|
||||
if (hasCollected == NO)
|
||||
{
|
||||
[self _collectChild];
|
||||
}
|
||||
if (hasTerminated == NO) {
|
||||
if (hasTerminated == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has not yet terminated"];
|
||||
}
|
||||
|
@ -243,6 +273,7 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
*/
|
||||
- (void) interrupt
|
||||
{
|
||||
[self notImplemented: _cmd]; /* Undocumented as yet */
|
||||
}
|
||||
|
||||
- (void) launch
|
||||
|
@ -262,16 +293,19 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
const char *envl[ec+1];
|
||||
int i;
|
||||
|
||||
if (hasLaunched) {
|
||||
if (hasLaunched)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (launchPath == nil) {
|
||||
if (launchPath == nil)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - no launch path set"];
|
||||
}
|
||||
else if ([[NSFileManager defaultManager] isExecutableFileAtPath:
|
||||
launchPath] == NO) {
|
||||
launchPath] == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - launch path is not valid"];
|
||||
}
|
||||
|
@ -279,21 +313,25 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
executable = [[self launchPath] cString];
|
||||
|
||||
args[0] = [[[self launchPath] lastPathComponent] cString];
|
||||
for (i = 0; i < ac; i++) {
|
||||
for (i = 0; i < ac; i++)
|
||||
{
|
||||
args[i+1] = [[[a objectAtIndex: i] description] cString];
|
||||
}
|
||||
args[ac+1] = 0;
|
||||
|
||||
for (i = 0; i < ec; i++) {
|
||||
for (i = 0; i < ec; i++)
|
||||
{
|
||||
NSString *s;
|
||||
id key = [k objectAtIndex: i];
|
||||
id val = [e objectForKey: key];
|
||||
|
||||
if (val) {
|
||||
if (val)
|
||||
{
|
||||
s = [NSString stringWithFormat: @"%s=%s",
|
||||
[key cString], [val cString]];
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
s = [NSString stringWithFormat: @"%s=", [key cString]];
|
||||
}
|
||||
envl[i] = [s cString];
|
||||
|
@ -305,21 +343,32 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
odesc = [[self standardError] fileDescriptor];
|
||||
edesc = [[self standardOutput] fileDescriptor];
|
||||
|
||||
signal(SIGCHLD, SIG_IGN);
|
||||
pid = fork();
|
||||
if (pid < 0) {
|
||||
if (pid < 0)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - failed to create child process"];
|
||||
}
|
||||
if (pid == 0) {
|
||||
if (idesc != 0) dup2(idesc, 0);
|
||||
if (odesc != 1) dup2(odesc, 1);
|
||||
if (edesc != 2) dup2(edesc, 2);
|
||||
if (pid == 0)
|
||||
{
|
||||
if (idesc != 0)
|
||||
{
|
||||
dup2(idesc, 0);
|
||||
}
|
||||
if (odesc != 1)
|
||||
{
|
||||
dup2(odesc, 1);
|
||||
}
|
||||
if (edesc != 2)
|
||||
{
|
||||
dup2(edesc, 2);
|
||||
}
|
||||
chdir(path);
|
||||
execve(executable, args, envl);
|
||||
exit(-1);
|
||||
}
|
||||
else {
|
||||
else
|
||||
{
|
||||
taskId = pid;
|
||||
hasLaunched = YES;
|
||||
}
|
||||
|
@ -327,11 +376,13 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) terminate
|
||||
{
|
||||
if (hasLaunched == NO) {
|
||||
if (hasLaunched == NO)
|
||||
{
|
||||
[NSException raise: NSInvalidArgumentException
|
||||
format: @"NSTask - task has not yet launched"];
|
||||
}
|
||||
if (hasTerminated) {
|
||||
if (hasTerminated)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -342,14 +393,16 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
kill(-taskId, SIGTERM);
|
||||
#endif
|
||||
|
||||
if (hasNotified == NO) {
|
||||
if (hasNotified == NO)
|
||||
{
|
||||
[self _sendNotification];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) waitUntilExit
|
||||
{
|
||||
while ([self isRunning]) {
|
||||
while ([self isRunning])
|
||||
{
|
||||
NSDate *limit;
|
||||
|
||||
/*
|
||||
|
@ -367,13 +420,17 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) _collectChild
|
||||
{
|
||||
if (hasCollected == NO) {
|
||||
if (waitpid(taskId, &terminationStatus, WNOHANG) == taskId) {
|
||||
if (WIFEXITED(terminationStatus)) {
|
||||
if (hasCollected == NO)
|
||||
{
|
||||
if (waitpid(taskId, &terminationStatus, WNOHANG) == taskId)
|
||||
{
|
||||
if (WIFEXITED(terminationStatus))
|
||||
{
|
||||
terminationStatus = WEXITSTATUS(terminationStatus);
|
||||
hasCollected = YES;
|
||||
hasTerminated = YES;
|
||||
if (hasNotified == NO) {
|
||||
if (hasNotified == NO)
|
||||
{
|
||||
[self _sendNotification];
|
||||
}
|
||||
}
|
||||
|
@ -383,7 +440,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
|||
|
||||
- (void) _sendNotification
|
||||
{
|
||||
if (hasNotified == NO) {
|
||||
if (hasNotified == NO)
|
||||
{
|
||||
NSNotification *n;
|
||||
|
||||
hasNotified = YES;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue