concatlist.c 3.5 KB

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