Browse Source

[settings] Eliminate calls to {fetch,store}f_named_setting() in NVO commands

A deliberate side effect of this commit is that the "read" command
will now preserve the type of the setting, if the setting name
contains no type information.  For example:

  iPXE> set foo:ipv4 192.168.0.1
  iPXE> read foo
  192.168.0.100
  iPXE> show foo
  foo:ipv4 = 192.168.0.100

rather than the arguably unexpected behaviour of resetting the type to
"string".

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 11 years ago
parent
commit
652abb6706
1 changed files with 56 additions and 40 deletions
  1. 56
    40
      src/hci/commands/nvo_cmd.c

+ 56
- 40
src/hci/commands/nvo_cmd.c View File

55
  */
55
  */
56
 static int show_exec ( int argc, char **argv ) {
56
 static int show_exec ( int argc, char **argv ) {
57
 	struct show_options opts;
57
 	struct show_options opts;
58
-	const char *name;
58
+	struct named_setting setting;
59
+	struct settings *origin;
59
 	char name_buf[32];
60
 	char name_buf[32];
60
-	char value_buf[256];
61
+	char *value;
61
 	int rc;
62
 	int rc;
62
 
63
 
63
 	/* Parse options */
64
 	/* Parse options */
64
 	if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
65
 	if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
65
-		return rc;
66
+		goto err_parse_options;
66
 
67
 
67
 	/* Parse setting name */
68
 	/* Parse setting name */
68
-	name = argv[optind];
69
+	if ( ( rc = parse_existing_setting ( argv[optind], &setting ) ) != 0 )
70
+		goto err_parse_setting;
69
 
71
 
70
-	/* Fetch setting */
71
-	if ( ( rc = fetchf_named_setting ( name, name_buf, sizeof ( name_buf ),
72
-					   value_buf,
73
-					   sizeof ( value_buf ) ) ) < 0 ) {
72
+	/* Fetch formatted setting value */
73
+	if ( ( rc = fetchf_setting_copy ( setting.settings, &setting.setting,
74
+					  &value ) ) < 0 ) {
74
 		printf ( "Could not find \"%s\": %s\n",
75
 		printf ( "Could not find \"%s\": %s\n",
75
-			 name, strerror ( rc ) );
76
-		return rc;
76
+			 setting.setting.name, strerror ( rc ) );
77
+		goto err_fetchf;
77
 	}
78
 	}
78
 
79
 
80
+	/* Fetch origin and format fully-qualified name */
81
+	origin = fetch_setting_origin ( setting.settings, &setting.setting );
82
+	assert ( origin != NULL );
83
+	setting_name ( origin, &setting.setting, name_buf, sizeof ( name_buf ));
84
+
79
 	/* Print setting value */
85
 	/* Print setting value */
80
-	printf ( "%s = %s\n", name_buf, value_buf );
86
+	printf ( "%s = %s\n", name_buf, value );
81
 
87
 
82
-	return 0;
88
+	/* Success */
89
+	rc = 0;
90
+
91
+	free ( value );
92
+ err_fetchf:
93
+ err_parse_setting:
94
+ err_parse_options:
95
+	return rc;
83
 }
96
 }
84
 
97
 
85
 /** "set", "clear", and "read" options */
98
 /** "set", "clear", and "read" options */
109
  */
122
  */
