2001-03-18 23:32:31 +00:00
|
|
|
/*
|
|
|
|
* command line and config file parser
|
|
|
|
*/
|
|
|
|
|
|
|
|
//#define DEBUG
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define ERR_NOT_AN_OPTION -1
|
|
|
|
#define ERR_MISSING_PARAM -2
|
|
|
|
#define ERR_OUT_OF_RANGE -3
|
2001-03-19 01:49:44 +00:00
|
|
|
#define ERR_FUNC_ERR -4
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
#define COMMAND_LINE 0
|
|
|
|
#define CONFIG_FILE 1
|
|
|
|
|
2001-03-20 22:55:50 +00:00
|
|
|
#define MAX_RECURSION_DEPTH 8
|
2001-03-19 21:06:56 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <assert.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cfgparser.h"
|
|
|
|
|
|
|
|
static struct config *config;
|
|
|
|
static int nr_options; /* number of options in 'conf' */
|
|
|
|
static int parser_mode; /* COMMAND_LINE or CONFIG_FILE */
|
2001-03-19 21:06:56 +00:00
|
|
|
static int recursion_depth = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
static int init_conf(struct config *conf, int mode)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(conf != NULL);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* calculate the number of options in 'conf' */
|
|
|
|
for (nr_options = 0; conf[nr_options].name != NULL; nr_options++)
|
|
|
|
/* NOTHING */;
|
|
|
|
|
|
|
|
config = conf;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mode != COMMAND_LINE && mode != CONFIG_FILE) {
|
2001-03-19 01:49:44 +00:00
|
|
|
printf("init_conf: wrong mode!\n");
|
2001-03-18 23:32:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
parser_mode = mode;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int read_option(char *opt, char *param)
|
|
|
|
{
|
|
|
|
int i;
|
2001-03-22 18:52:45 +00:00
|
|
|
long tmp_int;
|
|
|
|
double tmp_float;
|
2001-03-19 14:35:38 +00:00
|
|
|
int ret = -1;
|
2001-03-22 18:52:45 +00:00
|
|
|
char *endptr;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
for (i = 0; i < nr_options; i++) {
|
|
|
|
if (!strcasecmp(opt, config[i].name))
|
|
|
|
break;
|
|
|
|
}
|
2001-03-19 14:35:38 +00:00
|
|
|
if (i == nr_options) {
|
|
|
|
printf("invalid option:\n");
|
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (config[i].flags & CONF_NOCFG && parser_mode == CONFIG_FILE) {
|
|
|
|
printf("this option can only be used on command line:\n");
|
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (config[i].flags & CONF_NOCMD && parser_mode == COMMAND_LINE) {
|
|
|
|
printf("this option can only be used in config file:\n");
|
|
|
|
ret = ERR_NOT_AN_OPTION;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-19 01:49:44 +00:00
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
switch (config[i].type) {
|
|
|
|
case CONF_TYPE_FLAG:
|
|
|
|
/* flags need a parameter in config file */
|
|
|
|
if (parser_mode == CONFIG_FILE) {
|
|
|
|
if (!strcasecmp(param, "yes") || /* any other language? */
|
|
|
|
!strcasecmp(param, "ja") ||
|
2001-03-19 15:09:47 +00:00
|
|
|
!strcasecmp(param, "si") ||
|
2001-03-18 23:32:31 +00:00
|
|
|
!strcasecmp(param, "igen") ||
|
|
|
|
!strcasecmp(param, "y") ||
|
|
|
|
!strcasecmp(param, "i") ||
|
|
|
|
!strcmp(param, "1"))
|
|
|
|
*((int *) config[i].p) = config[i].max;
|
2001-03-19 02:29:37 +00:00
|
|
|
else if (!strcasecmp(param, "no") ||
|
2001-03-18 23:32:31 +00:00
|
|
|
!strcasecmp(param, "nein") ||
|
|
|
|
!strcasecmp(param, "nicht") ||
|
|
|
|
!strcasecmp(param, "nem") ||
|
|
|
|
!strcasecmp(param, "n") ||
|
|
|
|
!strcmp(param, "0"))
|
|
|
|
*((int *) config[i].p) = config[i].min;
|
2001-03-19 14:35:38 +00:00
|
|
|
else {
|
|
|
|
printf("invalid parameter for flag:\n");
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
} else { /* parser_mode == COMMAND_LINE */
|
|
|
|
*((int *) config[i].p) = config[i].max;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 0;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CONF_TYPE_INT:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-22 18:52:45 +00:00
|
|
|
|
|
|
|
tmp_int = strtol(param, &endptr, 0);
|
|
|
|
if (*endptr) {
|
2001-03-19 14:35:38 +00:00
|
|
|
printf("parameter must be an integer:\n");
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MIN)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (tmp_int < config[i].min) {
|
|
|
|
printf("parameter must be >= %d:\n", (int) config[i].min);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MAX)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (tmp_int > config[i].max) {
|
|
|
|
printf("parameter must be <= %d:\n", (int) config[i].max);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
*((int *) config[i].p) = tmp_int;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
case CONF_TYPE_FLOAT:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-22 18:52:45 +00:00
|
|
|
|
|
|
|
tmp_float = strtod(param, &endptr);
|
|
|
|
if (*endptr) {
|
2001-03-19 14:35:38 +00:00
|
|
|
printf("parameter must be a floating point number:\n");
|
|
|
|
ret = ERR_MISSING_PARAM;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MIN)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (tmp_float < config[i].min) {
|
|
|
|
printf("parameter must be >= %f:\n", config[i].min);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MAX)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (tmp_float > config[i].max) {
|
|
|
|
printf("parameter must be <= %f:\n", config[i].max);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
*((float *) config[i].p) = tmp_float;
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
|
|
|
case CONF_TYPE_STRING:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MIN)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (strlen(param) < config[i].min) {
|
|
|
|
printf("parameter must be >= %d chars:\n",
|
|
|
|
(int) config[i].min);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-19 03:45:49 +00:00
|
|
|
if (config[i].flags & CONF_MAX)
|
2001-03-19 14:35:38 +00:00
|
|
|
if (strlen(param) > config[i].max) {
|
|
|
|
printf("parameter must be <= %d chars:\n",
|
|
|
|
(int) config[i].max);
|
|
|
|
ret = ERR_OUT_OF_RANGE;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
|
|
|
*((char **) config[i].p) = strdup(param);
|
2001-03-19 14:35:38 +00:00
|
|
|
ret = 1;
|
2001-03-18 23:32:31 +00:00
|
|
|
break;
|
2001-03-19 02:29:37 +00:00
|
|
|
case CONF_TYPE_FUNC_PARAM:
|
|
|
|
if (param == NULL)
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_missing_param;
|
|
|
|
if ((((cfg_func_param_t) config[i].p)(config + i, param)) < 0) {
|
|
|
|
ret = ERR_FUNC_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 1;
|
2001-03-19 02:29:37 +00:00
|
|
|
break;
|
2001-03-19 01:49:44 +00:00
|
|
|
case CONF_TYPE_FUNC:
|
2001-03-19 14:35:38 +00:00
|
|
|
if ((((cfg_func_t) config[i].p)(config + i)) < 0) {
|
|
|
|
ret = ERR_FUNC_ERR;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
ret = 0;
|
2001-03-19 01:49:44 +00:00
|
|
|
break;
|
2001-03-19 02:29:37 +00:00
|
|
|
case CONF_TYPE_PRINT:
|
|
|
|
printf("%s", (char *) config[i].p);
|
|
|
|
exit(1);
|
2001-03-18 23:32:31 +00:00
|
|
|
default:
|
|
|
|
printf("picsaba\n");
|
|
|
|
break;
|
|
|
|
}
|
2001-03-19 14:35:38 +00:00
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
err_missing_param:
|
|
|
|
printf("missing parameter:\n");
|
|
|
|
ret = ERR_MISSING_PARAM;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int parse_config_file(struct config *conf, char *conffile)
|
|
|
|
{
|
|
|
|
#define PRINT_LINENUM printf("%s(%d): ", conffile, line_num)
|
|
|
|
#define MAX_LINE_LEN 1000
|
|
|
|
#define MAX_OPT_LEN 100
|
|
|
|
#define MAX_PARAM_LEN 100
|
|
|
|
FILE *fp;
|
|
|
|
char *line;
|
2001-03-21 02:56:06 +00:00
|
|
|
char opt[MAX_OPT_LEN + 1];
|
|
|
|
char param[MAX_PARAM_LEN + 1];
|
2001-03-19 21:58:20 +00:00
|
|
|
char c; /* for the "" and '' check */
|
2001-03-18 23:32:31 +00:00
|
|
|
int tmp;
|
|
|
|
int line_num = 0;
|
|
|
|
int line_pos; /* line pos */
|
|
|
|
int opt_pos; /* opt pos */
|
|
|
|
int param_pos; /* param pos */
|
|
|
|
int ret = 1;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(conffile != NULL);
|
|
|
|
#endif
|
2001-03-19 21:06:56 +00:00
|
|
|
if (++recursion_depth > MAX_RECURSION_DEPTH) {
|
|
|
|
printf("too deep 'include'. check your configfiles\n");
|
2001-03-21 02:56:06 +00:00
|
|
|
--recursion_depth;
|
2001-03-19 21:06:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2001-03-18 23:32:31 +00:00
|
|
|
printf("Reading config file: %s\n", conffile);
|
|
|
|
|
2001-03-19 21:06:56 +00:00
|
|
|
if (init_conf(conf, CONFIG_FILE) == -1) {
|
|
|
|
ret = -1;
|
|
|
|
goto out;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
|
2001-03-21 02:56:06 +00:00
|
|
|
if ((line = (char *) malloc(MAX_LINE_LEN + 1)) == NULL) {
|
2001-03-18 23:32:31 +00:00
|
|
|
perror("parse_config_file: can't get memory for 'line'");
|
2001-03-19 21:06:56 +00:00
|
|
|
ret = -1;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp = fopen(conffile, "r")) == NULL) {
|
|
|
|
perror("parse_config_file: can't open filename");
|
|
|
|
free(line);
|
2001-03-19 21:06:56 +00:00
|
|
|
ret = 0;
|
|
|
|
goto out;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets(line, MAX_LINE_LEN, fp)) {
|
|
|
|
line_num++;
|
|
|
|
line_pos = 0;
|
|
|
|
|
|
|
|
/* skip whitespaces */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* EOL / comment */
|
|
|
|
if (line[line_pos] == '\0' || line[line_pos] == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* read option. accept char if isalnum(char) */
|
|
|
|
for (opt_pos = 0; isalnum(line[line_pos]); /* NOTHING */) {
|
|
|
|
opt[opt_pos++] = line[line_pos++];
|
|
|
|
if (opt_pos >= MAX_OPT_LEN) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("too long option\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (opt_pos == 0) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("parse error\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
opt[opt_pos] = '\0';
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option: %s\n", opt);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* skip whitespaces */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* check '=' */
|
|
|
|
if (line[line_pos++] != '=') {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option without parameter\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* whitespaces... */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* read the parameter */
|
2001-03-19 21:58:20 +00:00
|
|
|
if (line[line_pos] == '"' || line[line_pos] == '\'') {
|
|
|
|
c = line[line_pos];
|
|
|
|
++line_pos;
|
|
|
|
for (param_pos = 0; line[line_pos] != c; /* NOTHING */) {
|
|
|
|
param[param_pos++] = line[line_pos++];
|
|
|
|
if (param_pos >= MAX_PARAM_LEN) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("too long parameter\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
line_pos++; /* skip the closing " or ' */
|
|
|
|
} else {
|
|
|
|
for (param_pos = 0; isprint(line[line_pos]) && !isspace(line[line_pos])
|
|
|
|
&& line[line_pos] != '#'; /* NOTHING */) {
|
|
|
|
param[param_pos++] = line[line_pos++];
|
|
|
|
if (param_pos >= MAX_PARAM_LEN) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("too long parameter\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
param[param_pos] = '\0';
|
|
|
|
|
|
|
|
/* did we read a parameter? */
|
|
|
|
if (param_pos == 0) {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("option without parameter\n");
|
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("parameter: %s\n", param);
|
|
|
|
#endif
|
|
|
|
/* now, check if we have some more chars on the line */
|
|
|
|
/* whitespace... */
|
|
|
|
while (isspace(line[line_pos]))
|
|
|
|
++line_pos;
|
|
|
|
|
|
|
|
/* EOL / comment */
|
|
|
|
if (line[line_pos] != '\0' && line[line_pos] != '#') {
|
|
|
|
PRINT_LINENUM;
|
|
|
|
printf("extra characters on line: %s\n", line+line_pos);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = read_option(opt, param);
|
|
|
|
switch (tmp) {
|
|
|
|
case ERR_NOT_AN_OPTION:
|
|
|
|
case ERR_MISSING_PARAM:
|
|
|
|
case ERR_OUT_OF_RANGE:
|
2001-03-19 01:49:44 +00:00
|
|
|
case ERR_FUNC_ERR:
|
|
|
|
PRINT_LINENUM;
|
2001-03-19 14:35:38 +00:00
|
|
|
printf("%s\n", opt);
|
2001-03-19 01:49:44 +00:00
|
|
|
ret = -1;
|
|
|
|
continue;
|
|
|
|
/* break */
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(line);
|
|
|
|
fclose(fp);
|
2001-03-19 21:06:56 +00:00
|
|
|
out:
|
|
|
|
--recursion_depth;
|
2001-03-18 23:32:31 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_command_line(struct config *conf, int argc, char **argv, char **envp, char **filename)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int found_filename = 0;
|
|
|
|
int tmp;
|
|
|
|
char *opt;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
assert(argv != NULL);
|
|
|
|
assert(envp != NULL);
|
|
|
|
assert(argc >= 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (init_conf(conf, COMMAND_LINE) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
opt = argv[i];
|
|
|
|
if (*opt != '-')
|
|
|
|
goto not_an_option;
|
|
|
|
|
|
|
|
/* remove trailing '-' */
|
|
|
|
opt++;
|
|
|
|
|
|
|
|
tmp = read_option(opt, argv[i + 1]);
|
|
|
|
|
|
|
|
switch (tmp) {
|
|
|
|
case ERR_NOT_AN_OPTION:
|
|
|
|
not_an_option:
|
|
|
|
/* opt is not an option -> treat it as a filename */
|
|
|
|
if (found_filename) {
|
|
|
|
/* we already have a filename */
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_out;
|
2001-03-18 23:32:31 +00:00
|
|
|
} else {
|
|
|
|
found_filename = 1;
|
|
|
|
*filename = argv[i];
|
|
|
|
printf("parse_command_line: found filename: %s\n", *filename);
|
|
|
|
continue; /* next option */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ERR_MISSING_PARAM:
|
|
|
|
case ERR_OUT_OF_RANGE:
|
2001-03-19 01:49:44 +00:00
|
|
|
case ERR_FUNC_ERR:
|
2001-03-19 14:35:38 +00:00
|
|
|
goto err_out;
|
2001-03-19 01:49:44 +00:00
|
|
|
/* break; */
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i += tmp; /* we already processed the params (if there was any) */
|
2001-03-19 14:35:38 +00:00
|
|
|
}
|
2001-03-18 23:32:31 +00:00
|
|
|
return found_filename;
|
2001-03-19 14:35:38 +00:00
|
|
|
err_out:
|
|
|
|
printf("parse_command_line: %s\n", argv[i]);
|
|
|
|
return -1;
|
2001-03-18 23:32:31 +00:00
|
|
|
}
|