1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
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>
37 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
38 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY) || defined(HAIKU)
47 using namespace ::osl
;
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
)
73 OUString uWorkingDir
, uUrlFileName
, uTmp
;
74 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
78 if (FileBase::getFileURLFromSystemPath(uFileName
, uTmp
)
83 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uTmp
, uUrlFileName
)
88 if (FileBase::getSystemPathFromFileURL(uUrlFileName
, uSysFileName
)
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
)
110 return OUStringToOString(uUrlFileName
, osl_getThreadTextEncoding());
116 static OString
makeTempName(const OString
& prefix
)
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
)
126 tmpPath
= OString("c:\\temp");
128 tmpPath
= OString("/tmp");
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
)
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
));
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
));
158 // the file shall later be reopened by stdio functions
159 close( nDescriptor
);
161 (void) mktemp(tmpFilePattern
);
165 return tmpFilePattern
;
168 bool copyFile(const OString
* source
, const OString
& target
)
172 FILE* pSource
= source
== nullptr ? stdin
: fopen(source
->getStr(), "rb");
176 FILE* pTarget
= fopen(target
.getStr(), "wb");
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) {
202 if (source
!= nullptr) {
205 if ( fclose(pTarget
) )
211 sal_Int32
compileFile(const OString
* pathname
)
213 // preprocess input file
214 OString tmpFile
= makeTempName("idli_");
215 OString preprocFile
= makeTempName("idlf_");
218 if (pathname
== nullptr) {
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(),
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();
245 sal_Int32 index
= fileName
.lastIndexOf(SEPARATOR
);
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
));
293 if (osl_getExecutableFile(&cpp
.pData
) != osl_Process_E_None
) {
297 sal_Int32 idx
= cpp
.lastIndexOf("idlc");
298 cpp
= cpp
.copy(0, idx
);
306 cpp
= OUString(UCPP
);
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
]);
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
)
331 if ( procError
|| (hInfo
.Code
!= 0) )
333 if ( procError
!= osl_Process_E_None
)
334 fprintf(stderr
, "%s: starting preprocessor failed\n", pOptions
->getProgramName().getStr());
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());
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());
364 yyin
= fopen(preprocFile
.getStr(), "r");
367 fprintf(stderr
, "%s: Could not open cpp output file %s\n",
368 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
372 //yydebug = 0 no trace information
373 //yydebug = 1 parser produce trace information
377 sal_Int32 nErrors
= idlc()->getErrorCount();
380 if (unlink(preprocFile
.getStr()) != 0)
382 fprintf(stderr
, "%s: Could not remove parser input file %s\n",
383 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */