xref: /aoo41x/main/pyuno/zipcore/python.cxx (revision 9813d809)
167c7d1c1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
367c7d1c1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
467c7d1c1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
567c7d1c1SAndrew Rist  * distributed with this work for additional information
667c7d1c1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
767c7d1c1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
867c7d1c1SAndrew Rist  * "License"); you may not use this file except in compliance
967c7d1c1SAndrew Rist  * with the License.  You may obtain a copy of the License at
1067c7d1c1SAndrew Rist  *
1167c7d1c1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
1267c7d1c1SAndrew Rist  *
1367c7d1c1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
1467c7d1c1SAndrew Rist  * software distributed under the License is distributed on an
1567c7d1c1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
1667c7d1c1SAndrew Rist  * KIND, either express or implied.  See the License for the
1767c7d1c1SAndrew Rist  * specific language governing permissions and limitations
1867c7d1c1SAndrew Rist  * under the License.
1967c7d1c1SAndrew Rist  *
2067c7d1c1SAndrew Rist  *************************************************************/
2167c7d1c1SAndrew Rist 
2267c7d1c1SAndrew 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 
encode(wchar_t * buffer,wchar_t const * text)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__
main(int argc,char ** argv,char **)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     }
96*9813d809SJürgen Schmidt 
97cdf0e10cSrcweir     wchar_t bootstrap[MY_LENGTH(L"vnd.sun.star.pathname:") + MAX_PATH] =
98cdf0e10cSrcweir         L"vnd.sun.star.pathname:"; //TODO: overflow
99cdf0e10cSrcweir     wchar_t * bootstrapEnd = tools::buildPath(
100cdf0e10cSrcweir         bootstrap + MY_LENGTH(L"vnd.sun.star.pathname:"), path, pathEnd,
101cdf0e10cSrcweir         MY_STRING(L"fundamental.ini"));
102*9813d809SJürgen Schmidt     if (bootstrapEnd == NULL)
103cdf0e10cSrcweir     {
104cdf0e10cSrcweir         exit(EXIT_FAILURE);
105cdf0e10cSrcweir     }
106*9813d809SJürgen Schmidt 
107cdf0e10cSrcweir     wchar_t pythonpath2[MAX_PATH];
108cdf0e10cSrcweir     wchar_t * pythonpath2End = tools::buildPath(
109cdf0e10cSrcweir         pythonpath2, path, pathEnd,
110*9813d809SJürgen Schmidt         MY_STRING(L"python-core-" MY_PYVERSION L"\\lib"));
111cdf0e10cSrcweir     if (pythonpath2End == NULL) {
112cdf0e10cSrcweir         exit(EXIT_FAILURE);
113cdf0e10cSrcweir     }
114*9813d809SJürgen Schmidt 
115cdf0e10cSrcweir     wchar_t pythonpath3[MAX_PATH];
116cdf0e10cSrcweir     wchar_t * pythonpath3End = tools::buildPath(
117cdf0e10cSrcweir         pythonpath3, path, pathEnd,
118cdf0e10cSrcweir         MY_STRING(
119*9813d809SJürgen Schmidt             L"python-core-" MY_PYVERSION L"\\lib\\site-packages"));
120cdf0e10cSrcweir     if (pythonpath3End == NULL) {
121cdf0e10cSrcweir         exit(EXIT_FAILURE);
122cdf0e10cSrcweir     }
123*9813d809SJürgen Schmidt 
124cdf0e10cSrcweir #ifdef __MINGW32__
125cdf0e10cSrcweir     wchar_t pythonpath4[MAX_PATH];
126cdf0e10cSrcweir     wchar_t * pythonpath4End = tools::buildPath(
127cdf0e10cSrcweir         pythonpath4, path, pathEnd,
128*9813d809SJürgen Schmidt         MY_STRING(L"python-core-" MY_PYVERSION L"\\lib\\lib-dynload"));
129cdf0e10cSrcweir     if (pythonpath4End == NULL) {
130cdf0e10cSrcweir         exit(EXIT_FAILURE);
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir     wchar_t pythonpath5[MAX_PATH];
133cdf0e10cSrcweir     wchar_t * pythonpath5End = tools::buildPath(
134cdf0e10cSrcweir         pythonpath5, path, pathEnd,
135*9813d809SJürgen Schmidt         MY_STRING(L"python-core-" MY_PYVERSION L"\\lib\\lib-dynload"));
136cdf0e10cSrcweir     if (pythonpath5End == NULL) {
137cdf0e10cSrcweir         exit(EXIT_FAILURE);
138cdf0e10cSrcweir     }
139cdf0e10cSrcweir #endif
140cdf0e10cSrcweir     wchar_t pythonhome[MAX_PATH];
141cdf0e10cSrcweir     wchar_t * pythonhomeEnd = tools::buildPath(
142cdf0e10cSrcweir         pythonhome, path, pathEnd,
143*9813d809SJürgen Schmidt         MY_STRING(L"python-core-" MY_PYVERSION));
144cdf0e10cSrcweir     if (pythonhomeEnd == NULL) {
145cdf0e10cSrcweir         exit(EXIT_FAILURE);
146cdf0e10cSrcweir     }
147cdf0e10cSrcweir     wchar_t pythonexe[MAX_PATH];
148cdf0e10cSrcweir     wchar_t * pythonexeEnd = tools::buildPath(
149cdf0e10cSrcweir         pythonexe, path, pathEnd,
150cdf0e10cSrcweir #ifdef __MINGW32__
151cdf0e10cSrcweir         MY_STRING(
152*9813d809SJürgen Schmidt             L"python-core-" MY_PYVERSION L"\\bin\\python.bin"));
153cdf0e10cSrcweir #else
154cdf0e10cSrcweir         MY_STRING(
155*9813d809SJürgen Schmidt             L"python-core-" MY_PYVERSION L"\\bin\\python.exe"));
156cdf0e10cSrcweir #endif
157cdf0e10cSrcweir     if (pythonexeEnd == NULL) {
158cdf0e10cSrcweir         exit(EXIT_FAILURE);
159cdf0e10cSrcweir     }
160*9813d809SJürgen Schmidt 
161cdf0e10cSrcweir     std::size_t clSize = MY_LENGTH(L"\"") + 4 * (pythonexeEnd - pythonexe) +
162cdf0e10cSrcweir         MY_LENGTH(L"\"\0"); //TODO: overflow
163cdf0e10cSrcweir         // 4 * len: each char preceded by backslash, each trailing backslash
164cdf0e10cSrcweir         // doubled
165cdf0e10cSrcweir     for (int i = 1; i < argc; ++i) {
166cdf0e10cSrcweir #ifdef __MINGW32__
167cdf0e10cSrcweir         clSize += MY_LENGTH(L" \"") + 4 * strlen(argv[i]) +
168cdf0e10cSrcweir #else
169cdf0e10cSrcweir         clSize += MY_LENGTH(L" \"") + 4 * wcslen(argv[i]) +
170cdf0e10cSrcweir #endif
171cdf0e10cSrcweir             MY_LENGTH(L"\""); //TODO: overflow
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir     wchar_t * cl = new wchar_t[clSize];
174cdf0e10cSrcweir     if (cl == NULL) {
175cdf0e10cSrcweir         exit(EXIT_FAILURE);
176cdf0e10cSrcweir     }
177cdf0e10cSrcweir     wchar_t * cp = encode(cl, pythonhome);
178cdf0e10cSrcweir     for (int i = 1; i < argc; ++i) {
179cdf0e10cSrcweir         *cp++ = L' ';
180cdf0e10cSrcweir #ifdef __MINGW32__
181cdf0e10cSrcweir         int nNeededWStrBuffSize = MultiByteToWideChar(CP_ACP, 0, argv[i], -1, NULL, 0);
182cdf0e10cSrcweir         WCHAR *buff = new WCHAR[nNeededWStrBuffSize+1];
183cdf0e10cSrcweir         MultiByteToWideChar(CP_ACP, 0, argv[i], -1, buff, nNeededWStrBuffSize);
184cdf0e10cSrcweir         buff[nNeededWStrBuffSize] = 0;
185cdf0e10cSrcweir         cp = encode(cp, buff);
186cdf0e10cSrcweir         delete [] buff;
187cdf0e10cSrcweir #else
188cdf0e10cSrcweir         cp = encode(cp, argv[i]);
189cdf0e10cSrcweir #endif
190cdf0e10cSrcweir     }
191cdf0e10cSrcweir     *cp = L'\0';
192cdf0e10cSrcweir     n = GetEnvironmentVariableW(L"PATH", NULL, 0);
193cdf0e10cSrcweir     wchar_t * orig;
194cdf0e10cSrcweir     if (n == 0) {
195cdf0e10cSrcweir         if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
196cdf0e10cSrcweir             exit(EXIT_FAILURE);
197cdf0e10cSrcweir         }
198cdf0e10cSrcweir         orig = L"";
199cdf0e10cSrcweir     } else {
200cdf0e10cSrcweir         orig = new wchar_t[n];
201cdf0e10cSrcweir         if (orig == NULL ||
202cdf0e10cSrcweir             GetEnvironmentVariableW(L"PATH", orig, n) != n - 1)
203cdf0e10cSrcweir         {
204cdf0e10cSrcweir             exit(EXIT_FAILURE);
205cdf0e10cSrcweir         }
206cdf0e10cSrcweir     }
207cdf0e10cSrcweir     wchar_t * value = new wchar_t[
208*9813d809SJürgen Schmidt        (pathEnd - path) + MY_LENGTH(L";") +
209*9813d809SJürgen Schmidt        (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow
210*9813d809SJürgen Schmidt 
211*9813d809SJürgen Schmidt     wsprintfW(value, L"%s%s%s", path, n == 0 ? L"" : L";", orig);
212*9813d809SJürgen Schmidt 
213cdf0e10cSrcweir     if (!SetEnvironmentVariableW(L"PATH", value)) {
214cdf0e10cSrcweir         exit(EXIT_FAILURE);
215cdf0e10cSrcweir     }
216cdf0e10cSrcweir     if (n != 0) {
217cdf0e10cSrcweir         delete [] orig;
218cdf0e10cSrcweir     }
219cdf0e10cSrcweir     delete [] value;
220cdf0e10cSrcweir     n = GetEnvironmentVariableW(L"PYTHONPATH", NULL, 0);
221cdf0e10cSrcweir     if (n == 0) {
222cdf0e10cSrcweir         if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
223cdf0e10cSrcweir             exit(EXIT_FAILURE);
224cdf0e10cSrcweir         }
225cdf0e10cSrcweir         orig = L"";
226cdf0e10cSrcweir     } else {
227cdf0e10cSrcweir         orig = new wchar_t[n];
228cdf0e10cSrcweir         if (orig == NULL ||
229cdf0e10cSrcweir             GetEnvironmentVariableW(L"PYTHONPATH", orig, n) != n - 1)
230cdf0e10cSrcweir         {
231cdf0e10cSrcweir             exit(EXIT_FAILURE);
232cdf0e10cSrcweir         }
233cdf0e10cSrcweir     }
234cdf0e10cSrcweir #ifdef __MINGW32__
235cdf0e10cSrcweir     value = new wchar_t[
236cdf0e10cSrcweir         (path1End - path1) + MY_LENGTH(L";") + (pythonpath2End - pythonpath2) +
237cdf0e10cSrcweir         MY_LENGTH(L";") + (pythonpath4End - pythonpath4) +
238cdf0e10cSrcweir         MY_LENGTH(L";") + (pythonpath5End - pythonpath5) +
239cdf0e10cSrcweir         MY_LENGTH(L";") + (pythonpath3End - pythonpath3) +
240cdf0e10cSrcweir         (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow
241cdf0e10cSrcweir     wsprintfW(
242cdf0e10cSrcweir         value, L"%s;%s;%s;%s;%s%s%s", path1, pythonpath2, pythonpath4,
243cdf0e10cSrcweir         pythonpath5, pythonpath3,
244cdf0e10cSrcweir         n == 0 ? L"" : L";", orig);
245cdf0e10cSrcweir #else
246cdf0e10cSrcweir     value = new wchar_t[
247*9813d809SJürgen Schmidt         (pythonpath2End - pythonpath2) +
248cdf0e10cSrcweir         MY_LENGTH(L";") + (pythonpath3End - pythonpath3) +
249cdf0e10cSrcweir         (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1]; //TODO: overflow
250cdf0e10cSrcweir     wsprintfW(
251*9813d809SJürgen Schmidt         value, L"%s;%s%s%s", pythonpath2, pythonpath3,
252cdf0e10cSrcweir         n == 0 ? L"" : L";", orig);
253cdf0e10cSrcweir #endif
254cdf0e10cSrcweir     if (!SetEnvironmentVariableW(L"PYTHONPATH", value)) {
255cdf0e10cSrcweir         exit(EXIT_FAILURE);
256cdf0e10cSrcweir     }
257cdf0e10cSrcweir     if (n != 0) {
258cdf0e10cSrcweir         delete [] orig;
259cdf0e10cSrcweir     }
260cdf0e10cSrcweir     delete [] value;
261cdf0e10cSrcweir     if (!SetEnvironmentVariableW(L"PYTHONHOME", pythonhome)) {
262cdf0e10cSrcweir         exit(EXIT_FAILURE);
263cdf0e10cSrcweir     }
264cdf0e10cSrcweir     n = GetEnvironmentVariableW(L"URE_BOOTSTRAP", NULL, 0);
265cdf0e10cSrcweir     if (n == 0) {
266cdf0e10cSrcweir         if (GetLastError() != ERROR_ENVVAR_NOT_FOUND ||
267cdf0e10cSrcweir             !SetEnvironmentVariableW(L"URE_BOOTSTRAP", bootstrap))
268cdf0e10cSrcweir         {
269cdf0e10cSrcweir             exit(EXIT_FAILURE);
270cdf0e10cSrcweir         }
271cdf0e10cSrcweir     }
272cdf0e10cSrcweir     STARTUPINFOW startinfo;
273cdf0e10cSrcweir     ZeroMemory(&startinfo, sizeof (STARTUPINFOW));
274cdf0e10cSrcweir     startinfo.cb = sizeof (STARTUPINFOW);
275cdf0e10cSrcweir     PROCESS_INFORMATION procinfo;
276cdf0e10cSrcweir     if (!CreateProcessW(
277cdf0e10cSrcweir             pythonexe, cl, NULL, NULL, FALSE, CREATE_UNICODE_ENVIRONMENT, NULL,
278cdf0e10cSrcweir             NULL, &startinfo, &procinfo)) {
279cdf0e10cSrcweir         exit(EXIT_FAILURE);
280cdf0e10cSrcweir     }
281cdf0e10cSrcweir     WaitForSingleObject(procinfo.hProcess,INFINITE);
282cdf0e10cSrcweir     DWORD exitStatus;
283cdf0e10cSrcweir     GetExitCodeProcess(procinfo.hProcess,&exitStatus);
284cdf0e10cSrcweir     exit(exitStatus);
285cdf0e10cSrcweir }
286