Переглянути джерело

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.
tags/v0.9.3
Michael Brown 18 роки тому
джерело
коміт
35edecac34

+ 18
- 22
src/core/settings.c Переглянути файл

@@ -116,7 +116,7 @@ find_or_build_config_setting ( const char *name,
116 116
 }
117 117
 
118 118
 /**
119
- * Show value of setting
119
+ * Show value of named setting
120 120
  *
121 121
  * @v context		Configuration context
122 122
  * @v name		Configuration setting name
@@ -124,27 +124,27 @@ find_or_build_config_setting ( const char *name,
124 124
  * @v len		Length of buffer
125 125
  * @ret rc		Return status code
126 126
  */
127
-int show_setting ( struct config_context *context, const char *name,
128
-		   char *buf, size_t len ) {
127
+int show_named_setting ( struct config_context *context, const char *name,
128
+			 char *buf, size_t len ) {
129 129
 	struct config_setting *setting;
130 130
 	struct config_setting tmp_setting;
131 131
 
132 132
 	setting = find_or_build_config_setting ( name, &tmp_setting );
133 133
 	if ( ! setting )
134 134
 		return -ENOENT;
135
-	return setting->type->show ( context, setting, buf, len );
135
+	return show_setting ( context, setting, buf, len );
136 136
 }
137 137
 
138 138
 /**
139
- * Set value of setting
139
+ * Set value of named setting
140 140
  *
141 141
  * @v context		Configuration context
142 142
  * @v name		Configuration setting name
143 143
  * @v value		Setting value (as a string)
144 144
  * @ret rc		Return status code
145 145
  */
146
-int set_setting ( struct config_context *context, const char *name,
147
-		  const char *value ) {
146
+int set_named_setting ( struct config_context *context, const char *name,
147
+			const char *value ) {
148 148
 	struct config_setting *setting;
149 149
 	struct config_setting tmp_setting;
150 150
 
@@ -155,24 +155,21 @@ int set_setting ( struct config_context *context, const char *name,
155 155
 }
156 156
 
157 157
 /**
158
- * Clear setting
158
+ * Set value of setting
159 159
  *
160 160
  * @v context		Configuration context
161
- * @v name		Configuration setting name
161
+ * @v setting		Configuration setting
162
+ * @v value		Setting value (as a string), or NULL
162 163
  * @ret rc		Return status code
163 164
  */
164
-int clear_setting ( struct config_context *context, const char *name ) {
165
-	struct config_setting *setting;
166
-	struct config_setting tmp_setting;
167
-
168
-	setting = find_or_build_config_setting ( name, &tmp_setting );
169
-	if ( ! setting )
170
-		return -ENOENT;
171
-
172
-	/* All types of settings get cleared the same way */
173
-	delete_dhcp_option ( context->options, setting->tag );
174
-
175
-	return 0;
165
+int set_setting ( struct config_context *context,
166
+		  struct config_setting *setting,
167
+		  const char *value ) {
168
+	if ( ( ! value ) || ( ! *value ) ) {
169
+		/* Save putting deletion logic in each individual handler */
170
+		return clear_setting ( context, setting );
171
+	}
172
+	return setting->type->set ( context, setting, value );
176 173
 }
177 174
 
178 175
 /**
@@ -259,7 +256,6 @@ static int set_ipv4 ( struct config_context *context,
259 256
 		      const char *value ) {
260 257
 	struct dhcp_option *option;
261 258
 	struct in_addr ipv4;
262
-	int rc;
263 259
 	
264 260
 	if ( inet_aton ( value, &ipv4 ) == 0 )
265 261
 		return -EINVAL;

+ 5
- 4
src/hci/commands/nvo_cmd.c Переглянути файл

@@ -27,8 +27,8 @@ static int show_exec ( int argc, char **argv ) {
27 27
 	}
28 28
 
29 29
 	dummy_context.options = ugly_nvo_hack->options;
30
-	if ( ( rc = show_setting ( &dummy_context, argv[1], buf,
31
-				   sizeof ( buf ) ) ) != 0 ) {
30
+	if ( ( rc = show_named_setting ( &dummy_context, argv[1], buf,
31
+					 sizeof ( buf ) ) ) != 0 ) {
32 32
 		printf ( "Could not find \"%s\": %s\n",
33 33
 			 argv[1], strerror ( -rc ) );
34 34
 		return 1;
@@ -59,7 +59,8 @@ static int set_exec ( int argc, char **argv ) {
59 59
 	}
60 60
 
61 61
 	dummy_context.options = ugly_nvo_hack->options;
62
-	if ( ( rc = set_setting ( &dummy_context, argv[1], argv[2] ) ) != 0 ) {
62
+	if ( ( rc = set_named_setting ( &dummy_context, argv[1],
63
+					argv[2] ) ) != 0 ) {
63 64
 		printf ( "Could not set \"%s\"=\"%s\": %s\n",
64 65
 			 argv[1], argv[2], strerror ( -rc ) );
65 66
 		return 1;
@@ -94,7 +95,7 @@ static int clear_exec ( int argc, char **argv ) {
94 95
 	}
95 96
 
96 97
 	dummy_context.options = ugly_nvo_hack->options;
97
-	if ( ( rc = clear_setting ( &dummy_context, argv[1] ) ) != 0 ) {
98
+	if ( ( rc = clear_named_setting ( &dummy_context, argv[1] ) ) != 0 ) {
98 99
 		printf ( "Could not clear \"%s\": %s\n",
99 100
 			 argv[1], strerror ( -rc ) );
100 101
 		return 1;

+ 4
- 6
src/hci/tui/settings_ui.c Переглянути файл

@@ -91,9 +91,8 @@ static void load_setting ( struct setting_widget *widget ) {
91 91
 	widget->editing = 0;
92 92
 
93 93
 	/* Read current setting value */
94
-	if ( widget->setting->type->show ( widget->context, widget->setting,
95
-					   widget->value,
96
-					   sizeof ( widget->value ) ) != 0 ) {
94
+	if ( show_setting ( widget->context, widget->setting,
95
+			    widget->value, sizeof ( widget->value ) ) != 0 ) {
97 96
 		widget->value[0] = '\0';
98 97
 	}	
99 98
 
@@ -110,8 +109,7 @@ static void load_setting ( struct setting_widget *widget ) {
110 109
  * @v widget		Setting widget
111 110
  */
112 111
 static int save_setting ( struct setting_widget *widget ) {
113
-	return widget->setting->type->set ( widget->context, widget->setting,
114
-					    widget->value );
112
+	return set_setting ( widget->context, widget->setting, widget->value );
115 113
 }
116 114
 
117 115
 /**
@@ -252,7 +250,7 @@ static void main_loop ( struct config_context *context ) {
252 250
 				if ( ( rc = save_setting ( &widget ) ) != 0 ) {
253 251
 					alert ( " Could not set %s: %s ",
254 252
 						widget.setting->name,
255
-						strerror ( -rc ) );
253
+						strerror ( rc ) );
256 254
 				}
257 255
 				/* Fall through */
258 256
 			case 0x03: /* Ctrl-C */

+ 47
- 5
src/include/gpxe/settings.h Переглянути файл

@@ -96,12 +96,54 @@ struct config_setting {
96 96
 /** Declare a configuration setting */
97 97
 #define	__config_setting __table ( config_settings, 01 )
98 98
 
99
-/* Function prototypes */
99
+/**
100
+ * Show value of setting
101
+ *
102
+ * @v context		Configuration context
103
+ * @v setting		Configuration setting
104
+ * @v buf		Buffer to contain value
105
+ * @v len		Length of buffer
106
+ * @ret rc		Return status code
107
+ */
108
+static inline int show_setting ( struct config_context *context,
109
+				 struct config_setting *setting,
110
+				 char *buf, size_t len ) {
111
+	return setting->type->show ( context, setting, buf, len );
112
+}
100 113
 
101
-extern int show_setting ( struct config_context *context, const char *name,
102
-			  char *buf, size_t len );
103
-extern int set_setting ( struct config_context *context, const char *name,
114
+extern int set_setting ( struct config_context *context,
115
+			 struct config_setting *setting,
104 116
 			 const char *value );
105
-extern int clear_setting ( struct config_context *context, const char *name );
117
+
118
+/**
119
+ * Clear setting
120
+ *
121
+ * @v context		Configuration context
122
+ * @v setting		Configuration setting
123
+ * @ret rc		Return status code
124
+ */
125
+static inline int clear_setting ( struct config_context *context,
126
+				  struct config_setting *setting ) {
127
+	delete_dhcp_option ( context->options, setting->tag );
128
+	return 0;
129
+}
130
+
131
+/* Function prototypes */
132
+extern int show_named_setting ( struct config_context *context,
133
+				const char *name, char *buf, size_t len );
134
+extern int set_named_setting ( struct config_context *context,
135
+			       const char *name, const char *value );
136
+
137
+/**
138
+ * Clear named setting
139
+ *
140
+ * @v context		Configuration context
141
+ * @v name		Configuration setting name
142
+ * @ret rc		Return status code
143
+ */
144
+static inline int clear_named_setting ( struct config_context *context,
145
+					const char *name ) {
146
+	return set_named_setting ( context, name, NULL );
147
+}
106 148
 
107 149
 #endif /* _GPXE_SETTINGS_H */

Завантаження…
Відмінити
Зберегти