Add comments and allow multiple -e options

This commit is contained in:
Dr-Carlos 2022-04-01 06:24:48 +10:30
parent 446590448b
commit ef40cb3469
1 changed files with 24 additions and 12 deletions

View File

@ -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<std::string> 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;
}