xref: /trunk/main/registry/tools/rdbedit.cxx (revision a180b29c)
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_registry.hxx"
26 
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include "registry/registry.hxx"
31 #include "registry/reflread.hxx"
32 #include <rtl/ustring.hxx>
33 #include <rtl/alloc.h>
34 #include <osl/process.h>
35 #include <osl/diagnose.h>
36 #include <osl/thread.h>
37 #include <osl/file.hxx>
38 
39 #ifdef SAL_UNX
40 #define SEPARATOR '/'
41 #else
42 #define SEPARATOR '\\'
43 #endif
44 
45 using namespace ::rtl;
46 using namespace ::osl;
47 
isFileUrl(const OString & fileName)48 sal_Bool isFileUrl(const OString& fileName)
49 {
50     if (fileName.indexOf("file://") == 0 )
51         return sal_True;
52     return sal_False;
53 }
54 
convertToFileUrl(const OString & fileName)55 OUString convertToFileUrl(const OString& fileName)
56 {
57     if ( isFileUrl(fileName) )
58     {
59         return OStringToOUString(fileName, osl_getThreadTextEncoding());
60     }
61 
62     OUString uUrlFileName;
63     OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
64     if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
65     {
66         OUString uWorkingDir;
67         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
68         {
69             OSL_ASSERT(false);
70         }
71         if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
72             != FileBase::E_None)
73         {
74             OSL_ASSERT(false);
75         }
76     } else
77     {
78         if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
79             != FileBase::E_None)
80         {
81             OSL_ASSERT(false);
82         }
83     }
84 
85     return uUrlFileName;
86 }
87 
88 #define U2S( s ) \
89 	OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
90 #define S2U( s ) \
91 	OStringToOUString(s, RTL_TEXTENCODING_UTF8)
92 
93 struct LessString
94 {
operator ()LessString95 	sal_Bool operator()(const OUString& str1, const OUString& str2) const
96 	{
97 		return (str1 < str2);
98 	}
99 };
100 
101 enum Command {
102   	 DELETEKEY
103 };
104 
105 class Options
106 {
107 public:
Options()108 	Options()
109 		: m_bVerbose(false)
110 		{}
~Options()111 	~Options()
112 		{}
113 
114 	bool initOptions(int ac, char* av[]);
115 
116 	OString prepareHelp();
117 	OString prepareVersion();
118 
getProgramName()119 	const OString& getProgramName()
120 		{ return m_program; }
getTypeReg()121 	const OString& getTypeReg()
122 		{ return m_typeRegName; }
getKeyName()123 	const OString& getKeyName()
124 		{ return m_keyName; }
getCommand()125 	Command getCommand()
126 		{ return m_command; }
verbose()127 	bool verbose()
128 		{ return m_bVerbose; }
129 protected:
130 	OString		m_program;
131 	OString 	m_typeRegName;
132 	OString 	m_keyName;
133     Command		m_command;
134     bool	    m_bVerbose;
135 };
136 
initOptions(int ac,char * av[])137 bool Options::initOptions(int ac, char* av[])
138 {
139 	bool bRet = true;
140 	sal_uInt16	i=1;
141 
142 	if (ac < 2)
143 	{
144 		fprintf(stderr, "%s", prepareHelp().getStr());
145 		bRet = sal_False;
146 	}
147 
148 	m_program = av[0];
149 	sal_Int32 index = -1;
150 	if ((index=m_program.lastIndexOf(SEPARATOR)) > 0)
151 	    m_program = av[0]+index+1;
152 
153 	char	*s=NULL;
154 	for (; i < ac; i++)
155 	{
156 		if (av[i][0] == '-')
157 		{
158 			switch (av[i][1])
159 			{
160 				case 'r':
161 				case 'R':
162 					if (av[i][2] == '\0')
163 					{
164 						if (i < ac - 1 && av[i+1][0] != '-')
165 						{
166 							i++;
167 							s = av[i];
168 						} else
169 						{
170 							fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
171 							bRet = sal_False;
172 							break;
173 						}
174 					} else
175 					{
176 						s = av[i] + 2;
177 					}
178 					m_typeRegName = OString(s);
179 					break;
180 				case 'd':
181 				case 'D':
182 					if (av[i][2] == '\0')
183 					{
184 						if (i < ac - 1 && av[i+1][0] != '-')
185 						{
186 							i++;
187 							s = av[i];
188 						} else
189 						{
190 							fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
191 							bRet = sal_False;
192 							break;
193 						}
194 					} else
195 					{
196 						s = av[i] + 2;
197 					}
198 					m_keyName = OString(s);
199 					break;
200 				case 'v':
201 				case 'V':
202 					if (av[i][2] != '\0')
203 					{
204 						fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
205 						bRet = sal_False;
206 					}
207 					m_bVerbose = true;
208 					break;
209 				case 'h':
210 				case '?':
211 					if (av[i][2] != '\0')
212 					{
213 						fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
214 						bRet = false;
215 					} else
216 					{
217 						fprintf(stdout, "%s", prepareHelp().getStr());
218 						exit(0);
219 					}
220 					break;
221 				default:
222 					fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
223 					bRet = false;
224 					break;
225 			}
226 		} else
227 		{
228 		    fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
229 			bRet = false;
230 		}
231 	}
232 
233 	return bRet;
234 }
235 
prepareHelp()236 OString Options::prepareHelp()
237 {
238 	OString help("\nusing: ");
239 	help += m_program + " -r<filename> <command>\n";
240 	help += "    -r<filename>  = filename specifies the name of the type registry.\n";
241 	help += "Commands:\n";
242 	help += "    -d <keyname>  = delete the specified key from the registry. Keyname\n";
243 	help += "                    specifies the name of the key that get deleted.\n";
244 	help += "    -v            = verbose output.\n";
245 	help += "    -h|-?         = print this help message and exit.\n";
246 	help += prepareVersion();
247 
248 	return help;
249 }
250 
prepareVersion()251 OString Options::prepareVersion()
252 {
253 	OString version(m_program);
254 	version += " Version 1.0\n\n";
255 	return version;
256 }
257 
258 static Options options;
259 
260 
261 #if (defined UNX) || (defined OS2) || (defined __MINGW32__)
main(int argc,char * argv[])262 int main( int argc, char * argv[] )
263 #else
264 int _cdecl main( int argc, char * argv[] )
265 #endif
266 {
267 	if ( !options.initOptions(argc, argv) )
268 	{
269 		exit(1);
270 	}
271 
272 	OUString typeRegName( convertToFileUrl(options.getTypeReg()) );
273 
274 	Registry typeReg;
275 
276 	if ( typeReg.open(typeRegName, REG_READWRITE) )
277 	{
278 		fprintf(stderr, "%s: open registry \"%s\" failed\n",
279 				options.getProgramName().getStr(), options.getTypeReg().getStr());
280 		exit(2);
281 	}
282 
283 	RegistryKey typeRoot;
284 	if ( typeReg.openRootKey(typeRoot) )
285 	{
286 		fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
287 				options.getProgramName().getStr(), options.getTypeReg().getStr());
288 		exit(3);
289 	}
290 
291 	if ( options.getCommand() == DELETEKEY )
292 	{
293 		if ( typeRoot.deleteKey(S2U(options.getKeyName())) )
294 	  	{
295 			fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\" failed\n",
296 					options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
297 			exit(4);
298 		} else {
299 		  if (options.verbose())
300 			fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\"\n",
301 					options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
302 		}
303 	}
304 
305 	typeRoot.releaseKey();
306 	if ( typeReg.close() )
307 	{
308 		fprintf(stderr, "%s: closing registry \"%s\" failed\n",
309 				options.getProgramName().getStr(), options.getTypeReg().getStr());
310 		exit(5);
311 	}
312 }
313 
314 
315