18 #include <sys/param.h> 28 void display_table(FILE*f,
char**table,
int rows,
int columns,
int padding);
39 size_t len = strlen(s) + 1;
40 char * t = (
char*) malloc(len);
50 value = strtol(s, (
char **)NULL, 10);
60 *p = strtod(s, &endptr);
68 for (i=1; i<argc; i++) {
69 const char * name = argv[i];
70 while(*name ==
'-') name++;
72 if(!strcmp(
"config_dump", name)) {
77 if(!strcmp(
"help",name) || !strcmp(
"h",name) ) {
82 if(!strcmp(
"config", name)) {
84 fprintf(stderr,
"Please specify config file.\n");
96 fprintf(stderr,
"Option '%s' not found (use -help to get list of options).\n", name);
103 fprintf(stderr,
"Argument %s needs value.\n", o->
name);
132 #define MAX_LINE_LENGTH 10000 135 char *line=linesto;
while(*line) {
if(*line==
'\n') *line=0; line++; }
137 while(isspace(*line)) line++;
138 if(*line ==
'#')
continue;
139 if(*line ==
'<') { line++;
140 while(isspace(*line)) line++;
148 const char *
name = line;
150 while(!isspace(*line)) line++;
154 if(*line == 0) value = empty;
else {
158 while(isspace(*line)) line++;
160 if(*line ==
'=') line++;
162 while(isspace(*line)) line++;
167 int len = strlen(value);
168 while(isspace(value[len-1]) && len > 0) {
169 value[len-1] = 0; len--;
181 char concat[PATH_MAX*2+1];
183 if(filename[0] !=
'/') {
186 strcat(concat, filename);
188 strcpy(concat, filename);
191 char resolved_path[PATH_MAX];
193 if(! (resolved = realpath(concat, resolved_path))) {
194 fprintf(stderr,
"Could not resolve '%s' ('%s').\n", concat, resolved);
198 const char * newdir = dirname(resolved);
200 fprintf(stderr,
"Could not get dirname for '%s'.\n", resolved);
206 file = fopen(resolved,
"r");
208 fprintf(stderr,
"Could not open '%s': %s.\n", resolved, strerror(errno));
222 if(!strcmp(name,ops[j].name))
232 fprintf(stderr,
"Option '%s' does not exist.\n", name);
246 int ok =
get_int(value_pointer, value);
248 fprintf(stderr,
"Could not parse int: '%s' = '%s'.\n", o->
name, value);
256 *value_pointer = (
char*)
strdup_(value);
268 fprintf(stderr,
"Could not parse double: '%s' = '%s'.\n", o->
name, value);
277 for(; a->label; a++) {
278 if( !strcasecmp(a->label, value) ) {
279 *value_pointer = a->
value;
283 fprintf(stderr,
"Could not recognize '%s' as one of the alternative for %s: ",
287 fprintf(stderr,
"\"%s\"", a->label);
288 if( (a+1)->label ) fprintf(stderr,
", ");
290 fprintf(stderr,
".\n");
298 fprintf(stderr,
"Could not parse type %d: '%s' = '%s'.\n", (
int) o->
type, o->
name, value);
306 void display_table(FILE*f,
char**table,
int rows,
int columns,
int padding) {
307 int col_size[columns];
310 for(j=0;j<columns;j++) {
312 for(i=0;i<rows;i++) {
313 const char * s = table[j+i*columns];
314 col_size[j] = MAX(col_size[j], (
int) strlen(s));
316 col_size[j] += padding;
319 for(i=0;i<rows;i++) {
320 for(j=0;j<columns;j++) {
321 const char * s = table[j+i*columns];
324 fprintf(f,
"%s%*s", s, (
int)(col_size[j]-strlen(s)),
"");
336 char**table = malloc(
sizeof(
char*)*nrows*3);
340 table[row*3 +0] =
strdup_(
"Option name");
341 table[row*3 +1] =
strdup_(
"Default");
342 table[row*3 +2] =
strdup_(
"Description");
344 table[row*3 +0] =
strdup_(
"-----------");
345 table[row*3 +1] =
strdup_(
"-------");
346 table[row*3 +2] =
strdup_(
"-----------");
368 strcat(extended, options[i].desc);
369 strcat(extended,
" Possible options are: ");
372 for(; a->
label; a++) {
373 strcat(extended,
"\"");
374 strcat(extended, a->
label);
375 strcat(extended,
"\"");
377 strcat(extended,
": ");
378 strcat(extended, a->
desc);
382 strcat(extended,
", ");
384 strcat(extended,
".");
386 table[row*3 +2] =
strdup_(extended);
394 int a;
for(a=0;a<nrows*3;a++) free((
void*)table[a]);
401 "Generic options: \n" 402 " -help Displays this help.\n" 403 " -config_dump Dumps the configuration on the standard output. \n" 404 " -config FILE Loads a config file in the format used by config_dump.\n" 438 for(; a->label; a++) {
439 if( a->value == *value_pointer )
void options_banner(const char *message)
int options_set(struct option *o, const char *value)
int get_double(double *p, const char *s)
int options_valid(struct option *o)
const char * options_value_as_string(struct option *o)
int options_parse_stream(struct option *ops, const char *pwd, FILE *file)
char * strdup_(const char *s)
void options_dump(struct option *options, FILE *f, int write_desc)
const char * options_banner_string
int get_int(int *p, const char *s)
int options_parse_file(struct option *ops, const char *pwd, const char *filename)
static char options_value_as_string_buf[1000]
struct option_alternative * alternative
void display_table(FILE *f, char **table, int rows, int columns, int padding)
void options_set_passed(struct option *o)
int options_requires_argument(struct option *o)
void options_print_help(struct option *options, FILE *f)
int options_try_pair(struct option *ops, const char *name, const char *value)
struct option * options_find(struct option *ops, const char *name)
int options_parse_args(struct option *ops, int argc, const char *argv[])