xref: /trunk/main/codemaker/source/cppumaker/cppuoptions.cxx (revision 8f71f2da3780fff02b58fe2fe2c8a220939be93f)
1ff7655f0SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3ff7655f0SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4ff7655f0SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5ff7655f0SAndrew Rist  * distributed with this work for additional information
6ff7655f0SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7ff7655f0SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8ff7655f0SAndrew Rist  * "License"); you may not use this file except in compliance
9ff7655f0SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11ff7655f0SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13ff7655f0SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14ff7655f0SAndrew Rist  * software distributed under the License is distributed on an
15ff7655f0SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16ff7655f0SAndrew Rist  * KIND, either express or implied.  See the License for the
17ff7655f0SAndrew Rist  * specific language governing permissions and limitations
18ff7655f0SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20ff7655f0SAndrew Rist  *************************************************************/
21ff7655f0SAndrew Rist 
22ff7655f0SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25*8f71f2daSDamjan Jovanovic #include "precompiled_cppumaker.hxx"
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir #include <string.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include "cppuoptions.hxx"
30cdf0e10cSrcweir #include "osl/thread.h"
31cdf0e10cSrcweir #include "osl/process.h"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #ifdef SAL_UNX
34cdf0e10cSrcweir #define SEPARATOR '/'
35cdf0e10cSrcweir #else
36cdf0e10cSrcweir #define SEPARATOR '\\'
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using namespace rtl;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir sal_Bool CppuOptions::initOptions(int ac, char* av[], sal_Bool bCmdFile)
42cdf0e10cSrcweir     throw( IllegalArgument )
43cdf0e10cSrcweir {
44cdf0e10cSrcweir     sal_Bool    ret = sal_True;
45cdf0e10cSrcweir     sal_uInt16  i=0;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     if (!bCmdFile)
48cdf0e10cSrcweir     {
49cdf0e10cSrcweir         bCmdFile = sal_True;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir         OString name(av[0]);
52cdf0e10cSrcweir         sal_Int32 index = name.lastIndexOf(SEPARATOR);
53cdf0e10cSrcweir         m_program = name.copy((index > 0 ? index+1 : 0));
54cdf0e10cSrcweir 
55cdf0e10cSrcweir         if (ac < 2)
56cdf0e10cSrcweir         {
57cdf0e10cSrcweir             fprintf(stderr, "%s", prepareHelp().getStr());
58cdf0e10cSrcweir             ret = sal_False;
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir         i = 1;
62cdf0e10cSrcweir     } else
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir         i = 0;
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     char    *s=NULL;
68cdf0e10cSrcweir     for( ; i < ac; i++)
69cdf0e10cSrcweir     {
70cdf0e10cSrcweir         if (av[i][0] == '-')
71cdf0e10cSrcweir         {
72cdf0e10cSrcweir             switch (av[i][1])
73cdf0e10cSrcweir             {
74cdf0e10cSrcweir                 case 'O':
75cdf0e10cSrcweir                     if (av[i][2] == '\0')
76cdf0e10cSrcweir                     {
77cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
78cdf0e10cSrcweir                         {
79cdf0e10cSrcweir                             i++;
80cdf0e10cSrcweir                             s = av[i];
81cdf0e10cSrcweir                         } else
82cdf0e10cSrcweir                         {
83cdf0e10cSrcweir                             OString tmp("'-O', please check");
84cdf0e10cSrcweir                             if (i <= ac - 1)
85cdf0e10cSrcweir                             {
86cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
87cdf0e10cSrcweir                             }
88cdf0e10cSrcweir 
89cdf0e10cSrcweir                             throw IllegalArgument(tmp);
90cdf0e10cSrcweir                         }
91cdf0e10cSrcweir                     } else
92cdf0e10cSrcweir                     {
93cdf0e10cSrcweir                         s = av[i] + 2;
94cdf0e10cSrcweir                     }
95cdf0e10cSrcweir 
96cdf0e10cSrcweir                     m_options["-O"] = OString(s);
97cdf0e10cSrcweir                     break;
98cdf0e10cSrcweir                 case 'B':
99cdf0e10cSrcweir                     if (av[i][2] == '\0')
100cdf0e10cSrcweir                     {
101cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
102cdf0e10cSrcweir                         {
103cdf0e10cSrcweir                             i++;
104cdf0e10cSrcweir                             s = av[i];
105cdf0e10cSrcweir                         } else
106cdf0e10cSrcweir                         {
107cdf0e10cSrcweir                             OString tmp("'-B', please check");
108cdf0e10cSrcweir                             if (i <= ac - 1)
109cdf0e10cSrcweir                             {
110cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
111cdf0e10cSrcweir                             }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir                             throw IllegalArgument(tmp);
114cdf0e10cSrcweir                         }
115cdf0e10cSrcweir                     } else
116cdf0e10cSrcweir                     {
117cdf0e10cSrcweir                         s = av[i] + 2;
118cdf0e10cSrcweir                     }
119cdf0e10cSrcweir 
120cdf0e10cSrcweir                     m_options["-B"] = OString(s);
121cdf0e10cSrcweir                     break;
122cdf0e10cSrcweir                 case 'T':
123cdf0e10cSrcweir                     if (av[i][2] == '\0')
124cdf0e10cSrcweir                     {
125cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
126cdf0e10cSrcweir                         {
127cdf0e10cSrcweir                             i++;
128cdf0e10cSrcweir                             s = av[i];
129cdf0e10cSrcweir                         } else
130cdf0e10cSrcweir                         {
131cdf0e10cSrcweir                             OString tmp("'-T', please check");
132cdf0e10cSrcweir                             if (i <= ac - 1)
133cdf0e10cSrcweir                             {
134cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
135cdf0e10cSrcweir                             }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir                             throw IllegalArgument(tmp);
138cdf0e10cSrcweir                         }
139cdf0e10cSrcweir                     } else
140cdf0e10cSrcweir                     {
141cdf0e10cSrcweir                         s = av[i] + 2;
142cdf0e10cSrcweir                     }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir                     if (m_options.count("-T") > 0)
145cdf0e10cSrcweir                     {
146cdf0e10cSrcweir                         OString tmp(m_options["-T"]);
147cdf0e10cSrcweir                         tmp = tmp + ";" + s;
148cdf0e10cSrcweir                         m_options["-T"] = tmp;
149cdf0e10cSrcweir                     } else
150cdf0e10cSrcweir                     {
151cdf0e10cSrcweir                         m_options["-T"] = OString(s);
152cdf0e10cSrcweir                     }
153cdf0e10cSrcweir                     break;
154cdf0e10cSrcweir                 case 'L':
155cdf0e10cSrcweir                     if (av[i][2] != '\0')
156cdf0e10cSrcweir                     {
157cdf0e10cSrcweir                         OString tmp("'-L', please check");
158cdf0e10cSrcweir                         if (i <= ac - 1)
159cdf0e10cSrcweir                         {
160cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
161cdf0e10cSrcweir                         }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir                         throw IllegalArgument(tmp);
164cdf0e10cSrcweir                     }
165cdf0e10cSrcweir 
166cdf0e10cSrcweir                     if (isValid("-C") || isValid("-CS"))
167cdf0e10cSrcweir                     {
168cdf0e10cSrcweir                         OString tmp("'-L' could not be combined with '-C' or '-CS' option");
169cdf0e10cSrcweir                         throw IllegalArgument(tmp);
170cdf0e10cSrcweir                     }
171cdf0e10cSrcweir                     m_options["-L"] = OString("");
172cdf0e10cSrcweir                     break;
173cdf0e10cSrcweir                 case 'C':
174cdf0e10cSrcweir                     if (av[i][2] == 'S')
175cdf0e10cSrcweir                     {
176cdf0e10cSrcweir                         if (av[i][3] != '\0')
177cdf0e10cSrcweir                         {
178cdf0e10cSrcweir                             OString tmp("'-CS', please check");
179cdf0e10cSrcweir                             if (i <= ac - 1)
180cdf0e10cSrcweir                             {
181cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i]) + "'";
182cdf0e10cSrcweir                             }
183cdf0e10cSrcweir 
184cdf0e10cSrcweir                             throw IllegalArgument(tmp);
185cdf0e10cSrcweir                         }
186cdf0e10cSrcweir 
187cdf0e10cSrcweir                         if (isValid("-L") || isValid("-C"))
188cdf0e10cSrcweir                         {
189cdf0e10cSrcweir                             OString tmp("'-CS' could not be combined with '-L' or '-C' option");
190cdf0e10cSrcweir                             throw IllegalArgument(tmp);
191cdf0e10cSrcweir                         }
192cdf0e10cSrcweir                         m_options["-CS"] = OString("");
193cdf0e10cSrcweir                         break;
194cdf0e10cSrcweir                     } else
195cdf0e10cSrcweir                     if (av[i][2] != '\0')
196cdf0e10cSrcweir                     {
197cdf0e10cSrcweir                         OString tmp("'-C', please check");
198cdf0e10cSrcweir                         if (i <= ac - 1)
199cdf0e10cSrcweir                         {
200cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
201cdf0e10cSrcweir                         }
202cdf0e10cSrcweir 
203cdf0e10cSrcweir                         throw IllegalArgument(tmp);
204cdf0e10cSrcweir                     }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir                     if (isValid("-L") || isValid("-CS"))
207cdf0e10cSrcweir                     {
208cdf0e10cSrcweir                         OString tmp("'-C' could not be combined with '-L' or '-CS' option");
209cdf0e10cSrcweir                         throw IllegalArgument(tmp);
210cdf0e10cSrcweir                     }
211cdf0e10cSrcweir                     m_options["-C"] = OString("");
212cdf0e10cSrcweir                     break;
213cdf0e10cSrcweir                 case 'G':
214cdf0e10cSrcweir                     if (av[i][2] == 'c')
215cdf0e10cSrcweir                     {
216cdf0e10cSrcweir                         if (av[i][3] != '\0')
217cdf0e10cSrcweir                         {
218cdf0e10cSrcweir                             OString tmp("'-Gc', please check");
219cdf0e10cSrcweir                             if (i <= ac - 1)
220cdf0e10cSrcweir                             {
221cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i]) + "'";
222cdf0e10cSrcweir                             }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir                             throw IllegalArgument(tmp);
225cdf0e10cSrcweir                         }
226cdf0e10cSrcweir 
227cdf0e10cSrcweir                         m_options["-Gc"] = OString("");
228cdf0e10cSrcweir                         break;
229cdf0e10cSrcweir                     } else
230cdf0e10cSrcweir                     if (av[i][2] != '\0')
231cdf0e10cSrcweir                     {
232cdf0e10cSrcweir                         OString tmp("'-G', please check");
233cdf0e10cSrcweir                         if (i <= ac - 1)
234cdf0e10cSrcweir                         {
235cdf0e10cSrcweir                             tmp += " your input '" + OString(av[i]) + "'";
236cdf0e10cSrcweir                         }
237cdf0e10cSrcweir 
238cdf0e10cSrcweir                         throw IllegalArgument(tmp);
239cdf0e10cSrcweir                     }
240cdf0e10cSrcweir 
241cdf0e10cSrcweir                     m_options["-G"] = OString("");
242cdf0e10cSrcweir                     break;
243cdf0e10cSrcweir                 case 'X': // support for eXtra type rdbs
244cdf0e10cSrcweir                 {
245cdf0e10cSrcweir                     if (av[i][2] == '\0')
246cdf0e10cSrcweir                     {
247cdf0e10cSrcweir                         if (i < ac - 1 && av[i+1][0] != '-')
248cdf0e10cSrcweir                         {
249cdf0e10cSrcweir                             i++;
250cdf0e10cSrcweir                             s = av[i];
251cdf0e10cSrcweir                         } else
252cdf0e10cSrcweir                         {
253cdf0e10cSrcweir                             OString tmp("'-X', please check");
254cdf0e10cSrcweir                             if (i <= ac - 1)
255cdf0e10cSrcweir                             {
256cdf0e10cSrcweir                                 tmp += " your input '" + OString(av[i+1]) + "'";
257cdf0e10cSrcweir                             }
258cdf0e10cSrcweir 
259cdf0e10cSrcweir                             throw IllegalArgument(tmp);
260cdf0e10cSrcweir                         }
261cdf0e10cSrcweir                     } else
262cdf0e10cSrcweir                     {
263cdf0e10cSrcweir                         s = av[i] + 2;
264cdf0e10cSrcweir                     }
265cdf0e10cSrcweir 
266cdf0e10cSrcweir                     m_extra_input_files.push_back( s );
267cdf0e10cSrcweir                     break;
268cdf0e10cSrcweir                 }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir                 default:
271cdf0e10cSrcweir                     throw IllegalArgument("the option is unknown" + OString(av[i]));
272cdf0e10cSrcweir             }
273cdf0e10cSrcweir         } else
274cdf0e10cSrcweir         {
275cdf0e10cSrcweir             if (av[i][0] == '@')
276cdf0e10cSrcweir             {
277cdf0e10cSrcweir                 FILE* cmdFile = fopen(av[i]+1, "r");
278cdf0e10cSrcweir                 if( cmdFile == NULL )
279cdf0e10cSrcweir                 {
280cdf0e10cSrcweir                     fprintf(stderr, "%s", prepareHelp().getStr());
281cdf0e10cSrcweir                     ret = sal_False;
282cdf0e10cSrcweir                 } else
283cdf0e10cSrcweir                 {
284cdf0e10cSrcweir                     int rargc=0;
285cdf0e10cSrcweir                     char* rargv[512];
286cdf0e10cSrcweir                     char  buffer[512];
287cdf0e10cSrcweir 
288cdf0e10cSrcweir                     while ( fscanf(cmdFile, "%s", buffer) != EOF )
289cdf0e10cSrcweir                     {
290cdf0e10cSrcweir                         rargv[rargc]= strdup(buffer);
291cdf0e10cSrcweir                         rargc++;
292cdf0e10cSrcweir                     }
293cdf0e10cSrcweir                     fclose(cmdFile);
294cdf0e10cSrcweir 
295cdf0e10cSrcweir                     ret = initOptions(rargc, rargv, bCmdFile);
296cdf0e10cSrcweir 
297cdf0e10cSrcweir                     for (long j=0; j < rargc; j++)
298cdf0e10cSrcweir                     {
299cdf0e10cSrcweir                         free(rargv[j]);
300cdf0e10cSrcweir                     }
301cdf0e10cSrcweir                 }
302cdf0e10cSrcweir             } else
303cdf0e10cSrcweir             {
304cdf0e10cSrcweir                 if (bCmdFile)
305cdf0e10cSrcweir                 {
306cdf0e10cSrcweir                     m_inputFiles.push_back(av[i]);
307cdf0e10cSrcweir                 } else
308cdf0e10cSrcweir                 {
309cdf0e10cSrcweir                     OUString system_filepath;
310cdf0e10cSrcweir                     if (osl_getCommandArg( i-1, &system_filepath.pData )
311cdf0e10cSrcweir                         != osl_Process_E_None)
312cdf0e10cSrcweir                     {
313cdf0e10cSrcweir                         OSL_ASSERT(false);
314cdf0e10cSrcweir                     }
315cdf0e10cSrcweir                     m_inputFiles.push_back(OUStringToOString(system_filepath, osl_getThreadTextEncoding()));
316cdf0e10cSrcweir                 }
317cdf0e10cSrcweir             }
318cdf0e10cSrcweir         }
319cdf0e10cSrcweir     }
320cdf0e10cSrcweir 
321cdf0e10cSrcweir     return ret;
322cdf0e10cSrcweir }
323cdf0e10cSrcweir 
324cdf0e10cSrcweir OString CppuOptions::prepareHelp()
325cdf0e10cSrcweir {
326cdf0e10cSrcweir     OString help("\nusing: ");
327cdf0e10cSrcweir     help += m_program + " [-options] file_1 ... file_n\nOptions:\n";
328cdf0e10cSrcweir     help += "    -O<path>   = path describes the root directory for the generated output.\n";
329cdf0e10cSrcweir     help += "                 The output directory tree is generated under this directory.\n";
330cdf0e10cSrcweir     help += "    -T<name>   = name specifies a type or a list of types. The output for this\n";
331cdf0e10cSrcweir     help += "      [t1;...]   type is generated. If no '-T' option is specified,\n";
332cdf0e10cSrcweir     help += "                 then output for all types is generated.\n";
333cdf0e10cSrcweir     help += "                 Example: 'com.sun.star.uno.XInterface' is a valid type.\n";
334cdf0e10cSrcweir     help += "    -B<name>   = name specifies the base node. All types are searched under this\n";
335cdf0e10cSrcweir     help += "                 node. Default is the root '/' of the registry files.\n";
336cdf0e10cSrcweir     help += "    -L         = UNO type functions are generated lightweight, that means only\n";
337cdf0e10cSrcweir     help += "                 the name and typeclass are given and everything else is retrieved\n";
338cdf0e10cSrcweir     help += "                 from the type library dynamically. The default is that UNO type\n";
339cdf0e10cSrcweir     help += "                 functions provides enough type information for boostrapping C++.\n";
340cdf0e10cSrcweir     help += "                 '-L' should be the default for external components.\n";
341cdf0e10cSrcweir     help += "    -C         = UNO type functions are generated comprehensive that means all\n";
342cdf0e10cSrcweir     help += "                 necessary information is available for bridging the type in UNO.\n";
343cdf0e10cSrcweir     help += "    -G         = generate only target files which does not exists.\n";
344cdf0e10cSrcweir     help += "    -Gc        = generate only target files which content will be changed.\n";
345cdf0e10cSrcweir     help += "    -X<file>   = extra types which will not be taken into account for generation.\n\n";
346cdf0e10cSrcweir     help += prepareVersion();
347cdf0e10cSrcweir 
348cdf0e10cSrcweir     return help;
349cdf0e10cSrcweir }
350cdf0e10cSrcweir 
351cdf0e10cSrcweir OString CppuOptions::prepareVersion()
352cdf0e10cSrcweir {
353cdf0e10cSrcweir     OString version(m_program);
354cdf0e10cSrcweir     version += " Version 2.0\n\n";
355cdf0e10cSrcweir     return version;
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir 
359