concatv.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @file concatv.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. * String concatenation module.
  25. *
  26. * Synopsis: concatv(list(string) strings)
  27. * Variables:
  28. * string (empty) - elements of strings concatenated
  29. */
  30. #include <stdlib.h>
  31. #include <misc/expstring.h>
  32. #include <ncd/NCDModule.h>
  33. #include <generated/blog_channel_ncd_concatv.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. struct instance {
  36. NCDModuleInst *i;
  37. char *string;
  38. };
  39. static void func_new (NCDModuleInst *i)
  40. {
  41. // allocate instance
  42. struct instance *o = malloc(sizeof(*o));
  43. if (!o) {
  44. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  45. goto fail0;
  46. }
  47. NCDModuleInst_Backend_SetUser(i, o);
  48. // init arguments
  49. o->i = i;
  50. // read arguments
  51. NCDValue *strings_arg;
  52. if (!NCDValue_ListRead(o->i->args, 1, &strings_arg)) {
  53. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  54. goto fail1;
  55. }
  56. if (NCDValue_Type(strings_arg) != NCDVALUE_LIST) {
  57. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  58. goto fail1;
  59. }
  60. // init string
  61. ExpString s;
  62. if (!ExpString_Init(&s)) {
  63. ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
  64. goto fail1;
  65. }
  66. // append arguments
  67. NCDValue *arg = NCDValue_ListFirst(strings_arg);
  68. while (arg) {
  69. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  70. ModuleLog(i, BLOG_ERROR, "wrong type");
  71. goto fail2;
  72. }
  73. if (!ExpString_Append(&s, NCDValue_StringValue(arg))) {
  74. ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
  75. goto fail2;
  76. }
  77. arg = NCDValue_ListNext(strings_arg, arg);
  78. }
  79. // set string
  80. o->string = ExpString_Get(&s);
  81. // signal up
  82. NCDModuleInst_Backend_Up(o->i);
  83. return;
  84. fail2:
  85. ExpString_Free(&s);
  86. fail1:
  87. free(o);
  88. fail0:
  89. NCDModuleInst_Backend_SetError(i);
  90. NCDModuleInst_Backend_Dead(i);
  91. }
  92. static void func_die (void *vo)
  93. {
  94. struct instance *o = vo;
  95. NCDModuleInst *i = o->i;
  96. // free string
  97. free(o->string);
  98. // free instance
  99. free(o);
  100. NCDModuleInst_Backend_Dead(i);
  101. }
  102. static int func_getvar (void *vo, const char *name, NCDValue *out)
  103. {
  104. struct instance *o = vo;
  105. if (!strcmp(name, "")) {
  106. if (!NCDValue_InitString(out, o->string)) {
  107. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. return 0;
  113. }
  114. static const struct NCDModule modules[] = {
  115. {
  116. .type = "concatv",
  117. .func_new = func_new,
  118. .func_die = func_die,
  119. .func_getvar = func_getvar
  120. }, {
  121. .type = NULL
  122. }
  123. };
  124. const struct NCDModuleGroup ncdmodule_concatv = {
  125. .modules = modules
  126. };