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
26 #include "sal/main.h"
27 #include <osl/diagnose.h>
28 #include <osl/file.h>
29
30 #include <cppuhelper/bootstrap.hxx>
31
32 #include <com/sun/star/registry/XSimpleRegistry.hpp>
33
34 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
35
36
37 using namespace ::rtl;
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40
print_options()41 static void print_options() SAL_THROW( () )
42 {
43 printf(
44 "\nusage: regsingleton [-r|-ra] registry_file singleton_name[=service_name] ...\n\n"
45 "Inserts a singleton entry into rdb.\n"
46 "Option -r revokes given entries, -ra revokes all entries.\n" );
47 }
48
49 //==================================================================================================
SAL_IMPLEMENT_MAIN_WITH_ARGS(argc,argv)50 SAL_IMPLEMENT_MAIN_WITH_ARGS(argc, argv)
51 {
52 if (argc < 3)
53 {
54 print_options();
55 return 1;
56 }
57
58 bool insert_entry = true;
59 bool remove_all = false;
60 int nPos = 1;
61 if ('-' == argv[ nPos ][ 0 ] && 'r' == argv[ nPos ][ 1 ])
62 {
63 if ('a' == argv[ nPos ][ 2 ] && '\0' == argv[ nPos ][ 3 ])
64 {
65 remove_all = true;
66 }
67 else if ('\0' != argv[ nPos ][ 2 ])
68 {
69 print_options();
70 return 1;
71 }
72 insert_entry = false;
73 ++nPos;
74 }
75
76 OUString sys_path( OUString::createFromAscii( argv[ nPos ] ) );
77 OUString file_url;
78 oslFileError rc = osl_getFileURLFromSystemPath( sys_path.pData, &file_url.pData );
79 if (osl_File_E_None != rc)
80 {
81 fprintf( stderr, "\nerror: cannot make file url out of %s\n", argv[ nPos ] );
82 return 1;
83 }
84 ++nPos;
85
86 try
87 {
88 Reference< registry::XSimpleRegistry > xSimReg( ::cppu::createSimpleRegistry() );
89 xSimReg->open( file_url, sal_False, sal_True );
90 Reference< registry::XRegistryKey > xRoot( xSimReg->getRootKey() );
91
92 if (remove_all)
93 {
94 try
95 {
96 xRoot->deleteKey( OUSTR("SINGLETONS") );
97 }
98 catch (registry::InvalidRegistryException & exc)
99 {
100 OString cstr_msg(
101 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
102 fprintf(
103 stderr, "\nwarning: removing all singletons failed: %s\n",
104 cstr_msg.getStr() );
105 }
106 }
107 else
108 {
109 Reference< registry::XRegistryKey > xKey( xRoot->openKey( OUSTR("SINGLETONS") ) );
110 if (! xKey.is())
111 xKey = xRoot->createKey( OUSTR("SINGLETONS") );
112
113 for ( ; nPos < argc; ++nPos )
114 {
115 OUString singleton( OUString::createFromAscii( argv[ nPos ] ) );
116 OUString service;
117 sal_Int32 eq = singleton.indexOf( '=' );
118 if (eq >= 0)
119 {
120 service = singleton.copy( eq +1 );
121 singleton = singleton.copy( 0, eq );
122 }
123
124 if (insert_entry)
125 {
126 if (service.getLength())
127 {
128 Reference< registry::XRegistryKey > xEntry( xKey->openKey( singleton ) );
129 if (! xEntry.is())
130 xEntry = xKey->createKey( singleton );
131 xEntry->setStringValue( service );
132 }
133 else
134 {
135 OString entry( OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
136 fprintf(
137 stderr, "\nwarning: no service name given for singleton %s!\n",
138 entry.getStr() );
139 }
140 }
141 else
142 {
143 try
144 {
145 xKey->deleteKey( singleton );
146 }
147 catch (registry::InvalidRegistryException & exc)
148 {
149 OString cstr_singleton(
150 OUStringToOString( singleton, RTL_TEXTENCODING_ASCII_US ) );
151 OString cstr_msg(
152 OUStringToOString( exc.Message, RTL_TEXTENCODING_ASCII_US ) );
153 fprintf(
154 stderr, "\nwarning: singleton %s is not registered: %s\n",
155 cstr_singleton.getStr(), cstr_msg.getStr() );
156 }
157 }
158 }
159 }
160
161 return 0;
162 }
163 catch (Exception & rExc)
164 {
165 OString msg( OUStringToOString( rExc.Message, RTL_TEXTENCODING_ASCII_US ) );
166 fprintf( stderr, "\nerror: %s\n", msg.getStr() );
167 return 1;
168 }
169 }
170