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