Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 1 | /* |
Martin v. Löwis | 6a8ca3e | 2012-06-21 19:29:37 +0200 | [diff] [blame] | 2 | * Copyright (C) 2011-2012 Vinay Sajip. |
| 3 | * Licensed to PSF under a contributor agreement. |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 4 | * |
| 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 |
| 21 | /* #define SEARCH_PATH */ |
| 22 | |
| 23 | /* Just for now - static definition */ |
| 24 | |
| 25 | static FILE * log_fp = NULL; |
| 26 | |
| 27 | static wchar_t * |
| 28 | skip_whitespace(wchar_t * p) |
| 29 | { |
| 30 | while (*p && isspace(*p)) |
| 31 | ++p; |
| 32 | return p; |
| 33 | } |
| 34 | |
| 35 | /* |
Martin v. Löwis | f36d65c | 2012-06-21 16:33:09 +0200 | [diff] [blame] | 36 | * This function is here to simplify memory management |
| 37 | * and to treat blank values as if they are absent. |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 38 | */ |
| 39 | static wchar_t * get_env(wchar_t * key) |
| 40 | { |
Martin v. Löwis | f36d65c | 2012-06-21 16:33:09 +0200 | [diff] [blame] | 41 | /* This is not thread-safe, just like getenv */ |
| 42 | static wchar_t buf[256]; |
| 43 | DWORD result = GetEnvironmentVariableW(key, buf, 256); |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 44 | |
Martin v. Löwis | 91a3468 | 2012-06-21 17:36:15 +0200 | [diff] [blame] | 45 | if (result > 255) { |
Martin v. Löwis | f36d65c | 2012-06-21 16:33:09 +0200 | [diff] [blame] | 46 | /* 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 Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 50 | } |
Martin v. Löwis | f36d65c | 2012-06-21 16:33:09 +0200 | [diff] [blame] | 51 | |
| 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 Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Martin v. Löwis | f36d65c | 2012-06-21 16:33:09 +0200 | [diff] [blame] | 60 | |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 61 | static void |
| 62 | debug(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 | |
| 72 | static void |
| 73 | winerror(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 | |
| 81 | static void |
| 82 | error(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 | |
| 125 | typedef 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 | |
| 137 | static INSTALLED_PYTHON installed_pythons[MAX_INSTALLED_PYTHONS]; |
| 138 | |
| 139 | static 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 | |
| 146 | static wchar_t * location_checks[] = { |
| 147 | L"\\", |
| 148 | L"\\PCBuild\\", |
| 149 | L"\\PCBuild\\amd64\\", |
| 150 | NULL |
| 151 | }; |
| 152 | |
| 153 | static INSTALLED_PYTHON * |
| 154 | find_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 | |
| 169 | static void |
| 170 | locate_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öwis | af21ebb | 2012-06-21 18:15:54 +0200 | [diff] [blame] | 212 | status = RegQueryValueExW(ip_key, NULL, NULL, &type, |
| 213 | (LPBYTE)ip->executable, &data_size); |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 214 | 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 \ |
| 239 | directory\n", |
| 240 | ip->executable, attrs); |
| 241 | } |
| 242 | else if (find_existing_python(ip->executable)) { |
| 243 | debug(L"locate_pythons_for_key: %s: already \ |
| 244 | found: %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: \ |
| 262 | invalid 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 \ |
| 276 | is 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 | |
| 296 | static int |
| 297 | compare_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 | |
| 309 | static void |
| 310 | locate_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 | |
| 334 | static INSTALLED_PYTHON * |
| 335 | find_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 | |
| 360 | static wchar_t appdata_ini_path[MAX_PATH]; |
| 361 | static 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 | */ |
| 367 | static wchar_t * |
| 368 | get_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 | |
| 412 | static INSTALLED_PYTHON * |
| 413 | locate_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 | |
| 464 | static BOOL |
| 465 | safe_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 | |
| 487 | static BOOL WINAPI |
| 488 | ctrl_c_handler(DWORD code) |
| 489 | { |
| 490 | return TRUE; /* We just ignore all control events. */ |
| 491 | } |
| 492 | |
| 493 | static void |
| 494 | run_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 Sajip | 66fef9f | 2013-02-26 16:29:06 +0000 | [diff] [blame] | 503 | #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 Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 518 | 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); |
| 553 | WaitForSingleObject(pi.hProcess, INFINITE); |
| 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 | |
| 561 | static void |
| 562 | invoke_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 | |
| 598 | static wchar_t * builtin_virtual_paths [] = { |
| 599 | L"/usr/bin/env python", |
| 600 | L"/usr/bin/python", |
| 601 | L"/usr/local/bin/python", |
| 602 | L"python", |
| 603 | NULL |
| 604 | }; |
| 605 | |
| 606 | /* For now, a static array of commands. */ |
| 607 | |
| 608 | #define MAX_COMMANDS 100 |
| 609 | |
| 610 | typedef struct { |
| 611 | wchar_t key[MAX_PATH]; |
| 612 | wchar_t value[MSGSIZE]; |
| 613 | } COMMAND; |
| 614 | |
| 615 | static COMMAND commands[MAX_COMMANDS]; |
| 616 | static int num_commands = 0; |
| 617 | |
| 618 | #if defined(SKIP_PREFIX) |
| 619 | |
| 620 | static wchar_t * builtin_prefixes [] = { |
| 621 | /* These must be in an order that the longest matches should be found, |
| 622 | * i.e. if the prefix is "/usr/bin/env ", it should match that entry |
| 623 | * *before* matching "/usr/bin/". |
| 624 | */ |
| 625 | L"/usr/bin/env ", |
| 626 | L"/usr/bin/", |
| 627 | L"/usr/local/bin/", |
| 628 | NULL |
| 629 | }; |
| 630 | |
| 631 | static wchar_t * skip_prefix(wchar_t * name) |
| 632 | { |
| 633 | wchar_t ** pp = builtin_prefixes; |
| 634 | wchar_t * result = name; |
| 635 | wchar_t * p; |
| 636 | size_t n; |
| 637 | |
| 638 | for (; p = *pp; pp++) { |
| 639 | n = wcslen(p); |
| 640 | if (_wcsnicmp(p, name, n) == 0) { |
| 641 | result += n; /* skip the prefix */ |
| 642 | if (p[n - 1] == L' ') /* No empty strings in table, so n > 1 */ |
| 643 | result = skip_whitespace(result); |
| 644 | break; |
| 645 | } |
| 646 | } |
| 647 | return result; |
| 648 | } |
| 649 | |
| 650 | #endif |
| 651 | |
| 652 | #if defined(SEARCH_PATH) |
| 653 | |
| 654 | static COMMAND path_command; |
| 655 | |
| 656 | static COMMAND * find_on_path(wchar_t * name) |
| 657 | { |
| 658 | wchar_t * pathext; |
| 659 | size_t varsize; |
| 660 | wchar_t * context = NULL; |
| 661 | wchar_t * extension; |
| 662 | COMMAND * result = NULL; |
| 663 | DWORD len; |
| 664 | errno_t rc; |
| 665 | |
| 666 | wcscpy_s(path_command.key, MAX_PATH, name); |
| 667 | if (wcschr(name, L'.') != NULL) { |
| 668 | /* assume it has an extension. */ |
| 669 | len = SearchPathW(NULL, name, NULL, MSGSIZE, path_command.value, NULL); |
| 670 | if (len) { |
| 671 | result = &path_command; |
| 672 | } |
| 673 | } |
| 674 | else { |
| 675 | /* No extension - search using registered extensions. */ |
| 676 | rc = _wdupenv_s(&pathext, &varsize, L"PATHEXT"); |
| 677 | if (rc == 0) { |
| 678 | extension = wcstok_s(pathext, L";", &context); |
| 679 | while (extension) { |
| 680 | len = SearchPathW(NULL, name, extension, MSGSIZE, path_command.value, NULL); |
| 681 | if (len) { |
| 682 | result = &path_command; |
| 683 | break; |
| 684 | } |
| 685 | extension = wcstok_s(NULL, L";", &context); |
| 686 | } |
| 687 | free(pathext); |
| 688 | } |
| 689 | } |
| 690 | return result; |
| 691 | } |
| 692 | |
| 693 | #endif |
| 694 | |
| 695 | static COMMAND * find_command(wchar_t * name) |
| 696 | { |
| 697 | COMMAND * result = NULL; |
| 698 | COMMAND * cp = commands; |
| 699 | int i; |
| 700 | |
| 701 | for (i = 0; i < num_commands; i++, cp++) { |
| 702 | if (_wcsicmp(cp->key, name) == 0) { |
| 703 | result = cp; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | #if defined(SEARCH_PATH) |
| 708 | if (result == NULL) |
| 709 | result = find_on_path(name); |
| 710 | #endif |
| 711 | return result; |
| 712 | } |
| 713 | |
| 714 | static void |
| 715 | update_command(COMMAND * cp, wchar_t * name, wchar_t * cmdline) |
| 716 | { |
| 717 | wcsncpy_s(cp->key, MAX_PATH, name, _TRUNCATE); |
| 718 | wcsncpy_s(cp->value, MSGSIZE, cmdline, _TRUNCATE); |
| 719 | } |
| 720 | |
| 721 | static void |
| 722 | add_command(wchar_t * name, wchar_t * cmdline) |
| 723 | { |
| 724 | if (num_commands >= MAX_COMMANDS) { |
| 725 | debug(L"can't add %s = '%s': no room\n", name, cmdline); |
| 726 | } |
| 727 | else { |
| 728 | COMMAND * cp = &commands[num_commands++]; |
| 729 | |
| 730 | update_command(cp, name, cmdline); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | static void |
| 735 | read_config_file(wchar_t * config_path) |
| 736 | { |
| 737 | wchar_t keynames[MSGSIZE]; |
| 738 | wchar_t value[MSGSIZE]; |
| 739 | DWORD read; |
| 740 | wchar_t * key; |
| 741 | COMMAND * cp; |
| 742 | wchar_t * cmdp; |
| 743 | |
| 744 | read = GetPrivateProfileStringW(L"commands", NULL, NULL, keynames, MSGSIZE, |
| 745 | config_path); |
| 746 | if (read == MSGSIZE - 1) { |
| 747 | debug(L"read_commands: %s: not enough space for names\n", config_path); |
| 748 | } |
| 749 | key = keynames; |
| 750 | while (*key) { |
| 751 | read = GetPrivateProfileStringW(L"commands", key, NULL, value, MSGSIZE, |
| 752 | config_path); |
| 753 | if (read == MSGSIZE - 1) { |
| 754 | debug(L"read_commands: %s: not enough space for %s\n", |
| 755 | config_path, key); |
| 756 | } |
| 757 | cmdp = skip_whitespace(value); |
| 758 | if (*cmdp) { |
| 759 | cp = find_command(key); |
| 760 | if (cp == NULL) |
| 761 | add_command(key, value); |
| 762 | else |
| 763 | update_command(cp, key, value); |
| 764 | } |
| 765 | key += wcslen(key) + 1; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | static void read_commands() |
| 770 | { |
| 771 | if (launcher_ini_path[0]) |
| 772 | read_config_file(launcher_ini_path); |
| 773 | if (appdata_ini_path[0]) |
| 774 | read_config_file(appdata_ini_path); |
| 775 | } |
| 776 | |
| 777 | static BOOL |
| 778 | parse_shebang(wchar_t * shebang_line, int nchars, wchar_t ** command, |
| 779 | wchar_t ** suffix) |
| 780 | { |
| 781 | BOOL rc = FALSE; |
| 782 | wchar_t ** vpp; |
| 783 | size_t plen; |
| 784 | wchar_t * p; |
| 785 | wchar_t zapped; |
| 786 | wchar_t * endp = shebang_line + nchars - 1; |
| 787 | COMMAND * cp; |
| 788 | wchar_t * skipped; |
| 789 | |
| 790 | *command = NULL; /* failure return */ |
| 791 | *suffix = NULL; |
| 792 | |
| 793 | if ((*shebang_line++ == L'#') && (*shebang_line++ == L'!')) { |
| 794 | shebang_line = skip_whitespace(shebang_line); |
| 795 | if (*shebang_line) { |
| 796 | *command = shebang_line; |
| 797 | for (vpp = builtin_virtual_paths; *vpp; ++vpp) { |
| 798 | plen = wcslen(*vpp); |
| 799 | if (wcsncmp(shebang_line, *vpp, plen) == 0) { |
| 800 | rc = TRUE; |
| 801 | /* We can do this because all builtin commands contain |
| 802 | * "python". |
| 803 | */ |
| 804 | *command = wcsstr(shebang_line, L"python"); |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | if (*vpp == NULL) { |
| 809 | /* |
Vinay Sajip | 9c10d6b | 2013-11-15 20:58:13 +0000 | [diff] [blame] | 810 | * Not found in builtins - look in customized commands. |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 811 | * |
| 812 | * We can't permanently modify the shebang line in case |
Vinay Sajip | 9c10d6b | 2013-11-15 20:58:13 +0000 | [diff] [blame] | 813 | * it's not a customized command, but we can temporarily |
Brian Curtin | 07165f7 | 2012-06-20 15:36:14 -0500 | [diff] [blame] | 814 | * stick a NUL after the command while searching for it, |
| 815 | * then put back the char we zapped. |
| 816 | */ |
| 817 | #if defined(SKIP_PREFIX) |
| 818 | skipped = skip_prefix(shebang_line); |
| 819 | #else |
| 820 | skipped = shebang_line; |
| 821 | #endif |
| 822 | p = wcspbrk(skipped, L" \t\r\n"); |
| 823 | if (p != NULL) { |
| 824 | zapped = *p; |
| 825 | *p = L'\0'; |
| 826 | } |
| 827 | cp = find_command(skipped); |
| 828 | if (p != NULL) |
| 829 | *p = zapped; |
| 830 | if (cp != NULL) { |
| 831 | *command = cp->value; |
| 832 | if (p != NULL) |
| 833 | *suffix = skip_whitespace(p); |
| 834 | } |
| 835 | } |
| 836 | /* remove trailing whitespace */ |
| 837 | while ((endp > shebang_line) && isspace(*endp)) |
| 838 | --endp; |
| 839 | if (endp > shebang_line) |
| 840 | endp[1] = L'\0'; |
| 841 | } |
| 842 | } |
| 843 | return rc; |
| 844 | } |
| 845 | |
| 846 | /* #define CP_UTF8 65001 defined in winnls.h */ |
| 847 | #define CP_UTF16LE 1200 |
| 848 | #define CP_UTF16BE 1201 |
| 849 | #define CP_UTF32LE 12000 |
| 850 | #define CP_UTF32BE 12001 |
| 851 | |
| 852 | typedef struct { |
| 853 | int length; |
| 854 | char sequence[4]; |
| 855 | UINT code_page; |
| 856 | } BOM; |
| 857 | |
| 858 | /* |
| 859 | * Strictly, we don't need to handle UTF-16 anf UTF-32, since Python itself |
| 860 | * doesn't. Never mind, one day it might - there's no harm leaving it in. |
| 861 | */ |
| 862 | static BOM BOMs[] = { |
| 863 | { 3, { 0xEF, 0xBB, 0xBF }, CP_UTF8 }, /* UTF-8 - keep first */ |
| 864 | { 2, { 0xFF, 0xFE }, CP_UTF16LE }, /* UTF-16LE */ |
| 865 | { 2, { 0xFE, 0xFF }, CP_UTF16BE }, /* UTF-16BE */ |
| 866 | { 4, { 0xFF, 0xFE, 0x00, 0x00 }, CP_UTF32LE }, /* UTF-32LE */ |
| 867 | { 4, { 0x00, 0x00, 0xFE, 0xFF }, CP_UTF32BE }, /* UTF-32BE */ |
| 868 | { 0 } /* sentinel */ |
| 869 | }; |
| 870 | |
| 871 | static BOM * |
| 872 | find_BOM(char * buffer) |
| 873 | { |
| 874 | /* |
| 875 | * Look for a BOM in the input and return a pointer to the |
| 876 | * corresponding structure, or NULL if not found. |
| 877 | */ |
| 878 | BOM * result = NULL; |
| 879 | BOM *bom; |
| 880 | |
| 881 | for (bom = BOMs; bom->length; bom++) { |
| 882 | if (strncmp(bom->sequence, buffer, bom->length) == 0) { |
| 883 | result = bom; |
| 884 | break; |
| 885 | } |
| 886 | } |
| 887 | return result; |
| 888 | } |
| 889 | |
| 890 | static char * |
| 891 | find_terminator(char * buffer, int len, BOM *bom) |
| 892 | { |
| 893 | char * result = NULL; |
| 894 | char * end = buffer + len; |
| 895 | char * p; |
| 896 | char c; |
| 897 | int cp; |
| 898 | |
| 899 | for (p = buffer; p < end; p++) { |
| 900 | c = *p; |
| 901 | if (c == '\r') { |
| 902 | result = p; |
| 903 | break; |
| 904 | } |
| 905 | if (c == '\n') { |
| 906 | result = p; |
| 907 | break; |
| 908 | } |
| 909 | } |
| 910 | if (result != NULL) { |
| 911 | cp = bom->code_page; |
| 912 | |
| 913 | /* adjustments to include all bytes of the char */ |
| 914 | /* no adjustment needed for UTF-8 or big endian */ |
| 915 | if (cp == CP_UTF16LE) |
| 916 | ++result; |
| 917 | else if (cp == CP_UTF32LE) |
| 918 | result += 3; |
| 919 | ++result; /* point just past terminator */ |
| 920 | } |
| 921 | return result; |
| 922 | } |
| 923 | |
| 924 | static BOOL |
| 925 | validate_version(wchar_t * p) |
| 926 | { |
| 927 | BOOL result = TRUE; |
| 928 | |
| 929 | if (!isdigit(*p)) /* expect major version */ |
| 930 | result = FALSE; |
| 931 | else if (*++p) { /* more to do */ |
| 932 | if (*p != L'.') /* major/minor separator */ |
| 933 | result = FALSE; |
| 934 | else { |
| 935 | ++p; |
| 936 | if (!isdigit(*p)) /* expect minor version */ |
| 937 | result = FALSE; |
| 938 | else { |
| 939 | ++p; |
| 940 | if (*p) { /* more to do */ |
| 941 | if (*p != L'-') |
| 942 | result = FALSE; |
| 943 | else { |
| 944 | ++p; |
| 945 | if ((*p != '3') && (*++p != '2') && !*++p) |
| 946 | result = FALSE; |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | } |
| 952 | return result; |
| 953 | } |
| 954 | |
| 955 | typedef struct { |
| 956 | unsigned short min; |
| 957 | unsigned short max; |
| 958 | wchar_t version[MAX_VERSION_SIZE]; |
| 959 | } PYC_MAGIC; |
| 960 | |
| 961 | static PYC_MAGIC magic_values[] = { |
| 962 | { 0xc687, 0xc687, L"2.0" }, |
| 963 | { 0xeb2a, 0xeb2a, L"2.1" }, |
| 964 | { 0xed2d, 0xed2d, L"2.2" }, |
| 965 | { 0xf23b, 0xf245, L"2.3" }, |
| 966 | { 0xf259, 0xf26d, L"2.4" }, |
| 967 | { 0xf277, 0xf2b3, L"2.5" }, |
| 968 | { 0xf2c7, 0xf2d1, L"2.6" }, |
| 969 | { 0xf2db, 0xf303, L"2.7" }, |
| 970 | { 0x0bb8, 0x0c3b, L"3.0" }, |
| 971 | { 0x0c45, 0x0c4f, L"3.1" }, |
| 972 | { 0x0c58, 0x0c6c, L"3.2" }, |
| 973 | { 0x0c76, 0x0c76, L"3.3" }, |
| 974 | { 0 } |
| 975 | }; |
| 976 | |
| 977 | static INSTALLED_PYTHON * |
| 978 | find_by_magic(unsigned short magic) |
| 979 | { |
| 980 | INSTALLED_PYTHON * result = NULL; |
| 981 | PYC_MAGIC * mp; |
| 982 | |
| 983 | for (mp = magic_values; mp->min; mp++) { |
| 984 | if ((magic >= mp->min) && (magic <= mp->max)) { |
| 985 | result = locate_python(mp->version); |
| 986 | if (result != NULL) |
| 987 | break; |
| 988 | } |
| 989 | } |
| 990 | return result; |
| 991 | } |
| 992 | |
| 993 | static void |
| 994 | maybe_handle_shebang(wchar_t ** argv, wchar_t * cmdline) |
| 995 | { |
| 996 | /* |
| 997 | * Look for a shebang line in the first argument. If found |
| 998 | * and we spawn a child process, this never returns. If it |
| 999 | * does return then we process the args "normally". |
| 1000 | * |
| 1001 | * argv[0] might be a filename with a shebang. |
| 1002 | */ |
| 1003 | FILE * fp; |
| 1004 | errno_t rc = _wfopen_s(&fp, *argv, L"rb"); |
| 1005 | unsigned char buffer[BUFSIZE]; |
| 1006 | wchar_t shebang_line[BUFSIZE + 1]; |
| 1007 | size_t read; |
| 1008 | char *p; |
| 1009 | char * start; |
| 1010 | char * shebang_alias = (char *) shebang_line; |
| 1011 | BOM* bom; |
| 1012 | int i, j, nchars = 0; |
| 1013 | int header_len; |
| 1014 | BOOL is_virt; |
| 1015 | wchar_t * command; |
| 1016 | wchar_t * suffix; |
| 1017 | INSTALLED_PYTHON * ip; |
| 1018 | |
| 1019 | if (rc == 0) { |
| 1020 | read = fread(buffer, sizeof(char), BUFSIZE, fp); |
| 1021 | debug(L"maybe_handle_shebang: read %d bytes\n", read); |
| 1022 | fclose(fp); |
| 1023 | |
| 1024 | if ((read >= 4) && (buffer[3] == '\n') && (buffer[2] == '\r')) { |
| 1025 | ip = find_by_magic((buffer[1] << 8 | buffer[0]) & 0xFFFF); |
| 1026 | if (ip != NULL) { |
| 1027 | debug(L"script file is compiled against Python %s\n", |
| 1028 | ip->version); |
| 1029 | invoke_child(ip->executable, NULL, cmdline); |
| 1030 | } |
| 1031 | } |
| 1032 | /* Look for BOM */ |
| 1033 | bom = find_BOM(buffer); |
| 1034 | if (bom == NULL) { |
| 1035 | start = buffer; |
| 1036 | debug(L"maybe_handle_shebang: BOM not found, using UTF-8\n"); |
| 1037 | bom = BOMs; /* points to UTF-8 entry - the default */ |
| 1038 | } |
| 1039 | else { |
| 1040 | debug(L"maybe_handle_shebang: BOM found, code page %d\n", |
| 1041 | bom->code_page); |
| 1042 | start = &buffer[bom->length]; |
| 1043 | } |
| 1044 | p = find_terminator(start, BUFSIZE, bom); |
| 1045 | /* |
| 1046 | * If no CR or LF was found in the heading, |
| 1047 | * we assume it's not a shebang file. |
| 1048 | */ |
| 1049 | if (p == NULL) { |
| 1050 | debug(L"maybe_handle_shebang: No line terminator found\n"); |
| 1051 | } |
| 1052 | else { |
| 1053 | /* |
| 1054 | * Found line terminator - parse the shebang. |
| 1055 | * |
| 1056 | * Strictly, we don't need to handle UTF-16 anf UTF-32, |
| 1057 | * since Python itself doesn't. |
| 1058 | * Never mind, one day it might. |
| 1059 | */ |
| 1060 | header_len = (int) (p - start); |
| 1061 | switch(bom->code_page) { |
| 1062 | case CP_UTF8: |
| 1063 | nchars = MultiByteToWideChar(bom->code_page, |
| 1064 | 0, |
| 1065 | start, header_len, shebang_line, |
| 1066 | BUFSIZE); |
| 1067 | break; |
| 1068 | case CP_UTF16BE: |
| 1069 | if (header_len % 2 != 0) { |
| 1070 | debug(L"maybe_handle_shebang: UTF-16BE, but an odd number \ |
| 1071 | of bytes: %d\n", header_len); |
| 1072 | /* nchars = 0; Not needed - initialised to 0. */ |
| 1073 | } |
| 1074 | else { |
| 1075 | for (i = header_len; i > 0; i -= 2) { |
| 1076 | shebang_alias[i - 1] = start[i - 2]; |
| 1077 | shebang_alias[i - 2] = start[i - 1]; |
| 1078 | } |
| 1079 | nchars = header_len / sizeof(wchar_t); |
| 1080 | } |
| 1081 | break; |
| 1082 | case CP_UTF16LE: |
| 1083 | if ((header_len % 2) != 0) { |
| 1084 | debug(L"UTF-16LE, but an odd number of bytes: %d\n", |
| 1085 | header_len); |
| 1086 | /* nchars = 0; Not needed - initialised to 0. */ |
| 1087 | } |
| 1088 | else { |
| 1089 | /* no actual conversion needed. */ |
| 1090 | memcpy(shebang_line, start, header_len); |
| 1091 | nchars = header_len / sizeof(wchar_t); |
| 1092 | } |
| 1093 | break; |
| 1094 | case CP_UTF32BE: |
| 1095 | if (header_len % 4 != 0) { |
| 1096 | debug(L"UTF-32BE, but not divisible by 4: %d\n", |
| 1097 | header_len); |
| 1098 | /* nchars = 0; Not needed - initialised to 0. */ |
| 1099 | } |
| 1100 | else { |
| 1101 | for (i = header_len, j = header_len / 2; i > 0; i -= 4, |
| 1102 | j -= 2) { |
| 1103 | shebang_alias[j - 1] = start[i - 2]; |
| 1104 | shebang_alias[j - 2] = start[i - 1]; |
| 1105 | } |
| 1106 | nchars = header_len / sizeof(wchar_t); |
| 1107 | } |
| 1108 | break; |
| 1109 | case CP_UTF32LE: |
| 1110 | if (header_len % 4 != 0) { |
| 1111 | debug(L"UTF-32LE, but not divisible by 4: %d\n", |
| 1112 | header_len); |
| 1113 | /* nchars = 0; Not needed - initialised to 0. */ |
| 1114 | } |
| 1115 | else { |
| 1116 | for (i = header_len, j = header_len / 2; i > 0; i -= 4, |
| 1117 | j -= 2) { |
| 1118 | shebang_alias[j - 1] = start[i - 3]; |
| 1119 | shebang_alias[j - 2] = start[i - 4]; |
| 1120 | } |
| 1121 | nchars = header_len / sizeof(wchar_t); |
| 1122 | } |
| 1123 | break; |
| 1124 | } |
| 1125 | if (nchars > 0) { |
| 1126 | shebang_line[--nchars] = L'\0'; |
| 1127 | is_virt = parse_shebang(shebang_line, nchars, &command, |
| 1128 | &suffix); |
| 1129 | if (command != NULL) { |
| 1130 | debug(L"parse_shebang: found command: %s\n", command); |
| 1131 | if (!is_virt) { |
| 1132 | invoke_child(command, suffix, cmdline); |
| 1133 | } |
| 1134 | else { |
| 1135 | suffix = wcschr(command, L' '); |
| 1136 | if (suffix != NULL) { |
| 1137 | *suffix++ = L'\0'; |
| 1138 | suffix = skip_whitespace(suffix); |
| 1139 | } |
| 1140 | if (wcsncmp(command, L"python", 6)) |
| 1141 | error(RC_BAD_VIRTUAL_PATH, L"Unknown virtual \ |
| 1142 | path '%s'", command); |
| 1143 | command += 6; /* skip past "python" */ |
| 1144 | if (*command && !validate_version(command)) |
| 1145 | error(RC_BAD_VIRTUAL_PATH, L"Invalid version \ |
| 1146 | specification: '%s'.\nIn the first line of the script, 'python' needs to be \ |
| 1147 | followed by a valid version specifier.\nPlease check the documentation.", |
| 1148 | command); |
| 1149 | /* TODO could call validate_version(command) */ |
| 1150 | ip = locate_python(command); |
| 1151 | if (ip == NULL) { |
| 1152 | error(RC_NO_PYTHON, L"Requested Python version \ |
| 1153 | (%s) is not installed", command); |
| 1154 | } |
| 1155 | else { |
| 1156 | invoke_child(ip->executable, suffix, cmdline); |
| 1157 | } |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
| 1165 | static wchar_t * |
| 1166 | skip_me(wchar_t * cmdline) |
| 1167 | { |
| 1168 | BOOL quoted; |
| 1169 | wchar_t c; |
| 1170 | wchar_t * result = cmdline; |
| 1171 | |
| 1172 | quoted = cmdline[0] == L'\"'; |
| 1173 | if (!quoted) |
| 1174 | c = L' '; |
| 1175 | else { |
| 1176 | c = L'\"'; |
| 1177 | ++result; |
| 1178 | } |
| 1179 | result = wcschr(result, c); |
| 1180 | if (result == NULL) /* when, for example, just exe name on command line */ |
| 1181 | result = L""; |
| 1182 | else { |
| 1183 | ++result; /* skip past space or closing quote */ |
| 1184 | result = skip_whitespace(result); |
| 1185 | } |
| 1186 | return result; |
| 1187 | } |
| 1188 | |
| 1189 | static DWORD version_high = 0; |
| 1190 | static DWORD version_low = 0; |
| 1191 | |
| 1192 | static void |
| 1193 | get_version_info(wchar_t * version_text, size_t size) |
| 1194 | { |
| 1195 | WORD maj, min, rel, bld; |
| 1196 | |
| 1197 | if (!version_high && !version_low) |
| 1198 | wcsncpy_s(version_text, size, L"0.1", _TRUNCATE); /* fallback */ |
| 1199 | else { |
| 1200 | maj = HIWORD(version_high); |
| 1201 | min = LOWORD(version_high); |
| 1202 | rel = HIWORD(version_low); |
| 1203 | bld = LOWORD(version_low); |
| 1204 | _snwprintf_s(version_text, size, _TRUNCATE, L"%d.%d.%d.%d", maj, |
| 1205 | min, rel, bld); |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | static int |
| 1210 | process(int argc, wchar_t ** argv) |
| 1211 | { |
| 1212 | wchar_t * wp; |
| 1213 | wchar_t * command; |
| 1214 | wchar_t * p; |
| 1215 | int rc = 0; |
| 1216 | size_t plen; |
| 1217 | INSTALLED_PYTHON * ip; |
| 1218 | BOOL valid; |
| 1219 | DWORD size, attrs; |
| 1220 | HRESULT hr; |
| 1221 | wchar_t message[MSGSIZE]; |
| 1222 | wchar_t version_text [MAX_PATH]; |
| 1223 | void * version_data; |
| 1224 | VS_FIXEDFILEINFO * file_info; |
| 1225 | UINT block_size; |
| 1226 | |
| 1227 | wp = get_env(L"PYLAUNCH_DEBUG"); |
| 1228 | if ((wp != NULL) && (*wp != L'\0')) |
| 1229 | log_fp = stderr; |
| 1230 | |
| 1231 | #if defined(_M_X64) |
| 1232 | debug(L"launcher build: 64bit\n"); |
| 1233 | #else |
| 1234 | debug(L"launcher build: 32bit\n"); |
| 1235 | #endif |
| 1236 | #if defined(_WINDOWS) |
| 1237 | debug(L"launcher executable: Windows\n"); |
| 1238 | #else |
| 1239 | debug(L"launcher executable: Console\n"); |
| 1240 | #endif |
| 1241 | /* Get the local appdata folder (non-roaming) */ |
| 1242 | hr = SHGetFolderPathW(NULL, CSIDL_LOCAL_APPDATA, |
| 1243 | NULL, 0, appdata_ini_path); |
| 1244 | if (hr != S_OK) { |
| 1245 | debug(L"SHGetFolderPath failed: %X\n", hr); |
| 1246 | appdata_ini_path[0] = L'\0'; |
| 1247 | } |
| 1248 | else { |
| 1249 | plen = wcslen(appdata_ini_path); |
| 1250 | p = &appdata_ini_path[plen]; |
| 1251 | wcsncpy_s(p, MAX_PATH - plen, L"\\py.ini", _TRUNCATE); |
| 1252 | attrs = GetFileAttributesW(appdata_ini_path); |
| 1253 | if (attrs == INVALID_FILE_ATTRIBUTES) { |
| 1254 | debug(L"File '%s' non-existent\n", appdata_ini_path); |
| 1255 | appdata_ini_path[0] = L'\0'; |
| 1256 | } else { |
| 1257 | debug(L"Using local configuration file '%s'\n", appdata_ini_path); |
| 1258 | } |
| 1259 | } |
| 1260 | plen = GetModuleFileNameW(NULL, launcher_ini_path, MAX_PATH); |
| 1261 | size = GetFileVersionInfoSizeW(launcher_ini_path, &size); |
| 1262 | if (size == 0) { |
| 1263 | winerror(GetLastError(), message, MSGSIZE); |
| 1264 | debug(L"GetFileVersionInfoSize failed: %s\n", message); |
| 1265 | } |
| 1266 | else { |
| 1267 | version_data = malloc(size); |
| 1268 | if (version_data) { |
| 1269 | valid = GetFileVersionInfoW(launcher_ini_path, 0, size, |
| 1270 | version_data); |
| 1271 | if (!valid) |
| 1272 | debug(L"GetFileVersionInfo failed: %X\n", GetLastError()); |
| 1273 | else { |
| 1274 | valid = VerQueryValueW(version_data, L"\\", &file_info, |
| 1275 | &block_size); |
| 1276 | if (!valid) |
| 1277 | debug(L"VerQueryValue failed: %X\n", GetLastError()); |
| 1278 | else { |
| 1279 | version_high = file_info->dwFileVersionMS; |
| 1280 | version_low = file_info->dwFileVersionLS; |
| 1281 | } |
| 1282 | } |
| 1283 | free(version_data); |
| 1284 | } |
| 1285 | } |
| 1286 | p = wcsrchr(launcher_ini_path, L'\\'); |
| 1287 | if (p == NULL) { |
| 1288 | debug(L"GetModuleFileNameW returned value has no backslash: %s\n", |
| 1289 | launcher_ini_path); |
| 1290 | launcher_ini_path[0] = L'\0'; |
| 1291 | } |
| 1292 | else { |
| 1293 | wcsncpy_s(p, MAX_PATH - (p - launcher_ini_path), L"\\py.ini", |
| 1294 | _TRUNCATE); |
| 1295 | attrs = GetFileAttributesW(launcher_ini_path); |
| 1296 | if (attrs == INVALID_FILE_ATTRIBUTES) { |
| 1297 | debug(L"File '%s' non-existent\n", launcher_ini_path); |
| 1298 | launcher_ini_path[0] = L'\0'; |
| 1299 | } else { |
| 1300 | debug(L"Using global configuration file '%s'\n", launcher_ini_path); |
| 1301 | } |
| 1302 | } |
| 1303 | |
| 1304 | command = skip_me(GetCommandLineW()); |
| 1305 | debug(L"Called with command line: %s", command); |
| 1306 | if (argc <= 1) { |
| 1307 | valid = FALSE; |
| 1308 | p = NULL; |
| 1309 | } |
| 1310 | else { |
| 1311 | p = argv[1]; |
| 1312 | plen = wcslen(p); |
| 1313 | if (p[0] != L'-') { |
| 1314 | read_commands(); |
| 1315 | maybe_handle_shebang(&argv[1], command); |
| 1316 | } |
| 1317 | /* No file with shebang, or an unrecognised shebang. |
| 1318 | * Is the first arg a special version qualifier? |
| 1319 | */ |
| 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 | } |
| 1330 | if (!valid) { |
| 1331 | ip = locate_python(L""); |
| 1332 | if (ip == NULL) |
| 1333 | error(RC_NO_PYTHON, L"Can't find a default Python."); |
| 1334 | if ((argc == 2) && (!_wcsicmp(p, L"-h") || !_wcsicmp(p, L"--help"))) { |
| 1335 | #if defined(_M_X64) |
| 1336 | BOOL canDo64bit = TRUE; |
| 1337 | #else |
| 1338 | // If we are a 32bit process on a 64bit Windows, first hit the 64bit keys. |
| 1339 | BOOL canDo64bit = FALSE; |
| 1340 | IsWow64Process(GetCurrentProcess(), &canDo64bit); |
| 1341 | #endif |
| 1342 | |
| 1343 | get_version_info(version_text, MAX_PATH); |
| 1344 | fwprintf(stdout, L"\ |
| 1345 | Python Launcher for Windows Version %s\n\n", version_text); |
| 1346 | fwprintf(stdout, L"\ |
| 1347 | usage: %s [ launcher-arguments ] script [ script-arguments ]\n\n", argv[0]); |
| 1348 | fputws(L"\ |
| 1349 | Launcher arguments:\n\n\ |
| 1350 | -2 : Launch the latest Python 2.x version\n\ |
| 1351 | -3 : Launch the latest Python 3.x version\n\ |
| 1352 | -X.Y : Launch the specified Python version\n", stdout); |
| 1353 | if (canDo64bit) { |
| 1354 | fputws(L"\ |
| 1355 | -X.Y-32: Launch the specified 32bit Python version", stdout); |
| 1356 | } |
| 1357 | fputws(L"\n\nThe following help text is from Python:\n\n", stdout); |
| 1358 | fflush(stdout); |
| 1359 | } |
| 1360 | } |
| 1361 | invoke_child(ip->executable, NULL, command); |
| 1362 | return rc; |
| 1363 | } |
| 1364 | |
| 1365 | #if defined(_WINDOWS) |
| 1366 | |
| 1367 | int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, |
| 1368 | LPWSTR lpstrCmd, int nShow) |
| 1369 | { |
| 1370 | return process(__argc, __wargv); |
| 1371 | } |
| 1372 | |
| 1373 | #else |
| 1374 | |
| 1375 | int cdecl wmain(int argc, wchar_t ** argv) |
| 1376 | { |
| 1377 | return process(argc, argv); |
| 1378 | } |
| 1379 | |
Vinay Sajip | 66fef9f | 2013-02-26 16:29:06 +0000 | [diff] [blame] | 1380 | #endif |