list.c 12 KB

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