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