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