bump product version to 6.4.0.3
[LibreOffice.git] / idlc / source / idlccompile.cxx
blob8d3897890e0ffb2e6f15c3f1c6ccca8774b9f4b2
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 <osl/process.h>
25 #include <osl/diagnose.h>
26 #include <osl/thread.h>
27 #include <osl/file.hxx>
29 #if defined(_WIN32)
30 #include <io.h>
31 #endif
33 #ifdef SAL_UNX
34 #include <errno.h>
35 #include <unistd.h>
36 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
37 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY) || defined(HAIKU)
38 #include <sys/wait.h>
39 #else
40 #include <wait.h>
41 #endif
42 #endif
44 #include <string.h>
46 using namespace ::osl;
48 extern int yyparse();
49 extern FILE* yyin;
50 extern int yydebug;
52 static sal_Char tmpFilePattern[512];
54 bool isFileUrl(const OString& fileName)
56 return fileName.startsWith("file://");
59 OString convertToAbsoluteSystemPath(const OString& fileName)
61 OUString uSysFileName;
62 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
63 if ( isFileUrl(fileName) )
65 if (FileBase::getSystemPathFromFileURL(uFileName, uSysFileName)
66 != FileBase::E_None)
68 OSL_ASSERT(false);
70 } else
72 OUString uWorkingDir, uUrlFileName, uTmp;
73 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
75 OSL_ASSERT(false);
77 if (FileBase::getFileURLFromSystemPath(uFileName, uTmp)
78 != FileBase::E_None)
80 OSL_ASSERT(false);
82 if (FileBase::getAbsoluteFileURL(uWorkingDir, uTmp, uUrlFileName)
83 != FileBase::E_None)
85 OSL_ASSERT(false);
87 if (FileBase::getSystemPathFromFileURL(uUrlFileName, uSysFileName)
88 != FileBase::E_None)
90 OSL_ASSERT(false);
94 return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());
97 OString convertToFileUrl(const OString& fileName)
99 if ( !isFileUrl(fileName) )
101 OString tmp = convertToAbsoluteSystemPath(fileName);
102 OUString uFileName(tmp.getStr(), tmp.getLength(), osl_getThreadTextEncoding());
103 OUString uUrlFileName;
104 if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
105 != FileBase::E_None)
107 OSL_ASSERT(false);
109 return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
112 return fileName;
115 static OString makeTempName(const OString& prefix)
117 OUString uTmpPath;
118 OString tmpPath;
120 if ( osl_getEnvironment(OUString("TMP").pData, &uTmpPath.pData) != osl_Process_E_None )
122 if ( osl_getEnvironment(OUString("TEMP").pData, &uTmpPath.pData) != osl_Process_E_None )
124 #if defined(_WIN32)
125 tmpPath = OString("c:\\temp");
126 #else
127 tmpPath = OString("/tmp");
128 #endif
132 if ( !uTmpPath.isEmpty() )
133 tmpPath = OUStringToOString(uTmpPath, RTL_TEXTENCODING_UTF8);
135 #if defined(_WIN32) || defined(SAL_UNX)
137 OSL_ASSERT( sizeof(tmpFilePattern) >
138 static_cast<size_t>( tmpPath.getLength()
139 + RTL_CONSTASCII_LENGTH( PATH_SEPARATOR )
140 + prefix.getLength()
141 + RTL_CONSTASCII_LENGTH( "XXXXXX") ) );
143 tmpFilePattern[ sizeof(tmpFilePattern)-1 ] = '\0';
144 strncpy(tmpFilePattern, tmpPath.getStr(), sizeof(tmpFilePattern)-1);
145 strncat(tmpFilePattern, PATH_SEPARATOR, sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
146 strncat(tmpFilePattern, prefix.getStr(), sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
147 strncat(tmpFilePattern, "XXXXXX", sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
149 #ifdef SAL_UNX
150 // coverity[secure_temp] - https://communities.coverity.com/thread/3179
151 int nDescriptor = mkstemp(tmpFilePattern);
152 if( -1 == nDescriptor )
154 fprintf(stderr, "idlc: mkstemp(\"%s\") failed: %s\n", tmpFilePattern, strerror(errno));
155 exit( 1 );
157 // the file shall later be reopened by stdio functions
158 close( nDescriptor );
159 #else
160 (void) mktemp(tmpFilePattern);
161 #endif
162 #endif
164 return tmpFilePattern;
167 bool copyFile(const OString* source, const OString& target)
169 bool bRet = true;
171 FILE* pSource = source == nullptr ? stdin : fopen(source->getStr(), "rb");
172 if ( !pSource )
173 return false;
175 FILE* pTarget = fopen(target.getStr(), "wb");
176 if ( !pTarget )
178 fclose(pSource);
179 return false;
182 size_t const totalSize = 512;
183 char pBuffer[totalSize + 1];
185 while ( !feof(pSource) )
187 size_t readSize;
188 if ( (readSize = fread(pBuffer, 1, totalSize, pSource)) > 0 && !ferror(pSource) )
190 if ( (fwrite(pBuffer, 1, readSize, pTarget)) != readSize || ferror(pTarget) )
192 if (source != nullptr) {
193 fclose(pSource);
195 fclose(pTarget);
196 return false;
201 if (source != nullptr) {
202 fclose(pSource);
204 if ( fclose(pTarget) )
205 bRet = false;
207 return bRet;
210 sal_Int32 compileFile(const OString * pathname)
212 // preprocess input file
213 OString tmpFile = makeTempName("idli_");
214 OString preprocFile = makeTempName("idlf_");
216 OString fileName;
217 if (pathname == nullptr) {
218 fileName = "stdin";
219 } else {
220 fileName = *pathname;
223 if ( !copyFile(pathname, tmpFile) )
225 fprintf(stderr, "%s: could not copy %s%s to %s\n",
226 idlc()->getOptions()->getProgramName().getStr(),
227 pathname == nullptr ? "" : "file ", fileName.getStr(),
228 tmpFile.getStr());
229 exit(99);
232 idlc()->setFileName(fileName);
233 idlc()->setMainFileName(fileName);
234 idlc()->setRealFileName(tmpFile);
236 ::std::vector< OUString> lCppArgs;
237 lCppArgs.emplace_back("-DIDL");
238 lCppArgs.emplace_back("-C");
239 lCppArgs.emplace_back("-zI");
241 Options* pOptions = idlc()->getOptions();
243 OString filePath;
244 sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
246 if ( index > 0)
248 filePath = fileName.copy(0, index);
250 if ( !filePath.isEmpty() )
252 OString cppArgs = "-I" + filePath;
253 lCppArgs.push_back(OStringToOUString(
254 cppArgs.replace('\\', '/'),
255 RTL_TEXTENCODING_UTF8));
259 if ( pOptions->isValid("-D") )
261 OString token, dOpt = pOptions->getOption("-D");
262 sal_Int32 nIndex = 0;
265 token = dOpt.getToken( 0, ' ', nIndex );
266 if (token.getLength())
267 lCppArgs.push_back(OStringToOUString("-D" + token, RTL_TEXTENCODING_UTF8));
268 } while( nIndex != -1 );
271 if ( pOptions->isValid("-I") )
273 OString token, incOpt = pOptions->getOption("-I");
274 sal_Int32 nIndex = 0;
277 token = incOpt.getToken( 0, ' ', nIndex );
278 if (token.getLength())
279 lCppArgs.push_back(OStringToOUString("-I" + token, RTL_TEXTENCODING_UTF8));
280 } while( nIndex != -1 );
283 lCppArgs.emplace_back("-o");
285 lCppArgs.push_back(OStringToOUString(preprocFile, RTL_TEXTENCODING_UTF8));
287 lCppArgs.push_back(OStringToOUString(tmpFile, RTL_TEXTENCODING_UTF8));
289 OUString cpp;
290 OUString startDir;
291 #ifndef SYSTEM_UCPP
292 if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
293 OSL_ASSERT(false);
296 sal_Int32 idx= cpp.lastIndexOf("idlc");
297 cpp = cpp.copy(0, idx);
299 #if defined(_WIN32)
300 cpp += "ucpp.exe";
301 #else
302 cpp += "ucpp";
303 #endif
304 #else // SYSTEM_UCPP
305 cpp = OUString(UCPP);
306 #endif
307 oslProcess hProcess = nullptr;
308 oslProcessError procError = osl_Process_E_None;
310 const int nCmdArgs = lCppArgs.size();
311 std::unique_ptr<rtl_uString*[]> pCmdArgs(new rtl_uString*[nCmdArgs]);
313 int i = 0;
314 for (auto const& elem : lCppArgs)
316 pCmdArgs[i++] = elem.pData;
319 procError = osl_executeProcess( cpp.pData, pCmdArgs.get(), nCmdArgs, osl_Process_WAIT,
320 nullptr, startDir.pData, nullptr, 0, &hProcess );
322 oslProcessInfo hInfo;
323 hInfo.Size = sal_uInt32(sizeof(oslProcessInfo));
324 if (osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &hInfo)
325 != osl_Process_E_None)
327 OSL_ASSERT(false);
330 if ( procError || (hInfo.Code != 0) )
332 if ( procError != osl_Process_E_None )
333 fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
334 else
335 fprintf(stderr, "%s: preprocessing %s%s failed\n",
336 pOptions->getProgramName().getStr(),
337 pathname == nullptr ? "" : "file ", fileName.getStr());
339 osl_freeProcessHandle(hProcess);
340 exit(hInfo.Code ? hInfo.Code : 99);
342 osl_freeProcessHandle(hProcess);
344 if (unlink(tmpFile.getStr()) != 0)
346 fprintf(stderr, "%s: Could not remove cpp input file %s\n",
347 pOptions->getProgramName().getStr(), tmpFile.getStr());
348 exit(99);
351 if ( pOptions->isValid("-E") )
353 if (unlink(preprocFile.getStr()) != 0)
355 fprintf(stderr, "%s: Could not remove parser input file %s\n",
356 pOptions->getProgramName().getStr(), preprocFile.getStr());
357 exit(99);
359 exit(0);
362 // parse file
363 yyin = fopen(preprocFile.getStr(), "r");
364 if (yyin == nullptr)
366 fprintf(stderr, "%s: Could not open cpp output file %s\n",
367 pOptions->getProgramName().getStr(), preprocFile.getStr());
368 exit(99);
371 //yydebug = 0 no trace information
372 //yydebug = 1 parser produce trace information
373 yydebug = 0;
375 yyparse();
376 sal_Int32 nErrors = idlc()->getErrorCount();
378 fclose(yyin);
379 if (unlink(preprocFile.getStr()) != 0)
381 fprintf(stderr, "%s: Could not remove parser input file %s\n",
382 pOptions->getProgramName().getStr(), preprocFile.getStr());
383 exit(99);
386 return nErrors;
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */