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