Browse code

core: pv api - relocated implementation of $env(name)

Daniel-Constantin Mierla authored on 22/12/2020 18:44:16
Showing 1 changed files
... ...
@@ -24,17 +24,25 @@
24 24
  * Module: \ref core
25 25
  */
26 26
 
27
+#include <stdlib.h>
28
+
27 29
 #include "pv_core.h"
28 30
 #include "pvar.h"
29 31
 #include "str.h"
32
+#include "mem/pkg.h"
30 33
 
31 34
 static int pv_get_retcode(struct sip_msg*, pv_param_t*, pv_value_t*);
35
+static int pv_parse_env_name(pv_spec_p sp, str *in);
36
+static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
37
+
32 38
 
33 39
 static pv_export_t core_pvs[] = {
34 40
 	/* return code, various synonims */
35 41
 	{ STR_STATIC_INIT("?"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
36 42
 	{ STR_STATIC_INIT("rc"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
37 43
 	{ STR_STATIC_INIT("retcode"), PVT_OTHER, pv_get_retcode, 0, 0, 0, 0, 0 },
44
+	{ STR_STATIC_INIT("env"), PVT_OTHER, pv_get_env, 0,
45
+		pv_parse_env_name, 0, 0, 0},
38 46
 
39 47
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
40 48
 };
... ...
@@ -52,6 +60,43 @@ static int pv_get_retcode(struct sip_msg* msg, pv_param_t* p, pv_value_t* res)
52 60
 }
53 61
 
54 62
 
63
+static int pv_parse_env_name(pv_spec_p sp, str *in)
64
+{
65
+	char *csname;
66
+
67
+	if(in->s==NULL || in->len<=0)
68
+		return -1;
69
+
70
+	csname = pkg_malloc(in->len + 1);
71
+
72
+	if (csname == NULL) {
73
+		LM_ERR("no more pkg memory");
74
+		return -1;
75
+	}
76
+
77
+	memcpy(csname, in->s, in->len);
78
+	csname[in->len] = '\0';
79
+
80
+	sp->pvp.pvn.u.dname = (void*)csname;
81
+	sp->pvp.pvn.type = PV_NAME_OTHER;
82
+	return 0;
83
+}
84
+
85
+static int pv_get_env(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
86
+{
87
+	char *val;
88
+	char *csname = (char *) param->pvn.u.dname;
89
+
90
+	if (csname) {
91
+		val = getenv(csname);
92
+
93
+		if (val) {
94
+			return pv_get_strzval(msg, param, res, val);
95
+		}
96
+	}
97
+	return pv_get_null(msg, param, res);
98
+}
99
+
55 100
 
56 101
 /**
57 102
  * register built-in core pvars.