Jack Jansen | 42218ce | 1997-01-31 16:15:11 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
| 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
| 16 | |
| 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
| 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 32 | #include "Python.h" |
| 33 | #include "osdefs.h" |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 34 | #include "macglue.h" |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 35 | #include "pythonresources.h" |
| 36 | |
| 37 | |
| 38 | /* Return the initial python search path. This is called once from |
| 39 | ** initsys() to initialize sys.path. |
| 40 | ** |
| 41 | ** If USE_BUILTIN_PATH is defined the path defined here is used |
| 42 | ** (after prepending the python home dir to each item). |
| 43 | ** If it is not defined the path is gotten from a resource in the |
| 44 | ** Preferences file. |
| 45 | ** |
| 46 | ** XXXX This code needs cleaning up. The routines here have moved |
| 47 | ** around quite a bit, and they're pretty messy for that reason. |
| 48 | */ |
| 49 | |
| 50 | #include <Files.h> |
| 51 | #include <Aliases.h> |
| 52 | #include <Folders.h> |
| 53 | #include <Resources.h> |
| 54 | #include <TextUtils.h> |
Jack Jansen | b39be21 | 1995-09-01 11:48:10 +0000 | [diff] [blame] | 55 | #include <Dialogs.h> |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 56 | |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 57 | #ifdef USE_GUSI |
| 58 | #include <GUSI.h> |
| 59 | #endif |
| 60 | |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 61 | #define PYTHONPATH "\ |
| 62 | :\n\ |
| 63 | :Lib\n\ |
| 64 | :Lib:stdwin\n\ |
| 65 | :Lib:test\n\ |
| 66 | :Lib:mac" |
| 67 | |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 68 | static int |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 69 | getpreffilefss(FSSpec *fssp) |
| 70 | { |
| 71 | static int diditbefore=0; |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 72 | static int rv = 1; |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 73 | static FSSpec fss; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 74 | short prefdirRefNum; |
| 75 | long prefdirDirID; |
| 76 | Handle namehandle; |
| 77 | |
| 78 | if ( !diditbefore ) { |
| 79 | if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |
| 80 | &prefdirDirID) != noErr ) { |
| 81 | /* Something wrong with preferences folder */ |
| 82 | (void)StopAlert(NOPREFDIR_ID, NULL); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
| 86 | if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) { |
| 87 | (void)StopAlert(NOPREFNAME_ID, NULL); |
| 88 | exit(1); |
| 89 | } |
| 90 | |
| 91 | HLock(namehandle); |
| 92 | if ( **namehandle == '\0' ) { |
| 93 | /* Empty string means don't use preferences file */ |
| 94 | rv = 0; |
| 95 | } else { |
| 96 | /* There is a filename, construct the fsspec */ |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 97 | (void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, (unsigned char *)*namehandle, &fss); |
| 98 | } |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 99 | HUnlock(namehandle); |
| 100 | ReleaseResource(namehandle); |
| 101 | diditbefore = 1; |
| 102 | } |
| 103 | *fssp = fss; |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 104 | return rv; |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 105 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 106 | |
| 107 | char * |
Jack Jansen | a547dca | 1996-07-10 15:48:25 +0000 | [diff] [blame] | 108 | Py_GetPath() |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 109 | { |
| 110 | /* Modified by Jack to do something a bit more sensible: |
| 111 | ** - Prepend the python home-directory (which is obtained from a Preferences |
| 112 | ** resource) |
| 113 | ** - Add : |
| 114 | */ |
| 115 | static char *pythonpath; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 116 | char *p, *endp; |
| 117 | int newlen; |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 118 | char *curwd; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 119 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 120 | staticforward char *PyMac_GetPythonPath(); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
| 123 | if ( pythonpath ) return pythonpath; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 124 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 125 | if ( pythonpath = PyMac_GetPythonPath() ) |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 126 | return pythonpath; |
| 127 | printf("Warning: No pythonpath resource found, using builtin default\n"); |
| 128 | #endif |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 129 | curwd = PyMac_GetPythonDir(); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 130 | p = PYTHONPATH; |
| 131 | endp = p; |
| 132 | pythonpath = malloc(2); |
| 133 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 134 | strcpy(pythonpath, ":"); |
| 135 | while (*endp) { |
| 136 | endp = strchr(p, '\n'); |
| 137 | if ( endp == NULL ) |
| 138 | endp = p + strlen(p); |
| 139 | newlen = strlen(pythonpath) + 1 + strlen(curwd) + (endp-p); |
| 140 | pythonpath = realloc(pythonpath, newlen+1); |
| 141 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 142 | strcat(pythonpath, "\n"); |
| 143 | if ( *p == ':' ) { |
| 144 | p++; |
| 145 | strcat(pythonpath, curwd); |
| 146 | strncat(pythonpath, p, (endp-p)); |
| 147 | newlen--; /* Ok, ok, we've allocated one byte too much */ |
| 148 | } else { |
| 149 | /* We've allocated too much in this case */ |
| 150 | newlen -= strlen(curwd); |
| 151 | pythonpath = realloc(pythonpath, newlen+1); |
| 152 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 153 | strncat(pythonpath, p, (endp-p)); |
| 154 | } |
| 155 | pythonpath[newlen] = '\0'; |
| 156 | p = endp + 1; |
| 157 | } |
| 158 | return pythonpath; |
| 159 | } |
| 160 | |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 161 | |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 162 | /* |
| 163 | ** Open/create the Python Preferences file, return the handle |
| 164 | */ |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 165 | static short |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 166 | PyMac_OpenPrefFile() |
| 167 | { |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 168 | AliasHandle handle; |
| 169 | FSSpec dirspec; |
| 170 | short prefrh; |
| 171 | OSErr err; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 172 | |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 173 | if ( !getpreffilefss(&dirspec)) |
| 174 | return -1; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 175 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 176 | if ( prefrh < 0 ) { |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 177 | #if 0 |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 178 | action = CautionAlert(NOPREFFILE_ID, NULL); |
| 179 | if ( action == NOPREFFILE_NO ) |
| 180 | exit(1); |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 181 | #endif |
Jack Jansen | 532e3c2 | 1996-02-21 15:36:26 +0000 | [diff] [blame] | 182 | FSpCreateResFile(&dirspec, 'Pyth', 'pref', 0); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 183 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 184 | if ( prefrh == -1 ) { |
| 185 | /* This "cannot happen":-) */ |
Jack Jansen | b39be21 | 1995-09-01 11:48:10 +0000 | [diff] [blame] | 186 | printf("Cannot create preferences file, error %d\n", ResError()); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 187 | exit(1); |
| 188 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 189 | if ( (err=PyMac_init_process_location()) != 0 ) { |
| 190 | printf("Cannot get application location, error %d\n", err); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 191 | exit(1); |
| 192 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 193 | dirspec = PyMac_ApplicationFSSpec; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 194 | dirspec.name[0] = 0; |
Jack Jansen | b39be21 | 1995-09-01 11:48:10 +0000 | [diff] [blame] | 195 | if ((err=NewAlias(NULL, &dirspec, &handle)) != 0 ) { |
| 196 | printf("Cannot make alias to application directory, error %d\n", err); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 197 | exit(1); |
| 198 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 199 | AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p"); |
| 200 | UpdateResFile(prefrh); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 201 | |
| 202 | } else { |
| 203 | UseResFile(prefrh); |
| 204 | } |
| 205 | return prefrh; |
| 206 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 207 | |
| 208 | /* |
| 209 | ** Return the name of the Python directory |
| 210 | */ |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 211 | char * |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 212 | PyMac_GetPythonDir() |
| 213 | { |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 214 | static int diditbefore = 0; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 215 | static char name[256] = {':', '\0'}; |
| 216 | AliasHandle handle; |
| 217 | FSSpec dirspec; |
| 218 | Boolean modified = 0; |
| 219 | short oldrh, prefrh = -1, homerh; |
| 220 | |
| 221 | if ( diditbefore ) |
| 222 | return name; |
| 223 | |
| 224 | oldrh = CurResFile(); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 225 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 226 | /* First look for an override in the application file */ |
| 227 | UseResFile(PyMac_AppRefNum); |
| 228 | handle = (AliasHandle)Get1Resource('alis', PYTHONHOMEOVERRIDE_ID); |
| 229 | UseResFile(oldrh); |
| 230 | if ( handle != NULL ) { |
| 231 | homerh = PyMac_AppRefNum; |
| 232 | } else { |
| 233 | /* Try to open preferences file in the preferences folder. */ |
| 234 | prefrh = PyMac_OpenPrefFile(); |
| 235 | handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID); |
| 236 | if ( handle == NULL ) { |
| 237 | /* (void)StopAlert(BADPREFFILE_ID, NULL); */ |
| 238 | diditbefore=1; |
| 239 | return ":"; |
| 240 | } |
| 241 | homerh = prefrh; |
| 242 | } |
| 243 | /* It exists. Resolve it (possibly updating it) */ |
| 244 | if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) { |
| 245 | (void)StopAlert(BADPREFFILE_ID, NULL); |
| 246 | diditbefore=1; |
| 247 | return ":"; |
| 248 | } |
| 249 | if ( modified ) { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 250 | ChangedResource((Handle)handle); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 251 | UpdateResFile(homerh); |
| 252 | } |
| 253 | if ( prefrh != -1 ) CloseResFile(prefrh); |
| 254 | UseResFile(oldrh); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 255 | |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 256 | if ( PyMac_GetFullPath(&dirspec, name) == 0 ) { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 257 | strcat(name, ":"); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 258 | } else { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 259 | /* If all fails, we return the current directory */ |
| 260 | printf("Python home dir exists but I cannot find the pathname!!\n"); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 261 | name[0] = 0; |
| 262 | (void)getwd(name); |
| 263 | } |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 264 | diditbefore = 1; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 265 | return name; |
| 266 | } |
| 267 | |
| 268 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 269 | char * |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 270 | PyMac_GetPythonPath() |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 271 | { |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 272 | short oldrh, prefrh = -1; |
| 273 | char *rv; |
| 274 | int i, newlen; |
| 275 | Str255 pathitem; |
| 276 | int resource_id; |
| 277 | OSErr err; |
| 278 | Handle h; |
| 279 | |
| 280 | oldrh = CurResFile(); |
| 281 | /* |
| 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 |
| 284 | ** with "our" id and using that for path initialization |
| 285 | */ |
| 286 | UseResFile(PyMac_AppRefNum); |
| 287 | SetResLoad(0); |
| 288 | if ( (h=Get1Resource('STR#', PYTHONPATHOVERRIDE_ID)) ) { |
| 289 | ReleaseResource(h); |
| 290 | resource_id = PYTHONPATHOVERRIDE_ID; |
| 291 | } else { |
| 292 | resource_id = PYTHONPATH_ID; |
| 293 | } |
| 294 | SetResLoad(1); |
| 295 | UseResFile(oldrh); |
| 296 | |
| 297 | /* Open the preferences file only if there is no override */ |
| 298 | if ( resource_id != PYTHONPATHOVERRIDE_ID ) |
| 299 | prefrh = PyMac_OpenPrefFile(); |
| 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 |
| 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 |
| 304 | */ |
| 305 | if( (rv = malloc(2)) == NULL ) |
| 306 | goto out; |
| 307 | strcpy(rv, "\n"); |
Jack Jansen | f12e709 | 1996-09-05 15:19:24 +0000 | [diff] [blame] | 308 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 309 | for(i=1; ; i++) { |
| 310 | GetIndString(pathitem, resource_id, i); |
| 311 | if( pathitem[0] == 0 ) |
| 312 | break; |
| 313 | if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { |
| 314 | /* We have to put the directory in place */ |
| 315 | char *dir = PyMac_GetPythonDir(); |
| 316 | |
| 317 | newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; |
| 318 | if( (rv=realloc(rv, newlen)) == NULL) |
| 319 | goto out; |
| 320 | strcat(rv, dir); |
| 321 | /* Skip a colon at the beginning of the item */ |
| 322 | if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) { |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 323 | memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10); |
| 324 | newlen--; |
| 325 | } else { |
| 326 | memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9); |
| 327 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 328 | rv[newlen-2] = '\n'; |
| 329 | rv[newlen-1] = 0; |
| 330 | } else if ( pathitem[0] >= 14 && strncmp((char *)pathitem+1, "$(APPLICATION)", 14) == 0 ) { |
| 331 | /* This is the application itself */ |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 332 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 333 | if ( (err=PyMac_init_process_location()) != 0 ) { |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 334 | printf("Cannot get application location, error %d\n", err); |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 335 | exit(1); |
| 336 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 337 | |
| 338 | newlen = strlen(rv) + strlen(PyMac_ApplicationPath) + 2; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 339 | if( (rv=realloc(rv, newlen)) == NULL) |
| 340 | goto out; |
| 341 | strcpy(rv+strlen(rv), PyMac_ApplicationPath); |
| 342 | rv[newlen-2] = '\n'; |
| 343 | rv[newlen-1] = 0; |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 344 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 345 | } else { |
| 346 | /* Use as-is */ |
| 347 | newlen = strlen(rv) + (pathitem[0]) + 2; |
| 348 | if( (rv=realloc(rv, newlen)) == NULL) |
| 349 | goto out; |
| 350 | memcpy(rv+strlen(rv), pathitem+1, pathitem[0]); |
| 351 | rv[newlen-2] = '\n'; |
| 352 | rv[newlen-1] = 0; |
| 353 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 354 | } |
| 355 | if( strlen(rv) == 1) { |
| 356 | free(rv); |
| 357 | rv = NULL; |
| 358 | } |
| 359 | if ( rv ) { |
| 360 | rv[strlen(rv)-1] = 0; |
| 361 | rv++; |
| 362 | } |
| 363 | out: |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 364 | if ( prefrh != -1) CloseResFile(prefrh); |
| 365 | UseResFile(oldrh); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 366 | return rv; |
| 367 | } |
| 368 | #endif /* !USE_BUILTIN_PATH */ |
| 369 | |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 370 | void |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 371 | PyMac_PreferenceOptions(PyMac_PrefRecord *pr) |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 372 | { |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 373 | short oldrh, prefrh = -1; |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 374 | Handle handle; |
| 375 | int size; |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 376 | PyMac_PrefRecord *p; |
| 377 | int action; |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 378 | |
| 379 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 380 | oldrh = CurResFile(); |
| 381 | |
| 382 | /* Attempt to load overrides from application */ |
| 383 | UseResFile(PyMac_AppRefNum); |
| 384 | handle = Get1Resource('Popt', PYTHONOPTIONSOVERRIDE_ID); |
| 385 | UseResFile(oldrh); |
| 386 | |
| 387 | /* Otherwise get options from prefs file or any other open resource file */ |
| 388 | if ( handle == NULL ) { |
| 389 | prefrh = PyMac_OpenPrefFile(); |
| 390 | handle = GetResource('Popt', PYTHONOPTIONS_ID); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 391 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 392 | if ( handle == NULL ) { |
| 393 | return; |
| 394 | } |
| 395 | HLock(handle); |
| 396 | size = GetHandleSize(handle); |
| 397 | p = (PyMac_PrefRecord *)*handle; |
| 398 | if ( p->version == POPT_VERSION_CURRENT && size == sizeof(PyMac_PrefRecord) ) { |
| 399 | *pr = *p; |
| 400 | } else { |
| 401 | action = CautionAlert(BADPREFERENCES_ID, NULL); |
| 402 | if ( action == BADPREF_DELETE ) { |
| 403 | OSErr err; |
| 404 | |
| 405 | RemoveResource(handle); |
| 406 | if ( (err=ResError()) ) printf("RemoveResource: %d\n", err); |
| 407 | if ( prefrh != -1 ) { |
| 408 | UpdateResFile(prefrh); |
| 409 | if ( (err=ResError()) ) printf("UpdateResFile: %d\n", err); |
| 410 | } |
| 411 | } else if ( action == BADPREF_QUIT ) |
| 412 | exit(1); |
| 413 | } |
| 414 | HUnlock(handle); |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 415 | |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 416 | if ( prefrh != -1) CloseResFile(prefrh); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 417 | UseResFile(oldrh); |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 418 | } |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 419 | |
| 420 | #ifdef USE_GUSI |
| 421 | void |
| 422 | PyMac_SetGUSIOptions() |
| 423 | { |
| 424 | Handle h; |
| 425 | short oldrh, prefrh = -1; |
| 426 | |
| 427 | oldrh = CurResFile(); |
| 428 | |
| 429 | /* Try override from the application resource fork */ |
| 430 | UseResFile(PyMac_AppRefNum); |
| 431 | h = Get1Resource('GU\267I', GUSIOPTIONSOVERRIDE_ID); |
| 432 | UseResFile(oldrh); |
| 433 | |
| 434 | /* If that didn't work try nonoverride from anywhere */ |
| 435 | if ( h == NULL ) { |
| 436 | prefrh = PyMac_OpenPrefFile(); |
| 437 | h = GetResource('GU\267I', GUSIOPTIONS_ID); |
| 438 | } |
| 439 | if ( h ) GUSILoadConfiguration(h); |
| 440 | if ( prefrh != -1) CloseResFile(prefrh); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame^] | 441 | UseResFile(oldrh); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 442 | } |
| 443 | #endif /* USE_GUSI */ |