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