Author: rfm Date: Sun Jun 26 07:56:10 2016 New Revision: 39924 URL: http://svn.gna.org/viewcvs/gnustep?rev=39924&view=rev Log: Add newere method
Modified: libs/base/trunk/ChangeLog libs/base/trunk/Headers/Foundation/NSTask.h libs/base/trunk/Source/NSTask.m libs/base/trunk/Tests/base/NSTask/general.m libs/base/trunk/Tests/base/NSTask/notify.m Modified: libs/base/trunk/ChangeLog URL: http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/ChangeLog?rev=39924&r1=39923&r2=39924&view=diff ============================================================================== --- libs/base/trunk/ChangeLog (original) +++ libs/base/trunk/ChangeLog Sun Jun 26 07:56:10 2016 @@ -1,3 +1,11 @@ +2016-06-26 Richard Frith-Macdonald <r...@gnu.org> + + * Headers/Foundation/NSTask.h: + * Source/NSTask.m: + * Tests/base/NSTask/general.m: + * Tests/base/NSTask/notify.m: + Add -terminationReason method from OSX10.5 + 2016-06-25 Richard Frith-Macdonald <r...@gnu.org> * Source/NSMessagePort.m: Modified: libs/base/trunk/Headers/Foundation/NSTask.h URL: http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Headers/Foundation/NSTask.h?rev=39924&r1=39923&r2=39924&view=diff ============================================================================== --- libs/base/trunk/Headers/Foundation/NSTask.h (original) +++ libs/base/trunk/Headers/Foundation/NSTask.h Sun Jun 26 07:56:10 2016 @@ -36,6 +36,14 @@ extern "C" { #endif +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5,GS_API_LATEST) +enum { + NSTaskTerminationReasonExit = 1, + NSTaskTerminationReasonUncaughtSignal = 2 +}; +typedef NSInteger NSTaskTerminationReason; +#endif + @interface NSTask : NSObject { #if GS_EXPOSE(NSTask) @@ -53,6 +61,7 @@ BOOL _hasTerminated; BOOL _hasCollected; BOOL _hasNotified; + NSTaskTerminationReason _terminationReason; #endif #if GS_NONFRAGILE #else @@ -97,6 +106,9 @@ #if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST) - (int) processIdentifier; #endif +#if OS_API_VERSION(MAC_OS_X_VERSION_10_5,GS_API_LATEST) +- (NSTaskTerminationReason) terminationReason; +#endif - (int) terminationStatus; /* Modified: libs/base/trunk/Source/NSTask.m URL: http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Source/NSTask.m?rev=39924&r1=39923&r2=39924&view=diff ============================================================================== --- libs/base/trunk/Source/NSTask.m (original) +++ libs/base/trunk/Source/NSTask.m Sun Jun 26 07:56:10 2016 @@ -118,6 +118,10 @@ } #ifdef _WIN32 +/* We use exit code 10 to denote a process termination. + * Windows does nt have an exit code to denote termination this way. + */ +#define WIN_SIGNALLED 10 @interface NSConcreteWindowsTask : NSTask { @public @@ -222,7 +226,7 @@ @interface NSTask (Private) - (NSString *) _fullLaunchPath; - (void) _collectChild; -- (void) _terminatedChild: (int)status; +- (void) _terminatedChild: (int)status reason: (NSTaskTerminationReason)reason; @end @@ -674,6 +678,30 @@ } /** + * Returns the termination reason of the task.<br /> + * If the task has not completed running, raises an + * NSInvalidArgumentException. + */ +- (NSTaskTerminationReason) terminationReason +{ + if (_hasLaunched == NO) + { + [NSException raise: NSInvalidArgumentException + format: @"NSTask - task has not yet launched"]; + } + if (_hasCollected == NO) + { + [self _collectChild]; + } + if (_hasTerminated == NO) + { + [NSException raise: NSInvalidArgumentException + format: @"NSTask - task has not yet terminated"]; + } + return _terminationReason; +} + +/** * Returns the termination status of the task.<br /> * If the task has not completed running, raises an * NSInvalidArgumentException. @@ -876,13 +904,14 @@ [self subclassResponsibility: _cmd]; } -- (void) _terminatedChild: (int)status +- (void) _terminatedChild: (int)status reason: (NSTaskTerminationReason)reason { [tasksLock lock]; IF_NO_GC([[self retain] autorelease];) NSMapRemove(activeTasks, (void*)(intptr_t)_taskId); [tasksLock unlock]; _terminationStatus = status; + _terminationReason = reason; _hasCollected = YES; _hasTerminated = YES; if (_hasNotified == NO) @@ -930,7 +959,9 @@ { if (eCode != STILL_ACTIVE) { - [t _terminatedChild: eCode]; + [t _terminatedChild: eCode reason: (WIN_SIGNALLED == eCode) + ? NSTaskTerminationReasonUncaughtSignal + : NSTaskTerminationReasonExit]; found = YES; } } @@ -972,8 +1003,11 @@ return; } + /* We use exit code 10 to denote a process termination. + * Windows does nt have an exit code to denote termination this way. + */ _hasTerminated = YES; - TerminateProcess(procInfo.hProcess, 10); + TerminateProcess(procInfo.hProcess, WIN_SIGNALLED); } @@ -1307,7 +1341,9 @@ } else if (eCode != STILL_ACTIVE) { - [self _terminatedChild: eCode]; + [self _terminatedChild: eCode reason: (WIN_SIGNALLED == eCode) + ? NSTaskTerminationReasonUncaughtSignal + : NSTaskTerminationReasonExit]; } } } @@ -1364,7 +1400,8 @@ NSLog(@"waitpid %d, exit status = %d", result, status); #endif - [t _terminatedChild: WEXITSTATUS(status)]; + [t _terminatedChild: WEXITSTATUS(status) + reason: NSTaskTerminationReasonExit]; found = YES; } else if (WIFSIGNALED(status)) @@ -1373,7 +1410,8 @@ NSLog(@"waitpid %d, termination status = %d", result, status); #endif - [t _terminatedChild: WTERMSIG(status)]; + [t _terminatedChild: WTERMSIG(status) + reason: NSTaskTerminationReasonUncaughtSignal]; found = YES; } else @@ -1669,7 +1707,8 @@ NSLog(@"waitpid %d, exit status = %d", result, status); #endif - [self _terminatedChild: WEXITSTATUS(status)]; + [self _terminatedChild: WEXITSTATUS(status) + reason: NSTaskTerminationReasonExit]; } else if (WIFSIGNALED(status)) { @@ -1677,7 +1716,8 @@ NSLog(@"waitpid %d, termination status = %d", result, status); #endif - [self _terminatedChild: WTERMSIG(status)]; + [self _terminatedChild: WTERMSIG(status) + reason: NSTaskTerminationReasonUncaughtSignal]; } else { Modified: libs/base/trunk/Tests/base/NSTask/general.m URL: http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Tests/base/NSTask/general.m?rev=39924&r1=39923&r2=39924&view=diff ============================================================================== --- libs/base/trunk/Tests/base/NSTask/general.m (original) +++ libs/base/trunk/Tests/base/NSTask/general.m Sun Jun 26 07:56:10 2016 @@ -49,6 +49,8 @@ [task setArguments: args]; [task launch]; [task waitUntilExit]; + PASS([task terminationReason] == NSTaskTerminationReasonExit, + "termination reason for normal exit works"); [arp release]; arp = nil; return 0; Modified: libs/base/trunk/Tests/base/NSTask/notify.m URL: http://svn.gna.org/viewcvs/gnustep/libs/base/trunk/Tests/base/NSTask/notify.m?rev=39924&r1=39923&r2=39924&view=diff ============================================================================== --- libs/base/trunk/Tests/base/NSTask/notify.m (original) +++ libs/base/trunk/Tests/base/NSTask/notify.m Sun Jun 26 07:56:10 2016 @@ -6,7 +6,7 @@ #import <Foundation/NSRunLoop.h> #import <Foundation/NSAutoreleasePool.h> -#import "ObjectTesting.h" +#import "ObjectTesting.h" @interface TaskHandler : NSObject { @@ -16,74 +16,81 @@ @implementation TaskHandler -static BOOL taskTerminationNotificationReceived; +static BOOL taskTerminationNotificationReceived; - (void) setLaunchPath: (NSString*)s { ASSIGNCOPY(path, s); } -- (void) taskDidTerminate: (NSNotification *)notification -{ - NSLog(@"Received NSTaskDidTerminateNotification %@", notification); - taskTerminationNotificationReceived = YES; -} +- (void) taskDidTerminate: (NSNotification *)notification +{ + NSLog(@"Received NSTaskDidTerminateNotification %@", notification); + taskTerminationNotificationReceived = YES; +} -- (void) testNSTaskNotifications -{ - NSDate *deadline; - BOOL earlyTermination = NO; +- (void) testNSTaskNotifications +{ + NSDate *deadline; + BOOL earlyTermination = NO; for (;;) - { - NSTask *task = [NSTask new]; + { + NSTask *task = [NSTask new]; [task setLaunchPath: path]; [task setArguments: [NSArray arrayWithObjects: - @"-c", @"echo Child starting; sleep 10; echo Child exiting", nil]]; - taskTerminationNotificationReceived = NO; + @"-c", @"echo Child starting; sleep 10; echo Child exiting", nil]]; + taskTerminationNotificationReceived = NO; [[NSNotificationCenter defaultCenter] - addObserver: self - selector: @selector(taskDidTerminate:) - name: NSTaskDidTerminateNotification - object: task]; - [task launch]; - NSLog(@"Launched pid %d", [task processIdentifier]); + addObserver: self + selector: @selector(taskDidTerminate:) + name: NSTaskDidTerminateNotification + object: task]; + [task launch]; + NSLog(@"Launched pid %d", [task processIdentifier]); if (earlyTermination) - { - NSLog(@"Running run loop for 5 seconds"); - deadline = [NSDate dateWithTimeIntervalSinceNow:5.0]; - while ([deadline timeIntervalSinceNow] > 0.0) + { + NSLog(@"Running run loop for 5 seconds"); + deadline = [NSDate dateWithTimeIntervalSinceNow:5.0]; + while ([deadline timeIntervalSinceNow] > 0.0 + && !taskTerminationNotificationReceived) { [[NSRunLoop currentRunLoop] - runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; - NSLog(@"Run loop finished, will now call -[NSTask terminate]"); - [task terminate]; - NSLog(@"Terminate returned, waiting for termination"); - [task waitUntilExit]; + runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; + NSLog(@"Run loop finished, will now call -[NSTask terminate]"); + [task terminate]; + NSLog(@"Terminate returned, waiting for termination"); + [task waitUntilExit]; + PASS([task terminationReason] + == NSTaskTerminationReasonUncaughtSignal, + "termination reason for signal exit works"); } } else - { - NSLog(@"Running run loop for 15 seconds"); - deadline = [NSDate dateWithTimeIntervalSinceNow: 15.0]; + { + NSLog(@"Running run loop for 15 seconds"); + deadline = [NSDate dateWithTimeIntervalSinceNow: 15.0]; while ([deadline timeIntervalSinceNow] > 0.0 - && !taskTerminationNotificationReceived) + && !taskTerminationNotificationReceived) { [[NSRunLoop currentRunLoop] - runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; + runUntilDate: [NSDate dateWithTimeIntervalSinceNow: 1.0]]; } - } - [task release]; + PASS([task terminationReason] + == NSTaskTerminationReasonExit, + "termination reason for normal exit works"); + } + [task release]; NSAssert(taskTerminationNotificationReceived, - @"termination notification not received"); + @"termination notification not received"); [[NSNotificationCenter defaultCenter] - removeObserver: self name: NSTaskDidTerminateNotification object: nil]; - if (earlyTermination) - break; - earlyTermination = YES; - } -} + removeObserver: self name: NSTaskDidTerminateNotification object: nil]; + if (earlyTermination) + break; + earlyTermination = YES; + } +} @end _______________________________________________ Gnustep-cvs mailing list Gnustep-cvs@gna.org https://mail.gna.org/listinfo/gnustep-cvs