NCDBuildProgram.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /**
  2. * @file NCDBuildProgram.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 <stddef.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <misc/debug.h>
  33. #include <misc/read_file.h>
  34. #include <misc/strdup.h>
  35. #include <misc/concat_strings.h>
  36. #include <base/BLog.h>
  37. #include <ncd/NCDConfigParser.h>
  38. #include "NCDBuildProgram.h"
  39. #include <generated/blog_channel_NCDBuildProgram.h>
  40. #define MAX_INCLUDE_DEPTH 32
  41. struct guard {
  42. char *id_data;
  43. size_t id_length;
  44. struct guard *next;
  45. };
  46. struct build_state {
  47. struct guard *top_guard;
  48. };
  49. static int add_guard (struct guard **first, const char *id_data, size_t id_length)
  50. {
  51. struct guard *g = malloc(sizeof(*g));
  52. if (!g) {
  53. goto fail0;
  54. }
  55. if (!(g->id_data = b_strdup_bin(id_data, id_length))) {
  56. goto fail1;
  57. }
  58. g->id_length = id_length;
  59. g->next = *first;
  60. *first = g;
  61. return 1;
  62. fail1:
  63. free(g);
  64. fail0:
  65. return 0;
  66. }
  67. static void prepend_guards (struct guard **first, struct guard *guards)
  68. {
  69. if (!guards) {
  70. return;
  71. }
  72. struct guard *last = guards;
  73. while (last->next) {
  74. last = last->next;
  75. }
  76. last->next = *first;
  77. *first = guards;
  78. }
  79. static void free_guards (struct guard *g)
  80. {
  81. while (g) {
  82. struct guard *next_g = g->next;
  83. free(g->id_data);
  84. free(g);
  85. g = next_g;
  86. }
  87. }
  88. static int guard_exists (struct guard *top_guard, const char *id_data, size_t id_length)
  89. {
  90. for (struct guard *g = top_guard; g; g = g->next) {
  91. if (g->id_length == id_length && !memcmp(g->id_data, id_data, id_length)) {
  92. return 1;
  93. }
  94. }
  95. return 0;
  96. }
  97. static char * make_dir_path (const char *file_path)
  98. {
  99. int found_slash = 0;
  100. size_t last_slash;
  101. for (size_t i = 0; file_path[i]; i++) {
  102. if (file_path[i] == '/') {
  103. found_slash = 1;
  104. last_slash = i;
  105. }
  106. }
  107. char *dir_path;
  108. if (!found_slash) {
  109. if (!file_path[0]) {
  110. BLog(BLOG_ERROR, "file '%s': file path must not be empty", file_path);
  111. return NULL;
  112. }
  113. dir_path = b_strdup("");
  114. } else {
  115. if (!file_path[last_slash + 1]) {
  116. BLog(BLOG_ERROR, "file '%s': file path must not end in a slash", file_path);
  117. return NULL;
  118. }
  119. dir_path = b_strdup_bin(file_path, last_slash + 1);
  120. }
  121. if (!dir_path) {
  122. BLog(BLOG_ERROR, "file '%s': b_strdup/b_strdup_bin failed", file_path);
  123. return NULL;
  124. }
  125. return dir_path;
  126. }
  127. static char * make_include_path (const char *file_path, const char *dir_path, const char *target, size_t target_len)
  128. {
  129. ASSERT(target_len == strlen(target))
  130. if (target_len == 0) {
  131. BLog(BLOG_ERROR, "file '%s': include target must not be empty", file_path);
  132. return NULL;
  133. }
  134. if (target[target_len - 1] == '/') {
  135. BLog(BLOG_ERROR, "file '%s': include target must not end in a slash", file_path);
  136. return NULL;
  137. }
  138. char *real_target;
  139. if (target[0] == '/') {
  140. real_target = b_strdup(target);
  141. } else {
  142. real_target = concat_strings(2, dir_path, target);
  143. }
  144. if (!real_target) {
  145. BLog(BLOG_ERROR, "file '%s': b_strdup/concat_strings failed", file_path);
  146. return NULL;
  147. }
  148. return real_target;
  149. }
  150. static int process_file (struct build_state *st, int depth, const char *file_path, NCDProgram *out_program, int *out_guarded)
  151. {
  152. int ret_val = 0;
  153. if (depth > MAX_INCLUDE_DEPTH) {
  154. BLog(BLOG_ERROR, "file '%s': maximum include depth (%d) exceeded (include cycle?)", file_path, (int)MAX_INCLUDE_DEPTH);
  155. goto fail0;
  156. }
  157. char *dir_path = make_dir_path(file_path);
  158. if (!dir_path) {
  159. goto fail0;
  160. }
  161. uint8_t *data;
  162. size_t len;
  163. if (!read_file(file_path, &data, &len)) {
  164. BLog(BLOG_ERROR, "file '%s': failed to read contents", file_path);
  165. goto fail1;
  166. }
  167. NCDProgram program;
  168. int res = NCDConfigParser_Parse((char *)data, len, &program);
  169. free(data);
  170. if (!res) {
  171. BLog(BLOG_ERROR, "file '%s': failed to parse", file_path);
  172. goto fail1;
  173. }
  174. struct guard *our_guards = NULL;
  175. NCDProgramElem *elem = NCDProgram_FirstElem(&program);
  176. while (elem) {
  177. NCDProgramElem *next_elem = NCDProgram_NextElem(&program, elem);
  178. if (NCDProgramElem_Type(elem) != NCDPROGRAMELEM_INCLUDE_GUARD) {
  179. elem = next_elem;
  180. continue;
  181. }
  182. const char *id_data = NCDProgramElem_IncludeGuardIdData(elem);
  183. size_t id_length = NCDProgramElem_IncludeGuardIdLength(elem);
  184. if (guard_exists(st->top_guard, id_data, id_length)) {
  185. *out_guarded = 1;
  186. ret_val = 1;
  187. goto fail2;
  188. }
  189. if (!add_guard(&our_guards, id_data, id_length)) {
  190. BLog(BLOG_ERROR, "file '%s': add_guard failed", file_path);
  191. goto fail2;
  192. }
  193. NCDProgram_RemoveElem(&program, elem);
  194. elem = next_elem;
  195. }
  196. prepend_guards(&st->top_guard, our_guards);
  197. our_guards = NULL;
  198. elem = NCDProgram_FirstElem(&program);
  199. while (elem) {
  200. NCDProgramElem *next_elem = NCDProgram_NextElem(&program, elem);
  201. if (NCDProgramElem_Type(elem) != NCDPROGRAMELEM_INCLUDE) {
  202. elem = next_elem;
  203. continue;
  204. }
  205. const char *target = NCDProgramElem_IncludePathData(elem);
  206. size_t target_len = NCDProgramElem_IncludePathLength(elem);
  207. if (strlen(target) != target_len) {
  208. BLog(BLOG_ERROR, "file '%s': include path must not contain null characters", file_path);
  209. goto fail2;
  210. }
  211. char *real_target = make_include_path(file_path, dir_path, target, target_len);
  212. if (!real_target) {
  213. goto fail2;
  214. }
  215. NCDProgram included_program;
  216. int included_guarded;
  217. int res = process_file(st, depth + 1, real_target, &included_program, &included_guarded);
  218. free(real_target);
  219. if (!res) {
  220. goto fail2;
  221. }
  222. if (included_guarded) {
  223. NCDProgram_RemoveElem(&program, elem);
  224. } else {
  225. if (!NCDProgram_ReplaceElemWithProgram(&program, elem, included_program)) {
  226. BLog(BLOG_ERROR, "file '%s': NCDProgram_ReplaceElemWithProgram failed", file_path);
  227. NCDProgram_Free(&included_program);
  228. goto fail2;
  229. }
  230. }
  231. elem = next_elem;
  232. }
  233. free(dir_path);
  234. *out_program = program;
  235. *out_guarded = 0;
  236. return 1;
  237. fail2:
  238. free_guards(our_guards);
  239. NCDProgram_Free(&program);
  240. fail1:
  241. free(dir_path);
  242. fail0:
  243. return ret_val;
  244. }
  245. int NCDBuildProgram_Build (const char *file_path, NCDProgram *out_program)
  246. {
  247. ASSERT(file_path)
  248. ASSERT(out_program)
  249. struct build_state st;
  250. st.top_guard = NULL;
  251. int guarded;
  252. int res = process_file(&st, 0, file_path, out_program, &guarded);
  253. ASSERT(!res || !guarded)
  254. free_guards(st.top_guard);
  255. return res;
  256. }