concat.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @file concat.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: concat(string elem1, ..., string elemN)
  27. * Variables:
  28. * string (empty) - elem1, ..., elemN concatenated
  29. */
  30. #include <stdlib.h>
  31. #include <misc/expstring.h>
  32. #include <ncd/NCDModule.h>
  33. #include <generated/blog_channel_ncd_concat.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. // init arguments
  48. o->i = i;
  49. // init string
  50. ExpString s;
  51. if (!ExpString_Init(&s)) {
  52. ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
  53. goto fail1;
  54. }
  55. // append arguments
  56. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  57. while (arg) {
  58. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  59. ModuleLog(i, BLOG_ERROR, "wrong type");
  60. goto fail2;
  61. }
  62. if (!ExpString_Append(&s, NCDValue_StringValue(arg))) {
  63. ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
  64. goto fail2;
  65. }
  66. arg = NCDValue_ListNext(o->i->args, arg);
  67. }
  68. // set string
  69. o->string = ExpString_Get(&s);
  70. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  71. return o;
  72. fail2:
  73. ExpString_Free(&s);
  74. fail1:
  75. free(o);
  76. fail0:
  77. return NULL;
  78. }
  79. static void func_free (void *vo)
  80. {
  81. struct instance *o = vo;
  82. // free string
  83. free(o->string);
  84. // free instance
  85. free(o);
  86. }
  87. static int func_getvar (void *vo, const char *name, NCDValue *out)
  88. {
  89. struct instance *o = vo;
  90. if (!strcmp(name, "")) {
  91. if (!NCDValue_InitString(out, o->string)) {
  92. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  93. return 0;
  94. }
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. static const struct NCDModule modules[] = {
  100. {
  101. .type = "concat",
  102. .func_new = func_new,
  103. .func_free = func_free,
  104. .func_getvar = func_getvar
  105. }, {
  106. .type = NULL
  107. }
  108. };
  109. const struct NCDModuleGroup ncdmodule_concat = {
  110. .modules = modules
  111. };