vports.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2006 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
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but 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 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/eval.h"
  24. #include "libguile/chars.h"
  25. #include "libguile/fports.h"
  26. #include "libguile/root.h"
  27. #include "libguile/strings.h"
  28. #include "libguile/vectors.h"
  29. #include "libguile/validate.h"
  30. #include "libguile/vports.h"
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. /* {Ports - soft ports}
  35. *
  36. */
  37. static scm_t_bits scm_tc16_sfport;
  38. static void
  39. sf_flush (SCM port)
  40. {
  41. scm_t_port *pt = SCM_PTAB_ENTRY (port);
  42. SCM stream = SCM_PACK (pt->stream);
  43. if (pt->write_pos > pt->write_buf)
  44. {
  45. /* write the byte. */
  46. scm_call_1 (SCM_SIMPLE_VECTOR_REF (stream, 0),
  47. SCM_MAKE_CHAR (*pt->write_buf));
  48. pt->write_pos = pt->write_buf;
  49. /* flush the output. */
  50. {
  51. SCM f = SCM_SIMPLE_VECTOR_REF (stream, 2);
  52. if (scm_is_true (f))
  53. scm_call_0 (f);
  54. }
  55. }
  56. }
  57. static void
  58. sf_write (SCM port, const void *data, size_t size)
  59. {
  60. SCM p = SCM_PACK (SCM_STREAM (port));
  61. scm_call_1 (SCM_SIMPLE_VECTOR_REF (p, 1),
  62. scm_from_locale_stringn ((char *) data, size));
  63. }
  64. /* calling the flush proc (element 2) is in case old code needs it,
  65. but perhaps softports could the use port buffer in the same way as
  66. fports. */
  67. /* places a single char in the input buffer. */
  68. static int
  69. sf_fill_input (SCM port)
  70. {
  71. SCM p = SCM_PACK (SCM_STREAM (port));
  72. SCM ans;
  73. ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */
  74. if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
  75. return EOF;
  76. SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input");
  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. }
  85. static int
  86. sf_close (SCM port)
  87. {
  88. SCM p = SCM_PACK (SCM_STREAM (port));
  89. SCM f = SCM_SIMPLE_VECTOR_REF (p, 4);
  90. if (scm_is_false (f))
  91. return 0;
  92. f = scm_call_0 (f);
  93. errno = 0;
  94. return scm_is_false (f) ? EOF : 0;
  95. }
  96. static int
  97. sf_input_waiting (SCM port)
  98. {
  99. SCM p = SCM_PACK (SCM_STREAM (port));
  100. if (SCM_SIMPLE_VECTOR_LENGTH (p) >= 6)
  101. {
  102. SCM f = SCM_SIMPLE_VECTOR_REF (p, 5);
  103. if (scm_is_true (f))
  104. return scm_to_int (scm_call_0 (f));
  105. }
  106. /* Default is such that char-ready? for soft ports returns #t, as it
  107. did before this extension was implemented. */
  108. return 1;
  109. }
  110. SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
  111. (SCM pv, SCM modes),
  112. "Return a port capable of receiving or delivering characters as\n"
  113. "specified by the @var{modes} string (@pxref{File Ports,\n"
  114. "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
  115. "components are as follows:\n"
  116. "\n"
  117. "@enumerate 0\n"
  118. "@item\n"
  119. "procedure accepting one character for output\n"
  120. "@item\n"
  121. "procedure accepting a string for output\n"
  122. "@item\n"
  123. "thunk for flushing output\n"
  124. "@item\n"
  125. "thunk for getting one character\n"
  126. "@item\n"
  127. "thunk for closing port (not by garbage collection)\n"
  128. "@item\n"
  129. "(if present and not @code{#f}) thunk for computing the number of\n"
  130. "characters that can be read from the port without blocking.\n"
  131. "@end enumerate\n"
  132. "\n"
  133. "For an output-only port only elements 0, 1, 2, and 4 need be\n"
  134. "procedures. For an input-only port only elements 3 and 4 need\n"
  135. "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
  136. "there is no useful operation for them to perform.\n"
  137. "\n"
  138. "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
  139. "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
  140. "Scheme}) it indicates that the port has reached end-of-file.\n"
  141. "For example:\n"
  142. "\n"
  143. "@lisp\n"
  144. "(define stdout (current-output-port))\n"
  145. "(define p (make-soft-port\n"
  146. " (vector\n"
  147. " (lambda (c) (write c stdout))\n"
  148. " (lambda (s) (display s stdout))\n"
  149. " (lambda () (display \".\" stdout))\n"
  150. " (lambda () (char-upcase (read-char)))\n"
  151. " (lambda () (display \"@@\" stdout)))\n"
  152. " \"rw\"))\n"
  153. "\n"
  154. "(write p p) @result{} #<input-output: soft 8081e20>\n"
  155. "@end lisp")
  156. #define FUNC_NAME s_scm_make_soft_port
  157. {
  158. int vlen;
  159. scm_t_port *pt;
  160. SCM z;
  161. SCM_VALIDATE_VECTOR (1, pv);
  162. vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
  163. SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
  164. SCM_VALIDATE_STRING (2, modes);
  165. scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
  166. z = scm_new_port_table_entry (scm_tc16_sfport);
  167. pt = SCM_PTAB_ENTRY (z);
  168. scm_port_non_buffer (pt);
  169. SCM_SET_CELL_TYPE (z, scm_tc16_sfport | scm_i_mode_bits (modes));
  170. SCM_SETSTREAM (z, SCM_UNPACK (pv));
  171. scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
  172. return z;
  173. }
  174. #undef FUNC_NAME
  175. static scm_t_bits
  176. scm_make_sfptob ()
  177. {
  178. scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
  179. scm_set_port_mark (tc, scm_markstream);
  180. scm_set_port_flush (tc, sf_flush);
  181. scm_set_port_close (tc, sf_close);
  182. scm_set_port_input_waiting (tc, sf_input_waiting);
  183. return tc;
  184. }
  185. void
  186. scm_init_vports ()
  187. {
  188. scm_tc16_sfport = scm_make_sfptob ();
  189. #include "libguile/vports.x"
  190. }
  191. /*
  192. Local Variables:
  193. c-file-style: "gnu"
  194. End:
  195. */