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