Changeset 88

Show
Ignore:
Timestamp:
12/27/05 19:13:01 (3 years ago)
Author:
mrowe
Message:

Primarily cosmetic changes to SSHTool. One minor functional change is that handleTerminateWithSelector:toObject:withInfo: now retains both theObject and theInfo to prevent them from disappearing out from under it. They are released when the task termination notification comes in.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Libs/SSHTool.m

    r41 r88  
    1010+ (id)toolWithName:(NSString *)toolname 
    1111{ 
    12         SSHTool *tool = [[[self alloc] init] autorelease]; 
    13          
    14         [tool setPath:[[[NSUserDefaults standardUserDefaults]  
    15                         stringForKey:SSHToolsPathString]  
    16                         stringByAppendingPathComponent:toolname]]; 
    17          
    18         return tool; 
     12        return [self toolWithPath:[[[NSUserDefaults standardUserDefaults]  
     13                                        stringForKey:SSHToolsPathString]  
     14                                        stringByAppendingPathComponent:toolname]]; 
    1915} 
    2016 
     
    2319{ 
    2420        SSHTool *tool = [[[self alloc] init] autorelease]; 
    25          
    2621        [tool setPath:path]; 
    27          
    2822        return tool; 
    2923} 
     
    3125- (id)init 
    3226{ 
    33         if(self = [super init]) 
    34         { 
    35                 task = [[NSTask alloc] init]; 
    36                 [task setStandardInput:[NSFileHandle fileHandleWithNullDevice]]; 
    37                 toolPath = nil; 
    38         } 
     27        if (! (self = [super init])) 
     28                return nil; 
     29                 
     30        task = [[NSTask alloc] init]; 
     31        [task setStandardInput:[NSFileHandle fileHandleWithNullDevice]]; 
    3932 
    4033        return self; 
     
    4740        [task release]; 
    4841        [toolPath release]; 
     42        [terminateObject release]; 
     43        [terminateInfo release]; 
    4944         
    5045        [super dealloc]; 
     
    5449- (void)setPath:(NSString *)path 
    5550{ 
    56         [toolPath autorelease]
     51        NSString *oldPath = toolPath
    5752        toolPath = [path copy]; 
     53        [oldPath release]; 
    5854} 
    5955 
     
    7975- (void)setEnvironmentVariable:(NSString *)variable withValue:(NSString *)value 
    8076{ 
    81         NSMutableDictionary *env = [NSMutableDictionary dictionaryWithDictionary:[task environment]]; 
     77        if (!variable || !value) 
     78                return; 
    8279 
    83         if((variable) && (value)) 
    84         { 
    85                 [env setObject:value forKey:variable]; 
    86                 [task setEnvironment:env]; 
    87         } 
     80        NSMutableDictionary *env = [[task environment] mutableCopy]; 
     81        [env setObject:value forKey:variable]; 
     82        [task setEnvironment:env]; 
     83        [env release]; 
    8884} 
    8985 
     
    9288{ 
    9389        NSPipe *thePipe = [[[NSPipe alloc] init] autorelease]; 
    94         NSString *theOutput; 
    9590 
    9691        [task setStandardOutput:thePipe]; 
    9792 
    98         if([self launchAndWait] == NO) 
    99         { 
     93        if (![self launchAndWait]) 
    10094                return nil; 
    101         } 
    102  
    103         /* Retrieve the stdout as a NSPipe. */ 
    104         thePipe = [task standardOutput]; 
    10595 
    10696        /* Put the data from thePipe to theOutput. */ 
    107         theOutput = [[[NSString alloc] initWithData:[[thePipe fileHandleForReading] readDataToEndOfFile] encoding:NSASCIIStringEncoding] autorelease]; 
    108  
    109         return theOutput; 
     97        NSData *theOutput = [[thePipe fileHandleForReading] readDataToEndOfFile]; 
     98        return [[[NSString alloc] initWithData:theOutput encoding:NSUTF8StringEncoding] autorelease]; 
    11099} 
    111100 
     
    113102- (BOOL)launchAndWait 
    114103{ 
    115         if(![self launch]) 
    116         { 
     104        if (![self launch]) 
    117105                return NO; 
    118         } 
    119106         
    120107        [task waitUntilExit]; 
    121          
    122         if([task terminationStatus] == 0) 
    123         { 
    124                 return YES; 
    125         } 
    126          
    127         else 
    128         { 
    129                 return NO; 
    130         } 
     108        return ![task terminationStatus]; 
    131109} 
    132110 
     
    135113{ 
    136114        /* Let's see if the path is accessible. */ 
    137         if(![[NSFileManager defaultManager] isExecutableFileAtPath:toolPath]) 
    138         { 
     115        if (![[NSFileManager defaultManager] isExecutableFileAtPath:toolPath]) 
    139116                return NO; 
    140         } 
    141117 
    142118        /* Set the launchpath to the path instance variable. */ 
     
    147123                [task launch]; 
    148124        NS_HANDLER 
    149                 if([[localException name] isEqualToString:NSInvalidArgumentException]) 
    150                 { 
     125                if ([[localException name] isEqualToString:NSInvalidArgumentException]) 
    151126                        retValue = NO; 
    152                 } 
    153127                 
    154128                else 
    155                 { 
    156129                        [localException raise]; 
    157                 } 
    158130        NS_ENDHANDLER 
    159131         
     
    170142- (void)handleTerminateWithSelector:(SEL)theSelector toObject:(id)theObject withInfo:(id)theInfo 
    171143{ 
    172         if((!theSelector) || (!theObject))  
    173         { 
     144        if (!theSelector || !theObject)  
    174145                return; 
    175         } 
     146 
     147        terminateSelector = theSelector; 
     148        terminateObject = [theObject retain]; 
    176149         
    177         terminateSelector = theSelector; 
    178         terminateObject = theObject; 
    179          
    180         if(theInfo) 
    181         { 
     150        if (theInfo) 
    182151                terminateInfo = theInfo; 
    183         } 
    184          
    185152        else 
    186         { 
    187153                terminateInfo = self; 
    188         } 
    189          
     154 
     155        [terminateInfo retain]; 
    190156        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(terminatedTaskNotification:) 
    191157                                                name:NSTaskDidTerminateNotification object:nil]; 
     
    195161- (void)terminatedTaskNotification:(NSNotification *)notification 
    196162{ 
    197         if([notification object] == task) 
     163        if ([notification object] == task) 
    198164        { 
    199165                [terminateObject performSelector:terminateSelector withObject:terminateInfo]; 
     166                [terminateObject release]; 
     167                [terminateInfo release]; 
     168                terminateObject = nil; 
     169                terminateInfo = nil; 
    200170        } 
    201171}