vports.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 <assert.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include "libguile/_scm.h"
  25. #include "libguile/eval.h"
  26. #include "libguile/chars.h"
  27. #include "libguile/ports.h"
  28. #include "libguile/ports-internal.h"
  29. #include "libguile/fports.h"
  30. #include "libguile/root.h"
  31. #include "libguile/strings.h"
  32. #include "libguile/vectors.h"
  33. #include "libguile/validate.h"
  34. #include "libguile/vports.h"
  35. #ifdef HAVE_STRING_H
  36. #include <string.h>
  37. #endif
  38. /* {Ports - soft ports}
  39. *
  40. */
  41. static scm_t_port_type *scm_soft_port_type;
  42. #define ENCODE_BUF_SIZE 10
  43. struct soft_port {
  44. SCM write_char;
  45. SCM write_string;
  46. SCM flush;
  47. SCM read_char;
  48. SCM close;
  49. SCM input_waiting;
  50. scm_t_uint8 encode_buf[ENCODE_BUF_SIZE];
  51. size_t encode_cur;
  52. size_t encode_end;
  53. };
  54. /* Sadly it seems that most code expects there to be no write buffering
  55. at all. */
  56. static void
  57. soft_port_get_natural_buffer_sizes (SCM port, size_t *read_size,
  58. size_t *write_size)
  59. {
  60. *write_size = 1;
  61. }
  62. static size_t
  63. soft_port_write (SCM port, SCM src, size_t start, size_t count)
  64. {
  65. struct soft_port *stream = (void *) SCM_STREAM (port);
  66. signed char * ptr = SCM_BYTEVECTOR_CONTENTS (src) + start;
  67. scm_call_1 (stream->write_string,
  68. scm_from_port_stringn ((char *) ptr, count, port));
  69. /* Backwards compatibility. */
  70. if (scm_is_true (stream->flush))
  71. scm_call_0 (stream->flush);
  72. return count;
  73. }
  74. /* places a single char in the input buffer. */
  75. static size_t
  76. soft_port_read (SCM port, SCM dst, size_t start, size_t count)
  77. {
  78. size_t written;
  79. struct soft_port *stream = (void *) SCM_STREAM (port);
  80. signed char *dst_ptr = SCM_BYTEVECTOR_CONTENTS (dst) + start;
  81. /* A character can be more than one byte, but we don't have a
  82. guarantee that there is more than one byte in the read buffer. So,
  83. use an intermediate buffer. Terrible. This whole facility should
  84. be (re)designed. */
  85. if (stream->encode_cur == stream->encode_end)
  86. {
  87. SCM ans;
  88. char *str;
  89. size_t len;
  90. ans = scm_call_0 (stream->read_char);
  91. if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
  92. return 0;
  93. SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "soft_port_read");
  94. /* It's possible to make a fast path here, but it would be fastest
  95. if the read procedure could fill its buffer directly. */
  96. str = scm_to_port_stringn (scm_string (scm_list_1 (ans)), &len, port);
  97. assert (len > 0 && len <= ENCODE_BUF_SIZE);
  98. stream->encode_cur = 0;
  99. stream->encode_end = len;
  100. memcpy (stream->encode_buf, str, len);
  101. free (str);
  102. }
  103. for (written = 0;
  104. written < count && stream->encode_cur < stream->encode_end;
  105. written++, stream->encode_cur++)
  106. dst_ptr[written] = stream->encode_buf[stream->encode_cur];
  107. return written;
  108. }
  109. static void
  110. soft_port_close (SCM port)
  111. {
  112. struct soft_port *stream = (void *) SCM_STREAM (port);
  113. if (scm_is_true (stream->close))
  114. scm_call_0 (stream->close);
  115. }
  116. static int
  117. soft_port_input_waiting (SCM port)
  118. {
  119. struct soft_port *stream = (void *) SCM_STREAM (port);
  120. if (scm_is_true (stream->input_waiting))
  121. return scm_to_int (scm_call_0 (stream->input_waiting));
  122. /* Default is such that char-ready? for soft ports returns #t, as it
  123. did before this extension was implemented. */
  124. return 1;
  125. }
  126. SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
  127. (SCM pv, SCM modes),
  128. "Return a port capable of receiving or delivering characters as\n"
  129. "specified by the @var{modes} string (@pxref{File Ports,\n"
  130. "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
  131. "components are as follows:\n"
  132. "\n"
  133. "@enumerate 0\n"
  134. "@item\n"
  135. "procedure accepting one character for output\n"
  136. "@item\n"
  137. "procedure accepting a string for output\n"
  138. "@item\n"
  139. "thunk for flushing output\n"
  140. "@item\n"
  141. "thunk for getting one character\n"
  142. "@item\n"
  143. "thunk for closing port (not by garbage collection)\n"
  144. "@item\n"
  145. "(if present and not @code{#f}) thunk for computing the number of\n"
  146. "characters that can be read from the port without blocking.\n"
  147. "@end enumerate\n"
  148. "\n"
  149. "For an output-only port only elements 0, 1, 2, and 4 need be\n"
  150. "procedures. For an input-only port only elements 3 and 4 need\n"
  151. "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
  152. "there is no useful operation for them to perform.\n"
  153. "\n"
  154. "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
  155. "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
  156. "Scheme}) it indicates that the port has reached end-of-file.\n"
  157. "For example:\n"
  158. "\n"
  159. "@lisp\n"
  160. "(define stdout (current-output-port))\n"
  161. "(define p (make-soft-port\n"
  162. " (vector\n"
  163. " (lambda (c) (write c stdout))\n"
  164. " (lambda (s) (display s stdout))\n"
  165. " (lambda () (display \".\" stdout))\n"
  166. " (lambda () (char-upcase (read-char)))\n"
  167. " (lambda () (display \"@@\" stdout)))\n"
  168. " \"rw\"))\n"
  169. "\n"
  170. "(write p p) @result{} #<input-output: soft 8081e20>\n"
  171. "@end lisp")
  172. #define FUNC_NAME s_scm_make_soft_port
  173. {
  174. int vlen;
  175. struct soft_port *stream;
  176. SCM_VALIDATE_VECTOR (1, pv);
  177. vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
  178. SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
  179. SCM_VALIDATE_STRING (2, modes);
  180. stream = scm_gc_typed_calloc (struct soft_port);
  181. stream->write_char = SCM_SIMPLE_VECTOR_REF (pv, 0);
  182. stream->write_string = SCM_SIMPLE_VECTOR_REF (pv, 1);
  183. stream->flush = SCM_SIMPLE_VECTOR_REF (pv, 2);
  184. stream->read_char = SCM_SIMPLE_VECTOR_REF (pv, 3);
  185. stream->close = SCM_SIMPLE_VECTOR_REF (pv, 4);
  186. stream->input_waiting =
  187. vlen == 6 ? SCM_SIMPLE_VECTOR_REF (pv, 5) : SCM_BOOL_F;
  188. return scm_c_make_port (scm_soft_port_type, scm_i_mode_bits (modes),
  189. (scm_t_bits) stream);
  190. }
  191. #undef FUNC_NAME
  192. static scm_t_port_type *
  193. scm_make_sfptob ()
  194. {
  195. scm_t_port_type *ptob = scm_make_port_type ("soft", soft_port_read,
  196. soft_port_write);
  197. scm_set_port_close (ptob, soft_port_close);
  198. scm_set_port_needs_close_on_gc (ptob, 1);
  199. scm_set_port_get_natural_buffer_sizes (ptob,
  200. soft_port_get_natural_buffer_sizes);
  201. scm_set_port_input_waiting (ptob, soft_port_input_waiting);
  202. return ptob;
  203. }
  204. void
  205. scm_init_vports ()
  206. {
  207. scm_soft_port_type = scm_make_sfptob ();
  208. #include "libguile/vports.x"
  209. }
  210. /*
  211. Local Variables:
  212. c-file-style: "gnu"
  213. End:
  214. */