1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_codemaker.hxx"
30*cdf0e10cSrcweir #include 	<stdio.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include	"idloptions.hxx"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir using namespace rtl;
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir sal_Bool IdlOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
37*cdf0e10cSrcweir 	throw( IllegalArgument )
38*cdf0e10cSrcweir {
39*cdf0e10cSrcweir 	sal_Bool 	ret = sal_True;
40*cdf0e10cSrcweir 	sal_uInt16	i=0;
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 	if (!bCmdFile)
43*cdf0e10cSrcweir 	{
44*cdf0e10cSrcweir 		bCmdFile = sal_True;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir 		m_program = av[0];
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir 		if (ac < 2)
49*cdf0e10cSrcweir 		{
50*cdf0e10cSrcweir 			fprintf(stderr, "%s", prepareHelp().getStr());
51*cdf0e10cSrcweir 			ret = sal_False;
52*cdf0e10cSrcweir 		}
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 		i = 1;
55*cdf0e10cSrcweir 	} else
56*cdf0e10cSrcweir 	{
57*cdf0e10cSrcweir 		i = 0;
58*cdf0e10cSrcweir 	}
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	char	*s=NULL;
61*cdf0e10cSrcweir 	for (i; i < ac; i++)
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir 		if (av[i][0] == '-')
64*cdf0e10cSrcweir 		{
65*cdf0e10cSrcweir 			switch (av[i][1])
66*cdf0e10cSrcweir 			{
67*cdf0e10cSrcweir 				case 'O':
68*cdf0e10cSrcweir 					if (av[i][2] == '\0')
69*cdf0e10cSrcweir 					{
70*cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
71*cdf0e10cSrcweir 						{
72*cdf0e10cSrcweir 							i++;
73*cdf0e10cSrcweir 							s = av[i];
74*cdf0e10cSrcweir 						} else
75*cdf0e10cSrcweir 						{
76*cdf0e10cSrcweir 							OString tmp("'-O', please check");
77*cdf0e10cSrcweir 							if (i <= ac - 1)
78*cdf0e10cSrcweir 							{
79*cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
80*cdf0e10cSrcweir 							}
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 							throw IllegalArgument(tmp);
83*cdf0e10cSrcweir 						}
84*cdf0e10cSrcweir 					} else
85*cdf0e10cSrcweir 					{
86*cdf0e10cSrcweir 						s = av[i] + 2;
87*cdf0e10cSrcweir 					}
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir 					m_options["-O"] = OString(s);
90*cdf0e10cSrcweir 					break;
91*cdf0e10cSrcweir 				case 'B':
92*cdf0e10cSrcweir 					if (av[i][2] == '\0')
93*cdf0e10cSrcweir 					{
94*cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
95*cdf0e10cSrcweir 						{
96*cdf0e10cSrcweir 							i++;
97*cdf0e10cSrcweir 							s = av[i];
98*cdf0e10cSrcweir 						} else
99*cdf0e10cSrcweir 						{
100*cdf0e10cSrcweir 							OString tmp("'-B', please check");
101*cdf0e10cSrcweir 							if (i <= ac - 1)
102*cdf0e10cSrcweir 							{
103*cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
104*cdf0e10cSrcweir 							}
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir 							throw IllegalArgument(tmp);
107*cdf0e10cSrcweir 						}
108*cdf0e10cSrcweir 					} else
109*cdf0e10cSrcweir 					{
110*cdf0e10cSrcweir 						s = av[i] + 2;
111*cdf0e10cSrcweir 					}
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 					m_options["-B"] = OString(s);
114*cdf0e10cSrcweir 					break;
115*cdf0e10cSrcweir 				case 'T':
116*cdf0e10cSrcweir 					if (av[i][2] == '\0')
117*cdf0e10cSrcweir 					{
118*cdf0e10cSrcweir 						if (i < ac - 1 && av[i+1][0] != '-')
119*cdf0e10cSrcweir 						{
120*cdf0e10cSrcweir 							i++;
121*cdf0e10cSrcweir 							s = av[i];
122*cdf0e10cSrcweir 						} else
123*cdf0e10cSrcweir 						{
124*cdf0e10cSrcweir 							OString tmp("'-T', please check");
125*cdf0e10cSrcweir 							if (i <= ac - 1)
126*cdf0e10cSrcweir 							{
127*cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i+1]) + "'";
128*cdf0e10cSrcweir 							}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 							throw IllegalArgument(tmp);
131*cdf0e10cSrcweir 						}
132*cdf0e10cSrcweir 					} else
133*cdf0e10cSrcweir 					{
134*cdf0e10cSrcweir 						s = av[i] + 2;
135*cdf0e10cSrcweir 					}
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 					if (m_options.count("-T") > 0)
138*cdf0e10cSrcweir 					{
139*cdf0e10cSrcweir 						OString tmp(m_options["-T"]);
140*cdf0e10cSrcweir 						tmp = tmp + ";" + s;
141*cdf0e10cSrcweir 						m_options["-T"] = tmp;
142*cdf0e10cSrcweir 					} else
143*cdf0e10cSrcweir 					{
144*cdf0e10cSrcweir 						m_options["-T"] = OString(s);
145*cdf0e10cSrcweir 					}
146*cdf0e10cSrcweir 					break;
147*cdf0e10cSrcweir 				case 'G':
148*cdf0e10cSrcweir 					if (av[i][2] == 'c')
149*cdf0e10cSrcweir 					{
150*cdf0e10cSrcweir 						if (av[i][3] != '\0')
151*cdf0e10cSrcweir 						{
152*cdf0e10cSrcweir 							OString tmp("'-Gc', please check");
153*cdf0e10cSrcweir 							if (i <= ac - 1)
154*cdf0e10cSrcweir 							{
155*cdf0e10cSrcweir 								tmp += " your input '" + OString(av[i]) + "'";
156*cdf0e10cSrcweir 							}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 							throw IllegalArgument(tmp);
159*cdf0e10cSrcweir 						}
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 						m_options["-Gc"] = OString("");
162*cdf0e10cSrcweir 						break;
163*cdf0e10cSrcweir 					} else
164*cdf0e10cSrcweir 					if (av[i][2] != '\0')
165*cdf0e10cSrcweir 					{
166*cdf0e10cSrcweir 						OString tmp("'-G', please check");
167*cdf0e10cSrcweir 						if (i <= ac - 1)
168*cdf0e10cSrcweir 						{
169*cdf0e10cSrcweir 							tmp += " your input '" + OString(av[i]) + "'";
170*cdf0e10cSrcweir 						}
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 						throw IllegalArgument(tmp);
173*cdf0e10cSrcweir 					}
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 					m_options["-G"] = OString("");
176*cdf0e10cSrcweir 					break;
177*cdf0e10cSrcweir 				default:
178*cdf0e10cSrcweir 					throw IllegalArgument("the option is unknown" + OString(av[i]));
179*cdf0e10cSrcweir 					break;
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 		} else
182*cdf0e10cSrcweir 		{
183*cdf0e10cSrcweir 			if (av[i][0] == '@')
184*cdf0e10cSrcweir 			{
185*cdf0e10cSrcweir 				FILE* cmdFile = fopen(av[i]+1, "r");
186*cdf0e10cSrcweir 		  		if( cmdFile == NULL )
187*cdf0e10cSrcweir       			{
188*cdf0e10cSrcweir 					fprintf(stderr, "%s", prepareHelp().getStr());
189*cdf0e10cSrcweir 					ret = sal_False;
190*cdf0e10cSrcweir 				} else
191*cdf0e10cSrcweir 				{
192*cdf0e10cSrcweir 					int rargc=0;
193*cdf0e10cSrcweir 					char* rargv[512];
194*cdf0e10cSrcweir 					char  buffer[512];
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 					while ( fscanf(cmdFile, "%s", buffer) != EOF )
197*cdf0e10cSrcweir 					{
198*cdf0e10cSrcweir 						rargv[rargc]= strdup(buffer);
199*cdf0e10cSrcweir 						rargc++;
200*cdf0e10cSrcweir 					}
201*cdf0e10cSrcweir 					fclose(cmdFile);
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 					ret = initOptions(rargc, rargv, bCmdFile);
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 					for (long i=0; i < rargc; i++)
206*cdf0e10cSrcweir 					{
207*cdf0e10cSrcweir 						free(rargv[i]);
208*cdf0e10cSrcweir 					}
209*cdf0e10cSrcweir 				}
210*cdf0e10cSrcweir 			} else
211*cdf0e10cSrcweir 			{
212*cdf0e10cSrcweir 				m_inputFiles.push_back(av[i]);
213*cdf0e10cSrcweir 			}
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 	}
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 	return ret;
218*cdf0e10cSrcweir }
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir OString	IdlOptions::prepareHelp()
221*cdf0e10cSrcweir {
222*cdf0e10cSrcweir 	OString help("\nusing: ");
223*cdf0e10cSrcweir 	help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
224*cdf0e10cSrcweir 	help += "    -O<path>   = path describes the root directory for the generated output.\n";
225*cdf0e10cSrcweir 	help += "                 The output directory tree is generated under this directory.\n";
226*cdf0e10cSrcweir 	help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
227*cdf0e10cSrcweir 	help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
228*cdf0e10cSrcweir 	help += "                 then output for all types is generated.\n";
229*cdf0e10cSrcweir 	help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
230*cdf0e10cSrcweir 	help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
231*cdf0e10cSrcweir 	help += "                 node. Default is the root '/' of the registry files.\n";
232*cdf0e10cSrcweir 	help += "    -G         = generate only target files which does not exists.\n";
233*cdf0e10cSrcweir 	help += "    -Gc        = generate only target files which content will be changed.\n";
234*cdf0e10cSrcweir 	help += "IMPORTANT:		  You lose enum values and struct, exception inheritance!\n";
235*cdf0e10cSrcweir 	help += "				  Parameter name Object is translated to _Object!\n";
236*cdf0e10cSrcweir 	help += "				  The type type is translated to CORBA::TypeCode!\n";
237*cdf0e10cSrcweir 	help += "				  Sequences are expanded to a typedef name Sequence_..._\"name\"!\n";
238*cdf0e10cSrcweir 	help += prepareVersion();
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	return help;
241*cdf0e10cSrcweir }
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir OString	IdlOptions::prepareVersion()
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir 	OString version(m_program);
246*cdf0e10cSrcweir 	version += " Version 2.0\n\n";
247*cdf0e10cSrcweir 	return version;
248*cdf0e10cSrcweir }
249*cdf0e10cSrcweir 
250*cdf0e10cSrcweir 
251