fxos/shell/e.cpp

76 lines
1.9 KiB
C++

#include "shell.h"
#include "parser.h"
#include "commands.h"
#include <fmt/core.h>
#include <optional>
//---
// e
//---
struct _e_args
{
std::string space_name;
std::vector<long> values;
};
static _e_args parse_e(Session &session, Parser &parser)
{
_e_args args {};
parser.option("vspace",
[&args](std::string const &value) { args.space_name = value; });
VirtualSpace *space = session.get_space(args.space_name);
if(space) {
while(!parser.at_end()) {
args.values.push_back(parser.expr(space));
}
}
// TODO: Error message when session specified in _e does not exist
parser.end();
return args;
}
void _e(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");
}
}
static ShellCommand _e_cmd(
"e",
[](Session &s, Parser &p) {
auto const &args = parse_e(s, p);
_e(s, args.space_name, args.values);
},
[](Session &s, Parser &p) { parse_e(s, p); }, "Evaluate expression", R"(
e [vspace=<virtual_space>] [<expression>...]
Evaluates the specified expressions. The expressions may include syscall
references (%0ab), named symbols (TRA), the current cursor ($), and
arithmetic expressions.
The parser doesn't accept arithmetic expressions directly on the command-line;
they must be placed within parentheses.
The resulting values undergo a simple analysis which recovers symbol names and
syscall addresses within the named virtual space, or using the current virtual
space if not provided.
e TRA ($+2)
Evaluate the address of the TRA register, and the address of the next
instruction.
)");