Merge branch 'master' into boehm-demers-weiser-gc
[bpt/guile.git] / libguile / vports.c
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 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
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <errno.h>
27
28 #include "libguile/_scm.h"
29 #include "libguile/eval.h"
30 #include "libguile/chars.h"
31 #include "libguile/fports.h"
32 #include "libguile/root.h"
33 #include "libguile/strings.h"
34 #include "libguile/vectors.h"
35
36 #include "libguile/validate.h"
37 #include "libguile/vports.h"
38
39 #ifdef HAVE_STRING_H
40 #include <string.h>
41 #endif
42
43 \f
44
45 /* {Ports - soft ports}
46 *
47 */
48
49
50 static scm_t_bits scm_tc16_sfport;
51
52
53 static void
54 sf_flush (SCM port)
55 {
56 scm_t_port *pt = SCM_PTAB_ENTRY (port);
57 SCM stream = SCM_PACK (pt->stream);
58
59 if (pt->write_pos > pt->write_buf)
60 {
61 /* write the byte. */
62 scm_call_1 (SCM_SIMPLE_VECTOR_REF (stream, 0),
63 SCM_MAKE_CHAR (*pt->write_buf));
64 pt->write_pos = pt->write_buf;
65
66 /* flush the output. */
67 {
68 SCM f = SCM_SIMPLE_VECTOR_REF (stream, 2);
69
70 if (scm_is_true (f))
71 scm_call_0 (f);
72 }
73 }
74 }
75
76 static void
77 sf_write (SCM port, const void *data, size_t size)
78 {
79 SCM p = SCM_PACK (SCM_STREAM (port));
80
81 scm_call_1 (SCM_SIMPLE_VECTOR_REF (p, 1),
82 scm_from_locale_stringn ((char *) data, size));
83 }
84
85 /* calling the flush proc (element 2) is in case old code needs it,
86 but perhaps softports could the use port buffer in the same way as
87 fports. */
88
89 /* places a single char in the input buffer. */
90 static int
91 sf_fill_input (SCM port)
92 {
93 SCM p = SCM_PACK (SCM_STREAM (port));
94 SCM ans;
95 scm_t_port *pt;
96
97 ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */
98 if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
99 return EOF;
100 SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input");
101 pt = SCM_PTAB_ENTRY (port);
102
103 if (pt->encoding == NULL)
104 {
105 scm_t_port *pt = SCM_PTAB_ENTRY (port);
106
107 *pt->read_buf = SCM_CHAR (ans);
108 pt->read_pos = pt->read_buf;
109 pt->read_end = pt->read_buf + 1;
110 return *pt->read_buf;
111 }
112 else
113 scm_ungetc (SCM_CHAR (ans), port);
114 return SCM_CHAR (ans);
115 }
116
117
118 static int
119 sf_close (SCM port)
120 {
121 SCM p = SCM_PACK (SCM_STREAM (port));
122 SCM f = SCM_SIMPLE_VECTOR_REF (p, 4);
123 if (scm_is_false (f))
124 return 0;
125 f = scm_call_0 (f);
126 errno = 0;
127 return scm_is_false (f) ? EOF : 0;
128 }
129
130
131 static int
132 sf_input_waiting (SCM port)
133 {
134 SCM p = SCM_PACK (SCM_STREAM (port));
135 if (SCM_SIMPLE_VECTOR_LENGTH (p) >= 6)
136 {
137 SCM f = SCM_SIMPLE_VECTOR_REF (p, 5);
138 if (scm_is_true (f))
139 return scm_to_int (scm_call_0 (f));
140 }
141 /* Default is such that char-ready? for soft ports returns #t, as it
142 did before this extension was implemented. */
143 return 1;
144 }
145
146
147
148 SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
149 (SCM pv, SCM modes),
150 "Return a port capable of receiving or delivering characters as\n"
151 "specified by the @var{modes} string (@pxref{File Ports,\n"
152 "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
153 "components are as follows:\n"
154 "\n"
155 "@enumerate 0\n"
156 "@item\n"
157 "procedure accepting one character for output\n"
158 "@item\n"
159 "procedure accepting a string for output\n"
160 "@item\n"
161 "thunk for flushing output\n"
162 "@item\n"
163 "thunk for getting one character\n"
164 "@item\n"
165 "thunk for closing port (not by garbage collection)\n"
166 "@item\n"
167 "(if present and not @code{#f}) thunk for computing the number of\n"
168 "characters that can be read from the port without blocking.\n"
169 "@end enumerate\n"
170 "\n"
171 "For an output-only port only elements 0, 1, 2, and 4 need be\n"
172 "procedures. For an input-only port only elements 3 and 4 need\n"
173 "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
174 "there is no useful operation for them to perform.\n"
175 "\n"
176 "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
177 "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
178 "Scheme}) it indicates that the port has reached end-of-file.\n"
179 "For example:\n"
180 "\n"
181 "@lisp\n"
182 "(define stdout (current-output-port))\n"
183 "(define p (make-soft-port\n"
184 " (vector\n"
185 " (lambda (c) (write c stdout))\n"
186 " (lambda (s) (display s stdout))\n"
187 " (lambda () (display \".\" stdout))\n"
188 " (lambda () (char-upcase (read-char)))\n"
189 " (lambda () (display \"@@\" stdout)))\n"
190 " \"rw\"))\n"
191 "\n"
192 "(write p p) @result{} #<input-output: soft 8081e20>\n"
193 "@end lisp")
194 #define FUNC_NAME s_scm_make_soft_port
195 {
196 int vlen;
197 scm_t_port *pt;
198 SCM z;
199
200 SCM_VALIDATE_VECTOR (1, pv);
201 vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
202 SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
203 SCM_VALIDATE_STRING (2, modes);
204
205 scm_i_scm_pthread_mutex_lock (&scm_i_port_table_mutex);
206 z = scm_new_port_table_entry (scm_tc16_sfport);
207 pt = SCM_PTAB_ENTRY (z);
208 scm_port_non_buffer (pt);
209 SCM_SET_CELL_TYPE (z, scm_tc16_sfport | scm_i_mode_bits (modes));
210
211 SCM_SETSTREAM (z, SCM_UNPACK (pv));
212 scm_i_pthread_mutex_unlock (&scm_i_port_table_mutex);
213 return z;
214 }
215 #undef FUNC_NAME
216
217
218 static scm_t_bits
219 scm_make_sfptob ()
220 {
221 scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
222
223 scm_set_port_mark (tc, scm_markstream);
224 scm_set_port_flush (tc, sf_flush);
225 scm_set_port_close (tc, sf_close);
226 scm_set_port_input_waiting (tc, sf_input_waiting);
227
228 return tc;
229 }
230
231 void
232 scm_init_vports ()
233 {
234 scm_tc16_sfport = scm_make_sfptob ();
235
236 #include "libguile/vports.x"
237 }
238
239 /*
240 Local Variables:
241 c-file-style: "gnu"
242 End:
243 */