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