110
 static int set_core_exec ( int argc, char **argv,
123
 static int set_core_exec ( int argc, char **argv,
111
 			   struct command_descriptor *cmd,
124
 			   struct command_descriptor *cmd,
112
-			   int ( * get_value ) ( const char *name,
125
+			   int ( * get_value ) ( struct named_setting *setting,
113
 						 char **args, char **value ) ) {
126
 						 char **args, char **value ) ) {
114
 	struct set_core_options opts;
127
 	struct set_core_options opts;
115
-	const char *name;
128
+	struct named_setting setting;
116
 	char *value;
129
 	char *value;
117
 	int rc;
130
 	int rc;
118
 
131
 
121
 		goto err_parse_options;
134
 		goto err_parse_options;
122
 
135
 
123
 	/* Parse setting name */
136
 	/* Parse setting name */
124
-	name = argv[optind];
137
+	if ( ( rc = parse_autovivified_setting ( argv[optind],
138
+						 &setting ) ) != 0 )
139
+		goto err_parse_setting;
125
 
140
 
126
 	/* Parse setting value */
141
 	/* Parse setting value */
127
-	if ( ( rc = get_value ( name, &argv[ optind + 1 ], &value ) ) != 0 )
142
+	if ( ( rc = get_value ( &setting, &argv[ optind + 1 ], &value ) ) != 0 )
128
 		goto err_get_value;
143
 		goto err_get_value;
129
 
144
 
130
-	/* Determine total length of command line */
131
-	if ( ( rc = storef_named_setting ( name, &setting_type_string,
132
-					   value ) ) != 0 ) {
133
-		printf ( "Could not %s \"%s\": %s\n",
134
-			 argv[0], name, strerror ( rc ) );
145
+	/* Apply default type if necessary */
146
+	if ( ! setting.setting.type )
147
+		setting.setting.type = &setting_type_string;
148
+
149
+	/* Store setting */
150
+	if ( ( rc = storef_setting ( setting.settings, &setting.setting,
151
+				     value ) ) != 0 ) {
152
+		printf ( "Could not store \"%s\": %s\n",
153
+			 setting.setting.name, strerror ( rc ) );
135
 		goto err_store;
154
 		goto err_store;
136
 	}
155
 	}
137
 
156
 
138
-	free ( value );
139
-	return 0;
140
-
141
  err_store:
157
  err_store:
142
 	free ( value );
158
 	free ( value );
143
  err_get_value:
159
  err_get_value:
160
+ err_parse_setting:
144
  err_parse_options:
161
  err_parse_options:
145
 	return rc;
162
 	return rc;
146
 }
163
 }
148
 /**
165
 /**
149
  * Get setting value for "set" command
166
  * Get setting value for "set" command
150
  *
167
  *
151
- * @v name		Setting name
168
+ * @v setting		Named setting
152
  * @v args		Remaining arguments
169
  * @v args		Remaining arguments
153
  * @ret value		Setting value
170
  * @ret value		Setting value
154
  * @ret rc		Return status code
171
  * @ret rc		Return status code
155
  */
172
  */
156
-static int set_value ( const char *name __unused, char **args, char **value ) {
173
+static int set_value ( struct named_setting *setting __unused,
174
+		       char **args, char **value ) {
157
 
175
 
158
 	*value = concat_args ( args );
176
 	*value = concat_args ( args );
159
 	if ( ! *value )
177
 	if ( ! *value )
176
 /**
194
 /**
177
  * Get setting value for "clear" command
195
  * Get setting value for "clear" command
178
  *
196
  *
179
- * @v name		Setting name
197
+ * @v setting		Named setting
180
  * @v args		Remaining arguments
198
  * @v args		Remaining arguments
181
  * @ret value		Setting value
199
  * @ret value		Setting value
182
  * @ret rc		Return status code
200
  * @ret rc		Return status code
183
  */
201
  */
184
-static int clear_value ( const char *name __unused, char **args __unused,
185
-			 char **value ) {
202
+static int clear_value ( struct named_setting *setting __unused,
203
+			 char **args __unused, char **value ) {
186
 
204
 
187
 	*value = NULL;
205
 	*value = NULL;
188
 	return 0;
206
 	return 0;
202
 /**
220
 /**
203
  * Get setting value for "read" command
221
  * Get setting value for "read" command
204
  *
222
  *
205
- * @v name		Setting name
223
+ * @v setting		Named setting
206
  * @v args		Remaining arguments
224
  * @v args		Remaining arguments
207
  * @ret value		Setting value
225
  * @ret value		Setting value
208
  * @ret rc		Return status code
226
  * @ret rc		Return status code
209
  */
227
  */
210
-static int read_value ( const char *name, char **args __unused, char **value ) {
228
+static int read_value ( struct named_setting *setting, char **args __unused,
229
+			char **value ) {
211
 	char *existing;
230
 	char *existing;
212
 	int rc;
231
 	int rc;
213
 
232
 
214
-	/* Read existing value */
215
-	if ( ( rc = fetchf_named_setting_copy ( name, &existing ) ) < 0 )
216
-		goto err_existing;
233
+	/* Read existing value, treating errors as equivalent to an
234
+	 * empty initial setting.
235
+	 */
236
+	fetchf_setting_copy ( setting->settings, &setting->setting, &existing );
217
 
237
 
218
 	/* Read new value */
238
 	/* Read new value */
219
 	if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
239
 	if ( ( rc = readline_history ( NULL, existing, NULL, value ) ) != 0 )
220
-		goto err_new;
221
-
222
-	/* Success */
223
-	rc = 0;
240
+		goto err_readline;
224
 
241
 
225
- err_new:
242
+ err_readline:
226
 	free ( existing );
243
 	free ( existing );
227
- err_existing:
228
 	return rc;
244
 	return rc;
229
 }
245
 }
230
 
246
 

Loading…
Cancel
Save