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:
Richard Frith-MacDonald 1998-11-30 10:15:35 +00:00
parent e3a761a64a
commit 3bc05a0c62

View file

@ -49,26 +49,34 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
@implementation NSTask @implementation NSTask
+ (void) initialize
{
if (self == [NSTask class])
{
signal(SIGCHLD, SIG_IGN);
}
}
+ (NSTask*)launchedTaskWithLaunchPath:(NSString*)path arguments: (NSArray*)args + (NSTask*)launchedTaskWithLaunchPath:(NSString*)path arguments: (NSArray*)args
{ {
NSTask* task = [NSTask new]; NSTask* task = [NSTask new];
[task setLaunchPath: path]; [task setLaunchPath: path];
[task setArguments: args]; [task setArguments: args];
[task launch]; [task launch];
return [task autorelease]; return [task autorelease];
} }
- (void) dealloc - (void) dealloc
{ {
[arguments release]; [arguments release];
[environment release]; [environment release];
[launchPath release]; [launchPath release];
[currentDirectoryPath release]; [currentDirectoryPath release];
[standardError release]; [standardError release];
[standardInput release]; [standardInput release];
[standardOutput release]; [standardOutput release];
[super dealloc]; [super dealloc];
} }
@ -78,134 +86,146 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
- (NSArray*) arguments - (NSArray*) arguments
{ {
return arguments; return arguments;
} }
- (NSString*) currentDirectoryPath - (NSString*) currentDirectoryPath
{ {
if (currentDirectoryPath == nil) { if (currentDirectoryPath == nil)
[self setCurrentDirectoryPath: {
[self setCurrentDirectoryPath:
[[NSFileManager defaultManager] currentDirectoryPath]]; [[NSFileManager defaultManager] currentDirectoryPath]];
} }
return currentDirectoryPath; return currentDirectoryPath;
} }
- (NSDictionary*) environment - (NSDictionary*) environment
{ {
if (environment == nil) { if (environment == nil)
[self setEnvironment: [[NSProcessInfo processInfo] environment]]; {
[self setEnvironment: [[NSProcessInfo processInfo] environment]];
} }
return environment; return environment;
} }
- (NSString*) launchPath - (NSString*) launchPath
{ {
return launchPath; return launchPath;
} }
- (NSFileHandle*) standardError - (NSFileHandle*) standardError
{ {
if (standardError == nil) { if (standardError == nil)
[self setStandardError: [NSFileHandle fileHandleWithStandardError]]; {
[self setStandardError: [NSFileHandle fileHandleWithStandardError]];
} }
return standardError; return standardError;
} }
- (NSFileHandle*) standardInput - (NSFileHandle*) standardInput
{ {
if (standardInput == nil) { if (standardInput == nil)
[self setStandardInput: [NSFileHandle fileHandleWithStandardInput]]; {
[self setStandardInput: [NSFileHandle fileHandleWithStandardInput]];
} }
return standardInput; return standardInput;
} }
- (NSFileHandle*) standardOutput - (NSFileHandle*) standardOutput
{ {
if (standardOutput == nil) { if (standardOutput == nil)
[self setStandardOutput: [NSFileHandle fileHandleWithStandardOutput]]; {
[self setStandardOutput: [NSFileHandle fileHandleWithStandardOutput]];
} }
return standardOutput; return standardOutput;
} }
/* /*
* Setting task parameters. * Setting task parameters.
*/ */
- (void)setArguments: (NSArray*)args - (void) setArguments: (NSArray*)args
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[args retain]; [args retain];
[arguments release]; [arguments release];
arguments = args; arguments = args;
} }
- (void)setCurrentDirectoryPath: (NSString*)path - (void) setCurrentDirectoryPath: (NSString*)path
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[path retain]; [path retain];
[currentDirectoryPath release]; [currentDirectoryPath release];
currentDirectoryPath = path; currentDirectoryPath = path;
} }
- (void)setEnvironment: (NSDictionary*)env - (void) setEnvironment: (NSDictionary*)env
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[env retain]; [env retain];
[environment release]; [environment release];
environment = env; environment = env;
} }
- (void)setLaunchPath: (NSString*)path - (void) setLaunchPath: (NSString*)path
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[path retain]; [path retain];
[launchPath release]; [launchPath release];
launchPath = path; launchPath = path;
} }
- (void)setStandardError: (NSFileHandle*)hdl - (void) setStandardError: (NSFileHandle*)hdl
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[hdl retain]; [hdl retain];
[standardError release]; [standardError release];
standardError = hdl; standardError = hdl;
} }
- (void)setStandardInput: (NSFileHandle*)hdl - (void) setStandardInput: (NSFileHandle*)hdl
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[hdl retain]; [hdl retain];
[standardInput release]; [standardInput release];
standardInput = hdl; standardInput = hdl;
} }
- (void)setStandardOutput: (NSFileHandle*)hdl - (void) setStandardOutput: (NSFileHandle*)hdl
{ {
if (hasLaunched) { if (hasLaunched)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has been launched"]; format: @"NSTask - task has been launched"];
} }
[hdl retain]; [hdl retain];
[standardOutput release]; [standardOutput release];
standardOutput = hdl; standardOutput = hdl;
} }
/* /*
@ -214,28 +234,38 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
- (BOOL) isRunning - (BOOL) isRunning
{ {
if (hasLaunched == NO) return NO; if (hasLaunched == NO)
if (hasCollected == NO) { {
[self _collectChild]; return NO;
} }
if (hasTerminated == YES) return NO; if (hasCollected == NO)
return YES; {
[self _collectChild];
}
if (hasTerminated == YES)
{
return NO;
}
return YES;
} }
- (int) terminationStatus - (int) terminationStatus
{ {
if (hasLaunched == NO) { if (hasLaunched == NO)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has not yet launched"]; format: @"NSTask - task has not yet launched"];
} }
if (hasCollected == NO) { if (hasCollected == NO)
[self _collectChild]; {
[self _collectChild];
} }
if (hasTerminated == NO) { if (hasTerminated == NO)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has not yet terminated"]; format: @"NSTask - task has not yet terminated"];
} }
return terminationStatus; return terminationStatus;
} }
/* /*
@ -243,122 +273,145 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
*/ */
- (void) interrupt - (void) interrupt
{ {
[self notImplemented: _cmd]; /* Undocumented as yet */
} }
- (void) launch - (void) launch
{ {
int pid; int pid;
const char* executable; const char *executable;
const char* path; const char *path;
int idesc; int idesc;
int odesc; int odesc;
int edesc; int edesc;
NSDictionary *e = [self environment]; NSDictionary *e = [self environment];
NSArray *k = [e allKeys]; NSArray *k = [e allKeys];
NSArray *a = [self arguments]; NSArray *a = [self arguments];
int ec = [e count]; int ec = [e count];
int ac = [a count]; int ac = [a count];
const char *args[ac+2]; const char *args[ac+2];
const char *envl[ec+1]; const char *envl[ec+1];
int i; int i;
if (hasLaunched) { if (hasLaunched)
return; {
return;
} }
if (launchPath == nil) { if (launchPath == nil)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - no launch path set"]; format: @"NSTask - no launch path set"];
} }
else if ([[NSFileManager defaultManager] isExecutableFileAtPath: else if ([[NSFileManager defaultManager] isExecutableFileAtPath:
launchPath] == NO) { launchPath] == NO)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - launch path is not valid"]; format: @"NSTask - launch path is not valid"];
} }
executable = [[self launchPath] cString]; executable = [[self launchPath] cString];
args[0] = [[[self launchPath] lastPathComponent] 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[i+1] = [[[a objectAtIndex: i] description] cString];
} }
args[ac+1] = 0; args[ac+1] = 0;
for (i = 0; i < ec; i++) { for (i = 0; i < ec; i++)
NSString *s; {
id key = [k objectAtIndex: i]; NSString *s;
id val = [e objectForKey: key]; id key = [k objectAtIndex: i];
id val = [e objectForKey: key];
if (val) { if (val)
s = [NSString stringWithFormat: @"%s=%s", {
s = [NSString stringWithFormat: @"%s=%s",
[key cString], [val cString]]; [key cString], [val cString]];
} }
else { else
s = [NSString stringWithFormat: @"%s=", [key cString]]; {
s = [NSString stringWithFormat: @"%s=", [key cString]];
} }
envl[i] = [s cString]; envl[i] = [s cString];
} }
envl[ec] = 0; envl[ec] = 0;
path = [[self currentDirectoryPath] cString]; path = [[self currentDirectoryPath] cString];
idesc = [[self standardInput] fileDescriptor]; idesc = [[self standardInput] fileDescriptor];
odesc = [[self standardError] fileDescriptor]; odesc = [[self standardError] fileDescriptor];
edesc = [[self standardOutput] fileDescriptor]; edesc = [[self standardOutput] fileDescriptor];
signal(SIGCHLD, SIG_IGN); pid = fork();
pid = fork(); if (pid < 0)
if (pid < 0) { {
[NSException raise: NSInvalidArgumentException [NSException raise: NSInvalidArgumentException
format: @"NSTask - failed to create child process"]; format: @"NSTask - failed to create child process"];
} }
if (pid == 0) { if (pid == 0)
if (idesc != 0) dup2(idesc, 0); {
if (odesc != 1) dup2(odesc, 1); if (idesc != 0)
if (edesc != 2) dup2(edesc, 2); {
chdir(path); dup2(idesc, 0);
execve(executable, args, envl); }
exit(-1); 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; taskId = pid;
hasLaunched = YES;
} }
} }
- (void) terminate - (void) terminate
{ {
if (hasLaunched == NO) { if (hasLaunched == NO)
[NSException raise: NSInvalidArgumentException {
[NSException raise: NSInvalidArgumentException
format: @"NSTask - task has not yet launched"]; format: @"NSTask - task has not yet launched"];
} }
if (hasTerminated) { if (hasTerminated)
return; {
return;
} }
hasTerminated = YES; hasTerminated = YES;
#ifdef HAVE_KILLPG #ifdef HAVE_KILLPG
killpg(taskId, SIGTERM); killpg(taskId, SIGTERM);
#else #else
kill(-taskId, SIGTERM); kill(-taskId, SIGTERM);
#endif #endif
if (hasNotified == NO) { if (hasNotified == NO)
[self _sendNotification]; {
[self _sendNotification];
} }
} }
- (void) waitUntilExit - (void) waitUntilExit
{ {
while ([self isRunning]) { while ([self isRunning])
NSDate *limit; {
NSDate *limit;
/* /*
* Poll at 1.0 second intervals. * Poll at 1.0 second intervals.
*/ */
limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 1.0]; limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 1.0];
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate: nil]; beforeDate: nil];
[limit release]; [limit release];
} }
} }
@end @end
@ -367,14 +420,18 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
- (void) _collectChild - (void) _collectChild
{ {
if (hasCollected == NO) { if (hasCollected == NO)
if (waitpid(taskId, &terminationStatus, WNOHANG) == taskId) { {
if (WIFEXITED(terminationStatus)) { if (waitpid(taskId, &terminationStatus, WNOHANG) == taskId)
terminationStatus = WEXITSTATUS(terminationStatus); {
hasCollected = YES; if (WIFEXITED(terminationStatus))
hasTerminated = YES; {
if (hasNotified == NO) { terminationStatus = WEXITSTATUS(terminationStatus);
[self _sendNotification]; hasCollected = YES;
hasTerminated = YES;
if (hasNotified == NO)
{
[self _sendNotification];
} }
} }
} }
@ -383,15 +440,16 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
- (void) _sendNotification - (void) _sendNotification
{ {
if (hasNotified == NO) { if (hasNotified == NO)
NSNotification *n; {
NSNotification *n;
hasNotified = YES; hasNotified = YES;
n = [NSNotification notificationWithName: NSTaskDidTerminateNotification n = [NSNotification notificationWithName: NSTaskDidTerminateNotification
object: self object: self
userInfo: nil]; userInfo: nil];
[[NSNotificationQueue defaultQueue] enqueueNotification: n [[NSNotificationQueue defaultQueue] enqueueNotification: n
postingStyle: NSPostASAP postingStyle: NSPostASAP
coalesceMask: NSNotificationNoCoalescing coalesceMask: NSNotificationNoCoalescing
forModes: nil]; forModes: nil];