diff --git a/core/tw-opts.c b/core/tw-opts.c index cf440d53a..1b2b03641 100644 --- a/core/tw-opts.c +++ b/core/tw-opts.c @@ -10,6 +10,12 @@ static int is_empty(const tw_optdef *def); static const tw_optdef *opt_groups[10]; static unsigned int opt_index = 0; +// internally set options registered with tw_opt_set +#define I_ARGV_MAX 16 +static int i_argc = 0; +static char * i_argv[I_ARGV_MAX]; +static int tw_parse_called = 0; + void tw_opt_add(const tw_optdef *options) { @@ -121,7 +127,7 @@ show_help(void) cnt++; } } - + // CMake used to pass options by command line flags fprintf(stderr, "ROSS CMake Configuration Options:\n"); fprintf(stderr, " (See build-dir/core/config.h)\n"); @@ -130,7 +136,7 @@ show_help(void) void tw_opt_settings(FILE *outfile) { const tw_optdef **group = all_groups; unsigned cnt = 0; - + for (; *group; group++){ const tw_optdef *def = *group; for (; def->type; def++){ @@ -213,7 +219,7 @@ tw_opt_print(void) const tw_optdef *def = *group; for (; def->type; def++) { - if (def->type == TWOPTTYPE_GROUP || + if (def->type == TWOPTTYPE_GROUP || (def->name && 0 == strcmp(def->name, "help"))) continue; @@ -398,6 +404,8 @@ tw_opt_parse(int *argc_p, char ***argv_p) char **argv = *argv_p; unsigned i; + tw_parse_called = 1; + program = strrchr(argv[0], '/'); if (program) program++; @@ -415,6 +423,13 @@ tw_opt_parse(int *argc_p, char ***argv_p) all_groups[i++] = basic; all_groups[i] = NULL; + while (i_argc > 0) { + i_argc--; + const char *s = i_argv[i_argc]; + match_opt(s); + free(s); + } + while (argc > 1) { const char *s = argv[1]; @@ -436,3 +451,29 @@ tw_opt_parse(int *argc_p, char ***argv_p) *argc_p = argc; *argv_p = argv; } + +/** + * construct internal arguments to look like command line arguments + * these cannot be processed until ross is fully set up and tw_opt_parse is called (from tw_init) + */ +void tw_opt_set(const char *opt, const char *value) { + if (i_argc >= I_ARGV_MAX) { + tw_error(TW_LOC, "Too many internal options, increase I_ARGV_MAX."); + } + + unsigned long len = strlen(opt) + strlen(value) + 4; + char * s = (char *)malloc(len*sizeof(char)); + strcpy(s, "--"); + strcat(s, opt); + strcat(s, "="); + strcat(s, value); + + if (!tw_parse_called) { + // save until ROSS has been fully initialized + i_argv[i_argc] = s; + i_argc++; + } else { + match_opt(s); + free(s); + } +} diff --git a/core/tw-opts.h b/core/tw-opts.h index 5337d9c06..c81d0bd34 100644 --- a/core/tw-opts.h +++ b/core/tw-opts.h @@ -36,6 +36,8 @@ struct tw_optdef extern void tw_opt_parse(int *argc, char ***argv); /** Add an opt group */ extern void tw_opt_add(const tw_optdef *options); +/** Set an option at runtime. Command line take precedence */ +extern void tw_opt_set(const char *option, const char *value); /** Pretty-print the option descriptions (for --help) */ extern void tw_opt_print(void); /** Pretty-print the option descriptions and current values */