Browse code

cfg framework: @cfg_selected.group added

@cfg_selected.<group_name> returns the selected instance id of the
specified group. If no group instance is selected, i.e. the default
is used, then empty string is returned.

Miklos Tirpak authored on 31/01/2011 11:45:57
Showing 3 changed files
... ...
@@ -60,14 +60,17 @@ static int cfg_new_select(str *gname, str *vname, void **group_p, void **var_p)
60 60
 	if (!sel->gname.s) goto error;
61 61
 	memcpy(sel->gname.s, gname->s, gname->len);
62 62
 	sel->gname.len = gname->len;
63
+	sel->group_p = group_p;
63 64
 
64
-	sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
65
-	if (!sel->vname.s) goto error;
66
-	memcpy(sel->vname.s, vname->s, vname->len);
67
-	sel->vname.len = vname->len;
65
+	if (vname) {
66
+		sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
67
+		if (!sel->vname.s) goto error;
68
+		memcpy(sel->vname.s, vname->s, vname->len);
69
+		sel->vname.len = vname->len;
70
+
71
+		sel->var_p = var_p;
72
+	}
68 73
 
69
-	sel->group_p = group_p;
70
-	sel->var_p = var_p;
71 74
 
72 75
 	sel->next = cfg_non_fixed_selects;
73 76
 	cfg_non_fixed_selects = sel;
... ...
@@ -111,14 +114,23 @@ int cfg_fixup_selects()
111 114
 
112 115
 	for (sel=cfg_non_fixed_selects; sel; sel=sel->next) {
113 116
 
114
-		if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
115
-			LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
116
-				sel->gname.len, sel->gname.s,
117
-				sel->vname.len, sel->vname.s);
118
-			return -1;
117
+		if (sel->var_p) {
118
+			if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
119
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
120
+					sel->gname.len, sel->gname.s,
121
+					sel->vname.len, sel->vname.s);
122
+				return -1;
123
+			}
124
+			*(sel->group_p) = (void *)group;
125
+			*(sel->var_p) = (void *)var;
126
+		} else {
127
+			if (!(group = cfg_lookup_group(sel->gname.s, sel->gname.len))) {
128
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown configuration group: %.*s\n",
129
+					sel->gname.len, sel->gname.s);
130
+				return -1;
131
+			}
132
+			*(sel->group_p) = (void *)group;
119 133
 		}
120
-		*(sel->group_p) = (void *)group;
121
-		*(sel->var_p) = (void *)var;
122 134
 	}
123 135
 	/* the select list is not needed anymore */
124 136
 	cfg_free_selects();
... ...
@@ -360,3 +372,67 @@ int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val)
360 372
 	*val = *(str *)(v2);
361 373
 	return 0;
362 374
 }
375
+
376
+/* return the selected group instance */
377
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg)
378
+{
379
+	cfg_group_t	*group;
380
+	cfg_group_inst_t	*inst;
381
+
382
+	if (msg == NULL) {
383
+		/* fixup call */
384
+
385
+		/* one parameter is mandatory: group name */
386
+		if (s->n != 2) {
387
+			LOG(L_ERR, "ERROR: selected_inst(): One parameter is expected\n");
388
+			return -1;
389
+		}
390
+
391
+		if (s->params[1].type != SEL_PARAM_STR) {
392
+			LOG(L_ERR, "ERROR: selected_inst(): string parameter is expected\n");
393
+			return -1;
394
+		}
395
+
396
+		/* look-up the group and the variable */
397
+		if (!(group = cfg_lookup_group(s->params[1].v.s.s, s->params[1].v.s.len))) {
398
+			if (cfg_shmized) {
399
+				LOG(L_ERR, "ERROR: selected_inst(): unknown configuration group: %.*s\n",
400
+					s->params[1].v.s.len, s->params[1].v.s.s);
401
+				return -1;
402
+			}
403
+			/* The group was not found, add it to the non-fixed select list.
404
+			 * So we act as if the fixup was successful, and we retry it later */
405
+			if (cfg_new_select(&s->params[1].v.s, NULL,
406
+						&s->params[1].v.p, NULL))
407
+				return -1;
408
+
409
+			LOG(L_DBG, "DEBUG: selected_inst(): select fixup is postponed: %.*s\n",
410
+				s->params[1].v.s.len, s->params[1].v.s.s);
411
+
412
+			s->params[1].type = SEL_PARAM_PTR;
413
+			s->params[1].v.p = NULL;
414
+
415
+			return 0;
416
+		}
417
+
418
+		s->params[1].type = SEL_PARAM_PTR;
419
+		s->params[1].v.p = (void *)group;
420
+
421
+		return 1;
422
+	}
423
+
424
+	group = (cfg_group_t *)s->params[1].v.p;
425
+	if (!group) return -1;
426
+
427
+	/* Get the current group instance from the group handle. */
428
+	inst = CFG_HANDLE_TO_GINST(*(group->handle));
429
+
430
+	if (inst) {
431
+		res->s = int2str(inst->id, &res->len);
432
+	} else {
433
+		res->s = "";
434
+		res->len = 0;
435
+	}
436
+	return 0;
437
+}
438
+
... ...
@@ -61,4 +61,7 @@ unsigned int read_cfg_var(struct cfg_read_handle *read_handle, void **val);
61 61
 int read_cfg_var_int(struct cfg_read_handle *read_handle, int *val);
62 62
 int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val);
63 63
 
64
+/* return the selected group instance */
65
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg);
66
+
64 67
 #endif /* _CFG_SELECT_H */
... ...
@@ -215,6 +215,7 @@ SELECT_F(select_identity)
215 215
 SELECT_F(select_identity_info)
216 216
 
217 217
 SELECT_F(select_cfg_var)
218
+SELECT_F(cfg_selected_inst)
218 219
 
219 220
 static select_row_t select_core[] = {
220 221
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("ruri"), select_ruri, 0}, /* not the same as request.uri because it is involved by new_uri */
... ...
@@ -410,6 +411,7 @@ static select_row_t select_core[] = {
410 411
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity_info"), select_identity_info, 0},
411 412
 
412 413
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_get"), select_cfg_var, CONSUME_ALL | FIXUP_CALL },
414
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_selected"), cfg_selected_inst, CONSUME_NEXT_STR | FIXUP_CALL },
413 415
 
414 416
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
415 417
 };