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