fxos/shell/session.cpp

62 lines
1.6 KiB
C++

#include "session.h"
#include "parser.h"
#include "errors.h"
#include <fmt/core.h>
Session::Session(): spaces {}
{
this->current_space = nullptr;
this->pc = -1;
}
VirtualSpace *Session::get_space(std::string name)
{
auto const &it = this->spaces.find(name);
return it == this->spaces.end() ? nullptr : it->second.get();
}
std::string Session::generate_space_name(std::string prefix, bool force_suffix)
{
if(!force_suffix && this->spaces.count(prefix) == 0)
return prefix;
int counter = 0;
while(1) {
std::string name = fmt::format("{}_{}", prefix, counter);
if(!this->spaces.count(name))
return name;
counter++;
}
}
fs::path Session::file(std::string name)
{
#define err(...) std::runtime_error(fmt::format(__VA_ARGS__))
fs::path relative_to = lex_last_used_file();
if(name[0] != '/') {
fs::path rel = relative_to.parent_path() / name;
if(!fs::exists(rel))
throw err("cannot find {} in current directory", name);
if(fs::symlink_status(rel).type() != fs::file_type::regular)
throw err("{} is neither a file nor a symlink to a file", name);
return rel;
}
fs::path filepath(name.substr(1));
for(auto const &p: this->path) {
if(!fs::exists(p / filepath))
continue;
/* This file exists, it can be the only one selected */
if(fs::symlink_status(p / filepath).type() != fs::file_type::regular)
throw err("{} is neither a file nor a symlink to a file", name);
return p / filepath;
}
throw err("cannot find {} in library", name);
#undef err
}