vports.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright 1995-1996,1998-2003,2006,2009-2011,2013,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "boolean.h"
  23. #include "chars.h"
  24. #include "eval.h"
  25. #include "fports.h"
  26. #include "gsubr.h"
  27. #include "numbers.h"
  28. #include "ports-internal.h"
  29. #include "ports.h"
  30. #include "strings.h"
  31. #include "vectors.h"
  32. #include "vports.h"
  33. /* {Ports - soft ports}
  34. *
  35. */
  36. static scm_t_port_type *scm_soft_port_type;
  37. #define ENCODE_BUF_SIZE 10
  38. struct soft_port {
  39. SCM write_char;
  40. SCM write_string;
  41. SCM flush;
  42. SCM read_char;
  43. SCM close;
  44. SCM input_waiting;
  45. uint8_t encode_buf[ENCODE_BUF_SIZE];
  46. size_t encode_cur;
  47. size_t encode_end;
  48. };
  49. /* Sadly it seems that most code expects there to be no write buffering
  50. at all. */
  51. static void
  52. soft_port_get_natural_buffer_sizes (SCM port, size_t *read_size,
  53. size_t *write_size)
  54. {
  55. *write_size = 1;
  56. }
  57. static size_t
  58. soft_port_write (SCM port, SCM src, size_t start, size_t count)
  59. {
  60. struct soft_port *stream = (void *) SCM_STREAM (port);
  61. signed char * ptr = SCM_BYTEVECTOR_CONTENTS (src) + start;
  62. scm_call_1 (stream->write_string,
  63. scm_from_port_stringn ((char *) ptr, count, port));
  64. /* Backwards compatibility. */
  65. if (scm_is_true (stream->flush))
  66. scm_call_0 (stream->flush);
  67. return count;
  68. }
  69. /* places a single char in the input buffer. */
  70. static size_t
  71. soft_port_read (SCM port, SCM dst, size_t start, size_t count)
  72. {
  73. size_t written;
  74. struct soft_port *stream = (void *) SCM_STREAM (port);
  75. signed char *dst_ptr = SCM_BYTEVECTOR_CONTENTS (dst) + start;
  76. /* A character can be more than one byte, but we don't have a
  77. guarantee that there is more than one byte in the read buffer. So,
  78. use an intermediate buffer. Terrible. This whole facility should
  79. be (re)designed. */
  80. if (stream->encode_cur == stream->encode_end)
  81. {
  82. SCM ans;
  83. char *str;
  84. size_t len;
  85. ans = scm_call_0 (stream->read_char);
  86. if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
  87. return 0;
  88. SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "soft_port_read");
  89. /* It's possible to make a fast path here, but it would be fastest
  90. if the read procedure could fill its buffer directly. */
  91. str = scm_to_port_stringn (scm_string (scm_list_1 (ans)), &len, port);
  92. assert (len > 0 && len <= ENCODE_BUF_SIZE);
  93. stream->encode_cur = 0;
  94. stream->encode_end = len;
  95. memcpy (stream->encode_buf, str, len);
  96. free (str);
  97. }
  98. for (written = 0;
  99. written < count && stream->encode_cur < stream->encode_end;
  100. written++, stream->encode_cur++)
  101. dst_ptr[written] = stream->encode_buf[stream->encode_cur];
  102. return written;
  103. }
  104. static void
  105. soft_port_close (SCM port)
  106. {
  107. struct soft_port *stream = (void *) SCM_STREAM (port);
  108. if (scm_is_true (stream->close))
  109. scm_call_0 (stream->close);
  110. }
  111. static int
  112. soft_port_input_waiting (SCM port)
  113. {
  114. struct soft_port *stream = (void *) SCM_STREAM (port);
  115. if (scm_is_true (stream->input_waiting))
  116. return scm_to_int (scm_call_0 (stream->input_waiting));
  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. struct soft_port *stream;
  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. stream = scm_gc_typed_calloc (struct soft_port);
  176. stream->write_char = SCM_SIMPLE_VECTOR_REF (pv, 0);
  177. stream->write_string = SCM_SIMPLE_VECTOR_REF (pv, 1);
  178. stream->flush = SCM_SIMPLE_VECTOR_REF (pv, 2);
  179. stream->read_char = SCM_SIMPLE_VECTOR_REF (pv, 3);
  180. stream->close = SCM_SIMPLE_VECTOR_REF (pv, 4);
  181. stream->input_waiting =
  182. vlen == 6 ? SCM_SIMPLE_VECTOR_REF (pv, 5) : SCM_BOOL_F;
  183. return scm_c_make_port (scm_soft_port_type, scm_i_mode_bits (modes),
  184. (scm_t_bits) stream);
  185. }
  186. #undef FUNC_NAME
  187. static scm_t_port_type *
  188. scm_make_sfptob ()
  189. {
  190. scm_t_port_type *ptob = scm_make_port_type ("soft", soft_port_read,
  191. soft_port_write);
  192. scm_set_port_close (ptob, soft_port_close);
  193. scm_set_port_needs_close_on_gc (ptob, 1);
  194. scm_set_port_get_natural_buffer_sizes (ptob,
  195. soft_port_get_natural_buffer_sizes);
  196. scm_set_port_input_waiting (ptob, soft_port_input_waiting);
  197. return ptob;
  198. }
  199. void
  200. scm_init_vports ()
  201. {
  202. scm_soft_port_type = scm_make_sfptob ();
  203. #include "vports.x"
  204. }