Kaynağa Gözat

[settings] Use a generic setting's own type as its default type

When fetching a named setting using a name that does not explicitly
specify a type, default to using the type stored when the setting was
created, rather than always defaulting to "string".  This allows the
behaviour of user-defined settings to match the behaviour of
predefined settings (which have a sensible default type).

For example:

  set server:ipv4 192.168.0.1
  echo ${server}

will now print "192.168.0.1", rather than trying to print out the raw
IPv4 address bytes as a string.

The downside of this change is that existing tricks for printing
special characters within scripts may require (backwards-compatible)
modification.  For example, the "clear screen" sequence:

  set esc:hex 1b
  set cls ${esc}[2J
  echo ${cls}

will now have to become

  set esc:hex 1b
  set cls ${esc:string}[2J  # Must now explicitly specify ":string"
  echo ${cls}

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 yıl önce
ebeveyn
işleme
25ec56e0ec

+ 48
- 5
src/core/settings.c Dosyayı Görüntüle

@@ -180,6 +180,11 @@ int generic_settings_fetch ( struct settings *settings,
180 180
 	if ( len > generic->data_len )
181 181
 		len = generic->data_len;
182 182
 	memcpy ( data, generic_setting_data ( generic ), len );
183
+
184
+	/* Set setting type, if not yet specified */
185
+	if ( ! setting->type )
186
+		setting->type = generic->setting.type;
187
+
183 188
 	return generic->data_len;
184 189
 }
185 190
 
@@ -614,8 +619,12 @@ static int fetch_setting_and_origin ( struct settings *settings,
614 619
 	if ( setting_applies ( settings, setting ) &&
615 620
 	     ( ( ret = settings->op->fetch ( settings, setting,
616 621
 					     data, len ) ) >= 0 ) ) {
622
+		/* Record origin, if applicable */
617 623
 		if ( origin )
618 624
 			*origin = settings;
625
+		/* Default to string setting type, if not yet specified */
626
+		if ( ! setting->type )
627
+			setting->type = &setting_type_string;
619 628
 		return ret;
620 629
 	}
621 630
 
@@ -1132,6 +1141,7 @@ static struct setting_type * find_setting_type ( const char *name ) {
1132 1141
  * @v get_child		Function to find or create child settings block
1133 1142
  * @v settings		Settings block to fill in
1134 1143
  * @v setting		Setting to fill in
1144
+ * @v default_type	Default type to use, if none specified
1135 1145
  * @v tmp_name		Buffer for copy of setting name
1136 1146
  * @ret rc		Return status code
1137 1147
  *
@@ -1147,6 +1157,7 @@ parse_setting_name ( const char *name,
1147 1157
 		     struct settings * ( * get_child ) ( struct settings *,
1148 1158
 							 const char * ),
1149 1159
 		     struct settings **settings, struct setting *setting,
1160
+		     struct setting_type *default_type,
1150 1161
 		     char *tmp_name ) {
1151 1162
 	char *settings_name;
1152 1163
 	char *setting_name;
@@ -1157,7 +1168,7 @@ parse_setting_name ( const char *name,
1157 1168
 	*settings = &settings_root;
1158 1169
 	memset ( setting, 0, sizeof ( *setting ) );
1159 1170
 	setting->name = "";
1160
-	setting->type = &setting_type_string;
1171
+	setting->type = default_type;
1161 1172
 
1162 1173
 	/* Split name into "[settings_name/]setting_name[:type_name]" */
1163 1174
 	strcpy ( tmp_name, name );
@@ -1226,14 +1237,45 @@ int setting_name ( struct settings *settings, struct setting *setting,
1226 1237
 			  setting->name, setting->type->name );
1227 1238
 }
1228 1239
 
1240
+/**
1241
+ * Store value of named setting
1242
+ *
1243
+ * @v name		Name of setting
1244
+ * @v default_type	Default type to use, if none specified
1245
+ * @v data		Setting data, or NULL to clear setting
1246
+ * @v len		Length of setting data
1247
+ * @ret rc		Return status code
1248
+ */
1249
+int store_named_setting ( const char *name, struct setting_type *default_type,
1250
+			  const void *data, size_t len ) {
1251
+	struct settings *settings;
1252
+	struct setting setting;
1253
+	char tmp_name[ strlen ( name ) + 1 ];
1254
+	int rc;
1255
+
1256
+	/* Parse setting name */
1257
+	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
1258
+					 &settings, &setting, default_type,
1259
+					 tmp_name ) ) != 0 )
1260
+		return rc;
1261
+
1262
+	/* Store setting */
1263
+	if ( ( rc = store_setting ( settings, &setting, data, len ) ) != 0 )
1264
+		return rc;
1265
+
1266
+	return 0;
1267
+}
1268
+
1229 1269
 /**
1230 1270
  * Parse and store value of named setting
1231 1271
  *
1232 1272
  * @v name		Name of setting
1273
+ * @v default_type	Default type to use, if none specified
1233 1274
  * @v value		Formatted setting data, or NULL
1234 1275
  * @ret rc		Return status code
1235 1276
  */
1236
-int storef_named_setting ( const char *name, const char *value ) {
1277
+int storef_named_setting ( const char *name, struct setting_type *default_type,
1278
+			   const char *value ) {
1237 1279
 	struct settings *settings;
1238 1280
 	struct setting setting;
1239 1281
 	char tmp_name[ strlen ( name ) + 1 ];
@@ -1241,7 +1283,8 @@ int storef_named_setting ( const char *name, const char *value ) {
1241 1283
 
1242 1284
 	/* Parse setting name */
1243 1285
 	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
1244
-					 &settings, &setting, tmp_name )) != 0)
1286
+					 &settings, &setting, default_type,
1287
+					 tmp_name ) ) != 0 )
1245 1288
 		return rc;
1246 1289
 
1247 1290
 	/* Store setting */
@@ -1272,8 +1315,8 @@ int fetchf_named_setting ( const char *name,
1272 1315
 	int rc;
1273 1316
 
1274 1317
 	/* Parse setting name */
1275
-	if ( ( rc = parse_setting_name ( name, find_child_settings,
1276
-					 &settings, &setting, tmp_name )) != 0)
1318
+	if ( ( rc = parse_setting_name ( name, find_child_settings, &settings,
1319
+					 &setting, NULL, tmp_name ) ) != 0 )
1277 1320
 		return rc;
1278 1321
 
1279 1322
 	/* Fetch setting */

+ 2
- 1
src/hci/commands/menu_cmd.c Dosyayı Görüntüle

@@ -249,7 +249,8 @@ static int choose_exec ( int argc, char **argv ) {
249 249
 		goto err_show_menu;
250 250
 
251 251
 	/* Store setting */
252
-	if ( ( rc = storef_named_setting ( setting, item->label ) ) != 0 ) {
252
+	if ( ( rc = storef_named_setting ( setting, &setting_type_string,
253
+					   item->label ) ) != 0 ) {
253 254
 		printf ( "Could not store \"%s\": %s\n",
254 255
 			 setting, strerror ( rc ) );
255 256
 		goto err_store;

+ 2
- 1
src/hci/commands/nvo_cmd.c Dosyayı Görüntüle

@@ -127,7 +127,8 @@ static int set_core_exec ( int argc, char **argv,
127 127
 		goto err_get_value;
128 128
 
129 129
 	/* Determine total length of command line */
130
-	if ( ( rc = storef_named_setting ( name, value ) ) != 0 ) {
130
+	if ( ( rc = storef_named_setting ( name, &setting_type_string,
131
+					   value ) ) != 0 ) {
131 132
 		printf ( "Could not %s \"%s\": %s\n",
132 133
 			 argv[0], name, strerror ( rc ) );
133 134
 		goto err_store;

+ 7
- 2
src/include/ipxe/settings.h Dosyayı Görüntüle

@@ -280,7 +280,12 @@ extern int fetchf_setting ( struct settings *settings, struct setting *setting,
280 280
 extern int storef_setting ( struct settings *settings,
281 281
 			    struct setting *setting,
282 282
 			    const char *value );
283
-extern int storef_named_setting ( const char *name, const char *value );
283
+extern int store_named_setting ( const char *name,
284
+				 struct setting_type *default_type,
285
+				 const void *data, size_t len );
286
+extern int storef_named_setting ( const char *name,
287
+				  struct setting_type *default_type,
288
+				  const char *value );
284 289
 extern int fetchf_named_setting ( const char *name, char *name_buf,
285 290
 				  size_t name_len, char *value_buf,
286 291
 				  size_t value_len );
@@ -366,7 +371,7 @@ static inline int delete_setting ( struct settings *settings,
366 371
  * @ret rc		Return status code
367 372
  */
368 373
 static inline int delete_named_setting ( const char *name ) {
369
-	return storef_named_setting ( name, NULL );
374
+	return store_named_setting ( name, NULL, NULL, 0 );
370 375
 }
371 376
 
372 377
 /**

Loading…
İptal
Kaydet