NCDConfigTokenizer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * @file NCDConfigTokenizer.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 <string.h>
  23. #include <stddef.h>
  24. #include <misc/debug.h>
  25. #include <misc/string_begins_with.h>
  26. #include <ncdconfig/NCDConfigTokenizer.h>
  27. static int is_name_char (char c)
  28. {
  29. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  30. }
  31. static int is_name_first_char (char c)
  32. {
  33. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  34. }
  35. static int is_space_char (char c)
  36. {
  37. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  38. }
  39. static int string_equals (char *str, int str_len, char *needle)
  40. {
  41. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  42. }
  43. void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
  44. {
  45. size_t position = 0;
  46. while (left > 0) {
  47. size_t l;
  48. int error = 0;
  49. int token;
  50. void *token_val = NULL;
  51. char dec[NCD_MAX_SIZE + 1];
  52. if (*str == '#') {
  53. l = 1;
  54. while (l < left && str[l] != '\n') {
  55. l++;
  56. }
  57. token = 0;
  58. }
  59. else if (l = data_begins_with(str, left, "{")) {
  60. token = NCD_TOKEN_CURLY_OPEN;
  61. }
  62. else if (l = data_begins_with(str, left, "}")) {
  63. token = NCD_TOKEN_CURLY_CLOSE;
  64. }
  65. else if (l = data_begins_with(str, left, "(")) {
  66. token = NCD_TOKEN_ROUND_OPEN;
  67. }
  68. else if (l = data_begins_with(str, left, ")")) {
  69. token = NCD_TOKEN_ROUND_CLOSE;
  70. }
  71. else if (l = data_begins_with(str, left, ";")) {
  72. token = NCD_TOKEN_SEMICOLON;
  73. }
  74. else if (l = data_begins_with(str, left, ".")) {
  75. token = NCD_TOKEN_DOT;
  76. }
  77. else if (l = data_begins_with(str, left, ",")) {
  78. token = NCD_TOKEN_COMMA;
  79. }
  80. else if (l = data_begins_with(str, left, "->")) {
  81. token = NCD_TOKEN_ARROW;
  82. }
  83. else if (is_name_first_char(*str)) {
  84. l = 1;
  85. while (l < left) {
  86. if (!is_name_char(str[l])) {
  87. break;
  88. }
  89. l++;
  90. }
  91. // check size
  92. if (l > NCD_MAX_SIZE) {
  93. error = 1;
  94. goto out;
  95. }
  96. // copy and terminate
  97. memcpy(dec, str, l);
  98. dec[l] = '\0';
  99. if (!strcmp(dec, "process")) {
  100. token = NCD_TOKEN_PROCESS;
  101. }
  102. else if (!strcmp(dec, "template")) {
  103. token = NCD_TOKEN_TEMPLATE;
  104. }
  105. else {
  106. token = NCD_TOKEN_NAME;
  107. token_val = dec;
  108. }
  109. }
  110. else if (*str == '"') {
  111. size_t dec_len = 0;
  112. // decode string on the fly
  113. l = 1;
  114. while (l < left) {
  115. char dec_ch;
  116. if (str[l] == '\\') {
  117. if (!(l + 1 < left)) {
  118. error = 1;
  119. goto out;
  120. }
  121. dec_ch = str[l + 1];
  122. l += 2;
  123. }
  124. else if (str[l] == '"') {
  125. break;
  126. }
  127. else {
  128. dec_ch = str[l];
  129. l++;
  130. }
  131. // check size
  132. if (dec_len == NCD_MAX_SIZE) {
  133. error = 1;
  134. goto out;
  135. }
  136. // append decoded char
  137. dec[dec_len] = dec_ch;
  138. dec_len++;
  139. }
  140. // make sure closing quote was found
  141. if (l == left) {
  142. error = 1;
  143. goto out;
  144. }
  145. l++;
  146. // terminate
  147. dec[dec_len] = '\0';
  148. token = NCD_TOKEN_STRING;
  149. token_val = dec;
  150. }
  151. else if (is_space_char(*str)) {
  152. token = 0;
  153. l = 1;
  154. }
  155. else {
  156. error = 1;
  157. }
  158. out:
  159. // report error
  160. if (error) {
  161. output(user, NCD_ERROR, NULL, position);
  162. return;
  163. }
  164. // output token
  165. if (token) {
  166. if (!output(user, token, token_val, position)) {
  167. return;
  168. }
  169. }
  170. str += l;
  171. left -= l;
  172. position += l;
  173. }
  174. output(user, NCD_EOF, NULL, position);
  175. }