ppcfg.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*!
  17. * \file
  18. * \brief Kamailio core :: ppcfg.c - config preprocessor directives
  19. * \ingroup core
  20. * Module: \ref core
  21. */
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <sys/types.h>
  25. #include <unistd.h>
  26. #include "mem/mem.h"
  27. #include "ut.h"
  28. #include "re.h"
  29. #include "dprint.h"
  30. #include "ppcfg.h"
  31. typedef struct _pp_subst_rule {
  32. char *indata;
  33. void *ppdata;
  34. struct _pp_subst_rule *next;
  35. } pp_subst_rule_t;
  36. static pp_subst_rule_t *pp_subst_rules_head = NULL;
  37. static pp_subst_rule_t *pp_subst_rules_tail = NULL;
  38. static int _pp_ifdef_level = 0;
  39. int pp_subst_add(char *data)
  40. {
  41. struct subst_expr* se;
  42. str subst;
  43. pp_subst_rule_t *pr;
  44. subst.s = data;
  45. subst.len = strlen(subst.s);
  46. /* check for early invalid rule */
  47. if(subst.len<=0)
  48. return -1;
  49. pr = (pp_subst_rule_t*)pkg_malloc(sizeof(pp_subst_rule_t));
  50. if(pr==NULL)
  51. {
  52. LM_ERR("no more pkg\n");
  53. return -1;
  54. }
  55. memset(pr, 0, sizeof(pp_subst_rule_t));
  56. se=subst_parser(&subst);
  57. if (se==0)
  58. {
  59. LM_ERR("bad subst expression: %s\n", data);
  60. pkg_free(pr);
  61. return -2;
  62. }
  63. pr->indata = data;
  64. pr->ppdata = (void*)se;
  65. if(pp_subst_rules_head==NULL)
  66. {
  67. pp_subst_rules_head = pr;
  68. } else {
  69. pp_subst_rules_tail->next = pr;
  70. }
  71. pp_subst_rules_tail = pr;
  72. LM_INFO("### added subst expression: %s\n", data);
  73. return 0;
  74. }
  75. int pp_substdef_add(char *data, int mode)
  76. {
  77. char c;
  78. char *p;
  79. str defname;
  80. str defvalue;
  81. if(pp_subst_add(data)<0) {
  82. LM_ERR("subst rule cannot be added\n");
  83. goto error;
  84. }
  85. p=data;
  86. c=*p;
  87. if (c=='\\') {
  88. LM_ERR("invalid separator char [%c] in [%s]\n", c, data);
  89. goto error;
  90. }
  91. p++;
  92. /* find regexp */
  93. defname.s=p;
  94. for ( ; *p; p++) {
  95. /* if unescaped sep. char */
  96. if ((*p==c) && (*(p-1)!='\\'))
  97. goto found_regexp;
  98. }
  99. LM_ERR("separator [%c] not found after regexp: [%s]\n", c, data);
  100. goto error;
  101. found_regexp:
  102. defname.len = p - defname.s;
  103. if(defname.len==0) {
  104. LM_ERR("define name too short\n");
  105. goto error;
  106. }
  107. p++;
  108. defvalue.s = p;
  109. /* find replacement */
  110. for ( ; *p; p++) {
  111. /* if unescaped sep. char */
  112. if ((*p==c) && (*(p-1)!='\\'))
  113. goto found_repl;
  114. }
  115. LM_ERR("separator [%c] not found after replacement: [%s]\n", c, data);
  116. goto error;
  117. found_repl:
  118. defvalue.len = p - defvalue.s;
  119. pp_define_set_type(0);
  120. if(pp_define(defname.len, defname.s)<0) {
  121. LM_ERR("cannot set define name\n");
  122. goto error;
  123. }
  124. if(mode==1) {
  125. /* define the value enclosed in double quotes */
  126. *(defvalue.s-1) = '"';
  127. defvalue.s[defvalue.len] = '"';
  128. defvalue.s--;
  129. defvalue.len += 2;
  130. }
  131. if(pp_define_set(defvalue.len, defvalue.s)<0) {
  132. LM_ERR("cannot set define value\n");
  133. goto error;
  134. }
  135. if(mode==1) {
  136. defvalue.s++;
  137. defvalue.len -= 2;
  138. *(defvalue.s-1) = c;
  139. defvalue.s[defvalue.len] = c;
  140. }
  141. LM_DBG("### added substdef: [%.*s]=[%.*s] (%d)\n", defname.len, defname.s,
  142. defvalue.len, defvalue.s, mode);
  143. return 0;
  144. error:
  145. return 1;
  146. }
  147. int pp_subst_run(char **data)
  148. {
  149. str* result;
  150. pp_subst_rule_t *pr;
  151. int i;
  152. if(pp_subst_rules_head==NULL)
  153. return 0;
  154. if(data==NULL || *data==NULL)
  155. return 0;
  156. if(strlen(*data)==0)
  157. return 0;
  158. pr = pp_subst_rules_head;
  159. i = 0;
  160. while(pr)
  161. {
  162. result=subst_str(*data, 0,
  163. (struct subst_expr*)pr->ppdata, 0); /* pkg malloc'ed result */
  164. if(result!=NULL)
  165. {
  166. i++;
  167. LM_DBG("preprocess subst applied [#%d] to [%s]"
  168. " - returning new string [%s]\n", i, *data, result->s);
  169. pkg_free(*data);
  170. *data = result->s;
  171. pkg_free(result);
  172. }
  173. pr = pr->next;
  174. }
  175. if(i!=0)
  176. return 1;
  177. return 0;
  178. }
  179. /**
  180. *
  181. */
  182. void pp_ifdef_level_update(int val)
  183. {
  184. _pp_ifdef_level += val;
  185. }
  186. /**
  187. *
  188. */
  189. void pp_ifdef_level_check(void)
  190. {
  191. if(_pp_ifdef_level!=0) {
  192. LM_WARN("different number of preprocessor directives:"
  193. " N(#!IF[N]DEF) - N(#!ENDIF) = %d\n", _pp_ifdef_level);
  194. } else {
  195. LM_DBG("same number of pairing preprocessor directives"
  196. " #!IF[N]DEF - #!ENDIF\n");
  197. }
  198. }
  199. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */