list.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  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 append_instance {
  102. NCDModuleInst *i;
  103. };
  104. struct appendv_instance {
  105. NCDModuleInst *i;
  106. };
  107. struct length_instance {
  108. NCDModuleInst *i;
  109. size_t length;
  110. };
  111. struct get_instance {
  112. NCDModuleInst *i;
  113. NCDValue value;
  114. };
  115. struct shift_instance {
  116. NCDModuleInst *i;
  117. };
  118. struct contains_instance {
  119. NCDModuleInst *i;
  120. int contains;
  121. };
  122. struct find_instance {
  123. NCDModuleInst *i;
  124. int is_found;
  125. size_t found_pos;
  126. };
  127. struct removeat_instance {
  128. NCDModuleInst *i;
  129. };
  130. struct remove_instance {
  131. NCDModuleInst *i;
  132. };
  133. struct set_instance {
  134. NCDModuleInst *i;
  135. };
  136. static int append_list_args (NCDModuleInst *i, NCDValue *list, NCDValue *args)
  137. {
  138. ASSERT(NCDValue_Type(list) == NCDVALUE_LIST)
  139. ASSERT(NCDValue_Type(args) == NCDVALUE_LIST)
  140. // append contents of list arguments
  141. for (NCDValue *arg = NCDValue_ListFirst(args); arg; arg = NCDValue_ListNext(args, arg)) {
  142. // check type
  143. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  144. ModuleLog(i, BLOG_ERROR, "wrong type");
  145. return 0;
  146. }
  147. // copy list
  148. NCDValue copy;
  149. if (!NCDValue_InitCopy(&copy, arg)) {
  150. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  151. return 0;
  152. }
  153. // append
  154. if (!NCDValue_ListAppendList(list, copy)) {
  155. ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  156. NCDValue_Free(&copy);
  157. return 0;
  158. }
  159. }
  160. return 1;
  161. }
  162. static NCDValue * find_in_list (NCDValue *list, NCDValue *val)
  163. {
  164. ASSERT(NCDValue_Type(list) == NCDVALUE_LIST)
  165. for (NCDValue *e = NCDValue_ListFirst(list); e; e = NCDValue_ListNext(list, e)) {
  166. if (NCDValue_Compare(e, val) == 0) {
  167. return e;
  168. }
  169. }
  170. return NULL;
  171. }
  172. static void func_new_list (NCDModuleInst *i)
  173. {
  174. // allocate instance
  175. struct 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. // copy list
  184. if (!NCDValue_InitCopy(&o->list, i->args)) {
  185. ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
  186. goto fail1;
  187. }
  188. // signal up
  189. NCDModuleInst_Backend_Up(o->i);
  190. return;
  191. fail1:
  192. free(o);
  193. fail0:
  194. NCDModuleInst_Backend_SetError(i);
  195. NCDModuleInst_Backend_Dead(i);
  196. }
  197. static void func_new_listfrom (NCDModuleInst *i)
  198. {
  199. // allocate instance
  200. struct instance *o = malloc(sizeof(*o));
  201. if (!o) {
  202. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  203. goto fail0;
  204. }
  205. NCDModuleInst_Backend_SetUser(i, o);
  206. // init arguments
  207. o->i = i;
  208. // init list
  209. NCDValue_InitList(&o->list);
  210. // append contents of list arguments
  211. if (!append_list_args(i, &o->list, i->args)) {
  212. goto fail2;
  213. }
  214. // signal up
  215. NCDModuleInst_Backend_Up(o->i);
  216. return;
  217. fail2:
  218. NCDValue_Free(&o->list);
  219. fail1:
  220. free(o);
  221. fail0:
  222. NCDModuleInst_Backend_SetError(i);
  223. NCDModuleInst_Backend_Dead(i);
  224. }
  225. static void func_die (void *vo)
  226. {
  227. struct instance *o = vo;
  228. NCDModuleInst *i = o->i;
  229. // free list
  230. NCDValue_Free(&o->list);
  231. // free instance
  232. free(o);
  233. NCDModuleInst_Backend_Dead(i);
  234. }
  235. static int func_getvar (void *vo, const char *name, NCDValue *out)
  236. {
  237. struct instance *o = vo;
  238. if (!strcmp(name, "")) {
  239. if (!NCDValue_InitCopy(out, &o->list)) {
  240. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  241. return 0;
  242. }
  243. return 1;
  244. }
  245. if (!strcmp(name, "length")) {
  246. char str[64];
  247. snprintf(str, sizeof(str), "%zu", NCDValue_ListCount(&o->list));
  248. if (!NCDValue_InitString(out, str)) {
  249. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  250. return 0;
  251. }
  252. return 1;
  253. }
  254. return 0;
  255. }
  256. static void append_func_new (NCDModuleInst *i)
  257. {
  258. // allocate instance
  259. struct append_instance *o = malloc(sizeof(*o));
  260. if (!o) {
  261. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  262. goto fail0;
  263. }
  264. NCDModuleInst_Backend_SetUser(i, o);
  265. // init arguments
  266. o->i = i;
  267. // check arguments
  268. NCDValue *arg;
  269. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  270. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  271. goto fail1;
  272. }
  273. // get method object
  274. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  275. // append
  276. NCDValue v;
  277. if (!NCDValue_InitCopy(&v, arg)) {
  278. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  279. goto fail1;
  280. }
  281. if (!NCDValue_ListAppend(&mo->list, v)) {
  282. NCDValue_Free(&v);
  283. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  284. goto fail1;
  285. }
  286. // signal up
  287. NCDModuleInst_Backend_Up(o->i);
  288. return;
  289. fail1:
  290. free(o);
  291. fail0:
  292. NCDModuleInst_Backend_SetError(i);
  293. NCDModuleInst_Backend_Dead(i);
  294. }
  295. static void append_func_die (void *vo)
  296. {
  297. struct append_instance *o = vo;
  298. NCDModuleInst *i = o->i;
  299. // free instance
  300. free(o);
  301. NCDModuleInst_Backend_Dead(i);
  302. }
  303. static void appendv_func_new (NCDModuleInst *i)
  304. {
  305. // allocate instance
  306. struct appendv_instance *o = malloc(sizeof(*o));
  307. if (!o) {
  308. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  309. goto fail0;
  310. }
  311. NCDModuleInst_Backend_SetUser(i, o);
  312. // init arguments
  313. o->i = i;
  314. // check arguments
  315. NCDValue *arg;
  316. if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
  317. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  318. goto fail1;
  319. }
  320. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  321. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  322. goto fail1;
  323. }
  324. // get method object
  325. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  326. // append
  327. NCDValue l;
  328. if (!NCDValue_InitCopy(&l, arg)) {
  329. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  330. goto fail1;
  331. }
  332. if (!NCDValue_ListAppendList(&mo->list, l)) {
  333. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
  334. NCDValue_Free(&l);
  335. goto fail1;
  336. }
  337. // signal up
  338. NCDModuleInst_Backend_Up(o->i);
  339. return;
  340. fail1:
  341. free(o);
  342. fail0:
  343. NCDModuleInst_Backend_SetError(i);
  344. NCDModuleInst_Backend_Dead(i);
  345. }
  346. static void appendv_func_die (void *vo)
  347. {
  348. struct appendv_instance *o = vo;
  349. NCDModuleInst *i = o->i;
  350. // free instance
  351. free(o);
  352. NCDModuleInst_Backend_Dead(i);
  353. }
  354. static void length_func_new (NCDModuleInst *i)
  355. {
  356. // allocate instance
  357. struct length_instance *o = malloc(sizeof(*o));
  358. if (!o) {
  359. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  360. goto fail0;
  361. }
  362. NCDModuleInst_Backend_SetUser(i, o);
  363. // init arguments
  364. o->i = i;
  365. // check arguments
  366. if (!NCDValue_ListRead(o->i->args, 0)) {
  367. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  368. goto fail1;
  369. }
  370. // get method object
  371. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  372. // remember length
  373. o->length = NCDValue_ListCount(&mo->list);
  374. // signal up
  375. NCDModuleInst_Backend_Up(o->i);
  376. return;
  377. fail1:
  378. free(o);
  379. fail0:
  380. NCDModuleInst_Backend_SetError(i);
  381. NCDModuleInst_Backend_Dead(i);
  382. }
  383. static void length_func_die (void *vo)
  384. {
  385. struct length_instance *o = vo;
  386. NCDModuleInst *i = o->i;
  387. // free instance
  388. free(o);
  389. NCDModuleInst_Backend_Dead(i);
  390. }
  391. static int length_func_getvar (void *vo, const char *name, NCDValue *out)
  392. {
  393. struct length_instance *o = vo;
  394. if (!strcmp(name, "")) {
  395. char str[64];
  396. snprintf(str, sizeof(str), "%zu", o->length);
  397. if (!NCDValue_InitString(out, str)) {
  398. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  399. return 0;
  400. }
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. static void get_func_new (NCDModuleInst *i)
  406. {
  407. // allocate instance
  408. struct get_instance *o = malloc(sizeof(*o));
  409. if (!o) {
  410. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  411. goto fail0;
  412. }
  413. NCDModuleInst_Backend_SetUser(i, o);
  414. // init arguments
  415. o->i = i;
  416. // check arguments
  417. NCDValue *index_arg;
  418. if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
  419. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  420. goto fail1;
  421. }
  422. if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
  423. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  424. goto fail1;
  425. }
  426. uintmax_t index;
  427. if (!parse_unsigned_integer(NCDValue_StringValue(index_arg), &index)) {
  428. ModuleLog(o->i, BLOG_ERROR, "wrong value");
  429. goto fail1;
  430. }
  431. // get method object
  432. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  433. // check index
  434. if (index >= NCDValue_ListCount(&mo->list)) {
  435. ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
  436. goto fail1;
  437. }
  438. // copy value
  439. if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
  440. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  441. goto fail1;
  442. }
  443. // signal up
  444. NCDModuleInst_Backend_Up(o->i);
  445. return;
  446. fail1:
  447. free(o);
  448. fail0:
  449. NCDModuleInst_Backend_SetError(i);
  450. NCDModuleInst_Backend_Dead(i);
  451. }
  452. static void get_func_die (void *vo)
  453. {
  454. struct get_instance *o = vo;
  455. NCDModuleInst *i = o->i;
  456. // free value
  457. NCDValue_Free(&o->value);
  458. // free instance
  459. free(o);
  460. NCDModuleInst_Backend_Dead(i);
  461. }
  462. static int get_func_getvar (void *vo, const char *name, NCDValue *out)
  463. {
  464. struct get_instance *o = vo;
  465. if (!strcmp(name, "")) {
  466. if (!NCDValue_InitCopy(out, &o->value)) {
  467. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  468. return 0;
  469. }
  470. return 1;
  471. }
  472. return 0;
  473. }
  474. static void shift_func_new (NCDModuleInst *i)
  475. {
  476. // allocate instance
  477. struct shift_instance *o = malloc(sizeof(*o));
  478. if (!o) {
  479. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  480. goto fail0;
  481. }
  482. NCDModuleInst_Backend_SetUser(i, o);
  483. // init arguments
  484. o->i = i;
  485. // check arguments
  486. if (!NCDValue_ListRead(o->i->args, 0)) {
  487. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  488. goto fail1;
  489. }
  490. // get method object
  491. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  492. // shift
  493. if (!NCDValue_ListFirst(&mo->list)) {
  494. ModuleLog(o->i, BLOG_ERROR, "list has no elements");
  495. goto fail1;
  496. }
  497. NCDValue v = NCDValue_ListShift(&mo->list);
  498. NCDValue_Free(&v);
  499. // signal up
  500. NCDModuleInst_Backend_Up(o->i);
  501. return;
  502. fail1:
  503. free(o);
  504. fail0:
  505. NCDModuleInst_Backend_SetError(i);
  506. NCDModuleInst_Backend_Dead(i);
  507. }
  508. static void shift_func_die (void *vo)
  509. {
  510. struct shift_instance *o = vo;
  511. NCDModuleInst *i = o->i;
  512. // free instance
  513. free(o);
  514. NCDModuleInst_Backend_Dead(i);
  515. }
  516. static void contains_func_new (NCDModuleInst *i)
  517. {
  518. // allocate instance
  519. struct contains_instance *o = malloc(sizeof(*o));
  520. if (!o) {
  521. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  522. goto fail0;
  523. }
  524. NCDModuleInst_Backend_SetUser(i, o);
  525. // init arguments
  526. o->i = i;
  527. // read arguments
  528. NCDValue *value_arg;
  529. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  530. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  531. goto fail1;
  532. }
  533. // get method object
  534. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  535. // search
  536. o->contains = 0;
  537. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  538. if (NCDValue_Compare(v, value_arg) == 0) {
  539. o->contains = 1;
  540. break;
  541. }
  542. }
  543. // signal up
  544. NCDModuleInst_Backend_Up(o->i);
  545. return;
  546. fail1:
  547. free(o);
  548. fail0:
  549. NCDModuleInst_Backend_SetError(i);
  550. NCDModuleInst_Backend_Dead(i);
  551. }
  552. static void contains_func_die (void *vo)
  553. {
  554. struct contains_instance *o = vo;
  555. NCDModuleInst *i = o->i;
  556. // free instance
  557. free(o);
  558. NCDModuleInst_Backend_Dead(i);
  559. }
  560. static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
  561. {
  562. struct contains_instance *o = vo;
  563. if (!strcmp(name, "")) {
  564. const char *value = (o->contains ? "true" : "false");
  565. if (!NCDValue_InitString(out, value)) {
  566. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  567. return 0;
  568. }
  569. return 1;
  570. }
  571. return 0;
  572. }
  573. static void find_func_new (NCDModuleInst *i)
  574. {
  575. // allocate instance
  576. struct find_instance *o = malloc(sizeof(*o));
  577. if (!o) {
  578. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  579. goto fail0;
  580. }
  581. NCDModuleInst_Backend_SetUser(i, o);
  582. // init arguments
  583. o->i = i;
  584. // read arguments
  585. NCDValue *start_pos_arg;
  586. NCDValue *value_arg;
  587. if (!NCDValue_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
  588. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  589. goto fail1;
  590. }
  591. if (NCDValue_Type(start_pos_arg) != NCDVALUE_STRING) {
  592. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  593. goto fail1;
  594. }
  595. // read start position
  596. uintmax_t start_pos;
  597. if (!parse_unsigned_integer(NCDValue_StringValue(start_pos_arg), &start_pos)) {
  598. ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
  599. goto fail1;
  600. }
  601. // get method object
  602. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  603. // search
  604. o->is_found = 0;
  605. size_t pos = 0;
  606. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  607. if (pos >= start_pos && NCDValue_Compare(v, value_arg) == 0) {
  608. o->is_found = 1;
  609. o->found_pos = pos;
  610. break;
  611. }
  612. pos++;
  613. }
  614. // signal up
  615. NCDModuleInst_Backend_Up(o->i);
  616. return;
  617. fail1:
  618. free(o);
  619. fail0:
  620. NCDModuleInst_Backend_SetError(i);
  621. NCDModuleInst_Backend_Dead(i);
  622. }
  623. static void find_func_die (void *vo)
  624. {
  625. struct find_instance *o = vo;
  626. NCDModuleInst *i = o->i;
  627. // free instance
  628. free(o);
  629. NCDModuleInst_Backend_Dead(i);
  630. }
  631. static int find_func_getvar (void *vo, const char *name, NCDValue *out)
  632. {
  633. struct find_instance *o = vo;
  634. if (!strcmp(name, "pos")) {
  635. char value[64];
  636. if (o->is_found) {
  637. snprintf(value, sizeof(value), "%zu", o->found_pos);
  638. } else {
  639. snprintf(value, sizeof(value), "none");
  640. }
  641. if (!NCDValue_InitString(out, value)) {
  642. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  643. return 0;
  644. }
  645. return 1;
  646. }
  647. if (!strcmp(name, "found")) {
  648. const char *value = (o->is_found ? "true" : "false");
  649. if (!NCDValue_InitString(out, value)) {
  650. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  651. return 0;
  652. }
  653. return 1;
  654. }
  655. return 0;
  656. }
  657. static void removeat_func_new (NCDModuleInst *i)
  658. {
  659. // allocate instance
  660. struct removeat_instance *o = malloc(sizeof(*o));
  661. if (!o) {
  662. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  663. goto fail0;
  664. }
  665. NCDModuleInst_Backend_SetUser(i, o);
  666. // init arguments
  667. o->i = i;
  668. // read arguments
  669. NCDValue *remove_pos_arg;
  670. if (!NCDValue_ListRead(i->args, 1, &remove_pos_arg)) {
  671. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  672. goto fail1;
  673. }
  674. if (NCDValue_Type(remove_pos_arg) != NCDVALUE_STRING) {
  675. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  676. goto fail1;
  677. }
  678. // read position
  679. uintmax_t remove_pos;
  680. if (!parse_unsigned_integer(NCDValue_StringValue(remove_pos_arg), &remove_pos)) {
  681. ModuleLog(o->i, BLOG_ERROR, "wrong pos");
  682. goto fail1;
  683. }
  684. // get method object
  685. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  686. // check position
  687. if (remove_pos >= NCDValue_ListCount(&mo->list)) {
  688. ModuleLog(o->i, BLOG_ERROR, "pos out of range");
  689. goto fail1;
  690. }
  691. // find and remove
  692. size_t pos = 0;
  693. for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
  694. if (pos == remove_pos) {
  695. NCDValue removed_v = NCDValue_ListRemove(&mo->list, v);
  696. NCDValue_Free(&removed_v);
  697. break;
  698. }
  699. pos++;
  700. }
  701. // signal up
  702. NCDModuleInst_Backend_Up(o->i);
  703. return;
  704. fail1:
  705. free(o);
  706. fail0:
  707. NCDModuleInst_Backend_SetError(i);
  708. NCDModuleInst_Backend_Dead(i);
  709. }
  710. static void removeat_func_die (void *vo)
  711. {
  712. struct removeat_instance *o = vo;
  713. NCDModuleInst *i = o->i;
  714. // free instance
  715. free(o);
  716. NCDModuleInst_Backend_Dead(i);
  717. }
  718. static void remove_func_new (NCDModuleInst *i)
  719. {
  720. // allocate instance
  721. struct remove_instance *o = malloc(sizeof(*o));
  722. if (!o) {
  723. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  724. goto fail0;
  725. }
  726. NCDModuleInst_Backend_SetUser(i, o);
  727. // init arguments
  728. o->i = i;
  729. // read arguments
  730. NCDValue *value_arg;
  731. if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
  732. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  733. goto fail1;
  734. }
  735. // get method object
  736. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  737. // find value
  738. NCDValue *e = find_in_list(&mo->list, value_arg);
  739. if (!e) {
  740. ModuleLog(o->i, BLOG_ERROR, "value does not exist");
  741. goto fail1;
  742. }
  743. // remove it
  744. NCDValue removed_v = NCDValue_ListRemove(&mo->list, e);
  745. NCDValue_Free(&removed_v);
  746. // signal up
  747. NCDModuleInst_Backend_Up(o->i);
  748. return;
  749. fail1:
  750. free(o);
  751. fail0:
  752. NCDModuleInst_Backend_SetError(i);
  753. NCDModuleInst_Backend_Dead(i);
  754. }
  755. static void remove_func_die (void *vo)
  756. {
  757. struct remove_instance *o = vo;
  758. NCDModuleInst *i = o->i;
  759. // free instance
  760. free(o);
  761. NCDModuleInst_Backend_Dead(i);
  762. }
  763. static void set_func_new (NCDModuleInst *i)
  764. {
  765. // allocate instance
  766. struct set_instance *o = malloc(sizeof(*o));
  767. if (!o) {
  768. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  769. goto fail0;
  770. }
  771. NCDModuleInst_Backend_SetUser(i, o);
  772. // init arguments
  773. o->i = i;
  774. // init list
  775. NCDValue list;
  776. NCDValue_InitList(&list);
  777. // append to list
  778. if (!append_list_args(i, &list, i->args)) {
  779. goto fail2;
  780. }
  781. // get method object
  782. struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
  783. // replace list
  784. NCDValue_Free(&mo->list);
  785. mo->list = list;
  786. // signal up
  787. NCDModuleInst_Backend_Up(o->i);
  788. return;
  789. fail2:
  790. NCDValue_Free(&list);
  791. free(o);
  792. fail0:
  793. NCDModuleInst_Backend_SetError(i);
  794. NCDModuleInst_Backend_Dead(i);
  795. }
  796. static void set_func_die (void *vo)
  797. {
  798. struct set_instance *o = vo;
  799. NCDModuleInst *i = o->i;
  800. // free instance
  801. free(o);
  802. NCDModuleInst_Backend_Dead(i);
  803. }
  804. static const struct NCDModule modules[] = {
  805. {
  806. .type = "list",
  807. .func_new = func_new_list,
  808. .func_die = func_die,
  809. .func_getvar = func_getvar
  810. }, {
  811. .type = "listfrom",
  812. .base_type = "list",
  813. .func_new = func_new_listfrom,
  814. .func_die = func_die,
  815. .func_getvar = func_getvar
  816. }, {
  817. .type = "concatlist", // alias for listfrom
  818. .base_type = "list",
  819. .func_new = func_new_listfrom,
  820. .func_die = func_die,
  821. .func_getvar = func_getvar
  822. }, {
  823. .type = "list::append",
  824. .func_new = append_func_new,
  825. .func_die = append_func_die
  826. }, {
  827. .type = "list::appendv",
  828. .func_new = appendv_func_new,
  829. .func_die = appendv_func_die
  830. }, {
  831. .type = "list::length",
  832. .func_new = length_func_new,
  833. .func_die = length_func_die,
  834. .func_getvar = length_func_getvar
  835. }, {
  836. .type = "list::get",
  837. .func_new = get_func_new,
  838. .func_die = get_func_die,
  839. .func_getvar = get_func_getvar
  840. }, {
  841. .type = "list::shift",
  842. .func_new = shift_func_new,
  843. .func_die = shift_func_die
  844. }, {
  845. .type = "list::contains",
  846. .func_new = contains_func_new,
  847. .func_die = contains_func_die,
  848. .func_getvar = contains_func_getvar
  849. }, {
  850. .type = "list::find",
  851. .func_new = find_func_new,
  852. .func_die = find_func_die,
  853. .func_getvar = find_func_getvar
  854. }, {
  855. .type = "list::remove_at",
  856. .func_new = removeat_func_new,
  857. .func_die = removeat_func_die
  858. }, {
  859. .type = "list::remove",
  860. .func_new = remove_func_new,
  861. .func_die = remove_func_die
  862. }, {
  863. .type = "list::set",
  864. .func_new = set_func_new,
  865. .func_die = set_func_die
  866. }, {
  867. .type = NULL
  868. }
  869. };
  870. const struct NCDModuleGroup ncdmodule_list = {
  871. .modules = modules
  872. };