Browse code

- rejecting config declarations with conflicting group name - cfg_declare_int() is extended with min/max support

Miklos Tirpak authored on 28/01/2008 15:35:56
Showing 7 changed files
... ...
@@ -1110,7 +1110,7 @@ assign_stm:
1110 1110
 	;
1111 1111
 cfg_var:
1112 1112
 	ID DOT ID EQUAL NUMBER {
1113
-		if (cfg_declare_int($1, $3, $5, NULL)) {
1113
+		if (cfg_declare_int($1, $3, $5, 0, 0, NULL)) {
1114 1114
 			yyerror("variable cannot be declared");
1115 1115
 		}
1116 1116
 	}
... ...
@@ -1120,7 +1120,7 @@ cfg_var:
1120 1120
 		}
1121 1121
 	}
1122 1122
 	| ID DOT ID EQUAL NUMBER CFG_DESCRIPTION STRING {
1123
-		if (cfg_declare_int($1, $3, $5, $7)) {
1123
+		if (cfg_declare_int($1, $3, $5, 0, 0, $7)) {
1124 1124
 			yyerror("variable cannot be declared");
1125 1125
 		}
1126 1126
 	}
... ...
@@ -122,6 +122,14 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
122 122
 	}
123 123
 
124 124
 	group_name_len = strlen(group_name);
125
+	/* check for duplicates */
126
+	if (cfg_lookup_group(group_name, group_name_len)) {
127
+		LOG(L_ERR, "ERROR: register_cfg_def(): "
128
+			"configuration group has been already declared: %s\n",
129
+			group_name);
130
+		goto error;
131
+	}
132
+
125 133
 	/* create a new group
126 134
 	I will allocate memory in shm mem for the variables later in a single block,
127 135
 	when we know the size of all the registered groups. */
... ...
@@ -152,7 +160,8 @@ error:
152 160
 }
153 161
 
154 162
 /* declares a single variable with integer type */
155
-int cfg_declare_int(char *group_name, char *var_name, int val, char *descr)
163
+int cfg_declare_int(char *group_name, char *var_name,
164
+		int val, int min, int max, char *descr)
156 165
 {
157 166
 	cfg_script_var_t	*var;
158 167
 
... ...
@@ -160,6 +169,8 @@ int cfg_declare_int(char *group_name, char *var_name, int val, char *descr)
160 169
 		return -1;
161 170
 
162 171
 	var->val.i = val;
172
+	var->min = min;
173
+	var->max = max;
163 174
 
164 175
 	return 0;
165 176
 }
