From 6cd966c6a9268e7f2fc9d672d297446b406a7fd5 Mon Sep 17 00:00:00 2001 From: York Jasper Niebuhr Date: Fri, 15 Aug 2025 17:49:08 +0200 Subject: [PATCH] Implemented shared library loading and launch mode differentiation --- include/cli.hpp | 5 ++++ include/dl.hpp | 17 +++++++++++++ src/cli.cpp | 16 ++++++++++++ src/dl.cpp | 47 ++++++++++++++++++++++++++++++++++ src/main.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 include/cli.hpp create mode 100644 include/dl.hpp create mode 100644 src/cli.cpp create mode 100644 src/dl.cpp diff --git a/include/cli.hpp b/include/cli.hpp new file mode 100644 index 0000000..a5c89cf --- /dev/null +++ b/include/cli.hpp @@ -0,0 +1,5 @@ +#pragma once + +enum class LAUNCH_MODE { HELP, INSTALL, UNINSTALL, LIST, RUN }; + +LAUNCH_MODE launch_mode(const char* str); diff --git a/include/dl.hpp b/include/dl.hpp new file mode 100644 index 0000000..b89f8dd --- /dev/null +++ b/include/dl.hpp @@ -0,0 +1,17 @@ +#pragma once + +class DL { + void* m_handle; +public: + DL(const DL& other) = delete; + DL(DL&& other) noexcept; + DL& operator=(const DL& other) = delete; + DL& operator=(DL&& other) noexcept; + + DL(const char* path); + ~DL(); + + void* resolve(const char* symbol) const; + + operator bool() const; +}; diff --git a/src/cli.cpp b/src/cli.cpp new file mode 100644 index 0000000..3cbf8f5 --- /dev/null +++ b/src/cli.cpp @@ -0,0 +1,16 @@ +#include "cli.hpp" + +#include + +LAUNCH_MODE launch_mode(const char* str) { + if (std::strcmp(str, "help") == 0) + return LAUNCH_MODE::HELP; + else if (std::strcmp(str, "install") == 0) + return LAUNCH_MODE::INSTALL; + else if (std::strcmp(str, "uninstall") == 0) + return LAUNCH_MODE::UNINSTALL; + else if (std::strcmp(str, "list") == 0) + return LAUNCH_MODE::LIST; + else + return LAUNCH_MODE::RUN; +} diff --git a/src/dl.cpp b/src/dl.cpp new file mode 100644 index 0000000..9afb54e --- /dev/null +++ b/src/dl.cpp @@ -0,0 +1,47 @@ +#include "dl.hpp" + +#include +#include + +DL::DL(DL&& other) noexcept { + m_handle = other.m_handle; + other.m_handle = nullptr; +} + +DL& DL::operator=(DL&& other) noexcept { + if (this == &other) + return *this; + + m_handle = other.m_handle; + other.m_handle = nullptr; + return *this; +} + +DL::DL(const char* path) : m_handle{ nullptr } { + if (!path) + return; + + m_handle = dlopen(path, RTLD_NOW); + + // Try opening in cwd if library is not known system-wide + if (!m_handle) { + std::string cwd_path = std::string{ "./" } + std::string{ path }; + m_handle = dlopen(cwd_path.c_str(), RTLD_NOW); + } +} + +DL::~DL() { + if (m_handle) + dlclose(m_handle); +} + +void* DL::resolve(const char* symbol) const { + if (!m_handle) + return nullptr; + + return dlsym(m_handle, symbol); +} + +DL::operator bool() const { + return m_handle != nullptr; +} diff --git a/src/main.cpp b/src/main.cpp index 64c91b7..8ef241e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,70 @@ #include -int main(int argc, char** argv) { - std::cout << "Hello world!" << std::endl; +#include "cli.hpp" +#include "dl.hpp" + +int launch_help(int argc, char** argv) { + std::cout << "Use rewire via one of the following commands:" << std::endl; + + std::cout << " \"rewire help\" -> You appear to already know what it does ;)" << std::endl; + + std::cout << " \"rewire install \" -> Installs a wirekit to execute programs with" << std::endl; + std::cout << " -> The shared object to be installed as wirekit" << std::endl; + std::cout << " -> (Optional) The name via which the wirekit is supposed to be used" << std::endl; + std::cout << " -> Defaults to \"wirekit\" when installing \"wirekit.so\"" << std::endl; + + std::cout << " \"rewire uninstall \" -> Removes a wirekit from the system" << std::endl; + std::cout << " -> The name of the wirekit to be uninstalled" << std::endl; + + std::cout << " \"rewire list\" -> Lists all installed wirekits" << std::endl; + + std::cout << " \"rewire \"" << std::endl; + std::cout << " -> The wirekit to use for execution" << std::endl; + std::cout << " -> (Optional) A command to be executed" << std::endl; + std::cout << " -> Acts as rewired shell if no command is given" << std::endl; + + return 0; +} + +int launch_install(int argc, char** argv) { + return 0; +} + +int launch_uninstall(int argc, char** argv) { + return 0; +} + +int launch_list(int argc, char** argv) { + return 0; +} + +int launch_run(int argc, char** argv) { + return 0; +} + +int main(int argc, char** argv) { + if (argc < 2) { + std::cout << "Invalid usage, try \"rewire help\"!" << std::endl; + return 1; + } + + LAUNCH_MODE lm = launch_mode(argv[1]); + + switch (lm) { + case LAUNCH_MODE::HELP: + return launch_help(argc, argv); + case LAUNCH_MODE::INSTALL: + return launch_install(argc, argv); + case LAUNCH_MODE::UNINSTALL: + return launch_uninstall(argc, argv); + case LAUNCH_MODE::LIST: + return launch_list(argc, argv); + case LAUNCH_MODE::RUN: + return launch_run(argc, argv); + default: + std::cout << "Invalid launch mode!" << std::endl; + return 1; + } + return 0; }