Browse code

kemix: exported functions to get env values

Daniel-Constantin Mierla authored on 04/01/2021 09:35:42
Showing 1 changed files
... ...
@@ -1093,6 +1093,52 @@ static int ki_kx_get_defn(sip_msg_t *msg, str *dname)
1093 1093
 	return n;
1094 1094
 }
1095 1095
 
1096
+/**
1097
+ *
1098
+ */
1099
+static sr_kemi_xval_t* ki_kx_get_env(sip_msg_t *msg, str *envname)
1100
+{
1101
+	char *val;
1102
+
1103
+	memset(&_sr_kemi_kx_xval, 0, sizeof(sr_kemi_xval_t));
1104
+	if(envname==NULL || envname->s==NULL || envname->len<=0) {
1105
+		sr_kemi_xval_null(&_sr_kemi_kx_xval, SR_KEMI_XVAL_NULL_EMPTY);
1106
+		return &_sr_kemi_kx_xval;
1107
+	}
1108
+
1109
+	val = getenv(envname->s);
1110
+	if (val == NULL) {
1111
+		sr_kemi_xval_null(&_sr_kemi_kx_xval, SR_KEMI_XVAL_NULL_EMPTY);
1112
+		return &_sr_kemi_kx_xval;
1113
+	}
1114
+
1115
+	_sr_kemi_kx_xval.vtype = SR_KEMIP_STR;
1116
+	_sr_kemi_kx_xval.v.s.s = val;
1117
+	_sr_kemi_kx_xval.v.s.len = strlen(val);
1118
+	return &_sr_kemi_kx_xval;
1119
+}
1120
+
1121
+/**
1122
+ *
1123
+ */
1124
+static int ki_kx_get_envn(sip_msg_t *msg, str *envname)
1125
+{
1126
+	str val;
1127
+	int r = 0;
1128
+
1129
+	if(envname==NULL || envname->s==NULL || envname->len<=0) {
1130
+		return 0;
1131
+	}
1132
+
1133
+	val.s = getenv(envname->s);
1134
+	if (val.s) {
1135
+		val.len = strlen(val.s);
1136
+		str2sint(&val, &r);
1137
+		return r;
1138
+	}
1139
+
1140
+	return r;
1141
+}
1096 1142
 
1097 1143
 /**
1098 1144
  *
... ...
@@ -1404,6 +1450,16 @@ static sr_kemi_t sr_kemi_kx_exports[] = {
1404 1450
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1405 1451
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1406 1452
 	},
1453
+	{ str_init("kx"), str_init("get_env"),
1454
+		SR_KEMIP_XVAL, ki_kx_get_env,
1455
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1456
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1457
+	},
1458
+	{ str_init("kx"), str_init("get_envn"),
1459
+		SR_KEMIP_INT, ki_kx_get_envn,
1460
+		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
1461
+			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
1462
+	},
1407 1463
 
1408 1464
 
1409 1465
 	{ {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }