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