NCDConfig.c 7.4 KB

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