-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSCLExecutableProvider.m
302 lines (248 loc) · 10.9 KB
/
QSCLExecutableProvider.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#import "QSCLExecutableProvider.h"
#import "QSTerminalMediator.h"
#include "QSTerminalPlugIn.h"
//#define kQSCLExecuteAction @"ShellScriptRunAction"
#define kQSCLExecuteWithArgsAction @"QSShellScriptRunAction"
//#define kQSCLTermExecuteAction @"QSCLTermExecuteAction"
#define kQSCLTermExecuteWithArgsAction @"QSCLTermExecuteWithArgsAction"
#define kQSCLTermExecuteWithRequiredArgsAction @"QSCLTermExecuteWithRequiredArgsAction"
#define kQSCLTermShowDirectoryAction @"QSCLTermShowDirectoryAction"
#define kQSCLTermShowManPageAction @"QSCLTermShowManPageAction"
#define kQSCLTermOpenParentAction @"QSCLTermOpenParentAction"
#define kQSShellScriptRunWithArgsAction @"QSShellScriptRunWithArgsAction"
#define kQSCLExecuteTextAction @"QSCLExecuteTextAction"
#define kQSCLTermExecuteTextAction @"QSCLTermExecuteTextAction"
@implementation QSCLExecutableProvider
- (NSArray *)validActionsForDirectObject:(QSObject *)dObject indirectObject:(QSObject *)iObject
{
if ([dObject objectForType:QSFilePathType])
{
NSString *path = [dObject singleFilePath];
if (!path) return nil;
BOOL isDirectory = NO;
if (![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDirectory]) return nil;
if (isDirectory) return [NSArray arrayWithObject:kQSCLTermShowDirectoryAction];
if ([dObject isExecutable] || UTTypeConformsTo((CFStringRef)[dObject fileUTI], (CFStringRef)@"public.script") || UTTypeConformsTo((CFStringRef)[dObject fileUTI], (CFStringRef)@"public.executable"))
{
BOOL executable = [[NSFileManager defaultManager] isExecutableFileAtPath:path];
if (!executable) {
NSFileHandle * fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
// Read in the first 5 bytes of the file to see if it contains #! (5 bytes, because some files contain byte order marks (3 bytes)
NSData * buffer = [fileHandle readDataOfLength:5];
NSString *string = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
if ([string containsString:@"#!"]) {
executable = YES;
}
[string release];
} else {
if ([dObject isApplication]) // Ignore applications
return NO;
}
if (executable) {
return [NSArray arrayWithObjects:kQSCLExecuteWithArgsAction, kQSCLTermExecuteWithArgsAction, kQSCLTermExecuteWithRequiredArgsAction, kQSCLTermShowManPageAction, kQSCLTermOpenParentAction,kQSShellScriptRunWithArgsAction, nil];
}
}
return [NSArray arrayWithObject:kQSCLTermOpenParentAction];
}
return nil;
}
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject{
QSObject *proxy=[QSObject textProxyObjectWithDefaultValue:@""];
return [NSArray arrayWithObject:proxy];
}
- (QSObject *) executeObject:(QSObject *)dObject arguments:(QSObject *)iObject{
NSString *result=[self runExecutable:[(QSObject *)dObject singleFilePath] withArguments:[iObject stringValue] inTerminal:NO];
if ([result length]) return [QSObject objectWithString:result];
return nil;
}
- (QSObject *) executeObjectInTerm:(QSObject *)dObject arguments:(QSObject *)iObject{
NSString *result=[self runExecutable:[(QSObject *)dObject singleFilePath] withArguments:[iObject stringValue] inTerminal:YES];
if ([result length]) return [QSObject objectWithString:result];
return nil;
}
- (NSString *)runExecutable:(NSString *)path withArguments:(NSString *)arguments inTerminal:(BOOL)inTerminal{
BOOL executable=[[NSFileManager defaultManager] isExecutableFileAtPath:path];
NSString *taskPath=path;
NSMutableArray *argArray=[NSMutableArray array];
NSCharacterSet *ws = [NSCharacterSet whitespaceCharacterSet];
if (!executable) {
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];
if (!fileHandle)
return nil;
NSData *buffer = [fileHandle readDataOfLength:1024];
[fileHandle closeFile];
NSString *contents = [[NSString alloc] initWithData:buffer encoding:NSUTF8StringEncoding];
NSScanner *scanner = [NSScanner scannerWithString:contents];
[contents release];
/* Doesn't start with a shebang, there's nothing we can do */
if (![scanner scanString:@"#!" intoString:nil]) {
return nil;
}
/* Scan the shebang line */
NSString *shellLine = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet newlineCharacterSet] intoString:&shellLine];
/* Split up the executable path and its parameters... */
NSArray *shellLineParameters = [shellLine componentsSeparatedByCharactersInSet:ws];
taskPath = [shellLineParameters head];
[argArray addObjectsFromArray:[shellLineParameters tail]];
/* ... and append the path to the actual file to execute */
[argArray addObject:path];
}
// include arguments for the command
if ([arguments length]) {
NSArray *commandArguments = [arguments componentsSeparatedByCharactersInSet:ws];
[argArray addObjectsFromArray:commandArguments];
}
if (inTerminal) {
NSString *fullCommand=[NSString stringWithFormat:@"%@ %@",[self escapeString:taskPath],[argArray componentsJoinedByString:@" "]];
[self performCommandInTerminal:fullCommand];
/// NSLog(@"Run Shell Script: %@",fullCommand);
}else{
NSTask *task=[[[NSTask alloc]init]autorelease];
[task setLaunchPath:taskPath];
[task setArguments:argArray];
[task setStandardOutput:[NSPipe pipe]];
[task launch];
[task waitUntilExit];
// NSLog(@"Run Task: %@ %@",taskPath,argArray);
NSString *string=[[[NSString alloc] initWithData:[[[task standardOutput]fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding]autorelease];
int status = [task terminationStatus];
if (status == 0) NSLog(@"Task succeeded.");
else NSLog(@"Task failed.");
return string;
}
return nil;
}
- (NSString *)escapeString:(NSString *)string{
NSString *escapeString=@"\\!$&\"'*(){[|;<>?~` ";
for (NSUInteger i = 0; i < [escapeString length]; i++) {
NSString *thisString=[escapeString substringWithRange:NSMakeRange(i,1)];
string=[[string componentsSeparatedByString:thisString]componentsJoinedByString:[@"\\" stringByAppendingString:thisString]];
}
return string;
}
- (QSObject *) showManPage:(QSObject *)dObject{
NSString *path=[dObject singleFilePath];
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"x-man-page://%@",[path lastPathComponent]]]];
return nil;
}
- (QSObject *) executeText:(QSObject *)dObject{
NSString *string=[dObject objectForType:QSShellCommandType];
if (!string)string=[dObject stringValue];
if ([string rangeOfString:@"sudo" options:NSCaseInsensitiveSearch].location!=NSNotFound){
#ifdef DEBUG
NSLog(@"sudo in %@",string);
#endif
if (![self sudoIfNeeded]){
NSBeep();
return nil;
}
}
NSTask *task=[[[NSTask alloc]init]autorelease];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:@"-c",string,nil]];
[task setStandardOutput:[NSPipe pipe]];
[task launch];
[task waitUntilExit];
NSString *result=[[[NSString alloc] initWithData:[[[task standardOutput]fileHandleForReading] readDataToEndOfFile] encoding:NSUTF8StringEncoding]autorelease];
int status = [task terminationStatus];
if (status == 0) NSLog(@"Task succeeded.");
else NSLog(@"Task failed.");
if ([result length])
return [QSObject objectWithString:result];
return nil;
}
- (QSObject *) executeTextInTerminal:(QSObject *)dObject{
NSString *string=[dObject objectForType:QSShellCommandType];
if (!string)string=[dObject stringValue];
[self performCommandInTerminal:string];
return nil;
}
- (void)openTerminalAtDirectory:(NSString *)path
{
BOOL isDir = NO;
while (!([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && isDir))
{
NSArray *comps = [path pathComponents];
path = [NSString pathWithComponents:[comps subarrayWithRange:(NSRange){0, [comps count] - 1}]];
}
[self performCommandInTerminal:[NSString stringWithFormat:@"cd %@", [self escapeString:path]]];
}
-(void)displayOpenTerminalError {
NSBeep();
QSShowAppNotifWithAttributes(@"TerminalNotif", NSLocalizedStringFromTableInBundle(@"Terminal Plugin", nil, [NSBundle bundleForClass:[self class]], @""), NSLocalizedStringFromTableInBundle(@"Could not open directory", nil, [NSBundle bundleForClass:[self class]], @""));
}
- (QSObject *)showParentDirectoryInTerminal:(QSObject *)dObject
{
NSString *path = [dObject singleFilePath];
if (!path) {
[self displayOpenTerminalError];
return nil;
}
NSArray *comps = [path pathComponents];
if ([comps count] > 1) path = [NSString pathWithComponents:[comps subarrayWithRange:(NSRange){0, [comps count] - 1}]];
[self openTerminalAtDirectory:path];
return nil;
}
- (QSObject *)showDirectoryInTerminal:(QSObject *)dObject
{
NSString *path = [dObject singleFilePath];
if (!path) {
[self displayOpenTerminalError];
return nil;
}
[self openTerminalAtDirectory:path];
return nil;
}
- (void)performCommandInTerminal:(NSString *)command{
[[QSReg preferredTerminalMediator] performCommandInTerminal:(NSString *)command];
}
- (void)ok:(id)sender{[NSApp stopModalWithCode:1];}
- (void)cancel:(id)sender{[NSApp stopModalWithCode:0];}
- (BOOL)sudoIfNeeded{
int status=1;
while (status) {
NSTask *task=[NSTask taskWithLaunchPath:@"/usr/bin/sudo" arguments:[NSArray arrayWithObjects:@"-v",@"-S",nil]];
[task setStandardInput:[NSPipe pipe]];
[task setStandardError:[NSPipe pipe]];
[task launch];
NSData *data= [[[task standardError] fileHandleForReading] availableData];
// Password is required
if ([data length]) {
__block NSModalResponse result = 0;
__block NSString *string = nil;
QSGCDMainSync(^{
if (!window) {
[NSBundle loadNibNamed:@"QSSudoPasswordAlert" owner:self];
}
[window makeKeyAndOrderFront:self];
result = [NSApp runModalForWindow:window];
if (result) {
// Obtains the password from the window, initial first responder is always the secure text field
string = [[[(NSSecureTextField *)[window initialFirstResponder] stringValue] stringByAppendingString:@"\n"] copy];
}
[window close];
});
if (!result){
[task interrupt];
return NO;
}
NSData *writeData = [string dataUsingEncoding:NSUTF8StringEncoding];
[[[task standardInput]fileHandleForWriting] writeData:writeData];
[[[task standardInput]fileHandleForWriting] closeFile];
}
usleep(250000);
if ([task isRunning])
[task interrupt];
else
status=[task terminationStatus];
if (status)
NSBeep();
//NSLog(@"term %d",status);
}
return !status;
}
- (void)setQuickIconForObject:(QSObject *)object{
[object setIcon:[QSResourceManager imageNamed:@"ExecutableBinaryIcon"]];
}
@end