... ...
@@ -76,7 +76,8 @@ int cfg_declare(char *group_name, cfg_def_t *def, void *values, int def_size,
76 76
 	((struct cfg_group_##gname *)handle)->var
77 77
 
78 78
 /* declares a single variable with integer type */
79
-int cfg_declare_int(char *group_name, char *var_name, int val, char *descr);
79
+int cfg_declare_int(char *group_name, char *var_name,
80
+		int val, int min, int max, char *descr);
80 81
 
81 82
 /* declares a single variable with str type */
82 83
 int cfg_declare_str(char *group_name, char *var_name, char *val, char *descr);
... ...
@@ -57,26 +57,32 @@ cfg_script_var_t *new_cfg_script_var(char *gname, char *vname, unsigned int type
57 57
 	}
58 58
 
59 59
 	gname_len = strlen(gname);
60
+	vname_len = strlen(vname);
60 61
 	/* the group may have been already declared */
61
-	for (	group = cfg_group;
62
-		group;
63
-		group = group->next
64
-	) {
65
-		if ((group->name_len == gname_len) &&
66
-		(memcmp(group->name, gname, gname_len) == 0)) {
67
-			if (group->dynamic == 0) {
68
-				/* the group has been already declared by a module or by the core */
69
-				LOG(L_ERR, "ERROR: new_cfg_script_var(): "
70
-					"configuration group has been already declared: %s\n",
71
-					gname);
62
+	group = cfg_lookup_group(gname, gname_len);
63
+	if (group) {
64
+		if (group->dynamic == 0) {
65
+			/* the group has been already declared by a module or by the core */
66
+			LOG(L_ERR, "ERROR: new_cfg_script_var(): "
67
+				"configuration group has been already declared: %s\n",
68
+				gname);
69
+			return NULL;
70
+		}
71
+		/* the dynamic group is found */
72
+		/* verify that the variable does not exist */
73
+		for (	var = (cfg_script_var_t *)group->vars;
74
+			var;
75
+			var = var->next
76
+		) {
77
+			if ((var->name_len == vname_len) &&
78
+			(memcmp(var->name, vname, vname_len) == 0)) {
79
+				LOG(L_ERR, "ERROR: new_cfg_script_var(): variable already exists: %s.%s\n",
80
+						gname, vname);
72 81
 				return NULL;
73 82
 			}
74
-			/* the dynamic group is found */
75
-			break;
76 83
 		}
77
-	}
78 84
 
79
-	if (!group) {
85
+	} else {
80 86
 		/* create a new group with NULL values, we will fix it later,
81 87
 		when all the variables are known */
82 88
 		group = cfg_new_group(gname, gname_len,
... ...
@@ -87,21 +93,6 @@ cfg_script_var_t *new_cfg_script_var(char *gname, char *vname, unsigned int type
87 93
 		group->dynamic = 1;
88 94
 	}
89 95
 
90
-	/* verify that the variable does not exist */
91
-	vname_len = strlen(vname);
92
-
93
-	for (	var = (cfg_script_var_t *)group->vars;
94
-		var;
95
-		var = var->next
96
-	) {
97
-		if ((var->name_len == vname_len) &&
98
-		(memcmp(var->name, vname, vname_len) == 0)) {
99
-			LOG(L_ERR, "ERROR: new_cfg_script_var(): variable already exists: %s.%s\n",
100
-					gname, vname);
101
-			return NULL;
102
-		}
103
-	}
104
-
105 96
 	switch (type) {
106 97
 	case CFG_VAR_INT:
107 98
 		group->size = ROUND_INT(group->size);
... ...
@@ -183,6 +174,8 @@ int cfg_script_fixup(cfg_group_t *group, unsigned char *block)
183 174
 		def[i].name = script_var->name;
184 175
 		def[i].type = script_var->type | (script_var->type << CFG_INPUT_SHIFT);
185 176
 		def[i].descr = script_var->descr;
177
+		def[i].min = script_var->min;
178
+		def[i].max = script_var->max;
186 179
 
187 180
 		mapping[i].def = &(def[i]);
188 181
 		mapping[i].name_len = script_var->name_len;
... ...
@@ -43,6 +43,8 @@ typedef struct _cfg_script_var {
43 43
 		str	s;
44 44
 		int	i;
45 45
 	} val;
46
+	int	min;
47
+	int	max;
46 48
 	struct _cfg_script_var	*next;
47 49
 	int	name_len;
48 50
 	char	*name;
... ...
@@ -397,6 +397,22 @@ void cfg_child_destroy(void)
397 397
 	cfg_child_cb = NULL;
398 398
 }
399 399
 
400
+/* searches a group by name */
401
+cfg_group_t *cfg_lookup_group(char *name, int len)
402
+{
403
+	cfg_group_t	*g;
404
+
405
+	for (	g = cfg_group;
406
+		g;
407
+		g = g->next
408
+	)
409
+		if ((g->name_len == len)
410
+		&& (memcmp(g->name, name, len)==0))
411
+			return g;
412
+
413
+	return NULL;
414
+}
415
+
400 416
 /* searches a variable definition by group and variable name */
401 417
 int cfg_lookup_var(str *gname, str *vname,
402 418
 			cfg_group_t **group, cfg_mapping_t **var)
... ...
@@ -248,6 +248,9 @@ static inline void cfg_update_local(void)
248 248
 		if (unlikely(cfg_local != *cfg_global)) \
249 249
 			cfg_update_local(); \
250 250
 	} while(0)
251
+
252
+/* searches a group by name */
253
+cfg_group_t *cfg_lookup_group(char *name, int len);
251 254
 	
252 255
 /* searches a variable definition by group and variable name */
253 256
 int cfg_lookup_var(str *gname, str *vname,