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

[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 роки тому
джерело
коміт
652abb6706
1 змінених файлів з 56 додано та 40 видалено
  1. 56
    40
      src/hci/commands/nvo_cmd.c

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

@@ -55,31 +55,44 @@ static struct command_descriptor show_cmd =
55 55
  */
56 56
 static int show_exec ( int argc, char **argv ) {
57 57
 	struct show_options opts;
58
-	const char *name;
58
+	struct named_setting setting;
59
+	struct settings *origin;
59 60
 	char name_buf[32];
60
-	char value_buf[256];
61
+	char *value;
61 62
 	int rc;
62 63
 
63 64
 	/* Parse options */
64 65
 	if ( ( rc = parse_options ( argc, argv, &show_cmd, &opts ) ) != 0 )
65
-		return rc;
66
+		goto err_parse_options;
66 67
 
67 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 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 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 98
 /** "set", "clear", and "read" options */
@@ -109,10 +122,10 @@ static struct command_descriptor clear_read_cmd =
109 122
  */
110 123
 static int set_core_exec ( int argc, char **argv,
111 124
 			   struct command_descriptor *cmd,
112
-			   int ( * get_value ) ( const char *name,
125
+			   int ( * get_value ) ( struct named_setting *setting,
113 126
 						 char **args, char **value ) ) {
114 127
 	struct set_core_options opts;
115
-	const char *name;
128
+	struct named_setting setting;
116 129
 	char *value;
117 130
 	int rc;
118 131
 
@@ -121,26 +134,30 @@ static int set_core_exec ( int argc, char **argv,
121 134
 		goto err_parse_options;
122 135
 
123 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 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 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 154
 		goto err_store;
136 155
 	}
137 156
 
138
-	free ( value );
139
-	return 0;
140
-
141 157
  err_store:
142 158
 	free ( value );
143 159
  err_get_value:
160
+ err_parse_setting:
144 161
  err_parse_options:
145 162
 	return rc;
146 163
 }
@@ -148,12 +165,13 @@ static int set_core_exec ( int argc, char **argv,
148 165
 /**
149 166
  * Get setting value for "set" command
150 167
  *
151
- * @v name		Setting name
168
+ * @v setting		Named setting
152 169
  * @v args		Remaining arguments
153 170
  * @ret value		Setting value
154 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 176
 	*value = concat_args ( args );
159 177
 	if ( ! *value )
@@ -176,13 +194,13 @@ static int set_exec ( int argc, char **argv ) {
176 194
 /**
177 195
  * Get setting value for "clear" command
178 196
  *
179
- * @v name		Setting name
197
+ * @v setting		Named setting
180 198
  * @v args		Remaining arguments
181 199
  * @ret value		Setting value
182 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 205
 	*value = NULL;
188 206
 	return 0;
@@ -202,29 +220,27 @@ static int clear_exec ( int argc, char **argv ) {
202 220
 /**
203 221
  * Get setting value for "read" command
204 222
  *
205
- * @v name		Setting name
223
+ * @v setting		Named setting
206 224
  * @v args		Remaining arguments
207 225
  * @ret value		Setting value
208 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 230
 	char *existing;
212 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 238
 	/* Read new value */
219 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 243
 	free ( existing );
227
- err_existing:
228 244
 	return rc;
229 245
 }
230 246
 

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