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 
28 #include	"cunooptions.hxx"
29 
30 using namespace rtl;
31 
initOptions(int ac,char * av[],sal_Bool bCmdFile)32 sal_Bool CunoOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
33 	throw( IllegalArgument )
34 {
35 	sal_Bool 	ret = sal_True;
36 	sal_uInt16	i=0;
37 
38 	if (!bCmdFile)
39 	{
40 		bCmdFile = sal_True;
41 
42 		m_program = av[0];
43 
44 		if (ac < 2)
45 		{
46 			fprintf(stderr, "%s", prepareHelp().getStr());
47 			ret = sal_False;
48 		}
49 
50 		i = 1;
51 	} else
52 	{
53 		i = 0;
54 	}
55 
56 	char	*s=NULL;
57 	for (i; i < ac; i++)
58 	{
59 		if (av[i][0] == '-')
60 		{
61 			switch (av[i][1])
62 			{
63 				case 'O':
64 					if (av[i][2] == 'C')
65 					{
66 						if (av[i][3] == '\0')
67 						{
68 							if (i < ac - 1 && av[i+1][0] != '-')
69 							{
70 								i++;
71 								s = av[i];
72 							} else
73 							{
74 								OString tmp("'-OC', please check");
75 								if (i <= ac - 1)
76 								{
77 									tmp += " your input '" + OString(av[i+1]) + "'";
78 								}
79 
80 								throw IllegalArgument(tmp);
81 							}
82 						} else
83 						{
84 							s = av[i] + 3;
85 						}
86 
87 						m_options["-OC"] = OString(s);
88 						break;
89 					} else
90 					if (av[i][2] == '\0')
91 					{
92 						if (i < ac - 1 && av[i+1][0] != '-')
93 						{
94 							i++;
95 							s = av[i];
96 						} else
97 						{
98 							OString tmp("'-O', please check");
99 							if (i <= ac - 1)
100 							{
101 								tmp += " your input '" + OString(av[i+1]) + "'";
102 							}
103 
104 							throw IllegalArgument(tmp);
105 						}
106 					} else
107 					{
108 						s = av[i] + 2;
109 					}
110 
111 					m_options["-O"] = OString(s);
112 					break;
113 				case 'B':
114 					if (av[i][2] == '\0')
115 					{
116 						if (i < ac - 1 && av[i+1][0] != '-')
117 						{
118 							i++;
119 							s = av[i];
120 						} else
121 						{
122 							OString tmp("'-B', please check");
123 							if (i <= ac - 1)
124 							{
125 								tmp += " your input '" + OString(av[i+1]) + "'";
126 							}
127 
128 							throw IllegalArgument(tmp);
129 						}
130 					} else
131 					{
132 						s = av[i] + 2;
133 					}
134 
135 					m_options["-B"] = OString(s);
136 					break;
137 				case 'T':
138 					if (av[i][2] == '\0')
139 					{
140 						if (i < ac - 1 && av[i+1][0] != '-')
141 						{
142 							i++;
143 							s = av[i];
144 						} else
145 						{
146 							OString tmp("'-T', please check");
147 							if (i <= ac - 1)
148 							{
149 								tmp += " your input '" + OString(av[i+1]) + "'";
150 							}
151 
152 							throw IllegalArgument(tmp);
153 						}
154 					} else
155 					{
156 						s = av[i] + 2;
157 					}
158 
159 					if (m_options.count("-T") > 0)
160 					{
161 						OString tmp(m_options["-T"]);
162 						tmp = tmp + ";" + s;
163 						m_options["-T"] = tmp;
164 					} else
165 					{
166 						m_options["-T"] = OString(s);
167 					}
168 					break;
169 				case 'U':
170 					if (av[i][2] != '\0')
171 					{
172 						OString tmp("'-U', please check");
173 						if (i <= ac - 1)
174 						{
175 							tmp += " your input '" + OString(av[i]) + "'";
176 						}
177 
178 						throw IllegalArgument(tmp);
179 					}
180 
181 					m_options["-U"] = OString("");
182 					break;
183 /*
184 				case 'L':
185 					if (av[i][2] != '\0')
186 					{
187 						OString tmp("'-L', please check");
188 						if (i <= ac - 1)
189 						{
190 							tmp += " your input '" + OString(av[i]) + "'";
191 						}
192 
193 						throw IllegalArgument(tmp);
194 					}
195 
196 					if (isValid("-C") || isValid("-CS"))
197 					{
198 						OString tmp("'-L' could not be combined with '-C' or '-CS' option");
199 						throw IllegalArgument(tmp);
200 					}
201 					m_options["-L"] = OString("");
202 					break;
203 */
204 				case 'C':
205 					if (av[i][2] != '\0')
206 					{
207 						OString tmp("'-C', please check");
208 						if (i <= ac - 1)
209 						{
210 							tmp += " your input '" + OString(av[i]) + "'";
211 						}
212 
213 						throw IllegalArgument(tmp);
214 					}
215 
216 					if (isValid("-L") || isValid("-CS"))
217 					{
218 						OString tmp("'-C' could not be combined with '-L' or '-CS' option");
219 						throw IllegalArgument(tmp);
220 					}
221 					m_options["-C"] = OString("");
222 					break;
223 				case 'G':
224 					if (av[i][2] == 'c')
225 					{
226 						if (av[i][3] != '\0')
227 						{
228 							OString tmp("'-Gc', please check");
229 							if (i <= ac - 1)
230 							{
231 								tmp += " your input '" + OString(av[i]) + "'";
232 							}
233 
234 							throw IllegalArgument(tmp);
235 						}
236 
237 						m_options["-Gc"] = OString("");
238 						break;
239 					} else
240 					if (av[i][2] != '\0')
241 					{
242 						OString tmp("'-G', please check");
243 						if (i <= ac - 1)
244 						{
245 							tmp += " your input '" + OString(av[i]) + "'";
246 						}
247 
248 						throw IllegalArgument(tmp);
249 					}
250 
251 					m_options["-G"] = OString("");
252 					break;
253 				default:
254 					throw IllegalArgument("the option is unknown" + OString(av[i]));
255 					break;
256 			}
257 		} else
258 		{
259 			if (av[i][0] == '@')
260 			{
261 				FILE* cmdFile = fopen(av[i]+1, "r");
262 		  		if( cmdFile == NULL )
263       			{
264 					fprintf(stderr, "%s", prepareHelp().getStr());
265 					ret = sal_False;
266 				} else
267 				{
268 					int rargc=0;
269 					char* rargv[512];
270 					char  buffer[512];
271 
272 					while ( fscanf(cmdFile, "%s", buffer) != EOF )
273 					{
274 						rargv[rargc]= strdup(buffer);
275 						rargc++;
276 					}
277 					fclose(cmdFile);
278 
279 					ret = initOptions(rargc, rargv, bCmdFile);
280 
281 					for (long i=0; i < rargc; i++)
282 					{
283 						free(rargv[i]);
284 					}
285 				}
286 			} else
287 			{
288 				m_inputFiles.push_back(av[i]);
289 			}
290 		}
291 	}
292 
293 	return ret;
294 }
295 
prepareHelp()296 OString	CunoOptions::prepareHelp()
297 {
298 	OString help("\nusing: ");
299 	help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
300 	help += "    -O<path>   = path describes the root directory for the generated output.\n";
301 	help += "                 The output directory tree is generated under this directory.\n";
302 	help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
303 	help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
304 	help += "                 then output for all types is generated.\n";
305 	help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
306 	help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
307 	help += "                 node. Default is the root '/' of the registry files.\n";
308 	help += "    -U         = activate the generating of a getCppuType_<name> function.\n";
309 //	help += "    -L         = getCppuType function with a known leak.\n";
310 	help += "    -C         = getCppuType_<name> function keeps comprehensive type information.\n";
311 	help += "    -G         = generate only target files which does not exists.\n";
312 	help += "    -Gc        = generate only target files which content will be changed.\n";
313 	help += prepareVersion();
314 
315 	return help;
316 }
317 
prepareVersion()318 OString	CunoOptions::prepareVersion()
319 {
320 	OString version(m_program);
321 	version += " Version 1.0\n\n";
322 	return version;
323 }
324 
325 
326