Просмотр исходного кода

[settings] Display canonical setting name in output of "show" command

Enable the "show" command to display the full, canonicalised name of
the fetched setting.  For example:

  iPXE> show mac
  net0/mac:hex = 52:54:00:12:34:56

  iPXE> dhcp && show ip
  DHCP (net0 52:54:00:12:34:56)... ok
  net0.dhcp/ip:ipv4 = 10.0.0.168

  iPXE> show net0/6
  net0.dhcp/dns:ipv4 = 10.0.0.6

Inspired-by: Glenn Brown <glenn@myri.com>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 13 лет назад
Родитель
Сommit
5fbd0207b2
3 измененных файлов: 50 добавлений и 22 удалений
  1. 41
    17
      src/core/settings.c
  2. 6
    4
      src/hci/commands/nvo_cmd.c
  3. 3
    1
      src/include/ipxe/settings.h

+ 41
- 17
src/core/settings.c Просмотреть файл

@@ -1007,7 +1007,6 @@ parse_setting_name ( const char *name,
1007 1007
 	char *setting_name;
1008 1008
 	char *type_name;
1009 1009
 	struct setting *named_setting;
1010
-	unsigned int tag;
1011 1010
 
1012 1011
 	/* Set defaults */
1013 1012
 	*settings = &settings_root;
@@ -1038,16 +1037,14 @@ parse_setting_name ( const char *name,
1038 1037
 	}
1039 1038
 
1040 1039
 	/* Identify setting */
1041
-	if ( ( named_setting = find_setting ( setting_name ) ) != NULL ) {
1040
+	setting->tag = parse_setting_tag ( *settings, setting_name );
1041
+	setting->name = setting_name;
1042
+	for_each_table_entry ( named_setting, SETTINGS ) {
1042 1043
 		/* Matches a defined named setting; use that setting */
1043
-		memcpy ( setting, named_setting, sizeof ( *setting ) );
1044
-	} else if ( ( tag = parse_setting_tag ( *settings,
1045
-						setting_name ) ) != 0 ) {
1046
-		/* Is a valid numeric tag; use the tag */
1047
-		setting->tag = tag;
1048
-	} else {
1049
-		/* Use the arbitrary name */
1050
-		setting->name = setting_name;
1044
+		if ( setting_cmp ( named_setting, setting ) == 0 ) {
1045
+			memcpy ( setting, named_setting, sizeof ( *setting ) );
1046
+			break;
1047
+		}
1051 1048
 	}
1052 1049
 
1053 1050
 	/* Identify setting type, if specified */
@@ -1076,30 +1073,57 @@ int storef_named_setting ( const char *name, const char *value ) {
1076 1073
 	char tmp_name[ strlen ( name ) + 1 ];
1077 1074
 	int rc;
1078 1075
 
1076
+	/* Parse setting name */
1079 1077
 	if ( ( rc = parse_setting_name ( name, autovivify_child_settings,
1080 1078
 					 &settings, &setting, tmp_name )) != 0)
1081 1079
 		return rc;
1082
-	return storef_setting ( settings, &setting, value );
1080
+
1081
+	/* Store setting */
1082
+	if ( ( rc = storef_setting ( settings, &setting, value ) ) != 0 )
1083
+		return rc;
1084
+
1085
+	return 0;
1083 1086
 }
1084 1087
 
1085 1088
 /**
1086 1089
  * Fetch and format value of named setting
1087 1090
  *
1088 1091
  * @v name		Name of setting
1089
- * @v buf		Buffer to contain formatted value
1090
- * @v len		Length of buffer
1092
+ * @v name_buf		Buffer to contain canonicalised name
1093
+ * @v name_len		Length of canonicalised name buffer
1094
+ * @v value_buf		Buffer to contain formatted value
1095
+ * @v value_len		Length of formatted value buffer
1091 1096
  * @ret len		Length of formatted value, or negative error
1092 1097
  */
1093
-int fetchf_named_setting ( const char *name, char *buf, size_t len ) {
1098
+int fetchf_named_setting ( const char *name,
1099
+			   char *name_buf, size_t name_len,
1100
+			   char *value_buf, size_t value_len ) {
1094 1101
 	struct settings *settings;
1095 1102
 	struct setting setting;
1103
+	struct settings *origin;
1104
+	const char *origin_name;
1096 1105
 	char tmp_name[ strlen ( name ) + 1 ];
1097 1106
 	int rc;
1098 1107
 
1108
+	/* Parse setting name */
1099 1109
 	if ( ( rc = parse_setting_name ( name, find_child_settings,
1100 1110
 					 &settings, &setting, tmp_name )) != 0)
1101 1111
 		return rc;
1102
-	return fetchf_setting ( settings, &setting, buf, len );
1112
+
1113
+	/* Fetch setting */
1114
+	if ( ( rc = fetchf_setting ( settings, &setting, value_buf,
1115
+				     value_len ) ) < 0 )
1116
+		return rc;
1117
+
1118
+	/* Construct setting name */
1119
+	origin = fetch_setting_origin ( settings, &setting );
1120
+	assert ( origin != NULL );
1121
+	origin_name = settings_name ( origin );
1122
+	snprintf ( name_buf, name_len, "%s%s%s:%s",
1123
+		   origin_name, ( origin_name[0] ? "/" : "" ),
1124
+		   setting.name, setting.type->name );
1125
+
1126
+	return 0;
1103 1127
 }
1104 1128
 
1105 1129
 /******************************************************************************
@@ -1597,7 +1621,7 @@ char * expand_settings ( const char *string ) {
1597 1621
 		tail = ( end + 1 );
1598 1622
 
1599 1623
 		/* Determine setting length */
1600
-		setting_len = fetchf_named_setting ( name, NULL, 0 );
1624
+		setting_len = fetchf_named_setting ( name, NULL, 0, NULL, 0 );
1601 1625
 		if ( setting_len < 0 )
1602 1626
 			setting_len = 0; /* Treat error as empty setting */
1603 1627
 
@@ -1606,7 +1630,7 @@ char * expand_settings ( const char *string ) {
1606 1630
 			char setting_buf[ setting_len + 1 ];
1607 1631
 
1608 1632
 			setting_buf[0] = '\0';
1609
-			fetchf_named_setting ( name, setting_buf,
1633
+			fetchf_named_setting ( name, NULL, 0, setting_buf,
1610 1634
 					       sizeof ( setting_buf ) );
1611 1635
 
1612 1636
 			/* Construct expanded string and discard old string */

+ 6
- 4
src/hci/commands/nvo_cmd.c Просмотреть файл

@@ -54,7 +54,8 @@ static struct command_descriptor show_cmd =
54 54
 static int show_exec ( int argc, char **argv ) {
55 55
 	struct show_options opts;
56 56
 	const char *name;
57
-	char buf[256];
57
+	char name_buf[32];
58
+	char value_buf[256];
58 59
 	int rc;
59 60
 
60 61
 	/* Parse options */
@@ -65,15 +66,16 @@ static int show_exec ( int argc, char **argv ) {
65 66
 	name = argv[optind];
66 67
 
67 68
 	/* Fetch setting */
68
-	if ( ( rc = fetchf_named_setting ( name, buf,
69
-					   sizeof ( buf ) ) ) < 0 ) {
69
+	if ( ( rc = fetchf_named_setting ( name, name_buf, sizeof ( name_buf ),
70
+					   value_buf,
71
+					   sizeof ( value_buf ) ) ) < 0 ) {
70 72
 		printf ( "Could not find \"%s\": %s\n",
71 73
 			 name, strerror ( rc ) );
72 74
 		return rc;
73 75
 	}
74 76
 
75 77
 	/* Print setting value */
76
-	printf ( "%s = %s\n", name, buf );
78
+	printf ( "%s = %s\n", name_buf, value_buf );
77 79
 
78 80
 	return 0;
79 81
 }

+ 3
- 1
src/include/ipxe/settings.h Просмотреть файл

@@ -255,7 +255,9 @@ extern int storef_setting ( struct settings *settings,
255 255
 			    struct setting *setting,
256 256
 			    const char *value );
257 257
 extern int storef_named_setting ( const char *name, const char *value );
258
-extern int fetchf_named_setting ( const char *name, char *buf, size_t len );
258
+extern int fetchf_named_setting ( const char *name, char *name_buf,
259
+				  size_t name_len, char *value_buf,
260
+				  size_t value_len );
259 261
 extern char * expand_settings ( const char *string );
260 262
 
261 263
 extern struct setting_type setting_type_string __setting_type;

Загрузка…
Отмена
Сохранить