list.c 4.2 KB

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