NCDConfig.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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 <ncdconfig/NCDConfig.h>
  27. void NCDConfig_free_interfaces (struct NCDConfig_interfaces *v)
  28. {
  29. if (!v) {
  30. return;
  31. }
  32. free(v->name);
  33. NCDConfig_free_statements(v->statements);
  34. NCDConfig_free_interfaces(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->names);
  43. NCDConfig_free_arguments(v->args);
  44. free(v->name);
  45. NCDConfig_free_statements(v->next);
  46. free(v);
  47. }
  48. void NCDConfig_free_arguments (struct NCDConfig_arguments *v)
  49. {
  50. if (!v) {
  51. return;
  52. }
  53. switch (v->type) {
  54. case NCDCONFIG_ARG_STRING:
  55. free(v->string);
  56. break;
  57. case NCDCONFIG_ARG_VAR:
  58. NCDConfig_free_strings(v->var);
  59. break;
  60. default:
  61. ASSERT(0);
  62. }
  63. NCDConfig_free_arguments(v->next);
  64. free(v);
  65. }
  66. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  67. {
  68. if (!v) {
  69. return;
  70. }
  71. free(v->value);
  72. NCDConfig_free_strings(v->next);
  73. free(v);
  74. }
  75. struct NCDConfig_interfaces * NCDConfig_make_interfaces (char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_interfaces *next)
  76. {
  77. if (!name || !statements || (need_next && !next)) {
  78. goto fail;
  79. }
  80. struct NCDConfig_interfaces *v = malloc(sizeof(*v));
  81. if (!v) {
  82. goto fail;
  83. }
  84. v->name = name;
  85. v->statements = statements;
  86. v->next = next;
  87. return v;
  88. fail:
  89. free(name);
  90. NCDConfig_free_statements(statements);
  91. NCDConfig_free_interfaces(next);
  92. return NULL;
  93. }
  94. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *names, struct NCDConfig_arguments *args, char *name, struct NCDConfig_statements *next)
  95. {
  96. struct NCDConfig_statements *v = malloc(sizeof(*v));
  97. if (!v) {
  98. goto fail;
  99. }
  100. v->names = names;
  101. v->args = args;
  102. v->name = name;
  103. v->next = next;
  104. return v;
  105. fail:
  106. NCDConfig_free_strings(names);
  107. NCDConfig_free_arguments(args);
  108. free(name);
  109. NCDConfig_free_statements(next);
  110. return NULL;
  111. }
  112. struct NCDConfig_arguments * NCDConfig_make_arguments_string (char *str, int need_next, struct NCDConfig_arguments *next)
  113. {
  114. if (!str || (need_next && !next)) {
  115. goto fail;
  116. }
  117. struct NCDConfig_arguments *v = malloc(sizeof(*v));
  118. if (!v) {
  119. goto fail;
  120. }
  121. v->type = NCDCONFIG_ARG_STRING;
  122. v->string = str;
  123. v->next = next;
  124. return v;
  125. fail:
  126. free(str);
  127. NCDConfig_free_arguments(next);
  128. return NULL;
  129. }
  130. struct NCDConfig_arguments * NCDConfig_make_arguments_var (struct NCDConfig_strings *var, int need_next, struct NCDConfig_arguments *next)
  131. {
  132. if (!var || (need_next && !next)) {
  133. goto fail;
  134. }
  135. struct NCDConfig_arguments *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_arguments(next);
  146. return NULL;
  147. }
  148. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  149. {
  150. if (!value || (need_next && !next)) {
  151. goto fail;
  152. }
  153. struct NCDConfig_strings *v = malloc(sizeof(*v));
  154. if (!v) {
  155. goto fail;
  156. }
  157. v->value = value;
  158. v->next = next;
  159. return v;
  160. fail:
  161. free(value);
  162. NCDConfig_free_strings(next);
  163. return NULL;
  164. }
  165. int NCDConfig_statement_name_is (struct NCDConfig_statements *st, const char *needle)
  166. {
  167. ASSERT(st->names)
  168. size_t l;
  169. struct NCDConfig_strings *name = st->names;
  170. if (!(l = string_begins_with(needle, name->value))) {
  171. return 0;
  172. }
  173. needle += l;
  174. name = name->next;
  175. while (name) {
  176. if (!(l = string_begins_with(needle, "."))) {
  177. return 0;
  178. }
  179. needle += l;
  180. if (!(l = string_begins_with(needle, name->value))) {
  181. return 0;
  182. }
  183. needle += l;
  184. name = name->next;
  185. }
  186. if (*needle) {
  187. return 0;
  188. }
  189. return 1;
  190. }
  191. struct NCDConfig_statements * NCDConfig_find_statement (struct NCDConfig_statements *st, const char *needle)
  192. {
  193. while (st) {
  194. if (NCDConfig_statement_name_is(st, needle)) {
  195. return st;
  196. }
  197. st = st->next;
  198. }
  199. return NULL;
  200. }
  201. char * NCDConfig_concat_strings (struct NCDConfig_strings *s)
  202. {
  203. ExpString str;
  204. if (!ExpString_Init(&str)) {
  205. goto fail0;
  206. }
  207. if (!ExpString_Append(&str, s->value)) {
  208. goto fail1;
  209. }
  210. s = s->next;
  211. while (s) {
  212. if (!ExpString_Append(&str, ".")) {
  213. goto fail1;
  214. }
  215. if (!ExpString_Append(&str, s->value)) {
  216. goto fail1;
  217. }
  218. s = s->next;
  219. }
  220. return ExpString_Get(&str);
  221. fail1:
  222. ExpString_Free(&str);
  223. fail0:
  224. return NULL;
  225. }