choose.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @file choose.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. * Multiple value selection based on boolean conditions.
  25. *
  26. * Synopsis:
  27. * choose({{string cond1, result1}, ..., {string condN, resultN}}, default_result)
  28. *
  29. * Variables:
  30. * (empty) - If cond1="true" then result1,
  31. * else if cond2="true" then result2,
  32. * ...,
  33. * else default_result.
  34. */
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <ncd/NCDModule.h>
  38. #include <generated/blog_channel_ncd_choose.h>
  39. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  40. struct instance {
  41. NCDModuleInst *i;
  42. NCDValue *result;
  43. };
  44. static void func_new (NCDModuleInst *i)
  45. {
  46. // allocate instance
  47. struct instance *o = malloc(sizeof(*o));
  48. if (!o) {
  49. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  50. goto fail0;
  51. }
  52. NCDModuleInst_Backend_SetUser(i, o);
  53. // init arguments
  54. o->i = i;
  55. // read arguments
  56. NCDValue *arg_choices;
  57. NCDValue *arg_default_result;
  58. if (!NCDValue_ListRead(i->args, 2, &arg_choices, &arg_default_result)) {
  59. ModuleLog(i, BLOG_ERROR, "wrong arity");
  60. goto fail1;
  61. }
  62. if (NCDValue_Type(arg_choices) != NCDVALUE_LIST) {
  63. ModuleLog(i, BLOG_ERROR, "wrong type");
  64. goto fail1;
  65. }
  66. // set no result
  67. o->result = NULL;
  68. // iterate choices
  69. for (NCDValue *c = NCDValue_ListFirst(arg_choices); c; c = NCDValue_ListNext(arg_choices, c)) {
  70. // check choice type
  71. if (NCDValue_Type(c) != NCDVALUE_LIST) {
  72. ModuleLog(i, BLOG_ERROR, "wrong choice type");
  73. goto fail1;
  74. }
  75. // read choice
  76. NCDValue *c_cond;
  77. NCDValue *c_result;
  78. if (!NCDValue_ListRead(c, 2, &c_cond, &c_result)) {
  79. ModuleLog(i, BLOG_ERROR, "wrong choice contents arity");
  80. goto fail1;
  81. }
  82. if (NCDValue_Type(c_cond) != NCDVALUE_STRING) {
  83. ModuleLog(i, BLOG_ERROR, "wrong choice condition type");
  84. goto fail1;
  85. }
  86. // update result
  87. if (!o->result && !strcmp(NCDValue_StringValue(c_cond), "true")) {
  88. o->result = c_result;
  89. }
  90. }
  91. // default?
  92. if (!o->result) {
  93. o->result = arg_default_result;
  94. }
  95. // signal up
  96. NCDModuleInst_Backend_Up(o->i);
  97. return;
  98. fail1:
  99. free(o);
  100. fail0:
  101. NCDModuleInst_Backend_SetError(i);
  102. NCDModuleInst_Backend_Dead(i);
  103. }
  104. static void func_die (void *vo)
  105. {
  106. struct instance *o = vo;
  107. NCDModuleInst *i = o->i;
  108. // free instance
  109. free(o);
  110. NCDModuleInst_Backend_Dead(i);
  111. }
  112. static int func_getvar (void *vo, const char *name, NCDValue *out)
  113. {
  114. struct instance *o = vo;
  115. if (!strcmp(name, "")) {
  116. if (!NCDValue_InitCopy(out, o->result)) {
  117. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. return 0;
  123. }
  124. static const struct NCDModule modules[] = {
  125. {
  126. .type = "choose",
  127. .func_new = func_new,
  128. .func_die = func_die,
  129. .func_getvar = func_getvar
  130. }, {
  131. .type = NULL
  132. }
  133. };
  134. const struct NCDModuleGroup ncdmodule_choose = {
  135. .modules = modules
  136. };