mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 13:41:24 +00:00 
			
		
		
		
	- Get preference filename from a resource
- Create the file if it doesn't exist and don't be fussy about it
This commit is contained in:
		
							parent
							
								
									d081b13c0d
								
							
						
					
					
						commit
						83c74dfa2a
					
				
					 1 changed files with 53 additions and 31 deletions
				
			
		|  | @ -34,6 +34,36 @@ | ||||||
| :Lib:test\n\ | :Lib:test\n\ | ||||||
| :Lib:mac" | :Lib:mac" | ||||||
| 
 | 
 | ||||||
|  | static void | ||||||
|  | getpreffilefss(FSSpec *fssp) | ||||||
|  | { | ||||||
|  | 	static int diditbefore=0; | ||||||
|  | 	static FSSpec fss; | ||||||
|  |     short prefdirRefNum; | ||||||
|  |     long prefdirDirID; | ||||||
|  |     Handle namehandle; | ||||||
|  |      | ||||||
|  |     if ( !diditbefore ) { | ||||||
|  | 	    if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, | ||||||
|  | 	    				&prefdirDirID) != noErr ) { | ||||||
|  | 	    	/* Something wrong with preferences folder */ | ||||||
|  | 	    	(void)StopAlert(NOPREFDIR_ID, NULL); | ||||||
|  | 	    	exit(1); | ||||||
|  | 	    } | ||||||
|  | 	     | ||||||
|  | 	    if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) { | ||||||
|  | 	    	(void)StopAlert(NOPREFNAME_ID, NULL); | ||||||
|  | 	    	exit(1); | ||||||
|  | 	    } | ||||||
|  | 	     | ||||||
|  | 	    HLock(namehandle); | ||||||
|  | 		(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, (unsigned char *)*namehandle, &fss); | ||||||
|  | 		HUnlock(namehandle); | ||||||
|  | 		ReleaseResource(namehandle); | ||||||
|  | 		diditbefore = 1; | ||||||
|  | 	} | ||||||
|  | 	*fssp = fss; | ||||||
|  | } | ||||||
| 
 | 
 | ||||||
| char * | char * | ||||||
| Py_GetPath() | Py_GetPath() | ||||||
|  | @ -44,21 +74,21 @@ Py_GetPath() | ||||||
| 	** - Add : | 	** - Add : | ||||||
| 	*/ | 	*/ | ||||||
| 	static char *pythonpath; | 	static char *pythonpath; | ||||||
| 	char *curwd; |  | ||||||
| 	char *p, *endp; | 	char *p, *endp; | ||||||
| 	int newlen; | 	int newlen; | ||||||
|  | 	char *curwd; | ||||||
| 	staticforward char *PyMac_GetPythonDir(); | 	staticforward char *PyMac_GetPythonDir(); | ||||||
| #ifndef USE_BUILTIN_PATH | #ifndef USE_BUILTIN_PATH | ||||||
| 	staticforward char *PyMac_GetPythonPath(); | 	staticforward char *PyMac_GetPythonPath(); | ||||||
| #endif | #endif | ||||||
| 	 | 	 | ||||||
| 	if ( pythonpath ) return pythonpath; | 	if ( pythonpath ) return pythonpath; | ||||||
| 	curwd = PyMac_GetPythonDir(); |  | ||||||
| #ifndef USE_BUILTIN_PATH | #ifndef USE_BUILTIN_PATH | ||||||
| 	if ( pythonpath = PyMac_GetPythonPath(curwd) ) | 	if ( pythonpath = PyMac_GetPythonPath() ) | ||||||
| 		return pythonpath; | 		return pythonpath; | ||||||
| 	printf("Warning: No pythonpath resource found, using builtin default\n"); | 	printf("Warning: No pythonpath resource found, using builtin default\n"); | ||||||
| #endif | #endif | ||||||
|  | 	curwd = PyMac_GetPythonDir(); | ||||||
| 	p = PYTHONPATH; | 	p = PYTHONPATH; | ||||||
| 	endp = p; | 	endp = p; | ||||||
| 	pythonpath = malloc(2); | 	pythonpath = malloc(2); | ||||||
|  | @ -90,6 +120,7 @@ Py_GetPath() | ||||||
| 	return pythonpath; | 	return pythonpath; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| /*
 | /*
 | ||||||
| ** Open/create the Python Preferences file, return the handle | ** Open/create the Python Preferences file, return the handle | ||||||
| */ | */ | ||||||
|  | @ -99,25 +130,16 @@ PyMac_OpenPrefFile() | ||||||
|     AliasHandle handle; |     AliasHandle handle; | ||||||
|     FSSpec dirspec; |     FSSpec dirspec; | ||||||
|     short prefrh; |     short prefrh; | ||||||
|     short prefdirRefNum; |  | ||||||
|     long prefdirDirID; |  | ||||||
|     short action; |  | ||||||
|     OSErr err; |     OSErr err; | ||||||
| 
 | 
 | ||||||
