list.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /**
  2. * @file list.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * List construction module.
  32. *
  33. * Synopsis:
  34. * list(elem1, ..., elemN)
  35. * list listfrom(list l1, ..., list lN)
  36. *
  37. * Description:
  38. * The first form creates a list with the given elements.
  39. * The second form creates a list by concatenating the given
  40. * lists.
  41. *
  42. * Variables:
  43. * (empty) - list containing elem1, ..., elemN
  44. * length - number of elements in list
  45. *
  46. * Synopsis: list::append(arg)
  47. *
  48. * Synopsis: list::appendv(list arg)
  49. * Description: Appends the elements of arg to the list.
  50. *
  51. * Synopsis: list::length()
  52. * Variables:
  53. * (empty) - number of elements in list at the time of initialization
  54. * of this method
  55. *
  56. * Synopsis: list::get(string index)
  57. * Variables:
  58. * (empty) - element of list at position index (starting from zero) at the time of initialization
  59. *
  60. * Synopsis: list::shift()
  61. *
  62. * Synopsis: list::contains(value)
  63. * Variables:
  64. * (empty) - "true" if list contains value, "false" if not
  65. *
  66. * Synopsis:
  67. * list::find(start_pos, value)
  68. * Description:
  69. * finds the first occurrence of 'value' in the list at position >='start_pos'.
  70. * Variables:
  71. * pos - position of element, or "none" if not found
  72. * found - "true" if found, "false" if not
  73. *
  74. * Sysnopsis:
  75. * list::remove_at(remove_pos)
  76. * Description:
  77. * Removes the element at position 'remove_pos', which must refer to an existing element.
  78. *
  79. * Synopsis:
  80. * list::remove(value)
  81. * Description:
  82. * Removes the first occurrence of value in the list, which must be in the list.
  83. *
  84. * Synopsis:
  85. * list::set(list l1, ..., list lN)
  86. * Description:
  87. * Replaces the list with the concatenation of given lists.
  88. */
  89. #include <stdlib.h>
  90. #include <string.h>
  91. #include <stdio.h>
  92. #include <inttypes.h>
  93. #include <misc/parse_number.h>
  94. #include <ncd/NCDModule.h>
  95. #include <generated/blog_channel_ncd_list.h>
  96. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  97. struct instance {
  98. NCDModuleInst *i;
  99. NCDValue list;
  100. };
  101. struct length_instance {
  102. NCDModuleInst *i;
  103. size_t length;
  104. };
  105. struct get_instance {
  106. NCDModuleInst *i;
  107. NCDValue value;
  108. };
  109. struct contains_instance {
  110. NCDModuleInst *i;
  111. int contains;
  112. };
  113. struct find_instance {
  114. NCDModuleInst *i;
  115. int is_found;
  116. size_t found_pos;
  117. };
  118. static int append_list_args (NCDModuleInst *i, NCDValue *list, NCDValue *args)
  119. {
  120. ASSERT(NCDValue_Type(list) == NCDVALUE_LIST)
  121. ASSERT(NCDValue_Type(args) == NCDVALUE_LIST)
  122. // append contents of list arguments
  123. for (NCDValue *arg = NCDValue_ListFirst(args); arg; arg = NCDValue_ListNext(args, arg)) {
  124. // check type
  125. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  126. ModuleLog(i, BLOG_ERROR, "wrong type");
  127. return 0;
  128. }
  129. // copy list
  130. NCDValue copy;
  131. if (!NCDValue_InitCopy(&copy, arg)) {
  132. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  133. return 0;
  134. }
  135. // append
  136. if (!NCDValue_ListAppendList(list, copy)) {
  137. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  138. NCDValue_Free(&copy);
  139. return 0;
  140. }
  141. }
  142. return 1;
  143. }
  144. static NCDValue * find_in_list (NCDValue *list, NCDValue *val)
  145. {
  146. ASSERT(NCDValue_Type(list) == NCDVALUE_LIST)
  147. for (NCDValue *e = NCDValue_ListFirst(list); e; e = NCDValue_ListNext(list, e)) {
  148. if (NCDValue_Compare(e, val) == 0) {
  149. return e;
  150. }
  151. }
  152. return NULL;
  153. }
  154. static void func_new_list (NCDModuleInst *i)
  155. {
  156. // allocate instance
  157. struct instance *o = malloc(sizeof(*o));
  158. if (!o) {
  159. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  160. goto fail0;
  161. }
  162. NCDModuleInst_Backend_SetUser(i, o);
  163. // init arguments
  164. o->i = i;
  165. // copy list
  166. if (!NCDValue_InitCopy(&o->list, i->args)) {
  167. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  168. goto fail1;
  169. }
  170. // signal up
  171. NCDModuleInst_Backend_Up(o->i);
  172. return;
  173. fail1:
  174. free(o);
  175. fail0:
  176. NCDModuleInst_Backend_SetError(i);
  177. NCDModuleInst_Backend_Dead(i);
  178. }
  179. static void func_new_listfrom (NCDModuleInst *i)
  180. {
  181. // allocate instance
  182. struct instance *o = malloc(sizeof(*o));
  183. if (!o) {
  184. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  185. goto fail0;
  186. }
  187. NCDModuleInst_Backend_SetUser(i, o);
  188. // init arguments
  189. o->i = i;
  190. // init list
  191. NCDValue_InitList(&o->list);
  192. // append contents of list arguments
  193. if (!append_list_args(i, &o->list, i->args)) {
  194. goto fail2;
  195. }
  196. // signal up
  197. NCDModuleInst_Backend_Up(o->i);
  198. return;
  199. fail2:
  200. NCDValue_Free(&o->list);
  201. fail1:
  202. free(o);
  203. fail0:
  204. NCDModuleInst_Backend_SetError(i);
  205. NCDModuleInst_Backend_Dead(i);
  206. }
  207. static void func_die (void *vo)
  208. {
  209. struct instance *o = vo;
  210. NCDModuleInst *i = o->i;
  211. // free list
  212. NCDValue_Free(&o->list);
  213. // free instance
  214. free(o);
  215. NCDModuleInst_Backend_Dead(i);
  216. }
  217. static int func_getvar (void *vo, const char *name, NCDValue *out)
  218. {
  219. struct instance *o = vo;
  220. if (!strcmp(name, "")) {
  221. if (!NCDValue_InitCopy(out, &o->list)) {
  222. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  223. return 0;
  224. }
  225. return 1;
  226. }
  227. if (!strcmp(name, "length")) {
  228. char str[64];
  229. snprintf(str, sizeof(str), "%zu", NCDValue_ListCount(&o->list));
  230. if (!NCDValue_InitString(out, str)) {
  231. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  232. return 0;
  233. }
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. static void append_func_new (NCDModuleInst *i)
  239. {
  240. // check arguments
  241. NCDValue *arg;
  242. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  243. ModuleLog(i, BLOG_ERROR, "wrong arity");
  244. goto fail0;
  245. }
  246. // get method object
  247. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  248. // append
  249. NCDValue v;
  250. if (!NCDValue_InitCopy(&v, arg)) {
  251. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  252. goto fail0;
  253. }
  254. if (!NCDValue_ListAppend(&mo->list, v)) {
  255. NCDValue_Free(&v);
  256. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppend failed");
  257. goto fail0;
  258. }
  259. // signal up
  260. NCDModuleInst_Backend_Up(i);
  261. return;
  262. fail0:
  263. NCDModuleInst_Backend_SetError(i);
  264. NCDModuleInst_Backend_Dead(i);
  265. }
  266. static void appendv_func_new (NCDModuleInst *i)
  267. {
  268. // check arguments
  269. NCDValue *arg;
  270. if (!NCDValue_ListRead(i->args, 1, &arg)) {
  271. ModuleLog(i, BLOG_ERROR, "wrong arity");
  272. goto fail0;
  273. }
  274. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  275. ModuleLog(i, BLOG_ERROR, "wrong type");
  276. goto fail0;
  277. }
  278. // get method object
  279. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  280. // append
  281. NCDValue l;
  282. if (!NCDValue_InitCopy(&l, arg)) {
  283. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  284. goto fail0;
  285. }
  286. if (!NCDValue_ListAppendList(&mo->list, l)) {
  287. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  288. NCDValue_Free(&l);
  289. goto fail0;
  290. }
  291. // signal up
  292. NCDModuleInst_Backend_Up(i);
  293. return;
  294. fail0:
  295. NCDModuleInst_Backend_SetError(i);
  296. NCDModuleInst_Backend_Dead(i);
  297. }
  298. static void length_func_new (NCDModuleInst *i)
  299. {
  300. // allocate instance
  301. struct length_instance *o = malloc(sizeof(*o));
  302. if (!o) {
  303. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  304. goto fail0;
  305. }
  306. NCDModuleInst_Backend_SetUser(i, o);
  307. // init arguments
  308. o->i = i;
  309. // check arguments
  310. if (!NCDValue_ListRead(o->i->args, 0)) {
  311. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  312. goto fail1;
  313. }
  314. // get method object
  315. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  316. // remember length
  317. o->length = NCDValue_ListCount(&mo->list);
  318. // signal up
  319. NCDModuleInst_Backend_Up(o->i);
  320. return;
  321. fail1:
  322. free(o);
  323. fail0:
  324. NCDModuleInst_Backend_SetError(i);
  325. NCDModuleInst_Backend_Dead(i);
  326. }
  327. static void length_func_die (void *vo)
  328. {
  329. struct length_instance *o = vo;
  330. NCDModuleInst *i = o->i;
  331. // free instance
  332. free(o);
  333. NCDModuleInst_Backend_Dead(i);
  334. }
  335. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  336. {
  337. struct length_instance *o = vo;
  338. if (!strcmp(name, "")) {
  339. char str[64];
  340. snprintf(str, sizeof(str), "%zu", o->length);
  341. if (!NCDValue_InitString(out, str)) {
  342. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. return 0;
  348. }
  349. static void get_func_new (NCDModuleInst *i)
  350. {
  351. // allocate instance
  352. struct get_instance *o = malloc(sizeof(*o));
  353. if (!o) {
  354. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  355. goto fail0;
  356. }
  357. NCDModuleInst_Backend_SetUser(i, o);
  358. // init arguments
  359. o->i = i;
  360. // check arguments
  361. NCDValue *index_arg;
  362. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  363. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  364. goto fail1;
  365. }
  366. if (!NCDValue_IsStringNoNulls(index_arg)) {
  367. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  368. goto fail1;
  369. }
  370. uintmax_t index;
  371. if (!parse_unsigned_integer(NCDValue_StringValue(index_arg), &index)) {
  372. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  373. goto fail1;
  374. }
  375. // get method object
  376. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  377. // check index
  378. if (index >= NCDValue_ListCount(&mo->list)) {
  379. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  380. goto fail1;
  381. }
  382. // copy value
  383. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  384. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  385. goto fail1;
  386. }
  387. // signal up
  388. NCDModuleInst_Backend_Up(o->i);
  389. return;
  390. fail1:
  391. free(o);
  392. fail0:
  393. NCDModuleInst_Backend_SetError(i);
  394. NCDModuleInst_Backend_Dead(i);
  395. }
  396. static void get_func_die (void *vo)
  397. {
  398. struct get_instance *o = vo;
  399. NCDModuleInst *i = o->i;
  400. // free value
  401. NCDValue_Free(&o->value);
  402. // free instance
  403. free(o);
  404. NCDModuleInst_Backend_Dead(i);
  405. }
  406. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  407. {
  408. struct get_instance *o = vo;
  409. if (!strcmp(name, "")) {
  410. if (!NCDValue_InitCopy(out, &o->value)) {
  411. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  412. return 0;
  413. }
  414. return 1;
  415. }
  416. return 0;
  417. }
  418. static void shift_func_new (NCDModuleInst *i)
  419. {
  420. // check arguments
  421. if (!NCDValue_ListRead(i->args, 0)) {
  422. ModuleLog(i, BLOG_ERROR, "wrong arity");
  423. goto fail0;
  424. }
  425. // get method object
  426. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  427. // shift
  428. if (!NCDValue_ListFirst(&mo->list)) {
  429. ModuleLog(i, BLOG_ERROR, "list has no elements");
  430. goto fail0;
  431. }
  432. NCDValue v = NCDValue_ListShift(&mo->list);
  433. NCDValue_Free(&v);
  434. // signal up
  435. NCDModuleInst_Backend_Up(i);
  436. return;
  437. fail0:
  438. NCDModuleInst_Backend_SetError(i);
  439. NCDModuleInst_Backend_Dead(i);
  440. }
  441. static void contains_func_new (NCDModuleInst *i)
  442. {
  443. // allocate instance
  444. struct contains_instance *o = malloc(sizeof(*o));
  445. if (!o) {
  446. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  447. goto fail0;
  448. }
  449. NCDModuleInst_Backend_SetUser(i, o);
  450. // init arguments
  451. o->i = i;
  452. // read arguments
  453. NCDValue *value_arg;
  454. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  455. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  456. goto fail1;
  457. }
  458. // get method object
  459. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  460. // search
  461. o->contains = 0;
  462. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  463. if (NCDValue_Compare(v, value_arg) == 0) {
  464. o->contains = 1;
  465. break;
  466. }
  467. }
  468. // signal up
  469. NCDModuleInst_Backend_Up(o->i);
  470. return;
  471. fail1:
  472. free(o);
  473. fail0:
  474. NCDModuleInst_Backend_SetError(i);
  475. NCDModuleInst_Backend_Dead(i);
  476. }
  477. static void contains_func_die (void *vo)
  478. {
  479. struct contains_instance *o = vo;
  480. NCDModuleInst *i = o->i;
  481. // free instance
  482. free(o);
  483. NCDModuleInst_Backend_Dead(i);
  484. }
  485. static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
  486. {
  487. struct contains_instance *o = vo;
  488. if (!strcmp(name, "")) {
  489. const char *value = (o->contains ? "true" : "false");
  490. if (!NCDValue_InitString(out, value)) {
  491. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  492. return 0;
  493. }
  494. return 1;
  495. }
  496. return 0;
  497. }
  498. static void find_func_new (NCDModuleInst *i)
  499. {
  500. // allocate instance
  501. struct find_instance *o = malloc(sizeof(*o));
  502. if (!o) {
  503. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  504. goto fail0;
  505. }
  506. NCDModuleInst_Backend_SetUser(i, o);
  507. // init arguments
  508. o->i = i;
  509. // read arguments
  510. NCDValue *start_pos_arg;
  511. NCDValue *value_arg;
  512. if (!NCDValue_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
  513. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  514. goto fail1;
  515. }
  516. if (!NCDValue_IsStringNoNulls(start_pos_arg)) {
  517. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  518. goto fail1;
  519. }
  520. // read start position
  521. uintmax_t start_pos;
  522. if (!parse_unsigned_integer(NCDValue_StringValue(start_pos_arg), &start_pos)) {
  523. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  524. goto fail1;
  525. }
  526. // get method object
  527. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  528. // search
  529. o->is_found = 0;
  530. size_t pos = 0;
  531. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  532. if (pos >= start_pos && NCDValue_Compare(v, value_arg) == 0) {
  533. o->is_found = 1;
  534. o->found_pos = pos;
  535. break;
  536. }
  537. pos++;
  538. }
  539. // signal up
  540. NCDModuleInst_Backend_Up(o->i);
  541. return;
  542. fail1:
  543. free(o);
  544. fail0:
  545. NCDModuleInst_Backend_SetError(i);
  546. NCDModuleInst_Backend_Dead(i);
  547. }
  548. static void find_func_die (void *vo)
  549. {
  550. struct find_instance *o = vo;
  551. NCDModuleInst *i = o->i;
  552. // free instance
  553. free(o);
  554. NCDModuleInst_Backend_Dead(i);
  555. }
  556. static int find_func_getvar (void *vo, const char *name, NCDValue *out)
  557. {
  558. struct find_instance *o = vo;
  559. if (!strcmp(name, "pos")) {
  560. char value[64];
  561. if (o->is_found) {
  562. snprintf(value, sizeof(value), "%zu", o->found_pos);
  563. } else {
  564. snprintf(value, sizeof(value), "none");
  565. }
  566. if (!NCDValue_InitString(out, value)) {
  567. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  568. return 0;
  569. }
  570. return 1;
  571. }
  572. if (!strcmp(name, "found")) {
  573. const char *value = (o->is_found ? "true" : "false");
  574. if (!NCDValue_InitString(out, value)) {
  575. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  576. return 0;
  577. }
  578. return 1;
  579. }
  580. return 0;
  581. }
  582. static void removeat_func_new (NCDModuleInst *i)
  583. {
  584. // read arguments
  585. NCDValue *remove_pos_arg;
  586. if (!NCDValue_ListRead(i->args, 1, &remove_pos_arg)) {
  587. ModuleLog(i, BLOG_ERROR, "wrong arity");
  588. goto fail0;
  589. }
  590. if (!NCDValue_IsStringNoNulls(remove_pos_arg)) {
  591. ModuleLog(i, BLOG_ERROR, "wrong type");
  592. goto fail0;
  593. }
  594. // read position
  595. uintmax_t remove_pos;
  596. if (!parse_unsigned_integer(NCDValue_StringValue(remove_pos_arg), &remove_pos)) {
  597. ModuleLog(i, BLOG_ERROR, "wrong pos");
  598. goto fail0;
  599. }
  600. // get method object
  601. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  602. // check position
  603. if (remove_pos >= NCDValue_ListCount(&mo->list)) {
  604. ModuleLog(i, BLOG_ERROR, "pos out of range");
  605. goto fail0;
  606. }
  607. // find and remove
  608. size_t pos = 0;
  609. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  610. if (pos == remove_pos) {
  611. NCDValue removed_v = NCDValue_ListRemove(&mo->list, v);
  612. NCDValue_Free(&removed_v);
  613. break;
  614. }
  615. pos++;
  616. }
  617. // signal up
  618. NCDModuleInst_Backend_Up(i);
  619. return;
  620. fail0:
  621. NCDModuleInst_Backend_SetError(i);
  622. NCDModuleInst_Backend_Dead(i);
  623. }
  624. static void remove_func_new (NCDModuleInst *i)
  625. {
  626. // read arguments
  627. NCDValue *value_arg;
  628. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  629. ModuleLog(i, BLOG_ERROR, "wrong arity");
  630. goto fail0;
  631. }
  632. // get method object
  633. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  634. // find value
  635. NCDValue *e = find_in_list(&mo->list, value_arg);
  636. if (!e) {
  637. ModuleLog(i, BLOG_ERROR, "value does not exist");
  638. goto fail0;
  639. }
  640. // remove it
  641. NCDValue removed_v = NCDValue_ListRemove(&mo->list, e);
  642. NCDValue_Free(&removed_v);
  643. // signal up
  644. NCDModuleInst_Backend_Up(i);
  645. return;
  646. fail0:
  647. NCDModuleInst_Backend_SetError(i);
  648. NCDModuleInst_Backend_Dead(i);
  649. }
  650. static void set_func_new (NCDModuleInst *i)
  651. {
  652. // init list
  653. NCDValue list;
  654. NCDValue_InitList(&list);
  655. // append to list
  656. if (!append_list_args(i, &list, i->args)) {
  657. goto fail1;
  658. }
  659. // get method object
  660. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
  661. // replace list
  662. NCDValue_Free(&mo->list);
  663. mo->list = list;
  664. // signal up
  665. NCDModuleInst_Backend_Up(i);
  666. return;
  667. fail1:
  668. NCDValue_Free(&list);
  669. fail0:
  670. NCDModuleInst_Backend_SetError(i);
  671. NCDModuleInst_Backend_Dead(i);
  672. }
  673. static const struct NCDModule modules[] = {
  674. {
  675. .type = "list",
  676. .func_new = func_new_list,
  677. .func_die = func_die,
  678. .func_getvar = func_getvar
  679. }, {
  680. .type = "listfrom",
  681. .base_type = "list",
  682. .func_new = func_new_listfrom,
  683. .func_die = func_die,
  684. .func_getvar = func_getvar
  685. }, {
  686. .type = "concatlist", // alias for listfrom
  687. .base_type = "list",
  688. .func_new = func_new_listfrom,
  689. .func_die = func_die,
  690. .func_getvar = func_getvar
  691. }, {
  692. .type = "list::append",
  693. .func_new = append_func_new
  694. }, {
  695. .type = "list::appendv",
  696. .func_new = appendv_func_new
  697. }, {
  698. .type = "list::length",
  699. .func_new = length_func_new,
  700. .func_die = length_func_die,
  701. .func_getvar = length_func_getvar
  702. }, {
  703. .type = "list::get",
  704. .func_new = get_func_new,
  705. .func_die = get_func_die,
  706. .func_getvar = get_func_getvar
  707. }, {
  708. .type = "list::shift",
  709. .func_new = shift_func_new
  710. }, {
  711. .type = "list::contains",
  712. .func_new = contains_func_new,
  713. .func_die = contains_func_die,
  714. .func_getvar = contains_func_getvar
  715. }, {
  716. .type = "list::find",
  717. .func_new = find_func_new,
  718. .func_die = find_func_die,
  719. .func_getvar = find_func_getvar
  720. }, {
  721. .type = "list::remove_at",
  722. .func_new = removeat_func_new
  723. }, {
  724. .type = "list::remove",
  725. .func_new = remove_func_new
  726. }, {
  727. .type = "list::set",
  728. .func_new = set_func_new
  729. }, {
  730. .type = NULL
  731. }
  732. };
  733. const struct NCDModuleGroup ncdmodule_list = {
  734. .modules = modules
  735. };