| version 1.39, 2002/05/29 15:12:11 |
version 1.40, 2002/05/30 22:29:45 |
|
|
| |
|
| /* $Id$ */ |
/* $Id$ */ |
| |
|
| |
/* Includes {{{ */ |
| #include "config.h" |
#include "config.h" |
| |
|
| #include <stdio.h> |
#include <stdio.h> |
|
|
| |
|
| #include "cfg+.h" |
#include "cfg+.h" |
| #include "shared.h" |
#include "shared.h" |
| |
/* }}} */ |
| |
|
| static char *default_line_stop_str[1] = { /*"--",*/ NULL }; |
extern char *cfg_default_properties[CFG_N_PROPS][4]; |
| static char *default_file_stop_prefix[1] = { /*"[",*/ NULL }; |
|
| static char *default_file_comment_prefix[4] = { "#", ";", "//", NULL }; |
|
| static char *default_short_option_prefix[2] = { "-", NULL }; |
|
| static char *default_long_option_prefix[2] = { "--", NULL }; |
|
| static char *default_line_multi_vals_separator[4] = { " ", ",", ";", NULL }; |
|
| static char *default_file_multi_vals_separator[4] = { " ", ",", ";", NULL }; |
|
| static char *default_line_option_arg_separator[2] = { "=", NULL }; |
|
| static char *default_file_option_arg_separator[2] = { "=", NULL }; |
|
| static char *default_multi_line_postfix[3] = { "\\", "multi\\", NULL }; |
|
| static char *default_quote_prefix[3] = { "\"", "'", NULL }; |
|
| static char *default_quote_postfix[3] = { "\"", "'", NULL }; |
|
| |
|
| CFG_CONTEXT |
CFG_CONTEXT |
| cfg_get_context(options) |
cfg_get_context(options) |
| struct cfg_option *options; |
struct cfg_option *options; |
| { |
{ /* {{{ */ |
| |
register int i; |
| CFG_CONTEXT con; |
CFG_CONTEXT con; |
| |
|
| con = (CFG_CONTEXT) malloc(sizeof(*con)); |
con = (CFG_CONTEXT) malloc(sizeof(*con)); |
| if (con == NULL) |
if (con == NULL) |
| return NULL; |
return NULL; |
| |
|
| /* Setting all struct values to 0 or NULL. */ |
/* Setting all struct values to 0 or NULL */ |
| memset(con, '\0', sizeof(*con)); |
memset(con, '\0', sizeof(*con)); |
| con->type = CFG_NO_CONTEXT; |
|
| |
|
| con->line_stop_str = strdyn_create_ar(default_line_stop_str); |
|
| con->file_stop_prefix = strdyn_create_ar(default_file_stop_prefix); |
|
| con->comment_prefix = strdyn_create_ar(default_file_comment_prefix); |
|
| con->short_option_prefix = strdyn_create_ar(default_short_option_prefix); |
|
| con->long_option_prefix = strdyn_create_ar(default_long_option_prefix); |
|
| con->multi_line_postfix = strdyn_create_ar(default_multi_line_postfix); |
|
| con->line_multi_vals_separator = |
|
| strdyn_create_ar(default_line_multi_vals_separator); |
|
| con->file_multi_vals_separator = |
|
| strdyn_create_ar(default_file_multi_vals_separator); |
|
| con->line_option_arg_separator = |
|
| strdyn_create_ar(default_line_option_arg_separator); |
|
| con->file_option_arg_separator = |
|
| strdyn_create_ar(default_file_option_arg_separator); |
|
| con->quote_prefix = strdyn_create_ar(default_quote_prefix); |
|
| con->quote_postfix = strdyn_create_ar(default_quote_postfix); |
|
| |
|
| /* If any memory allocation failure. */ |
|
| if (con->line_stop_str == NULL |
|
| || con->file_stop_prefix == NULL |
|
| || con->comment_prefix == NULL |
|
| || con->short_option_prefix == NULL |
|
| || con->long_option_prefix == NULL |
|
| || con->multi_line_postfix == NULL |
|
| || con->line_multi_vals_separator == NULL |
|
| || con->file_multi_vals_separator == NULL |
|
| || con->line_option_arg_separator == NULL |
|
| || con->file_option_arg_separator == NULL |
|
| || con->quote_prefix == NULL |
|
| || con->quote_postfix == NULL |
|
| ) |
|
| return NULL; |
|
| |
|
| |
/* Initializing context type and options set */ |
| |
con->type = CFG_NO_CONTEXT; |
| con->options = options; |
con->options = options; |
| |
|
| |
/* Initializaing properties to default values */ |
| |
for (i = 0; i < CFG_N_PROPS; i++) { |
| |
con->prop[i] = strdyn_create_ar(cfg_default_properties[i]); |
| |
if (con->prop[i] == NULL) { |
| |
/*TODO: possible freeing on failure */ |
| |
return NULL; |
| |
} |
| |
} |
| |
|
| return con; |
return con; |
| } |
} /* }}} */ |
| |
|
| CFG_CONTEXT |
CFG_CONTEXT |
| cfg_get_cmdline_context(argc, argv, options) |
cfg_get_cmdline_context(argc, argv, options) |
| int argc; |
int argc; |
| char **argv; |
char **argv; |
| struct cfg_option *options; |
struct cfg_option *options; |
| { |
{ /* {{{ */ |
| CFG_CONTEXT con; |
CFG_CONTEXT con; |
| |
|
| con = cfg_get_context(options); |
con = cfg_get_context(options); |
| Line 123 cfg_get_cmdline_context(argc, argv, opti |
|
| Line 92 cfg_get_cmdline_context(argc, argv, opti |
|
| cfg_set_cmdline_context(con, argc, argv); |
cfg_set_cmdline_context(con, argc, argv); |
| |
|
| return con; |
return con; |
| } |
} /* }}} */ |
| |
|
| CFG_CONTEXT |
CFG_CONTEXT |
| cfg_get_cfgfile_context(begin_pos, end_pos, filename, options) |
cfg_get_cfgfile_context(begin_pos, end_pos, filename, options) |
| Line 131 cfg_get_cfgfile_context(begin_pos, end_p |
|
| Line 100 cfg_get_cfgfile_context(begin_pos, end_p |
|
| long end_pos; |
long end_pos; |
| char *filename; |
char *filename; |
| struct cfg_option *options; |
struct cfg_option *options; |
| { |
{ /* {{{ */ |
| CFG_CONTEXT con; |
CFG_CONTEXT con; |
| |
|
| con = cfg_get_context(options); |
con = cfg_get_context(options); |
| Line 141 cfg_get_cfgfile_context(begin_pos, end_p |
|
| Line 110 cfg_get_cfgfile_context(begin_pos, end_p |
|
| cfg_set_cfgfile_context(con, begin_pos, end_pos, filename); |
cfg_set_cfgfile_context(con, begin_pos, end_pos, filename); |
| |
|
| return con; |
return con; |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_set_cmdline_context(con, argc, argv) |
cfg_set_cmdline_context(con, argc, argv) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| int argc; |
int argc; |
| char **argv; |
char **argv; |
| { |
{ /* {{{ */ |
| cfg_reset_context(con); |
cfg_reset_context(con); |
| |
|
| con->type = CFG_CMDLINE; |
con->type = CFG_CMDLINE; |
| |
|
| con->argc = argc; |
con->argc = argc; |
| con->argv = argv; |
con->argv = argv; |
| |
|
| con->cur_idx_tmp = con->flags & CFG_PROCESS_FIRST ? 0 : 1; |
con->cur_idx_tmp = con->flags & CFG_PROCESS_FIRST ? 0 : 1; |
| con->cur_idx = 0; |
con->cur_idx = 0; |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_set_cfgfile_context(con, begin_pos, end_pos, filename) |
cfg_set_cfgfile_context(con, begin_pos, end_pos, filename) |
| Line 166 cfg_set_cfgfile_context(con, begin_pos, |
|
| Line 134 cfg_set_cfgfile_context(con, begin_pos, |
|
| long begin_pos; |
long begin_pos; |
| long end_pos; |
long end_pos; |
| char *filename; |
char *filename; |
| { |
{ /* {{{ */ |
| cfg_reset_context(con); |
cfg_reset_context(con); |
| |
|
| if (filename == NULL) { |
if (filename == NULL) { |
| Line 174 cfg_set_cfgfile_context(con, begin_pos, |
|
| Line 142 cfg_set_cfgfile_context(con, begin_pos, |
|
| fseek(con->fhandle, begin_pos, SEEK_SET); |
fseek(con->fhandle, begin_pos, SEEK_SET); |
| } |
} |
| |
|
| con->type = CFG_CFGFILE; |
con->type = CFG_CFGFILE; |
| |
|
| con->begin_pos = begin_pos; |
con->begin_pos = begin_pos; |
| con->end_pos = end_pos; |
con->end_pos = end_pos; |
| |
|
| /* TODO: filename copying? (if yes than don't forget memory freeing) */ |
/* TODO: filename copying? (if yes than don't forget memory freeing) */ |
| if (filename != NULL) |
if (filename != NULL) |
| con->filename = (char *) filename; |
con->filename = (char *) filename; |
| |
|
| con->fhandle = NULL; |
con->fhandle = NULL; |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_reset_context(con) |
cfg_reset_context(con) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| { |
{ /* {{{ */ |
| con->error_code = CFG_OK; |
con->error_code = CFG_OK; |
| con->cur_idx = 0; |
con->cur_idx = 0; |
| con->cur_idx_tmp = 0; |
con->cur_idx_tmp = 0; |
| Line 206 cfg_reset_context(con) |
|
| Line 173 cfg_reset_context(con) |
|
| fclose(con->fhandle); |
fclose(con->fhandle); |
| con->fhandle = NULL; |
con->fhandle = NULL; |
| } */ |
} */ |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_free_context(con) |
cfg_free_context(con) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| { |
{ /* {{{ */ |
| |
register int i; |
| |
|
| cfg_reset_context(con); |
cfg_reset_context(con); |
| |
|
| cfg_clear_cmdline_stop_string(con); |
for (i = 0; i < CFG_N_PROPS; i++) |
| cfg_clear_cfgfile_stop_prefix(con); |
cfg_clear_property(con, i); |
| cfg_clear_cfgfile_comment_prefix(con); |
|
| cfg_clear_cmdline_short_option_prefix(con); |
|
| cfg_clear_cmdline_long_option_prefix(con); |
|
| cfg_clear_cmdline_multi_vals_separator(con); |
|
| cfg_clear_cfgfile_multi_vals_separator(con); |
|
| cfg_clear_cmdline_option_arg_separator(con); |
|
| cfg_clear_cfgfile_option_arg_separator(con); |
|
| cfg_clear_cfgfile_multi_line_postfix(con); |
|
| cfg_clear_quote_strings(con); |
|
| |
|
| con->argc = 0; |
con->argc = 0; |
| con->argv = NULL; |
con->argv = NULL; |
| Line 236 cfg_free_context(con) |
|
| Line 196 cfg_free_context(con) |
|
| con->type = CFG_NO_CONTEXT; |
con->type = CFG_NO_CONTEXT; |
| |
|
| free((void *) con); |
free((void *) con); |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_print_error(con) |
cfg_print_error(con) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| { |
{ /* {{{ */ |
| cfg_fprint_error(con, stderr); |
cfg_fprint_error(con, stderr); |
| } |
} /* }}} */ |
| |
|
| void |
void |
| cfg_fprint_error(con, fh) |
cfg_fprint_error(con, fh) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| FILE *fh; |
FILE *fh; |
| { |
{ /* {{{ */ |
| char *s; |
char *s; |
| |
|
| s = cfg_get_error_str(con); |
s = cfg_get_error_str(con); |
| Line 261 cfg_fprint_error(con, fh) |
|
| Line 221 cfg_fprint_error(con, fh) |
|
| fputs(s, fh); |
fputs(s, fh); |
| free(s); |
free(s); |
| } |
} |
| } |
} /* }}} */ |
| |
|
| char * |
char * |
| cfg_get_error_str(con) |
cfg_get_error_str(con) |
| const CFG_CONTEXT con; |
const CFG_CONTEXT con; |
| { |
{ /* {{{ */ |
| char *s; |
char *s; |
| char *str_type = con->type == CFG_LINE ? "on command line" : "in config file"; |
char *str_type = con->type == CFG_LINE ? "on command line" : "in config file"; |
| char *str_pos = con->type == CFG_LINE ? /* "near" */ "at position" |
char *str_pos = con->type == CFG_LINE ? /* "near" */ "at position" |
| Line 402 cfg_get_error_str(con) |
|
| Line 362 cfg_get_error_str(con) |
|
| } |
} |
| |
|
| return s; |
return s; |
| } |
} /* }}} */ |
| |
|
| char * |
char * |
| cfg_get_static_error_str(errorcode) |
cfg_get_static_error_str(errorcode) |
| const int errorcode; |
const int errorcode; |
| { |
{ /* {{{ */ |
| switch (errorcode) { |
switch (errorcode) { |
| |
|
| /* case CFG_ERR_OK: */ |
/* case CFG_ERR_OK: */ |
| Line 475 cfg_get_static_error_str(errorcode) |
|
| Line 435 cfg_get_static_error_str(errorcode) |
|
| } |
} |
| |
|
| return "unknown error"; |
return "unknown error"; |
| } |
} /* }}} */ |
| |
|
| #if defined(SELF) || defined(SELFTEST) || defined(SELF_CFG) |
#if defined(SELF) || defined(SELFTEST) || defined(SELF_CFG) |
| |
|
| |
|
| void |
void |
| ar_print(name, format, vals) |
ar_print(name, format, vals) |
| char *name; |
char *name; |
| char *format; |
char *format; |
| void **vals; |
void **vals; |
| { |
{ /* {{{ */ |
| if (vals != NULL) { |
if (vals != NULL) { |
| if (strcmp(format, "%s")) { |
if (strcmp(format, "%s")) { |
| register int i; |
register int i; |
| Line 515 ar_print(name, format, vals) |
|
| Line 474 ar_print(name, format, vals) |
|
| } |
} |
| else |
else |
| printf("%s uninitialized\n", name); |
printf("%s uninitialized\n", name); |
| } |
} /* }}} */ |
| |
|
| int |
int |
| main(argc, argv) |
main(argc, argv) |
| int argc; |
int argc; |
| char **argv; |
char **argv; |
| { |
{ /* {{{ */ |
| |
|
| CFG_CONTEXT con; |
CFG_CONTEXT con; |
| int ret, i; |
int ret, i; |
| Line 585 main(argc, argv) |
|
| Line 544 main(argc, argv) |
|
| } |
} |
| else { |
else { |
| cfg_set_cfgfile_context(con, 1, 0, o_cfgfile); |
cfg_set_cfgfile_context(con, 1, 0, o_cfgfile); |
| cfg_remove_cfgfile_comment_prefix(con, "//"); |
/* cfg_remove_cfgfile_comment_prefix(con, "//"); |
| cfg_add_cfgfile_comment_prefix(con, "//"); |
cfg_add_cfgfile_comment_prefix(con, "//"); */ |
| |
cfg_remove_property(con, CFG_FILE_COMMENT_PREFIX, "//"); |
| |
cfg_add_property(con, CFG_FILE_COMMENT_PREFIX, "//"); |
| } |
} |
| |
|
| while ((ret = cfg_get_next_opt(con)) > 0) { |
while ((ret = cfg_get_next_opt(con)) > 0) { |
| Line 662 main(argc, argv) |
|
| Line 623 main(argc, argv) |
|
| ar_print("o_leftover", "%s", (void **) o_leftover); |
ar_print("o_leftover", "%s", (void **) o_leftover); |
| |
|
| return 0; |
return 0; |
| } |
} /* }}} */ |
| |
|
| #endif |
#endif |
| |
|
| |
/* Modeline for ViM {{{ |
| |
* vim:set ts=4: |
| |
* vim600:fdm=marker fdl=0 fdc=0: |
| |
* }}} */ |
| |
|