Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / PC / os2vacpp / getpathp.c
blob5860e752c350a791abd87f970fb2658753b97013
2 /* Return the initial module search path. */
3 /* Used by DOS, OS/2, Windows 3.1. Works on NT too. */
5 #include "Python.h"
6 #include "osdefs.h"
8 #ifdef MS_WIN32
9 #include <windows.h>
10 extern BOOL PyWin_IsWin32s(void);
11 #endif
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <string.h>
17 #if HAVE_UNISTD_H
18 #include <unistd.h>
19 #endif /* HAVE_UNISTD_H */
21 /* Search in some common locations for the associated Python libraries.
23 * Two directories must be found, the platform independent directory
24 * (prefix), containing the common .py and .pyc files, and the platform
25 * dependent directory (exec_prefix), containing the shared library
26 * modules. Note that prefix and exec_prefix can be the same directory,
27 * but for some installations, they are different.
29 * Py_GetPath() tries to return a sensible Python module search path.
31 * First, we look to see if the executable is in a subdirectory of
32 * the Python build directory. We calculate the full path of the
33 * directory containing the executable as progpath. We work backwards
34 * along progpath and look for $dir/Modules/Setup.in, a distinctive
35 * landmark. If found, we use $dir/Lib as $root. The returned
36 * Python path is the compiled #define PYTHONPATH with all the initial
37 * "./lib" replaced by $root.
39 * Otherwise, if there is a PYTHONPATH environment variable, we return that.
41 * Otherwise we try to find $progpath/lib/os.py, and if found, then
42 * root is $progpath/lib, and we return Python path as compiled PYTHONPATH
43 * with all "./lib" replaced by $root (as above).
47 #ifndef LANDMARK
48 #define LANDMARK "lib\\os.py"
49 #endif
51 static char prefix[MAXPATHLEN+1];
52 static char exec_prefix[MAXPATHLEN+1];
53 static char progpath[MAXPATHLEN+1];
54 static char *module_search_path = NULL;
57 static int
58 is_sep(char ch) /* determine if "ch" is a separator character */
60 #ifdef ALTSEP
61 return ch == SEP || ch == ALTSEP;
62 #else
63 return ch == SEP;
64 #endif
68 static void
69 reduce(char *dir)
71 int i = strlen(dir);
72 while (i > 0 && !is_sep(dir[i]))
73 --i;
74 dir[i] = '\0';
78 static int
79 exists(char *filename)
81 struct stat buf;
82 return stat(filename, &buf) == 0;
86 static void
87 join(char *buffer, char *stuff)
89 int n, k;
90 if (is_sep(stuff[0]))
91 n = 0;
92 else {
93 n = strlen(buffer);
94 if (n > 0 && !is_sep(buffer[n-1]) && n < MAXPATHLEN)
95 buffer[n++] = SEP;
97 k = strlen(stuff);
98 if (n + k > MAXPATHLEN)
99 k = MAXPATHLEN - n;
100 strncpy(buffer+n, stuff, k);
101 buffer[n+k] = '\0';
105 static int
106 search_for_prefix(char *argv0_path, char *landmark)
108 int n;
110 /* Search from argv0_path, until root is found */
111 strcpy(prefix, argv0_path);
112 do {
113 n = strlen(prefix);
114 join(prefix, landmark);
115 if (exists(prefix)) {
116 prefix[n] = '\0';
117 return 1;
119 prefix[n] = '\0';
120 reduce(prefix);
121 } while (prefix[0]);
122 return 0;
125 #ifdef MS_WIN32
126 #include "malloc.h" // for alloca - see comments below!
127 extern const char *PyWin_DLLVersionString; // a string loaded from the DLL at startup.
130 /* Load a PYTHONPATH value from the registry.
131 Load from either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
133 Returns NULL, or a pointer that should be freed.
136 static char *
137 getpythonregpath(HKEY keyBase, BOOL bWin32s)
139 HKEY newKey = 0;
140 DWORD nameSize = 0;
141 DWORD dataSize = 0;
142 DWORD numEntries = 0;
143 LONG rc;
144 char *retval = NULL;
145 char *dataBuf;
146 const char keyPrefix[] = "Software\\Python\\PythonCore\\";
147 const char keySuffix[] = "\\PythonPath";
148 int versionLen;
149 char *keyBuf;
151 // Tried to use sysget("winver") but here is too early :-(
152 versionLen = strlen(PyWin_DLLVersionString);
153 // alloca == no free required, but memory only local to fn.
154 // also no heap fragmentation! Am I being silly?
155 keyBuf = alloca(sizeof(keyPrefix)-1 + versionLen + sizeof(keySuffix)); // chars only, plus 1 NULL.
156 // lots of constants here for the compiler to optimize away :-)
157 memcpy(keyBuf, keyPrefix, sizeof(keyPrefix)-1);
158 memcpy(keyBuf+sizeof(keyPrefix)-1, PyWin_DLLVersionString, versionLen);
159 memcpy(keyBuf+sizeof(keyPrefix)-1+versionLen, keySuffix, sizeof(keySuffix)); // NULL comes with this one!
161 rc=RegOpenKey(keyBase,
162 keyBuf,
163 &newKey);
164 if (rc==ERROR_SUCCESS) {
165 RegQueryInfoKey(newKey, NULL, NULL, NULL, NULL, NULL, NULL,
166 &numEntries, &nameSize, &dataSize, NULL, NULL);
168 if (bWin32s && numEntries==0 && dataSize==0) {
169 /* must hardcode for Win32s */
170 numEntries = 1;
171 dataSize = 511;
173 if (numEntries) {
174 /* Loop over all subkeys. */
175 /* Win32s doesnt know how many subkeys, so we do
176 it twice */
177 char keyBuf[MAX_PATH+1];
178 int index = 0;
179 int off = 0;
180 for(index=0;;index++) {
181 long reqdSize = 0;
182 DWORD rc = RegEnumKey(newKey,
183 index, keyBuf, MAX_PATH+1);
184 if (rc) break;
185 rc = RegQueryValue(newKey, keyBuf, NULL, &reqdSize);
186 if (rc) break;
187 if (bWin32s && reqdSize==0) reqdSize = 512;
188 dataSize += reqdSize + 1; /* 1 for the ";" */
190 dataBuf = malloc(dataSize+1);
191 if (dataBuf==NULL)
192 return NULL; /* pretty serious? Raise error? */
193 /* Now loop over, grabbing the paths.
194 Subkeys before main library */
195 for(index=0;;index++) {
196 int adjust;
197 long reqdSize = dataSize;
198 DWORD rc = RegEnumKey(newKey,
199 index, keyBuf,MAX_PATH+1);
200 if (rc) break;
201 rc = RegQueryValue(newKey,
202 keyBuf, dataBuf+off, &reqdSize);
203 if (rc) break;
204 if (reqdSize>1) {
205 /* If Nothing, or only '\0' copied. */
206 adjust = strlen(dataBuf+off);
207 dataSize -= adjust;
208 off += adjust;
209 dataBuf[off++] = ';';
210 dataBuf[off] = '\0';
211 dataSize--;
214 /* Additionally, win32s doesnt work as expected, so
215 the specific strlen() is required for 3.1. */
216 rc = RegQueryValue(newKey, "", dataBuf+off, &dataSize);
217 if (rc==ERROR_SUCCESS) {
218 if (strlen(dataBuf)==0)
219 free(dataBuf);
220 else
221 retval = dataBuf; /* caller will free */
223 else
224 free(dataBuf);
227 if (newKey)
228 RegCloseKey(newKey);
229 return retval;
231 #endif /* MS_WIN32 */
233 static void
234 get_progpath(void)
236 extern char *Py_GetProgramName(void);
237 char *path = getenv("PATH");
238 char *prog = Py_GetProgramName();
240 #ifdef MS_WIN32
241 if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
242 return;
243 #endif
244 if (prog == NULL || *prog == '\0')
245 prog = "python";
247 /* If there is no slash in the argv0 path, then we have to
248 * assume python is on the user's $PATH, since there's no
249 * other way to find a directory to start the search from. If
250 * $PATH isn't exported, you lose.
252 #ifdef ALTSEP
253 if (strchr(prog, SEP) || strchr(prog, ALTSEP))
254 #else
255 if (strchr(prog, SEP))
256 #endif
257 strcpy(progpath, prog);
258 else if (path) {
259 while (1) {
260 char *delim = strchr(path, DELIM);
262 if (delim) {
263 int len = delim - path;
264 strncpy(progpath, path, len);
265 *(progpath + len) = '\0';
267 else
268 strcpy(progpath, path);
270 join(progpath, prog);
271 if (exists(progpath))
272 break;
274 if (!delim) {
275 progpath[0] = '\0';
276 break;
278 path = delim + 1;
281 else
282 progpath[0] = '\0';
285 static void
286 calculate_path(void)
288 char argv0_path[MAXPATHLEN+1];
289 char *buf;
290 int bufsz;
291 char *pythonhome = Py_GetPythonHome();
292 char *envpath = Py_GETENV("PYTHONPATH");
293 #ifdef MS_WIN32
294 char *machinepath, *userpath;
296 /* Are we running under Windows 3.1(1) Win32s? */
297 if (PyWin_IsWin32s()) {
298 /* Only CLASSES_ROOT is supported */
299 machinepath = getpythonregpath(HKEY_CLASSES_ROOT, TRUE);
300 userpath = NULL;
301 } else {
302 machinepath = getpythonregpath(HKEY_LOCAL_MACHINE, FALSE);
303 userpath = getpythonregpath(HKEY_CURRENT_USER, FALSE);
305 #endif
307 get_progpath();
308 strcpy(argv0_path, progpath);
309 reduce(argv0_path);
310 if (pythonhome == NULL || *pythonhome == '\0') {
311 if (search_for_prefix(argv0_path, LANDMARK))
312 pythonhome = prefix;
313 else
314 pythonhome = NULL;
316 else {
317 char *delim;
319 strcpy(prefix, pythonhome);
321 /* Extract Any Optional Trailing EXEC_PREFIX */
322 /* e.g. PYTHONHOME=<prefix>:<exec_prefix> */
323 delim = strchr(prefix, DELIM);
324 if (delim) {
325 *delim = '\0';
326 strcpy(exec_prefix, delim+1);
327 } else
328 strcpy(exec_prefix, EXEC_PREFIX);
331 if (envpath && *envpath == '\0')
332 envpath = NULL;
334 /* We need to construct a path from the following parts:
335 (1) the PYTHONPATH environment variable, if set;
336 (2) for Win32, the machinepath and userpath, if set;
337 (3) the PYTHONPATH config macro, with the leading "."
338 of each component replaced with pythonhome, if set;
339 (4) the directory containing the executable (argv0_path).
340 The length calculation calculates #3 first.
343 /* Calculate size of return buffer */
344 if (pythonhome != NULL) {
345 char *p;
346 bufsz = 1;
347 for (p = PYTHONPATH; *p; p++) {
348 if (*p == DELIM)
349 bufsz++; /* number of DELIM plus one */
351 bufsz *= strlen(pythonhome);
353 else
354 bufsz = 0;
355 bufsz += strlen(PYTHONPATH) + 1;
356 if (envpath != NULL)
357 bufsz += strlen(envpath) + 1;
358 bufsz += strlen(argv0_path) + 1;
359 #ifdef MS_WIN32
360 if (machinepath)
361 bufsz += strlen(machinepath) + 1;
362 if (userpath)
363 bufsz += strlen(userpath) + 1;
364 #endif
366 module_search_path = buf = malloc(bufsz);
367 if (buf == NULL) {
368 /* We can't exit, so print a warning and limp along */
369 fprintf(stderr, "Can't malloc dynamic PYTHONPATH.\n");
370 if (envpath) {
371 fprintf(stderr, "Using default static $PYTHONPATH.\n");
372 module_search_path = envpath;
374 else {
375 fprintf(stderr, "Using environment $PYTHONPATH.\n");
376 module_search_path = PYTHONPATH;
378 return;
381 if (envpath) {
382 strcpy(buf, envpath);
383 buf = strchr(buf, '\0');
384 *buf++ = DELIM;
386 #ifdef MS_WIN32
387 if (machinepath) {
388 strcpy(buf, machinepath);
389 buf = strchr(buf, '\0');
390 *buf++ = DELIM;
392 if (userpath) {
393 strcpy(buf, userpath);
394 buf = strchr(buf, '\0');
395 *buf++ = DELIM;
397 #endif
398 if (pythonhome == NULL) {
399 strcpy(buf, PYTHONPATH);
400 buf = strchr(buf, '\0');
402 else {
403 char *p = PYTHONPATH;
404 char *q;
405 int n;
406 for (;;) {
407 q = strchr(p, DELIM);
408 if (q == NULL)
409 n = strlen(p);
410 else
411 n = q-p;
412 if (p[0] == '.' && is_sep(p[1])) {
413 strcpy(buf, pythonhome);
414 buf = strchr(buf, '\0');
415 p++;
416 n--;
418 strncpy(buf, p, n);
419 buf += n;
420 if (q == NULL)
421 break;
422 *buf++ = DELIM;
423 p = q+1;
426 if (argv0_path) {
427 *buf++ = DELIM;
428 strcpy(buf, argv0_path);
429 buf = strchr(buf, '\0');
431 *buf = '\0';
435 /* External interface */
437 char *
438 Py_GetPath(void)
440 if (!module_search_path)
441 calculate_path();
443 return module_search_path;
446 char *
447 Py_GetPrefix(void)
449 if (!module_search_path)
450 calculate_path();
452 return prefix;
455 char *
456 Py_GetExecPrefix(void)
458 if (!module_search_path)
459 calculate_path();
461 return exec_prefix;
464 char *
465 Py_GetProgramFullPath(void)
467 if (!module_search_path)
468 calculate_path();
470 return progpath;