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