ncd_parser_test.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * @file ncd_parser_test.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 <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <inttypes.h>
  34. #include <misc/debug.h>
  35. #include <misc/expstring.h>
  36. #include <base/BLog.h>
  37. #include <ncd/NCDConfigParser.h>
  38. #include <ncd/NCDValGenerator.h>
  39. #include <ncd/NCDSugar.h>
  40. static int generate_val (NCDValue *value, ExpString *out_str)
  41. {
  42. switch (NCDValue_Type(value)) {
  43. case NCDVALUE_STRING: {
  44. const char *str = NCDValue_StringValue(value);
  45. size_t len = NCDValue_StringLength(value);
  46. if (!ExpString_AppendChar(out_str, '"')) {
  47. goto fail;
  48. }
  49. for (size_t i = 0; i < len; i++) {
  50. if (str[i] == '\0') {
  51. char buf[5];
  52. snprintf(buf, sizeof(buf), "\\x%02"PRIx8, (uint8_t)str[i]);
  53. if (!ExpString_Append(out_str, buf)) {
  54. goto fail;
  55. }
  56. continue;
  57. }
  58. if (str[i] == '"' || str[i] == '\\') {
  59. if (!ExpString_AppendChar(out_str, '\\')) {
  60. goto fail;
  61. }
  62. }
  63. if (!ExpString_AppendChar(out_str, str[i])) {
  64. goto fail;
  65. }
  66. }
  67. if (!ExpString_AppendChar(out_str, '"')) {
  68. goto fail;
  69. }
  70. } break;
  71. case NCDVALUE_LIST: {
  72. if (!ExpString_AppendChar(out_str, '{')) {
  73. goto fail;
  74. }
  75. int is_first = 1;
  76. for (NCDValue *e = NCDValue_ListFirst(value); e; e = NCDValue_ListNext(value, e)) {
  77. if (!is_first) {
  78. if (!ExpString_Append(out_str, ", ")) {
  79. goto fail;
  80. }
  81. }
  82. if (!generate_val(e, out_str)) {
  83. goto fail;
  84. }
  85. is_first = 0;
  86. }
  87. if (!ExpString_AppendChar(out_str, '}')) {
  88. goto fail;
  89. }
  90. } break;
  91. case NCDVALUE_MAP: {
  92. if (!ExpString_AppendChar(out_str, '[')) {
  93. goto fail;
  94. }
  95. int is_first = 1;
  96. for (NCDValue *ekey = NCDValue_MapFirstKey(value); ekey; ekey = NCDValue_MapNextKey(value, ekey)) {
  97. NCDValue *eval = NCDValue_MapKeyValue(value, ekey);
  98. if (!is_first) {
  99. if (!ExpString_Append(out_str, ", ")) {
  100. goto fail;
  101. }
  102. }
  103. if (!generate_val(ekey, out_str)) {
  104. goto fail;
  105. }
  106. if (!ExpString_AppendChar(out_str, ':')) {
  107. goto fail;
  108. }
  109. if (!generate_val(eval, out_str)) {
  110. goto fail;
  111. }
  112. is_first = 0;
  113. }
  114. if (!ExpString_AppendChar(out_str, ']')) {
  115. goto fail;
  116. }
  117. } break;
  118. case NCDVALUE_VAR: {
  119. if (!ExpString_Append(out_str, NCDValue_VarName(value))) {
  120. goto fail;
  121. }
  122. } break;
  123. default: ASSERT(0);
  124. }
  125. return 1;
  126. fail:
  127. return 0;
  128. }
  129. static void print_indent (unsigned int indent)
  130. {
  131. while (indent > 0) {
  132. printf(" ");
  133. indent--;
  134. }
  135. }
  136. static void print_value (NCDValue *v, unsigned int indent)
  137. {
  138. ExpString estr;
  139. if (!ExpString_Init(&estr)) {
  140. DEBUG("ExpString_Init failed");
  141. exit(1);
  142. }
  143. if (!generate_val(v, &estr)) {
  144. DEBUG("generate_val failed");
  145. exit(1);
  146. }
  147. print_indent(indent);
  148. printf("%s\n", ExpString_Get(&estr));
  149. ExpString_Free(&estr);
  150. }
  151. static void print_block (NCDBlock *block, unsigned int indent)
  152. {
  153. for (NCDStatement *st = NCDBlock_FirstStatement(block); st; st = NCDBlock_NextStatement(block, st)) {
  154. const char *name = NCDStatement_Name(st) ? NCDStatement_Name(st) : "";
  155. switch (NCDStatement_Type(st)) {
  156. case NCDSTATEMENT_REG: {
  157. const char *objname = NCDStatement_RegObjName(st) ? NCDStatement_RegObjName(st) : "";
  158. const char *cmdname = NCDStatement_RegCmdName(st);
  159. print_indent(indent);
  160. printf("reg name=%s objname=%s cmdname=%s args:\n", name, objname, cmdname);
  161. print_value(NCDStatement_RegArgs(st), indent + 2);
  162. } break;
  163. case NCDSTATEMENT_IF: {
  164. print_indent(indent);
  165. printf("if name=%s\n", name);
  166. NCDIfBlock *ifb = NCDStatement_IfBlock(st);
  167. for (NCDIf *ifc = NCDIfBlock_FirstIf(ifb); ifc; ifc = NCDIfBlock_NextIf(ifb, ifc)) {
  168. print_indent(indent + 2);
  169. printf("if\n");
  170. print_value(NCDIf_Cond(ifc), indent + 4);
  171. print_indent(indent + 2);
  172. printf("then\n");
  173. print_block(NCDIf_Block(ifc), indent + 4);
  174. }
  175. if (NCDStatement_IfElse(st)) {
  176. print_indent(indent + 2);
  177. printf("else\n");
  178. print_block(NCDStatement_IfElse(st), indent + 4);
  179. }
  180. } break;
  181. case NCDSTATEMENT_FOREACH: {
  182. const char *name1 = NCDStatement_ForeachName1(st);
  183. const char *name2 = NCDStatement_ForeachName2(st) ? NCDStatement_ForeachName2(st) : "";
  184. print_indent(indent);
  185. printf("foreach name=%s name1=%s name2=%s\n", name, name1, name2);
  186. print_block(NCDStatement_ForeachBlock(st), indent + 2);
  187. } break;
  188. default: ASSERT(0);
  189. }
  190. }
  191. }
  192. int main (int argc, char **argv)
  193. {
  194. int res = 1;
  195. if (argc != 3) {
  196. printf("Usage: %s <desugar=0/1> <string>\n", (argc > 0 ? argv[0] : ""));
  197. goto fail0;
  198. }
  199. int desugar = atoi(argv[1]);
  200. char *text = argv[2];
  201. BLog_InitStdout();
  202. // parse
  203. NCDProgram prog;
  204. if (!NCDConfigParser_Parse(text, strlen(text), &prog)) {
  205. DEBUG("NCDConfigParser_Parse failed");
  206. goto fail1;
  207. }
  208. // desugar
  209. if (desugar) {
  210. if (!NCDSugar_Desugar(&prog)) {
  211. DEBUG("NCDSugar_Desugar failed");
  212. goto fail2;
  213. }
  214. }
  215. // print
  216. for (NCDProgramElem *elem = NCDProgram_FirstElem(&prog); elem; elem = NCDProgram_NextElem(&prog, elem)) {
  217. switch (NCDProgramElem_Type(elem)) {
  218. case NCDPROGRAMELEM_PROCESS: {
  219. NCDProcess *p = NCDProgramElem_Process(elem);
  220. printf("process name=%s is_template=%d\n", NCDProcess_Name(p), NCDProcess_IsTemplate(p));
  221. print_block(NCDProcess_Block(p), 2);
  222. } break;
  223. case NCDPROGRAMELEM_INCLUDE: {
  224. printf("include path=%s\n", NCDProgramElem_IncludePathData(elem));
  225. } break;
  226. case NCDPROGRAMELEM_INCLUDE_GUARD: {
  227. printf("include_guard id=%s\n", NCDProgramElem_IncludeGuardIdData(elem));
  228. } break;
  229. default: ASSERT(0);
  230. }
  231. }
  232. res = 0;
  233. fail2:
  234. NCDProgram_Free(&prog);
  235. fail1:
  236. BLog_Free();
  237. fail0:
  238. return res;
  239. }