You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1514 lines
48 KiB

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