NCDConfigParser.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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, CURLY_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_ROUND_OPEN: {
  57. Parse(state->parser, ROUND_OPEN, NULL, &state->out);
  58. } break;
  59. case NCD_TOKEN_ROUND_CLOSE: {
  60. Parse(state->parser, ROUND_CLOSE, NULL, &state->out);
  61. } break;
  62. case NCD_TOKEN_SEMICOLON: {
  63. Parse(state->parser, SEMICOLON, NULL, &state->out);
  64. } break;
  65. case NCD_TOKEN_DOT: {
  66. Parse(state->parser, DOT, NULL, &state->out);
  67. } break;
  68. case NCD_TOKEN_COMMA: {
  69. Parse(state->parser, COMMA, NULL, &state->out);
  70. } break;
  71. case NCD_TOKEN_PROCESS: {
  72. Parse(state->parser, PROCESS, NULL, &state->out);
  73. } break;
  74. case NCD_TOKEN_NAME: {
  75. char *v = malloc(strlen(value) + 1);
  76. if (v) {
  77. strcpy(v, value);
  78. }
  79. Parse(state->parser, NAME, v, &state->out);
  80. } break;
  81. case NCD_TOKEN_STRING: {
  82. char *v = malloc(strlen(value) + 1);
  83. if (v) {
  84. strcpy(v, value);
  85. }
  86. Parse(state->parser, STRING, v, &state->out);
  87. } break;
  88. default:
  89. ASSERT(0);
  90. }
  91. // if we got syntax error, stop parsing
  92. if (state->out.syntax_error) {
  93. DEBUG("syntax error at %zu", position);
  94. state->error = 1;
  95. return 0;
  96. }
  97. if (state->out.out_of_memory) {
  98. DEBUG("out of memory at %zu", position);
  99. state->error = 1;
  100. return 0;
  101. }
  102. return 1;
  103. }
  104. int NCDConfigParser_Parse (char *config, size_t config_len, struct NCDConfig_interfaces **out_ast)
  105. {
  106. struct parser_state state;
  107. state.out.out_of_memory = 0;
  108. state.out.syntax_error = 0;
  109. state.out.ast = NULL;
  110. state.error = 0;
  111. if (!(state.parser = ParseAlloc(malloc))) {
  112. DEBUG("ParseAlloc failed");
  113. return 0;
  114. }
  115. // tokenize and parse
  116. NCDConfigTokenizer_Tokenize(config, config_len, tokenizer_output, &state);
  117. if (state.error) {
  118. ParseFree(state.parser, free);
  119. NCDConfig_free_interfaces(state.out.ast);
  120. return 0;
  121. }
  122. ParseFree(state.parser, free);
  123. *out_ast = state.out.ast;
  124. return 1;
  125. }