var.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 <string.h>
  32. #include <ncd/NCDModule.h>
  33. #include <generated/blog_channel_ncd_var.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. struct instance {
  36. NCDModuleInst *i;
  37. NCDValue *arg;
  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 argument
  51. if (!NCDValue_ListRead(o->i->args, 1, &o->arg)) {
  52. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  53. goto fail1;
  54. }
  55. // signal up
  56. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  57. return;
  58. fail1:
  59. free(o);
  60. fail0:
  61. NCDModuleInst_Backend_SetError(i);
  62. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  63. }
  64. static void func_die (void *vo)
  65. {
  66. struct instance *o = vo;
  67. NCDModuleInst *i = o->i;
  68. // free instance
  69. free(o);
  70. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  71. }
  72. static int func_getvar (void *vo, const char *name, NCDValue *out)
  73. {
  74. struct instance *o = vo;
  75. if (!strcmp(name, "")) {
  76. if (!NCDValue_InitCopy(out, o->arg)) {
  77. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  78. return 0;
  79. }
  80. return 1;
  81. }
  82. return 0;
  83. }
  84. static const struct NCDModule modules[] = {
  85. {
  86. .type = "var",
  87. .func_new = func_new,
  88. .func_die = func_die,
  89. .func_getvar = func_getvar
  90. }, {
  91. .type = NULL
  92. }
  93. };
  94. const struct NCDModuleGroup ncdmodule_var = {
  95. .modules = modules
  96. };