Bladeren bron

[Settings] Allow named settings to have their type specified explicitly

Allow setting names such as "ip:hex".
tags/v0.9.4
Michael Brown 16 jaren geleden
bovenliggende
commit
af1c6b869c
1 gewijzigde bestanden met toevoegingen van 48 en 45 verwijderingen
  1. 48
    45
      src/core/settings.c

+ 48
- 45
src/core/settings.c Bestand weergeven

@@ -87,49 +87,54 @@ static struct config_setting * find_config_setting ( const char *name ) {
87 87
  * Find or build configuration setting
88 88
  *
89 89
  * @v name		Name
90
- * @v tmp_setting	Temporary buffer for constructing a setting
91
- * @ret setting		Configuration setting, or NULL
90
+ * @v setting		Buffer to fill in with setting
91
+ * @ret rc		Return status code
92 92
  *
93 93
  * Find setting if it exists.  If it doesn't exist, but the name is of
94 94
  * the form "<num>:<type>" (e.g. "12:string"), then construct a
95 95
  * setting for that tag and data type, and return it.  The constructed
96
- * setting will be placed in the temporary buffer.
96
+ * setting will be placed in the buffer.
97 97
  */
98
-static struct config_setting *
99
-find_or_build_config_setting ( const char *name,
100
-			       struct config_setting *tmp_setting ) {
101
-	struct config_setting *setting;
102
-	char *separator;
103
-
104
-	/* Look in the list of registered settings first */
105
-	setting = find_config_setting ( name );
106
-	if ( setting )
107
-		return setting;
108
-
109
-	/* If name is of the form "<num>:<type>", try to construct a setting */
110
-	setting = tmp_setting;
98
+static int find_or_build_config_setting ( const char *name,
99
+					  struct config_setting *setting ) {
100
+	struct config_setting *known_setting;
101
+	char tmp_name[ strlen ( name ) + 1 ];
102
+	char *qualifier;
103
+	char *tmp;
104
+
105
+	/* Set defaults */
111 106
 	memset ( setting, 0, sizeof ( *setting ) );
112 107
 	setting->name = name;
113
-	for ( separator = ( char * ) name ; 1 ; separator++ ) {
114
-		setting->tag = ( ( setting->tag << 8 ) |
115
-				 strtoul ( separator, &separator, 0 ) );
116
-		if ( *separator != '.' )
117
-			break;
108
+	setting->type = &config_setting_type_hex;
109
+
110
+	/* Strip qualifier, if present */
111
+	memcpy ( tmp_name, name, sizeof ( tmp_name ) );
112
+	if ( ( qualifier = strchr ( tmp_name, ':' ) ) != NULL )
113
+		*(qualifier++) = 0;
114
+
115
+	/* If we recognise the name of the setting, use it */
116
+	if ( ( known_setting = find_config_setting ( tmp_name ) ) != NULL ) {
117
+		memcpy ( setting, known_setting, sizeof ( *setting ) );
118
+	} else {
119
+		/* Otherwise, try to interpret as a numerical setting */
120
+		for ( tmp = tmp_name ; 1 ; tmp++ ) {
121
+			setting->tag = ( ( setting->tag << 8 ) |
122
+					 strtoul ( tmp, &tmp, 0 ) );
123
+			if ( *tmp != '.' )
124
+				break;
125
+		}
126
+		if ( *tmp != 0 )
127
+			return -EINVAL;
118 128
 	}
119 129
 
120
-	switch ( *separator ) {
121
-	case ':' :
122
-		setting->type = find_config_setting_type ( separator + 1 );
123
-		break;
124
-	case '\0' :
125
-		setting->type = &config_setting_type_hex;
126
-		break;
127
-	default :
128
-		break;
130
+	/* Apply qualifier, if present */
131
+	if ( qualifier ) {
132
+		setting->type = find_config_setting_type ( qualifier );
133
+		if ( ! setting->type )
134
+			return -EINVAL;
129 135
 	}
130
-	if ( ! setting->type )
131
-		return NULL;
132
-	return setting;
136
+
137
+	return 0;
133 138
 }
134 139
 
135 140
 /**
@@ -143,13 +148,12 @@ find_or_build_config_setting ( const char *name,
143 148
  */
144 149
 int show_named_setting ( struct config_context *context, const char *name,
145 150
 			 char *buf, size_t len ) {
146
-	struct config_setting *setting;
147
-	struct config_setting tmp_setting;
151
+	struct config_setting setting;
152
+	int rc;
148 153
 
149
-	setting = find_or_build_config_setting ( name, &tmp_setting );
150
-	if ( ! setting )
151
-		return -ENOENT;
152
-	return show_setting ( context, setting, buf, len );
154
+	if ( ( rc = find_or_build_config_setting ( name, &setting ) ) != 0 )
155
+		return rc;
156
+	return show_setting ( context, &setting, buf, len );
153 157
 }
154 158
 
155 159
 /**
@@ -162,13 +166,12 @@ int show_named_setting ( struct config_context *context, const char *name,
162 166
  */
163 167
 int set_named_setting ( struct config_context *context, const char *name,
164 168
 			const char *value ) {
165
-	struct config_setting *setting;
166
-	struct config_setting tmp_setting;
169
+	struct config_setting setting;
170
+	int rc;
167 171
 
168
-	setting = find_or_build_config_setting ( name, &tmp_setting );
169
-	if ( ! setting )
170
-		return -ENOENT;
171
-	return setting->type->set ( context, setting, value );
172
+	if ( ( rc = find_or_build_config_setting ( name, &setting ) ) != 0 )
173
+		return rc;
174
+	return set_setting ( context, &setting, value );
172 175
 }
173 176
 
174 177
 /**

Laden…
Annuleren
Opslaan