1 /* Copyright (C) 1995,1996,1997,1998,1999 Free Software Foundation, Inc.
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)
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.
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
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
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.
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
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.
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. */
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
72 #ifdef HAVE_SYS_IOCTL_H
73 #include <sys/ioctl.h>
77 /* The port kind table --- a dynamically resized array of port types. */
80 /* scm_ptobs scm_numptob
81 * implement a dynamicly resized array of ptob records.
82 * Indexes into this table are used when generating type
83 * tags for smobjects (if you know a tag you can get an index and conversely).
85 scm_ptob_descriptor
*scm_ptobs
;
88 /* GC marker for a port with stream of SCM type. */
90 scm_markstream (SCM ptr
)
93 openp
= SCM_UNPACK_CAR (ptr
) & SCM_OPN
;
95 return SCM_STREAM (ptr
);
101 * We choose to use an interface similar to the smob interface with
102 * fill_input and write as standard fields, passed to the port
103 * type constructor, and optional fields set by setters.
107 flush_port_default (SCM port
)
112 end_input_default (SCM port
, int offset
)
117 scm_make_port_type (char *name
,
118 int (*fill_input
) (SCM port
),
119 void (*write
) (SCM port
, const void *data
, size_t size
))
122 if (255 <= scm_numptob
)
125 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
,
127 * sizeof (scm_ptob_descriptor
)));
130 scm_ptobs
= (scm_ptob_descriptor
*) tmp
;
132 scm_ptobs
[scm_numptob
].name
= name
;
133 scm_ptobs
[scm_numptob
].mark
= 0;
134 scm_ptobs
[scm_numptob
].free
= scm_free0
;
135 scm_ptobs
[scm_numptob
].print
= scm_port_print
;
136 scm_ptobs
[scm_numptob
].equalp
= 0;
137 scm_ptobs
[scm_numptob
].close
= 0;
139 scm_ptobs
[scm_numptob
].write
= write
;
140 scm_ptobs
[scm_numptob
].flush
= flush_port_default
;
142 scm_ptobs
[scm_numptob
].end_input
= end_input_default
;
143 scm_ptobs
[scm_numptob
].fill_input
= fill_input
;
144 scm_ptobs
[scm_numptob
].input_waiting
= 0;
146 scm_ptobs
[scm_numptob
].seek
= 0;
147 scm_ptobs
[scm_numptob
].truncate
= 0;
153 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
),
154 (char *) SCM_NALLOC
, "scm_make_port_type");
155 /* Make a class object if Goops is present */
157 scm_make_port_classes (scm_numptob
- 1, SCM_PTOBNAME (scm_numptob
- 1));
158 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
162 scm_set_port_mark (long tc
, SCM (*mark
) (SCM
))
164 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].mark
= mark
;
168 scm_set_port_free (long tc
, scm_sizet (*free
) (SCM
))
170 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].free
= free
;
174 scm_set_port_print (long tc
, int (*print
) (SCM exp
, SCM port
,
175 scm_print_state
*pstate
))
177 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].print
= print
;
181 scm_set_port_equalp (long tc
, SCM (*equalp
) (SCM
, SCM
))
183 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].equalp
= equalp
;
187 scm_set_port_flush (long tc
, void (*flush
) (SCM port
))
189 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].flush
= flush
;
193 scm_set_port_end_input (long tc
, void (*end_input
) (SCM port
, int offset
))
195 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].end_input
= end_input
;
199 scm_set_port_close (long tc
, int (*close
) (SCM
))
201 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].close
= close
;
205 scm_set_port_seek (long tc
, off_t (*seek
) (SCM port
,
209 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].seek
= seek
;
213 scm_set_port_truncate (long tc
, void (*truncate
) (SCM port
, off_t length
))
215 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].truncate
= truncate
;
219 scm_set_port_input_waiting (long tc
, int (*input_waiting
) (SCM
))
221 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].input_waiting
= input_waiting
;
226 SCM_DEFINE (scm_char_ready_p
, "char-ready?", 0, 1, 0,
228 "Returns @code{#t} if a character is ready on input @var{port} and\n"
229 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
230 "then the next @code{read-char} operation on @var{port} is\n"
231 "guaranteed not to hang. If @var{port} is a file port at end of\n"
232 "file then @code{char-ready?} returns @code{#t}.\n"
233 "@footnote{@code{char-ready?} exists to make it possible for a\n"
234 "program to accept characters from interactive ports without getting\n"
235 "stuck waiting for input. Any input editors associated with such ports\n"
236 "must make sure that characters whose existence has been asserted by\n"
237 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
238 "return @code{#f} at end of file, a port at end of file would be\n"
239 "indistinguishable from an interactive port that has no ready\n"
241 #define FUNC_NAME s_scm_char_ready_p
245 if (SCM_UNBNDP (port
))
248 SCM_VALIDATE_OPINPORT (1,port
);
250 pt
= SCM_PTAB_ENTRY (port
);
252 /* if the current read buffer is filled, or the
253 last pushed-back char has been read and the saved buffer is
254 filled, result is true. */
255 if (pt
->read_pos
< pt
->read_end
256 || (pt
->read_buf
== pt
->putback_buf
257 && pt
->saved_read_pos
< pt
->saved_read_end
))
261 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
263 if (ptob
->input_waiting
)
264 return SCM_BOOL(ptob
->input_waiting (port
));
271 /* Clear a port's read buffers, returning the contents. */
272 SCM_DEFINE (scm_drain_input
, "drain-input", 1, 0, 0,
274 "Drains @var{PORT}'s read buffers (including any pushed-back characters)\n"
275 "and returns the contents as a single string.")
276 #define FUNC_NAME s_scm_drain_input
279 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
283 SCM_VALIDATE_OPINPORT (1,port
);
285 count
= pt
->read_end
- pt
->read_pos
;
286 if (pt
->read_buf
== pt
->putback_buf
)
287 count
+= pt
->saved_read_end
- pt
->saved_read_pos
;
289 result
= scm_makstr (count
, 0);
290 dst
= SCM_CHARS (result
);
292 while (pt
->read_pos
< pt
->read_end
)
293 *dst
++ = *(pt
->read_pos
++);
295 if (pt
->read_buf
== pt
->putback_buf
)
297 while (pt
->saved_read_pos
< pt
->saved_read_end
)
298 *dst
++ = *(pt
->saved_read_pos
++);
306 /* Standard ports --- current input, output, error, and more(!). */
308 SCM_DEFINE (scm_current_input_port
, "current-input-port", 0, 0, 0,
310 "Returns the current input port. This is the default port used by many\n"
311 "input procedures. Initially, @code{current-input-port} returns the\n"
312 "value of @code{???}.")
313 #define FUNC_NAME s_scm_current_input_port
319 SCM_DEFINE (scm_current_output_port
, "current-output-port", 0, 0, 0,
321 "Returns the current output port. This is the default port used by many\n"
322 "output procedures. Initially, @code{current-output-port} returns the\n"
323 "value of @code{???}.")
324 #define FUNC_NAME s_scm_current_output_port
330 SCM_DEFINE (scm_current_error_port
, "current-error-port", 0, 0, 0,
332 "Return the port to which errors and warnings should be sent (the\n"
333 "@dfn{standard error} in Unix and C terminology).")
334 #define FUNC_NAME s_scm_current_error_port
340 SCM_DEFINE (scm_current_load_port
, "current-load-port", 0, 0, 0,
342 "Return the current-load-port.\n"
343 "The load port is used internally by `primitive-load'.")
344 #define FUNC_NAME s_scm_current_load_port
346 return scm_cur_loadp
;
350 SCM_DEFINE (scm_set_current_input_port
, "set-current-input-port", 1, 0, 0,
352 "@deffnx primitive set-current-output-port port\n"
353 "@deffnx primitive set-current-error-port port\n"
354 "Change the ports returned by @code{current-input-port},\n"
355 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
356 "so that they use the supplied @var{port} for input or output.")
357 #define FUNC_NAME s_scm_set_current_input_port
359 SCM oinp
= scm_cur_inp
;
360 SCM_VALIDATE_OPINPORT (1,port
);
367 SCM_DEFINE (scm_set_current_output_port
, "set-current-output-port", 1, 0, 0,
369 "Set the current default output port to PORT.")
370 #define FUNC_NAME s_scm_set_current_output_port
372 SCM ooutp
= scm_cur_outp
;
373 port
= SCM_COERCE_OUTPORT (port
);
374 SCM_VALIDATE_OPOUTPORT (1,port
);
381 SCM_DEFINE (scm_set_current_error_port
, "set-current-error-port", 1, 0, 0,
383 "Set the current default error port to PORT.")
384 #define FUNC_NAME s_scm_set_current_error_port
386 SCM oerrp
= scm_cur_errp
;
387 port
= SCM_COERCE_OUTPORT (port
);
388 SCM_VALIDATE_OPOUTPORT (1,port
);
395 /* The port table --- an array of pointers to ports. */
397 scm_port
**scm_port_table
;
399 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
400 int scm_port_table_room
= 20; /* Size of the array. */
402 /* Add a port to the table. */
405 scm_add_to_port_table (SCM port
)
409 if (scm_port_table_size
== scm_port_table_room
)
411 void *newt
= realloc ((char *) scm_port_table
,
412 (scm_sizet
) (sizeof (scm_port
*)
413 * scm_port_table_room
* 2));
415 scm_memory_error ("scm_add_to_port_table");
416 scm_port_table
= (scm_port
**) newt
;
417 scm_port_table_room
*= 2;
419 entry
= (scm_port
*) malloc (sizeof (scm_port
));
421 scm_memory_error ("scm_add_to_port_table");
424 entry
->entry
= scm_port_table_size
;
427 entry
->file_name
= SCM_BOOL_F
;
428 entry
->line_number
= 0;
429 entry
->column_number
= 0;
430 entry
->putback_buf
= 0;
431 entry
->putback_buf_size
= 0;
432 entry
->rw_active
= SCM_PORT_NEITHER
;
433 entry
->rw_random
= 0;
435 scm_port_table
[scm_port_table_size
] = entry
;
436 scm_port_table_size
++;
441 /* Remove a port from the table and destroy it. */
444 scm_remove_from_port_table (SCM port
)
446 scm_port
*p
= SCM_PTAB_ENTRY (port
);
449 if (i
>= scm_port_table_size
)
450 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
452 free (p
->putback_buf
);
454 /* Since we have just freed slot i we can shrink the table by moving
455 the last entry to that slot... */
456 if (i
< scm_port_table_size
- 1)
458 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
459 scm_port_table
[i
]->entry
= i
;
461 SCM_SETPTAB_ENTRY (port
, 0);
462 scm_port_table_size
--;
466 /* Functions for debugging. */
468 SCM_DEFINE (scm_pt_size
, "pt-size", 0, 0, 0,
470 "Returns the number of ports in the port table.\n"
471 "`pt-size' is only included in GUILE_DEBUG builds.")
472 #define FUNC_NAME s_scm_pt_size
474 return SCM_MAKINUM (scm_port_table_size
);
478 SCM_DEFINE (scm_pt_member
, "pt-member", 1, 0, 0,
480 "Returns the port at INDEX in the port table.\n"
481 "`pt-member' is only included in GUILE_DEBUG builds.")
482 #define FUNC_NAME s_scm_pt_member
485 SCM_VALIDATE_INUM_COPY (1,index
,i
);
486 if (i
< 0 || i
>= scm_port_table_size
)
489 return scm_port_table
[i
]->port
;
495 scm_port_non_buffer (scm_port
*pt
)
497 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
498 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
499 pt
->read_buf_size
= pt
->write_buf_size
= 1;
500 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
504 /* Revealed counts --- an oddity inherited from SCSH. */
506 /* Find a port in the table and return its revealed count.
507 Also used by the garbage collector.
511 scm_revealed_count (SCM port
)
513 return SCM_REVEALED(port
);
518 /* Return the revealed count for a port. */
520 SCM_DEFINE (scm_port_revealed
, "port-revealed", 1, 0, 0,
522 "Returns the revealed count for @var{port}.")
523 #define FUNC_NAME s_scm_port_revealed
525 port
= SCM_COERCE_OUTPORT (port
);
526 SCM_VALIDATE_PORT (1,port
);
527 return SCM_MAKINUM (scm_revealed_count (port
));
531 /* Set the revealed count for a port. */
532 SCM_DEFINE (scm_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0,
533 (SCM port
, SCM rcount
),
534 "Sets the revealed count for a port to a given value.\n"
535 "The return value is unspecified.")
536 #define FUNC_NAME s_scm_set_port_revealed_x
538 port
= SCM_COERCE_OUTPORT (port
);
539 SCM_VALIDATE_PORT (1,port
);
540 SCM_VALIDATE_INUM (2,rcount
);
541 SCM_REVEALED (port
) = SCM_INUM (rcount
);
542 return SCM_UNSPECIFIED
;
548 /* Retrieving a port's mode. */
550 /* Return the flags that characterize a port based on the mode
551 * string used to open a file for that port.
553 * See PORT FLAGS in scm.h
557 scm_mode_bits (char *modes
)
560 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
561 | ( strchr (modes
, 'w')
562 || strchr (modes
, 'a')
563 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
564 | (strchr (modes
, '0') ? SCM_BUF0
: 0)
565 | (strchr (modes
, 'l') ? SCM_BUFLINE
: 0));
569 /* Return the mode flags from an open port.
570 * Some modes such as "append" are only used when opening
571 * a file and are not returned here. */
573 SCM_DEFINE (scm_port_mode
, "port-mode", 1, 0, 0,
575 "Returns the port modes associated with the open port @var{port}. These\n"
576 "will not necessarily be identical to the modes used when the port was\n"
577 "opened, since modes such as \"append\" which are used only during\n"
578 "port creation are not retained.")
579 #define FUNC_NAME s_scm_port_mode
584 port
= SCM_COERCE_OUTPORT (port
);
585 SCM_VALIDATE_OPPORT (1,port
);
586 if (SCM_UNPACK_CAR (port
) & SCM_RDNG
) {
587 if (SCM_UNPACK_CAR (port
) & SCM_WRTNG
)
588 strcpy (modes
, "r+");
592 else if (SCM_UNPACK_CAR (port
) & SCM_WRTNG
)
594 if (SCM_UNPACK_CAR (port
) & SCM_BUF0
)
596 return scm_makfromstr (modes
, strlen (modes
), 0);
605 * Call the close operation on a port object.
606 * see also scm_close.
608 SCM_DEFINE (scm_close_port
, "close-port", 1, 0, 0,
610 "Close the specified port object. Returns @code{#t} if it successfully\n"
611 "closes a port or @code{#f} if it was already\n"
612 "closed. An exception may be raised if an error occurs, for example\n"
613 "when flushing buffered output.\n"
614 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
615 "which can close file descriptors.")
616 #define FUNC_NAME s_scm_close_port
621 port
= SCM_COERCE_OUTPORT (port
);
623 SCM_VALIDATE_PORT (1,port
);
624 if (SCM_CLOSEDP (port
))
626 i
= SCM_PTOBNUM (port
);
627 if (scm_ptobs
[i
].close
)
628 rv
= (scm_ptobs
[i
].close
) (port
);
631 scm_remove_from_port_table (port
);
632 SCM_SETAND_CAR (port
, ~SCM_OPN
);
633 return SCM_NEGATE_BOOL(rv
< 0);
637 SCM_DEFINE (scm_close_all_ports_except
, "close-all-ports-except", 0, 0, 1,
639 "Close all open file ports used by the interpreter\n"
640 "except for those supplied as arguments. This procedure\n"
641 "is intended to be used before an exec call to close file descriptors\n"
642 "which are not needed in the new process.Close all open file ports used by the interpreter\n"
643 "except for those supplied as arguments. This procedure\n"
644 "is intended to be used before an exec call to close file descriptors\n"
645 "which are not needed in the new process.")
646 #define FUNC_NAME s_scm_close_all_ports_except
649 SCM_VALIDATE_CONS (1,ports
);
650 while (i
< scm_port_table_size
)
652 SCM thisport
= scm_port_table
[i
]->port
;
654 SCM ports_ptr
= ports
;
656 while (SCM_NNULLP (ports_ptr
))
658 SCM port
= SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr
));
660 SCM_VALIDATE_OPPORT (SCM_ARG1
,port
);
661 if (port
== thisport
)
663 ports_ptr
= SCM_CDR (ports_ptr
);
668 /* i is not to be incremented here. */
669 scm_close_port (thisport
);
671 return SCM_UNSPECIFIED
;
677 /* Utter miscellany. Gosh, we should clean this up some time. */
679 SCM_DEFINE (scm_input_port_p
, "input-port?", 1, 0, 0,
681 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
682 "@code{#f}. Any object satisfying this predicate also satisfies\n"
684 #define FUNC_NAME s_scm_input_port_p
688 return SCM_BOOL(SCM_INPORTP (x
));
692 SCM_DEFINE (scm_output_port_p
, "output-port?", 1, 0, 0,
694 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
695 "@code{#f}. Any object satisfying this predicate also satisfies\n"
697 #define FUNC_NAME s_scm_output_port_p
701 if (SCM_PORT_WITH_PS_P (x
))
702 x
= SCM_PORT_WITH_PS_PORT (x
);
703 return SCM_BOOL(SCM_OUTPORTP (x
));
707 SCM_DEFINE (scm_port_closed_p
, "port-closed?", 1, 0, 0,
709 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
710 #define FUNC_NAME s_scm_port_closed_p
712 SCM_VALIDATE_PORT (1,port
);
713 return SCM_NEGATE_BOOL(SCM_OPPORTP (port
));
717 SCM_DEFINE (scm_eof_object_p
, "eof-object?", 1, 0, 0,
719 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
720 "returns @code{#f}.")
721 #define FUNC_NAME s_scm_eof_object_p
723 return SCM_BOOL(SCM_EOF_OBJECT_P (x
));
727 SCM_DEFINE (scm_force_output
, "force-output", 0, 1, 0,
729 "Flush the specified output port, or the current output port if @var{port}\n"
730 "is omitted. The current output buffer contents are passed to the \n"
731 "underlying port implementation (e.g., in the case of fports, the\n"
732 "data will be written to the file and the output buffer will be cleared.)\n"
733 "It has no effect on an unbuffered port.\n\n"
734 "The return value is unspecified.")
735 #define FUNC_NAME s_scm_force_output
737 if (SCM_UNBNDP (port
))
741 port
= SCM_COERCE_OUTPORT (port
);
742 SCM_VALIDATE_OPOUTPORT (1,port
);
745 return SCM_UNSPECIFIED
;
749 SCM_DEFINE (scm_flush_all_ports
, "flush-all-ports", 0, 0, 0,
751 "Equivalent to calling @code{force-output} on\n"
752 "all open output ports. The return value is unspecified.")
753 #define FUNC_NAME s_scm_flush_all_ports
757 for (i
= 0; i
< scm_port_table_size
; i
++)
759 if (SCM_OPOUTPORTP (scm_port_table
[i
]->port
))
760 scm_flush (scm_port_table
[i
]->port
);
762 return SCM_UNSPECIFIED
;
766 SCM_DEFINE (scm_read_char
, "read-char", 0, 1, 0,
768 "Returns the next character available from @var{port}, updating\n"
769 "@var{port} to point to the following character. If no more\n"
770 "characters are available, an end-of-file object is returned.")
771 #define FUNC_NAME s_scm_read_char
774 if (SCM_UNBNDP (port
))
776 SCM_VALIDATE_OPINPORT (1,port
);
780 return SCM_MAKE_CHAR (c
);
784 /* this should only be called when the read buffer is empty. it
785 tries to refill the read buffer. it returns the first char from
786 the port, which is either EOF or *(pt->read_pos). */
788 scm_fill_input (SCM port
)
790 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
792 if (pt
->read_buf
== pt
->putback_buf
)
794 /* finished reading put-back chars. */
795 pt
->read_buf
= pt
->saved_read_buf
;
796 pt
->read_pos
= pt
->saved_read_pos
;
797 pt
->read_end
= pt
->saved_read_end
;
798 pt
->read_buf_size
= pt
->saved_read_buf_size
;
799 if (pt
->read_pos
< pt
->read_end
)
800 return *(pt
->read_pos
);
802 return scm_ptobs
[SCM_PTOBNUM (port
)].fill_input (port
);
809 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
811 if (pt
->rw_active
== SCM_PORT_WRITE
)
813 /* may be marginally faster than calling scm_flush. */
814 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
818 pt
->rw_active
= SCM_PORT_READ
;
820 if (pt
->read_pos
>= pt
->read_end
)
822 if (scm_fill_input (port
) == EOF
)
826 c
= *(pt
->read_pos
++);
845 scm_putc (char c
, SCM port
)
847 scm_lfwrite (&c
, 1, port
);
851 scm_puts (const char *s
, SCM port
)
853 scm_lfwrite (s
, strlen (s
), port
);
857 scm_lfwrite (const char *ptr
, scm_sizet size
, SCM port
)
859 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
860 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
862 if (pt
->rw_active
== SCM_PORT_READ
)
863 scm_end_input (port
);
865 ptob
->write (port
, ptr
, size
);
868 pt
->rw_active
= SCM_PORT_WRITE
;
875 scm_sizet i
= SCM_PTOBNUM (port
);
876 (scm_ptobs
[i
].flush
) (port
);
880 scm_end_input (SCM port
)
883 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
885 if (pt
->read_buf
== pt
->putback_buf
)
887 offset
= pt
->read_end
- pt
->read_pos
;
888 pt
->read_buf
= pt
->saved_read_buf
;
889 pt
->read_pos
= pt
->saved_read_pos
;
890 pt
->read_end
= pt
->saved_read_end
;
891 pt
->read_buf_size
= pt
->saved_read_buf_size
;
896 scm_ptobs
[SCM_PTOBNUM (port
)].end_input (port
, offset
);
903 scm_ungetc (int c
, SCM port
)
905 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
907 if (pt
->read_buf
== pt
->putback_buf
)
908 /* already using the put-back buffer. */
910 /* enlarge putback_buf if necessary. */
911 if (pt
->read_end
== pt
->read_buf
+ pt
->read_buf_size
912 && pt
->read_buf
== pt
->read_pos
)
914 int new_size
= pt
->read_buf_size
* 2;
916 (unsigned char *) realloc (pt
->putback_buf
, new_size
);
919 scm_memory_error ("scm_ungetc");
920 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
= tmp
;
921 pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
922 pt
->read_buf_size
= pt
->putback_buf_size
= new_size
;
925 /* shift any existing bytes to buffer + 1. */
926 if (pt
->read_pos
== pt
->read_end
)
927 pt
->read_end
= pt
->read_buf
+ 1;
928 else if (pt
->read_pos
!= pt
->read_buf
+ 1)
930 int count
= pt
->read_end
- pt
->read_pos
;
932 memmove (pt
->read_buf
+ 1, pt
->read_pos
, count
);
933 pt
->read_end
= pt
->read_buf
+ 1 + count
;
936 pt
->read_pos
= pt
->read_buf
;
939 /* switch to the put-back buffer. */
941 if (pt
->putback_buf
== NULL
)
943 pt
->putback_buf
= (char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE
);
944 if (pt
->putback_buf
== NULL
)
945 scm_memory_error ("scm_ungetc");
946 pt
->putback_buf_size
= SCM_INITIAL_PUTBACK_BUF_SIZE
;
949 pt
->saved_read_buf
= pt
->read_buf
;
950 pt
->saved_read_pos
= pt
->read_pos
;
951 pt
->saved_read_end
= pt
->read_end
;
952 pt
->saved_read_buf_size
= pt
->read_buf_size
;
954 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
;
955 pt
->read_end
= pt
->read_buf
+ 1;
956 pt
->read_buf_size
= pt
->putback_buf_size
;
962 pt
->rw_active
= SCM_PORT_READ
;
966 /* What should col be in this case?
967 * We'll leave it at -1.
969 SCM_LINUM (port
) -= 1;
977 scm_ungets (const char *s
, int n
, SCM port
)
979 /* This is simple minded and inefficient, but unreading strings is
980 * probably not a common operation, and remember that line and
981 * column numbers have to be handled...
983 * Please feel free to write an optimized version!
986 scm_ungetc (s
[n
], port
);
990 SCM_DEFINE (scm_peek_char
, "peek-char", 0, 1, 0,
992 "Returns the next character available from @var{port},\n"
993 "@emph{without} updating @var{port} to point to the following\n"
994 "character. If no more characters are available, an end-of-file object\n"
995 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
996 "is the same as the value that would have been returned by a call to\n"
997 "@code{read-char} on the same port. The only difference is that the very\n"
998 "next call to @code{read-char} or @code{peek-char} on that\n"
999 "@var{port} will return the value returned by the preceding call to\n"
1000 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1001 "interactive port will hang waiting for input whenever a call to\n"
1002 "@code{read-char} would have hung.}")
1003 #define FUNC_NAME s_scm_peek_char
1006 if (SCM_UNBNDP (port
))
1009 SCM_VALIDATE_OPINPORT (1,port
);
1010 c
= scm_getc (port
);
1013 scm_ungetc (c
, port
);
1014 return SCM_MAKE_CHAR (c
);
1018 SCM_DEFINE (scm_unread_char
, "unread-char", 2, 0, 0,
1019 (SCM cobj
, SCM port
),
1020 "Place @var{char} in @var{port} so that it will be read by the\n"
1021 "next read operation. If called multiple times, the unread characters\n"
1022 "will be read again in last-in first-out order. If @var{port} is\n"
1023 "not supplied, the current input port is used.")
1024 #define FUNC_NAME s_scm_unread_char
1028 SCM_VALIDATE_CHAR (1,cobj
);
1029 if (SCM_UNBNDP (port
))
1032 SCM_VALIDATE_OPINPORT (2,port
);
1034 c
= SCM_CHAR (cobj
);
1036 scm_ungetc (c
, port
);
1041 SCM_DEFINE (scm_unread_string
, "unread-string", 2, 0, 0,
1042 (SCM str
, SCM port
),
1043 "Place the string @var{str} in @var{port} so that its characters will be\n"
1044 "read in subsequent read operations. If called multiple times, the\n"
1045 "unread characters will be read again in last-in first-out order. If\n"
1046 "@var{port} is not supplied, the current-input-port is used.")
1047 #define FUNC_NAME s_scm_unread_string
1049 SCM_VALIDATE_STRING (1,str
);
1050 if (SCM_UNBNDP (port
))
1053 SCM_VALIDATE_OPINPORT (2,port
);
1055 scm_ungets (SCM_ROUCHARS (str
), SCM_LENGTH (str
), port
);
1061 SCM_DEFINE (scm_seek
, "seek", 3, 0, 0,
1062 (SCM object
, SCM offset
, SCM whence
),
1063 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1064 "which is interpreted according to the value of @var{whence}.\n\n"
1065 "One of the following variables should be supplied\n"
1066 "for @var{whence}:\n"
1067 "@defvar SEEK_SET\n"
1068 "Seek from the beginning of the file.\n"
1070 "@defvar SEEK_CUR\n"
1071 "Seek from the current position.\n"
1073 "@defvar SEEK_END\n"
1074 "Seek from the end of the file.\n"
1076 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1077 "@code{lseek}. @var{port} may be a string port.\n\n"
1078 "The value returned is the new position in the file. This means that\n"
1079 "the current position of a port can be obtained using:\n"
1081 "(seek port 0 SEEK_CUR)\n"
1083 #define FUNC_NAME s_scm_seek
1089 object
= SCM_COERCE_OUTPORT (object
);
1091 off
= SCM_NUM2LONG (2,offset
);
1092 SCM_VALIDATE_INUM_COPY (3,whence
,how
);
1093 if (how
!= SEEK_SET
&& how
!= SEEK_CUR
&& how
!= SEEK_END
)
1094 SCM_OUT_OF_RANGE (3, whence
);
1095 if (SCM_OPPORTP (object
))
1097 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1100 SCM_MISC_ERROR ("port is not seekable",
1101 scm_cons (object
, SCM_EOL
));
1103 rv
= ptob
->seek (object
, off
, how
);
1105 else /* file descriptor?. */
1107 SCM_VALIDATE_INUM (1,object
);
1108 rv
= lseek (SCM_INUM (object
), off
, how
);
1112 return scm_long2num (rv
);
1116 SCM_DEFINE (scm_truncate_file
, "truncate-file", 1, 1, 0,
1117 (SCM object
, SCM length
),
1118 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1119 "@var{obj} can be a string containing a file name or an integer file\n"
1120 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1121 "a file name, in which case the truncation occurs at the current port.\n"
1123 "The return value is unspecified.")
1124 #define FUNC_NAME s_scm_truncate_file
1129 /* object can be a port, fdes or filename. */
1131 if (SCM_UNBNDP (length
))
1133 /* must supply length if object is a filename. */
1134 if (SCM_ROSTRINGP (object
))
1135 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL
);
1137 length
= scm_seek (object
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
1139 c_length
= SCM_NUM2LONG (2,length
);
1141 SCM_MISC_ERROR ("negative offset", SCM_EOL
);
1143 object
= SCM_COERCE_OUTPORT (object
);
1144 if (SCM_INUMP (object
))
1146 SCM_SYSCALL (rv
= ftruncate (SCM_INUM (object
), c_length
));
1148 else if (SCM_OPOUTPORTP (object
))
1150 scm_port
*pt
= SCM_PTAB_ENTRY (object
);
1151 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1153 if (!ptob
->truncate
)
1154 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL
);
1155 if (pt
->rw_active
== SCM_PORT_READ
)
1156 scm_end_input (object
);
1157 else if (pt
->rw_active
== SCM_PORT_WRITE
)
1158 ptob
->flush (object
);
1160 ptob
->truncate (object
, c_length
);
1165 SCM_VALIDATE_ROSTRING (1,object
);
1166 SCM_COERCE_SUBSTR (object
);
1167 SCM_SYSCALL (rv
= truncate (SCM_ROCHARS (object
), c_length
));
1171 return SCM_UNSPECIFIED
;
1175 SCM_DEFINE (scm_port_line
, "port-line", 1, 0, 0,
1177 "Return the current line number for PORT.")
1178 #define FUNC_NAME s_scm_port_line
1180 port
= SCM_COERCE_OUTPORT (port
);
1181 SCM_VALIDATE_OPENPORT (1,port
);
1182 return SCM_MAKINUM (SCM_LINUM (port
));
1186 SCM_DEFINE (scm_set_port_line_x
, "set-port-line!", 2, 0, 0,
1187 (SCM port
, SCM line
),
1188 "Set the current line number for PORT to LINE.")
1189 #define FUNC_NAME s_scm_set_port_line_x
1191 port
= SCM_COERCE_OUTPORT (port
);
1192 SCM_VALIDATE_OPENPORT (1,port
);
1193 SCM_VALIDATE_INUM (2,line
);
1194 SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
1195 return SCM_UNSPECIFIED
;
1199 SCM_DEFINE (scm_port_column
, "port-column", 1, 0, 0,
1201 "@deffnx primitive port-line [input-port]\n"
1202 "Return the current column number or line number of @var{input-port},\n"
1203 "using the current input port if none is specified. If the number is\n"
1204 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1205 "- i.e. the first character of the first line is line 0, column 0.\n"
1206 "(However, when you display a file position, for example in an error\n"
1207 "message, we recommand you add 1 to get 1-origin integers. This is\n"
1208 "because lines and column numbers traditionally start with 1, and that is\n"
1209 "what non-programmers will find most natural.)")
1210 #define FUNC_NAME s_scm_port_column
1212 port
= SCM_COERCE_OUTPORT (port
);
1213 SCM_VALIDATE_OPENPORT (1,port
);
1214 return SCM_MAKINUM (SCM_COL (port
));
1218 SCM_DEFINE (scm_set_port_column_x
, "set-port-column!", 2, 0, 0,
1219 (SCM port
, SCM column
),
1220 "@deffnx primitive set-port-column! [input-port] column\n"
1221 "Set the current column or line number of @var{input-port}, using the\n"
1222 "current input port if none is specified.")
1223 #define FUNC_NAME s_scm_set_port_column_x
1225 port
= SCM_COERCE_OUTPORT (port
);
1226 SCM_VALIDATE_OPENPORT (1,port
);
1227 SCM_VALIDATE_INUM (2,column
);
1228 SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
1229 return SCM_UNSPECIFIED
;
1233 SCM_DEFINE (scm_port_filename
, "port-filename", 1, 0, 0,
1235 "Return the filename associated with @var{port}. This function returns\n"
1236 "the strings \"standard input\", \"standard output\" and \"standard error\""
1237 "when called on the current input, output and error ports respectively.")
1238 #define FUNC_NAME s_scm_port_filename
1240 port
= SCM_COERCE_OUTPORT (port
);
1241 SCM_VALIDATE_OPENPORT (1,port
);
1242 return SCM_PTAB_ENTRY (port
)->file_name
;
1246 SCM_DEFINE (scm_set_port_filename_x
, "set-port-filename!", 2, 0, 0,
1247 (SCM port
, SCM filename
),
1248 "Change the filename associated with @var{port}, using the current input\n"
1249 "port if none is specified. Note that this does not change the port's\n"
1250 "source of data, but only the value that is returned by\n"
1251 "@code{port-filename} and reported in diagnostic output.")
1252 #define FUNC_NAME s_scm_set_port_filename_x
1254 port
= SCM_COERCE_OUTPORT (port
);
1255 SCM_VALIDATE_OPENPORT (1,port
);
1256 /* We allow the user to set the filename to whatever he likes. */
1257 return SCM_PTAB_ENTRY (port
)->file_name
= filename
;
1262 extern char * ttyname();
1266 scm_print_port_mode (SCM exp
, SCM port
)
1268 scm_puts (SCM_CLOSEDP (exp
)
1270 : (SCM_RDNG
& SCM_UNPACK_CAR (exp
)
1271 ? (SCM_WRTNG
& SCM_UNPACK_CAR (exp
)
1274 : (SCM_WRTNG
& SCM_UNPACK_CAR (exp
)
1281 scm_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1283 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
1286 scm_puts ("#<", port
);
1287 scm_print_port_mode (exp
, port
);
1288 scm_puts (type
, port
);
1289 scm_putc (' ', port
);
1290 scm_intprint ((int) SCM_CDR (exp
), 16, port
);
1291 scm_putc ('>', port
);
1295 extern void scm_make_fptob ();
1296 extern void scm_make_stptob ();
1297 extern void scm_make_sfptob ();
1300 scm_ports_prehistory ()
1303 scm_ptobs
= (scm_ptob_descriptor
*) malloc (sizeof (scm_ptob_descriptor
));
1305 /* WARNING: These scm_newptob calls must be done in this order.
1306 * They must agree with the port declarations in tags.h.
1308 /* scm_tc16_fport = */ scm_make_fptob ();
1309 /* scm_tc16_pipe was here */ scm_make_fptob (); /* dummy. */
1310 /* scm_tc16_strport = */ scm_make_stptob ();
1311 /* scm_tc16_sfport = */ scm_make_sfptob ();
1318 long scm_tc16_void_port
= 0;
1320 static int fill_input_void_port (SCM port
)
1326 write_void_port (SCM port
, const void *data
, size_t size
)
1331 scm_void_port (char *mode_str
)
1337 SCM_NEWCELL (answer
);
1339 mode_bits
= scm_mode_bits (mode_str
);
1340 pt
= scm_add_to_port_table (answer
);
1341 scm_port_non_buffer (pt
);
1342 SCM_SETPTAB_ENTRY (answer
, pt
);
1343 SCM_SETSTREAM (answer
, 0);
1344 SCM_SETCAR (answer
, scm_tc16_void_port
| mode_bits
);
1349 SCM_DEFINE (scm_sys_make_void_port
, "%make-void-port", 1, 0, 0,
1351 "Create and return a new void port. A void port acts like\n"
1352 "/dev/null. The @var{mode} argument\n"
1353 "specifies the input/output modes for this port: see the\n"
1354 "documentation for @code{open-file} in @ref{File Ports}.")
1355 #define FUNC_NAME s_scm_sys_make_void_port
1357 SCM_VALIDATE_ROSTRING (1,mode
);
1358 SCM_COERCE_SUBSTR (mode
);
1359 return scm_void_port (SCM_ROCHARS (mode
));
1364 /* Initialization. */
1369 /* lseek() symbols. */
1370 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
1371 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
1372 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
1374 scm_tc16_void_port
= scm_make_port_type ("void", fill_input_void_port
,