list.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 <ncd/NCDModule.h>
  32. #include <generated/blog_channel_ncd_list.h>
  33. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  34. struct instance {
  35. NCDModuleInst *i;
  36. };
  37. static void * func_new (NCDModuleInst *i)
  38. {
  39. // allocate instance
  40. struct instance *o = malloc(sizeof(*o));
  41. if (!o) {
  42. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  43. goto fail0;
  44. }
  45. // init arguments
  46. o->i = i;
  47. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  48. return o;
  49. fail0:
  50. return NULL;
  51. }
  52. static void func_free (void *vo)
  53. {
  54. struct instance *o = vo;
  55. // free instance
  56. free(o);
  57. }
  58. static int func_getvar (void *vo, const char *name, NCDValue *out)
  59. {
  60. struct instance *o = vo;
  61. if (!strcmp(name, "")) {
  62. if (!NCDValue_InitCopy(out, o->i->args)) {
  63. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  64. return 0;
  65. }
  66. return 1;
  67. }
  68. return 0;
  69. }
  70. static const struct NCDModule modules[] = {
  71. {
  72. .type = "list",
  73. .func_new = func_new,
  74. .func_free = func_free,
  75. .func_getvar = func_getvar
  76. }, {
  77. .type = NULL
  78. }
  79. };
  80. const struct NCDModuleGroup ncdmodule_list = {
  81. .modules = modules
  82. };