1 /* Copyright (C) 1995,1996 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, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
63 #ifdef HAVE_SYS_IOCTL_H
64 #include <sys/ioctl.h>
69 /* scm_ptobs scm_numptob
70 * implement a dynamicly resized array of ptob records.
71 * Indexes into this table are used when generating type
72 * tags for smobjects (if you know a tag you can get an index and conversely).
74 scm_ptobfuns
*scm_ptobs
;
75 scm_sizet scm_numptob
;
83 if (SCM_GC8MARKP (ptr
))
85 openp
= SCM_CAR (ptr
) & SCM_OPN
;
88 return SCM_STREAM (ptr
);
100 if (255 <= scm_numptob
)
103 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
, (1 + scm_numptob
) * sizeof (scm_ptobfuns
)));
106 scm_ptobs
= (scm_ptobfuns
*) tmp
;
107 scm_ptobs
[scm_numptob
].mark
= ptob
->mark
;
108 scm_ptobs
[scm_numptob
].free
= ptob
->free
;
109 scm_ptobs
[scm_numptob
].print
= ptob
->print
;
110 scm_ptobs
[scm_numptob
].equalp
= ptob
->equalp
;
111 scm_ptobs
[scm_numptob
].fputc
= ptob
->fputc
;
112 scm_ptobs
[scm_numptob
].fputs
= ptob
->fputs
;
113 scm_ptobs
[scm_numptob
].fwrite
= ptob
->fwrite
;
114 scm_ptobs
[scm_numptob
].fflush
= ptob
->fflush
;
115 scm_ptobs
[scm_numptob
].fgetc
= ptob
->fgetc
;
116 scm_ptobs
[scm_numptob
].fclose
= ptob
->fclose
;
121 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
), (char *) SCM_NALLOC
, "newptob");
122 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
126 /* internal SCM call */
132 scm_sizet i
= SCM_PTOBNUM (port
);
133 (scm_ptobs
[i
].fflush
) (SCM_STREAM (port
));
138 SCM_PROC(s_char_ready_p
, "char-ready?", 1, 0, 0, scm_char_ready_p
);
141 scm_char_ready_p (port
)
144 if (SCM_UNBNDP (port
))
147 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_char_ready_p
);
148 if (SCM_CRDYP (port
) || !SCM_FPORTP (port
))
150 return (scm_input_waiting_p ((FILE *) SCM_STREAM (port
), s_char_ready_p
)
158 SCM_PROC (s_ungetc_char_ready_p
, "ungetc-char-ready?", 1, 0, 0, scm_ungetc_char_ready_p
);
161 scm_ungetc_char_ready_p (port
)
164 if (SCM_UNBNDP (port
))
167 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_ungetc_char_ready_p
);
168 return (SCM_CRDYP (port
)
179 SCM_PROC(s_current_input_port
, "current-input-port", 0, 0, 0, scm_current_input_port
);
182 scm_current_input_port ()
187 SCM_PROC(s_current_output_port
, "current-output-port", 0, 0, 0, scm_current_output_port
);
190 scm_current_output_port ()
195 SCM_PROC(s_current_error_port
, "current-error-port", 0, 0, 0, scm_current_error_port
);
198 scm_current_error_port ()
203 SCM_PROC(s_set_current_input_port
, "set-current-input-port", 1, 0, 0, scm_set_current_input_port
);
206 scm_set_current_input_port (port
)
209 SCM oinp
= scm_cur_inp
;
210 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_set_current_input_port
);
216 SCM_PROC(s_set_current_output_port
, "set-current-output-port", 1, 0, 0, scm_set_current_output_port
);
219 scm_set_current_output_port (port
)
222 SCM ooutp
= scm_cur_outp
;
223 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_output_port
);
229 SCM_PROC(s_set_current_error_port
, "set-current-error-port", 1, 0, 0, scm_set_current_error_port
);
232 scm_set_current_error_port (port
)
235 SCM oerrp
= scm_cur_errp
;
236 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_error_port
);
243 /* {Ports - in general}
247 /* Array of open ports, required for reliable MOVE->FDES etc. */
248 struct scm_port_table
**scm_port_table
;
250 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
251 int scm_port_table_room
= 20; /* Size of the array. */
253 /* Add a port to the table. Call with SCM_DEFER_INTS active. */
255 struct scm_port_table
*
256 scm_add_to_port_table (port
)
259 if (scm_port_table_size
== scm_port_table_room
)
261 scm_port_table
= ((struct scm_port_table
**)
262 realloc ((char *) scm_port_table
,
263 (long) (sizeof (struct scm_port_table
)
264 * scm_port_table_room
* 2)));
265 /* !!! error checking */
266 scm_port_table_room
*= 2;
268 scm_port_table
[scm_port_table_size
] = ((struct scm_port_table
*)
269 scm_must_malloc (sizeof (struct scm_port_table
),
270 "system port table"));
271 scm_port_table
[scm_port_table_size
]->port
= port
;
272 scm_port_table
[scm_port_table_size
]->revealed
= 0;
273 scm_port_table
[scm_port_table_size
]->stream
= 0;
274 scm_port_table
[scm_port_table_size
]->file_name
= SCM_BOOL_F
;
275 scm_port_table
[scm_port_table_size
]->line_number
= 1;
276 scm_port_table
[scm_port_table_size
]->column_number
= 0;
277 scm_port_table
[scm_port_table_size
]->representation
= scm_regular_port
;
278 return scm_port_table
[scm_port_table_size
++];
281 /* Remove a port from the table. Call with SCM_DEFER_INTS active. */
284 scm_remove_from_port_table (port
)
288 while (scm_port_table
[i
]->port
!= port
)
291 /* Error if not found: too violent? May occur in GC. */
292 if (i
>= scm_port_table_size
)
293 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
295 scm_must_free ((char *)scm_port_table
[i
]);
296 scm_mallocated
-= sizeof (*scm_port_table
[i
]);
297 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
298 SCM_SETPTAB_ENTRY (port
, 0);
299 scm_port_table_size
--;
303 /* Undocumented functions for debugging. */
304 /* Return the number of ports in the table. */
306 SCM_PROC(s_pt_size
, "pt-size", 0, 0, 0, scm_pt_size
);
310 return SCM_MAKINUM (scm_port_table_size
);
313 /* Return the ith member of the port table. */
314 SCM_PROC(s_pt_member
, "pt-member", 1, 0, 0, scm_pt_member
);
316 scm_pt_member (member
)
320 SCM_ASSERT (SCM_INUMP (member
), member
, SCM_ARG1
, s_pt_member
);
321 i
= SCM_INUM (member
);
322 if (i
< 0 || i
>= scm_port_table_size
)
325 return scm_port_table
[i
]->port
;
330 /* Find a port in the table and return its revealed count.
331 Also used by the garbage collector.
335 scm_revealed_count (port
)
338 return SCM_REVEALED(port
);
343 /* Return the revealed count for a port. */
345 SCM_PROC(s_port_revealed
, "port-revealed", 1, 0, 0, scm_port_revealed
);
348 scm_port_revealed (port
)
351 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_port_revealed
);
352 return SCM_MAKINUM (scm_revealed_count (port
));
355 /* Set the revealed count for a port. */
356 SCM_PROC(s_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x
);
359 scm_set_port_revealed_x (port
, rcount
)
363 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_set_port_revealed_x
);
364 SCM_ASSERT (SCM_INUMP (rcount
), rcount
, SCM_ARG2
, s_set_port_revealed_x
);
366 SCM_REVEALED (port
) = SCM_INUM (rcount
);
368 return SCM_UNSPECIFIED
;
372 * Call the close operation on a port object.
374 SCM_PROC(s_close_port
, "close-port", 1, 0, 0, scm_close_port
);
377 scm_close_port (port
)
381 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_close_port
);
382 if (SCM_CLOSEDP (port
))
383 return SCM_UNSPECIFIED
;
384 i
= SCM_PTOBNUM (port
);
386 if (scm_ptobs
[i
].fclose
)
387 SCM_SYSCALL ((scm_ptobs
[i
].fclose
) (SCM_STREAM (port
)));
388 scm_remove_from_port_table (port
);
389 SCM_SETAND_CAR (port
, ~SCM_OPN
);
391 return SCM_UNSPECIFIED
;
394 SCM_PROC(s_close_all_ports_except
, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except
);
397 scm_close_all_ports_except (ports
)
401 SCM_ASSERT (SCM_NIMP (ports
) && SCM_CONSP (ports
), ports
, SCM_ARG1
, s_close_all_ports_except
);
403 while (i
< scm_port_table_size
)
405 SCM thisport
= scm_port_table
[i
]->port
;
407 SCM ports_ptr
= ports
;
409 while (SCM_NNULLP (ports_ptr
))
411 SCM port
= SCM_CAR (ports_ptr
);
413 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_close_all_ports_except
);
414 if (port
== thisport
)
416 ports_ptr
= SCM_CDR (ports_ptr
);
421 /* i is not to be incremented here. */
422 scm_close_port (thisport
);
425 return SCM_UNSPECIFIED
;
428 SCM_PROC(s_input_port_p
, "input-port?", 1, 0, 0, scm_input_port_p
);
436 return SCM_INPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
439 SCM_PROC(s_output_port_p
, "output-port?", 1, 0, 0, scm_output_port_p
);
442 scm_output_port_p (x
)
447 return SCM_OUTPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
451 SCM_PROC(s_eof_object_p
, "eof-object?", 1, 0, 0, scm_eof_object_p
);
457 return (SCM_EOF_VAL
== x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
460 SCM_PROC(s_force_output
, "force-output", 0, 1, 0, scm_force_output
);
463 scm_force_output (port
)
466 if (SCM_UNBNDP (port
))
469 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_force_output
);
471 scm_sizet i
= SCM_PTOBNUM (port
);
472 SCM_SYSCALL ((scm_ptobs
[i
].fflush
) (SCM_STREAM (port
)));
473 return SCM_UNSPECIFIED
;
478 SCM_PROC(s_read_char
, "read-char", 0, 1, 0, scm_read_char
);
485 if (SCM_UNBNDP (port
))
488 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_char
);
489 c
= scm_gen_getc (port
);
492 return SCM_MAKICHR (c
);
496 SCM_PROC(s_peek_char
, "peek-char", 0, 1, 0, scm_peek_char
);
503 if (SCM_UNBNDP (port
))
506 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_peek_char
);
507 c
= scm_gen_getc (port
);
510 scm_gen_ungetc (c
, port
);
511 return SCM_MAKICHR (c
);
514 SCM_PROC (s_unread_char
, "unread-char", 2, 0, 0, scm_unread_char
);
517 scm_unread_char (cobj
, port
)
523 SCM_ASSERT (SCM_ICHRP (cobj
), cobj
, SCM_ARG1
, s_unread_char
);
525 if (SCM_UNBNDP (port
))
528 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_unread_char
);
533 scm_gen_ungetc (c
, port
);
539 SCM_PROC (s_port_line
, "port-line", 0, 1, 0, scm_port_line
);
546 p
= ((port
== SCM_UNDEFINED
)
549 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
552 return SCM_MAKINUM (SCM_LINUM (p
));
555 SCM_PROC (s_set_port_line_x
, "set-port-line!", 1, 1, 0, scm_set_port_line_x
);
558 scm_set_port_line_x (port
, line
)
562 if (line
== SCM_UNDEFINED
)
568 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
572 return SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
575 SCM_PROC (s_port_column
, "port-column", 0, 1, 0, scm_port_column
);
578 scm_port_column (port
)
582 p
= ((port
== SCM_UNDEFINED
)
585 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
588 return SCM_MAKINUM (SCM_COL (p
));
591 SCM_PROC (s_set_port_column_x
, "set-port-column!", 1, 1, 0, scm_set_port_column_x
);
594 scm_set_port_column_x (port
, column
)
598 if (column
== SCM_UNDEFINED
)
604 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
607 s_set_port_column_x
);
608 return SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
611 SCM_PROC (s_port_filename
, "port-filename", 0, 1, 0, scm_port_filename
);
614 scm_port_filename (port
)
618 p
= ((port
== SCM_UNDEFINED
)
621 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
624 return SCM_PTAB_ENTRY (p
)->file_name
;
627 SCM_PROC (s_set_port_filename_x
, "set-port-filename!", 1, 1, 0, scm_set_port_filename_x
);
630 scm_set_port_filename_x (port
, filename
)
634 if (filename
== SCM_UNDEFINED
)
640 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
643 s_set_port_filename_x
);
644 return SCM_PTAB_ENTRY (port
)->file_name
= filename
;
648 extern char * ttyname();
653 scm_prinport (exp
, port
, type
)
658 scm_gen_puts (scm_regular_string
, "#<", port
);
659 if (SCM_CLOSEDP (exp
))
660 scm_gen_puts (scm_regular_string
, "closed: ", port
);
663 if (SCM_RDNG
& SCM_CAR (exp
))
664 scm_gen_puts (scm_regular_string
, "input: ", port
);
665 if (SCM_WRTNG
& SCM_CAR (exp
))
666 scm_gen_puts (scm_regular_string
, "output: ", port
);
668 scm_gen_puts (scm_regular_string
, type
, port
);
669 scm_gen_putc (' ', port
);
675 if (SCM_OPENP (exp
) && scm_tc16_fport
== SCM_TYP16 (exp
) && isatty (fileno ((FILE *)SCM_STREAM (exp
))))
676 scm_gen_puts (scm_regular_string
, ttyname (fileno ((FILE *)SCM_STREAM (exp
))), port
);
683 if (SCM_OPFPORTP (exp
))
684 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp
)), 10, port
);
686 scm_intprint (SCM_CDR (exp
), 16, port
);
687 scm_gen_putc ('>', port
);
692 scm_ports_prehistory ()
695 scm_ptobs
= (scm_ptobfuns
*) malloc (sizeof (scm_ptobfuns
));
697 /* WARNING: These scm_newptob calls must be done in this order.
698 * They must agree with the port declarations in tags.h.
700 /* scm_tc16_fport = */ scm_newptob (&scm_fptob
);
701 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob
);
702 /* scm_tc16_strport = */ scm_newptob (&scm_stptob
);
703 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob
);
711 int scm_tc16_void_port
= 0;
714 print_void_port (exp
, port
, writing
)
719 scm_prinport (exp
, port
, "void");
724 putc_void_port (c
, strm
)
728 return 0; /* vestigial return value */
732 puts_void_port (s
, strm
)
736 return 0; /* vestigial return value */
740 write_void_port (ptr
, size
, nitems
, strm
)
752 static int flush_void_port
SCM_P ((SCM strm
));
755 flush_void_port (strm
)
762 static int getc_void_port
SCM_P ((SCM strm
));
765 getc_void_port (strm
)
772 static int close_void_port
SCM_P ((SCM strm
));
775 close_void_port (strm
)
778 return 0; /* this is ignored by scm_close_port. */
783 static int noop0
SCM_P ((SCM stream
));
793 static struct scm_ptobfuns void_port_ptob
=
811 scm_void_port (mode_str
)
816 struct scm_port_table
* pt
;
818 SCM_NEWCELL (answer
);
820 mode_bits
= scm_mode_bits (mode_str
);
821 pt
= scm_add_to_port_table (answer
);
822 SCM_SETCAR (answer
, scm_tc16_void_port
| mode_bits
);
823 SCM_SETPTAB_ENTRY (answer
, pt
);
824 SCM_SETSTREAM (answer
, SCM_BOOL_F
);
830 SCM_PROC (s_sys_make_void_port
, "%make-void-port", 1, 0, 0, scm_sys_make_void_port
);
833 scm_sys_make_void_port (mode
)
836 SCM_ASSERT (SCM_NIMP (mode
) && SCM_STRINGP (mode
), mode
,
837 SCM_ARG1
, s_sys_make_void_port
);
839 return scm_void_port (SCM_ROCHARS (mode
));
850 scm_tc16_void_port
= scm_newptob (&void_port_ptob
);