Commit | Line | Data |
---|---|---|
f2c9fcb0 | 1 | /* Copyright (C) 1995,1996,1998,1999, 2000 Free Software Foundation, Inc. |
0f2d19dd JB |
2 | * |
3 | * This program is free software; you can redistribute it and/or modify | |
4 | * it under the terms of the GNU General Public License as published by | |
5 | * the Free Software Foundation; either version 2, or (at your option) | |
6 | * any later version. | |
7 | * | |
8 | * This program 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 | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this software; see the file COPYING. If not, write to | |
82892bed JB |
15 | * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, |
16 | * Boston, MA 02111-1307 USA | |
0f2d19dd JB |
17 | * |
18 | * As a special exception, the Free Software Foundation gives permission | |
19 | * for additional uses of the text contained in its release of GUILE. | |
20 | * | |
21 | * The exception is that, if you link the GUILE library with other files | |
22 | * to produce an executable, this does not by itself cause the | |
23 | * resulting executable to be covered by the GNU General Public License. | |
24 | * Your use of that executable is in no way restricted on account of | |
25 | * linking the GUILE library code into it. | |
26 | * | |
27 | * This exception does not however invalidate any other reasons why | |
28 | * the executable file might be covered by the GNU General Public License. | |
29 | * | |
30 | * This exception applies only to the code released by the | |
31 | * Free Software Foundation under the name GUILE. If you copy | |
32 | * code from other Free Software Foundation releases into a copy of | |
33 | * GUILE, as the General Public License permits, the exception does | |
34 | * not apply to the code that you add in this way. To avoid misleading | |
35 | * anyone as to the status of such modified files, you must delete | |
36 | * this exception notice from them. | |
37 | * | |
38 | * If you write modifications of your own for GUILE, it is your choice | |
39 | * whether to permit this exception to apply to your modifications. | |
82892bed | 40 | * If you do not wish that, delete this exception notice. */ |
1bbd0b84 GB |
41 | |
42 | /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999, | |
43 | gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */ | |
44 | ||
0f2d19dd JB |
45 | \f |
46 | ||
47 | #include <stdio.h> | |
a0599745 MD |
48 | #include "libguile/_scm.h" |
49 | #include "libguile/eval.h" | |
50 | #include "libguile/chars.h" | |
51 | #include "libguile/fports.h" | |
52 | #include "libguile/root.h" | |
53 | #include "libguile/strings.h" | |
54 | #include "libguile/vectors.h" | |
20e6290e | 55 | |
a0599745 MD |
56 | #include "libguile/validate.h" |
57 | #include "libguile/vports.h" | |
0f2d19dd | 58 | |
95b88819 GH |
59 | #ifdef HAVE_STRING_H |
60 | #include <string.h> | |
61 | #endif | |
62 | ||
0f2d19dd JB |
63 | \f |
64 | ||
65 | /* {Ports - soft ports} | |
66 | * | |
67 | */ | |
68 | ||
69 | ||
a98bddfd DH |
70 | static scm_bits_t scm_tc16_sfport; |
71 | ||
72 | ||
ee149d03 | 73 | static void |
affc96b5 | 74 | sf_flush (SCM port) |
0f2d19dd | 75 | { |
840ae05d | 76 | scm_port *pt = SCM_PTAB_ENTRY (port); |
74a16888 | 77 | SCM stream = SCM_PACK (pt->stream); |
ee149d03 JB |
78 | |
79 | if (pt->write_pos > pt->write_buf) | |
80 | { | |
840ae05d | 81 | /* write the byte. */ |
7866a09b | 82 | scm_apply (SCM_VELTS (stream)[0], SCM_MAKE_CHAR (*pt->write_buf), |
ee149d03 JB |
83 | scm_listofnull); |
84 | pt->write_pos = pt->write_buf; | |
85 | ||
86 | /* flush the output. */ | |
87 | { | |
88 | SCM f = SCM_VELTS (stream)[2]; | |
89 | ||
4260a7fc | 90 | if (!SCM_FALSEP (f)) |
ee149d03 JB |
91 | scm_apply (f, SCM_EOL, SCM_EOL); |
92 | } | |
93 | } | |
0f2d19dd JB |
94 | } |
95 | ||
31703ab8 | 96 | static void |
8aa011a1 | 97 | sf_write (SCM port, const void *data, size_t size) |
31703ab8 | 98 | { |
74a16888 | 99 | SCM p = SCM_PACK (SCM_STREAM (port)); |
31703ab8 | 100 | |
1d7bdb25 GH |
101 | scm_apply (SCM_VELTS (p)[1], |
102 | scm_cons (scm_makfromstr ((char *) data, size, 0), SCM_EOL), | |
31703ab8 GH |
103 | SCM_EOL); |
104 | } | |
1cc91f1b | 105 | |
ee149d03 JB |
106 | /* calling the flush proc (element 2) is in case old code needs it, |
107 | but perhaps softports could the use port buffer in the same way as | |
108 | fports. */ | |
109 | ||
affc96b5 | 110 | /* places a single char in the input buffer. */ |
0f2d19dd | 111 | static int |
affc96b5 | 112 | sf_fill_input (SCM port) |
0f2d19dd | 113 | { |
74a16888 | 114 | SCM p = SCM_PACK (SCM_STREAM (port)); |
0f2d19dd | 115 | SCM ans; |
ee149d03 JB |
116 | |
117 | ans = scm_apply (SCM_VELTS (p)[3], SCM_EOL, SCM_EOL); /* get char. */ | |
0c32d76c | 118 | if (SCM_FALSEP (ans) || SCM_EOF_OBJECT_P (ans)) |
0f2d19dd | 119 | return EOF; |
7866a09b | 120 | SCM_ASSERT (SCM_CHARP (ans), ans, SCM_ARG1, "sf_fill_input"); |
5c070ca7 GH |
121 | { |
122 | scm_port *pt = SCM_PTAB_ENTRY (port); | |
123 | ||
7866a09b | 124 | *pt->read_buf = SCM_CHAR (ans); |
5c070ca7 GH |
125 | pt->read_pos = pt->read_buf; |
126 | pt->read_end = pt->read_buf + 1; | |
127 | return *pt->read_buf; | |
128 | } | |
0f2d19dd JB |
129 | } |
130 | ||
1cc91f1b | 131 | |
0f2d19dd | 132 | static int |
affc96b5 | 133 | sf_close (SCM port) |
0f2d19dd | 134 | { |
74a16888 | 135 | SCM p = SCM_PACK (SCM_STREAM (port)); |
0f2d19dd | 136 | SCM f = SCM_VELTS (p)[4]; |
4260a7fc | 137 | if (SCM_FALSEP (f)) |
0f2d19dd JB |
138 | return 0; |
139 | f = scm_apply (f, SCM_EOL, SCM_EOL); | |
140 | errno = 0; | |
4260a7fc | 141 | return SCM_FALSEP (f) ? EOF : 0; |
0f2d19dd JB |
142 | } |
143 | ||
144 | ||
145 | ||
3b3b36dd | 146 | SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0, |
1bbd0b84 | 147 | (SCM pv, SCM modes), |
d3818c29 MD |
148 | "Returns a port capable of receiving or delivering characters as\n" |
149 | "specified by the @var{modes} string (@pxref{File Ports,\n" | |
150 | "open-file}). @var{vector} must be a vector of length 6. Its components\n" | |
151 | "are as follows:\n\n" | |
152 | "@enumerate 0\n" | |
153 | "@item\n" | |
154 | "procedure accepting one character for output\n" | |
155 | "@item\n" | |
156 | "procedure accepting a string for output\n" | |
157 | "@item\n" | |
158 | "thunk for flushing output\n" | |
159 | "@item\n" | |
160 | "thunk for getting one character\n" | |
161 | "@item\n" | |
162 | "thunk for closing port (not by garbage collection)\n" | |
163 | "@end enumerate\n\n" | |
164 | "For an output-only port only elements 0, 1, 2, and 4 need be\n" | |
165 | "procedures. For an input-only port only elements 3 and 4 need be\n" | |
166 | "procedures. Thunks 2 and 4 can instead be @code{#f} if there is no useful\n" | |
167 | "operation for them to perform.\n\n" | |
168 | "If thunk 3 returns @code{#f} or an @code{eof-object} (@pxref{Input,\n" | |
169 | "eof-object?, ,r4rs, The Revised^4 Report on Scheme}) it indicates that\n" | |
170 | "the port has reached end-of-file. For example:\n\n" | |
171 | "@example\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\n" | |
ee826bae | 181 | "(write p p) @result{} #<input-output: soft 8081e20>\n" |
d3818c29 | 182 | "@end example") |
1bbd0b84 | 183 | #define FUNC_NAME s_scm_make_soft_port |
0f2d19dd | 184 | { |
840ae05d | 185 | scm_port *pt; |
0f2d19dd | 186 | SCM z; |
3b3b36dd | 187 | SCM_VALIDATE_VECTOR_LEN (1,pv,5); |
a6d9e5ab DH |
188 | SCM_VALIDATE_STRING (2, modes); |
189 | SCM_STRING_COERCE_0TERMINATION_X (modes); | |
0f2d19dd JB |
190 | SCM_NEWCELL (z); |
191 | SCM_DEFER_INTS; | |
192 | pt = scm_add_to_port_table (z); | |
70df8af6 | 193 | scm_port_non_buffer (pt); |
a6d9e5ab | 194 | SCM_SET_CELL_TYPE (z, scm_tc16_sfport | scm_mode_bits (SCM_STRING_CHARS (modes))); |
0f2d19dd | 195 | SCM_SETPTAB_ENTRY (z, pt); |
74a16888 | 196 | SCM_SETSTREAM (z, SCM_UNPACK (pv)); |
0f2d19dd JB |
197 | SCM_ALLOW_INTS; |
198 | return z; | |
199 | } | |
1bbd0b84 | 200 | #undef FUNC_NAME |
0f2d19dd | 201 | |
1cc91f1b | 202 | |
a98bddfd | 203 | static scm_bits_t |
10efca5b | 204 | scm_make_sfptob () |
0f2d19dd | 205 | { |
a98bddfd DH |
206 | scm_bits_t tc = scm_make_port_type ("soft", sf_fill_input, sf_write); |
207 | ||
6c747373 | 208 | scm_set_port_mark (tc, scm_markstream); |
affc96b5 GH |
209 | scm_set_port_flush (tc, sf_flush); |
210 | scm_set_port_close (tc, sf_close); | |
a98bddfd DH |
211 | |
212 | return tc; | |
10efca5b | 213 | } |
1cc91f1b | 214 | |
0f2d19dd JB |
215 | void |
216 | scm_init_vports () | |
0f2d19dd | 217 | { |
a98bddfd DH |
218 | scm_tc16_sfport = scm_make_sfptob (); |
219 | ||
8dc9439f | 220 | #ifndef SCM_MAGIC_SNARFER |
a0599745 | 221 | #include "libguile/vports.x" |
8dc9439f | 222 | #endif |
0f2d19dd | 223 | } |
89e00824 ML |
224 | |
225 | /* | |
226 | Local Variables: | |
227 | c-file-style: "gnu" | |
228 | End: | |
229 | */ |