ncd_parser_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @file ncd_tokenizer_test.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stddef.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <misc/debug.h>
  26. #include <base/BLog.h>
  27. #include <ncd/NCDConfigParser.h>
  28. int error;
  29. static void print_indent (unsigned int indent)
  30. {
  31. while (indent > 0) {
  32. printf(" ");
  33. indent--;
  34. }
  35. }
  36. static void print_list (struct NCDConfig_list *l, unsigned int indent)
  37. {
  38. while (l) {
  39. print_indent(indent);
  40. switch (l->type) {
  41. case NCDCONFIG_ARG_STRING: {
  42. printf("string: %s\n", l->string);
  43. } break;
  44. case NCDCONFIG_ARG_VAR: {
  45. printf("var: ");
  46. struct NCDConfig_strings *n = l->var;
  47. printf("%s", n->value);
  48. n = n->next;
  49. while (n) {
  50. printf(".%s", n->value);
  51. n = n->next;
  52. }
  53. printf("\n");
  54. } break;
  55. case NCDCONFIG_ARG_LIST: {
  56. printf("list\n");
  57. print_list(l->list, indent + 1);
  58. } break;
  59. default:
  60. ASSERT(0);
  61. }
  62. l = l->next;
  63. }
  64. }
  65. int main (int argc, char **argv)
  66. {
  67. if (argc < 1) {
  68. return 1;
  69. }
  70. if (argc != 2) {
  71. printf("Usage: %s <string>\n", argv[0]);
  72. return 1;
  73. }
  74. BLog_InitStdout();
  75. // parse
  76. struct NCDConfig_processes *ast;
  77. if (!NCDConfigParser_Parse(argv[1], strlen(argv[1]), &ast)) {
  78. DEBUG("NCDConfigParser_Parse failed");
  79. return 1;
  80. }
  81. // print
  82. struct NCDConfig_processes *iface = ast;
  83. while (iface) {
  84. printf("process %s\n", iface->name);
  85. struct NCDConfig_statements *st = iface->statements;
  86. while (st) {
  87. struct NCDConfig_strings *name = st->names;
  88. ASSERT(name)
  89. printf(" %s", name->value);
  90. name = name->next;
  91. while (name) {
  92. printf(".%s", name->value);
  93. name = name->next;
  94. }
  95. printf("\n");
  96. print_list(st->args, 2);
  97. st = st->next;
  98. }
  99. iface = iface->next;
  100. }
  101. NCDConfig_free_processes(ast);
  102. return 0;
  103. }