ref.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @file ref.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. * References module.
  25. *
  26. * Synopsis:
  27. * refhere()
  28. * Variables:
  29. * Exposes variables and objects as seen from this refhere() statement.
  30. *
  31. * Synopsis:
  32. * ref refhere::ref()
  33. * ref ref::ref()
  34. * Variables:
  35. * Exposes variables and objects as seen from the corresponding refhere()
  36. * statement.
  37. */
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <misc/offset.h>
  41. #include <structure/LinkedList0.h>
  42. #include <ncd/NCDModule.h>
  43. #include <generated/blog_channel_ncd_ref.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct refhere_instance {
  46. NCDModuleInst *i;
  47. LinkedList0 refs_list;
  48. };
  49. struct ref_instance {
  50. NCDModuleInst *i;
  51. struct refhere_instance *rh;
  52. LinkedList0Node refs_list_node;
  53. };
  54. static void ref_instance_free (struct ref_instance *o);
  55. static void refhere_func_new (NCDModuleInst *i)
  56. {
  57. // allocate instance
  58. struct refhere_instance *o = malloc(sizeof(*o));
  59. if (!o) {
  60. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  61. goto fail0;
  62. }
  63. NCDModuleInst_Backend_SetUser(i, o);
  64. // init arguments
  65. o->i = i;
  66. // check arguments
  67. if (!NCDValue_ListRead(i->args, 0)) {
  68. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  69. goto fail1;
  70. }
  71. // init refs list
  72. LinkedList0_Init(&o->refs_list);
  73. // signal up
  74. NCDModuleInst_Backend_Up(o->i);
  75. return;
  76. fail1:
  77. free(o);
  78. fail0:
  79. NCDModuleInst_Backend_SetError(i);
  80. NCDModuleInst_Backend_Dead(i);
  81. }
  82. static void refhere_func_die (void *vo)
  83. {
  84. struct refhere_instance *o = vo;
  85. NCDModuleInst *i = o->i;
  86. // die refs
  87. while (!LinkedList0_IsEmpty(&o->refs_list)) {
  88. struct ref_instance *ref = UPPER_OBJECT(LinkedList0_GetFirst(&o->refs_list), struct ref_instance, refs_list_node);
  89. ASSERT(ref->rh == o)
  90. ref_instance_free(ref);
  91. }
  92. // free instance
  93. free(o);
  94. NCDModuleInst_Backend_Dead(i);
  95. }
  96. static int refhere_func_getvar (void *vo, const char *varname, NCDValue *out)
  97. {
  98. struct refhere_instance *o = vo;
  99. return NCDModuleInst_Backend_GetVar(o->i, varname, out);
  100. }
  101. static NCDModuleInst * refhere_func_getobj (void *vo, const char *objname)
  102. {
  103. struct refhere_instance *o = vo;
  104. return NCDModuleInst_Backend_GetObj(o->i, objname);
  105. }
  106. static void ref_func_new_templ (NCDModuleInst *i, struct refhere_instance *rh)
  107. {
  108. // allocate instance
  109. struct ref_instance *o = malloc(sizeof(*o));
  110. if (!o) {
  111. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  112. goto fail0;
  113. }
  114. NCDModuleInst_Backend_SetUser(i, o);
  115. // init arguments
  116. o->i = i;
  117. // check arguments
  118. if (!NCDValue_ListRead(i->args, 0)) {
  119. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  120. goto fail1;
  121. }
  122. // set refhere
  123. o->rh = rh;
  124. // add to refhere's refs list
  125. LinkedList0_Prepend(&o->rh->refs_list, &o->refs_list_node);
  126. // signal up
  127. NCDModuleInst_Backend_Up(o->i);
  128. return;
  129. fail1:
  130. free(o);
  131. fail0:
  132. NCDModuleInst_Backend_SetError(i);
  133. NCDModuleInst_Backend_Dead(i);
  134. }
  135. static void ref_func_new_from_refhere (NCDModuleInst *i)
  136. {
  137. struct refhere_instance *rh = i->method_object->inst_user;
  138. return ref_func_new_templ(i, rh);
  139. }
  140. static void ref_func_new_from_ref (NCDModuleInst *i)
  141. {
  142. struct ref_instance *ref = i->method_object->inst_user;
  143. return ref_func_new_templ(i, ref->rh);
  144. }
  145. static void ref_instance_free (struct ref_instance *o)
  146. {
  147. NCDModuleInst *i = o->i;
  148. // remove from refhere's reft list
  149. LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
  150. // free instance
  151. free(o);
  152. NCDModuleInst_Backend_Dead(i);
  153. }
  154. static void ref_func_die (void *vo)
  155. {
  156. struct ref_instance *o = vo;
  157. ref_instance_free(o);
  158. }
  159. static int ref_func_getvar (void *vo, const char *varname, NCDValue *out)
  160. {
  161. struct ref_instance *o = vo;
  162. return NCDModuleInst_Backend_GetVar(o->rh->i, varname, out);
  163. }
  164. static NCDModuleInst * ref_func_getobj (void *vo, const char *objname)
  165. {
  166. struct ref_instance *o = vo;
  167. return NCDModuleInst_Backend_GetObj(o->rh->i, objname);
  168. }
  169. static const struct NCDModule modules[] = {
  170. {
  171. .type = "refhere",
  172. .func_new = refhere_func_new,
  173. .func_die = refhere_func_die,
  174. .func_getvar = refhere_func_getvar,
  175. .func_getobj = refhere_func_getobj
  176. }, {
  177. .type = "refhere::ref",
  178. .base_type = "ref",
  179. .func_new = ref_func_new_from_refhere,
  180. .func_die = ref_func_die,
  181. .func_getvar = ref_func_getvar,
  182. .func_getobj = ref_func_getobj
  183. }, {
  184. .type = "ref::ref",
  185. .base_type = "ref",
  186. .func_new = ref_func_new_from_ref,
  187. .func_die = ref_func_die,
  188. .func_getvar = ref_func_getvar,
  189. .func_getobj = ref_func_getobj
  190. }, {
  191. .type = NULL
  192. }
  193. };
  194. const struct NCDModuleGroup ncdmodule_ref = {
  195. .modules = modules
  196. };