Browse code

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

Daniel-Constantin Mierla authored on 02/01/2021 15:10:07
Showing 1 changed files
... ...
@@ -82,6 +82,45 @@ static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
82 82
 	return pv_get_null(msg, param, res);
83 83
 }
84 84
 
85
+static int pv_parse_envn_name(pv_spec_p sp, str *in)
86
+{
87
+	char *csname;
88
+
89
+	if(in->s==NULL || in->len<=0)
90
+		return -1;
91
+
92
+	csname = pkg_malloc(in->len + 1);
93
+
94
+	if (csname == NULL) {
95
+		LM_ERR("no more pkg memory");
96
+		return -1;
97
+	}
98
+
99
+	memcpy(csname, in->s, in->len);
100
+	csname[in->len] = '\0';
101
+
102
+	sp->pvp.pvn.u.dname = (void*)csname;
103
+	sp->pvp.pvn.type = PV_NAME_OTHER;
104
+	return 0;
105
+}
106
+
107
+static int pv_get_envn(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
108
+{
109
+	str val;
110
+	int r = 0;
111
+	char *csname = (char *) param->pvn.u.dname;
112
+
113
+	if (csname) {
114
+		val.s = getenv(csname);
115
+		if (val.s) {
116
+			val.len = strlen(val.s);
117
+			str2sint(&val, &r);
118
+			return pv_get_intstrval(msg, param, res, r, &val);
119
+		}
120
+	}
121
+	return pv_get_null(msg, param, res);
122
+}
123
+
85 124
 static int pv_parse_def_name(pv_spec_p sp, str *in)
86 125
 {
87 126
 	if (in == NULL || in->s == NULL || sp == NULL) {
... ...
@@ -142,6 +181,8 @@ static pv_export_t core_pvs[] = {
142 181
 	{ STR_STATIC_INIT("retcode"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
143 182
 	{ STR_STATIC_INIT("env"), PVT_OTHER, pv_get_env, 0,
144 183
 		pv_parse_env_name, 0, 0, 0 },
184
+	{ STR_STATIC_INIT("envn"), PVT_OTHER, pv_get_envn, 0,
185
+		pv_parse_envn_name, 0, 0, 0 },
145 186
 	{ STR_STATIC_INIT("def"), PVT_OTHER, pv_get_def, 0,
146 187
 		pv_parse_def_name, 0, 0, 0 },
147 188
 	{ STR_STATIC_INIT("defn"), PVT_OTHER, pv_get_defn, 0,