ncd_parser_test.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. int main (int argc, char **argv)
  30. {
  31. if (argc < 1) {
  32. return 1;
  33. }
  34. if (argc != 2) {
  35. printf("Usage: %s <string>\n", argv[0]);
  36. return 1;
  37. }
  38. BLog_InitStdout();
  39. // parse
  40. struct NCDConfig_processes *ast;
  41. if (!NCDConfigParser_Parse(argv[1], strlen(argv[1]), &ast)) {
  42. DEBUG("NCDConfigParser_Parse failed");
  43. return 1;
  44. }
  45. // print
  46. struct NCDConfig_processes *iface = ast;
  47. while (iface) {
  48. printf("process %s\n", iface->name);
  49. struct NCDConfig_statements *st = iface->statements;
  50. while (st) {
  51. struct NCDConfig_strings *name = st->names;
  52. ASSERT(name)
  53. printf(" %s", name->value);
  54. name = name->next;
  55. while (name) {
  56. printf(".%s", name->value);
  57. name = name->next;
  58. }
  59. printf("\n");
  60. struct NCDConfig_list *arg = st->args;
  61. while (arg) {
  62. switch (arg->type) {
  63. case NCDCONFIG_ARG_STRING:
  64. printf(" string: %s\n", arg->string);
  65. break;
  66. case NCDCONFIG_ARG_VAR:
  67. printf(" var: ");
  68. struct NCDConfig_strings *n = arg->var;
  69. printf("%s", n->value);
  70. n = n->next;
  71. while (n) {
  72. printf(".%s", n->value);
  73. n = n->next;
  74. }
  75. printf("\n");
  76. break;
  77. default:
  78. ASSERT(0);
  79. }
  80. arg = arg->next;
  81. }
  82. st = st->next;
  83. }
  84. iface = iface->next;
  85. }
  86. NCDConfig_free_processes(ast);
  87. return 0;
  88. }