parse.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @file parse.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. * @section DESCRIPTION
  30. *
  31. * Synopsis:
  32. * parse_number(string str)
  33. * parse_value(string str)
  34. * parse_ipv4_addr(string str)
  35. *
  36. * Variables:
  37. * succeeded - "true" or "false", reflecting success of the parsing
  38. * (empty) - normalized parsed value (only if succeeded)
  39. */
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <inttypes.h>
  44. #include <misc/parse_number.h>
  45. #include <misc/ipaddr.h>
  46. #include <ncd/NCDValueParser.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_parse.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. struct instance {
  51. NCDModuleInst *i;
  52. NCDValMem mem;
  53. NCDValRef value;
  54. int succeeded;
  55. };
  56. typedef int (*parse_func) (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out);
  57. static int parse_number (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  58. {
  59. uintmax_t n;
  60. if (!parse_unsigned_integer(str, &n)) {
  61. ModuleLog(i, BLOG_ERROR, "failed to parse number");
  62. return 0;
  63. }
  64. char buf[25];
  65. snprintf(buf, sizeof(buf), "%"PRIuMAX, n);
  66. *out = NCDVal_NewString(mem, buf);
  67. if (NCDVal_IsInvalid(*out)) {
  68. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  69. return 0;
  70. }
  71. return 1;
  72. }
  73. static int parse_value (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  74. {
  75. if (!NCDValParser_Parse(str, strlen(str), mem, out)) {
  76. ModuleLog(i, BLOG_ERROR, "failed to parse value");
  77. return 0;
  78. }
  79. return 1;
  80. }
  81. static int parse_ipv4_addr (NCDModuleInst *i, const char *str, NCDValMem *mem, NCDValRef *out)
  82. {
  83. uint32_t addr;
  84. if (!ipaddr_parse_ipv4_addr((char *)str, &addr)) {
  85. ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
  86. return 0;
  87. }
  88. uint8_t *x = (void *)&addr;
  89. char buf[20];
  90. sprintf(buf, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, x[0], x[1], x[2], x[3]);
  91. *out = NCDVal_NewString(mem, buf);
  92. if (NCDVal_IsInvalid(*out)) {
  93. ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. static void new_templ (NCDModuleInst *i, parse_func pfunc)
  99. {
  100. // allocate structure
  101. struct instance *o = malloc(sizeof(*o));
  102. if (!o) {
  103. ModuleLog(i, BLOG_ERROR, "malloc failed");
  104. goto fail0;
  105. }
  106. o->i = i;
  107. NCDModuleInst_Backend_SetUser(i, o);
  108. // read arguments
  109. NCDValRef str_arg;
  110. if (!NCDVal_ListRead(i->args, 1, &str_arg)) {
  111. ModuleLog(i, BLOG_ERROR, "wrong arity");
  112. goto fail1;
  113. }
  114. if (!NCDVal_IsString(str_arg)) {
  115. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  116. goto fail1;
  117. }
  118. // init mem
  119. NCDValMem_Init(&o->mem);
  120. // parse
  121. if (NCDVal_StringHasNulls(str_arg)) {
  122. ModuleLog(o->i, BLOG_ERROR, "string has nulls");
  123. o->succeeded = 0;
  124. } else {
  125. o->succeeded = pfunc(i, NCDVal_StringValue(str_arg), &o->mem, &o->value);
  126. }
  127. // signal up
  128. NCDModuleInst_Backend_Up(i);
  129. return;
  130. fail1:
  131. free(o);
  132. fail0:
  133. NCDModuleInst_Backend_SetError(i);
  134. NCDModuleInst_Backend_Dead(i);
  135. }
  136. static void func_die (void *vo)
  137. {
  138. struct instance *o = vo;
  139. NCDModuleInst *i = o->i;
  140. // free mem
  141. NCDValMem_Free(&o->mem);
  142. // free instance
  143. free(o);
  144. NCDModuleInst_Backend_Dead(i);
  145. }
  146. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  147. {
  148. struct instance *o = vo;
  149. if (!strcmp(name, "succeeded")) {
  150. const char *str = o->succeeded ? "true" : "false";
  151. *out = NCDVal_NewString(mem, str);
  152. if (NCDVal_IsInvalid(*out)) {
  153. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
  154. }
  155. return 1;
  156. }
  157. if (o->succeeded && !strcmp(name, "")) {
  158. *out = NCDVal_NewCopy(mem, o->value);
  159. if (NCDVal_IsInvalid(*out)) {
  160. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  161. }
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. static void func_new_parse_number (NCDModuleInst *i)
  167. {
  168. new_templ(i, parse_number);
  169. }
  170. static void func_new_parse_value (NCDModuleInst *i)
  171. {
  172. new_templ(i, parse_value);
  173. }
  174. static void func_new_parse_ipv4_addr (NCDModuleInst *i)
  175. {
  176. new_templ(i, parse_ipv4_addr);
  177. }
  178. static const struct NCDModule modules[] = {
  179. {
  180. .type = "parse_number",
  181. .func_new = func_new_parse_number,
  182. .func_die = func_die,
  183. .func_getvar = func_getvar
  184. }, {
  185. .type = "parse_value",
  186. .func_new = func_new_parse_value,
  187. .func_die = func_die,
  188. .func_getvar = func_getvar
  189. }, {
  190. .type = "parse_ipv4_addr",
  191. .func_new = func_new_parse_ipv4_addr,
  192. .func_die = func_die,
  193. .func_getvar = func_getvar
  194. }, {
  195. .type = NULL
  196. }
  197. };
  198. const struct NCDModuleGroup ncdmodule_parse = {
  199. .modules = modules
  200. };