core.cpp
sleela-terminal/core.cpp · 154 line(s) · c
// SleelaTerminal(TM) shell -- L1 core model implementation.
// Original SLeeLa work (see NOTICE).
#include "core.hpp"
#include <cctype>
#include <cstdlib>
#include <pwd.h>
#include <unistd.h>
extern char** environ;
namespace sleela::sh {
long Value::asInt() const noexcept {
std::size_t i = 0;
const std::size_t n = text.size();
while (i < n && std::isspace(static_cast<unsigned char>(text[i]))) ++i;
long sign = 1;
if (i < n && (text[i] == '+' || text[i] == '-')) {
if (text[i] == '-') sign = -1;
++i;
}
long v = 0;
bool any = false;
while (i < n && std::isdigit(static_cast<unsigned char>(text[i]))) {
v = v * 10 + (text[i] - '0');
++i;
any = true;
}
return any ? sign * v : 0;
}
Value Value::fromInt(long v) { return Value(std::to_string(v)); }
Environment::Environment() {
for (char** e = ::environ; e && *e; ++e) {
std::string entry(*e);
const auto eq = entry.find('=');
if (eq == std::string::npos) continue;
const std::string name = entry.substr(0, eq);
const std::string value = entry.substr(eq + 1);
vars_[name] = Var{value, true};
}
// Prefer the environment's account name; fall back to the POSIX account DB.
std::string username = get("USER");
if (username.empty()) username = get("LOGNAME");
if (username.empty()) {
if (const passwd* account = ::getpwuid(::getuid())) {
if (account->pw_name) username = account->pw_name;
}
}
if (username.empty()) username = "user";
// Keep the stored prompt plain text; slsh applies colours only when rendering.
char hostname_buffer[256] = {};
std::string hostname;
if (::gethostname(hostname_buffer, sizeof(hostname_buffer) - 1) == 0) {
hostname_buffer[sizeof(hostname_buffer) - 1] = '\0';
hostname = hostname_buffer;
}
if (hostname.empty()) hostname = "computer";
prompt_ = username + "@" + hostname + ": ";
}
std::string Environment::get(const std::string& name) const {
const auto it = vars_.find(name);
return it == vars_.end() ? std::string() : it->second.value;
}
bool Environment::has(const std::string& name) const { return vars_.find(name) != vars_.end(); }
void Environment::set(const std::string& name, const std::string& value) {
auto& v = vars_[name];
v.value = value;
}
void Environment::unset(const std::string& name) { vars_.erase(name); }
void Environment::exportVar(const std::string& name) { vars_[name].exported = true; }
bool Environment::isExported(const std::string& name) const {
const auto it = vars_.find(name);
return it != vars_.end() && it->second.exported;
}
std::vector<std::string> Environment::exportedEnviron() const {
std::vector<std::string> out;
out.reserve(vars_.size());
for (const auto& [name, var] : vars_) {
if (var.exported) out.push_back(name + "=" + var.value);
}
return out;
}
Environment Environment::scopedCopy() const {
Environment copy;
copy.vars_ = vars_;
copy.functions_ = functions_;
copy.positionals_ = positionals_;
copy.prompt_ = prompt_;
copy.last_status_ = last_status_;
copy.should_exit_ = false;
copy.exit_code_ = 0;
copy.loop_break_ = 0;
copy.loop_continue_ = 0;
copy.jobs_.clear();
copy.next_job_id_ = 1;
return copy;
}
void Environment::defineFunction(const std::string& name, std::shared_ptr<Node> body) {
functions_[name] = std::move(body);
}
std::shared_ptr<Node> Environment::lookupFunction(const std::string& name) const {
const auto it = functions_.find(name);
return it == functions_.end() ? nullptr : it->second;
}
bool Environment::hasFunction(const std::string& name) const {
return functions_.find(name) != functions_.end();
}
void Environment::setPositionals(std::vector<std::string> args) { positionals_ = std::move(args); }
std::string Environment::getPositional(std::size_t n) const {
if (n == 0 || n > positionals_.size()) return std::string();
return positionals_[n - 1];
}
int Environment::addJob(long pid, const std::string& command) {
Job j;
j.id = next_job_id_++;
j.pid = pid;
j.command = command;
j.running = true;
jobs_.push_back(j);
return j.id;
}
Environment::Job* Environment::findJob(int id) noexcept {
for (auto& j : jobs_) if (j.id == id) return &j;
return nullptr;
}
void Environment::removeJob(int id) noexcept {
for (auto it = jobs_.begin(); it != jobs_.end(); ++it) {
if (it->id == id) { jobs_.erase(it); return; }
}
}
} // namespace sleela::sh