1*01797804SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*01797804SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*01797804SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*01797804SAndrew Rist  * distributed with this work for additional information
6*01797804SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*01797804SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*01797804SAndrew Rist  * "License"); you may not use this file except in compliance
9*01797804SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*01797804SAndrew Rist  *
11*01797804SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*01797804SAndrew Rist  *
13*01797804SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*01797804SAndrew Rist  * software distributed under the License is distributed on an
15*01797804SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*01797804SAndrew Rist  * KIND, either express or implied.  See the License for the
17*01797804SAndrew Rist  * specific language governing permissions and limitations
18*01797804SAndrew Rist  * under the License.
19*01797804SAndrew Rist  *
20*01797804SAndrew Rist  *************************************************************/
21*01797804SAndrew Rist 
22*01797804SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "sal/main.h"
27cdf0e10cSrcweir #include <osl/diagnose.h>
28cdf0e10cSrcweir #include <osl/file.h>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <cppuhelper/bootstrap.hxx>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/registry/XSimpleRegistry.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
35cdf0e10cSrcweir 
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace ::rtl;
38cdf0e10cSrcweir using namespace ::com::sun::star;
39cdf0e10cSrcweir using namespace ::com::sun::star::uno;
40cdf0e10cSrcweir 
print_options()41cdf0e10cSrcweir static void print_options() SAL_THROW( () )
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     printf(
44cdf0e10cSrcweir         "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n"
45cdf0e10cSrcweir         "Inserts a singleton entry into rdb.\n"
46cdf0e10cSrcweir         "Option -r revokes given entries, -ra revokes all entries.\n" );
47cdf0e10cSrcweir }
48cdf0e10cSrcweir 
49cdf0e10cSrcweir //==================================================================================================
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)50cdf0e10cSrcweir SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
51cdf0e10cSrcweir {
52cdf0e10cSrcweir     if (argc < 3)
53cdf0e10cSrcweir     {
54cdf0e10cSrcweir         print_options();
55cdf0e10cSrcweir         return 1;
56cdf0e10cSrcweir     }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     bool insert_entry = true;
59cdf0e10cSrcweir     bool remove_all = false;
60cdf0e10cSrcweir     int nPos = 1;
61cdf0e10cSrcweir     if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ])
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ])
64cdf0e10cSrcweir         {
65cdf0e10cSrcweir             remove_all = true;
66cdf0e10cSrcweir         }
67cdf0e10cSrcweir         else if ('\0' != argv[ nPos ][ 2 ])
68cdf0e10cSrcweir         {
69cdf0e10cSrcweir             print_options();
70cdf0e10cSrcweir             return 1;
71cdf0e10cSrcweir         }
72cdf0e10cSrcweir         insert_entry = false;
73cdf0e10cSrcweir         ++nPos;
74cdf0e10cSrcweir     }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) );
77cdf0e10cSrcweir     OUString file_url;
78cdf0e10cSrcweir     oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData );
79cdf0e10cSrcweir     if (osl_File_E_None != rc)
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         fprintf( stderr, "\nerror: cannot make file url out of %s\n", argv[ nPos ] );
82cdf0e10cSrcweir         return 1;
83cdf0e10cSrcweir     }
84cdf0e10cSrcweir     ++nPos;
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	try
87cdf0e10cSrcweir 	{
88cdf0e10cSrcweir         Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() );
89cdf0e10cSrcweir         xSimReg->open( file_url, sal_False, sal_True );
90cdf0e10cSrcweir         Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() );
91cdf0e10cSrcweir 
92cdf0e10cSrcweir         if (remove_all)
93cdf0e10cSrcweir         {
94cdf0e10cSrcweir             try
95cdf0e10cSrcweir             {
96cdf0e10cSrcweir                 xRoot->deleteKey( OUSTR("SINGLETONS") );
97cdf0e10cSrcweir             }
98cdf0e10cSrcweir             catch (registry::InvalidRegistryException & exc)
99cdf0e10cSrcweir             {
100cdf0e10cSrcweir                 OString cstr_msg(
101cdf0e10cSrcweir                     OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
102cdf0e10cSrcweir                 fprintf(
103cdf0e10cSrcweir                     stderr, "\nwarning: removing all singletons failed: %s\n",
104cdf0e10cSrcweir                     cstr_msg.getStr() );
105cdf0e10cSrcweir             }
106cdf0e10cSrcweir         }
107cdf0e10cSrcweir         else
108cdf0e10cSrcweir         {
109cdf0e10cSrcweir             Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) );
110cdf0e10cSrcweir             if (! xKey.is())
111cdf0e10cSrcweir                 xKey = xRoot->createKey( OUSTR("SINGLETONS") );
112cdf0e10cSrcweir 
113cdf0e10cSrcweir             for ( ; nPos < argc; ++nPos )
114cdf0e10cSrcweir             {
115cdf0e10cSrcweir                 OUString singleton( OUString::createFromAscii( argv[ nPos ] ) );
116cdf0e10cSrcweir                 OUString service;
117cdf0e10cSrcweir                 sal_Int32 eq = singleton.indexOf( '=' );
118cdf0e10cSrcweir                 if (eq >= 0)
119cdf0e10cSrcweir                 {
120cdf0e10cSrcweir                     service = singleton.copy( eq +1 );
121cdf0e10cSrcweir                     singleton = singleton.copy( 0, eq );
122cdf0e10cSrcweir                 }
123cdf0e10cSrcweir 
124cdf0e10cSrcweir                 if (insert_entry)
125cdf0e10cSrcweir                 {
126cdf0e10cSrcweir                     if (service.getLength())
127cdf0e10cSrcweir                     {
128cdf0e10cSrcweir                         Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) );
129cdf0e10cSrcweir                         if (! xEntry.is())
130cdf0e10cSrcweir                             xEntry = xKey->createKey( singleton );
131cdf0e10cSrcweir                         xEntry->setStringValue( service );
132cdf0e10cSrcweir                     }
133cdf0e10cSrcweir                     else
134cdf0e10cSrcweir                     {
135cdf0e10cSrcweir                         OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
136cdf0e10cSrcweir                         fprintf(
137cdf0e10cSrcweir                             stderr, "\nwarning: no service name given for singleton %s!\n",
138cdf0e10cSrcweir                             entry.getStr() );
139cdf0e10cSrcweir                     }
140cdf0e10cSrcweir                 }
141cdf0e10cSrcweir                 else
142cdf0e10cSrcweir                 {
143cdf0e10cSrcweir                     try
144cdf0e10cSrcweir                     {
145cdf0e10cSrcweir                         xKey->deleteKey( singleton );
146cdf0e10cSrcweir                     }
147cdf0e10cSrcweir                     catch (registry::InvalidRegistryException & exc)
148cdf0e10cSrcweir                     {
149cdf0e10cSrcweir                         OString cstr_singleton(
150cdf0e10cSrcweir                             OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
151cdf0e10cSrcweir                         OString cstr_msg(
152cdf0e10cSrcweir                             OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
153cdf0e10cSrcweir                         fprintf(
154cdf0e10cSrcweir                             stderr, "\nwarning: singleton %s is not registered: %s\n",
155cdf0e10cSrcweir                             cstr_singleton.getStr(), cstr_msg.getStr() );
156cdf0e10cSrcweir                     }
157cdf0e10cSrcweir                 }
158cdf0e10cSrcweir             }
159cdf0e10cSrcweir         }
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         return 0;
162cdf0e10cSrcweir 	}
163cdf0e10cSrcweir 	catch (Exception & rExc)
164cdf0e10cSrcweir 	{
165cdf0e10cSrcweir 		OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
166cdf0e10cSrcweir         fprintf( stderr, "\nerror: %s\n", msg.getStr() );
167cdf0e10cSrcweir         return 1;
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir }
170