Browse Source

Add string configuration type

tags/v0.9.3
Michael Brown 18 years ago
parent
commit
7029fb8eff
2 changed files with 62 additions and 10 deletions
  1. 54
    5
      src/core/settings.c
  2. 8
    5
      src/include/gpxe/settings.h

+ 54
- 5
src/core/settings.c View File

@@ -116,17 +116,19 @@ find_or_build_config_setting ( const char *name,
116 116
  *
117 117
  * @v context		Configuration context
118 118
  * @v name		Configuration setting name
119
- * @ret value		Setting value (as a string), or NULL
119
+ * @v buf		Buffer to contain value
120
+ * @v len		Length of buffer
121
+ * @ret rc		Return status code
120 122
  */
121
-const char * ( show_setting ) ( struct config_context *context,
122
-				const char *name ) {
123
+int ( show_setting ) ( struct config_context *context, const char *name,
124
+		       char *buf, size_t len ) {
123 125
 	struct config_setting *setting;
124 126
 	struct config_setting tmp_setting;
125 127
 
126 128
 	setting = find_or_build_config_setting ( name, &tmp_setting );
127 129
 	if ( ! setting )
128
-		return NULL;
129
-	return setting->type->show ( context, setting );
130
+		return -ENOENT;
131
+	return setting->type->show ( context, setting, buf, len );
130 132
 }
131 133
 
132 134
 /** Set value of setting
@@ -146,3 +148,50 @@ int ( set_setting ) ( struct config_context *context, const char *name,
146 148
 		return -ENOENT;
147 149
 	return setting->type->set ( context, setting, value );
148 150
 }
151
+
152
+/**
153
+ * Show value of string setting
154
+ *
155
+ * @v context		Configuration context
156
+ * @v setting		Configuration setting
157
+ * @v buf		Buffer to contain value
158
+ * @v len		Length of buffer
159
+ * @ret rc		Return status code
160
+ */
161
+static int show_string ( struct config_context *context,
162
+			 struct config_setting *setting,
163
+			 char *buf, size_t len ) {
164
+	struct dhcp_option *option;
165
+
166
+	option = find_dhcp_option ( context->options, setting->tag );
167
+	if ( ! option )
168
+		return -ENOENT;
169
+	dhcp_snprintf ( buf, len, option );
170
+	return 0;
171
+}
172
+
173
+/** Set value of string setting
174
+ *
175
+ * @v context		Configuration context
176
+ * @v setting		Configuration setting
177
+ * @v value		Setting value (as a string)
178
+ * @ret rc		Return status code
179
+ */ 
180
+static int set_string ( struct config_context *context,
181
+			struct config_setting *setting,
182
+			const char *value ) {
183
+	struct dhcp_option *option;
184
+
185
+	option = set_dhcp_option ( context->options, setting->tag,
186
+				   value, strlen ( value ) );
187
+	if ( ! option )
188
+		return -ENOMEM;
189
+	return 0;
190
+}
191
+
192
+/** A string configuration setting */
193
+struct config_setting_type config_setting_type_string __config_setting_type = {
194
+	.name = "string",
195
+	.show = show_string,
196
+	.set = set_string,
197
+};

+ 8
- 5
src/include/gpxe/settings.h View File

@@ -44,10 +44,13 @@ struct config_setting_type {
44 44
 	 *
45 45
 	 * @v context		Configuration context
46 46
 	 * @v setting		Configuration setting
47
-	 * @ret value		Setting value (as a string), or NULL
47
+	 * @v buf		Buffer to contain value
48
+	 * @v len		Length of buffer
49
+	 * @ret rc		Return status code
48 50
 	 */
49
-	const char * ( * show ) ( struct config_context *context,
50
-				  struct config_setting *setting );
51
+	int ( * show ) ( struct config_context *context,
52
+			 struct config_setting *setting,
53
+			 char *buf, size_t len );
51 54
 	/** Set value of setting
52 55
 	 *
53 56
 	 * @v context		Configuration context
@@ -95,8 +98,8 @@ struct config_setting {
95 98
 
96 99
 /* Function prototypes */
97 100
 
98
-extern const char * ( show_setting ) ( struct config_context *context,
99
-				       const char *name );
101
+extern int ( show_setting ) ( struct config_context *context, const char *name,
102
+			      char *buf, size_t len );
100 103
 extern int ( set_setting ) ( struct config_context *context, const char *name,
101 104
 			     const char *value );
102 105
 

Loading…
Cancel
Save