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