NCDConfigParser.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @file NCDConfigParser.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 <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <misc/debug.h>
  26. #include <ncdconfig/NCDConfigTokenizer.h>
  27. #include <ncdconfig/NCDConfigParser.h>
  28. #include "../generated/NCDConfigParser_parse.c"
  29. #include "../generated/NCDConfigParser_parse.h"
  30. struct parser_state {
  31. struct parser_out out;
  32. int error;
  33. void *parser;
  34. };
  35. static int tokenizer_output (void *user, int token, char *value, size_t position)
  36. {
  37. struct parser_state *state = (struct parser_state *)user;
  38. ASSERT(!state->out.out_of_memory)
  39. ASSERT(!state->out.syntax_error)
  40. ASSERT(!state->error)
  41. if (token == NCD_ERROR) {
  42. DEBUG("tokenizer error at %zu", position);
  43. state->error = 1;
  44. return 0;
  45. }
  46. switch (token) {
  47. case NCD_EOF: {
  48. Parse(state->parser, 0, NULL, &state->out);
  49. } break;
  50. case NCD_TOKEN_CURLY_OPEN: {
  51. Parse(state->parser, CURRLY_OPEN, NULL, &state->out);
  52. } break;
  53. case NCD_TOKEN_CURLY_CLOSE: {
  54. Parse(state->parser, CURLY_CLOSE, NULL, &state->out);
  55. } break;
  56. case NCD_TOKEN_SEMICOLON: {
  57. Parse(state->parser, SEMICOLON, NULL, &state->out);
  58. } break;
  59. case NCD_TOKEN_DOT: {
  60. Parse(state->parser, DOT, NULL, &state->out);
  61. } break;
  62. case NCD_TOKEN_INTERFACE: {
  63. Parse(state->parser, INTERFACE, NULL, &state->out);
  64. } break;
  65. case NCD_TOKEN_NAME: {
  66. char *v = malloc(strlen(value) + 1);
  67. if (v) {
  68. strcpy(v, value);
  69. }
  70. Parse(state->parser, NAME, v, &state->out);
  71. } break;
  72. case NCD_TOKEN_STRING: {
  73. char *v = malloc(strlen(value) + 1);
  74. if (v) {
  75. strcpy(v, value);
  76. }
  77. Parse(state->parser, STRING, v, &state->out);
  78. } break;
  79. default:
  80. ASSERT(0);
  81. }
  82. // if we got syntax error, stop parsing
  83. if (state->out.syntax_error) {
  84. DEBUG("syntax error at %zu", position);
  85. state->error = 1;
  86. return 0;
  87. }
  88. if (state->out.out_of_memory) {
  89. DEBUG("out of memory at %zu", position);
  90. state->error = 1;
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. int NCDConfigParser_Parse (char *config, size_t config_len, struct NCDConfig_interfaces **out_ast)
  96. {
  97. struct parser_state state;
  98. state.out.out_of_memory = 0;
  99. state.out.syntax_error = 0;
  100. state.out.ast = NULL;
  101. state.error = 0;
  102. if (!(state.parser = ParseAlloc(malloc))) {
  103. DEBUG("ParseAlloc failed");
  104. return 0;
  105. }
  106. // tokenize and parse
  107. NCDConfigTokenizer_Tokenize(config, config_len, tokenizer_output, &state);
  108. if (state.error) {
  109. ParseFree(state.parser, free);
  110. NCDConfig_free_interfaces(state.out.ast);
  111. return 0;
  112. }
  113. ParseFree(state.parser, free);
  114. *out_ast = state.out.ast;
  115. return 1;
  116. }