summaryrefslogtreecommitdiffstats
path: root/Mac/Python/macgetpath.c
blob: 12fcf71e8403a22c4fbe4c0ad7b92a5faf5f300c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "Python.h"
#include "osdefs.h"

#include "pythonresources.h"


/* Return the initial python search path.  This is called once from
** initsys() to initialize sys.path.
**
** If USE_BUILTIN_PATH is defined the path defined here is used
** (after prepending the python home dir to each item).
** If it is not defined the path is gotten from a resource in the
** Preferences file.
**
** XXXX This code needs cleaning up. The routines here have moved
** around quite a bit, and they're pretty messy for that reason.
*/

#include <Files.h>
#include <Aliases.h>
#include <Folders.h>
#include <Resources.h>
#include <TextUtils.h>

#define PYTHONPATH "\
:\n\
:Lib\n\
:Lib:stdwin\n\
:Lib:test\n\
:Lib:mac"


char *
getpythonpath()
{
	/* Modified by Jack to do something a bit more sensible:
	** - Prepend the python home-directory (which is obtained from a Preferences
	**   resource)
	** - Add :
	*/
	static char *pythonpath;
	char *curwd;
	char *p, *endp;
	int newlen;
	extern char *PyMac_GetPythonDir();
#ifndef USE_BUILTIN_PATH
	extern char *PyMac_GetPythonPath();
#endif
	
	if ( pythonpath ) return pythonpath;
	curwd = PyMac_GetPythonDir();
#ifndef USE_BUILTIN_PATH
	if ( pythonpath = PyMac_GetPythonPath(curwd) )
		return pythonpath;
	printf("Warning: No pythonpath resource found, using builtin default\n");
#endif
	p = PYTHONPATH;
	endp = p;
	pythonpath = malloc(2);
	if ( pythonpath == NULL ) return PYTHONPATH;
	strcpy(pythonpath, ":");
	while (*endp) {
		endp = strchr(p, '\n');
		if ( endp == NULL )
			endp = p + strlen(p);
		newlen = strlen(pythonpath) + 1 + strlen(curwd) + (endp-p);
		pythonpath = realloc(pythonpath, newlen+1);
		if ( pythonpath == NULL ) return PYTHONPATH;
		strcat(pythonpath, "\n");
		if ( *p == ':' ) {
			p++;
			strcat(pythonpath, curwd);
			strncat(pythonpath, p, (endp-p));
			newlen--;   /* Ok, ok, we've allocated one byte too much */
		} else {
			/* We've allocated too much in this case */
			newlen -= strlen(curwd);
			pythonpath = realloc(pythonpath, newlen+1);
			if ( pythonpath == NULL ) return PYTHONPATH;
			strncat(pythonpath, p, (endp-p));
		}
		pythonpath[newlen] = '\0';
		p = endp + 1;
	}
	return pythonpath;
}


/*
** Return the name of the Python directory
*/
char *
PyMac_GetPythonDir()
{
    int item;
    static char name[256];
    AliasHandle handle;
    FSSpec dirspec;
    int ok = 0;
    Boolean modified = 0, cannotmodify = 0;
    short oldrh, prefrh;
    short prefdirRefNum;
    long prefdirDirID;
    
    /*
    ** Remember old resource file and try to open preferences file
    ** in the preferences folder. If it doesn't exist we try to create
    ** it. If anything fails here we limp on, but set cannotmodify so
    ** we don't try to store things later on.
    */
    oldrh = CurResFile();
    if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
    				&prefdirDirID) != noErr ) {
    	/* Something wrong with preferences folder */
    	cannotmodify = 1;
    } else {
    	(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
		if ( prefrh == -1 ) {
#ifdef USE_MAC_MODPREFS
			/* It doesn't exist. Try to create it */
			FSpCreateResFile(&dirspec, 'PYTH', 'pref', 0);
	  		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
			if ( prefrh == -1 ) {
				/* This is strange, what should we do now? */
				cannotmodify = 1;
			} else {
				UseResFile(prefrh);
    		}
#else
			printf("Error: no Preferences file. Attempting to limp on...\n");
			name[0] = 0;
			getwd(name);
			return name;
#endif
    	}
    }
    /* So, we've opened our preferences file, we hope. Look for the alias */
    handle = (AliasHandle)Get1Resource('alis', PYTHONHOME_ID);
    if ( handle ) {
    	/* It exists. Resolve it (possibly updating it) */
    	if ( ResolveAlias(NULL, handle, &dirspec, &modified) == noErr ) {
    		ok = 1;
    	}
    }
    if ( !ok ) {
#ifdef USE_MAC_MODPREFS
    	/* No luck, so far. ask the user for help */
	    item = Alert(NOPYTHON_ALERT, NULL);
	    if ( item == YES_ITEM ) {
	    	/* The user wants to point us to a directory. Let her do so */
	    	ok = PyMac_GetDirectory(&dirspec);
	    	if ( ok )
	    		modified = 1;
	    } else if ( item == CURWD_ITEM ) {
	    	/* The user told us the current directory is fine. Build an FSSpec for it */
	    	if ( getwd(name) ) {
	    		if ( FSMakeFSSpec(0, 0, Pstring(name), &dirspec) == 0 ) {
	    			ok = 1;
	    			modified = 1;
	    		}
	    	}
	    }
	    if ( handle ) {
	    	/* Set the (old, invalid) alias record to the new data */
	    	UpdateAlias(NULL, &dirspec, handle, &modified);
	    }
#else
		printf("Error: corrupted Preferences file. Attempting to limp on...\n");
		name[0] = 0;
		getwd(name);
		return name;
#endif
    }
#ifdef USE_MAC_MODPREFS
    if ( ok && modified && !cannotmodify) {
    	/* We have a new, valid fsspec and we can update the preferences file. Do so. */
    	if ( !handle ) {
    		if (NewAlias(NULL, &dirspec, &handle) == 0 )
    			AddResource((Handle)handle, 'alis', PYTHONHOME_ID, "\p");
    	} else {
    		ChangedResource((Handle)handle);
    	}
    	UpdateResFile(prefrh);
    }
#endif
    if ( !cannotmodify ) {
    	/* This means we have the resfile open. Close it. */
    	CloseResFile(prefrh);
    }
    /* Back to the old resource file */
    UseResFile(oldrh);
    /* Now turn the fsspec into a path to give back to our caller */
    if ( ok ) {
    	ok = (nfullpath(&dirspec, name) == 0);
    	if ( ok ) strcat(name, ":");
    }
    if ( !ok ) {
		/* If all fails, we return the current directory */
		name[0] = 0;
		(void)getwd(name);
	}
	return name;
}

