parse.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. o->succeeded = pfunc(i, NCDValue_StringValue(str_arg), &o->value);
  117. // signal up
  118. NCDModuleInst_Backend_Up(i);
  119. return;
  120. fail1:
  121. free(o);
  122. fail0:
  123. NCDModuleInst_Backend_SetError(i);
  124. NCDModuleInst_Backend_Dead(i);
  125. }
  126. static void func_die (void *vo)
  127. {
  128. struct instance *o = vo;
  129. NCDModuleInst *i = o->i;
  130. // free value
  131. if (o->succeeded) {
  132. NCDValue_Free(&o->value);
  133. }
  134. // free instance
  135. free(o);
  136. NCDModuleInst_Backend_Dead(i);
  137. }
  138. static int func_getvar (void *vo, const char *name, NCDValue *out_value)
  139. {
  140. struct instance *o = vo;
  141. if (!strcmp(name, "succeeded")) {
  142. const char *str = o->succeeded ? "true" : "false";
  143. if (!NCDValue_InitString(out_value, str)) {
  144. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  145. return 0;
  146. }
  147. return 1;
  148. }
  149. if (o->succeeded && !strcmp(name, "")) {
  150. if (!NCDValue_InitCopy(out_value, &o->value)) {
  151. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. static void func_new_parse_number (NCDModuleInst *i)
  159. {
  160. new_templ(i, parse_number);
  161. }
  162. static void func_new_parse_value (NCDModuleInst *i)
  163. {
  164. new_templ(i, parse_value);
  165. }
  166. static void func_new_parse_ipv4_addr (NCDModuleInst *i)
  167. {
  168. new_templ(i, parse_ipv4_addr);
  169. }
  170. static const struct NCDModule modules[] = {
  171. {
  172. .type = "parse_number",
  173. .func_new = func_new_parse_number,
  174. .func_die = func_die,
  175. .func_getvar = func_getvar
  176. }, {
  177. .type = "parse_value",
  178. .func_new = func_new_parse_value,
  179. .func_die = func_die,
  180. .func_getvar = func_getvar
  181. }, {
  182. .type = "parse_ipv4_addr",
  183. .func_new = func_new_parse_ipv4_addr,
  184. .func_die = func_die,
  185. .func_getvar = func_getvar
  186. }, {
  187. .type = NULL
  188. }
  189. };
  190. const struct NCDModuleGroup ncdmodule_parse = {
  191. .modules = modules
  192. };