NCDConfig.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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, struct NCDConfig_processes *next)
  89. {
  90. struct NCDConfig_processes *v = malloc(sizeof(*v));
  91. if (!v) {
  92. goto fail;
  93. }
  94. v->is_template = is_template;
  95. v->name = name;
  96. v->statements = statements;
  97. v->next = next;
  98. return v;
  99. fail:
  100. free(name);
  101. NCDConfig_free_statements(statements);
  102. NCDConfig_free_processes(next);
  103. return NULL;
  104. }
  105. struct NCDConfig_statements * NCDConfig_make_statements (struct NCDConfig_strings *objname, struct NCDConfig_strings *names, struct NCDConfig_list *args, char *name, struct NCDConfig_statements *next)
  106. {
  107. struct NCDConfig_statements *v = malloc(sizeof(*v));
  108. if (!v) {
  109. goto fail;
  110. }
  111. v->objname = objname;
  112. v->names = names;
  113. v->args = args;
  114. v->name = name;
  115. v->next = next;
  116. return v;
  117. fail:
  118. NCDConfig_free_strings(names);
  119. NCDConfig_free_list(args);
  120. free(name);
  121. NCDConfig_free_statements(next);
  122. return NULL;
  123. }
  124. struct NCDConfig_list * NCDConfig_make_list_string (char *str, size_t len, struct NCDConfig_list *next)
  125. {
  126. ASSERT(str[len] == '\0')
  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->string_len = len;
  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. char * NCDConfig_concat_strings (struct NCDConfig_strings *s)
  204. {
  205. ExpString str;
  206. if (!ExpString_Init(&str)) {
  207. goto fail0;
  208. }
  209. if (!ExpString_Append(&str, s->value)) {
  210. goto fail1;
  211. }
  212. s = s->next;
  213. while (s) {
  214. if (!ExpString_Append(&str, ".")) {
  215. goto fail1;
  216. }
  217. if (!ExpString_Append(&str, s->value)) {
  218. goto fail1;
  219. }
  220. s = s->next;
  221. }
  222. return ExpString_Get(&str);
  223. fail1:
  224. ExpString_Free(&str);
  225. fail0:
  226. return NULL;
  227. }