From b6fbc352e0b6de58d3d4468b197dd49e90f1ab91 Mon Sep 17 00:00:00 2001 From: Dr-Carlos Date: Thu, 31 Mar 2022 19:54:44 +1030 Subject: [PATCH 1/3] Add --norc, --help, -e, and FILE options --- shell/commands.h | 3 ++ shell/main.cpp | 116 ++++++++++++++++++++++++++++++++++++++++++----- shell/v.cpp | 2 +- 3 files changed, 109 insertions(+), 12 deletions(-) diff --git a/shell/commands.h b/shell/commands.h index 9bd45c8..f5cbbd2 100644 --- a/shell/commands.h +++ b/shell/commands.h @@ -28,6 +28,9 @@ void _vl(Session &s, std::vector 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=""); diff --git a/shell/main.cpp b/shell/main.cpp index c2a6362..837991e 100644 --- a/shell/main.cpp +++ b/shell/main.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -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 fxosrc_files; - for(auto const &path: s.path) { - if(fs::exists(path / "fxosrc")) - fxosrc_files.push_back(path / "fxosrc"); + if (!norc) { + std::vector 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); diff --git a/shell/v.cpp b/shell/v.cpp index 537c485..78f087b 100644 --- a/shell/v.cpp +++ b/shell/v.cpp @@ -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); From 446590448bde4d6e96eab30576d5b7ff6c2f305d Mon Sep 17 00:00:00 2001 From: Dr-Carlos Date: Thu, 31 Mar 2022 20:04:04 +1030 Subject: [PATCH 2/3] Change debugger to disassembler in --help --- shell/main.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/shell/main.cpp b/shell/main.cpp index 837991e..096bbfa 100644 --- a/shell/main.cpp +++ b/shell/main.cpp @@ -246,7 +246,7 @@ static void parse_options(int argc, char **argv) { 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("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\n"); printf(" --norc\t\tdo not execute any fxosrc files\n"); @@ -254,9 +254,6 @@ static void parse_options(int argc, char **argv) { exit(0); } - // if (norc) - // option_index++; - if (optind < argc) { if ((argc - optind) > 1) { printf("%s only supports 1 positional argument, FILE.\n", argv[0]); From ef40cb3469c6376645e7b4c2950c90286c6766d1 Mon Sep 17 00:00:00 2001 From: Dr-Carlos Date: Fri, 1 Apr 2022 06:24:48 +1030 Subject: [PATCH 3/3] Add comments and allow multiple -e options --- shell/main.cpp | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/shell/main.cpp b/shell/main.cpp index 096bbfa..711ef8e 100644 --- a/shell/main.cpp +++ b/shell/main.cpp @@ -200,14 +200,16 @@ static std::string read_interactive(Session const &s, bool &leave) } int norc; -char* execute_arg = NULL; +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} }; @@ -215,25 +217,23 @@ static void parse_options(int argc, char **argv) { 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). + /* 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; + execute_arg += ";"; + execute_arg += optarg; break; case '?': printf("Try %s --help for more information.\n", argv[0]); @@ -244,16 +244,20 @@ static void parse_options(int argc, char **argv) { } } + /* 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("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\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]); @@ -267,6 +271,7 @@ static void parse_options(int argc, char **argv) { int main(int argc, char **argv) { + /* Parse command-line options first */ parse_options(argc, argv); Session &s = global_session; @@ -315,7 +320,7 @@ 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 */ + /* Load fxosrc files from all library folders if wanted */ if (!norc) { std::vector fxosrc_files; for(auto const &path: s.path) { @@ -327,20 +332,26 @@ int main(int argc, char **argv) } + /* 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()) { + /* 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"); + /* Add FILE as a ROM and ROM_P2 */ _vm(s, extra_rom, {MemoryRegion::ROM, MemoryRegion::ROM_P2}); } - if (execute_arg) { + /* If we need to execute a command */ + if (!execute_arg.empty()) { if (has_idled) exit(0); else @@ -354,6 +365,7 @@ int main(int argc, char **argv) lex_repl(cmdline); } + /* We have already handled FILE and --execute, don't do it again */ has_idled = true; }