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