File: pv_svar.c

package info (click to toggle)
kamailio 4.2.0-2%2Bdeb8u3
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 56,276 kB
  • sloc: ansic: 552,836; xml: 166,484; sh: 8,659; makefile: 7,676; sql: 6,235; perl: 3,487; yacc: 3,428; python: 1,457; cpp: 1,219; php: 1,047; java: 449; pascal: 194; cs: 40; awk: 27
file content (195 lines) | stat: -rw-r--r-- 4,023 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * $Id$
 *
 * Copyright (C) 2006 voice-system.ro
 *
 * This file is part of Kamailio, a free SIP server.
 *
 * Kamailio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version
 *
 * Kamailio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

/*!
 * \file
 * \brief Script variables
 */

#include <assert.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#include "../../dprint.h"
#include "../../mem/mem.h"

#include "pv_svar.h"

static script_var_t *script_vars = 0;

script_var_t* add_var(str *name)
{
	script_var_t *it;

	if(name==0 || name->s==0 || name->len<=0)
		return 0;

	for(it=script_vars; it; it=it->next)
	{
		if(it->name.len==name->len
				&& strncmp(name->s, it->name.s, name->len)==0)
			return it;
	}
	it = (script_var_t*)pkg_malloc(sizeof(script_var_t));
	if(it==0)
	{
		LM_ERR("out of pkg mem\n");
		return 0;
	}
	memset(it, 0, sizeof(script_var_t));
	it->name.s = (char*)pkg_malloc((name->len+1)*sizeof(char));

	if(it->name.s==0)
	{
		LM_ERR("out of pkg mem!\n");
		return 0;
	}
	it->name.len = name->len;
	strncpy(it->name.s, name->s, name->len);
	it->name.s[it->name.len] = '\0';

	it->next = script_vars;

	script_vars = it;

	return it;
}

script_var_t* set_var_value(script_var_t* var, int_str *value, int flags)
{
	if(var==0)
		return 0;
	if(value==NULL)
	{
		if(var->v.flags&VAR_VAL_STR)
		{
			pkg_free(var->v.value.s.s);
			var->v.flags &= ~VAR_VAL_STR;
		}
		memset(&var->v.value, 0, sizeof(int_str));

		return var;
	}

	if(flags&VAR_VAL_STR)
	{
		if(var->v.flags&VAR_VAL_STR)
		{ /* old and new value is str */
			if(value->s.len>var->v.value.s.len)
			{ /* not enough space to copy */
				pkg_free(var->v.value.s.s);
				memset(&var->v.value, 0, sizeof(int_str));
				var->v.value.s.s =
					(char*)pkg_malloc((value->s.len+1)*sizeof(char));
				if(var->v.value.s.s==0)
				{
					LM_ERR("out of pkg mem\n");
					goto error;
				}
			}
		} else {
			memset(&var->v.value, 0, sizeof(int_str));
			var->v.value.s.s =
					(char*)pkg_malloc((value->s.len+1)*sizeof(char));
			if(var->v.value.s.s==0)
			{
				LM_ERR("out of pkg mem!\n");
				goto error;
			}
			var->v.flags |= VAR_VAL_STR;
		}
		strncpy(var->v.value.s.s, value->s.s, value->s.len);
		var->v.value.s.len = value->s.len;
		var->v.value.s.s[value->s.len] = '\0';
	} else {
		if(var->v.flags&VAR_VAL_STR)
		{
			pkg_free(var->v.value.s.s);
			var->v.flags &= ~VAR_VAL_STR;
			memset(&var->v.value, 0, sizeof(int_str));
		}
		var->v.value.n = value->n;
	}

	return var;
error:
	/* set the var to init value */
	memset(&var->v.value, 0, sizeof(int_str));
	var->v.flags &= ~VAR_VAL_STR;
	return NULL;
}

script_var_t* get_var_by_name(str *name)
{
	script_var_t *it;

	if(name==0 || name->s==0 || name->len<=0)
		return 0;

	for(it=script_vars; 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;
	for(it=script_vars; it; it=it->next)
	{
		if(it->v.flags&VAR_VAL_STR)
		{
			pkg_free(it->v.value.s.s);
			it->v.flags &= ~VAR_VAL_STR;
		}
		memset(&it->v.value, 0, sizeof(int_str));
	}
}

void destroy_vars_list(script_var_t *svl)
{
	script_var_t *it;
	script_var_t *it0;

	it = svl;
	while(it)
	{
		it0 = it;
		it = it->next;
		pkg_free(it0->name.s);
		if(it0->v.flags&VAR_VAL_STR)
			pkg_free(it0->v.value.s.s);
		pkg_free(it0);
	}

	svl = 0;
}

void destroy_vars(void)
{
	destroy_vars_list(script_vars);
}