Browse Source

[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 years ago
parent
commit
25ec56e0ec
4 changed files with 59 additions and 9 deletions
  1. 48
    5
      src/core/settings.c
  2. 2
    1
      src/hci/commands/menu_cmd.c
  3. 2
    1
      src/hci/commands/nvo_cmd.c
  4. 7
    2
      src/include/ipxe/settings.h

+ 48
- 5
src/core/settings.c View File

180
 	if ( len > generic->data_len )
180
 	if ( len > generic->data_len )
181
 		len = generic->data_len;
181
 		len = generic->data_len;
182
 	memcpy ( data, generic_setting_data ( generic ), len );
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
 	return generic->data_len;
188
 	return generic->data_len;
184
 }
189
 }
185
 
190
 
614
 	if ( setting_applies ( settings, setting ) &&
619
 	if ( setting_applies ( settings, setting ) &&
615
 	     ( ( ret = settings->op->fetch ( settings, setting,
620
 	     ( ( ret = settings->op->fetch ( settings, setting,
616
 					     data, len ) ) >= 0 ) ) {
621
 					     data, len ) ) >= 0 ) ) {
622
+		/* Record origin, if applicable */
617
 		if ( origin )
623
 		if ( origin )
618
 			*origin = settings;
624
 			*origin = settings;
625
+		/* Default to string setting type, if not yet specified */
626
+		if ( ! setting->type )
627
+			setting->type = &setting_type_string;
619
 		return ret;
628
 		return ret;
620
 	}
629
 	}
621
 
630
 
1132
  * @v get_child		Function to find or create child settings block
1141
  * @v get_child		Function to find or create child settings block
1133
  * @v settings		Settings block to fill in
1142
  * @v settings		Settings block to fill in
1134
  * @v setting		Setting to fill in
1143
  * @v setting		Setting to fill in
1144
+ * @v default_type	Default type to use, if none specified
1135
  * @v tmp_name		Buffer for copy of setting name
1145
  * @v tmp_name		Buffer for copy of setting name
1136
  * @ret rc		Return status code
1146
  * @ret rc		Return status code
1137
  *
1147
  *
1147
 		     struct settings * ( * get_child ) ( struct settings *,
1157
 		     struct settings * ( * get_child ) ( struct settings *,
1148
 							 const char * ),
1158
 							 const char * ),
1149
 		     struct settings **settings, struct setting *setting,
1159
 		     struct settings **settings, struct setting *setting,
1160
+		     struct setting_type *default_type,
1150
 		     char *tmp_name ) {
1161
 		     char *tmp_name ) {
1151
 	char *settings_name;
1162
 	char *settings_name;
1152
 	char *setting_name;
1163
 	char *setting_name;
1157
 	*settings = &settings_root;
1168
 	*settings = &settings_root;
1158
 	memset ( setting, 0, sizeof ( *setting ) );
1169
 	memset ( setting, 0, sizeof ( *setting ) );
1159
 	setting->name = "";
1170
 	setting->name = "";
1160
-	setting->type = &setting_type_string;
1171
+	setting->type = default_type;
1161
 
1172
 
1162
 	/* Split name into "[settings_name/]setting_name[:type_name]" */
1173
 	/* Split name into "[settings_name/]setting_name[:type_name]" */
1163
 	strcpy ( tmp_name, name );
1174
 	strcpy ( tmp_name, name );
1226
 			  setting->name, setting->type->name );
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
  * Parse and store value of named setting
1270
  * Parse and store value of named setting
1231
  *
1271
  *
1232
  * @v name		Name of setting
1272
  * @v name		Name of setting
1273
+ * @v default_type	Default type to use, if none specified
1233
  * @v value		Formatted setting data, or NULL
1274
  * @v value		Formatted setting data, or NULL
1234
  * @ret rc		Return status code
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
 	struct settings *settings;
1279
 	struct settings *settings;
1238
 	struct setting setting;
1280
 	struct setting setting;
1239
 	char tmp_name[ strlen ( name ) + 1 ];
1281
 	char tmp_name[ strlen ( name ) + 1 ];
1241
 
1283
 
1242
 	/* Parse setting name */
1284
 	/* Parse setting name */
1243
 	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
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
 		return rc;
1288
 		return rc;
1246
 
1289
 
1247
 	/* Store setting */
1290
 	/* Store setting */
1272
 	int rc;
1315
 	int rc;
1273
 
1316
 
1274
 	/* Parse setting name */
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
 		return rc;
1320
 		return rc;
1278
 
1321
 
1279
 	/* Fetch setting */
1322
 	/* Fetch setting */

+ 2
- 1
src/hci/commands/menu_cmd.c View File

249
 		goto err_show_menu;
249
 		goto err_show_menu;
250
 
250
 
251
 	/* Store setting */
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
 		printf ( "Could not store \"%s\": %s\n",
254
 		printf ( "Could not store \"%s\": %s\n",
254
 			 setting, strerror ( rc ) );
255
 			 setting, strerror ( rc ) );
255
 		goto err_store;
256
 		goto err_store;

+ 2
- 1
src/hci/commands/nvo_cmd.c View File

127
 		goto err_get_value;
127
 		goto err_get_value;
128
 
128
 
129
 	/* Determine total length of command line */
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
 		printf ( "Could not %s \"%s\": %s\n",
132
 		printf ( "Could not %s \"%s\": %s\n",
132
 			 argv[0], name, strerror ( rc ) );
133
 			 argv[0], name, strerror ( rc ) );
133
 		goto err_store;
134
 		goto err_store;

+ 7
- 2
src/include/ipxe/settings.h View File

280
 extern int storef_setting ( struct settings *settings,
280
 extern int storef_setting ( struct settings *settings,
281
 			    struct setting *setting,
281
 			    struct setting *setting,
282
 			    const char *value );
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
 extern int fetchf_named_setting ( const char *name, char *name_buf,
289
 extern int fetchf_named_setting ( const char *name, char *name_buf,
285
 				  size_t name_len, char *value_buf,
290
 				  size_t name_len, char *value_buf,
286
 				  size_t value_len );
291
 				  size_t value_len );
366
  * @ret rc		Return status code
371
  * @ret rc		Return status code
367
  */
372
  */
368
 static inline int delete_named_setting ( const char *name ) {
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…
Cancel
Save