ppcfg.c 4.5 KB

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