list.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. * Synopsis: list::appendv(list arg)
  33. * Description: Appends the elements of arg to the list.
  34. *
  35. * Synopsis: list::length()
  36. * Variables:
  37. * (empty) - number of elements in list at the time of initialization
  38. *
  39. * Synopsis: list::get(string index)
  40. * Variables:
  41. * (empty) - element of list at position index (starting from zero) at the time of initialization
  42. */
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <stdio.h>
  46. #include <inttypes.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_list.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. struct instance {
  51. NCDModuleInst *i;
  52. NCDValue list;
  53. };
  54. struct append_instance {
  55. NCDModuleInst *i;
  56. };
  57. struct appendv_instance {
  58. NCDModuleInst *i;
  59. };
  60. struct length_instance {
  61. NCDModuleInst *i;
  62. size_t length;
  63. };
  64. struct get_instance {
  65. NCDModuleInst *i;
  66. NCDValue value;
  67. };
  68. static void func_new (NCDModuleInst *i)
  69. {
  70. // allocate instance
  71. struct instance *o = malloc(sizeof(*o));
  72. if (!o) {
  73. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  74. goto fail0;
  75. }
  76. NCDModuleInst_Backend_SetUser(i, o);
  77. // init arguments
  78. o->i = i;
  79. // copy list
  80. if (!NCDValue_InitCopy(&o->list, o->i->args)) {
  81. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  82. goto fail1;
  83. }
  84. // signal up
  85. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  86. return;
  87. fail1:
  88. free(o);
  89. fail0:
  90. NCDModuleInst_Backend_SetError(i);
  91. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  92. }
  93. static void func_die (void *vo)
  94. {
  95. struct instance *o = vo;
  96. NCDModuleInst *i = o->i;
  97. // free list
  98. NCDValue_Free(&o->list);
  99. // free instance
  100. free(o);
  101. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  102. }
  103. static int func_getvar (void *vo, const char *name, NCDValue *out)
  104. {
  105. struct instance *o = vo;
  106. if (!strcmp(name, "")) {
  107. if (!NCDValue_InitCopy(out, &o->list)) {
  108. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  109. return 0;
  110. }
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. static void append_func_new (NCDModuleInst *i)
  116. {
  117. // allocate instance
  118. struct append_instance *o = malloc(sizeof(*o));
  119. if (!o) {
  120. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  121. goto fail0;
  122. }
  123. NCDModuleInst_Backend_SetUser(i, o);
  124. // init arguments
  125. o->i = i;
  126. // check arguments
  127. NCDValue *arg;
  128. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  129. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  130. goto fail1;
  131. }
  132. // get method object
  133. struct instance *mo = i->method_object->inst_user;
  134. // append
  135. NCDValue v;
  136. if (!NCDValue_InitCopy(&v, arg)) {
  137. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  138. goto fail1;
  139. }
  140. if (!NCDValue_ListAppend(&mo->list, v)) {
  141. NCDValue_Free(&v);
  142. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  143. goto fail1;
  144. }
  145. // signal up
  146. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  147. return;
  148. fail1:
  149. free(o);
  150. fail0:
  151. NCDModuleInst_Backend_SetError(i);
  152. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  153. }
  154. static void append_func_die (void *vo)
  155. {
  156. struct append_instance *o = vo;
  157. NCDModuleInst *i = o->i;
  158. // free instance
  159. free(o);
  160. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  161. }
  162. static void appendv_func_new (NCDModuleInst *i)
  163. {
  164. // allocate instance
  165. struct appendv_instance *o = malloc(sizeof(*o));
  166. if (!o) {
  167. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  168. goto fail0;
  169. }
  170. NCDModuleInst_Backend_SetUser(i, o);
  171. // init arguments
  172. o->i = i;
  173. // check arguments
  174. NCDValue *arg;
  175. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  176. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  177. goto fail1;
  178. }
  179. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  180. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  181. goto fail1;
  182. }
  183. // get method object
  184. struct instance *mo = i->method_object->inst_user;
  185. // append
  186. NCDValue l;
  187. if (!NCDValue_InitCopy(&l, arg)) {
  188. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  189. goto fail1;
  190. }
  191. if (!NCDValue_ListAppendList(&mo->list, l)) {
  192. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  193. NCDValue_Free(&l);
  194. goto fail1;
  195. }
  196. // signal up
  197. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  198. return;
  199. fail1:
  200. free(o);
  201. fail0:
  202. NCDModuleInst_Backend_SetError(i);
  203. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  204. }
  205. static void appendv_func_die (void *vo)
  206. {
  207. struct appendv_instance *o = vo;
  208. NCDModuleInst *i = o->i;
  209. // free instance
  210. free(o);
  211. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  212. }
  213. static void length_func_new (NCDModuleInst *i)
  214. {
  215. // allocate instance
  216. struct length_instance *o = malloc(sizeof(*o));
  217. if (!o) {
  218. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  219. goto fail0;
  220. }
  221. NCDModuleInst_Backend_SetUser(i, o);
  222. // init arguments
  223. o->i = i;
  224. // check arguments
  225. if (!NCDValue_ListRead(o->i->args, 0)) {
  226. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  227. goto fail1;
  228. }
  229. // get method object
  230. struct instance *mo = i->method_object->inst_user;
  231. // remember length
  232. o->length = NCDValue_ListCount(&mo->list);
  233. // signal up
  234. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  235. return;
  236. fail1:
  237. free(o);
  238. fail0:
  239. NCDModuleInst_Backend_SetError(i);
  240. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  241. }
  242. static void length_func_die (void *vo)
  243. {
  244. struct length_instance *o = vo;
  245. NCDModuleInst *i = o->i;
  246. // free instance
  247. free(o);
  248. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  249. }
  250. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  251. {
  252. struct length_instance *o = vo;
  253. if (!strcmp(name, "")) {
  254. char str[50];
  255. snprintf(str, sizeof(str), "%"PRIuMAX, (uintmax_t)o->length);
  256. if (!NCDValue_InitString(out, str)) {
  257. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. return 0;
  263. }
  264. static void get_func_new (NCDModuleInst *i)
  265. {
  266. // allocate instance
  267. struct get_instance *o = malloc(sizeof(*o));
  268. if (!o) {
  269. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  270. goto fail0;
  271. }
  272. NCDModuleInst_Backend_SetUser(i, o);
  273. // init arguments
  274. o->i = i;
  275. // check arguments
  276. NCDValue *index_arg;
  277. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  278. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  279. goto fail1;
  280. }
  281. if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
  282. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  283. goto fail1;
  284. }
  285. uintmax_t index;
  286. if (sscanf(NCDValue_StringValue(index_arg), "%"SCNuMAX, &index) != 1) {
  287. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  288. goto fail1;
  289. }
  290. // get method object
  291. struct instance *mo = i->method_object->inst_user;
  292. // check index
  293. if (index >= NCDValue_ListCount(&mo->list)) {
  294. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  295. goto fail1;
  296. }
  297. // copy value
  298. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  299. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  300. goto fail1;
  301. }
  302. // signal up
  303. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  304. return;
  305. fail1:
  306. free(o);
  307. fail0:
  308. NCDModuleInst_Backend_SetError(i);
  309. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  310. }
  311. static void get_func_die (void *vo)
  312. {
  313. struct get_instance *o = vo;
  314. NCDModuleInst *i = o->i;
  315. // free value
  316. NCDValue_Free(&o->value);
  317. // free instance
  318. free(o);
  319. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  320. }
  321. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  322. {
  323. struct get_instance *o = vo;
  324. if (!strcmp(name, "")) {
  325. if (!NCDValue_InitCopy(out, &o->value)) {
  326. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  327. return 0;
  328. }
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. static const struct NCDModule modules[] = {
  334. {
  335. .type = "list",
  336. .func_new = func_new,
  337. .func_die = func_die,
  338. .func_getvar = func_getvar
  339. }, {
  340. .type = "list::append",
  341. .func_new = append_func_new,
  342. .func_die = append_func_die
  343. }, {
  344. .type = "list::appendv",
  345. .func_new = appendv_func_new,
  346. .func_die = appendv_func_die
  347. }, {
  348. .type = "list::length",
  349. .func_new = length_func_new,
  350. .func_die = length_func_die,
  351. .func_getvar = length_func_getvar
  352. }, {
  353. .type = "list::get",
  354. .func_new = get_func_new,
  355. .func_die = get_func_die,
  356. .func_getvar = get_func_getvar
  357. }, {
  358. .type = NULL
  359. }
  360. };
  361. const struct NCDModuleGroup ncdmodule_list = {
  362. .modules = modules
  363. };