list.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. * Synopsis:
  60. * list::find(start_pos, value)
  61. * Description:
  62. * finds the first occurance of 'value' in the list at position >='start_pos'.
  63. * Variables:
  64. * pos - position of element, or "none" if not found
  65. * found - "true" if found, "false" if not
  66. *
  67. * Sysnopsis:
  68. * list::remove_at(remove_pos)
  69. * Description:
  70. * Removes the element at position 'remove_pos', which must refer to an existing element.
  71. */
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <stdio.h>
  75. #include <inttypes.h>
  76. #include <misc/parse_number.h>
  77. #include <ncd/NCDModule.h>
  78. #include <generated/blog_channel_ncd_list.h>
  79. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  80. struct instance {
  81. NCDModuleInst *i;
  82. NCDValue list;
  83. };
  84. struct append_instance {
  85. NCDModuleInst *i;
  86. };
  87. struct appendv_instance {
  88. NCDModuleInst *i;
  89. };
  90. struct length_instance {
  91. NCDModuleInst *i;
  92. size_t length;
  93. };
  94. struct get_instance {
  95. NCDModuleInst *i;
  96. NCDValue value;
  97. };
  98. struct shift_instance {
  99. NCDModuleInst *i;
  100. };
  101. struct contains_instance {
  102. NCDModuleInst *i;
  103. int contains;
  104. };
  105. struct find_instance {
  106. NCDModuleInst *i;
  107. int is_found;
  108. size_t found_pos;
  109. };
  110. struct removeat_instance {
  111. NCDModuleInst *i;
  112. };
  113. static void func_new_list (NCDModuleInst *i)
  114. {
  115. // allocate instance
  116. struct instance *o = malloc(sizeof(*o));
  117. if (!o) {
  118. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  119. goto fail0;
  120. }
  121. NCDModuleInst_Backend_SetUser(i, o);
  122. // init arguments
  123. o->i = i;
  124. // copy list
  125. if (!NCDValue_InitCopy(&o->list, i->args)) {
  126. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  127. goto fail1;
  128. }
  129. // signal up
  130. NCDModuleInst_Backend_Up(o->i);
  131. return;
  132. fail1:
  133. free(o);
  134. fail0:
  135. NCDModuleInst_Backend_SetError(i);
  136. NCDModuleInst_Backend_Dead(i);
  137. }
  138. static void func_new_listfrom (NCDModuleInst *i)
  139. {
  140. // allocate instance
  141. struct instance *o = malloc(sizeof(*o));
  142. if (!o) {
  143. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  144. goto fail0;
  145. }
  146. NCDModuleInst_Backend_SetUser(i, o);
  147. // init arguments
  148. o->i = i;
  149. // init list
  150. NCDValue_InitList(&o->list);
  151. // append contents of list arguments
  152. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  153. // check type
  154. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  155. ModuleLog(i, BLOG_ERROR, "wrong type");
  156. goto fail2;
  157. }
  158. // copy list
  159. NCDValue copy;
  160. if (!NCDValue_InitCopy(&copy, arg)) {
  161. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  162. goto fail2;
  163. }
  164. // append
  165. if (!NCDValue_ListAppendList(&o->list, copy)) {
  166. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  167. NCDValue_Free(&copy);
  168. goto fail2;
  169. }
  170. }
  171. // signal up
  172. NCDModuleInst_Backend_Up(o->i);
  173. return;
  174. fail2:
  175. NCDValue_Free(&o->list);
  176. fail1:
  177. free(o);
  178. fail0:
  179. NCDModuleInst_Backend_SetError(i);
  180. NCDModuleInst_Backend_Dead(i);
  181. }
  182. static void func_die (void *vo)
  183. {
  184. struct instance *o = vo;
  185. NCDModuleInst *i = o->i;
  186. // free list
  187. NCDValue_Free(&o->list);
  188. // free instance
  189. free(o);
  190. NCDModuleInst_Backend_Dead(i);
  191. }
  192. static int func_getvar (void *vo, const char *name, NCDValue *out)
  193. {
  194. struct instance *o = vo;
  195. if (!strcmp(name, "")) {
  196. if (!NCDValue_InitCopy(out, &o->list)) {
  197. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  198. return 0;
  199. }
  200. return 1;
  201. }
  202. if (!strcmp(name, "length")) {
  203. char str[64];
  204. snprintf(str, sizeof(str), "%zu", NCDValue_ListCount(&o->list));
  205. if (!NCDValue_InitString(out, str)) {
  206. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  207. return 0;
  208. }
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. static void append_func_new (NCDModuleInst *i)
  214. {
  215. // allocate instance
  216. struct append_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. NCDValue *arg;
  226. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  227. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  228. goto fail1;
  229. }
  230. // get method object
  231. struct instance *mo = i->method_object->inst_user;
  232. // append
  233. NCDValue v;
  234. if (!NCDValue_InitCopy(&v, arg)) {
  235. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  236. goto fail1;
  237. }
  238. if (!NCDValue_ListAppend(&mo->list, v)) {
  239. NCDValue_Free(&v);
  240. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  241. goto fail1;
  242. }
  243. // signal up
  244. NCDModuleInst_Backend_Up(o->i);
  245. return;
  246. fail1:
  247. free(o);
  248. fail0:
  249. NCDModuleInst_Backend_SetError(i);
  250. NCDModuleInst_Backend_Dead(i);
  251. }
  252. static void append_func_die (void *vo)
  253. {
  254. struct append_instance *o = vo;
  255. NCDModuleInst *i = o->i;
  256. // free instance
  257. free(o);
  258. NCDModuleInst_Backend_Dead(i);
  259. }
  260. static void appendv_func_new (NCDModuleInst *i)
  261. {
  262. // allocate instance
  263. struct appendv_instance *o = malloc(sizeof(*o));
  264. if (!o) {
  265. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  266. goto fail0;
  267. }
  268. NCDModuleInst_Backend_SetUser(i, o);
  269. // init arguments
  270. o->i = i;
  271. // check arguments
  272. NCDValue *arg;
  273. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  274. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  275. goto fail1;
  276. }
  277. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  278. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  279. goto fail1;
  280. }
  281. // get method object
  282. struct instance *mo = i->method_object->inst_user;
  283. // append
  284. NCDValue l;
  285. if (!NCDValue_InitCopy(&l, arg)) {
  286. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  287. goto fail1;
  288. }
  289. if (!NCDValue_ListAppendList(&mo->list, l)) {
  290. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  291. NCDValue_Free(&l);
  292. goto fail1;
  293. }
  294. // signal up
  295. NCDModuleInst_Backend_Up(o->i);
  296. return;
  297. fail1:
  298. free(o);
  299. fail0:
  300. NCDModuleInst_Backend_SetError(i);
  301. NCDModuleInst_Backend_Dead(i);
  302. }
  303. static void appendv_func_die (void *vo)
  304. {
  305. struct appendv_instance *o = vo;
  306. NCDModuleInst *i = o->i;
  307. // free instance
  308. free(o);
  309. NCDModuleInst_Backend_Dead(i);
  310. }
  311. static void length_func_new (NCDModuleInst *i)
  312. {
  313. // allocate instance
  314. struct length_instance *o = malloc(sizeof(*o));
  315. if (!o) {
  316. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  317. goto fail0;
  318. }
  319. NCDModuleInst_Backend_SetUser(i, o);
  320. // init arguments
  321. o->i = i;
  322. // check arguments
  323. if (!NCDValue_ListRead(o->i->args, 0)) {
  324. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  325. goto fail1;
  326. }
  327. // get method object
  328. struct instance *mo = i->method_object->inst_user;
  329. // remember length
  330. o->length = NCDValue_ListCount(&mo->list);
  331. // signal up
  332. NCDModuleInst_Backend_Up(o->i);
  333. return;
  334. fail1:
  335. free(o);
  336. fail0:
  337. NCDModuleInst_Backend_SetError(i);
  338. NCDModuleInst_Backend_Dead(i);
  339. }
  340. static void length_func_die (void *vo)
  341. {
  342. struct length_instance *o = vo;
  343. NCDModuleInst *i = o->i;
  344. // free instance
  345. free(o);
  346. NCDModuleInst_Backend_Dead(i);
  347. }
  348. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  349. {
  350. struct length_instance *o = vo;
  351. if (!strcmp(name, "")) {
  352. char str[64];
  353. snprintf(str, sizeof(str), "%zu", o->length);
  354. if (!NCDValue_InitString(out, str)) {
  355. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  356. return 0;
  357. }
  358. return 1;
  359. }
  360. return 0;
  361. }
  362. static void get_func_new (NCDModuleInst *i)
  363. {
  364. // allocate instance
  365. struct get_instance *o = malloc(sizeof(*o));
  366. if (!o) {
  367. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  368. goto fail0;
  369. }
  370. NCDModuleInst_Backend_SetUser(i, o);
  371. // init arguments
  372. o->i = i;
  373. // check arguments
  374. NCDValue *index_arg;
  375. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  376. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  377. goto fail1;
  378. }
  379. if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
  380. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  381. goto fail1;
  382. }
  383. uintmax_t index;
  384. if (!parse_unsigned_integer(NCDValue_StringValue(index_arg), &index)) {
  385. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  386. goto fail1;
  387. }
  388. // get method object
  389. struct instance *mo = i->method_object->inst_user;
  390. // check index
  391. if (index >= NCDValue_ListCount(&mo->list)) {
  392. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  393. goto fail1;
  394. }
  395. // copy value
  396. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  397. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  398. goto fail1;
  399. }
  400. // signal up
  401. NCDModuleInst_Backend_Up(o->i);
  402. return;
  403. fail1:
  404. free(o);
  405. fail0:
  406. NCDModuleInst_Backend_SetError(i);
  407. NCDModuleInst_Backend_Dead(i);
  408. }
  409. static void get_func_die (void *vo)
  410. {
  411. struct get_instance *o = vo;
  412. NCDModuleInst *i = o->i;
  413. // free value
  414. NCDValue_Free(&o->value);
  415. // free instance
  416. free(o);
  417. NCDModuleInst_Backend_Dead(i);
  418. }
  419. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  420. {
  421. struct get_instance *o = vo;
  422. if (!strcmp(name, "")) {
  423. if (!NCDValue_InitCopy(out, &o->value)) {
  424. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  425. return 0;
  426. }
  427. return 1;
  428. }
  429. return 0;
  430. }
  431. static void shift_func_new (NCDModuleInst *i)
  432. {
  433. // allocate instance
  434. struct shift_instance *o = malloc(sizeof(*o));
  435. if (!o) {
  436. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  437. goto fail0;
  438. }
  439. NCDModuleInst_Backend_SetUser(i, o);
  440. // init arguments
  441. o->i = i;
  442. // check arguments
  443. if (!NCDValue_ListRead(o->i->args, 0)) {
  444. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  445. goto fail1;
  446. }
  447. // get method object
  448. struct instance *mo = i->method_object->inst_user;
  449. // shift
  450. if (!NCDValue_ListFirst(&mo->list)) {
  451. ModuleLog(o->i, BLOG_ERROR, "list has no elements");
  452. goto fail1;
  453. }
  454. NCDValue v = NCDValue_ListShift(&mo->list);
  455. NCDValue_Free(&v);
  456. // signal up
  457. NCDModuleInst_Backend_Up(o->i);
  458. return;
  459. fail1:
  460. free(o);
  461. fail0:
  462. NCDModuleInst_Backend_SetError(i);
  463. NCDModuleInst_Backend_Dead(i);
  464. }
  465. static void shift_func_die (void *vo)
  466. {
  467. struct shift_instance *o = vo;
  468. NCDModuleInst *i = o->i;
  469. // free instance
  470. free(o);
  471. NCDModuleInst_Backend_Dead(i);
  472. }
  473. static void contains_func_new (NCDModuleInst *i)
  474. {
  475. // allocate instance
  476. struct contains_instance *o = malloc(sizeof(*o));
  477. if (!o) {
  478. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  479. goto fail0;
  480. }
  481. NCDModuleInst_Backend_SetUser(i, o);
  482. // init arguments
  483. o->i = i;
  484. // read arguments
  485. NCDValue *value_arg;
  486. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  487. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  488. goto fail1;
  489. }
  490. // get method object
  491. struct instance *mo = i->method_object->inst_user;
  492. // search
  493. o->contains = 0;
  494. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  495. if (NCDValue_Compare(v, value_arg) == 0) {
  496. o->contains = 1;
  497. break;
  498. }
  499. }
  500. // signal up
  501. NCDModuleInst_Backend_Up(o->i);
  502. return;
  503. fail1:
  504. free(o);
  505. fail0:
  506. NCDModuleInst_Backend_SetError(i);
  507. NCDModuleInst_Backend_Dead(i);
  508. }
  509. static void contains_func_die (void *vo)
  510. {
  511. struct contains_instance *o = vo;
  512. NCDModuleInst *i = o->i;
  513. // free instance
  514. free(o);
  515. NCDModuleInst_Backend_Dead(i);
  516. }
  517. static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
  518. {
  519. struct contains_instance *o = vo;
  520. if (!strcmp(name, "")) {
  521. const char *value = (o->contains ? "true" : "false");
  522. if (!NCDValue_InitString(out, value)) {
  523. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  524. return 0;
  525. }
  526. return 1;
  527. }
  528. return 0;
  529. }
  530. static void find_func_new (NCDModuleInst *i)
  531. {
  532. // allocate instance
  533. struct find_instance *o = malloc(sizeof(*o));
  534. if (!o) {
  535. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  536. goto fail0;
  537. }
  538. NCDModuleInst_Backend_SetUser(i, o);
  539. // init arguments
  540. o->i = i;
  541. // read arguments
  542. NCDValue *start_pos_arg;
  543. NCDValue *value_arg;
  544. if (!NCDValue_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
  545. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  546. goto fail1;
  547. }
  548. if (NCDValue_Type(start_pos_arg) != NCDVALUE_STRING) {
  549. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  550. goto fail1;
  551. }
  552. // read start position
  553. uintmax_t start_pos;
  554. if (!parse_unsigned_integer(NCDValue_StringValue(start_pos_arg), &start_pos)) {
  555. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  556. goto fail1;
  557. }
  558. // get method object
  559. struct instance *mo = i->method_object->inst_user;
  560. // search
  561. o->is_found = 0;
  562. size_t pos = 0;
  563. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  564. if (pos >= start_pos && NCDValue_Compare(v, value_arg) == 0) {
  565. o->is_found = 1;
  566. o->found_pos = pos;
  567. break;
  568. }
  569. pos++;
  570. }
  571. // signal up
  572. NCDModuleInst_Backend_Up(o->i);
  573. return;
  574. fail1:
  575. free(o);
  576. fail0:
  577. NCDModuleInst_Backend_SetError(i);
  578. NCDModuleInst_Backend_Dead(i);
  579. }
  580. static void find_func_die (void *vo)
  581. {
  582. struct find_instance *o = vo;
  583. NCDModuleInst *i = o->i;
  584. // free instance
  585. free(o);
  586. NCDModuleInst_Backend_Dead(i);
  587. }
  588. static int find_func_getvar (void *vo, const char *name, NCDValue *out)
  589. {
  590. struct find_instance *o = vo;
  591. if (!strcmp(name, "pos")) {
  592. char value[64];
  593. if (o->is_found) {
  594. snprintf(value, sizeof(value), "%zu", o->found_pos);
  595. } else {
  596. snprintf(value, sizeof(value), "none");
  597. }
  598. if (!NCDValue_InitString(out, value)) {
  599. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  600. return 0;
  601. }
  602. return 1;
  603. }
  604. if (!strcmp(name, "found")) {
  605. const char *value = (o->is_found ? "true" : "false");
  606. if (!NCDValue_InitString(out, value)) {
  607. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  608. return 0;
  609. }
  610. return 1;
  611. }
  612. return 0;
  613. }
  614. static void removeat_func_new (NCDModuleInst *i)
  615. {
  616. // allocate instance
  617. struct removeat_instance *o = malloc(sizeof(*o));
  618. if (!o) {
  619. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  620. goto fail0;
  621. }
  622. NCDModuleInst_Backend_SetUser(i, o);
  623. // init arguments
  624. o->i = i;
  625. // read arguments
  626. NCDValue *remove_pos_arg;
  627. if (!NCDValue_ListRead(i->args, 1, &remove_pos_arg)) {
  628. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  629. goto fail1;
  630. }
  631. if (NCDValue_Type(remove_pos_arg) != NCDVALUE_STRING) {
  632. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  633. goto fail1;
  634. }
  635. // read position
  636. uintmax_t remove_pos;
  637. if (!parse_unsigned_integer(NCDValue_StringValue(remove_pos_arg), &remove_pos)) {
  638. ModuleLog(o->i, BLOG_ERROR, "wrong pos");
  639. goto fail1;
  640. }
  641. // get method object
  642. struct instance *mo = i->method_object->inst_user;
  643. // check position
  644. if (remove_pos >= NCDValue_ListCount(&mo->list)) {
  645. ModuleLog(o->i, BLOG_ERROR, "pos out of range");
  646. goto fail1;
  647. }
  648. // find and remove
  649. size_t pos = 0;
  650. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  651. if (pos == remove_pos) {
  652. NCDValue removed_v = NCDValue_ListRemove(&mo->list, v);
  653. NCDValue_Free(&removed_v);
  654. break;
  655. }
  656. pos++;
  657. }
  658. // signal up
  659. NCDModuleInst_Backend_Up(o->i);
  660. return;
  661. fail1:
  662. free(o);
  663. fail0:
  664. NCDModuleInst_Backend_SetError(i);
  665. NCDModuleInst_Backend_Dead(i);
  666. }
  667. static void removeat_func_die (void *vo)
  668. {
  669. struct removeat_instance *o = vo;
  670. NCDModuleInst *i = o->i;
  671. // free instance
  672. free(o);
  673. NCDModuleInst_Backend_Dead(i);
  674. }
  675. static const struct NCDModule modules[] = {
  676. {
  677. .type = "list",
  678. .func_new = func_new_list,
  679. .func_die = func_die,
  680. .func_getvar = func_getvar
  681. }, {
  682. .type = "listfrom",
  683. .base_type = "list",
  684. .func_new = func_new_listfrom,
  685. .func_die = func_die,
  686. .func_getvar = func_getvar
  687. }, {
  688. .type = "concatlist", // alias for listfrom
  689. .base_type = "list",
  690. .func_new = func_new_listfrom,
  691. .func_die = func_die,
  692. .func_getvar = func_getvar
  693. }, {
  694. .type = "list::append",
  695. .func_new = append_func_new,
  696. .func_die = append_func_die
  697. }, {
  698. .type = "list::appendv",
  699. .func_new = appendv_func_new,
  700. .func_die = appendv_func_die
  701. }, {
  702. .type = "list::length",
  703. .func_new = length_func_new,
  704. .func_die = length_func_die,
  705. .func_getvar = length_func_getvar
  706. }, {
  707. .type = "list::get",
  708. .func_new = get_func_new,
  709. .func_die = get_func_die,
  710. .func_getvar = get_func_getvar
  711. }, {
  712. .type = "list::shift",
  713. .func_new = shift_func_new,
  714. .func_die = shift_func_die
  715. }, {
  716. .type = "list::contains",
  717. .func_new = contains_func_new,
  718. .func_die = contains_func_die,
  719. .func_getvar = contains_func_getvar
  720. }, {
  721. .type = "list::find",
  722. .func_new = find_func_new,
  723. .func_die = find_func_die,
  724. .func_getvar = find_func_getvar
  725. }, {
  726. .type = "list::remove_at",
  727. .func_new = removeat_func_new,
  728. .func_die = removeat_func_die
  729. }, {
  730. .type = NULL
  731. }
  732. };
  733. const struct NCDModuleGroup ncdmodule_list = {
  734. .modules = modules
  735. };