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