vports.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2006, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include "libguile/_scm.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/chars.h"
  26. #include "libguile/ports.h"
  27. #include "libguile/ports-internal.h"
  28. #include "libguile/fports.h"
  29. #include "libguile/root.h"
  30. #include "libguile/strings.h"
  31. #include "libguile/vectors.h"
  32. #include "libguile/validate.h"
  33. #include "libguile/vports.h"
  34. #ifdef HAVE_STRING_H
  35. #include <string.h>
  36. #endif
  37. /* {Ports - soft ports}
  38. *
  39. */
  40. static scm_t_bits scm_tc16_sfport;
  41. static void
  42. sf_flush (SCM port)
  43. {
  44. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  45. SCM stream = SCM_PACK (pt->stream);
  46. SCM f = SCM_SIMPLE_VECTOR_REF (stream, 2);
  47. if (scm_is_true (f))
  48. scm_call_0 (f);
  49. }
  50. static void
  51. sf_write (SCM port, const void *data, size_t size)
  52. {
  53. SCM p = SCM_PACK (SCM_STREAM (port));
  54. /* DATA is assumed to be a locale-encoded C string, which makes it
  55. hard to reliably pass binary data to a soft port. It can be
  56. achieved by choosing a Latin-1 locale, though, but the recommended
  57. approach is to use an R6RS "custom binary output port" instead. */
  58. scm_call_1 (SCM_SIMPLE_VECTOR_REF (p, 1),
  59. scm_from_locale_stringn ((char *) data, size));
  60. }
  61. /* calling the flush proc (element 2) is in case old code needs it,
  62. but perhaps softports could the use port buffer in the same way as
  63. fports. */
  64. /* places a single char in the input buffer. */
  65. static int
  66. sf_fill_input (SCM port)
  67. {
  68. SCM p = SCM_PACK (SCM_STREAM (port));
  69. SCM ans;
  70. scm_t_wchar c;
  71. scm_t_port_internal *pti;
  72. ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */
  73. if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
  74. return EOF;
  75. SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input");
  76. pti = SCM_PORT_GET_INTERNAL (port);
  77. c = SCM_CHAR (ans);
  78. if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1
  79. || (pti->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8 && c < 0xff))
  80. {
  81. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  82. *pt->read_buf = c;
  83. pt->read_pos = pt->read_buf;
  84. pt->read_end = pt->read_buf + 1;
  85. }
  86. else
  87. {
  88. long line = SCM_LINUM (port);
  89. int column = SCM_COL (port);
  90. scm_ungetc_unlocked (c, port);
  91. SCM_LINUM (port) = line;
  92. SCM_COL (port) = column;
  93. }
  94. return c;
  95. }
  96. static int
  97. sf_close (SCM port)
  98. {
  99. SCM p = SCM_PACK (SCM_STREAM (port));
  100. SCM f = SCM_SIMPLE_VECTOR_REF (p, 4);
  101. if (scm_is_false (f))
  102. return 0;
  103. f = scm_call_0 (f);
  104. errno = 0;
  105. return scm_is_false (f) ? EOF : 0;
  106. }
  107. static int
  108. sf_input_waiting (SCM port)
  109. {
  110. SCM p = SCM_PACK (SCM_STREAM (port));
  111. if (SCM_SIMPLE_VECTOR_LENGTH (p) >= 6)
  112. {
  113. SCM f = SCM_SIMPLE_VECTOR_REF (p, 5);
  114. if (scm_is_true (f))
  115. return scm_to_int (scm_call_0 (f));
  116. }
  117. /* Default is such that char-ready? for soft ports returns #t, as it
  118. did before this extension was implemented. */
  119. return 1;
  120. }
  121. SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
  122. (SCM pv, SCM modes),
  123. "Return a port capable of receiving or delivering characters as\n"
  124. "specified by the @var{modes} string (@pxref{File Ports,\n"
  125. "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
  126. "components are as follows:\n"
  127. "\n"
  128. "@enumerate 0\n"
  129. "@item\n"
  130. "procedure accepting one character for output\n"
  131. "@item\n"
  132. "procedure accepting a string for output\n"
  133. "@item\n"
  134. "thunk for flushing output\n"
  135. "@item\n"
  136. "thunk for getting one character\n"
  137. "@item\n"
  138. "thunk for closing port (not by garbage collection)\n"
  139. "@item\n"
  140. "(if present and not @code{#f}) thunk for computing the number of\n"
  141. "characters that can be read from the port without blocking.\n"
  142. "@end enumerate\n"
  143. "\n"
  144. "For an output-only port only elements 0, 1, 2, and 4 need be\n"
  145. "procedures. For an input-only port only elements 3 and 4 need\n"
  146. "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
  147. "there is no useful operation for them to perform.\n"
  148. "\n"
  149. "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
  150. "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
  151. "Scheme}) it indicates that the port has reached end-of-file.\n"
  152. "For example:\n"
  153. "\n"
  154. "@lisp\n"
  155. "(define stdout (current-output-port))\n"
  156. "(define p (make-soft-port\n"
  157. " (vector\n"
  158. " (lambda (c) (write c stdout))\n"
  159. " (lambda (s) (display s stdout))\n"
  160. " (lambda () (display \".\" stdout))\n"
  161. " (lambda () (char-upcase (read-char)))\n"
  162. " (lambda () (display \"@@\" stdout)))\n"
  163. " \"rw\"))\n"
  164. "\n"
  165. "(write p p) @result{} #<input-output: soft 8081e20>\n"
  166. "@end lisp")
  167. #define FUNC_NAME s_scm_make_soft_port
  168. {
  169. int vlen;
  170. SCM z;
  171. SCM_VALIDATE_VECTOR (1, pv);
  172. vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
  173. SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
  174. SCM_VALIDATE_STRING (2, modes);
  175. z = scm_c_make_port (scm_tc16_sfport, scm_i_mode_bits (modes),
  176. SCM_UNPACK (pv));
  177. scm_port_non_buffer (SCM_PTAB_ENTRY (z));
  178. return z;
  179. }
  180. #undef FUNC_NAME
  181. static scm_t_bits
  182. scm_make_sfptob ()
  183. {
  184. scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
  185. scm_set_port_flush (tc, sf_flush);
  186. scm_set_port_close (tc, sf_close);
  187. scm_set_port_input_waiting (tc, sf_input_waiting);
  188. return tc;
  189. }
  190. void
  191. scm_init_vports ()
  192. {
  193. scm_tc16_sfport = scm_make_sfptob ();
  194. #include "libguile/vports.x"
  195. }
  196. /*
  197. Local Variables:
  198. c-file-style: "gnu"
  199. End:
  200. */