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 #include 	<stdio.h>
24 #include	<string.h>
25 
26 #include	"rdboptions.hxx"
27 
28 using namespace rtl;
29 
initOptions(int ac,char * av[],sal_Bool bCmdFile)30 sal_Bool RdbOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
31 	throw( IllegalArgument )
32 {
33 	sal_Bool 	ret = sal_True;
34 	sal_uInt16	i=0;
35 
36 	if (!bCmdFile)
37 	{
38 		bCmdFile = sal_True;
39 
40 		m_program = av[0];
41 
42 		if (ac < 2)
43 		{
44 			fprintf(stderr, "%s", prepareHelp().getStr());
45 			ret = sal_False;
46 		}
47 
48 		i = 1;
49 	} else
50 	{
51 		i = 0;
52 	}
53 
54 	char	*s=NULL;
55 	for (; i < ac; i++)
56 	{
57 		if (av[i][0] == '-')
58 		{
59 			switch (av[i][1])
60 			{
61 				case 'O':
62 					if (av[i][2] == '\0')
63 					{
64 						if (i < ac - 1 && av[i+1][0] != '-')
65 						{
66 							i++;
67 							s = av[i];
68 						} else
69 						{
70 							OString tmp("'-O', please check");
71 							if (i <= ac - 1)
72 							{
73 								tmp += " your input '" + OString(av[i+1]) + "'";
74 							}
75 
76 							throw IllegalArgument(tmp);
77 						}
78 					} else
79 					{
80 						s = av[i] + 2;
81 					}
82 
83 					m_options["-O"] = OString(s);
84 					break;
85 				case 'X':
86 					if (av[i][2] == '\0')
87 					{
88 						if (i < ac - 1 && av[i+1][0] != '-')
89 						{
90 							i++;
91 							s = av[i];
92 						} else
93 						{
94 							OString tmp("'-X', please check");
95 							if (i <= ac - 1)
96 							{
97 								tmp += " your input '" + OString(av[i+1]) + "'";
98 							}
99 
100 							throw IllegalArgument(tmp);
101 						}
102 					} else
103 					{
104 						s = av[i] + 2;
105 					}
106 
107 					m_options["-X"] = OString(s);
108 					break;
109 				case 'R':
110 					if (av[i][2] == '\0')
111 					{
112 						if (i < ac - 1 && av[i+1][0] != '-')
113 						{
114 							i++;
115 							s = av[i];
116 						} else
117 						{
118 							OString tmp("'-R', please check");
119 							if (i <= ac - 1)
120 							{
121 								tmp += " your input '" + OString(av[i+1]) + "'";
122 							}
123 
124 							throw IllegalArgument(tmp);
125 						}
126 					} else
127 					{
128 						s = av[i] + 2;
129 					}
130 
131 					m_options["-R"] = OString(s);
132 					break;
133 				case 'B':
134 					if (av[i][2] == '\0')
135 					{
136 						if (i < ac - 1 && av[i+1][0] != '-')
137 						{
138 							i++;
139 							s = av[i];
140 						} else
141 						{
142 							OString tmp("'-B', please check");
143 							if (i <= ac - 1)
144 							{
145 								tmp += " your input '" + OString(av[i+1]) + "'";
146 							}
147 
148 							throw IllegalArgument(tmp);
149 						}
150 					} else
151 					{
152 						s = av[i] + 2;
153 					}
154 
155 					m_options["-B"] = OString(s);
156 					break;
157 				case 'b':
158 					if (av[i][2] == '\0')
159 					{
160 						if (i < ac - 1 && av[i+1][0] != '-')
161 						{
162 							i++;
163 							s = av[i];
164 						} else
165 						{
166 							OString tmp("'-b', please check");
167 							if (i <= ac - 1)
168 							{
169 								tmp += " your input '" + OString(av[i+1]) + "'";
170 							}
171 
172 							throw IllegalArgument(tmp);
173 						}
174 					} else
175 					{
176 						s = av[i] + 2;
177 					}
178 
179 					m_options["-b"] = OString(s);
180 					break;
181 				case 'T':
182 					if (av[i][2] == '\0')
183 					{
184 						if (i < ac - 1 && av[i+1][0] != '-')
185 						{
186 							i++;
187 							s = av[i];
188 						} else
189 						{
190 							OString tmp("'-T', please check");
191 							if (i <= ac - 1)
192 							{
193 								tmp += " your input '" + OString(av[i+1]) + "'";
194 							}
195 
196 							throw IllegalArgument(tmp);
197 						}
198 					} else
199 					{
200 						s = av[i] + 2;
201 					}
202 
203 					if (m_options.count("-T") > 0)
204 					{
205 						OString tmp(m_options["-T"]);
206 						tmp = tmp + ";" + s;
207 						m_options["-T"] = tmp;
208 					} else
209 					{
210 						m_options["-T"] = OString(s);
211 					}
212 					break;
213 				case 'F':
214 					if (av[i][2] == 'T')
215 					{
216 						if (av[i][3] == '\0')
217 						{
218 							if (i < ac - 1 && av[i+1][0] != '-')
219 							{
220 								i++;
221 								s = av[i];
222 							} else
223 							{
224 								OString tmp("'-FT', please check");
225 								if (i <= ac - 1)
226 								{
227 									tmp += " your input '" + OString(av[i+1]) + "'";
228 								}
229 
230 								throw IllegalArgument(tmp);
231 							}
232 						} else
233 						{
234 							s = av[i] + 3;
235 						}
236 
237 						if (m_options.count("-FT") > 0)
238 						{
239 							OString tmp(m_options["-FT"]);
240 							tmp = tmp + ";" + s;
241 							m_options["-FT"] = tmp;
242 						} else
243 						{
244 							m_options["-FT"] = OString(s);
245 						}
246 					} else
247 					{
248 						if (av[i][2] == '\0')
249 						{
250 							if (i < ac - 1 && av[i+1][0] != '-')
251 							{
252 								i++;
253 								s = av[i];
254 							} else
255 							{
256 								OString tmp("'-F', please check");
257 								if (i <= ac - 1)
258 								{
259 									tmp += " your input '" + OString(av[i+1]) + "'";
260 								}
261 
262 								throw IllegalArgument(tmp);
263 							}
264 						} else
265 						{
266 							s = av[i] + 2;
267 						}
268 
269 						m_options["-F"] = OString(s);
270 					}
271 					break;
272 				case 'L':
273 					if (av[i][2] != '\0')
274 					{
275 						OString tmp("'-L', please check");
276 						if (i <= ac - 1)
277 						{
278 							tmp += " your input '" + OString(av[i+1]) + "'";
279 						}
280 
281 						throw IllegalArgument(tmp);
282 					}
283 
284 					m_options["-L"] = OString();
285 					m_generateTypeList = sal_True;
286 					break;
287 				default:
288 					throw IllegalArgument("the option is unknown" + OString(av[i]));
289 			}
290 		} else
291 		{
292 			if (av[i][0] == '@')
293 			{
294 				FILE* cmdFile = fopen(av[i]+1, "r");
295 		  		if( cmdFile == NULL )
296       			{
297 					fprintf(stderr, "%s", prepareHelp().getStr());
298 					ret = sal_False;
299 				} else
300 				{
301 					int rargc=0;
302 					char* rargv[512];
303 					char  buffer[512];
304 
305 					while ( fscanf(cmdFile, "%s", buffer) != EOF )
306 					{
307 						rargv[rargc]= strdup(buffer);
308 						rargc++;
309 					}
310 					fclose(cmdFile);
311 
312 					ret = initOptions(rargc, rargv, bCmdFile);
313 
314 					for (long j=0; j < rargc; j++)
315 					{
316 						free(rargv[j]);
317 					}
318 				}
319 			} else
320 			{
321 				m_inputFiles.push_back(av[i]);
322 			}
323 		}
324 	}
325 
326 	return ret;
327 }
328 
prepareHelp()329 OString	RdbOptions::prepareHelp()
330 {
331 	OString help("\nusing: ");
332 	help += m_program + " [-options] (-R<regname> | file_1 [... file_n])\n";
333     help += "The rdbmaker supports 2 modes:\n";
334     help += " 1. using the internal UNO type description manager -> use -R<regname>\n"
335             "    where regname specifies the type library used by the UNO type description manager\n"
336             "    after UNO is bootstrapped. This option disables the use of any other type libraries.\n"
337             "    The tpye library must be a valid product type library which means that all types are\n"
338             "    stored under the global base node UCR (Uno Core Reflection data).\n";
339     help += " 2. using one or more type library files -> use file_1 ... file_n\n"
340             "    file_1 .. file_n specifies one or more valid type library files which are used to\n"
341             "    find the needed type information. The used type libraries have to support the same base\n"
342             "    node (-B option).\n";
343     help += "Options:\n";
344 	help += "    -O<filename> = filename specifies the name of the generated registry\n";
345 	help += "                   or text file.\n";
346 	help += "    -L           = specifies that only a text file is generated with the\n";
347 	help += "                   names of the specified types and their dependencies.\n";
348 	help += "                   Default is that a registry file will be created\n";
349 //	help += "    -X<xmlfile>  = xmlfile specifies the name of an xml description where\n";
350 //	help += "                   all types are specified which will be generated.\n";
351 	help += "    -T<name>     = name specifies a type or a list of types. The output for\n";
352 	help += "      [t1;...]     this type is generated.\n";
353 	help += "                   Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
354 	help += "    -FT<name>    = name specifies a type or a list of types. For this types\n";
355 	help += "      [t1;...]     nothing will be generated.\n";
356 	help += "     |F<file>    = file specifies an text file. For the specified types in\n" ;
357 	help += "                   this file nothing will be generated.\n";
358 	help += "    -B<name>     = name specifies the base node. All types are searched under\n";
359 	help += "                   this node. Default is the root '/' of the registry files.\n";
360 	help += "                   This option takes effect using run mode 2 only.\n";
361 	help += "    -b<name>     = name specifies the base node of the output registry. All\n";
362 	help += "                   types will be generated under this node. Default is the\n";
363 	help += "                   root '/' of the registry file.\n";
364 	help += prepareVersion();
365 
366 	return help;
367 }
368 
prepareVersion()369 OString	RdbOptions::prepareVersion()
370 {
371 	OString version("\nSun Microsystems (R) ");
372 	version += m_program + " Version 2.0\n\n";
373 
374 	return version;
375 }
376 
377 
378