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 <osl/process.h>
25 #include <osl/diagnose.h>
26 #include <osl/thread.h>
27 #include <osl/file.hxx>
36 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
37 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY) || defined(HAIKU)
46 using namespace ::osl
;
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
)
72 OUString uWorkingDir
, uUrlFileName
, uTmp
;
73 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
77 if (FileBase::getFileURLFromSystemPath(uFileName
, uTmp
)
82 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uTmp
, uUrlFileName
)
87 if (FileBase::getSystemPathFromFileURL(uUrlFileName
, uSysFileName
)
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
)
109 return OUStringToOString(uUrlFileName
, osl_getThreadTextEncoding());
115 static OString
makeTempName(const OString
& prefix
)
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
)
125 tmpPath
= OString("c:\\temp");
127 tmpPath
= OString("/tmp");
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
)
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
));
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
));
157 // the file shall later be reopened by stdio functions
158 close( nDescriptor
);
160 (void) mktemp(tmpFilePattern
);
164 return tmpFilePattern
;
167 bool copyFile(const OString
* source
, const OString
& target
)
171 FILE* pSource
= source
== nullptr ? stdin
: fopen(source
->getStr(), "rb");
175 FILE* pTarget
= fopen(target
.getStr(), "wb");
182 size_t const totalSize
= 512;
183 char pBuffer
[totalSize
+ 1];
185 while ( !feof(pSource
) )
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) {
201 if (source
!= nullptr) {
204 if ( fclose(pTarget
) )
210 sal_Int32
compileFile(const OString
* pathname
)
212 // preprocess input file
213 OString tmpFile
= makeTempName("idli_");
214 OString preprocFile
= makeTempName("idlf_");
217 if (pathname
== nullptr) {
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(),
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();
244 sal_Int32 index
= fileName
.lastIndexOf(SEPARATOR
);
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
));
292 if (osl_getExecutableFile(&cpp
.pData
) != osl_Process_E_None
) {
296 sal_Int32 idx
= cpp
.lastIndexOf("idlc");
297 cpp
= cpp
.copy(0, idx
);
305 cpp
= OUString(UCPP
);
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
]);
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
)
330 if ( procError
|| (hInfo
.Code
!= 0) )
332 if ( procError
!= osl_Process_E_None
)
333 fprintf(stderr
, "%s: starting preprocessor failed\n", pOptions
->getProgramName().getStr());
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());
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());
363 yyin
= fopen(preprocFile
.getStr(), "r");
366 fprintf(stderr
, "%s: Could not open cpp output file %s\n",
367 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
371 //yydebug = 0 no trace information
372 //yydebug = 1 parser produce trace information
376 sal_Int32 nErrors
= idlc()->getErrorCount();
379 if (unlink(preprocFile
.getStr()) != 0)
381 fprintf(stderr
, "%s: Could not remove parser input file %s\n",
382 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */