Skip to content

Commit

Permalink
Add a --libpath option; currently unused.
Browse files Browse the repository at this point in the history
Will control where loadbytecode will look. Needed for NQP bootstrap.
  • Loading branch information
jnthn committed Oct 5, 2013
1 parent 83a2a67 commit 355b4e0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/core/instance.h
Expand Up @@ -125,6 +125,8 @@ struct MVMInstance {
const char *prog_name;
/* cached parsed command line args */
MVMObject *clargs;
/* Any --libpath=... option, to prefix in loadbytecode lookups. */
const char *lib_path;

/* Hash of HLLConfig objects. */
MVMHLLConfig *hll_configs;
Expand Down
31 changes: 27 additions & 4 deletions src/main.c
Expand Up @@ -21,6 +21,8 @@ enum {
FLAG_DUMP,
FLAG_HELP,
FLAG_TRACING,

OPT_LIBPATH
};

static const char *const FLAGS[] = {
Expand All @@ -31,34 +33,50 @@ static const char *const FLAGS[] = {
};

static const char USAGE[] = "\
USAGE: moar [--dump] [--crash] " TRACING_OPT "input.moarvm [program args]\n\
USAGE: moar [--dump] [--crash] [--libpath=...] " TRACING_OPT "input.moarvm [program args]\n\
moar [--help]\n\
\n\
--help display this message\n\
--dump dump the bytecode to stdout instead of executing\n\
--crash abort instead of exiting on unhandled exception"
--crash abort instead of exiting on unhandled exception\n\
--libpath specify path loadbytecode should search in"
TRACING_USAGE;

static int cmp_flag(const void *key, const void *value)
{
return strcmp(key, *(char **)value);
}

static int starts_with(const char *str, const char *want) {
size_t str_len = strlen(str);
size_t want_len = strlen(want);
return str_len < want_len
? 0
: strncmp(str, want, want_len) == 0;
}

static int parse_flag(const char *arg)
{
const char *const *found;

if(!arg || arg[0] != '-')
if (!arg || arg[0] != '-')
return NOT_A_FLAG;

found = bsearch(arg, FLAGS, sizeof FLAGS / sizeof *FLAGS, sizeof *FLAGS, cmp_flag);
return found ? (int)(found - FLAGS) : UNKNOWN_FLAG;

if (found)
return (int)(found - FLAGS);
else if (starts_with(arg, "--libpath="))
return OPT_LIBPATH;
else
return UNKNOWN_FLAG;
}

int main(int argc, char *argv[])
{
MVMInstance *instance;
const char *input_file;
const char *lib_path = NULL;

int dump = 0;
int argi = 1;
Expand All @@ -84,6 +102,10 @@ int main(int argc, char *argv[])
continue;
#endif

case OPT_LIBPATH:
lib_path = argv[argi] + strlen("--libpath=");
continue;

default:
fprintf(stderr, "ERROR: Unknown flag %s.\n\n%s\n", argv[argi], USAGE);
return EXIT_FAILURE;
Expand All @@ -102,6 +124,7 @@ int main(int argc, char *argv[])
instance->num_clargs = argc - argi;
instance->raw_clargs = argv + argi;
instance->prog_name = input_file;
instance->lib_path = lib_path;

if (dump) MVM_vm_dump_file(instance, input_file);
else MVM_vm_run_file(instance, input_file);
Expand Down

0 comments on commit 355b4e0

Please sign in to comment.