NCDConfigTokenizer.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 <misc/parse_number.h>
  37. #include <base/BLog.h>
  38. #include <ncd/NCDConfigTokenizer.h>
  39. #include <generated/blog_channel_NCDConfigTokenizer.h>
  40. static int is_name_char (char c)
  41. {
  42. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_');
  43. }
  44. static int is_name_first_char (char c)
  45. {
  46. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_');
  47. }
  48. static int is_space_char (char c)
  49. {
  50. return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
  51. }
  52. static int string_equals (char *str, int str_len, char *needle)
  53. {
  54. return (str_len == strlen(needle) && !memcmp(str, needle, str_len));
  55. }
  56. void NCDConfigTokenizer_Tokenize (char *str, size_t left, NCDConfigTokenizer_output output, void *user)
  57. {
  58. size_t line = 1;
  59. size_t line_char = 1;
  60. while (left > 0) {
  61. size_t l;
  62. int error = 0;
  63. int token;
  64. void *token_val = NULL;
  65. size_t token_len = 0;
  66. if (*str == '#') {
  67. l = 1;
  68. while (l < left && str[l] != '\n') {
  69. l++;
  70. }
  71. token = 0;
  72. }
  73. else if (l = data_begins_with(str, left, "{")) {
  74. token = NCD_TOKEN_CURLY_OPEN;
  75. }
  76. else if (l = data_begins_with(str, left, "}")) {
  77. token = NCD_TOKEN_CURLY_CLOSE;
  78. }
  79. else if (l = data_begins_with(str, left, "(")) {
  80. token = NCD_TOKEN_ROUND_OPEN;
  81. }
  82. else if (l = data_begins_with(str, left, ")")) {
  83. token = NCD_TOKEN_ROUND_CLOSE;
  84. }
  85. else if (l = data_begins_with(str, left, ";")) {
  86. token = NCD_TOKEN_SEMICOLON;
  87. }
  88. else if (l = data_begins_with(str, left, ".")) {
  89. token = NCD_TOKEN_DOT;
  90. }
  91. else if (l = data_begins_with(str, left, ",")) {
  92. token = NCD_TOKEN_COMMA;
  93. }
  94. else if (l = data_begins_with(str, left, ":")) {
  95. token = NCD_TOKEN_COLON;
  96. }
  97. else if (l = data_begins_with(str, left, "[")) {
  98. token = NCD_TOKEN_BRACKET_OPEN;
  99. }
  100. else if (l = data_begins_with(str, left, "]")) {
  101. token = NCD_TOKEN_BRACKET_CLOSE;
  102. }
  103. else if (l = data_begins_with(str, left, "->")) {
  104. token = NCD_TOKEN_ARROW;
  105. }
  106. else if (is_name_first_char(*str)) {
  107. l = 1;
  108. while (l < left && is_name_char(str[l])) {
  109. l++;
  110. }
  111. // allocate buffer
  112. bsize_t bufsize = bsize_add(bsize_fromsize(l), bsize_fromint(1));
  113. char *buf;
  114. if (bufsize.is_overflow || !(buf = malloc(bufsize.value))) {
  115. BLog(BLOG_ERROR, "malloc failed");
  116. error = 1;
  117. goto out;
  118. }
  119. // copy and terminate
  120. memcpy(buf, str, l);
  121. buf[l] = '\0';
  122. if (!strcmp(buf, "process")) {
  123. token = NCD_TOKEN_PROCESS;
  124. free(buf);
  125. }
  126. else if (!strcmp(buf, "template")) {
  127. token = NCD_TOKEN_TEMPLATE;
  128. free(buf);
  129. }
  130. else {
  131. token = NCD_TOKEN_NAME;
  132. token_val = buf;
  133. token_len = l;
  134. }
  135. }
  136. else if (*str == '"') do {
  137. // init string
  138. ExpString estr;
  139. if (!ExpString_Init(&estr)) {
  140. BLog(BLOG_ERROR, "ExpString_Init failed");
  141. goto string_fail0;
  142. }
  143. // skip start quote
  144. l = 1;
  145. // decode string
  146. while (l < left) {
  147. uint8_t dec_ch;
  148. // get character
  149. if (str[l] == '\\') {
  150. if (left - l < 2) {
  151. BLog(BLOG_ERROR, "escape character found in string but nothing follows");
  152. goto string_fail1;
  153. }
  154. size_t extra = 0;
  155. switch (str[l + 1]) {
  156. case '\'':
  157. case '\"':
  158. case '\\':
  159. case '\?':
  160. dec_ch = str[l + 1]; break;
  161. case 'a':
  162. dec_ch = '\a'; break;
  163. case 'b':
  164. dec_ch = '\b'; break;
  165. case 'f':
  166. dec_ch = '\f'; break;
  167. case 'n':
  168. dec_ch = '\n'; break;
  169. case 'r':
  170. dec_ch = '\r'; break;
  171. case 't':
  172. dec_ch = '\t'; break;
  173. case 'v':
  174. dec_ch = '\v'; break;
  175. case '0':
  176. dec_ch = 0; break;
  177. case 'x': {
  178. if (left - l < 4) {
  179. BLog(BLOG_ERROR, "hexadecimal escape found in string but too little characters follow");
  180. goto string_fail1;
  181. }
  182. uintmax_t hex_val;
  183. if (!parse_unsigned_hex_integer_bin(&str[l + 2], 2, &hex_val)) {
  184. BLog(BLOG_ERROR, "hexadecimal escape found in string but two hex characters don't follow");
  185. goto string_fail1;
  186. }
  187. dec_ch = hex_val;
  188. extra = 2;
  189. } break;
  190. default:
  191. BLog(BLOG_ERROR, "bad escape sequence in string");
  192. goto string_fail1;
  193. }
  194. l += 2 + extra;
  195. }
  196. else if (str[l] == '"') {
  197. break;
  198. }
  199. else {
  200. dec_ch = str[l];
  201. l++;
  202. }
  203. // append character to string
  204. if (!ExpString_AppendByte(&estr, dec_ch)) {
  205. BLog(BLOG_ERROR, "ExpString_AppendChar failed");
  206. goto string_fail1;
  207. }
  208. }
  209. // make sure ending quote was found
  210. if (l == left) {
  211. BLog(BLOG_ERROR, "missing ending quote for string");
  212. goto string_fail1;
  213. }
  214. // skip ending quote
  215. l++;
  216. token = NCD_TOKEN_STRING;
  217. token_val = ExpString_Get(&estr);
  218. token_len = ExpString_Length(&estr);
  219. break;
  220. string_fail1:
  221. ExpString_Free(&estr);
  222. string_fail0:
  223. error = 1;
  224. } while (0);
  225. else if (is_space_char(*str)) {
  226. token = 0;
  227. l = 1;
  228. }
  229. else {
  230. BLog(BLOG_ERROR, "unrecognized character");
  231. error = 1;
  232. }
  233. out:
  234. // report error
  235. if (error) {
  236. output(user, NCD_ERROR, NULL, 0, line, line_char);
  237. return;
  238. }
  239. // output token
  240. if (token) {
  241. if (!output(user, token, token_val, token_len, line, line_char)) {
  242. return;
  243. }
  244. }
  245. // update line/char counters
  246. for (size_t i = 0; i < l; i++) {
  247. if (str[i] == '\n') {
  248. line++;
  249. line_char = 1;
  250. } else {
  251. line_char++;
  252. }
  253. }
  254. str += l;
  255. left -= l;
  256. }
  257. output(user, NCD_EOF, NULL, 0, line, line_char);
  258. }