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