var.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /**
  2. * @file var.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. * Variable module.
  25. *
  26. * Synopsis: var(value)
  27. * Variables:
  28. * (empty) - value
  29. *
  30. * Synopsis: var::set(value)
  31. */
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <ncd/NCDModule.h>
  35. #include <generated/blog_channel_ncd_var.h>
  36. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  37. struct instance {
  38. NCDModuleInst *i;
  39. NCDValue value;
  40. };
  41. struct set_instance {
  42. NCDModuleInst *i;
  43. };
  44. static void func_new (NCDModuleInst *i)
  45. {
  46. // allocate instance
  47. struct instance *o = malloc(sizeof(*o));
  48. if (!o) {
  49. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  50. goto fail0;
  51. }
  52. NCDModuleInst_Backend_SetUser(i, o);
  53. // init arguments
  54. o->i = i;
  55. // read argument
  56. NCDValue *value_arg;
  57. if (!NCDValue_ListRead(o->i->args, 1, &value_arg)) {
  58. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  59. goto fail1;
  60. }
  61. // copy to value
  62. if (!NCDValue_InitCopy(&o->value, value_arg)) {
  63. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  64. goto fail1;
  65. }
  66. // signal up
  67. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  68. return;
  69. fail1:
  70. free(o);
  71. fail0:
  72. NCDModuleInst_Backend_SetError(i);
  73. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  74. }
  75. static void func_die (void *vo)
  76. {
  77. struct instance *o = vo;
  78. NCDModuleInst *i = o->i;
  79. // free value
  80. NCDValue_Free(&o->value);
  81. // free instance
  82. free(o);
  83. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  84. }
  85. static int func_getvar (void *vo, const char *name, NCDValue *out)
  86. {
  87. struct instance *o = vo;
  88. if (!strcmp(name, "")) {
  89. if (!NCDValue_InitCopy(out, &o->value)) {
  90. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. return 0;
  96. }
  97. static void set_func_new (NCDModuleInst *i)
  98. {
  99. // allocate instance
  100. struct set_instance *o = malloc(sizeof(*o));
  101. if (!o) {
  102. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  103. goto fail0;
  104. }
  105. NCDModuleInst_Backend_SetUser(i, o);
  106. // init arguments
  107. o->i = i;
  108. // read argument
  109. NCDValue *value_arg;
  110. if (!NCDValue_ListRead(o->i->args, 1, &value_arg)) {
  111. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  112. goto fail1;
  113. }
  114. // get method object
  115. struct instance *mo = i->method_object->inst_user;
  116. // set
  117. NCDValue v;
  118. if (!NCDValue_InitCopy(&v, value_arg)) {
  119. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  120. goto fail1;
  121. }
  122. NCDValue_Free(&mo->value);
  123. mo->value = v;
  124. // signal up
  125. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  126. return;
  127. fail1:
  128. free(o);
  129. fail0:
  130. NCDModuleInst_Backend_SetError(i);
  131. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  132. }
  133. static void set_func_die (void *vo)
  134. {
  135. struct set_instance *o = vo;
  136. NCDModuleInst *i = o->i;
  137. // free instance
  138. free(o);
  139. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  140. }
  141. static const struct NCDModule modules[] = {
  142. {
  143. .type = "var",
  144. .func_new = func_new,
  145. .func_die = func_die,
  146. .func_getvar = func_getvar
  147. }, {
  148. .type = "var::set",
  149. .func_new = set_func_new,
  150. .func_die = set_func_die
  151. }, {
  152. .type = NULL
  153. }
  154. };
  155. const struct NCDModuleGroup ncdmodule_var = {
  156. .modules = modules
  157. };