NCDConfig.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <misc/debug.h>
  34. #include <ncd/NCDConfig.h>
  35. void NCDConfig_free_processes (struct NCDConfig_processes *v)
  36. {
  37. if (!v) {
  38. return;
  39. }
  40. free(v->name);
  41. NCDConfig_free_statements(v->statements);
  42. NCDConfig_free_processes(v->next);
  43. free(v);
  44. }
  45. void NCDConfig_free_statements (struct NCDConfig_statements *v)
  46. {
  47. if (!v) {
  48. return;
  49. }
  50. NCDConfig_free_strings(v->objname);
  51. NCDConfig_free_strings(v->names);
  52. NCDConfig_free_list(v->args);
  53. free(v->name);
  54. NCDConfig_free_statements(v->next);
  55. free(v);
  56. }
  57. void NCDConfig_free_list (struct NCDConfig_list *v)
  58. {
  59. if (!v) {
  60. return;
  61. }
  62. switch (v->type) {
  63. case NCDCONFIG_ARG_STRING:
  64. free(v->string);
  65. break;
  66. case NCDCONFIG_ARG_VAR:
  67. NCDConfig_free_strings(v->var);
  68. break;
  69. case NCDCONFIG_ARG_LIST:
  70. case NCDCONFIG_ARG_MAPLIST:
  71. NCDConfig_free_list(v->list);
  72. break;
  73. default:
  74. ASSERT(0);
  75. }
  76. NCDConfig_free_list(v->next);
  77. free(v);
  78. }
  79. void NCDConfig_free_strings (struct NCDConfig_strings *v)
  80. {
  81. if (!v) {
  82. return;
  83. }
  84. free(v->value);
  85. NCDConfig_free_strings(v->next);
  86. free(v);
  87. }
  88. struct NCDConfig_processes * NCDConfig_make_processes (int is_template, char *name, struct NCDConfig_statements *statements, int need_next, struct NCDConfig_processes *next)
  89. {
  90. if (!name || !statements || (need_next && !next)) {
  91. goto fail;
  92. }
  93. struct NCDConfig_processes *v = malloc(sizeof(*v));
  94. if (!v) {
  95. goto fail;
  96. }
  97. v->is_template = is_template;
  98. v->name = name;
  99. v->statements = statements;
  100. v->next = next;
  101. return v;
  102. fail:
  103. free(name);
  104. NCDConfig_free_statements(statements);
  105. NCDConfig_free_processes(next);
  106. return NULL;
  107. }
  108. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *objname, struct NCDConfig_strings *names, struct NCDConfig_list *args, char *name, struct NCDConfig_statements *next)
  109. {
  110. struct NCDConfig_statements *v = malloc(sizeof(*v));
  111. if (!v) {
  112. goto fail;
  113. }
  114. v->objname = objname;
  115. v->names = names;
  116. v->args = args;
  117. v->name = name;
  118. v->next = next;
  119. return v;
  120. fail:
  121. NCDConfig_free_strings(names);
  122. NCDConfig_free_list(args);
  123. free(name);
  124. NCDConfig_free_statements(next);
  125. return NULL;
  126. }
  127. struct NCDConfig_list * NCDConfig_make_list_string (char *str, size_t len, struct NCDConfig_list *next)
  128. {
  129. ASSERT(str[len] == '\0')
  130. struct NCDConfig_list *v = malloc(sizeof(*v));
  131. if (!v) {
  132. goto fail;
  133. }
  134. v->type = NCDCONFIG_ARG_STRING;
  135. v->string = str;
  136. v->string_len = len;
  137. v->next = next;
  138. return v;
  139. fail:
  140. free(str);
  141. NCDConfig_free_list(next);
  142. return NULL;
  143. }
  144. struct NCDConfig_list * NCDConfig_make_list_var (struct NCDConfig_strings *var, struct NCDConfig_list *next)
  145. {
  146. struct NCDConfig_list *v = malloc(sizeof(*v));
  147. if (!v) {
  148. goto fail;
  149. }
  150. v->type = NCDCONFIG_ARG_VAR;
  151. v->var = var;
  152. v->next = next;
  153. return v;
  154. fail:
  155. NCDConfig_free_strings(var);
  156. NCDConfig_free_list(next);
  157. return NULL;
  158. }
  159. struct NCDConfig_list * NCDConfig_make_list_list (struct NCDConfig_list *list, struct NCDConfig_list *next)
  160. {
  161. struct NCDConfig_list *v = malloc(sizeof(*v));
  162. if (!v) {
  163. goto fail;
  164. }
  165. v->type = NCDCONFIG_ARG_LIST;
  166. v->list = list;
  167. v->next = next;
  168. return v;
  169. fail:
  170. NCDConfig_free_list(list);
  171. NCDConfig_free_list(next);
  172. return NULL;
  173. }
  174. struct NCDConfig_list * NCDConfig_make_list_maplist (struct NCDConfig_list *list, struct NCDConfig_list *next)
  175. {
  176. struct NCDConfig_list *v = malloc(sizeof(*v));
  177. if (!v) {
  178. goto fail;
  179. }
  180. v->type = NCDCONFIG_ARG_MAPLIST;
  181. v->list = list;
  182. v->next = next;
  183. return v;
  184. fail:
  185. NCDConfig_free_list(list);
  186. NCDConfig_free_list(next);
  187. return NULL;
  188. }
  189. struct NCDConfig_strings * NCDConfig_make_strings (char *value, int need_next, struct NCDConfig_strings *next)
  190. {
  191. if (!value || (need_next && !next)) {
  192. goto fail;
  193. }
  194. struct NCDConfig_strings *v = malloc(sizeof(*v));
  195. if (!v) {
  196. goto fail;
  197. }
  198. v->value = value;
  199. v->next = next;
  200. return v;
  201. fail:
  202. free(value);
  203. NCDConfig_free_strings(next);
  204. return NULL;
  205. }
  206. int NCDConfig_statement_name_is (struct NCDConfig_statements *st, const char *needle)
  207. {
  208. ASSERT(st->names)
  209. size_t l;
  210. struct NCDConfig_strings *name = st->names;
  211. if (!(l = string_begins_with(needle, name->value))) {
  212. return 0;
  213. }
  214. needle += l;
  215. name = name->next;
  216. while (name) {
  217. if (!(l = string_begins_with(needle, "."))) {
  218. return 0;
  219. }
  220. needle += l;
  221. if (!(l = string_begins_with(needle, name->value))) {
  222. return 0;
  223. }
  224. needle += l;
  225. name = name->next;
  226. }
  227. if (*needle) {
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. struct NCDConfig_statements * NCDConfig_find_statement (struct NCDConfig_statements *st, const char *needle)
  233. {
  234. while (st) {
  235. if (NCDConfig_statement_name_is(st, needle)) {
  236. return st;
  237. }
  238. st = st->next;
  239. }
  240. return NULL;
  241. }
  242. char * NCDConfig_concat_strings (struct NCDConfig_strings *s)
  243. {
  244. ExpString str;
  245. if (!ExpString_Init(&str)) {
  246. goto fail0;
  247. }
  248. if (!ExpString_Append(&str, s->value)) {
  249. goto fail1;
  250. }
  251. s = s->next;
  252. while (s) {
  253. if (!ExpString_Append(&str, ".")) {
  254. goto fail1;
  255. }
  256. if (!ExpString_Append(&str, s->value)) {
  257. goto fail1;
  258. }
  259. s = s->next;
  260. }
  261. return ExpString_Get(&str);
  262. fail1:
  263. ExpString_Free(&str);
  264. fail0:
  265. return NULL;
  266. }