NCDConfig.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <ncdconfig/NCDConfig.h>
  26. void NCDConfig_free_interfaces (struct NCDConfig_interfaces *v)
  27. {
  28. if (!v) {
  29. return;
  30. }
  31. free(v->name);
  32. NCDConfig_free_statements(v->statements);
  33. NCDConfig_free_interfaces(v->next);
  34. free(v);
  35. }
  36. void NCDConfig_free_statements (struct NCDConfig_statements *v)
  37. {
  38. if (!v) {
  39. return;
  40. }
  41. NCDConfig_free_strings(v->names);
  42. NCDConfig_free_strings(v->args);
  43. NCDConfig_free_statements(v->next);
  44. free(v);
  45. }
  46. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  47. {
  48. if (!v) {
  49. return;
  50. }
  51. free(v->value);
  52. NCDConfig_free_strings(v->next);
  53. free(v);
  54. }
  55. struct NCDConfig_interfaces * NCDConfig_make_interfaces (char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_interfaces *next)
  56. {
  57. if (!name || !statements || (need_next && !next)) {
  58. goto fail;
  59. }
  60. struct NCDConfig_interfaces *v = malloc(sizeof(*v));
  61. if (!v) {
  62. goto fail;
  63. }
  64. v->name = name;
  65. v->statements = statements;
  66. v->next = next;
  67. return v;
  68. fail:
  69. free(name);
  70. NCDConfig_free_statements(statements);
  71. NCDConfig_free_interfaces(next);
  72. return NULL;
  73. }
  74. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *names, int need_args, struct NCDConfig_strings *args, int need_next, struct NCDConfig_statements *next)
  75. {
  76. if (!names || (need_args && !args) || (need_next && !next)) {
  77. goto fail;
  78. }
  79. struct NCDConfig_statements *v = malloc(sizeof(*v));
  80. if (!v) {
  81. goto fail;
  82. }
  83. v->names = names;
  84. v->args = args;
  85. v->next = next;
  86. return v;
  87. fail:
  88. NCDConfig_free_strings(names);
  89. NCDConfig_free_strings(args);
  90. NCDConfig_free_statements(next);
  91. return NULL;
  92. }
  93. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  94. {
  95. if (!value || (need_next && !next)) {
  96. goto fail;
  97. }
  98. struct NCDConfig_strings *v = malloc(sizeof(*v));
  99. if (!v) {
  100. goto fail;
  101. }
  102. v->value = value;
  103. v->next = next;
  104. return v;
  105. fail:
  106. free(value);
  107. NCDConfig_free_strings(next);
  108. return NULL;
  109. }
  110. int NCDConfig_statement_name_is (struct NCDConfig_statements *st, const char *needle)
  111. {
  112. ASSERT(st->names)
  113. size_t l;
  114. struct NCDConfig_strings *name = st->names;
  115. if (!(l = string_begins_with(needle, name->value))) {
  116. return 0;
  117. }
  118. needle += l;
  119. name = name->next;
  120. while (name) {
  121. if (!(l = string_begins_with(needle, "."))) {
  122. return 0;
  123. }
  124. needle += l;
  125. if (!(l = string_begins_with(needle, name->value))) {
  126. return 0;
  127. }
  128. needle += l;
  129. name = name->next;
  130. }
  131. if (*needle) {
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. struct NCDConfig_statements * NCDConfig_find_statement (struct NCDConfig_statements *st, const char *needle)
  137. {
  138. while (st) {
  139. if (NCDConfig_statement_name_is(st, needle)) {
  140. return st;
  141. }
  142. st = st->next;
  143. }
  144. return NULL;
  145. }
  146. int NCDConfig_statement_has_one_arg (struct NCDConfig_statements *st, char **arg1_out)
  147. {
  148. if (!(st->args && !st->args->next)) {
  149. return 0;
  150. }
  151. *arg1_out = st->args->value;
  152. return 1;
  153. }
  154. int NCDConfig_statement_has_two_args (struct NCDConfig_statements *st, char **arg1_out, char **arg2_out)
  155. {
  156. if (!(st->args && st->args->next && !st->args->next->next)) {
  157. return 0;
  158. }
  159. *arg1_out = st->args->value;
  160. *arg2_out = st->args->next->value;
  161. return 1;
  162. }
  163. int NCDConfig_statement_has_three_args (struct NCDConfig_statements *st, char **arg1_out, char **arg2_out, char **arg3_out)
  164. {
  165. if (!(st->args && st->args->next && st->args->next->next && !st->args->next->next->next)) {
  166. return 0;
  167. }
  168. *arg1_out = st->args->value;
  169. *arg2_out = st->args->next->value;
  170. *arg3_out = st->args->next->next->value;
  171. return 1;
  172. }