diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 00bde2f5a7fd0345b77f26b2b2d411e59b804442..d5b33b45c6c2be7015045b09c2cdefc91735ff86 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,22 +5,22 @@ include_directories(. ${mhwd_SOURCE_DIR}/libmhwd) ### set( HEADERS - Config.hpp - Data.hpp - Device.hpp - Enums.hpp - Mhwd.hpp - Printer.hpp - Transaction.hpp + Config.hpp + ConsoleWriter.hpp + Data.hpp + Device.hpp + Enums.hpp + Mhwd.hpp + Transaction.hpp ) set( SOURCES Config.cpp + ConsoleWriter.cpp Data.cpp Device.cpp main.cpp Mhwd.cpp - Printer.cpp Transaction.cpp ) diff --git a/src/Config.cpp b/src/Config.cpp index 42209cfe04d25b3cb32609005ee5b0265a8a005e..add85f4a35cebc9f840e543103c0ef5f96852867 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> diff --git a/src/Printer.cpp b/src/ConsoleWriter.cpp similarity index 79% rename from src/Printer.cpp rename to src/ConsoleWriter.cpp index 69a4590cc4a3245901853d5dee8dda270569b422..5aa888563bb137594973bd7d263b430c3a07ad32 100644 --- a/src/Printer.cpp +++ b/src/ConsoleWriter.cpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -22,7 +22,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ -#include "Printer.hpp" +#include "ConsoleWriter.hpp" #include <hd.h> @@ -32,56 +32,56 @@ #include <string> #include <vector> -void Printer::printStatus(std::string statusMsg) const +void ConsoleWriter::printStatus(std::string statusMsg) const { std::cout << CONSOLE_RED_MESSAGE_COLOR << "> " << CONSOLE_COLOR_RESET << statusMsg << std::endl; } -void Printer::printError(std::string errorMsg) const +void ConsoleWriter::printError(std::string errorMsg) const { std::cout << CONSOLE_RED_MESSAGE_COLOR << "Error: " << CONSOLE_COLOR_RESET << errorMsg << std::endl; } -void Printer::printWarning(std::string warningMsg) const +void ConsoleWriter::printWarning(std::string warningMsg) const { std::cout << CONSOLE_RED_MESSAGE_COLOR << "Warning: " << CONSOLE_COLOR_RESET << warningMsg << std::endl; } -void Printer::printMessage(MHWD::MESSAGETYPE type, std::string msg) const +void ConsoleWriter::printMessage(MHWD::MESSAGETYPE type, std::string msg) const { - switch(type) - { - case MHWD::MESSAGETYPE::CONSOLE_OUTPUT: - std::cout << CONSOLE_TEXT_OUTPUT_COLOR << msg << CONSOLE_COLOR_RESET; - break; - case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START: - printStatus("Installing dependency " + msg + "..."); - break; - case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END: - printStatus("Successfully installed dependency " + msg); - break; - case MHWD::MESSAGETYPE::INSTALL_START: - printStatus("Installing " + msg + "..."); - break; - case MHWD::MESSAGETYPE::INSTALL_END: - printStatus("Successfully installed " + msg); - break; - case MHWD::MESSAGETYPE::REMOVE_START: - printStatus("Removing " + msg + "..."); - break; - case MHWD::MESSAGETYPE::REMOVE_END: - printStatus("Successfully removed " + msg); - break; - default: - printError("You shouldn't see this?! Unknown message type!"); - break; - } + switch(type) + { + case MHWD::MESSAGETYPE::CONSOLE_OUTPUT: + std::cout << CONSOLE_TEXT_OUTPUT_COLOR << msg << CONSOLE_COLOR_RESET; + break; + case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START: + printStatus("Installing dependency " + msg + "..."); + break; + case MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END: + printStatus("Successfully installed dependency " + msg); + break; + case MHWD::MESSAGETYPE::INSTALL_START: + printStatus("Installing " + msg + "..."); + break; + case MHWD::MESSAGETYPE::INSTALL_END: + printStatus("Successfully installed " + msg); + break; + case MHWD::MESSAGETYPE::REMOVE_START: + printStatus("Removing " + msg + "..."); + break; + case MHWD::MESSAGETYPE::REMOVE_END: + printStatus("Successfully removed " + msg); + break; + default: + printError("You shouldn't see this?! Unknown message type!"); + break; + } } -void Printer::printHelp() const +void ConsoleWriter::printHelp() const { std::cout << "Usage: mhwd [OPTIONS] <config(s)>\n\n" << " --pci\t\t\t\t\tlist only pci devices and driver configs\n" @@ -103,16 +103,16 @@ void Printer::printHelp() const << " --pmroot <path>\t\t\tset package manager root\n" << std::endl; } -void Printer::printVersion(std::string versionMhwd, std::string yearCopy) const +void ConsoleWriter::printVersion(std::string versionMhwd, std::string yearCopy) const { - std::cout << "Manjaro Hardware Detection v"<< versionMhwd <<"\n\n" + std::cout << "Manjaro Hardware Detection v"<< versionMhwd <<"\n\n" << "Copyright (C) "<< yearCopy <<" Manjaro Linux Developers\n" << "This is free software licensed under GNU GPL v3.0\n" - << "FITNESS FOR A PARTICULAR PURPOSE.\n" + << "FITNESS FOR A PARTICULAR PURPOSE.\n" << std::endl; } -void Printer::listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string type) const +void ConsoleWriter::listDevices(const std::vector<std::shared_ptr<Device>>& devices, std::string type) const { if (devices.empty()) { @@ -142,7 +142,7 @@ void Printer::listDevices(const std::vector<std::shared_ptr<Device>>& devices, s } } -void Printer::listConfigs(const std::vector<std::shared_ptr<Config>>& configs, std::string header) const +void ConsoleWriter::listConfigs(const std::vector<std::shared_ptr<Config>>& configs, std::string header) const { printStatus(header); printLine(); @@ -161,7 +161,7 @@ void Printer::listConfigs(const std::vector<std::shared_ptr<Config>>& configs, s std::cout << std::endl << std::endl; } -void Printer::printAvailableConfigsInDetail(const std::string& deviceType, +void ConsoleWriter::printAvailableConfigsInDetail(const std::string& deviceType, const std::vector<std::shared_ptr<Device>>& devices) const { bool configFound = false; @@ -211,7 +211,7 @@ void Printer::printAvailableConfigsInDetail(const std::string& deviceType, } } -void Printer::printInstalledConfigs(const std::string& deviceType, +void ConsoleWriter::printInstalledConfigs(const std::string& deviceType, const std::vector<std::shared_ptr<Config>>& installedConfigs) const { if (installedConfigs.empty()) @@ -228,7 +228,7 @@ void Printer::printInstalledConfigs(const std::string& deviceType, } } -void Printer::printConfigDetails(const Config& config) const +void ConsoleWriter::printConfigDetails(const Config& config) const { std::string classids; std::string vendorids; @@ -267,12 +267,12 @@ void Printer::printConfigDetails(const Config& config) const << "\n VENDORIDS:\t" << vendorids << "\n" << std::endl; } -void Printer::printLine() const +void ConsoleWriter::printLine() const { - std::cout << std::setfill('-') << std::setw(80) << "-" << std::setfill(' ') << std::endl; + std::cout << std::string(80, '-') << std::endl; } -void Printer::printDeviceDetails(hw_item hw, FILE *f) const +void ConsoleWriter::printDeviceDetails(hw_item hw, FILE *f) const { std::unique_ptr<hd_data_t> hd_data{new hd_data_t()}; hd_t *hd = hd_list(hd_data.get(), hw, 1, nullptr); diff --git a/src/Printer.hpp b/src/ConsoleWriter.hpp similarity index 99% rename from src/Printer.hpp rename to src/ConsoleWriter.hpp index 8bfa64f358e0e85e7d4bc41e6355ae032f46ccb1..43b450393557f3a8df280278c84bb904f0a4c0fc 100644 --- a/src/Printer.hpp +++ b/src/ConsoleWriter.hpp @@ -34,7 +34,7 @@ #include "Device.hpp" #include "Enums.hpp" -class Printer +class ConsoleWriter { public: void printStatus(std::string statusMsg) const; diff --git a/src/Data.cpp b/src/Data.cpp index 8dfd7f62b446aaa8016c1ca0d90ed7eee080ed3e..5620f371fef9eb4ff370f2e492a15335bc4d0872 100644 --- a/src/Data.cpp +++ b/src/Data.cpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -131,8 +131,8 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev { // Check class ids bool found = std::find_if(hwdID->classIDs.begin(), hwdID->classIDs.end(), [i_device](const std::string& classID){ - return (("*" == classID) || (classID == (*i_device)->classID_)); - }) != hwdID->classIDs.end(); + return (("*" == classID) || (classID == (*i_device)->classID_)); + }) != hwdID->classIDs.end(); if (!found) { @@ -142,8 +142,8 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev { // Check blacklisted class ids found = std::find_if(hwdID->blacklistedClassIDs.begin(), hwdID->blacklistedClassIDs.end(), [i_device](const std::string& blacklistedClassID){ - return (blacklistedClassID == (*i_device)->classID_); - }) != hwdID->blacklistedClassIDs.end(); + return (blacklistedClassID == (*i_device)->classID_); + }) != hwdID->blacklistedClassIDs.end(); if (found) { @@ -152,9 +152,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev else { // Check vendor ids - found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){ - return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_)); - }) != hwdID->vendorIDs.end(); + found = std::find_if(hwdID->vendorIDs.begin(), hwdID->vendorIDs.end(), [i_device](const std::string& vendorID){ + return (("*" == vendorID) || (vendorID == (*i_device)->vendorID_)); + }) != hwdID->vendorIDs.end(); if (!found) { @@ -164,8 +164,8 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev { // Check blacklisted vendor ids found = std::find_if(hwdID->blacklistedVendorIDs.begin(), hwdID->blacklistedVendorIDs.end(), [i_device](const std::string& blacklistedVendorID){ - return (blacklistedVendorID == (*i_device)->vendorID_); - }) != hwdID->blacklistedVendorIDs.end(); + return (blacklistedVendorID == (*i_device)->vendorID_); + }) != hwdID->blacklistedVendorIDs.end(); if (found) { @@ -174,9 +174,9 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev else { // Check device ids - found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){ - return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_)); - }) != hwdID->deviceIDs.end(); + found = std::find_if(hwdID->deviceIDs.begin(), hwdID->deviceIDs.end(), [i_device](const std::string& deviceID){ + return (("*" == deviceID) || (deviceID == (*i_device)->deviceID_)); + }) != hwdID->deviceIDs.end(); if (!found) { @@ -186,8 +186,8 @@ void Data::getAllDevicesOfConfig(const std::vector<std::shared_ptr<Device>>& dev { // Check blacklisted device ids found = std::find_if(hwdID->blacklistedDeviceIDs.begin(), hwdID->blacklistedDeviceIDs.end(), [i_device](const std::string& blacklistedDeviceID){ - return (blacklistedDeviceID == (*i_device)->deviceID_); - }) != hwdID->blacklistedDeviceIDs.end(); + return (blacklistedDeviceID == (*i_device)->deviceID_); + }) != hwdID->blacklistedDeviceIDs.end(); if (found) { continue; @@ -265,7 +265,7 @@ void Data::getAllDependenciesToInstall(std::shared_ptr<Config> config, { // Add to vector and check for further subdepends... std::shared_ptr<Config> dependconfig { - getDatabaseConfig((*configDependency), config->type_)}; + getDatabaseConfig((*configDependency), config->type_)}; if (nullptr == dependconfig) { continue; @@ -596,7 +596,7 @@ void Data::setMatchingConfigs(const std::vector<std::shared_ptr<Device>>& device } void Data::setMatchingConfig(std::shared_ptr<Config> config, - const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled) + const std::vector<std::shared_ptr<Device>>& devices, bool setAsInstalled) { std::vector<std::shared_ptr<Device>> foundDevices; @@ -618,7 +618,7 @@ void Data::setMatchingConfig(std::shared_ptr<Config> config, } void Data::addConfigSorted(std::vector<std::shared_ptr<Config>>& configs, - std::shared_ptr<Config> config) + std::shared_ptr<Config> config) { for (auto&& iterator = configs.begin(); iterator != configs.end(); iterator++) diff --git a/src/Mhwd.cpp b/src/Mhwd.cpp index dcb245be1036697fe96a8c6af99c857c43026718..da3e803d4f252ca08901debdcb54b1be829e980a 100644 --- a/src/Mhwd.cpp +++ b/src/Mhwd.cpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -61,7 +61,7 @@ bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONT conflicts += " " + conflictedConfig->name_; } - printer_.printError("config '" + config->name_ + "' conflicts with config(s):" + + consoleWriter_.printError("config '" + config->name_ + "' conflicts with config(s):" + conflicts); return false; } @@ -76,7 +76,7 @@ bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONT dependencies += " " + dependencyConfig->name_; } - printer_.printStatus("Dependencies to install:" + dependencies + + consoleWriter_.printStatus("Dependencies to install:" + dependencies + "\nProceed with installation? [Y/n]"); std::string input; std::getline(std::cin, input); @@ -95,7 +95,7 @@ bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONT requirements += " " + requirement->name_; } - printer_.printError("config '" + config->name_ + "' is required by config(s):" + + consoleWriter_.printError("config '" + config->name_ + "' is required by config(s):" + requirements); return false; } @@ -105,32 +105,32 @@ bool Mhwd::performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONT switch (status) { - case MHWD::STATUS::SUCCESS: - break; - case MHWD::STATUS::ERROR_CONFLICTS: - printer_.printError("config '" + config->name_ + - "' conflicts with installed config(s)!"); - break; - case MHWD::STATUS::ERROR_REQUIREMENTS: - printer_.printError("config '" + config->name_ + - "' is required by installed config(s)!"); - break; - case MHWD::STATUS::ERROR_NOT_INSTALLED: - printer_.printError("config '" + config->name_ + "' is not installed!"); - break; - case MHWD::STATUS::ERROR_ALREADY_INSTALLED: - printer_.printWarning("a version of config '" + config->name_ + - "' is already installed!\nUse -f/--force to force installation..."); + case MHWD::STATUS::SUCCESS: + break; + case MHWD::STATUS::ERROR_CONFLICTS: + consoleWriter_.printError("config '" + config->name_ + + "' conflicts with installed config(s)!"); + break; + case MHWD::STATUS::ERROR_REQUIREMENTS: + consoleWriter_.printError("config '" + config->name_ + + "' is required by installed config(s)!"); + break; + case MHWD::STATUS::ERROR_NOT_INSTALLED: + consoleWriter_.printError("config '" + config->name_ + "' is not installed!"); + break; + case MHWD::STATUS::ERROR_ALREADY_INSTALLED: + consoleWriter_.printWarning("a version of config '" + config->name_ + + "' is already installed!\nUse -f/--force to force installation..."); + break; + case MHWD::STATUS::ERROR_NO_MATCH_LOCAL_CONFIG: + consoleWriter_.printError("passed config does not match with installed config!"); + break; + case MHWD::STATUS::ERROR_SCRIPT_FAILED: + consoleWriter_.printError("script failed!"); + break; + case MHWD::STATUS::ERROR_SET_DATABASE: + consoleWriter_.printError("failed to set database!"); break; - case MHWD::STATUS::ERROR_NO_MATCH_LOCAL_CONFIG: - printer_.printError("passed config does not match with installed config!"); - break; - case MHWD::STATUS::ERROR_SCRIPT_FAILED: - printer_.printError("script failed!"); - break; - case MHWD::STATUS::ERROR_SET_DATABASE: - printer_.printError("failed to set database!"); - break; } data_.updateInstalledConfigData(); @@ -166,22 +166,22 @@ bool Mhwd::isUserRoot() const std::vector<std::string> Mhwd::checkEnvironment() const { - std::vector<std::string> missingDirs; + std::vector<std::string> missingDirs; if (!dirExists(MHWD_USB_CONFIG_DIR)) { - missingDirs.emplace_back(MHWD_USB_CONFIG_DIR); + missingDirs.emplace_back(MHWD_USB_CONFIG_DIR); } if (!dirExists(MHWD_PCI_CONFIG_DIR)) { - missingDirs.emplace_back(MHWD_PCI_CONFIG_DIR); + missingDirs.emplace_back(MHWD_PCI_CONFIG_DIR); } if (!dirExists(MHWD_USB_DATABASE_DIR)) { - missingDirs.emplace_back(MHWD_USB_DATABASE_DIR); + missingDirs.emplace_back(MHWD_USB_DATABASE_DIR); } if (!dirExists(MHWD_PCI_DATABASE_DIR)) { - missingDirs.emplace_back(MHWD_PCI_DATABASE_DIR); + missingDirs.emplace_back(MHWD_PCI_DATABASE_DIR); } return missingDirs; @@ -230,7 +230,7 @@ std::shared_ptr<Config> Mhwd::getDatabaseConfig(const std::string& configName, } auto config = std::find_if(allConfigs->begin(), allConfigs->end(), - [configName](const std::shared_ptr<Config>& config) { + [configName](const std::shared_ptr<Config>& config) { return config->name_ == configName; }); if (config != allConfigs->end()) @@ -306,14 +306,14 @@ MHWD::STATUS Mhwd::performTransaction(const Transaction& transaction) } else { - printer_.printMessage(MHWD::MESSAGETYPE::REMOVE_START, installedConfig->name_); + consoleWriter_.printMessage(MHWD::MESSAGETYPE::REMOVE_START, installedConfig->name_); if (MHWD::STATUS::SUCCESS != (status = uninstallConfig(installedConfig.get()))) { return status; } else { - printer_.printMessage(MHWD::MESSAGETYPE::REMOVE_END, installedConfig->name_); + consoleWriter_.printMessage(MHWD::MESSAGETYPE::REMOVE_END, installedConfig->name_); } } } @@ -332,7 +332,7 @@ MHWD::STATUS Mhwd::performTransaction(const Transaction& transaction) dependencyConfig != transaction.dependencyConfigs_.begin() - 1; --dependencyConfig) { - printer_.printMessage(MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START, + consoleWriter_.printMessage(MHWD::MESSAGETYPE::INSTALLDEPENDENCY_START, (*dependencyConfig)->name_); if (MHWD::STATUS::SUCCESS != (status = installConfig((*dependencyConfig)))) { @@ -340,19 +340,19 @@ MHWD::STATUS Mhwd::performTransaction(const Transaction& transaction) } else { - printer_.printMessage(MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END, + consoleWriter_.printMessage(MHWD::MESSAGETYPE::INSTALLDEPENDENCY_END, (*dependencyConfig)->name_); } } - printer_.printMessage(MHWD::MESSAGETYPE::INSTALL_START, transaction.config_->name_); + consoleWriter_.printMessage(MHWD::MESSAGETYPE::INSTALL_START, transaction.config_->name_); if (MHWD::STATUS::SUCCESS != (status = installConfig(transaction.config_))) { return status; } else { - printer_.printMessage(MHWD::MESSAGETYPE::INSTALL_END, + consoleWriter_.printMessage(MHWD::MESSAGETYPE::INSTALL_END, transaction.config_->name_); } } @@ -439,7 +439,7 @@ bool Mhwd::copyFile(const std::string& source, const std::string destination, co std::ofstream dst(destination, std::ios::binary); if (!src || !dst) { - return false; + return false; } dst << src.rdbuf(); @@ -668,7 +668,7 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera char buff[512]; while (fgets(buff, sizeof(buff), in) != nullptr) { - printer_.printMessage(MHWD::MESSAGETYPE::CONSOLE_OUTPUT, buff); + consoleWriter_.printMessage(MHWD::MESSAGETYPE::CONSOLE_OUTPUT, buff); } int stat = pclose(in); @@ -691,238 +691,238 @@ bool Mhwd::runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE opera void Mhwd::setVersionMhwd(std::string versionOfSoftware, std::string yearCopyright) { - version_ = versionOfSoftware; - year_ = yearCopyright; + version_ = versionOfSoftware; + year_ = yearCopyright; } void Mhwd::tryToParseCmdLineOptions(int argc, char* argv[], bool& autoConfigureNonFreeDriver, - std::string& operationType, std::string& autoConfigureClassID) + std::string& operationType, std::string& autoConfigureClassID) { - if (argc <= 1) - { - arguments_.LIST_AVAILABLE = true; - } - for (int nArg = 1; nArg < argc; ++nArg) - { - const std::string option{ argv[nArg] }; - if (("-h" == option) || ("--help" == option)) - { - printer_.printHelp(); - } - else if (("-v" == option) || ("--version" == option)) - { - printer_.printVersion(version_, year_); - } - else if (("-f" == option) || ("--force" == option)) - { - arguments_.FORCE = true; - } - else if (("-d" == option) || ("--detail" == option)) - { - arguments_.DETAIL = true; - } - else if (("-la" == option) || ("--listall" == option)) - { - arguments_.LIST_ALL = true; - } - else if (("-li" == option) || ("--listinstalled" == option)) - { - arguments_.LIST_INSTALLED = true; - } - else if (("-l" == option) || ("--list" == option)) - { - arguments_.LIST_AVAILABLE = true; - } - else if (("-lh" == option) || ("--listhardware" == option)) - { - arguments_.LIST_HARDWARE = true; - } - else if ("--pci" == option) - { - arguments_.SHOW_PCI = true; - } - else if ("--usb" == option) - { - arguments_.SHOW_USB = true; - } - else if (("-a" == option) || ("--auto" == option)) - { - if ((nArg + 3) >= argc) - { - throw std::runtime_error{"invalid use of option: -a/--auto\n"}; - } - else - { - const std::string deviceType{ argv[nArg + 1] }; - const std::string driverType{ argv[nArg + 2] }; - const std::string classID{ argv[nArg + 3] }; - if ((("pci" != deviceType) && ("usb" != deviceType)) - || (("free" != driverType) && ("nonfree" != driverType))) - { - throw std::runtime_error{"invalid use of option: -a/--auto\n"}; - } - else - { - operationType = Vita::string{ deviceType }.toUpper(); - autoConfigureNonFreeDriver = ("nonfree" == driverType); - autoConfigureClassID = Vita::string(classID).toLower().trim(); - arguments_.AUTOCONFIGURE = true; - nArg += 3; - } - } - } - else if (("-ic" == option) || ("--installcustom" == option)) - { - if ((nArg + 1) >= argc) - { - throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; - } - else - { - const std::string deviceType{ argv[++nArg] }; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; - } - else - { - operationType = Vita::string{ deviceType }.toUpper(); - arguments_.CUSTOM_INSTALL = true; - } - } - } - else if (("-i" == option) || ("--install" == option)) - { - if ((nArg + 1) >= argc) - { - throw std::runtime_error{"invalid use of option: -i/--install\n"}; - } - else - { - const std::string deviceType{ argv[++nArg] }; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - throw std::runtime_error{"invalid use of option: -i/--install\n"}; - } - else - { - operationType = Vita::string{ deviceType }.toUpper(); - arguments_.INSTALL = true; - } - } - } - else if (("-r" == option) || ("--remove" == option)) - { - if ((nArg + 1) >= argc) - { - throw std::runtime_error{"invalid use of option: -r/--remove\n"}; - } - else - { - const std::string deviceType{ argv[++nArg] }; - if (("pci" != deviceType) && ("usb" != deviceType)) - { - throw std::runtime_error{"invalid use of option: -r/--remove\n"}; - } - else - { - operationType = Vita::string{ deviceType }.toUpper(); - arguments_.REMOVE = true; - } - } - } - else if ("--pmcachedir" == option) - { - if (nArg + 1 >= argc) - { - throw std::runtime_error{"invalid use of option: --pmcachedir\n"}; - } - else - { - data_.environment.PMCachePath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if ("--pmconfig" == option) - { - if (nArg + 1 >= argc) - { - throw std::runtime_error{"invalid use of option: --pmconfig\n"}; - } - else - { - data_.environment.PMConfigPath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if ("--pmroot" == option) - { - if (nArg + 1 >= argc) - { - throw std::runtime_error{"invalid use of option: --pmroot\n"}; - } - else - { - data_.environment.PMRootPath = Vita::string(argv[++nArg]).trim("\"").trim(); - } - } - else if (arguments_.INSTALL || arguments_.REMOVE) - { - bool found = false; - std::string name; - if (arguments_.CUSTOM_INSTALL) - { - name = std::string{ argv[nArg] }; - } - else - { - name = Vita::string(argv[nArg]).toLower(); - } - for (const auto& config : configs_) - { - if (config == name) - { - found = true; - break; - } - } - if (!found) - { - configs_.push_back(name); - } - } - else - { - throw std::runtime_error{"invalid option: " + std::string(argv[nArg]) + "\n"}; - } - } - if (!arguments_.SHOW_PCI && !arguments_.SHOW_USB) - { - arguments_.SHOW_USB = true; - arguments_.SHOW_PCI = true; - } + if (argc <= 1) + { + arguments_.LIST_AVAILABLE = true; + } + for (int nArg = 1; nArg < argc; ++nArg) + { + const std::string option{ argv[nArg] }; + if (("-h" == option) || ("--help" == option)) + { + consoleWriter_.printHelp(); + } + else if (("-v" == option) || ("--version" == option)) + { + consoleWriter_.printVersion(version_, year_); + } + else if (("-f" == option) || ("--force" == option)) + { + arguments_.FORCE = true; + } + else if (("-d" == option) || ("--detail" == option)) + { + arguments_.DETAIL = true; + } + else if (("-la" == option) || ("--listall" == option)) + { + arguments_.LIST_ALL = true; + } + else if (("-li" == option) || ("--listinstalled" == option)) + { + arguments_.LIST_INSTALLED = true; + } + else if (("-l" == option) || ("--list" == option)) + { + arguments_.LIST_AVAILABLE = true; + } + else if (("-lh" == option) || ("--listhardware" == option)) + { + arguments_.LIST_HARDWARE = true; + } + else if ("--pci" == option) + { + arguments_.SHOW_PCI = true; + } + else if ("--usb" == option) + { + arguments_.SHOW_USB = true; + } + else if (("-a" == option) || ("--auto" == option)) + { + if ((nArg + 3) >= argc) + { + throw std::runtime_error{"invalid use of option: -a/--auto\n"}; + } + else + { + const std::string deviceType{ argv[nArg + 1] }; + const std::string driverType{ argv[nArg + 2] }; + const std::string classID{ argv[nArg + 3] }; + if ((("pci" != deviceType) && ("usb" != deviceType)) + || (("free" != driverType) && ("nonfree" != driverType))) + { + throw std::runtime_error{"invalid use of option: -a/--auto\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + autoConfigureNonFreeDriver = ("nonfree" == driverType); + autoConfigureClassID = Vita::string(classID).toLower().trim(); + arguments_.AUTOCONFIGURE = true; + nArg += 3; + } + } + } + else if (("-ic" == option) || ("--installcustom" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -ic/--installcustom\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.CUSTOM_INSTALL = true; + } + } + } + else if (("-i" == option) || ("--install" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -i/--install\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -i/--install\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.INSTALL = true; + } + } + } + else if (("-r" == option) || ("--remove" == option)) + { + if ((nArg + 1) >= argc) + { + throw std::runtime_error{"invalid use of option: -r/--remove\n"}; + } + else + { + const std::string deviceType{ argv[++nArg] }; + if (("pci" != deviceType) && ("usb" != deviceType)) + { + throw std::runtime_error{"invalid use of option: -r/--remove\n"}; + } + else + { + operationType = Vita::string{ deviceType }.toUpper(); + arguments_.REMOVE = true; + } + } + } + else if ("--pmcachedir" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmcachedir\n"}; + } + else + { + data_.environment.PMCachePath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if ("--pmconfig" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmconfig\n"}; + } + else + { + data_.environment.PMConfigPath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if ("--pmroot" == option) + { + if (nArg + 1 >= argc) + { + throw std::runtime_error{"invalid use of option: --pmroot\n"}; + } + else + { + data_.environment.PMRootPath = Vita::string(argv[++nArg]).trim("\"").trim(); + } + } + else if (arguments_.INSTALL || arguments_.REMOVE) + { + bool found = false; + std::string name; + if (arguments_.CUSTOM_INSTALL) + { + name = std::string{ argv[nArg] }; + } + else + { + name = Vita::string(argv[nArg]).toLower(); + } + for (const auto& config : configs_) + { + if (config == name) + { + found = true; + break; + } + } + if (!found) + { + configs_.push_back(name); + } + } + else + { + throw std::runtime_error{"invalid option: " + std::string(argv[nArg]) + "\n"}; + } + } + if (!arguments_.SHOW_PCI && !arguments_.SHOW_USB) + { + arguments_.SHOW_USB = true; + arguments_.SHOW_PCI = true; + } } bool Mhwd::optionsDontInterfereWithEachOther() const { - if (arguments_.INSTALL && arguments_.REMOVE) - { - printer_.printError("install and remove options can only be used separately!\n"); - printer_.printHelp(); - return false; - } - else if ((arguments_.INSTALL || arguments_.REMOVE) && arguments_.AUTOCONFIGURE) - { - printer_.printError("auto option can't be combined with install and remove options!\n"); - printer_.printHelp(); - return false; - } - else if ((arguments_.REMOVE || arguments_.INSTALL) && configs_.empty()) - { - printer_.printError("nothing to do?!\n"); - printer_.printHelp(); - return false; - } - - return true; + if (arguments_.INSTALL && arguments_.REMOVE) + { + consoleWriter_.printError("install and remove options can only be used separately!\n"); + consoleWriter_.printHelp(); + return false; + } + else if ((arguments_.INSTALL || arguments_.REMOVE) && arguments_.AUTOCONFIGURE) + { + consoleWriter_.printError("auto option can't be combined with install and remove options!\n"); + consoleWriter_.printHelp(); + return false; + } + else if ((arguments_.REMOVE || arguments_.INSTALL) && configs_.empty()) + { + consoleWriter_.printError("nothing to do?!\n"); + consoleWriter_.printHelp(); + return false; + } + + return true; } int Mhwd::launch(int argc, char *argv[]) @@ -930,11 +930,11 @@ int Mhwd::launch(int argc, char *argv[]) std::vector<std::string> missingDirs { checkEnvironment() }; if (!missingDirs.empty()) { - printer_.printError("Following directories do not exist:"); - for (const auto& dir : missingDirs) - { - printer_.printStatus(dir); - } + consoleWriter_.printError("Following directories do not exist:"); + for (const auto& dir : missingDirs) + { + consoleWriter_.printStatus(dir); + } return 1; } @@ -944,25 +944,25 @@ int Mhwd::launch(int argc, char *argv[]) try { - tryToParseCmdLineOptions(argc, argv, autoConfigureNonFreeDriver, operationType, - autoConfigureClassID); + tryToParseCmdLineOptions(argc, argv, autoConfigureNonFreeDriver, operationType, + autoConfigureClassID); } catch(const std::runtime_error& e) { - printer_.printError(e.what()); - printer_.printHelp(); - return 1; + consoleWriter_.printError(e.what()); + consoleWriter_.printHelp(); + return 1; } if (!optionsDontInterfereWithEachOther()) { - return 1; + return 1; } // Check for invalid configs for (auto&& invalidConfig : data_.invalidConfigs) { - printer_.printWarning("config '" + invalidConfig->configPath_ + "' is invalid!"); + consoleWriter_.printWarning("config '" + invalidConfig->configPath_ + "' is invalid!"); } // > Perform operations: @@ -972,22 +972,22 @@ int Mhwd::launch(int argc, char *argv[]) { if (!data_.allPCIConfigs.empty()) { - printer_.listConfigs(data_.allPCIConfigs, "All PCI configs:"); + consoleWriter_.listConfigs(data_.allPCIConfigs, "All PCI configs:"); } else { - printer_.printWarning("No PCI configs found!"); + consoleWriter_.printWarning("No PCI configs found!"); } } if (arguments_.LIST_ALL && arguments_.SHOW_USB) { if (!data_.allUSBConfigs.empty()) { - printer_.listConfigs(data_.allUSBConfigs, "All USB configs:"); + consoleWriter_.listConfigs(data_.allUSBConfigs, "All USB configs:"); } else { - printer_.printWarning("No USB configs found!"); + consoleWriter_.printWarning("No USB configs found!"); } } @@ -996,17 +996,17 @@ int Mhwd::launch(int argc, char *argv[]) { if (arguments_.DETAIL) { - printer_.printInstalledConfigs("PCI", data_.installedPCIConfigs); + consoleWriter_.printInstalledConfigs("PCI", data_.installedPCIConfigs); } else { if (!data_.installedPCIConfigs.empty()) { - printer_.listConfigs(data_.installedPCIConfigs, "Installed PCI configs:"); + consoleWriter_.listConfigs(data_.installedPCIConfigs, "Installed PCI configs:"); } else { - printer_.printWarning("No installed PCI configs!"); + consoleWriter_.printWarning("No installed PCI configs!"); } } } @@ -1014,17 +1014,17 @@ int Mhwd::launch(int argc, char *argv[]) { if (arguments_.DETAIL) { - printer_.printInstalledConfigs("USB", data_.installedUSBConfigs); + consoleWriter_.printInstalledConfigs("USB", data_.installedUSBConfigs); } else { if (!data_.installedUSBConfigs.empty()) { - printer_.listConfigs(data_.installedUSBConfigs, "Installed USB configs:"); + consoleWriter_.listConfigs(data_.installedUSBConfigs, "Installed USB configs:"); } else { - printer_.printWarning("No installed USB configs!"); + consoleWriter_.printWarning("No installed USB configs!"); } } } @@ -1034,7 +1034,7 @@ int Mhwd::launch(int argc, char *argv[]) { if (arguments_.DETAIL) { - printer_.printAvailableConfigsInDetail("PCI", data_.PCIDevices); + consoleWriter_.printAvailableConfigsInDetail("PCI", data_.PCIDevices); } else { @@ -1042,7 +1042,7 @@ int Mhwd::launch(int argc, char *argv[]) { if (!PCIDevice->availableConfigs_.empty()) { - printer_.listConfigs(PCIDevice->availableConfigs_, + consoleWriter_.listConfigs(PCIDevice->availableConfigs_, PCIDevice->sysfsBusID_ + " (" + PCIDevice->classID_ + ":" + PCIDevice->vendorID_ + ":" + PCIDevice->deviceID_ + ") " + PCIDevice->className_ + " " + PCIDevice->vendorName_ + ":"); @@ -1055,7 +1055,7 @@ int Mhwd::launch(int argc, char *argv[]) { if (arguments_.DETAIL) { - printer_.printAvailableConfigsInDetail("USB", data_.USBDevices); + consoleWriter_.printAvailableConfigsInDetail("USB", data_.USBDevices); } else @@ -1064,10 +1064,10 @@ int Mhwd::launch(int argc, char *argv[]) { if (!USBdevice->availableConfigs_.empty()) { - printer_.listConfigs(USBdevice->availableConfigs_, + consoleWriter_.listConfigs(USBdevice->availableConfigs_, USBdevice->sysfsBusID_ + " (" + USBdevice->classID_ + ":" - + USBdevice->vendorID_ + ":" + USBdevice->deviceID_ + ") " - + USBdevice->className_ + " " + USBdevice->vendorName_ + ":"); + + USBdevice->vendorID_ + ":" + USBdevice->deviceID_ + ") " + + USBdevice->className_ + " " + USBdevice->vendorName_ + ":"); } } } @@ -1078,22 +1078,22 @@ int Mhwd::launch(int argc, char *argv[]) { if (arguments_.DETAIL) { - printer_.printDeviceDetails(hw_pci); + consoleWriter_.printDeviceDetails(hw_pci); } else { - printer_.listDevices(data_.PCIDevices, "PCI"); + consoleWriter_.listDevices(data_.PCIDevices, "PCI"); } } if (arguments_.LIST_HARDWARE && arguments_.SHOW_USB) { if (arguments_.DETAIL) { - printer_.printDeviceDetails(hw_usb); + consoleWriter_.printDeviceDetails(hw_usb); } else { - printer_.listDevices(data_.USBDevices, "USB"); + consoleWriter_.listDevices(data_.USBDevices, "USB"); } } @@ -1136,7 +1136,7 @@ int Mhwd::launch(int argc, char *argv[]) if (nullptr == config) { - printer_.printWarning( + consoleWriter_.printWarning( "No config found for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + device->deviceID_ + ") " + device->className_ + " " @@ -1149,15 +1149,15 @@ int Mhwd::launch(int argc, char *argv[]) bool skip = false; if (!arguments_.FORCE) { - skip = std::find_if(installedConfigs->begin(), installedConfigs->end(), - [&config](const std::shared_ptr<Config>& conf) -> bool { - return conf->name_ == config->name_; - }) != installedConfigs->end(); + skip = std::find_if(installedConfigs->begin(), installedConfigs->end(), + [&config](const std::shared_ptr<Config>& conf) -> bool { + return conf->name_ == config->name_; + }) != installedConfigs->end(); } // Print found config if (skip) { - printer_.printStatus( + consoleWriter_.printStatus( "Skipping already installed config '" + config->name_ + "' for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + @@ -1166,7 +1166,7 @@ int Mhwd::launch(int argc, char *argv[]) } else { - printer_.printStatus( + consoleWriter_.printStatus( "Using config '" + config->name_ + "' for device: " + device->sysfsBusID_ + " (" + device->classID_ + ":" + device->vendorID_ + ":" + device->deviceID_ + ") " + @@ -1185,7 +1185,7 @@ int Mhwd::launch(int argc, char *argv[]) if (!foundDevice) { - printer_.printWarning("No device of class " + autoConfigureClassID + " found!"); + consoleWriter_.printWarning("No device of class " + autoConfigureClassID + " found!"); } else if (!configs_.empty()) { @@ -1198,7 +1198,7 @@ int Mhwd::launch(int argc, char *argv[]) { if (!isUserRoot()) { - printer_.printError("You cannot perform this operation unless you are root!"); + consoleWriter_.printError("You cannot perform this operation unless you are root!"); } else { @@ -1213,12 +1213,12 @@ int Mhwd::launch(int argc, char *argv[]) if (0 != stat(filepath.c_str(), &filestatus)) { - printer_.printError("custom config '" + filepath + "' does not exist!"); + consoleWriter_.printError("custom config '" + filepath + "' does not exist!"); return 1; } else if (!S_ISREG(filestatus.st_mode)) { - printer_.printError("custom config '" + filepath + "' is invalid!"); + consoleWriter_.printError("custom config '" + filepath + "' is invalid!"); return 1; } else @@ -1226,7 +1226,7 @@ int Mhwd::launch(int argc, char *argv[]) config_.reset(new Config(filepath, operationType)); if (!config_->readConfigFile(filepath)) { - printer_.printError("failed to read custom config '" + filepath + "'!"); + consoleWriter_.printError("failed to read custom config '" + filepath + "'!"); return 1; } @@ -1244,12 +1244,12 @@ int Mhwd::launch(int argc, char *argv[]) config_ = getDatabaseConfig((*configName), operationType); if (config_ == nullptr) { - printer_.printError("config '" + (*configName) + "' does not exist!"); + consoleWriter_.printError("config '" + (*configName) + "' does not exist!"); return 1; } else { - printer_.printWarning( + consoleWriter_.printWarning( "no matching device for config '" + (*configName) + "' found!"); } } @@ -1265,7 +1265,7 @@ int Mhwd::launch(int argc, char *argv[]) if (nullptr == config_) { - printer_.printError("config '" + (*configName) + "' is not installed!"); + consoleWriter_.printError("config '" + (*configName) + "' is not installed!"); return 1; } diff --git a/src/Mhwd.hpp b/src/Mhwd.hpp index 4cf99bf6634b307315c8fefd5b420da80a333232..c8db047b64d68326aa9c92004c2dc81b22bea966 100644 --- a/src/Mhwd.hpp +++ b/src/Mhwd.hpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -38,10 +38,10 @@ #include "Config.hpp" #include "const.h" +#include "ConsoleWriter.hpp" #include "Data.hpp" #include "Device.hpp" #include "Enums.hpp" -#include "Printer.hpp" #include "vita/string.hpp" #include "Transaction.hpp" @@ -71,10 +71,10 @@ private: } arguments_; std::shared_ptr<Config> config_; Data data_; - Printer printer_; + ConsoleWriter consoleWriter_; std::vector<std::string> configs_; std::string version_, year_; - + bool performTransaction(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type); bool isUserRoot() const; std::vector<std::string> checkEnvironment() const; @@ -99,9 +99,9 @@ private: MHWD::STATUS installConfig(std::shared_ptr<Config> config); MHWD::STATUS uninstallConfig(Config *config); bool runScript(std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE operationType); - void tryToParseCmdLineOptions(int argc, char* argv[], bool& autoConfigureNonFreeDriver, - std::string& operationType, std::string& autoConfigureClassID); - bool optionsDontInterfereWithEachOther() const; + void tryToParseCmdLineOptions(int argc, char* argv[], bool& autoConfigureNonFreeDriver, + std::string& operationType, std::string& autoConfigureClassID); + bool optionsDontInterfereWithEachOther() const; }; #endif /* MHWD_HPP_ */ diff --git a/src/Transaction.hpp b/src/Transaction.hpp index aa7e4e102d6b3fc610dde33860202d0157540429..e3aed6f504cfed77ba153b913f886c40b7ca5733 100644 --- a/src/Transaction.hpp +++ b/src/Transaction.hpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -36,7 +36,7 @@ class Transaction public: Transaction() = delete; Transaction(Data data, std::shared_ptr<Config> config, MHWD::TRANSACTIONTYPE type, - bool allowReinstallation); + bool allowReinstallation); bool isAllowedToReinstall() const; std::shared_ptr<Config> config_; diff --git a/src/main.cpp b/src/main.cpp index 0ef4088dba29d08d616932f604f28a9f09409882..3c8577060f2f85a958f13ad579163db6bacd4c91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,6 @@ /* * This file is part of the mhwd - Manjaro Hardware Detection project - * + * * mhwd - Manjaro Hardware Detection * Roland Singer <roland@manjaro.org> * Åukasz Matysiak <december0123@gmail.com> @@ -30,13 +30,13 @@ int main(int argc, char *argv[]) { try { - Mhwd mhwd; - mhwd.setVersionMhwd("0.5.4","2012-2015"); - return mhwd.launch(argc, argv); + Mhwd mhwd; + mhwd.setVersionMhwd("0.5.5","2012-2015"); + return mhwd.launch(argc, argv); } catch(...) { - std::cerr << "Unknown errors occured..."; - return -1; + std::cerr << "Unknown errors occured..."; + return -1; } }