1 | n/a | /* |
---|
2 | n/a | * Copyright (C) 2011-2013 Vinay Sajip. |
---|
3 | n/a | * Licensed to PSF under a contributor agreement. |
---|
4 | n/a | * |
---|
5 | n/a | * Based on the work of: |
---|
6 | n/a | * |
---|
7 | n/a | * Mark Hammond (original author of Python version) |
---|
8 | n/a | * Curt Hagenlocher (job management) |
---|
9 | n/a | */ |
---|
10 | n/a | |
---|
11 | n/a | #include <windows.h> |
---|
12 | n/a | #include <shlobj.h> |
---|
13 | n/a | #include <stdio.h> |
---|
14 | n/a | #include <tchar.h> |
---|
15 | n/a | |
---|
16 | n/a | #define BUFSIZE 256 |
---|
17 | n/a | #define MSGSIZE 1024 |
---|
18 | n/a | |
---|
19 | n/a | /* Build options. */ |
---|
20 | n/a | #define SKIP_PREFIX |
---|
21 | n/a | #define SEARCH_PATH |
---|
22 | n/a | |
---|
23 | n/a | /* Error codes */ |
---|
24 | n/a | |
---|
25 | n/a | #define RC_NO_STD_HANDLES 100 |
---|
26 | n/a | #define RC_CREATE_PROCESS 101 |
---|
27 | n/a | #define RC_BAD_VIRTUAL_PATH 102 |
---|
28 | n/a | #define RC_NO_PYTHON 103 |
---|
29 | n/a | #define RC_NO_MEMORY 104 |
---|
30 | n/a | /* |
---|
31 | n/a | * SCRIPT_WRAPPER is used to choose between two variants of an executable built |
---|
32 | n/a | * from this source file. If not defined, the PEP 397 Python launcher is built; |
---|
33 | n/a | * if defined, a script launcher of the type used by setuptools is built, which |
---|
34 | n/a | * looks for a script name related to the executable name and runs that script |
---|
35 | n/a | * with the appropriate Python interpreter. |
---|
36 | n/a | * |
---|
37 | n/a | * SCRIPT_WRAPPER should be undefined in the source, and defined in a VS project |
---|
38 | n/a | * which builds the setuptools-style launcher. |
---|
39 | n/a | */ |
---|
40 | n/a | #if defined(SCRIPT_WRAPPER) |
---|
41 | n/a | #define RC_NO_SCRIPT 105 |
---|
42 | n/a | #endif |
---|
43 | n/a | |
---|
44 | n/a | /* Just for now - static definition */ |
---|
45 | n/a | |
---|
46 | n/a | static FILE * log_fp = NULL; |
---|
47 | n/a | |
---|
48 | n/a | static wchar_t * |
---|
49 | n/a | skip_whitespace(wchar_t * p) |
---|
50 | n/a | { |
---|
51 | n/a | while (*p && isspace(*p)) |
---|
52 | n/a | ++p; |
---|
53 | n/a | return p; |
---|
54 | n/a | } |
---|
55 | n/a | |
---|
56 | n/a | static void |
---|
57 | n/a | debug(wchar_t * format, ...) |
---|
58 | n/a | { |
---|
59 | n/a | va_list va; |
---|
60 | n/a | |
---|
61 | n/a | if (log_fp != NULL) { |
---|
62 | n/a | va_start(va, format); |
---|
63 | n/a | vfwprintf_s(log_fp, format, va); |
---|
64 | n/a | } |
---|
65 | n/a | } |
---|
66 | n/a | |
---|
67 | n/a | static void |
---|
68 | n/a | winerror(int rc, wchar_t * message, int size) |
---|
69 | n/a | { |
---|
70 | n/a | FormatMessageW( |
---|
71 | n/a | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, |
---|
72 | n/a | NULL, rc, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
---|
73 | n/a | message, size, NULL); |
---|
74 | n/a | } |
---|
75 | n/a | |
---|
76 | n/a | static void |
---|
77 | n/a | error(int rc, wchar_t * format, ... ) |
---|
78 | n/a | { |
---|
79 | n/a | va_list va; |
---|
80 | n/a | wchar_t message[MSGSIZE]; |
---|
81 | n/a | wchar_t win_message[MSGSIZE]; |
---|
82 | n/a | int len; |
---|
83 | n/a | |
---|
84 | n/a | va_start(va, format); |
---|
85 | n/a | len = _vsnwprintf_s(message, MSGSIZE, _TRUNCATE, format, va); |
---|
86 | n/a | |
---|
87 | n/a | if (rc == 0) { /* a Windows error */ |
---|
88 | n/a | winerror(GetLastError(), win_message, MSGSIZE); |
---|
89 | n/a | if (len >= 0) { |
---|
90 | n/a | _snwprintf_s(&message[len], MSGSIZE - len, _TRUNCATE, L": %ls", |
---|
91 | n/a | win_message); |
---|
92 | n/a | } |
---|
93 | n/a | } |
---|
94 | n/a | |
---|
95 | n/a | #if !defined(_WINDOWS) |
---|
96 | n/a | fwprintf(stderr, L"%ls\n", message); |
---|
97 | n/a | #else |
---|
98 | n/a | MessageBox(NULL, message, TEXT("Python Launcher is sorry to say ..."), |
---|
99 | n/a | MB_OK); |
---|
100 | n/a | #endif |
---|
101 | n/a | exit(rc); |
---|
102 | n/a | } |
---|
103 | n/a | |
---|
104 | n/a | /* |
---|
105 | n/a | * This function is here to simplify memory management |
---|
106 | n/a | * and to treat blank values as if they are absent. |
---|
107 | n/a | */ |
---|
108 | n/a | static wchar_t * get_env(wchar_t * key) |
---|
109 | n/a | { |
---|
110 | n/a | /* This is not thread-safe, just like getenv */ |
---|
111 | n/a | static wchar_t buf[BUFSIZE]; |
---|
112 | n/a | DWORD result = GetEnvironmentVariableW(key, buf, BUFSIZE); |
---|
113 | n/a | |
---|
114 | n/a | if (result >= BUFSIZE) { |
---|
115 | n/a | /* Large environment variable. Accept some leakage */ |
---|
116 | n/a | wchar_t *buf2 = (wchar_t*)malloc(sizeof(wchar_t) * (result+1)); |
---|
117 | n/a | if (buf2 == NULL) { |
---|
118 | n/a | error(RC_NO_MEMORY, L"Could not allocate environment buffer"); |
---|
119 | n/a | } |
---|
120 | n/a | GetEnvironmentVariableW(key, buf2, result); |
---|
121 | n/a | return buf2; |
---|
122 | n/a | } |
---|
123 | n/a | |
---|
124 | n/a | if (result == 0) |
---|
125 | n/a | /* Either some error, e.g. ERROR_ENVVAR_NOT_FOUND, |
---|
126 | n/a | or an empty environment variable. */ |
---|
127 | n/a | return NULL; |
---|
128 | n/a | |
---|
129 | n/a | return buf; |
---|
130 | n/a | } |
---|
131 | n/a | |
---|
132 | n/a | #if defined(_WINDOWS) |
---|
133 | n/a | |
---|
134 | n/a | #define PYTHON_EXECUTABLE L"pythonw.exe" |
---|
135 | n/a | |
---|
136 | n/a | #else |
---|
137 | n/a | |
---|
138 | n/a | #define PYTHON_EXECUTABLE L"python.exe" |
---|
139 | n/a | |
---|
140 | n/a | #endif |
---|
141 | n/a | |
---|
142 | n/a | #define MAX_VERSION_SIZE 4 |
---|
143 | n/a | |
---|
144 | n/a | typedef struct { |
---|
145 | n/a | wchar_t version[MAX_VERSION_SIZE]; /* m.n */ |
---|
146 | n/a | int bits; /* 32 or 64 */ |
---|
147 | n/a | wchar_t executable[MAX_PATH]; |
---|
148 | n/a | } INSTALLED_PYTHON; |
---|
149 | n/a | |
---|
150 | n/a | /* |
---|
151 | n/a | * To avoid messing about with heap allocations, just assume we can allocate |
---|
152 | n/a | * statically and never have to deal with more versions than this. |
---|
153 | n/a | */ |
---|
154 | n/a | #define MAX_INSTALLED_PYTHONS 100 |
---|
155 | n/a | |
---|
156 | n/a | static INSTALLED_PYTHON installed_pythons[MAX_INSTALLED_PYTHONS]; |
---|
157 | n/a | |
---|
158 | n/a | static size_t num_installed_pythons = 0; |
---|
159 | n/a | |
---|
160 | n/a | /* |
---|
161 | n/a | * To hold SOFTWARE\Python\PythonCore\X.Y...\InstallPath |
---|
162 | n/a | * The version name can be longer than MAX_VERSION_SIZE, but will be |
---|
163 | n/a | * truncated to just X.Y for comparisons. |
---|
164 | n/a | */ |
---|
165 | n/a | #define IP_BASE_SIZE 40 |
---|
166 | n/a | #define IP_VERSION_SIZE 8 |
---|
167 | n/a | #define IP_SIZE (IP_BASE_SIZE + IP_VERSION_SIZE) |
---|
168 | n/a | #define CORE_PATH L"SOFTWARE\\Python\\PythonCore" |
---|
169 | n/a | |
---|
170 | n/a | static wchar_t * location_checks[] = { |
---|
171 | n/a | L"\\", |
---|
172 | n/a | L"\\PCBuild\\win32\\", |
---|
173 | n/a | L"\\PCBuild\\amd64\\", |
---|
174 | n/a | // To support early 32bit versions of Python that stuck the build binaries |
---|
175 | n/a | // directly in PCBuild... |
---|
176 | n/a | L"\\PCBuild\\", |
---|
177 | n/a | NULL |
---|
178 | n/a | }; |
---|
179 | n/a | |
---|
180 | n/a | static INSTALLED_PYTHON * |
---|
181 | n/a | find_existing_python(wchar_t * path) |
---|
182 | n/a | { |
---|
183 | n/a | INSTALLED_PYTHON * result = NULL; |
---|
184 | n/a | size_t i; |
---|
185 | n/a | INSTALLED_PYTHON * ip; |
---|
186 | n/a | |
---|
187 | n/a | for (i = 0, ip = installed_pythons; i < num_installed_pythons; i++, ip++) { |
---|
188 | n/a | if (_wcsicmp(path, ip->executable) == 0) { |
---|
189 | n/a | result = ip; |
---|
190 | n/a | break; |
---|
191 | n/a | } |
---|
192 | n/a | } |
---|
193 | n/a | return result; |
---|
194 | n/a | } |
---|
195 | n/a | |
---|
196 | n/a | static void |
---|
197 | n/a | locate_pythons_for_key(HKEY root, REGSAM flags) |
---|
198 | n/a | { |
---|
199 | n/a | HKEY core_root, ip_key; |
---|
200 | n/a | LSTATUS status = RegOpenKeyExW(root, CORE_PATH, 0, flags, &core_root); |
---|
201 | n/a | wchar_t message[MSGSIZE]; |
---|
202 | n/a | DWORD i; |
---|
203 | n/a | size_t n; |
---|
204 | n/a | BOOL ok; |
---|
205 | n/a | DWORD type, data_size, attrs; |
---|
206 | n/a | INSTALLED_PYTHON * ip, * pip; |
---|
207 | n/a | wchar_t ip_version[IP_VERSION_SIZE]; |
---|
208 | n/a | wchar_t ip_path[IP_SIZE]; |
---|
209 | n/a | wchar_t * check; |
---|
210 | n/a | wchar_t ** checkp; |
---|
211 | n/a | wchar_t *key_name = (root == HKEY_LOCAL_MACHINE) ? L"HKLM" : L"HKCU"; |
---|
212 | n/a | |
---|
213 | n/a | if (status != ERROR_SUCCESS) |
---|
214 | n/a | debug(L"locate_pythons_for_key: unable to open PythonCore key in %ls\n", |
---|
215 | n/a | key_name); |
---|
216 | n/a | else { |
---|
217 | n/a | ip = &installed_pythons[num_installed_pythons]; |
---|
218 | n/a | for (i = 0; num_installed_pythons < MAX_INSTALLED_PYTHONS; i++) { |
---|
219 | n/a | status = RegEnumKeyW(core_root, i, ip_version, IP_VERSION_SIZE); |
---|
220 | n/a | if (status != ERROR_SUCCESS) { |
---|
221 | n/a | if (status != ERROR_NO_MORE_ITEMS) { |
---|
222 | n/a | /* unexpected error */ |
---|
223 | n/a | winerror(status, message, MSGSIZE); |
---|
224 | n/a | debug(L"Can't enumerate registry key for version %ls: %ls\n", |
---|
225 | n/a | ip_version, message); |
---|
226 | n/a | } |
---|
227 | n/a | break; |
---|
228 | n/a | } |
---|
229 | n/a | else { |
---|
230 | n/a | wcsncpy_s(ip->version, MAX_VERSION_SIZE, ip_version, |
---|
231 | n/a | MAX_VERSION_SIZE-1); |
---|
232 | n/a | _snwprintf_s(ip_path, IP_SIZE, _TRUNCATE, |
---|
233 | n/a | L"%ls\\%ls\\InstallPath", CORE_PATH, ip_version); |
---|
234 | n/a | status = RegOpenKeyExW(root, ip_path, 0, flags, &ip_key); |
---|
235 | n/a | if (status != ERROR_SUCCESS) { |
---|
236 | n/a | winerror(status, message, MSGSIZE); |
---|
237 | n/a | // Note: 'message' already has a trailing \n |
---|
238 | n/a | debug(L"%ls\\%ls: %ls", key_name, ip_path, message); |
---|
239 | n/a | continue; |
---|
240 | n/a | } |
---|
241 | n/a | data_size = sizeof(ip->executable) - 1; |
---|
242 | n/a | status = RegQueryValueExW(ip_key, NULL, NULL, &type, |
---|
243 | n/a | (LPBYTE)ip->executable, &data_size); |
---|
244 | n/a | RegCloseKey(ip_key); |
---|
245 | n/a | if (status != ERROR_SUCCESS) { |
---|
246 | n/a | winerror(status, message, MSGSIZE); |
---|
247 | n/a | debug(L"%ls\\%ls: %ls\n", key_name, ip_path, message); |
---|
248 | n/a | continue; |
---|
249 | n/a | } |
---|
250 | n/a | if (type == REG_SZ) { |
---|
251 | n/a | data_size = data_size / sizeof(wchar_t) - 1; /* for NUL */ |
---|
252 | n/a | if (ip->executable[data_size - 1] == L'\\') |
---|
253 | n/a | --data_size; /* reg value ended in a backslash */ |
---|
254 | n/a | /* ip->executable is data_size long */ |
---|
255 | n/a | for (checkp = location_checks; *checkp; ++checkp) { |
---|
256 | n/a | check = *checkp; |
---|
257 | n/a | _snwprintf_s(&ip->executable[data_size], |
---|
258 | n/a | MAX_PATH - data_size, |
---|
259 | n/a | MAX_PATH - data_size, |
---|
260 | n/a | L"%ls%ls", check, PYTHON_EXECUTABLE); |
---|
261 | n/a | attrs = GetFileAttributesW(ip->executable); |
---|
262 | n/a | if (attrs == INVALID_FILE_ATTRIBUTES) { |
---|
263 | n/a | winerror(GetLastError(), message, MSGSIZE); |
---|
264 | n/a | debug(L"locate_pythons_for_key: %ls: %ls", |
---|
265 | n/a | ip->executable, message); |
---|
266 | n/a | } |
---|
267 | n/a | else if (attrs & FILE_ATTRIBUTE_DIRECTORY) { |
---|
268 | n/a | debug(L"locate_pythons_for_key: '%ls' is a \ |
---|
269 | n/a | directory\n", |
---|
270 | n/a | ip->executable, attrs); |
---|
271 | n/a | } |
---|
272 | n/a | else if (find_existing_python(ip->executable)) { |
---|
273 | n/a | debug(L"locate_pythons_for_key: %ls: already \ |
---|
274 | n/a | found\n", ip->executable); |
---|
275 | n/a | } |
---|
276 | n/a | else { |
---|
277 | n/a | /* check the executable type. */ |
---|
278 | n/a | ok = GetBinaryTypeW(ip->executable, &attrs); |
---|
279 | n/a | if (!ok) { |
---|
280 | n/a | debug(L"Failure getting binary type: %ls\n", |
---|
281 | n/a | ip->executable); |
---|
282 | n/a | } |
---|
283 | n/a | else { |
---|
284 | n/a | if (attrs == SCS_64BIT_BINARY) |
---|
285 | n/a | ip->bits = 64; |
---|
286 | n/a | else if (attrs == SCS_32BIT_BINARY) |
---|
287 | n/a | ip->bits = 32; |
---|
288 | n/a | else |
---|
289 | n/a | ip->bits = 0; |
---|
290 | n/a | if (ip->bits == 0) { |
---|
291 | n/a | debug(L"locate_pythons_for_key: %ls: \ |
---|
292 | n/a | invalid binary type: %X\n", |
---|
293 | n/a | ip->executable, attrs); |
---|
294 | n/a | } |
---|
295 | n/a | else { |
---|
296 | n/a | if (wcschr(ip->executable, L' ') != NULL) { |
---|
297 | n/a | /* has spaces, so quote */ |
---|
298 | n/a | n = wcslen(ip->executable); |
---|
299 | n/a | memmove(&ip->executable[1], |
---|
300 | n/a | ip->executable, n * sizeof(wchar_t)); |
---|
301 | n/a | ip->executable[0] = L'\"'; |
---|
302 | n/a | ip->executable[n + 1] = L'\"'; |
---|
303 | n/a | ip->executable[n + 2] = L'\0'; |
---|
304 | n/a | } |
---|
305 | n/a | debug(L"locate_pythons_for_key: %ls \ |
---|
306 | n/a | is a %dbit executable\n", |
---|
307 | n/a | ip->executable, ip->bits); |
---|
308 | n/a | ++num_installed_pythons; |
---|
309 | n/a | pip = ip++; |
---|
310 | n/a | if (num_installed_pythons >= |
---|
311 | n/a | MAX_INSTALLED_PYTHONS) |
---|
312 | n/a | break; |
---|
313 | n/a | /* Copy over the attributes for the next */ |
---|
314 | n/a | *ip = *pip; |
---|
315 | n/a | } |
---|
316 | n/a | } |
---|
317 | n/a | } |
---|
318 | n/a | } |
---|
319 | n/a | } |
---|
320 | n/a | } |
---|
321 | n/a | } |
---|
322 | n/a | RegCloseKey(core_root); |
---|
323 | n/a | } |
---|
324 | n/a | } |
---|
325 | n/a | |
---|
326 | n/a | static int |
---|
327 | n/a | compare_pythons(const void * p1, const void * p2) |
---|
328 | n/a | { |
---|
329 | n/a | INSTALLED_PYTHON * ip1 = (INSTALLED_PYTHON *) p1; |
---|
330 | n/a | INSTALLED_PYTHON * ip2 = (INSTALLED_PYTHON *) p2; |
---|
331 | n/a | /* note reverse sorting on version */ |
---|
332 | n/a | int result = wcscmp(ip2->version, ip1->version); |
---|
333 | n/a | |
---|
334 | n/a | if (result == 0) |
---|
335 | n/a | result = ip2->bits - ip1->bits; /* 64 before 32 */ |
---|
336 | n/a | return result; |
---|
337 | n/a | } |
---|
338 | n/a | |
---|
339 | n/a | static void |
---|
340 | n/a | locate_all_pythons() |
---|
341 | n/a | { |
---|
342 | n/a | #if defined(_M_X64) |
---|
343 | n/a | // If we are a 64bit process, first hit the 32bit keys. |
---|
344 | n/a | debug(L"locating Pythons in 32bit registry\n"); |
---|
345 | n/a | locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ | KEY_WOW64_32KEY); |
---|
346 | n/a | locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ | KEY_WOW64_32KEY); |
---|
347 | n/a | #else |
---|
348 | n/a | // If we are a 32bit process on a 64bit Windows, first hit the 64bit keys. |
---|
349 | n/a | BOOL f64 = FALSE; |
---|
350 | n/a | if (IsWow64Process(GetCurrentProcess(), &f64) && f64) { |
---|
351 | n/a | debug(L"locating Pythons in 64bit registry\n"); |
---|
352 | n/a | locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ | KEY_WOW64_64KEY); |
---|
353 | n/a | locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ | KEY_WOW64_64KEY); |
---|
354 | n/a | } |
---|
355 | n/a | #endif |
---|
356 | n/a | // now hit the "native" key for this process bittedness. |
---|
357 | n/a | debug(L"locating Pythons in native registry\n"); |
---|
358 | n/a | locate_pythons_for_key(HKEY_CURRENT_USER, KEY_READ); |
---|
359 | n/a | locate_pythons_for_key(HKEY_LOCAL_MACHINE, KEY_READ); |
---|
360 | n/a | qsort(installed_pythons, num_installed_pythons, sizeof(INSTALLED_PYTHON), |
---|
361 | n/a | compare_pythons); |
---|
362 | n/a | } |
---|
363 | n/a | |
---|
364 | n/a | static INSTALLED_PYTHON * |
---|
365 | n/a | find_python_by_version(wchar_t const * wanted_ver) |
---|
366 | n/a | { |
---|
367 | n/a | INSTALLED_PYTHON * result = NULL; |
---|
368 | n/a | INSTALLED_PYTHON * ip = installed_pythons; |
---|
369 | n/a | size_t i, n; |
---|
370 | n/a | size_t wlen = wcslen(wanted_ver); |
---|
371 | n/a | int bits = 0; |
---|
372 | n/a | |
---|
373 | n/a | if (wcsstr(wanted_ver, L"-32")) |
---|
374 | n/a | bits = 32; |
---|
375 | n/a | for (i = 0; i < num_installed_pythons; i++, ip++) { |
---|
376 | n/a | n = wcslen(ip->version); |
---|
377 | n/a | if (n > wlen) |
---|
378 | n/a | n = wlen; |
---|
379 | n/a | if ((wcsncmp(ip->version, wanted_ver, n) == 0) && |
---|
380 | n/a | /* bits == 0 => don't care */ |
---|
381 | n/a | ((bits == 0) || (ip->bits == bits))) { |
---|
382 | n/a | result = ip; |
---|
383 | n/a | break; |
---|
384 | n/a | } |
---|
385 | n/a | } |
---|
386 | n/a | return result; |
---|
387 | n/a | } |
---|
388 | n/a | |
---|
389 | n/a | |
---|
390 | n/a | static wchar_t * |
---|
391 | n/a | find_python_by_venv() |
---|
392 | n/a | { |
---|
393 | n/a | static wchar_t venv_python[MAX_PATH]; |
---|
394 | n/a | wchar_t *virtual_env = get_env(L"VIRTUAL_ENV"); |
---|
395 | n/a | DWORD attrs; |
---|
396 | n/a | |
---|
397 | n/a | /* Check for VIRTUAL_ENV environment variable */ |
---|
398 | n/a | if (virtual_env == NULL || virtual_env[0] == L'\0') { |
---|
399 | n/a | return NULL; |
---|
400 | n/a | } |
---|
401 | n/a | |
---|
402 | n/a | /* Check for a python executable in the venv */ |
---|
403 | n/a | debug(L"Checking for Python executable in virtual env '%ls'\n", virtual_env); |
---|
404 | n/a | _snwprintf_s(venv_python, MAX_PATH, _TRUNCATE, |
---|
405 | n/a | L"%ls\\Scripts\\%ls", virtual_env, PYTHON_EXECUTABLE); |
---|
406 | n/a | attrs = GetFileAttributesW(venv_python); |
---|
407 | n/a | if (attrs == INVALID_FILE_ATTRIBUTES) { |
---|
408 | n/a | debug(L"Python executable %ls missing from virtual env\n", venv_python); |
---|
409 | n/a | return NULL; |
---|
410 | n/a | } |
---|
411 | n/a | |
---|
412 | n/a | return venv_python; |
---|
413 | n/a | } |
---|
414 | n/a | |
---|
415 | n/a | static wchar_t appdata_ini_path[MAX_PATH]; |
---|
416 | n/a | static wchar_t launcher_ini_path[MAX_PATH]; |
---|
417 | n/a | |
---|
418 | n/a | /* |
---|
419 | n/a | * Get a value either from the environment or a configuration file. |
---|
420 | n/a | * The key passed in will either be "python", "python2" or "python3". |
---|
421 | n/a | */ |
---|
422 | n/a | static wchar_t * |
---|
423 | n/a | get_configured_value(wchar_t * key) |
---|
424 | n/a | { |
---|
425 | n/a | /* |
---|
426 | n/a | * Note: this static value is used to return a configured value |
---|
427 | n/a | * obtained either from the environment or configuration file. |
---|
428 | n/a | * This should be OK since there wouldn't be any concurrent calls. |
---|
429 | n/a | */ |
---|
430 | n/a | static wchar_t configured_value[MSGSIZE]; |
---|
431 | n/a | wchar_t * result = NULL; |
---|
432 | n/a | wchar_t * found_in = L"environment"; |
---|
433 | n/a | DWORD size; |
---|
434 | n/a | |
---|
435 | n/a | /* First, search the environment. */ |
---|
436 | n/a | _snwprintf_s(configured_value, MSGSIZE, _TRUNCATE, L"py_%ls", key); |
---|
437 | n/a | result = get_env(configured_value); |
---|
438 | n/a | if (result == NULL && appdata_ini_path[0]) { |
---|
439 | n/a | /* Not in environment: check local configuration. */ |
---|
440 | n/a | size = GetPrivateProfileStringW(L"defaults", key, NULL, |
---|
441 | n/a | configured_value, MSGSIZE, |
---|
442 | n/a | appdata_ini_path); |
---|
443 | n/a | if (size > 0) { |
---|
444 | n/a | result = configured_value; |
---|
445 | n/a | found_in = appdata_ini_path; |
---|
446 | n/a | } |
---|
447 | n/a | } |
---|
448 | n/a | if (result == NULL && launcher_ini_path[0]) { |
---|
449 | n/a | /* Not in environment or local: check global configuration. */ |
---|
450 | n/a | size = GetPrivateProfileStringW(L"defaults", key, NULL, |
---|
451 | n/a | configured_value, MSGSIZE, |
---|
452 | n/a | launcher_ini_path); |
---|
453 | n/a | if (size > 0) { |
---|
454 | n/a | result = configured_value; |
---|
455 | n/a | found_in = launcher_ini_path; |
---|
456 | n/a | } |
---|
457 | n/a | } |
---|
458 | n/a | if (result) { |
---|
459 | n/a | debug(L"found configured value '%ls=%ls' in %ls\n", |
---|
460 | n/a | key, result, found_in ? found_in : L"(unknown)"); |
---|
461 | n/a | } else { |
---|
462 | n/a | debug(L"found no configured value for '%ls'\n", key); |
---|
463 | n/a | } |
---|
464 | n/a | return result; |
---|
465 | n/a | } |
---|
466 | n/a | |
---|
467 | n/a | static INSTALLED_PYTHON * |
---|
468 | n/a | locate_python(wchar_t * wanted_ver, BOOL from_shebang) |
---|
469 | n/a | { |
---|
470 | n/a | static wchar_t config_key [] = { L"pythonX" }; |
---|
471 | n/a | static wchar_t * last_char = &config_key[sizeof(config_key) / |
---|
472 | n/a | sizeof(wchar_t) - 2]; |
---|
473 | n/a | INSTALLED_PYTHON * result = NULL; |
---|
474 | n/a | size_t n = wcslen(wanted_ver); |
---|
475 | n/a | wchar_t * configured_value; |
---|
476 | n/a | |
---|
477 | n/a | if (num_installed_pythons == 0) |
---|
478 | n/a | locate_all_pythons(); |
---|
479 | n/a | |
---|
480 | n/a | if (n == 1) { /* just major version specified */ |
---|
481 | n/a | *last_char = *wanted_ver; |
---|
482 | n/a | configured_value = get_configured_value(config_key); |
---|
483 | n/a | if (configured_value != NULL) |
---|
484 | n/a | wanted_ver = configured_value; |
---|
485 | n/a | } |
---|
486 | n/a | if (*wanted_ver) { |
---|
487 | n/a | result = find_python_by_version(wanted_ver); |
---|
488 | n/a | debug(L"search for Python version '%ls' found ", wanted_ver); |
---|
489 | n/a | if (result) { |
---|
490 | n/a | debug(L"'%ls'\n", result->executable); |
---|
491 | n/a | } else { |
---|
492 | n/a | debug(L"no interpreter\n"); |
---|
493 | n/a | } |
---|
494 | n/a | } |
---|
495 | n/a | else { |
---|
496 | n/a | *last_char = L'\0'; /* look for an overall default */ |
---|
497 | n/a | configured_value = get_configured_value(config_key); |
---|
498 | n/a | if (configured_value) |
---|
499 | n/a | result = find_python_by_version(configured_value); |
---|
500 | n/a | /* Not found a value yet - try by major version. |
---|
501 | n/a | * If we're looking for an interpreter specified in a shebang line, |
---|
502 | n/a | * we want to try Python 2 first, then Python 3 (for Unix and backward |
---|
503 | n/a | * compatibility). If we're being called interactively, assume the user |
---|
504 | n/a | * wants the latest version available, so try Python 3 first, then |
---|
505 | n/a | * Python 2. |
---|
506 | n/a | */ |
---|
507 | n/a | if (result == NULL) |
---|
508 | n/a | result = find_python_by_version(from_shebang ? L"2" : L"3"); |
---|
509 | n/a | if (result == NULL) |
---|
510 | n/a | result = find_python_by_version(from_shebang ? L"3" : L"2"); |
---|
511 | n/a | debug(L"search for default Python found "); |
---|
512 | n/a | if (result) { |
---|
513 | n/a | debug(L"version %ls at '%ls'\n", |
---|
514 | n/a | result->version, result->executable); |
---|
515 | n/a | } else { |
---|
516 | n/a | debug(L"no interpreter\n"); |
---|
517 | n/a | } |
---|
518 | n/a | } |
---|
519 | n/a | return result; |
---|
520 | n/a | } |
---|
521 | n/a | |
---|
522 | n/a | #if defined(SCRIPT_WRAPPER) |
---|
523 | n/a | /* |
---|
524 | n/a | * Check for a script located alongside the executable |
---|
525 | n/a | */ |
---|
526 | n/a | |
---|
527 | n/a | #if defined(_WINDOWS) |
---|
528 | n/a | #define SCRIPT_SUFFIX L"-script.pyw" |
---|
529 | n/a | #else |
---|
530 | n/a | #define SCRIPT_SUFFIX L"-script.py" |
---|
531 | n/a | #endif |
---|
532 | n/a | |
---|
533 | n/a | static wchar_t wrapped_script_path[MAX_PATH]; |
---|
534 | n/a | |
---|
535 | n/a | /* Locate the script being wrapped. |
---|
536 | n/a | * |
---|
537 | n/a | * This code should store the name of the wrapped script in |
---|
538 | n/a | * wrapped_script_path, or terminate the program with an error if there is no |
---|
539 | n/a | * valid wrapped script file. |
---|
540 | n/a | */ |
---|
541 | n/a | static void |
---|
542 | n/a | locate_wrapped_script() |
---|
543 | n/a | { |
---|
544 | n/a | wchar_t * p; |
---|
545 | n/a | size_t plen; |
---|
546 | n/a | DWORD attrs; |
---|
547 | n/a | |
---|
548 | n/a | plen = GetModuleFileNameW(NULL, wrapped_script_path, MAX_PATH); |
---|
549 | n/a | p = wcsrchr(wrapped_script_path, L'.'); |
---|
550 | n/a | if (p == NULL) { |
---|
551 | n/a | debug(L"GetModuleFileNameW returned value has no extension: %ls\n", |
---|
552 | n/a | wrapped_script_path); |
---|
553 | n/a | error(RC_NO_SCRIPT, L"Wrapper name '%ls' is not valid.", wrapped_script_path); |
---|
554 | n/a | } |
---|
555 | n/a | |
---|
556 | n/a | wcsncpy_s(p, MAX_PATH - (p - wrapped_script_path) + 1, SCRIPT_SUFFIX, _TRUNCATE); |
---|
557 | n/a | attrs = GetFileAttributesW(wrapped_script_path); |
---|
558 | n/a | if (attrs == INVALID_FILE_ATTRIBUTES) { |
---|
559 | n/a | debug(L"File '%ls' non-existent\n", wrapped_script_path); |
---|
560 | n/a | error(RC_NO_SCRIPT, L"Script file '%ls' is not present.", wrapped_script_path); |
---|
561 | n/a | } |
---|
562 | n/a | |
---|
563 | n/a | debug(L"Using wrapped script file '%ls'\n", wrapped_script_path); |
---|
564 | n/a | } |
---|
565 | n/a | #endif |
---|
566 | n/a | |
---|
567 | n/a | /* |
---|
568 | n/a | * Process creation code |
---|
569 | n/a | */ |
---|
570 | n/a | |
---|
571 | n/a | static BOOL |
---|
572 | n/a | safe_duplicate_handle(HANDLE in, HANDLE * pout) |
---|
573 | n/a | { |
---|
574 | n/a | BOOL ok; |
---|
575 | n/a | HANDLE process = GetCurrentProcess(); |
---|
576 | n/a | DWORD rc; |
---|
577 | n/a | |
---|
578 | n/a | *pout = NULL; |
---|
579 | n/a | ok = DuplicateHandle(process, in, process, pout, 0, TRUE, |
---|
580 | n/a | DUPLICATE_SAME_ACCESS); |
---|
581 | n/a | if (!ok) { |
---|
582 | n/a | rc = GetLastError(); |
---|
583 | n/a | if (rc == ERROR_INVALID_HANDLE) { |
---|
584 | n/a | debug(L"DuplicateHandle returned ERROR_INVALID_HANDLE\n"); |
---|
585 | n/a | ok = TRUE; |
---|
586 | n/a | } |
---|
587 | n/a | else { |
---|
588 | n/a | debug(L"DuplicateHandle returned %d\n", rc); |
---|
589 | n/a | } |
---|
590 | n/a | } |
---|
591 | n/a | return ok; |
---|
592 | n/a | } |
---|
593 | n/a | |
---|
594 | n/a | static BOOL WINAPI |
---|
595 | n/a | ctrl_c_handler(DWORD code) |
---|
596 | n/a | { |
---|
597 | n/a | return TRUE; /* We just ignore all control events. */ |
---|
598 | n/a | } |
---|
599 | n/a | |
---|
600 | n/a | static void |
---|
601 | n/a | run_child(wchar_t * cmdline) |
---|
602 | n/a | { |
---|
603 | n/a | HANDLE job; |
---|
604 | n/a | JOBOBJECT_EXTENDED_LIMIT_INFORMATION info; |
---|
605 | n/a | DWORD rc; |
---|
606 | n/a | BOOL ok; |
---|
607 | n/a | STARTUPINFOW si; |
---|
608 | n/a | PROCESS_INFORMATION pi; |
---|
609 | n/a | |
---|
610 | n/a | #if defined(_WINDOWS) |
---|
611 | n/a | // When explorer launches a Windows (GUI) application, it displays |
---|
612 | n/a | // the "app starting" (the "pointer + hourglass") cursor for a number |
---|
613 | n/a | // of seconds, or until the app does something UI-ish (eg, creating a |
---|
614 | n/a | // window, or fetching a message). As this launcher doesn't do this |
---|
615 | n/a | // directly, that cursor remains even after the child process does these |
---|
616 | n/a | // things. We avoid that by doing a simple post+get message. |
---|
617 | n/a | // See http://bugs.python.org/issue17290 and |
---|
618 | n/a | // https://bitbucket.org/vinay.sajip/pylauncher/issue/20/busy-cursor-for-a-long-time-when-running |
---|
619 | n/a | MSG msg; |
---|
620 | n/a | |
---|
621 | n/a | PostMessage(0, 0, 0, 0); |
---|
622 | n/a | GetMessage(&msg, 0, 0, 0); |
---|
623 | n/a | #endif |
---|
624 | n/a | |
---|
625 | n/a | debug(L"run_child: about to run '%ls'\n", cmdline); |
---|
626 | n/a | job = CreateJobObject(NULL, NULL); |
---|
627 | n/a | ok = QueryInformationJobObject(job, JobObjectExtendedLimitInformation, |
---|
628 | n/a | &info, sizeof(info), &rc); |
---|
629 | n/a | if (!ok || (rc != sizeof(info)) || !job) |
---|
630 | n/a | error(RC_CREATE_PROCESS, L"Job information querying failed"); |
---|
631 | n/a | info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | |
---|
632 | n/a | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK; |
---|
633 | n/a | ok = SetInformationJobObject(job, JobObjectExtendedLimitInformation, &info, |
---|
634 | n/a | sizeof(info)); |
---|
635 | n/a | if (!ok) |
---|
636 | n/a | error(RC_CREATE_PROCESS, L"Job information setting failed"); |
---|
637 | n/a | memset(&si, 0, sizeof(si)); |
---|
638 | n/a | si.cb = sizeof(si); |
---|
639 | n/a | ok = safe_duplicate_handle(GetStdHandle(STD_INPUT_HANDLE), &si.hStdInput); |
---|
640 | n/a | if (!ok) |
---|
641 | n/a | error(RC_NO_STD_HANDLES, L"stdin duplication failed"); |
---|
642 | n/a | ok = safe_duplicate_handle(GetStdHandle(STD_OUTPUT_HANDLE), &si.hStdOutput); |
---|
643 | n/a | if (!ok) |
---|
644 | n/a | error(RC_NO_STD_HANDLES, L"stdout duplication failed"); |
---|
645 | n/a | ok = safe_duplicate_handle(GetStdHandle(STD_ERROR_HANDLE), &si.hStdError); |
---|
646 | n/a | if (!ok) |
---|
647 | n/a | error(RC_NO_STD_HANDLES, L"stderr duplication failed"); |
---|
648 | n/a | |
---|
649 | n/a | ok = SetConsoleCtrlHandler(ctrl_c_handler, TRUE); |
---|
650 | n/a | if (!ok) |
---|
651 | n/a | error(RC_CREATE_PROCESS, L"control handler setting failed"); |
---|
652 | n/a | |
---|
653 | n/a | si.dwFlags = STARTF_USESTDHANDLES; |
---|
654 | n/a | ok = CreateProcessW(NULL, cmdline, NULL, NULL, TRUE, |
---|
655 | n/a | 0, NULL, NULL, &si, &pi); |
---|
656 | n/a | if (!ok) |
---|
657 | n/a | error(RC_CREATE_PROCESS, L"Unable to create process using '%ls'", cmdline); |
---|
658 | n/a | AssignProcessToJobObject(job, pi.hProcess); |
---|
659 | n/a | CloseHandle(pi.hThread); |
---|
660 | n/a | WaitForSingleObjectEx(pi.hProcess, INFINITE, FALSE); |
---|
661 | n/a | ok = GetExitCodeProcess(pi.hProcess, &rc); |
---|
662 | n/a | if (!ok) |
---|
663 | n/a | error(RC_CREATE_PROCESS, L"Failed to get exit code of process"); |
---|
664 | n/a | debug(L"child process exit code: %d\n", rc); |
---|
665 | n/a | exit(rc); |
---|
666 | n/a | } |
---|
667 | n/a | |
---|
668 | n/a | static void |
---|
669 | n/a | invoke_child(wchar_t * executable, wchar_t * suffix, wchar_t * cmdline) |
---|
670 | n/a | { |
---|
671 | n/a | wchar_t * child_command; |
---|
672 | n/a | size_t child_command_size; |
---|
673 | n/a | BOOL no_suffix = (suffix == NULL) || (*suffix == L'\0'); |
---|
674 | n/a | BOOL no_cmdline = (*cmdline == L'\0'); |
---|
675 | n/a | |
---|
676 | n/a | if (no_suffix && no_cmdline) |
---|
677 | n/a | run_child(executable); |
---|
678 | n/a | else { |
---|
679 | n/a | if (no_suffix) { |
---|
680 | n/a | /* add 2 for space separator + terminating NUL. */ |
---|
681 | n/a | child_command_size = wcslen(executable) + wcslen(cmdline) + 2; |
---|
682 | n/a | } |
---|
683 | n/a | else { |
---|
684 | n/a | /* add 3 for 2 space separators + terminating NUL. */ |
---|
685 | n/a | child_command_size = wcslen(executable) + wcslen(suffix) + |
---|
686 | n/a | wcslen(cmdline) + 3; |
---|
687 | n/a | } |
---|
688 | n/a | child_command = calloc(child_command_size, sizeof(wchar_t)); |
---|
689 | n/a | if (child_command == NULL) |
---|
690 | n/a | error(RC_CREATE_PROCESS, L"unable to allocate %d bytes for child command.", |
---|
691 | n/a | child_command_size); |
---|
692 | n/a | if (no_suffix) |
---|
693 | n/a | _snwprintf_s(child_command, child_command_size, |
---|
694 | n/a | child_command_size - 1, L"%ls %ls", |
---|
695 | n/a | executable, cmdline); |
---|
696 | n/a | else |
---|
697 | n/a | _snwprintf_s(child_command, child_command_size, |
---|
698 | n/a | child_command_size - 1, L"%ls %ls %ls", |
---|
699 | n/a | executable, suffix, cmdline); |
---|
700 | n/a | run_child(child_command); |
---|
701 | n/a | free(child_command); |
---|
702 | n/a | } |
---|
703 | n/a | } |
---|
704 | n/a | |
---|
705 | n/a | typedef struct { |
---|
706 | n/a | wchar_t *shebang; |
---|
707 | n/a | BOOL search; |
---|
708 | n/a | } SHEBANG; |
---|
709 | n/a | |
---|
710 | n/a | static SHEBANG builtin_virtual_paths [] = { |
---|
711 | n/a | { L"/usr/bin/env python", TRUE }, |
---|
712 | n/a | { L"/usr/bin/python", FALSE }, |
---|
713 | n/a | { L"/usr/local/bin/python", FALSE }, |
---|
714 | n/a | { L"python", FALSE }, |
---|
715 | n/a | { NULL, FALSE }, |
---|
716 | n/a | }; |
---|
717 | n/a | |
---|
718 | n/a | /* For now, a static array of commands. */ |
---|
719 | n/a | |
---|
720 | n/a | #define MAX_COMMANDS 100 |
---|
721 | n/a | |
---|
722 | n/a | typedef struct { |
---|
723 | n/a | wchar_t key[MAX_PATH]; |
---|
724 | n/a | wchar_t value[MSGSIZE]; |
---|
725 | n/a | } COMMAND; |
---|
726 | n/a | |
---|
727 | n/a | static COMMAND commands[MAX_COMMANDS]; |
---|
728 | n/a | static int num_commands = 0; |
---|
729 | n/a | |
---|
730 | n/a | #if defined(SKIP_PREFIX) |
---|
731 | n/a | |
---|
732 | n/a | static wchar_t * builtin_prefixes [] = { |
---|
733 | n/a | /* These must be in an order that the longest matches should be found, |
---|
734 | n/a | * i.e. if the prefix is "/usr/bin/env ", it should match that entry |
---|
735 | n/a | * *before* matching "/usr/bin/". |
---|
736 | n/a | */ |
---|
737 | n/a | L"/usr/bin/env ", |
---|
738 | n/a | L"/usr/bin/", |
---|
739 | n/a | L"/usr/local/bin/", |
---|
740 | n/a | NULL |
---|
741 | n/a | }; |
---|
742 | n/a | |
---|
743 | n/a | static wchar_t * skip_prefix(wchar_t * name) |
---|
744 | n/a | { |
---|
745 | n/a | wchar_t ** pp = builtin_prefixes; |
---|
746 | n/a | wchar_t * result = name; |
---|
747 | n/a | wchar_t * p; |
---|
748 | n/a | size_t n; |
---|
749 | n/a | |
---|
750 | n/a | for (; p = *pp; pp++) { |
---|
751 | n/a | n = wcslen(p); |
---|
752 | n/a | if (_wcsnicmp(p, name, n) == 0) { |
---|
753 | n/a | result += n; /* skip the prefix */ |
---|
754 | n/a | if (p[n - 1] == L' ') /* No empty strings in table, so n > 1 */ |
---|
755 | n/a | result = skip_whitespace(result); |
---|
756 | n/a | break; |
---|
757 | n/a | } |
---|
758 | n/a | } |
---|
759 | n/a | return result; |
---|
760 | n/a | } |
---|
761 | n/a | |
---|
762 | n/a | #endif |
---|
763 | n/a | |
---|
764 | n/a | #if defined(SEARCH_PATH) |
---|
765 | n/a | |
---|
766 | n/a | static COMMAND path_command; |
---|
767 | n/a | |
---|
768 | n/a | static COMMAND * find_on_path(wchar_t * name) |
---|
769 | n/a | { |
---|
770 | n/a | wchar_t * pathext; |
---|
771 | n/a | size_t varsize; |
---|
772 | n/a | wchar_t * context = NULL; |
---|
773 | n/a | wchar_t * extension; |
---|
774 | n/a | COMMAND * result = NULL; |
---|
775 | n/a | DWORD len; |
---|
776 | n/a | errno_t rc; |
---|
777 | n/a | |
---|
778 | n/a | wcscpy_s(path_command.key, MAX_PATH, name); |
---|
779 | n/a | if (wcschr(name, L'.') != NULL) { |
---|
780 | n/a | /* assume it has an extension. */ |
---|
781 | n/a | len = SearchPathW(NULL, name, NULL, MSGSIZE, path_command.value, NULL); |
---|
782 | n/a | if (len) { |
---|
783 | n/a | result = &path_command; |
---|
784 | n/a | } |
---|
785 | n/a | } |
---|
786 | n/a | else { |
---|
787 | n/a | /* No extension - search using registered extensions. */ |
---|
788 | n/a | rc = _wdupenv_s(&pathext, &varsize, L"PATHEXT"); |
---|
789 | n/a | if (rc == 0) { |
---|
790 | n/a | extension = wcstok_s(pathext, L";", &context); |
---|
791 | n/a | while (extension) { |
---|
792 | n/a | len = SearchPathW(NULL, name, extension, MSGSIZE, path_command.value, NULL); |
---|
793 | n/a | if (len) { |
---|
794 | n/a | result = &path_command; |
---|
795 | n/a | break; |
---|
796 | n/a | } |
---|
797 | n/a | extension = wcstok_s(NULL, L";", &context); |
---|
798 | n/a | } |
---|
799 | n/a | free(pathext); |
---|
800 | n/a | } |
---|
801 | n/a | } |
---|
802 | n/a | return result; |
---|
803 | n/a | } |
---|
804 | n/a | |
---|
805 | n/a | #endif |
---|
806 | n/a | |
---|
807 | n/a | static COMMAND * find_command(wchar_t * name) |
---|
808 | n/a | { |
---|
809 | n/a | COMMAND * result = NULL; |
---|
810 | n/a | COMMAND * cp = commands; |
---|
811 | n/a | int i; |
---|
812 | n/a | |
---|
813 | n/a | for (i = 0; i < num_commands; i++, cp++) { |
---|
814 | n/a | if (_wcsicmp(cp->key, name) == 0) { |
---|
815 | n/a | result = cp; |
---|
816 | n/a | break; |
---|
817 | n/a | } |
---|
818 | n/a | } |
---|
819 | n/a | #if defined(SEARCH_PATH) |
---|
820 | n/a | if (result == NULL) |
---|
821 | n/a | result = find_on_path(name); |
---|
822 | n/a | #endif |
---|
823 | n/a | return result; |
---|
824 | n/a | } |
---|
825 | n/a | |
---|
826 | n/a | static void |
---|
827 | n/a | update_command(COMMAND * cp, wchar_t * name, wchar_t * cmdline) |
---|
828 | n/a | { |
---|
829 | n/a | wcsncpy_s(cp->key, MAX_PATH, name, _TRUNCATE); |
---|
830 | n/a | wcsncpy_s(cp->value, MSGSIZE, cmdline, _TRUNCATE); |
---|
831 | n/a | } |
---|
832 | n/a | |
---|
833 | n/a | static void |
---|
834 | n/a | add_command(wchar_t * name, wchar_t * cmdline) |
---|
835 | n/a | { |
---|
836 | n/a | if (num_commands >= MAX_COMMANDS) { |
---|
837 | n/a | debug(L"can't add %ls = '%ls': no room\n", name, cmdline); |
---|
838 | n/a | } |
---|
839 | n/a | else { |
---|
840 | n/a | COMMAND * cp = &commands[num_commands++]; |
---|
841 | n/a | |
---|
842 | n/a | update_command(cp, name, cmdline); |
---|
843 | n/a | } |
---|
844 | n/a | } |
---|
845 | n/a | |
---|
846 | n/a | static void |
---|
847 | n/a | read_config_file(wchar_t * config_path) |
---|
848 | n/a | { |
---|
849 | n/a | wchar_t keynames[MSGSIZE]; |
---|
850 | n/a | wchar_t value[MSGSIZE]; |
---|
851 | n/a | DWORD read; |
---|
852 | n/a | wchar_t * key; |
---|
853 | n/a | COMMAND * cp; |
---|
854 | n/a | wchar_t * cmdp; |
---|
855 | n/a | |
---|
856 | n/a | read = GetPrivateProfileStringW(L"commands", NULL, NULL, keynames, MSGSIZE, |
---|
857 | n/a | config_path); |
---|
858 | n/a | if (read == MSGSIZE - 1) { |
---|
859 | n/a | debug(L"read_commands: %ls: not enough space for names\n", config_path); |
---|
860 | n/a | } |
---|
861 | n/a | key = keynames; |
---|
862 | n/a | while (*key) { |
---|
863 | n/a | read = GetPrivateProfileStringW(L"commands", key, NULL, value, MSGSIZE, |
---|
864 | n/a | config_path); |
---|
865 | n/a | if (read == MSGSIZE - 1) { |
---|
866 | n/a | debug(L"read_commands: %ls: not enough space for %ls\n", |
---|
867 | n/a | config_path, key); |
---|
868 | n/a | } |
---|
869 | n/a | cmdp = skip_whitespace(value); |
---|
870 | n/a | if (*cmdp) { |
---|
871 | n/a | cp = find_command(key); |
---|
872 | n/a | if (cp == NULL) |
---|
873 | n/a | add_command(key, value); |
---|
874 | n/a | else |
---|
875 | n/a | update_command(cp, key, value); |
---|
876 | n/a | } |
---|
877 | n/a | key += wcslen(key) + 1; |
---|
878 | n/a | } |
---|
879 | n/a | } |
---|
880 | n/a | |
---|
881 | n/a | static void read_commands() |
---|
882 | n/a | { |
---|
883 | n/a | if (launcher_ini_path[0]) |
---|
884 | n/a | read_config_file(launcher_ini_path); |
---|
885 | n/a | if (appdata_ini_path[0]) |
---|
886 | n/a | read_config_file(appdata_ini_path); |
---|
887 | n/a | } |
---|
888 | n/a | |
---|
889 | n/a | static BOOL |
---|
890 | n/a | parse_shebang(wchar_t * shebang_line, int nchars, wchar_t ** command, |
---|
891 | n/a | wchar_t ** suffix, BOOL *search) |
---|
892 | n/a | { |
---|
893 | n/a | BOOL rc = FALSE; |
---|
894 | n/a | SHEBANG * vpp; |
---|
895 | n/a | size_t plen; |
---|
896 | n/a | wchar_t * p; |
---|
897 | n/a | wchar_t zapped; |
---|
898 | n/a | wchar_t * endp = shebang_line + nchars - 1; |
---|
899 | n/a | COMMAND * cp; |
---|
900 | n/a | wchar_t * skipped; |
---|
901 | n/a | |
---|
902 | n/a | *command = NULL; /* failure return */ |
---|
903 | n/a | *suffix = NULL; |
---|
904 | n/a | *search = FALSE; |
---|
905 | n/a | |
---|
906 | n/a | if ((*shebang_line++ == L'#') && (*shebang_line++ == L'!')) { |
---|
907 | n/a | shebang_line = skip_whitespace(shebang_line); |
---|
908 | n/a | if (*shebang_line) { |
---|
909 | n/a | *command = shebang_line; |
---|
910 | n/a | for (vpp = builtin_virtual_paths; vpp->shebang; ++vpp) { |
---|
911 | n/a | plen = wcslen(vpp->shebang); |
---|
912 | n/a | if (wcsncmp(shebang_line, vpp->shebang, plen) == 0) { |
---|
913 | n/a | rc = TRUE; |
---|
914 | n/a | *search = vpp->search; |
---|
915 | n/a | /* We can do this because all builtin commands contain |
---|
916 | n/a | * "python". |
---|
917 | n/a | */ |
---|
918 | n/a | *command = wcsstr(shebang_line, L"python"); |
---|
919 | n/a | break; |
---|
920 | n/a | } |
---|
921 | n/a | } |
---|
922 | n/a | if (vpp->shebang == NULL) { |
---|
923 | n/a | /* |
---|
924 | n/a | * Not found in builtins - look in customized commands. |
---|
925 | n/a | * |
---|
926 | n/a | * We can't permanently modify the shebang line in case |
---|
927 | n/a | * it's not a customized command, but we can temporarily |
---|
928 | n/a | * stick a NUL after the command while searching for it, |
---|
929 | n/a | * then put back the char we zapped. |
---|
930 | n/a | */ |
---|
931 | n/a | #if defined(SKIP_PREFIX) |
---|
932 | n/a | skipped = skip_prefix(shebang_line); |
---|
933 | n/a | #else |
---|
934 | n/a | skipped = shebang_line; |
---|
935 | n/a | #endif |
---|
936 | n/a | p = wcspbrk(skipped, L" \t\r\n"); |
---|
937 | n/a | if (p != NULL) { |
---|
938 | n/a | zapped = *p; |
---|
939 | n/a | *p = L'\0'; |
---|
940 | n/a | } |
---|
941 | n/a | cp = find_command(skipped); |
---|
942 | n/a | if (p != NULL) |
---|
943 | n/a | *p = zapped; |
---|
944 | n/a | if (cp != NULL) { |
---|
945 | n/a | *command = cp->value; |
---|
946 | n/a | if (p != NULL) |
---|
947 | n/a | *suffix = skip_whitespace(p); |
---|
948 | n/a | } |
---|
949 | n/a | } |
---|
950 | n/a | /* remove trailing whitespace */ |
---|
951 | n/a | while ((endp > shebang_line) && isspace(*endp)) |
---|
952 | n/a | --endp; |
---|
953 | n/a | if (endp > shebang_line) |
---|
954 | n/a | endp[1] = L'\0'; |
---|
955 | n/a | } |
---|
956 | n/a | } |
---|
957 | n/a | return rc; |
---|
958 | n/a | } |
---|
959 | n/a | |
---|
960 | n/a | /* #define CP_UTF8 65001 defined in winnls.h */ |
---|
961 | n/a | #define CP_UTF16LE 1200 |
---|
962 | n/a | #define CP_UTF16BE 1201 |
---|
963 | n/a | #define CP_UTF32LE 12000 |
---|
964 | n/a | #define CP_UTF32BE 12001 |
---|
965 | n/a | |
---|
966 | n/a | typedef struct { |
---|
967 | n/a | int length; |
---|
968 | n/a | char sequence[4]; |
---|
969 | n/a | UINT code_page; |
---|
970 | n/a | } BOM; |
---|
971 | n/a | |
---|
972 | n/a | /* |
---|
973 | n/a | * Strictly, we don't need to handle UTF-16 and UTF-32, since Python itself |
---|
974 | n/a | * doesn't. Never mind, one day it might - there's no harm leaving it in. |
---|
975 | n/a | */ |
---|
976 | n/a | static BOM BOMs[] = { |
---|
977 | n/a | { 3, { 0xEF, 0xBB, 0xBF }, CP_UTF8 }, /* UTF-8 - keep first */ |
---|
978 | n/a | /* Test UTF-32LE before UTF-16LE since UTF-16LE BOM is a prefix |
---|
979 | n/a | * of UTF-32LE BOM. */ |
---|
980 | n/a | { 4, { 0xFF, 0xFE, 0x00, 0x00 }, CP_UTF32LE }, /* UTF-32LE */ |
---|
981 | n/a | { 4, { 0x00, 0x00, 0xFE, 0xFF }, CP_UTF32BE }, /* UTF-32BE */ |
---|
982 | n/a | { 2, { 0xFF, 0xFE }, CP_UTF16LE }, /* UTF-16LE */ |
---|
983 | n/a | { 2, { 0xFE, 0xFF }, CP_UTF16BE }, /* UTF-16BE */ |
---|
984 | n/a | { 0 } /* sentinel */ |
---|
985 | n/a | }; |
---|
986 | n/a | |
---|
987 | n/a | static BOM * |
---|
988 | n/a | find_BOM(char * buffer) |
---|
989 | n/a | { |
---|
990 | n/a | /* |
---|
991 | n/a | * Look for a BOM in the input and return a pointer to the |
---|
992 | n/a | * corresponding structure, or NULL if not found. |
---|
993 | n/a | */ |
---|
994 | n/a | BOM * result = NULL; |
---|
995 | n/a | BOM *bom; |
---|
996 | n/a | |
---|
997 | n/a | for (bom = BOMs; bom->length; bom++) { |
---|
998 | n/a | if (strncmp(bom->sequence, buffer, bom->length) == 0) { |
---|
999 | n/a | result = bom; |
---|
1000 | n/a | break; |
---|
1001 | n/a | } |
---|
1002 | n/a | } |
---|
1003 | n/a | return result; |
---|
1004 | n/a | } |
---|
1005 | n/a | |
---|
1006 | n/a | static char * |
---|
1007 | n/a | find_terminator(char * buffer, int len, BOM *bom) |
---|
1008 | n/a | { |
---|
1009 | n/a | char * result = NULL; |
---|
1010 | n/a | char * end = buffer + len; |
---|
1011 | n/a | char * p; |
---|
1012 | n/a | char c; |
---|
1013 | n/a | int cp; |
---|
1014 | n/a | |
---|
1015 | n/a | for (p = buffer; p < end; p++) { |
---|
1016 | n/a | c = *p; |
---|
1017 | n/a | if (c == '\r') { |
---|
1018 | n/a | result = p; |
---|
1019 | n/a | break; |
---|
1020 | n/a | } |
---|
1021 | n/a | if (c == '\n') { |
---|
1022 | n/a | result = p; |
---|
1023 | n/a | break; |
---|
1024 | n/a | } |
---|
1025 | n/a | } |
---|
1026 | n/a | if (result != NULL) { |
---|
1027 | n/a | cp = bom->code_page; |
---|
1028 | n/a | |
---|
1029 | n/a | /* adjustments to include all bytes of the char */ |
---|
1030 | n/a | /* no adjustment needed for UTF-8 or big endian */ |
---|
1031 | n/a | if (cp == CP_UTF16LE) |
---|
1032 | n/a | ++result; |
---|
1033 | n/a | else if (cp == CP_UTF32LE) |
---|
1034 | n/a | result += 3; |
---|
1035 | n/a | ++result; /* point just past terminator */ |
---|
1036 | n/a | } |
---|
1037 | n/a | return result; |
---|
1038 | n/a | } |
---|
1039 | n/a | |
---|
1040 | n/a | static BOOL |
---|
1041 | n/a | validate_version(wchar_t * p) |
---|
1042 | n/a | { |
---|
1043 | n/a | BOOL result = TRUE; |
---|
1044 | n/a | |
---|
1045 | n/a | if (!isdigit(*p)) /* expect major version */ |
---|
1046 | n/a | result = FALSE; |
---|
1047 | n/a | else if (*++p) { /* more to do */ |
---|
1048 | n/a | if (*p != L'.') /* major/minor separator */ |
---|
1049 | n/a | result = FALSE; |
---|
1050 | n/a | else { |
---|
1051 | n/a | ++p; |
---|
1052 | n/a | if (!isdigit(*p)) /* expect minor version */ |
---|
1053 | n/a | result = FALSE; |
---|
1054 | n/a | else { |
---|
1055 | n/a | ++p; |
---|
1056 | n/a | if (*p) { /* more to do */ |
---|
1057 | n/a | if (*p != L'-') |
---|
1058 | n/a | result = FALSE; |
---|
1059 | n/a | else { |
---|
1060 | n/a | ++p; |
---|
1061 | n/a | if ((*p != '3') && (*++p != '2') && !*++p) |
---|
1062 | n/a | result = FALSE; |
---|
1063 | n/a | } |
---|
1064 | n/a | } |
---|
1065 | n/a | } |
---|
1066 | n/a | } |
---|
1067 | n/a | } |
---|
1068 | n/a | return result; |
---|
1069 | n/a | } |
---|
1070 | n/a | |
---|
1071 | n/a | typedef struct { |
---|
1072 | n/a | unsigned short min; |
---|
1073 | n/a | unsigned short max; |
---|
1074 | n/a | wchar_t version[MAX_VERSION_SIZE]; |
---|
1075 | n/a | } PYC_MAGIC; |
---|
1076 | n/a | |
---|
1077 | n/a | static PYC_MAGIC magic_values[] = { |
---|
1078 | n/a | { 50823, 50823, L"2.0" }, |
---|
1079 | n/a | { 60202, 60202, L"2.1" }, |
---|
1080 | n/a | { 60717, 60717, L"2.2" }, |
---|
1081 | n/a | { 62011, 62021, L"2.3" }, |
---|
1082 | n/a | { 62041, 62061, L"2.4" }, |
---|
1083 | n/a | { 62071, 62131, L"2.5" }, |
---|
1084 | n/a | { 62151, 62161, L"2.6" }, |
---|
1085 | n/a | { 62171, 62211, L"2.7" }, |
---|
1086 | n/a | { 3000, 3131, L"3.0" }, |
---|
1087 | n/a | { 3141, 3151, L"3.1" }, |
---|
1088 | n/a | { 3160, 3180, L"3.2" }, |
---|
1089 | n/a | { 3190, 3230, L"3.3" }, |
---|
1090 | n/a | { 3250, 3310, L"3.4" }, |
---|
1091 | n/a | { 3320, 3351, L"3.5" }, |
---|
1092 | n/a | { 3360, 3379, L"3.6" }, |
---|
1093 | n/a | { 3390, 3399, L"3.7" }, |
---|
1094 | n/a | { 0 } |
---|
1095 | n/a | }; |
---|
1096 | n/a | |
---|
1097 | n/a | static INSTALLED_PYTHON * |
---|
1098 | n/a | find_by_magic(unsigned short magic) |
---|
1099 | n/a | { |
---|
1100 | n/a | INSTALLED_PYTHON * result = NULL; |
---|
1101 | n/a | PYC_MAGIC * mp; |
---|
1102 | n/a | |
---|
1103 | n/a | for (mp = magic_values; mp->min; mp++) { |
---|
1104 | n/a | if ((magic >= mp->min) && (magic <= mp->max)) { |
---|
1105 | n/a | result = locate_python(mp->version, FALSE); |
---|
1106 | n/a | if (result != NULL) |
---|
1107 | n/a | break; |
---|
1108 | n/a | } |
---|
1109 | n/a | } |
---|
1110 | n/a | return result; |
---|
1111 | n/a | } |
---|
1112 | n/a | |
---|
1113 | n/a | static void |
---|
1114 | n/a | maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline) |
---|
1115 | n/a | { |
---|
1116 | n/a | /* |
---|
1117 | n/a | * Look for a shebang line in the first argument. If found |
---|
1118 | n/a | * and we spawn a child process, this never returns. If it |
---|
1119 | n/a | * does return then we process the args "normally". |
---|
1120 | n/a | * |
---|
1121 | n/a | * argv[0] might be a filename with a shebang. |
---|
1122 | n/a | */ |
---|
1123 | n/a | FILE * fp; |
---|
1124 | n/a | errno_t rc = _wfopen_s(&fp, *argv, L"rb"); |
---|
1125 | n/a | char buffer[BUFSIZE]; |
---|
1126 | n/a | wchar_t shebang_line[BUFSIZE + 1]; |
---|
1127 | n/a | size_t read; |
---|
1128 | n/a | char *p; |
---|
1129 | n/a | char * start; |
---|
1130 | n/a | char * shebang_alias = (char *) shebang_line; |
---|
1131 | n/a | BOM* bom; |
---|
1132 | n/a | int i, j, nchars = 0; |
---|
1133 | n/a | int header_len; |
---|
1134 | n/a | BOOL is_virt; |
---|
1135 | n/a | BOOL search; |
---|
1136 | n/a | wchar_t * command; |
---|
1137 | n/a | wchar_t * suffix; |
---|
1138 | n/a | COMMAND *cmd = NULL; |
---|
1139 | n/a | INSTALLED_PYTHON * ip; |
---|
1140 | n/a | |
---|
1141 | n/a | if (rc == 0) { |
---|
1142 | n/a | read = fread(buffer, sizeof(char), BUFSIZE, fp); |
---|
1143 | n/a | debug(L"maybe_handle_shebang: read %d bytes\n", read); |
---|
1144 | n/a | fclose(fp); |
---|
1145 | n/a | |
---|
1146 | n/a | if ((read >= 4) && (buffer[3] == '\n') && (buffer[2] == '\r')) { |
---|
1147 | n/a | ip = find_by_magic((((unsigned char)buffer[1]) << 8 | |
---|
1148 | n/a | (unsigned char)buffer[0]) & 0xFFFF); |
---|
1149 | n/a | if (ip != NULL) { |
---|
1150 | n/a | debug(L"script file is compiled against Python %ls\n", |
---|
1151 | n/a | ip->version); |
---|
1152 | n/a | invoke_child(ip->executable, NULL, cmdline); |
---|
1153 | n/a | } |
---|
1154 | n/a | } |
---|
1155 | n/a | /* Look for BOM */ |
---|
1156 | n/a | bom = find_BOM(buffer); |
---|
1157 | n/a | if (bom == NULL) { |
---|
1158 | n/a | start = buffer; |
---|
1159 | n/a | debug(L"maybe_handle_shebang: BOM not found, using UTF-8\n"); |
---|
1160 | n/a | bom = BOMs; /* points to UTF-8 entry - the default */ |
---|
1161 | n/a | } |
---|
1162 | n/a | else { |
---|
1163 | n/a | debug(L"maybe_handle_shebang: BOM found, code page %d\n", |
---|
1164 | n/a | bom->code_page); |
---|
1165 | n/a | start = &buffer[bom->length]; |
---|
1166 | n/a | } |
---|
1167 | n/a | p = find_terminator(start, BUFSIZE, bom); |
---|
1168 | n/a | /* |
---|
1169 | n/a | * If no CR or LF was found in the heading, |
---|
1170 | n/a | * we assume it's not a shebang file. |
---|
1171 | n/a | */ |
---|
1172 | n/a | if (p == NULL) { |
---|
1173 | n/a | debug(L"maybe_handle_shebang: No line terminator found\n"); |
---|
1174 | n/a | } |
---|
1175 | n/a | else { |
---|
1176 | n/a | /* |
---|
1177 | n/a | * Found line terminator - parse the shebang. |
---|
1178 | n/a | * |
---|
1179 | n/a | * Strictly, we don't need to handle UTF-16 anf UTF-32, |
---|
1180 | n/a | * since Python itself doesn't. |
---|
1181 | n/a | * Never mind, one day it might. |
---|
1182 | n/a | */ |
---|
1183 | n/a | header_len = (int) (p - start); |
---|
1184 | n/a | switch(bom->code_page) { |
---|
1185 | n/a | case CP_UTF8: |
---|
1186 | n/a | nchars = MultiByteToWideChar(bom->code_page, |
---|
1187 | n/a | 0, |
---|
1188 | n/a | start, header_len, shebang_line, |
---|
1189 | n/a | BUFSIZE); |
---|
1190 | n/a | break; |
---|
1191 | n/a | case CP_UTF16BE: |
---|
1192 | n/a | if (header_len % 2 != 0) { |
---|
1193 | n/a | debug(L"maybe_handle_shebang: UTF-16BE, but an odd number \ |
---|
1194 | n/a | of bytes: %d\n", header_len); |
---|
1195 | n/a | /* nchars = 0; Not needed - initialised to 0. */ |
---|
1196 | n/a | } |
---|
1197 | n/a | else { |
---|
1198 | n/a | for (i = header_len; i > 0; i -= 2) { |
---|
1199 | n/a | shebang_alias[i - 1] = start[i - 2]; |
---|
1200 | n/a | shebang_alias[i - 2] = start[i - 1]; |
---|
1201 | n/a | } |
---|
1202 | n/a | nchars = header_len / sizeof(wchar_t); |
---|
1203 | n/a | } |
---|
1204 | n/a | break; |
---|
1205 | n/a | case CP_UTF16LE: |
---|
1206 | n/a | if ((header_len % 2) != 0) { |
---|
1207 | n/a | debug(L"UTF-16LE, but an odd number of bytes: %d\n", |
---|
1208 | n/a | header_len); |
---|
1209 | n/a | /* nchars = 0; Not needed - initialised to 0. */ |
---|
1210 | n/a | } |
---|
1211 | n/a | else { |
---|
1212 | n/a | /* no actual conversion needed. */ |
---|
1213 | n/a | memcpy(shebang_line, start, header_len); |
---|
1214 | n/a | nchars = header_len / sizeof(wchar_t); |
---|
1215 | n/a | } |
---|
1216 | n/a | break; |
---|
1217 | n/a | case CP_UTF32BE: |
---|
1218 | n/a | if (header_len % 4 != 0) { |
---|
1219 | n/a | debug(L"UTF-32BE, but not divisible by 4: %d\n", |
---|
1220 | n/a | header_len); |
---|
1221 | n/a | /* nchars = 0; Not needed - initialised to 0. */ |
---|
1222 | n/a | } |
---|
1223 | n/a | else { |
---|
1224 | n/a | for (i = header_len, j = header_len / 2; i > 0; i -= 4, |
---|
1225 | n/a | j -= 2) { |
---|
1226 | n/a | shebang_alias[j - 1] = start[i - 2]; |
---|
1227 | n/a | shebang_alias[j - 2] = start[i - 1]; |
---|
1228 | n/a | } |
---|
1229 | n/a | nchars = header_len / sizeof(wchar_t); |
---|
1230 | n/a | } |
---|
1231 | n/a | break; |
---|
1232 | n/a | case CP_UTF32LE: |
---|
1233 | n/a | if (header_len % 4 != 0) { |
---|
1234 | n/a | debug(L"UTF-32LE, but not divisible by 4: %d\n", |
---|
1235 | n/a | header_len); |
---|
1236 | n/a | /* nchars = 0; Not needed - initialised to 0. */ |
---|
1237 | n/a | } |
---|
1238 | n/a | else { |
---|
1239 | n/a | for (i = header_len, j = header_len / 2; i > 0; i -= 4, |
---|
1240 | n/a | j -= 2) { |
---|
1241 | n/a | shebang_alias[j - 1] = start[i - 3]; |
---|
1242 | n/a | shebang_alias[j - 2] = start[i - 4]; |
---|
1243 | n/a | } |
---|
1244 | n/a | nchars = header_len / sizeof(wchar_t); |
---|
1245 | n/a | } |
---|
1246 | n/a | break; |
---|
1247 | n/a | } |
---|
1248 | n/a | if (nchars > 0) { |
---|
1249 | n/a | shebang_line[--nchars] = L'\0'; |
---|
1250 | n/a | is_virt = parse_shebang(shebang_line, nchars, &command, |
---|
1251 | n/a | &suffix, &search); |
---|
1252 | n/a | if (command != NULL) { |
---|
1253 | n/a | debug(L"parse_shebang: found command: %ls\n", command); |
---|
1254 | n/a | if (!is_virt) { |
---|
1255 | n/a | invoke_child(command, suffix, cmdline); |
---|
1256 | n/a | } |
---|
1257 | n/a | else { |
---|
1258 | n/a | suffix = wcschr(command, L' '); |
---|
1259 | n/a | if (suffix != NULL) { |
---|
1260 | n/a | *suffix++ = L'\0'; |
---|
1261 | n/a | suffix = skip_whitespace(suffix); |
---|
1262 | n/a | } |
---|
1263 | n/a | if (wcsncmp(command, L"python", 6)) |
---|
1264 | n/a | error(RC_BAD_VIRTUAL_PATH, L"Unknown virtual \ |
---|
1265 | n/a | path '%ls'", command); |
---|
1266 | n/a | command += 6; /* skip past "python" */ |
---|
1267 | n/a | if (search && ((*command == L'\0') || isspace(*command))) { |
---|
1268 | n/a | /* Command is eligible for path search, and there |
---|
1269 | n/a | * is no version specification. |
---|
1270 | n/a | */ |
---|
1271 | n/a | debug(L"searching PATH for python executable\n"); |
---|
1272 | n/a | cmd = find_on_path(PYTHON_EXECUTABLE); |
---|
1273 | n/a | debug(L"Python on path: %ls\n", cmd ? cmd->value : L"<not found>"); |
---|
1274 | n/a | if (cmd) { |
---|
1275 | n/a | debug(L"located python on PATH: %ls\n", cmd->value); |
---|
1276 | n/a | invoke_child(cmd->value, suffix, cmdline); |
---|
1277 | n/a | /* Exit here, as we have found the command */ |
---|
1278 | n/a | return; |
---|
1279 | n/a | } |
---|
1280 | n/a | /* FALL THROUGH: No python found on PATH, so fall |
---|
1281 | n/a | * back to locating the correct installed python. |
---|
1282 | n/a | */ |
---|
1283 | n/a | } |
---|
1284 | n/a | if (*command && !validate_version(command)) |
---|
1285 | n/a | error(RC_BAD_VIRTUAL_PATH, L"Invalid version \ |
---|
1286 | n/a | specification: '%ls'.\nIn the first line of the script, 'python' needs to be \ |
---|
1287 | n/a | followed by a valid version specifier.\nPlease check the documentation.", |
---|
1288 | n/a | command); |
---|
1289 | n/a | /* TODO could call validate_version(command) */ |
---|
1290 | n/a | ip = locate_python(command, TRUE); |
---|
1291 | n/a | if (ip == NULL) { |
---|
1292 | n/a | error(RC_NO_PYTHON, L"Requested Python version \ |
---|
1293 | n/a | (%ls) is not installed", command); |
---|
1294 | n/a | } |
---|
1295 | n/a | else { |
---|
1296 | n/a | invoke_child(ip->executable, suffix, cmdline); |
---|
1297 | n/a | } |
---|
1298 | n/a | } |
---|
1299 | n/a | } |
---|
1300 | n/a | } |
---|
1301 | n/a | } |
---|
1302 | n/a | } |
---|
1303 | n/a | } |
---|
1304 | n/a | |
---|
1305 | n/a | static wchar_t * |
---|
1306 | n/a | skip_me(wchar_t * cmdline) |
---|
1307 | n/a | { |
---|
1308 | n/a | BOOL quoted; |
---|
1309 | n/a | wchar_t c; |
---|
1310 | n/a | wchar_t * result = cmdline; |
---|
1311 | n/a | |
---|
1312 | n/a | quoted = cmdline[0] == L'\"'; |
---|
1313 | n/a | if (!quoted) |
---|
1314 | n/a | c = L' '; |
---|
1315 | n/a | else { |
---|
1316 | n/a | c = L'\"'; |
---|
1317 | n/a | ++result; |
---|
1318 | n/a | } |
---|
1319 | n/a | result = wcschr(result, c); |
---|
1320 | n/a | if (result == NULL) /* when, for example, just exe name on command line */ |
---|
1321 | n/a | result = L""; |
---|
1322 | n/a | else { |
---|
1323 | n/a | ++result; /* skip past space or closing quote */ |
---|
1324 | n/a | result = skip_whitespace(result); |
---|
1325 | n/a | } |
---|
1326 | n/a | return result; |
---|
1327 | n/a | } |
---|
1328 | n/a | |
---|
1329 | n/a | static DWORD version_high = 0; |
---|
1330 | n/a | static DWORD version_low = 0; |
---|
1331 | n/a | |
---|
1332 | n/a | static void |
---|
1333 | n/a | get_version_info(wchar_t * version_text, size_t size) |
---|
1334 | n/a | { |
---|
1335 | n/a | WORD maj, min, rel, bld; |
---|
1336 | n/a | |
---|
1337 | n/a | if (!version_high && !version_low) |
---|
1338 | n/a | wcsncpy_s(version_text, size, L"0.1", _TRUNCATE); /* fallback */ |
---|
1339 | n/a | else { |
---|
1340 | n/a | maj = HIWORD(version_high); |
---|
1341 | n/a | min = LOWORD(version_high); |
---|
1342 | n/a | rel = HIWORD(version_low); |
---|
1343 | n/a | bld = LOWORD(version_low); |
---|
1344 | n/a | _snwprintf_s(version_text, size, _TRUNCATE, L"%d.%d.%d.%d", maj, |
---|
1345 | n/a | min, rel, bld); |
---|
1346 | n/a | } |
---|
1347 | n/a | } |
---|
1348 | n/a | |
---|
1349 | n/a | static int |
---|
1350 | n/a | process(int argc, wchar_t ** argv) |
---|
1351 | n/a | { |
---|
1352 | n/a | wchar_t * wp; |
---|
1353 | n/a | wchar_t * command; |
---|
1354 | n/a | wchar_t * executable; |
---|
1355 | n/a | wchar_t * p; |
---|
1356 | n/a | int rc = 0; |
---|
1357 | n/a | size_t plen; |
---|
1358 | n/a | INSTALLED_PYTHON * ip; |
---|
1359 | n/a | BOOL valid; |
---|
1360 | n/a | DWORD size, attrs; |
---|
1361 | n/a | HRESULT hr; |
---|
1362 | n/a | wchar_t message[MSGSIZE]; |
---|
1363 | n/a | wchar_t version_text [MAX_PATH]; |
---|
1364 | n/a | void * version_data; |
---|
1365 | n/a | VS_FIXEDFILEINFO * file_info; |
---|
1366 | n/a | UINT block_size; |
---|
1367 | n/a | int index; |
---|
1368 | n/a | #if defined(SCRIPT_WRAPPER) |
---|
1369 | n/a | int newlen; |
---|
1370 | n/a | wchar_t * newcommand; |
---|
1371 | n/a | wchar_t * av[2]; |
---|
1372 | n/a | #endif |
---|
1373 | n/a | |
---|
1374 | n/a | setvbuf(stderr, (char *)NULL, _IONBF, 0); |
---|
1375 | n/a | wp = get_env(L"PYLAUNCH_DEBUG"); |
---|
1376 | n/a | if ((wp != NULL) && (*wp != L'\0')) |
---|
1377 | n/a | log_fp = stderr; |
---|
1378 | n/a | |
---|
1379 | n/a | #if defined(_M_X64) |
---|
1380 | n/a | debug(L"launcher build: 64bit\n"); |
---|
1381 | n/a | #else |
---|
1382 | n/a | debug(L"launcher build: 32bit\n"); |
---|
1383 | n/a | #endif |
---|
1384 | n/a | #if defined(_WINDOWS) |
---|
1385 | n/a | debug(L"launcher executable: Windows\n"); |
---|
1386 | n/a | #else |
---|
1387 | n/a | debug(L"launcher executable: Console\n"); |
---|
1388 | n/a | #endif |
---|
1389 | n/a | /* Get the local appdata folder (non-roaming) */ |
---|
1390 | n/a | hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, |
---|
1391 | n/a | NULL, 0, appdata_ini_path); |
---|
1392 | n/a | if (hr != S_OK) { |
---|
1393 | n/a | debug(L"SHGetFolderPath failed: %X\n", hr); |
---|
1394 | n/a | appdata_ini_path[0] = L'\0'; |
---|
1395 | n/a | } |
---|
1396 | n/a | else { |
---|
1397 | n/a | plen = wcslen(appdata_ini_path); |
---|
1398 | n/a | p = &appdata_ini_path[plen]; |
---|
1399 | n/a | wcsncpy_s(p, MAX_PATH - plen, L"\\py.ini", _TRUNCATE); |
---|
1400 | n/a | attrs = GetFileAttributesW(appdata_ini_path); |
---|
1401 | n/a | if (attrs == INVALID_FILE_ATTRIBUTES) { |
---|
1402 | n/a | debug(L"File '%ls' non-existent\n", appdata_ini_path); |
---|
1403 | n/a | appdata_ini_path[0] = L'\0'; |
---|
1404 | n/a | } else { |
---|
1405 | n/a | debug(L"Using local configuration file '%ls'\n", appdata_ini_path); |
---|
1406 | n/a | } |
---|
1407 | n/a | } |
---|
1408 | n/a | plen = GetModuleFileNameW(NULL, launcher_ini_path, MAX_PATH); |
---|
1409 | n/a | size = GetFileVersionInfoSizeW(launcher_ini_path, &size); |
---|
1410 | n/a | if (size == 0) { |
---|
1411 | n/a | winerror(GetLastError(), message, MSGSIZE); |
---|
1412 | n/a | debug(L"GetFileVersionInfoSize failed: %ls\n", message); |
---|
1413 | n/a | } |
---|
1414 | n/a | else { |
---|
1415 | n/a | version_data = malloc(size); |
---|
1416 | n/a | if (version_data) { |
---|
1417 | n/a | valid = GetFileVersionInfoW(launcher_ini_path, 0, size, |
---|
1418 | n/a | version_data); |
---|
1419 | n/a | if (!valid) |
---|
1420 | n/a | debug(L"GetFileVersionInfo failed: %X\n", GetLastError()); |
---|
1421 | n/a | else { |
---|
1422 | n/a | valid = VerQueryValueW(version_data, L"\\", |
---|
1423 | n/a | (LPVOID *) &file_info, &block_size); |
---|
1424 | n/a | if (!valid) |
---|
1425 | n/a | debug(L"VerQueryValue failed: %X\n", GetLastError()); |
---|
1426 | n/a | else { |
---|
1427 | n/a | version_high = file_info->dwFileVersionMS; |
---|
1428 | n/a | version_low = file_info->dwFileVersionLS; |
---|
1429 | n/a | } |
---|
1430 | n/a | } |
---|
1431 | n/a | free(version_data); |
---|
1432 | n/a | } |
---|
1433 | n/a | } |
---|
1434 | n/a | p = wcsrchr(launcher_ini_path, L'\\'); |
---|
1435 | n/a | if (p == NULL) { |
---|
1436 | n/a | debug(L"GetModuleFileNameW returned value has no backslash: %ls\n", |
---|
1437 | n/a | launcher_ini_path); |
---|
1438 | n/a | launcher_ini_path[0] = L'\0'; |
---|
1439 | n/a | } |
---|
1440 | n/a | else { |
---|
1441 | n/a | wcsncpy_s(p, MAX_PATH - (p - launcher_ini_path), L"\\py.ini", |
---|
1442 | n/a | _TRUNCATE); |
---|
1443 | n/a | attrs = GetFileAttributesW(launcher_ini_path); |
---|
1444 | n/a | if (attrs == INVALID_FILE_ATTRIBUTES) { |
---|
1445 | n/a | debug(L"File '%ls' non-existent\n", launcher_ini_path); |
---|
1446 | n/a | launcher_ini_path[0] = L'\0'; |
---|
1447 | n/a | } else { |
---|
1448 | n/a | debug(L"Using global configuration file '%ls'\n", launcher_ini_path); |
---|
1449 | n/a | } |
---|
1450 | n/a | } |
---|
1451 | n/a | |
---|
1452 | n/a | command = skip_me(GetCommandLineW()); |
---|
1453 | n/a | debug(L"Called with command line: %ls\n", command); |
---|
1454 | n/a | |
---|
1455 | n/a | #if defined(SCRIPT_WRAPPER) |
---|
1456 | n/a | /* The launcher is being used in "script wrapper" mode. |
---|
1457 | n/a | * There should therefore be a Python script named <exename>-script.py in |
---|
1458 | n/a | * the same directory as the launcher executable. |
---|
1459 | n/a | * Put the script name into argv as the first (script name) argument. |
---|
1460 | n/a | */ |
---|
1461 | n/a | |
---|
1462 | n/a | /* Get the wrapped script name - if the script is not present, this will |
---|
1463 | n/a | * terminate the program with an error. |
---|
1464 | n/a | */ |
---|
1465 | n/a | locate_wrapped_script(); |
---|
1466 | n/a | |
---|
1467 | n/a | /* Add the wrapped script to the start of command */ |
---|
1468 | n/a | newlen = wcslen(wrapped_script_path) + wcslen(command) + 2; /* ' ' + NUL */ |
---|
1469 | n/a | newcommand = malloc(sizeof(wchar_t) * newlen); |
---|
1470 | n/a | if (!newcommand) { |
---|
1471 | n/a | error(RC_NO_MEMORY, L"Could not allocate new command line"); |
---|
1472 | n/a | } |
---|
1473 | n/a | else { |
---|
1474 | n/a | wcscpy_s(newcommand, newlen, wrapped_script_path); |
---|
1475 | n/a | wcscat_s(newcommand, newlen, L" "); |
---|
1476 | n/a | wcscat_s(newcommand, newlen, command); |
---|
1477 | n/a | debug(L"Running wrapped script with command line '%ls'\n", newcommand); |
---|
1478 | n/a | read_commands(); |
---|
1479 | n/a | av[0] = wrapped_script_path; |
---|
1480 | n/a | av[1] = NULL; |
---|
1481 | n/a | maybe_handle_shebang(av, newcommand); |
---|
1482 | n/a | /* Returns if no shebang line - pass to default processing */ |
---|
1483 | n/a | command = newcommand; |
---|
1484 | n/a | valid = FALSE; |
---|
1485 | n/a | } |
---|
1486 | n/a | #else |
---|
1487 | n/a | if (argc <= 1) { |
---|
1488 | n/a | valid = FALSE; |
---|
1489 | n/a | p = NULL; |
---|
1490 | n/a | } |
---|
1491 | n/a | else { |
---|
1492 | n/a | p = argv[1]; |
---|
1493 | n/a | plen = wcslen(p); |
---|
1494 | n/a | valid = (*p == L'-') && validate_version(&p[1]); |
---|
1495 | n/a | if (valid) { |
---|
1496 | n/a | ip = locate_python(&p[1], FALSE); |
---|
1497 | n/a | if (ip == NULL) |
---|
1498 | n/a | error(RC_NO_PYTHON, L"Requested Python version (%ls) not \ |
---|
1499 | n/a | installed", &p[1]); |
---|
1500 | n/a | executable = ip->executable; |
---|
1501 | n/a | command += wcslen(p); |
---|
1502 | n/a | command = skip_whitespace(command); |
---|
1503 | n/a | } |
---|
1504 | n/a | else { |
---|
1505 | n/a | for (index = 1; index < argc; ++index) { |
---|
1506 | n/a | if (*argv[index] != L'-') |
---|
1507 | n/a | break; |
---|
1508 | n/a | } |
---|
1509 | n/a | if (index < argc) { |
---|
1510 | n/a | read_commands(); |
---|
1511 | n/a | maybe_handle_shebang(&argv[index], command); |
---|
1512 | n/a | } |
---|
1513 | n/a | } |
---|
1514 | n/a | } |
---|
1515 | n/a | #endif |
---|
1516 | n/a | |
---|
1517 | n/a | if (!valid) { |
---|
1518 | n/a | /* Look for an active virtualenv */ |
---|
1519 | n/a | executable = find_python_by_venv(); |
---|
1520 | n/a | |
---|
1521 | n/a | /* If we didn't find one, look for the default Python */ |
---|
1522 | n/a | if (executable == NULL) { |
---|
1523 | n/a | ip = locate_python(L"", FALSE); |
---|
1524 | n/a | if (ip == NULL) |
---|
1525 | n/a | error(RC_NO_PYTHON, L"Can't find a default Python."); |
---|
1526 | n/a | executable = ip->executable; |
---|
1527 | n/a | } |
---|
1528 | n/a | if ((argc == 2) && (!_wcsicmp(p, L"-h") || !_wcsicmp(p, L"--help"))) { |
---|
1529 | n/a | #if defined(_M_X64) |
---|
1530 | n/a | BOOL canDo64bit = TRUE; |
---|
1531 | n/a | #else |
---|
1532 | n/a | // If we are a 32bit process on a 64bit Windows, first hit the 64bit keys. |
---|
1533 | n/a | BOOL canDo64bit = FALSE; |
---|
1534 | n/a | IsWow64Process(GetCurrentProcess(), &canDo64bit); |
---|
1535 | n/a | #endif |
---|
1536 | n/a | |
---|
1537 | n/a | get_version_info(version_text, MAX_PATH); |
---|
1538 | n/a | fwprintf(stdout, L"\ |
---|
1539 | n/a | Python Launcher for Windows Version %ls\n\n", version_text); |
---|
1540 | n/a | fwprintf(stdout, L"\ |
---|
1541 | n/a | usage: %ls [ launcher-arguments ] [ python-arguments ] script [ script-arguments ]\n\n", argv[0]); |
---|
1542 | n/a | fputws(L"\ |
---|
1543 | n/a | Launcher arguments:\n\n\ |
---|
1544 | n/a | -2 : Launch the latest Python 2.x version\n\ |
---|
1545 | n/a | -3 : Launch the latest Python 3.x version\n\ |
---|
1546 | n/a | -X.Y : Launch the specified Python version\n", stdout); |
---|
1547 | n/a | if (canDo64bit) { |
---|
1548 | n/a | fputws(L"\ |
---|
1549 | n/a | -X.Y-32: Launch the specified 32bit Python version", stdout); |
---|
1550 | n/a | } |
---|
1551 | n/a | fputws(L"\n\nThe following help text is from Python:\n\n", stdout); |
---|
1552 | n/a | fflush(stdout); |
---|
1553 | n/a | } |
---|
1554 | n/a | } |
---|
1555 | n/a | invoke_child(executable, NULL, command); |
---|
1556 | n/a | return rc; |
---|
1557 | n/a | } |
---|
1558 | n/a | |
---|
1559 | n/a | #if defined(_WINDOWS) |
---|
1560 | n/a | |
---|
1561 | n/a | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
---|
1562 | n/a | LPWSTR lpstrCmd, int nShow) |
---|
1563 | n/a | { |
---|
1564 | n/a | return process(__argc, __wargv); |
---|
1565 | n/a | } |
---|
1566 | n/a | |
---|
1567 | n/a | #else |
---|
1568 | n/a | |
---|
1569 | n/a | int cdecl wmain(int argc, wchar_t ** argv) |
---|
1570 | n/a | { |
---|
1571 | n/a | return process(argc, argv); |
---|
1572 | n/a | } |
---|
1573 | n/a | |
---|
1574 | n/a | #endif |
---|