xref: /trunk/main/registry/tools/rdbedit.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb) !
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_registry.hxx"
30 
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include "registry/registry.hxx"
35 #include "registry/reflread.hxx"
36 #include <rtl/ustring.hxx>
37 #include <rtl/alloc.h>
38 #include <osl/process.h>
39 #include <osl/diagnose.h>
40 #include <osl/thread.h>
41 #include <osl/file.hxx>
42 
43 #ifdef SAL_UNX
44 #define SEPARATOR '/'
45 #else
46 #define SEPARATOR '\\'
47 #endif
48 
49 using namespace ::rtl;
50 using namespace ::osl;
51 
52 sal_Bool isFileUrl(const OString& fileName)
53 {
54     if (fileName.indexOf("file://") == 0 )
55         return sal_True;
56     return sal_False;
57 }
58 
59 OUString convertToFileUrl(const OString& fileName)
60 {
61     if ( isFileUrl(fileName) )
62     {
63         return OStringToOUString(fileName, osl_getThreadTextEncoding());
64     }
65 
66     OUString uUrlFileName;
67     OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
68     if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
69     {
70         OUString uWorkingDir;
71         if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
72         {
73             OSL_ASSERT(false);
74         }
75         if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName)
76             != FileBase::E_None)
77         {
78             OSL_ASSERT(false);
79         }
80     } else
81     {
82         if (FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName)
83             != FileBase::E_None)
84         {
85             OSL_ASSERT(false);
86         }
87     }
88 
89     return uUrlFileName;
90 }
91 
92 #define U2S( s ) \
93     OUStringToOString(s, RTL_TEXTENCODING_UTF8).getStr()
94 #define S2U( s ) \
95     OStringToOUString(s, RTL_TEXTENCODING_UTF8)
96 
97 struct LessString
98 {
99     sal_Bool operator()(const OUString& str1, const OUString& str2) const
100     {
101         return (str1 < str2);
102     }
103 };
104 
105 enum Command {
106      DELETEKEY
107 };
108 
109 class Options
110 {
111 public:
112     Options()
113         : m_bVerbose(false)
114         {}
115     ~Options()
116         {}
117 
118     bool initOptions(int ac, char* av[]);
119 
120     OString prepareHelp();
121     OString prepareVersion();
122 
123     const OString& getProgramName()
124         { return m_program; }
125     const OString& getTypeReg()
126         { return m_typeRegName; }
127     const OString& getKeyName()
128         { return m_keyName; }
129     const Command getCommand()
130         { return m_command; }
131     bool verbose()
132         { return m_bVerbose; }
133 protected:
134     OString     m_program;
135     OString     m_typeRegName;
136     OString     m_keyName;
137     Command     m_command;
138     bool        m_bVerbose;
139 };
140 
141 bool Options::initOptions(int ac, char* av[])
142 {
143     bool bRet = true;
144     sal_uInt16  i=1;
145 
146     if (ac < 2)
147     {
148         fprintf(stderr, "%s", prepareHelp().getStr());
149         bRet = sal_False;
150     }
151 
152     m_program = av[0];
153     sal_Int32 index = -1;
154     if ((index=m_program.lastIndexOf(SEPARATOR)) > 0)
155         m_program = av[0]+index+1;
156 
157     char    *s=NULL;
158     for (; i < ac; i++)
159     {
160         if (av[i][0] == '-')
161         {
162             switch (av[i][1])
163             {
164                 case 'r':
165                 case 'R':
166                     if (av[i][2] == '\0')
167                     {
168                         if (i < ac - 1 && av[i+1][0] != '-')
169                         {
170                             i++;
171                             s = av[i];
172                         } else
173                         {
174                             fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
175                             bRet = sal_False;
176                             break;
177                         }
178                     } else
179                     {
180                         s = av[i] + 2;
181                     }
182                     m_typeRegName = OString(s);
183                     break;
184                 case 'd':
185                 case 'D':
186                     if (av[i][2] == '\0')
187                     {
188                         if (i < ac - 1 && av[i+1][0] != '-')
189                         {
190                             i++;
191                             s = av[i];
192                         } else
193                         {
194                             fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
195                             bRet = sal_False;
196                             break;
197                         }
198                     } else
199                     {
200                         s = av[i] + 2;
201                     }
202                     m_keyName = OString(s);
203                     break;
204                 case 'v':
205                 case 'V':
206                     if (av[i][2] != '\0')
207                     {
208                         fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
209                         bRet = sal_False;
210                     }
211                     m_bVerbose = true;
212                     break;
213                 case 'h':
214                 case '?':
215                     if (av[i][2] != '\0')
216                     {
217                         fprintf(stderr, "%s: invalid option '%s'\n", m_program.getStr(), av[i]);
218                         bRet = false;
219                     } else
220                     {
221                         fprintf(stdout, "%s", prepareHelp().getStr());
222                         exit(0);
223                     }
224                     break;
225                 default:
226                     fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
227                     bRet = false;
228                     break;
229             }
230         } else
231         {
232             fprintf(stderr, "%s: unknown option '%s'\n", m_program.getStr(), av[i]);
233             bRet = false;
234         }
235     }
236 
237     return bRet;
238 }
239 
240 OString Options::prepareHelp()
241 {
242     OString help("\nusing: ");
243     help += m_program + " -r<filename> <command>\n";
244     help += "    -r<filename>  = filename specifies the name of the type registry.\n";
245     help += "Commands:\n";
246     help += "    -d <keyname>  = delete the specified key from the registry. Keyname\n";
247     help += "                    specifies the name of the key that get deleted.\n";
248     help += "    -v            = verbose output.\n";
249     help += "    -h|-?         = print this help message and exit.\n";
250     help += prepareVersion();
251 
252     return help;
253 }
254 
255 OString Options::prepareVersion()
256 {
257     OString version(m_program);
258     version += " Version 1.0\n\n";
259     return version;
260 }
261 
262 static Options options;
263 
264 
265 #if (defined UNX) || (defined OS2) || (defined __MINGW32__)
266 int main( int argc, char * argv[] )
267 #else
268 int _cdecl main( int argc, char * argv[] )
269 #endif
270 {
271     if ( !options.initOptions(argc, argv) )
272     {
273         exit(1);
274     }
275 
276     OUString typeRegName( convertToFileUrl(options.getTypeReg()) );
277 
278     Registry typeReg;
279 
280     if ( typeReg.open(typeRegName, REG_READWRITE) )
281     {
282         fprintf(stderr, "%s: open registry \"%s\" failed\n",
283                 options.getProgramName().getStr(), options.getTypeReg().getStr());
284         exit(2);
285     }
286 
287     RegistryKey typeRoot;
288     if ( typeReg.openRootKey(typeRoot) )
289     {
290         fprintf(stderr, "%s: open root key of registry \"%s\" failed\n",
291                 options.getProgramName().getStr(), options.getTypeReg().getStr());
292         exit(3);
293     }
294 
295     if ( options.getCommand() == DELETEKEY )
296     {
297         if ( typeRoot.deleteKey(S2U(options.getKeyName())) )
298         {
299             fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\" failed\n",
300                     options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
301             exit(4);
302         } else {
303           if (options.verbose())
304             fprintf(stderr, "%s: delete key \"%s\" of registry \"%s\"\n",
305                     options.getProgramName().getStr(), options.getKeyName().getStr(), options.getTypeReg().getStr());
306         }
307     }
308 
309     typeRoot.releaseKey();
310     if ( typeReg.close() )
311     {
312         fprintf(stderr, "%s: closing registry \"%s\" failed\n",
313                 options.getProgramName().getStr(), options.getTypeReg().getStr());
314         exit(5);
315     }
316 }
317 
318 
319