瀏覽代碼

pv: new pv - $vn(name)

- similar to $var(name) and able to hold $null
- default value is $null
	$vn(x) = 0; $vn(x) = "abc"; $vn(x) = $null;
- $vz(name) aliased to $var(name)
Daniel-Constantin Mierla 10 年之前
父節點
當前提交
f95d0f5871
共有 6 個文件被更改,包括 82 次插入10 次删除
  1. 4 0
      modules/pv/pv.c
  2. 20 2
      modules/pv/pv_core.c
  3. 1 0
      modules/pv/pv_core.h
  4. 1 1
      modules/pv/pv_shv.c
  5. 48 5
      modules/pv/pv_svar.c
  6. 8 2
      modules/pv/pv_svar.h

+ 4 - 0
modules/pv/pv.c

@@ -97,6 +97,10 @@ static pv_export_t mod_pvs[] = {
 		pv_parse_index, 0, 0},
 	{{"var", (sizeof("var")-1)}, PVT_SCRIPTVAR, pv_get_scriptvar,
 		pv_set_scriptvar, pv_parse_scriptvar_name, 0, 0, 0},
+	{{"vz", (sizeof("vz")-1)}, PVT_SCRIPTVAR, pv_get_scriptvar,
+		pv_set_scriptvar, pv_parse_scriptvar_name, 0, 0, 0},
+	{{"vn", (sizeof("vn")-1)}, PVT_SCRIPTVAR, pv_get_scriptvar,
+		pv_set_scriptvar, pv_parse_scriptvarnull_name, 0, 0, 0},
 	{{"ai", (sizeof("ai")-1)}, /* */
 		PVT_OTHER, pv_get_pai, 0,
 		0, pv_parse_index, 0, 0},

+ 20 - 2
modules/pv/pv_core.c

