NCDConfigTokenizer.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @file NCDConfigTokenizer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stddef.h>
  31. #include <stdlib.h>
  32. #include <misc/debug.h>
  33. #include <misc/string_begins_with.h>
  34. #include <misc/balloc.h>
  35. #include <misc/expstring.h>
  36. #include <base/BLog.h>
  37. #include <ncd/NCDConfigTokenizer.h>
  38. #include <generated/blog_channel_NCDConfigTokenizer.h>
  39. static int is_name_char (char c)
  40. {
  41. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  42. }
  43. static int is_name_first_char (char c)
  44. {
  45. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  46. }
  47. static int is_space_char (char c)
  48. {
  49. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  50. }
  51. static int string_equals (char *str, int str_len, char *needle)
  52. {
  53. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  54. }
  55. void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
  56. {
  57. size_t line = 1;
  58. size_t line_char = 1;
  59. while (left > 0) {
  60. size_t l;
  61. int error = 0;
  62. int token;
  63. void *token_val = NULL;
  64. size_t token_len = 0;
  65. if (*str == '#') {
  66. l = 1;
  67. while (l < left && str[l] != '\n') {
  68. l++;
  69. }
  70. token = 0;
  71. }
  72. else if (l = data_begins_with(str, left, "{")) {
  73. token = NCD_TOKEN_CURLY_OPEN;
  74. }
  75. else if (l = data_begins_with(str, left, "}")) {
  76. token = NCD_TOKEN_CURLY_CLOSE;
  77. }
  78. else if (l = data_begins_with(str, left, "(")) {
  79. token = NCD_TOKEN_ROUND_OPEN;
  80. }
  81. else if (l = data_begins_with(str, left, ")")) {
  82. token = NCD_TOKEN_ROUND_CLOSE;
  83. }
  84. else if (l = data_begins_with(str, left, ";")) {
  85. token = NCD_TOKEN_SEMICOLON;
  86. }
  87. else if (l = data_begins_with(str, left, ".")) {
  88. token = NCD_TOKEN_DOT;
  89. }
  90. else if (l = data_begins_with(str, left, ",")) {
  91. token = NCD_TOKEN_COMMA;
  92. }
  93. else if (l = data_begins_with(str, left, ":")) {
  94. token = NCD_TOKEN_COLON;
  95. }
  96. else if (l = data_begins_with(str, left, "[")) {
  97. token = NCD_TOKEN_BRACKET_OPEN;
  98. }
  99. else if (l = data_begins_with(str, left, "]")) {
  100. token = NCD_TOKEN_BRACKET_CLOSE;
  101. }
  102. else if (l = data_begins_with(str, left, "->")) {
  103. token = NCD_TOKEN_ARROW;
  104. }
  105. else if (is_name_first_char(*str)) {
  106. l = 1;
  107. while (l < left && is_name_char(str[l])) {
  108. l++;
  109. }
  110. // allocate buffer
  111. bsize_t bufsize = bsize_add(bsize_fromsize(l), bsize_fromint(1));
  112. char *buf;
  113. if (bufsize.is_overflow || !(buf = malloc(bufsize.value))) {
  114. BLog(BLOG_ERROR, "malloc failed");
  115. error = 1;
  116. goto out;
  117. }
  118. // copy and terminate
  119. memcpy(buf, str, l);
  120. buf[l] = '\0';
  121. if (!strcmp(buf, "process")) {
  122. token = NCD_TOKEN_PROCESS;
  123. free(buf);
  124. }
  125. else if (!strcmp(buf, "template")) {
  126. token = NCD_TOKEN_TEMPLATE;
  127. free(buf);
  128. }
  129. else {
  130. token = NCD_TOKEN_NAME;
  131. token_val = buf;
  132. token_len = l;
  133. }
  134. }
  135. else if (*str == '"') do {
  136. // init string
  137. ExpString estr;
  138. if (!ExpString_Init(&estr)) {
  139. BLog(BLOG_ERROR, "ExpString_Init failed");
  140. goto string_fail0;
  141. }
  142. // skip start quote
  143. l = 1;
  144. // decode string
  145. while (l < left) {
  146. uint8_t dec_ch;
  147. // get character
  148. if (str[l] == '\\') {
  149. if (left - l < 2) {
  150. BLog(BLOG_ERROR, "escape character found in string but nothing follows");
  151. goto string_fail1;
  152. }
  153. dec_ch = str[l + 1];
  154. l += 2;
  155. }
  156. else if (str[l] == '"') {
  157. break;
  158. }
  159. else {
  160. dec_ch = str[l];
  161. l++;
  162. }
  163. // string cannot contain zeros bytes
  164. if (dec_ch == '\0') {
  165. BLog(BLOG_ERROR, "string contains zero byte");
  166. goto string_fail1;
  167. }
  168. // append character to string
  169. if (!ExpString_AppendByte(&estr, dec_ch)) {
  170. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  171. goto string_fail1;
  172. }
  173. }
  174. // make sure ending quote was found
  175. if (l == left) {
  176. BLog(BLOG_ERROR, "missing ending quote for string");
  177. goto string_fail1;
  178. }
  179. // skip ending quote
  180. l++;
  181. token = NCD_TOKEN_STRING;
  182. token_val = ExpString_Get(&estr);
  183. token_len = ExpString_Length(&estr);
  184. break;
  185. string_fail1:
  186. ExpString_Free(&estr);
  187. string_fail0:
  188. error = 1;
  189. } while (0);
  190. else if (is_space_char(*str)) {
  191. token = 0;
  192. l = 1;
  193. }
  194. else {
  195. BLog(BLOG_ERROR, "unrecognized character");
  196. error = 1;
  197. }
  198. out:
  199. // report error
  200. if (error) {
  201. output(user, NCD_ERROR, NULL, 0, line, line_char);
  202. return;
  203. }
  204. // output token
  205. if (token) {
  206. if (!output(user, token, token_val, token_len, line, line_char)) {
  207. return;
  208. }
  209. }
  210. // update line/char counters
  211. for (size_t i = 0; i < l; i++) {
  212. if (str[i] == '\n') {
  213. line++;
  214. line_char = 1;
  215. } else {
  216. line_char++;
  217. }
  218. }
  219. str += l;
  220. left -= l;
  221. }
  222. output(user, NCD_EOF, NULL, 0, line, line_char);
  223. }