瀏覽代碼

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
共有 4 個檔案被更改,包括 74 行新增37 行删除
  1. 18
    22
      src/core/settings.c
  2. 5
    4
      src/hci/commands/nvo_cmd.c
  3. 4
    6
      src/hci/tui/settings_ui.c
  4. 47
    5
      src/include/gpxe/settings.h

+ 18
- 22
src/core/settings.c 查看文件

116
 }
116
 }
117
 
117
 
118
 /**
118
 /**
119
- * Show value of setting
119
+ * Show value of named setting
120
  *
120
  *
121
  * @v context		Configuration context
121
  * @v context		Configuration context
122
  * @v name		Configuration setting name
122
  * @v name		Configuration setting name
124
  * @v len		Length of buffer
124
  * @v len		Length of buffer
125
  * @ret rc		Return status code
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
 	struct config_setting *setting;
129
 	struct config_setting *setting;
130
 	struct config_setting tmp_setting;
130
 	struct config_setting tmp_setting;
131
 
131
 
132
 	setting = find_or_build_config_setting ( name, &tmp_setting );
132
 	setting = find_or_build_config_setting ( name, &tmp_setting );
133
 	if ( ! setting )
133
 	if ( ! setting )
134
 		return -ENOENT;
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
  * @v context		Configuration context
141
  * @v context		Configuration context
142
  * @v name		Configuration setting name
142
  * @v name		Configuration setting name
143
  * @v value		Setting value (as a string)
143
  * @v value		Setting value (as a string)
144
  * @ret rc		Return status code
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
 	struct config_setting *setting;
148
 	struct config_setting *setting;
149
 	struct config_setting tmp_setting;
149
 	struct config_setting tmp_setting;
150
 
150
 
155
 }
155
 }
156
 
156
 
157
 /**
157
 /**
158
- * Clear setting
158
+ * Set value of setting
159
  *
159
  *
160
  * @v context		Configuration context
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
  * @ret rc		Return status code
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
 		      const char *value ) {
256
 		      const char *value ) {
260
 	struct dhcp_option *option;
257
 	struct dhcp_option *option;
261
 	struct in_addr ipv4;
258
 	struct in_addr ipv4;
262
-	int rc;
263
 	
259
 	
264
 	if ( inet_aton ( value, &ipv4 ) == 0 )
260
 	if ( inet_aton ( value, &ipv4 ) == 0 )
265
 		return -EINVAL;
261
 		return -EINVAL;

+ 5
- 4
src/hci/commands/nvo_cmd.c 查看文件

27
 	}
27
 	}
28
 
28
 
29
 	dummy_context.options = ugly_nvo_hack->options;
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
 		printf ( "Could not find \"%s\": %s\n",
32
 		printf ( "Could not find \"%s\": %s\n",
33
 			 argv[1], strerror ( -rc ) );
33
 			 argv[1], strerror ( -rc ) );
34
 		return 1;
34
 		return 1;
59
 	}
59
 	}
60
 
60
 
61
 	dummy_context.options = ugly_nvo_hack->options;
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
 		printf ( "Could not set \"%s\"=\"%s\": %s\n",
64
 		printf ( "Could not set \"%s\"=\"%s\": %s\n",
64
 			 argv[1], argv[2], strerror ( -rc ) );
65
 			 argv[1], argv[2], strerror ( -rc ) );
65
 		return 1;
66
 		return 1;
94
 	}
95
 	}
95
 
96
 
96
 	dummy_context.options = ugly_nvo_hack->options;
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
 		printf ( "Could not clear \"%s\": %s\n",
99
 		printf ( "Could not clear \"%s\": %s\n",
99
 			 argv[1], strerror ( -rc ) );
100
 			 argv[1], strerror ( -rc ) );
100
 		return 1;
101
 		return 1;

+ 4
- 6
src/hci/tui/settings_ui.c 查看文件

91
 	widget->editing = 0;
91
 	widget->editing = 0;
92
 
92
 
93
 	/* Read current setting value */
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
 		widget->value[0] = '\0';
96
 		widget->value[0] = '\0';
98
 	}	
97
 	}	
99
 
98
 
110
  * @v widget		Setting widget
109
  * @v widget		Setting widget
111
  */
110
  */
112
 static int save_setting ( struct setting_widget *widget ) {
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
 				if ( ( rc = save_setting ( &widget ) ) != 0 ) {
250
 				if ( ( rc = save_setting ( &widget ) ) != 0 ) {
253
 					alert ( " Could not set %s: %s ",
251
 					alert ( " Could not set %s: %s ",
254
 						widget.setting->name,
252
 						widget.setting->name,
255
-						strerror ( -rc ) );
253
+						strerror ( rc ) );
256
 				}
254
 				}
257
 				/* Fall through */
255
 				/* Fall through */
258
 			case 0x03: /* Ctrl-C */
256
 			case 0x03: /* Ctrl-C */

+ 47
- 5
src/include/gpxe/settings.h 查看文件

96
 /** Declare a configuration setting */
96
 /** Declare a configuration setting */
97
 #define	__config_setting __table ( config_settings, 01 )
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
 			 const char *value );
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
 #endif /* _GPXE_SETTINGS_H */
149
 #endif /* _GPXE_SETTINGS_H */

Loading…
取消
儲存