parse.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. NCDValue value;
  53. int succeeded;
  54. };
  55. typedef int (*parse_func) (NCDModuleInst *i, const char *str, NCDValue *out_value);
  56. static int parse_number (NCDModuleInst *i, const char *str, NCDValue *out_value)
  57. {
  58. uintmax_t n;
  59. if (!parse_unsigned_integer(str, &n)) {
  60. ModuleLog(i, BLOG_ERROR, "failed to parse number");
  61. return 0;
  62. }
  63. char buf[25];
  64. sprintf(buf, "%"PRIuMAX, n);
  65. if (!NCDValue_InitString(out_value, buf)) {
  66. ModuleLog(i, BLOG_ERROR, "NCDValue_InitString failed");
  67. return 0;
  68. }
  69. return 1;
  70. }
  71. static int parse_value (NCDModuleInst *i, const char *str, NCDValue *out_value)
  72. {
  73. if (!NCDValueParser_Parse(str, strlen(str), out_value)) {
  74. ModuleLog(i, BLOG_ERROR, "failed to parse value");
  75. return 0;
  76. }
  77. return 1;
  78. }
  79. static int parse_ipv4_addr (NCDModuleInst *i, const char *str, NCDValue *out_value)
  80. {
  81. uint32_t addr;
  82. if (!ipaddr_parse_ipv4_addr((char *)str, &addr)) {
  83. ModuleLog(i, BLOG_ERROR, "failed to parse ipv4 addresss");
  84. return 0;
  85. }
  86. uint8_t *x = (void *)&addr;
  87. char buf[20];
  88. sprintf(buf, "%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, x[0], x[1], x[2], x[3]);
  89. if (!NCDValue_InitString(out_value, buf)) {
  90. ModuleLog(i, BLOG_ERROR, "NCDValue_InitString failed");
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static void new_templ (NCDModuleInst *i, parse_func pfunc)
  96. {
  97. // allocate structure
  98. struct instance *o = malloc(sizeof(*o));
  99. if (!o) {
  100. ModuleLog(i, BLOG_ERROR, "malloc failed");
  101. goto fail0;
  102. }
  103. o->i = i;
  104. NCDModuleInst_Backend_SetUser(i, o);
  105. // read arguments
  106. NCDValue *str_arg;
  107. if (!NCDValue_ListRead(i->args, 1, &str_arg)) {
  108. ModuleLog(i, BLOG_ERROR, "wrong arity");
  109. goto fail1;
  110. }
  111. if (NCDValue_Type(str_arg) != NCDVALUE_STRING) {
  112. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  113. goto fail1;
  114. }
  115. // parse
  116. if (NCDValue_StringHasNulls(str_arg)) {
  117. ModuleLog(o->i, BLOG_ERROR, "string has nulls");
  118. o->succeeded = 0;
  119. } else {
  120. o->succeeded = pfunc(i, NCDValue_StringValue(str_arg), &o->value);
  121. }
  122. // signal up
  123. NCDModuleInst_Backend_Up(i);
  124. return;
  125. fail1:
  126. free(o);
  127. fail0:
  128. NCDModuleInst_Backend_SetError(i);
  129. NCDModuleInst_Backend_Dead(i);
  130. }
  131. static void func_die (void *vo)
  132. {
  133. struct instance *o = vo;
  134. NCDModuleInst *i = o->i;
  135. // free value
  136. if (o->succeeded) {
  137. NCDValue_Free(&o->value);
  138. }
  139. // free instance
  140. free(o);
  141. NCDModuleInst_Backend_Dead(i);
  142. }
  143. static int func_getvar (void *vo, const char *name, NCDValue *out_value)
  144. {
  145. struct instance *o = vo;
  146. if (!strcmp(name, "succeeded")) {
  147. const char *str = o->succeeded ? "true" : "false";
  148. if (!NCDValue_InitString(out_value, str)) {
  149. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. if (o->succeeded && !strcmp(name, "")) {
  155. if (!NCDValue_InitCopy(out_value, &o->value)) {
  156. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  157. return 0;
  158. }
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. static void func_new_parse_number (NCDModuleInst *i)
  164. {
  165. new_templ(i, parse_number);
  166. }
  167. static void func_new_parse_value (NCDModuleInst *i)
  168. {
  169. new_templ(i, parse_value);
  170. }
  171. static void func_new_parse_ipv4_addr (NCDModuleInst *i)
  172. {
  173. new_templ(i, parse_ipv4_addr);
  174. }
  175. static const struct NCDModule modules[] = {
  176. {
  177. .type = "parse_number",
  178. .func_new = func_new_parse_number,
  179. .func_die = func_die,
  180. .func_getvar = func_getvar
  181. }, {
  182. .type = "parse_value",
  183. .func_new = func_new_parse_value,
  184. .func_die = func_die,
  185. .func_getvar = func_getvar
  186. }, {
  187. .type = "parse_ipv4_addr",
  188. .func_new = func_new_parse_ipv4_addr,
  189. .func_die = func_die,
  190. .func_getvar = func_getvar
  191. }, {
  192. .type = NULL
  193. }
  194. };
  195. const struct NCDModuleGroup ncdmodule_parse = {
  196. .modules = modules
  197. };