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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
//
// FileSettings.m
// PythonLauncher
//
// Created by Jack Jansen on Sun Jul 21 2002.
// Copyright (c) 2002 __MyCompanyName__. All rights reserved.
//
#import "FileSettings.h"
@implementation FileSettings
+ (id)getFactorySettingsForFileType: (NSString *)filetype
{
static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc;
FileSettings **curdefault;
if ([filetype isEqualToString: @"Python Script"]) {
curdefault = &fsdefault_py;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
curdefault = &fsdefault_pyw;
} else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
curdefault = &fsdefault_pyc;
} else {
NSLog(@"Funny File Type: %@\n", filetype);
curdefault = &fsdefault_py;
filetype = @"Python Script";
}
if (!*curdefault) {
*curdefault = [[FileSettings new] initForFSDefaultFileType: filetype];
}
return *curdefault;
}
+ (id)getDefaultsForFileType: (NSString *)filetype
{
static FileSettings *default_py, *default_pyw, *default_pyc;
FileSettings **curdefault;
if ([filetype isEqualToString: @"Python Script"]) {
curdefault = &default_py;
} else if ([filetype isEqualToString: @"Python GUI Script"]) {
curdefault = &default_pyw;
} else if ([filetype isEqualToString: @"Python Bytecode Document"]) {
curdefault = &default_pyc;
} else {
NSLog(@"Funny File Type: %@\n", filetype);
curdefault = &default_py;
filetype = @"Python Script";
}
if (!*curdefault) {
*curdefault = [[FileSettings new] initForDefaultFileType: filetype];
}
return *curdefault;
}
+ (id)newSettingsForFileType: (NSString *)filetype
{
FileSettings *cur;
cur = [FileSettings new];
[cur initForFileType: filetype];
return [cur retain];
}
- (id)initWithFileSettings: (FileSettings *)source
{
self = [super init];
if (!self) return self;
interpreter = [source->interpreter retain];
honourhashbang = source->honourhashbang;
debug = source->debug;
verbose = source->verbose;
inspect = source->inspect;
optimize = source->optimize;
nosite = source->nosite;
tabs = source->tabs;
others = [source->others retain];
scriptargs = [source->scriptargs retain];
with_terminal = source->with_terminal;
prefskey = source->prefskey;
if (prefskey) [prefskey retain];
return self;
}
- (id)initForFileType: (NSString *)filetype
{
FileSettings *defaults;
defaults = [FileSettings getDefaultsForFileType: filetype];
self = [self initWithFileSettings: defaults];
origsource = [defaults retain];
return self;
}
//- (id)init
//{
// self = [self initForFileType: @"Python Script"];
// return self;
//}
- (id)initForFSDefaultFileType: (NSString *)filetype
{
int i;
NSString *filename;
NSDictionary *dict;
static NSDictionary *factorySettings;
self = [super init];
if (!self) return self;
if (factorySettings == NULL) {
NSBundle *bdl = [NSBundle mainBundle];
NSString *path = [ bdl pathForResource: @"factorySettings"
ofType: @"plist"];
factorySettings = [[NSDictionary dictionaryWithContentsOfFile:
path] retain];
if (factorySettings == NULL) {
NSLog(@"Missing %@", path);
return NULL;
}
}
dict = [factorySettings objectForKey: filetype];
if (dict == NULL) {
NSLog(@"factorySettings.plist misses file type \"%@\"", filetype);
interpreter = [@"no default found" retain];
return NULL;
}
[self applyValuesFromDict: dict];
interpreters = [dict objectForKey: @"interpreter_list"];
interpreter = NULL;
for (i=0; i < [interpreters count]; i++) {
filename = [interpreters objectAtIndex: i];
filename = [filename stringByExpandingTildeInPath];
if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) {
interpreter = [filename retain];
break;
}
}
if (interpreter == NULL)
interpreter = [@"no default found" retain];
origsource = NULL;
return self;
}
- (void)applyUserDefaults: (NSString *)filetype
{
NSUserDefaults *defaults;
NSDictionary *dict;
defaults = [NSUserDefaults standardUserDefaults];
dict = [defaults dictionaryForKey: filetype];
if (!dict)
return;
[self applyValuesFromDict: dict];
}
- (id)initForDefaultFileType: (NSString *)filetype
{
FileSettings *fsdefaults;
fsdefaults = [FileSettings getFactorySettingsForFileType: filetype];
self = [self initWithFileSettings: fsdefaults];
if (!self) return self;
interpreters = [fsdefaults->interpreters retain];
scriptargs = [@"" retain];
[self applyUserDefaults: filetype];
prefskey = [filetype retain];
return self;
}
- (void)reset
{
if (origsource) {
[self updateFromSource: origsource];
} else {
FileSettings *fsdefaults;
fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey];
[self updateFromSource: fsdefaults];
}
}
- (void)updateFromSource: (id <FileSettingsSource>)source
{
interpreter = [[source interpreter] retain];
honourhashbang = [source honourhashbang];
debug = [source debug];
verbose = [source verbose];
inspect = [source inspect];
optimize = [source optimize];
nosite = [source nosite];
tabs = [source tabs];
others = [[source others] retain];
scriptargs = [[source scriptargs] retain];
with_terminal = [source with_terminal];
// And if this is a user defaults object we also save the
// values
if (!origsource) {
NSUserDefaults *defaults;
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
interpreter, @"interpreter",
[NSNumber numberWithBool: honourhashbang], @"honourhashbang",
[NSNumber numberWithBool: debug], @"debug",
[NSNumber numberWithBool: verbose], @"verbose",
[NSNumber numberWithBool: inspect], @"inspect",
[NSNumber numberWithBool: optimize], @"optimize",
[NSNumber numberWithBool: nosite], @"nosite",
[NSNumber numberWithBool: nosite], @"nosite",
others, @"others",
scriptargs, @"scriptargs",
[NSNumber numberWithBool: with_terminal], @"with_terminal",
nil];
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: dict forKey: prefskey];
}
}
- (void)applyValuesFromDict: (NSDictionary *)dict
{
id value;
value = [dict objectForKey: @"interpreter"];
if (value) interpreter = [value retain];
value = [dict objectForKey: @"honourhashbang"];
if (value) honourhashbang = [value boolValue];
value = [dict objectForKey: @"debug"];
if (value) debug = [value boolValue];
value = [dict objectForKey: @"verbose"];
if (value) verbose = [value boolValue];
value = [dict objectForKey: @"inspect"];
if (value) inspect = [value boolValue];
value = [dict objectForKey: @"optimize"];
if (value) optimize = [value boolValue];
value = [dict objectForKey: @"nosite"];
if (value) nosite = [value boolValue];
value = [dict objectForKey: @"nosite"];
if (value) tabs = [value boolValue];
value = [dict objectForKey: @"others"];
if (value) others = [value retain];
value = [dict objectForKey: @"scriptargs"];
if (value) scriptargs = [value retain];
value = [dict objectForKey: @"with_terminal"];
if (value) with_terminal = [value boolValue];
}
- (NSString*)_replaceSingleQuotes: (NSString*)string
{
/* Replace all single-quotes by '"'"', that way shellquoting will
* be correct when the result value is delimited using single quotes.
*/
NSArray* components = [string componentsSeparatedByString:@"'"];
return [components componentsJoinedByString:@"'\"'\"'"];
}
- (NSString *)commandLineForScript: (NSString *)script
{
NSString *cur_interp = NULL;
NSString* script_dir = NULL;
char hashbangbuf[1024];
FILE *fp;
char *p;
script_dir = [script substringToIndex:
[script length]-[[script lastPathComponent] length]];
if (honourhashbang &&
(fp=fopen([script cString], "r")) &&
fgets(hashbangbuf, sizeof(hashbangbuf), fp) &&
strncmp(hashbangbuf, "#!", 2) == 0 &&
(p=strchr(hashbangbuf, '\n'))) {
*p = '\0';
p = hashbangbuf + 2;
while (*p == ' ') p++;
cur_interp = [NSString stringWithCString: p];
}
if (!cur_interp)
cur_interp = interpreter;
return [NSString stringWithFormat:
@"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s",
[self _replaceSingleQuotes:script_dir],
[self _replaceSingleQuotes:cur_interp],
debug?" -d":"",
verbose?" -v":"",
inspect?" -i":"",
optimize?" -O":"",
nosite?" -S":"",
tabs?" -t":"",
others,
[self _replaceSingleQuotes:script],
scriptargs,
with_terminal? "&& echo Exit status: $? && exit 1" : " &"];
}
- (NSArray *) interpreters { return interpreters;};
// FileSettingsSource protocol
- (NSString *) interpreter { return interpreter;};
- (BOOL) honourhashbang { return honourhashbang; };
- (BOOL) debug { return debug;};
- (BOOL) verbose { return verbose;};
- (BOOL) inspect { return inspect;};
- (BOOL) optimize { return optimize;};
- (BOOL) nosite { return nosite;};
- (BOOL) tabs { return tabs;};
- (NSString *) others { return others;};
- (NSString *) scriptargs { return scriptargs;};
- (BOOL) with_terminal { return with_terminal;};
@end
|