* deprecated.h, boolean.h (SCM_FALSEP, SCM_NFALSEP, SCM_BOOL,
[bpt/guile.git] / libguile / vports.c
1 /* Copyright (C) 1995,1996,1998,1999,2000,2001, 2002, 2003 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18
19 \f
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <errno.h>
26
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"
34
35 #include "libguile/validate.h"
36 #include "libguile/vports.h"
37
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41
42 \f
43
44 /* {Ports - soft ports}
45 *
46 */
47
48
49 static scm_t_bits scm_tc16_sfport;
50
51
52 static void
53 sf_flush (SCM port)
54 {
55 scm_t_port *pt = SCM_PTAB_ENTRY (port);
56 SCM stream = SCM_PACK (pt->stream);
57
58 if (pt->write_pos > pt->write_buf)
59 {
60 /* write the byte. */
61 scm_call_1 (SCM_VELTS (stream)[0], SCM_MAKE_CHAR (*pt->write_buf));
62 pt->write_pos = pt->write_buf;
63
64 /* flush the output. */
65 {
66 SCM f = SCM_VELTS (stream)[2];
67
68 if (scm_is_true (f))
69 scm_call_0 (f);
70 }
71 }
72 }
73
74 static void
75 sf_write (SCM port, const void *data, size_t size)
76 {
77 SCM p = SCM_PACK (SCM_STREAM (port));
78
79 scm_call_1 (SCM_VELTS (p)[1], scm_mem2string ((char *) data, size));
80 }
81
82 /* calling the flush proc (element 2) is in case old code needs it,
83 but perhaps softports could the use port buffer in the same way as
84 fports. */
85
86 /* places a single char in the input buffer. */
87 static int
88 sf_fill_input (SCM port)
89 {
90 SCM p = SCM_PACK (SCM_STREAM (port));
91 SCM ans;
92
93 ans = scm_call_0 (SCM_VELTS (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 {
98 scm_t_port *pt = SCM_PTAB_ENTRY (port);
99
100 *pt->read_buf = SCM_CHAR (ans);
101 pt->read_pos = pt->read_buf;
102 pt->read_end = pt->read_buf + 1;
103 return *pt->read_buf;
104 }
105 }
106
107
108 static int
109 sf_close (SCM port)
110 {
111 SCM p = SCM_PACK (SCM_STREAM (port));
112 SCM f = SCM_VELTS (p)[4];
113 if (scm_is_false (f))
114 return 0;
115 f = scm_call_0 (f);
116 errno = 0;
117 return scm_is_false (f) ? EOF : 0;
118 }
119
120
121 static int
122 sf_input_waiting (SCM port)
123 {
124 SCM p = SCM_PACK (SCM_STREAM (port));
125 if (SCM_VECTOR_LENGTH (p) >= 6)
126 {
127 SCM f = SCM_VELTS (p)[5];
128 if (scm_is_true (f))
129 return scm_num2int (scm_call_0 (f), SCM_ARGn, NULL);
130 }
131 /* Default is such that char-ready? for soft ports returns #t, as it
132 did before this extension was implemented. */
133 return 1;
134 }
135
136
137
138 SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
139 (SCM pv, SCM modes),
140 "Return a port capable of receiving or delivering characters as\n"
141 "specified by the @var{modes} string (@pxref{File Ports,\n"
142 "open-file}). @var{pv} must be a vector of length 5 or 6. Its\n"
143 "components are as follows:\n"
144 "\n"
145 "@enumerate 0\n"
146 "@item\n"
147 "procedure accepting one character for output\n"
148 "@item\n"
149 "procedure accepting a string for output\n"
150 "@item\n"
151 "thunk for flushing output\n"
152 "@item\n"
153 "thunk for getting one character\n"
154 "@item\n"
155 "thunk for closing port (not by garbage collection)\n"
156 "@item\n"
157 "(if present and not @code{#f}) thunk for computing the number of\n"
158 "characters that can be read from the port without blocking.\n"
159 "@end enumerate\n"
160 "\n"
161 "For an output-only port only elements 0, 1, 2, and 4 need be\n"
162 "procedures. For an input-only port only elements 3 and 4 need\n"
163 "be procedures. Thunks 2 and 4 can instead be @code{#f} if\n"
164 "there is no useful operation for them to perform.\n"
165 "\n"
166 "If thunk 3 returns @code{#f} or an @code{eof-object}\n"
167 "(@pxref{Input, eof-object?, ,r5rs, The Revised^5 Report on\n"
168 "Scheme}) it indicates that the port has reached end-of-file.\n"
169 "For example:\n"
170 "\n"
171 "@lisp\n"
172 "(define stdout (current-output-port))\n"
173 "(define p (make-soft-port\n"
174 " (vector\n"
175 " (lambda (c) (write c stdout))\n"
176 " (lambda (s) (display s stdout))\n"
177 " (lambda () (display \".\" stdout))\n"
178 " (lambda () (char-upcase (read-char)))\n"
179 " (lambda () (display \"@@\" stdout)))\n"
180 " \"rw\"))\n"
181 "\n"
182 "(write p p) @result{} #<input-output: soft 8081e20>\n"
183 "@end lisp")
184 #define FUNC_NAME s_scm_make_soft_port
185 {
186 int vlen;
187 scm_t_port *pt;
188 SCM z;
189
190 SCM_VALIDATE_VECTOR (1, pv);
191 vlen = SCM_VECTOR_LENGTH (pv);
192 SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
193 SCM_VALIDATE_STRING (2, modes);
194
195 scm_mutex_lock (&scm_i_port_table_mutex);
196 z = scm_new_port_table_entry (scm_tc16_sfport);
197 pt = SCM_PTAB_ENTRY (z);
198 scm_port_non_buffer (pt);
199 SCM_SET_CELL_TYPE (z, scm_tc16_sfport | scm_mode_bits (SCM_STRING_CHARS (modes)));
200
201 SCM_SETSTREAM (z, SCM_UNPACK (pv));
202 scm_mutex_unlock (&scm_i_port_table_mutex);
203 return z;
204 }
205 #undef FUNC_NAME
206
207
208 static scm_t_bits
209 scm_make_sfptob ()
210 {
211 scm_t_bits tc = scm_make_port_type ("soft", sf_fill_input, sf_write);
212
213 scm_set_port_mark (tc, scm_markstream);
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 */