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.
57 /* scm_ptobs scm_numptob
58 * implement a dynamicly resized array of ptob records.
59 * Indexes into this table are used when generating type
60 * tags for smobjects (if you know a tag you can get an index and conversely).
62 scm_ptobfuns
*scm_ptobs
;
63 scm_sizet scm_numptob
;
67 scm_markstream (SCM ptr
)
75 if (SCM_GC8MARKP (ptr
))
77 openp
= SCM_CAR (ptr
) & SCM_OPN
;
80 return SCM_STREAM (ptr
);
88 scm_newptob (scm_ptobfuns
*ptob
)
96 if (255 <= scm_numptob
)
99 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
, (1 + scm_numptob
) * sizeof (scm_ptobfuns
)));
102 scm_ptobs
= (scm_ptobfuns
*) tmp
;
103 scm_ptobs
[scm_numptob
].mark
= ptob
->mark
;
104 scm_ptobs
[scm_numptob
].free
= ptob
->free
;
105 scm_ptobs
[scm_numptob
].print
= ptob
->print
;
106 scm_ptobs
[scm_numptob
].equalp
= ptob
->equalp
;
107 scm_ptobs
[scm_numptob
].fputc
= ptob
->fputc
;
108 scm_ptobs
[scm_numptob
].fputs
= ptob
->fputs
;
109 scm_ptobs
[scm_numptob
].fwrite
= ptob
->fwrite
;
110 scm_ptobs
[scm_numptob
].fflush
= ptob
->fflush
;
111 scm_ptobs
[scm_numptob
].fgetc
= ptob
->fgetc
;
112 scm_ptobs
[scm_numptob
].fclose
= ptob
->fclose
;
117 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
), (char *) SCM_NALLOC
, "newptob");
118 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
122 /* internal SCM call */
125 scm_fflush (SCM port
)
132 scm_sizet i
= SCM_PTOBNUM (port
);
133 (scm_ptobs
[i
].fflush
) (SCM_STREAM (port
));
148 input_waiting (FILE *f
)
157 if (fileno (f
) == fileno (stdin
) && (isatty (fileno (stdin
))))
173 # include <sys/ioctl.h>
184 input_waiting(FILE *f
)
193 if (feof(f
)) return 1;
194 ioctl(fileno(f
), FIONREAD
, &remir
);
202 SCM_PROC(s_char_ready_p
, "char-ready?", 1, 0, 0, scm_char_ready_p
);
205 scm_char_ready_p (SCM port
)
208 scm_char_ready_p (port
)
212 if (SCM_UNBNDP (port
))
215 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_char_ready_p
);
216 if (SCM_CRDYP (port
) || !SCM_FPORTP (port
))
218 return input_waiting ((FILE *)SCM_STREAM (port
)) ? SCM_BOOL_T
: SCM_BOOL_F
;
224 SCM_PROC (s_ungetc_char_ready_p
, "ungetc-char-ready?", 1, 0, 0, scm_ungetc_char_ready_p
);
227 scm_ungetc_char_ready_p (SCM port
)
230 scm_ungetc_char_ready_p (port
)
234 if (SCM_UNBNDP (port
))
237 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_ungetc_char_ready_p
);
238 return (SCM_CRDYP (port
)
249 SCM_PROC(s_current_input_port
, "current-input-port", 0, 0, 0, scm_current_input_port
);
252 scm_current_input_port (void)
255 scm_current_input_port ()
261 SCM_PROC(s_current_output_port
, "current-output-port", 0, 0, 0, scm_current_output_port
);
264 scm_current_output_port (void)
267 scm_current_output_port ()
273 SCM_PROC(s_current_error_port
, "current-error-port", 0, 0, 0, scm_current_error_port
);
276 scm_current_error_port (void)
279 scm_current_error_port ()
285 SCM_PROC(s_set_current_input_port
, "set-current-input-port", 1, 0, 0, scm_set_current_input_port
);
288 scm_set_current_input_port (SCM port
)
291 scm_set_current_input_port (port
)
295 SCM oinp
= scm_cur_inp
;
296 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_set_current_input_port
);
302 SCM_PROC(s_set_current_output_port
, "set-current-output-port", 1, 0, 0, scm_set_current_output_port
);
305 scm_set_current_output_port (SCM port
)
308 scm_set_current_output_port (port
)
312 SCM ooutp
= scm_cur_outp
;
313 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_output_port
);
319 SCM_PROC(s_set_current_error_port
, "set-current-error-port", 1, 0, 0, scm_set_current_error_port
);
322 scm_set_current_error_port (SCM port
)
325 scm_set_current_error_port (port
)
329 SCM oerrp
= scm_cur_errp
;
330 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_error_port
);
337 /* {Ports - in general}
341 /* Array of open ports, required for reliable MOVE->FDES etc. */
342 struct scm_port_table
**scm_port_table
;
344 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
345 int scm_port_table_room
= 20; /* Size of the array. */
347 /* Add a port to the table. Call with SCM_DEFER_INTS active. */
349 struct scm_port_table
*
350 scm_add_to_port_table (SCM port
)
352 struct scm_port_table
*
353 scm_add_to_port_table (port
)
357 if (scm_port_table_size
== scm_port_table_room
)
359 scm_port_table
= ((struct scm_port_table
**)
360 realloc ((char *) scm_port_table
,
361 (long) (sizeof (struct scm_port_table
)
362 * scm_port_table_room
* 2)));
363 /* !!! error checking */
364 scm_port_table_room
*= 2;
366 scm_port_table
[scm_port_table_size
] = ((struct scm_port_table
*)
367 scm_must_malloc (sizeof (struct scm_port_table
),
368 "system port table"));
369 scm_port_table
[scm_port_table_size
]->port
= port
;
370 scm_port_table
[scm_port_table_size
]->revealed
= 0;
371 scm_port_table
[scm_port_table_size
]->stream
= 0;
372 scm_port_table
[scm_port_table_size
]->file_name
= SCM_BOOL_F
;
373 scm_port_table
[scm_port_table_size
]->line_number
= 1;
374 scm_port_table
[scm_port_table_size
]->column_number
= 0;
375 scm_port_table
[scm_port_table_size
]->representation
= scm_regular_port
;
376 return scm_port_table
[scm_port_table_size
++];
379 /* Remove a port from the table. Call with SCM_DEFER_INTS active. */
382 scm_remove_from_port_table (SCM port
)
385 scm_remove_from_port_table (port
)
390 while (scm_port_table
[i
]->port
!= port
)
393 /* Error if not found: too violent? May occur in GC. */
394 if (i
>= scm_port_table_size
)
395 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
397 scm_must_free ((char *)scm_port_table
[i
]);
398 scm_mallocated
-= sizeof (*scm_port_table
[i
]);
399 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
400 SCM_SETPTAB_ENTRY (port
, 0);
401 scm_port_table_size
--;
405 /* Undocumented functions for debugging. */
406 /* Return the number of ports in the table. */
407 static char s_pt_size
[] = "pt-size";
416 return SCM_MAKINUM (scm_port_table_size
);
419 /* Return the ith member of the port table. */
420 static char s_pt_member
[] = "pt-member";
423 scm_pt_member (SCM member
)
426 scm_pt_member (member
)
431 SCM_ASSERT (SCM_INUMP (member
), member
, SCM_ARG1
, s_pt_member
);
432 i
= SCM_INUM (member
);
433 if (i
< 0 || i
>= scm_port_table_size
)
436 return scm_port_table
[i
]->port
;
441 /* Find a port in the table and return its revealed count.
442 Also used by the garbage collector.
446 scm_revealed_count (SCM port
)
449 scm_revealed_count (port
)
453 return SCM_REVEALED(port
);
458 /* Return the revealed count for a port. */
460 SCM_PROC(s_port_revealed
, "port-revealed", 1, 0, 0, scm_port_revealed
);
463 scm_port_revealed (SCM port
)
466 scm_port_revealed (port
)
470 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_port_revealed
);
471 return SCM_MAKINUM (scm_revealed_count (port
));
474 /* Set the revealed count for a port. */
475 SCM_PROC(s_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x
);
478 scm_set_port_revealed_x (SCM port
, SCM rcount
)
481 scm_set_port_revealed_x (port
, rcount
)
486 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_set_port_revealed_x
);
487 SCM_ASSERT (SCM_INUMP (rcount
), rcount
, SCM_ARG2
, s_set_port_revealed_x
);
489 SCM_REVEALED (port
) = SCM_INUM (rcount
);
491 return SCM_UNSPECIFIED
;
495 * Call the close operation on a port object.
497 SCM_PROC(s_close_port
, "close-port", 1, 0, 0, scm_close_port
);
500 scm_close_port (SCM port
)
503 scm_close_port (port
)
508 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_close_port
);
509 if (SCM_CLOSEDP (port
))
510 return SCM_UNSPECIFIED
;
511 i
= SCM_PTOBNUM (port
);
513 if (scm_ptobs
[i
].fclose
)
514 SCM_SYSCALL ((scm_ptobs
[i
].fclose
) (SCM_STREAM (port
)));
515 scm_remove_from_port_table (port
);
516 SCM_CAR (port
) &= ~SCM_OPN
;
518 return SCM_UNSPECIFIED
;
521 SCM_PROC(s_close_all_ports_except
, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except
);
524 scm_close_all_ports_except (SCM ports
)
527 scm_close_all_ports_except (ports
)
532 SCM_ASSERT (SCM_NIMP (ports
) && SCM_CONSP (ports
), ports
, SCM_ARG1
, s_close_all_ports_except
);
534 while (i
< scm_port_table_size
)
536 SCM thisport
= scm_port_table
[i
]->port
;
538 SCM ports_ptr
= ports
;
540 while (SCM_NNULLP (ports_ptr
))
542 SCM port
= SCM_CAR (ports_ptr
);
544 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_close_all_ports_except
);
545 if (port
== thisport
)
547 ports_ptr
= SCM_CDR (ports_ptr
);
552 /* i is not to be incremented here. */
553 scm_close_port (thisport
);
556 return SCM_UNSPECIFIED
;
559 SCM_PROC(s_input_port_p
, "input-port?", 1, 0, 0, scm_input_port_p
);
562 scm_input_port_p (SCM x
)
571 return SCM_INPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
574 SCM_PROC(s_output_port_p
, "output-port?", 1, 0, 0, scm_output_port_p
);
577 scm_output_port_p (SCM x
)
580 scm_output_port_p (x
)
586 return SCM_OUTPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
590 SCM_PROC(s_eof_object_p
, "eof-object?", 1, 0, 0, scm_eof_object_p
);
593 scm_eof_object_p (SCM x
)
600 return (SCM_EOF_VAL
== x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
603 SCM_PROC(s_force_output
, "force-output", 0, 1, 0, scm_force_output
);
606 scm_force_output (SCM port
)
609 scm_force_output (port
)
613 if (SCM_UNBNDP (port
))
616 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_force_output
);
618 scm_sizet i
= SCM_PTOBNUM (port
);
619 SCM_SYSCALL ((scm_ptobs
[i
].fflush
) (SCM_STREAM (port
)));
620 return SCM_UNSPECIFIED
;
625 SCM_PROC(s_read_char
, "read-char", 0, 1, 0, scm_read_char
);
628 scm_read_char (SCM port
)
636 if (SCM_UNBNDP (port
))
639 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_char
);
640 c
= scm_gen_getc (port
);
643 return SCM_MAKICHR (c
);
647 SCM_PROC(s_peek_char
, "peek-char", 0, 1, 0, scm_peek_char
);
650 scm_peek_char (SCM port
)
658 if (SCM_UNBNDP (port
))
661 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_peek_char
);
662 c
= scm_gen_getc (port
);
665 scm_gen_ungetc (c
, port
);
666 return SCM_MAKICHR (c
);
669 SCM_PROC (s_unread_char
, "unread-char", 2, 0, 0, scm_unread_char
);
672 scm_unread_char (SCM cobj
, SCM port
)
675 scm_unread_char (cobj
, port
)
682 SCM_ASSERT (SCM_ICHRP (cobj
), cobj
, SCM_ARG1
, s_unread_char
);
684 if (SCM_UNBNDP (port
))
687 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_unread_char
);
692 scm_gen_ungetc (c
, port
);
698 SCM_PROC (s_line_number
, "line-number", 0, 1, 0, scm_line_number
);
701 scm_line_number (SCM port
)
704 scm_line_number (port
)
709 p
= ((port
== SCM_UNDEFINED
)
712 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
715 return SCM_MAKINUM (SCM_LINUM (p
));
718 SCM_PROC (s_column_number
, "column-number", 0, 1, 0, scm_column_number
);
721 scm_column_number (SCM port
)
724 scm_column_number (port
)
729 p
= ((port
== SCM_UNDEFINED
)
732 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
735 return SCM_MAKINUM (SCM_COL (p
));
738 /* !!! dubious feature */
739 SCM_PROC (s_port_file_name
, "port-file-name", 0, 1, 0, scm_port_file_name
);
742 scm_port_file_name (SCM port
)
745 scm_port_file_name (port
)
750 p
= ((port
== SCM_UNDEFINED
)
753 if (!(SCM_NIMP (p
) && SCM_PORTP (p
)))
756 return SCM_PTAB_ENTRY (p
)->file_name
;
760 extern char * ttyname();
765 scm_prinport (SCM exp
, SCM port
, char *type
)
768 scm_prinport (exp
, port
, type
)
774 scm_gen_puts (scm_regular_string
, "#<", port
);
775 if (SCM_CLOSEDP (exp
))
776 scm_gen_puts (scm_regular_string
, "closed: ", port
);
779 if (SCM_RDNG
& SCM_CAR (exp
))
780 scm_gen_puts (scm_regular_string
, "input: ", port
);
781 if (SCM_WRTNG
& SCM_CAR (exp
))
782 scm_gen_puts (scm_regular_string
, "output: ", port
);
784 scm_gen_puts (scm_regular_string
, type
, port
);
785 scm_gen_putc (' ', port
);
791 if (SCM_OPENP (exp
) && scm_tc16_fport
== SCM_TYP16 (exp
) && isatty (fileno ((FILE *)SCM_STREAM (exp
))))
792 scm_gen_puts (scm_regular_string
, ttyname (fileno ((FILE *)SCM_STREAM (exp
))), port
);
799 if (SCM_OPFPORTP (exp
))
800 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp
)), 10, port
);
802 scm_intprint (SCM_CDR (exp
), 16, port
);
803 scm_gen_putc ('>', port
);
808 scm_ports_prehistory (void)
811 scm_ports_prehistory ()
815 scm_ptobs
= (scm_ptobfuns
*) malloc (sizeof (scm_ptobfuns
));
817 /* WARNING: These scm_newptob calls must be done in this order.
818 * They must agree with the port declarations in tags.h.
820 /* scm_tc16_fport = */ scm_newptob (&scm_fptob
);
821 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob
);
822 /* scm_tc16_strport = */ scm_newptob (&scm_stptob
);
823 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob
);
831 int scm_tc16_void_port
= 0;
834 print_void_port (exp
, port
, writing
)
839 scm_prinport (exp
, port
, "void");
844 putc_void_port (c
, strm
)
848 return 0; /* vestigial return value */
852 puts_void_port (s
, strm
)
856 return 0; /* vestigial return value */
860 write_void_port (ptr
, size
, nitems
, strm
)
873 flush_void_port (SCM strm
)
876 flush_void_port (strm
)
885 getc_void_port (SCM strm
)
888 getc_void_port (strm
)
897 close_void_port (SCM strm
)
900 close_void_port (strm
)
904 return 0; /* this is ignored by scm_close_port. */
921 static struct scm_ptobfuns void_port_ptob
=
939 scm_void_port (char * mode_str
)
942 scm_void_port (mode_str
)
948 struct scm_port_table
* pt
;
950 SCM_NEWCELL (answer
);
952 mode_bits
= scm_mode_bits (mode_str
);
953 pt
= scm_add_to_port_table (answer
);
954 SCM_CAR (answer
) = scm_tc16_void_port
| mode_bits
;
955 SCM_SETPTAB_ENTRY (answer
, pt
);
956 SCM_SETSTREAM (answer
, SCM_BOOL_F
);
962 SCM_PROC (s_sys_make_void_port
, "%make-void-port", 1, 0, 0, scm_sys_make_void_port
);
965 scm_sys_make_void_port (SCM mode
)
968 scm_sys_make_void_port (mode
)
972 SCM_ASSERT (SCM_NIMP (mode
) && SCM_STRINGP (mode
), mode
,
973 SCM_ARG1
, s_sys_make_void_port
);
975 return scm_void_port (SCM_ROCHARS (mode
));
984 scm_init_ports (void)
990 scm_tc16_void_port
= scm_newptob (&void_port_ptob
);