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