Stop leaking all ScPostIt instances.
[LibreOffice.git] / idlc / source / idlccompile.cxx
blob2bd544d2fe2557328db5acfce03f5816e58ef10f
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/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>
28 #if defined(SAL_W32)
29 #include <io.h>
30 #endif
32 #ifdef SAL_UNX
33 #include <errno.h>
34 #include <unistd.h>
35 #if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD) || \
36 defined(AIX) || defined(OPENBSD) || defined(DRAGONFLY)
37 #include <sys/wait.h>
38 #else
39 #include <wait.h>
40 #endif
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #endif
45 #include <string.h>
47 using namespace ::rtl;
48 using namespace ::osl;
50 extern int yyparse();
51 extern FILE* yyin;
52 extern int yydebug;
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://") )
64 return sal_True;
65 return sal_False;
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)
75 != FileBase::E_None)
77 OSL_ASSERT(false);
79 } else
81 OUString uWorkingDir, uUrlFileName, uTmp;
82 if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
84 OSL_ASSERT(false);
86 if (FileBase::getFileURLFromSystemPath(uFileName, uTmp)
87 != FileBase::E_None)
89 OSL_ASSERT(false);
91 if (FileBase::getAbsoluteFileURL(uWorkingDir, uTmp, uUrlFileName)
92 != FileBase::E_None)
94 OSL_ASSERT(false);
96 if (FileBase::getSystemPathFromFileURL(uUrlFileName, uSysFileName)
97 != FileBase::E_None)
99 OSL_ASSERT(false);
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)
114 != FileBase::E_None)
116 OSL_ASSERT(false);
118 return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
121 return fileName;
124 OString makeTempName(const OString& prefix)
126 OUString uTmpPath;
127 OString tmpPath;
129 if ( osl_getEnvironment(TMP.pData, &uTmpPath.pData) != osl_Process_E_None )
131 if ( osl_getEnvironment(TEMP.pData, &uTmpPath.pData) != osl_Process_E_None )
133 #if defined(SAL_W32)
134 tmpPath = OString("c:\\temp");
135 #else
136 tmpPath = OString("/tmp");
137 #endif
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 )
149 + prefix.getLength()
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));
158 #ifdef SAL_UNX
159 mode_t nOrigMode = umask(S_IRWXG | S_IRWXO);
160 int nDescriptor = mkstemp(tmpFilePattern);
161 umask(nOrigMode);
162 if( -1 == nDescriptor )
164 fprintf(stderr, "idlc: mkstemp(\"%s\") failed: %s\n", tmpFilePattern, strerror(errno));
165 exit( 1 );
167 // the file shall later be reopened by stdio functions
168 close( nDescriptor );
169 #else
170 (void) mktemp(tmpFilePattern);
171 #endif
172 #endif
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");
182 if ( !pSource )
183 return sal_False;
185 FILE* pTarget = fopen(target.getStr(), "wb");
186 if ( !pTarget )
188 fclose(pSource);
189 return sal_False;
192 size_t totalSize = 512;
193 size_t readSize = 0;
194 char pBuffer[513];
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) )
202 if (source != 0) {
203 fclose(pSource);
205 fclose(pTarget);
206 return sal_False;
211 if (source != 0) {
212 fclose(pSource);
214 if ( fflush(pTarget) )
215 bRet = sal_False;
216 fclose(pTarget);
218 return bRet;
221 sal_Int32 compileFile(const OString * pathname)
223 // preprocess input file
224 OString tmpFile = makeTempName(OString("idli_"));
225 OString preprocFile = makeTempName(OString("idlf_"));
227 OString fileName;
228 if (pathname == 0) {
229 fileName = "stdin";
230 } else {
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(),
239 tmpFile.getStr());
240 exit(99);
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();
255 OString filePath;
256 sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
258 if ( index > 0)
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));
304 OUString cpp;
305 OUString startDir;
306 #ifndef SYSTEM_UCPP
307 if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
308 OSL_ASSERT(false);
311 sal_Int32 idx= cpp.lastIndexOf("idlc");
312 cpp = cpp.copy(0, idx);
314 #if defined(SAL_W32)
315 cpp += "ucpp.exe";
316 #else
317 cpp += "ucpp";
318 #endif
319 #else // SYSTEM_UCPP
320 cpp = OUString(UCPP);
321 #endif
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();
331 int i = 0;
332 while ( iter != end ) {
333 pCmdArgs[i++] = (*iter).pData;
334 ++iter;
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)
345 OSL_ASSERT(false);
348 if ( procError || (hInfo.Code != 0) )
350 if ( procError != osl_Process_E_None )
351 fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
352 else
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());
368 exit(99);
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());
377 exit(99);
379 exit(0);
382 // parse file
383 yyin = fopen(preprocFile.getStr(), "r");
384 if (yyin == NULL)
386 fprintf(stderr, "%s: Could not open cpp output file %s\n",
387 pOptions->getProgramName().getStr(), preprocFile.getStr());
388 exit(99);
391 //yydebug = 0 no trace information
392 //yydebug = 1 parser produce trace information
393 yydebug = 0;
395 sal_Int32 nErrors = yyparse();
396 nErrors = idlc()->getErrorCount();
398 fclose(yyin);
399 if (unlink(preprocFile.getStr()) != 0)
401 fprintf(stderr, "%s: Could not remove parser input file %s\n",
402 pOptions->getProgramName().getStr(), preprocFile.getStr());
403 exit(99);
406 return nErrors;
409 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */