75,12 → 75,13 |
#define CONFIG_PASSWORD "password" |
|
struct config { |
char config_file[FILENAME_MAX]; |
ushort port; |
char password[MAX_CONFIG_LINE]; |
}; |
|
static int parse_cmd_line(int argc, const char* argv[], long *port, char *config_name); |
static int parse_config_file(const char *config_name, long *port, char *password); |
static int parse_cmd_line(struct config *cfg, int argc, const char* argv[]); |
static int parse_config_file(struct config *cfg, const char *config_name); |
static int get_revision(void); |
|
static uint min(uint a, uint b) |
200,52 → 201,57 |
/* return: 0 - no error, continue; 1 - no error, but exit; 2 - user error, exit; 3 - unexpected error, exit */ |
static int read_config(struct config *cfg, int argc, const char* argv[]) |
{ |
int res_cmd_line; |
int res_config_file; |
long cmd_port; |
long file_port; |
char cmd_config[MAX_CONFIG_LINE]; |
int res_cmd_line; |
int res_config_file; |
struct config cmd_cfg; |
struct config file_cfg; |
|
res_cmd_line = parse_cmd_line(argc, argv, &cmd_port, cmd_config); |
res_cmd_line = parse_cmd_line(&cmd_cfg, argc, argv); |
if(res_cmd_line != RESULT_OK) return res_cmd_line; |
|
res_config_file = parse_config_file(cmd_config[0] != '\0' ? cmd_config : DEFAULT_CFG_FILE, |
&file_port, cfg->password); |
res_config_file = parse_config_file(&file_cfg, |
cmd_cfg.config_file[0] != '\0' ? cmd_cfg.config_file : DEFAULT_CFG_FILE); |
if(res_config_file != RESULT_OK) return res_config_file; |
|
if(cfg->password[0] == '\0') { |
if(file_cfg.password[0] == '\0') { |
fprintf(stderr, "Password is not set\n"); |
return RESULT_ERROR; |
} |
|
if(cmd_port > 0) |
cfg->port = (ushort)cmd_port; |
else if(file_port > 0) |
cfg->port = (ushort)file_port; |
/* save parsed values to general config */ |
strncpy(cfg->config_file, cmd_cfg.config_file, sizeof(cfg->config_file)); |
|
if(cmd_cfg.port > 0) |
cfg->port = cmd_cfg.port; |
else if(file_cfg.port > 0) |
cfg->port = file_cfg.port; |
else |
cfg->port = DEFAULT_PORT; |
|
strncpy(cfg->password, file_cfg.password, sizeof(cfg->password)); |
|
return RESULT_OK; |
} |
|
static int parse_cmd_line(int argc, const char* argv[], long *port, char *config_name) |
static int parse_cmd_line(struct config *cfg, int argc, const char* argv[]) |
{ |
char *end; |
int i; |
long port; |
|
*port = 0; |
config_name[0] = '\0'; |
memset(cfg, 0, sizeof(struct config)); |
|
for(i = 1; i < argc; i++) { |
if(0 == strcmp(argv[i], PARAM_PORT_1) || 0 == strcmp(argv[i], PARAM_PORT_2)) { |
if(*port > 0) |
if(cfg->port > 0) |
return print_cmd_error(argc, argv, "Port is already set", NULL); |
|
if(++i < argc) { |
*port = strtol(argv[i], &end, 10); |
if(*end != '\0' || *port < PORT_MIN || *port > PORT_MAX) |
port = strtol(argv[i], &end, 10); |
if(*end != '\0' || port < PORT_MIN || port > PORT_MAX) |
return print_cmd_error(argc, argv, |
"Port number must be integer between 1 and 65535", NULL); |
cfg->port = (ushort)port; |
} |
else { |
return print_cmd_error(argc, argv, "Port number expected", NULL); |
252,12 → 258,12 |
} |
} |
else if(0 == strcmp(argv[i], PARAM_CONFIG_1) || 0 == strcmp(argv[i], PARAM_CONFIG_2)) { |
if(config_name[0] != '\0') |
if(cfg->config_file[0] != '\0') |
return print_cmd_error(argc, argv, "Config file is already set", NULL); |
|
if(++i < argc) { |
strncpy(config_name, argv[i], MAX_CONFIG_LINE); |
if(config_name[MAX_CONFIG_LINE - 1] != '\0') |
strncpy(cfg->config_file, argv[i], MAX_CONFIG_LINE); |
if(cfg->config_file[MAX_CONFIG_LINE - 1] != '\0') |
return print_cmd_error(argc, argv, |
"Config file name is too long", NULL); |
} |
282,7 → 288,7 |
} |
|
|
static int parse_config_file(const char *config_name, long *port, char *password) |
static int parse_config_file(struct config *cfg, const char *config_name) |
{ |
FILE *config_file; |
char buf[MAX_CONFIG_LINE]; |
291,7 → 297,11 |
char *cur; |
int count; |
uint len; |
uint password_len; |
long port; |
|
memset(cfg, 0, sizeof(struct config)); |
|
config_file = fopen(config_name, "r"); |
if(!config_file) { |
fprintf(stderr, "Can not open config file %s: %s\n", config_name, strerror(errno)); |
325,13 → 335,14 |
if('=' != subline[0]) |
return print_config_error(config_name, count, "Equal sign expected", NULL); |
subline++; |
*port = strtol(subline, &subline, 10); |
if(*port < PORT_MIN || *port > PORT_MAX) |
port = strtol(subline, &subline, 10); |
if(port < PORT_MIN || port > PORT_MAX) |
return print_config_error(config_name, count, |
"Port number must be integer between 1 and 65535", NULL); |
while(subline[0] != '\0' && isspace(subline[0])) subline++; |
if('\0' != subline[0] && '#' != subline[0]) |
return print_config_error(config_name, count, "End of line expected", NULL); |
cfg->port = (ushort)port; |
} |
else if(strncmp(line, CONFIG_PASSWORD, min(sizeof(CONFIG_PASSWORD) - 1, len)) == 0) { |
subline = line + sizeof(CONFIG_PASSWORD) - 1; |
346,11 → 357,15 |
return print_config_error(config_name, count, "Open quot expected", NULL); |
subline++; |
|
cur = password; |
while(subline[0] != '\0' && subline[0] != '"') { |
cur = cfg->password; |
password_len = 0; |
while(password_len < sizeof(cfg->password) - 1 |
&& subline[0] != '\0' && subline[0] != '"') |
{ |
cur[0] = subline[0]; |
cur++; |
subline++; |
password_len++; |
} |
|
cur[0] = '\0'; |