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 byExtendingEnvironment:nil inputString:nil retValue:ret];
57 + (NSString*) outputForCommand:(NSString *) cmd
58 withArgs:(NSArray *) args
59 inDir:(NSString *) dir
60 inputString:(NSString *) input
63 return [self outputForCommand:cmd withArgs:args inDir:dir byExtendingEnvironment:nil inputString:input retValue:ret];
66 + (NSString*) outputForCommand:(NSString *) cmd
67 withArgs:(NSArray *) args
68 inDir:(NSString *) dir
69 byExtendingEnvironment:(NSDictionary *)dict
70 inputString:(NSString *) input
73 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
76 NSMutableDictionary *env = [[[NSProcessInfo processInfo] environment] mutableCopy];
77 [env addEntriesFromDictionary:dict];
78 task.environment = env;
81 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
84 task.standardInput = [NSPipe pipe];
85 NSFileHandle *inHandle = [task.standardInput fileHandleForWriting];
86 [inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
92 NSData* data = [handle readDataToEndOfFile];
93 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
95 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
97 // Strip trailing newline
98 if ([string hasSuffix:@"\n"])
99 string = [string substringToIndex:[string length]-1];
101 [task waitUntilExit];
103 *ret = [task terminationStatus];
107 // We don't use the above function because then we'd have to wait until the program was finished
110 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args inDir: (NSString*) dir
112 NSTask *task = [self taskForCommand:cmd withArgs:args inDir:dir];
113 NSFileHandle* handle = [task.standardOutput fileHandleForReading];
116 #warning This can cause a "Bad file descriptor"... when?
119 data = [handle readDataToEndOfFile];
121 @catch (NSException * e) {
122 NSLog(@"Got a bad file descriptor in %s!", _cmd);
123 if ([NSThread currentThread] != [NSThread mainThread])
124 [task waitUntilExit];
128 NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
130 string = [[NSString alloc] initWithData:data encoding:NSISOLatin1StringEncoding];
132 // Strip trailing newline
133 if ([string hasSuffix:@"\n"])
134 string = [string substringToIndex:[string length]-1];
136 if ([NSThread currentThread] != [NSThread mainThread])
137 [task waitUntilExit];
143 + (NSString*) outputForCommand: (NSString*) cmd withArgs: (NSArray*) args
145 return [self outputForCommand:cmd withArgs:args inDir:nil];