fxos/shell/session.cpp

64 lines
1.5 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;
}
std::string Session::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++;
}
}
void Session::require_vspace(std::string name) const
{
if(name == "" && !this->current_space)
throw NoVirtualSpaceError();
if(name != "" && !this->spaces.count(name))
throw NoVirtualSpaceError(name);
}
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
}