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 .
20 #include <idlc/idlc.hxx>
21 #include <rtl/ustring.hxx>
22 #include <rtl/strbuf.hxx>
23 #include <osl/process.h>
24 #include <osl/diagnose.h>
25 #include <osl/thread.h>
26 #include <osl/file.hxx>
35 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
36 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY)
41 #include <sys/types.h>
47 using namespace ::rtl
;
48 using namespace ::osl
;
54 sal_Int32 lineNumber
= 1;
57 static OUString
TMP("TMP");
58 static OUString
TEMP("TEMP");
59 static sal_Char tmpFilePattern
[512];
61 sal_Bool
isFileUrl(const OString
& fileName
)
63 if (fileName
.startsWith("file://") )
68 OString
convertToAbsoluteSystemPath(const OString
& fileName
)
70 OUString uSysFileName
;
71 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
72 if ( isFileUrl(fileName
) )
74 if (FileBase::getSystemPathFromFileURL(uFileName
, uSysFileName
)
81 OUString uWorkingDir
, uUrlFileName
, uTmp
;
82 if (osl_getProcessWorkingDir(&uWorkingDir
.pData
) != osl_Process_E_None
)
86 if (FileBase::getFileURLFromSystemPath(uFileName
, uTmp
)
91 if (FileBase::getAbsoluteFileURL(uWorkingDir
, uTmp
, uUrlFileName
)
96 if (FileBase::getSystemPathFromFileURL(uUrlFileName
, uSysFileName
)
103 return OUStringToOString(uSysFileName
, osl_getThreadTextEncoding());
106 OString
convertToFileUrl(const OString
& fileName
)
108 if ( !isFileUrl(fileName
) )
110 OString tmp
= convertToAbsoluteSystemPath(fileName
);
111 OUString
uFileName(tmp
.getStr(), tmp
.getLength(), osl_getThreadTextEncoding());
112 OUString uUrlFileName
;
113 if (FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
)
118 return OUStringToOString(uUrlFileName
, osl_getThreadTextEncoding());
124 OString
makeTempName(const OString
& prefix
)
129 if ( osl_getEnvironment(TMP
.pData
, &uTmpPath
.pData
) != osl_Process_E_None
)
131 if ( osl_getEnvironment(TEMP
.pData
, &uTmpPath
.pData
) != osl_Process_E_None
)
134 tmpPath
= OString("c:\\temp");
136 tmpPath
= OString("/tmp");
141 if ( !uTmpPath
.isEmpty() )
142 tmpPath
= OUStringToOString(uTmpPath
, RTL_TEXTENCODING_UTF8
);
144 #if defined(SAL_W32) || defined(SAL_UNX)
146 OSL_ASSERT( sizeof(tmpFilePattern
) >
147 (size_t) ( tmpPath
.getLength()
148 + RTL_CONSTASCII_LENGTH( PATH_SEPARATOR
)
150 + RTL_CONSTASCII_LENGTH( "XXXXXX") ) );
152 tmpFilePattern
[ sizeof(tmpFilePattern
)-1 ] = '\0';
153 strncpy(tmpFilePattern
, tmpPath
.getStr(), sizeof(tmpFilePattern
)-1);
154 strncat(tmpFilePattern
, PATH_SEPARATOR
, sizeof(tmpFilePattern
)-1-strlen(tmpFilePattern
));
155 strncat(tmpFilePattern
, prefix
.getStr(), sizeof(tmpFilePattern
)-1-strlen(tmpFilePattern
));
156 strncat(tmpFilePattern
, "XXXXXX", sizeof(tmpFilePattern
)-1-strlen(tmpFilePattern
));
159 mode_t nOrigMode
= umask(S_IRWXG
| S_IRWXO
);
160 int nDescriptor
= mkstemp(tmpFilePattern
);
162 if( -1 == nDescriptor
)
164 fprintf(stderr
, "idlc: mkstemp(\"%s\") failed: %s\n", tmpFilePattern
, strerror(errno
));
167 // the file shall later be reopened by stdio functions
168 close( nDescriptor
);
170 (void) mktemp(tmpFilePattern
);
174 return OString(tmpFilePattern
);
177 sal_Bool
copyFile(const OString
* source
, const OString
& target
)
179 sal_Bool bRet
= sal_True
;
181 FILE* pSource
= source
== 0 ? stdin
: fopen(source
->getStr(), "rb");
185 FILE* pTarget
= fopen(target
.getStr(), "wb");
192 size_t totalSize
= 512;
196 while ( !feof(pSource
) )
198 if ( (readSize
= fread(pBuffer
, 1, totalSize
, pSource
)) > 0 && !ferror(pSource
) )
200 if ( (fwrite(pBuffer
, 1, readSize
, pTarget
)) != readSize
|| ferror(pTarget
) )
214 if ( fflush(pTarget
) )
221 sal_Int32
compileFile(const OString
* pathname
)
223 // preprocess input file
224 OString tmpFile
= makeTempName(OString("idli_"));
225 OString preprocFile
= makeTempName(OString("idlf_"));
231 fileName
= *pathname
;
234 if ( !copyFile(pathname
, tmpFile
) )
236 fprintf(stderr
, "%s: could not copy %s%s to %s\n",
237 idlc()->getOptions()->getProgramName().getStr(),
238 pathname
== 0 ? "" : "file ", fileName
.getStr(),
243 idlc()->setFileName(fileName
);
244 idlc()->setMainFileName(fileName
);
245 idlc()->setRealFileName(tmpFile
);
247 ::std::vector
< OUString
> lCppArgs
;
248 lCppArgs
.push_back("-DIDL");
249 lCppArgs
.push_back("-C");
250 lCppArgs
.push_back("-zI");
252 OStringBuffer
cppArgs(256);
253 Options
* pOptions
= idlc()->getOptions();
256 sal_Int32 index
= fileName
.lastIndexOf(SEPARATOR
);
260 filePath
= fileName
.copy(0, index
);
262 if ( !filePath
.isEmpty() )
264 cppArgs
.append("-I");
265 cppArgs
.append(filePath
);
266 lCppArgs
.push_back(OStringToOUString(
267 cppArgs
.makeStringAndClear().replace('\\', '/'),
268 RTL_TEXTENCODING_UTF8
));
272 if ( pOptions
->isValid("-D") )
274 OString token
, dOpt
= pOptions
->getOption("-D");
275 sal_Int32 nIndex
= 0;
278 token
= dOpt
.getToken( 0, ' ', nIndex
);
279 if (token
.getLength())
280 lCppArgs
.push_back(OStringToOUString("-D" + token
, RTL_TEXTENCODING_UTF8
));
281 } while( nIndex
!= -1 );
284 if ( pOptions
->isValid("-I") )
286 OString token
, incOpt
= pOptions
->getOption("-I");
287 sal_Int32 nIndex
= 0;
290 token
= incOpt
.getToken( 0, ' ', nIndex
);
291 if (token
.getLength())
292 lCppArgs
.push_back(OStringToOUString("-I" + token
, RTL_TEXTENCODING_UTF8
));
293 } while( nIndex
!= -1 );
296 lCppArgs
.push_back(OUString("-o"));
298 cppArgs
.append(preprocFile
);
299 lCppArgs
.push_back(OStringToOUString(cppArgs
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
));
301 cppArgs
.append(tmpFile
);
302 lCppArgs
.push_back(OStringToOUString(cppArgs
.makeStringAndClear(), RTL_TEXTENCODING_UTF8
));
307 if (osl_getExecutableFile(&cpp
.pData
) != osl_Process_E_None
) {
311 sal_Int32 idx
= cpp
.lastIndexOf("idlc");
312 cpp
= cpp
.copy(0, idx
);
320 cpp
= OUString(UCPP
);
322 oslProcess hProcess
= NULL
;
323 oslProcessError procError
= osl_Process_E_None
;
325 const int nCmdArgs
= lCppArgs
.size();
326 rtl_uString
** pCmdArgs
= 0;
327 pCmdArgs
= (rtl_uString
**)rtl_allocateZeroMemory(nCmdArgs
* sizeof(rtl_uString
*));
329 ::std::vector
< OUString
>::iterator iter
= lCppArgs
.begin();
330 ::std::vector
< OUString
>::iterator end
= lCppArgs
.end();
332 while ( iter
!= end
) {
333 pCmdArgs
[i
++] = (*iter
).pData
;
337 procError
= osl_executeProcess( cpp
.pData
, pCmdArgs
, nCmdArgs
, osl_Process_WAIT
,
338 0, startDir
.pData
, 0, 0, &hProcess
);
340 oslProcessInfo hInfo
;
341 hInfo
.Size
= (sal_uInt32
)(sizeof(oslProcessInfo
));
342 if (osl_getProcessInfo(hProcess
, osl_Process_EXITCODE
, &hInfo
)
343 != osl_Process_E_None
)
348 if ( procError
|| (hInfo
.Code
!= 0) )
350 if ( procError
!= osl_Process_E_None
)
351 fprintf(stderr
, "%s: starting preprocessor failed\n", pOptions
->getProgramName().getStr());
353 fprintf(stderr
, "%s: preprocessing %s%s failed\n",
354 pOptions
->getProgramName().getStr(),
355 pathname
== 0 ? "" : "file ", fileName
.getStr());
357 osl_freeProcessHandle(hProcess
);
358 rtl_freeMemory(pCmdArgs
);
359 exit(hInfo
.Code
? hInfo
.Code
: 99);
361 osl_freeProcessHandle(hProcess
);
362 rtl_freeMemory(pCmdArgs
);
364 if (unlink(tmpFile
.getStr()) != 0)
366 fprintf(stderr
, "%s: Could not remove cpp input file %s\n",
367 pOptions
->getProgramName().getStr(), tmpFile
.getStr());
371 if ( pOptions
->isValid("-E") )
373 if (unlink(preprocFile
.getStr()) != 0)
375 fprintf(stderr
, "%s: Could not remove parser input file %s\n",
376 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
383 yyin
= fopen(preprocFile
.getStr(), "r");
386 fprintf(stderr
, "%s: Could not open cpp output file %s\n",
387 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
391 //yydebug = 0 no trace information
392 //yydebug = 1 parser produce trace information
395 sal_Int32 nErrors
= yyparse();
396 nErrors
= idlc()->getErrorCount();
399 if (unlink(preprocFile
.getStr()) != 0)
401 fprintf(stderr
, "%s: Could not remove parser input file %s\n",
402 pOptions
->getProgramName().getStr(), preprocFile
.getStr());
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */