forked from Lephenixnoir/fxos
Merge pull request 'Add --norc, --help, -e, and FILE options' (#1) from Dr-Carlos/fxos:master into master
Reviewed-on: https://gitea.planet-casio.com/Lephenixnoir/fxos/pulls/1
This commit is contained in:
commit
ae1db62314
|
@ -28,6 +28,9 @@ void _vl(Session &s, std::vector<std::string> const &spaces);
|
|||
/* Select the current virtual space */
|
||||
void _vs(Session &s, std::string const &name);
|
||||
|
||||
/* Create a new virtual space */
|
||||
void _vc(Session &s, std::string name);
|
||||
|
||||
/* Create a new virtual space from a target */
|
||||
void _vct(Session &s, std::string filename, std::string space="");
|
||||
|
||||
|
|
127
shell/main.cpp
127
shell/main.cpp
|
@ -4,6 +4,7 @@
|
|||
#include <csignal>
|
||||
#include <csetjmp>
|
||||
#include <functional>
|
||||
#include <getopt.h>
|
||||
|
||||
#include <readline/readline.h>
|
||||
#include <readline/history.h>
|
||||
|
@ -198,9 +199,83 @@ static std::string read_interactive(Session const &s, bool &leave)
|
|||
return cmd;
|
||||
}
|
||||
|
||||
int norc;
|
||||
std::string execute_arg;
|
||||
char* extra_rom = NULL;
|
||||
|
||||
static void parse_options(int argc, char **argv) {
|
||||
/* List of options */
|
||||
static struct option long_options[] = {
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"execute", required_argument, 0, 'e'},
|
||||
{"norc", no_argument, &norc, 1},
|
||||
/* This is here as a fallback if nothing is passed */
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
bool show_help;
|
||||
|
||||
int opt, option_index;
|
||||
while (1) {
|
||||
/* Get the next command-line option */
|
||||
opt = getopt_long(argc, argv, "he:", long_options, &option_index);
|
||||
|
||||
if (opt == -1)
|
||||
break;
|
||||
|
||||
/* Handle options */
|
||||
switch (opt) {
|
||||
case 0:
|
||||
/* This happens if we store the argument to a variable (e.g. --norc) */
|
||||
break;
|
||||
case 'h':
|
||||
show_help = true;
|
||||
break;
|
||||
case 'e':
|
||||
execute_arg += ";";
|
||||
execute_arg += optarg;
|
||||
break;
|
||||
case '?':
|
||||
printf("Try %s --help for more information.\n", argv[0]);
|
||||
exit(1);
|
||||
default:
|
||||
printf("Try %s --help for more information.\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Print help message if --help is used at least once */
|
||||
if (show_help) {
|
||||
printf("Usage: %s [OPTION]... [FILE]\n", argv[0]);
|
||||
printf("Open the fxos disassembler, optionally with [FILE] loaded into "
|
||||
"a new empty space with ROM and ROM_P2\n");
|
||||
printf("Options:\n");
|
||||
printf(" -e, --execute=CMD\texecute CMD and exit, can be used "
|
||||
"multiple times\n");
|
||||
printf(" --norc\t\tdo not execute any fxosrc files\n");
|
||||
printf(" -h, --help\t\tdisplay this help and exit\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
/* Handle positional arguments */
|
||||
if (optind < argc) {
|
||||
if ((argc - optind) > 1) {
|
||||
printf("%s only supports 1 positional argument, FILE.\n", argv[0]);
|
||||
printf("Try %s --help for more information.\n", argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
extra_rom = argv[optind++];
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
/* Parse command-line options first */
|
||||
parse_options(argc, argv);
|
||||
|
||||
Session &s = global_session;
|
||||
|
||||
theme_builtin("tomorrow-night");
|
||||
|
||||
rl_completion_entry_function = autocomplete;
|
||||
|
@ -245,25 +320,53 @@ int main(int argc, char **argv)
|
|||
snprintf(histfile, 128, "%s/.fxos_history", std::getenv("HOME"));
|
||||
if(histfile[0]) read_history(histfile);
|
||||
|
||||
/* Load fxosrc files from all library folders */
|
||||
std::vector<std::string> fxosrc_files;
|
||||
for(auto const &path: s.path) {
|
||||
if(fs::exists(path / "fxosrc"))
|
||||
fxosrc_files.push_back(path / "fxosrc");
|
||||
/* Load fxosrc files from all library folders if wanted */
|
||||
if (!norc) {
|
||||
std::vector<std::string> fxosrc_files;
|
||||
for(auto const &path: s.path) {
|
||||
if(fs::exists(path / "fxosrc"))
|
||||
fxosrc_files.push_back(path / "fxosrc");
|
||||
}
|
||||
if(fxosrc_files.size() > 0)
|
||||
_dot(s, fxosrc_files, true);
|
||||
}
|
||||
if(fxosrc_files.size() > 0)
|
||||
_dot(s, fxosrc_files, true);
|
||||
|
||||
|
||||
/* Stores whether we have already idled once, handling extra rom
|
||||
* and execute options */
|
||||
bool has_idled;
|
||||
|
||||
/* Shell main loop */
|
||||
while(true) {
|
||||
/* Get a command if there isn't a file being executed */
|
||||
if(lex_idle()) {
|
||||
while(sigsetjmp(sigint_buf, 1) != 0);
|
||||
/* If we passed in FILE and we haven't handled it yet */
|
||||
if (extra_rom && !has_idled) {
|
||||
/* Create new 'file' virtual space and switch to it */
|
||||
_vc(s, "file");
|
||||
_vs(s, "file");
|
||||
|
||||
bool leave = false;
|
||||
std::string cmdline = read_interactive(s, leave);
|
||||
if(leave) break;
|
||||
lex_repl(cmdline);
|
||||
/* Add FILE as a ROM and ROM_P2 */
|
||||
_vm(s, extra_rom, {MemoryRegion::ROM, MemoryRegion::ROM_P2});
|
||||
}
|
||||
|
||||
/* If we need to execute a command */
|
||||
if (!execute_arg.empty()) {
|
||||
if (has_idled)
|
||||
exit(0);
|
||||
else
|
||||
lex_repl(execute_arg);
|
||||
} else {
|
||||
while(sigsetjmp(sigint_buf, 1) != 0);
|
||||
|
||||
bool leave = false;
|
||||
std::string cmdline = read_interactive(s, leave);
|
||||
if(leave) break;
|
||||
lex_repl(cmdline);
|
||||
}
|
||||
|
||||
/* We have already handled FILE and --execute, don't do it again */
|
||||
has_idled = true;
|
||||
}
|
||||
|
||||
Parser parser(false);
|
||||
|
|
|
@ -100,7 +100,7 @@ static std::string parse_vc(Session &, Parser &parser)
|
|||
return name;
|
||||
}
|
||||
|
||||
static void _vc(Session &session, std::string name)
|
||||
void _vc(Session &session, std::string name)
|
||||
{
|
||||
if(name == "") name = session.generate_space_name("space", true);
|
||||
else name = session.generate_space_name(name, false);
|
||||
|
|
Loading…
Reference in New Issue