FE 0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
Loading...
Searching...
No Matches
sys.h
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <optional>
5#include <stdexcept>
6#include <string>
7#include <string_view>
8
9namespace fe::sys {
10
11#ifdef _WIN32
12static constexpr auto Which = "where";
13static constexpr auto Path_Sep = ';';
14static constexpr auto Path_Sep_word = "semicolon";
15static constexpr auto Path_Sep_Word = "Semicolon";
16#else
17/// Name of the command that locates an executable on this platform.
18static constexpr auto Which = "which";
19/// Separates the entries of a `PATH`-like environment variable on this platform; Windows needs `;`, Unix `:`.
20static constexpr auto Path_Sep = ':';
21static constexpr auto Path_Sep_word = "colon";
22static constexpr auto Path_Sep_Word = "Colon";
23#endif
24
25/// Path of the executable or dynamic library that contains @p addr.
26/// Pass the address of a function of your own to find out where *you* have been loaded from.
27/// @returns `std::nullopt` if an error occurred or the platform is not supported.
28std::optional<std::filesystem::path> path_to_lib(const void* addr);
29
30/// Executes command @p cmd.
31/// @returns the output as string.
32std::string exec(std::string cmd);
33
34/// Locates @p cmd via sys::which; the resulting path may not exist.
35std::string find_cmd(std::string cmd);
36
37/// Thrown by sys::require_cmd when a command cannot be located on the system.
38class CmdNotFound : public std::logic_error {
39public:
40 CmdNotFound(const std::string& s)
41 : std::logic_error(s) {}
42};
43
44/// Locates @p name on the system or throws CmdNotFound.
45std::string require_cmd(std::string_view name);
46
47/// Wraps `std::system` and makes the return value usable.
48int system(std::string cmd);
49
50/// Runs @p cmd via sys::system and throws if it exits with a non-zero status.
51void require_run(const std::string& cmd);
52
53/// Wraps sys::system and puts `.exe` at the back (Windows) and `./` at the front (otherwise) of @p cmd.
54int run(std::string cmd, std::string args = {});
55
56/// Returns the @p path as `std::string` and escapes all whitespaces with backslash.
57std::string escape(const std::filesystem::path& path);
58
59} // namespace fe::sys
CmdNotFound(const std::string &s)
Definition sys.h:40
Definition sys.h:9
int run(std::string cmd, std::string args={})
Wraps sys::system and puts .exe at the back (Windows) and ./ at the front (otherwise) of cmd.
static constexpr auto Path_Sep_Word
Definition sys.h:22
std::string find_cmd(std::string cmd)
Locates cmd via sys::which; the resulting path may not exist.
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
int system(std::string cmd)
Wraps std::system and makes the return value usable.
std::optional< std::filesystem::path > path_to_lib(const void *addr)
Path of the executable or dynamic library that contains addr.
static constexpr auto Path_Sep
Separates the entries of a PATH-like environment variable on this platform; Windows needs ;,...
Definition sys.h:20
void require_run(const std::string &cmd)
Runs cmd via sys::system and throws if it exits with a non-zero status.
std::string require_cmd(std::string_view name)
Locates name on the system or throws CmdNotFound.
std::string exec(std::string cmd)
Executes command cmd.
static constexpr auto Which
Name of the command that locates an executable on this platform.
Definition sys.h:18
static constexpr auto Path_Sep_word
Definition sys.h:21
Definition span.h:150