ncd_parser_test.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. default: ASSERT(0);
  119. }
  120. return 1;
  121. fail:
  122. return 0;
  123. }
  124. static void print_indent (unsigned int indent)
  125. {
  126. while (indent > 0) {
  127. printf(" ");
  128. indent--;
  129. }
  130. }
  131. static void print_value (NCDValue *v, unsigned int indent)
  132. {
  133. ExpString estr;
  134. if (!ExpString_Init(&estr)) {
  135. DEBUG("ExpString_Init failed");
  136. exit(1);
  137. }
  138. if (!generate_val(v, &estr)) {
  139. DEBUG("generate_val failed");
  140. exit(1);
  141. }
  142. print_indent(indent);
  143. printf("%s\n", ExpString_Get(&estr));
  144. ExpString_Free(&estr);
  145. }
  146. static void print_block (NCDBlock *block, unsigned int indent)
  147. {
  148. for (NCDStatement *st = NCDBlock_FirstStatement(block); st; st = NCDBlock_NextStatement(block, st)) {
  149. const char *name = NCDStatement_Name(st) ? NCDStatement_Name(st) : "";
  150. switch (NCDStatement_Type(st)) {
  151. case NCDSTATEMENT_REG: {
  152. const char *objname = NCDStatement_RegObjName(st) ? NCDStatement_RegObjName(st) : "";
  153. const char *cmdname = NCDStatement_RegCmdName(st);
  154. print_indent(indent);
  155. printf("reg name=%s objname=%s cmdname=%s args:\n", name, objname, cmdname);
  156. print_value(NCDStatement_RegArgs(st), indent + 2);
  157. } break;
  158. case NCDSTATEMENT_IF: {
  159. print_indent(indent);
  160. printf("if name=%s\n", name);
  161. NCDIfBlock *ifb = NCDStatement_IfBlock(st);
  162. for (NCDIf *ifc = NCDIfBlock_FirstIf(ifb); ifc; ifc = NCDIfBlock_NextIf(ifb, ifc)) {
  163. print_indent(indent + 2);
  164. printf("if\n");
  165. print_value(NCDIf_Cond(ifc), indent + 4);
  166. print_indent(indent + 2);
  167. printf("then\n");
  168. print_block(NCDIf_Block(ifc), indent + 4);
  169. }
  170. if (NCDStatement_IfElse(st)) {
  171. print_indent(indent + 2);
  172. printf("else\n");
  173. print_block(NCDStatement_IfElse(st), indent + 4);
  174. }
  175. } break;
  176. case NCDSTATEMENT_FOREACH: {
  177. const char *name1 = NCDStatement_ForeachName1(st);
  178. const char *name2 = NCDStatement_ForeachName2(st) ? NCDStatement_ForeachName2(st) : "";
  179. print_indent(indent);
  180. printf("foreach name=%s name1=%s name2=%s\n", name, name1, name2);
  181. print_block(NCDStatement_ForeachBlock(st), indent + 2);
  182. } break;
  183. default: ASSERT(0);
  184. }
  185. }
  186. }
  187. int main (int argc, char **argv)
  188. {
  189. int res = 1;
  190. if (argc != 3) {
  191. printf("Usage: %s <desugar=0/1> <string>\n", (argc > 0 ? argv[0] : ""));
  192. goto fail0;
  193. }
  194. int desugar = atoi(argv[1]);
  195. char *text = argv[2];
  196. BLog_InitStdout();
  197. // parse
  198. NCDProgram prog;
  199. if (!NCDConfigParser_Parse(text, strlen(text), &prog)) {
  200. DEBUG("NCDConfigParser_Parse failed");
  201. goto fail1;
  202. }
  203. // desugar
  204. if (desugar) {
  205. if (!NCDSugar_Desugar(&prog)) {
  206. DEBUG("NCDSugar_Desugar failed");
  207. goto fail2;
  208. }
  209. }
  210. // print
  211. for (NCDProgramElem *elem = NCDProgram_FirstElem(&prog); elem; elem = NCDProgram_NextElem(&prog, elem)) {
  212. switch (NCDProgramElem_Type(elem)) {
  213. case NCDPROGRAMELEM_PROCESS: {
  214. NCDProcess *p = NCDProgramElem_Process(elem);
  215. printf("process name=%s is_template=%d\n", NCDProcess_Name(p), NCDProcess_IsTemplate(p));
  216. print_block(NCDProcess_Block(p), 2);
  217. } break;
  218. case NCDPROGRAMELEM_INCLUDE: {
  219. printf("include path=%s\n", NCDProgramElem_IncludePathData(elem));
  220. } break;
  221. case NCDPROGRAMELEM_INCLUDE_GUARD: {
  222. printf("include_guard id=%s\n", NCDProgramElem_IncludeGuardIdData(elem));
  223. } break;
  224. default: ASSERT(0);
  225. }
  226. }
  227. res = 0;
  228. fail2:
  229. NCDProgram_Free(&prog);
  230. fail1:
  231. BLog_Free();
  232. fail0:
  233. return res;
  234. }