Commit | Line | Data |
---|---|---|
b9ad392e | 1 | /* Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2003 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 | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
1bbd0b84 | 17 | |
1bbd0b84 | 18 | |
0f2d19dd | 19 | \f |
d68fee48 JB |
20 | /* Headers. */ |
21 | ||
bd515f37 RB |
22 | #if HAVE_CONFIG_H |
23 | # include <config.h> | |
24 | #endif | |
25 | ||
0f2d19dd | 26 | #include <stdio.h> |
e6e2e95a MD |
27 | #include <errno.h> |
28 | ||
a0599745 | 29 | #include "libguile/_scm.h" |
f0942910 | 30 | #include "libguile/eval.h" |
a0599745 MD |
31 | #include "libguile/objects.h" |
32 | #include "libguile/smob.h" | |
33 | #include "libguile/chars.h" | |
0f2d19dd | 34 | |
a0599745 MD |
35 | #include "libguile/keywords.h" |
36 | #include "libguile/root.h" | |
37 | #include "libguile/strings.h" | |
20e6290e | 38 | |
a0599745 MD |
39 | #include "libguile/validate.h" |
40 | #include "libguile/ports.h" | |
0f2d19dd | 41 | |
bd9e24b3 GH |
42 | #ifdef HAVE_STRING_H |
43 | #include <string.h> | |
44 | #endif | |
45 | ||
0f2d19dd | 46 | #ifdef HAVE_MALLOC_H |
95b88819 | 47 | #include <malloc.h> |
0f2d19dd JB |
48 | #endif |
49 | ||
ec65f5da MV |
50 | #ifdef HAVE_IO_H |
51 | #include <io.h> | |
52 | #endif | |
53 | ||
0f2d19dd JB |
54 | #ifdef HAVE_UNISTD_H |
55 | #include <unistd.h> | |
56 | #endif | |
57 | ||
95b88819 GH |
58 | #ifdef HAVE_SYS_IOCTL_H |
59 | #include <sys/ioctl.h> | |
60 | #endif | |
d68fee48 | 61 | |
82893676 MG |
62 | #ifdef __MINGW32__ |
63 | #include <fcntl.h> | |
64 | #define ftruncate(fd, size) chsize (fd, size) | |
65 | #endif | |
66 | ||
0f2d19dd | 67 | \f |
d68fee48 | 68 | /* The port kind table --- a dynamically resized array of port types. */ |
0f2d19dd JB |
69 | |
70 | ||
71 | /* scm_ptobs scm_numptob | |
72 | * implement a dynamicly resized array of ptob records. | |
73 | * Indexes into this table are used when generating type | |
74 | * tags for smobjects (if you know a tag you can get an index and conversely). | |
75 | */ | |
92c2555f | 76 | scm_t_ptob_descriptor *scm_ptobs; |
c014a02e | 77 | long scm_numptob; |
0f2d19dd | 78 | |
ee149d03 | 79 | /* GC marker for a port with stream of SCM type. */ |
0f2d19dd | 80 | SCM |
a284e297 | 81 | scm_markstream (SCM ptr) |
0f2d19dd JB |
82 | { |
83 | int openp; | |
f9a64404 | 84 | openp = SCM_CELL_WORD_0 (ptr) & SCM_OPN; |
0f2d19dd | 85 | if (openp) |
74a16888 | 86 | return SCM_PACK (SCM_STREAM (ptr)); |
0f2d19dd JB |
87 | else |
88 | return SCM_BOOL_F; | |
89 | } | |
90 | ||
f12733c9 | 91 | /* |
f12733c9 | 92 | * We choose to use an interface similar to the smob interface with |
affc96b5 | 93 | * fill_input and write as standard fields, passed to the port |
f12733c9 MD |
94 | * type constructor, and optional fields set by setters. |
95 | */ | |
96 | ||
70df8af6 | 97 | static void |
e81d98ec | 98 | flush_port_default (SCM port SCM_UNUSED) |
70df8af6 GH |
99 | { |
100 | } | |
101 | ||
102 | static void | |
e81d98ec | 103 | end_input_default (SCM port SCM_UNUSED, int offset SCM_UNUSED) |
70df8af6 GH |
104 | { |
105 | } | |
0f2d19dd | 106 | |
4c9419ac MV |
107 | static size_t |
108 | scm_port_free0 (SCM port) | |
109 | { | |
110 | return 0; | |
111 | } | |
112 | ||
92c2555f | 113 | scm_t_bits |
f12733c9 | 114 | scm_make_port_type (char *name, |
affc96b5 | 115 | int (*fill_input) (SCM port), |
8aa011a1 | 116 | void (*write) (SCM port, const void *data, size_t size)) |
0f2d19dd JB |
117 | { |
118 | char *tmp; | |
119 | if (255 <= scm_numptob) | |
120 | goto ptoberr; | |
f12733c9 MD |
121 | SCM_DEFER_INTS; |
122 | SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs, | |
123 | (1 + scm_numptob) | |
92c2555f | 124 | * sizeof (scm_t_ptob_descriptor))); |
0f2d19dd JB |
125 | if (tmp) |
126 | { | |
92c2555f | 127 | scm_ptobs = (scm_t_ptob_descriptor *) tmp; |
affc96b5 | 128 | |
f12733c9 MD |
129 | scm_ptobs[scm_numptob].name = name; |
130 | scm_ptobs[scm_numptob].mark = 0; | |
4c9419ac | 131 | scm_ptobs[scm_numptob].free = scm_port_free0; |
f12733c9 MD |
132 | scm_ptobs[scm_numptob].print = scm_port_print; |
133 | scm_ptobs[scm_numptob].equalp = 0; | |
affc96b5 GH |
134 | scm_ptobs[scm_numptob].close = 0; |
135 | ||
136 | scm_ptobs[scm_numptob].write = write; | |
70df8af6 | 137 | scm_ptobs[scm_numptob].flush = flush_port_default; |
affc96b5 | 138 | |
70df8af6 | 139 | scm_ptobs[scm_numptob].end_input = end_input_default; |
affc96b5 GH |
140 | scm_ptobs[scm_numptob].fill_input = fill_input; |
141 | scm_ptobs[scm_numptob].input_waiting = 0; | |
142 | ||
f12733c9 | 143 | scm_ptobs[scm_numptob].seek = 0; |
affc96b5 GH |
144 | scm_ptobs[scm_numptob].truncate = 0; |
145 | ||
0f2d19dd JB |
146 | scm_numptob++; |
147 | } | |
f12733c9 | 148 | SCM_ALLOW_INTS; |
0f2d19dd | 149 | if (!tmp) |
2500356c DH |
150 | { |
151 | ptoberr: | |
152 | scm_memory_error ("scm_make_port_type"); | |
153 | } | |
f12733c9 MD |
154 | /* Make a class object if Goops is present */ |
155 | if (scm_port_class) | |
156 | scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1)); | |
0f2d19dd JB |
157 | return scm_tc7_port + (scm_numptob - 1) * 256; |
158 | } | |
159 | ||
f12733c9 | 160 | void |
6c747373 | 161 | scm_set_port_mark (long tc, SCM (*mark) (SCM)) |
f12733c9 MD |
162 | { |
163 | scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark; | |
164 | } | |
165 | ||
166 | void | |
1be6b49c | 167 | scm_set_port_free (long tc, size_t (*free) (SCM)) |
f12733c9 MD |
168 | { |
169 | scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free; | |
170 | } | |
171 | ||
172 | void | |
6c747373 | 173 | scm_set_port_print (long tc, int (*print) (SCM exp, SCM port, |
f12733c9 MD |
174 | scm_print_state *pstate)) |
175 | { | |
176 | scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print; | |
177 | } | |
178 | ||
179 | void | |
6c747373 | 180 | scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM)) |
f12733c9 MD |
181 | { |
182 | scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp; | |
183 | } | |
184 | ||
31703ab8 | 185 | void |
affc96b5 | 186 | scm_set_port_flush (long tc, void (*flush) (SCM port)) |
31703ab8 | 187 | { |
affc96b5 | 188 | scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush; |
31703ab8 GH |
189 | } |
190 | ||
f12733c9 | 191 | void |
affc96b5 | 192 | scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset)) |
f12733c9 | 193 | { |
affc96b5 | 194 | scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input; |
f12733c9 MD |
195 | } |
196 | ||
197 | void | |
6c747373 | 198 | scm_set_port_close (long tc, int (*close) (SCM)) |
f12733c9 | 199 | { |
affc96b5 | 200 | scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close; |
f12733c9 MD |
201 | } |
202 | ||
203 | void | |
6c747373 | 204 | scm_set_port_seek (long tc, off_t (*seek) (SCM port, |
f12733c9 MD |
205 | off_t OFFSET, |
206 | int WHENCE)) | |
207 | { | |
208 | scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek; | |
209 | } | |
210 | ||
211 | void | |
6c747373 | 212 | scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length)) |
f12733c9 | 213 | { |
affc96b5 | 214 | scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate; |
f12733c9 MD |
215 | } |
216 | ||
217 | void | |
affc96b5 | 218 | scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM)) |
f12733c9 | 219 | { |
affc96b5 | 220 | scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting; |
f12733c9 MD |
221 | } |
222 | ||
0f2d19dd | 223 | \f |
0f2d19dd | 224 | |
3b3b36dd | 225 | SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0, |
1e6808ea MG |
226 | (SCM port), |
227 | "Return @code{#t} if a character is ready on input @var{port}\n" | |
228 | "and return @code{#f} otherwise. If @code{char-ready?} returns\n" | |
229 | "@code{#t} then the next @code{read-char} operation on\n" | |
230 | "@var{port} is guaranteed not to hang. If @var{port} is a file\n" | |
231 | "port at end of file then @code{char-ready?} returns @code{#t}.\n" | |
c2dfff19 KR |
232 | "\n" |
233 | "@code{char-ready?} exists to make it possible for a\n" | |
1e6808ea MG |
234 | "program to accept characters from interactive ports without\n" |
235 | "getting stuck waiting for input. Any input editors associated\n" | |
236 | "with such ports must make sure that characters whose existence\n" | |
237 | "has been asserted by @code{char-ready?} cannot be rubbed out.\n" | |
238 | "If @code{char-ready?} were to return @code{#f} at end of file,\n" | |
239 | "a port at end of file would be indistinguishable from an\n" | |
c2dfff19 | 240 | "interactive port that has no ready characters.") |
1bbd0b84 | 241 | #define FUNC_NAME s_scm_char_ready_p |
0f2d19dd | 242 | { |
92c2555f | 243 | scm_t_port *pt; |
6c951427 | 244 | |
0f2d19dd JB |
245 | if (SCM_UNBNDP (port)) |
246 | port = scm_cur_inp; | |
247 | else | |
34d19ef6 | 248 | SCM_VALIDATE_OPINPORT (1, port); |
d68fee48 | 249 | |
ae4c4016 JB |
250 | pt = SCM_PTAB_ENTRY (port); |
251 | ||
6c951427 GH |
252 | /* if the current read buffer is filled, or the |
253 | last pushed-back char has been read and the saved buffer is | |
254 | filled, result is true. */ | |
255 | if (pt->read_pos < pt->read_end | |
256 | || (pt->read_buf == pt->putback_buf | |
257 | && pt->saved_read_pos < pt->saved_read_end)) | |
0f2d19dd | 258 | return SCM_BOOL_T; |
ee149d03 JB |
259 | else |
260 | { | |
92c2555f | 261 | scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)]; |
ee149d03 | 262 | |
affc96b5 | 263 | if (ptob->input_waiting) |
1bbd0b84 | 264 | return SCM_BOOL(ptob->input_waiting (port)); |
ee149d03 | 265 | else |
6c951427 | 266 | return SCM_BOOL_T; |
ee149d03 | 267 | } |
0f2d19dd | 268 | } |
1bbd0b84 | 269 | #undef FUNC_NAME |
0f2d19dd | 270 | |
c2da2648 GH |
271 | /* move up to read_len chars from port's putback and/or read buffers |
272 | into memory starting at dest. returns the number of chars moved. */ | |
273 | size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len) | |
274 | { | |
92c2555f | 275 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
c2da2648 GH |
276 | size_t chars_read = 0; |
277 | size_t from_buf = min (pt->read_end - pt->read_pos, read_len); | |
278 | ||
279 | if (from_buf > 0) | |
280 | { | |
281 | memcpy (dest, pt->read_pos, from_buf); | |
282 | pt->read_pos += from_buf; | |
283 | chars_read += from_buf; | |
284 | read_len -= from_buf; | |
285 | dest += from_buf; | |
286 | } | |
287 | ||
288 | /* if putback was active, try the real input buffer too. */ | |
289 | if (pt->read_buf == pt->putback_buf) | |
290 | { | |
291 | from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len); | |
292 | if (from_buf > 0) | |
293 | { | |
294 | memcpy (dest, pt->saved_read_pos, from_buf); | |
295 | pt->saved_read_pos += from_buf; | |
296 | chars_read += from_buf; | |
297 | } | |
298 | } | |
299 | return chars_read; | |
300 | } | |
301 | ||
6c951427 | 302 | /* Clear a port's read buffers, returning the contents. */ |
a1ec6916 | 303 | SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0, |
1bbd0b84 | 304 | (SCM port), |
4a151b3d GH |
305 | "This procedure clears a port's input buffers, similar\n" |
306 | "to the way that force-output clears the output buffer. The\n" | |
307 | "contents of the buffers are returned as a single string, e.g.,\n" | |
308 | "\n" | |
309 | "@lisp\n" | |
310 | "(define p (open-input-file ...))\n" | |
311 | "(drain-input p) => empty string, nothing buffered yet.\n" | |
312 | "(unread-char (read-char p) p)\n" | |
313 | "(drain-input p) => initial chars from p, up to the buffer size.\n" | |
314 | "@end lisp\n\n" | |
315 | "Draining the buffers may be useful for cleanly finishing\n" | |
316 | "buffered I/O so that the file descriptor can be used directly\n" | |
317 | "for further input.") | |
1bbd0b84 | 318 | #define FUNC_NAME s_scm_drain_input |
ee149d03 | 319 | { |
840ae05d | 320 | SCM result; |
92c2555f | 321 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
c014a02e | 322 | long count; |
ee149d03 | 323 | |
34d19ef6 | 324 | SCM_VALIDATE_OPINPORT (1, port); |
840ae05d | 325 | |
6c951427 GH |
326 | count = pt->read_end - pt->read_pos; |
327 | if (pt->read_buf == pt->putback_buf) | |
328 | count += pt->saved_read_end - pt->saved_read_pos; | |
840ae05d | 329 | |
be54b15d | 330 | result = scm_allocate_string (count); |
c2da2648 | 331 | scm_take_from_input_buffers (port, SCM_STRING_CHARS (result), count); |
6c951427 | 332 | |
840ae05d | 333 | return result; |
ee149d03 | 334 | } |
1bbd0b84 | 335 | #undef FUNC_NAME |
0f2d19dd JB |
336 | |
337 | \f | |
d68fee48 | 338 | /* Standard ports --- current input, output, error, and more(!). */ |
0f2d19dd | 339 | |
3b3b36dd | 340 | SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0, |
e1546b65 MG |
341 | (), |
342 | "Return the current input port. This is the default port used\n" | |
343 | "by many input procedures. Initially, @code{current-input-port}\n" | |
344 | "returns the @dfn{standard input} in Unix and C terminology.") | |
1bbd0b84 | 345 | #define FUNC_NAME s_scm_current_input_port |
0f2d19dd JB |
346 | { |
347 | return scm_cur_inp; | |
348 | } | |
1bbd0b84 | 349 | #undef FUNC_NAME |
0f2d19dd | 350 | |
3b3b36dd | 351 | SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0, |
e1546b65 MG |
352 | (), |
353 | "Return the current output port. This is the default port used\n" | |
9401323e | 354 | "by many output procedures. Initially,\n" |
e1546b65 MG |
355 | "@code{current-output-port} returns the @dfn{standard output} in\n" |
356 | "Unix and C terminology.") | |
1bbd0b84 | 357 | #define FUNC_NAME s_scm_current_output_port |
0f2d19dd JB |
358 | { |
359 | return scm_cur_outp; | |
360 | } | |
1bbd0b84 | 361 | #undef FUNC_NAME |
0f2d19dd | 362 | |
3b3b36dd | 363 | SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0, |
1bbd0b84 | 364 | (), |
b380b885 MD |
365 | "Return the port to which errors and warnings should be sent (the\n" |
366 | "@dfn{standard error} in Unix and C terminology).") | |
1bbd0b84 | 367 | #define FUNC_NAME s_scm_current_error_port |
0f2d19dd JB |
368 | { |
369 | return scm_cur_errp; | |
370 | } | |
1bbd0b84 | 371 | #undef FUNC_NAME |
0f2d19dd | 372 | |
3b3b36dd | 373 | SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0, |
e1546b65 | 374 | (), |
b450f070 | 375 | "Return the current-load-port.\n" |
e1546b65 | 376 | "The load port is used internally by @code{primitive-load}.") |
1bbd0b84 | 377 | #define FUNC_NAME s_scm_current_load_port |
31614d8e MD |
378 | { |
379 | return scm_cur_loadp; | |
380 | } | |
1bbd0b84 | 381 | #undef FUNC_NAME |
31614d8e | 382 | |
3b3b36dd | 383 | SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0, |
1bbd0b84 | 384 | (SCM port), |
8f85c0c6 NJ |
385 | "@deffnx {Scheme Procedure} set-current-output-port port\n" |
386 | "@deffnx {Scheme Procedure} set-current-error-port port\n" | |
b380b885 MD |
387 | "Change the ports returned by @code{current-input-port},\n" |
388 | "@code{current-output-port} and @code{current-error-port}, respectively,\n" | |
389 | "so that they use the supplied @var{port} for input or output.") | |
1bbd0b84 | 390 | #define FUNC_NAME s_scm_set_current_input_port |
0f2d19dd JB |
391 | { |
392 | SCM oinp = scm_cur_inp; | |
34d19ef6 | 393 | SCM_VALIDATE_OPINPORT (1, port); |
0f2d19dd JB |
394 | scm_cur_inp = port; |
395 | return oinp; | |
396 | } | |
1bbd0b84 | 397 | #undef FUNC_NAME |
0f2d19dd JB |
398 | |
399 | ||
3b3b36dd | 400 | SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0, |
e1546b65 MG |
401 | (SCM port), |
402 | "Set the current default output port to @var{port}.") | |
1bbd0b84 | 403 | #define FUNC_NAME s_scm_set_current_output_port |
0f2d19dd JB |
404 | { |
405 | SCM ooutp = scm_cur_outp; | |
78446828 | 406 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 407 | SCM_VALIDATE_OPOUTPORT (1, port); |
0f2d19dd JB |
408 | scm_cur_outp = port; |
409 | return ooutp; | |
410 | } | |
1bbd0b84 | 411 | #undef FUNC_NAME |
0f2d19dd JB |
412 | |
413 | ||
3b3b36dd | 414 | SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0, |
e1546b65 MG |
415 | (SCM port), |
416 | "Set the current default error port to @var{port}.") | |
1bbd0b84 | 417 | #define FUNC_NAME s_scm_set_current_error_port |
0f2d19dd JB |
418 | { |
419 | SCM oerrp = scm_cur_errp; | |
78446828 | 420 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 421 | SCM_VALIDATE_OPOUTPORT (1, port); |
0f2d19dd JB |
422 | scm_cur_errp = port; |
423 | return oerrp; | |
424 | } | |
1bbd0b84 | 425 | #undef FUNC_NAME |
0f2d19dd JB |
426 | |
427 | \f | |
840ae05d | 428 | /* The port table --- an array of pointers to ports. */ |
0f2d19dd | 429 | |
67329a9e | 430 | scm_t_port **scm_i_port_table; |
0f2d19dd | 431 | |
67329a9e HWN |
432 | long scm_i_port_table_size = 0; /* Number of ports in scm_i_port_table. */ |
433 | long scm_i_port_table_room = 20; /* Size of the array. */ | |
0f2d19dd | 434 | |
b9ad392e MD |
435 | SCM_GLOBAL_MUTEX (scm_i_port_table_mutex); |
436 | ||
437 | /* This function is not and should not be thread safe. */ | |
1cc91f1b | 438 | |
da220f27 HWN |
439 | SCM |
440 | scm_new_port_table_entry (scm_t_bits tag) | |
402788a9 | 441 | #define FUNC_NAME "scm_new_port_table_entry" |
0f2d19dd | 442 | { |
85835e59 HWN |
443 | /* |
444 | We initialize the cell to empty, this is in case scm_gc_calloc | |
445 | triggers GC ; we don't want the GC to scan a half-finished Z. | |
446 | */ | |
447 | ||
67329a9e | 448 | SCM z = scm_cons (SCM_EOL, SCM_EOL); |
39e8f371 | 449 | scm_t_port *entry = (scm_t_port *) scm_gc_calloc (sizeof (scm_t_port), "port"); |
67329a9e | 450 | if (scm_i_port_table_size == scm_i_port_table_room) |
0f2d19dd | 451 | { |
4c9419ac | 452 | /* initial malloc is in gc.c. this doesn't use scm_gc_malloc etc., |
c6c79933 | 453 | since it can never be freed during gc. */ |
67329a9e | 454 | void *newt = scm_realloc ((char *) scm_i_port_table, |
4c9419ac | 455 | (size_t) (sizeof (scm_t_port *) |
67329a9e HWN |
456 | * scm_i_port_table_room * 2)); |
457 | scm_i_port_table = (scm_t_port **) newt; | |
458 | scm_i_port_table_room *= 2; | |
0f2d19dd | 459 | } |
840ae05d | 460 | |
67329a9e | 461 | entry->entry = scm_i_port_table_size; |
5f16b897 | 462 | |
840ae05d | 463 | entry->file_name = SCM_BOOL_F; |
61e452ba | 464 | entry->rw_active = SCM_PORT_NEITHER; |
5f16b897 | 465 | |
67329a9e HWN |
466 | scm_i_port_table[scm_i_port_table_size] = entry; |
467 | scm_i_port_table_size++; | |
840ae05d | 468 | |
da220f27 HWN |
469 | entry->port = z; |
470 | SCM_SET_CELL_TYPE(z, tag); | |
471 | SCM_SETPTAB_ENTRY(z, entry); | |
472 | ||
473 | return z; | |
0f2d19dd | 474 | } |
c6c79933 | 475 | #undef FUNC_NAME |
0f2d19dd | 476 | |
67329a9e HWN |
477 | #if SCM_ENABLE_DEPRECATED==1 |
478 | SCM_API scm_t_port * | |
479 | scm_add_to_port_table (SCM port) | |
480 | { | |
481 | SCM z = scm_new_port_table_entry (scm_tc7_port); | |
482 | scm_t_port * pt = SCM_PTAB_ENTRY(z); | |
483 | ||
484 | pt->port = port; | |
485 | SCM_SETCAR(z, SCM_EOL); | |
486 | SCM_SETCDR(z, SCM_EOL); | |
85835e59 | 487 | SCM_SETPTAB_ENTRY (port, pt); |
67329a9e HWN |
488 | return pt; |
489 | } | |
490 | #endif | |
491 | ||
492 | ||
6c951427 | 493 | /* Remove a port from the table and destroy it. */ |
b9ad392e MD |
494 | |
495 | /* This function is not and should not be thread safe. */ | |
496 | ||
0f2d19dd | 497 | void |
a284e297 | 498 | scm_remove_from_port_table (SCM port) |
db4b4ca6 | 499 | #define FUNC_NAME "scm_remove_from_port_table" |
0f2d19dd | 500 | { |
92c2555f | 501 | scm_t_port *p = SCM_PTAB_ENTRY (port); |
c014a02e | 502 | long i = p->entry; |
6c951427 | 503 | |
67329a9e | 504 | if (i >= scm_i_port_table_size) |
1afff620 | 505 | SCM_MISC_ERROR ("Port not in table: ~S", scm_list_1 (port)); |
6c951427 | 506 | if (p->putback_buf) |
4c9419ac MV |
507 | scm_gc_free (p->putback_buf, p->putback_buf_size, "putback buffer"); |
508 | scm_gc_free (p, sizeof (scm_t_port), "port"); | |
ee1e7e13 MD |
509 | /* Since we have just freed slot i we can shrink the table by moving |
510 | the last entry to that slot... */ | |
67329a9e | 511 | if (i < scm_i_port_table_size - 1) |
0f2d19dd | 512 | { |
67329a9e HWN |
513 | scm_i_port_table[i] = scm_i_port_table[scm_i_port_table_size - 1]; |
514 | scm_i_port_table[i]->entry = i; | |
0f2d19dd | 515 | } |
0f2d19dd | 516 | SCM_SETPTAB_ENTRY (port, 0); |
67329a9e | 517 | scm_i_port_table_size--; |
0f2d19dd | 518 | } |
db4b4ca6 DH |
519 | #undef FUNC_NAME |
520 | ||
0f2d19dd | 521 | |
fea6b4ea | 522 | #ifdef GUILE_DEBUG |
b450f070 | 523 | /* Functions for debugging. */ |
1cc91f1b | 524 | |
3b3b36dd | 525 | SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0, |
b450f070 | 526 | (), |
1e6808ea | 527 | "Return the number of ports in the port table. @code{pt-size}\n" |
5352393c | 528 | "is only included in @code{--enable-guile-debug} builds.") |
1bbd0b84 | 529 | #define FUNC_NAME s_scm_pt_size |
0f2d19dd | 530 | { |
67329a9e | 531 | return SCM_MAKINUM (scm_i_port_table_size); |
0f2d19dd | 532 | } |
1bbd0b84 | 533 | #undef FUNC_NAME |
0f2d19dd | 534 | |
3b3b36dd | 535 | SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0, |
b450f070 | 536 | (SCM index), |
1e6808ea | 537 | "Return the port at @var{index} in the port table.\n" |
5352393c MG |
538 | "@code{pt-member} is only included in\n" |
539 | "@code{--enable-guile-debug} builds.") | |
1bbd0b84 | 540 | #define FUNC_NAME s_scm_pt_member |
0f2d19dd | 541 | { |
c014a02e | 542 | long i; |
34d19ef6 | 543 | SCM_VALIDATE_INUM_COPY (1, index, i); |
67329a9e | 544 | if (i < 0 || i >= scm_i_port_table_size) |
0f2d19dd JB |
545 | return SCM_BOOL_F; |
546 | else | |
67329a9e | 547 | return scm_i_port_table[i]->port; |
0f2d19dd | 548 | } |
1bbd0b84 | 549 | #undef FUNC_NAME |
0f2d19dd JB |
550 | #endif |
551 | ||
70df8af6 | 552 | void |
92c2555f | 553 | scm_port_non_buffer (scm_t_port *pt) |
70df8af6 GH |
554 | { |
555 | pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf; | |
556 | pt->write_buf = pt->write_pos = &pt->shortbuf; | |
557 | pt->read_buf_size = pt->write_buf_size = 1; | |
558 | pt->write_end = pt->write_buf + pt->write_buf_size; | |
559 | } | |
0f2d19dd | 560 | |
d68fee48 JB |
561 | \f |
562 | /* Revealed counts --- an oddity inherited from SCSH. */ | |
563 | ||
8b13c6b3 GH |
564 | /* Find a port in the table and return its revealed count. |
565 | Also used by the garbage collector. | |
0f2d19dd | 566 | */ |
1cc91f1b | 567 | |
0f2d19dd | 568 | int |
a284e297 | 569 | scm_revealed_count (SCM port) |
0f2d19dd JB |
570 | { |
571 | return SCM_REVEALED(port); | |
572 | } | |
573 | ||
574 | ||
575 | ||
576 | /* Return the revealed count for a port. */ | |
577 | ||
3b3b36dd | 578 | SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0, |
1bbd0b84 | 579 | (SCM port), |
1e6808ea | 580 | "Return the revealed count for @var{port}.") |
1bbd0b84 | 581 | #define FUNC_NAME s_scm_port_revealed |
0f2d19dd | 582 | { |
78446828 | 583 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 584 | SCM_VALIDATE_OPENPORT (1, port); |
8b13c6b3 | 585 | return SCM_MAKINUM (scm_revealed_count (port)); |
0f2d19dd | 586 | } |
1bbd0b84 | 587 | #undef FUNC_NAME |
0f2d19dd JB |
588 | |
589 | /* Set the revealed count for a port. */ | |
3b3b36dd | 590 | SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, |
1bbd0b84 | 591 | (SCM port, SCM rcount), |
b450f070 | 592 | "Sets the revealed count for a port to a given value.\n" |
b380b885 | 593 | "The return value is unspecified.") |
1bbd0b84 | 594 | #define FUNC_NAME s_scm_set_port_revealed_x |
0f2d19dd | 595 | { |
78446828 | 596 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 HWN |
597 | SCM_VALIDATE_OPENPORT (1, port); |
598 | SCM_VALIDATE_INUM (2, rcount); | |
0f2d19dd | 599 | SCM_REVEALED (port) = SCM_INUM (rcount); |
8b13c6b3 | 600 | return SCM_UNSPECIFIED; |
0f2d19dd | 601 | } |
1bbd0b84 | 602 | #undef FUNC_NAME |
0f2d19dd | 603 | |
d68fee48 JB |
604 | |
605 | \f | |
606 | /* Retrieving a port's mode. */ | |
607 | ||
eadd48de GH |
608 | /* Return the flags that characterize a port based on the mode |
609 | * string used to open a file for that port. | |
610 | * | |
611 | * See PORT FLAGS in scm.h | |
612 | */ | |
613 | ||
614 | long | |
a284e297 | 615 | scm_mode_bits (char *modes) |
eadd48de GH |
616 | { |
617 | return (SCM_OPN | |
618 | | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0) | |
619 | | ( strchr (modes, 'w') | |
620 | || strchr (modes, 'a') | |
621 | || strchr (modes, '+') ? SCM_WRTNG : 0) | |
ee149d03 JB |
622 | | (strchr (modes, '0') ? SCM_BUF0 : 0) |
623 | | (strchr (modes, 'l') ? SCM_BUFLINE : 0)); | |
eadd48de GH |
624 | } |
625 | ||
626 | ||
627 | /* Return the mode flags from an open port. | |
628 | * Some modes such as "append" are only used when opening | |
629 | * a file and are not returned here. */ | |
630 | ||
3b3b36dd | 631 | SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0, |
1bbd0b84 | 632 | (SCM port), |
1e6808ea MG |
633 | "Return the port modes associated with the open port @var{port}.\n" |
634 | "These will not necessarily be identical to the modes used when\n" | |
635 | "the port was opened, since modes such as \"append\" which are\n" | |
636 | "used only during port creation are not retained.") | |
1bbd0b84 | 637 | #define FUNC_NAME s_scm_port_mode |
eadd48de | 638 | { |
26a3038d | 639 | char modes[4]; |
eadd48de | 640 | modes[0] = '\0'; |
78446828 MV |
641 | |
642 | port = SCM_COERCE_OUTPORT (port); | |
34d19ef6 | 643 | SCM_VALIDATE_OPPORT (1, port); |
f9a64404 DH |
644 | if (SCM_CELL_WORD_0 (port) & SCM_RDNG) { |
645 | if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) | |
eadd48de GH |
646 | strcpy (modes, "r+"); |
647 | else | |
648 | strcpy (modes, "r"); | |
649 | } | |
f9a64404 | 650 | else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG) |
eadd48de | 651 | strcpy (modes, "w"); |
f9a64404 | 652 | if (SCM_CELL_WORD_0 (port) & SCM_BUF0) |
eadd48de | 653 | strcat (modes, "0"); |
36284627 | 654 | return scm_mem2string (modes, strlen (modes)); |
eadd48de | 655 | } |
1bbd0b84 | 656 | #undef FUNC_NAME |
eadd48de GH |
657 | |
658 | ||
d68fee48 JB |
659 | \f |
660 | /* Closing ports. */ | |
661 | ||
0f2d19dd JB |
662 | /* scm_close_port |
663 | * Call the close operation on a port object. | |
eadd48de | 664 | * see also scm_close. |
0f2d19dd | 665 | */ |
3b3b36dd | 666 | SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0, |
1bbd0b84 | 667 | (SCM port), |
1e6808ea MG |
668 | "Close the specified port object. Return @code{#t} if it\n" |
669 | "successfully closes a port or @code{#f} if it was already\n" | |
670 | "closed. An exception may be raised if an error occurs, for\n" | |
671 | "example when flushing buffered output. See also @ref{Ports and\n" | |
672 | "File Descriptors, close}, for a procedure which can close file\n" | |
673 | "descriptors.") | |
1bbd0b84 | 674 | #define FUNC_NAME s_scm_close_port |
0f2d19dd | 675 | { |
1be6b49c | 676 | size_t i; |
eadd48de GH |
677 | int rv; |
678 | ||
78446828 MV |
679 | port = SCM_COERCE_OUTPORT (port); |
680 | ||
7a754ca6 | 681 | SCM_VALIDATE_PORT (1, port); |
0f2d19dd | 682 | if (SCM_CLOSEDP (port)) |
eadd48de | 683 | return SCM_BOOL_F; |
0f2d19dd | 684 | i = SCM_PTOBNUM (port); |
affc96b5 GH |
685 | if (scm_ptobs[i].close) |
686 | rv = (scm_ptobs[i].close) (port); | |
eadd48de GH |
687 | else |
688 | rv = 0; | |
b9ad392e | 689 | scm_mutex_lock (&scm_i_port_table_mutex); |
0f2d19dd | 690 | scm_remove_from_port_table (port); |
b9ad392e | 691 | scm_mutex_unlock (&scm_i_port_table_mutex); |
22a52da1 | 692 | SCM_CLR_PORT_OPEN_FLAG (port); |
36284627 | 693 | return SCM_BOOL (rv >= 0); |
7a754ca6 MD |
694 | } |
695 | #undef FUNC_NAME | |
696 | ||
697 | SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0, | |
698 | (SCM port), | |
699 | "Close the specified input port object. The routine has no effect if\n" | |
700 | "the file has already been closed. An exception may be raised if an\n" | |
701 | "error occurs. The value returned is unspecified.\n\n" | |
702 | "See also @ref{Ports and File Descriptors, close}, for a procedure\n" | |
703 | "which can close file descriptors.") | |
704 | #define FUNC_NAME s_scm_close_input_port | |
705 | { | |
706 | SCM_VALIDATE_INPUT_PORT (1, port); | |
707 | scm_close_port (port); | |
708 | return SCM_UNSPECIFIED; | |
709 | } | |
710 | #undef FUNC_NAME | |
711 | ||
712 | SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0, | |
713 | (SCM port), | |
714 | "Close the specified output port object. The routine has no effect if\n" | |
715 | "the file has already been closed. An exception may be raised if an\n" | |
716 | "error occurs. The value returned is unspecified.\n\n" | |
717 | "See also @ref{Ports and File Descriptors, close}, for a procedure\n" | |
718 | "which can close file descriptors.") | |
719 | #define FUNC_NAME s_scm_close_output_port | |
720 | { | |
721 | port = SCM_COERCE_OUTPORT (port); | |
722 | SCM_VALIDATE_OUTPUT_PORT (1, port); | |
723 | scm_close_port (port); | |
724 | return SCM_UNSPECIFIED; | |
0f2d19dd | 725 | } |
1bbd0b84 | 726 | #undef FUNC_NAME |
0f2d19dd | 727 | |
c536b4b3 MV |
728 | void |
729 | scm_c_port_for_each (void (*proc)(void *data, SCM p), void *data) | |
c2ca4493 | 730 | { |
c014a02e | 731 | long i; |
fdfe6305 MV |
732 | SCM ports; |
733 | ||
fdfe6305 MV |
734 | /* Even without pre-emptive multithreading, running arbitrary code |
735 | while scanning the port table is unsafe because the port table | |
736 | can change arbitrarily (from a GC, for example). So we build a | |
737 | list in advance while blocking the GC. -mvo */ | |
738 | ||
b9ad392e | 739 | scm_mutex_lock (&scm_i_port_table_mutex); |
fdfe6305 MV |
740 | scm_block_gc++; |
741 | ports = SCM_EOL; | |
67329a9e HWN |
742 | for (i = 0; i < scm_i_port_table_size; i++) |
743 | ports = scm_cons (scm_i_port_table[i]->port, ports); | |
fdfe6305 | 744 | scm_block_gc--; |
b9ad392e | 745 | scm_mutex_unlock (&scm_i_port_table_mutex); |
fdfe6305 MV |
746 | |
747 | while (ports != SCM_EOL) | |
c2ca4493 | 748 | { |
c536b4b3 | 749 | proc (data, SCM_CAR (ports)); |
fdfe6305 | 750 | ports = SCM_CDR (ports); |
c2ca4493 | 751 | } |
c536b4b3 | 752 | } |
fdfe6305 | 753 | |
c536b4b3 MV |
754 | SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0, |
755 | (SCM proc), | |
756 | "Apply @var{proc} to each port in the Guile port table\n" | |
757 | "in turn. The return value is unspecified. More specifically,\n" | |
758 | "@var{proc} is applied exactly once to every port that exists\n" | |
759 | "in the system at the time @var{port-for-each} is invoked.\n" | |
760 | "Changes to the port table while @var{port-for-each} is running\n" | |
761 | "have no effect as far as @var{port-for-each} is concerned.") | |
762 | #define FUNC_NAME s_scm_port_for_each | |
763 | { | |
764 | SCM_VALIDATE_PROC (1, proc); | |
765 | ||
766 | scm_c_port_for_each ((void (*)(void*,SCM))scm_call_1, proc); | |
c2ca4493 GH |
767 | return SCM_UNSPECIFIED; |
768 | } | |
769 | #undef FUNC_NAME | |
770 | ||
c536b4b3 | 771 | |
d68fee48 JB |
772 | \f |
773 | /* Utter miscellany. Gosh, we should clean this up some time. */ | |
774 | ||
3b3b36dd | 775 | SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0, |
1bbd0b84 | 776 | (SCM x), |
1e6808ea MG |
777 | "Return @code{#t} if @var{x} is an input port, otherwise return\n" |
778 | "@code{#f}. Any object satisfying this predicate also satisfies\n" | |
779 | "@code{port?}.") | |
1bbd0b84 | 780 | #define FUNC_NAME s_scm_input_port_p |
0f2d19dd | 781 | { |
36284627 | 782 | return SCM_BOOL (SCM_INPUT_PORT_P (x)); |
0f2d19dd | 783 | } |
1bbd0b84 | 784 | #undef FUNC_NAME |
0f2d19dd | 785 | |
3b3b36dd | 786 | SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0, |
1bbd0b84 | 787 | (SCM x), |
1e6808ea MG |
788 | "Return @code{#t} if @var{x} is an output port, otherwise return\n" |
789 | "@code{#f}. Any object satisfying this predicate also satisfies\n" | |
790 | "@code{port?}.") | |
1bbd0b84 | 791 | #define FUNC_NAME s_scm_output_port_p |
0f2d19dd | 792 | { |
82893676 | 793 | x = SCM_COERCE_OUTPORT (x); |
36284627 | 794 | return SCM_BOOL (SCM_OUTPUT_PORT_P (x)); |
0f2d19dd | 795 | } |
1bbd0b84 | 796 | #undef FUNC_NAME |
0f2d19dd | 797 | |
eb5c0a2a GH |
798 | SCM_DEFINE (scm_port_p, "port?", 1, 0, 0, |
799 | (SCM x), | |
1e6808ea | 800 | "Return a boolean indicating whether @var{x} is a port.\n" |
5352393c MG |
801 | "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n" |
802 | "@var{x}))}.") | |
eb5c0a2a GH |
803 | #define FUNC_NAME s_scm_port_p |
804 | { | |
805 | return SCM_BOOL (SCM_PORTP (x)); | |
806 | } | |
807 | #undef FUNC_NAME | |
808 | ||
3b3b36dd | 809 | SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0, |
1bbd0b84 | 810 | (SCM port), |
1e6808ea MG |
811 | "Return @code{#t} if @var{port} is closed or @code{#f} if it is\n" |
812 | "open.") | |
1bbd0b84 | 813 | #define FUNC_NAME s_scm_port_closed_p |
60d0643d | 814 | { |
34d19ef6 | 815 | SCM_VALIDATE_PORT (1, port); |
36284627 | 816 | return SCM_BOOL (!SCM_OPPORTP (port)); |
60d0643d | 817 | } |
1bbd0b84 | 818 | #undef FUNC_NAME |
0f2d19dd | 819 | |
3b3b36dd | 820 | SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0, |
1bbd0b84 | 821 | (SCM x), |
1e6808ea MG |
822 | "Return @code{#t} if @var{x} is an end-of-file object; otherwise\n" |
823 | "return @code{#f}.") | |
1bbd0b84 | 824 | #define FUNC_NAME s_scm_eof_object_p |
0f2d19dd | 825 | { |
1bbd0b84 | 826 | return SCM_BOOL(SCM_EOF_OBJECT_P (x)); |
0f2d19dd | 827 | } |
1bbd0b84 | 828 | #undef FUNC_NAME |
0f2d19dd | 829 | |
3b3b36dd | 830 | SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0, |
1bbd0b84 | 831 | (SCM port), |
b380b885 | 832 | "Flush the specified output port, or the current output port if @var{port}\n" |
9401323e | 833 | "is omitted. The current output buffer contents are passed to the\n" |
b380b885 MD |
834 | "underlying port implementation (e.g., in the case of fports, the\n" |
835 | "data will be written to the file and the output buffer will be cleared.)\n" | |
836 | "It has no effect on an unbuffered port.\n\n" | |
837 | "The return value is unspecified.") | |
1bbd0b84 | 838 | #define FUNC_NAME s_scm_force_output |
0f2d19dd JB |
839 | { |
840 | if (SCM_UNBNDP (port)) | |
3e877d15 | 841 | port = scm_cur_outp; |
0f2d19dd | 842 | else |
78446828 MV |
843 | { |
844 | port = SCM_COERCE_OUTPORT (port); | |
34d19ef6 | 845 | SCM_VALIDATE_OPOUTPORT (1, port); |
78446828 | 846 | } |
affc96b5 | 847 | scm_flush (port); |
ee149d03 | 848 | return SCM_UNSPECIFIED; |
0f2d19dd | 849 | } |
1bbd0b84 | 850 | #undef FUNC_NAME |
0f2d19dd | 851 | |
a1ec6916 | 852 | SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0, |
1bbd0b84 | 853 | (), |
b380b885 MD |
854 | "Equivalent to calling @code{force-output} on\n" |
855 | "all open output ports. The return value is unspecified.") | |
1bbd0b84 | 856 | #define FUNC_NAME s_scm_flush_all_ports |
89ea5b7c | 857 | { |
1be6b49c | 858 | size_t i; |
89ea5b7c | 859 | |
b9ad392e | 860 | scm_mutex_lock (&scm_i_port_table_mutex); |
67329a9e | 861 | for (i = 0; i < scm_i_port_table_size; i++) |
89ea5b7c | 862 | { |
67329a9e HWN |
863 | if (SCM_OPOUTPORTP (scm_i_port_table[i]->port)) |
864 | scm_flush (scm_i_port_table[i]->port); | |
89ea5b7c | 865 | } |
b9ad392e | 866 | scm_mutex_unlock (&scm_i_port_table_mutex); |
89ea5b7c GH |
867 | return SCM_UNSPECIFIED; |
868 | } | |
1bbd0b84 | 869 | #undef FUNC_NAME |
0f2d19dd | 870 | |
3b3b36dd | 871 | SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0, |
1bbd0b84 | 872 | (SCM port), |
1e6808ea MG |
873 | "Return the next character available from @var{port}, updating\n" |
874 | "@var{port} to point to the following character. If no more\n" | |
875 | "characters are available, the end-of-file object is returned.") | |
1bbd0b84 | 876 | #define FUNC_NAME s_scm_read_char |
0f2d19dd JB |
877 | { |
878 | int c; | |
879 | if (SCM_UNBNDP (port)) | |
334341aa | 880 | port = scm_cur_inp; |
34d19ef6 | 881 | SCM_VALIDATE_OPINPORT (1, port); |
b7f3516f | 882 | c = scm_getc (port); |
0f2d19dd JB |
883 | if (EOF == c) |
884 | return SCM_EOF_VAL; | |
7866a09b | 885 | return SCM_MAKE_CHAR (c); |
0f2d19dd | 886 | } |
1bbd0b84 | 887 | #undef FUNC_NAME |
0f2d19dd | 888 | |
5c070ca7 | 889 | /* this should only be called when the read buffer is empty. it |
affc96b5 | 890 | tries to refill the read buffer. it returns the first char from |
5c070ca7 | 891 | the port, which is either EOF or *(pt->read_pos). */ |
6c951427 | 892 | int |
affc96b5 | 893 | scm_fill_input (SCM port) |
6c951427 | 894 | { |
92c2555f | 895 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
283a1a0e | 896 | |
6c951427 GH |
897 | if (pt->read_buf == pt->putback_buf) |
898 | { | |
899 | /* finished reading put-back chars. */ | |
900 | pt->read_buf = pt->saved_read_buf; | |
901 | pt->read_pos = pt->saved_read_pos; | |
902 | pt->read_end = pt->saved_read_end; | |
903 | pt->read_buf_size = pt->saved_read_buf_size; | |
904 | if (pt->read_pos < pt->read_end) | |
5c070ca7 | 905 | return *(pt->read_pos); |
6c951427 | 906 | } |
affc96b5 | 907 | return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port); |
6c951427 GH |
908 | } |
909 | ||
ee149d03 | 910 | int |
a284e297 | 911 | scm_getc (SCM port) |
0f2d19dd JB |
912 | { |
913 | int c; | |
92c2555f | 914 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
ee149d03 | 915 | |
840ae05d | 916 | if (pt->rw_active == SCM_PORT_WRITE) |
aab1caad RB |
917 | /* may be marginally faster than calling scm_flush. */ |
918 | scm_ptobs[SCM_PTOBNUM (port)].flush (port); | |
6c951427 | 919 | |
5c070ca7 GH |
920 | if (pt->rw_random) |
921 | pt->rw_active = SCM_PORT_READ; | |
922 | ||
923 | if (pt->read_pos >= pt->read_end) | |
ee149d03 | 924 | { |
affc96b5 | 925 | if (scm_fill_input (port) == EOF) |
5c070ca7 | 926 | return EOF; |
ee149d03 JB |
927 | } |
928 | ||
5c070ca7 | 929 | c = *(pt->read_pos++); |
840ae05d | 930 | |
aab1caad | 931 | switch (c) |
ee149d03 | 932 | { |
aab1caad RB |
933 | case '\n': |
934 | SCM_INCLINE (port); | |
935 | break; | |
936 | case '\t': | |
937 | SCM_TABCOL (port); | |
938 | break; | |
939 | default: | |
940 | SCM_INCCOL (port); | |
941 | break; | |
ee149d03 | 942 | } |
aab1caad | 943 | |
ee149d03 | 944 | return c; |
0f2d19dd JB |
945 | } |
946 | ||
ee149d03 | 947 | void |
a284e297 | 948 | scm_putc (char c, SCM port) |
ee149d03 | 949 | { |
265e6a4d | 950 | scm_lfwrite (&c, 1, port); |
ee149d03 | 951 | } |
3cb988bd | 952 | |
ee149d03 | 953 | void |
70d63753 | 954 | scm_puts (const char *s, SCM port) |
3cb988bd | 955 | { |
265e6a4d | 956 | scm_lfwrite (s, strlen (s), port); |
ee149d03 | 957 | } |
3cb988bd | 958 | |
6fe692e9 MD |
959 | /* scm_lfwrite |
960 | * | |
53802ea8 MV |
961 | * This function differs from scm_c_write; it updates port line and |
962 | * column. */ | |
6fe692e9 | 963 | |
ee149d03 | 964 | void |
1be6b49c | 965 | scm_lfwrite (const char *ptr, size_t size, SCM port) |
ee149d03 | 966 | { |
92c2555f MV |
967 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
968 | scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)]; | |
3e2043c4 | 969 | |
840ae05d | 970 | if (pt->rw_active == SCM_PORT_READ) |
affc96b5 | 971 | scm_end_input (port); |
283a1a0e | 972 | |
31703ab8 | 973 | ptob->write (port, ptr, size); |
840ae05d | 974 | |
53802ea8 MV |
975 | for (; size; ptr++, size--) { |
976 | if (*ptr == '\n') { | |
977 | SCM_INCLINE(port); | |
978 | } | |
979 | else if (*ptr == '\t') { | |
980 | SCM_TABCOL(port); | |
981 | } | |
982 | else { | |
983 | SCM_INCCOL(port); | |
984 | } | |
985 | } | |
986 | ||
840ae05d JB |
987 | if (pt->rw_random) |
988 | pt->rw_active = SCM_PORT_WRITE; | |
ee149d03 | 989 | } |
3cb988bd | 990 | |
6fe692e9 MD |
991 | /* scm_c_read |
992 | * | |
993 | * Used by an application to read arbitrary number of bytes from an | |
994 | * SCM port. Same semantics as libc read, except that scm_c_read only | |
995 | * returns less than SIZE bytes if at end-of-file. | |
996 | * | |
997 | * Warning: Doesn't update port line and column counts! */ | |
998 | ||
1be6b49c ML |
999 | size_t |
1000 | scm_c_read (SCM port, void *buffer, size_t size) | |
6fe692e9 | 1001 | { |
92c2555f | 1002 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
1be6b49c | 1003 | size_t n_read = 0, n_available; |
6fe692e9 MD |
1004 | |
1005 | if (pt->rw_active == SCM_PORT_WRITE) | |
1006 | scm_ptobs[SCM_PTOBNUM (port)].flush (port); | |
1007 | ||
1008 | if (pt->rw_random) | |
1009 | pt->rw_active = SCM_PORT_READ; | |
1010 | ||
1011 | if (SCM_READ_BUFFER_EMPTY_P (pt)) | |
1012 | { | |
1013 | if (scm_fill_input (port) == EOF) | |
1014 | return 0; | |
1015 | } | |
1016 | ||
1017 | n_available = pt->read_end - pt->read_pos; | |
1018 | ||
1019 | while (n_available < size) | |
1020 | { | |
1021 | memcpy (buffer, pt->read_pos, n_available); | |
910d1e40 | 1022 | buffer = (char *) buffer + n_available; |
6fe692e9 MD |
1023 | pt->read_pos += n_available; |
1024 | n_read += n_available; | |
1025 | ||
1026 | if (SCM_READ_BUFFER_EMPTY_P (pt)) | |
1027 | { | |
1028 | if (scm_fill_input (port) == EOF) | |
1029 | return n_read; | |
1030 | } | |
1031 | ||
1032 | size -= n_available; | |
1033 | n_available = pt->read_end - pt->read_pos; | |
1034 | } | |
1035 | ||
1036 | memcpy (buffer, pt->read_pos, size); | |
1037 | pt->read_pos += size; | |
1038 | ||
1039 | return n_read + size; | |
1040 | } | |
1041 | ||
1042 | /* scm_c_write | |
1043 | * | |
1044 | * Used by an application to write arbitrary number of bytes to an SCM | |
1045 | * port. Similar semantics as libc write. However, unlike libc | |
1046 | * write, scm_c_write writes the requested number of bytes and has no | |
1047 | * return value. | |
1048 | * | |
1049 | * Warning: Doesn't update port line and column counts! | |
1050 | */ | |
1051 | ||
1052 | void | |
1be6b49c | 1053 | scm_c_write (SCM port, const void *ptr, size_t size) |
6fe692e9 | 1054 | { |
92c2555f MV |
1055 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
1056 | scm_t_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)]; | |
6fe692e9 MD |
1057 | |
1058 | if (pt->rw_active == SCM_PORT_READ) | |
1059 | scm_end_input (port); | |
1060 | ||
1061 | ptob->write (port, ptr, size); | |
1062 | ||
1063 | if (pt->rw_random) | |
1064 | pt->rw_active = SCM_PORT_WRITE; | |
1065 | } | |
3cb988bd | 1066 | |
ee149d03 | 1067 | void |
a284e297 | 1068 | scm_flush (SCM port) |
ee149d03 | 1069 | { |
c014a02e | 1070 | long i = SCM_PTOBNUM (port); |
affc96b5 | 1071 | (scm_ptobs[i].flush) (port); |
ee149d03 JB |
1072 | } |
1073 | ||
283a1a0e | 1074 | void |
a284e297 | 1075 | scm_end_input (SCM port) |
283a1a0e | 1076 | { |
c014a02e | 1077 | long offset; |
92c2555f | 1078 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
283a1a0e GH |
1079 | |
1080 | if (pt->read_buf == pt->putback_buf) | |
1081 | { | |
1082 | offset = pt->read_end - pt->read_pos; | |
1083 | pt->read_buf = pt->saved_read_buf; | |
1084 | pt->read_pos = pt->saved_read_pos; | |
1085 | pt->read_end = pt->saved_read_end; | |
1086 | pt->read_buf_size = pt->saved_read_buf_size; | |
1087 | } | |
1088 | else | |
1089 | offset = 0; | |
1090 | ||
affc96b5 | 1091 | scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset); |
283a1a0e GH |
1092 | } |
1093 | ||
ee149d03 JB |
1094 | \f |
1095 | ||
1096 | ||
1097 | void | |
a284e297 | 1098 | scm_ungetc (int c, SCM port) |
c6c79933 | 1099 | #define FUNC_NAME "scm_ungetc" |
ee149d03 | 1100 | { |
92c2555f | 1101 | scm_t_port *pt = SCM_PTAB_ENTRY (port); |
840ae05d | 1102 | |
6c951427 GH |
1103 | if (pt->read_buf == pt->putback_buf) |
1104 | /* already using the put-back buffer. */ | |
1105 | { | |
1106 | /* enlarge putback_buf if necessary. */ | |
1107 | if (pt->read_end == pt->read_buf + pt->read_buf_size | |
1108 | && pt->read_buf == pt->read_pos) | |
1109 | { | |
1be6b49c | 1110 | size_t new_size = pt->read_buf_size * 2; |
c6c79933 | 1111 | unsigned char *tmp = (unsigned char *) |
4c9419ac MV |
1112 | scm_gc_realloc (pt->putback_buf, pt->read_buf_size, new_size, |
1113 | "putback buffer"); | |
6c951427 | 1114 | |
6c951427 GH |
1115 | pt->read_pos = pt->read_buf = pt->putback_buf = tmp; |
1116 | pt->read_end = pt->read_buf + pt->read_buf_size; | |
1117 | pt->read_buf_size = pt->putback_buf_size = new_size; | |
1118 | } | |
1119 | ||
1120 | /* shift any existing bytes to buffer + 1. */ | |
1121 | if (pt->read_pos == pt->read_end) | |
1122 | pt->read_end = pt->read_buf + 1; | |
1123 | else if (pt->read_pos != pt->read_buf + 1) | |
1124 | { | |
1125 | int count = pt->read_end - pt->read_pos; | |
1126 | ||
1127 | memmove (pt->read_buf + 1, pt->read_pos, count); | |
1128 | pt->read_end = pt->read_buf + 1 + count; | |
1129 | } | |
1130 | ||
1131 | pt->read_pos = pt->read_buf; | |
1132 | } | |
1133 | else | |
1134 | /* switch to the put-back buffer. */ | |
1135 | { | |
1136 | if (pt->putback_buf == NULL) | |
1137 | { | |
c357d546 | 1138 | pt->putback_buf |
4c9419ac MV |
1139 | = (unsigned char *) scm_gc_malloc (SCM_INITIAL_PUTBACK_BUF_SIZE, |
1140 | "putback buffer"); | |
6c951427 GH |
1141 | pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE; |
1142 | } | |
1143 | ||
1144 | pt->saved_read_buf = pt->read_buf; | |
1145 | pt->saved_read_pos = pt->read_pos; | |
1146 | pt->saved_read_end = pt->read_end; | |
1147 | pt->saved_read_buf_size = pt->read_buf_size; | |
1148 | ||
1149 | pt->read_pos = pt->read_buf = pt->putback_buf; | |
1150 | pt->read_end = pt->read_buf + 1; | |
1151 | pt->read_buf_size = pt->putback_buf_size; | |
1152 | } | |
1153 | ||
1154 | *pt->read_buf = c; | |
ee149d03 | 1155 | |
840ae05d JB |
1156 | if (pt->rw_random) |
1157 | pt->rw_active = SCM_PORT_READ; | |
1158 | ||
ee149d03 JB |
1159 | if (c == '\n') |
1160 | { | |
1161 | /* What should col be in this case? | |
1162 | * We'll leave it at -1. | |
1163 | */ | |
1164 | SCM_LINUM (port) -= 1; | |
1165 | } | |
1166 | else | |
1167 | SCM_COL(port) -= 1; | |
1168 | } | |
c6c79933 | 1169 | #undef FUNC_NAME |
ee149d03 JB |
1170 | |
1171 | ||
1172 | void | |
70d63753 | 1173 | scm_ungets (const char *s, int n, SCM port) |
ee149d03 JB |
1174 | { |
1175 | /* This is simple minded and inefficient, but unreading strings is | |
1176 | * probably not a common operation, and remember that line and | |
1177 | * column numbers have to be handled... | |
1178 | * | |
1179 | * Please feel free to write an optimized version! | |
1180 | */ | |
1181 | while (n--) | |
1182 | scm_ungetc (s[n], port); | |
1183 | } | |
1184 | ||
1185 | ||
3b3b36dd | 1186 | SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0, |
1bbd0b84 | 1187 | (SCM port), |
1e6808ea MG |
1188 | "Return the next character available from @var{port},\n" |
1189 | "@emph{without} updating @var{port} to point to the following\n" | |
1190 | "character. If no more characters are available, the\n" | |
c2dfff19 KR |
1191 | "end-of-file object is returned.\n" |
1192 | "\n" | |
1193 | "The value returned by\n" | |
1e6808ea MG |
1194 | "a call to @code{peek-char} is the same as the value that would\n" |
1195 | "have been returned by a call to @code{read-char} on the same\n" | |
1196 | "port. The only difference is that the very next call to\n" | |
1197 | "@code{read-char} or @code{peek-char} on that @var{port} will\n" | |
1198 | "return the value returned by the preceding call to\n" | |
1199 | "@code{peek-char}. In particular, a call to @code{peek-char} on\n" | |
1200 | "an interactive port will hang waiting for input whenever a call\n" | |
c2dfff19 | 1201 | "to @code{read-char} would have hung.") |
1bbd0b84 | 1202 | #define FUNC_NAME s_scm_peek_char |
ee149d03 | 1203 | { |
1a973c42 | 1204 | int c, column; |
ee149d03 JB |
1205 | if (SCM_UNBNDP (port)) |
1206 | port = scm_cur_inp; | |
1207 | else | |
34d19ef6 | 1208 | SCM_VALIDATE_OPINPORT (1, port); |
1a973c42 | 1209 | column = SCM_COL(port); |
ee149d03 JB |
1210 | c = scm_getc (port); |
1211 | if (EOF == c) | |
1212 | return SCM_EOF_VAL; | |
1213 | scm_ungetc (c, port); | |
1a973c42 | 1214 | SCM_COL(port) = column; |
7866a09b | 1215 | return SCM_MAKE_CHAR (c); |
3cb988bd | 1216 | } |
1bbd0b84 | 1217 | #undef FUNC_NAME |
3cb988bd | 1218 | |
1be4270a | 1219 | SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0, |
1bbd0b84 | 1220 | (SCM cobj, SCM port), |
b380b885 MD |
1221 | "Place @var{char} in @var{port} so that it will be read by the\n" |
1222 | "next read operation. If called multiple times, the unread characters\n" | |
1223 | "will be read again in last-in first-out order. If @var{port} is\n" | |
1224 | "not supplied, the current input port is used.") | |
1bbd0b84 | 1225 | #define FUNC_NAME s_scm_unread_char |
0f2d19dd JB |
1226 | { |
1227 | int c; | |
1228 | ||
34d19ef6 | 1229 | SCM_VALIDATE_CHAR (1, cobj); |
0f2d19dd JB |
1230 | if (SCM_UNBNDP (port)) |
1231 | port = scm_cur_inp; | |
1232 | else | |
34d19ef6 | 1233 | SCM_VALIDATE_OPINPORT (2, port); |
0f2d19dd | 1234 | |
7866a09b | 1235 | c = SCM_CHAR (cobj); |
0f2d19dd | 1236 | |
b7f3516f | 1237 | scm_ungetc (c, port); |
0f2d19dd JB |
1238 | return cobj; |
1239 | } | |
1bbd0b84 | 1240 | #undef FUNC_NAME |
0f2d19dd | 1241 | |
a1ec6916 | 1242 | SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0, |
1bbd0b84 | 1243 | (SCM str, SCM port), |
b380b885 MD |
1244 | "Place the string @var{str} in @var{port} so that its characters will be\n" |
1245 | "read in subsequent read operations. If called multiple times, the\n" | |
1246 | "unread characters will be read again in last-in first-out order. If\n" | |
1247 | "@var{port} is not supplied, the current-input-port is used.") | |
1bbd0b84 | 1248 | #define FUNC_NAME s_scm_unread_string |
ee1e7e13 | 1249 | { |
34d19ef6 | 1250 | SCM_VALIDATE_STRING (1, str); |
ee1e7e13 MD |
1251 | if (SCM_UNBNDP (port)) |
1252 | port = scm_cur_inp; | |
1253 | else | |
34d19ef6 | 1254 | SCM_VALIDATE_OPINPORT (2, port); |
ee1e7e13 | 1255 | |
34f0f2b8 | 1256 | scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port); |
ee1e7e13 MD |
1257 | |
1258 | return str; | |
1259 | } | |
1bbd0b84 | 1260 | #undef FUNC_NAME |
ee1e7e13 | 1261 | |
a1ec6916 | 1262 | SCM_DEFINE (scm_seek, "seek", 3, 0, 0, |
1e6808ea MG |
1263 | (SCM fd_port, SCM offset, SCM whence), |
1264 | "Sets the current position of @var{fd/port} to the integer\n" | |
1265 | "@var{offset}, which is interpreted according to the value of\n" | |
1266 | "@var{whence}.\n" | |
1267 | "\n" | |
1268 | "One of the following variables should be supplied for\n" | |
1269 | "@var{whence}:\n" | |
b380b885 MD |
1270 | "@defvar SEEK_SET\n" |
1271 | "Seek from the beginning of the file.\n" | |
1272 | "@end defvar\n" | |
1273 | "@defvar SEEK_CUR\n" | |
1274 | "Seek from the current position.\n" | |
1275 | "@end defvar\n" | |
1276 | "@defvar SEEK_END\n" | |
1277 | "Seek from the end of the file.\n" | |
1e6808ea MG |
1278 | "@end defvar\n" |
1279 | "If @var{fd/port} is a file descriptor, the underlying system\n" | |
1280 | "call is @code{lseek}. @var{port} may be a string port.\n" | |
1281 | "\n" | |
1282 | "The value returned is the new position in the file. This means\n" | |
1283 | "that the current position of a port can be obtained using:\n" | |
1284 | "@lisp\n" | |
b380b885 | 1285 | "(seek port 0 SEEK_CUR)\n" |
1e6808ea | 1286 | "@end lisp") |
1bbd0b84 | 1287 | #define FUNC_NAME s_scm_seek |
840ae05d JB |
1288 | { |
1289 | off_t off; | |
1290 | off_t rv; | |
1291 | int how; | |
1292 | ||
1e6808ea | 1293 | fd_port = SCM_COERCE_OUTPORT (fd_port); |
840ae05d | 1294 | |
6fe692e9 MD |
1295 | off = SCM_NUM2LONG (2, offset); |
1296 | SCM_VALIDATE_INUM_COPY (3, whence, how); | |
840ae05d | 1297 | if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END) |
1bbd0b84 | 1298 | SCM_OUT_OF_RANGE (3, whence); |
1e6808ea | 1299 | if (SCM_OPPORTP (fd_port)) |
840ae05d | 1300 | { |
92c2555f | 1301 | scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (fd_port); |
840ae05d JB |
1302 | |
1303 | if (!ptob->seek) | |
1bbd0b84 | 1304 | SCM_MISC_ERROR ("port is not seekable", |
1e6808ea | 1305 | scm_cons (fd_port, SCM_EOL)); |
840ae05d | 1306 | else |
1e6808ea | 1307 | rv = ptob->seek (fd_port, off, how); |
840ae05d JB |
1308 | } |
1309 | else /* file descriptor?. */ | |
1310 | { | |
34d19ef6 | 1311 | SCM_VALIDATE_INUM (1, fd_port); |
1e6808ea | 1312 | rv = lseek (SCM_INUM (fd_port), off, how); |
840ae05d | 1313 | if (rv == -1) |
1bbd0b84 | 1314 | SCM_SYSERROR; |
840ae05d JB |
1315 | } |
1316 | return scm_long2num (rv); | |
1317 | } | |
1bbd0b84 | 1318 | #undef FUNC_NAME |
840ae05d | 1319 | |
82893676 MG |
1320 | #ifdef __MINGW32__ |
1321 | /* Define this function since it is not supported under Windows. */ | |
1322 | static int truncate (char *file, int length) | |
1323 | { | |
1324 | int ret = -1, fdes; | |
1325 | if ((fdes = open (file, O_BINARY | O_WRONLY)) != -1) | |
1326 | { | |
1327 | ret = chsize (fdes, length); | |
1328 | close (fdes); | |
1329 | } | |
1330 | return ret; | |
1331 | } | |
1332 | #endif /* __MINGW32__ */ | |
1333 | ||
a1ec6916 | 1334 | SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0, |
1bbd0b84 | 1335 | (SCM object, SCM length), |
1e6808ea MG |
1336 | "Truncates the object referred to by @var{object} to at most\n" |
1337 | "@var{length} bytes. @var{object} can be a string containing a\n" | |
1338 | "file name or an integer file descriptor or a port.\n" | |
1339 | "@var{length} may be omitted if @var{object} is not a file name,\n" | |
1340 | "in which case the truncation occurs at the current port.\n" | |
1341 | "position. The return value is unspecified.") | |
1bbd0b84 | 1342 | #define FUNC_NAME s_scm_truncate_file |
840ae05d | 1343 | { |
69bc9ff3 GH |
1344 | int rv; |
1345 | off_t c_length; | |
1346 | ||
1347 | /* object can be a port, fdes or filename. */ | |
840ae05d | 1348 | |
840ae05d JB |
1349 | if (SCM_UNBNDP (length)) |
1350 | { | |
69bc9ff3 | 1351 | /* must supply length if object is a filename. */ |
a6d9e5ab | 1352 | if (SCM_STRINGP (object)) |
34d19ef6 | 1353 | SCM_MISC_ERROR("must supply length if OBJECT is a filename", SCM_EOL); |
69bc9ff3 | 1354 | |
c94577b4 | 1355 | length = scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR)); |
840ae05d | 1356 | } |
34d19ef6 | 1357 | c_length = SCM_NUM2LONG (2, length); |
69bc9ff3 | 1358 | if (c_length < 0) |
1bbd0b84 | 1359 | SCM_MISC_ERROR ("negative offset", SCM_EOL); |
3fe6190f | 1360 | |
69bc9ff3 GH |
1361 | object = SCM_COERCE_OUTPORT (object); |
1362 | if (SCM_INUMP (object)) | |
1363 | { | |
1364 | SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length)); | |
1365 | } | |
0c95b57d | 1366 | else if (SCM_OPOUTPORTP (object)) |
69bc9ff3 | 1367 | { |
92c2555f MV |
1368 | scm_t_port *pt = SCM_PTAB_ENTRY (object); |
1369 | scm_t_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object); | |
69bc9ff3 | 1370 | |
affc96b5 | 1371 | if (!ptob->truncate) |
1bbd0b84 | 1372 | SCM_MISC_ERROR ("port is not truncatable", SCM_EOL); |
69bc9ff3 | 1373 | if (pt->rw_active == SCM_PORT_READ) |
affc96b5 | 1374 | scm_end_input (object); |
69bc9ff3 | 1375 | else if (pt->rw_active == SCM_PORT_WRITE) |
affc96b5 | 1376 | ptob->flush (object); |
69bc9ff3 | 1377 | |
affc96b5 | 1378 | ptob->truncate (object, c_length); |
69bc9ff3 GH |
1379 | rv = 0; |
1380 | } | |
1381 | else | |
1382 | { | |
a6d9e5ab | 1383 | SCM_VALIDATE_STRING (1, object); |
a6d9e5ab | 1384 | SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length)); |
69bc9ff3 GH |
1385 | } |
1386 | if (rv == -1) | |
1bbd0b84 | 1387 | SCM_SYSERROR; |
840ae05d JB |
1388 | return SCM_UNSPECIFIED; |
1389 | } | |
1bbd0b84 | 1390 | #undef FUNC_NAME |
840ae05d | 1391 | |
a1ec6916 | 1392 | SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0, |
1bbd0b84 | 1393 | (SCM port), |
e1546b65 | 1394 | "Return the current line number for @var{port}.") |
1bbd0b84 | 1395 | #define FUNC_NAME s_scm_port_line |
0f2d19dd | 1396 | { |
78446828 | 1397 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 1398 | SCM_VALIDATE_OPENPORT (1, port); |
360fc44c | 1399 | return SCM_MAKINUM (SCM_LINUM (port)); |
0f2d19dd | 1400 | } |
1bbd0b84 | 1401 | #undef FUNC_NAME |
0f2d19dd | 1402 | |
a1ec6916 | 1403 | SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0, |
1bbd0b84 | 1404 | (SCM port, SCM line), |
e1546b65 | 1405 | "Set the current line number for @var{port} to @var{line}.") |
1bbd0b84 | 1406 | #define FUNC_NAME s_scm_set_port_line_x |
d043d8c2 | 1407 | { |
360fc44c | 1408 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 HWN |
1409 | SCM_VALIDATE_OPENPORT (1, port); |
1410 | SCM_VALIDATE_INUM (2, line); | |
564478fd GB |
1411 | SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line); |
1412 | return SCM_UNSPECIFIED; | |
d043d8c2 | 1413 | } |
1bbd0b84 | 1414 | #undef FUNC_NAME |
d043d8c2 | 1415 | |
a1ec6916 | 1416 | SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0, |
1bbd0b84 | 1417 | (SCM port), |
8f85c0c6 | 1418 | "@deffnx {Scheme Procedure} port-line port\n" |
650a1cf9 | 1419 | "Return the current column number or line number of @var{port},\n" |
b380b885 MD |
1420 | "using the current input port if none is specified. If the number is\n" |
1421 | "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n" | |
1422 | "- i.e. the first character of the first line is line 0, column 0.\n" | |
1423 | "(However, when you display a file position, for example in an error\n" | |
650a1cf9 | 1424 | "message, we recommend you add 1 to get 1-origin integers. This is\n" |
b380b885 MD |
1425 | "because lines and column numbers traditionally start with 1, and that is\n" |
1426 | "what non-programmers will find most natural.)") | |
1bbd0b84 | 1427 | #define FUNC_NAME s_scm_port_column |
0f2d19dd | 1428 | { |
78446828 | 1429 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 1430 | SCM_VALIDATE_OPENPORT (1, port); |
360fc44c | 1431 | return SCM_MAKINUM (SCM_COL (port)); |
0f2d19dd | 1432 | } |
1bbd0b84 | 1433 | #undef FUNC_NAME |
0f2d19dd | 1434 | |
a1ec6916 | 1435 | SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0, |
1bbd0b84 | 1436 | (SCM port, SCM column), |
8f85c0c6 | 1437 | "@deffnx {Scheme Procedure} set-port-line! port line\n" |
92ccc1f1 | 1438 | "Set the current column or line number of @var{port}, using the\n" |
b380b885 | 1439 | "current input port if none is specified.") |
1bbd0b84 | 1440 | #define FUNC_NAME s_scm_set_port_column_x |
d043d8c2 | 1441 | { |
360fc44c | 1442 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 HWN |
1443 | SCM_VALIDATE_OPENPORT (1, port); |
1444 | SCM_VALIDATE_INUM (2, column); | |
564478fd GB |
1445 | SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column); |
1446 | return SCM_UNSPECIFIED; | |
d043d8c2 | 1447 | } |
1bbd0b84 | 1448 | #undef FUNC_NAME |
d043d8c2 | 1449 | |
a1ec6916 | 1450 | SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0, |
1bbd0b84 | 1451 | (SCM port), |
b380b885 | 1452 | "Return the filename associated with @var{port}. This function returns\n" |
2a2a730b | 1453 | "the strings \"standard input\", \"standard output\" and \"standard error\"\n" |
a3c8b9fc | 1454 | "when called on the current input, output and error ports respectively.") |
1bbd0b84 | 1455 | #define FUNC_NAME s_scm_port_filename |
0f2d19dd | 1456 | { |
78446828 | 1457 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 1458 | SCM_VALIDATE_OPENPORT (1, port); |
b24b5e13 | 1459 | return SCM_FILENAME (port); |
0f2d19dd | 1460 | } |
1bbd0b84 | 1461 | #undef FUNC_NAME |
0f2d19dd | 1462 | |
a1ec6916 | 1463 | SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0, |
1bbd0b84 | 1464 | (SCM port, SCM filename), |
b380b885 MD |
1465 | "Change the filename associated with @var{port}, using the current input\n" |
1466 | "port if none is specified. Note that this does not change the port's\n" | |
1467 | "source of data, but only the value that is returned by\n" | |
1468 | "@code{port-filename} and reported in diagnostic output.") | |
1bbd0b84 | 1469 | #define FUNC_NAME s_scm_set_port_filename_x |
d14af9f2 | 1470 | { |
360fc44c | 1471 | port = SCM_COERCE_OUTPORT (port); |
34d19ef6 | 1472 | SCM_VALIDATE_OPENPORT (1, port); |
360fc44c | 1473 | /* We allow the user to set the filename to whatever he likes. */ |
b24b5e13 DH |
1474 | SCM_SET_FILENAME (port, filename); |
1475 | return SCM_UNSPECIFIED; | |
d14af9f2 | 1476 | } |
1bbd0b84 | 1477 | #undef FUNC_NAME |
d14af9f2 | 1478 | |
0f2d19dd JB |
1479 | #ifndef ttyname |
1480 | extern char * ttyname(); | |
1481 | #endif | |
1482 | ||
f12733c9 MD |
1483 | void |
1484 | scm_print_port_mode (SCM exp, SCM port) | |
1485 | { | |
1486 | scm_puts (SCM_CLOSEDP (exp) | |
1487 | ? "closed: " | |
f9a64404 DH |
1488 | : (SCM_RDNG & SCM_CELL_WORD_0 (exp) |
1489 | ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp) | |
f12733c9 MD |
1490 | ? "input-output: " |
1491 | : "input: ") | |
f9a64404 | 1492 | : (SCM_WRTNG & SCM_CELL_WORD_0 (exp) |
f12733c9 MD |
1493 | ? "output: " |
1494 | : "bogus: ")), | |
1495 | port); | |
1496 | } | |
1cc91f1b | 1497 | |
f12733c9 | 1498 | int |
e81d98ec | 1499 | scm_port_print (SCM exp, SCM port, scm_print_state *pstate SCM_UNUSED) |
0f2d19dd | 1500 | { |
f12733c9 MD |
1501 | char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp)); |
1502 | if (!type) | |
1503 | type = "port"; | |
b7f3516f | 1504 | scm_puts ("#<", port); |
f12733c9 | 1505 | scm_print_port_mode (exp, port); |
b7f3516f TT |
1506 | scm_puts (type, port); |
1507 | scm_putc (' ', port); | |
12a8b769 | 1508 | scm_intprint (SCM_CELL_WORD_1 (exp), 16, port); |
b7f3516f | 1509 | scm_putc ('>', port); |
f12733c9 | 1510 | return 1; |
0f2d19dd JB |
1511 | } |
1512 | ||
0f2d19dd JB |
1513 | void |
1514 | scm_ports_prehistory () | |
0f2d19dd JB |
1515 | { |
1516 | scm_numptob = 0; | |
67329a9e | 1517 | scm_ptobs = (scm_t_ptob_descriptor *) scm_malloc (sizeof (scm_t_ptob_descriptor)); |
0f2d19dd | 1518 | } |
0f2d19dd JB |
1519 | |
1520 | \f | |
ee149d03 | 1521 | |
d68fee48 | 1522 | /* Void ports. */ |
0f2d19dd | 1523 | |
92c2555f | 1524 | scm_t_bits scm_tc16_void_port = 0; |
0f2d19dd | 1525 | |
e81d98ec | 1526 | static int fill_input_void_port (SCM port SCM_UNUSED) |
283a1a0e | 1527 | { |
70df8af6 | 1528 | return EOF; |
283a1a0e GH |
1529 | } |
1530 | ||
31703ab8 | 1531 | static void |
e81d98ec DH |
1532 | write_void_port (SCM port SCM_UNUSED, |
1533 | const void *data SCM_UNUSED, | |
1534 | size_t size SCM_UNUSED) | |
31703ab8 GH |
1535 | { |
1536 | } | |
1537 | ||
0f2d19dd | 1538 | SCM |
a284e297 | 1539 | scm_void_port (char *mode_str) |
0f2d19dd | 1540 | { |
b9ad392e | 1541 | scm_mutex_lock (&scm_i_port_table_mutex); |
402788a9 HWN |
1542 | { |
1543 | int mode_bits = scm_mode_bits (mode_str); | |
da220f27 HWN |
1544 | SCM answer = scm_new_port_table_entry (scm_tc16_void_port); |
1545 | scm_t_port * pt = SCM_PTAB_ENTRY(answer); | |
1546 | ||
402788a9 | 1547 | scm_port_non_buffer (pt); |
402788a9 HWN |
1548 | |
1549 | SCM_SETSTREAM (answer, 0); | |
1550 | SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits); | |
b9ad392e | 1551 | scm_mutex_unlock (&scm_i_port_table_mutex); |
402788a9 HWN |
1552 | return answer; |
1553 | } | |
0f2d19dd JB |
1554 | } |
1555 | ||
a1ec6916 | 1556 | SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0, |
1bbd0b84 | 1557 | (SCM mode), |
70df8af6 | 1558 | "Create and return a new void port. A void port acts like\n" |
bb2c02f2 | 1559 | "@file{/dev/null}. The @var{mode} argument\n" |
70df8af6 | 1560 | "specifies the input/output modes for this port: see the\n" |
b380b885 | 1561 | "documentation for @code{open-file} in @ref{File Ports}.") |
1bbd0b84 | 1562 | #define FUNC_NAME s_scm_sys_make_void_port |
0f2d19dd | 1563 | { |
a6d9e5ab | 1564 | SCM_VALIDATE_STRING (1, mode); |
a6d9e5ab | 1565 | return scm_void_port (SCM_STRING_CHARS (mode)); |
0f2d19dd | 1566 | } |
1bbd0b84 | 1567 | #undef FUNC_NAME |
0f2d19dd | 1568 | |
0f2d19dd | 1569 | \f |
89545eba | 1570 | /* Initialization. */ |
1cc91f1b | 1571 | |
0f2d19dd JB |
1572 | void |
1573 | scm_init_ports () | |
0f2d19dd | 1574 | { |
840ae05d | 1575 | /* lseek() symbols. */ |
86d31dfe MV |
1576 | scm_c_define ("SEEK_SET", SCM_MAKINUM (SEEK_SET)); |
1577 | scm_c_define ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR)); | |
1578 | scm_c_define ("SEEK_END", SCM_MAKINUM (SEEK_END)); | |
840ae05d | 1579 | |
70df8af6 GH |
1580 | scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port, |
1581 | write_void_port); | |
a0599745 | 1582 | #include "libguile/ports.x" |
0f2d19dd | 1583 | } |
89e00824 ML |
1584 | |
1585 | /* | |
1586 | Local Variables: | |
1587 | c-file-style: "gnu" | |
1588 | End: | |
1589 | */ |