| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801 |
- /**
- * @file list.c
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * @section DESCRIPTION
- *
- * List construction module.
- *
- * Synopsis:
- * list(elem1, ..., elemN)
- * list listfrom(list l1, ..., list lN)
- *
- * Description:
- * The first form creates a list with the given elements.
- * The second form creates a list by concatenating the given
- * lists.
- *
- * Variables:
- * (empty) - list containing elem1, ..., elemN
- * length - number of elements in list
- *
- * Synopsis: list::append(arg)
- *
- * Synopsis: list::appendv(list arg)
- * Description: Appends the elements of arg to the list.
- *
- * Synopsis: list::length()
- * Variables:
- * (empty) - number of elements in list at the time of initialization
- * of this method
- *
- * Synopsis: list::get(string index)
- * Variables:
- * (empty) - element of list at position index (starting from zero) at the time of initialization
- *
- * Synopsis: list::shift()
- *
- * Synopsis: list::contains(value)
- * Variables:
- * (empty) - "true" if list contains value, "false" if not
- *
- * Synopsis:
- * list::find(start_pos, value)
- * Description:
- * finds the first occurance of 'value' in the list at position >='start_pos'.
- * Variables:
- * pos - position of element, or "none" if not found
- * found - "true" if found, "false" if not
- */
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <inttypes.h>
- #include <misc/parse_number.h>
- #include <ncd/NCDModule.h>
- #include <generated/blog_channel_ncd_list.h>
- #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
- struct instance {
- NCDModuleInst *i;
- NCDValue list;
- };
- struct append_instance {
- NCDModuleInst *i;
- };
- struct appendv_instance {
- NCDModuleInst *i;
- };
- struct length_instance {
- NCDModuleInst *i;
- size_t length;
- };
- struct get_instance {
- NCDModuleInst *i;
- NCDValue value;
- };
- struct shift_instance {
- NCDModuleInst *i;
- };
- struct contains_instance {
- NCDModuleInst *i;
- int contains;
- };
- struct find_instance {
- NCDModuleInst *i;
- int is_found;
- size_t found_pos;
- };
- static void func_new_list (NCDModuleInst *i)
- {
- // allocate instance
- struct instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // copy list
- if (!NCDValue_InitCopy(&o->list, i->args)) {
- ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
- goto fail1;
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void func_new_listfrom (NCDModuleInst *i)
- {
- // allocate instance
- struct instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // init list
- NCDValue_InitList(&o->list);
-
- // append contents of list arguments
- for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
- // check type
- if (NCDValue_Type(arg) != NCDVALUE_LIST) {
- ModuleLog(i, BLOG_ERROR, "wrong type");
- goto fail2;
- }
-
- // copy list
- NCDValue copy;
- if (!NCDValue_InitCopy(©, arg)) {
- ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
- goto fail2;
- }
-
- // append
- if (!NCDValue_ListAppendList(&o->list, copy)) {
- ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
- NCDValue_Free(©);
- goto fail2;
- }
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- return;
-
- fail2:
- NCDValue_Free(&o->list);
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void func_die (void *vo)
- {
- struct instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free list
- NCDValue_Free(&o->list);
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static int func_getvar (void *vo, const char *name, NCDValue *out)
- {
- struct instance *o = vo;
-
- if (!strcmp(name, "")) {
- if (!NCDValue_InitCopy(out, &o->list)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
- return 0;
- }
-
- return 1;
- }
-
- if (!strcmp(name, "length")) {
- char str[64];
- snprintf(str, sizeof(str), "%zu", NCDValue_ListCount(&o->list));
-
- if (!NCDValue_InitString(out, str)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
-
- return 1;
- }
-
- return 0;
- }
- static void append_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct append_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // check arguments
- NCDValue *arg;
- if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // append
- NCDValue v;
- if (!NCDValue_InitCopy(&v, arg)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
- goto fail1;
- }
- if (!NCDValue_ListAppend(&mo->list, v)) {
- NCDValue_Free(&v);
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
- goto fail1;
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void append_func_die (void *vo)
- {
- struct append_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void appendv_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct appendv_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // check arguments
- NCDValue *arg;
- if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(arg) != NCDVALUE_LIST) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // append
- NCDValue l;
- if (!NCDValue_InitCopy(&l, arg)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
- goto fail1;
- }
- if (!NCDValue_ListAppendList(&mo->list, l)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
- NCDValue_Free(&l);
- goto fail1;
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void appendv_func_die (void *vo)
- {
- struct appendv_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void length_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct length_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // check arguments
- if (!NCDValue_ListRead(o->i->args, 0)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // remember length
- o->length = NCDValue_ListCount(&mo->list);
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void length_func_die (void *vo)
- {
- struct length_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static int length_func_getvar (void *vo, const char *name, NCDValue *out)
- {
- struct length_instance *o = vo;
-
- if (!strcmp(name, "")) {
- char str[64];
- snprintf(str, sizeof(str), "%zu", o->length);
-
- if (!NCDValue_InitString(out, str)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
-
- return 1;
- }
-
- return 0;
- }
-
- static void get_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct get_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // check arguments
- NCDValue *index_arg;
- if (!NCDValue_ListRead(o->i->args, 1, &index_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(index_arg) != NCDVALUE_STRING) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
- uintmax_t index;
- if (!parse_unsigned_integer(NCDValue_StringValue(index_arg), &index)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong value");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // check index
- if (index >= NCDValue_ListCount(&mo->list)) {
- ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
- goto fail1;
- }
-
- // copy value
- if (!NCDValue_InitCopy(&o->value, NCDValue_ListGet(&mo->list, index))) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
- goto fail1;
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void get_func_die (void *vo)
- {
- struct get_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free value
- NCDValue_Free(&o->value);
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static int get_func_getvar (void *vo, const char *name, NCDValue *out)
- {
- struct get_instance *o = vo;
-
- if (!strcmp(name, "")) {
- if (!NCDValue_InitCopy(out, &o->value)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
- return 0;
- }
-
- return 1;
- }
-
- return 0;
- }
- static void shift_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct shift_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // check arguments
- if (!NCDValue_ListRead(o->i->args, 0)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // shift
- if (!NCDValue_ListFirst(&mo->list)) {
- ModuleLog(o->i, BLOG_ERROR, "list has no elements");
- goto fail1;
- }
- NCDValue v = NCDValue_ListShift(&mo->list);
- NCDValue_Free(&v);
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
-
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void shift_func_die (void *vo)
- {
- struct shift_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static void contains_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct contains_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // read arguments
- NCDValue *value_arg;
- if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // search
- o->contains = 0;
- for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
- if (NCDValue_Compare(v, value_arg) == 0) {
- o->contains = 1;
- break;
- }
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void contains_func_die (void *vo)
- {
- struct contains_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static int contains_func_getvar (void *vo, const char *name, NCDValue *out)
- {
- struct contains_instance *o = vo;
-
- if (!strcmp(name, "")) {
- const char *value = (o->contains ? "true" : "false");
-
- if (!NCDValue_InitString(out, value)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
-
- return 1;
- }
-
- return 0;
- }
- static void find_func_new (NCDModuleInst *i)
- {
- // allocate instance
- struct find_instance *o = malloc(sizeof(*o));
- if (!o) {
- ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
- goto fail0;
- }
- NCDModuleInst_Backend_SetUser(i, o);
-
- // init arguments
- o->i = i;
-
- // read arguments
- NCDValue *start_pos_arg;
- NCDValue *value_arg;
- if (!NCDValue_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong arity");
- goto fail1;
- }
- if (NCDValue_Type(start_pos_arg) != NCDVALUE_STRING) {
- ModuleLog(o->i, BLOG_ERROR, "wrong type");
- goto fail1;
- }
-
- // read start position
- uintmax_t start_pos;
- if (!parse_unsigned_integer(NCDValue_StringValue(start_pos_arg), &start_pos)) {
- ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
- goto fail1;
- }
-
- // get method object
- struct instance *mo = i->method_object->inst_user;
-
- // search
- o->is_found = 0;
- size_t pos = 0;
- for (NCDValue *v = NCDValue_ListFirst(&mo->list); v; v = NCDValue_ListNext(&mo->list, v)) {
- if (pos >= start_pos && NCDValue_Compare(v, value_arg) == 0) {
- o->is_found = 1;
- o->found_pos = pos;
- break;
- }
- pos++;
- }
-
- // signal up
- NCDModuleInst_Backend_Up(o->i);
- return;
-
- fail1:
- free(o);
- fail0:
- NCDModuleInst_Backend_SetError(i);
- NCDModuleInst_Backend_Dead(i);
- }
- static void find_func_die (void *vo)
- {
- struct find_instance *o = vo;
- NCDModuleInst *i = o->i;
-
- // free instance
- free(o);
-
- NCDModuleInst_Backend_Dead(i);
- }
- static int find_func_getvar (void *vo, const char *name, NCDValue *out)
- {
- struct find_instance *o = vo;
-
- if (!strcmp(name, "pos")) {
- char value[64];
-
- if (o->is_found) {
- snprintf(value, sizeof(value), "%zu", o->found_pos);
- } else {
- snprintf(value, sizeof(value), "none");
- }
-
- if (!NCDValue_InitString(out, value)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
-
- return 1;
- }
-
- if (!strcmp(name, "found")) {
- const char *value = (o->is_found ? "true" : "false");
-
- if (!NCDValue_InitString(out, value)) {
- ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
- return 0;
- }
-
- return 1;
- }
-
- return 0;
- }
- static const struct NCDModule modules[] = {
- {
- .type = "list",
- .func_new = func_new_list,
- .func_die = func_die,
- .func_getvar = func_getvar
- }, {
- .type = "listfrom",
- .base_type = "list",
- .func_new = func_new_listfrom,
- .func_die = func_die,
- .func_getvar = func_getvar
- }, {
- .type = "concatlist", // alias for listfrom
- .base_type = "list",
- .func_new = func_new_listfrom,
- .func_die = func_die,
- .func_getvar = func_getvar
- }, {
- .type = "list::append",
- .func_new = append_func_new,
- .func_die = append_func_die
- }, {
- .type = "list::appendv",
- .func_new = appendv_func_new,
- .func_die = appendv_func_die
- }, {
- .type = "list::length",
- .func_new = length_func_new,
- .func_die = length_func_die,
- .func_getvar = length_func_getvar
- }, {
- .type = "list::get",
- .func_new = get_func_new,
- .func_die = get_func_die,
- .func_getvar = get_func_getvar
- }, {
- .type = "list::shift",
- .func_new = shift_func_new,
- .func_die = shift_func_die
- }, {
- .type = "list::contains",
- .func_new = contains_func_new,
- .func_die = contains_func_die,
- .func_getvar = contains_func_getvar
- }, {
- .type = "list::find",
- .func_new = find_func_new,
- .func_die = find_func_die,
- .func_getvar = find_func_getvar
- }, {
- .type = NULL
- }
- };
- const struct NCDModuleGroup ncdmodule_list = {
- .modules = modules
- };
|