bump product version to 7.2.5.1
[LibreOffice.git] / idlc / source / idlccompile.cxx
blobbf3325d6821a8bb7b2b98886284e031f09d13b72
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 <idlc.hxx>
21 #include <rtl/alloc.h>
22 #include <rtl/ustring.hxx>
23 #include <rtl/strbuf.hxx>
24 #include <o3tl/safeint.hxx>
25 #include <osl/process.h>
26 #include <osl/diagnose.h>
27 #include <osl/thread.h>
28 #include <osl/file.hxx>
30 #if defined(_WIN32)
31 #include <io.h>
32 #endif
34 #ifdef SAL_UNX
35 #include <errno.h>
36 #include <unistd.h>
37 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
38 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY) || defined(HAIKU)
39 #include <sys/wait.h>
40 #else
41 #include <wait.h>
42 #endif
43 #endif
45 #include <string.h>
47 using namespace ::osl;
49 extern int yyparse();
50 extern FILE* yyin;
51 extern int yydebug;
53 static char tmpFilePattern[512];
55 bool isFileUrl(const OString& fileName)
57 return fileName.startsWith("file://");
60 OString convertToAbsoluteSystemPath(const OString& fileName)
62 OUString uSysFileName;
63 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
64 if ( isFileUrl(fileName) )
66 if (FileBase::getSystemPathFromFileURL(uFileName, uSysFileName)
67 != FileBase::E_None)
69 OSL_ASSERT(false);
71 } else
73 OUString uWorkingDir, uUrlFileName, uTmp;
74 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
76 OSL_ASSERT(false);
78 if (FileBase::getFileURLFromSystemPath(uFileName, uTmp)
79 != FileBase::E_None)
81 OSL_ASSERT(false);
83 if (FileBase::getAbsoluteFileURL(uWorkingDir, uTmp, uUrlFileName)
84 != FileBase::E_None)
86 OSL_ASSERT(false);
88 if (FileBase::getSystemPathFromFileURL(uUrlFileName, uSysFileName)
89 != FileBase::E_None)
91 OSL_ASSERT(false);
95 return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());
98 OString convertToFileUrl(const OString& fileName)
100 if ( !isFileUrl(fileName) )
102 OString tmp = convertToAbsoluteSystemPath(fileName);
103 OUString uFileName(tmp.getStr(), tmp.getLength(), osl_getThreadTextEncoding());
104 OUString uUrlFileName;
105 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
106 != FileBase::E_None)
108 OSL_ASSERT(false);
110 return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
113 return fileName;
116 static OString makeTempName(const OString& prefix)
118 OUString uTmpPath;
119 OString tmpPath;
121 if ( osl_getEnvironment(OUString("TMP").pData, &uTmpPath.pData) != osl_Process_E_None )
123 if ( osl_getEnvironment(OUString("TEMP").pData, &uTmpPath.pData) != osl_Process_E_None )
125 #if defined(_WIN32)
126 tmpPath = OString("c:\\temp");
127 #else
128 tmpPath = OString("/tmp");
129 #endif
133 if ( !uTmpPath.isEmpty() )
134 tmpPath = OUStringToOString(uTmpPath, RTL_TEXTENCODING_UTF8);
136 #if defined(_WIN32) || defined(SAL_UNX)
138 OSL_ASSERT( sizeof(tmpFilePattern) >
139 o3tl::make_unsigned( tmpPath.getLength()
140 + RTL_CONSTASCII_LENGTH( PATH_SEPARATOR )
141 + prefix.getLength()
142 + RTL_CONSTASCII_LENGTH( "XXXXXX") ) );
144 tmpFilePattern[ sizeof(tmpFilePattern)-1 ] = '\0';
145 strncpy(tmpFilePattern, tmpPath.getStr(), sizeof(tmpFilePattern)-1);
146 strncat(tmpFilePattern, PATH_SEPARATOR, sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
147 strncat(tmpFilePattern, prefix.getStr(), sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
148 strncat(tmpFilePattern, "XXXXXX", sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
150 #ifdef SAL_UNX
151 // coverity[secure_temp] - https://communities.coverity.com/thread/3179
152 int nDescriptor = mkstemp(tmpFilePattern);
153 if( -1 == nDescriptor )
155 fprintf(stderr, "idlc: mkstemp(\"%s\") failed: %s\n", tmpFilePattern, strerror(errno));
156 exit( 1 );
158 // the file shall later be reopened by stdio functions
159 close( nDescriptor );
160 #else
161 (void) mktemp(tmpFilePattern);
162 #endif
163 #endif
165 return tmpFilePattern;
168 bool copyFile(const OString* source, const OString& target)
170 bool bRet = true;
172 FILE* pSource = source == nullptr ? stdin : fopen(source->getStr(), "rb");
173 if ( !pSource )
174 return false;
176 FILE* pTarget = fopen(target.getStr(), "wb");
177 if ( !pTarget )
179 fclose(pSource);
180 return false;
183 size_t const totalSize = 512;
184 char pBuffer[totalSize + 1];
186 while ( !feof(pSource) )
188 size_t readSize = fread(pBuffer, 1, totalSize, pSource);
189 if ( readSize > 0 && !ferror(pSource) )
191 if ( (fwrite(pBuffer, 1, readSize, pTarget)) != readSize || ferror(pTarget) )
193 if (source != nullptr) {
194 fclose(pSource);
196 fclose(pTarget);
197 return false;
202 if (source != nullptr) {
203 fclose(pSource);
205 if ( fclose(pTarget) )
206 bRet = false;
208 return bRet;
211 sal_Int32 compileFile(const OString * pathname)
213 // preprocess input file
214 OString tmpFile = makeTempName("idli_");
215 OString preprocFile = makeTempName("idlf_");
217 OString fileName;
218 if (pathname == nullptr) {
219 fileName = "stdin";
220 } else {
221 fileName = *pathname;
224 if ( !copyFile(pathname, tmpFile) )
226 fprintf(stderr, "%s: could not copy %s%s to %s\n",
227 idlc()->getOptions()->getProgramName().getStr(),
228 pathname == nullptr ? "" : "file ", fileName.getStr(),
229 tmpFile.getStr());
230 exit(99);
233 idlc()->setFileName(fileName);
234 idlc()->setMainFileName(fileName);
235 idlc()->setRealFileName(tmpFile);
237 ::std::vector< OUString> lCppArgs;
238 lCppArgs.emplace_back("-DIDL");
239 lCppArgs.emplace_back("-C");
240 lCppArgs.emplace_back("-zI");
242 Options* pOptions = idlc()->getOptions();
244 OString filePath;
245 sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
247 if ( index > 0)
249 filePath = fileName.copy(0, index);
251 if ( !filePath.isEmpty() )
253 OString cppArgs = "-I" + filePath;
254 lCppArgs.push_back(OStringToOUString(
255 cppArgs.replace('\\', '/'),
256 RTL_TEXTENCODING_UTF8));
260 if ( pOptions->isValid("-D") )
262 OString token, dOpt = pOptions->getOption("-D");
263 sal_Int32 nIndex = 0;
266 token = dOpt.getToken( 0, ' ', nIndex );
267 if (token.getLength())
268 lCppArgs.push_back("-D" + OStringToOUString(token, RTL_TEXTENCODING_UTF8));
269 } while( nIndex != -1 );
272 if ( pOptions->isValid("-I") )
274 OString token, incOpt = pOptions->getOption("-I");
275 sal_Int32 nIndex = 0;
278 token = incOpt.getToken( 0, ' ', nIndex );
279 if (token.getLength())
280 lCppArgs.push_back("-I" + OStringToOUString(token, RTL_TEXTENCODING_UTF8));
281 } while( nIndex != -1 );
284 lCppArgs.emplace_back("-o");
286 lCppArgs.push_back(OStringToOUString(preprocFile, RTL_TEXTENCODING_UTF8));
288 lCppArgs.push_back(OStringToOUString(tmpFile, RTL_TEXTENCODING_UTF8));
290 OUString cpp;
291 OUString startDir;
292 #ifndef SYSTEM_UCPP
293 if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
294 OSL_ASSERT(false);
297 sal_Int32 idx= cpp.lastIndexOf("idlc");
298 cpp = cpp.copy(0, idx);
300 #if defined(_WIN32)
301 cpp += "ucpp.exe";
302 #else
303 cpp += "ucpp";
304 #endif
305 #else // SYSTEM_UCPP
306 cpp = OUString(UCPP);
307 #endif
308 oslProcess hProcess = nullptr;
309 oslProcessError procError = osl_Process_E_None;
311 const int nCmdArgs = lCppArgs.size();
312 std::unique_ptr<rtl_uString*[]> pCmdArgs(new rtl_uString*[nCmdArgs]);
314 int i = 0;
315 for (auto const& elem : lCppArgs)
317 pCmdArgs[i++] = elem.pData;
320 procError = osl_executeProcess( cpp.pData, pCmdArgs.get(), nCmdArgs, osl_Process_WAIT,
321 nullptr, startDir.pData, nullptr, 0, &hProcess );
323 oslProcessInfo hInfo;
324 hInfo.Size = sal_uInt32(sizeof(oslProcessInfo));
325 if (osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &hInfo)
326 != osl_Process_E_None)
328 OSL_ASSERT(false);
331 if ( procError || (hInfo.Code != 0) )
333 if ( procError != osl_Process_E_None )
334 fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
335 else
336 fprintf(stderr, "%s: preprocessing %s%s failed\n",
337 pOptions->getProgramName().getStr(),
338 pathname == nullptr ? "" : "file ", fileName.getStr());
340 osl_freeProcessHandle(hProcess);
341 exit(hInfo.Code ? hInfo.Code : 99);
343 osl_freeProcessHandle(hProcess);
345 if (unlink(tmpFile.getStr()) != 0)
347 fprintf(stderr, "%s: Could not remove cpp input file %s\n",
348 pOptions->getProgramName().getStr(), tmpFile.getStr());
349 exit(99);
352 if ( pOptions->isValid("-E") )
354 if (unlink(preprocFile.getStr()) != 0)
356 fprintf(stderr, "%s: Could not remove parser input file %s\n",
357 pOptions->getProgramName().getStr(), preprocFile.getStr());
358 exit(99);
360 exit(0);
363 // parse file
364 yyin = fopen(preprocFile.getStr(), "r");
365 if (yyin == nullptr)
367 fprintf(stderr, "%s: Could not open cpp output file %s\n",
368 pOptions->getProgramName().getStr(), preprocFile.getStr());
369 exit(99);
372 //yydebug = 0 no trace information
373 //yydebug = 1 parser produce trace information
374 yydebug = 0;
376 yyparse();
377 sal_Int32 nErrors = idlc()->getErrorCount();
379 fclose(yyin);
380 if (unlink(preprocFile.getStr()) != 0)
382 fprintf(stderr, "%s: Could not remove parser input file %s\n",
383 pOptions->getProgramName().getStr(), preprocFile.getStr());
384 exit(99);
387 return nErrors;
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */