Update ooo320-m1
[ooovba.git] / idlc / source / options.cxx
blob35c5f149db046b1ccb3f56c1171780536ddc3603
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: options.cxx,v $
10 * $Revision: 1.18 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_idlc.hxx"
34 #include <stdio.h>
35 #include /*MSVC trouble: <cstring>*/ <string.h>
36 #include <idlc/options.hxx>
38 using namespace rtl;
40 Options::Options(): m_stdin(false), m_verbose(false), m_quiet(false)
44 Options::~Options()
48 sal_Bool Options::initOptions(int ac, char* av[], sal_Bool bCmdFile)
49 throw( IllegalArgument )
51 sal_Bool ret = sal_True;
52 sal_uInt16 j=0;
54 if (!bCmdFile)
56 bCmdFile = sal_True;
58 m_program = av[0];
60 if (ac < 2)
62 fprintf(stderr, "%s", prepareHelp().getStr());
63 ret = sal_False;
66 j = 1;
67 } else
69 j = 0;
72 char *s=NULL;
73 for (; j < ac; j++)
75 if (av[j][0] == '-')
77 switch (av[j][1])
79 case 'O':
80 if (av[j][2] == '\0')
82 if (j < ac - 1 && av[j+1][0] != '-')
84 j++;
85 s = av[j];
86 } else
88 OString tmp("'-O', please check");
89 if (j <= ac - 1)
91 tmp += " your input '" + OString(av[j+1]) + "'";
94 throw IllegalArgument(tmp);
96 } else
98 s = av[j] + 2;
101 m_options["-O"] = OString(s);
102 break;
103 case 'I':
105 if (av[j][2] == '\0')
107 if (j < ac - 1 && av[j+1][0] != '-')
109 j++;
110 s = av[j];
111 } else
113 OString tmp("'-I', please check");
114 if (j <= ac - 1)
116 tmp += " your input '" + OString(av[j+1]) + "'";
119 throw IllegalArgument(tmp);
121 } else
123 s = av[j] + 2;
126 OString inc(s);
127 if ( inc.indexOf(';') > 0 )
129 OString tmp(s);
130 sal_Int32 nIndex = 0;
131 inc = OString();
132 do inc = inc + " -I\"" + tmp.getToken( 0, ';', nIndex ) +"\""; while( nIndex != -1 );
133 } else
134 inc = OString("-I\"") + s + "\"";
136 if (m_options.count("-I") > 0)
138 OString tmp(m_options["-I"]);
139 tmp = tmp + " " + inc;
140 m_options["-I"] = tmp;
141 } else
143 m_options["-I"] = inc;
146 break;
147 case 'D':
148 if (av[j][2] == '\0')
150 if (j < ac - 1 && av[j+1][0] != '-')
152 j++;
153 s = av[j];
154 } else
156 OString tmp("'-D', please check");
157 if (j <= ac - 1)
159 tmp += " your input '" + OString(av[j+1]) + "'";
162 throw IllegalArgument(tmp);
164 } else
166 s = av[j];
169 if (m_options.count("-D") > 0)
171 OString tmp(m_options["-D"]);
172 tmp = tmp + " " + s;
173 m_options["-D"] = tmp;
174 } else
175 m_options["-D"] = OString(s);
176 break;
177 case 'C':
178 if (av[j][2] != '\0')
180 throw IllegalArgument(OString(av[j]) + ", please check your input");
182 if (m_options.count("-C") == 0)
183 m_options["-C"] = OString(av[j]);
184 break;
185 case 'c':
186 if (av[j][2] == 'i' && av[j][3] == 'd' && av[j][4] == '\0')
188 if (m_options.count("-cid") == 0)
189 m_options["-cid"] = OString(av[j]);
190 } else
191 throw IllegalArgument(OString(av[j]) + ", please check your input");
192 break;
193 case 'v':
194 if ( 0 == strcmp( &av[j][1], "verbose" ) )
196 m_verbose = true;
198 else
199 throw IllegalArgument(OString(av[j]) + ", please check your input");
200 break;
201 case 'q':
202 if ( 0 == strcmp( &av[j][1], "quiet" ) )
204 m_quiet = true;
206 else
207 throw IllegalArgument(OString(av[j]) + ", please check your input");
208 break;
209 case 'w':
210 if (av[j][2] == 'e' && av[j][3] == '\0') {
211 if (m_options.count("-we") == 0)
212 m_options["-we"] = OString(av[j]);
213 } else {
214 if (av[j][2] == '\0') {
215 if (m_options.count("-w") == 0)
216 m_options["-w"] = OString(av[j]);
217 } else
218 throw IllegalArgument(OString(av[j]) + ", please check your input");
220 break;
221 case 'h':
222 case '?':
223 if (av[j][2] != '\0')
225 throw IllegalArgument(OString(av[j]) + ", please check your input");
226 } else
228 fprintf(stdout, "%s", prepareHelp().getStr());
229 exit(0);
231 case 's':
232 if (/*MSVC trouble: std::*/strcmp(&av[j][2], "tdin") == 0)
234 m_stdin = true;
235 break;
237 // fall through
238 default:
239 throw IllegalArgument("the option is unknown" + OString(av[j]));
241 } else
243 if (av[j][0] == '@')
245 FILE* cmdFile = fopen(av[j]+1, "r");
246 if( cmdFile == NULL )
248 fprintf(stderr, "%s", prepareHelp().getStr());
249 ret = sal_False;
250 } else
252 int rargc=0;
253 char* rargv[512];
254 char buffer[512]="";
256 int i=0;
257 int found = 0;
258 char c;
259 while ( fscanf(cmdFile, "%c", &c) != EOF )
261 if (c=='\"') {
262 if (found) {
263 found=0;
264 } else {
265 found=1;
266 continue;
268 } else {
269 if (c!=13 && c!=10) {
270 if (found || c!=' ') {
271 buffer[i++]=c;
272 continue;
275 if (i==0)
276 continue;
278 buffer[i]='\0';
279 found=0;
280 i=0;
281 rargv[rargc]= strdup(buffer);
282 rargc++;
283 buffer[0]='\0';
285 if (buffer[0] != '\0') {
286 buffer[i]='\0';
287 rargv[rargc]= strdup(buffer);
288 rargc++;
290 fclose(cmdFile);
292 ret = initOptions(rargc, rargv, bCmdFile);
294 long ii = 0;
295 for (ii=0; ii < rargc; ii++)
297 free(rargv[ii]);
300 } else
302 OString name(av[j]);
303 name = name.toAsciiLowerCase();
304 if ( name.lastIndexOf(".idl") != (name.getLength() - 4) )
306 throw IllegalArgument("'" + OString(av[j]) +
307 "' is not a valid input file, only '*.idl' files will be accepted");
309 m_inputFiles.push_back(av[j]);
314 return ret;
317 OString Options::prepareHelp()
319 OString help("\nusing: ");
320 help += m_program
321 + " [-options] <file_1> ... <file_n> | @<filename> | -stdin\n";
322 help += " <file_n> = file_n specifies one or more idl files.\n";
323 help += " Only files with the extension '.idl' are valid.\n";
324 help += " @<filename> = filename specifies the name of a command file.\n";
325 help += " -stdin = read idl file from standard input.\n";
326 help += " Options:\n";
327 help += " -O<path> = path specifies the output directory.\n";
328 help += " The generated output is a registry file with\n";
329 help += " the same name as the idl input file (or 'stdin'\n";
330 help += " for -stdin).\n";
331 help += " -I<path> = path specifies a directory where include\n";
332 help += " files will be searched by the preprocessor.\n";
333 help += " Multiple directories can be combined with ';'.\n";
334 help += " -D<name> = name defines a macro for the preprocessor.\n";
335 help += " -C = generate complete type information, including\n";
336 help += " documentation.\n";
337 help += " -cid = check if identifiers fulfill the UNO naming\n";
338 help += " requirements.\n";
339 help += " -w = display warning messages.\n";
340 help += " -we = treat warnings as errors.\n";
341 help += " -h|-? = print this help message and exit.\n";
342 help += prepareVersion();
344 return help;
347 OString Options::prepareVersion()
349 OString version("\nSun Microsystems (R) ");
350 version += m_program + " Version 1.1\n\n";
351 return version;
354 const OString& Options::getProgramName() const
356 return m_program;
359 sal_Bool Options::isValid(const OString& option)
361 return (m_options.count(option) > 0);
364 const OString Options::getOption(const OString& option)
365 throw( IllegalArgument )
367 if (m_options.count(option) > 0)
369 return m_options[option];
370 } else
372 throw IllegalArgument("Option is not valid or currently not set.");