-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jigish Patel
committed
Oct 17, 2012
1 parent
e3ebb14
commit 9c0906b
Showing
14 changed files
with
390 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// | ||
// ShellOperation.h | ||
// Slate | ||
// | ||
// Created by Jigish Patel on 10/17/12. | ||
// Copyright 2012 Jigish Patel. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses | ||
|
||
#import "Operation.h" | ||
|
||
@interface ShellOperation : Operation { | ||
NSString *command; | ||
NSArray *args; | ||
NSString *currentPath; | ||
BOOL waitForExit; | ||
} | ||
|
||
@property NSString *command; | ||
@property NSArray *args; | ||
@property NSString *currentPath; | ||
@property BOOL waitForExit; | ||
|
||
- (id)initWithCommand:(NSString *)theCommand args:(NSArray *)theArgs waitForExit:(BOOL)theWaitForExit currentPath:(NSString *)theCurrentPath; | ||
|
||
+ (id)shellOperationFromString:(NSString *)shellOperation; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// ShellOperation.m | ||
// Slate | ||
// | ||
// Created by Jigish Patel on 10/17/12. | ||
// Copyright 2012 Jigish Patel. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses | ||
|
||
#import "ShellOperation.h" | ||
#import "Constants.h" | ||
#import "SlateLogger.h" | ||
#import "StringTokenizer.h" | ||
#import "ShellUtils.h" | ||
|
||
@implementation ShellOperation | ||
|
||
@synthesize command, args, waitForExit, currentPath; | ||
|
||
- (id)initWithCommand:(NSString *)theCommand args:(NSArray *)theArgs waitForExit:(BOOL)theWaitForExit currentPath:(NSString *)theCurrentPath { | ||
self = [super init]; | ||
if (self) { | ||
[self setCommand:theCommand]; | ||
[self setArgs:theArgs]; | ||
[self setWaitForExit:theWaitForExit]; | ||
[self setCurrentPath:theCurrentPath]; | ||
} | ||
return self; | ||
} | ||
|
||
- (BOOL)doOperation { | ||
SlateLogger(@"----------------- Begin Shell Operation -----------------"); | ||
// We don't use the passed in AccessibilityWrapper or ScreenWrapper so they are nil. No need to waste time creating them here. | ||
BOOL success = [self doOperationWithAccessibilityWrapper:nil screenWrapper:nil]; | ||
SlateLogger(@"----------------- End Shell Operation -----------------"); | ||
return success; | ||
} | ||
|
||
- (BOOL)doOperationWithAccessibilityWrapper:(AccessibilityWrapper *)iamnil screenWrapper:(ScreenWrapper *)iamalsonil { | ||
NSTask *task = [ShellUtils run:[self command] args:[self args] wait:[self waitForExit] path:[self currentPath]]; | ||
return task != nil; | ||
} | ||
|
||
- (BOOL)testOperation { | ||
return [ShellUtils commandExists:[self command]]; | ||
} | ||
|
||
+ (id)shellOperationFromString:(NSString *)shellOperation { | ||
// shell [wait] 'command' | ||
NSMutableArray *tokens = [[NSMutableArray alloc] initWithCapacity:10]; | ||
[StringTokenizer tokenize:shellOperation into:tokens quoteChars:[NSCharacterSet characterSetWithCharactersInString:QUOTES]]; | ||
|
||
if ([tokens count] < 2) { | ||
SlateLogger(@"ERROR: Invalid Parameters '%@'", shellOperation); | ||
@throw([NSException exceptionWithName:@"Invalid Parameters" reason:[NSString stringWithFormat:@"Invalid Parameters in '%@'. Shell operations require the following format: shell [wait] 'command'", shellOperation] userInfo:nil]); | ||
} | ||
|
||
BOOL waitForExit = NO; | ||
NSString *currentPath = nil; | ||
for (NSInteger i = 1; i < [tokens count] - 1; i++) { | ||
if ([[tokens objectAtIndex:i] isEqualToString:WAIT]) { | ||
waitForExit = YES; | ||
} else if ([[tokens objectAtIndex:i] hasPrefix:PATH]) { | ||
currentPath = [[tokens objectAtIndex:i] stringByReplacingOccurrencesOfString:PATH withString:EMPTY]; | ||
if ([currentPath hasPrefix:TILDA]) { | ||
currentPath = [currentPath stringByExpandingTildeInPath]; | ||
} | ||
} | ||
} | ||
NSString *commandAndArgs = [tokens lastObject]; | ||
NSMutableArray *commandAndArgsTokens = [NSMutableArray array]; | ||
[StringTokenizer tokenize:commandAndArgs into:commandAndArgsTokens]; | ||
if ([commandAndArgsTokens count] < 1) { | ||
SlateLogger(@"ERROR: Invalid Parameters '%@'", shellOperation); | ||
@throw([NSException exceptionWithName:@"Invalid Parameters" reason:[NSString stringWithFormat:@"Invalid Parameters in '%@'. Shell operations require the following format: shell [wait] 'command'", shellOperation] userInfo:nil]); | ||
} | ||
NSString *command = [commandAndArgsTokens objectAtIndex:0]; | ||
NSMutableArray *args = [NSMutableArray array]; | ||
for (NSInteger i = 1; i < [commandAndArgsTokens count]; i++) { | ||
[args addObject:[commandAndArgsTokens objectAtIndex:i]]; | ||
} | ||
|
||
Operation *op = [[ShellOperation alloc] initWithCommand:command args:args waitForExit:waitForExit currentPath:currentPath]; | ||
return op; | ||
|
||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// | ||
// ShellUtils.h | ||
// Slate | ||
// | ||
// Created by Jigish Patel on 10/17/12. | ||
// Copyright 2012 Jigish Patel. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface ShellUtils : NSObject | ||
|
||
+ (BOOL)commandExists:(NSString *)command; | ||
+ (NSTask *)run:(NSString *)command args:(NSArray *)args wait:(BOOL)wait path:(NSString *)path; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// ShellUtils.m | ||
// Slate | ||
// | ||
// Created by Jigish Patel on 10/17/12. | ||
// Copyright 2012 Jigish Patel. All rights reserved. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses | ||
|
||
#import "ShellUtils.h" | ||
#import "Constants.h" | ||
|
||
@implementation ShellUtils | ||
|
||
+ (BOOL)commandExists:(NSString *)command { | ||
if (command == nil || [EMPTY isEqualToString:command]) return NO; | ||
@try { | ||
NSTask *task; | ||
task = [[NSTask alloc] init]; | ||
[task setLaunchPath:@"/usr/bin/command"]; | ||
|
||
NSArray *arguments; | ||
arguments = [NSArray arrayWithObjects:@"-v", command, nil]; | ||
[task setArguments:arguments]; | ||
|
||
NSPipe *pipe; | ||
pipe = [NSPipe pipe]; | ||
[task setStandardOutput:pipe]; | ||
[task setStandardInput:[NSPipe pipe]]; | ||
|
||
NSFileHandle *file; | ||
file = [pipe fileHandleForReading]; | ||
|
||
[task launch]; | ||
|
||
NSData *data; | ||
data = [file readDataToEndOfFile]; | ||
|
||
NSString *string; | ||
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; | ||
if ([string isEqualToString:EMPTY]) { | ||
return NO; | ||
} | ||
return YES; | ||
} @catch (id ex) { | ||
return NO; | ||
} | ||
} | ||
|
||
+ (NSTask *)run:(NSString *)command args:(NSArray *)args wait:(BOOL)wait path:(NSString *)path { | ||
NSTask *task; | ||
task = [[NSTask alloc] init]; | ||
[task setLaunchPath:command]; | ||
[task setArguments:args]; | ||
if (path != nil) [task setCurrentDirectoryPath:path]; | ||
|
||
NSPipe *pipe; | ||
pipe = [NSPipe pipe]; | ||
[task setStandardOutput:pipe]; | ||
[task setStandardInput:[NSPipe pipe]]; | ||
|
||
NSFileHandle *file; | ||
file = [pipe fileHandleForReading]; | ||
|
||
[task launch]; | ||
if (wait) [task waitUntilExit]; | ||
return task; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
9c0906b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm misreading this completely, your implementation of
commandExists
actually runs the command?Surely you should just be searching
PATH
for an executable file with that name, or testing for the file's existance if it's name contains a slash?9c0906b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Edit, I misread this. You're shelling out to call command, but this is probably still not a great idea. The original comment doesn't make sense though. Disregard :)