Browse Source

core: added $envn(name) - return environment variable as int number

Daniel-Constantin Mierla 4 years ago
parent
commit
3dfc7242e0
1 changed files with 41 additions and 0 deletions
  1. 41 0
      src/core/pv_core.c

+ 41 - 0
src/core/pv_core.c

@@ -82,6 +82,45 @@ static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
 	return pv_get_null(msg, param, res);
 }
 
+static int pv_parse_envn_name(pv_spec_p sp, str *in)
+{
+	char *csname;
+
+	if(in->s==NULL || in->len<=0)
+		return -1;
+
+	csname = pkg_malloc(in->len + 1);
+
+	if (csname == NULL) {
+		LM_ERR("no more pkg memory");
+		return -1;
+	}
+
+	memcpy(csname, in->s, in->len);
+	csname[in->len] = '\0';
+
+	sp->pvp.pvn.u.dname = (void*)csname;
+	sp->pvp.pvn.type = PV_NAME_OTHER;
+	return 0;
+}
+
+static int pv_get_envn(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
+{
+	str val;
+	int r = 0;
+	char *csname = (char *) param->pvn.u.dname;
+
+	if (csname) {
+		val.s = getenv(csname);
+		if (val.s) {
+			val.len = strlen(val.s);
+			str2sint(&val, &r);
+			return pv_get_intstrval(msg, param, res, r, &val);
+		}
+	}
+	return pv_get_null(msg, param, res);
+}
+
 static int pv_parse_def_name(pv_spec_p sp, str *in)
 {
 	if (in == NULL || in->s == NULL || sp == NULL) {
@@ -142,6 +181,8 @@ static pv_export_t core_pvs[] = {
 	{ STR_STATIC_INIT("retcode"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
 	{ STR_STATIC_INIT("env"), PVT_OTHER, pv_get_env, 0,
 		pv_parse_env_name, 0, 0, 0 },
+	{ STR_STATIC_INIT("envn"), PVT_OTHER, pv_get_envn, 0,
+		pv_parse_envn_name, 0, 0, 0 },
 	{ STR_STATIC_INIT("def"), PVT_OTHER, pv_get_def, 0,
 		pv_parse_def_name, 0, 0, 0 },
 	{ STR_STATIC_INIT("defn"), PVT_OTHER, pv_get_defn, 0,