Drop the VarMap stuff, use the new EnvFake class.
This commit is contained in:
parent
f696e1e3a0
commit
8a7002f5c5
11 changed files with 30 additions and 135 deletions
|
@ -71,7 +71,7 @@ namespace duck {
|
||||||
{
|
{
|
||||||
// If it's set in the env, trust the setting. If it's wrong,
|
// If it's set in the env, trust the setting. If it's wrong,
|
||||||
// then that's the caller's problem.
|
// then that's the caller's problem.
|
||||||
auto ret = sp.env("CHOST");
|
auto ret = sp.env().get("CHOST");
|
||||||
if (ret and not ret->empty())
|
if (ret and not ret->empty())
|
||||||
return string(*ret);
|
return string(*ret);
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,15 +82,16 @@ namespace duck {
|
||||||
std::string_view msg,
|
std::string_view msg,
|
||||||
const char* newline
|
const char* newline
|
||||||
) {
|
) {
|
||||||
if (yesno(sp, sp[quiet_var])) {
|
if (yesno(sp, sp.env()[quiet_var])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (not yesno(sp, sp["RC_ENDCOL"]) and g_last_e_cmd == "ebegin") {
|
const auto& env = sp.env();
|
||||||
|
if (not yesno(sp, env["RC_ENDCOL"]) and g_last_e_cmd == "ebegin") {
|
||||||
stream << '\n';
|
stream << '\n';
|
||||||
}
|
}
|
||||||
stream << ' ' << sp[msg_prefix_var] << '*'
|
stream << ' ' << env[msg_prefix_var] << '*'
|
||||||
<< sp["NORMAL"] << ' ' << sp["RC_INDENTATION"]
|
<< env["NORMAL"] << ' ' << env["RC_INDENTATION"]
|
||||||
<< msg << newline;
|
<< msg << newline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,7 +101,7 @@ namespace duck {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
void vewarn (const SysPaths& sp, std::string_view msg) {
|
void vewarn (const SysPaths& sp, std::string_view msg) {
|
||||||
if (yesno(sp, sp["EINFO_VERBOSE"], true))
|
if (yesno(sp, sp.env()["EINFO_VERBOSE"], true))
|
||||||
ewarn(sp, msg);
|
ewarn(sp, msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,7 +120,7 @@ namespace duck {
|
||||||
}
|
}
|
||||||
|
|
||||||
void esyslog(const SysPaths& sp, std::string_view pri, std::string_view tag, std::string_view msg) {
|
void esyslog(const SysPaths& sp, std::string_view pri, std::string_view tag, std::string_view msg) {
|
||||||
if (not sp["EINFO_LOG"].empty() and not run_cmd_retcode("command -v logger > /dev/null 2>&1")) {
|
if (not sp.env()["EINFO_LOG"].empty() and not run_cmd_retcode("command -v logger > /dev/null 2>&1")) {
|
||||||
if (msg.empty())
|
if (msg.empty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -138,7 +139,7 @@ namespace duck {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto val = sp[var];
|
auto val = sp.env()[var];
|
||||||
if (val.empty())
|
if (val.empty())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|
|
@ -168,12 +168,12 @@ namespace duck {
|
||||||
}
|
}
|
||||||
|
|
||||||
std::cout << " [" << z << "] " << filename;
|
std::cout << " [" << z << "] " << filename;
|
||||||
auto colours = sp.colours();
|
const auto& env = sp.env();
|
||||||
if (filename == current_native) {
|
if (filename == current_native) {
|
||||||
std::cout << ' ' << colours["GOOD"] << '*' << colours["NORMAL"];
|
std::cout << ' ' << env["GOOD"] << '*' << env["NORMAL"];
|
||||||
}
|
}
|
||||||
else if (fs::is_regular_file(sp.gcc_env_d() / ("config-" + target)) and filename == current) {
|
else if (fs::is_regular_file(sp.gcc_env_d() / ("config-" + target)) and filename == current) {
|
||||||
std::cout << ' ' << colours["HILITE"] << '*' << colours["NORMAL"];
|
std::cout << ' ' << env["HILITE"] << '*' << env["NORMAL"];
|
||||||
}
|
}
|
||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ int main(int argc, char* argv[]) {
|
||||||
}
|
}
|
||||||
|
|
||||||
duck::SysPaths syspaths;
|
duck::SysPaths syspaths;
|
||||||
syspaths.set_colours(g_simple_colours);
|
syspaths.add_to_env(g_simple_colours);
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
std::cout << syspaths << '\n';
|
std::cout << syspaths << '\n';
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -18,7 +18,6 @@ executable(meson.project_name(),
|
||||||
'replace_bash_vars.cpp',
|
'replace_bash_vars.cpp',
|
||||||
'to_var_map.cpp',
|
'to_var_map.cpp',
|
||||||
'load_file.cpp',
|
'load_file.cpp',
|
||||||
'var_map.cpp',
|
|
||||||
'env_base.cpp',
|
'env_base.cpp',
|
||||||
'env_real.cpp',
|
'env_real.cpp',
|
||||||
'env_fake.cpp',
|
'env_fake.cpp',
|
||||||
|
|
|
@ -59,7 +59,7 @@ namespace duck {
|
||||||
std::string_view token(text.substr(2, end - 2));
|
std::string_view token(text.substr(2, end - 2));
|
||||||
std::string_view retval(token.empty() || token.front() != '!' ? token : token.substr(1));
|
std::string_view retval(token.empty() || token.front() != '!' ? token : token.substr(1));
|
||||||
if (not token.empty() and token.front() == '!') {
|
if (not token.empty() and token.front() == '!') {
|
||||||
return {sp[retval], end + 1};
|
return {sp.env()[retval], end + 1};
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return {retval, end + 1};
|
return {retval, end + 1};
|
||||||
|
@ -95,7 +95,7 @@ namespace duck {
|
||||||
if (var.name == "argv0")
|
if (var.name == "argv0")
|
||||||
rtext += PROJECT_NAME;
|
rtext += PROJECT_NAME;
|
||||||
else
|
else
|
||||||
rtext += sp[var.name];
|
rtext += sp.env()[var.name];
|
||||||
pos += var.orig_len;
|
pos += var.orig_len;
|
||||||
prev_start = pos;
|
prev_start = pos;
|
||||||
}
|
}
|
||||||
|
|
|
@ -34,54 +34,22 @@ namespace duck {
|
||||||
const std::size_t pos = text.find_first_not_of('/');
|
const std::size_t pos = text.find_first_not_of('/');
|
||||||
return (text.npos == pos ? text : text.substr(pos));
|
return (text.npos == pos ? text : text.substr(pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view val_or_def (const VarMap& map, std::string_view key, std::string_view def) noexcept {
|
|
||||||
try {
|
|
||||||
auto itm = map.find(key);
|
|
||||||
return (map.end() == itm ? def : itm->second);
|
|
||||||
}
|
|
||||||
catch (...) {
|
|
||||||
return def;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
SysPaths::SysPaths() :
|
SysPaths::SysPaths() :
|
||||||
m_env(to_var_map_ignore_ownership(environ)),
|
m_env(),
|
||||||
m_root(val_or_def(m_env, "ROOT", "/")),
|
m_root(m_env.get("ROOT", "/")),
|
||||||
m_eprefix(fix_eprefix(val_or_def(m_env, "EPREFIX", ""))),
|
m_eprefix(fix_eprefix(m_env.get("EPREFIX", ""))),
|
||||||
m_eroot(m_root / remove_front_slash(std::string(m_eprefix))),
|
m_eroot(m_root / remove_front_slash(std::string(m_eprefix))),
|
||||||
m_env_d(m_eroot / "etc/env.d"),
|
m_env_d(m_eroot / "etc/env.d"),
|
||||||
m_gcc_env_d(m_env_d / "gcc")
|
m_gcc_env_d(m_env_d / "gcc")
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string_view SysPaths::operator[] (std::string_view name) const noexcept {
|
void SysPaths::add_to_env (std::string_view colours) {
|
||||||
assert(not name.empty());
|
for (auto& entry : to_var_map_ignore_ownership(colours)) {
|
||||||
return val_or_def(m_env, name, "");
|
m_env.set(entry.first, entry.second);
|
||||||
}
|
|
||||||
|
|
||||||
std::optional<std::string_view> SysPaths::env (std::string_view name) const noexcept {
|
|
||||||
try {
|
|
||||||
assert(not name.empty());
|
|
||||||
auto it_found = m_env.find(name);
|
|
||||||
if (m_env.end() == it_found)
|
|
||||||
return {};
|
|
||||||
else
|
|
||||||
return std::make_optional(it_found->second);
|
|
||||||
}
|
}
|
||||||
catch (...) {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view SysPaths::env (std::string_view name, std::string_view def) const noexcept {
|
|
||||||
assert(not name.empty());
|
|
||||||
return val_or_def(m_env, name, def);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SysPaths::set_colours (std::string_view colours) {
|
|
||||||
m_colours = to_var_map_ignore_ownership(colours);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto SysPaths::root() const -> const path_type& {
|
auto SysPaths::root() const -> const path_type& {
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "var_map.hpp"
|
#include "env_fake.hpp"
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <string>
|
#include <string>
|
||||||
#if !defined(NDEBUG)
|
#if !defined(NDEBUG)
|
||||||
|
@ -35,12 +35,10 @@ namespace duck {
|
||||||
SysPaths();
|
SysPaths();
|
||||||
~SysPaths() noexcept = default;
|
~SysPaths() noexcept = default;
|
||||||
|
|
||||||
std::string_view operator[] (std::string_view name) const noexcept;
|
EnvFake& env() noexcept { return m_env; }
|
||||||
std::optional<std::string_view> env (std::string_view name) const noexcept;
|
const EnvFake& env() const noexcept { return m_env; }
|
||||||
std::string_view env (std::string_view name, std::string_view def) const noexcept;
|
|
||||||
|
|
||||||
void set_colours (std::string_view colours);
|
void add_to_env (std::string_view colours);
|
||||||
VarMapView colours() const { return {&m_colours}; }
|
|
||||||
|
|
||||||
const path_type& root() const;
|
const path_type& root() const;
|
||||||
const path_type& eroot() const;
|
const path_type& eroot() const;
|
||||||
|
@ -49,8 +47,7 @@ namespace duck {
|
||||||
const path_type& gcc_env_d() const;
|
const path_type& gcc_env_d() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
VarMap m_env;
|
EnvFake m_env;
|
||||||
VarMap m_colours;
|
|
||||||
path_type m_root;
|
path_type m_root;
|
||||||
path_type m_eprefix;
|
path_type m_eprefix;
|
||||||
path_type m_eroot;
|
path_type m_eroot;
|
||||||
|
|
|
@ -17,10 +17,13 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "var_map.hpp"
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
namespace duck {
|
namespace duck {
|
||||||
|
typedef std::unordered_map<std::string_view, std::string_view> VarMap;
|
||||||
|
|
||||||
//parameter is non-const so caller hopefully remembers to keep it alive
|
//parameter is non-const so caller hopefully remembers to keep it alive
|
||||||
//(ie: they can't just pass a temporary in)
|
//(ie: they can't just pass a temporary in)
|
||||||
VarMap to_var_map (std::string& text);
|
VarMap to_var_map (std::string& text);
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
/* Copyright 2020, Michele Santullo
|
|
||||||
* This file is part of user-gcc.
|
|
||||||
*
|
|
||||||
* User-gcc is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* User-gcc is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with user-gcc. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "var_map.hpp"
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
namespace duck {
|
|
||||||
VarMapView::VarMapView (const VarMap* vm) :
|
|
||||||
m_map(vm)
|
|
||||||
{
|
|
||||||
assert(m_map);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string_view VarMapView::operator[] (std::string_view key) const {
|
|
||||||
if (key.empty())
|
|
||||||
return "";
|
|
||||||
|
|
||||||
assert(m_map);
|
|
||||||
auto it_found = m_map->find(key);
|
|
||||||
if (m_map->end() != it_found)
|
|
||||||
return it_found->second;
|
|
||||||
else
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
} //namespace duck
|
|
|
@ -1,34 +0,0 @@
|
||||||
/* Copyright 2020, Michele Santullo
|
|
||||||
* This file is part of user-gcc.
|
|
||||||
*
|
|
||||||
* User-gcc is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* User-gcc is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with user-gcc. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <unordered_map>
|
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
namespace duck {
|
|
||||||
typedef std::unordered_map<std::string_view, std::string_view> VarMap;
|
|
||||||
|
|
||||||
class VarMapView {
|
|
||||||
public:
|
|
||||||
VarMapView (const VarMap* vm);
|
|
||||||
std::string_view operator[] (std::string_view key) const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
const VarMap* m_map;
|
|
||||||
};
|
|
||||||
} //namespace duck
|
|
Loading…
Reference in a new issue