idlccompile.cxx (2fe1ca3d) idlccompile.cxx (5979ef3c)
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 21 unchanged lines hidden (view full) ---

30#include <osl/diagnose.h>
31#include <osl/thread.h>
32#include <osl/file.hxx>
33
34#if defined(SAL_W32) || defined(SAL_OS2)
35#include <io.h>
36#endif
37
1/**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance

--- 21 unchanged lines hidden (view full) ---

30#include <osl/diagnose.h>
31#include <osl/thread.h>
32#include <osl/file.hxx>
33
34#if defined(SAL_W32) || defined(SAL_OS2)
35#include <io.h>
36#endif
37
38#ifdef SAL_UNX
38#ifdef SAL_UNX
39#include <unistd.h>
40#if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD)
41#include <sys/wait.h>
42#else
43#include <wait.h>
44#endif
45#endif
46

--- 73 unchanged lines hidden (view full) ---

120 return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
121 }
122
123 return fileName;
124}
125
126OString makeTempName(const OString& prefix)
127{
39#include <unistd.h>
40#if defined(MACOSX) || defined(FREEBSD) || defined(NETBSD)
41#include <sys/wait.h>
42#else
43#include <wait.h>
44#endif
45#endif
46

--- 73 unchanged lines hidden (view full) ---

120 return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
121 }
122
123 return fileName;
124}
125
126OString makeTempName(const OString& prefix)
127{
128 OUString uTmpPath;
129 OString tmpPath;
128 OUString uTmpPath;
129 OString tmpPath;
130
130
131 if ( osl_getEnvironment(TMP.pData, &uTmpPath.pData) != osl_Process_E_None )
132 {
133 if ( osl_getEnvironment(TEMP.pData, &uTmpPath.pData) != osl_Process_E_None )
134 {
131 if ( osl_getEnvironment(TMP.pData, &uTmpPath.pData) != osl_Process_E_None )
132 {
133 if ( osl_getEnvironment(TEMP.pData, &uTmpPath.pData) != osl_Process_E_None )
134 {
135#if defined(SAL_W32)
135#if defined(SAL_W32)
136 tmpPath = OString("c:\\temp");
136 tmpPath = OString("c:\\temp");
137#else
137#else
138 tmpPath = OString("/tmp");
138 tmpPath = OString("/tmp");
139#endif
139#endif
140 }
141 }
140 }
141 }
142
142
143 if ( uTmpPath.getLength() )
144 tmpPath = OUStringToOString(uTmpPath, RTL_TEXTENCODING_UTF8);
143 if ( uTmpPath.getLength() )
144 tmpPath = OUStringToOString(uTmpPath, RTL_TEXTENCODING_UTF8);
145
146#if defined(SAL_W32) || defined(SAL_UNX) || defined(SAL_OS2)
147
148 OSL_ASSERT( sizeof(tmpFilePattern) > ( strlen(tmpPath)
149 + RTL_CONSTASCII_LENGTH(
150 PATH_SEPARATOR )
151 + prefix.getLength()
152 + RTL_CONSTASCII_LENGTH(
153 "XXXXXX") ) );
154
155 tmpFilePattern[ sizeof(tmpFilePattern)-1 ] = '\0';
156 strncpy(tmpFilePattern, tmpPath, sizeof(tmpFilePattern)-1);
157 strncat(tmpFilePattern, PATH_SEPARATOR, sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
158 strncat(tmpFilePattern, prefix.getStr(), sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
159 strncat(tmpFilePattern, "XXXXXX", sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
160
161#ifdef SAL_UNX
145
146#if defined(SAL_W32) || defined(SAL_UNX) || defined(SAL_OS2)
147
148 OSL_ASSERT( sizeof(tmpFilePattern) > ( strlen(tmpPath)
149 + RTL_CONSTASCII_LENGTH(
150 PATH_SEPARATOR )
151 + prefix.getLength()
152 + RTL_CONSTASCII_LENGTH(
153 "XXXXXX") ) );
154
155 tmpFilePattern[ sizeof(tmpFilePattern)-1 ] = '\0';
156 strncpy(tmpFilePattern, tmpPath, sizeof(tmpFilePattern)-1);
157 strncat(tmpFilePattern, PATH_SEPARATOR, sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
158 strncat(tmpFilePattern, prefix.getStr(), sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
159 strncat(tmpFilePattern, "XXXXXX", sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
160
161#ifdef SAL_UNX
162 int nDescriptor = mkstemp(tmpFilePattern);
163 if( -1 == nDescriptor )
164 {
165 fprintf( stderr,"idlc: couldn't create temporary file\n" );
166 exit( 1 );
167 }
168 // the file shall later be reopened by stdio functions
169 close( nDescriptor );
162 int nDescriptor = mkstemp(tmpFilePattern);
163 if( -1 == nDescriptor )
164 {
165 fprintf( stderr,"idlc: couldn't create temporary file %s\n", tmpFilePattern );
166 exit( 1 );
167 }
168 // the file shall later be reopened by stdio functions
169 close( nDescriptor );
170#else
170#else
171 (void) mktemp(tmpFilePattern);
171 (void) mktemp(tmpFilePattern);
172#endif
173#endif
174
172#endif
173#endif
174
175 return OString(tmpFilePattern);
175 return OString(tmpFilePattern);
176}
177
178sal_Bool copyFile(const OString* source, const OString& target)
179{
180 sal_Bool bRet = sal_True;
181
176}
177
178sal_Bool copyFile(const OString* source, const OString& target)
179{
180 sal_Bool bRet = sal_True;
181
182 FILE* pSource = source == 0 ? stdin : fopen(source->getStr(), "rb");
183
184 if ( !pSource )
182 FILE* pSource = source == 0 ? stdin : fopen(source->getStr(), "rb");
183 if ( !pSource )
185 return sal_False;
186
184 return sal_False;
185
187 FILE* pTarget = fopen(target.getStr(), "wb");
188
189 if ( !pTarget )
186 FILE* pTarget = fopen(target.getStr(), "wb");
187 if ( !pTarget )
190 {
191 fclose(pSource);
192 return sal_False;
193 }
188 {
189 fclose(pSource);
190 return sal_False;
191 }
194
192
195 size_t totalSize = 512;
196 size_t readSize = 0;
197 size_t writeSize = 0;
193 size_t totalSize = 512;
194 size_t readSize = 0;
195 size_t writeSize = 0;
198 char pBuffer[513];
196 char pBuffer[513];
199
200 while ( !feof(pSource) )
201 {
202 if ( (readSize = fread(pBuffer, 1, totalSize, pSource)) > 0 && !ferror(pSource) )
203 {
204 if ( (writeSize = fwrite(pBuffer, 1, readSize, pTarget)) != readSize || ferror(pTarget) )
205 {
206 if (source != 0) {
207 fclose(pSource);
208 }
209 fclose(pTarget);
210 return sal_False;
211 }
212 }
213 }
214
215 if (source != 0) {
216 fclose(pSource);
217 }
197
198 while ( !feof(pSource) )
199 {
200 if ( (readSize = fread(pBuffer, 1, totalSize, pSource)) > 0 && !ferror(pSource) )
201 {
202 if ( (writeSize = fwrite(pBuffer, 1, readSize, pTarget)) != readSize || ferror(pTarget) )
203 {
204 if (source != 0) {
205 fclose(pSource);
206 }
207 fclose(pTarget);
208 return sal_False;
209 }
210 }
211 }
212
213 if (source != 0) {
214 fclose(pSource);
215 }
218 if ( fflush(pTarget) )
216 if ( fflush(pTarget) )
219 bRet = sal_False;
220 fclose(pTarget);
221
222 return bRet;
223}
224
225sal_Int32 compileFile(const OString * pathname)
226{
217 bRet = sal_False;
218 fclose(pTarget);
219
220 return bRet;
221}
222
223sal_Int32 compileFile(const OString * pathname)
224{
227 // preprocess input file
228 OString tmpFile = makeTempName(OString("idli_"));
229 OString preprocFile = makeTempName(OString("idlf_"));
225 // preprocess input file
226 OString tmpFile = makeTempName(OString("idli_"));
227 OString preprocFile = makeTempName(OString("idlf_"));
230
231 OString fileName;
232 if (pathname == 0) {
233 fileName = "stdin";
234 } else {
235 fileName = *pathname;
236 }
237
228
229 OString fileName;
230 if (pathname == 0) {
231 fileName = "stdin";
232 } else {
233 fileName = *pathname;
234 }
235
238 if ( !copyFile(pathname, tmpFile) )
236 if ( !copyFile(pathname, tmpFile) )
239 {
237 {
240 fprintf(stderr, "%s: could not copy %s%s to %s\n",
238 fprintf(stderr, "%s: could not copy %s%s to %s\n",
241 idlc()->getOptions()->getProgramName().getStr(),
242 pathname == 0 ? "" : "file ", fileName.getStr(),
243 tmpFile.getStr());
239 idlc()->getOptions()->getProgramName().getStr(),
240 pathname == 0 ? "" : "file ", fileName.getStr(),
241 tmpFile.getStr());
244 exit(99);
242 exit(99);
245 }
246
243 }
244
247 idlc()->setFileName(fileName);
248 idlc()->setMainFileName(fileName);
249 idlc()->setRealFileName(tmpFile);
245 idlc()->setFileName(fileName);
246 idlc()->setMainFileName(fileName);
247 idlc()->setRealFileName(tmpFile);
250
248
251 OStringBuffer cppArgs(512);
252 cppArgs.append("-DIDL -Xi -Xc -+ -I.");
253 Options* pOptions = idlc()->getOptions();
249 ::std::vector< ::rtl::OUString > lCppArgs;
250 lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-DIDL")));
251 lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-C")));
252 lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-zI")));
253 lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-I.")));
254
255 OStringBuffer cppArgs(256);
256 Options* pOptions = idlc()->getOptions();
254
257
255 OString filePath;
256 sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
258 OString filePath;
259 sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
257
260
258 if ( index > 0)
259 {
260 filePath = fileName.copy(0, index);
261 if ( index > 0)
262 {
263 filePath = fileName.copy(0, index);
261
264
262 if ( filePath.getLength() )
263 {
264 cppArgs.append(" -I\"");
265 cppArgs.append(filePath);
266 cppArgs.append("\"");
265 if ( filePath.getLength() )
266 {
267 cppArgs.append("-I");
268 cppArgs.append(filePath);
269 lCppArgs.push_back(OStringToOUString(cppArgs.makeStringAndClear().replace('\\', '/'), RTL_TEXTENCODING_UTF8));
267 }
268 }
269
270 if ( pOptions->isValid("-D") )
271 {
270 }
271 }
272
273 if ( pOptions->isValid("-D") )
274 {
272 cppArgs.append(" ");
273 cppArgs.append(pOptions->getOption("-D"));
275 OString dOpt = pOptions->getOption("-D");
276 OString token;
277 sal_Int32 nIndex = 0;
278 do
279 {
280 token = dOpt.getToken( 0, ' ', nIndex );
281 if (token.getLength())
282 {
283 lCppArgs.push_back(OStringToOUString(token, RTL_TEXTENCODING_UTF8));
284 }
285 } while( nIndex != -1 );
274 }
286 }
287
275 if ( pOptions->isValid("-I") )
276 {
288 if ( pOptions->isValid("-I") )
289 {
277 cppArgs.append(" ");
278 cppArgs.append(pOptions->getOption("-I"));
290 OString incOpt = pOptions->getOption("-I");
291 OString token;
292 sal_Int32 nIndex = 0;
293 do
294 {
295 token = incOpt.getToken( 0, ' ', nIndex );
296 if (token.getLength())
297 {
298 lCppArgs.push_back(OStringToOUString(token, RTL_TEXTENCODING_UTF8));
299 }
300 } while( nIndex != -1 );
279 }
280
301 }
302
281 cppArgs.append(" \"");
282 cppArgs.append(tmpFile);
283 cppArgs.append("\" \"");
303 lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-o")));
304
284 cppArgs.append(preprocFile);
305 cppArgs.append(preprocFile);
285 cppArgs.append("\"");
306 lCppArgs.push_back(OStringToOUString(cppArgs.makeStringAndClear(), RTL_TEXTENCODING_UTF8));
286
307
287 OString cmdFileName = makeTempName(OString("idlc_"));
288 FILE* pCmdFile = fopen(cmdFileName, "w");
289
290 if ( !pCmdFile )
291 {
292 fprintf(stderr, "%s: couldn't open temporary file for preprocessor commands: %s\n",
293 idlc()->getOptions()->getProgramName().getStr(), cmdFileName.getStr());
294 exit(99);
295 }
296#ifdef SAL_OS2_00
297 char* tok = strtok( (char*)cppArgs.getStr(), " \t\n\r");
298 while( tok) {
299 if (tok[strlen(tok)-1] == '\"')
300 tok[strlen(tok)-1] = '\0';
301 if (*tok == '\"')
302 memcpy( tok, tok+1, strlen(tok));
303 if (strlen(tok)>0) {
304 fputs(tok, pCmdFile);
305 fputc('\n', pCmdFile);
306 }
307 tok = strtok( NULL, " \t\n\r");
308 }
309#else
310 fprintf(pCmdFile, "%s", cppArgs.getStr());
311#endif
312 fclose(pCmdFile);
313
314 OUString cmdArg(RTL_CONSTASCII_USTRINGPARAM("@"));
315 cmdArg += OStringToOUString(cmdFileName, RTL_TEXTENCODING_UTF8);
316
308 cppArgs.append(tmpFile);
309 lCppArgs.push_back(OStringToOUString(cppArgs.makeStringAndClear(), RTL_TEXTENCODING_UTF8));
310
317 OUString cpp;
318 OUString startDir;
319 if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
320 OSL_ASSERT(false);
321 }
322
323 sal_Int32 idx= cpp.lastIndexOf(OUString( RTL_CONSTASCII_USTRINGPARAM("idlc")) );
324 cpp = cpp.copy(0, idx);
325
326#if defined(SAL_W32) || defined(SAL_OS2)
311 OUString cpp;
312 OUString startDir;
313 if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
314 OSL_ASSERT(false);
315 }
316
317 sal_Int32 idx= cpp.lastIndexOf(OUString( RTL_CONSTASCII_USTRINGPARAM("idlc")) );
318 cpp = cpp.copy(0, idx);
319
320#if defined(SAL_W32) || defined(SAL_OS2)
327 cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("idlcpp.exe"));
321 cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("ucpp.exe"));
328#else
322#else
329 cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("idlcpp"));
323 cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("ucpp"));
330#endif
331
332 oslProcess hProcess = NULL;
333 oslProcessError procError = osl_Process_E_None;
334
324#endif
325
326 oslProcess hProcess = NULL;
327 oslProcessError procError = osl_Process_E_None;
328
335 procError = osl_executeProcess(cpp.pData, &cmdArg.pData, 1, osl_Process_WAIT,
329 const int nCmdArgs = lCppArgs.size();
330 rtl_uString** pCmdArgs = 0;
331 pCmdArgs = (rtl_uString**)rtl_allocateZeroMemory(nCmdArgs * sizeof(rtl_uString*));
332
333 ::std::vector< ::rtl::OUString >::iterator iter = lCppArgs.begin();
334 ::std::vector< ::rtl::OUString >::iterator end = lCppArgs.end();
335 int i = 0;
336 while ( iter != end ) {
337 pCmdArgs[i++] = (*iter).pData;
338 ++iter;
339 }
340
341 procError = osl_executeProcess(cpp.pData, pCmdArgs, nCmdArgs, osl_Process_WAIT,
336 0, startDir.pData, 0, 0, &hProcess);
337
338 oslProcessInfo hInfo;
339 hInfo.Size = (sal_uInt32)(sizeof(oslProcessInfo));
340 if (osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &hInfo)
341 != osl_Process_E_None)
342 {
343 OSL_ASSERT(false);
344 }
345
346 if ( procError || (hInfo.Code != 0) )
347 {
348 if ( procError != osl_Process_E_None )
349 fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
350 else
351 fprintf(stderr, "%s: preprocessing %s%s failed\n",
352 pOptions->getProgramName().getStr(),
353 pathname == 0 ? "" : "file ", fileName.getStr());
354
342 0, startDir.pData, 0, 0, &hProcess);
343
344 oslProcessInfo hInfo;
345 hInfo.Size = (sal_uInt32)(sizeof(oslProcessInfo));
346 if (osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &hInfo)
347 != osl_Process_E_None)
348 {
349 OSL_ASSERT(false);
350 }
351
352 if ( procError || (hInfo.Code != 0) )
353 {
354 if ( procError != osl_Process_E_None )
355 fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
356 else
357 fprintf(stderr, "%s: preprocessing %s%s failed\n",
358 pOptions->getProgramName().getStr(),
359 pathname == 0 ? "" : "file ", fileName.getStr());
360
355 unlink(tmpFile.getStr());
356 unlink(preprocFile.getStr());
357 unlink(cmdFileName.getStr());
361 // unlink(tmpFile.getStr());
362 // unlink(preprocFile.getStr());
363 // unlink(cmdFileName.getStr());
358 osl_freeProcessHandle(hProcess);
364 osl_freeProcessHandle(hProcess);
365 rtl_freeMemory(pCmdArgs);
359 exit(hInfo.Code ? hInfo.Code : 99);
360 }
361 osl_freeProcessHandle(hProcess);
366 exit(hInfo.Code ? hInfo.Code : 99);
367 }
368 osl_freeProcessHandle(hProcess);
369 rtl_freeMemory(pCmdArgs);
362
363 if (unlink(tmpFile.getStr()) != 0)
364 {
365 fprintf(stderr, "%s: Could not remove cpp input file %s\n",
366 pOptions->getProgramName().getStr(), tmpFile.getStr());
367 exit(99);
368 }
369
370
371 if (unlink(tmpFile.getStr()) != 0)
372 {
373 fprintf(stderr, "%s: Could not remove cpp input file %s\n",
374 pOptions->getProgramName().getStr(), tmpFile.getStr());
375 exit(99);
376 }
377
370 if (unlink(cmdFileName.getStr()) != 0)
371 {
372 fprintf(stderr, "%s: Could not remove unocpp command file %s\n",
373 pOptions->getProgramName().getStr(), cmdFileName.getStr());
374
375 exit(99);
376 }
377
378 if ( pOptions->isValid("-E") )
379 {
380 if (unlink(preprocFile) != 0)
381 {
382 fprintf(stderr, "%s: Could not remove parser input file %s\n",
383 pOptions->getProgramName().getStr(), preprocFile.getStr());
384 exit(99);
385 }

--- 14 unchanged lines hidden (view full) ---

400 yydebug = 0;
401
402 sal_Int32 nErrors = yyparse();
403 nErrors = idlc()->getErrorCount();
404
405 fclose(yyin);
406 if (unlink(preprocFile.getStr()) != 0)
407 {
378 if ( pOptions->isValid("-E") )
379 {
380 if (unlink(preprocFile) != 0)
381 {
382 fprintf(stderr, "%s: Could not remove parser input file %s\n",
383 pOptions->getProgramName().getStr(), preprocFile.getStr());
384 exit(99);
385 }

--- 14 unchanged lines hidden (view full) ---

400 yydebug = 0;
401
402 sal_Int32 nErrors = yyparse();
403 nErrors = idlc()->getErrorCount();
404
405 fclose(yyin);
406 if (unlink(preprocFile.getStr()) != 0)
407 {
408 fprintf(stderr, "%s: Could not remove parser input file %s\n",
408 fprintf(stderr, "%s: Could not remove parser input file %s\n",
409 pOptions->getProgramName().getStr(), preprocFile.getStr());
410 exit(99);
411 }
412
413 return nErrors;
414}
409 pOptions->getProgramName().getStr(), preprocFile.getStr());
410 exit(99);
411 }
412
413 return nErrors;
414}