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