list.c 15 KB

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