vports.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_port_internal *pti;
  71. ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */
  72. if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
  73. return EOF;
  74. SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input");
  75. pti = SCM_PORT_GET_INTERNAL (port);
  76. if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1)
  77. {
  78. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  79. *pt->read_buf = SCM_CHAR (ans);
  80. pt->read_pos = pt->read_buf;
  81. pt->read_end = pt->read_buf + 1;
  82. return *pt->read_buf;
  83. }
  84. else
  85. scm_ungetc_unlocked (SCM_CHAR (ans), port);
  86. return SCM_CHAR (ans);
  87. }
  88. static int
  89. sf_close (SCM port)
  90. {
  91. SCM p = SCM_PACK (SCM_STREAM (port));
  92. SCM f = SCM_SIMPLE_VECTOR_REF (p, 4);
  93. if (scm_is_false (f))
  94. return 0;
  95. f = scm_call_0 (f);
  96. errno = 0;
  97. return scm_is_false (f) ? EOF : 0;
  98. }
  99. static int
  100. sf_input_waiting (SCM port)
  101. {
  102. SCM p = SCM_PACK (SCM_STREAM (port));
  103. if (SCM_SIMPLE_VECTOR_LENGTH (p) >= 6)
  104. {
  105. SCM f = SCM_SIMPLE_VECTOR_REF (p, 5);
  106. if (scm_is_true (f))
  107. return scm_to_int (scm_call_0 (f));
  108. }
  109. /* Default is such that char-ready? for soft ports returns #t, as it
  110. did before this extension was implemented. */
  111. return 1;
  112. }
  113. SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
  114. (SCM pv, SCM modes),
  115. "Return a port capable of receiving or delivering characters as\n"
  116. "specified by the @var{modes} string (@pxref{File Ports,\n"
  117. "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
  118. "components are as follows:\n"
  119. "\n"
  120. "@enumerate 0\n"
  121. "@item\n"
  122. "procedure accepting one character for output\n"
  123. "@item\n"
  124. "procedure accepting a string for output\n"
  125. "@item\n"
  126. "thunk for flushing output\n"
  127. "@item\n"
  128. "thunk for getting one character\n"
  129. "@item\n"
  130. "thunk for closing port (not by garbage collection)\n"
  131. "@item\n"
  132. "(if present and not @code{#f}) thunk for computing the number of\n"
  133. "characters that can be read from the port without blocking.\n"
  134. "@end enumerate\n"
  135. "\n"
  136. "For an output-only port only elements 0, 1, 2, and 4 need be\n"
  137. "procedures. For an input-only port only elements 3 and 4 need\n"
  138. "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
  139. "there is no useful operation for them to perform.\n"
  140. "\n"
  141. "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
  142. "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
  143. "Scheme}) it indicates that the port has reached end-of-file.\n"
  144. "For example:\n"
  145. "\n"
  146. "@lisp\n"
  147. "(define stdout (current-output-port))\n"
  148. "(define p (make-soft-port\n"
  149. " (vector\n"
  150. " (lambda (c) (write c stdout))\n"
  151. " (lambda (s) (display s stdout))\n"
  152. " (lambda () (display \".\" stdout))\n"
  153. " (lambda () (char-upcase (read-char)))\n"
  154. " (lambda () (display \"@@\" stdout)))\n"
  155. " \"rw\"))\n"
  156. "\n"
  157. "(write p p) @result{} #<input-output: soft 8081e20>\n"
  158. "@end lisp")
  159. #define FUNC_NAME s_scm_make_soft_port
  160. {
  161. int vlen;
  162. SCM z;
  163. SCM_VALIDATE_VECTOR (1, pv);
  164. vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
  165. SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
  166. SCM_VALIDATE_STRING (2, modes);
  167. z = scm_c_make_port (scm_tc16_sfport, scm_i_mode_bits (modes),
  168. SCM_UNPACK (pv));
  169. scm_port_non_buffer (SCM_PTAB_ENTRY (z));
  170. return z;
  171. }
  172. #undef FUNC_NAME
  173. static scm_t_bits
  174. scm_make_sfptob ()
  175. {
  176. scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
  177. scm_set_port_flush (tc, sf_flush);
  178. scm_set_port_close (tc, sf_close);
  179. scm_set_port_input_waiting (tc, sf_input_waiting);
  180. return tc;
  181. }
  182. void
  183. scm_init_vports ()
  184. {
  185. scm_tc16_sfport = scm_make_sfptob ();
  186. #include "libguile/vports.x"
  187. }
  188. /*
  189. Local Variables:
  190. c-file-style: "gnu"
  191. End:
  192. */