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