FE 0.13.1
Header-only C++ frontend library
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/// Name of the command that locates an executable on this platform.
12static constexpr auto which =
13#ifdef _WIN32
14 "where";
15#else
16 "which";
17#endif
18
19/// Path of the executable or dynamic library that contains @p addr.
20/// Pass the address of a function of your own to find out where *you* have been loaded from.
21/// @returns `std::nullopt` if an error occurred or the platform is not supported.
22std::optional<std::filesystem::path> path_to_lib(const void* addr);
23
24/// Executes command @p cmd.
25/// @returns the output as string.
26std::string exec(std::string cmd);
27
28/// Locates @p cmd via sys::which; the resulting path may not exist.
29std::string find_cmd(std::string cmd);
30
31/// Thrown by sys::require_cmd when a command cannot be located on the system.
32class CmdNotFound : public std::logic_error {
33public:
34 CmdNotFound(const std::string& s)
35 : std::logic_error(s) {}
36};
37
38/// Locates @p name on the system or throws CmdNotFound.
39std::string require_cmd(std::string_view name);
40
41/// Wraps `std::system` and makes the return value usable.
42int system(std::string cmd);
43
44/// Runs @p cmd via sys::system and throws if it exits with a non-zero status.
45void require_run(const std::string& cmd);
46
47/// Wraps sys::system and puts `.exe` at the back (Windows) and `./` at the front (otherwise) of @p cmd.
48int run(std::string cmd, std::string args = {});
49
50/// Returns the @p path as `std::string` and escapes all whitespaces with backslash.
51std::string escape(const std::filesystem::path& path);
52
53} // namespace fe::sys
CmdNotFound(const std::string &s)
Definition sys.h:34
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.
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.
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.
static constexpr auto which
Name of the command that locates an executable on this platform.
Definition sys.h:12
std::string exec(std::string cmd)
Executes command cmd.
Definition span.h:129