Implemented shared library loading and launch mode differentiation
This commit is contained in:
parent
d4cab5ae04
commit
6cd966c6a9
5
include/cli.hpp
Normal file
5
include/cli.hpp
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
enum class LAUNCH_MODE { HELP, INSTALL, UNINSTALL, LIST, RUN };
|
||||
|
||||
LAUNCH_MODE launch_mode(const char* str);
|
||||
17
include/dl.hpp
Normal file
17
include/dl.hpp
Normal file
@ -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;
|
||||
};
|
||||
16
src/cli.cpp
Normal file
16
src/cli.cpp
Normal file
@ -0,0 +1,16 @@
|
||||
#include "cli.hpp"
|
||||
|
||||
#include <cstring>
|
||||
|
||||
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;
|
||||
}
|
||||
47
src/dl.cpp
Normal file
47
src/dl.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
#include "dl.hpp"
|
||||
|
||||
#include <string>
|
||||
#include <dlfcn.h>
|
||||
|
||||
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;
|
||||
}
|
||||
68
src/main.cpp
68
src/main.cpp
@ -1,6 +1,70 @@
|
||||
#include <iostream>
|
||||
|
||||
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 <wirekit.so> <name>\" -> Installs a wirekit to execute programs with" << std::endl;
|
||||
std::cout << " <wirekit.so> -> The shared object to be installed as wirekit" << std::endl;
|
||||
std::cout << " <name> -> (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 <name>\" -> Removes a wirekit from the system" << std::endl;
|
||||
std::cout << " <name> -> The name of the wirekit to be uninstalled" << std::endl;
|
||||
|
||||
std::cout << " \"rewire list\" -> Lists all installed wirekits" << std::endl;
|
||||
|
||||
std::cout << " \"rewire <wirekit> <command+args>\"" << std::endl;
|
||||
std::cout << " <wirekit> -> The wirekit to use for execution" << std::endl;
|
||||
std::cout << " <command+args> -> (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;
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user