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 <stdio.h>
21 : #include <string.h>
22 :
23 : #include "cppuoptions.hxx"
24 : #include "osl/thread.h"
25 : #include "osl/process.h"
26 :
27 : using ::rtl::OUString;
28 : using ::rtl::OUStringToOString;
29 : using ::rtl::OString;
30 :
31 : #ifdef SAL_UNX
32 : #define SEPARATOR '/'
33 : #else
34 : #define SEPARATOR '\\'
35 : #endif
36 :
37 20 : sal_Bool CppuOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
38 : throw( IllegalArgument )
39 : {
40 20 : sal_Bool ret = sal_True;
41 20 : sal_uInt16 i=0;
42 :
43 20 : if (!bCmdFile)
44 : {
45 10 : bCmdFile = sal_True;
46 :
47 10 : OString name(av[0]);
48 10 : sal_Int32 index = name.lastIndexOf(SEPARATOR);
49 10 : m_program = name.copy((index > 0 ? index+1 : 0));
50 :
51 10 : if (ac < 2)
52 : {
53 0 : fprintf(stderr, "%s", prepareHelp().getStr());
54 0 : ret = sal_False;
55 : }
56 :
57 10 : i = 1;
58 : }
59 : else
60 : {
61 10 : i = 0;
62 : }
63 :
64 20 : char *s=NULL;
65 90 : for( ; i < ac; i++)
66 : {
67 70 : if (av[i][0] == '-')
68 : {
69 50 : switch (av[i][1])
70 : {
71 : case 'O':
72 10 : if (av[i][2] == '\0')
73 : {
74 0 : if (i < ac - 1 && av[i+1][0] != '-')
75 : {
76 0 : i++;
77 0 : s = av[i];
78 : }
79 : else
80 : {
81 0 : OString tmp("'-O', please check");
82 0 : if (i <= ac - 1)
83 : {
84 0 : tmp += " your input '" + OString(av[i+1]) + "'";
85 : }
86 :
87 0 : throw IllegalArgument(tmp);
88 : }
89 : }
90 : else
91 : {
92 10 : s = av[i] + 2;
93 : }
94 :
95 10 : m_options["-O"] = OString(s);
96 10 : break;
97 : case 'B':
98 10 : if (av[i][2] == '\0')
99 : {
100 0 : if (i < ac - 1 && av[i+1][0] != '-')
101 : {
102 0 : i++;
103 0 : s = av[i];
104 : }
105 : else
106 : {
107 0 : OString tmp("'-B', please check");
108 0 : if (i <= ac - 1)
109 : {
110 0 : tmp += " your input '" + OString(av[i+1]) + "'";
111 : }
112 :
113 0 : throw IllegalArgument(tmp);
114 : }
115 : }
116 : else
117 : {
118 10 : s = av[i] + 2;
119 : }
120 :
121 10 : m_options["-B"] = OString(s);
122 10 : break;
123 : case 'T':
124 0 : if (av[i][2] == '\0')
125 : {
126 0 : if (i < ac - 1 && av[i+1][0] != '-')
127 : {
128 0 : i++;
129 0 : s = av[i];
130 : }
131 : else
132 : {
133 0 : OString tmp("'-T', please check");
134 0 : if (i <= ac - 1)
135 : {
136 0 : tmp += " your input '" + OString(av[i+1]) + "'";
137 : }
138 :
139 0 : throw IllegalArgument(tmp);
140 : }
141 : }
142 : else
143 : {
144 0 : s = av[i] + 2;
145 : }
146 :
147 0 : if (m_options.count("-T") > 0)
148 : {
149 0 : OString tmp(m_options["-T"]);
150 0 : tmp = tmp + ";" + s;
151 0 : m_options["-T"] = tmp;
152 : }
153 : else
154 : {
155 0 : m_options["-T"] = OString(s);
156 : }
157 0 : break;
158 : case 'L':
159 4 : if (av[i][2] != '\0')
160 : {
161 0 : OString tmp("'-L', please check");
162 0 : if (i <= ac - 1)
163 : {
164 0 : tmp += " your input '" + OString(av[i]) + "'";
165 : }
166 :
167 0 : throw IllegalArgument(tmp);
168 : }
169 :
170 4 : if (isValid("-C") || isValid("-CS"))
171 : {
172 0 : OString tmp("'-L' could not be combined with '-C' or '-CS' option");
173 0 : throw IllegalArgument(tmp);
174 : }
175 4 : m_options["-L"] = OString("");
176 4 : break;
177 : case 'C':
178 5 : if (av[i][2] == 'S')
179 : {
180 0 : if (av[i][3] != '\0')
181 : {
182 0 : OString tmp("'-CS', please check");
183 0 : if (i <= ac - 1)
184 : {
185 0 : tmp += " your input '" + OString(av[i]) + "'";
186 : }
187 :
188 0 : throw IllegalArgument(tmp);
189 : }
190 :
191 0 : if (isValid("-L") || isValid("-C"))
192 : {
193 0 : OString tmp("'-CS' could not be combined with '-L' or '-C' option");
194 0 : throw IllegalArgument(tmp);
195 : }
196 0 : m_options["-CS"] = OString("");
197 0 : break;
198 : }
199 5 : else if (av[i][2] != '\0')
200 : {
201 0 : OString tmp("'-C', please check");
202 0 : if (i <= ac - 1)
203 : {
204 0 : tmp += " your input '" + OString(av[i]) + "'";
205 : }
206 :
207 0 : throw IllegalArgument(tmp);
208 : }
209 :
210 5 : if (isValid("-L") || isValid("-CS"))
211 : {
212 0 : OString tmp("'-C' could not be combined with '-L' or '-CS' option");
213 0 : throw IllegalArgument(tmp);
214 : }
215 5 : m_options["-C"] = OString("");
216 5 : break;
217 : case 'G':
218 10 : if (av[i][2] == 'c')
219 : {
220 10 : if (av[i][3] != '\0')
221 : {
222 0 : OString tmp("'-Gc', please check");
223 0 : if (i <= ac - 1)
224 : {
225 0 : tmp += " your input '" + OString(av[i]) + "'";
226 : }
227 :
228 0 : throw IllegalArgument(tmp);
229 : }
230 :
231 10 : m_options["-Gc"] = OString("");
232 10 : break;
233 : }
234 0 : else if (av[i][2] != '\0')
235 : {
236 0 : OString tmp("'-G', please check");
237 0 : if (i <= ac - 1)
238 : {
239 0 : tmp += " your input '" + OString(av[i]) + "'";
240 : }
241 :
242 0 : throw IllegalArgument(tmp);
243 : }
244 :
245 0 : m_options["-G"] = OString("");
246 0 : break;
247 : case 'X': // support for eXtra type rdbs
248 : {
249 11 : if (av[i][2] == '\0')
250 : {
251 0 : if (i < ac - 1 && av[i+1][0] != '-')
252 : {
253 0 : i++;
254 0 : s = av[i];
255 : }
256 : else
257 : {
258 0 : OString tmp("'-X', please check");
259 0 : if (i <= ac - 1)
260 : {
261 0 : tmp += " your input '" + OString(av[i+1]) + "'";
262 : }
263 :
264 0 : throw IllegalArgument(tmp);
265 : }
266 : }
267 : else
268 : {
269 11 : s = av[i] + 2;
270 : }
271 :
272 11 : m_extra_input_files.push_back( s );
273 11 : break;
274 : }
275 :
276 : default:
277 0 : throw IllegalArgument("the option is unknown" + OString(av[i]));
278 : }
279 : } else
280 : {
281 20 : if (av[i][0] == '@')
282 : {
283 10 : FILE* cmdFile = fopen(av[i]+1, "r");
284 10 : if( cmdFile == NULL )
285 : {
286 0 : fprintf(stderr, "%s", prepareHelp().getStr());
287 0 : ret = sal_False;
288 : }
289 : else
290 : {
291 10 : int rargc=0;
292 : char* rargv[512];
293 : char buffer[512];
294 :
295 31 : while ( fscanf(cmdFile, "%s", buffer) != EOF )
296 : {
297 11 : rargv[rargc]= strdup(buffer);
298 11 : rargc++;
299 : }
300 10 : fclose(cmdFile);
301 :
302 10 : ret = initOptions(rargc, rargv, bCmdFile);
303 :
304 21 : for (long j=0; j < rargc; j++)
305 : {
306 11 : free(rargv[j]);
307 : }
308 : }
309 : }
310 : else
311 : {
312 10 : if (bCmdFile)
313 : {
314 10 : m_inputFiles.push_back(av[i]);
315 : }
316 : else
317 : {
318 0 : OUString system_filepath;
319 0 : if (osl_getCommandArg( i-1, &system_filepath.pData )
320 : != osl_Process_E_None)
321 : {
322 : OSL_ASSERT(false);
323 : }
324 0 : m_inputFiles.push_back(OUStringToOString(system_filepath, osl_getThreadTextEncoding()));
325 : }
326 : }
327 : }
328 : }
329 :
330 20 : return ret;
331 : }
332 :
333 0 : OString CppuOptions::prepareHelp()
334 : {
335 0 : OString help("\nusing: ");
336 0 : help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
337 0 : help += " -O<path> = path describes the root directory for the generated output.\n";
338 0 : help += " The output directory tree is generated under this directory.\n";
339 0 : help += " -T<name> = name specifies a type or a list of types. The output for this\n";
340 0 : help += " [t1;...] type is generated. If no '-T' option is specified,\n";
341 0 : help += " then output for all types is generated.\n";
342 0 : help += " Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
343 0 : help += " -B<name> = name specifies the base node. All types are searched under this\n";
344 0 : help += " node. Default is the root '/' of the registry files.\n";
345 0 : help += " -L = UNO type functions are generated lightweight, that means only\n";
346 0 : help += " the name and typeclass are given and everything else is retrieved\n";
347 0 : help += " from the type library dynamically. The default is that UNO type\n";
348 0 : help += " functions provides enough type information for boostrapping C++.\n";
349 0 : help += " '-L' should be the default for external components.\n";
350 0 : help += " -C = UNO type functions are generated comprehensive that means all\n";
351 0 : help += " necessary information is available for bridging the type in UNO.\n";
352 0 : help += " -G = generate only target files which does not exists.\n";
353 0 : help += " -Gc = generate only target files which content will be changed.\n";
354 0 : help += " -X<file> = extra types which will not be taken into account for generation.\n\n";
355 0 : help += prepareVersion();
356 :
357 0 : return help;
358 : }
359 :
360 0 : OString CppuOptions::prepareVersion()
361 : {
362 0 : OString version(m_program);
363 0 : version += " Version 2.0\n\n";
364 0 : return version;
365 : }
366 :
367 :
368 :
369 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|