diff options
Diffstat (limited to '')
-rw-r--r-- | registry/tools/fileurl.cxx | 79 | ||||
-rw-r--r-- | registry/tools/fileurl.hxx | 37 | ||||
-rw-r--r-- | registry/tools/options.cxx | 144 | ||||
-rw-r--r-- | registry/tools/options.hxx | 60 | ||||
-rw-r--r-- | registry/tools/regmerge.cxx | 183 | ||||
-rw-r--r-- | registry/tools/regview.cxx | 113 |
6 files changed, 616 insertions, 0 deletions
diff --git a/registry/tools/fileurl.cxx b/registry/tools/fileurl.cxx new file mode 100644 index 000000000..dec3be5e5 --- /dev/null +++ b/registry/tools/fileurl.cxx @@ -0,0 +1,79 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "fileurl.hxx" + +#include <rtl/ustring.hxx> +#include <osl/file.hxx> +#include <osl/process.h> +#include <osl/thread.h> + +#include <string.h> + +#ifdef SAL_UNX +#define SEPARATOR '/' +#else +#define SEPARATOR '\\' +#endif + +using osl::FileBase; + +namespace registry::tools +{ + +OUString convertToFileUrl(char const * filename, sal_Int32 length) +{ + OUString const uFileName(filename, length, osl_getThreadTextEncoding()); + if (strncmp(filename, "file://", 7) == 0) + { + // already a FileUrl. + return uFileName; + } + + OUString uFileUrl; + if (length > 0) + { + if (filename[0] != SEPARATOR) + { + // relative path name. + OUString uWorkingDir; + if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None) + { + assert(false); + } + if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uFileUrl) != FileBase::E_None) + { + assert(false); + } + } + else + { + // absolute path name. + if (FileBase::getFileURLFromSystemPath(uFileName, uFileUrl) != FileBase::E_None) + { + assert(false); + } + } + } + return uFileUrl; +} + +} // namespace registry::tools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/tools/fileurl.hxx b/registry/tools/fileurl.hxx new file mode 100644 index 000000000..684ca8c3c --- /dev/null +++ b/registry/tools/fileurl.hxx @@ -0,0 +1,37 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_TOOLS_FILEURL_HXX +#define INCLUDED_REGISTRY_TOOLS_FILEURL_HXX + +#include <rtl/ustring.hxx> + +namespace registry +{ +namespace tools +{ + +OUString convertToFileUrl(char const * filename, sal_Int32 length); + +} // namespace tools +} // namespace registry + +#endif /* INCLUDED_REGISTRY_TOOLS_FILEURL_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/tools/options.cxx b/registry/tools/options.cxx new file mode 100644 index 000000000..81d1131f7 --- /dev/null +++ b/registry/tools/options.cxx @@ -0,0 +1,144 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include "options.hxx" + +#include <osl/diagnose.h> + +#include <stdio.h> + +namespace registry::tools +{ + +Options::Options (char const * program) + : m_program (program) +{} + +Options::~Options() +{} + +// static +bool Options::checkArgument(std::vector< std::string> & rArgs, char const * arg, size_t len) +{ + bool result = ((arg != nullptr) && (len > 0)); + OSL_PRECOND(result, "registry::tools::Options::checkArgument(): invalid arguments"); + if (result) + { + switch (arg[0]) + { + case '@': + result = len > 1; + if (result) + { + // "@<cmdfile>" + result = Options::checkCommandFile(rArgs, &(arg[1])); + } + break; + case '-': + result = len > 1; + if (result) + { + // "-<option>" + std::string option (&(arg[0]), 2); + rArgs.push_back(option); + if (len > 2) + { + // "-<option><param>" + std::string param(&(arg[2]), len - 2); + rArgs.push_back(param); + } + } + break; + default: + rArgs.push_back(std::string(arg, len)); + break; + } + } + return result; +} + +// static +bool Options::checkCommandFile(std::vector< std::string > & rArgs, char const * filename) +{ + FILE * fp = fopen(filename, "r"); + if (fp == nullptr) + { + fprintf(stderr, "ERROR: Can't open command file \"%s\"\n", filename); + return false; + } + + std::string buffer; + buffer.reserve(256); + + bool quoted = false; + int c = EOF; + while ((c = fgetc(fp)) != EOF) + { + switch(c) + { + case '\"': + quoted = !quoted; + break; + case ' ': + case '\t': + case '\r': + case '\n': + if (!quoted) + { + if (!buffer.empty()) + { + if (!checkArgument(rArgs, buffer.c_str(), buffer.size())) + { + // failure. + (void) fclose(fp); + return false; + } + buffer.clear(); + } + break; + } + [[fallthrough]]; + default: + buffer.push_back(sal::static_int_cast<char>(c)); + break; + } + } + return fclose(fp) == 0; +} + +bool Options::initOptions (std::vector< std::string > & rArgs) +{ + return initOptions_Impl (rArgs); +} + +bool Options::badOption (char const * reason, char const * option) const +{ + (void) fprintf(stderr, "%s: %s option '%s'\n", m_program.c_str(), reason, option); + return printUsage(); +} + +bool Options::printUsage() const +{ + printUsage_Impl(); + return false; +} + +} // namespace registry::tools + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/tools/options.hxx b/registry/tools/options.hxx new file mode 100644 index 000000000..a0ae71b49 --- /dev/null +++ b/registry/tools/options.hxx @@ -0,0 +1,60 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#ifndef INCLUDED_REGISTRY_TOOLS_OPTIONS_HXX +#define INCLUDED_REGISTRY_TOOLS_OPTIONS_HXX + +#include <string> +#include <vector> + +namespace registry +{ +namespace tools +{ +class Options +{ + std::string m_program; + + Options (Options const &) = delete; + Options & operator= (Options const &) = delete; + +public: + explicit Options (char const * program); + virtual ~Options(); + + static bool checkArgument (std::vector< std::string > & rArgs, char const * arg, size_t len); + + bool initOptions (std::vector< std::string > & rArgs); + bool badOption (char const * reason, char const * option) const; + + bool printUsage() const; + +protected: + static bool checkCommandFile(std::vector< std::string > & rArgs, char const * filename); + + virtual bool initOptions_Impl(std::vector< std::string > & rArgs) = 0; + virtual void printUsage_Impl() const = 0; +}; + +} // namespace tools +} // namespace registry + +#endif /* INCLUDED_REGISTRY_TOOLS_OPTIONS_HXX */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/tools/regmerge.cxx b/registry/tools/regmerge.cxx new file mode 100644 index 000000000..409773a70 --- /dev/null +++ b/registry/tools/regmerge.cxx @@ -0,0 +1,183 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <registry/registry.hxx> +#include "fileurl.hxx" +#include "options.hxx" + +#include <rtl/ustring.hxx> +#include <sal/log.hxx> + +#include <stdio.h> +#include <string.h> + +using namespace registry::tools; + +namespace { + +class Options_Impl : public Options +{ + bool m_bVerbose; + +public: + explicit Options_Impl (char const * program) + : Options(program), m_bVerbose(false) + {} + bool isVerbose() const { return m_bVerbose; } + +protected: + virtual void printUsage_Impl() const override; + virtual bool initOptions_Impl(std::vector< std::string > & rArgs) override; +}; + +} + +void Options_Impl::printUsage_Impl() const +{ + fprintf(stderr, "using: regmerge [-v|--verbose] mergefile mergeKeyName regfile_1 ... regfile_n\n"); + fprintf(stderr, " regmerge @regcmds\nOptions:\n"); + fprintf(stderr, " -v, --verbose : verbose output on stdout.\n"); + fprintf(stderr, " mergefile : specifies the merged registry file. If this file doesn't exists,\n"); + fprintf(stderr, " it is created.\n"); + fprintf(stderr, " mergeKeyName : specifies the merge key, everything is merged under this key.\n"); + fprintf(stderr, " If this key doesn't exists, it is created.\n"); + fprintf(stderr, " regfile_1..n : specifies one or more registry files which are merged.\n"); +} + +bool Options_Impl::initOptions_Impl (std::vector< std::string > & rArgs) +{ + std::vector< std::string >::iterator first = rArgs.begin(), last = rArgs.end(); + if ((first != last) && ((*first)[0] == '-')) + { + std::string option(*first); + if ((option.compare("-v") == 0) || (option.compare("--verbose") == 0)) + { + m_bVerbose = true; + } + else if ((option.compare("-h") == 0) || (option.compare("-?") == 0)) + { + return printUsage(); + } + else + { + return badOption("unknown", option.c_str()); + } + (void) rArgs.erase(first); + } + return true; +} + +#if (defined UNX) +int main( int argc, char * argv[] ) +#else +int __cdecl main( int argc, char * argv[] ) +#endif +{ + try + { + Options_Impl options(argv[0]); + + std::vector< std::string > args; + for (int i = 1; i < argc; i++) + { + if (!Options::checkArgument(args, argv[i], strlen(argv[i]))) + { + options.printUsage(); + return 1; + } + } + if (!options.initOptions(args)) + { + return 1; + } + if (args.size() < 3) + { + options.printUsage(); + return 1; + } + + Registry reg; + OUString regName( convertToFileUrl(args[0].c_str(), args[0].size()) ); + if (reg.open(regName, RegAccessMode::READWRITE) != RegError::NO_ERROR) + { + if (reg.create(regName) != RegError::NO_ERROR) + { + if (options.isVerbose()) + fprintf(stderr, "open registry \"%s\" failed\n", args[0].c_str()); + return -1; + } + } + + RegistryKey rootKey; + if (reg.openRootKey(rootKey) != RegError::NO_ERROR) + { + if (options.isVerbose()) + fprintf(stderr, "open root key of registry \"%s\" failed\n", args[0].c_str()); + return -4; + } + + OUString mergeKeyName( OUString::createFromAscii(args[1].c_str()) ); + for (size_t i = 2; i < args.size(); i++) + { + OUString targetRegName( convertToFileUrl(args[i].c_str(), args[i].size()) ); + RegError _ret = reg.mergeKey(rootKey, mergeKeyName, targetRegName, options.isVerbose()); + if (_ret != RegError::NO_ERROR) + { + if (_ret == RegError::MERGE_CONFLICT) + { + if (options.isVerbose()) + fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n", + args[i].c_str(), args[1].c_str(), args[0].c_str()); + } + else + { + if (options.isVerbose()) + fprintf(stderr, "ERROR: merging registry \"%s\" under key \"%s\" in registry \"%s\" failed.\n", + args[i].c_str(), args[1].c_str(), args[0].c_str()); + return -2; + } + } + else + { + if (options.isVerbose()) + fprintf(stderr, "merging registry \"%s\" under key \"%s\" in registry \"%s\".\n", + args[i].c_str(), args[1].c_str(), args[0].c_str()); + } + } + + rootKey.releaseKey(); + if (reg.close() != RegError::NO_ERROR) + { + if (options.isVerbose()) + fprintf(stderr, "closing registry \"%s\" failed\n", args[0].c_str()); + return -5; + } + } + catch (const std::exception &e) + { + SAL_WARN("registry", "Fatal exception: " << e.what()); + return -5; + } + + + return 0; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/registry/tools/regview.cxx b/registry/tools/regview.cxx new file mode 100644 index 000000000..56aa343f4 --- /dev/null +++ b/registry/tools/regview.cxx @@ -0,0 +1,113 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + + +#include <regapi.hxx> +#include "fileurl.hxx" + +#include <rtl/ustring.hxx> + +#include <stdio.h> +#include <string.h> + +using namespace registry::tools; + +#if (defined UNX) +int main( int argc, char * argv[] ) +#else +int __cdecl main( int argc, char * argv[] ) +#endif +{ + try + { + RegHandle hReg; + RegKeyHandle hRootKey, hKey; + + if (argc < 2 || argc > 3) + { + fprintf(stderr, "using: regview registryfile [keyName]\n"); + exit(1); + } + + OUString regName( convertToFileUrl(argv[1], strlen(argv[1])) ); + if (reg_openRegistry(regName.pData, &hReg) != RegError::NO_ERROR) + { + fprintf(stderr, "open registry \"%s\" failed\n", argv[1]); + exit(1); + } + + if (reg_openRootKey(hReg, &hRootKey) == RegError::NO_ERROR) + { + if (argc == 3) + { + OUString keyName( OUString::createFromAscii(argv[2]) ); + if (reg_openKey(hRootKey, keyName.pData, &hKey) == RegError::NO_ERROR) + { + if (reg_dumpRegistry(hKey) != RegError::NO_ERROR) + { + fprintf(stderr, "dumping registry \"%s\" failed\n", argv[1]); + } + + if (reg_closeKey(hKey) != RegError::NO_ERROR) + { + fprintf(stderr, "closing key \"%s\" of registry \"%s\" failed\n", + argv[2], argv[1]); + } + } + else + { + fprintf(stderr, "key \"%s\" not exists in registry \"%s\"\n", + argv[2], argv[1]); + } + } + else + { + if (reg_dumpRegistry(hRootKey) != RegError::NO_ERROR) + { + fprintf(stderr, "dumping registry \"%s\" failed\n", argv[1]); + } + } + + if (reg_closeKey(hRootKey) != RegError::NO_ERROR) + { + fprintf(stderr, "closing root key of registry \"%s\" failed\n", argv[1]); + } + } + else + { + fprintf(stderr, "open root key of registry \"%s\" failed\n", argv[1]); + } + + if (reg_closeRegistry(hReg) != RegError::NO_ERROR) + { + fprintf(stderr, "closing registry \"%s\" failed\n", argv[1]); + exit(1); + } + + return 0; + } + catch (std::exception& e) + { + fprintf(stderr, "failure: \"%s\"\n", e.what()); + return 1; + } +} + + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |