xref: /AOO41X/main/idlc/source/options.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_idlc.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "idlc/options.hxx"
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include "osl/diagnose.h"
34*cdf0e10cSrcweir #include "rtl/string.hxx"
35*cdf0e10cSrcweir #include "rtl/strbuf.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <stdio.h>
38*cdf0e10cSrcweir #include <string.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir using rtl::OString;
41*cdf0e10cSrcweir using rtl::OStringBuffer;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #ifdef SAL_UNX
44*cdf0e10cSrcweir #define SEPARATOR '/'
45*cdf0e10cSrcweir #else
46*cdf0e10cSrcweir #define SEPARATOR '\\'
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir Options::Options(char const * progname)
50*cdf0e10cSrcweir   : m_program(progname), m_stdin(false), m_verbose(false), m_quiet(false)
51*cdf0e10cSrcweir {
52*cdf0e10cSrcweir }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir Options::~Options()
55*cdf0e10cSrcweir {
56*cdf0e10cSrcweir }
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir // static
59*cdf0e10cSrcweir bool Options::checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len)
60*cdf0e10cSrcweir {
61*cdf0e10cSrcweir   bool result = ((arg != 0) && (len > 0));
62*cdf0e10cSrcweir   OSL_PRECOND(result, "idlc::Options::checkArgument(): invalid arguments");
63*cdf0e10cSrcweir   if (result)
64*cdf0e10cSrcweir   {
65*cdf0e10cSrcweir     switch(arg[0])
66*cdf0e10cSrcweir     {
67*cdf0e10cSrcweir     case '@':
68*cdf0e10cSrcweir       if ((result = (len > 1)) == true)
69*cdf0e10cSrcweir       {
70*cdf0e10cSrcweir         // "@<cmdfile>"
71*cdf0e10cSrcweir         result = Options::checkCommandFile (rArgs, &(arg[1]));
72*cdf0e10cSrcweir       }
73*cdf0e10cSrcweir       break;
74*cdf0e10cSrcweir     case '-':
75*cdf0e10cSrcweir       if ((result = (len > 1)) == true)
76*cdf0e10cSrcweir       {
77*cdf0e10cSrcweir         // "-<option>"
78*cdf0e10cSrcweir         switch (arg[1])
79*cdf0e10cSrcweir         {
80*cdf0e10cSrcweir         case 'O':
81*cdf0e10cSrcweir         case 'I':
82*cdf0e10cSrcweir         case 'D':
83*cdf0e10cSrcweir           {
84*cdf0e10cSrcweir             // "-<option>[<param>]
85*cdf0e10cSrcweir             std::string option(&(arg[0]), 2);
86*cdf0e10cSrcweir             rArgs.push_back(option);
87*cdf0e10cSrcweir             if (len > 2)
88*cdf0e10cSrcweir             {
89*cdf0e10cSrcweir               // "-<option><param>"
90*cdf0e10cSrcweir               std::string param(&(arg[2]), len - 2);
91*cdf0e10cSrcweir               rArgs.push_back(param);
92*cdf0e10cSrcweir             }
93*cdf0e10cSrcweir             break;
94*cdf0e10cSrcweir           }
95*cdf0e10cSrcweir         default:
96*cdf0e10cSrcweir           // "-<option>" ([long] option, w/o param)
97*cdf0e10cSrcweir           rArgs.push_back(std::string(arg, len));
98*cdf0e10cSrcweir           break;
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir       }
101*cdf0e10cSrcweir       break;
102*cdf0e10cSrcweir     default:
103*cdf0e10cSrcweir       // "<param>"
104*cdf0e10cSrcweir       rArgs.push_back(std::string(arg, len));
105*cdf0e10cSrcweir       break;
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir   }
108*cdf0e10cSrcweir   return (result);
109*cdf0e10cSrcweir }
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir // static
112*cdf0e10cSrcweir bool Options::checkCommandFile (std::vector< std::string > & rArgs, char const * filename)
113*cdf0e10cSrcweir {
114*cdf0e10cSrcweir     FILE * fp = fopen(filename, "r");
115*cdf0e10cSrcweir     if (fp == 0)
116*cdf0e10cSrcweir     {
117*cdf0e10cSrcweir         fprintf(stderr, "ERROR: can't open command file \"%s\"\n", filename);
118*cdf0e10cSrcweir         return (false);
119*cdf0e10cSrcweir     }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir     std::string buffer;
122*cdf0e10cSrcweir     buffer.reserve(256);
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir     bool quoted = false;
125*cdf0e10cSrcweir     int c = EOF;
126*cdf0e10cSrcweir     while ((c = fgetc(fp)) != EOF)
127*cdf0e10cSrcweir     {
128*cdf0e10cSrcweir         switch(c)
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir         case '\"':
131*cdf0e10cSrcweir           quoted = !quoted;
132*cdf0e10cSrcweir           break;
133*cdf0e10cSrcweir         case ' ':
134*cdf0e10cSrcweir         case '\t':
135*cdf0e10cSrcweir         case '\r':
136*cdf0e10cSrcweir         case '\n':
137*cdf0e10cSrcweir           if (!quoted)
138*cdf0e10cSrcweir           {
139*cdf0e10cSrcweir               if (!buffer.empty())
140*cdf0e10cSrcweir               {
141*cdf0e10cSrcweir                   // append current argument.
142*cdf0e10cSrcweir                   if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
143*cdf0e10cSrcweir                   {
144*cdf0e10cSrcweir                       (void) fclose(fp);
145*cdf0e10cSrcweir                       return (false);
146*cdf0e10cSrcweir                   }
147*cdf0e10cSrcweir                   buffer.clear();
148*cdf0e10cSrcweir               }
149*cdf0e10cSrcweir               break;
150*cdf0e10cSrcweir           }
151*cdf0e10cSrcweir         default:
152*cdf0e10cSrcweir           // quoted white-space fall through
153*cdf0e10cSrcweir           buffer.push_back(sal::static_int_cast<char>(c));
154*cdf0e10cSrcweir           break;
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir     }
157*cdf0e10cSrcweir     if (!buffer.empty())
158*cdf0e10cSrcweir     {
159*cdf0e10cSrcweir         // append unterminated argument.
160*cdf0e10cSrcweir         if (!Options::checkArgument(rArgs, buffer.c_str(), buffer.size()))
161*cdf0e10cSrcweir         {
162*cdf0e10cSrcweir             (void) fclose(fp);
163*cdf0e10cSrcweir             return (false);
164*cdf0e10cSrcweir         }
165*cdf0e10cSrcweir         buffer.clear();
166*cdf0e10cSrcweir     }
167*cdf0e10cSrcweir     return (fclose(fp) == 0);
168*cdf0e10cSrcweir }
169*cdf0e10cSrcweir 
170*cdf0e10cSrcweir bool Options::badOption(char const * reason, std::string const & rArg) throw(IllegalArgument)
171*cdf0e10cSrcweir {
172*cdf0e10cSrcweir   OStringBuffer message;
173*cdf0e10cSrcweir   if (reason != 0)
174*cdf0e10cSrcweir   {
175*cdf0e10cSrcweir     message.append(reason); message.append(" option '"); message.append(rArg.c_str()); message.append("'");
176*cdf0e10cSrcweir     throw IllegalArgument(message.makeStringAndClear());
177*cdf0e10cSrcweir   }
178*cdf0e10cSrcweir   return false;
179*cdf0e10cSrcweir }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir bool Options::setOption(char const * option, std::string const & rArg)
182*cdf0e10cSrcweir {
183*cdf0e10cSrcweir   bool result = (0 == strcmp(option, rArg.c_str()));
184*cdf0e10cSrcweir   if (result)
185*cdf0e10cSrcweir     m_options[rArg.c_str()] = OString(rArg.c_str(), rArg.size());
186*cdf0e10cSrcweir   return (result);
187*cdf0e10cSrcweir }
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir bool Options::initOptions(std::vector< std::string > & rArgs) throw(IllegalArgument)
190*cdf0e10cSrcweir {
191*cdf0e10cSrcweir   std::vector< std::string >::const_iterator first = rArgs.begin(), last = rArgs.end();
192*cdf0e10cSrcweir   for (; first != last; ++first)
193*cdf0e10cSrcweir   {
194*cdf0e10cSrcweir     if ((*first)[0] != '-')
195*cdf0e10cSrcweir     {
196*cdf0e10cSrcweir       OString filename((*first).c_str(), (*first).size());
197*cdf0e10cSrcweir       OString tmp(filename.toAsciiLowerCase());
198*cdf0e10cSrcweir       if (tmp.lastIndexOf(".idl") != (tmp.getLength() - 4))
199*cdf0e10cSrcweir       {
200*cdf0e10cSrcweir         throw IllegalArgument("'" + filename + "' is not a valid input file, only '*.idl' files will be accepted");
201*cdf0e10cSrcweir       }
202*cdf0e10cSrcweir       m_inputFiles.push_back(filename);
203*cdf0e10cSrcweir       continue;
204*cdf0e10cSrcweir     }
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir     std::string const option(*first);
207*cdf0e10cSrcweir     switch((*first)[1])
208*cdf0e10cSrcweir     {
209*cdf0e10cSrcweir     case 'O':
210*cdf0e10cSrcweir       {
211*cdf0e10cSrcweir         if (!((++first != last) && ((*first)[0] != '-')))
212*cdf0e10cSrcweir         {
213*cdf0e10cSrcweir           return badOption("invalid", option);
214*cdf0e10cSrcweir         }
215*cdf0e10cSrcweir         OString param((*first).c_str(), (*first).size());
216*cdf0e10cSrcweir         m_options["-O"] = param;
217*cdf0e10cSrcweir         break;
218*cdf0e10cSrcweir       }
219*cdf0e10cSrcweir     case 'I':
220*cdf0e10cSrcweir       {
221*cdf0e10cSrcweir         if (!((++first != last) && ((*first)[0] != '-')))
222*cdf0e10cSrcweir         {
223*cdf0e10cSrcweir           return badOption("invalid", option);
224*cdf0e10cSrcweir         }
225*cdf0e10cSrcweir         OString param((*first).c_str(), (*first).size());
226*cdf0e10cSrcweir         {
227*cdf0e10cSrcweir           // quote param token(s).
228*cdf0e10cSrcweir           OStringBuffer buffer;
229*cdf0e10cSrcweir           sal_Int32 k = 0;
230*cdf0e10cSrcweir           do
231*cdf0e10cSrcweir           {
232*cdf0e10cSrcweir             OStringBuffer token; token.append("-I\""); token.append(param.getToken(0, ';', k)); token.append("\"");
233*cdf0e10cSrcweir             if (buffer.getLength() > 0)
234*cdf0e10cSrcweir               buffer.append(' ');
235*cdf0e10cSrcweir             buffer.append(token);
236*cdf0e10cSrcweir           } while (k != -1);
237*cdf0e10cSrcweir           param = buffer.makeStringAndClear();
238*cdf0e10cSrcweir         }
239*cdf0e10cSrcweir         if (m_options.count("-I") > 0)
240*cdf0e10cSrcweir         {
241*cdf0e10cSrcweir           // append param.
242*cdf0e10cSrcweir           OStringBuffer buffer(m_options["-I"]);
243*cdf0e10cSrcweir           buffer.append(' '); buffer.append(param);
244*cdf0e10cSrcweir           param = buffer.makeStringAndClear();
245*cdf0e10cSrcweir         }
246*cdf0e10cSrcweir         m_options["-I"] = param;
247*cdf0e10cSrcweir         break;
248*cdf0e10cSrcweir       }
249*cdf0e10cSrcweir     case 'D':
250*cdf0e10cSrcweir       {
251*cdf0e10cSrcweir         if (!((++first != last) && ((*first)[0] != '-')))
252*cdf0e10cSrcweir         {
253*cdf0e10cSrcweir           return badOption("invalid", option);
254*cdf0e10cSrcweir         }
255*cdf0e10cSrcweir         OString param("-D"); param += OString((*first).c_str(), (*first).size());
256*cdf0e10cSrcweir         if (m_options.count("-D") > 0)
257*cdf0e10cSrcweir         {
258*cdf0e10cSrcweir           OStringBuffer buffer(m_options["-D"]);
259*cdf0e10cSrcweir           buffer.append(' '); buffer.append(param);
260*cdf0e10cSrcweir           param = buffer.makeStringAndClear();
261*cdf0e10cSrcweir         }
262*cdf0e10cSrcweir         m_options["-D"] = param;
263*cdf0e10cSrcweir         break;
264*cdf0e10cSrcweir       }
265*cdf0e10cSrcweir     case 'C':
266*cdf0e10cSrcweir       {
267*cdf0e10cSrcweir         if (!setOption("-C", option))
268*cdf0e10cSrcweir         {
269*cdf0e10cSrcweir           return badOption("invalid", option);
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir         break;
272*cdf0e10cSrcweir       }
273*cdf0e10cSrcweir     case 'c':
274*cdf0e10cSrcweir       {
275*cdf0e10cSrcweir         if (!setOption("-cid", option))
276*cdf0e10cSrcweir         {
277*cdf0e10cSrcweir           return badOption("invalid", option);
278*cdf0e10cSrcweir         }
279*cdf0e10cSrcweir         break;
280*cdf0e10cSrcweir       }
281*cdf0e10cSrcweir     case 'q':
282*cdf0e10cSrcweir       {
283*cdf0e10cSrcweir         if (!setOption("-quiet", option))
284*cdf0e10cSrcweir         {
285*cdf0e10cSrcweir           return badOption("invalid", option);
286*cdf0e10cSrcweir         }
287*cdf0e10cSrcweir         m_quiet = true;
288*cdf0e10cSrcweir         break;
289*cdf0e10cSrcweir       }
290*cdf0e10cSrcweir     case 'v':
291*cdf0e10cSrcweir       {
292*cdf0e10cSrcweir         if (!setOption("-verbose", option))
293*cdf0e10cSrcweir         {
294*cdf0e10cSrcweir           return badOption("invalid", option);
295*cdf0e10cSrcweir         }
296*cdf0e10cSrcweir         m_verbose = true;
297*cdf0e10cSrcweir         break;
298*cdf0e10cSrcweir       }
299*cdf0e10cSrcweir     case 'w':
300*cdf0e10cSrcweir       {
301*cdf0e10cSrcweir         if (!(setOption("-w", option) || setOption("-we", option)))
302*cdf0e10cSrcweir         {
303*cdf0e10cSrcweir           return badOption("invalid", option);
304*cdf0e10cSrcweir         }
305*cdf0e10cSrcweir         break;
306*cdf0e10cSrcweir       }
307*cdf0e10cSrcweir     case 'h':
308*cdf0e10cSrcweir     case '?':
309*cdf0e10cSrcweir       {
310*cdf0e10cSrcweir         if (!(setOption("-h", option) || setOption("-?", option)))
311*cdf0e10cSrcweir         {
312*cdf0e10cSrcweir           return badOption("invalid", option);
313*cdf0e10cSrcweir         }
314*cdf0e10cSrcweir         {
315*cdf0e10cSrcweir           (void) fprintf(stdout, "%s", prepareHelp().getStr());
316*cdf0e10cSrcweir           return (false);
317*cdf0e10cSrcweir         }
318*cdf0e10cSrcweir         // break; // Unreachable
319*cdf0e10cSrcweir       }
320*cdf0e10cSrcweir     case 's':
321*cdf0e10cSrcweir       {
322*cdf0e10cSrcweir         if (!setOption("-stdin", option))
323*cdf0e10cSrcweir         {
324*cdf0e10cSrcweir           return badOption("invalid", option);
325*cdf0e10cSrcweir         }
326*cdf0e10cSrcweir         m_stdin = true;
327*cdf0e10cSrcweir         break;
328*cdf0e10cSrcweir       }
329*cdf0e10cSrcweir     default:
330*cdf0e10cSrcweir       return badOption("unknown", option);
331*cdf0e10cSrcweir     }
332*cdf0e10cSrcweir   }
333*cdf0e10cSrcweir   return (true);
334*cdf0e10cSrcweir }
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir OString	Options::prepareHelp()
337*cdf0e10cSrcweir {
338*cdf0e10cSrcweir 	OString help("\nusing: ");
339*cdf0e10cSrcweir 	help += m_program
340*cdf0e10cSrcweir         + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
341*cdf0e10cSrcweir 	help += "    <file_n>    = file_n specifies one or more idl files.\n";
342*cdf0e10cSrcweir 	help += "                  Only files with the extension '.idl' are valid.\n";
343*cdf0e10cSrcweir 	help += "    @<filename> = filename specifies the name of a command file.\n";
344*cdf0e10cSrcweir     help += "    -stdin      = read idl file from standard input.\n";
345*cdf0e10cSrcweir 	help += "  Options:\n";
346*cdf0e10cSrcweir 	help += "    -O<path>    = path specifies the output directory.\n";
347*cdf0e10cSrcweir 	help += "                  The generated output is a registry file with\n";
348*cdf0e10cSrcweir 	help += "                  the same name as the idl input file (or 'stdin'\n";
349*cdf0e10cSrcweir     help += "                  for -stdin).\n";
350*cdf0e10cSrcweir 	help += "    -I<path>    = path specifies a directory where include\n";
351*cdf0e10cSrcweir 	help += "                  files will be searched by the preprocessor.\n";
352*cdf0e10cSrcweir 	help += "                  Multiple directories can be combined with ';'.\n";
353*cdf0e10cSrcweir 	help += "    -D<name>    = name defines a macro for the preprocessor.\n";
354*cdf0e10cSrcweir 	help += "    -C          = generate complete type information, including\n";
355*cdf0e10cSrcweir 	help += "                  documentation.\n";
356*cdf0e10cSrcweir 	help += "    -cid        = check if identifiers fulfill the UNO naming\n";
357*cdf0e10cSrcweir     help += "                  requirements.\n";
358*cdf0e10cSrcweir 	help += "    -w          = display warning messages.\n";
359*cdf0e10cSrcweir 	help += "    -we         = treat warnings as errors.\n";
360*cdf0e10cSrcweir 	help += "    -h|-?       = print this help message and exit.\n\n";
361*cdf0e10cSrcweir 	help += prepareVersion();
362*cdf0e10cSrcweir 
363*cdf0e10cSrcweir 	return help;
364*cdf0e10cSrcweir }
365*cdf0e10cSrcweir 
366*cdf0e10cSrcweir OString	Options::prepareVersion()
367*cdf0e10cSrcweir {
368*cdf0e10cSrcweir 	OString version(m_program);
369*cdf0e10cSrcweir 	version += " Version 1.1\n\n";
370*cdf0e10cSrcweir 	return version;
371*cdf0e10cSrcweir }
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir const OString& Options::getProgramName() const
374*cdf0e10cSrcweir {
375*cdf0e10cSrcweir 	return m_program;
376*cdf0e10cSrcweir }
377*cdf0e10cSrcweir 
378*cdf0e10cSrcweir bool Options::isValid(const OString& option)
379*cdf0e10cSrcweir {
380*cdf0e10cSrcweir 	return (m_options.count(option) > 0);
381*cdf0e10cSrcweir }
382*cdf0e10cSrcweir 
383*cdf0e10cSrcweir const OString& Options::getOption(const OString& option)
384*cdf0e10cSrcweir 	throw( IllegalArgument )
385*cdf0e10cSrcweir {
386*cdf0e10cSrcweir 	if (!isValid(option))
387*cdf0e10cSrcweir 	{
388*cdf0e10cSrcweir 		throw IllegalArgument("Option is not valid or currently not set.");
389*cdf0e10cSrcweir 	}
390*cdf0e10cSrcweir     return m_options[option];
391*cdf0e10cSrcweir }
392*cdf0e10cSrcweir /* vi:set tabstop=4 shiftwidth=4 expandtab: */
393