Changeset 88
- Timestamp:
- 12/27/05 19:13:01 (3 years ago)
- Files:
-
- trunk/Libs/SSHTool.m (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Libs/SSHTool.m
r41 r88 10 10 + (id)toolWithName:(NSString *)toolname 11 11 { 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]]; 19 15 } 20 16 … … 23 19 { 24 20 SSHTool *tool = [[[self alloc] init] autorelease]; 25 26 21 [tool setPath:path]; 27 28 22 return tool; 29 23 } … … 31 25 - (id)init 32 26 { 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]]; 39 32 40 33 return self; … … 47 40 [task release]; 48 41 [toolPath release]; 42 [terminateObject release]; 43 [terminateInfo release]; 49 44 50 45 [super dealloc]; … … 54 49 - (void)setPath:(NSString *)path 55 50 { 56 [toolPath autorelease];51 NSString *oldPath = toolPath; 57 52 toolPath = [path copy]; 53 [oldPath release]; 58 54 } 59 55 … … 79 75 - (void)setEnvironmentVariable:(NSString *)variable withValue:(NSString *)value 80 76 { 81 NSMutableDictionary *env = [NSMutableDictionary dictionaryWithDictionary:[task environment]]; 77 if (!variable || !value) 78 return; 82 79 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]; 88 84 } 89 85 … … 92 88 { 93 89 NSPipe *thePipe = [[[NSPipe alloc] init] autorelease]; 94 NSString *theOutput;95 90 96 91 [task setStandardOutput:thePipe]; 97 92 98 if([self launchAndWait] == NO) 99 { 93 if (![self launchAndWait]) 100 94 return nil; 101 }102 103 /* Retrieve the stdout as a NSPipe. */104 thePipe = [task standardOutput];105 95 106 96 /* 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]; 110 99 } 111 100 … … 113 102 - (BOOL)launchAndWait 114 103 { 115 if(![self launch]) 116 { 104 if (![self launch]) 117 105 return NO; 118 }119 106 120 107 [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]; 131 109 } 132 110 … … 135 113 { 136 114 /* Let's see if the path is accessible. */ 137 if(![[NSFileManager defaultManager] isExecutableFileAtPath:toolPath]) 138 { 115 if (![[NSFileManager defaultManager] isExecutableFileAtPath:toolPath]) 139 116 return NO; 140 }141 117 142 118 /* Set the launchpath to the path instance variable. */ … … 147 123 [task launch]; 148 124 NS_HANDLER 149 if([[localException name] isEqualToString:NSInvalidArgumentException]) 150 { 125 if ([[localException name] isEqualToString:NSInvalidArgumentException]) 151 126 retValue = NO; 152 }153 127 154 128 else 155 {156 129 [localException raise]; 157 }158 130 NS_ENDHANDLER 159 131 … … 170 142 - (void)handleTerminateWithSelector:(SEL)theSelector toObject:(id)theObject withInfo:(id)theInfo 171 143 { 172 if((!theSelector) || (!theObject)) 173 { 144 if (!theSelector || !theObject) 174 145 return; 175 } 146 147 terminateSelector = theSelector; 148 terminateObject = [theObject retain]; 176 149 177 terminateSelector = theSelector; 178 terminateObject = theObject; 179 180 if(theInfo) 181 { 150 if (theInfo) 182 151 terminateInfo = theInfo; 183 }184 185 152 else 186 {187 153 terminateInfo = self; 188 } 189 154 155 [terminateInfo retain]; 190 156 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(terminatedTaskNotification:) 191 157 name:NSTaskDidTerminateNotification object:nil]; … … 195 161 - (void)terminatedTaskNotification:(NSNotification *)notification 196 162 { 197 if ([notification object] == task)163 if ([notification object] == task) 198 164 { 199 165 [terminateObject performSelector:terminateSelector withObject:terminateInfo]; 166 [terminateObject release]; 167 [terminateInfo release]; 168 terminateObject = nil; 169 terminateInfo = nil; 200 170 } 201 171 }
