strcmp.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * @file strcmp.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. * String comparison module.
  25. *
  26. * Synopsis: strcmp(string str1, string str2)
  27. * Variables:
  28. * string (empty) - "true" if str1 and str2 are equal, "false" if not
  29. */
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ncd/NCDModule.h>
  33. #include <generated/blog_channel_ncd_strcmp.h>
  34. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  35. struct instance {
  36. NCDModuleInst *i;
  37. NCDValue *arg1;
  38. NCDValue *arg2;
  39. };
  40. static void func_new (NCDModuleInst *i)
  41. {
  42. // allocate instance
  43. struct instance *o = malloc(sizeof(*o));
  44. if (!o) {
  45. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  46. goto fail0;
  47. }
  48. NCDModuleInst_Backend_SetUser(i, o);
  49. // init arguments
  50. o->i = i;
  51. // read arguments
  52. if (!NCDValue_ListRead(o->i->args, 2, &o->arg1, &o->arg2)) {
  53. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  54. goto fail1;
  55. }
  56. if (NCDValue_Type(o->arg1) != NCDVALUE_STRING || NCDValue_Type(o->arg2) != NCDVALUE_STRING) {
  57. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  58. goto fail1;
  59. }
  60. // signal up
  61. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  62. return;
  63. fail1:
  64. free(o);
  65. fail0:
  66. NCDModuleInst_Backend_SetError(i);
  67. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  68. }
  69. static void func_die (void *vo)
  70. {
  71. struct instance *o = vo;
  72. NCDModuleInst *i = o->i;
  73. // free instance
  74. free(o);
  75. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  76. }
  77. static int func_getvar (void *vo, const char *name, NCDValue *out)
  78. {
  79. struct instance *o = vo;
  80. if (!strcmp(name, "")) {
  81. const char *v = (!strcmp(NCDValue_StringValue(o->arg1), NCDValue_StringValue(o->arg2)) ? "true" : "false");
  82. if (!NCDValue_InitString(out, v)) {
  83. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. return 0;
  89. }
  90. static const struct NCDModule modules[] = {
  91. {
  92. .type = "strcmp",
  93. .func_new = func_new,
  94. .func_die = func_die,
  95. .func_getvar = func_getvar
  96. }, {
  97. .type = NULL
  98. }
  99. };
  100. const struct NCDModuleGroup ncdmodule_strcmp = {
  101. .modules = modules
  102. };