print.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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/module_common.h>
  56. #include <generated/blog_channel_ncd_print.h>
  57. struct rprint_instance {
  58. NCDModuleInst *i;
  59. NCDValRef args;
  60. int ln;
  61. };
  62. static int check_args (NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  63. {
  64. size_t num_args = NCDVal_ListCount(params->args);
  65. for (size_t j = 0; j < num_args; j++) {
  66. NCDValRef arg = NCDVal_ListGet(params->args, j);
  67. if (!NCDVal_IsString(arg)) {
  68. ModuleLog(i, BLOG_ERROR, "wrong type");
  69. return 0;
  70. }
  71. }
  72. return 1;
  73. }
  74. static void do_print (NCDModuleInst *i, NCDValRef args, int ln)
  75. {
  76. size_t num_args = NCDVal_ListCount(args);
  77. for (size_t j = 0; j < num_args; j++) {
  78. NCDValRef arg = NCDVal_ListGet(args, j);
  79. ASSERT(NCDVal_IsString(arg))
  80. b_cstring arg_cstr = NCDVal_StringCstring(arg);
  81. B_CSTRING_LOOP_RANGE(arg_cstr, 0, arg_cstr.length, pos, chunk_data, chunk_length, {
  82. size_t chunk_pos = 0;
  83. while (chunk_pos < chunk_length) {
  84. ssize_t res = fwrite(chunk_data + chunk_pos, 1, chunk_length - chunk_pos, stdout);
  85. if (res <= 0) {
  86. goto out;
  87. }
  88. chunk_pos += res;
  89. }
  90. })
  91. }
  92. out:
  93. if (ln) {
  94. printf("\n");
  95. }
  96. }
  97. static void rprint_func_new_common (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int ln)
  98. {
  99. struct rprint_instance *o = vo;
  100. o->i = i;
  101. o->args = params->args;
  102. o->ln = ln;
  103. if (!check_args(i, params)) {
  104. goto fail0;
  105. }
  106. NCDModuleInst_Backend_Up(i);
  107. return;
  108. fail0:
  109. NCDModuleInst_Backend_DeadError(i);
  110. }
  111. static void rprint_func_die (void *vo)
  112. {
  113. struct rprint_instance *o = vo;
  114. do_print(o->i, o->args, o->ln);
  115. NCDModuleInst_Backend_Dead(o->i);
  116. }
  117. static void print_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  118. {
  119. if (!check_args(i, params)) {
  120. goto fail0;
  121. }
  122. do_print(i, params->args, 0);
  123. NCDModuleInst_Backend_Up(i);
  124. return;
  125. fail0:
  126. NCDModuleInst_Backend_DeadError(i);
  127. }
  128. static void println_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  129. {
  130. if (!check_args(i, params)) {
  131. goto fail0;
  132. }
  133. do_print(i, params->args, 1);
  134. NCDModuleInst_Backend_Up(i);
  135. return;
  136. fail0:
  137. NCDModuleInst_Backend_DeadError(i);
  138. }
  139. static void rprint_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  140. {
  141. return rprint_func_new_common(vo, i, params, 0);
  142. }
  143. static void rprintln_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  144. {
  145. return rprint_func_new_common(vo, i, params, 1);
  146. }
  147. static struct NCDModule modules[] = {
  148. {
  149. .type = "print",
  150. .func_new2 = print_func_new,
  151. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  152. }, {
  153. .type = "println",
  154. .func_new2 = println_func_new,
  155. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  156. }, {
  157. .type = "rprint",
  158. .func_new2 = rprint_func_new,
  159. .func_die = rprint_func_die,
  160. .alloc_size = sizeof(struct rprint_instance),
  161. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  162. }, {
  163. .type = "rprintln",
  164. .func_new2 = rprintln_func_new,
  165. .func_die = rprint_func_die,
  166. .alloc_size = sizeof(struct rprint_instance),
  167. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  168. }, {
  169. .type = NULL
  170. }
  171. };
  172. const struct NCDModuleGroup ncdmodule_print = {
  173. .modules = modules
  174. };