shell: go back to unix shell-like options

This commit is contained in:
Lephenixnoir 2023-09-25 21:26:05 +02:00
parent 2a3f1845de
commit dffc3c7b02
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
6 changed files with 42 additions and 50 deletions

View File

@ -21,8 +21,8 @@ static _e_args parse_e(Session &session, Parser &parser)
{
_e_args args {};
parser.option("binary",
[&args](std::string const &value) { args.binary_name = value; });
parser.option("-b",
[&args](Parser &p) { args.binary_name = p.symbol("binary_name"); });
parser.accept_options();
@ -70,18 +70,14 @@ static ShellCommand _e_cmd(
_e(s, args.binary_name, args.values);
},
[](Session &s, Parser &p) { parse_e(s, p); }, "Evaluate expression", R"(
e [binary=<binary_name>] [<expression>...]
e [-b <binary>] [<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.
references (%0ab), named symbols (TRA), and arithmetic expressions (within
parentheses).
The resulting values undergo a simple analysis which recovers symbol names and
syscall addresses within the named binary, or using the current binary if not
provided.
syscall addresses within the current binary (or the one specified with -b).
e TRA (Bdisp_PutDisp_DD+2)
Evaluate the address of the TRA register, and the address of the second

View File

@ -175,8 +175,8 @@ static struct _io_args parse_io(Session &session, Parser &parser)
_io_args args;
std::string binname = session.currentBinaryName();
parser.option(
"binary", [&binname](std::string const &value) { binname = value; });
parser.option("-b",
[&binname](Parser &p) { binname = p.symbol("binary_name"); });
parser.accept_options();
args.binary = session.project().getBinary(binname);
@ -280,9 +280,12 @@ static char const *syscall_nonrom_str = " %%%04x -> %08x (%s memory)\n";
static std::string parse_ios(Session &, Parser &parser)
{
std::string name = parser.at_end() ? "" : parser.symbol("binary_name");
std::string binname;
parser.option("-b",
[&binname](Parser &p) { binname = p.symbol("binary_name"); });
parser.accept_options();
parser.end();
return name;
return binname;
}
void _ios(Session &session, std::string name)
@ -399,11 +402,10 @@ static struct _isc_args parse_isc(Session &session, Parser &parser)
{
_isc_args args;
parser.option("sort",
[&args](std::string const &value) { args.sort = (value == "true"); });
parser.option("binary",
[&args](std::string const &value) { args.binary_name = value; });
parser.option("-s", [&args](Parser &) { args.sort = true; });
parser.option("-b", [&args](Parser &p) {
args.binary_name = p.symbol("binary_name");
});
parser.accept_options();
Binary *binary = session.currentBinary();
@ -537,20 +539,20 @@ static ShellCommand _io_cmd(
_io(s, args.binary, args.addresses);
},
[](Session &s, Parser &p) { parse_io(s, p); }, "Info Objects", R"(
io [binary=<binary_name>] [<expr>...]
io [-b <binary>] [<expr>...]
Lists all objects in the specified binary (default: the current one). If
Lists all objects in the current binary (or the one specified with -b). If
expressions are provided, instead list objects defined at these addresses or
that cover these addresses. With sort=true, sorts by increasing addresses.
that cover these addresses.
)");
static ShellCommand _ios_cmd(
"ios", [](Session &s, Parser &p) { _ios(s, parse_ios(s, p)); },
[](Session &s, Parser &p) { parse_ios(s, p); }, "Info OS", R"(
ios [<binary_name>]
ios [-b <binary>]
Prints information about the OS mapped in the named virtual space (defaults to
the current one). This usually requires an OS binary to be mapped to ROM.
Prints information about the OS mapped in current binary (or the one specified
with -b). This usually requires an OS binary to be mapped to ROM.
)");
static ShellCommand _ip_cmd(
@ -559,7 +561,7 @@ static ShellCommand _ip_cmd(
ip
Projects information about the currently-loaded project, and paths and names of
recent projects;
recent projects.
)");
static ShellCommand _isc_cmd(
@ -569,9 +571,9 @@ static ShellCommand _isc_cmd(
_isc(s, args.binary_name, args.sort, args.addresses);
},
[](Session &s, Parser &p) { parse_isc(s, p); }, "Info Syscalls", R"(
isc [sort=true] [binary=<binary_name>] [<address>...]
isc [-b <binary>] [-s] [<address>...]
Prints the syscall table for the specified binary (default: the current one).
By default, syscalls are enumerated by syscall number. If sort=true is
specified, they are instead sorted by address.
Prints the syscall table for the current binary (or the one specified with -b).
By default, syscalls are enumerated by syscall number. If -s is given, they are
instead sorted by address.
)");

View File

@ -148,7 +148,7 @@ num_suffix [kMG]
num ({num_hex}|{num_dec}|{num_bin}){num_suffix}?
syscall [%][a-fA-F0-9]+
option [a-zA-Z]+=[^ ]*
option -[a-zA-Z]
symbol \.|\.?[a-zA-Z_][a-zA-Z0-9_.]*
space [ \t]+
@ -175,7 +175,6 @@ space [ \t]+
<EXPR>">>" { return '>'; }
<EXPR>"<<" { return '<'; }
<*>"$" { return '$'; }
<*>"(" { lex_current_input().expr_depth++;
BEGIN(EXPR); return '('; }
":" { return ':'; }

View File

@ -289,18 +289,12 @@ void Parser::accept_options()
{
while(m_la.type == T::OPTION) {
Token t = expect(T::OPTION);
std::string opt = t.value.STRING;
std::string name = opt.substr(0, opt.find('='));
if(!m_options.count(name)) {
if(!m_complete)
FxOS_log(ERR, "unrecognized option %s", name);
}
else {
std::string value = opt.substr(opt.find('=') + 1);
m_options[name](value);
}
if(m_options.count(opt))
m_options[opt](*this);
else if(!m_complete)
FxOS_log(ERR, "unrecognized option %s", opt);
}
}

View File

@ -129,7 +129,7 @@ public:
Token expect(T type, bool ignore_spaces=true);
Token expect(std::initializer_list<T> types, bool ignore_spaces=true);
using OptionHandler = std::function<void(std::string const &value)>;
using OptionHandler = std::function<void(Parser &)>;
/* Specify an option to be accepted until the next SEPARATOR or END token
(basically for the current command). The function will be called when

View File

@ -51,11 +51,11 @@ static _sh_args parse_sh(Session &session, Parser &parser)
args.distance = -1;
args.align = -1;
parser.option("align", [&args](std::string const &value) {
args.align = atoi(value.c_str());
parser.option("-a", [&args, &session](Parser &p) {
args.align = p.expr(session.currentBinary());
});
parser.option("distance", [&args](std::string const &value) {
args.distance = atoi(value.c_str());
parser.option("-d", [&args, &session](Parser &p) {
args.distance = p.expr(session.currentBinary());
});
parser.accept_options();
@ -267,7 +267,7 @@ static ShellCommand _sh_cmd(
},
[](Session &s, Parser &p) { parse_sh(s, p); }, "Search Hexadecimal pattern",
R"(
sh [align=<value>] "<pattern>" [<regions>...]
sh [-a <alignment>] [-d <distance>] "<pattern>" [<regions>...]
Searches mapped memory for a hexadecimal pattern with hole bytes. The pattern
should be a hexadecimal string like "0123..6789..cd", consisting either of
@ -278,8 +278,9 @@ An occurrence of the pattern is any sequence of bytes that matches the explicit
bytes exactly, with no constraint on the hole bytes (ie. any value matches).
If regions are specified, searches these regions. Otherwise, searches every
binding in the current virtual space. If align=<align> is specified, only
considers addresses that are multiples of the specified alignment value.
binding in the current virtual space. If -a is given, only considers addresses
that are multiples of the specified alignment value. -d influences how much
context is shown in result hexdumps.
sh "434153494f......00" ROM
Searches strings ending with "CASIO" followed by up to 3 characters in ROM.