index.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**
  2. * @file index.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Synopsis:
  25. * index index(string value)
  26. * index index::next()
  27. *
  28. * Description:
  29. * Non-negative integer with range of a size_t.
  30. * The first form creates an index from the given decimal string.
  31. * The second form cretes an index with value one more than an existing
  32. * index.
  33. *
  34. * Variables:
  35. * string (empty) - the index value. Note this may be different from
  36. * than the value given to index() if it was not in normal form.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <stddef.h>
  41. #include <stdio.h>
  42. #include <misc/parse_number.h>
  43. #include <ncd/NCDModule.h>
  44. #include <generated/blog_channel_ncd_index.h>
  45. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  46. struct instance {
  47. NCDModuleInst *i;
  48. size_t value;
  49. };
  50. static void func_new_templ (NCDModuleInst *i, size_t value)
  51. {
  52. // allocate instance
  53. struct instance *o = malloc(sizeof(*o));
  54. if (!o) {
  55. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  56. goto fail0;
  57. }
  58. NCDModuleInst_Backend_SetUser(i, o);
  59. // init arguments
  60. o->i = i;
  61. o->value = value;
  62. // signal up
  63. NCDModuleInst_Backend_Up(o->i);
  64. return;
  65. fail0:
  66. NCDModuleInst_Backend_SetError(i);
  67. NCDModuleInst_Backend_Dead(i);
  68. }
  69. static void func_new_from_value (NCDModuleInst *i)
  70. {
  71. // read arguments
  72. NCDValue *arg_value;
  73. if (!NCDValue_ListRead(i->args, 1, &arg_value)) {
  74. ModuleLog(i, BLOG_ERROR, "wrong arity");
  75. goto fail0;
  76. }
  77. if (NCDValue_Type(arg_value) != NCDVALUE_STRING) {
  78. ModuleLog(i, BLOG_ERROR, "wrong type");
  79. goto fail0;
  80. }
  81. // parse value
  82. uintmax_t value;
  83. if (!parse_unsigned_integer(NCDValue_StringValue(arg_value), &value)) {
  84. ModuleLog(i, BLOG_ERROR, "wrong value");
  85. goto fail0;
  86. }
  87. // check overflow
  88. if (value > SIZE_MAX) {
  89. ModuleLog(i, BLOG_ERROR, "value too large");
  90. goto fail0;
  91. }
  92. func_new_templ(i, value);
  93. return;
  94. fail0:
  95. NCDModuleInst_Backend_SetError(i);
  96. NCDModuleInst_Backend_Dead(i);
  97. }
  98. static void func_new_from_index (NCDModuleInst *i)
  99. {
  100. struct instance *index = i->method_object->inst_user;
  101. // check overflow
  102. if (index->value == SIZE_MAX) {
  103. ModuleLog(i, BLOG_ERROR, "overflow");
  104. goto fail0;
  105. }
  106. func_new_templ(i, index->value + 1);
  107. return;
  108. fail0:
  109. NCDModuleInst_Backend_SetError(i);
  110. NCDModuleInst_Backend_Dead(i);
  111. }
  112. static void func_die (void *vo)
  113. {
  114. struct instance *o = vo;
  115. NCDModuleInst *i = o->i;
  116. // free instance
  117. free(o);
  118. NCDModuleInst_Backend_Dead(i);
  119. }
  120. static int func_getvar (void *vo, const char *name, NCDValue *out)
  121. {
  122. struct instance *o = vo;
  123. if (!strcmp(name, "")) {
  124. char str[64];
  125. snprintf(str, sizeof(str), "%zu", o->value);
  126. if (!NCDValue_InitString(out, str)) {
  127. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. return 0;
  133. }
  134. static const struct NCDModule modules[] = {
  135. {
  136. .type = "index",
  137. .func_new = func_new_from_value,
  138. .func_die = func_die,
  139. .func_getvar = func_getvar
  140. }, {
  141. .type = "index::next",
  142. .base_type = "index",
  143. .func_new = func_new_from_index,
  144. .func_die = func_die,
  145. .func_getvar = func_getvar
  146. }, {
  147. .type = NULL
  148. }
  149. };
  150. const struct NCDModuleGroup ncdmodule_index = {
  151. .modules = modules
  152. };