more/better error checks, harmonized coding style

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