mpw-shell/environment.cpp

111 lines
2.2 KiB
C++
Raw Normal View History

2016-02-02 01:38:29 +00:00
#include "environment.h"
#include <cstdio>
#include <cstdarg>
2016-02-02 21:19:13 +00:00
#include "error.h"
2016-02-02 01:38:29 +00:00
namespace {
std::string &lowercase(std::string &s) {
std::transform(s.begin(), s.end(), s.begin(), [](char c){ return std::tolower(c); });
return s;
}
/* 0 or "" -> false. all others -> true */
bool tf(const std::string &s) {
if (s.empty()) return false;
if (s.size() == 1 && s == "0") return false;
return true;
}
}
Environment::iterator Environment::find( const std::string & key ) {
std::string k(key);
lowercase(k);
return _table.find(k);
}
Environment::const_iterator Environment::find( const std::string & key ) const {
std::string k(key);
lowercase(k);
return _table.find(k);
}
void Environment::set(const std::string &key, const std::string &value, bool exported) {
std::string k(key);
lowercase(k);
if (k == "echo") _echo = tf(value);
if (k == "exit") _exit = tf(value);
if (k == "test") _test = tf(value);
// don't need to check {status} because that will be clobbered
// by the return value.
EnvironmentEntry v(value, exported);
auto iter = _table.find(k);
if (iter == _table.end()) {
_table.emplace(std::make_pair(k, std::move(v)));
}
else {
// if previously exported, keep exported.
if (iter->second) v = true;
iter->second = std::move(v);
}
}
void Environment::unset(const std::string &key) {
std::string k(key);
lowercase(k);
if (k == "echo") _echo = false;
if (k == "exit") _exit = false;
if (k == "test") _test = false;
_table.erase(k);
}
void Environment::unset() {
_table.clear();
_echo = false;
_exit = false;
_test = false;
_status = 0;
}
2016-02-02 21:19:13 +00:00
int Environment::status(int i, const std::nothrow_t &) {
2016-02-02 01:38:29 +00:00
if (_status == i) return i;
_status = i;
_table["status"] = std::to_string(i);
return i;
}
2016-02-02 21:19:13 +00:00
int Environment::status(int i, bool throwup) {
2016-02-02 01:38:29 +00:00
status(i, std::nothrow);
2016-02-02 21:19:13 +00:00
if (throwup && _exit && i) {
throw execution_of_input_terminated(i);
2016-02-02 01:38:29 +00:00
}
return i;
}
void Environment::echo(const char *fmt, ...) {
2016-02-02 21:19:13 +00:00
if (_echo && !_startup) {
for (unsigned i = 0; i < _indent; ++i) {
fputc(' ', stderr);
fputc(' ', stderr);
}
2016-02-02 01:38:29 +00:00
va_list ap;
va_start(ap, fmt);
va_end(ap);
vfprintf(stderr, fmt, ap);
2016-02-02 21:19:13 +00:00
fputc('\n', stderr);
2016-02-02 01:38:29 +00:00
}
}
2016-02-02 21:19:13 +00:00