pv_svar.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2006 voice-system.ro
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. /*!
  24. * \file
  25. * \brief Script variables
  26. */
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include "../../dprint.h"
  32. #include "../../mem/mem.h"
  33. #include "pv_svar.h"
  34. static script_var_t *script_vars = 0;
  35. script_var_t* add_var(str *name)
  36. {
  37. script_var_t *it;
  38. if(name==0 || name->s==0 || name->len<=0)
  39. return 0;
  40. for(it=script_vars; it; it=it->next)
  41. {
  42. if(it->name.len==name->len
  43. && strncmp(name->s, it->name.s, name->len)==0)
  44. return it;
  45. }
  46. it = (script_var_t*)pkg_malloc(sizeof(script_var_t));
  47. if(it==0)
  48. {
  49. LM_ERR("out of pkg mem\n");
  50. return 0;
  51. }
  52. memset(it, 0, sizeof(script_var_t));
  53. it->name.s = (char*)pkg_malloc((name->len+1)*sizeof(char));
  54. if(it->name.s==0)
  55. {
  56. LM_ERR("out of pkg mem!\n");
  57. return 0;
  58. }
  59. it->name.len = name->len;
  60. strncpy(it->name.s, name->s, name->len);
  61. it->name.s[it->name.len] = '\0';
  62. it->next = script_vars;
  63. script_vars = it;
  64. return it;
  65. }
  66. script_var_t* set_var_value(script_var_t* var, int_str *value, int flags)
  67. {
  68. if(var==0)
  69. return 0;
  70. if(value==NULL)
  71. {
  72. if(var->v.flags&VAR_VAL_STR)
  73. {
  74. pkg_free(var->v.value.s.s);
  75. var->v.flags &= ~VAR_VAL_STR;
  76. }
  77. memset(&var->v.value, 0, sizeof(int_str));
  78. return var;
  79. }
  80. if(flags&VAR_VAL_STR)
  81. {
  82. if(var->v.flags&VAR_VAL_STR)
  83. { /* old and new value is str */
  84. if(value->s.len>var->v.value.s.len)
  85. { /* not enough space to copy */
  86. pkg_free(var->v.value.s.s);
  87. memset(&var->v.value, 0, sizeof(int_str));
  88. var->v.value.s.s =
  89. (char*)pkg_malloc((value->s.len+1)*sizeof(char));
  90. if(var->v.value.s.s==0)
  91. {
  92. LM_ERR("out of pkg mem\n");
  93. goto error;
  94. }
  95. }
  96. } else {
  97. memset(&var->v.value, 0, sizeof(int_str));
  98. var->v.value.s.s =
  99. (char*)pkg_malloc((value->s.len+1)*sizeof(char));
  100. if(var->v.value.s.s==0)
  101. {
  102. LM_ERR("out of pkg mem!\n");
  103. goto error;
  104. }
  105. var->v.flags |= VAR_VAL_STR;
  106. }
  107. strncpy(var->v.value.s.s, value->s.s, value->s.len);
  108. var->v.value.s.len = value->s.len;
  109. var->v.value.s.s[value->s.len] = '\0';
  110. } else {
  111. if(var->v.flags&VAR_VAL_STR)
  112. {
  113. pkg_free(var->v.value.s.s);
  114. var->v.flags &= ~VAR_VAL_STR;
  115. memset(&var->v.value, 0, sizeof(int_str));
  116. }
  117. var->v.value.n = value->n;
  118. }
  119. return var;
  120. error:
  121. /* set the var to init value */
  122. memset(&var->v.value, 0, sizeof(int_str));
  123. var->v.flags &= ~VAR_VAL_STR;
  124. return NULL;
  125. }
  126. script_var_t* get_var_by_name(str *name)
  127. {
  128. script_var_t *it;
  129. if(name==0 || name->s==0 || name->len<=0)
  130. return 0;
  131. for(it=script_vars; it; it=it->next)
  132. {
  133. if(it->name.len==name->len
  134. && strncmp(name->s, it->name.s, name->len)==0)
  135. return it;
  136. }
  137. return 0;
  138. }
  139. void reset_vars(void)
  140. {
  141. script_var_t *it;
  142. for(it=script_vars; it; it=it->next)
  143. {
  144. if(it->v.flags&VAR_VAL_STR)
  145. {
  146. pkg_free(it->v.value.s.s);
  147. it->v.flags &= ~VAR_VAL_STR;
  148. }
  149. memset(&it->v.value, 0, sizeof(int_str));
  150. }
  151. }
  152. void destroy_vars_list(script_var_t *svl)
  153. {
  154. script_var_t *it;
  155. script_var_t *it0;
  156. it = svl;
  157. while(it)
  158. {
  159. it0 = it;
  160. it = it->next;
  161. pkg_free(it0->name.s);
  162. if(it0->v.flags&VAR_VAL_STR)
  163. pkg_free(it0->v.value.s.s);
  164. pkg_free(it0);
  165. }
  166. svl = 0;
  167. }
  168. void destroy_vars(void)
  169. {
  170. destroy_vars_list(script_vars);
  171. }