GaspardTerminal/src/commands.cpp¶
GaspardTerminal source module. Keep its repository-relative directory layout alongside the shared bindings.
Source files · Download commands.cpp
#include "language.hpp"
#include <cstdio>
namespace terminal
{
static std::string join(const Arguments& args, std::size_t first)
{
std::string result;
for (std::size_t i = first; i < args.size(); ++i)
{
if (i > first)
result += ' ';
result += args[i];
}
return result;
}
static int help(Engine& e, const Arguments& a)
{
if (a.size() > 2)
return -22;
std::string target = a.size() == 2 ? a[1] : "";
for (std::size_t i = 0; i < target.size(); ++i)
if (target[i] >= 'A' && target[i] <= 'Z')
target[i] += 'a' - 'A';
bool found = false;
for (std::size_t i = 0; i < e.commands().size(); ++i)
{
const Command& c = e.commands()[i];
if (a.size() == 1 || target == c.name)
{
e.line(c.usage + " - " + c.description);
found = true;
}
}
if (a.size() == 1)
e.line("Variables: $NAME or ${NAME}; quotes: 'literal' / \"expanded\"; ; separates "
"commands; !! / !N recall history.");
return found ? 0 : -2;
}
static int echo(Engine& e, const Arguments& a)
{
e.line(join(a, 1));
return 0;
}
static int set(Engine& e, const Arguments& a)
{
if (a.size() == 1)
{
for (std::map<std::string, std::string>::const_iterator i = e.variables().begin();
i != e.variables().end();
++i)
e.line(i->first + " = " + i->second);
e.line("PWD = " + e.cwd());
return 0;
}
if (a.size() == 2)
{
std::string value;
if (!e.variable(a[1], value))
return -2;
e.line(value);
return 0;
}
return e.set(a[1], join(a, 2)) ? 0 : -22;
}
static int unset(Engine& e, const Arguments& a)
{
return a.size() == 2 && e.unset(a[1]) ? 0 : -22;
}
static int pwd(Engine& e, const Arguments& a)
{
if (a.size() != 1)
return -22;
e.line(e.cwd());
return 0;
}
static int cd(Engine& e, const Arguments& a)
{
return a.size() <= 2 ? e.changeDirectory(a.size() == 1 ? "" : a[1]) : -22;
}
static int ls(Engine& e, const Arguments& a)
{
if (a.size() > 2)
return -22;
std::string path = e.path(a.size() == 1 ? "" : a[1]);
return path.empty() ? -22 : e.backend().list(path);
}
static int run(Engine& e, const Arguments& a)
{
if (a.size() < 2 || a.size() > 21)
return -22;
std::string path = e.path(a[1]);
if (path.empty() || path == "/")
return -22;
Arguments argv(a.begin() + 1, a.end());
argv[0] = path;
return e.backend().launch(path, argv);
}
static int history(Engine& e, const Arguments& a)
{
if (a.size() != 1)
return -22;
for (std::size_t i = 0; i < e.history().size(); ++i)
{
char id[32];
std::sprintf(id, "%u ", e.history()[i].id);
e.line(id + e.history()[i].line);
}
return 0;
}
static int repeat(Engine& e, const Arguments& a)
{
if (a.size() != 3 || a[1].empty() || a[1].size() > 4)
return -22;
unsigned count = 0;
for (std::size_t i = 0; i < a[1].size(); ++i)
{
if (a[1][i] < '0' || a[1][i] > '9')
return -22;
count = count * 10 + a[1][i] - '0';
}
return e.repeat(count, a[2]) ? 0 : -22;
}
static int clear(Engine& e, const Arguments& a)
{
if (a.size() != 1)
return -22;
e.output().clear();
return 0;
}
static int exit(Engine& e, const Arguments& a)
{
if (a.size() != 1)
return -22;
e.exit();
return 0;
}
// ADDING A COMMAND:
// 1. Define int myCommand(Engine&, const Arguments&) in a separate module.
// arguments[0] is its name. Return 0 or a nonzero error code.
// 2. Register Command("name", "usage", "help text", &myCommand).
// 3. Call that module's registration function during application startup.
// help uses the same registry automatically; no parser change is necessary.
void registerCoreCommands(Engine& e)
{
e.registerCommand(
Command("help", "help [command]", "Show language and command help", help));
e.registerCommand(Command("echo", "echo [text ...]", "Print expanded arguments", echo));
e.registerCommand(
Command("set", "set [NAME [value ...]]", "List, inspect or assign a variable", set));
e.registerCommand(Command("unset", "unset NAME", "Remove a user variable", unset));
e.registerCommand(
Command("history", "history", "Show the last 64 submitted lines", history));
e.registerCommand(
Command("repeat", "repeat N \"command\"", "Run a batch N times (1..1000)", repeat));
e.registerCommand(Command("clear", "clear", "Clear terminal scrollback", clear));
e.registerCommand(Command("exit", "exit", "Close GaspardTerminal", exit));
}
void registerFileCommands(Engine& e)
{
e.registerCommand(Command("pwd", "pwd", "Print current directory", pwd));
e.registerCommand(
Command("cd", "cd [path | .. | -]", "Change directory; no argument returns to /", cd));
e.registerCommand(
Command("ls", "ls [path]", "List a directory; / lists mounted volumes", ls));
}
void registerProcessCommands(Engine& e)
{
e.registerCommand(Command("run",
"run program.elf [arguments ...]",
"Launch an ELF64 program asynchronously",
run));
}
void registerDefaultCommands(Engine& e)
{
registerCoreCommands(e);
registerFileCommands(e);
registerProcessCommands(e);
}
} // namespace terminal