2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <gpxe/settings.h>
28 * Configuration settings
32 /** Registered configuration setting types */
33 static struct config_setting_type
34 config_setting_types[0] __table_start ( config_setting_types );
35 static struct config_setting_type
36 config_setting_types_end[0] __table_end ( config_setting_types );
38 /** Registered configuration settings */
39 static struct config_setting
40 config_settings[0] __table_start ( config_settings );
41 static struct config_setting
42 config_settings_end[0] __table_end ( config_settings );
45 * Find configuration setting type
48 * @ret type Configuration setting type, or NULL
50 static struct config_setting_type *
51 find_config_setting_type ( const char *name ) {
52 struct config_setting_type *type;
54 for ( type = config_setting_types ; type < config_setting_types_end ;
56 if ( strcmp ( name, type->name ) == 0 )
63 * Find configuration setting
66 * @ret setting Configuration setting, or NULL
68 static struct config_setting * find_config_setting ( const char *name ) {
69 struct config_setting *setting;
71 for ( setting = config_settings ; setting < config_settings_end ;
73 if ( strcmp ( name, setting->name ) == 0 )
80 * Find or build configuration setting
83 * @v tmp_setting Temporary buffer for constructing a setting
84 * @ret setting Configuration setting, or NULL
86 * Find setting if it exists. If it doesn't exist, but the name is of
87 * the form "<num>.<type>" (e.g. "12.string"), then construct a
88 * setting for that tag and data type, and return it. The constructed
89 * setting will be placed in the temporary buffer.
91 static struct config_setting *
92 find_or_build_config_setting ( const char *name,
93 struct config_setting *tmp_setting ) {
94 struct config_setting *setting;
97 /* Look in the list of registered settings first */
98 setting = find_config_setting ( name );
102 /* If name is of the form "<num>.<type>", try to construct a setting */
103 setting = tmp_setting;
104 memset ( setting, 0, sizeof ( *setting ) );
105 setting->name = name;
106 setting->tag = strtoul ( name, &separator, 10 );
107 if ( *separator != '.' )
109 setting->type = find_config_setting_type ( separator + 1 );
110 if ( ! setting->type )
115 /** Show value of setting
117 * @v context Configuration context
118 * @v name Configuration setting name
119 * @ret value Setting value (as a string), or NULL
121 const char * ( show_setting ) ( struct config_context *context,
123 struct config_setting *setting;
124 struct config_setting tmp_setting;
126 setting = find_or_build_config_setting ( name, &tmp_setting );
129 return setting->type->show ( context, setting );
132 /** Set value of setting
134 * @v context Configuration context
135 * @v name Configuration setting name
136 * @v value Setting value (as a string)
137 * @ret rc Return status code
139 int ( set_setting ) ( struct config_context *context, const char *name,
140 const char *value ) {
141 struct config_setting *setting;
142 struct config_setting tmp_setting;
144 setting = find_or_build_config_setting ( name, &tmp_setting );
147 return setting->type->set ( context, setting, value );