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 | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 57 | #ifdef USE_GUSI1 |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 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; |
Just van Rossum | 26a69db | 1999-02-02 15:49:03 +0000 | [diff] [blame] | 76 | long pyprefdirDirID; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 77 | Handle namehandle; |
Just van Rossum | 26a69db | 1999-02-02 15:49:03 +0000 | [diff] [blame] | 78 | OSErr err; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 79 | |
| 80 | if ( !diditbefore ) { |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 81 | if ( (namehandle=GetNamedResource('STR ', PREFFILENAME_NAME)) == NULL ) { |
| 82 | (void)StopAlert(NOPREFNAME_ID, NULL); |
| 83 | exit(1); |
| 84 | } |
| 85 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 86 | if ( **namehandle == '\0' ) { |
| 87 | /* Empty string means don't use preferences file */ |
| 88 | rv = 0; |
| 89 | } else { |
| 90 | /* There is a filename, construct the fsspec */ |
Just van Rossum | 26a69db | 1999-02-02 15:49:03 +0000 | [diff] [blame] | 91 | if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum, |
| 92 | &prefdirDirID) != noErr ) { |
| 93 | /* Something wrong with preferences folder */ |
| 94 | (void)StopAlert(NOPREFDIR_ID, NULL); |
| 95 | exit(1); |
| 96 | } |
| 97 | /* make fsspec for the "Python" folder inside the prefs folder */ |
| 98 | err = FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython", &fss); |
| 99 | if (err == fnfErr) { |
| 100 | /* it doesn't exist: create it */ |
| 101 | err = FSpDirCreate(&fss, smSystemScript, &pyprefdirDirID); |
| 102 | } else { |
| 103 | /* it does exist, now find out the dirID of the Python prefs folder, brrr. */ |
| 104 | CInfoPBRec info; |
| 105 | info.dirInfo.ioVRefNum = fss.vRefNum; |
| 106 | info.dirInfo.ioDrDirID = fss.parID; |
| 107 | info.dirInfo.ioNamePtr = fss.name; |
| 108 | info.dirInfo.ioFDirIndex = 0; |
| 109 | info.dirInfo.ioACUser = 0; |
| 110 | err = PBGetCatInfo(&info, 0); |
| 111 | if (err == noErr) { |
| 112 | pyprefdirDirID = info.dirInfo.ioDrDirID; |
| 113 | } |
| 114 | } |
| 115 | if (err != noErr) { |
| 116 | (void)StopAlert(NOPREFDIR_ID, NULL); |
| 117 | exit(1); |
| 118 | } |
| 119 | HLock(namehandle); |
| 120 | err = FSMakeFSSpec(fss.vRefNum, pyprefdirDirID, (unsigned char *)*namehandle, &fss); |
| 121 | HUnlock(namehandle); |
| 122 | if (err != noErr && err != fnfErr) { |
| 123 | (void)StopAlert(NOPREFDIR_ID, NULL); |
| 124 | exit(1); |
| 125 | } |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 126 | } |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 127 | ReleaseResource(namehandle); |
| 128 | diditbefore = 1; |
| 129 | } |
| 130 | *fssp = fss; |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 131 | return rv; |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 132 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 133 | |
| 134 | char * |
Jack Jansen | a547dca | 1996-07-10 15:48:25 +0000 | [diff] [blame] | 135 | Py_GetPath() |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 136 | { |
| 137 | /* Modified by Jack to do something a bit more sensible: |
| 138 | ** - Prepend the python home-directory (which is obtained from a Preferences |
| 139 | ** resource) |
| 140 | ** - Add : |
| 141 | */ |
| 142 | static char *pythonpath; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 143 | char *p, *endp; |
| 144 | int newlen; |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 145 | char *curwd; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 146 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 01fbc68 | 1996-02-28 15:42:47 +0000 | [diff] [blame] | 147 | staticforward char *PyMac_GetPythonPath(); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 148 | #endif |
| 149 | |
| 150 | if ( pythonpath ) return pythonpath; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 151 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 152 | if ( pythonpath = PyMac_GetPythonPath() ) |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 153 | return pythonpath; |
| 154 | printf("Warning: No pythonpath resource found, using builtin default\n"); |
| 155 | #endif |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 156 | curwd = PyMac_GetPythonDir(); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 157 | p = PYTHONPATH; |
| 158 | endp = p; |
| 159 | pythonpath = malloc(2); |
| 160 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 161 | strcpy(pythonpath, ":"); |
| 162 | while (*endp) { |
| 163 | endp = strchr(p, '\n'); |
| 164 | if ( endp == NULL ) |
| 165 | endp = p + strlen(p); |
| 166 | newlen = strlen(pythonpath) + 1 + strlen(curwd) + (endp-p); |
| 167 | pythonpath = realloc(pythonpath, newlen+1); |
| 168 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 169 | strcat(pythonpath, "\n"); |
| 170 | if ( *p == ':' ) { |
| 171 | p++; |
| 172 | strcat(pythonpath, curwd); |
| 173 | strncat(pythonpath, p, (endp-p)); |
| 174 | newlen--; /* Ok, ok, we've allocated one byte too much */ |
| 175 | } else { |
| 176 | /* We've allocated too much in this case */ |
| 177 | newlen -= strlen(curwd); |
| 178 | pythonpath = realloc(pythonpath, newlen+1); |
| 179 | if ( pythonpath == NULL ) return PYTHONPATH; |
| 180 | strncat(pythonpath, p, (endp-p)); |
| 181 | } |
| 182 | pythonpath[newlen] = '\0'; |
| 183 | p = endp + 1; |
| 184 | } |
| 185 | return pythonpath; |
| 186 | } |
| 187 | |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 188 | |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 189 | /* |
| 190 | ** Open/create the Python Preferences file, return the handle |
| 191 | */ |
Jack Jansen | ee08104 | 2000-04-21 23:53:37 +0000 | [diff] [blame] | 192 | short |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 193 | PyMac_OpenPrefFile() |
| 194 | { |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 195 | AliasHandle handle; |
| 196 | FSSpec dirspec; |
| 197 | short prefrh; |
| 198 | OSErr err; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 199 | |
Jack Jansen | ac82b6a | 1998-07-13 13:38:29 +0000 | [diff] [blame] | 200 | if ( !getpreffilefss(&dirspec)) |
| 201 | return -1; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 202 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 203 | if ( prefrh < 0 ) { |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 204 | #if 0 |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 205 | action = CautionAlert(NOPREFFILE_ID, NULL); |
| 206 | if ( action == NOPREFFILE_NO ) |
| 207 | exit(1); |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 208 | #endif |
Jack Jansen | 532e3c2 | 1996-02-21 15:36:26 +0000 | [diff] [blame] | 209 | FSpCreateResFile(&dirspec, 'Pyth', 'pref', 0); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 210 | prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm); |
| 211 | if ( prefrh == -1 ) { |
| 212 | /* This "cannot happen":-) */ |
Jack Jansen | b39be21 | 1995-09-01 11:48:10 +0000 | [diff] [blame] | 213 | printf("Cannot create preferences file, error %d\n", ResError()); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 214 | exit(1); |
| 215 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 216 | if ( (err=PyMac_init_process_location()) != 0 ) { |
| 217 | printf("Cannot get application location, error %d\n", err); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 218 | exit(1); |
| 219 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 220 | dirspec = PyMac_ApplicationFSSpec; |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 221 | dirspec.name[0] = 0; |
Jack Jansen | b39be21 | 1995-09-01 11:48:10 +0000 | [diff] [blame] | 222 | if ((err=NewAlias(NULL, &dirspec, &handle)) != 0 ) { |
| 223 | printf("Cannot make alias to application directory, error %d\n", err); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 224 | exit(1); |
| 225 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 226 | AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p"); |
| 227 | UpdateResFile(prefrh); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 228 | |
| 229 | } else { |
| 230 | UseResFile(prefrh); |
| 231 | } |
| 232 | return prefrh; |
| 233 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 234 | |
| 235 | /* |
| 236 | ** Return the name of the Python directory |
| 237 | */ |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 238 | char * |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 239 | PyMac_GetPythonDir() |
| 240 | { |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 241 | static int diditbefore = 0; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 242 | static char name[256] = {':', '\0'}; |
| 243 | AliasHandle handle; |
| 244 | FSSpec dirspec; |
| 245 | Boolean modified = 0; |
| 246 | short oldrh, prefrh = -1, homerh; |
| 247 | |
| 248 | if ( diditbefore ) |
| 249 | return name; |
| 250 | |
| 251 | oldrh = CurResFile(); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 252 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 253 | /* First look for an override in the application file */ |
| 254 | UseResFile(PyMac_AppRefNum); |
| 255 | handle = (AliasHandle)Get1Resource('alis', PYTHONHOMEOVERRIDE_ID); |
| 256 | UseResFile(oldrh); |
| 257 | if ( handle != NULL ) { |
| 258 | homerh = PyMac_AppRefNum; |
| 259 | } else { |
| 260 | /* Try to open preferences file in the preferences folder. */ |
| 261 | prefrh = PyMac_OpenPrefFile(); |
| 262 | handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID); |
| 263 | if ( handle == NULL ) { |
| 264 | /* (void)StopAlert(BADPREFFILE_ID, NULL); */ |
| 265 | diditbefore=1; |
| 266 | return ":"; |
| 267 | } |
| 268 | homerh = prefrh; |
| 269 | } |
| 270 | /* It exists. Resolve it (possibly updating it) */ |
| 271 | if ( ResolveAlias(NULL, handle, &dirspec, &modified) != noErr ) { |
| 272 | (void)StopAlert(BADPREFFILE_ID, NULL); |
| 273 | diditbefore=1; |
| 274 | return ":"; |
| 275 | } |
| 276 | if ( modified ) { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 277 | ChangedResource((Handle)handle); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 278 | UpdateResFile(homerh); |
| 279 | } |
| 280 | if ( prefrh != -1 ) CloseResFile(prefrh); |
| 281 | UseResFile(oldrh); |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 282 | |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 283 | if ( PyMac_GetFullPath(&dirspec, name) == 0 ) { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 284 | strcat(name, ":"); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 285 | } else { |
Jack Jansen | 41fa7ea | 1995-08-31 13:59:36 +0000 | [diff] [blame] | 286 | /* If all fails, we return the current directory */ |
| 287 | printf("Python home dir exists but I cannot find the pathname!!\n"); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 288 | name[0] = 0; |
| 289 | (void)getwd(name); |
| 290 | } |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 291 | diditbefore = 1; |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 292 | return name; |
| 293 | } |
| 294 | |
| 295 | #ifndef USE_BUILTIN_PATH |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 296 | char * |
Jack Jansen | 83c74df | 1996-10-22 15:25:42 +0000 | [diff] [blame] | 297 | PyMac_GetPythonPath() |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 298 | { |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 299 | short oldrh, prefrh = -1; |
| 300 | char *rv; |
| 301 | int i, newlen; |
| 302 | Str255 pathitem; |
| 303 | int resource_id; |
| 304 | OSErr err; |
| 305 | Handle h; |
| 306 | |
| 307 | oldrh = CurResFile(); |
| 308 | /* |
| 309 | ** This is a bit tricky. We check here whether the application file |
| 310 | ** contains an override. This is to forestall us finding another STR# resource |
| 311 | ** with "our" id and using that for path initialization |
| 312 | */ |
| 313 | UseResFile(PyMac_AppRefNum); |
| 314 | SetResLoad(0); |
| 315 | if ( (h=Get1Resource('STR#', PYTHONPATHOVERRIDE_ID)) ) { |
| 316 | ReleaseResource(h); |
| 317 | resource_id = PYTHONPATHOVERRIDE_ID; |
| 318 | } else { |
| 319 | resource_id = PYTHONPATH_ID; |
| 320 | } |
| 321 | SetResLoad(1); |
| 322 | UseResFile(oldrh); |
| 323 | |
| 324 | /* Open the preferences file only if there is no override */ |
| 325 | if ( resource_id != PYTHONPATHOVERRIDE_ID ) |
| 326 | prefrh = PyMac_OpenPrefFile(); |
| 327 | /* At this point, we may or may not have the preferences file open, and it |
| 328 | ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't |
| 329 | ** exist we use the one from the application (the default). |
| 330 | ** We put an initial '\n' in front of the path that we don't return to the caller |
| 331 | */ |
| 332 | if( (rv = malloc(2)) == NULL ) |
| 333 | goto out; |
| 334 | strcpy(rv, "\n"); |
Jack Jansen | f12e709 | 1996-09-05 15:19:24 +0000 | [diff] [blame] | 335 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 336 | for(i=1; ; i++) { |
| 337 | GetIndString(pathitem, resource_id, i); |
| 338 | if( pathitem[0] == 0 ) |
| 339 | break; |
| 340 | if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) { |
| 341 | /* We have to put the directory in place */ |
| 342 | char *dir = PyMac_GetPythonDir(); |
| 343 | |
| 344 | newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2; |
| 345 | if( (rv=realloc(rv, newlen)) == NULL) |
| 346 | goto out; |
| 347 | strcat(rv, dir); |
| 348 | /* Skip a colon at the beginning of the item */ |
| 349 | if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) { |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 350 | memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10); |
| 351 | newlen--; |
| 352 | } else { |
| 353 | memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9); |
| 354 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 355 | rv[newlen-2] = '\n'; |
| 356 | rv[newlen-1] = 0; |
| 357 | } else if ( pathitem[0] >= 14 && strncmp((char *)pathitem+1, "$(APPLICATION)", 14) == 0 ) { |
| 358 | /* This is the application itself */ |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 359 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 360 | if ( (err=PyMac_init_process_location()) != 0 ) { |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 361 | printf("Cannot get application location, error %d\n", err); |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 362 | exit(1); |
| 363 | } |
Jack Jansen | 26ee126 | 1996-11-09 18:45:18 +0000 | [diff] [blame] | 364 | |
| 365 | newlen = strlen(rv) + strlen(PyMac_ApplicationPath) + 2; |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 366 | if( (rv=realloc(rv, newlen)) == NULL) |
| 367 | goto out; |
| 368 | strcpy(rv+strlen(rv), PyMac_ApplicationPath); |
| 369 | rv[newlen-2] = '\n'; |
| 370 | rv[newlen-1] = 0; |
Jack Jansen | a486a55 | 1996-04-04 15:39:18 +0000 | [diff] [blame] | 371 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 372 | } else { |
| 373 | /* Use as-is */ |
| 374 | newlen = strlen(rv) + (pathitem[0]) + 2; |
| 375 | if( (rv=realloc(rv, newlen)) == NULL) |
| 376 | goto out; |
| 377 | memcpy(rv+strlen(rv), pathitem+1, pathitem[0]); |
| 378 | rv[newlen-2] = '\n'; |
| 379 | rv[newlen-1] = 0; |
| 380 | } |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 381 | } |
| 382 | if( strlen(rv) == 1) { |
| 383 | free(rv); |
| 384 | rv = NULL; |
| 385 | } |
| 386 | if ( rv ) { |
| 387 | rv[strlen(rv)-1] = 0; |
| 388 | rv++; |
| 389 | } |
| 390 | out: |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 391 | if ( prefrh != -1) CloseResFile(prefrh); |
| 392 | UseResFile(oldrh); |
Jack Jansen | 12fce3e | 1995-08-14 12:31:44 +0000 | [diff] [blame] | 393 | return rv; |
| 394 | } |
| 395 | #endif /* !USE_BUILTIN_PATH */ |
| 396 | |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 397 | void |
Jack Jansen | 7d5f9e8 | 1996-09-07 17:09:31 +0000 | [diff] [blame] | 398 | PyMac_PreferenceOptions(PyMac_PrefRecord *pr) |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 399 | { |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 400 | short oldrh, prefrh = -1; |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 401 | Handle handle; |
| 402 | int size; |
Jack Jansen | 5b3c971 | 1997-09-08 13:22:49 +0000 | [diff] [blame] | 403 | PyMac_PrefRecord *p; |
| 404 | int action; |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 405 | |
| 406 | |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 407 | oldrh = CurResFile(); |
| 408 | |
| 409 | /* Attempt to load overrides from application */ |
| 410 | UseResFile(PyMac_AppRefNum); |
| 411 | handle = Get1Resource('Popt', PYTHONOPTIONSOVERRIDE_ID); |
| 412 | UseResFile(oldrh); |
| 413 | |
| 414 | /* Otherwise get options from prefs file or any other open resource file */ |
| 415 | if ( handle == NULL ) { |
| 416 | prefrh = PyMac_OpenPrefFile(); |
| 417 | handle = GetResource('Popt', PYTHONOPTIONS_ID); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 418 | } |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 419 | if ( handle == NULL ) { |
| 420 | return; |
| 421 | } |
| 422 | HLock(handle); |
| 423 | size = GetHandleSize(handle); |
| 424 | p = (PyMac_PrefRecord *)*handle; |
| 425 | if ( p->version == POPT_VERSION_CURRENT && size == sizeof(PyMac_PrefRecord) ) { |
| 426 | *pr = *p; |
| 427 | } else { |
| 428 | action = CautionAlert(BADPREFERENCES_ID, NULL); |
| 429 | if ( action == BADPREF_DELETE ) { |
| 430 | OSErr err; |
| 431 | |
| 432 | RemoveResource(handle); |
| 433 | if ( (err=ResError()) ) printf("RemoveResource: %d\n", err); |
| 434 | if ( prefrh != -1 ) { |
| 435 | UpdateResFile(prefrh); |
| 436 | if ( (err=ResError()) ) printf("UpdateResFile: %d\n", err); |
| 437 | } |
| 438 | } else if ( action == BADPREF_QUIT ) |
| 439 | exit(1); |
| 440 | } |
| 441 | HUnlock(handle); |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 442 | |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 443 | if ( prefrh != -1) CloseResFile(prefrh); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 444 | UseResFile(oldrh); |
Jack Jansen | a4b7e14 | 1996-02-21 16:46:57 +0000 | [diff] [blame] | 445 | } |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 446 | |
Jack Jansen | 2d1306b | 2000-04-07 09:10:49 +0000 | [diff] [blame] | 447 | #ifdef USE_GUSI1 |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 448 | void |
| 449 | PyMac_SetGUSIOptions() |
| 450 | { |
| 451 | Handle h; |
| 452 | short oldrh, prefrh = -1; |
| 453 | |
| 454 | oldrh = CurResFile(); |
| 455 | |
| 456 | /* Try override from the application resource fork */ |
| 457 | UseResFile(PyMac_AppRefNum); |
| 458 | h = Get1Resource('GU\267I', GUSIOPTIONSOVERRIDE_ID); |
| 459 | UseResFile(oldrh); |
| 460 | |
| 461 | /* If that didn't work try nonoverride from anywhere */ |
| 462 | if ( h == NULL ) { |
| 463 | prefrh = PyMac_OpenPrefFile(); |
| 464 | h = GetResource('GU\267I', GUSIOPTIONS_ID); |
| 465 | } |
| 466 | if ( h ) GUSILoadConfiguration(h); |
| 467 | if ( prefrh != -1) CloseResFile(prefrh); |
Jack Jansen | abdf93c | 1998-07-31 09:33:28 +0000 | [diff] [blame] | 468 | UseResFile(oldrh); |
Jack Jansen | 3f7d2b4 | 1996-09-06 22:21:07 +0000 | [diff] [blame] | 469 | } |
Jack Jansen | ee08104 | 2000-04-21 23:53:37 +0000 | [diff] [blame] | 470 | #endif /* USE_GUSI1 */ |