david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

[Settings] Allow named settings to have their type specified explicitly

Allow setting names such as "ip:hex".
This commit is contained in:
Michael Brown 2008-03-18 15:51:03 +00:00
parent 070cbebc26
commit af1c6b869c
1 changed files with 47 additions and 44 deletions

View File

@ -87,49 +87,54 @@ static struct config_setting * find_config_setting ( const char *name ) {
* Find or build configuration setting * Find or build configuration setting
* *
* @v name Name * @v name Name
* @v tmp_setting Temporary buffer for constructing a setting * @v setting Buffer to fill in with setting
* @ret setting Configuration setting, or NULL * @ret rc Return status code
* *
* Find setting if it exists. If it doesn't exist, but the name is of * Find setting if it exists. If it doesn't exist, but the name is of
* the form "<num>:<type>" (e.g. "12:string"), then construct a * the form "<num>:<type>" (e.g. "12:string"), then construct a
* setting for that tag and data type, and return it. The constructed * setting for that tag and data type, and return it. The constructed
* setting will be placed in the temporary buffer. * setting will be placed in the buffer.
*/ */
static struct config_setting * static int find_or_build_config_setting ( const char *name,
find_or_build_config_setting ( const char *name, struct config_setting *setting ) {
struct config_setting *tmp_setting ) { struct config_setting *known_setting;
struct config_setting *setting; char tmp_name[ strlen ( name ) + 1 ];
char *separator; char *qualifier;
char *tmp;
/* Look in the list of registered settings first */ /* Set defaults */
setting = find_config_setting ( name );
if ( setting )
return setting;
/* If name is of the form "<num>:<type>", try to construct a setting */
setting = tmp_setting;
memset ( setting, 0, sizeof ( *setting ) ); memset ( setting, 0, sizeof ( *setting ) );
setting->name = name; setting->name = name;
for ( separator = ( char * ) name ; 1 ; separator++ ) { setting->type = &config_setting_type_hex;
setting->tag = ( ( setting->tag << 8 ) |
strtoul ( separator, &separator, 0 ) ); /* Strip qualifier, if present */
if ( *separator != '.' ) memcpy ( tmp_name, name, sizeof ( tmp_name ) );
break; if ( ( qualifier = strchr ( tmp_name, ':' ) ) != NULL )
*(qualifier++) = 0;
/* If we recognise the name of the setting, use it */
if ( ( known_setting = find_config_setting ( tmp_name ) ) != NULL ) {
memcpy ( setting, known_setting, sizeof ( *setting ) );
} else {
/* Otherwise, try to interpret as a numerical setting */
for ( tmp = tmp_name ; 1 ; tmp++ ) {
setting->tag = ( ( setting->tag << 8 ) |
strtoul ( tmp, &tmp, 0 ) );
if ( *tmp != '.' )
break;
}
if ( *tmp != 0 )
return -EINVAL;
} }
switch ( *separator ) { /* Apply qualifier, if present */
case ':' : if ( qualifier ) {
setting->type = find_config_setting_type ( separator + 1 ); setting->type = find_config_setting_type ( qualifier );
break; if ( ! setting->type )
case '\0' : return -EINVAL;
setting->type = &config_setting_type_hex;
break;
default :
break;
} }
if ( ! setting->type )
return NULL; return 0;
return setting;
} }
/** /**
@ -143,13 +148,12 @@ find_or_build_config_setting ( const char *name,
*/ */
int show_named_setting ( struct config_context *context, const char *name, int show_named_setting ( struct config_context *context, const char *name,
char *buf, size_t len ) { char *buf, size_t len ) {
struct config_setting *setting; struct config_setting setting;
struct config_setting tmp_setting; int rc;
setting = find_or_build_config_setting ( name, &tmp_setting ); if ( ( rc = find_or_build_config_setting ( name, &setting ) ) != 0 )
if ( ! setting ) return rc;
return -ENOENT; return show_setting ( context, &setting, buf, len );
return show_setting ( context, setting, buf, len );
} }
/** /**
@ -162,13 +166,12 @@ int show_named_setting ( struct config_context *context, const char *name,
*/ */
int set_named_setting ( struct config_context *context, const char *name, int set_named_setting ( struct config_context *context, const char *name,
const char *value ) { const char *value ) {
struct config_setting *setting; struct config_setting setting;
struct config_setting tmp_setting; int rc;
setting = find_or_build_config_setting ( name, &tmp_setting ); if ( ( rc = find_or_build_config_setting ( name, &setting ) ) != 0 )
if ( ! setting ) return rc;
return -ENOENT; return set_setting ( context, &setting, value );
return setting->type->set ( context, setting, value );
} }
/** /**