Fix infinite loop in expander
[bpt/guile.git] / libguile / vports.c
CommitLineData
d1b9f8ac 1/* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003, 2006, 2009, 2010, 2011, 2013 Free Software Foundation, Inc.
0f2d19dd 2 *
73be1d9e 3 * This library is free software; you can redistribute it and/or
53befeb7
NJ
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.
0f2d19dd 7 *
53befeb7
NJ
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
0f2d19dd 12 *
73be1d9e
MV
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
53befeb7
NJ
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
73be1d9e 17 */
1bbd0b84 18
1bbd0b84 19
0f2d19dd 20\f
dbb605f5 21#ifdef HAVE_CONFIG_H
fed43b8f
RB
22# include <config.h>
23#endif
0f2d19dd
JB
24
25#include <stdio.h>
e6e2e95a
MD
26#include <errno.h>
27
a0599745
MD
28#include "libguile/_scm.h"
29#include "libguile/eval.h"
30#include "libguile/chars.h"
f6f4feb0
MW
31#include "libguile/ports.h"
32#include "libguile/ports-internal.h"
a0599745
MD
33#include "libguile/fports.h"
34#include "libguile/root.h"
35#include "libguile/strings.h"
36#include "libguile/vectors.h"
20e6290e 37
a0599745
MD
38#include "libguile/validate.h"
39#include "libguile/vports.h"
0f2d19dd 40
95b88819
GH
41#ifdef HAVE_STRING_H
42#include <string.h>
43#endif
44
0f2d19dd
JB
45\f
46
47/* {Ports - soft ports}
48 *
49 */
50
51
92c2555f 52static scm_t_bits scm_tc16_sfport;
a98bddfd
DH
53
54
ee149d03 55static void
affc96b5 56sf_flush (SCM port)
0f2d19dd 57{
92c2555f 58 scm_t_port *pt = SCM_PTAB_ENTRY (port);
74a16888 59 SCM stream = SCM_PACK (pt->stream);
ee149d03 60
be79627c
IP
61 SCM f = SCM_SIMPLE_VECTOR_REF (stream, 2);
62
63 if (scm_is_true (f))
64 scm_call_0 (f);
65
0f2d19dd
JB
66}
67
31703ab8 68static void
8aa011a1 69sf_write (SCM port, const void *data, size_t size)
31703ab8 70{
74a16888 71 SCM p = SCM_PACK (SCM_STREAM (port));
31703ab8 72
bf08e10f
LC
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. */
4057a3e0
MV
77 scm_call_1 (SCM_SIMPLE_VECTOR_REF (p, 1),
78 scm_from_locale_stringn ((char *) data, size));
31703ab8 79}
1cc91f1b 80
ee149d03
JB
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
affc96b5 85/* places a single char in the input buffer. */
0f2d19dd 86static int
affc96b5 87sf_fill_input (SCM port)
0f2d19dd 88{
74a16888 89 SCM p = SCM_PACK (SCM_STREAM (port));
0f2d19dd 90 SCM ans;
3c01acbc 91 scm_t_wchar c;
f6f4feb0 92 scm_t_port_internal *pti;
ee149d03 93
4057a3e0 94 ans = scm_call_0 (SCM_SIMPLE_VECTOR_REF (p, 3)); /* get char. */
7888309b 95 if (scm_is_false (ans) || SCM_EOF_OBJECT_P (ans))
0f2d19dd 96 return EOF;
7866a09b 97 SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input");
f6f4feb0 98 pti = SCM_PORT_GET_INTERNAL (port);
75192345 99
3c01acbc
AW
100 c = SCM_CHAR (ans);
101
102 if (pti->encoding_mode == SCM_PORT_ENCODING_MODE_LATIN1
103 || (pti->encoding_mode == SCM_PORT_ENCODING_MODE_UTF8 && c < 0xff))
75192345
MG
104 {
105 scm_t_port *pt = SCM_PTAB_ENTRY (port);
106
3c01acbc 107 *pt->read_buf = c;
75192345
MG
108 pt->read_pos = pt->read_buf;
109 pt->read_end = pt->read_buf + 1;
75192345
MG
110 }
111 else
3c01acbc
AW
112 {
113 long line = SCM_LINUM (port);
114 int column = SCM_COL (port);
115
116 scm_ungetc_unlocked (c, port);
117
118 SCM_LINUM (port) = line;
119 SCM_COL (port) = column;
120 }
121
122 return c;
0f2d19dd
JB
123}
124
1cc91f1b 125
0f2d19dd 126static int
affc96b5 127sf_close (SCM port)
0f2d19dd 128{
74a16888 129 SCM p = SCM_PACK (SCM_STREAM (port));
4057a3e0 130 SCM f = SCM_SIMPLE_VECTOR_REF (p, 4);
7888309b 131 if (scm_is_false (f))
0f2d19dd 132 return 0;
fdc28395 133 f = scm_call_0 (f);
0f2d19dd 134 errno = 0;
7888309b 135 return scm_is_false (f) ? EOF : 0;
0f2d19dd
JB
136}
137
138
34010f56
NJ
139static int
140sf_input_waiting (SCM port)
141{
142 SCM p = SCM_PACK (SCM_STREAM (port));
4057a3e0 143 if (SCM_SIMPLE_VECTOR_LENGTH (p) >= 6)
34010f56 144 {
4057a3e0 145 SCM f = SCM_SIMPLE_VECTOR_REF (p, 5);
7888309b 146 if (scm_is_true (f))
b9bd8526 147 return scm_to_int (scm_call_0 (f));
34010f56
NJ
148 }
149 /* Default is such that char-ready? for soft ports returns #t, as it
150 did before this extension was implemented. */
151 return 1;
152}
153
154
0f2d19dd 155
3b3b36dd 156SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
1bbd0b84 157 (SCM pv, SCM modes),
1e6808ea 158 "Return a port capable of receiving or delivering characters as\n"
d3818c29 159 "specified by the @var{modes} string (@pxref{File Ports,\n"
34010f56 160 "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
1e6808ea
MG
161 "components are as follows:\n"
162 "\n"
d3818c29
MD
163 "@enumerate 0\n"
164 "@item\n"
165 "procedure accepting one character for output\n"
166 "@item\n"
167 "procedure accepting a string for output\n"
168 "@item\n"
169 "thunk for flushing output\n"
170 "@item\n"
171 "thunk for getting one character\n"
172 "@item\n"
173 "thunk for closing port (not by garbage collection)\n"
34010f56
NJ
174 "@item\n"
175 "(if present and not @code{#f}) thunk for computing the number of\n"
176 "characters that can be read from the port without blocking.\n"
1e6808ea
MG
177 "@end enumerate\n"
178 "\n"
d3818c29 179 "For an output-only port only elements 0, 1, 2, and 4 need be\n"
1e6808ea
MG
180 "procedures. For an input-only port only elements 3 and 4 need\n"
181 "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
182 "there is no useful operation for them to perform.\n"
183 "\n"
184 "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
7a095584 185 "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
1e6808ea
MG
186 "Scheme}) it indicates that the port has reached end-of-file.\n"
187 "For example:\n"
188 "\n"
189 "@lisp\n"
d3818c29
MD
190 "(define stdout (current-output-port))\n"
191 "(define p (make-soft-port\n"
192 " (vector\n"
193 " (lambda (c) (write c stdout))\n"
194 " (lambda (s) (display s stdout))\n"
195 " (lambda () (display \".\" stdout))\n"
196 " (lambda () (char-upcase (read-char)))\n"
197 " (lambda () (display \"@@\" stdout)))\n"
1e6808ea
MG
198 " \"rw\"))\n"
199 "\n"
ee826bae 200 "(write p p) @result{} #<input-output: soft 8081e20>\n"
1e6808ea 201 "@end lisp")
1bbd0b84 202#define FUNC_NAME s_scm_make_soft_port
0f2d19dd 203{
34010f56 204 int vlen;
0f2d19dd 205 SCM z;
34010f56
NJ
206
207 SCM_VALIDATE_VECTOR (1, pv);
4057a3e0 208 vlen = SCM_SIMPLE_VECTOR_LENGTH (pv);
34010f56 209 SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
a6d9e5ab 210 SCM_VALIDATE_STRING (2, modes);
402788a9 211
2721f918
AW
212 z = scm_c_make_port (scm_tc16_sfport, scm_i_mode_bits (modes),
213 SCM_UNPACK (pv));
214 scm_port_non_buffer (SCM_PTAB_ENTRY (z));
215
0f2d19dd
JB
216 return z;
217}
1bbd0b84 218#undef FUNC_NAME
0f2d19dd 219
1cc91f1b 220
92c2555f 221static scm_t_bits
10efca5b 222scm_make_sfptob ()
0f2d19dd 223{
92c2555f 224 scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
a98bddfd 225
affc96b5
GH
226 scm_set_port_flush (tc, sf_flush);
227 scm_set_port_close (tc, sf_close);
34010f56 228 scm_set_port_input_waiting (tc, sf_input_waiting);
a98bddfd
DH
229
230 return tc;
10efca5b 231}
1cc91f1b 232
0f2d19dd
JB
233void
234scm_init_vports ()
0f2d19dd 235{
a98bddfd
DH
236 scm_tc16_sfport = scm_make_sfptob ();
237
a0599745 238#include "libguile/vports.x"
0f2d19dd 239}
89e00824
ML
240
241/*
242 Local Variables:
243 c-file-style: "gnu"
244 End:
245*/