From 80e609859d582b518b29735ad5314a687ee10abb Mon Sep 17 00:00:00 2001 From: Bibiko Date: Sat, 6 Feb 2010 12:29:56 +0000 Subject: =?UTF-8?q?=E2=80=A2=20further=20preparations=20to=20allow=20to=20?= =?UTF-8?q?run=20any=20bash=20command(s)=20incl.=20any=20scripts=20like=20?= =?UTF-8?q?Perl,=20Ruby,=20AppleScript=20etc=20inside=20the=20text=20macro?= =?UTF-8?q?=20snippets=20of=20the=20query=20favorites=20and=20insert=20the?= =?UTF-8?q?=20result=20for=20such=20a=20bash=20command=20as=20snippet=20?= =?UTF-8?q?=C3=A0=20la:=20SELECT=20${1:$(cat=20~/Desktop/foo.txt)}=20or=20?= =?UTF-8?q?${1:$(open=20'http://www.sequelpro.com')}=20-=20such=20a=20task?= =?UTF-8?q?=20can=20be=20interrupted=20by=20pressing=20=E2=8C=98=20+=20.?= =?UTF-8?q?=20anytime=20-=20if=20the=20task=20doesn't=20exit=20successfull?= =?UTF-8?q?y=20the=20error=20will=20be=20written=20into=20the=20the=20stan?= =?UTF-8?q?dard=20Console=20log=20-=20not=20yet=20activate?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/CMTextView.m | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'Source/CMTextView.m') diff --git a/Source/CMTextView.m b/Source/CMTextView.m index 33d56544..37b2ee13 100644 --- a/Source/CMTextView.m +++ b/Source/CMTextView.m @@ -1259,6 +1259,63 @@ NSInteger alphabeticSort(id string1, id string2, void *reverse) } +/* + * Run 'command' as BASH script and return the result. + * This task can be interrupted by pressing ⌘. + */ +- (NSString *)runBashCommand:(NSString *)command +{ + + NSTask *bashTask = [[NSTask alloc] init]; + [bashTask setLaunchPath: @"/bin/sh"]; + + NSArray *arguments; + arguments = [NSArray arrayWithObjects: @"-c", command, nil]; + [bashTask setArguments: arguments]; + + NSPipe *pipe; + pipe = [NSPipe pipe]; + [bashTask setStandardOutput: pipe]; + + NSFileHandle *file; + file = [pipe fileHandleForReading]; + + [bashTask launch]; + while([bashTask isRunning]) { + NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask + untilDate:[NSDate distantFuture] + inMode:NSDefaultRunLoopMode + dequeue:YES]; + if(!event) continue; + if ([event type] == NSKeyDown) { + unichar key = [[event characters] length] == 1 ? [[event characters] characterAtIndex:0] : 0; + if (([event modifierFlags] & NSCommandKeyMask) && key == '.') { + [bashTask terminate]; + break; + } + } + usleep(10000); + } + [bashTask waitUntilExit]; + NSInteger status = [bashTask terminationStatus]; + NSData *data; + data = [file readDataToEndOfFile]; + + NSString *string; + string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; + [bashTask release]; + + if (status == 0) { + return [string autorelease]; + } else { + NSLog(@"Error: %@", string); + [string autorelease]; + NSBeep(); + return @""; + } + + +} /* * Checks whether the current caret position in inside of a defined snippet range */ -- cgit v1.2.3