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	"corbaoptions.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace rtl;
31cdf0e10cSrcweir 
initOptions(int ac,char * av[],sal_Bool bCmdFile)32cdf0e10cSrcweir sal_Bool CorbaOptions::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] == '\0')
65cdf0e10cSrcweir 					{
66cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
67cdf0e10cSrcweir 						{
68cdf0e10cSrcweir 							i++;
69cdf0e10cSrcweir 							s = av[i];
70cdf0e10cSrcweir 						} else
71cdf0e10cSrcweir 						{
72cdf0e10cSrcweir 							OString tmp("'-O', please check");
73cdf0e10cSrcweir 							if (i <= ac - 1)
74cdf0e10cSrcweir 							{
75cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
76cdf0e10cSrcweir 							}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 							throw IllegalArgument(tmp);
79cdf0e10cSrcweir 						}
80cdf0e10cSrcweir 					} else
81cdf0e10cSrcweir 					{
82cdf0e10cSrcweir 						s = av[i] + 2;
83cdf0e10cSrcweir 					}
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 					m_options["-O"] = OString(s);
86cdf0e10cSrcweir 					break;
87cdf0e10cSrcweir 				case 'H':
88cdf0e10cSrcweir 					if (av[i][2] == '\0')
89cdf0e10cSrcweir 					{
90cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
91cdf0e10cSrcweir 						{
92cdf0e10cSrcweir 							i++;
93cdf0e10cSrcweir 							s = av[i];
94cdf0e10cSrcweir 						} else
95cdf0e10cSrcweir 						{
96cdf0e10cSrcweir 							OString tmp("'-H', please check");
97cdf0e10cSrcweir 							if (i <= ac - 1)
98cdf0e10cSrcweir 							{
99cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
100cdf0e10cSrcweir 							}
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 							throw IllegalArgument(tmp);
103cdf0e10cSrcweir 						}
104cdf0e10cSrcweir 					} else
105cdf0e10cSrcweir 					{
106cdf0e10cSrcweir 						s = av[i] + 2;
107cdf0e10cSrcweir 					}
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 					m_options["-H"] = OString(s);
110cdf0e10cSrcweir 					break;
111cdf0e10cSrcweir 				case 'B':
112cdf0e10cSrcweir 					if (av[i][2] == '\0')
113cdf0e10cSrcweir 					{
114cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
115cdf0e10cSrcweir 						{
116cdf0e10cSrcweir 							i++;
117cdf0e10cSrcweir 							s = av[i];
118cdf0e10cSrcweir 						} else
119cdf0e10cSrcweir 						{
120cdf0e10cSrcweir 							OString tmp("'-B', please check");
121cdf0e10cSrcweir 							if (i <= ac - 1)
122cdf0e10cSrcweir 							{
123cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
124cdf0e10cSrcweir 							}
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 							throw IllegalArgument(tmp);
127cdf0e10cSrcweir 						}
128cdf0e10cSrcweir 					} else
129cdf0e10cSrcweir 					{
130cdf0e10cSrcweir 						s = av[i] + 2;
131cdf0e10cSrcweir 					}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 					m_options["-B"] = OString(s);
134cdf0e10cSrcweir 					break;
135cdf0e10cSrcweir 				case 'T':
136cdf0e10cSrcweir 					if (av[i][2] == '\0')
137cdf0e10cSrcweir 					{
138cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
139cdf0e10cSrcweir 						{
140cdf0e10cSrcweir 							i++;
141cdf0e10cSrcweir 							s = av[i];
142cdf0e10cSrcweir 						} else
143cdf0e10cSrcweir 						{
144cdf0e10cSrcweir 							OString tmp("'-T', please check");
145cdf0e10cSrcweir 							if (i <= ac - 1)
146cdf0e10cSrcweir 							{
147cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
148cdf0e10cSrcweir 							}
149cdf0e10cSrcweir 
150cdf0e10cSrcweir 							throw IllegalArgument(tmp);
151cdf0e10cSrcweir 						}
152cdf0e10cSrcweir 					} else
153cdf0e10cSrcweir 					{
154cdf0e10cSrcweir 						s = av[i] + 2;
155cdf0e10cSrcweir 					}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 					if (m_options.count("-T") > 0)
158cdf0e10cSrcweir 					{
159cdf0e10cSrcweir 						OString tmp(m_options["-T"]);
160cdf0e10cSrcweir 						tmp = tmp + ";" + s;
161cdf0e10cSrcweir 						m_options["-T"] = tmp;
162cdf0e10cSrcweir 					} else
163cdf0e10cSrcweir 					{
164cdf0e10cSrcweir 						m_options["-T"] = OString(s);
165cdf0e10cSrcweir 					}
166cdf0e10cSrcweir 					break;
167cdf0e10cSrcweir 				case 'G':
168cdf0e10cSrcweir 					if (av[i][2] != '\0')
169cdf0e10cSrcweir 					{
170cdf0e10cSrcweir 						OString tmp("'-G', please check");
171cdf0e10cSrcweir 						if (i <= ac - 1)
172cdf0e10cSrcweir 						{
173cdf0e10cSrcweir 							tmp += " your input '" + OString(av[i]) + "'";
174cdf0e10cSrcweir 						}
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 						throw IllegalArgument(tmp);
177cdf0e10cSrcweir 					}
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 					m_options["-G"] = OString("");
180cdf0e10cSrcweir 					break;
181cdf0e10cSrcweir 				default:
182cdf0e10cSrcweir 					throw IllegalArgument("the option is unknown" + OString(av[i]));
183cdf0e10cSrcweir 					break;
184cdf0e10cSrcweir 			}
185cdf0e10cSrcweir 		} else
186cdf0e10cSrcweir 		{
187cdf0e10cSrcweir 			if (av[i][0] == '@')
188cdf0e10cSrcweir 			{
189cdf0e10cSrcweir 				FILE* cmdFile = fopen(av[i]+1, "r");
190cdf0e10cSrcweir 		  		if( cmdFile == NULL )
191cdf0e10cSrcweir       			{
192cdf0e10cSrcweir 					fprintf(stderr, "%s", prepareHelp().getStr());
193cdf0e10cSrcweir 					ret = sal_False;
194cdf0e10cSrcweir 				} else
195cdf0e10cSrcweir 				{
196cdf0e10cSrcweir 					int rargc=0;
197cdf0e10cSrcweir 					char* rargv[512];
198cdf0e10cSrcweir 					char  buffer[512];
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 					while ( fscanf(cmdFile, "%s", buffer) != EOF )
201cdf0e10cSrcweir 					{
202cdf0e10cSrcweir 						rargv[rargc]= strdup(buffer);
203cdf0e10cSrcweir 						rargc++;
204cdf0e10cSrcweir 					}
205cdf0e10cSrcweir 					fclose(cmdFile);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 					ret = initOptions(rargc, rargv, bCmdFile);
208cdf0e10cSrcweir 
209cdf0e10cSrcweir 					for (long i=0; i < rargc; i++)
210cdf0e10cSrcweir 					{
211cdf0e10cSrcweir 						free(rargv[i]);
212cdf0e10cSrcweir 					}
213cdf0e10cSrcweir 				}
214cdf0e10cSrcweir 			} else
215cdf0e10cSrcweir 			{
216cdf0e10cSrcweir 				m_inputFiles.push_back(av[i]);
217cdf0e10cSrcweir 			}
218cdf0e10cSrcweir 		}
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 	printf("-T: %s\n", 						m_options["-T"].getStr());
221cdf0e10cSrcweir 
222cdf0e10cSrcweir 	return ret;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
prepareHelp()225cdf0e10cSrcweir OString	CorbaOptions::prepareHelp()
226cdf0e10cSrcweir {
227cdf0e10cSrcweir 	OString help("\nusing: ");
228cdf0e10cSrcweir 	help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
229cdf0e10cSrcweir 	help += "    -O<file>   = file name for the generated output.\n";
230cdf0e10cSrcweir 	help += "                 The output directory tree is generated under this directory.\n";
231cdf0e10cSrcweir 	help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
232cdf0e10cSrcweir 	help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
233cdf0e10cSrcweir 	help += "                 then output for all types is generated.\n";
234cdf0e10cSrcweir 	help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
235cdf0e10cSrcweir 	help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
236cdf0e10cSrcweir 	help += "                 node. Default is the root '/' of the registry files.\n";
237cdf0e10cSrcweir 	help += "    -G         = generate only target files which does not exists.\n";
238cdf0e10cSrcweir 	help += "    -H<header> = include CORBA generated <header>.\n";
239cdf0e10cSrcweir 	help += prepareVersion();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 	return help;
242cdf0e10cSrcweir }
243cdf0e10cSrcweir 
prepareVersion()244cdf0e10cSrcweir OString	CorbaOptions::prepareVersion()
245cdf0e10cSrcweir {
246cdf0e10cSrcweir 	OString version(m_program);
247cdf0e10cSrcweir 	version += m_program + " Version 2.0\n\n";
248cdf0e10cSrcweir 	return version;
249cdf0e10cSrcweir }
250cdf0e10cSrcweir 
251cdf0e10cSrcweir 
252