|     if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, | 	getpreffilefss(&dirspec); | ||||||
|     				&prefdirDirID) != noErr ) { |  | ||||||
|     	/* Something wrong with preferences folder */ |  | ||||||
|     	(void)StopAlert(NOPREFDIR_ID, NULL); |  | ||||||
|     	exit(1); |  | ||||||
|     } |  | ||||||
|      |  | ||||||
| 	(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec); |  | ||||||
| 	prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); | 	prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); | ||||||
| 	if ( prefrh < 0 ) { | 	if ( prefrh < 0 ) { | ||||||
|  | #if 0 | ||||||
| 		action = CautionAlert(NOPREFFILE_ID, NULL); | 		action = CautionAlert(NOPREFFILE_ID, NULL); | ||||||
| 		if ( action == NOPREFFILE_NO ) | 		if ( action == NOPREFFILE_NO ) | ||||||
| 			exit(1); | 			exit(1); | ||||||
| 	 | #endif | ||||||
| 		FSpCreateResFile(&dirspec, 'Pyth', 'pref', 0); | 		FSpCreateResFile(&dirspec, 'Pyth', 'pref', 0); | ||||||
| 		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); | 		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); | ||||||
| 		if ( prefrh == -1 ) { | 		if ( prefrh == -1 ) { | ||||||
|  | @ -149,12 +171,16 @@ PyMac_OpenPrefFile() | ||||||
| static char * | static char * | ||||||
| PyMac_GetPythonDir() | PyMac_GetPythonDir() | ||||||
| { | { | ||||||
|     static char name[256]; | 	static int diditbefore = 0; | ||||||
|  |     static char name[256] = {':', '\0'}; | ||||||
|     AliasHandle handle; |     AliasHandle handle; | ||||||
|     FSSpec dirspec; |     FSSpec dirspec; | ||||||
|     Boolean modified = 0; |     Boolean modified = 0; | ||||||
|     short oldrh, prefrh = -1, homerh; |     short oldrh, prefrh = -1, homerh; | ||||||
|      |      | ||||||
|  |     if ( diditbefore ) | ||||||
|  |     	return name; | ||||||
|  |     	 | ||||||
|     oldrh = CurResFile(); |     oldrh = CurResFile(); | ||||||
| 
 | 
 | ||||||
|     /* First look for an override in the application file */ |     /* First look for an override in the application file */ | ||||||
|  | @ -168,14 +194,16 @@ PyMac_GetPythonDir() | ||||||
| 	    handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID); | 	    handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID); | ||||||
| 	    if ( handle == NULL ) { | 	    if ( handle == NULL ) { | ||||||
| 	    	(void)StopAlert(BADPREFFILE_ID, NULL); | 	    	(void)StopAlert(BADPREFFILE_ID, NULL); | ||||||
| 	    	exit(1); | 	    	diditbefore=1; | ||||||
|  | 	    	return ":"; | ||||||
| 	    } | 	    } | ||||||
| 	    homerh = prefrh; | 	    homerh = prefrh; | ||||||
|     } |     } | ||||||
| 	/* It exists. Resolve it (possibly updating it) */ | 	/* It exists. Resolve it (possibly updating it) */ | ||||||
| 	if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) { | 	if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) { | ||||||
|     	(void)StopAlert(BADPREFFILE_ID, NULL); |     	(void)StopAlert(BADPREFFILE_ID, NULL); | ||||||
|     	exit(1); |     	diditbefore=1; | ||||||
|  |     	return ":"; | ||||||
|     } |     } | ||||||
|     if ( modified ) { |     if ( modified ) { | ||||||
|    		ChangedResource((Handle)handle); |    		ChangedResource((Handle)handle); | ||||||
|  | @ -192,18 +220,16 @@ PyMac_GetPythonDir() | ||||||
| 		name[0] = 0; | 		name[0] = 0; | ||||||
| 		(void)getwd(name); | 		(void)getwd(name); | ||||||
| 	} | 	} | ||||||
|  | 	diditbefore = 1; | ||||||
| 	return name; | 	return name; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| #ifndef USE_BUILTIN_PATH | #ifndef USE_BUILTIN_PATH | ||||||
| static char * | static char * | ||||||
| PyMac_GetPythonPath(dir) | PyMac_GetPythonPath() | ||||||
| char *dir; |  | ||||||
| { | { | ||||||
|     FSSpec dirspec; |     FSSpec dirspec; | ||||||
|     short oldrh, prefrh = -1; |     short oldrh, prefrh = -1; | ||||||
|     short prefdirRefNum; |  | ||||||
|     long prefdirDirID; |  | ||||||
|     char *rv; |     char *rv; | ||||||
|     int i, newlen; |     int i, newlen; | ||||||
|     Str255 pathitem; |     Str255 pathitem; | ||||||
|  | @ -228,15 +254,9 @@ char *dir; | ||||||
|     SetResLoad(1); |     SetResLoad(1); | ||||||
|     UseResFile(oldrh); |     UseResFile(oldrh); | ||||||
|      |      | ||||||
|     /*
 |     /* Open the preferences file only if there is no override */ | ||||||
|     ** Remember old resource file and try to open preferences file |     if ( resource_id != PYTHONPATHOVERRIDE_ID ) | ||||||
|     ** in the preferences folder. | 	    prefrh = PyMac_OpenPrefFile(); | ||||||
|     */ |  | ||||||
|     if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |  | ||||||
|     				&prefdirDirID) == noErr ) { |  | ||||||
|     	(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec); |  | ||||||
| 		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |  | ||||||
|     } |  | ||||||
|     /* At this point, we may or may not have the preferences file open, and it
 |     /* At this point, we may or may not have the preferences file open, and it
 | ||||||
|     ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't |     ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't | ||||||
|     ** exist we use the one from the application (the default). |     ** exist we use the one from the application (the default). | ||||||
|  | @ -252,6 +272,8 @@ char *dir; | ||||||
|     		break; |     		break; | ||||||
|     	if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { |     	if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { | ||||||
|     		/* We have to put the directory in place */ |     		/* We have to put the directory in place */ | ||||||
|  |     		char *dir = PyMac_GetPythonDir(); | ||||||
|  |     		 | ||||||
|     		newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; |     		newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; | ||||||
|     		if( (rv=realloc(rv, newlen)) == NULL) |     		if( (rv=realloc(rv, newlen)) == NULL) | ||||||
|     			goto out; |     			goto out; | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Jack Jansen
						Jack Jansen