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