Skip to content

Commit

Permalink
Add string configuration type
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Brown committed Aug 11, 2006
1 parent db0ff17 commit 7029fb8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
59 changes: 54 additions & 5 deletions src/core/settings.c
Expand Up @@ -116,17 +116,19 @@ find_or_build_config_setting ( const char *name,
*
* @v context Configuration context
* @v name Configuration setting name
* @ret value Setting value (as a string), or NULL
* @v buf Buffer to contain value
* @v len Length of buffer
* @ret rc Return status code
*/
const char * ( show_setting ) ( struct config_context *context,
const char *name ) {
int ( show_setting ) ( struct config_context *context, const char *name,
char *buf, size_t len ) {
struct config_setting *setting;
struct config_setting tmp_setting;

setting = find_or_build_config_setting ( name, &tmp_setting );
if ( ! setting )
return NULL;
return setting->type->show ( context, setting );
return -ENOENT;
return setting->type->show ( context, setting, buf, len );
}

/** Set value of setting
Expand All @@ -146,3 +148,50 @@ int ( set_setting ) ( struct config_context *context, const char *name,
return -ENOENT;
return setting->type->set ( context, setting, value );
}

/**
* Show value of string setting
*
* @v context Configuration context
* @v setting Configuration setting
* @v buf Buffer to contain value
* @v len Length of buffer
* @ret rc Return status code
*/
static int show_string ( struct config_context *context,
struct config_setting *setting,
char *buf, size_t len ) {
struct dhcp_option *option;

option = find_dhcp_option ( context->options, setting->tag );
if ( ! option )
return -ENOENT;
dhcp_snprintf ( buf, len, option );
return 0;
}

/** Set value of string setting
*
* @v context Configuration context
* @v setting Configuration setting
* @v value Setting value (as a string)
* @ret rc Return status code
*/
static int set_string ( struct config_context *context,
struct config_setting *setting,
const char *value ) {
struct dhcp_option *option;

option = set_dhcp_option ( context->options, setting->tag,
value, strlen ( value ) );
if ( ! option )
return -ENOMEM;
return 0;
}

/** A string configuration setting */
struct config_setting_type config_setting_type_string __config_setting_type = {
.name = "string",
.show = show_string,
.set = set_string,
};
13 changes: 8 additions & 5 deletions src/include/gpxe/settings.h
Expand Up @@ -44,10 +44,13 @@ struct config_setting_type {
*
* @v context Configuration context
* @v setting Configuration setting
* @ret value Setting value (as a string), or NULL
* @v buf Buffer to contain value
* @v len Length of buffer
* @ret rc Return status code
*/
const char * ( * show ) ( struct config_context *context,
struct config_setting *setting );
int ( * show ) ( struct config_context *context,
struct config_setting *setting,
char *buf, size_t len );
/** Set value of setting
*
* @v context Configuration context
Expand Down Expand Up @@ -95,8 +98,8 @@ struct config_setting {

/* Function prototypes */

extern const char * ( show_setting ) ( struct config_context *context,
const char *name );
extern int ( show_setting ) ( struct config_context *context, const char *name,
char *buf, size_t len );
extern int ( set_setting ) ( struct config_context *context, const char *name,
const char *value );

Expand Down

0 comments on commit 7029fb8

Please sign in to comment.