concatlist.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file concatlist.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 concatenation module.
  25. *
  26. * Synopsis: concatlist(list elem1, ..., list elemN)
  27. * Variables:
  28. * list (empty) - elem1, ..., elemN concatenated
  29. */
  30. #include <stdlib.h>
  31. #include <ncd/NCDModule.h>
  32. #include <generated/blog_channel_ncd_concatlist.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. struct instance {
  35. NCDModuleInst *i;
  36. };
  37. static void * func_new (NCDModuleInst *i)
  38. {
  39. // allocate instance
  40. struct instance *o = malloc(sizeof(*o));
  41. if (!o) {
  42. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  43. goto fail0;
  44. }
  45. // init arguments
  46. o->i = i;
  47. // check arguments
  48. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  49. while (arg) {
  50. if (NCDValue_Type(arg) != NCDVALUE_LIST) {
  51. ModuleLog(i, BLOG_ERROR, "wrong type");
  52. goto fail1;
  53. }
  54. arg = NCDValue_ListNext(o->i->args, arg);
  55. }
  56. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  57. return o;
  58. fail1:
  59. free(o);
  60. fail0:
  61. return NULL;
  62. }
  63. static void func_free (void *vo)
  64. {
  65. struct instance *o = vo;
  66. // free instance
  67. free(o);
  68. }
  69. static int func_getvar (void *vo, const char *name, NCDValue *out)
  70. {
  71. struct instance *o = vo;
  72. if (!strcmp(name, "")) {
  73. NCDValue_InitList(out);
  74. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  75. while (arg) {
  76. NCDValue *val = NCDValue_ListFirst(arg);
  77. while (val) {
  78. NCDValue copy;
  79. if (!NCDValue_InitCopy(&copy, val)) {
  80. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  81. goto fail1;
  82. }
  83. if (!NCDValue_ListAppend(out, copy)) {
  84. ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
  85. NCDValue_Free(&copy);
  86. goto fail1;
  87. }
  88. val = NCDValue_ListNext(arg, val);
  89. }
  90. arg = NCDValue_ListNext(o->i->args, arg);
  91. }
  92. return 1;
  93. fail1:
  94. NCDValue_Free(out);
  95. fail0:
  96. return 0;
  97. }
  98. return 0;
  99. }
  100. static const struct NCDModule modules[] = {
  101. {
  102. .type = "concatlist",
  103. .func_new = func_new,
  104. .func_free = func_free,
  105. .func_getvar = func_getvar
  106. }, {
  107. .type = NULL
  108. }
  109. };
  110. const struct NCDModuleGroup ncdmodule_concatlist = {
  111. .modules = modules
  112. };