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.
54 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
60 /* scm_ptobs scm_numptob
61 * implement a dynamicly resized array of ptob records.
62 * Indexes into this table are used when generating type
63 * tags for smobjects (if you know a tag you can get an index and conversely).
65 scm_ptobfuns
*scm_ptobs
;
66 scm_sizet scm_numptob
;
70 scm_markstream (SCM ptr
)
78 if (SCM_GC8MARKP (ptr
))
80 openp
= SCM_CAR (ptr
) & SCM_OPN
;
83 return SCM_STREAM (ptr
);
91 scm_newptob (scm_ptobfuns
*ptob
)
99 if (255 <= scm_numptob
)
102 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
, (1 + scm_numptob
) * sizeof (scm_ptobfuns
)));
105 scm_ptobs
= (scm_ptobfuns
*) tmp
;
106 scm_ptobs
[scm_numptob
].mark
= ptob
->mark
;
107 scm_ptobs
[scm_numptob
].free
= ptob
->free
;
108 scm_ptobs
[scm_numptob
].print
= ptob
->print
;
109 scm_ptobs
[scm_numptob
].equalp
= ptob
->equalp
;
110 scm_ptobs
[scm_numptob
].fputc
= ptob
->fputc
;
111 scm_ptobs
[scm_numptob
].fputs
= ptob
->fputs
;
112 scm_ptobs
[scm_numptob
].fwrite
= ptob
->fwrite
;
113 scm_ptobs
[scm_numptob
].fflush
= ptob
->fflush
;
114 scm_ptobs
[scm_numptob
].fgetc
= ptob
->fgetc
;
115 scm_ptobs
[scm_numptob
].fclose
= ptob
->fclose
;
120 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
), (char *) SCM_NALLOC
, "newptob");
121 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
125 /* internal SCM call */
128 scm_fflush (SCM port
)
135 scm_sizet i
= SCM_PTOBNUM (port
);
136 (scm_ptobs
[i
].fflush
) (SCM_STREAM (port
));
151 input_waiting (FILE *f
)
160 if (fileno (f
) == fileno (stdin
) && (isatty (fileno (stdin
))))
176 # include <sys/ioctl.h>
187 input_waiting(FILE *f
)
196 if (feof(f
)) return 1;
197 ioctl(fileno(f
), FIONREAD
, &remir
);
205 SCM_PROC(s_char_ready_p
, "char-ready?", 1, 0, 0, scm_char_ready_p
);
208 scm_char_ready_p (SCM port
)
211 scm_char_ready_p (port
)
215 if (SCM_UNBNDP (port
))
218 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_char_ready_p
);
219 if (SCM_CRDYP (port
) || !SCM_FPORTP (port
))
221 return input_waiting ((FILE *)SCM_STREAM (port
)) ? SCM_BOOL_T
: SCM_BOOL_F
;
227 SCM_PROC (s_ungetc_char_ready_p
, "ungetc-char-ready?", 1, 0, 0, scm_ungetc_char_ready_p
);
230 scm_ungetc_char_ready_p (SCM port
)
233 scm_ungetc_char_ready_p (port
)
237 if (SCM_UNBNDP (port
))
240 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_ungetc_char_ready_p
);
241 return (SCM_CRDYP (port
)
252 SCM_PROC(s_current_input_port
, "current-input-port", 0, 0, 0, scm_current_input_port
);
255 scm_current_input_port (void)
258 scm_current_input_port ()
264 SCM_PROC(s_current_output_port
, "current-output-port", 0, 0, 0, scm_current_output_port
);
267 scm_current_output_port (void)
270 scm_current_output_port ()
276 SCM_PROC(s_current_error_port
, "current-error-port", 0, 0, 0, scm_current_error_port
);
279 scm_current_error_port (void)
282 scm_current_error_port ()
288 SCM_PROC(s_set_current_input_port
, "set-current-input-port", 1, 0, 0, scm_set_current_input_port
);
291 scm_set_current_input_port (SCM port
)
294 scm_set_current_input_port (port
)
298 SCM oinp
= scm_cur_inp
;
299 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_set_current_input_port
);
305 SCM_PROC(s_set_current_output_port
, "set-current-output-port", 1, 0, 0, scm_set_current_output_port
);
308 scm_set_current_output_port (SCM port
)
311 scm_set_current_output_port (port
)
315 SCM ooutp
= scm_cur_outp
;
316 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_output_port
);
322 SCM_PROC(s_set_current_error_port
, "set-current-error-port", 1, 0, 0, scm_set_current_error_port
);
325 scm_set_current_error_port (SCM port
)
328 scm_set_current_error_port (port
)
332 SCM oerrp
= scm_cur_errp
;
333 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_error_port
);
340 /* {Ports - in general}
344 /* Array of open ports, required for reliable MOVE->FDES etc. */
345 struct scm_port_table
**scm_port_table
;
347 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
348 int scm_port_table_room
= 20; /* Size of the array. */
350 /* Add a port to the table. Call with SCM_DEFER_INTS active. */
352 struct scm_port_table
*
353 scm_add_to_port_table (SCM port
)
355 struct scm_port_table
*
356 scm_add_to_port_table (port
)
360 if (scm_port_table_size
== scm_port_table_room
)
362 scm_port_table
= ((struct scm_port_table
**)
363 realloc ((char *) scm_port_table
,
364 (long) (sizeof (struct scm_port_table
)
365 * scm_port_table_room
* 2)));
366 /* !!! error checking */
367 scm_port_table_room
*= 2;
369 scm_port_table
[scm_port_table_size
] = ((struct scm_port_table
*)
370 scm_must_malloc (sizeof (struct scm_port_table
),
371 "system port table"));
372 scm_port_table
[scm_port_table_size
]->port
= port
;
373 scm_port_table
[scm_port_table_size
]->revealed
= 0;
374 scm_port_table
[scm_port_table_size
]->stream
= 0;
375 scm_port_table
[scm_port_table_size
]->file_name
= SCM_BOOL_F
;
376 scm_port_table
[scm_port_table_size
]->line_number
= 1;
377 scm_port_table
[scm_port_table_size
]->column_number
= 0;
378 scm_port_table
[scm_port_table_size
]->representation
= scm_regular_port
;
379 return scm_port_table
[scm_port_table_size
++];
382 /* Remove a port from the table. Call with SCM_DEFER_INTS active. */
385 scm_remove_from_port_table (SCM port
)
388 scm_remove_from_port_table (port
)
393 while (scm_port_table
[i
]->port
!= port
)
396 /* Error if not found: too violent? May occur in GC. */
397 if (i
>= scm_port_table_size
)
398 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
400 scm_must_free ((char *)scm_port_table
[i
]);
401 scm_mallocated
-= sizeof (*scm_port_table
[i
]);
402 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
403 SCM_SETPTAB_ENTRY (port
, 0);
404 scm_port_table_size
--;
408 /* Undocumented functions for debugging. */
409 /* Return the number of ports in the table. */
410 static char s_pt_size
[] = "pt-size";
419 return SCM_MAKINUM (scm_port_table_size
);
422 /* Return the ith member of the port table. */
423 static char s_pt_member
[] = "pt-member";
426 scm_pt_member (SCM member
)
429 scm_pt_member (member
)
434 SCM_ASSERT (SCM_INUMP (member
), member
, SCM_ARG1
, s_pt_member
);
435 i
= SCM_INUM (member
);
436 if (i
< 0 || i
>= scm_port_table_size
)
439 return scm_port_table
[i
]->port
;
444 /* Find a port in the table and return its revealed count.
445 Also used by the garbage collector.
449 scm_revealed_count (SCM port
)
452 scm_revealed_count (port
)
456 return SCM_REVEALED(port
);
461 /* Return the revealed count for a port. */
463 SCM_PROC(s_port_revealed
, "port-revealed", 1, 0, 0, scm_port_revealed
);
466 scm_port_revealed (SCM port
)
469 scm_port_revealed (port
)
473 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_port_revealed
);
474 return SCM_MAKINUM (scm_revealed_count (port
));
477 /* Set the revealed count for a port. */
478 SCM_PROC(s_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x
);
481 scm_set_port_revealed_x (SCM port
, SCM rcount
)
484 scm_set_port_revealed_x (port
, rcount
)
489 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_set_port_revealed_x
);
490 SCM_ASSERT (SCM_INUMP (rcount
), rcount
, SCM_ARG2
, s_set_port_revealed_x
);
492 SCM_REVEALED (port
) = SCM_INUM (rcount
);
494 return SCM_UNSPECIFIED
;
498 * Call the close operation on a port object.
500 SCM_PROC(s_close_port
, "close-port", 1, 0, 0, scm_close_port
);
503 scm_close_port (SCM port
)
506 scm_close_port (port
)
511 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_close_port
);
512 if (SCM_CLOSEDP (port
))
513 return SCM_UNSPECIFIED
;
514 i
= SCM_PTOBNUM (port
);
516 if (scm_ptobs
[i
].fclose
)
517 SCM_SYSCALL ((scm_ptobs
[i
].fclose
) (SCM_STREAM (port
)));
518 scm_remove_from_port_table (port
);
519 SCM_CAR (port
) &= ~SCM_OPN
;
521 return SCM_UNSPECIFIED
;
524 SCM_PROC(s_close_all_ports_except
, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except
);
527 scm_close_all_ports_except (SCM ports
)
530 scm_close_all_ports_except (ports
)
535 SCM_ASSERT (SCM_NIMP (ports
) && SCM_CONSP (ports
), ports
, SCM_ARG1
, s_close_all_ports_except
);
537 while (i
< scm_port_table_size
)
539 SCM thisport
= scm_port_table
[i
]->port
;
541 SCM ports_ptr
= ports
;
543 while (SCM_NNULLP (ports_ptr
))
545 SCM port
= SCM_CAR (ports_ptr
);
547 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_close_all_ports_except
);
548 if (port
== thisport
)
550 ports_ptr
= SCM_CDR (ports_ptr
);
555 /* i is not to be incremented here. */
556 scm_close_port (thisport
);
559 return SCM_UNSPECIFIED
;
562 SCM_PROC(s_input_port_p
, "input-port?", 1, 0, 0, scm_input_port_p
);
565 scm_input_port_p (SCM x
)
574 return SCM_INPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
577 SCM_PROC(s_output_port_p
, "output-port?", 1, 0, 0, scm_output_port_p
);
580 scm_output_port_p (SCM x
)
583 scm_output_port_p (x
)
589 return SCM_OUTPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
593 SCM_PROC(s_eof_object_p
, "eof-object?", 1, 0, 0, scm_eof_object_p
);
596 scm_eof_object_p (SCM x
)
603 return (SCM_EOF_VAL
== x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
606 SCM_PROC(s_force_output
, "force-output", 0, 1, 0, scm_force_output
);
609 scm_force_output (SCM port
)
612 scm_force_output (port
)
616 if (SCM_UNBNDP (port
))
619 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_force_output
);
621 scm_sizet i
= SCM_PTOBNUM (port
);
622 SCM_SYSCALL ((scm_ptobs
[i
].fflush
) (SCM_STREAM (port
)));
623 return SCM_UNSPECIFIED
;
628 SCM_PROC(s_read_char
, "read-char", 0, 1, 0, scm_read_char
);
631 scm_read_char (SCM port
)
639 if (SCM_UNBNDP (port
))
642 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_char
);
643 c
= scm_gen_getc (port
);
646 return SCM_MAKICHR (c
);
650 SCM_PROC(s_peek_char
, "peek-char", 0, 1, 0, scm_peek_char
);
653 scm_peek_char (SCM port
)
661 if (SCM_UNBNDP (port
))
664 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_peek_char
);
665 c
= scm_gen_getc (port
);
668 scm_gen_ungetc (c
, port
);
669 return SCM_MAKICHR (c
);
672 SCM_PROC (s_unread_char
, "unread-char", 2, 0, 0, scm_unread_char
);
675 scm_unread_char (SCM cobj
, SCM port
)
678 scm_unread_char (cobj
, port
)
685 SCM_ASSERT (SCM_ICHRP (cobj
), cobj
, SCM_ARG1
, s_unread_char
);
687 if (SCM_UNBNDP (port
))
690 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_unread_char
);
695 scm_gen_ungetc (c
, port
);
701 SCM_PROC (s_line_number
, "line-number", 0, 1, 0, scm_line_number
);
704 scm_line_number (SCM port
)
707 scm_line_number (port
)
712 p
= ((port
== SCM_UNDEFINED
)
715 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
718 return SCM_MAKINUM (SCM_LINUM (p
));
721 SCM_PROC (s_column_number
, "column-number", 0, 1, 0, scm_column_number
);
724 scm_column_number (SCM port
)
727 scm_column_number (port
)
732 p
= ((port
== SCM_UNDEFINED
)
735 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
738 return SCM_MAKINUM (SCM_COL (p
));
741 /* !!! dubious feature */
742 SCM_PROC (s_port_file_name
, "port-file-name", 0, 1, 0, scm_port_file_name
);
745 scm_port_file_name (SCM port
)
748 scm_port_file_name (port
)
753 p
= ((port
== SCM_UNDEFINED
)
756 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
759 return SCM_PTAB_ENTRY (p
)->file_name
;
763 extern char * ttyname();
768 scm_prinport (SCM exp
, SCM port
, char *type
)
771 scm_prinport (exp
, port
, type
)
777 scm_gen_puts (scm_regular_string
, "#<", port
);
778 if (SCM_CLOSEDP (exp
))
779 scm_gen_puts (scm_regular_string
, "closed: ", port
);
782 if (SCM_RDNG
& SCM_CAR (exp
))
783 scm_gen_puts (scm_regular_string
, "input: ", port
);
784 if (SCM_WRTNG
& SCM_CAR (exp
))
785 scm_gen_puts (scm_regular_string
, "output: ", port
);
787 scm_gen_puts (scm_regular_string
, type
, port
);
788 scm_gen_putc (' ', port
);
794 if (SCM_OPENP (exp
) && scm_tc16_fport
== SCM_TYP16 (exp
) && isatty (fileno ((FILE *)SCM_STREAM (exp
))))
795 scm_gen_puts (scm_regular_string
, ttyname (fileno ((FILE *)SCM_STREAM (exp
))), port
);
802 if (SCM_OPFPORTP (exp
))
803 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp
)), 10, port
);
805 scm_intprint (SCM_CDR (exp
), 16, port
);
806 scm_gen_putc ('>', port
);
811 scm_ports_prehistory (void)
814 scm_ports_prehistory ()
818 scm_ptobs
= (scm_ptobfuns
*) malloc (sizeof (scm_ptobfuns
));
820 /* WARNING: These scm_newptob calls must be done in this order.
821 * They must agree with the port declarations in tags.h.
823 /* scm_tc16_fport = */ scm_newptob (&scm_fptob
);
824 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob
);
825 /* scm_tc16_strport = */ scm_newptob (&scm_stptob
);
826 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob
);
834 int scm_tc16_void_port
= 0;
837 print_void_port (exp
, port
, writing
)
842 scm_prinport (exp
, port
, "void");
847 putc_void_port (c
, strm
)
851 return 0; /* vestigial return value */
855 puts_void_port (s
, strm
)
859 return 0; /* vestigial return value */
863 write_void_port (ptr
, size
, nitems
, strm
)
876 flush_void_port (SCM strm
)
879 flush_void_port (strm
)
888 getc_void_port (SCM strm
)
891 getc_void_port (strm
)
900 close_void_port (SCM strm
)
903 close_void_port (strm
)
907 return 0; /* this is ignored by scm_close_port. */
924 static struct scm_ptobfuns void_port_ptob
=
942 scm_void_port (char * mode_str
)
945 scm_void_port (mode_str
)
951 struct scm_port_table
* pt
;
953 SCM_NEWCELL (answer
);
955 mode_bits
= scm_mode_bits (mode_str
);
956 pt
= scm_add_to_port_table (answer
);
957 SCM_CAR (answer
) = scm_tc16_void_port
| mode_bits
;
958 SCM_SETPTAB_ENTRY (answer
, pt
);
959 SCM_SETSTREAM (answer
, SCM_BOOL_F
);
965 SCM_PROC (s_sys_make_void_port
, "%make-void-port", 1, 0, 0, scm_sys_make_void_port
);
968 scm_sys_make_void_port (SCM mode
)
971 scm_sys_make_void_port (mode
)
975 SCM_ASSERT (SCM_NIMP (mode
) && SCM_STRINGP (mode
), mode
,
976 SCM_ARG1
, s_sys_make_void_port
);
978 return scm_void_port (SCM_ROCHARS (mode
));
987 scm_init_ports (void)
993 scm_tc16_void_port
= scm_newptob (&void_port_ptob
);