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