1 /* Copyright (C) 1995,1996,1997,1998,1999, 2000, 2001 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 */
51 #include "libguile/_scm.h"
52 #include "libguile/eval.h"
53 #include "libguile/objects.h"
54 #include "libguile/smob.h"
55 #include "libguile/chars.h"
57 #include "libguile/keywords.h"
58 #include "libguile/root.h"
59 #include "libguile/strings.h"
61 #include "libguile/validate.h"
62 #include "libguile/ports.h"
76 #ifdef HAVE_SYS_IOCTL_H
77 #include <sys/ioctl.h>
81 /* The port kind table --- a dynamically resized array of port types. */
84 /* scm_ptobs scm_numptob
85 * implement a dynamicly resized array of ptob records.
86 * Indexes into this table are used when generating type
87 * tags for smobjects (if you know a tag you can get an index and conversely).
89 scm_ptob_descriptor
*scm_ptobs
;
92 /* GC marker for a port with stream of SCM type. */
94 scm_markstream (SCM ptr
)
97 openp
= SCM_CELL_WORD_0 (ptr
) & SCM_OPN
;
99 return SCM_PACK (SCM_STREAM (ptr
));
105 * We choose to use an interface similar to the smob interface with
106 * fill_input and write as standard fields, passed to the port
107 * type constructor, and optional fields set by setters.
111 flush_port_default (SCM port
)
116 end_input_default (SCM port
, int offset
)
121 scm_make_port_type (char *name
,
122 int (*fill_input
) (SCM port
),
123 void (*write
) (SCM port
, const void *data
, size_t size
))
126 if (255 <= scm_numptob
)
129 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
,
131 * sizeof (scm_ptob_descriptor
)));
134 scm_ptobs
= (scm_ptob_descriptor
*) tmp
;
136 scm_ptobs
[scm_numptob
].name
= name
;
137 scm_ptobs
[scm_numptob
].mark
= 0;
138 scm_ptobs
[scm_numptob
].free
= scm_free0
;
139 scm_ptobs
[scm_numptob
].print
= scm_port_print
;
140 scm_ptobs
[scm_numptob
].equalp
= 0;
141 scm_ptobs
[scm_numptob
].close
= 0;
143 scm_ptobs
[scm_numptob
].write
= write
;
144 scm_ptobs
[scm_numptob
].flush
= flush_port_default
;
146 scm_ptobs
[scm_numptob
].end_input
= end_input_default
;
147 scm_ptobs
[scm_numptob
].fill_input
= fill_input
;
148 scm_ptobs
[scm_numptob
].input_waiting
= 0;
150 scm_ptobs
[scm_numptob
].seek
= 0;
151 scm_ptobs
[scm_numptob
].truncate
= 0;
159 scm_memory_error ("scm_make_port_type");
161 /* Make a class object if Goops is present */
163 scm_make_port_classes (scm_numptob
- 1, SCM_PTOBNAME (scm_numptob
- 1));
164 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
168 scm_set_port_mark (long tc
, SCM (*mark
) (SCM
))
170 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].mark
= mark
;
174 scm_set_port_free (long tc
, scm_sizet (*free
) (SCM
))
176 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].free
= free
;
180 scm_set_port_print (long tc
, int (*print
) (SCM exp
, SCM port
,
181 scm_print_state
*pstate
))
183 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].print
= print
;
187 scm_set_port_equalp (long tc
, SCM (*equalp
) (SCM
, SCM
))
189 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].equalp
= equalp
;
193 scm_set_port_flush (long tc
, void (*flush
) (SCM port
))
195 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].flush
= flush
;
199 scm_set_port_end_input (long tc
, void (*end_input
) (SCM port
, int offset
))
201 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].end_input
= end_input
;
205 scm_set_port_close (long tc
, int (*close
) (SCM
))
207 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].close
= close
;
211 scm_set_port_seek (long tc
, off_t (*seek
) (SCM port
,
215 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].seek
= seek
;
219 scm_set_port_truncate (long tc
, void (*truncate
) (SCM port
, off_t length
))
221 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].truncate
= truncate
;
225 scm_set_port_input_waiting (long tc
, int (*input_waiting
) (SCM
))
227 scm_ptobs
[SCM_TC2PTOBNUM (tc
)].input_waiting
= input_waiting
;
232 SCM_DEFINE (scm_char_ready_p
, "char-ready?", 0, 1, 0,
234 "Returns @code{#t} if a character is ready on input @var{port} and\n"
235 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
236 "then the next @code{read-char} operation on @var{port} is\n"
237 "guaranteed not to hang. If @var{port} is a file port at end of\n"
238 "file then @code{char-ready?} returns @code{#t}.\n"
239 "@footnote{@code{char-ready?} exists to make it possible for a\n"
240 "program to accept characters from interactive ports without getting\n"
241 "stuck waiting for input. Any input editors associated with such ports\n"
242 "must make sure that characters whose existence has been asserted by\n"
243 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
244 "return @code{#f} at end of file, a port at end of file would be\n"
245 "indistinguishable from an interactive port that has no ready\n"
247 #define FUNC_NAME s_scm_char_ready_p
251 if (SCM_UNBNDP (port
))
254 SCM_VALIDATE_OPINPORT (1,port
);
256 pt
= SCM_PTAB_ENTRY (port
);
258 /* if the current read buffer is filled, or the
259 last pushed-back char has been read and the saved buffer is
260 filled, result is true. */
261 if (pt
->read_pos
< pt
->read_end
262 || (pt
->read_buf
== pt
->putback_buf
263 && pt
->saved_read_pos
< pt
->saved_read_end
))
267 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
269 if (ptob
->input_waiting
)
270 return SCM_BOOL(ptob
->input_waiting (port
));
277 /* move up to read_len chars from port's putback and/or read buffers
278 into memory starting at dest. returns the number of chars moved. */
279 size_t scm_take_from_input_buffers (SCM port
, char *dest
, size_t read_len
)
281 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
282 size_t chars_read
= 0;
283 size_t from_buf
= min (pt
->read_end
- pt
->read_pos
, read_len
);
287 memcpy (dest
, pt
->read_pos
, from_buf
);
288 pt
->read_pos
+= from_buf
;
289 chars_read
+= from_buf
;
290 read_len
-= from_buf
;
294 /* if putback was active, try the real input buffer too. */
295 if (pt
->read_buf
== pt
->putback_buf
)
297 from_buf
= min (pt
->saved_read_end
- pt
->saved_read_pos
, read_len
);
300 memcpy (dest
, pt
->saved_read_pos
, from_buf
);
301 pt
->saved_read_pos
+= from_buf
;
302 chars_read
+= from_buf
;
308 /* Clear a port's read buffers, returning the contents. */
309 SCM_DEFINE (scm_drain_input
, "drain-input", 1, 0, 0,
311 "Drain @var{port}'s read buffers (including any pushed-back\n"
312 "characters) and returns the content as a single string.")
313 #define FUNC_NAME s_scm_drain_input
316 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
319 SCM_VALIDATE_OPINPORT (1,port
);
321 count
= pt
->read_end
- pt
->read_pos
;
322 if (pt
->read_buf
== pt
->putback_buf
)
323 count
+= pt
->saved_read_end
- pt
->saved_read_pos
;
325 result
= scm_makstr (count
, 0);
326 scm_take_from_input_buffers (port
, SCM_STRING_CHARS (result
), count
);
333 /* Standard ports --- current input, output, error, and more(!). */
335 SCM_DEFINE (scm_current_input_port
, "current-input-port", 0, 0, 0,
337 "Return the current input port. This is the default port used\n"
338 "by many input procedures. Initially, @code{current-input-port}\n"
339 "returns the @dfn{standard input} in Unix and C terminology.")
340 #define FUNC_NAME s_scm_current_input_port
346 SCM_DEFINE (scm_current_output_port
, "current-output-port", 0, 0, 0,
348 "Return the current output port. This is the default port used\n"
349 "by many output procedures. Initially, \n"
350 "@code{current-output-port} returns the @dfn{standard output} in\n"
351 "Unix and C terminology.")
352 #define FUNC_NAME s_scm_current_output_port
358 SCM_DEFINE (scm_current_error_port
, "current-error-port", 0, 0, 0,
360 "Return the port to which errors and warnings should be sent (the\n"
361 "@dfn{standard error} in Unix and C terminology).")
362 #define FUNC_NAME s_scm_current_error_port
368 SCM_DEFINE (scm_current_load_port
, "current-load-port", 0, 0, 0,
370 "Return the current-load-port.\n"
371 "The load port is used internally by @code{primitive-load}.")
372 #define FUNC_NAME s_scm_current_load_port
374 return scm_cur_loadp
;
378 SCM_DEFINE (scm_set_current_input_port
, "set-current-input-port", 1, 0, 0,
380 "@deffnx primitive set-current-output-port port\n"
381 "@deffnx primitive set-current-error-port port\n"
382 "Change the ports returned by @code{current-input-port},\n"
383 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
384 "so that they use the supplied @var{port} for input or output.")
385 #define FUNC_NAME s_scm_set_current_input_port
387 SCM oinp
= scm_cur_inp
;
388 SCM_VALIDATE_OPINPORT (1,port
);
395 SCM_DEFINE (scm_set_current_output_port
, "set-current-output-port", 1, 0, 0,
397 "Set the current default output port to @var{port}.")
398 #define FUNC_NAME s_scm_set_current_output_port
400 SCM ooutp
= scm_cur_outp
;
401 port
= SCM_COERCE_OUTPORT (port
);
402 SCM_VALIDATE_OPOUTPORT (1,port
);
409 SCM_DEFINE (scm_set_current_error_port
, "set-current-error-port", 1, 0, 0,
411 "Set the current default error port to @var{port}.")
412 #define FUNC_NAME s_scm_set_current_error_port
414 SCM oerrp
= scm_cur_errp
;
415 port
= SCM_COERCE_OUTPORT (port
);
416 SCM_VALIDATE_OPOUTPORT (1,port
);
423 /* The port table --- an array of pointers to ports. */
425 scm_port
**scm_port_table
;
427 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
428 int scm_port_table_room
= 20; /* Size of the array. */
430 /* Add a port to the table. */
433 scm_add_to_port_table (SCM port
)
437 if (scm_port_table_size
== scm_port_table_room
)
439 void *newt
= realloc ((char *) scm_port_table
,
440 (scm_sizet
) (sizeof (scm_port
*)
441 * scm_port_table_room
* 2));
443 scm_memory_error ("scm_add_to_port_table");
444 scm_port_table
= (scm_port
**) newt
;
445 scm_port_table_room
*= 2;
447 entry
= (scm_port
*) malloc (sizeof (scm_port
));
449 scm_memory_error ("scm_add_to_port_table");
452 entry
->entry
= scm_port_table_size
;
455 entry
->file_name
= SCM_BOOL_F
;
456 entry
->line_number
= 0;
457 entry
->column_number
= 0;
458 entry
->putback_buf
= 0;
459 entry
->putback_buf_size
= 0;
460 entry
->rw_active
= SCM_PORT_NEITHER
;
461 entry
->rw_random
= 0;
463 scm_port_table
[scm_port_table_size
] = entry
;
464 scm_port_table_size
++;
469 /* Remove a port from the table and destroy it. */
472 scm_remove_from_port_table (SCM port
)
473 #define FUNC_NAME "scm_remove_from_port_table"
475 scm_port
*p
= SCM_PTAB_ENTRY (port
);
478 if (i
>= scm_port_table_size
)
479 SCM_MISC_ERROR ("Port not in table: ~S", SCM_LIST1 (port
));
481 free (p
->putback_buf
);
483 /* Since we have just freed slot i we can shrink the table by moving
484 the last entry to that slot... */
485 if (i
< scm_port_table_size
- 1)
487 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
488 scm_port_table
[i
]->entry
= i
;
490 SCM_SETPTAB_ENTRY (port
, 0);
491 scm_port_table_size
--;
497 /* Functions for debugging. */
499 SCM_DEFINE (scm_pt_size
, "pt-size", 0, 0, 0,
501 "Returns the number of ports in the port table. @code{pt-size}\n"
502 "is only included in @code{--enable-guile-debug} builds.")
503 #define FUNC_NAME s_scm_pt_size
505 return SCM_MAKINUM (scm_port_table_size
);
509 SCM_DEFINE (scm_pt_member
, "pt-member", 1, 0, 0,
511 "Returns the port at @var{index} in the port table.\n"
512 "@code{pt-member} is only included in\n"
513 "@code{--enable-guile-debug} builds.")
514 #define FUNC_NAME s_scm_pt_member
517 SCM_VALIDATE_INUM_COPY (1,index
,i
);
518 if (i
< 0 || i
>= scm_port_table_size
)
521 return scm_port_table
[i
]->port
;
527 scm_port_non_buffer (scm_port
*pt
)
529 pt
->read_pos
= pt
->read_buf
= pt
->read_end
= &pt
->shortbuf
;
530 pt
->write_buf
= pt
->write_pos
= &pt
->shortbuf
;
531 pt
->read_buf_size
= pt
->write_buf_size
= 1;
532 pt
->write_end
= pt
->write_buf
+ pt
->write_buf_size
;
536 /* Revealed counts --- an oddity inherited from SCSH. */
538 /* Find a port in the table and return its revealed count.
539 Also used by the garbage collector.
543 scm_revealed_count (SCM port
)
545 return SCM_REVEALED(port
);
550 /* Return the revealed count for a port. */
552 SCM_DEFINE (scm_port_revealed
, "port-revealed", 1, 0, 0,
554 "Returns the revealed count for @var{port}.")
555 #define FUNC_NAME s_scm_port_revealed
557 port
= SCM_COERCE_OUTPORT (port
);
558 SCM_VALIDATE_PORT (1,port
);
559 return SCM_MAKINUM (scm_revealed_count (port
));
563 /* Set the revealed count for a port. */
564 SCM_DEFINE (scm_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0,
565 (SCM port
, SCM rcount
),
566 "Sets the revealed count for a port to a given value.\n"
567 "The return value is unspecified.")
568 #define FUNC_NAME s_scm_set_port_revealed_x
570 port
= SCM_COERCE_OUTPORT (port
);
571 SCM_VALIDATE_PORT (1,port
);
572 SCM_VALIDATE_INUM (2,rcount
);
573 SCM_REVEALED (port
) = SCM_INUM (rcount
);
574 return SCM_UNSPECIFIED
;
580 /* Retrieving a port's mode. */
582 /* Return the flags that characterize a port based on the mode
583 * string used to open a file for that port.
585 * See PORT FLAGS in scm.h
589 scm_mode_bits (char *modes
)
592 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
593 | ( strchr (modes
, 'w')
594 || strchr (modes
, 'a')
595 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
596 | (strchr (modes
, '0') ? SCM_BUF0
: 0)
597 | (strchr (modes
, 'l') ? SCM_BUFLINE
: 0));
601 /* Return the mode flags from an open port.
602 * Some modes such as "append" are only used when opening
603 * a file and are not returned here. */
605 SCM_DEFINE (scm_port_mode
, "port-mode", 1, 0, 0,
607 "Returns the port modes associated with the open port @var{port}. These\n"
608 "will not necessarily be identical to the modes used when the port was\n"
609 "opened, since modes such as \"append\" which are used only during\n"
610 "port creation are not retained.")
611 #define FUNC_NAME s_scm_port_mode
616 port
= SCM_COERCE_OUTPORT (port
);
617 SCM_VALIDATE_OPPORT (1,port
);
618 if (SCM_CELL_WORD_0 (port
) & SCM_RDNG
) {
619 if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
620 strcpy (modes
, "r+");
624 else if (SCM_CELL_WORD_0 (port
) & SCM_WRTNG
)
626 if (SCM_CELL_WORD_0 (port
) & SCM_BUF0
)
628 return scm_makfromstr (modes
, strlen (modes
), 0);
637 * Call the close operation on a port object.
638 * see also scm_close.
640 SCM_DEFINE (scm_close_port
, "close-port", 1, 0, 0,
642 "Close the specified port object. Returns @code{#t} if it successfully\n"
643 "closes a port or @code{#f} if it was already\n"
644 "closed. An exception may be raised if an error occurs, for example\n"
645 "when flushing buffered output.\n"
646 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
647 "which can close file descriptors.")
648 #define FUNC_NAME s_scm_close_port
653 port
= SCM_COERCE_OUTPORT (port
);
655 SCM_VALIDATE_PORT (1, port
);
656 if (SCM_CLOSEDP (port
))
658 i
= SCM_PTOBNUM (port
);
659 if (scm_ptobs
[i
].close
)
660 rv
= (scm_ptobs
[i
].close
) (port
);
663 scm_remove_from_port_table (port
);
664 SCM_SETAND_CAR (port
, ~SCM_OPN
);
665 return SCM_NEGATE_BOOL (rv
< 0);
669 SCM_DEFINE (scm_close_input_port
, "close-input-port", 1, 0, 0,
671 "Close the specified input port object. The routine has no effect if\n"
672 "the file has already been closed. An exception may be raised if an\n"
673 "error occurs. The value returned is unspecified.\n\n"
674 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
675 "which can close file descriptors.")
676 #define FUNC_NAME s_scm_close_input_port
678 SCM_VALIDATE_INPUT_PORT (1, port
);
679 scm_close_port (port
);
680 return SCM_UNSPECIFIED
;
684 SCM_DEFINE (scm_close_output_port
, "close-output-port", 1, 0, 0,
686 "Close the specified output port object. The routine has no effect if\n"
687 "the file has already been closed. An exception may be raised if an\n"
688 "error occurs. The value returned is unspecified.\n\n"
689 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
690 "which can close file descriptors.")
691 #define FUNC_NAME s_scm_close_output_port
693 port
= SCM_COERCE_OUTPORT (port
);
694 SCM_VALIDATE_OUTPUT_PORT (1, port
);
695 scm_close_port (port
);
696 return SCM_UNSPECIFIED
;
700 SCM_DEFINE (scm_port_for_each
, "port-for-each", 1, 0, 0,
702 "Apply @var{proc} to each port in the Guile port table\n"
703 "in turn. The return value is unspecified. More specifically,\n"
704 "@var{proc} is applied exactly once to every port that exists\n"
705 "in the system at the time @var{port-for-each} is invoked.\n"
706 "Changes to the port table while @var{port-for-each} is running\n"
707 "have no effect as far as @var{port-for-each} is concerned.\n")
708 #define FUNC_NAME s_scm_port_for_each
713 SCM_VALIDATE_PROC (1, proc
);
715 /* when pre-emptive multithreading is supported, access to the port
716 table will need to be controlled by a mutex. */
718 /* Even without pre-emptive multithreading, running arbitrary code
719 while scanning the port table is unsafe because the port table
720 can change arbitrarily (from a GC, for example). So we build a
721 list in advance while blocking the GC. -mvo */
726 for (i
= 0; i
< scm_port_table_size
; i
++)
727 ports
= scm_cons (scm_port_table
[i
]->port
, ports
);
731 while (ports
!= SCM_EOL
)
733 scm_apply (proc
, scm_cons (SCM_CAR (ports
), SCM_EOL
), SCM_EOL
);
734 ports
= SCM_CDR (ports
);
737 return SCM_UNSPECIFIED
;
741 #if (SCM_DEBUG_DEPRECATED == 0)
743 SCM_DEFINE (scm_close_all_ports_except
, "close-all-ports-except", 0, 0, 1,
745 "[DEPRECATED] Close all open file ports used by the interpreter\n"
746 "except for those supplied as arguments. This procedure\n"
747 "was intended to be used before an exec call to close file descriptors\n"
748 "which are not needed in the new process. However it has the\n"
749 "undesirable side-effect of flushing buffes, so it's deprecated.\n"
750 "Use port-for-each instead.")
751 #define FUNC_NAME s_scm_close_all_ports_except
754 SCM_VALIDATE_REST_ARGUMENT (ports
);
755 while (i
< scm_port_table_size
)
757 SCM thisport
= scm_port_table
[i
]->port
;
759 SCM ports_ptr
= ports
;
761 while (SCM_NNULLP (ports_ptr
))
763 SCM port
= SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr
));
765 SCM_VALIDATE_OPPORT (SCM_ARG1
,port
);
766 if (SCM_EQ_P (port
, thisport
))
768 ports_ptr
= SCM_CDR (ports_ptr
);
773 /* i is not to be incremented here. */
774 scm_close_port (thisport
);
776 return SCM_UNSPECIFIED
;
783 /* Utter miscellany. Gosh, we should clean this up some time. */
785 SCM_DEFINE (scm_input_port_p
, "input-port?", 1, 0, 0,
787 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
788 "@code{#f}. Any object satisfying this predicate also satisfies\n"
790 #define FUNC_NAME s_scm_input_port_p
794 return SCM_BOOL(SCM_INPUT_PORT_P (x
));
798 SCM_DEFINE (scm_output_port_p
, "output-port?", 1, 0, 0,
800 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
801 "@code{#f}. Any object satisfying this predicate also satisfies\n"
803 #define FUNC_NAME s_scm_output_port_p
807 if (SCM_PORT_WITH_PS_P (x
))
808 x
= SCM_PORT_WITH_PS_PORT (x
);
809 return SCM_BOOL(SCM_OUTPUT_PORT_P (x
));
813 SCM_DEFINE (scm_port_p
, "port?", 1, 0, 0,
815 "Returns a boolean indicating whether @var{x} is a port.\n"
816 "Equivalent to @code{(or (input-port? @var{x}) (output-port?\n"
818 #define FUNC_NAME s_scm_port_p
820 return SCM_BOOL (SCM_PORTP (x
));
824 SCM_DEFINE (scm_port_closed_p
, "port-closed?", 1, 0, 0,
826 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
827 #define FUNC_NAME s_scm_port_closed_p
829 SCM_VALIDATE_PORT (1,port
);
830 return SCM_NEGATE_BOOL(SCM_OPPORTP (port
));
834 SCM_DEFINE (scm_eof_object_p
, "eof-object?", 1, 0, 0,
836 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
837 "returns @code{#f}.")
838 #define FUNC_NAME s_scm_eof_object_p
840 return SCM_BOOL(SCM_EOF_OBJECT_P (x
));
844 SCM_DEFINE (scm_force_output
, "force-output", 0, 1, 0,
846 "Flush the specified output port, or the current output port if @var{port}\n"
847 "is omitted. The current output buffer contents are passed to the \n"
848 "underlying port implementation (e.g., in the case of fports, the\n"
849 "data will be written to the file and the output buffer will be cleared.)\n"
850 "It has no effect on an unbuffered port.\n\n"
851 "The return value is unspecified.")
852 #define FUNC_NAME s_scm_force_output
854 if (SCM_UNBNDP (port
))
858 port
= SCM_COERCE_OUTPORT (port
);
859 SCM_VALIDATE_OPOUTPORT (1,port
);
862 return SCM_UNSPECIFIED
;
866 SCM_DEFINE (scm_flush_all_ports
, "flush-all-ports", 0, 0, 0,
868 "Equivalent to calling @code{force-output} on\n"
869 "all open output ports. The return value is unspecified.")
870 #define FUNC_NAME s_scm_flush_all_ports
874 for (i
= 0; i
< scm_port_table_size
; i
++)
876 if (SCM_OPOUTPORTP (scm_port_table
[i
]->port
))
877 scm_flush (scm_port_table
[i
]->port
);
879 return SCM_UNSPECIFIED
;
883 SCM_DEFINE (scm_read_char
, "read-char", 0, 1, 0,
885 "Returns the next character available from @var{port}, updating\n"
886 "@var{port} to point to the following character. If no more\n"
887 "characters are available, an end-of-file object is returned.")
888 #define FUNC_NAME s_scm_read_char
891 if (SCM_UNBNDP (port
))
893 SCM_VALIDATE_OPINPORT (1,port
);
897 return SCM_MAKE_CHAR (c
);
901 /* this should only be called when the read buffer is empty. it
902 tries to refill the read buffer. it returns the first char from
903 the port, which is either EOF or *(pt->read_pos). */
905 scm_fill_input (SCM port
)
907 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
909 if (pt
->read_buf
== pt
->putback_buf
)
911 /* finished reading put-back chars. */
912 pt
->read_buf
= pt
->saved_read_buf
;
913 pt
->read_pos
= pt
->saved_read_pos
;
914 pt
->read_end
= pt
->saved_read_end
;
915 pt
->read_buf_size
= pt
->saved_read_buf_size
;
916 if (pt
->read_pos
< pt
->read_end
)
917 return *(pt
->read_pos
);
919 return scm_ptobs
[SCM_PTOBNUM (port
)].fill_input (port
);
926 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
928 if (pt
->rw_active
== SCM_PORT_WRITE
)
930 /* may be marginally faster than calling scm_flush. */
931 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
935 pt
->rw_active
= SCM_PORT_READ
;
937 if (pt
->read_pos
>= pt
->read_end
)
939 if (scm_fill_input (port
) == EOF
)
943 c
= *(pt
->read_pos
++);
962 scm_putc (char c
, SCM port
)
964 scm_lfwrite (&c
, 1, port
);
968 scm_puts (const char *s
, SCM port
)
970 scm_lfwrite (s
, strlen (s
), port
);
975 * Currently, this function has an identical implementation to
976 * scm_c_write. We could have turned it into a macro expanding into a
977 * call to scm_c_write. However, the implementation is small and
978 * might differ in the future.
982 scm_lfwrite (const char *ptr
, scm_sizet size
, SCM port
)
984 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
985 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
987 if (pt
->rw_active
== SCM_PORT_READ
)
988 scm_end_input (port
);
990 ptob
->write (port
, ptr
, size
);
993 pt
->rw_active
= SCM_PORT_WRITE
;
998 * Used by an application to read arbitrary number of bytes from an
999 * SCM port. Same semantics as libc read, except that scm_c_read only
1000 * returns less than SIZE bytes if at end-of-file.
1002 * Warning: Doesn't update port line and column counts! */
1005 scm_c_read (SCM port
, void *buffer
, scm_sizet size
)
1007 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
1008 scm_sizet n_read
= 0, n_available
;
1010 if (pt
->rw_active
== SCM_PORT_WRITE
)
1011 scm_ptobs
[SCM_PTOBNUM (port
)].flush (port
);
1014 pt
->rw_active
= SCM_PORT_READ
;
1016 if (SCM_READ_BUFFER_EMPTY_P (pt
))
1018 if (scm_fill_input (port
) == EOF
)
1022 n_available
= pt
->read_end
- pt
->read_pos
;
1024 while (n_available
< size
)
1026 memcpy (buffer
, pt
->read_pos
, n_available
);
1027 buffer
+= n_available
;
1028 pt
->read_pos
+= n_available
;
1029 n_read
+= n_available
;
1031 if (SCM_READ_BUFFER_EMPTY_P (pt
))
1033 if (scm_fill_input (port
) == EOF
)
1037 size
-= n_available
;
1038 n_available
= pt
->read_end
- pt
->read_pos
;
1041 memcpy (buffer
, pt
->read_pos
, size
);
1042 pt
->read_pos
+= size
;
1044 return n_read
+ size
;
1049 * Used by an application to write arbitrary number of bytes to an SCM
1050 * port. Similar semantics as libc write. However, unlike libc
1051 * write, scm_c_write writes the requested number of bytes and has no
1054 * Warning: Doesn't update port line and column counts!
1058 scm_c_write (SCM port
, const void *ptr
, scm_sizet size
)
1060 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
1061 scm_ptob_descriptor
*ptob
= &scm_ptobs
[SCM_PTOBNUM (port
)];
1063 if (pt
->rw_active
== SCM_PORT_READ
)
1064 scm_end_input (port
);
1066 ptob
->write (port
, ptr
, size
);
1069 pt
->rw_active
= SCM_PORT_WRITE
;
1073 scm_flush (SCM port
)
1075 scm_sizet i
= SCM_PTOBNUM (port
);
1076 (scm_ptobs
[i
].flush
) (port
);
1080 scm_end_input (SCM port
)
1083 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
1085 if (pt
->read_buf
== pt
->putback_buf
)
1087 offset
= pt
->read_end
- pt
->read_pos
;
1088 pt
->read_buf
= pt
->saved_read_buf
;
1089 pt
->read_pos
= pt
->saved_read_pos
;
1090 pt
->read_end
= pt
->saved_read_end
;
1091 pt
->read_buf_size
= pt
->saved_read_buf_size
;
1096 scm_ptobs
[SCM_PTOBNUM (port
)].end_input (port
, offset
);
1103 scm_ungetc (int c
, SCM port
)
1105 scm_port
*pt
= SCM_PTAB_ENTRY (port
);
1107 if (pt
->read_buf
== pt
->putback_buf
)
1108 /* already using the put-back buffer. */
1110 /* enlarge putback_buf if necessary. */
1111 if (pt
->read_end
== pt
->read_buf
+ pt
->read_buf_size
1112 && pt
->read_buf
== pt
->read_pos
)
1114 int new_size
= pt
->read_buf_size
* 2;
1115 unsigned char *tmp
=
1116 (unsigned char *) realloc (pt
->putback_buf
, new_size
);
1119 scm_memory_error ("scm_ungetc");
1120 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
= tmp
;
1121 pt
->read_end
= pt
->read_buf
+ pt
->read_buf_size
;
1122 pt
->read_buf_size
= pt
->putback_buf_size
= new_size
;
1125 /* shift any existing bytes to buffer + 1. */
1126 if (pt
->read_pos
== pt
->read_end
)
1127 pt
->read_end
= pt
->read_buf
+ 1;
1128 else if (pt
->read_pos
!= pt
->read_buf
+ 1)
1130 int count
= pt
->read_end
- pt
->read_pos
;
1132 memmove (pt
->read_buf
+ 1, pt
->read_pos
, count
);
1133 pt
->read_end
= pt
->read_buf
+ 1 + count
;
1136 pt
->read_pos
= pt
->read_buf
;
1139 /* switch to the put-back buffer. */
1141 if (pt
->putback_buf
== NULL
)
1144 = (unsigned char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE
);
1145 if (pt
->putback_buf
== NULL
)
1146 scm_memory_error ("scm_ungetc");
1147 pt
->putback_buf_size
= SCM_INITIAL_PUTBACK_BUF_SIZE
;
1150 pt
->saved_read_buf
= pt
->read_buf
;
1151 pt
->saved_read_pos
= pt
->read_pos
;
1152 pt
->saved_read_end
= pt
->read_end
;
1153 pt
->saved_read_buf_size
= pt
->read_buf_size
;
1155 pt
->read_pos
= pt
->read_buf
= pt
->putback_buf
;
1156 pt
->read_end
= pt
->read_buf
+ 1;
1157 pt
->read_buf_size
= pt
->putback_buf_size
;
1163 pt
->rw_active
= SCM_PORT_READ
;
1167 /* What should col be in this case?
1168 * We'll leave it at -1.
1170 SCM_LINUM (port
) -= 1;
1178 scm_ungets (const char *s
, int n
, SCM port
)
1180 /* This is simple minded and inefficient, but unreading strings is
1181 * probably not a common operation, and remember that line and
1182 * column numbers have to be handled...
1184 * Please feel free to write an optimized version!
1187 scm_ungetc (s
[n
], port
);
1191 SCM_DEFINE (scm_peek_char
, "peek-char", 0, 1, 0,
1193 "Returns the next character available from @var{port},\n"
1194 "@emph{without} updating @var{port} to point to the following\n"
1195 "character. If no more characters are available, an end-of-file object\n"
1196 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
1197 "is the same as the value that would have been returned by a call to\n"
1198 "@code{read-char} on the same port. The only difference is that the very\n"
1199 "next call to @code{read-char} or @code{peek-char} on that\n"
1200 "@var{port} will return the value returned by the preceding call to\n"
1201 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1202 "interactive port will hang waiting for input whenever a call to\n"
1203 "@code{read-char} would have hung.}")
1204 #define FUNC_NAME s_scm_peek_char
1207 if (SCM_UNBNDP (port
))
1210 SCM_VALIDATE_OPINPORT (1,port
);
1211 c
= scm_getc (port
);
1214 scm_ungetc (c
, port
);
1215 return SCM_MAKE_CHAR (c
);
1219 SCM_DEFINE (scm_unread_char
, "unread-char", 1, 1, 0,
1220 (SCM cobj
, SCM port
),
1221 "Place @var{char} in @var{port} so that it will be read by the\n"
1222 "next read operation. If called multiple times, the unread characters\n"
1223 "will be read again in last-in first-out order. If @var{port} is\n"
1224 "not supplied, the current input port is used.")
1225 #define FUNC_NAME s_scm_unread_char
1229 SCM_VALIDATE_CHAR (1,cobj
);
1230 if (SCM_UNBNDP (port
))
1233 SCM_VALIDATE_OPINPORT (2,port
);
1235 c
= SCM_CHAR (cobj
);
1237 scm_ungetc (c
, port
);
1242 SCM_DEFINE (scm_unread_string
, "unread-string", 2, 0, 0,
1243 (SCM str
, SCM port
),
1244 "Place the string @var{str} in @var{port} so that its characters will be\n"
1245 "read in subsequent read operations. If called multiple times, the\n"
1246 "unread characters will be read again in last-in first-out order. If\n"
1247 "@var{port} is not supplied, the current-input-port is used.")
1248 #define FUNC_NAME s_scm_unread_string
1250 SCM_VALIDATE_STRING (1,str
);
1251 if (SCM_UNBNDP (port
))
1254 SCM_VALIDATE_OPINPORT (2,port
);
1256 scm_ungets (SCM_STRING_CHARS (str
), SCM_STRING_LENGTH (str
), port
);
1262 SCM_DEFINE (scm_seek
, "seek", 3, 0, 0,
1263 (SCM object
, SCM offset
, SCM whence
),
1264 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1265 "which is interpreted according to the value of @var{whence}.\n\n"
1266 "One of the following variables should be supplied\n"
1267 "for @var{whence}:\n"
1268 "@defvar SEEK_SET\n"
1269 "Seek from the beginning of the file.\n"
1271 "@defvar SEEK_CUR\n"
1272 "Seek from the current position.\n"
1274 "@defvar SEEK_END\n"
1275 "Seek from the end of the file.\n"
1277 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1278 "@code{lseek}. @var{port} may be a string port.\n\n"
1279 "The value returned is the new position in the file. This means that\n"
1280 "the current position of a port can be obtained using:\n"
1282 "(seek port 0 SEEK_CUR)\n"
1284 #define FUNC_NAME s_scm_seek
1290 object
= SCM_COERCE_OUTPORT (object
);
1292 off
= SCM_NUM2LONG (2, offset
);
1293 SCM_VALIDATE_INUM_COPY (3, whence
, how
);
1294 if (how
!= SEEK_SET
&& how
!= SEEK_CUR
&& how
!= SEEK_END
)
1295 SCM_OUT_OF_RANGE (3, whence
);
1296 if (SCM_OPPORTP (object
))
1298 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1301 SCM_MISC_ERROR ("port is not seekable",
1302 scm_cons (object
, SCM_EOL
));
1304 rv
= ptob
->seek (object
, off
, how
);
1306 else /* file descriptor?. */
1308 SCM_VALIDATE_INUM (1,object
);
1309 rv
= lseek (SCM_INUM (object
), off
, how
);
1313 return scm_long2num (rv
);
1317 SCM_DEFINE (scm_truncate_file
, "truncate-file", 1, 1, 0,
1318 (SCM object
, SCM length
),
1319 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1320 "@var{obj} can be a string containing a file name or an integer file\n"
1321 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1322 "a file name, in which case the truncation occurs at the current port.\n"
1324 "The return value is unspecified.")
1325 #define FUNC_NAME s_scm_truncate_file
1330 /* object can be a port, fdes or filename. */
1332 if (SCM_UNBNDP (length
))
1334 /* must supply length if object is a filename. */
1335 if (SCM_STRINGP (object
))
1336 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL
);
1338 length
= scm_seek (object
, SCM_INUM0
, SCM_MAKINUM (SEEK_CUR
));
1340 c_length
= SCM_NUM2LONG (2,length
);
1342 SCM_MISC_ERROR ("negative offset", SCM_EOL
);
1344 object
= SCM_COERCE_OUTPORT (object
);
1345 if (SCM_INUMP (object
))
1347 SCM_SYSCALL (rv
= ftruncate (SCM_INUM (object
), c_length
));
1349 else if (SCM_OPOUTPORTP (object
))
1351 scm_port
*pt
= SCM_PTAB_ENTRY (object
);
1352 scm_ptob_descriptor
*ptob
= scm_ptobs
+ SCM_PTOBNUM (object
);
1354 if (!ptob
->truncate
)
1355 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL
);
1356 if (pt
->rw_active
== SCM_PORT_READ
)
1357 scm_end_input (object
);
1358 else if (pt
->rw_active
== SCM_PORT_WRITE
)
1359 ptob
->flush (object
);
1361 ptob
->truncate (object
, c_length
);
1366 SCM_VALIDATE_STRING (1, object
);
1367 SCM_STRING_COERCE_0TERMINATION_X (object
);
1368 SCM_SYSCALL (rv
= truncate (SCM_STRING_CHARS (object
), c_length
));
1372 return SCM_UNSPECIFIED
;
1376 SCM_DEFINE (scm_port_line
, "port-line", 1, 0, 0,
1378 "Return the current line number for @var{port}.")
1379 #define FUNC_NAME s_scm_port_line
1381 port
= SCM_COERCE_OUTPORT (port
);
1382 SCM_VALIDATE_OPENPORT (1,port
);
1383 return SCM_MAKINUM (SCM_LINUM (port
));
1387 SCM_DEFINE (scm_set_port_line_x
, "set-port-line!", 2, 0, 0,
1388 (SCM port
, SCM line
),
1389 "Set the current line number for @var{port} to @var{line}.")
1390 #define FUNC_NAME s_scm_set_port_line_x
1392 port
= SCM_COERCE_OUTPORT (port
);
1393 SCM_VALIDATE_OPENPORT (1,port
);
1394 SCM_VALIDATE_INUM (2,line
);
1395 SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
1396 return SCM_UNSPECIFIED
;
1400 SCM_DEFINE (scm_port_column
, "port-column", 1, 0, 0,
1402 "@deffnx primitive port-line port\n"
1403 "Return the current column number or line number of @var{port},\n"
1404 "using the current input port if none is specified. If the number is\n"
1405 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1406 "- i.e. the first character of the first line is line 0, column 0.\n"
1407 "(However, when you display a file position, for example in an error\n"
1408 "message, we recommend you add 1 to get 1-origin integers. This is\n"
1409 "because lines and column numbers traditionally start with 1, and that is\n"
1410 "what non-programmers will find most natural.)")
1411 #define FUNC_NAME s_scm_port_column
1413 port
= SCM_COERCE_OUTPORT (port
);
1414 SCM_VALIDATE_OPENPORT (1,port
);
1415 return SCM_MAKINUM (SCM_COL (port
));
1419 SCM_DEFINE (scm_set_port_column_x
, "set-port-column!", 2, 0, 0,
1420 (SCM port
, SCM column
),
1421 "@deffnx primitive set-port-line! port line\n"
1422 "Set the current column or line number of @var{port}, using the\n"
1423 "current input port if none is specified.")
1424 #define FUNC_NAME s_scm_set_port_column_x
1426 port
= SCM_COERCE_OUTPORT (port
);
1427 SCM_VALIDATE_OPENPORT (1,port
);
1428 SCM_VALIDATE_INUM (2,column
);
1429 SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
1430 return SCM_UNSPECIFIED
;
1434 SCM_DEFINE (scm_port_filename
, "port-filename", 1, 0, 0,
1436 "Return the filename associated with @var{port}. This function returns\n"
1437 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
1438 "when called on the current input, output and error ports respectively.")
1439 #define FUNC_NAME s_scm_port_filename
1441 port
= SCM_COERCE_OUTPORT (port
);
1442 SCM_VALIDATE_OPENPORT (1,port
);
1443 return SCM_FILENAME (port
);
1447 SCM_DEFINE (scm_set_port_filename_x
, "set-port-filename!", 2, 0, 0,
1448 (SCM port
, SCM filename
),
1449 "Change the filename associated with @var{port}, using the current input\n"
1450 "port if none is specified. Note that this does not change the port's\n"
1451 "source of data, but only the value that is returned by\n"
1452 "@code{port-filename} and reported in diagnostic output.")
1453 #define FUNC_NAME s_scm_set_port_filename_x
1455 port
= SCM_COERCE_OUTPORT (port
);
1456 SCM_VALIDATE_OPENPORT (1,port
);
1457 /* We allow the user to set the filename to whatever he likes. */
1458 SCM_SET_FILENAME (port
, filename
);
1459 return SCM_UNSPECIFIED
;
1464 extern char * ttyname();
1468 scm_print_port_mode (SCM exp
, SCM port
)
1470 scm_puts (SCM_CLOSEDP (exp
)
1472 : (SCM_RDNG
& SCM_CELL_WORD_0 (exp
)
1473 ? (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
1476 : (SCM_WRTNG
& SCM_CELL_WORD_0 (exp
)
1483 scm_port_print (SCM exp
, SCM port
, scm_print_state
*pstate
)
1485 char *type
= SCM_PTOBNAME (SCM_PTOBNUM (exp
));
1488 scm_puts ("#<", port
);
1489 scm_print_port_mode (exp
, port
);
1490 scm_puts (type
, port
);
1491 scm_putc (' ', port
);
1492 scm_intprint (SCM_CELL_WORD_1 (exp
), 16, port
);
1493 scm_putc ('>', port
);
1498 scm_ports_prehistory ()
1501 scm_ptobs
= (scm_ptob_descriptor
*) malloc (sizeof (scm_ptob_descriptor
));
1508 scm_bits_t scm_tc16_void_port
= 0;
1510 static int fill_input_void_port (SCM port
)
1516 write_void_port (SCM port
, const void *data
, size_t size
)
1521 scm_void_port (char *mode_str
)
1527 SCM_NEWCELL (answer
);
1529 mode_bits
= scm_mode_bits (mode_str
);
1530 pt
= scm_add_to_port_table (answer
);
1531 scm_port_non_buffer (pt
);
1532 SCM_SETPTAB_ENTRY (answer
, pt
);
1533 SCM_SETSTREAM (answer
, 0);
1534 SCM_SET_CELL_TYPE (answer
, scm_tc16_void_port
| mode_bits
);
1539 SCM_DEFINE (scm_sys_make_void_port
, "%make-void-port", 1, 0, 0,
1541 "Create and return a new void port. A void port acts like\n"
1542 "/dev/null. The @var{mode} argument\n"
1543 "specifies the input/output modes for this port: see the\n"
1544 "documentation for @code{open-file} in @ref{File Ports}.")
1545 #define FUNC_NAME s_scm_sys_make_void_port
1547 SCM_VALIDATE_STRING (1, mode
);
1548 SCM_STRING_COERCE_0TERMINATION_X (mode
);
1549 return scm_void_port (SCM_STRING_CHARS (mode
));
1554 /* Initialization. */
1559 /* lseek() symbols. */
1560 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET
));
1561 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR
));
1562 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END
));
1564 scm_tc16_void_port
= scm_make_port_type ("void", fill_input_void_port
,
1566 #ifndef SCM_MAGIC_SNARFER
1567 #include "libguile/ports.x"