comparison Mac/Python/macgetpath.c @ 8788:d642e6748d12 legacy-trunk

Re-indented properly (Just).
author Jack Jansen <jack.jansen@cwi.nl>
date Fri, 31 Jul 1998 09:33:28 +0000
parents 6245b819bdd8
children ecebcd84df37
comparison
equal deleted inserted replaced
8787:d2bfc8654280 8788:d642e6748d12
69 getpreffilefss(FSSpec *fssp) 69 getpreffilefss(FSSpec *fssp)
70 { 70 {
71 static int diditbefore=0; 71 static int diditbefore=0;
72 static int rv = 1; 72 static int rv = 1;
73 static FSSpec fss; 73 static FSSpec fss;
74 short prefdirRefNum; 74 short prefdirRefNum;
75 long prefdirDirID; 75 long prefdirDirID;
76 Handle namehandle; 76 Handle namehandle;
77 77
78 if ( !diditbefore ) { 78 if ( !diditbefore ) {
79 if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, 79 if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
80 &prefdirDirID) != noErr ) { 80 &prefdirDirID) != noErr ) {
81 /* Something wrong with preferences folder */ 81 /* Something wrong with preferences folder */
82 (void)StopAlert(NOPREFDIR_ID, NULL); 82 (void)StopAlert(NOPREFDIR_ID, NULL);
83 exit(1); 83 exit(1);
84 } 84 }
85 85
86 if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) { 86 if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) {
87 (void)StopAlert(NOPREFNAME_ID, NULL); 87 (void)StopAlert(NOPREFNAME_ID, NULL);
88 exit(1); 88 exit(1);
89 } 89 }
90 90
91 HLock(namehandle); 91 HLock(namehandle);
92 if ( **namehandle == '\0' ) { 92 if ( **namehandle == '\0' ) {
93 /* Empty string means don't use preferences file */ 93 /* Empty string means don't use preferences file */
94 rv = 0; 94 rv = 0;
95 } else { 95 } else {
96 /* There is a filename, construct the fsspec */ 96 /* There is a filename, construct the fsspec */
97 (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, (unsigned char *)*namehandle, &fss); 97 (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, (unsigned char *)*namehandle, &fss);
98 } 98 }
99 HUnlock(namehandle); 99 HUnlock(namehandle);
100 ReleaseResource(namehandle); 100 ReleaseResource(namehandle);
101 diditbefore = 1; 101 diditbefore = 1;
163 ** Open/create the Python Preferences file, return the handle 163 ** Open/create the Python Preferences file, return the handle
164 */ 164 */
165 static short 165 static short
166 PyMac_OpenPrefFile() 166 PyMac_OpenPrefFile()
167 { 167 {
168 AliasHandle handle; 168 AliasHandle handle;
169 FSSpec dirspec; 169 FSSpec dirspec;
170 short prefrh; 170 short prefrh;
171 OSErr err; 171 OSErr err;
172 172
173 if ( !getpreffilefss(&dirspec)) 173 if ( !getpreffilefss(&dirspec))
174 return -1; 174 return -1;
175 prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); 175 prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
176 if ( prefrh < 0 ) { 176 if ( prefrh < 0 ) {
194 dirspec.name[0] = 0; 194 dirspec.name[0] = 0;
195 if ((err=NewAlias(NULL, &dirspec, &handle)) != 0 ) { 195 if ((err=NewAlias(NULL, &dirspec, &handle)) != 0 ) {
196 printf("Cannot make alias to application directory, error %d\n", err); 196 printf("Cannot make alias to application directory, error %d\n", err);
197 exit(1); 197 exit(1);
198 } 198 }
199 AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p"); 199 AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p");
200 UpdateResFile(prefrh); 200 UpdateResFile(prefrh);
201 201
202 } else { 202 } else {
203 UseResFile(prefrh); 203 UseResFile(prefrh);
204 } 204 }
205 return prefrh; 205 return prefrh;
210 */ 210 */
211 char * 211 char *
212 PyMac_GetPythonDir() 212 PyMac_GetPythonDir()
213 { 213 {
214 static int diditbefore = 0; 214 static int diditbefore = 0;
215 static char name[256] = {':', '\0'}; 215 static char name[256] = {':', '\0'};
216 AliasHandle handle; 216 AliasHandle handle;
217 FSSpec dirspec; 217 FSSpec dirspec;
218 Boolean modified = 0; 218 Boolean modified = 0;
219 short oldrh, prefrh = -1, homerh; 219 short oldrh, prefrh = -1, homerh;
220 220
221 if ( diditbefore ) 221 if ( diditbefore )
222 return name; 222 return name;
223 223
224 oldrh = CurResFile(); 224 oldrh = CurResFile();
225 225
226 /* First look for an override in the application file */ 226 /* First look for an override in the application file */
227 UseResFile(PyMac_AppRefNum); 227 UseResFile(PyMac_AppRefNum);
228 handle = (AliasHandle)Get1Resource('alis', PYTHONHOMEOVERRIDE_ID); 228 handle = (AliasHandle)Get1Resource('alis', PYTHONHOMEOVERRIDE_ID);
229 UseResFile(oldrh); 229 UseResFile(oldrh);
230 if ( handle != NULL ) { 230 if ( handle != NULL ) {
231 homerh = PyMac_AppRefNum; 231 homerh = PyMac_AppRefNum;
232 } else { 232 } else {
233 /* Try to open preferences file in the preferences folder. */ 233 /* Try to open preferences file in the preferences folder. */
234 prefrh = PyMac_OpenPrefFile(); 234 prefrh = PyMac_OpenPrefFile();
235 handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID); 235 handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID);
236 if ( handle == NULL ) { 236 if ( handle == NULL ) {
237 /* (void)StopAlert(BADPREFFILE_ID, NULL); */ 237 /* (void)StopAlert(BADPREFFILE_ID, NULL); */
238 diditbefore=1; 238 diditbefore=1;
239 return ":"; 239 return ":";
240 } 240 }
241 homerh = prefrh; 241 homerh = prefrh;
242 } 242 }
243 /* It exists. Resolve it (possibly updating it) */ 243 /* It exists. Resolve it (possibly updating it) */
244 if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) { 244 if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) {
245 (void)StopAlert(BADPREFFILE_ID, NULL); 245 (void)StopAlert(BADPREFFILE_ID, NULL);
246 diditbefore=1; 246 diditbefore=1;
247 return ":"; 247 return ":";
248 } 248 }
249 if ( modified ) { 249 if ( modified ) {
250 ChangedResource((Handle)handle); 250 ChangedResource((Handle)handle);
251 UpdateResFile(homerh); 251 UpdateResFile(homerh);
252 } 252 }
253 if ( prefrh != -1 ) CloseResFile(prefrh); 253 if ( prefrh != -1 ) CloseResFile(prefrh);
254 UseResFile(oldrh); 254 UseResFile(oldrh);
255 255
256 if ( PyMac_GetFullPath(&dirspec, name) == 0 ) { 256 if ( PyMac_GetFullPath(&dirspec, name) == 0 ) {
257 strcat(name, ":"); 257 strcat(name, ":");
258 } else { 258 } else {
259 /* If all fails, we return the current directory */ 259 /* If all fails, we return the current directory */
260 printf("Python home dir exists but I cannot find the pathname!!\n"); 260 printf("Python home dir exists but I cannot find the pathname!!\n");
261 name[0] = 0; 261 name[0] = 0;
262 (void)getwd(name); 262 (void)getwd(name);
263 } 263 }
267 267
268 #ifndef USE_BUILTIN_PATH 268 #ifndef USE_BUILTIN_PATH
269 char * 269 char *
270 PyMac_GetPythonPath() 270 PyMac_GetPythonPath()
271 { 271 {
272 short oldrh, prefrh = -1; 272 short oldrh, prefrh = -1;
273 char *rv; 273 char *rv;
274 int i, newlen; 274 int i, newlen;
275 Str255 pathitem; 275 Str255 pathitem;
276 int resource_id; 276 int resource_id;
277 OSErr err; 277 OSErr err;
278 Handle h; 278 Handle h;
279 279
280 oldrh = CurResFile(); 280 oldrh = CurResFile();
281 /* 281 /*
282 ** This is a bit tricky. We check here whether the application file 282 ** This is a bit tricky. We check here whether the application file
283 ** contains an override. This is to forestall us finding another STR# resource 283 ** contains an override. This is to forestall us finding another STR# resource
284 ** with "our" id and using that for path initialization 284 ** with "our" id and using that for path initialization
285 */ 285 */
286 UseResFile(PyMac_AppRefNum); 286 UseResFile(PyMac_AppRefNum);
287 SetResLoad(0); 287 SetResLoad(0);
288 if ( (h=Get1Resource('STR#', PYTHONPATHOVERRIDE_ID)) ) { 288 if ( (h=Get1Resource('STR#', PYTHONPATHOVERRIDE_ID)) ) {
289 ReleaseResource(h); 289 ReleaseResource(h);
290 resource_id = PYTHONPATHOVERRIDE_ID; 290 resource_id = PYTHONPATHOVERRIDE_ID;
291 } else { 291 } else {
292 resource_id = PYTHONPATH_ID; 292 resource_id = PYTHONPATH_ID;
293 } 293 }
294 SetResLoad(1); 294 SetResLoad(1);
295 UseResFile(oldrh); 295 UseResFile(oldrh);
296 296
297 /* Open the preferences file only if there is no override */ 297 /* Open the preferences file only if there is no override */
298 if ( resource_id != PYTHONPATHOVERRIDE_ID ) 298 if ( resource_id != PYTHONPATHOVERRIDE_ID )
299 prefrh = PyMac_OpenPrefFile(); 299 prefrh = PyMac_OpenPrefFile();
300 /* At this point, we may or may not have the preferences file open, and it 300 /* At this point, we may or may not have the preferences file open, and it
301 ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't 301 ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't
302 ** exist we use the one from the application (the default). 302 ** exist we use the one from the application (the default).
303 ** We put an initial '\n' in front of the path that we don't return to the caller 303 ** We put an initial '\n' in front of the path that we don't return to the caller
304 */ 304 */
305 if( (rv = malloc(2)) == NULL ) 305 if( (rv = malloc(2)) == NULL )
306 goto out; 306 goto out;
307 strcpy(rv, "\n"); 307 strcpy(rv, "\n");
308 308
309 for(i=1; ; i++) { 309 for(i=1; ; i++) {
310 GetIndString(pathitem, resource_id, i); 310 GetIndString(pathitem, resource_id, i);
311 if( pathitem[0] == 0 ) 311 if( pathitem[0] == 0 )
312 break; 312 break;
313 if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { 313 if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) {
314 /* We have to put the directory in place */ 314 /* We have to put the directory in place */
315 char *dir = PyMac_GetPythonDir(); 315 char *dir = PyMac_GetPythonDir();
316 316
317 newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; 317 newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2;
318 if( (rv=realloc(rv, newlen)) == NULL) 318 if( (rv=realloc(rv, newlen)) == NULL)
319 goto out; 319 goto out;
320 strcat(rv, dir); 320 strcat(rv, dir);
321 /* Skip a colon at the beginning of the item */ 321 /* Skip a colon at the beginning of the item */
322 if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) { 322 if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) {
323 memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10); 323 memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10);
324 newlen--; 324 newlen--;
325 } else { 325 } else {
326 memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9); 326 memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9);
327 } 327 }
328 rv[newlen-2] = '\n'; 328 rv[newlen-2] = '\n';
329 rv[newlen-1] = 0; 329 rv[newlen-1] = 0;
330 } else if ( pathitem[0] >= 14 && strncmp((char *)pathitem+1, "$(APPLICATION)", 14) == 0 ) { 330 } else if ( pathitem[0] >= 14 && strncmp((char *)pathitem+1, "$(APPLICATION)", 14) == 0 ) {
331 /* This is the application itself */ 331 /* This is the application itself */
332 332
333 if ( (err=PyMac_init_process_location()) != 0 ) { 333 if ( (err=PyMac_init_process_location()) != 0 ) {
334 printf("Cannot get application location, error %d\n", err); 334 printf("Cannot get application location, error %d\n", err);
335 exit(1); 335 exit(1);
336 } 336 }
337 337
338 newlen = strlen(rv) + strlen(PyMac_ApplicationPath) + 2; 338 newlen = strlen(rv) + strlen(PyMac_ApplicationPath) + 2;
339 if( (rv=realloc(rv, newlen)) == NULL) 339 if( (rv=realloc(rv, newlen)) == NULL)
340 goto out; 340 goto out;
341 strcpy(rv+strlen(rv), PyMac_ApplicationPath); 341 strcpy(rv+strlen(rv), PyMac_ApplicationPath);
342 rv[newlen-2] = '\n'; 342 rv[newlen-2] = '\n';
343 rv[newlen-1] = 0; 343 rv[newlen-1] = 0;
344 344
345 } else { 345 } else {
346 /* Use as-is */ 346 /* Use as-is */
347 newlen = strlen(rv) + (pathitem[0]) + 2; 347 newlen = strlen(rv) + (pathitem[0]) + 2;
348 if( (rv=realloc(rv, newlen)) == NULL) 348 if( (rv=realloc(rv, newlen)) == NULL)
349 goto out; 349 goto out;
350 memcpy(rv+strlen(rv), pathitem+1, pathitem[0]); 350 memcpy(rv+strlen(rv), pathitem+1, pathitem[0]);
351 rv[newlen-2] = '\n'; 351 rv[newlen-2] = '\n';
352 rv[newlen-1] = 0; 352 rv[newlen-1] = 0;
353 } 353 }
354 } 354 }
355 if( strlen(rv) == 1) { 355 if( strlen(rv) == 1) {
356 free(rv); 356 free(rv);
357 rv = NULL; 357 rv = NULL;
358 } 358 }
375 int size; 375 int size;
376 PyMac_PrefRecord *p; 376 PyMac_PrefRecord *p;
377 int action; 377 int action;
378 378
379 379
380 oldrh = CurResFile(); 380 oldrh = CurResFile();
381 381
382 /* Attempt to load overrides from application */ 382 /* Attempt to load overrides from application */
383 UseResFile(PyMac_AppRefNum); 383 UseResFile(PyMac_AppRefNum);
384 handle = Get1Resource('Popt', PYTHONOPTIONSOVERRIDE_ID); 384 handle = Get1Resource('Popt', PYTHONOPTIONSOVERRIDE_ID);
385 UseResFile(oldrh); 385 UseResFile(oldrh);
386 386
387 /* Otherwise get options from prefs file or any other open resource file */ 387 /* Otherwise get options from prefs file or any other open resource file */
388 if ( handle == NULL ) { 388 if ( handle == NULL ) {
389 prefrh = PyMac_OpenPrefFile(); 389 prefrh = PyMac_OpenPrefFile();
390 handle = GetResource('Popt', PYTHONOPTIONS_ID); 390 handle = GetResource('Popt', PYTHONOPTIONS_ID);
391 } 391 }
392 if ( handle == NULL ) { 392 if ( handle == NULL ) {
393 return; 393 return;
394 } 394 }
395 HLock(handle); 395 HLock(handle);
396 size = GetHandleSize(handle); 396 size = GetHandleSize(handle);
397 p = (PyMac_PrefRecord *)*handle; 397 p = (PyMac_PrefRecord *)*handle;
398 if ( p->version == POPT_VERSION_CURRENT && size == sizeof(PyMac_PrefRecord) ) { 398 if ( p->version == POPT_VERSION_CURRENT && size == sizeof(PyMac_PrefRecord) ) {
399 *pr = *p; 399 *pr = *p;
400 } else { 400 } else {
401 action = CautionAlert(BADPREFERENCES_ID, NULL); 401 action = CautionAlert(BADPREFERENCES_ID, NULL);
402 if ( action == BADPREF_DELETE ) { 402 if ( action == BADPREF_DELETE ) {
403 OSErr err; 403 OSErr err;
404 404
405 RemoveResource(handle); 405 RemoveResource(handle);
406 if ( (err=ResError()) ) printf("RemoveResource: %d\n", err); 406 if ( (err=ResError()) ) printf("RemoveResource: %d\n", err);
407 if ( prefrh != -1 ) { 407 if ( prefrh != -1 ) {
408 UpdateResFile(prefrh); 408 UpdateResFile(prefrh);
409 if ( (err=ResError()) ) printf("UpdateResFile: %d\n", err); 409 if ( (err=ResError()) ) printf("UpdateResFile: %d\n", err);
410 } 410 }
411 } else if ( action == BADPREF_QUIT ) 411 } else if ( action == BADPREF_QUIT )
412 exit(1); 412 exit(1);
413 } 413 }
414 HUnlock(handle); 414 HUnlock(handle);
415 415
416 if ( prefrh != -1) CloseResFile(prefrh); 416 if ( prefrh != -1) CloseResFile(prefrh);
417 UseResFile(oldrh); 417 UseResFile(oldrh);
418 } 418 }
419 419
420 #ifdef USE_GUSI 420 #ifdef USE_GUSI
421 void 421 void
422 PyMac_SetGUSIOptions() 422 PyMac_SetGUSIOptions()
436 prefrh = PyMac_OpenPrefFile(); 436 prefrh = PyMac_OpenPrefFile();
437 h = GetResource('GU\267I', GUSIOPTIONS_ID); 437 h = GetResource('GU\267I', GUSIOPTIONS_ID);
438 } 438 }
439 if ( h ) GUSILoadConfiguration(h); 439 if ( h ) GUSILoadConfiguration(h);
440 if ( prefrh != -1) CloseResFile(prefrh); 440 if ( prefrh != -1) CloseResFile(prefrh);
441 UseResFile(oldrh); 441 UseResFile(oldrh);
442 } 442 }
443 #endif /* USE_GUSI */ 443 #endif /* USE_GUSI */