ncd_parser_test.c 2.8 KB

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