ppcfg.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. int pp_subst_add(char *data)
  38. {
  39. struct subst_expr* se;
  40. str subst;
  41. pp_subst_rule_t *pr;
  42. subst.s = data;
  43. subst.len = strlen(subst.s);
  44. /* check for early invalid rule */
  45. if(subst.len<=0)
  46. return -1;
  47. pr = (pp_subst_rule_t*)pkg_malloc(sizeof(pp_subst_rule_t));
  48. if(pr==NULL)
  49. {
  50. LM_ERR("no more pkg\n");
  51. return -1;
  52. }
  53. memset(pr, 0, sizeof(pp_subst_rule_t));
  54. se=subst_parser(&subst);
  55. if (se==0)
  56. {
  57. LM_ERR("bad subst expression: %s\n", data);
  58. pkg_free(pr);
  59. return -2;
  60. }
  61. pr->indata = data;
  62. pr->ppdata = (void*)se;
  63. if(pp_subst_rules_head==NULL)
  64. {
  65. pp_subst_rules_head = pr;
  66. } else {
  67. pp_subst_rules_tail->next = pr;
  68. }
  69. pp_subst_rules_tail = pr;
  70. LM_INFO("### added subst expression: %s\n", data);
  71. return 0;
  72. }
  73. int pp_substdef_add(char *data, int mode)
  74. {
  75. char c;
  76. char *p;
  77. str defname;
  78. str defvalue;
  79. if(pp_subst_add(data)<0) {
  80. LM_ERR("subst rule cannot be added\n");
  81. goto error;
  82. }
  83. p=data;
  84. c=*p;
  85. if (c=='\\') {
  86. LM_ERR("invalid separator char [%c] in [%s]\n", c, data);
  87. goto error;
  88. }
  89. p++;
  90. /* find regexp */
  91. defname.s=p;
  92. for ( ; *p; p++) {
  93. /* if unescaped sep. char */
  94. if ((*p==c) && (*(p-1)!='\\'))
  95. goto found_regexp;
  96. }
  97. LM_ERR("separator [%c] not found after regexp: [%s]\n", c, data);
  98. goto error;
  99. found_regexp:
  100. defname.len = p - defname.s;
  101. if(defname.len==0) {
  102. LM_ERR("define name too short\n");
  103. goto error;
  104. }
  105. p++;
  106. defvalue.s = p;
  107. /* find replacement */
  108. for ( ; *p; p++) {
  109. /* if unescaped sep. char */
  110. if ((*p==c) && (*(p-1)!='\\'))
  111. goto found_repl;
  112. }
  113. LM_ERR("separator [%c] not found after replacement: [%s]\n", c, data);
  114. goto error;
  115. found_repl:
  116. defvalue.len = p - defvalue.s;
  117. pp_define_set_type(0);
  118. if(pp_define(defname.len, defname.s)<0) {
  119. LM_ERR("cannot set define name\n");
  120. goto error;
  121. }
  122. if(mode==1) {
  123. /* define the value enclosed in double quotes */
  124. *(defvalue.s-1) = '"';
  125. defvalue.s[defvalue.len] = '"';
  126. defvalue.s--;
  127. defvalue.len += 2;
  128. }
  129. if(pp_define_set(defvalue.len, defvalue.s)<0) {
  130. LM_ERR("cannot set define value\n");
  131. goto error;
  132. }
  133. if(mode==1) {
  134. defvalue.s++;
  135. defvalue.len -= 2;
  136. *(defvalue.s-1) = c;
  137. defvalue.s[defvalue.len] = c;
  138. }
  139. LM_DBG("### added substdef: [%.*s]=[%.*s] (%d)\n", defname.len, defname.s,
  140. defvalue.len, defvalue.s, mode);
  141. return 0;
  142. error:
  143. return 1;
  144. }
  145. int pp_subst_run(char **data)
  146. {
  147. str* result;
  148. pp_subst_rule_t *pr;
  149. int i;
  150. if(pp_subst_rules_head==NULL)
  151. return 0;
  152. if(data==NULL || *data==NULL)
  153. return 0;
  154. if(strlen(*data)==0)
  155. return 0;
  156. pr = pp_subst_rules_head;
  157. i = 0;
  158. while(pr)
  159. {
  160. result=subst_str(*data, 0,
  161. (struct subst_expr*)pr->ppdata, 0); /* pkg malloc'ed result */
  162. if(result!=NULL)
  163. {
  164. i++;
  165. LM_DBG("preprocess subst applied [#%d] to [%s]"
  166. " - returning new string [%s]\n", i, *data, result->s);
  167. pkg_free(*data);
  168. *data = result->s;
  169. pkg_free(result);
  170. }
  171. pr = pr->next;
  172. }
  173. if(i!=0)
  174. return 1;
  175. return 0;
  176. }
  177. /* vi: set ts=4 sw=4 tw=79:ai:cindent: */