print.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /**
  2. * @file print.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. * Conditional module.
  25. *
  26. * Synopsis: print(string str)
  27. * Synopsis: println(string str)
  28. */
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <ncd/NCDModule.h>
  32. #include <generated/blog_channel_ncd_print.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. NCDModuleInst_Backend_SetUser(i, o);
  46. // init arguments
  47. o->i = i;
  48. // print
  49. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  50. while (arg) {
  51. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  52. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  53. goto fail1;
  54. }
  55. printf("%s", NCDValue_StringValue(arg));
  56. arg = NCDValue_ListNext(o->i->args, arg);
  57. }
  58. return;
  59. fail1:
  60. free(o);
  61. fail0:
  62. NCDModuleInst_Backend_SetError(i);
  63. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  64. }
  65. static void func_new_ln (NCDModuleInst *i)
  66. {
  67. // allocate instance
  68. struct instance *o = malloc(sizeof(*o));
  69. if (!o) {
  70. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  71. goto fail0;
  72. }
  73. NCDModuleInst_Backend_SetUser(i, o);
  74. // init arguments
  75. o->i = i;
  76. // print
  77. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  78. while (arg) {
  79. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  80. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  81. goto fail1;
  82. }
  83. printf("%s", NCDValue_StringValue(arg));
  84. arg = NCDValue_ListNext(o->i->args, arg);
  85. }
  86. printf("\n");
  87. return;
  88. fail1:
  89. free(o);
  90. fail0:
  91. NCDModuleInst_Backend_SetError(i);
  92. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  93. }
  94. static void func_die (void *vo)
  95. {
  96. struct instance *o = vo;
  97. NCDModuleInst *i = o->i;
  98. // free instance
  99. free(o);
  100. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  101. }
  102. static const struct NCDModule modules[] = {
  103. {
  104. .type = "print",
  105. .func_new = func_new,
  106. .func_die = func_die
  107. }, {
  108. .type = "println",
  109. .func_new = func_new_ln,
  110. .func_die = func_die
  111. }, {
  112. .type = NULL
  113. }
  114. };
  115. const struct NCDModuleGroup ncdmodule_print = {
  116. .modules = modules
  117. };