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