david/ipxe
david
/
ipxe
Archived
1
0
Fork 0

Move {show,set,clear}_setting() to {show,set,clear}_named_setting().

Introduce new {show,set,clear}_setting() that take a struct setting *
rather than a const char *.

set_setting() handles calling clear_setting() when appropriate, so that
individual setting types don't have to check for empty strings.
This commit is contained in:
Michael Brown 2006-12-20 04:20:13 +00:00
parent fc7dcc201b
commit 35edecac34
4 changed files with 74 additions and 37 deletions

View File

@ -116,7 +116,7 @@ find_or_build_config_setting ( const char *name,
} }
/** /**
* Show value of setting * Show value of named setting
* *
* @v context Configuration context * @v context Configuration context
* @v name Configuration setting name * @v name Configuration setting name
@ -124,27 +124,27 @@ find_or_build_config_setting ( const char *name,
* @v len Length of buffer * @v len Length of buffer
* @ret rc Return status code * @ret rc Return status code
*/ */
int show_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; struct config_setting tmp_setting;
setting = find_or_build_config_setting ( name, &tmp_setting ); setting = find_or_build_config_setting ( name, &tmp_setting );
if ( ! setting ) if ( ! setting )
return -ENOENT; return -ENOENT;
return setting->type->show ( context, setting, buf, len ); return show_setting ( context, setting, buf, len );
} }
/** /**
* Set value of setting * Set value of named setting
* *
* @v context Configuration context * @v context Configuration context
* @v name Configuration setting name * @v name Configuration setting name
* @v value Setting value (as a string) * @v value Setting value (as a string)
* @ret rc Return status code * @ret rc Return status code
*/ */
int set_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; struct config_setting tmp_setting;
@ -155,24 +155,21 @@ int set_setting ( struct config_context *context, const char *name,
} }
/** /**
* Clear setting * Set value of setting
* *
* @v context Configuration context * @v context Configuration context
* @v name Configuration setting name * @v setting Configuration setting
* @v value Setting value (as a string), or NULL
* @ret rc Return status code * @ret rc Return status code
*/ */
int clear_setting ( struct config_context *context, const char *name ) { int set_setting ( struct config_context *context,
struct config_setting *setting; struct config_setting *setting,
struct config_setting tmp_setting; const char *value ) {
if ( ( ! value ) || ( ! *value ) ) {
setting = find_or_build_config_setting ( name, &tmp_setting ); /* Save putting deletion logic in each individual handler */
if ( ! setting ) return clear_setting ( context, setting );
return -ENOENT; }
return setting->type->set ( context, setting, value );
/* All types of settings get cleared the same way */
delete_dhcp_option ( context->options, setting->tag );
return 0;
} }
/** /**
@ -259,7 +256,6 @@ static int set_ipv4 ( struct config_context *context,
const char *value ) { const char *value ) {
struct dhcp_option *option; struct dhcp_option *option;
struct in_addr ipv4; struct in_addr ipv4;
int rc;
if ( inet_aton ( value, &ipv4 ) == 0 ) if ( inet_aton ( value, &ipv4 ) == 0 )
return -EINVAL; return -EINVAL;

View File

@ -27,8 +27,8 @@ static int show_exec ( int argc, char **argv ) {
} }
dummy_context.options = ugly_nvo_hack->options; dummy_context.options = ugly_nvo_hack->options;
if ( ( rc = show_setting ( &dummy_context, argv[1], buf, if ( ( rc = show_named_setting ( &dummy_context, argv[1], buf,
sizeof ( buf ) ) ) != 0 ) { sizeof ( buf ) ) ) != 0 ) {
printf ( "Could not find \"%s\": %s\n", printf ( "Could not find \"%s\": %s\n",
argv[1], strerror ( -rc ) ); argv[1], strerror ( -rc ) );
return 1; return 1;
@ -59,7 +59,8 @@ static int set_exec ( int argc, char **argv ) {
} }
dummy_context.options = ugly_nvo_hack->options; dummy_context.options = ugly_nvo_hack->options;
if ( ( rc = set_setting ( &dummy_context, argv[1], argv[2] ) ) != 0 ) { if ( ( rc = set_named_setting ( &dummy_context, argv[1],
argv[2] ) ) != 0 ) {
printf ( "Could not set \"%s\"=\"%s\": %s\n", printf ( "Could not set \"%s\"=\"%s\": %s\n",
argv[1], argv[2], strerror ( -rc ) ); argv[1], argv[2], strerror ( -rc ) );
return 1; return 1;
@ -94,7 +95,7 @@ static int clear_exec ( int argc, char **argv ) {
} }
dummy_context.options = ugly_nvo_hack->options; dummy_context.options = ugly_nvo_hack->options;
if ( ( rc = clear_setting ( &dummy_context, argv[1] ) ) != 0 ) { if ( ( rc = clear_named_setting ( &dummy_context, argv[1] ) ) != 0 ) {
printf ( "Could not clear \"%s\": %s\n", printf ( "Could not clear \"%s\": %s\n",
argv[1], strerror ( -rc ) ); argv[1], strerror ( -rc ) );
return 1; return 1;

View File

@ -91,9 +91,8 @@ static void load_setting ( struct setting_widget *widget ) {
widget->editing = 0; widget->editing = 0;
/* Read current setting value */ /* Read current setting value */
if ( widget->setting->type->show ( widget->context, widget->setting, if ( show_setting ( widget->context, widget->setting,
widget->value, widget->value, sizeof ( widget->value ) ) != 0 ) {
sizeof ( widget->value ) ) != 0 ) {
widget->value[0] = '\0'; widget->value[0] = '\0';
} }
@ -110,8 +109,7 @@ static void load_setting ( struct setting_widget *widget ) {
* @v widget Setting widget * @v widget Setting widget
*/ */
static int save_setting ( struct setting_widget *widget ) { static int save_setting ( struct setting_widget *widget ) {
return widget->setting->type->set ( widget->context, widget->setting, return set_setting ( widget->context, widget->setting, widget->value );
widget->value );
} }
/** /**
@ -252,7 +250,7 @@ static void main_loop ( struct config_context *context ) {
if ( ( rc = save_setting ( &widget ) ) != 0 ) { if ( ( rc = save_setting ( &widget ) ) != 0 ) {
alert ( " Could not set %s: %s ", alert ( " Could not set %s: %s ",
widget.setting->name, widget.setting->name,
strerror ( -rc ) ); strerror ( rc ) );
} }
/* Fall through */ /* Fall through */
case 0x03: /* Ctrl-C */ case 0x03: /* Ctrl-C */

View File

@ -96,12 +96,54 @@ struct config_setting {
/** Declare a configuration setting */ /** Declare a configuration setting */
#define __config_setting __table ( config_settings, 01 ) #define __config_setting __table ( config_settings, 01 )
/* Function prototypes */ /**
* Show value of 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 inline int show_setting ( struct config_context *context,
struct config_setting *setting,
char *buf, size_t len ) {
return setting->type->show ( context, setting, buf, len );
}
extern int show_setting ( struct config_context *context, const char *name, extern int set_setting ( struct config_context *context,
char *buf, size_t len ); struct config_setting *setting,
extern int set_setting ( struct config_context *context, const char *name,
const char *value ); const char *value );
extern int clear_setting ( struct config_context *context, const char *name );
/**
* Clear setting
*
* @v context Configuration context
* @v setting Configuration setting
* @ret rc Return status code
*/
static inline int clear_setting ( struct config_context *context,
struct config_setting *setting ) {
delete_dhcp_option ( context->options, setting->tag );
return 0;
}
/* Function prototypes */
extern int show_named_setting ( struct config_context *context,
const char *name, char *buf, size_t len );
extern int set_named_setting ( struct config_context *context,
const char *name, const char *value );
/**
* Clear named setting
*
* @v context Configuration context
* @v name Configuration setting name
* @ret rc Return status code
*/
static inline int clear_named_setting ( struct config_context *context,
const char *name ) {
return set_named_setting ( context, name, NULL );
}
#endif /* _GPXE_SETTINGS_H */ #endif /* _GPXE_SETTINGS_H */