LCOV - code coverage report
Current view: top level - idlc/source - options.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 45 182 24.7 %
Date: 2015-06-13 12:38:46 Functions: 7 11 63.6 %
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             : 
      21             : #include "idlc/options.hxx"
      22             : 
      23             : #include <osl/diagnose.h>
      24             : #include <rtl/string.hxx>
      25             : #include <rtl/strbuf.hxx>
      26             : 
      27             : #include "rtl/ustring.hxx"
      28             : #include "osl/file.hxx"
      29             : 
      30             : #ifdef WNT
      31             : #   include <windows.h>
      32             : #endif
      33             : 
      34             : /*
      35             : #ifndef WIN32_LEAN_AND_MEAN
      36             : #   define WIN32_LEAN_AND_MEAN
      37             : # ifdef _MSC_VER
      38             : #   pragma warning(push,1)
      39             : # endif
      40             : #   include <windows.h>
      41             : # ifdef _MSC_VER
      42             : #   pragma warning(pop)
      43             : # endif
      44             : #   include <tchar.h>
      45             : #   undef WIN32_LEAN_AND_MEAN
      46             : #endif
      47             : */
      48             : 
      49             : #include <stdio.h>
      50             : #include <string.h>
      51             : 
      52             : 
      53         329 : Options::Options(char const * progname)
      54         329 :   : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false)
      55             : {
      56         329 : }
      57             : 
      58         329 : Options::~Options()
      59             : {
      60         329 : }
      61             : 
      62             : // static
      63         987 : bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len)
      64             : {
      65         987 :   bool result = ((arg != 0) && (len > 0));
      66             :   OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments");
      67         987 :   if (result)
      68             :   {
      69         987 :     switch(arg[0])
      70             :     {
      71             :     case '@':
      72           0 :       if ((result = (len > 1)))
      73             :       {
      74             :         // "@<cmdfile>"
      75           0 :         result = Options::checkCommandFile (rArgs, &(arg[1]));
      76             :       }
      77           0 :       break;
      78             :     case '-':
      79         329 :       if ((result = (len > 1)))
      80             :       {
      81             :         // "-<option>"
      82         329 :         switch (arg[1])
      83             :         {
      84             :         case 'O':
      85             :         case 'M':
      86             :         case 'I':
      87             :         case 'D':
      88             :           {
      89             :             // "-<option>[<param>]
      90         329 :             std::string option(&(arg[0]), 2);
      91         329 :             rArgs.push_back(option);
      92         329 :             if (len > 2)
      93             :             {
      94             :               // "-<option><param>"
      95           0 :               std::string param(&(arg[2]), len - 2);
      96           0 :               rArgs.push_back(param);
      97             :             }
      98         329 :             break;
      99             :           }
     100             :         default:
     101             :           // "-<option>" ([long] option, w/o param)
     102           0 :           rArgs.push_back(std::string(arg, len));
     103           0 :           break;
     104             :         }
     105             :       }
     106         329 :       break;
     107             :     default:
     108             :       // "<param>"
     109         658 :       rArgs.push_back(std::string(arg, len));
     110         658 :       break;
     111             :     }
     112             :   }
     113         987 :   return result;
     114             : }
     115             : 
     116             : // static
     117           0 : bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename)
     118             : {
     119           0 :     FILE * fp = fopen(filename, "r");
     120           0 :     if (fp == 0)
     121             :     {
     122           0 :         fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename);
     123           0 :         return false;
     124             :     }
     125             : 
     126           0 :     std::string buffer;
     127           0 :     buffer.reserve(256);
     128             : 
     129           0 :     bool quoted = false;
     130           0 :     int c = EOF;
     131           0 :     while ((c = fgetc(fp)) != EOF)
     132             :     {
     133           0 :         switch(c)
     134             :         {
     135             :         case '\"':
     136           0 :           quoted = !quoted;
     137           0 :           break;
     138             :         case ' ':
     139             :         case '\t':
     140             :         case '\r':
     141             :         case '\n':
     142           0 :           if (!quoted)
     143             :           {
     144           0 :               if (!buffer.empty())
     145             :               {
     146             :                   // append current argument.
     147           0 :                   if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
     148             :                   {
     149           0 :                       (void) fclose(fp);
     150           0 :                       return false;
     151             :                   }
     152           0 :                   buffer.clear();
     153             :               }
     154           0 :               break;
     155             :           }
     156             :         default:
     157             :           // quoted white-space fall through
     158           0 :           buffer.push_back(sal::static_int_cast<char>(c));
     159           0 :           break;
     160             :         }
     161             :     }
     162           0 :     if (!buffer.empty())
     163             :     {
     164             :         // append unterminated argument.
     165           0 :         if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
     166             :         {
     167           0 :             (void) fclose(fp);
     168           0 :             return false;
     169             :         }
     170           0 :         buffer.clear();
     171             :     }
     172           0 :     return (fclose(fp) == 0);
     173             : }
     174             : 
     175           0 : bool Options::badOption(char const * reason, std::string const & rArg) throw(IllegalArgument)
     176             : {
     177           0 :   OStringBuffer message;
     178           0 :   if (reason != 0)
     179             :   {
     180           0 :     message.append(reason); message.append(" option '"); message.append(rArg.c_str()); message.append("'");
     181           0 :     throw IllegalArgument(message.makeStringAndClear());
     182             :   }
     183           0 :   return false;
     184             : }
     185             : 
     186           0 : bool Options::setOption(char const * option, std::string const & rArg)
     187             : {
     188           0 :   bool result = (0 == strcmp(option, rArg.c_str()));
     189           0 :   if (result)
     190           0 :     m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size());
     191           0 :   return result;
     192             : }
     193             : 
     194             : #ifdef WNT
     195             : /* Helper functiopn to convert windows paths including spaces, brackets etc. into
     196             :    a windows short Url. The ucpp preprocessor has problems with such paths and returns
     197             :    with error.
     198             : */
     199             : OString convertIncPathtoShortWindowsPath(const OString& incPath) {
     200             :     OUString path = OStringToOUString(incPath, RTL_TEXTENCODING_UTF8);
     201             : 
     202             :     std::vector<sal_Unicode> vec(path.getLength() + 1);
     203             :     //GetShortPathNameW only works if the file can be found!
     204             :     const DWORD len = GetShortPathNameW(
     205             :         reinterpret_cast<LPCWSTR>(path.getStr()), reinterpret_cast<LPWSTR>(&vec[0]), path.getLength() + 1);
     206             : 
     207             :     if (len > 0)
     208             :     {
     209             :         OUString ret(&vec[0], len);
     210             :         return OUStringToOString(ret, RTL_TEXTENCODING_UTF8);
     211             :     }
     212             : 
     213             :     return incPath;
     214             : }
     215             : #endif
     216             : 
     217         329 : bool Options::initOptions(std::vector< std::string > & rArgs) throw(IllegalArgument)
     218             : {
     219         329 :   std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
     220         987 :   for (; first != last; ++first)
     221             :   {
     222         658 :     if ((*first)[0] != '-')
     223             :     {
     224         329 :       OString filename((*first).c_str(), (*first).size());
     225         658 :       OString tmp(filename.toAsciiLowerCase());
     226         329 :       if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4))
     227             :       {
     228           0 :         throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted");
     229             :       }
     230         329 :       m_inputFiles.push_back(filename);
     231         658 :       continue;
     232             :     }
     233             : 
     234         329 :     std::string const option(*first);
     235         329 :     switch((*first)[1])
     236             :     {
     237             :     case 'O':
     238             :       {
     239         329 :         if (!((++first != last) && ((*first)[0] != '-')))
     240             :         {
     241           0 :           return badOption("invalid", option);
     242             :         }
     243         329 :         OString param((*first).c_str(), (*first).size());
     244         329 :         m_options["-O"] = param;
     245         329 :         break;
     246             :       }
     247             :     case 'M':
     248             :       {
     249           0 :         if (!((++first != last) && ((*first)[0] != '-')))
     250             :         {
     251           0 :           return badOption("invalid", option);
     252             :         }
     253           0 :         OString param((*first).c_str(), (*first).size());
     254           0 :         m_options["-M"] = param;
     255           0 :         break;
     256             :       }
     257             :     case 'I':
     258             :       {
     259           0 :         if (!((++first != last) && ((*first)[0] != '-')))
     260             :         {
     261           0 :           return badOption("invalid", option);
     262             :         }
     263           0 :         OString param((*first).c_str(), (*first).size());
     264             :         {
     265             :           // quote param token(s).
     266           0 :           OStringBuffer buffer;
     267           0 :           sal_Int32 k = 0;
     268           0 :           do
     269             :           {
     270           0 :             if (!buffer.isEmpty())
     271           0 :               buffer.append(' ');
     272             : //          buffer.append("-I\"");
     273             : #ifdef WNT
     274             :             OString incpath = convertIncPathtoShortWindowsPath(param.getToken(0, ';', k));
     275             : #else
     276           0 :             OString incpath = param.getToken(0, ';', k);
     277             : #endif
     278           0 :             buffer.append(incpath);
     279             : //          buffer.append("\"");
     280           0 :           } while (k != -1);
     281           0 :           param = buffer.makeStringAndClear();
     282             :         }
     283           0 :         if (m_options.count("-I") > 0)
     284             :         {
     285             :           // append param.
     286           0 :           OStringBuffer buffer(m_options["-I"]);
     287           0 :           buffer.append(' '); buffer.append(param);
     288           0 :           param = buffer.makeStringAndClear();
     289             :         }
     290           0 :         m_options["-I"] = param;
     291           0 :         break;
     292             :       }
     293             :     case 'D':
     294             :       {
     295           0 :         if (!((++first != last) && ((*first)[0] != '-')))
     296             :         {
     297           0 :           return badOption("invalid", option);
     298             :         }
     299           0 :         OString param("-D"); param += OString((*first).c_str(), (*first).size());
     300           0 :         if (m_options.count("-D") > 0)
     301             :         {
     302           0 :           OStringBuffer buffer(m_options["-D"]);
     303           0 :           buffer.append(' '); buffer.append(param);
     304           0 :           param = buffer.makeStringAndClear();
     305             :         }
     306           0 :         m_options["-D"] = param;
     307           0 :         break;
     308             :       }
     309             :     case 'C':
     310             :       {
     311           0 :         if (!setOption("-C", option))
     312             :         {
     313           0 :           return badOption("invalid", option);
     314             :         }
     315           0 :         break;
     316             :       }
     317             :     case 'c':
     318             :       {
     319           0 :         if (!setOption("-cid", option))
     320             :         {
     321           0 :           return badOption("invalid", option);
     322             :         }
     323           0 :         break;
     324             :       }
     325             :     case 'q':
     326             :       {
     327           0 :         if (!setOption("-quiet", option))
     328             :         {
     329           0 :           return badOption("invalid", option);
     330             :         }
     331           0 :         m_quiet = true;
     332           0 :         break;
     333             :       }
     334             :     case 'v':
     335             :       {
     336           0 :         if (!setOption("-verbose", option))
     337             :         {
     338           0 :           return badOption("invalid", option);
     339             :         }
     340           0 :         m_verbose = true;
     341           0 :         break;
     342             :       }
     343             :     case 'w':
     344             :       {
     345           0 :         if (!(setOption("-w", option) || setOption("-we", option)))
     346             :         {
     347           0 :           return badOption("invalid", option);
     348             :         }
     349           0 :         break;
     350             :       }
     351             :     case 'h':
     352             :     case '?':
     353             :       {
     354           0 :         if (!(setOption("-h", option) || setOption("-?", option)))
     355             :         {
     356           0 :           return badOption("invalid", option);
     357             :         }
     358             :         {
     359           0 :           (void) fprintf(stdout, "%s", prepareHelp().getStr());
     360           0 :           return false;
     361             :         }
     362             :         // break; // Unreachable
     363             :       }
     364             :     case 's':
     365             :       {
     366           0 :         if (!setOption("-stdin", option))
     367             :         {
     368           0 :           return badOption("invalid", option);
     369             :         }
     370           0 :         m_stdin = true;
     371           0 :         break;
     372             :       }
     373             :     default:
     374           0 :       return badOption("unknown", option);
     375             :     }
     376         329 :   }
     377         329 :   return true;
     378             : }
     379             : 
     380           0 : OString Options::prepareHelp()
     381             : {
     382           0 :     OString help("\nusing: ");
     383           0 :     help += m_program + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
     384           0 :     help += "    <file_n>    = file_n specifies one or more idl files.\n";
     385           0 :     help += "                  Only files with the extension '.idl' are valid.\n";
     386           0 :     help += "    @<filename> = filename specifies the name of a command file.\n";
     387           0 :     help += "    -stdin      = read idl file from standard input.\n";
     388           0 :     help += "  Options:\n";
     389           0 :     help += "    -O<path>    = path specifies the output directory.\n";
     390           0 :     help += "                  The generated output is a registry file with\n";
     391           0 :     help += "                  the same name as the idl input file (or 'stdin'\n";
     392           0 :     help += "                  for -stdin).\n";
     393           0 :     help += "    -M<path>    = path specifies the output directory for deps.\n";
     394           0 :     help += "                  Generate GNU make dependency files with the\n";
     395           0 :     help += "                  same name as the idl input file.\n";
     396           0 :     help += "    -I<path>    = path specifies a directory where include\n";
     397           0 :     help += "                  files will be searched by the preprocessor.\n";
     398           0 :     help += "                  Multiple directories can be combined with ';'.\n";
     399           0 :     help += "    -D<name>    = name defines a macro for the preprocessor.\n";
     400           0 :     help += "    -C          = generate complete type information, including\n";
     401           0 :     help += "                  documentation.\n";
     402           0 :     help += "    -cid        = check if identifiers fulfill the UNO naming\n";
     403           0 :     help += "                  requirements.\n";
     404           0 :     help += "    -quiet      = no output.\n";
     405           0 :     help += "    -verbose    = verbose output.\n";
     406           0 :     help += "    -w          = display warning messages.\n";
     407           0 :     help += "    -we         = treat warnings as errors.\n";
     408           0 :     help += "    -h|-?       = print this help message and exit.\n\n";
     409           0 :     help += prepareVersion();
     410             : 
     411           0 :     return help;
     412             : }
     413             : 
     414         185 : OString Options::prepareVersion()
     415             : {
     416         185 :     OString version(m_program);
     417         185 :     version += " Version 1.1\n\n";
     418         185 :     return version;
     419             : }
     420             : 
     421             : 
     422        2959 : bool Options::isValid(const OString& option)
     423             : {
     424        2959 :     return (m_options.count(option) > 0);
     425             : }
     426             : 
     427         329 : const OString& Options::getOption(const OString& option)
     428             :     throw( IllegalArgument )
     429             : {
     430         329 :     if (!isValid(option))
     431             :     {
     432           0 :         throw IllegalArgument("Option is not valid or currently not set.");
     433             :     }
     434         329 :     return m_options[option];
     435             : }
     436             : 
     437             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.11