NCDConfig.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**
  2. * @file NCDConfig.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN 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 along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <misc/string_begins_with.h>
  25. #include <misc/expstring.h>
  26. #include <ncd/NCDConfig.h>
  27. void NCDConfig_free_processes (struct NCDConfig_processes *v)
  28. {
  29. if (!v) {
  30. return;
  31. }
  32. free(v->name);
  33. NCDConfig_free_statements(v->statements);
  34. NCDConfig_free_processes(v->next);
  35. free(v);
  36. }
  37. void NCDConfig_free_statements (struct NCDConfig_statements *v)
  38. {
  39. if (!v) {
  40. return;
  41. }
  42. NCDConfig_free_strings(v->objname);
  43. NCDConfig_free_strings(v->names);
  44. NCDConfig_free_list(v->args);
  45. free(v->name);
  46. NCDConfig_free_statements(v->next);
  47. free(v);
  48. }
  49. void NCDConfig_free_list (struct NCDConfig_list *v)
  50. {
  51. if (!v) {
  52. return;
  53. }
  54. switch (v->type) {
  55. case NCDCONFIG_ARG_STRING:
  56. free(v->string);
  57. break;
  58. case NCDCONFIG_ARG_VAR:
  59. NCDConfig_free_strings(v->var);
  60. break;
  61. case NCDCONFIG_ARG_LIST:
  62. NCDConfig_free_list(v->list);
  63. break;
  64. default:
  65. ASSERT(0);
  66. }
  67. NCDConfig_free_list(v->next);
  68. free(v);
  69. }
  70. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  71. {
  72. if (!v) {
  73. return;
  74. }
  75. free(v->value);
  76. NCDConfig_free_strings(v->next);
  77. free(v);
  78. }
  79. struct NCDConfig_processes * NCDConfig_make_processes (int is_template, char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_processes *next)
  80. {
  81. if (!name || !statements || (need_next && !next)) {
  82. goto fail;
  83. }
  84. struct NCDConfig_processes *v = malloc(sizeof(*v));
  85. if (!v) {
  86. goto fail;
  87. }
  88. v->is_template = is_template;
  89. v->name = name;
  90. v->statements = statements;
  91. v->next = next;
  92. return v;
  93. fail:
  94. free(name);
  95. NCDConfig_free_statements(statements);
  96. NCDConfig_free_processes(next);
  97. return NULL;
  98. }
  99. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *objname, struct NCDConfig_strings *names, struct NCDConfig_list *args, char *name, struct NCDConfig_statements *next)
  100. {
  101. struct NCDConfig_statements *v = malloc(sizeof(*v));
  102. if (!v) {
  103. goto fail;
  104. }
  105. v->objname = objname;
  106. v->names = names;
  107. v->args = args;
  108. v->name = name;
  109. v->next = next;
  110. return v;
  111. fail:
  112. NCDConfig_free_strings(names);
  113. NCDConfig_free_list(args);
  114. free(name);
  115. NCDConfig_free_statements(next);
  116. return NULL;
  117. }
  118. struct NCDConfig_list * NCDConfig_make_list_string (char *str, struct NCDConfig_list *next)
  119. {
  120. struct NCDConfig_list *v = malloc(sizeof(*v));
  121. if (!v) {
  122. goto fail;
  123. }
  124. v->type = NCDCONFIG_ARG_STRING;
  125. v->string = str;
  126. v->next = next;
  127. return v;
  128. fail:
  129. free(str);
  130. NCDConfig_free_list(next);
  131. return NULL;
  132. }
  133. struct NCDConfig_list * NCDConfig_make_list_var (struct NCDConfig_strings *var, struct NCDConfig_list *next)
  134. {
  135. struct NCDConfig_list *v = malloc(sizeof(*v));
  136. if (!v) {
  137. goto fail;
  138. }
  139. v->type = NCDCONFIG_ARG_VAR;
  140. v->var = var;
  141. v->next = next;
  142. return v;
  143. fail:
  144. NCDConfig_free_strings(var);
  145. NCDConfig_free_list(next);
  146. return NULL;
  147. }
  148. struct NCDConfig_list * NCDConfig_make_list_list (struct NCDConfig_list *list, struct NCDConfig_list *next)
  149. {
  150. struct NCDConfig_list *v = malloc(sizeof(*v));
  151. if (!v) {
  152. goto fail;
  153. }
  154. v->type = NCDCONFIG_ARG_LIST;
  155. v->list = list;
  156. v->next = next;
  157. return v;
  158. fail:
  159. NCDConfig_free_list(list);
  160. NCDConfig_free_list(next);
  161. return NULL;
  162. }
  163. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  164. {
  165. if (!value || (need_next && !next)) {
  166. goto fail;
  167. }
  168. struct NCDConfig_strings *v = malloc(sizeof(*v));
  169. if (!v) {
  170. goto fail;
  171. }
  172. v->value = value;
  173. v->next = next;
  174. return v;
  175. fail:
  176. free(value);
  177. NCDConfig_free_strings(next);
  178. return NULL;
  179. }
  180. int NCDConfig_statement_name_is (struct NCDConfig_statements *st, const char *needle)
  181. {
  182. ASSERT(st->names)
  183. size_t l;
  184. struct NCDConfig_strings *name = st->names;
  185. if (!(l = string_begins_with(needle, name->value))) {
  186. return 0;
  187. }
  188. needle += l;
  189. name = name->next;
  190. while (name) {
  191. if (!(l = string_begins_with(needle, "."))) {
  192. return 0;
  193. }
  194. needle += l;
  195. if (!(l = string_begins_with(needle, name->value))) {
  196. return 0;
  197. }
  198. needle += l;
  199. name = name->next;
  200. }
  201. if (*needle) {
  202. return 0;
  203. }
  204. return 1;
  205. }
  206. struct NCDConfig_statements * NCDConfig_find_statement (struct NCDConfig_statements *st, const char *needle)
  207. {
  208. while (st) {
  209. if (NCDConfig_statement_name_is(st, needle)) {
  210. return st;
  211. }
  212. st = st->next;
  213. }
  214. return NULL;
  215. }
  216. char * NCDConfig_concat_strings (struct NCDConfig_strings *s)
  217. {
  218. ExpString str;
  219. if (!ExpString_Init(&str)) {
  220. goto fail0;
  221. }
  222. if (!ExpString_Append(&str, s->value)) {
  223. goto fail1;
  224. }
  225. s = s->next;
  226. while (s) {
  227. if (!ExpString_Append(&str, ".")) {
  228. goto fail1;
  229. }
  230. if (!ExpString_Append(&str, s->value)) {
  231. goto fail1;
  232. }
  233. s = s->next;
  234. }
  235. return ExpString_Get(&str);
  236. fail1:
  237. ExpString_Free(&str);
  238. fail0:
  239. return NULL;
  240. }