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