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 111
 
112 112
 	for (sel=cfg_non_fixed_selects; sel; sel=sel->next) {
113 113
 
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;
114
+		if (sel->var_p) {
115
+			if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
116
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
117
+					sel->gname.len, sel->gname.s,
118
+					sel->vname.len, sel->vname.s);
119
+				return -1;
120
+			}
121
+			*(sel->group_p) = (void *)group;
122
+			*(sel->var_p) = (void *)var;
123
+		} else {
124
+			if (!(group = cfg_lookup_group(sel->gname.s, sel->gname.len))) {
125
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown configuration group: %.*s\n",
126
+					sel->gname.len, sel->gname.s);
127
+				return -1;
128
+			}
129
+			*(sel->group_p) = (void *)group;
119 130
 		}
120
-		*(sel->group_p) = (void *)group;
121
-		*(sel->var_p) = (void *)var;
122 131
 	}
123 132
 	/* the select list is not needed anymore */
124 133
 	cfg_free_selects();
... ...
@@ -360,3 +372,67 @@ int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val)
360 360
 	*val = *(str *)(v2);
361 361
 	return 0;
362 362
 }
363
+
364
+/* return the selected group instance */
365
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg)
366
+{
367
+	cfg_group_t	*group;
368
+	cfg_group_inst_t	*inst;
369
+
370
+	if (msg == NULL) {
371
+		/* fixup call */
372
+
373
+		/* one parameter is mandatory: group name */
374
+		if (s->n != 2) {
375
+			LOG(L_ERR, "ERROR: selected_inst(): One parameter is expected\n");
376
+			return -1;
377
+		}
378
+
379
+		if (s->params[1].type != SEL_PARAM_STR) {
380
+			LOG(L_ERR, "ERROR: selected_inst(): string parameter is expected\n");
381
+			return -1;
382
+		}
383
+
384
+		/* look-up the group and the variable */
385
+		if (!(group = cfg_lookup_group(s->params[1].v.s.s, s->params[1].v.s.len))) {
386
+			if (cfg_shmized) {
387
+				LOG(L_ERR, "ERROR: selected_inst(): unknown configuration group: %.*s\n",
388
+					s->params[1].v.s.len, s->params[1].v.s.s);
389
+				return -1;
390
+			}
391
+			/* The group was not found, add it to the non-fixed select list.
392
+			 * So we act as if the fixup was successful, and we retry it later */
393
+			if (cfg_new_select(&s->params[1].v.s, NULL,
394
+						&s->params[1].v.p, NULL))
395
+				return -1;
396
+
397
+			LOG(L_DBG, "DEBUG: selected_inst(): select fixup is postponed: %.*s\n",
398
+				s->params[1].v.s.len, s->params[1].v.s.s);
399
+
400
+			s->params[1].type = SEL_PARAM_PTR;
401
+			s->params[1].v.p = NULL;
402
+
403
+			return 0;
404
+		}
405
+
406
+		s->params[1].type = SEL_PARAM_PTR;
407
+		s->params[1].v.p = (void *)group;
408
+
409
+		return 1;
410
+	}
411
+
412
+	group = (cfg_group_t *)s->params[1].v.p;
413
+	if (!group) return -1;
414
+
415
+	/* Get the current group instance from the group handle. */
416
+	inst = CFG_HANDLE_TO_GINST(*(group->handle));
417
+
418
+	if (inst) {
419
+		res->s = int2str(inst->id, &res->len);
420
+	} else {
421
+		res->s = "";
422
+		res->len = 0;
423
+	}
424
+	return 0;
425
+}
426
+
... ...
@@ -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 410
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity_info"), select_identity_info, 0},
411 411
 
412 412
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_get"), select_cfg_var, CONSUME_ALL | FIXUP_CALL },
413
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_selected"), cfg_selected_inst, CONSUME_NEXT_STR | FIXUP_CALL },
413 414
 
414 415
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
415 416
 };