list.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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:
  27. * list(elem1, ..., elemN)
  28. * list listfrom(list l1, ..., list lN)
  29. *
  30. * Description:
  31. * The first form creates a list with the given elements.
  32. * The second form creates a list by concatenating the given
  33. * lists.
  34. *
  35. * Variables:
  36. * (empty) - list containing elem1, ..., elemN
  37. *
  38. * Synopsis: list::append(arg)
  39. *
  40. * Synopsis: list::appendv(list arg)
  41. * Description: Appends the elements of arg to the list.
  42. *
  43. * Synopsis: list::length()
  44. * Variables:
  45. * (empty) - number of elements in list at the time of initialization
  46. *
  47. * Synopsis: list::get(string index)
  48. * Variables:
  49. * (empty) - element of list at position index (starting from zero) at the time of initialization
  50. *
  51. * Synopsis: list::shift()
  52. */
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <stdio.h>
  56. #include <inttypes.h>
  57. #include <ncd/NCDModule.h>
  58. #include <generated/blog_channel_ncd_list.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. struct instance {
  61. NCDModuleInst *i;
  62. NCDValue list;
  63. };
  64. struct append_instance {
  65. NCDModuleInst *i;
  66. };
  67. struct appendv_instance {
  68. NCDModuleInst *i;
  69. };
  70. struct length_instance {
  71. NCDModuleInst *i;
  72. size_t length;
  73. };
  74. struct get_instance {
  75. NCDModuleInst *i;
  76. NCDValue value;
  77. };
  78. struct shift_instance {
  79. NCDModuleInst *i;
  80. };
  81. static void func_new_list (NCDModuleInst *i)
  82. {
  83. // allocate instance
  84. struct instance *o = malloc(sizeof(*o));
  85. if (!o) {
  86. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  87. goto fail0;
  88. }
  89. NCDModuleInst_Backend_SetUser(i, o);
  90. // init arguments
  91. o->i = i;
  92. // copy list
  93. if (!NCDValue_InitCopy(&o->list, i->args)) {
  94. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  95. goto fail1;
  96. }
  97. // signal up
  98. NCDModuleInst_Backend_Up(o->i);
  99. return;
  100. fail1:
  101. free(o);
  102. fail0:
  103. NCDModuleInst_Backend_SetError(i);
  104. NCDModuleInst_Backend_Dead(i);
  105. }
  106. static void func_new_listfrom (NCDModuleInst *i)
  107. {
  108. // allocate instance
  109. struct 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. // init list
  118. NCDValue_InitList(&o->list);
  119. // append contents of list arguments
  120. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  121. // check type
  122. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  123. ModuleLog(i, BLOG_ERROR, "wrong type");
  124. goto fail2;
  125. }
  126. // copy list
  127. NCDValue copy;
  128. if (!NCDValue_InitCopy(&copy, arg)) {
  129. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  130. goto fail2;
  131. }
  132. // append
  133. if (!NCDValue_ListAppendList(&o->list, copy)) {
  134. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  135. NCDValue_Free(&copy);
  136. goto fail2;
  137. }
  138. }
  139. // signal up
  140. NCDModuleInst_Backend_Up(o->i);
  141. return;
  142. fail2:
  143. NCDValue_Free(&o->list);
  144. fail1:
  145. free(o);
  146. fail0:
  147. NCDModuleInst_Backend_SetError(i);
  148. NCDModuleInst_Backend_Dead(i);
  149. }
  150. static void func_die (void *vo)
  151. {
  152. struct instance *o = vo;
  153. NCDModuleInst *i = o->i;
  154. // free list
  155. NCDValue_Free(&o->list);
  156. // free instance
  157. free(o);
  158. NCDModuleInst_Backend_Dead(i);
  159. }
  160. static int func_getvar (void *vo, const char *name, NCDValue *out)
  161. {
  162. struct instance *o = vo;
  163. if (!strcmp(name, "")) {
  164. if (!NCDValue_InitCopy(out, &o->list)) {
  165. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  166. return 0;
  167. }
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static void append_func_new (NCDModuleInst *i)
  173. {
  174. // allocate instance
  175. struct append_instance *o = malloc(sizeof(*o));
  176. if (!o) {
  177. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  178. goto fail0;
  179. }
  180. NCDModuleInst_Backend_SetUser(i, o);
  181. // init arguments
  182. o->i = i;
  183. // check arguments
  184. NCDValue *arg;
  185. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  186. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  187. goto fail1;
  188. }
  189. // get method object
  190. struct instance *mo = i->method_object->inst_user;
  191. // append
  192. NCDValue v;
  193. if (!NCDValue_InitCopy(&v, arg)) {
  194. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  195. goto fail1;
  196. }
  197. if (!NCDValue_ListAppend(&mo->list, v)) {
  198. NCDValue_Free(&v);
  199. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  200. goto fail1;
  201. }
  202. // signal up
  203. NCDModuleInst_Backend_Up(o->i);
  204. return;
  205. fail1:
  206. free(o);
  207. fail0:
  208. NCDModuleInst_Backend_SetError(i);
  209. NCDModuleInst_Backend_Dead(i);
  210. }
  211. static void append_func_die (void *vo)
  212. {
  213. struct append_instance *o = vo;
  214. NCDModuleInst *i = o->i;
  215. // free instance
  216. free(o);
  217. NCDModuleInst_Backend_Dead(i);
  218. }
  219. static void appendv_func_new (NCDModuleInst *i)
  220. {
  221. // allocate instance
  222. struct appendv_instance *o = malloc(sizeof(*o));
  223. if (!o) {
  224. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  225. goto fail0;
  226. }
  227. NCDModuleInst_Backend_SetUser(i, o);
  228. // init arguments
  229. o->i = i;
  230. // check arguments
  231. NCDValue *arg;
  232. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  233. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  234. goto fail1;
  235. }
  236. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  237. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  238. goto fail1;
  239. }
  240. // get method object
  241. struct instance *mo = i->method_object->inst_user;
  242. // append
  243. NCDValue l;
  244. if (!NCDValue_InitCopy(&l, arg)) {
  245. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  246. goto fail1;
  247. }
  248. if (!NCDValue_ListAppendList(&mo->list, l)) {
  249. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  250. NCDValue_Free(&l);
  251. goto fail1;
  252. }
  253. // signal up
  254. NCDModuleInst_Backend_Up(o->i);
  255. return;
  256. fail1:
  257. free(o);
  258. fail0:
  259. NCDModuleInst_Backend_SetError(i);
  260. NCDModuleInst_Backend_Dead(i);
  261. }
  262. static void appendv_func_die (void *vo)
  263. {
  264. struct appendv_instance *o = vo;
  265. NCDModuleInst *i = o->i;
  266. // free instance
  267. free(o);
  268. NCDModuleInst_Backend_Dead(i);
  269. }
  270. static void length_func_new (NCDModuleInst *i)
  271. {
  272. // allocate instance
  273. struct length_instance *o = malloc(sizeof(*o));
  274. if (!o) {
  275. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  276. goto fail0;
  277. }
  278. NCDModuleInst_Backend_SetUser(i, o);
  279. // init arguments
  280. o->i = i;
  281. // check arguments
  282. if (!NCDValue_ListRead(o->i->args, 0)) {
  283. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  284. goto fail1;
  285. }
  286. // get method object
  287. struct instance *mo = i->method_object->inst_user;
  288. // remember length
  289. o->length = NCDValue_ListCount(&mo->list);
  290. // signal up
  291. NCDModuleInst_Backend_Up(o->i);
  292. return;
  293. fail1:
  294. free(o);
  295. fail0:
  296. NCDModuleInst_Backend_SetError(i);
  297. NCDModuleInst_Backend_Dead(i);
  298. }
  299. static void length_func_die (void *vo)
  300. {
  301. struct length_instance *o = vo;
  302. NCDModuleInst *i = o->i;
  303. // free instance
  304. free(o);
  305. NCDModuleInst_Backend_Dead(i);
  306. }
  307. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  308. {
  309. struct length_instance *o = vo;
  310. if (!strcmp(name, "")) {
  311. char str[50];
  312. snprintf(str, sizeof(str), "%"PRIuMAX, (uintmax_t)o->length);
  313. if (!NCDValue_InitString(out, str)) {
  314. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. return 0;
  320. }
  321. static void get_func_new (NCDModuleInst *i)
  322. {
  323. // allocate instance
  324. struct get_instance *o = malloc(sizeof(*o));
  325. if (!o) {
  326. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  327. goto fail0;
  328. }
  329. NCDModuleInst_Backend_SetUser(i, o);
  330. // init arguments
  331. o->i = i;
  332. // check arguments
  333. NCDValue *index_arg;
  334. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  335. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  336. goto fail1;
  337. }
  338. if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
  339. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  340. goto fail1;
  341. }
  342. uintmax_t index;
  343. if (sscanf(NCDValue_StringValue(index_arg), "%"SCNuMAX, &index) != 1) {
  344. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  345. goto fail1;
  346. }
  347. // get method object
  348. struct instance *mo = i->method_object->inst_user;
  349. // check index
  350. if (index >= NCDValue_ListCount(&mo->list)) {
  351. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  352. goto fail1;
  353. }
  354. // copy value
  355. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  356. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  357. goto fail1;
  358. }
  359. // signal up
  360. NCDModuleInst_Backend_Up(o->i);
  361. return;
  362. fail1:
  363. free(o);
  364. fail0:
  365. NCDModuleInst_Backend_SetError(i);
  366. NCDModuleInst_Backend_Dead(i);
  367. }
  368. static void get_func_die (void *vo)
  369. {
  370. struct get_instance *o = vo;
  371. NCDModuleInst *i = o->i;
  372. // free value
  373. NCDValue_Free(&o->value);
  374. // free instance
  375. free(o);
  376. NCDModuleInst_Backend_Dead(i);
  377. }
  378. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  379. {
  380. struct get_instance *o = vo;
  381. if (!strcmp(name, "")) {
  382. if (!NCDValue_InitCopy(out, &o->value)) {
  383. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  384. return 0;
  385. }
  386. return 1;
  387. }
  388. return 0;
  389. }
  390. static void shift_func_new (NCDModuleInst *i)
  391. {
  392. // allocate instance
  393. struct shift_instance *o = malloc(sizeof(*o));
  394. if (!o) {
  395. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  396. goto fail0;
  397. }
  398. NCDModuleInst_Backend_SetUser(i, o);
  399. // init arguments
  400. o->i = i;
  401. // check arguments
  402. if (!NCDValue_ListRead(o->i->args, 0)) {
  403. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  404. goto fail1;
  405. }
  406. // get method object
  407. struct instance *mo = i->method_object->inst_user;
  408. // shift
  409. if (!NCDValue_ListFirst(&mo->list)) {
  410. ModuleLog(o->i, BLOG_ERROR, "list has no elements");
  411. goto fail1;
  412. }
  413. NCDValue v = NCDValue_ListShift(&mo->list);
  414. NCDValue_Free(&v);
  415. // signal up
  416. NCDModuleInst_Backend_Up(o->i);
  417. return;
  418. fail1:
  419. free(o);
  420. fail0:
  421. NCDModuleInst_Backend_SetError(i);
  422. NCDModuleInst_Backend_Dead(i);
  423. }
  424. static void shift_func_die (void *vo)
  425. {
  426. struct shift_instance *o = vo;
  427. NCDModuleInst *i = o->i;
  428. // free instance
  429. free(o);
  430. NCDModuleInst_Backend_Dead(i);
  431. }
  432. static const struct NCDModule modules[] = {
  433. {
  434. .type = "list",
  435. .func_new = func_new_list,
  436. .func_die = func_die,
  437. .func_getvar = func_getvar
  438. }, {
  439. .type = "listfrom",
  440. .base_type = "list",
  441. .func_new = func_new_listfrom,
  442. .func_die = func_die,
  443. .func_getvar = func_getvar
  444. }, {
  445. .type = "list::append",
  446. .func_new = append_func_new,
  447. .func_die = append_func_die
  448. }, {
  449. .type = "list::appendv",
  450. .func_new = appendv_func_new,
  451. .func_die = appendv_func_die
  452. }, {
  453. .type = "list::length",
  454. .func_new = length_func_new,
  455. .func_die = length_func_die,
  456. .func_getvar = length_func_getvar
  457. }, {
  458. .type = "list::get",
  459. .func_new = get_func_new,
  460. .func_die = get_func_die,
  461. .func_getvar = get_func_getvar
  462. }, {
  463. .type = "list::shift",
  464. .func_new = shift_func_new,
  465. .func_die = shift_func_die
  466. }, {
  467. .type = NULL
  468. }
  469. };
  470. const struct NCDModuleGroup ncdmodule_list = {
  471. .modules = modules
  472. };