var.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @file var.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. * Variable module.
  25. *
  26. * Synopsis: var(value)
  27. * Variables:
  28. * (empty) - value
  29. */
  30. #include <stdlib.h>
  31. #include <ncd/NCDModule.h>
  32. #include <generated/blog_channel_ncd_var.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. struct instance {
  35. NCDModuleInst *i;
  36. NCDValue *arg;
  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. // init arguments
  47. o->i = i;
  48. // read argument
  49. if (!NCDValue_ListRead(o->i->args, 1, &o->arg)) {
  50. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  51. goto fail1;
  52. }
  53. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  54. return o;
  55. fail1:
  56. free(o);
  57. fail0:
  58. return NULL;
  59. }
  60. static void func_free (void *vo)
  61. {
  62. struct instance *o = vo;
  63. // free instance
  64. free(o);
  65. }
  66. static int func_getvar (void *vo, const char *name, NCDValue *out)
  67. {
  68. struct instance *o = vo;
  69. if (!strcmp(name, "")) {
  70. if (!NCDValue_InitCopy(out, o->arg)) {
  71. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  72. return 0;
  73. }
  74. return 1;
  75. }
  76. return 0;
  77. }
  78. static const struct NCDModule modules[] = {
  79. {
  80. .type = "var",
  81. .func_new = func_new,
  82. .func_free = func_free,
  83. .func_getvar = func_getvar
  84. }, {
  85. .type = NULL
  86. }
  87. };
  88. const struct NCDModuleGroup ncdmodule_var = {
  89. .modules = modules
  90. };