#ifndef USE_BUILTIN_PATH
char *
PyMac_GetPythonPath(dir)
char *dir;
{
    FSSpec dirspec;
    short oldrh, prefrh = -1;
    short prefdirRefNum;
    long prefdirDirID;
    char *rv;
    int i, newlen;
    Str255 pathitem;
    
    /*
    ** Remember old resource file and try to open preferences file
    ** in the preferences folder.
    */
    oldrh = CurResFile();
    if ( FindFolder(kOnSystemDisk, 'pref', kDontCreateFolder, &prefdirRefNum,
    				&prefdirDirID) == noErr ) {
    	(void)FSMakeFSSpec(prefdirRefNum, prefdirDirID, "\pPython Preferences", &dirspec);
		prefrh = FSpOpenResFile(&dirspec, fsRdWrShPerm);
    }
    /* At this point, we may or may not have the preferences file open, and it
    ** may or may not contain a sys.path STR# resource. We don't care, if it doesn't
    ** exist we use the one from the application (the default).
    ** We put an initial '\n' in front of the path that we don't return to the caller
    */
    if( (rv = malloc(2)) == NULL )
    	goto out;
    strcpy(rv, "\n");
    for(i=1; ; i++) {
    	GetIndString(pathitem, PYTHONPATH_ID, i);
    	if( pathitem[0] == 0 )
    		break;
    	if ( pathitem[0] >= 9 && strncmp((char *)pathitem+1, "$(PYTHON)", 9) == 0 ) {
    		/* We have to put the directory in place */
    		newlen = strlen(rv) + strlen(dir) + (pathitem[0]-9) + 2;
    		if( (rv=realloc(rv, newlen)) == NULL)
    			goto out;
    		strcat(rv, dir);
    		/* Skip a colon at the beginning of the item */
    		if ( pathitem[0] > 9 && pathitem[1+9] == ':' ) {
				memcpy(rv+strlen(rv), pathitem+1+10, pathitem[0]-10);
				newlen--;
			} else {
				memcpy(rv+strlen(rv), pathitem+1+9, pathitem[0]-9);
			}
    		rv[newlen-2] = '\n';
    		rv[newlen-1] = 0;
    	} else {
    		/* Use as-is */
    		newlen = strlen(rv) + (pathitem[0]) + 2;
    		if( (rv=realloc(rv, newlen)) == NULL)
    			goto out;
    		memcpy(rv+strlen(rv), pathitem+1, pathitem[0]);
    		rv[newlen-2] = '\n';
    		rv[newlen-1] = 0;
    	}
	}
	if( strlen(rv) == 1) {
		free(rv);
		rv = NULL;
	}
	if ( rv ) {
		rv[strlen(rv)-1] = 0;
		rv++;
	}
out:
	if ( prefrh ) {
		CloseResFile(prefrh);
		UseResFile(oldrh);
	}
	return rv;
}
#endif /* !USE_BUILTIN_PATH */