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 
27 #include <stdio.h>
28 
29 #include "sal/main.h"
30 
31 #include <codemaker/typemanager.hxx>
32 #include <codemaker/dependency.hxx>
33 
34 #include "idloptions.hxx"
35 #include "idltype.hxx"
36 
37 using namespace rtl;
38 
produceAllTypes(const OString & typeName,TypeManager & typeMgr,TypeDependency & typeDependencies,IdlOptions * pOptions,sal_Bool bFullScope)39 sal_Bool produceAllTypes(const OString& typeName,
40 						 TypeManager& typeMgr,
41 						 TypeDependency& typeDependencies,
42 						 IdlOptions* pOptions,
43 						 sal_Bool bFullScope)
44 	throw( CannotDumpException )
45 {
46 	if (!produceType(typeName, typeMgr,	typeDependencies, pOptions))
47 	{
48 		fprintf(stderr, "%s ERROR: %s\n",
49 				pOptions->getProgramName().getStr(),
50 				OString("cannot dump Type '" + typeName + "'").getStr());
51 		exit(99);
52 	}
53 
54 	RegistryKey	typeKey = typeMgr.getTypeKey(typeName);
55 	RegistryKeyNames subKeys;
56 
57 	if (typeKey.getKeyNames(OUString(), subKeys))
58 		return sal_False;
59 
60 	OString tmpName;
61 	for (sal_uInt32 i=0; i < subKeys.getLength(); i++)
62 	{
63 		tmpName = OUStringToOString(subKeys.getElement(i), RTL_TEXTENCODING_UTF8);
64 
65 		if (pOptions->isValid("-B"))
66 			tmpName = tmpName.copy(tmpName.indexOf('/', 1) + 1);
67 		else
68 			tmpName = tmpName.copy(1);
69 
70 		if (bFullScope)
71 		{
72 			if (!produceAllTypes(tmpName, typeMgr, typeDependencies, pOptions, sal_True))
73 				return sal_False;
74 		} else
75 		{
76 			if (!produceType(tmpName, typeMgr, typeDependencies, pOptions))
77 				return sal_False;
78 		}
79 	}
80 
81 	return sal_True;
82 }
83 
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)84 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
85 {
86 	IdlOptions options;
87 
88 	try
89 	{
90 		if (!options.initOptions(argc, argv))
91 		{
92 			exit(1);
93 		}
94 	}
95 	catch( IllegalArgument& e)
96 	{
97 		fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
98 		exit(99);
99 	}
100 
101 	RegistryTypeManager typeMgr;
102 	TypeDependency		typeDependencies;
103 
104 	if (!typeMgr.init(!options.isValid("-T"), options.getInputFiles()))
105 	{
106 		fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
107 		exit(99);
108 	}
109 
110 	if (options.isValid("-B"))
111 	{
112 		typeMgr.setBase(options.getOption("-B"));
113 	}
114 
115 	try
116 	{
117 		if (options.isValid("-T"))
118 		{
119 			OString tOption(options.getOption("-T"));
120 
121 			OString typeName, tmpName;
122 			sal_Bool ret = sal_False;
123             sal_Int32 nIndex = 0;
124             do
125 			{
126 				typeName = tOption.getToken(0, ';', nIndex);
127 
128                 sal_Int32 nPos = typeName.lastIndexOf( '.' );
129                 tmpName = typeName.copy( nPos != -1 ? nPos+1 : 0 );
130 				if (tmpName == "*")
131 				{
132 					// produce this type and his scope, but the scope is not recursively  generated.
133 					if (typeName.equals("*"))
134 					{
135 						tmpName = "/";
136 					} else
137 					{
138 						tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
139 						if ( tmpName.isEmpty() )
140 							tmpName = "/";
141 						else
142 							tmpName.replace('.', '/');
143 					}
144 					ret = produceAllTypes(tmpName, typeMgr, typeDependencies, &options, sal_False);
145 				} else
146 				{
147 					// produce only this type
148 					ret = produceType(typeName.replace('.', '/'), typeMgr, typeDependencies, &options);
149 				}
150 
151 				if (!ret)
152 				{
153 					fprintf(stderr, "%s ERROR: %s\n",
154 							options.getProgramName().getStr(),
155 							OString("cannot dump Type '" + typeName + "'").getStr());
156 					exit(99);
157 				}
158 			} while( nIndex != -1 );
159 		} else
160 		{
161 			// produce all types
162 			if (!produceAllTypes("/", typeMgr, typeDependencies, &options, sal_True))
163 			{
164 				fprintf(stderr, "%s ERROR: %s\n",
165 						options.getProgramName().getStr(),
166 						"an error occurs while dumping all types.");
167 				exit(99);
168 			}
169 		}
170 	}
171 	catch( CannotDumpException& e)
172 	{
173 		fprintf(stderr, "%s ERROR: %s\n",
174 				options.getProgramName().getStr(),
175 				e.m_message.getStr());
176 		exit(99);
177 	}
178 
179 	return 0;
180 }
181 
182 
183