SvtLanguageTable::AddLanguageTag() unneeded return can be void
[LibreOffice.git] / pyuno / zipcore / python.cxx
blob7580e32789e376b4fe0852be61c12f5013ad7d60
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <config_python.h>
22 #include <cstddef>
23 #include <stdlib.h>
24 #include <wchar.h>
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
29 #include <tools/pathutils.hxx>
31 #define MY_LENGTH(s) (sizeof (s) / sizeof *(s) - 1)
32 #define MY_STRING(s) (s), MY_LENGTH(s)
34 static wchar_t * encode(wchar_t * buffer, wchar_t const * text) {
35 *buffer++ = L'"';
36 std::size_t n = 0;
37 for (;;) {
38 wchar_t c = *text++;
39 if (c == L'\0') {
40 break;
41 } else if (c == L'"') {
42 // Double any preceding backslashes as required by Windows:
43 for (std::size_t i = 0; i < n; ++i) {
44 *buffer++ = L'\\';
46 *buffer++ = L'\\';
47 *buffer++ = L'"';
48 n = 0;
49 } else if (c == L'\\') {
50 *buffer++ = L'\\';
51 ++n;
52 } else {
53 *buffer++ = c;
54 n = 0;
57 // The command line will continue with a double quote, so double any
58 // preceding backslashes as required by Windows:
59 for (std::size_t i = 0; i < n; ++i) {
60 *buffer++ = L'\\';
62 *buffer++ = L'"';
63 return buffer;
66 int wmain(int argc, wchar_t ** argv, wchar_t **) {
67 wchar_t path[MAX_PATH];
68 DWORD n = GetModuleFileNameW(nullptr, path, MAX_PATH);
69 if (n == 0 || n >= MAX_PATH) {
70 exit(EXIT_FAILURE);
72 wchar_t * pathEnd = tools::filename(path);
73 *pathEnd = L'\0';
74 n = GetEnvironmentVariableW(L"UNO_PATH", nullptr, 0);
75 if (n == 0) {
76 if (GetLastError() != ERROR_ENVVAR_NOT_FOUND ||
77 !SetEnvironmentVariableW(L"UNO_PATH", path))
79 exit(EXIT_FAILURE);
82 wchar_t bootstrap[MY_LENGTH(L"vnd.sun.star.pathname:") + MAX_PATH] =
83 L"vnd.sun.star.pathname:"; //TODO: overflow
84 wchar_t * bootstrapEnd = tools::buildPath(
85 bootstrap + MY_LENGTH(L"vnd.sun.star.pathname:"), path, pathEnd,
86 MY_STRING(L"fundamental.ini"));
87 if (bootstrapEnd == nullptr) {
88 exit(EXIT_FAILURE);
90 wchar_t pythonpath2[MAX_PATH];
91 wchar_t * pythonpath2End = tools::buildPath(
92 pythonpath2, path, pathEnd,
93 MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\lib"));
94 if (pythonpath2End == nullptr) {
95 exit(EXIT_FAILURE);
97 wchar_t pythonpath3[MAX_PATH];
98 wchar_t * pythonpath3End = tools::buildPath(
99 pythonpath3, path, pathEnd,
100 MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\lib\\site-packages"));
101 if (pythonpath3End == nullptr) {
102 exit(EXIT_FAILURE);
104 wchar_t pythonhome[MAX_PATH];
105 wchar_t * pythonhomeEnd = tools::buildPath(
106 pythonhome, path, pathEnd, MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING));
107 if (pythonhomeEnd == nullptr) {
108 exit(EXIT_FAILURE);
110 wchar_t pythonexe[MAX_PATH];
111 wchar_t * pythonexeEnd = tools::buildPath(
112 pythonexe, path, pathEnd,
113 MY_STRING(L"\\python-core-" PYTHON_VERSION_STRING L"\\bin\\python.exe"));
114 if (pythonexeEnd == nullptr) {
115 exit(EXIT_FAILURE);
117 std::size_t clSize = MY_LENGTH(L"\"") + 4 * (pythonexeEnd - pythonexe) +
118 MY_LENGTH(L"\"\0"); //TODO: overflow
119 // 4 * len: each char preceded by backslash, each trailing backslash
120 // doubled
121 for (int i = 1; i < argc; ++i) {
122 clSize += MY_LENGTH(L" \"") + 4 * wcslen(argv[i]) + MY_LENGTH(L"\"");
123 //TODO: overflow
125 wchar_t * cl = new wchar_t[clSize];
126 wchar_t * cp = encode(cl, pythonhome);
127 for (int i = 1; i < argc; ++i) {
128 *cp++ = L' ';
129 cp = encode(cp, argv[i]);
131 *cp = L'\0';
132 n = GetEnvironmentVariableW(L"PATH", nullptr, 0);
133 wchar_t * orig;
134 if (n == 0) {
135 if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
136 exit(EXIT_FAILURE);
138 orig = const_cast<wchar_t *>(L"");
139 } else {
140 orig = new wchar_t[n];
141 if (GetEnvironmentVariableW(L"PATH", orig, n) != n - 1)
143 exit(EXIT_FAILURE);
146 std::size_t len = (pathEnd - path) + (n == 0 ? 0 : MY_LENGTH(L";") +
147 (n - 1)) + 1;
148 //TODO: overflow
149 wchar_t * value = new wchar_t[len];
150 _snwprintf(
151 value, len, L"%s%s%s", path, n == 0 ? L"" : L";", orig);
152 if (!SetEnvironmentVariableW(L"PATH", value)) {
153 exit(EXIT_FAILURE);
155 if (n != 0) {
156 delete [] orig;
158 delete [] value;
159 n = GetEnvironmentVariableW(L"PYTHONPATH", nullptr, 0);
160 if (n == 0) {
161 if (GetLastError() != ERROR_ENVVAR_NOT_FOUND) {
162 exit(EXIT_FAILURE);
164 orig = const_cast<wchar_t *>(L"");
165 } else {
166 orig = new wchar_t[n];
167 if (GetEnvironmentVariableW(L"PYTHONPATH", orig, n) != n - 1)
169 exit(EXIT_FAILURE);
172 len = (pathEnd - path) + MY_LENGTH(L";") + (pythonpath2End - pythonpath2) +
173 MY_LENGTH(L";") + (pythonpath3End - pythonpath3) +
174 (n == 0 ? 0 : MY_LENGTH(L";") + (n - 1)) + 1; //TODO: overflow
175 value = new wchar_t[len];
176 _snwprintf(
177 value, len, L"%s;%s;%s%s%s", path, pythonpath2, pythonpath3,
178 n == 0 ? L"" : L";", orig);
179 if (!SetEnvironmentVariableW(L"PYTHONPATH", value)) {
180 exit(EXIT_FAILURE);
182 if (n != 0) {
183 delete [] orig;
185 delete [] value;
186 if (!SetEnvironmentVariableW(L"PYTHONHOME", pythonhome)) {
187 exit(EXIT_FAILURE);
189 n = GetEnvironmentVariableW(L"URE_BOOTSTRAP", nullptr, 0);
190 if (n == 0) {
191 if (GetLastError() != ERROR_ENVVAR_NOT_FOUND ||
192 !SetEnvironmentVariableW(L"URE_BOOTSTRAP", bootstrap))
194 exit(EXIT_FAILURE);
197 STARTUPINFOW startinfo;
198 ZeroMemory(&startinfo, sizeof (STARTUPINFOW));
199 startinfo.cb = sizeof (STARTUPINFOW);
200 PROCESS_INFORMATION procinfo;
201 if (!CreateProcessW(
202 pythonexe, cl, nullptr, nullptr, FALSE, CREATE_UNICODE_ENVIRONMENT, nullptr,
203 nullptr, &startinfo, &procinfo)) {
204 exit(EXIT_FAILURE);
206 WaitForSingleObject(procinfo.hProcess,INFINITE);
207 DWORD exitStatus;
208 GetExitCodeProcess(procinfo.hProcess,&exitStatus);
209 exit(exitStatus);
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */