Browse Source

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 14 years ago
parent
commit
f9ad9ccf83
3 changed files with 94 additions and 13 deletions
  1. 89 13
      cfg/cfg_select.c
  2. 3 0
      cfg/cfg_select.h
  3. 2 0
      select_core.h

+ 89 - 13
cfg/cfg_select.c

@@ -60,14 +60,17 @@ static int cfg_new_select(str *gname, str *vname, void **group_p, void **var_p)
 	if (!sel->gname.s) goto error;
 	memcpy(sel->gname.s, gname->s, gname->len);
 	sel->gname.len = gname->len;
+	sel->group_p = group_p;
 
-	sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
-	if (!sel->vname.s) goto error;
-	memcpy(sel->vname.s, vname->s, vname->len);
-	sel->vname.len = vname->len;
+	if (vname) {
+		sel->vname.s = (char *)pkg_malloc(sizeof(char)*vname->len);
+		if (!sel->vname.s) goto error;
+		memcpy(sel->vname.s, vname->s, vname->len);
+		sel->vname.len = vname->len;
+
+		sel->var_p = var_p;
+	}
 
-	sel->group_p = group_p;
-	sel->var_p = var_p;
 
 	sel->next = cfg_non_fixed_selects;
 	cfg_non_fixed_selects = sel;
@@ -111,14 +114,23 @@ int cfg_fixup_selects()
 
 	for (sel=cfg_non_fixed_selects; sel; sel=sel->next) {
 
-		if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
-			LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
-				sel->gname.len, sel->gname.s,
-				sel->vname.len, sel->vname.s);
-			return -1;
+		if (sel->var_p) {
+			if (cfg_lookup_var(&sel->gname, &sel->vname, &group, &var)) {
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown variable: %.*s.%.*s\n",
+					sel->gname.len, sel->gname.s,
+					sel->vname.len, sel->vname.s);
+				return -1;
+			}
+			*(sel->group_p) = (void *)group;
+			*(sel->var_p) = (void *)var;
+		} else {
+			if (!(group = cfg_lookup_group(sel->gname.s, sel->gname.len))) {
+				LOG(L_ERR, "ERROR: cfg_parse_selects(): unknown configuration group: %.*s\n",
+					sel->gname.len, sel->gname.s);
+				return -1;
+			}
+			*(sel->group_p) = (void *)group;
 		}
-		*(sel->group_p) = (void *)group;
-		*(sel->var_p) = (void *)var;
 	}
 	/* the select list is not needed anymore */
 	cfg_free_selects();
@@ -360,3 +372,67 @@ int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val)
 	*val = *(str *)(v2);
 	return 0;
 }
+
+/* return the selected group instance */
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg)
+{
+	cfg_group_t	*group;
+	cfg_group_inst_t	*inst;
+
+	if (msg == NULL) {
+		/* fixup call */
+
+		/* one parameter is mandatory: group name */
+		if (s->n != 2) {
+			LOG(L_ERR, "ERROR: selected_inst(): One parameter is expected\n");
+			return -1;
+		}
+
+		if (s->params[1].type != SEL_PARAM_STR) {
+			LOG(L_ERR, "ERROR: selected_inst(): string parameter is expected\n");
+			return -1;
+		}
+
+		/* look-up the group and the variable */
+		if (!(group = cfg_lookup_group(s->params[1].v.s.s, s->params[1].v.s.len))) {
+			if (cfg_shmized) {
+				LOG(L_ERR, "ERROR: selected_inst(): unknown configuration group: %.*s\n",
+					s->params[1].v.s.len, s->params[1].v.s.s);
+				return -1;
+			}
+			/* The group was not found, add it to the non-fixed select list.
+			 * So we act as if the fixup was successful, and we retry it later */
+			if (cfg_new_select(&s->params[1].v.s, NULL,
+						&s->params[1].v.p, NULL))
+				return -1;
+
+			LOG(L_DBG, "DEBUG: selected_inst(): select fixup is postponed: %.*s\n",
+				s->params[1].v.s.len, s->params[1].v.s.s);
+
+			s->params[1].type = SEL_PARAM_PTR;
+			s->params[1].v.p = NULL;
+
+			return 0;
+		}
+
+		s->params[1].type = SEL_PARAM_PTR;
+		s->params[1].v.p = (void *)group;
+
+		return 1;
+	}
+
+	group = (cfg_group_t *)s->params[1].v.p;
+	if (!group) return -1;
+
+	/* Get the current group instance from the group handle. */
+	inst = CFG_HANDLE_TO_GINST(*(group->handle));
+
+	if (inst) {
+		res->s = int2str(inst->id, &res->len);
+	} else {
+		res->s = "";
+		res->len = 0;
+	}
+	return 0;
+}
+

+ 3 - 0
cfg/cfg_select.h

@@ -61,4 +61,7 @@ unsigned int read_cfg_var(struct cfg_read_handle *read_handle, void **val);
 int read_cfg_var_int(struct cfg_read_handle *read_handle, int *val);
 int read_cfg_var_str(struct cfg_read_handle *read_handle, str *val);
 
+/* return the selected group instance */
+int cfg_selected_inst(str *res, select_t *s, struct sip_msg *msg);
+
 #endif /* _CFG_SELECT_H */

+ 2 - 0
select_core.h

@@ -215,6 +215,7 @@ SELECT_F(select_identity)
 SELECT_F(select_identity_info)
 
 SELECT_F(select_cfg_var)
+SELECT_F(cfg_selected_inst)
 
 static select_row_t select_core[] = {
 	{ 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[] = {
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("identity_info"), select_identity_info, 0},
 
 	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_get"), select_cfg_var, CONSUME_ALL | FIXUP_CALL },
+	{ NULL, SEL_PARAM_STR, STR_STATIC_INIT("cfg_selected"), cfg_selected_inst, CONSUME_NEXT_STR | FIXUP_CALL },
 
 	{ NULL, SEL_PARAM_INT, STR_NULL, NULL, 0}
 };