mirror of
https://github.com/gnustep/libs-base.git
synced 2025-06-01 09:02:01 +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
e3a761a64a
commit
3bc05a0c62
1 changed files with 229 additions and 171 deletions
158
Source/NSTask.m
158
Source/NSTask.m
|
@ -49,6 +49,14 @@ 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];
|
||||||
|
@ -83,7 +91,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (NSString*) currentDirectoryPath
|
- (NSString*) currentDirectoryPath
|
||||||
{
|
{
|
||||||
if (currentDirectoryPath == nil) {
|
if (currentDirectoryPath == nil)
|
||||||
|
{
|
||||||
[self setCurrentDirectoryPath:
|
[self setCurrentDirectoryPath:
|
||||||
[[NSFileManager defaultManager] currentDirectoryPath]];
|
[[NSFileManager defaultManager] currentDirectoryPath]];
|
||||||
}
|
}
|
||||||
|
@ -92,7 +101,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (NSDictionary*) environment
|
- (NSDictionary*) environment
|
||||||
{
|
{
|
||||||
if (environment == nil) {
|
if (environment == nil)
|
||||||
|
{
|
||||||
[self setEnvironment: [[NSProcessInfo processInfo] environment]];
|
[self setEnvironment: [[NSProcessInfo processInfo] environment]];
|
||||||
}
|
}
|
||||||
return environment;
|
return environment;
|
||||||
|
@ -105,7 +115,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (NSFileHandle*) standardError
|
- (NSFileHandle*) standardError
|
||||||
{
|
{
|
||||||
if (standardError == nil) {
|
if (standardError == nil)
|
||||||
|
{
|
||||||
[self setStandardError: [NSFileHandle fileHandleWithStandardError]];
|
[self setStandardError: [NSFileHandle fileHandleWithStandardError]];
|
||||||
}
|
}
|
||||||
return standardError;
|
return standardError;
|
||||||
|
@ -113,7 +124,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (NSFileHandle*) standardInput
|
- (NSFileHandle*) standardInput
|
||||||
{
|
{
|
||||||
if (standardInput == nil) {
|
if (standardInput == nil)
|
||||||
|
{
|
||||||
[self setStandardInput: [NSFileHandle fileHandleWithStandardInput]];
|
[self setStandardInput: [NSFileHandle fileHandleWithStandardInput]];
|
||||||
}
|
}
|
||||||
return standardInput;
|
return standardInput;
|
||||||
|
@ -121,7 +133,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (NSFileHandle*) standardOutput
|
- (NSFileHandle*) standardOutput
|
||||||
{
|
{
|
||||||
if (standardOutput == nil) {
|
if (standardOutput == nil)
|
||||||
|
{
|
||||||
[self setStandardOutput: [NSFileHandle fileHandleWithStandardOutput]];
|
[self setStandardOutput: [NSFileHandle fileHandleWithStandardOutput]];
|
||||||
}
|
}
|
||||||
return standardOutput;
|
return standardOutput;
|
||||||
|
@ -131,9 +144,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
* 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"];
|
||||||
}
|
}
|
||||||
|
@ -142,9 +156,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -153,9 +168,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -164,9 +180,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -175,9 +192,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -186,9 +204,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -197,9 +216,10 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -214,24 +234,34 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (BOOL) isRunning
|
- (BOOL) isRunning
|
||||||
{
|
{
|
||||||
if (hasLaunched == NO) return NO;
|
if (hasLaunched == NO)
|
||||||
if (hasCollected == NO) {
|
{
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
if (hasCollected == NO)
|
||||||
|
{
|
||||||
[self _collectChild];
|
[self _collectChild];
|
||||||
}
|
}
|
||||||
if (hasTerminated == YES) return NO;
|
if (hasTerminated == YES)
|
||||||
|
{
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
return YES;
|
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"];
|
||||||
}
|
}
|
||||||
|
@ -243,13 +273,14 @@ 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;
|
||||||
|
@ -262,16 +293,19 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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"];
|
||||||
}
|
}
|
||||||
|
@ -279,21 +313,25 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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;
|
NSString *s;
|
||||||
id key = [k objectAtIndex: i];
|
id key = [k objectAtIndex: i];
|
||||||
id val = [e objectForKey: key];
|
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];
|
||||||
|
@ -305,21 +343,32 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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);
|
{
|
||||||
|
dup2(idesc, 0);
|
||||||
|
}
|
||||||
|
if (odesc != 1)
|
||||||
|
{
|
||||||
|
dup2(odesc, 1);
|
||||||
|
}
|
||||||
|
if (edesc != 2)
|
||||||
|
{
|
||||||
|
dup2(edesc, 2);
|
||||||
|
}
|
||||||
chdir(path);
|
chdir(path);
|
||||||
execve(executable, args, envl);
|
execve(executable, args, envl);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
taskId = pid;
|
taskId = pid;
|
||||||
hasLaunched = YES;
|
hasLaunched = YES;
|
||||||
}
|
}
|
||||||
|
@ -327,11 +376,13 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -342,14 +393,16 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
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;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -367,13 +420,17 @@ 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)
|
||||||
|
{
|
||||||
|
if (WIFEXITED(terminationStatus))
|
||||||
|
{
|
||||||
terminationStatus = WEXITSTATUS(terminationStatus);
|
terminationStatus = WEXITSTATUS(terminationStatus);
|
||||||
hasCollected = YES;
|
hasCollected = YES;
|
||||||
hasTerminated = YES;
|
hasTerminated = YES;
|
||||||
if (hasNotified == NO) {
|
if (hasNotified == NO)
|
||||||
|
{
|
||||||
[self _sendNotification];
|
[self _sendNotification];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -383,7 +440,8 @@ NSString *NSTaskDidTerminateNotification = @"NSTaskDidTerminateNotification";
|
||||||
|
|
||||||
- (void) _sendNotification
|
- (void) _sendNotification
|
||||||
{
|
{
|
||||||
if (hasNotified == NO) {
|
if (hasNotified == NO)
|
||||||
|
{
|
||||||
NSNotification *n;
|
NSNotification *n;
|
||||||
|
|
||||||
hasNotified = YES;
|
hasNotified = YES;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue