NCDConfig.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * @file NCDConfig.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/string_begins_with.h>
  32. #include <misc/expstring.h>
  33. #include <ncd/NCDConfig.h>
  34. void NCDConfig_free_processes (struct NCDConfig_processes *v)
  35. {
  36. if (!v) {
  37. return;
  38. }
  39. free(v->name);
  40. NCDConfig_free_statements(v->statements);
  41. NCDConfig_free_processes(v->next);
  42. free(v);
  43. }
  44. void NCDConfig_free_statements (struct NCDConfig_statements *v)
  45. {
  46. if (!v) {
  47. return;
  48. }
  49. NCDConfig_free_strings(v->objname);
  50. NCDConfig_free_strings(v->names);
  51. NCDConfig_free_list(v->args);
  52. free(v->name);
  53. NCDConfig_free_statements(v->next);
  54. free(v);
  55. }
  56. void NCDConfig_free_list (struct NCDConfig_list *v)
  57. {
  58. if (!v) {
  59. return;
  60. }
  61. switch (v->type) {
  62. case NCDCONFIG_ARG_STRING:
  63. free(v->string);
  64. break;
  65. case NCDCONFIG_ARG_VAR:
  66. NCDConfig_free_strings(v->var);
  67. break;
  68. case NCDCONFIG_ARG_LIST:
  69. NCDConfig_free_list(v->list);
  70. break;
  71. default:
  72. ASSERT(0);
  73. }
  74. NCDConfig_free_list(v->next);
  75. free(v);
  76. }
  77. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  78. {
  79. if (!v) {
  80. return;
  81. }
  82. free(v->value);
  83. NCDConfig_free_strings(v->next);
  84. free(v);
  85. }
  86. struct NCDConfig_processes * NCDConfig_make_processes (int is_template, char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_processes *next)
  87. {
  88. if (!name || !statements || (need_next && !next)) {
  89. goto fail;
  90. }
  91. struct NCDConfig_processes *v = malloc(sizeof(*v));
  92. if (!v) {
  93. goto fail;
  94. }
  95. v->is_template = is_template;
  96. v->name = name;
  97. v->statements = statements;
  98. v->next = next;
  99. return v;
  100. fail:
  101. free(name);
  102. NCDConfig_free_statements(statements);
  103. NCDConfig_free_processes(next);
  104. return NULL;
  105. }
  106. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *objname, struct NCDConfig_strings *names, struct NCDConfig_list *args, char *name, struct NCDConfig_statements *next)
  107. {
  108. struct NCDConfig_statements *v = malloc(sizeof(*v));
  109. if (!v) {
  110. goto fail;
  111. }
  112. v->objname = objname;
  113. v->names = names;
  114. v->args = args;
  115. v->name = name;
  116. v->next = next;
  117. return v;
  118. fail:
  119. NCDConfig_free_strings(names);
  120. NCDConfig_free_list(args);
  121. free(name);
  122. NCDConfig_free_statements(next);
  123. return NULL;
  124. }
  125. struct NCDConfig_list * NCDConfig_make_list_string (char *str, struct NCDConfig_list *next)
  126. {
  127. struct NCDConfig_list *v = malloc(sizeof(*v));
  128. if (!v) {
  129. goto fail;
  130. }
  131. v->type = NCDCONFIG_ARG_STRING;
  132. v->string = str;
  133. v->next = next;
  134. return v;
  135. fail:
  136. free(str);
  137. NCDConfig_free_list(next);
  138. return NULL;
  139. }
  140. struct NCDConfig_list * NCDConfig_make_list_var (struct NCDConfig_strings *var, struct NCDConfig_list *next)
  141. {
  142. struct NCDConfig_list *v = malloc(sizeof(*v));
  143. if (!v) {
  144. goto fail;
  145. }
  146. v->type = NCDCONFIG_ARG_VAR;
  147. v->var = var;
  148. v->next = next;
  149. return v;
  150. fail:
  151. NCDConfig_free_strings(var);
  152. NCDConfig_free_list(next);
  153. return NULL;
  154. }
  155. struct NCDConfig_list * NCDConfig_make_list_list (struct NCDConfig_list *list, struct NCDConfig_list *next)
  156. {
  157. struct NCDConfig_list *v = malloc(sizeof(*v));
  158. if (!v) {
  159. goto fail;
  160. }
  161. v->type = NCDCONFIG_ARG_LIST;
  162. v->list = list;
  163. v->next = next;
  164. return v;
  165. fail:
  166. NCDConfig_free_list(list);
  167. NCDConfig_free_list(next);
  168. return NULL;
  169. }
  170. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  171. {
  172. if (!value || (need_next && !next)) {
  173. goto fail;
  174. }
  175. struct NCDConfig_strings *v = malloc(sizeof(*v));
  176. if (!v) {
  177. goto fail;
  178. }
  179. v->value = value;
  180. v->next = next;
  181. return v;
  182. fail:
  183. free(value);
  184. NCDConfig_free_strings(next);
  185. return NULL;
  186. }
  187. int NCDConfig_statement_name_is (struct NCDConfig_statements *st, const char *needle)
  188. {
  189. ASSERT(st->names)
  190. size_t l;
  191. struct NCDConfig_strings *name = st->names;
  192. if (!(l = string_begins_with(needle, name->value))) {
  193. return 0;
  194. }
  195. needle += l;
  196. name = name->next;
  197. while (name) {
  198. if (!(l = string_begins_with(needle, "."))) {
  199. return 0;
  200. }
  201. needle += l;
  202. if (!(l = string_begins_with(needle, name->value))) {
  203. return 0;
  204. }
  205. needle += l;
  206. name = name->next;
  207. }
  208. if (*needle) {
  209. return 0;
  210. }
  211. return 1;
  212. }
  213. struct NCDConfig_statements * NCDConfig_find_statement (struct NCDConfig_statements *st, const char *needle)
  214. {
  215. while (st) {
  216. if (NCDConfig_statement_name_is(st, needle)) {
  217. return st;
  218. }
  219. st = st->next;
  220. }
  221. return NULL;
  222. }
  223. char * NCDConfig_concat_strings (struct NCDConfig_strings *s)
  224. {
  225. ExpString str;
  226. if (!ExpString_Init(&str)) {
  227. goto fail0;
  228. }
  229. if (!ExpString_Append(&str, s->value)) {
  230. goto fail1;
  231. }
  232. s = s->next;
  233. while (s) {
  234. if (!ExpString_Append(&str, ".")) {
  235. goto fail1;
  236. }
  237. if (!ExpString_Append(&str, s->value)) {
  238. goto fail1;
  239. }
  240. s = s->next;
  241. }
  242. return ExpString_Get(&str);
  243. fail1:
  244. ExpString_Free(&str);
  245. fail0:
  246. return NULL;
  247. }