more/better error checks, harmonized coding style
This commit is contained in:
+81
-82
@@ -52,6 +52,7 @@ key * add_key( section * s, char * keyname, char * value );
|
||||
char get_next_char_f(buffile * f);
|
||||
int process_config(section ** current_section, char(*get_next_char)(), char modify_section_allowed, char * source_descr, buffile * f);
|
||||
|
||||
|
||||
#ifdef WITH_LDAP_SUPPORT
|
||||
int connect_to_ldap(void);
|
||||
|
||||
@@ -67,6 +68,7 @@ int ldap_port;
|
||||
*/
|
||||
#endif /* WITH_LDAP_SUPPORT */
|
||||
|
||||
|
||||
/**** EXTERNAL FUNCTIONS ****/
|
||||
|
||||
int config_read_file(char *filename)
|
||||
@@ -133,9 +135,8 @@ int config_read_string( char *sectionname, char *str )
|
||||
return str[pos++];
|
||||
}
|
||||
|
||||
if( !( s=find_section( sectionname ))) {
|
||||
if ((s = find_section(sectionname)) == NULL)
|
||||
s = add_section(sectionname);
|
||||
}
|
||||
|
||||
process_config(&s, get_next_char, 0, "command line", NULL);
|
||||
|
||||
@@ -146,13 +147,10 @@ int config_read_string( char *sectionname, char *str )
|
||||
char *config_get_string(char * sectionname, char * keyname,
|
||||
int skip, char * default_value)
|
||||
{
|
||||
section * s;
|
||||
key * k;
|
||||
key *k = find_key(find_section(sectionname), keyname, skip);
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( !s ) return default_value;
|
||||
k = find_key( s, keyname, skip );
|
||||
if( !k ) return default_value;
|
||||
if (k == NULL)
|
||||
return default_value;
|
||||
|
||||
return k->value;
|
||||
|
||||
@@ -171,13 +169,10 @@ char *config_get_string( char * sectionname, char * keyname,
|
||||
short config_get_bool(char *sectionname, char *keyname,
|
||||
int skip, short default_value)
|
||||
{
|
||||
section * s;
|
||||
key * k;
|
||||
key *k = find_key(find_section(sectionname), keyname, skip);
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( !s ) return default_value;
|
||||
k = find_key( s, keyname, skip );
|
||||
if( !k ) return default_value;
|
||||
if (k == NULL)
|
||||
return default_value;
|
||||
|
||||
if (strcasecmp(k->value, "0") == 0 || strcasecmp(k->value, "false") == 0
|
||||
|| strcasecmp(k->value, "n") == 0 || strcasecmp(k->value, "no") == 0) {
|
||||
@@ -194,70 +189,54 @@ short config_get_bool( char *sectionname, char *keyname,
|
||||
long int config_get_int(char *sectionname, char *keyname,
|
||||
int skip, long int default_value)
|
||||
{
|
||||
section * s;
|
||||
key * k;
|
||||
long int v;
|
||||
char * v_end;
|
||||
key *k = find_key(find_section(sectionname), keyname, skip);
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( !s ) return default_value;
|
||||
k = find_key( s, keyname, skip );
|
||||
if( !k ) return default_value;
|
||||
if (k != NULL) {
|
||||
char *end;
|
||||
long int v = strtol(k->value, &end, 0);
|
||||
|
||||
v = strtol( k->value, &v_end, 0 );
|
||||
if( v_end-(k->value) != strlen(k->value) ) {
|
||||
/* Conversion not succesful*/
|
||||
return default_value;
|
||||
}
|
||||
if ((end != NULL) && (end != k->value) && (*end == '\0'))
|
||||
/* Conversion succesful*/
|
||||
return v;
|
||||
}
|
||||
return default_value;
|
||||
}
|
||||
|
||||
|
||||
double config_get_float(char *sectionname, char *keyname,
|
||||
int skip, double default_value)
|
||||
{
|
||||
section * s;
|
||||
key * k;
|
||||
double v;
|
||||
char * v_end;
|
||||
key *k = find_key(find_section(sectionname), keyname, skip);
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( !s ) return default_value;
|
||||
k = find_key( s, keyname, skip );
|
||||
if( !k ) return default_value;
|
||||
if (k != NULL) {
|
||||
char *end;
|
||||
double v = strtod(k->value, &end);
|
||||
|
||||
v = strtod( k->value, &v_end );
|
||||
if( v_end-(k->value) != strlen(k->value) ) {
|
||||
/* Conversion not succesful*/
|
||||
return default_value;
|
||||
}
|
||||
if ((end != NULL) && (end != k->value) && (*end == '\0'))
|
||||
/* Conversion succesful*/
|
||||
return v;
|
||||
}
|
||||
return default_value;
|
||||
}
|
||||
|
||||
|
||||
int config_has_section(char *sectionname)
|
||||
{
|
||||
section * s;
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( s ) return 1; else return 0;
|
||||
return (find_section(sectionname) != NULL) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
int config_has_key(char *sectionname, char *keyname)
|
||||
{
|
||||
section * s;
|
||||
key * k;
|
||||
|
||||
section *s = find_section(sectionname);
|
||||
int count = 0;
|
||||
|
||||
s = find_section( sectionname );
|
||||
if( !s ) return 0;
|
||||
|
||||
for( k=s->first_key; k; k=k->next_key ) {
|
||||
if (s != NULL) {
|
||||
key *k;
|
||||
|
||||
for (k = s->first_key; k != NULL; k = k->next_key) {
|
||||
/* Did we find the right key ?*/
|
||||
if( strcasecmp( k->name, keyname ) == 0 ) {
|
||||
if (strcasecmp(k->name, keyname) == 0)
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -269,19 +248,18 @@ void config_clear()
|
||||
{
|
||||
section *s;
|
||||
section *next_s;
|
||||
|
||||
for (s = first_section; s != NULL; s = next_s) {
|
||||
key *k;
|
||||
key *next_k;
|
||||
|
||||
for( s = first_section; s; ) {
|
||||
for( k=s->first_key; k; ) {
|
||||
for (k = s->first_key; k != NULL; k = next_k) {
|
||||
/* Advance before we destroy the current key */
|
||||
next_k = k->next_key;
|
||||
|
||||
free(k->name);
|
||||
free(k->value);
|
||||
free(k);
|
||||
|
||||
k = next_k;
|
||||
}
|
||||
/* Advance before we destroy the current section */
|
||||
next_s = s->next_section;
|
||||
@@ -289,10 +267,8 @@ void config_clear()
|
||||
/* And destroy it */
|
||||
free(s->name);
|
||||
free(s);
|
||||
|
||||
s = next_s;
|
||||
}
|
||||
/* And make everything inaccessable */
|
||||
/* Finally make everything inaccessable */
|
||||
first_section = NULL;
|
||||
}
|
||||
|
||||
@@ -380,14 +356,16 @@ section * find_section( char * sectionname )
|
||||
ldap_msgfree(res);
|
||||
debug(RPT_DEBUG, "Found section [%s] in LDAP", sectionname);
|
||||
s = (section*) malloc(sizeof(section));
|
||||
if (s != NULL) {
|
||||
s->name = strdup(sectionname);
|
||||
s->first_key = NULL;
|
||||
s->next_section = NULL;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
#endif /* WITH_LDAP_SUPPORT */
|
||||
|
||||
for( s=first_section; s; s=s->next_section ) {
|
||||
for (s = first_section; s != NULL; s = s->next_section) {
|
||||
if (strcasecmp(s->name, sectionname) == 0) {
|
||||
return s;
|
||||
}
|
||||
@@ -400,13 +378,15 @@ section * add_section( char * sectionname )
|
||||
section *s;
|
||||
section **place = &first_section;
|
||||
|
||||
for( s=first_section; s; s=s->next_section )
|
||||
for (s = first_section; s != NULL; s = s->next_section)
|
||||
place = &(s->next_section);
|
||||
|
||||
*place = (section*) malloc(sizeof(section));
|
||||
if (*place != NULL) {
|
||||
(*place)->name = strdup(sectionname);
|
||||
(*place)->first_key = NULL;
|
||||
(*place)->next_section = NULL;
|
||||
}
|
||||
|
||||
return(*place);
|
||||
}
|
||||
@@ -426,7 +406,8 @@ key * find_key( section * s, char * keyname, int skip )
|
||||
#endif /* WITH_LDAP_SUPPORT */
|
||||
|
||||
/* Check for NULL section*/
|
||||
if(!s) return NULL;
|
||||
if (s == NULL)
|
||||
return NULL;
|
||||
|
||||
#ifdef WITH_LDAP_SUPPORT
|
||||
if (use_ldap) {
|
||||
@@ -495,9 +476,11 @@ key * find_key( section * s, char * keyname, int skip )
|
||||
ldap_value_free(vals);
|
||||
|
||||
k = (key *) malloc(sizeof(key));
|
||||
if ( k != NULL) {
|
||||
k->name = strdup(keyname);
|
||||
k->value = strdup(buf);
|
||||
k->next_key = NULL;
|
||||
}
|
||||
|
||||
if (NULL != buf) {
|
||||
free(buf);
|
||||
@@ -515,44 +498,49 @@ key * find_key( section * s, char * keyname, int skip )
|
||||
}
|
||||
#endif /* WITH_LDAP_SUPPORT */
|
||||
|
||||
for( k=s->first_key; k; k=k->next_key ) {
|
||||
for (k = s->first_key; k != NULL; k = k->next_key) {
|
||||
|
||||
/* Did we find the right key ?*/
|
||||
if (strcasecmp(k->name, keyname) == 0) {
|
||||
if( count == skip ) {
|
||||
if (count == skip)
|
||||
return k;
|
||||
} else {
|
||||
|
||||
count++;
|
||||
last_key = k;
|
||||
}
|
||||
}
|
||||
}
|
||||
if( skip == -1 ) {
|
||||
if (skip == -1)
|
||||
return last_key;
|
||||
}
|
||||
|
||||
return NULL; /* not found*/
|
||||
}
|
||||
|
||||
key * add_key(section * s, char * keyname, char * value)
|
||||
{
|
||||
if (s != NULL) {
|
||||
key *k;
|
||||
|
||||
key **place = &(s->first_key);
|
||||
|
||||
for( k=s->first_key; k; k=k->next_key )
|
||||
for (k = s->first_key; k != NULL; k = k->next_key)
|
||||
place = &(k->next_key);
|
||||
|
||||
*place = (key *) malloc(sizeof(key));
|
||||
if (*place != NULL) {
|
||||
(*place)->name = strdup(keyname);
|
||||
(*place)->value = strdup(value);
|
||||
(*place)->next_key = NULL;
|
||||
}
|
||||
|
||||
return(*place);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char get_next_char_f(buffile * f) {
|
||||
char get_next_char_f(buffile * f)
|
||||
{
|
||||
char * buf = buffile_read(f, 1);
|
||||
char c = buf[0];
|
||||
|
||||
free(buf);
|
||||
return c;
|
||||
}
|
||||
@@ -595,17 +583,13 @@ int process_config( section ** current_section, char (*get_next_char)(), char mo
|
||||
|
||||
while (state != ST_END) {
|
||||
|
||||
if (NULL != f) {
|
||||
ch = get_next_char(f);
|
||||
}
|
||||
else {
|
||||
ch = get_next_char();
|
||||
}
|
||||
ch = (f != NULL)
|
||||
? get_next_char(f)
|
||||
: get_next_char();
|
||||
|
||||
/* Secretly keep count of the line numbers*/
|
||||
if( ch == '\n' ) {
|
||||
if (ch == '\n')
|
||||
line_nr++;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case ST_INITIAL:
|
||||
@@ -647,6 +631,7 @@ int process_config( section ** current_section, char (*get_next_char)(), char mo
|
||||
break;
|
||||
case ST_SECTIONNAME:
|
||||
switch (ch) {
|
||||
case '\0':
|
||||
case '\n':
|
||||
report(RPT_WARNING, "Section name incorrectly closed on line %d of %s: %s", line_nr, source_descr, sectionname);
|
||||
state = ST_INITIAL;
|
||||
@@ -665,9 +650,6 @@ int process_config( section ** current_section, char (*get_next_char)(), char mo
|
||||
}
|
||||
state = ST_INITIAL;
|
||||
break;
|
||||
case '\0':
|
||||
report( RPT_WARNING, "Section name incorrectly closed on line %d of %s: %s", line_nr, source_descr, sectionname );
|
||||
break;
|
||||
default:
|
||||
if (sectionname_pos < MAXSECTIONNAMELENGTH) {
|
||||
sectionname[sectionname_pos++] = ch;
|
||||
@@ -820,3 +802,20 @@ int process_config( section ** current_section, char (*get_next_char)(), char mo
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if 0
|
||||
void config_dump(void)
|
||||
{
|
||||
section *s;
|
||||
|
||||
for (s = first_section; s != NULL; s = s->next_section) {
|
||||
key *k;
|
||||
|
||||
fprintf(stderr, "[%s]\n", s->name);
|
||||
|
||||
for (k = s->first_key; k != NULL; k = k->next_key)
|
||||
fprintf(stderr, "%s = '%s'\n", k->name, k->value);
|
||||
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user