print.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @file print.c
  3. * @author Ambroz Bizjak <[email protected]>
  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. * Modules for printing to standard output.
  25. *
  26. * Synopsis:
  27. * print([string str ...])
  28. * Description:
  29. * On initialization, prints strings to standard output.
  30. *
  31. * Synopsis:
  32. * println([string str ...])
  33. * Description:
  34. * On initialization, prints strings to standard output, and a newline.
  35. *
  36. * Synopsis:
  37. * rprint([string str ...])
  38. * Description:
  39. * On deinitialization, prints strings to standard output.
  40. *
  41. * Synopsis: rprintln([string str ...])
  42. * Description:
  43. * On deinitialization, prints strings to standard output, and a newline.
  44. */
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <ncd/NCDModule.h>
  48. #include <generated/blog_channel_ncd_print.h>
  49. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  50. struct instance {
  51. NCDModuleInst *i;
  52. int ln;
  53. int rev;
  54. };
  55. static void do_print (NCDModuleInst *i, int ln)
  56. {
  57. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  58. ASSERT(NCDValue_Type(arg) == NCDVALUE_STRING)
  59. printf("%s", NCDValue_StringValue(arg));
  60. }
  61. if (ln) {
  62. printf("\n");
  63. }
  64. }
  65. static void func_new_temp (NCDModuleInst *i, int ln, int rev)
  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. o->ln = ln;
  77. o->rev = rev;
  78. // check arguments
  79. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  80. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  81. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  82. goto fail1;
  83. }
  84. }
  85. // print
  86. if (!o->rev) {
  87. do_print(o->i, o->ln);
  88. }
  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. // print
  103. if (o->rev) {
  104. do_print(o->i, o->ln);
  105. }
  106. // free instance
  107. free(o);
  108. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  109. }
  110. static void print_func_new (NCDModuleInst *i)
  111. {
  112. return func_new_temp(i, 0, 0);
  113. }
  114. static void println_func_new (NCDModuleInst *i)
  115. {
  116. return func_new_temp(i, 1, 0);
  117. }
  118. static void rprint_func_new (NCDModuleInst *i)
  119. {
  120. return func_new_temp(i, 0, 1);
  121. }
  122. static void rprintln_func_new (NCDModuleInst *i)
  123. {
  124. return func_new_temp(i, 1, 1);
  125. }
  126. static const struct NCDModule modules[] = {
  127. {
  128. .type = "print",
  129. .func_new = print_func_new,
  130. .func_die = func_die
  131. }, {
  132. .type = "println",
  133. .func_new = println_func_new,
  134. .func_die = func_die
  135. }, {
  136. .type = "rprint",
  137. .func_new = rprint_func_new,
  138. .func_die = func_die
  139. }, {
  140. .type = "rprintln",
  141. .func_new = rprintln_func_new,
  142. .func_die = func_die
  143. }, {
  144. .type = NULL
  145. }
  146. };
  147. const struct NCDModuleGroup ncdmodule_print = {
  148. .modules = modules
  149. };