fxos/shell/e.cpp

112 lines
2.4 KiB
C++

#include "shell.h"
#include "parser.h"
#include "commands.h"
#include <fmt/core.h>
#include <optional>
//---
// e
//---
static std::vector<long> parse_e(Session &session, Parser &parser)
{
std::vector<long> values;
while(!parser.at_end()) {
values.push_back(parser.expr(session.current_space));
}
parser.end();
return values;
}
void _e(Session &session, std::vector<long> const &values)
{
for(long value: values) {
/* Hexa format */
int length = (labs(value) <= (1ll << 32) ? 8 : 16) + 2 + (value < 0);
std::string format = fmt::format("{{:#0{}x}}", length);
fmt::print(format, value);
long a = abs(value);
if(a <= 100 || a % 100 <= 1 || a % 100 >= 99)
fmt::print(" = {}", a);
VirtualSpace *space = session.current_space;
if(space) {
std::optional<std::string> opt;
/* Promote to syscall ID */
OS *os = space->os_analysis();
int syscall_id;
if(os && (syscall_id = os->find_syscall(value)) != -1) {
fmt::print(" = %{:03x}", syscall_id);
opt = space->symbols.query(Symbol::Syscall, syscall_id);
if(opt) fmt::print(" = {}", *opt);
}
opt = space->symbols.query(Symbol::Address, value);
if(opt) fmt::print(" = {}", *opt);
}
fmt::print("\n");
}
}
//---
// ev
//---
struct _ev_args {
std::string space_name;
std::vector<long> values;
};
static _ev_args parse_ev(Session &session, Parser &parser)
{
_ev_args args {};
args.space_name = parser.symbol("space_name");
session.require_vspace(args.space_name);
VirtualSpace *space = &session.spaces.at(args.space_name);
while(!parser.at_end()) {
args.values.push_back(parser.expr(space));
}
parser.end();
return args;
}
void _ev(Session &, std::string, std::vector<long> const &values)
{
for(long value: values) {
/* Hexa format */
int length = (labs(value) <= (1ll << 32) ? 8 : 16) + 2 + (value < 0);
std::string format = fmt::format("{{:#0{}x}}", length);
fmt::print(format, value);
long a = abs(value);
if(a <= 100 || a % 100 <= 1 || a % 100 >= 99)
fmt::print(" = {}", a);
fmt::print("\n");
}
}
[[gnu::constructor]] static void _(void)
{
shell_register_command("e",
[](Session &s, Parser &p){ _e(s, parse_e(s, p)); },
[](Session &s, Parser &p){ parse_e(s, p); });
shell_register_command("ev",
[](Session &s, Parser &p){
auto const &args = parse_ev(s, p);
_ev(s, args.space_name, args.values); },
[](Session &s, Parser &p){ parse_ev(s, p); });
}