list.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * @file list.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 construction module.
  25. *
  26. * Synopsis: list(elem1, ..., elemN)
  27. * Variables:
  28. * (empty) - list containing elem1, ..., elemN
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ncd/NCDModule.h>
  33. #include <generated/blog_channel_ncd_list.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. // signal up
  50. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  51. return;
  52. fail0:
  53. NCDModuleInst_Backend_SetError(i);
  54. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  55. }
  56. static void func_die (void *vo)
  57. {
  58. struct instance *o = vo;
  59. NCDModuleInst *i = o->i;
  60. // free instance
  61. free(o);
  62. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  63. }
  64. static int func_getvar (void *vo, const char *name, NCDValue *out)
  65. {
  66. struct instance *o = vo;
  67. if (!strcmp(name, "")) {
  68. if (!NCDValue_InitCopy(out, o->i->args)) {
  69. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. static const struct NCDModule modules[] = {
  77. {
  78. .type = "list",
  79. .func_new = func_new,
  80. .func_die = func_die,
  81. .func_getvar = func_getvar
  82. }, {
  83. .type = NULL
  84. }
  85. };
  86. const struct NCDModuleGroup ncdmodule_list = {
  87. .modules = modules
  88. };