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 #include <stdio.h>
25 #include <osl/file.hxx>
26 #include <osl/process.h>
27 #include <codemaker/typemanager.hxx>
28 #include <codemaker/dependency.hxx>
29
30 #ifndef _RTL_OSTRINGBUFFER_HXX_
31 #include <rtl/strbuf.hxx>
32 #endif
33
34 #if defined(SAL_W32) || defined(SAL_OS2)
35 #include <io.h>
36 #include <direct.h>
37 #include <errno.h>
38 #endif
39
40 #ifdef UNX
41 #include <stdlib.h>
42 #include <sys/stat.h>
43 #include <errno.h>
44 #include <unistd.h>
45 #endif
46
47 #include "specialtypemanager.hxx"
48 #include "rdboptions.hxx"
49 #include "rdbtype.hxx"
50
51 #define PATH_DELEMITTER '/'
52
53 using namespace rtl;
54 using namespace osl;
55
56 FileStream listFile;
57 RegistryKey rootKey;
58 Registry regFile;
59 sal_Bool useSpecial;
60 TypeManager* pTypeMgr = NULL;
61 StringList dirEntries;
62 StringSet filterTypes;
63
getFullNameOfApplicatRdb()64 OString getFullNameOfApplicatRdb()
65 {
66 OUString bootReg;
67 OUString uTmpStr;
68 if( osl_getExecutableFile(&uTmpStr.pData) == osl_Process_E_None )
69 {
70 sal_uInt32 lastIndex = uTmpStr.lastIndexOf(PATH_DELEMITTER);
71 OUString tmpReg;
72
73 if ( lastIndex > 0 )
74 {
75 tmpReg =uTmpStr.copy(0, lastIndex + 1);
76 }
77
78 tmpReg += OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb") );
79
80 FileBase::getSystemPathFromFileURL(tmpReg, bootReg);
81 }
82
83 return OUStringToOString(bootReg, RTL_TEXTENCODING_ASCII_US);
84 }
85
initFilterTypes(RdbOptions * pOptions)86 void initFilterTypes(RdbOptions* pOptions)
87 {
88 if (pOptions->isValid("-FT"))
89 {
90 OString fOption(pOptions->getOption("-FT"));
91 sal_Int32 nIndex = 0;
92 do
93 {
94 filterTypes.insert( fOption.getToken( 0, ';', nIndex ).replace('.', '/') );
95 }
96 while ( nIndex >= 0 );
97 }
98 if (pOptions->isValid("-F"))
99 {
100 FILE *f = fopen(pOptions->getOption("-F").getStr(), "r");
101
102 if (f)
103 {
104 sal_Char buffer[1024+1];
105 sal_Char *pBuf = fgets(buffer, 1024, f);
106 sal_Char *s = NULL;
107 sal_Char *p = NULL;
108 while ( pBuf && !feof(f))
109 {
110 p = pBuf;
111 if (*p != '\n' && *p != '\r')
112 {
113 while (*p == ' ' && *p =='\t')
114 p++;
115
116 s = p;
117 while (*p != '\n' && *p != '\r' && *p != ' ' && *p != '\t')
118 p++;
119
120 *p = '\0';
121 filterTypes.insert( OString(s).replace('.', '/') );
122 }
123
124 pBuf = fgets(buffer, 1024, f);
125 }
126
127 fclose(f);
128 }
129 }
130 }
131
checkFilterTypes(const OString & type)132 sal_Bool checkFilterTypes(const OString& type)
133 {
134 StringSet::iterator iter = filterTypes.begin();
135 while ( iter != filterTypes.end() )
136 {
137 if ( type.indexOf( *iter ) == 0 )
138 {
139 return sal_True;
140 }
141
142 iter++;
143 }
144
145 return sal_False;
146 }
147
cleanUp(sal_Bool bError)148 void cleanUp( sal_Bool bError)
149 {
150 if ( pTypeMgr )
151 {
152 delete pTypeMgr;
153 }
154 if (useSpecial)
155 {
156 pTypeMgr = new SpecialTypeManager();
157 }else
158 {
159 pTypeMgr = new RegistryTypeManager();
160 }
161
162 if ( rootKey.isValid() )
163 {
164 rootKey.closeKey();
165 }
166 if ( regFile.isValid() )
167 {
168 if ( bError )
169 {
170 regFile.destroy(OUString());
171 } else
172 {
173 regFile.close();
174 }
175 }
176 if ( listFile.isValid() )
177 {
178 listFile.close();
179 unlink(listFile.getName().getStr());
180 }
181
182 StringList::reverse_iterator iter = dirEntries.rbegin();
183 while ( iter != dirEntries.rend() )
184 {
185 if (rmdir((char*)(*iter).getStr()) == -1)
186 {
187 break;
188 }
189
190 iter++;
191 }
192 }
193
createFileName(const OString & path)194 OString createFileName(const OString& path)
195 {
196 OString fileName(path);
197
198 sal_Char token;
199 #ifdef SAL_UNX
200 fileName = fileName.replace('\\', '/');
201 token = '/';
202 #else
203 fileName = fileName.replace('/', '\\');
204 token = '\\';
205 #endif
206
207 OStringBuffer nameBuffer( path.getLength() );
208
209 sal_Int32 nIndex = 0;
210 do
211 {
212 nameBuffer.append(fileName.getToken( 0, token, nIndex ).getStr());
213 if ( nIndex == -1 ) break;
214
215 if (nameBuffer.getLength() == 0 || OString(".") == nameBuffer.getStr())
216 {
217 nameBuffer.append(token);
218 continue;
219 }
220
221 #if defined(SAL_UNX) || defined(SAL_OS2)
222 if (mkdir((char*)nameBuffer.getStr(), 0777) == -1)
223 #else
224 if (mkdir((char*)nameBuffer.getStr()) == -1)
225 #endif
226 {
227 if ( errno == ENOENT )
228 return OString();
229 } else
230 {
231 dirEntries.push_back(nameBuffer.getStr());
232 }
233
234 nameBuffer.append(token);
235 }
236 while ( nIndex >= 0 );
237
238 return fileName;
239 }
240
produceAllTypes(const OString & typeName,TypeManager & typeMgr,TypeDependency & typeDependencies,RdbOptions * pOptions,sal_Bool bFullScope,FileStream & o,RegistryKey & regKey,StringSet & filterTypes2)241 sal_Bool produceAllTypes(const OString& typeName,
242 TypeManager& typeMgr,
243 TypeDependency& typeDependencies,
244 RdbOptions* pOptions,
245 sal_Bool bFullScope,
246 FileStream& o,
247 RegistryKey& regKey,
248 StringSet& filterTypes2)
249 {
250 if (!produceType(typeName, typeMgr, typeDependencies, pOptions, o, regKey, filterTypes2))
251 {
252 fprintf(stderr, "%s ERROR: %s\n",
253 pOptions->getProgramName().getStr(),
254 OString("cannot dump Type '" + typeName + "'").getStr());
255 cleanUp(sal_True);
256 exit(99);
257 }
258
259 RegistryKey typeKey = typeMgr.getTypeKey(typeName);
260 RegistryKeyNames subKeys;
261
262 if (typeKey.getKeyNames(OUString(), subKeys))
263 return sal_False;
264
265 OString tmpName;
266 for (sal_uInt32 i=0; i < subKeys.getLength(); i++)
267 {
268 tmpName = OUStringToOString(subKeys.getElement(i), RTL_TEXTENCODING_UTF8);
269
270 if (pOptions->isValid("-B"))
271 tmpName = tmpName.copy(tmpName.indexOf('/', 1) + 1);
272 else
273 tmpName = tmpName.copy(1);
274
275 if (bFullScope)
276 {
277 if (!produceAllTypes(tmpName, typeMgr, typeDependencies, pOptions, sal_True,
278 o, regKey, filterTypes2))
279 return sal_False;
280 } else
281 {
282 if (!produceType(tmpName, typeMgr, typeDependencies, pOptions, o, regKey, filterTypes2))
283 return sal_False;
284 }
285 }
286
287 return sal_True;
288 }
289
290
291 #if (defined UNX) || (defined OS2)
main(int argc,char * argv[])292 int main( int argc, char * argv[] )
293 #else
294 int _cdecl main( int argc, char * argv[] )
295 #endif
296 {
297 RdbOptions options;
298
299 try
300 {
301 if (!options.initOptions(argc, argv))
302 {
303 cleanUp(sal_True);
304 exit(1);
305 }
306 }
307 catch( IllegalArgument& e)
308 {
309 fprintf(stderr, "Illegal option: %s\n", e.m_message.getStr());
310 cleanUp(sal_True);
311 exit(99);
312 }
313
314 TypeDependency typeDependencies;
315
316 OString bootReg;
317
318 if ( options.isValid("-R") )
319 {
320 bootReg = options.getOption("-R");
321 } else
322 {
323 if (options.getInputFiles().empty())
324 {
325 bootReg = getFullNameOfApplicatRdb();
326 }
327 }
328
329 if ( bootReg.getLength() )
330 {
331 pTypeMgr = new SpecialTypeManager();
332 useSpecial = sal_True;
333 } else
334 {
335 pTypeMgr = new RegistryTypeManager();
336 useSpecial = sal_False;
337 }
338
339 TypeManager& typeMgr = *pTypeMgr;
340
341 if ( useSpecial && !typeMgr.init( bootReg ) )
342 {
343 fprintf(stderr, "%s : init typemanager failed, check your environment for bootstrapping uno.\n", options.getProgramName().getStr());
344 cleanUp(sal_True);
345 exit(99);
346 }
347 if ( !useSpecial && !typeMgr.init(!options.isValid("-T"), options.getInputFiles()))
348 {
349 fprintf(stderr, "%s : init registries failed, check your registry files.\n", options.getProgramName().getStr());
350 cleanUp(sal_True);
351 exit(99);
352 }
353
354 initFilterTypes(&options);
355
356 if (options.isValid("-B"))
357 {
358 typeMgr.setBase(options.getOption("-B"));
359 }
360
361 if ( !options.isValid("-O") )
362 {
363 fprintf(stderr, "%s ERROR: %s\n",
364 options.getProgramName().getStr(),
365 "no output file is specified.");
366 cleanUp(sal_True);
367 exit(99);
368 }
369
370 if ( options.generateTypeList() )
371 {
372 OString fileName = createFileName( options.getOption("-O") );
373 listFile.open(fileName);
374
375 if ( !listFile.isValid() )
376 {
377 fprintf(stderr, "%s ERROR: %s\n",
378 options.getProgramName().getStr(),
379 "could not open output file.");
380 cleanUp(sal_True);
381 exit(99);
382 }
383 } else
384 {
385 OUString fileName( OStringToOUString(createFileName( options.getOption("-O") ), RTL_TEXTENCODING_UTF8) );
386 if ( regFile.create(fileName) )
387 {
388 fprintf(stderr, "%s ERROR: %s\n",
389 options.getProgramName().getStr(),
390 "could not create registry output file.");
391 cleanUp(sal_True);
392 exit(99);
393 }
394
395
396 if (options.isValid("-b"))
397 {
398 RegistryKey tmpKey;
399 regFile.openRootKey(tmpKey);
400
401 tmpKey.createKey( OStringToOUString(options.getOption("-b"), RTL_TEXTENCODING_UTF8), rootKey);
402 } else
403 {
404 regFile.openRootKey(rootKey);
405 }
406 }
407
408 try
409 {
410 if (options.isValid("-T"))
411 {
412 OString tOption(options.getOption("-T"));
413 OString typeName, tmpName;
414 sal_Bool ret = sal_False;
415 sal_Int32 nIndex = 0;
416 do
417 {
418 typeName = tOption.getToken( 0, ';', nIndex);
419 sal_Int32 lastIndex = typeName.lastIndexOf('.');
420 tmpName = typeName.copy( lastIndex+1 );
421 if (tmpName == "*")
422 {
423 if (bootReg.getLength())
424 {
425 fprintf(stderr, "%s ERROR: %s\n",
426 options.getProgramName().getStr(),
427 "dumping all types of a scope is not possible if -R option is used.");
428 exit(99);
429 }
430 // produce this type and his scope, but the scope is not recursively generated.
431 if (typeName.equals("*"))
432 {
433 tmpName = "/";
434 } else
435 {
436 tmpName = typeName.copy(0, typeName.lastIndexOf('.')).replace('.', '/');
437 if (tmpName.getLength() == 0)
438 tmpName = "/";
439 else
440 tmpName.replace('.', '/');
441 }
442 ret = produceAllTypes(tmpName, typeMgr, typeDependencies, &options, sal_False,
443 listFile, rootKey, filterTypes);
444 } else
445 {
446 // produce only this type
447 ret = produceType(typeName.replace('.', '/'), typeMgr, typeDependencies,
448 &options, listFile, rootKey, filterTypes);
449 }
450 /*
451 // produce only this type
452 ret = produceType(typeName.replace('.', '/'), typeMgr, typeDependencies,
453 &options, listFile, rootKey, filterTypes);
454 */
455 if (!ret)
456 {
457 fprintf(stderr, "%s ERROR: %s\n",
458 options.getProgramName().getStr(),
459 OString("cannot dump Type '" + typeName + "'").getStr());
460 cleanUp(sal_True);
461 exit(99);
462 }
463 }
464 while ( nIndex >= 0 );
465 } else
466 if (options.isValid("-X"))
467 {
468 } else
469 {
470 if (!bootReg.getLength())
471 {
472 // produce all types
473 if (!produceAllTypes("/", typeMgr, typeDependencies, &options, sal_True,
474 listFile, rootKey, filterTypes))
475 {
476 fprintf(stderr, "%s ERROR: %s\n",
477 options.getProgramName().getStr(),
478 "an error occurs while dumping all types.");
479 exit(99);
480 }
481 } else
482 {
483 fprintf(stderr, "%s ERROR: %s\n",
484 options.getProgramName().getStr(),
485 "dumping all types is not possible if -R option is used.");
486 exit(99);
487 }
488 }
489 }
490 catch( CannotDumpException& e)
491 {
492 fprintf(stderr, "%s ERROR: %s\n",
493 options.getProgramName().getStr(),
494 e.m_message.getStr());
495 cleanUp(sal_True);
496 exit(99);
497 }
498
499 cleanUp(sal_False);
500 return 0;
501 }
502