Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 1 | // |
| 2 | // FileSettings.m |
| 3 | // PythonLauncher |
| 4 | // |
| 5 | // Created by Jack Jansen on Sun Jul 21 2002. |
| 6 | // Copyright (c) 2002 __MyCompanyName__. All rights reserved. |
| 7 | // |
| 8 | |
| 9 | #import "FileSettings.h" |
| 10 | |
| 11 | @implementation FileSettings |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 12 | |
| 13 | + (id)getFactorySettingsForFileType: (NSString *)filetype |
| 14 | { |
| 15 | static FileSettings *fsdefault_py, *fsdefault_pyw, *fsdefault_pyc; |
| 16 | FileSettings **curdefault; |
| 17 | |
| 18 | if ([filetype isEqualToString: @"Python Script"]) { |
| 19 | curdefault = &fsdefault_py; |
| 20 | } else if ([filetype isEqualToString: @"Python GUI Script"]) { |
| 21 | curdefault = &fsdefault_pyw; |
| 22 | } else if ([filetype isEqualToString: @"Python Bytecode Document"]) { |
| 23 | curdefault = &fsdefault_pyc; |
| 24 | } else { |
| 25 | NSLog(@"Funny File Type: %@\n", filetype); |
| 26 | curdefault = &fsdefault_py; |
| 27 | filetype = @"Python Script"; |
| 28 | } |
| 29 | if (!*curdefault) { |
| 30 | *curdefault = [[FileSettings new] initForFSDefaultFileType: filetype]; |
| 31 | } |
| 32 | return *curdefault; |
| 33 | } |
| 34 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 35 | + (id)getDefaultsForFileType: (NSString *)filetype |
| 36 | { |
| 37 | static FileSettings *default_py, *default_pyw, *default_pyc; |
| 38 | FileSettings **curdefault; |
| 39 | |
| 40 | if ([filetype isEqualToString: @"Python Script"]) { |
| 41 | curdefault = &default_py; |
| 42 | } else if ([filetype isEqualToString: @"Python GUI Script"]) { |
| 43 | curdefault = &default_pyw; |
| 44 | } else if ([filetype isEqualToString: @"Python Bytecode Document"]) { |
| 45 | curdefault = &default_pyc; |
| 46 | } else { |
| 47 | NSLog(@"Funny File Type: %@\n", filetype); |
| 48 | curdefault = &default_py; |
| 49 | filetype = @"Python Script"; |
| 50 | } |
| 51 | if (!*curdefault) { |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 52 | *curdefault = [[FileSettings new] initForDefaultFileType: filetype]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 53 | } |
| 54 | return *curdefault; |
| 55 | } |
| 56 | |
| 57 | + (id)newSettingsForFileType: (NSString *)filetype |
| 58 | { |
| 59 | FileSettings *cur; |
| 60 | |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 61 | cur = [FileSettings new]; |
| 62 | [cur initForFileType: filetype]; |
| 63 | return [cur retain]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | - (id)initWithFileSettings: (FileSettings *)source |
| 67 | { |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 68 | self = [super init]; |
| 69 | if (!self) return self; |
| 70 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 71 | interpreter = [source->interpreter retain]; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 72 | honourhashbang = source->honourhashbang; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 73 | debug = source->debug; |
| 74 | verbose = source->verbose; |
| 75 | inspect = source->inspect; |
| 76 | optimize = source->optimize; |
| 77 | nosite = source->nosite; |
| 78 | tabs = source->tabs; |
| 79 | others = [source->others retain]; |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 80 | scriptargs = [source->scriptargs retain]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 81 | with_terminal = source->with_terminal; |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 82 | prefskey = source->prefskey; |
| 83 | if (prefskey) [prefskey retain]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 84 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 85 | return self; |
| 86 | } |
| 87 | |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 88 | - (id)initForFileType: (NSString *)filetype |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 89 | { |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 90 | FileSettings *defaults; |
| 91 | |
| 92 | defaults = [FileSettings getDefaultsForFileType: filetype]; |
| 93 | self = [self initWithFileSettings: defaults]; |
| 94 | origsource = [defaults retain]; |
| 95 | return self; |
| 96 | } |
| 97 | |
| 98 | //- (id)init |
| 99 | //{ |
| 100 | // self = [self initForFileType: @"Python Script"]; |
| 101 | // return self; |
| 102 | //} |
| 103 | |
| 104 | - (id)initForFSDefaultFileType: (NSString *)filetype |
| 105 | { |
| 106 | int i; |
| 107 | NSString *filename; |
| 108 | NSDictionary *dict; |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 109 | static NSDictionary *factorySettings; |
| 110 | |
| 111 | self = [super init]; |
| 112 | if (!self) return self; |
| 113 | |
| 114 | if (factorySettings == NULL) { |
| 115 | NSBundle *bdl = [NSBundle mainBundle]; |
| 116 | NSString *path = [ bdl pathForResource: @"factorySettings" |
| 117 | ofType: @"plist"]; |
| 118 | factorySettings = [[NSDictionary dictionaryWithContentsOfFile: |
| 119 | path] retain]; |
| 120 | if (factorySettings == NULL) { |
| 121 | NSLog(@"Missing %@", path); |
| 122 | return NULL; |
| 123 | } |
| 124 | } |
| 125 | dict = [factorySettings objectForKey: filetype]; |
| 126 | if (dict == NULL) { |
| 127 | NSLog(@"factorySettings.plist misses file type \"%@\"", filetype); |
| 128 | interpreter = [@"no default found" retain]; |
| 129 | return NULL; |
| 130 | } |
| 131 | [self applyValuesFromDict: dict]; |
| 132 | interpreters = [dict objectForKey: @"interpreter_list"]; |
| 133 | interpreter = NULL; |
| 134 | for (i=0; i < [interpreters count]; i++) { |
| 135 | filename = [interpreters objectAtIndex: i]; |
| 136 | filename = [filename stringByExpandingTildeInPath]; |
| 137 | if ([[NSFileManager defaultManager] fileExistsAtPath: filename]) { |
| 138 | interpreter = [filename retain]; |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | if (interpreter == NULL) |
| 143 | interpreter = [@"no default found" retain]; |
| 144 | origsource = NULL; |
| 145 | return self; |
| 146 | } |
| 147 | |
| 148 | - (void)applyUserDefaults: (NSString *)filetype |
| 149 | { |
| 150 | NSUserDefaults *defaults; |
| 151 | NSDictionary *dict; |
| 152 | |
| 153 | defaults = [NSUserDefaults standardUserDefaults]; |
| 154 | dict = [defaults dictionaryForKey: filetype]; |
| 155 | if (!dict) |
| 156 | return; |
| 157 | [self applyValuesFromDict: dict]; |
| 158 | } |
| 159 | |
| 160 | - (id)initForDefaultFileType: (NSString *)filetype |
| 161 | { |
| 162 | FileSettings *fsdefaults; |
| 163 | |
| 164 | fsdefaults = [FileSettings getFactorySettingsForFileType: filetype]; |
| 165 | self = [self initWithFileSettings: fsdefaults]; |
| 166 | if (!self) return self; |
Jack Jansen | f044e09 | 2002-12-26 22:10:53 +0000 | [diff] [blame] | 167 | interpreters = [fsdefaults->interpreters retain]; |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 168 | scriptargs = [@"" retain]; |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 169 | [self applyUserDefaults: filetype]; |
| 170 | prefskey = [filetype retain]; |
| 171 | return self; |
| 172 | } |
| 173 | |
| 174 | - (void)reset |
| 175 | { |
| 176 | if (origsource) { |
| 177 | [self updateFromSource: origsource]; |
| 178 | } else { |
| 179 | FileSettings *fsdefaults; |
| 180 | fsdefaults = [FileSettings getFactorySettingsForFileType: prefskey]; |
| 181 | [self updateFromSource: fsdefaults]; |
| 182 | } |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | - (void)updateFromSource: (id <FileSettingsSource>)source |
| 186 | { |
| 187 | interpreter = [[source interpreter] retain]; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 188 | honourhashbang = [source honourhashbang]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 189 | debug = [source debug]; |
| 190 | verbose = [source verbose]; |
| 191 | inspect = [source inspect]; |
| 192 | optimize = [source optimize]; |
| 193 | nosite = [source nosite]; |
| 194 | tabs = [source tabs]; |
| 195 | others = [[source others] retain]; |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 196 | scriptargs = [[source scriptargs] retain]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 197 | with_terminal = [source with_terminal]; |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 198 | // And if this is a user defaults object we also save the |
| 199 | // values |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 200 | if (!origsource) { |
| 201 | NSUserDefaults *defaults; |
| 202 | NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: |
| 203 | interpreter, @"interpreter", |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 204 | [NSNumber numberWithBool: honourhashbang], @"honourhashbang", |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 205 | [NSNumber numberWithBool: debug], @"debug", |
| 206 | [NSNumber numberWithBool: verbose], @"verbose", |
| 207 | [NSNumber numberWithBool: inspect], @"inspect", |
| 208 | [NSNumber numberWithBool: optimize], @"optimize", |
| 209 | [NSNumber numberWithBool: nosite], @"nosite", |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 210 | [NSNumber numberWithBool: tabs], @"tabs", |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 211 | others, @"others", |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 212 | scriptargs, @"scriptargs", |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 213 | [NSNumber numberWithBool: with_terminal], @"with_terminal", |
| 214 | nil]; |
| 215 | defaults = [NSUserDefaults standardUserDefaults]; |
| 216 | [defaults setObject: dict forKey: prefskey]; |
| 217 | } |
| 218 | } |
| 219 | |
Jack Jansen | 2095c06 | 2002-11-25 13:11:06 +0000 | [diff] [blame] | 220 | - (void)applyValuesFromDict: (NSDictionary *)dict |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 221 | { |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 222 | id value; |
| 223 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 224 | value = [dict objectForKey: @"interpreter"]; |
| 225 | if (value) interpreter = [value retain]; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 226 | value = [dict objectForKey: @"honourhashbang"]; |
| 227 | if (value) honourhashbang = [value boolValue]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 228 | value = [dict objectForKey: @"debug"]; |
| 229 | if (value) debug = [value boolValue]; |
| 230 | value = [dict objectForKey: @"verbose"]; |
| 231 | if (value) verbose = [value boolValue]; |
| 232 | value = [dict objectForKey: @"inspect"]; |
| 233 | if (value) inspect = [value boolValue]; |
| 234 | value = [dict objectForKey: @"optimize"]; |
| 235 | if (value) optimize = [value boolValue]; |
| 236 | value = [dict objectForKey: @"nosite"]; |
| 237 | if (value) nosite = [value boolValue]; |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 238 | value = [dict objectForKey: @"tabs"]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 239 | if (value) tabs = [value boolValue]; |
| 240 | value = [dict objectForKey: @"others"]; |
| 241 | if (value) others = [value retain]; |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 242 | value = [dict objectForKey: @"scriptargs"]; |
| 243 | if (value) scriptargs = [value retain]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 244 | value = [dict objectForKey: @"with_terminal"]; |
| 245 | if (value) with_terminal = [value boolValue]; |
| 246 | } |
| 247 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 248 | - (NSString*)_replaceSingleQuotes: (NSString*)string |
| 249 | { |
| 250 | /* Replace all single-quotes by '"'"', that way shellquoting will |
| 251 | * be correct when the result value is delimited using single quotes. |
| 252 | */ |
| 253 | NSArray* components = [string componentsSeparatedByString:@"'"]; |
| 254 | |
| 255 | return [components componentsJoinedByString:@"'\"'\"'"]; |
| 256 | } |
| 257 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 258 | - (NSString *)commandLineForScript: (NSString *)script |
| 259 | { |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 260 | NSString *cur_interp = NULL; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 261 | NSString* script_dir = NULL; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 262 | char hashbangbuf[1024]; |
| 263 | FILE *fp; |
| 264 | char *p; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 265 | |
| 266 | script_dir = [script substringToIndex: |
| 267 | [script length]-[[script lastPathComponent] length]]; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 268 | |
| 269 | if (honourhashbang && |
Ronald Oussoren | 4a85269 | 2009-03-30 19:25:21 +0000 | [diff] [blame] | 270 | (fp=fopen([script fileSystemRepresentation], "r")) && |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 271 | fgets(hashbangbuf, sizeof(hashbangbuf), fp) && |
| 272 | strncmp(hashbangbuf, "#!", 2) == 0 && |
| 273 | (p=strchr(hashbangbuf, '\n'))) { |
| 274 | *p = '\0'; |
| 275 | p = hashbangbuf + 2; |
| 276 | while (*p == ' ') p++; |
Ronald Oussoren | 4a85269 | 2009-03-30 19:25:21 +0000 | [diff] [blame] | 277 | cur_interp = [NSString stringWithUTF8String: p]; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 278 | } |
| 279 | if (!cur_interp) |
| 280 | cur_interp = interpreter; |
| 281 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 282 | return [NSString stringWithFormat: |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 283 | @"cd '%@' && '%@'%s%s%s%s%s%s %@ '%@' %@ %s", |
| 284 | [self _replaceSingleQuotes:script_dir], |
| 285 | [self _replaceSingleQuotes:cur_interp], |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 286 | debug?" -d":"", |
| 287 | verbose?" -v":"", |
| 288 | inspect?" -i":"", |
| 289 | optimize?" -O":"", |
| 290 | nosite?" -S":"", |
| 291 | tabs?" -t":"", |
| 292 | others, |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 293 | [self _replaceSingleQuotes:script], |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 294 | scriptargs ? scriptargs : @"", |
Jack Jansen | f044e09 | 2002-12-26 22:10:53 +0000 | [diff] [blame] | 295 | with_terminal? "&& echo Exit status: $? && exit 1" : " &"]; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Jack Jansen | f044e09 | 2002-12-26 22:10:53 +0000 | [diff] [blame] | 298 | - (NSArray *) interpreters { return interpreters;}; |
| 299 | |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 300 | // FileSettingsSource protocol |
| 301 | - (NSString *) interpreter { return interpreter;}; |
Jack Jansen | 3d3b746 | 2003-02-17 15:40:00 +0000 | [diff] [blame] | 302 | - (BOOL) honourhashbang { return honourhashbang; }; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 303 | - (BOOL) debug { return debug;}; |
| 304 | - (BOOL) verbose { return verbose;}; |
| 305 | - (BOOL) inspect { return inspect;}; |
| 306 | - (BOOL) optimize { return optimize;}; |
| 307 | - (BOOL) nosite { return nosite;}; |
| 308 | - (BOOL) tabs { return tabs;}; |
| 309 | - (NSString *) others { return others;}; |
Jack Jansen | d7cccdd | 2003-06-20 22:21:03 +0000 | [diff] [blame] | 310 | - (NSString *) scriptargs { return scriptargs;}; |
Jack Jansen | 3bbb617 | 2002-07-29 21:36:35 +0000 | [diff] [blame] | 311 | - (BOOL) with_terminal { return with_terminal;}; |
| 312 | |
| 313 | @end |