Add --norc, --help, -e, and FILE options

This commit is contained in:
Dr-Carlos 2022-03-31 19:54:44 +10:30
parent 59ed0c8621
commit b6fbc352e0
3 changed files with 109 additions and 12 deletions

View File

@ -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="");

View File

@ -4,6 +4,7 @@
#include <csignal>
#include <csetjmp>
#include <functional>
#include <getopt.h>
#include <readline/readline.h>
#include <readline/history.h>
@ -198,9 +199,81 @@ static std::string read_interactive(Session const &s, bool &leave)
return cmd;
}
int norc;
char* execute_arg = NULL;
char* extra_rom = NULL;
static void parse_options(int argc, char **argv) {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"execute", required_argument, 0, 'e'},
{"norc", no_argument, &norc, 1},
{0, 0, 0, 0}
};
bool show_help;
int opt, option_index;
while (1) {
opt = getopt_long(argc, argv, "he:", long_options, &option_index);
if (opt == -1)
break;
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':
if (execute_arg) {
printf("--execute/-e option cannot be used twice. Use ';' to separate commands.\n");
exit(1);
}
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);
}
}
if (show_help) {
printf("Usage: %s [OPTION]... [FILE]\n", argv[0]);
printf("Open the fxos debugger, 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\n");
printf(" --norc\t\tdo not execute any fxosrc files\n");
printf(" -h, --help\t\tdisplay this help and exit\n");
exit(0);
}
// if (norc)
// option_index++;
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_options(argc, argv);
Session &s = global_session;
theme_builtin("tomorrow-night");
rl_completion_entry_function = autocomplete;
@ -246,24 +319,45 @@ int main(int argc, char **argv)
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");
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);
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 (extra_rom && !has_idled) {
_vc(s, "file");
_vs(s, "file");
bool leave = false;
std::string cmdline = read_interactive(s, leave);
if(leave) break;
lex_repl(cmdline);
_vm(s, extra_rom, {MemoryRegion::ROM, MemoryRegion::ROM_P2});
}
if (execute_arg) {
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);
}
has_idled = true;
}
Parser parser(false);

View File

@ -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);