print.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // signal up
  59. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  60. return;
  61. fail1:
  62. free(o);
  63. fail0:
  64. NCDModuleInst_Backend_SetError(i);
  65. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  66. }
  67. static void func_new_ln (NCDModuleInst *i)
  68. {
  69. // allocate instance
  70. struct instance *o = malloc(sizeof(*o));
  71. if (!o) {
  72. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  73. goto fail0;
  74. }
  75. NCDModuleInst_Backend_SetUser(i, o);
  76. // init arguments
  77. o->i = i;
  78. // print
  79. NCDValue *arg = NCDValue_ListFirst(o->i->args);
  80. while (arg) {
  81. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  83. goto fail1;
  84. }
  85. printf("%s", NCDValue_StringValue(arg));
  86. arg = NCDValue_ListNext(o->i->args, arg);
  87. }
  88. printf("\n");
  89. // signal up
  90. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  91. return;
  92. fail1:
  93. free(o);
  94. fail0:
  95. NCDModuleInst_Backend_SetError(i);
  96. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  97. }
  98. static void func_die (void *vo)
  99. {
  100. struct instance *o = vo;
  101. NCDModuleInst *i = o->i;
  102. // free instance
  103. free(o);
  104. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  105. }
  106. static const struct NCDModule modules[] = {
  107. {
  108. .type = "print",
  109. .func_new = func_new,
  110. .func_die = func_die
  111. }, {
  112. .type = "println",
  113. .func_new = func_new_ln,
  114. .func_die = func_die
  115. }, {
  116. .type = NULL
  117. }
  118. };
  119. const struct NCDModuleGroup ncdmodule_print = {
  120. .modules = modules
  121. };