Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tw_opt_set #164

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions core/tw-opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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");
Expand All @@ -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++){
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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++;
Expand All @@ -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];
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions core/tw-opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down