6#define ARRAY_SIZE(a) ( sizeof((a)) / sizeof((a)[0]) )
14static void parse_args_file(
const char* file_name,
int* argc_p,
char ***argv_p);
38 fprintf(stderr,
"usage: %s [options] [-- [args]]\n",
program);
41 for (; *group; group++)
44 for (; def->
type; def++)
52 fprintf(stderr,
"%s:\n", def->
help);
57 pos += fprintf(stderr,
" --%s", def->
name);
63 pos += fprintf(stderr,
"=n");
67 pos += fprintf(stderr,
"=dbl");
71 pos += fprintf(stderr,
"=ts");
75 pos += fprintf(stderr,
"=str");
87 fprintf(stderr,
"%*s", col - pos,
"");
90 fprintf(stderr,
"%*s", col,
"");
93 fputs(def->
help, stderr);
101 fprintf(stderr,
" (default %lu)", *((
unsigned long*)def->
value));
105 fprintf(stderr,
" (default %llu)", *((
unsigned long long*)def->
value));
109 fprintf(stderr,
" (default %u)", *((
unsigned int*)def->
value));
113 fprintf(stderr,
" (default %.2f)", *((
double*)def->
value));
117 fprintf(stderr,
" (default %.2f)", *((
tw_stime*)def->
value));
121 fprintf(stderr,
" (default %s)", (
char *) def->
value);
125 if((*(
unsigned int*)def->
value) == 0) {
126 fprintf(stderr,
" (default off)");
128 fprintf(stderr,
" (default on)");
143 fprintf(stderr,
"ROSS CMake Configuration Options:\n");
144 fprintf(stderr,
" (See build-dir/core/config.h)\n");
151 for (; *group; group++){
153 for (; def->
type; def++){
158 fputc(
'\n', outfile);
159 fprintf(outfile,
"%s:\n", def->
help);
164 pos += fprintf(outfile,
" --%s", def->
name);
170 fprintf(outfile,
"%*s", col - pos,
"");
172 fputc(
'\n', outfile);
173 fprintf(outfile,
"%*s", col,
"");
181 fprintf(outfile,
"%lu", *((
unsigned long*)def->
value));
185 fprintf(outfile,
"%llu", *((
unsigned long long*)def->
value));
189 fprintf(outfile,
"%u", *((
unsigned int*)def->
value));
193 fprintf(outfile,
"%.2f", *((
double*)def->
value));
201 fprintf(outfile,
"%s", (
char *) def->
value);
205 if((*(
unsigned int*)def->
value) == 0) {
206 fprintf(outfile,
"off");
208 fprintf(outfile,
"on");
217 fputc(
'\n', outfile);
232 for (; *group; group++)
235 for (; def->
type; def++)
238 (def->
name && 0 == strcmp(def->
name,
"help")))
246 fprintf(f,
"%lu,", *((
unsigned long*)def->
value));
250 fprintf(f,
"%llu,", *((
unsigned long long*)def->
value));
254 fprintf(f,
"%u,", *((
unsigned int*)def->
value));
258 fprintf(f,
"%.2f,", *((
double*)def->
value));
266 fprintf(f,
"%s,", (
char *)def->
value);
270 fprintf(f,
"%s,", (
char *)def->
name);
277 fprintf(f,
"undefined,");
289 "%s: option --%s requires a valid argument\n",
298 while(isspace(*s)) s++;
299 if (*s ==
'\0' || *s ==
'#') {
return NULL; }
305 char * to_ret = *line;
308 while(!(isspace(**line) || **line ==
'\0' || **line ==
'#')) {
340 v = strtoul(value, &end, 10);
347 *((
unsigned long*)def->
value) = v;
350 *((
unsigned long long*)def->
value) = v;
354 *((
unsigned int*)def->
value) = (
unsigned int)v;
369 v = strtod(value, &end);
372 *((
double*)def->
value) = v;
381 tw_warning(
TW_LOC,
"Option type stime (TWOPT_STIME) is deprecated. Please use double (TWOPT_DOUBLE).");
385 v = strtod(value, &end);
398 strcpy((
char *) def->
value, value);
403 *((
unsigned int*)def->
value) = 1;
416 tw_error(
TW_LOC,
"--args-file cannot be invoked inside an args-file.");
421 char** argv_parsed = (
char**)malloc(
sizeof(
char*));
424 size_t prog_name_len = strlen(
program);
425 argv_parsed[0] = malloc(prog_name_len + 3);
426 strcpy(argv_parsed[0],
"./");
427 strcat(argv_parsed[0],
program);
428 argv_parsed[0][prog_name_len+2] =
'\0';
434 printf(
"Arguments passed through --args-file:\n");
435 for (
int i = 1; i < argc_parsed; i++) {
436 printf(
"%s\n", argv_parsed[i]);
443 for (
size_t i = 0; i < argc_parsed; i++) {
444 free(argv_parsed[0]);
460 const char *eq = strchr(arg + 2,
'=');
463 for (; *group; group++)
466 for (; def->
type; def++)
470 if (!eq && !strcmp(def->
name, arg + 2))
475 else if (eq && !strncmp(def->
name, arg + 2, eq - arg - 2))
485 "%s: option '%s' not recognized; see --help for details\n",
498 for (; def->
type; def++) {
511 char* argument = NULL;
515 char** argv = *argv_p;
517 file = fopen(file_name,
"r");
523 while ((read = getline(&line, &len, file)) != -1) {
524 char * start_line = line;
529 argv = (
char**)realloc(argv,
sizeof(
char*)*argc);
532 size_t argument_len = strlen(argument);
533 argv[argc-1] = (
char*)malloc(
sizeof(
char)*(argument_len+1));
534 strcpy(argv[argc-1], argument);
539 if(strcmp(argument,
"--")==0) {
540 tw_error(
TW_LOC,
"Argument `--' is invalid inside of args-file.");
545 if (line) { free(line); }
555 char **argv = *argv_p;
558 program = strrchr(argv[0],
'/');
579 const char *s = argv[1];
580 if (strncmp(s,
"--", 2))
582 printf(
"Warning: found ill-formated argument: %s, stopping arg parsing here!! \n", s );
589 memmove(argv + 1, argv + 2, (argc - 1) *
sizeof(*argv));
591 if (!strcmp(s,
"--"))
void tw_net_stop(void)
Stops the network library after simulation end.
void tw_error(const char *file, int line, const char *fmt,...)
void tw_warning(const char *file, int line, const char *fmt,...)
static int tw_ismaster(void)
char * next_argument(char **line)
void tw_opt_parse(int *argc_p, char ***argv_p)
static void need_argument(const tw_optdef *def)
static void apply_opt(const tw_optdef *def, const char *value)
static void match_opt(const char *arg)
void tw_opt_settings(FILE *outfile)
static int is_empty(const tw_optdef *def)
static const tw_optdef * opt_groups[10]
static const tw_optdef * all_groups[10]
static const tw_optdef basic[]
static void parse_args_file(const char *file_name, int *argc_p, char ***argv_p)
static unsigned int opt_index
static uint args_file_depth
void tw_opt_add(const tw_optdef *options)
static const char * program
static void show_help(void)