1*67c7d1c1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*67c7d1c1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*67c7d1c1SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*67c7d1c1SAndrew Rist * distributed with this work for additional information 6*67c7d1c1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*67c7d1c1SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*67c7d1c1SAndrew Rist * "License"); you may not use this file except in compliance 9*67c7d1c1SAndrew Rist * with the License. You may obtain a copy of the License at 10*67c7d1c1SAndrew Rist * 11*67c7d1c1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*67c7d1c1SAndrew Rist * 13*67c7d1c1SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*67c7d1c1SAndrew Rist * software distributed under the License is distributed on an 15*67c7d1c1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*67c7d1c1SAndrew Rist * KIND, either express or implied. See the License for the 17*67c7d1c1SAndrew Rist * specific language governing permissions and limitations 18*67c7d1c1SAndrew Rist * under the License. 19*67c7d1c1SAndrew Rist * 20*67c7d1c1SAndrew Rist *************************************************************/ 21*67c7d1c1SAndrew Rist 22*67c7d1c1SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir #include <cstddef> 25cdf0e10cSrcweir #include <stdlib.h> 26cdf0e10cSrcweir #include <wchar.h> 27cdf0e10cSrcweir 28cdf0e10cSrcweir #define WIN32_LEAN_AND_MEAN 29cdf0e10cSrcweir #if defined _MSC_VER 30cdf0e10cSrcweir #pragma warning(push, 1) 31cdf0e10cSrcweir #endif 32cdf0e10cSrcweir #include <windows.h> 33cdf0e10cSrcweir #if defined _MSC_VER 34cdf0e10cSrcweir #pragma warning(pop) 35cdf0e10cSrcweir #endif 36cdf0e10cSrcweir 37cdf0e10cSrcweir #include "tools/pathutils.hxx" 38cdf0e10cSrcweir 39cdf0e10cSrcweir #include "pyversion.hxx" 40cdf0e10cSrcweir 41cdf0e10cSrcweir #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1) 42cdf0e10cSrcweir #define MY_STRING(s) (s), MY_LENGTH(s) 43cdf0e10cSrcweir 44cdf0e10cSrcweir wchar_t * encode(wchar_t * buffer, wchar_t const * text) { 45cdf0e10cSrcweir *buffer++ = L'"'; 46cdf0e10cSrcweir std::size_t n = 0; 47cdf0e10cSrcweir for (;;) { 48cdf0e10cSrcweir wchar_t c = *text++; 49cdf0e10cSrcweir if (c == L'\0') { 50cdf0e10cSrcweir break; 51cdf0e10cSrcweir } else if (c == L'"') { 52cdf0e10cSrcweir // Double any preceding backslashes as required by Windows: 53cdf0e10cSrcweir for (std::size_t i = 0; i < n; ++i) { 54cdf0e10cSrcweir *buffer++ = L'\\'; 55cdf0e10cSrcweir } 56cdf0e10cSrcweir *buffer++ = L'\\'; 57cdf0e10cSrcweir *buffer++ = L'"'; 58cdf0e10cSrcweir n = 0; 59cdf0e10cSrcweir } else if (c == L'\\') { 60cdf0e10cSrcweir *buffer++ = L'\\'; 61cdf0e10cSrcweir ++n; 62cdf0e10cSrcweir } else { 63cdf0e10cSrcweir *buffer++ = c; 64cdf0e10cSrcweir n = 0; 65cdf0e10cSrcweir } 66cdf0e10cSrcweir } 67cdf0e10cSrcweir // The command line will continue with a double quote, so double any 68cdf0e10cSrcweir // preceding backslashes as required by Windows: 69cdf0e10cSrcweir for (std::size_t i = 0; i < n; ++i) { 70cdf0e10cSrcweir *buffer++ = L'\\'; 71cdf0e10cSrcweir } 72cdf0e10cSrcweir *buffer++ = L'"'; 73cdf0e10cSrcweir return buffer; 74cdf0e10cSrcweir } 75cdf0e10cSrcweir 76cdf0e10cSrcweir #ifdef __MINGW32__ 77cdf0e10cSrcweir int main(int argc, char ** argv, char **) { 78cdf0e10cSrcweir #else 79cdf0e10cSrcweir int wmain(int argc, wchar_t ** argv, wchar_t **) { 80cdf0e10cSrcweir #endif 81cdf0e10cSrcweir wchar_t path[MAX_PATH]; 82cdf0e10cSrcweir DWORD n = GetModuleFileNameW(NULL, path, MAX_PATH); 83cdf0e10cSrcweir if (n == 0 || n >= MAX_PATH) { 84cdf0e10cSrcweir exit(EXIT_FAILURE); 85cdf0e10cSrcweir } 86cdf0e10cSrcweir wchar_t * pathEnd = tools::filename(path); 87cdf0e10cSrcweir *pathEnd = L'\0'; 88cdf0e10cSrcweir n = GetEnvironmentVariableW(L"UNO_PATH", NULL, 0); 89cdf0e10cSrcweir if (n == 0) { 90cdf0e10cSrcweir if (GetLastError() != ERROR_ENVVAR_NOT_FOUND || 91cdf0e10cSrcweir !SetEnvironmentVariableW(L"UNO_PATH", path)) 92cdf0e10cSrcweir { 93cdf0e10cSrcweir exit(EXIT_FAILURE); 94cdf0e10cSrcweir } 95cdf0e10cSrcweir } 96cdf0e10cSrcweir wchar_t bootstrap[MY_LENGTH(L"vnd.sun.star.pathname:") + MAX_PATH] = 97cdf0e10cSrcweir L"vnd.sun.star.pathname:"; //TODO: overflow 98cdf0e10cSrcweir wchar_t * bootstrapEnd = tools::buildPath( 99cdf0e10cSrcweir bootstrap + MY_LENGTH(L"vnd.sun.star.pathname:"), path, pathEnd, 100cdf0e10cSrcweir MY_STRING(L"fundamental.ini")); 101cdf0e10cSrcweir if (bootstrapEnd == NULL || 102cdf0e10cSrcweir (tools::buildPath(path, path, pathEnd, MY_STRING(L"..\\basis-link")) 103cdf0e10cSrcweir == NULL)) 104cdf0e10cSrcweir { 105cdf0e10cSrcweir exit(EXIT_FAILURE); 106cdf0e10cSrcweir } 107cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 108cdf0e10cSrcweir wchar_t path1[MAX_PATH]; 109cdf0e10cSrcweir wchar_t * path1End = tools::buildPath( 110cdf0e10cSrcweir path1, path, pathEnd, MY_STRING(L"\\program")); 111cdf0e10cSrcweir if (path1End == NULL) { 112cdf0e10cSrcweir exit(EXIT_FAILURE); 113cdf0e10cSrcweir } 114cdf0e10cSrcweir wchar_t pythonpath2[MAX_PATH]; 115cdf0e10cSrcweir wchar_t * pythonpath2End = tools::buildPath( 116cdf0e10cSrcweir pythonpath2, path, pathEnd, 117cdf0e10cSrcweir MY_STRING(L"\\program\\python-core-" MY_PYVERSION L"\\lib")); 118cdf0e10cSrcweir if (pythonpath2End == NULL) { 119cdf0e10cSrcweir exit(EXIT_FAILURE); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir wchar_t pythonpath3[MAX_PATH]; 122cdf0e10cSrcweir wchar_t * pythonpath3End = tools::buildPath( 123cdf0e10cSrcweir pythonpath3, path, pathEnd, 124cdf0e10cSrcweir MY_STRING( 125cdf0e10cSrcweir L"\\program\\python-core-" MY_PYVERSION L"\\lib\\site-packages")); 126cdf0e10cSrcweir if (pythonpath3End == NULL) { 127cdf0e10cSrcweir exit(EXIT_FAILURE); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir #ifdef __MINGW32__ 130cdf0e10cSrcweir wchar_t pythonpath4[MAX_PATH]; 131cdf0e10cSrcweir wchar_t * pythonpath4End = tools::buildPath( 132cdf0e10cSrcweir pythonpath4, path, pathEnd, 133cdf0e10cSrcweir MY_STRING(L"\\program\\python-core-" MY_PYVERSION L"\\lib\\lib-dynload")); 134cdf0e10cSrcweir if (pythonpath4End == NULL) { 135cdf0e10cSrcweir exit(EXIT_FAILURE); 136cdf0e10cSrcweir } 137cdf0e10cSrcweir wchar_t pythonpath5[MAX_PATH]; 138cdf0e10cSrcweir wchar_t * pythonpath5End = tools::buildPath( 139cdf0e10cSrcweir pythonpath5, path, pathEnd, 140cdf0e10cSrcweir MY_STRING(L"\\program\\python-core-" MY_PYVERSION L"\\lib\\lib-dynload")); 141cdf0e10cSrcweir if (pythonpath5End == NULL) { 142cdf0e10cSrcweir exit(EXIT_FAILURE); 143cdf0e10cSrcweir } 144cdf0e10cSrcweir #endif 145cdf0e10cSrcweir wchar_t pythonhome[MAX_PATH]; 146cdf0e10cSrcweir wchar_t * pythonhomeEnd = tools::buildPath( 147cdf0e10cSrcweir pythonhome, path, pathEnd, 148cdf0e10cSrcweir MY_STRING(L"\\program\\python-core-" MY_PYVERSION)); 149cdf0e10cSrcweir if (pythonhomeEnd == NULL) { 150cdf0e10cSrcweir exit(EXIT_FAILURE); 151cdf0e10cSrcweir } 152cdf0e10cSrcweir wchar_t pythonexe[MAX_PATH]; 153cdf0e10cSrcweir wchar_t * pythonexeEnd = tools::buildPath( 154cdf0e10cSrcweir pythonexe, path, pathEnd, 155cdf0e10cSrcweir #ifdef __MINGW32__ 156cdf0e10cSrcweir MY_STRING( 157cdf0e10cSrcweir L"\\program\\python-core-" MY_PYVERSION L"\\bin\\python.bin")); 158cdf0e10cSrcweir #else 159cdf0e10cSrcweir MY_STRING( 160cdf0e10cSrcweir L"\\program\\python-core-" MY_PYVERSION L"\\bin\\python.exe")); 161cdf0e10cSrcweir #endif 162cdf0e10cSrcweir if (pythonexeEnd == NULL) { 163cdf0e10cSrcweir exit(EXIT_FAILURE); 164cdf0e10cSrcweir } 165cdf0e10cSrcweir if (tools::buildPath(path, path, pathEnd, MY_STRING(L"\\ure-link")) == NULL) 166cdf0e10cSrcweir { 167cdf0e10cSrcweir exit(EXIT_FAILURE); 168cdf0e10cSrcweir } 169cdf0e10cSrcweir pathEnd = tools::resolveLink(path); 170cdf0e10cSrcweir if (pathEnd == NULL) { 171cdf0e10cSrcweir exit(EXIT_FAILURE); 172cdf0e10cSrcweir } 173cdf0e10cSrcweir pathEnd = tools::buildPath(path, path, pathEnd, MY_STRING(L"\\bin")); 174cdf0e10cSrcweir if (pathEnd == NULL) { 175cdf0e10cSrcweir exit(EXIT_FAILURE); 176cdf0e10cSrcweir } 177cdf0e10cSrcweir std::size_t clSize = MY_LENGTH(L"\"") + 4 * (pythonexeEnd - pythonexe) + 178cdf0e10cSrcweir MY_LENGTH(L"\"\0"); //TODO: overflow 179cdf0e10cSrcweir // 4 * len: each char preceded by backslash, each trailing backslash 180cdf0e10cSrcweir // doubled 181cdf0e10cSrcweir for (int i = 1; i < argc; ++i) { 182cdf0e10cSrcweir #ifdef __MINGW32__ 183cdf0e10cSrcweir clSize += MY_LENGTH(L" \"") + 4 * strlen(argv[i]) + 184cdf0e10cSrcweir #else 185cdf0e10cSrcweir clSize += MY_LENGTH(L" \"") + 4 * wcslen(argv[i]) + 186cdf0e10cSrcweir #endif 187cdf0e10cSrcweir MY_LENGTH(L"\""); //TODO: overflow 188cdf0e10cSrcweir } 189cdf0e10cSrcweir wchar_t * cl = new wchar_t[clSize]; 190cdf0e10cSrcweir if (cl == NULL) { 191cdf0e10cSrcweir exit(EXIT_FAILURE); 192cdf0e10cSrcweir } 193cdf0e10cSrcweir wchar_t * cp = encode(cl, pythonhome); 194cdf0e10cSrcweir for (int i = 1; i < argc; ++i) { 195cdf0e10cSrcweir *cp++ = L' '; 196cdf0e10cSrcweir #ifdef __MINGW32__ 197cdf0e10cSrcweir int nNeededWStrBuffSize = MultiByteToWideChar(CP_ACP, 0, argv[i], -1, NULL, 0); 198cdf0e10cSrcweir WCHAR *buff = new WCHAR[nNeededWStrBuffSize+1]; 199cdf0e10cSrcweir MultiByteToWideChar(CP_ACP, 0, argv[i], -1, buff, nNeededWStrBuffSize); 200cdf0e10cSrcweir buff[nNeededWStrBuffSize] = 0; 201cdf0e10cSrcweir cp = encode(cp, buff); 202cdf0e10cSrcweir delete [] buff; 203cdf0e10cSrcweir #else 204cdf0e10cSrcweir cp = encode(cp, argv[i]); 205cdf0e10cSrcweir #endif 206cdf0e10cSrcweir } 207cdf0e10cSrcweir *cp = L'\0'; 208cdf0e10cSrcweir n = GetEnvironmentVariableW(L"PATH", NULL, 0); 209cdf0e10cSrcweir wchar_t * orig; 210cdf0e10cSrcweir if (n == 0) { 211cdf0e10cSrcweir if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { 212cdf0e10cSrcweir exit(EXIT_FAILURE); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir orig = L""; 215cdf0e10cSrcweir } else { 216cdf0e10cSrcweir orig = new wchar_t[n]; 217cdf0e10cSrcweir if (orig == NULL || 218cdf0e10cSrcweir GetEnvironmentVariableW(L"PATH", orig, n) != n - 1) 219cdf0e10cSrcweir { 220cdf0e10cSrcweir exit(EXIT_FAILURE); 221cdf0e10cSrcweir } 222cdf0e10cSrcweir } 223cdf0e10cSrcweir wchar_t * value = new wchar_t[ 224cdf0e10cSrcweir (pathEnd - path) + MY_LENGTH(L";") + (path1End - path1) + 225cdf0e10cSrcweir (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow 226cdf0e10cSrcweir wsprintfW(value, L"%s;%s%s%s", path, path1, n == 0 ? L"" : L";", orig); 227cdf0e10cSrcweir if (!SetEnvironmentVariableW(L"PATH", value)) { 228cdf0e10cSrcweir exit(EXIT_FAILURE); 229cdf0e10cSrcweir } 230cdf0e10cSrcweir if (n != 0) { 231cdf0e10cSrcweir delete [] orig; 232cdf0e10cSrcweir } 233cdf0e10cSrcweir delete [] value; 234cdf0e10cSrcweir n = GetEnvironmentVariableW(L"PYTHONPATH", NULL, 0); 235cdf0e10cSrcweir if (n == 0) { 236cdf0e10cSrcweir if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) { 237cdf0e10cSrcweir exit(EXIT_FAILURE); 238cdf0e10cSrcweir } 239cdf0e10cSrcweir orig = L""; 240cdf0e10cSrcweir } else { 241cdf0e10cSrcweir orig = new wchar_t[n]; 242cdf0e10cSrcweir if (orig == NULL || 243cdf0e10cSrcweir GetEnvironmentVariableW(L"PYTHONPATH", orig, n) != n - 1) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir exit(EXIT_FAILURE); 246cdf0e10cSrcweir } 247cdf0e10cSrcweir } 248cdf0e10cSrcweir #ifdef __MINGW32__ 249cdf0e10cSrcweir value = new wchar_t[ 250cdf0e10cSrcweir (path1End - path1) + MY_LENGTH(L";") + (pythonpath2End - pythonpath2) + 251cdf0e10cSrcweir MY_LENGTH(L";") + (pythonpath4End - pythonpath4) + 252cdf0e10cSrcweir MY_LENGTH(L";") + (pythonpath5End - pythonpath5) + 253cdf0e10cSrcweir MY_LENGTH(L";") + (pythonpath3End - pythonpath3) + 254cdf0e10cSrcweir (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow 255cdf0e10cSrcweir wsprintfW( 256cdf0e10cSrcweir value, L"%s;%s;%s;%s;%s%s%s", path1, pythonpath2, pythonpath4, 257cdf0e10cSrcweir pythonpath5, pythonpath3, 258cdf0e10cSrcweir n == 0 ? L"" : L";", orig); 259cdf0e10cSrcweir #else 260cdf0e10cSrcweir value = new wchar_t[ 261cdf0e10cSrcweir (path1End - path1) + MY_LENGTH(L";") + (pythonpath2End - pythonpath2) + 262cdf0e10cSrcweir MY_LENGTH(L";") + (pythonpath3End - pythonpath3) + 263cdf0e10cSrcweir (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow 264cdf0e10cSrcweir wsprintfW( 265cdf0e10cSrcweir value, L"%s;%s;%s%s%s", path1, pythonpath2, pythonpath3, 266cdf0e10cSrcweir n == 0 ? L"" : L";", orig); 267cdf0e10cSrcweir #endif 268cdf0e10cSrcweir if (!SetEnvironmentVariableW(L"PYTHONPATH", value)) { 269cdf0e10cSrcweir exit(EXIT_FAILURE); 270cdf0e10cSrcweir } 271cdf0e10cSrcweir if (n != 0) { 272cdf0e10cSrcweir delete [] orig; 273cdf0e10cSrcweir } 274cdf0e10cSrcweir delete [] value; 275cdf0e10cSrcweir if (!SetEnvironmentVariableW(L"PYTHONHOME", pythonhome)) { 276cdf0e10cSrcweir exit(EXIT_FAILURE); 277cdf0e10cSrcweir } 278cdf0e10cSrcweir n = GetEnvironmentVariableW(L"URE_BOOTSTRAP", NULL, 0); 279cdf0e10cSrcweir if (n == 0) { 280cdf0e10cSrcweir if (GetLastError() != ERROR_ENVVAR_NOT_FOUND || 281cdf0e10cSrcweir !SetEnvironmentVariableW(L"URE_BOOTSTRAP", bootstrap)) 282cdf0e10cSrcweir { 283cdf0e10cSrcweir exit(EXIT_FAILURE); 284cdf0e10cSrcweir } 285cdf0e10cSrcweir } 286cdf0e10cSrcweir STARTUPINFOW startinfo; 287cdf0e10cSrcweir ZeroMemory(&startinfo, sizeof (STARTUPINFOW)); 288cdf0e10cSrcweir startinfo.cb = sizeof (STARTUPINFOW); 289cdf0e10cSrcweir PROCESS_INFORMATION procinfo; 290cdf0e10cSrcweir if (!CreateProcessW( 291cdf0e10cSrcweir pythonexe, cl, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, NULL, 292cdf0e10cSrcweir NULL, &startinfo, &procinfo)) { 293cdf0e10cSrcweir exit(EXIT_FAILURE); 294cdf0e10cSrcweir } 295cdf0e10cSrcweir WaitForSingleObject(procinfo.hProcess,INFINITE); 296cdf0e10cSrcweir DWORD exitStatus; 297cdf0e10cSrcweir GetExitCodeProcess(procinfo.hProcess,&exitStatus); 298cdf0e10cSrcweir exit(exitStatus); 299cdf0e10cSrcweir } 300