1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_codemaker.hxx"
30 #include 	<stdio.h>
31 
32 #include	"cunooptions.hxx"
33 
34 using namespace rtl;
35 
36 sal_Bool CunoOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
37 	throw( IllegalArgument )
38 {
39 	sal_Bool 	ret = sal_True;
40 	sal_uInt16	i=0;
41 
42 	if (!bCmdFile)
43 	{
44 		bCmdFile = sal_True;
45 
46 		m_program = av[0];
47 
48 		if (ac < 2)
49 		{
50 			fprintf(stderr, "%s", prepareHelp().getStr());
51 			ret = sal_False;
52 		}
53 
54 		i = 1;
55 	} else
56 	{
57 		i = 0;
58 	}
59 
60 	char	*s=NULL;
61 	for (i; i < ac; i++)
62 	{
63 		if (av[i][0] == '-')
64 		{
65 			switch (av[i][1])
66 			{
67 				case 'O':
68 					if (av[i][2] == 'C')
69 					{
70 						if (av[i][3] == '\0')
71 						{
72 							if (i < ac - 1 && av[i+1][0] != '-')
73 							{
74 								i++;
75 								s = av[i];
76 							} else
77 							{
78 								OString tmp("'-OC', please check");
79 								if (i <= ac - 1)
80 								{
81 									tmp += " your input '" + OString(av[i+1]) + "'";
82 								}
83 
84 								throw IllegalArgument(tmp);
85 							}
86 						} else
87 						{
88 							s = av[i] + 3;
89 						}
90 
91 						m_options["-OC"] = OString(s);
92 						break;
93 					} else
94 					if (av[i][2] == '\0')
95 					{
96 						if (i < ac - 1 && av[i+1][0] != '-')
97 						{
98 							i++;
99 							s = av[i];
100 						} else
101 						{
102 							OString tmp("'-O', please check");
103 							if (i <= ac - 1)
104 							{
105 								tmp += " your input '" + OString(av[i+1]) + "'";
106 							}
107 
108 							throw IllegalArgument(tmp);
109 						}
110 					} else
111 					{
112 						s = av[i] + 2;
113 					}
114 
115 					m_options["-O"] = OString(s);
116 					break;
117 				case 'B':
118 					if (av[i][2] == '\0')
119 					{
120 						if (i < ac - 1 && av[i+1][0] != '-')
121 						{
122 							i++;
123 							s = av[i];
124 						} else
125 						{
126 							OString tmp("'-B', please check");
127 							if (i <= ac - 1)
128 							{
129 								tmp += " your input '" + OString(av[i+1]) + "'";
130 							}
131 
132 							throw IllegalArgument(tmp);
133 						}
134 					} else
135 					{
136 						s = av[i] + 2;
137 					}
138 
139 					m_options["-B"] = OString(s);
140 					break;
141 				case 'T':
142 					if (av[i][2] == '\0')
143 					{
144 						if (i < ac - 1 && av[i+1][0] != '-')
145 						{
146 							i++;
147 							s = av[i];
148 						} else
149 						{
150 							OString tmp("'-T', please check");
151 							if (i <= ac - 1)
152 							{
153 								tmp += " your input '" + OString(av[i+1]) + "'";
154 							}
155 
156 							throw IllegalArgument(tmp);
157 						}
158 					} else
159 					{
160 						s = av[i] + 2;
161 					}
162 
163 					if (m_options.count("-T") > 0)
164 					{
165 						OString tmp(m_options["-T"]);
166 						tmp = tmp + ";" + s;
167 						m_options["-T"] = tmp;
168 					} else
169 					{
170 						m_options["-T"] = OString(s);
171 					}
172 					break;
173 				case 'U':
174 					if (av[i][2] != '\0')
175 					{
176 						OString tmp("'-U', please check");
177 						if (i <= ac - 1)
178 						{
179 							tmp += " your input '" + OString(av[i]) + "'";
180 						}
181 
182 						throw IllegalArgument(tmp);
183 					}
184 
185 					m_options["-U"] = OString("");
186 					break;
187 /*
188 				case 'L':
189 					if (av[i][2] != '\0')
190 					{
191 						OString tmp("'-L', please check");
192 						if (i <= ac - 1)
193 						{
194 							tmp += " your input '" + OString(av[i]) + "'";
195 						}
196 
197 						throw IllegalArgument(tmp);
198 					}
199 
200 					if (isValid("-C") || isValid("-CS"))
201 					{
202 						OString tmp("'-L' could not be combined with '-C' or '-CS' option");
203 						throw IllegalArgument(tmp);
204 					}
205 					m_options["-L"] = OString("");
206 					break;
207 */
208 				case 'C':
209 					if (av[i][2] != '\0')
210 					{
211 						OString tmp("'-C', please check");
212 						if (i <= ac - 1)
213 						{
214 							tmp += " your input '" + OString(av[i]) + "'";
215 						}
216 
217 						throw IllegalArgument(tmp);
218 					}
219 
220 					if (isValid("-L") || isValid("-CS"))
221 					{
222 						OString tmp("'-C' could not be combined with '-L' or '-CS' option");
223 						throw IllegalArgument(tmp);
224 					}
225 					m_options["-C"] = OString("");
226 					break;
227 				case 'G':
228 					if (av[i][2] == 'c')
229 					{
230 						if (av[i][3] != '\0')
231 						{
232 							OString tmp("'-Gc', 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["-Gc"] = OString("");
242 						break;
243 					} else
244 					if (av[i][2] != '\0')
245 					{
246 						OString tmp("'-G', please check");
247 						if (i <= ac - 1)
248 						{
249 							tmp += " your input '" + OString(av[i]) + "'";
250 						}
251 
252 						throw IllegalArgument(tmp);
253 					}
254 
255 					m_options["-G"] = OString("");
256 					break;
257 				default:
258 					throw IllegalArgument("the option is unknown" + OString(av[i]));
259 					break;
260 			}
261 		} else
262 		{
263 			if (av[i][0] == '@')
264 			{
265 				FILE* cmdFile = fopen(av[i]+1, "r");
266 		  		if( cmdFile == NULL )
267       			{
268 					fprintf(stderr, "%s", prepareHelp().getStr());
269 					ret = sal_False;
270 				} else
271 				{
272 					int rargc=0;
273 					char* rargv[512];
274 					char  buffer[512];
275 
276 					while ( fscanf(cmdFile, "%s", buffer) != EOF )
277 					{
278 						rargv[rargc]= strdup(buffer);
279 						rargc++;
280 					}
281 					fclose(cmdFile);
282 
283 					ret = initOptions(rargc, rargv, bCmdFile);
284 
285 					for (long i=0; i < rargc; i++)
286 					{
287 						free(rargv[i]);
288 					}
289 				}
290 			} else
291 			{
292 				m_inputFiles.push_back(av[i]);
293 			}
294 		}
295 	}
296 
297 	return ret;
298 }
299 
300 OString	CunoOptions::prepareHelp()
301 {
302 	OString help("\nusing: ");
303 	help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
304 	help += "    -O<path>   = path describes the root directory for the generated output.\n";
305 	help += "                 The output directory tree is generated under this directory.\n";
306 	help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
307 	help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
308 	help += "                 then output for all types is generated.\n";
309 	help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
310 	help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
311 	help += "                 node. Default is the root '/' of the registry files.\n";
312 	help += "    -U         = activate the generating of a getCppuType_<name> function.\n";
313 //	help += "    -L         = getCppuType function with a known leak.\n";
314 	help += "    -C         = getCppuType_<name> function keeps comprehensive type information.\n";
315 	help += "    -G         = generate only target files which does not exists.\n";
316 	help += "    -Gc        = generate only target files which content will be changed.\n";
317 	help += prepareVersion();
318 
319 	return help;
320 }
321 
322 OString	CunoOptions::prepareVersion()
323 {
324 	OString version(m_program);
325 	version += " Version 1.0\n\n";
326 	return version;
327 }
328 
329 
330