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