list.c 15 KB

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