FE
0.15.0
A C++23 toolkit for writing compiler/interpreter frontends.
Toggle main menu visibility
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
9
namespace
fe::sys
{
10
11
#ifdef _WIN32
12
static
constexpr
auto
Which
=
"where"
;
13
static
constexpr
auto
Path_Sep
=
';'
;
14
static
constexpr
auto
Path_Sep_word
=
"semicolon"
;
15
static
constexpr
auto
Path_Sep_Word
=
"Semicolon"
;
16
#else
17
/// Name of the command that locates an executable on this platform.
18
static
constexpr
auto
Which
=
"which"
;
19
/// Separates the entries of a `PATH`-like environment variable on this platform; Windows needs `;`, Unix `:`.
20
static
constexpr
auto
Path_Sep
=
':'
;
21
static
constexpr
auto
Path_Sep_word
=
"colon"
;
22
static
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.
28
std::optional<std::filesystem::path>
path_to_lib
(
const
void
* addr);
29
30
/// Executes command @p cmd.
31
/// @returns the output as string.
32
std::string
exec
(std::string cmd);
33
34
/// Locates @p cmd via sys::which; the resulting path may not exist.
35
std::string
find_cmd
(std::string cmd);
36
37
/// Thrown by sys::require_cmd when a command cannot be located on the system.
38
class
CmdNotFound
:
public
std::logic_error {
39
public
:
40
CmdNotFound
(
const
std::string& s)
41
:
std
::logic_error(s) {}
42
};
43
44
/// Locates @p name on the system or throws CmdNotFound.
45
std::string
require_cmd
(std::string_view name);
46
47
/// Wraps `std::system` and makes the return value usable.
48
int
system
(std::string cmd);
49
50
/// Runs @p cmd via sys::system and throws if it exits with a non-zero status.
51
void
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.
54
int
run
(std::string cmd, std::string args = {});
55
56
/// Returns the @p path as `std::string` and escapes all whitespaces with backslash.
57
std::string
escape
(
const
std::filesystem::path& path);
58
59
}
// namespace fe::sys
fe::sys::CmdNotFound::CmdNotFound
CmdNotFound(const std::string &s)
Definition
sys.h:40
fe::sys
Definition
sys.h:9
fe::sys::run
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.
fe::sys::Path_Sep_Word
static constexpr auto Path_Sep_Word
Definition
sys.h:22
fe::sys::find_cmd
std::string find_cmd(std::string cmd)
Locates cmd via sys::which; the resulting path may not exist.
fe::sys::escape
std::string escape(const std::filesystem::path &path)
Returns the path as std::string and escapes all whitespaces with backslash.
fe::sys::system
int system(std::string cmd)
Wraps std::system and makes the return value usable.
fe::sys::path_to_lib
std::optional< std::filesystem::path > path_to_lib(const void *addr)
Path of the executable or dynamic library that contains addr.
fe::sys::Path_Sep
static constexpr auto Path_Sep
Separates the entries of a PATH-like environment variable on this platform; Windows needs ;,...
Definition
sys.h:20
fe::sys::require_run
void require_run(const std::string &cmd)
Runs cmd via sys::system and throws if it exits with a non-zero status.
fe::sys::require_cmd
std::string require_cmd(std::string_view name)
Locates name on the system or throws CmdNotFound.
fe::sys::exec
std::string exec(std::string cmd)
Executes command cmd.
fe::sys::Which
static constexpr auto Which
Name of the command that locates an executable on this platform.
Definition
sys.h:18
fe::sys::Path_Sep_word
static constexpr auto Path_Sep_word
Definition
sys.h:21
std
Definition
span.h:150
fe
sys.h
Generated by
1.18.0