print.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. * 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:
  42. * rprintln([string str ...])
  43. * Description:
  44. * On deinitialization, prints strings to standard output, and a newline.
  45. */
  46. #include <stdlib.h>
  47. #include <stdio.h>
  48. #include <ncd/NCDModule.h>
  49. #include <generated/blog_channel_ncd_print.h>
  50. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  51. struct instance {
  52. NCDModuleInst *i;
  53. int ln;
  54. int rev;
  55. };
  56. static void do_print (NCDModuleInst *i, int ln)
  57. {
  58. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  59. ASSERT(NCDValue_Type(arg) == NCDVALUE_STRING)
  60. printf("%s", NCDValue_StringValue(arg));
  61. }
  62. if (ln) {
  63. printf("\n");
  64. }
  65. }
  66. static void func_new_temp (NCDModuleInst *i, int ln, int rev)
  67. {
  68. // allocate instance
  69. struct instance *o = malloc(sizeof(*o));
  70. if (!o) {
  71. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  72. goto fail0;
  73. }
  74. NCDModuleInst_Backend_SetUser(i, o);
  75. // init arguments
  76. o->i = i;
  77. o->ln = ln;
  78. o->rev = rev;
  79. // check arguments
  80. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  81. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  82. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  83. goto fail1;
  84. }
  85. }
  86. // print
  87. if (!o->rev) {
  88. do_print(o->i, o->ln);
  89. }
  90. // signal up
  91. NCDModuleInst_Backend_Up(o->i);
  92. return;
  93. fail1:
  94. free(o);
  95. fail0:
  96. NCDModuleInst_Backend_SetError(i);
  97. NCDModuleInst_Backend_Dead(i);
  98. }
  99. static void func_die (void *vo)
  100. {
  101. struct instance *o = vo;
  102. NCDModuleInst *i = o->i;
  103. // print
  104. if (o->rev) {
  105. do_print(o->i, o->ln);
  106. }
  107. // free instance
  108. free(o);
  109. NCDModuleInst_Backend_Dead(i);
  110. }
  111. static void print_func_new (NCDModuleInst *i)
  112. {
  113. return func_new_temp(i, 0, 0);
  114. }
  115. static void println_func_new (NCDModuleInst *i)
  116. {
  117. return func_new_temp(i, 1, 0);
  118. }
  119. static void rprint_func_new (NCDModuleInst *i)
  120. {
  121. return func_new_temp(i, 0, 1);
  122. }
  123. static void rprintln_func_new (NCDModuleInst *i)
  124. {
  125. return func_new_temp(i, 1, 1);
  126. }
  127. static const struct NCDModule modules[] = {
  128. {
  129. .type = "print",
  130. .func_new = print_func_new,
  131. .func_die = func_die
  132. }, {
  133. .type = "println",
  134. .func_new = println_func_new,
  135. .func_die = func_die
  136. }, {
  137. .type = "rprint",
  138. .func_new = rprint_func_new,
  139. .func_die = func_die
  140. }, {
  141. .type = "rprintln",
  142. .func_new = rprintln_func_new,
  143. .func_die = func_die
  144. }, {
  145. .type = NULL
  146. }
  147. };
  148. const struct NCDModuleGroup ncdmodule_print = {
  149. .modules = modules
  150. };