NCDConfigParser.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <base/BLog.h>
  27. #include <ncd/NCDConfigTokenizer.h>
  28. #include <ncd/NCDConfigParser.h>
  29. #include <generated/blog_channel_NCDConfigParser.h>
  30. #include "../generated/NCDConfigParser_parse.c"
  31. #include "../generated/NCDConfigParser_parse.h"
  32. struct parser_state {
  33. struct parser_out out;
  34. int error;
  35. void *parser;
  36. };
  37. static int tokenizer_output (void *user, int token, char *value, size_t position)
  38. {
  39. struct parser_state *state = (struct parser_state *)user;
  40. ASSERT(!state->out.out_of_memory)
  41. ASSERT(!state->out.syntax_error)
  42. ASSERT(!state->error)
  43. if (token == NCD_ERROR) {
  44. BLog(BLOG_ERROR, "tokenizer error at %zu", position);
  45. state->error = 1;
  46. return 0;
  47. }
  48. switch (token) {
  49. case NCD_EOF: {
  50. Parse(state->parser, 0, NULL, &state->out);
  51. } break;
  52. case NCD_TOKEN_CURLY_OPEN: {
  53. Parse(state->parser, CURLY_OPEN, NULL, &state->out);
  54. } break;
  55. case NCD_TOKEN_CURLY_CLOSE: {
  56. Parse(state->parser, CURLY_CLOSE, NULL, &state->out);
  57. } break;
  58. case NCD_TOKEN_ROUND_OPEN: {
  59. Parse(state->parser, ROUND_OPEN, NULL, &state->out);
  60. } break;
  61. case NCD_TOKEN_ROUND_CLOSE: {
  62. Parse(state->parser, ROUND_CLOSE, NULL, &state->out);
  63. } break;
  64. case NCD_TOKEN_SEMICOLON: {
  65. Parse(state->parser, SEMICOLON, NULL, &state->out);
  66. } break;
  67. case NCD_TOKEN_DOT: {
  68. Parse(state->parser, DOT, NULL, &state->out);
  69. } break;
  70. case NCD_TOKEN_COMMA: {
  71. Parse(state->parser, COMMA, NULL, &state->out);
  72. } break;
  73. case NCD_TOKEN_ARROW: {
  74. Parse(state->parser, ARROW, NULL, &state->out);
  75. } break;
  76. case NCD_TOKEN_PROCESS: {
  77. Parse(state->parser, PROCESS, NULL, &state->out);
  78. } break;
  79. case NCD_TOKEN_TEMPLATE: {
  80. Parse(state->parser, TEMPLATE, NULL, &state->out);
  81. } break;
  82. case NCD_TOKEN_NAME: {
  83. char *v = malloc(strlen(value) + 1);
  84. if (!v) {
  85. state->out.out_of_memory = 1;
  86. break;
  87. }
  88. strcpy(v, value);
  89. Parse(state->parser, NAME, v, &state->out);
  90. } break;
  91. case NCD_TOKEN_STRING: {
  92. char *v = malloc(strlen(value) + 1);
  93. if (!v) {
  94. state->out.out_of_memory = 1;
  95. break;
  96. }
  97. strcpy(v, value);
  98. Parse(state->parser, STRING, v, &state->out);
  99. } break;
  100. default:
  101. ASSERT(0);
  102. }
  103. // if we got syntax error, stop parsing
  104. if (state->out.syntax_error) {
  105. BLog(BLOG_ERROR, "syntax error at %zu", position);
  106. state->error = 1;
  107. return 0;
  108. }
  109. if (state->out.out_of_memory) {
  110. BLog(BLOG_ERROR, "out of memory at %zu", position);
  111. state->error = 1;
  112. return 0;
  113. }
  114. return 1;
  115. }
  116. int NCDConfigParser_Parse (char *config, size_t config_len, struct NCDConfig_interfaces **out_ast)
  117. {
  118. struct parser_state state;
  119. state.out.out_of_memory = 0;
  120. state.out.syntax_error = 0;
  121. state.out.ast = NULL;
  122. state.error = 0;
  123. if (!(state.parser = ParseAlloc(malloc))) {
  124. BLog(BLOG_ERROR, "ParseAlloc failed");
  125. return 0;
  126. }
  127. // tokenize and parse
  128. NCDConfigTokenizer_Tokenize(config, config_len, tokenizer_output, &state);
  129. if (state.error) {
  130. ParseFree(state.parser, free);
  131. NCDConfig_free_interfaces(state.out.ast);
  132. return 0;
  133. }
  134. ParseFree(state.parser, free);
  135. *out_ast = state.out.ast;
  136. return 1;
  137. }