5 // Created by Pieter de Bie on 16-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
12 @implementation PBEasyPipe
14 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args
16 return [self handleForCommand:cmd withArgs:args inDir:nil];
19 + (NSTask *) taskForCommand:(NSString *)cmd withArgs:(NSArray *)args inDir:(NSString *)dir
21 NSTask* task = [[NSTask alloc] init];
22 task.launchPath = cmd;
23 task.arguments = args;
25 task.currentDirectoryPath = dir;
27 if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Show Debug Messages"])
28 NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
30 NSLog(@"Starting command `%@ %@` in dir %@", cmd, [args componentsJoinedByString:@" "], dir);
33 NSPipe* pipe = [NSPipe pipe];
34 task.standardOutput = pipe;
38 + (NSFileHandle*) handleForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
40 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
41 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
49 + (NSString*) outputForCommand:(NSString *) cmd
50 withArgs:(NSArray *) args
51 inDir:(NSString *) dir
54 return [self outputForCommand:cmd withArgs:args inDir:dir inputString:NULL retValue:ret];
57 // TODO: Refactor this to use the function above
58 + (NSString*) outputForCommand:(NSString *) cmd
59 withArgs:(NSArray *) args
60 inDir:(NSString *) dir
61 inputString:(NSString *)input
64 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
65 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
68 task.standardInput = [NSPipe pipe];
69 NSFileHandle *inHandle = [task.standardInput fileHandleForWriting];
70 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
76 NSData* data = [handle readDataToEndOfFile];
77 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
79 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
81 // Strip trailing newline
82 if ([string hasSuffix:@"\n"])
83 string = [string substringToIndex:[string length]-1];
87 *ret = [task terminationStatus];
91 // We don't use the above function because then we'd have to wait until the program was finished
94 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
96 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
97 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
100 #warning This can cause a "Bad file descriptor"... when?
103 data = [handle readDataToEndOfFile];
105 @catch (NSException * e) {
106 NSLog(@"Got a bad file descriptor in %s!", _cmd);
107 if ([NSThread currentThread] != [NSThread mainThread])
108 [task waitUntilExit];
112 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
114 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
116 // Strip trailing newline
117 if ([string hasSuffix:@"\n"])
118 string = [string substringToIndex:[string length]-1];
120 if ([NSThread currentThread] != [NSThread mainThread])
121 [task waitUntilExit];
127 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
129 return [self outputForCommand:cmd withArgs:args inDir:nil];