LCOV - code coverage report
Current view: top level - libreoffice/idlc/source - idlccompile.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 130 169 76.9 %
Date: 2012-12-27 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      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>
      27             : 
      28             : #if defined(SAL_W32)
      29             : #include <io.h>
      30             : #endif
      31             : 
      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             : #endif
      42             : 
      43             : #include <string.h>
      44             : 
      45             : using namespace ::rtl;
      46             : using namespace ::osl;
      47             : 
      48             : extern int yyparse();
      49             : extern FILE* yyin;
      50             : extern int yydebug;
      51             : 
      52             : sal_Int32 lineNumber = 1;
      53             : 
      54             : 
      55         138 : static OUString TMP(RTL_CONSTASCII_USTRINGPARAM("TMP"));
      56         138 : static OUString TEMP(RTL_CONSTASCII_USTRINGPARAM("TEMP"));
      57             : static sal_Char tmpFilePattern[512];
      58             : 
      59       41480 : sal_Bool isFileUrl(const OString& fileName)
      60             : {
      61       41480 :     if (fileName.indexOf("file://") == 0 )
      62       15555 :         return sal_True;
      63       25925 :     return sal_False;
      64             : }
      65             : 
      66       25925 : OString convertToAbsoluteSystemPath(const OString& fileName)
      67             : {
      68       25925 :     OUString uSysFileName;
      69       25925 :     OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
      70       25925 :     if ( isFileUrl(fileName) )
      71             :     {
      72       10370 :         if (FileBase::getSystemPathFromFileURL(uFileName, uSysFileName)
      73             :             != FileBase::E_None)
      74             :         {
      75             :             OSL_ASSERT(false);
      76             :         }
      77             :     } else
      78             :     {
      79       15555 :         OUString uWorkingDir, uUrlFileName, uTmp;
      80       15555 :         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
      81             :         {
      82             :             OSL_ASSERT(false);
      83             :         }
      84       15555 :         if (FileBase::getFileURLFromSystemPath(uFileName, uTmp)
      85             :             != FileBase::E_None)
      86             :         {
      87             :             OSL_ASSERT(false);
      88             :         }
      89       15555 :         if (FileBase::getAbsoluteFileURL(uWorkingDir, uTmp, uUrlFileName)
      90             :             != FileBase::E_None)
      91             :         {
      92             :             OSL_ASSERT(false);
      93             :         }
      94       15555 :         if (FileBase::getSystemPathFromFileURL(uUrlFileName, uSysFileName)
      95             :             != FileBase::E_None)
      96             :         {
      97             :             OSL_ASSERT(false);
      98       15555 :         }
      99             :     }
     100             : 
     101       25925 :     return OUStringToOString(uSysFileName, osl_getThreadTextEncoding());
     102             : }
     103             : 
     104       15555 : OString convertToFileUrl(const OString& fileName)
     105             : {
     106       15555 :     if ( !isFileUrl(fileName) )
     107             :     {
     108       10370 :         OString tmp = convertToAbsoluteSystemPath(fileName);
     109       10370 :         OUString uFileName(tmp.getStr(), tmp.getLength(), osl_getThreadTextEncoding());
     110       10370 :         OUString uUrlFileName;
     111       10370 :         if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
     112             :             != FileBase::E_None)
     113             :         {
     114             :             OSL_ASSERT(false);
     115             :         }
     116       10370 :         return OUStringToOString(uUrlFileName, osl_getThreadTextEncoding());
     117             :     }
     118             : 
     119        5185 :     return fileName;
     120             : }
     121             : 
     122       10370 : OString makeTempName(const OString& prefix)
     123             : {
     124       10370 :     OUString uTmpPath;
     125       10370 :     OString tmpPath;
     126             : 
     127       10370 :     if ( osl_getEnvironment(TMP.pData, &uTmpPath.pData) != osl_Process_E_None )
     128             :     {
     129       10370 :         if ( osl_getEnvironment(TEMP.pData, &uTmpPath.pData) != osl_Process_E_None )
     130             :         {
     131             : #if defined(SAL_W32)
     132             :             tmpPath = OString("c:\\temp");
     133             : #else
     134       10370 :             tmpPath = OString("/tmp");
     135             : #endif
     136             :         }
     137             :     }
     138             : 
     139       10370 :     if ( !uTmpPath.isEmpty() )
     140           0 :         tmpPath = OUStringToOString(uTmpPath, RTL_TEXTENCODING_UTF8);
     141             : 
     142             : #if defined(SAL_W32) || defined(SAL_UNX)
     143             : 
     144             :     OSL_ASSERT( sizeof(tmpFilePattern) >
     145             :                 (size_t) ( tmpPath.getLength()
     146             :                            + RTL_CONSTASCII_LENGTH( PATH_SEPARATOR )
     147             :                            + prefix.getLength()
     148             :                            + RTL_CONSTASCII_LENGTH( "XXXXXX") ) );
     149             : 
     150       10370 :     tmpFilePattern[ sizeof(tmpFilePattern)-1 ] = '\0';
     151       10370 :     strncpy(tmpFilePattern, tmpPath.getStr(), sizeof(tmpFilePattern)-1);
     152       10370 :     strncat(tmpFilePattern, PATH_SEPARATOR, sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
     153       10370 :     strncat(tmpFilePattern, prefix.getStr(), sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
     154       10370 :     strncat(tmpFilePattern, "XXXXXX", sizeof(tmpFilePattern)-1-strlen(tmpFilePattern));
     155             : 
     156             : #ifdef SAL_UNX
     157       10370 :     int nDescriptor = mkstemp(tmpFilePattern);
     158       10370 :     if( -1 == nDescriptor )
     159             :     {
     160           0 :         fprintf(stderr, "idlc: mkstemp(\"%s\") failed: %s\n", tmpFilePattern, strerror(errno));
     161           0 :         exit( 1 );
     162             :     }
     163             :     // the file shall later be reopened by stdio functions
     164       10370 :     close( nDescriptor );
     165             : #else
     166             :     (void) mktemp(tmpFilePattern);
     167             : #endif
     168             : #endif
     169             : 
     170       10370 :     return OString(tmpFilePattern);
     171             : }
     172             : 
     173        5185 : sal_Bool copyFile(const OString* source, const OString& target)
     174             : {
     175        5185 :     sal_Bool bRet = sal_True;
     176             : 
     177        5185 :     FILE* pSource = source == 0 ? stdin : fopen(source->getStr(), "rb");
     178        5185 :     if ( !pSource )
     179           0 :         return sal_False;
     180             : 
     181        5185 :     FILE* pTarget = fopen(target.getStr(), "wb");
     182        5185 :     if ( !pTarget )
     183             :     {
     184           0 :         fclose(pSource);
     185           0 :         return sal_False;
     186             :     }
     187             : 
     188        5185 :     size_t totalSize = 512;
     189        5185 :     size_t readSize  = 0;
     190             :     char   pBuffer[513];
     191             : 
     192       38158 :     while ( !feof(pSource) )
     193             :     {
     194       27788 :         if ( (readSize = fread(pBuffer, 1, totalSize, pSource)) > 0 && !ferror(pSource) )
     195             :         {
     196       27775 :             if ( (fwrite(pBuffer, 1, readSize, pTarget)) != readSize || ferror(pTarget) )
     197             :             {
     198           0 :                 if (source != 0) {
     199           0 :                     fclose(pSource);
     200             :                 }
     201           0 :                 fclose(pTarget);
     202           0 :                 return sal_False;
     203             :             }
     204             :         }
     205             :     }
     206             : 
     207        5185 :     if (source != 0) {
     208        5185 :         fclose(pSource);
     209             :     }
     210        5185 :     if ( fflush(pTarget) )
     211           0 :         bRet = sal_False;
     212        5185 :     fclose(pTarget);
     213             : 
     214        5185 :     return bRet;
     215             : }
     216             : 
     217        5185 : sal_Int32 compileFile(const OString * pathname)
     218             : {
     219             :     // preprocess input file
     220        5185 :     OString tmpFile = makeTempName(OString("idli_"));
     221        5185 :     OString preprocFile = makeTempName(OString("idlf_"));
     222             : 
     223        5185 :     OString fileName;
     224        5185 :     if (pathname == 0) {
     225           0 :         fileName = "stdin";
     226             :     } else {
     227        5185 :         fileName = *pathname;
     228             :     }
     229             : 
     230        5185 :     if ( !copyFile(pathname, tmpFile) )
     231             :     {
     232             :           fprintf(stderr, "%s: could not copy %s%s to %s\n",
     233           0 :                 idlc()->getOptions()->getProgramName().getStr(),
     234             :                 pathname == 0 ? "" : "file ", fileName.getStr(),
     235           0 :                 tmpFile.getStr());
     236           0 :           exit(99);
     237             :     }
     238             : 
     239        5185 :     idlc()->setFileName(fileName);
     240        5185 :     idlc()->setMainFileName(fileName);
     241        5185 :     idlc()->setRealFileName(tmpFile);
     242             : 
     243        5185 :     ::std::vector< ::rtl::OUString> lCppArgs;
     244        5185 :     lCppArgs.push_back("-DIDL");
     245        5185 :     lCppArgs.push_back("-C");
     246        5185 :     lCppArgs.push_back("-zI");
     247             : 
     248        5185 :     OStringBuffer cppArgs(256);
     249        5185 :     Options* pOptions = idlc()->getOptions();
     250             : 
     251        5185 :     OString filePath;
     252        5185 :     sal_Int32 index = fileName.lastIndexOf(SEPARATOR);
     253             : 
     254        5185 :     if ( index > 0)
     255             :     {
     256        5185 :         filePath = fileName.copy(0, index);
     257             : 
     258        5185 :         if ( !filePath.isEmpty() )
     259             :         {
     260        5185 :             cppArgs.append("-I");
     261        5185 :             cppArgs.append(filePath);
     262             :             lCppArgs.push_back(rtl::OStringToOUString(
     263             :                 cppArgs.makeStringAndClear().replace('\\', '/'),
     264        5185 :                 RTL_TEXTENCODING_UTF8));
     265             :         }
     266             :     }
     267             : 
     268        5185 :     if ( pOptions->isValid("-D") )
     269             :     {
     270           0 :         OString token, dOpt = pOptions->getOption("-D");
     271           0 :         sal_Int32 nIndex = 0;
     272           0 :         do
     273             :         {
     274           0 :             token = dOpt.getToken( 0, ' ', nIndex );
     275           0 :             if (token.getLength())
     276           0 :                 lCppArgs.push_back(rtl::OStringToOUString("-D" + token, RTL_TEXTENCODING_UTF8));
     277           0 :         } while( nIndex != -1 );
     278             :     }
     279             : 
     280        5185 :         if ( pOptions->isValid("-I") )
     281             :     {
     282        5185 :         OString token, incOpt = pOptions->getOption("-I");
     283        5185 :         sal_Int32 nIndex = 0;
     284       10365 :         do
     285             :         {
     286       10365 :             token = incOpt.getToken( 0, ' ', nIndex );
     287       10365 :             if (token.getLength())
     288       10365 :                 lCppArgs.push_back(rtl::OStringToOUString("-I" + token, RTL_TEXTENCODING_UTF8));
     289        5185 :         } while( nIndex != -1 );
     290             :     }
     291             : 
     292        5185 :     lCppArgs.push_back(OUString(RTL_CONSTASCII_USTRINGPARAM("-o")));
     293             : 
     294        5185 :         cppArgs.append(preprocFile);
     295        5185 :     lCppArgs.push_back(OStringToOUString(cppArgs.makeStringAndClear(), RTL_TEXTENCODING_UTF8));
     296             : 
     297        5185 :     cppArgs.append(tmpFile);
     298        5185 :     lCppArgs.push_back(OStringToOUString(cppArgs.makeStringAndClear(), RTL_TEXTENCODING_UTF8));
     299             : 
     300        5185 :     OUString cpp;
     301        5185 :     OUString startDir;
     302             : #ifndef SYSTEM_UCPP
     303        5185 :     if (osl_getExecutableFile(&cpp.pData) != osl_Process_E_None) {
     304             :         OSL_ASSERT(false);
     305             :     }
     306             : 
     307        5185 :     sal_Int32 idx= cpp.lastIndexOf("idlc");
     308        5185 :     cpp = cpp.copy(0, idx);
     309             : 
     310             : #if defined(SAL_W32)
     311             :     cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("ucpp.exe"));
     312             : #else
     313        5185 :     cpp += OUString( RTL_CONSTASCII_USTRINGPARAM("ucpp"));
     314             : #endif
     315             : #else // SYSTEM_UCPP
     316             :     cpp = OUString( RTL_CONSTASCII_USTRINGPARAM(UCPP));
     317             : #endif
     318        5185 :     oslProcess      hProcess = NULL;
     319        5185 :     oslProcessError procError = osl_Process_E_None;
     320             : 
     321        5185 :     const int nCmdArgs = lCppArgs.size();
     322        5185 :     rtl_uString** pCmdArgs = 0;
     323        5185 :     pCmdArgs = (rtl_uString**)rtl_allocateZeroMemory(nCmdArgs * sizeof(rtl_uString*));
     324             : 
     325        5185 :     ::std::vector< ::rtl::OUString >::iterator iter = lCppArgs.begin();
     326        5185 :     ::std::vector< ::rtl::OUString >::iterator end = lCppArgs.end();
     327        5185 :     int i = 0;
     328       57030 :         while ( iter != end ) {
     329       46660 :         pCmdArgs[i++] = (*iter).pData;
     330       46660 :         ++iter;
     331             :     }
     332             : 
     333             :         procError = osl_executeProcess( cpp.pData, pCmdArgs, nCmdArgs, osl_Process_WAIT,
     334        5185 :                                     0, startDir.pData, 0, 0, &hProcess );
     335             : 
     336             :     oslProcessInfo hInfo;
     337        5185 :     hInfo.Size = (sal_uInt32)(sizeof(oslProcessInfo));
     338        5185 :     if (osl_getProcessInfo(hProcess, osl_Process_EXITCODE, &hInfo)
     339             :         != osl_Process_E_None)
     340             :     {
     341             :         OSL_ASSERT(false);
     342             :     }
     343             : 
     344        5185 :     if ( procError || (hInfo.Code != 0) )
     345             :     {
     346           0 :         if ( procError != osl_Process_E_None )
     347           0 :             fprintf(stderr, "%s: starting preprocessor failed\n", pOptions->getProgramName().getStr());
     348             :         else
     349             :             fprintf(stderr, "%s: preprocessing %s%s failed\n",
     350           0 :                     pOptions->getProgramName().getStr(),
     351           0 :                     pathname == 0 ? "" : "file ", fileName.getStr());
     352             : 
     353           0 :         osl_freeProcessHandle(hProcess);
     354           0 :         rtl_freeMemory(pCmdArgs);
     355           0 :         exit(hInfo.Code ? hInfo.Code : 99);
     356             :     }
     357        5185 :     osl_freeProcessHandle(hProcess);
     358        5185 :     rtl_freeMemory(pCmdArgs);
     359             : 
     360        5185 :     if (unlink(tmpFile.getStr()) != 0)
     361             :     {
     362             :         fprintf(stderr, "%s: Could not remove cpp input file %s\n",
     363           0 :                  pOptions->getProgramName().getStr(), tmpFile.getStr());
     364           0 :         exit(99);
     365             :     }
     366             : 
     367        5185 :     if ( pOptions->isValid("-E") )
     368             :     {
     369           0 :         if (unlink(preprocFile.getStr()) != 0)
     370             :         {
     371             :             fprintf(stderr, "%s: Could not remove parser input file %s\n",
     372           0 :                        pOptions->getProgramName().getStr(), preprocFile.getStr());
     373           0 :             exit(99);
     374             :         }
     375           0 :         exit(0);
     376             :     }
     377             : 
     378             :     // parse file
     379        5185 :     yyin = fopen(preprocFile.getStr(), "r");
     380        5185 :     if (yyin == NULL)
     381             :     {
     382             :         fprintf(stderr, "%s: Could not open cpp output file %s\n",
     383           0 :                    pOptions->getProgramName().getStr(), preprocFile.getStr());
     384           0 :         exit(99);
     385             :     }
     386             : 
     387             :     //yydebug = 0 no trace information
     388             :     //yydebug = 1 parser produce trace information
     389        5185 :     yydebug = 0;
     390             : 
     391        5185 :     sal_Int32 nErrors = yyparse();
     392        5185 :     nErrors = idlc()->getErrorCount();
     393             : 
     394        5185 :     fclose(yyin);
     395        5185 :     if (unlink(preprocFile.getStr()) != 0)
     396             :     {
     397             :         fprintf(stderr, "%s: Could not remove parser input file %s\n",
     398           0 :                 pOptions->getProgramName().getStr(), preprocFile.getStr());
     399           0 :         exit(99);
     400             :     }
     401             : 
     402        5185 :     return nErrors;
     403         414 : }
     404             : 
     405             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10