@@ -1804,6 +1804,9 @@ int pv_get_scriptvar(struct sip_msg *msg,  pv_param_t *param,
 	
 	sv= (script_var_t*)param->pvn.u.dname;
 
+	if((sv->v.flags&VAR_TYPE_NULL) && (sv->v.flags&VAR_VAL_NULL))
+			return pv_get_null(msg, param, res);
+
 	if(sv->v.flags&VAR_VAL_STR)
 	{
 		res->rs = sv->v.value.s;
@@ -2748,9 +2751,24 @@ int pv_parse_scriptvar_name(pv_spec_p sp, str *in)
 {
 	if(in==NULL || in->s==NULL || sp==NULL)
 		return -1;
-	
+
+	sp->pvp.pvn.type = PV_NAME_PVAR;
+	sp->pvp.pvn.u.dname = (void*)add_var(in, VAR_TYPE_ZERO);
+	if(sp->pvp.pvn.u.dname==NULL)
+	{
+		LM_ERR("cannot register var [%.*s]\n", in->len, in->s);
+		return -1;
+	}
+	return 0;
+}
+
+int pv_parse_scriptvarnull_name(pv_spec_p sp, str *in)
+{
+	if(in==NULL || in->s==NULL || sp==NULL)
+		return -1;
+
 	sp->pvp.pvn.type = PV_NAME_PVAR;
-	sp->pvp.pvn.u.dname = (void*)add_var(in);
+	sp->pvp.pvn.u.dname = (void*)add_var(in, VAR_TYPE_NULL);
 	if(sp->pvp.pvn.u.dname==NULL)
 	{
 		LM_ERR("cannot register var [%.*s]\n", in->len, in->s);

+ 1 - 0
modules/pv/pv_core.h

@@ -301,6 +301,7 @@ int pv_set_from_display(struct sip_msg* msg, pv_param_t *param,
 /********* end PV set functions *********/
 
 int pv_parse_scriptvar_name(pv_spec_p sp, str *in);
+int pv_parse_scriptvarnull_name(pv_spec_p sp, str *in);
 
 int pv_parse_hdr_name(pv_spec_p sp, str *in);
 

+ 1 - 1
modules/pv/pv_shv.c

@@ -810,7 +810,7 @@ int param_set_xvar( modparam_t type, void* val, int mode)
 		isv.n = ival;
 	}
 	if(mode==0) {
-		pkv = add_var(&s);
+		pkv = add_var(&s, VAR_TYPE_ZERO);
 		if(pkv==NULL)
 			goto error;
 		if(set_var_value(pkv, &isv, flags)==NULL)

+ 48 - 5
modules/pv/pv_svar.c

@@ -37,15 +37,21 @@
 #include "pv_svar.h"
 
 static script_var_t *script_vars = 0;
+static script_var_t *script_vars_null = 0;
 
-script_var_t* add_var(str *name)
+script_var_t* add_var(str *name, int vtype)
 {
 	script_var_t *it;
 
 	if(name==0 || name->s==0 || name->len<=0)
 		return 0;
 
-	for(it=script_vars; it; it=it->next)
+	if(vtype==VAR_TYPE_NULL) {
+		it=script_vars_null;
+	} else {
+		it=script_vars;
+	}
+	for(; it; it=it->next)
 	{
 		if(it->name.len==name->len
 				&& strncmp(name->s, it->name.s, name->len)==0)
@@ -69,9 +75,14 @@ script_var_t* add_var(str *name)
 	strncpy(it->name.s, name->s, name->len);
 	it->name.s[it->name.len] = '\0';
 
-	it->next = script_vars;
-
-	script_vars = it;
+	if(vtype==VAR_TYPE_NULL) {
+		it->v.flags = VAR_VAL_NULL|VAR_TYPE_NULL;
+		it->next = script_vars_null;
+		script_vars_null = it;
+	} else {
+		it->next = script_vars;
+		script_vars = it;
+	}
 
 	return it;
 }
@@ -87,11 +98,16 @@ script_var_t* set_var_value(script_var_t* var, int_str *value, int flags)
 			pkg_free(var->v.value.s.s);
 			var->v.flags &= ~VAR_VAL_STR;
 		}
+
+		if(var->v.flags&VAR_TYPE_NULL)
+			var->v.flags |= VAR_VAL_NULL;
+
 		memset(&var->v.value, 0, sizeof(int_str));
 
 		return var;
 	}
 
+	var->v.flags &= ~VAR_VAL_NULL;
 	if(flags&VAR_VAL_STR)
 	{
 		if(var->v.flags&VAR_VAL_STR)
@@ -156,6 +172,22 @@ script_var_t* get_var_by_name(str *name)
 	return 0;
 }
 
+script_var_t* get_varnull_by_name(str *name)
+{
+	script_var_t *it;
+
+	if(name==0 || name->s==0 || name->len<=0)
+		return 0;
+
+	for(it=script_vars_null; it; it=it->next)
+	{
+		if(it->name.len==name->len
+				&& strncmp(name->s, it->name.s, name->len)==0)
+			return it;
+	}
+	return 0;
+}
+
 void reset_vars(void)
 {
 	script_var_t *it;
@@ -168,6 +200,16 @@ void reset_vars(void)
 		}
 		memset(&it->v.value, 0, sizeof(int_str));
 	}
+	for(it=script_vars_null; it; it=it->next)
+	{
+		if(it->v.flags&VAR_VAL_STR)
+		{
+			pkg_free(it->v.value.s.s);
+			it->v.flags &= ~VAR_VAL_STR;
+		}
+		it->v.flags |= VAR_VAL_NULL;
+		memset(&it->v.value, 0, sizeof(int_str));
+	}
 }
 
 void destroy_vars_list(script_var_t *svl)
@@ -192,4 +234,5 @@ void destroy_vars_list(script_var_t *svl)
 void destroy_vars(void)
 {
 	destroy_vars_list(script_vars);
+	destroy_vars_list(script_vars_null);
 }

+ 8 - 2
modules/pv/pv_svar.h

@@ -31,7 +31,12 @@
 
 #include "../../usr_avp.h"
 
-#define VAR_VAL_STR	(1<<0)
+#define VAR_VAL_INT		(0)		/* value is INT (other flags not set) */
+#define VAR_VAL_STR		(1<<0)  /* value is STR */
+#define VAR_VAL_NULL	(1<<1)	/* value is NULL */
+
+#define VAR_TYPE_ZERO	(0)		/* default value is 0 (type NULL not set) */
+#define VAR_TYPE_NULL	(1<<15) /* default value is NULL */
 
 typedef struct script_val {
 	int flags;
@@ -44,9 +49,10 @@ typedef struct script_var {
 	struct script_var *next;
 } script_var_t, *script_var_p;
 
-script_var_t* add_var(str *name);
+script_var_t* add_var(str *name, int vtype);
 script_var_t* set_var_value(script_var_t *var, int_str *value, int flags);
 script_var_t* get_var_by_name(str *name);
+script_var_t* get_varnull_by_name(str *name);
 
 void reset_vars(void);
 void destroy_vars(void);