print.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /**
  2. * @file print.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Modules for printing to standard output.
  32. *
  33. * Synopsis:
  34. * print([string str ...])
  35. * Description:
  36. * On initialization, prints strings to standard output.
  37. *
  38. * Synopsis:
  39. * println([string str ...])
  40. * Description:
  41. * On initialization, prints strings to standard output, and a newline.
  42. *
  43. * Synopsis:
  44. * rprint([string str ...])
  45. * Description:
  46. * On deinitialization, prints strings to standard output.
  47. *
  48. * Synopsis:
  49. * rprintln([string str ...])
  50. * Description:
  51. * On deinitialization, prints strings to standard output, and a newline.
  52. */
  53. #include <stdlib.h>
  54. #include <stdio.h>
  55. #include <ncd/NCDModule.h>
  56. #include <generated/blog_channel_ncd_print.h>
  57. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  58. struct instance {
  59. NCDModuleInst *i;
  60. int ln;
  61. int rev;
  62. };
  63. static void do_print (NCDModuleInst *i, int ln)
  64. {
  65. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  66. ASSERT(NCDValue_Type(arg) == NCDVALUE_STRING)
  67. printf("%s", NCDValue_StringValue(arg));
  68. }
  69. if (ln) {
  70. printf("\n");
  71. }
  72. }
  73. static void func_new_temp (NCDModuleInst *i, int ln, int rev)
  74. {
  75. // allocate instance
  76. struct instance *o = malloc(sizeof(*o));
  77. if (!o) {
  78. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  79. goto fail0;
  80. }
  81. NCDModuleInst_Backend_SetUser(i, o);
  82. // init arguments
  83. o->i = i;
  84. o->ln = ln;
  85. o->rev = rev;
  86. // check arguments
  87. for (NCDValue *arg = NCDValue_ListFirst(i->args); arg; arg = NCDValue_ListNext(i->args, arg)) {
  88. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  89. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  90. goto fail1;
  91. }
  92. }
  93. // print
  94. if (!o->rev) {
  95. do_print(o->i, o->ln);
  96. }
  97. // signal up
  98. NCDModuleInst_Backend_Up(o->i);
  99. return;
  100. fail1:
  101. free(o);
  102. fail0:
  103. NCDModuleInst_Backend_SetError(i);
  104. NCDModuleInst_Backend_Dead(i);
  105. }
  106. static void func_die (void *vo)
  107. {
  108. struct instance *o = vo;
  109. NCDModuleInst *i = o->i;
  110. // print
  111. if (o->rev) {
  112. do_print(o->i, o->ln);
  113. }
  114. // free instance
  115. free(o);
  116. NCDModuleInst_Backend_Dead(i);
  117. }
  118. static void print_func_new (NCDModuleInst *i)
  119. {
  120. return func_new_temp(i, 0, 0);
  121. }
  122. static void println_func_new (NCDModuleInst *i)
  123. {
  124. return func_new_temp(i, 1, 0);
  125. }
  126. static void rprint_func_new (NCDModuleInst *i)
  127. {
  128. return func_new_temp(i, 0, 1);
  129. }
  130. static void rprintln_func_new (NCDModuleInst *i)
  131. {
  132. return func_new_temp(i, 1, 1);
  133. }
  134. static const struct NCDModule modules[] = {
  135. {
  136. .type = "print",
  137. .func_new = print_func_new,
  138. .func_die = func_die
  139. }, {
  140. .type = "println",
  141. .func_new = println_func_new,
  142. .func_die = func_die
  143. }, {
  144. .type = "rprint",
  145. .func_new = rprint_func_new,
  146. .func_die = func_die
  147. }, {
  148. .type = "rprintln",
  149. .func_new = rprintln_func_new,
  150. .func_die = func_die
  151. }, {
  152. .type = NULL
  153. }
  154. };
  155. const struct NCDModuleGroup ncdmodule_print = {
  156. .modules = modules
  157. };