1 /* Copyright (C) 1995,1996,1997 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. */
64 #ifdef HAVE_SYS_IOCTL_H
65 #include <sys/ioctl.h>
70 /* scm_ptobs scm_numptob
71 * implement a dynamicly resized array of ptob records.
72 * Indexes into this table are used when generating type
73 * tags for smobjects (if you know a tag you can get an index and conversely).
75 scm_ptobfuns
*scm_ptobs
;
76 scm_sizet scm_numptob
;
84 if (SCM_GC8MARKP (ptr
))
86 openp
= SCM_CAR (ptr
) & SCM_OPN
;
89 return SCM_STREAM (ptr
);
101 if (255 <= scm_numptob
)
104 SCM_SYSCALL (tmp
= (char *) realloc ((char *) scm_ptobs
, (1 + scm_numptob
) * sizeof (scm_ptobfuns
)));
107 scm_ptobs
= (scm_ptobfuns
*) tmp
;
108 scm_ptobs
[scm_numptob
].mark
= ptob
->mark
;
109 scm_ptobs
[scm_numptob
].free
= ptob
->free
;
110 scm_ptobs
[scm_numptob
].print
= ptob
->print
;
111 scm_ptobs
[scm_numptob
].equalp
= ptob
->equalp
;
112 scm_ptobs
[scm_numptob
].fputc
= ptob
->fputc
;
113 scm_ptobs
[scm_numptob
].fputs
= ptob
->fputs
;
114 scm_ptobs
[scm_numptob
].fwrite
= ptob
->fwrite
;
115 scm_ptobs
[scm_numptob
].fflush
= ptob
->fflush
;
116 scm_ptobs
[scm_numptob
].fgetc
= ptob
->fgetc
;
117 scm_ptobs
[scm_numptob
].fgets
= ptob
->fgets
;
118 scm_ptobs
[scm_numptob
].fclose
= ptob
->fclose
;
123 ptoberr
:scm_wta (SCM_MAKINUM ((long) scm_numptob
), (char *) SCM_NALLOC
, "newptob");
124 return scm_tc7_port
+ (scm_numptob
- 1) * 256;
128 /* internal SCM call */
134 scm_sizet i
= SCM_PTOBNUM (port
);
135 (scm_ptobs
[i
].fflush
) (SCM_STREAM (port
));
140 SCM_PROC(s_char_ready_p
, "char-ready?", 0, 1, 0, scm_char_ready_p
);
143 scm_char_ready_p (port
)
146 if (SCM_UNBNDP (port
))
149 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_char_ready_p
);
150 if (SCM_CRDYP (port
) || !SCM_FPORTP (port
))
152 return (scm_input_waiting_p ((FILE *) SCM_STREAM (port
), s_char_ready_p
)
163 SCM_PROC(s_current_input_port
, "current-input-port", 0, 0, 0, scm_current_input_port
);
166 scm_current_input_port ()
171 SCM_PROC(s_current_output_port
, "current-output-port", 0, 0, 0, scm_current_output_port
);
174 scm_current_output_port ()
179 SCM_PROC(s_current_error_port
, "current-error-port", 0, 0, 0, scm_current_error_port
);
182 scm_current_error_port ()
187 SCM_PROC(s_set_current_input_port
, "set-current-input-port", 1, 0, 0, scm_set_current_input_port
);
190 scm_set_current_input_port (port
)
193 SCM oinp
= scm_cur_inp
;
194 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_set_current_input_port
);
200 SCM_PROC(s_set_current_output_port
, "set-current-output-port", 1, 0, 0, scm_set_current_output_port
);
203 scm_set_current_output_port (port
)
206 SCM ooutp
= scm_cur_outp
;
207 port
= SCM_COERCE_OUTPORT (port
);
208 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_output_port
);
214 SCM_PROC(s_set_current_error_port
, "set-current-error-port", 1, 0, 0, scm_set_current_error_port
);
217 scm_set_current_error_port (port
)
220 SCM oerrp
= scm_cur_errp
;
221 port
= SCM_COERCE_OUTPORT (port
);
222 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_set_current_error_port
);
229 /* {Ports - in general}
233 /* Array of open ports, required for reliable MOVE->FDES etc. */
234 struct scm_port_table
**scm_port_table
;
236 int scm_port_table_size
= 0; /* Number of ports in scm_port_table. */
237 int scm_port_table_room
= 20; /* Size of the array. */
239 /* Add a port to the table. Call with SCM_DEFER_INTS active. */
241 struct scm_port_table
*
242 scm_add_to_port_table (port
)
245 if (scm_port_table_size
== scm_port_table_room
)
247 scm_port_table
= ((struct scm_port_table
**)
248 realloc ((char *) scm_port_table
,
249 (scm_sizet
) (sizeof (struct scm_port_table
*)
250 * scm_port_table_room
* 2)));
251 /* !!! error checking */
252 scm_port_table_room
*= 2;
254 scm_port_table
[scm_port_table_size
] = ((struct scm_port_table
*)
255 scm_must_malloc (sizeof (struct scm_port_table
),
256 "system port table"));
257 scm_port_table
[scm_port_table_size
]->port
= port
;
258 scm_port_table
[scm_port_table_size
]->revealed
= 0;
259 scm_port_table
[scm_port_table_size
]->stream
= 0;
260 scm_port_table
[scm_port_table_size
]->file_name
= SCM_BOOL_F
;
261 scm_port_table
[scm_port_table_size
]->line_number
= 0;
262 scm_port_table
[scm_port_table_size
]->column_number
= 0;
263 return scm_port_table
[scm_port_table_size
++];
266 /* Remove a port from the table. Call with SCM_DEFER_INTS active. */
269 scm_remove_from_port_table (port
)
273 while (scm_port_table
[i
]->port
!= port
)
276 /* Error if not found: too violent? May occur in GC. */
277 if (i
>= scm_port_table_size
)
278 scm_wta (port
, "Port not in table", "scm_remove_from_port_table");
280 scm_must_free ((char *)scm_port_table
[i
]);
281 scm_mallocated
-= sizeof (*scm_port_table
[i
]);
282 scm_port_table
[i
] = scm_port_table
[scm_port_table_size
- 1];
283 SCM_SETPTAB_ENTRY (port
, 0);
284 scm_port_table_size
--;
288 /* Undocumented functions for debugging. */
289 /* Return the number of ports in the table. */
291 SCM_PROC(s_pt_size
, "pt-size", 0, 0, 0, scm_pt_size
);
295 return SCM_MAKINUM (scm_port_table_size
);
298 /* Return the ith member of the port table. */
299 SCM_PROC(s_pt_member
, "pt-member", 1, 0, 0, scm_pt_member
);
301 scm_pt_member (member
)
305 SCM_ASSERT (SCM_INUMP (member
), member
, SCM_ARG1
, s_pt_member
);
306 i
= SCM_INUM (member
);
307 if (i
< 0 || i
>= scm_port_table_size
)
310 return scm_port_table
[i
]->port
;
315 /* Find a port in the table and return its revealed count.
316 Also used by the garbage collector.
320 scm_revealed_count (port
)
323 return SCM_REVEALED(port
);
328 /* Return the revealed count for a port. */
330 SCM_PROC(s_port_revealed
, "port-revealed", 1, 0, 0, scm_port_revealed
);
333 scm_port_revealed (port
)
336 port
= SCM_COERCE_OUTPORT (port
);
337 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_port_revealed
);
338 return SCM_MAKINUM (scm_revealed_count (port
));
341 /* Set the revealed count for a port. */
342 SCM_PROC(s_set_port_revealed_x
, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x
);
345 scm_set_port_revealed_x (port
, rcount
)
349 port
= SCM_COERCE_OUTPORT (port
);
350 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
, s_set_port_revealed_x
);
351 SCM_ASSERT (SCM_INUMP (rcount
), rcount
, SCM_ARG2
, s_set_port_revealed_x
);
353 SCM_REVEALED (port
) = SCM_INUM (rcount
);
355 return SCM_UNSPECIFIED
;
358 /* Return the flags that characterize a port based on the mode
359 * string used to open a file for that port.
361 * See PORT FLAGS in scm.h
365 scm_mode_bits (modes
)
369 | (strchr (modes
, 'r') || strchr (modes
, '+') ? SCM_RDNG
: 0)
370 | ( strchr (modes
, 'w')
371 || strchr (modes
, 'a')
372 || strchr (modes
, '+') ? SCM_WRTNG
: 0)
373 | (strchr (modes
, '0') ? SCM_BUF0
: 0));
377 /* Return the mode flags from an open port.
378 * Some modes such as "append" are only used when opening
379 * a file and are not returned here. */
381 SCM_PROC(s_port_mode
, "port-mode", 1, 0, 0, scm_port_mode
);
390 port
= SCM_COERCE_OUTPORT (port
);
391 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_port_mode
);
392 if (SCM_CAR (port
) & SCM_RDNG
) {
393 if (SCM_CAR (port
) & SCM_WRTNG
)
394 strcpy (modes
, "r+");
398 else if (SCM_CAR (port
) & SCM_WRTNG
)
400 if (SCM_CAR (port
) & SCM_BUF0
)
402 return scm_makfromstr (modes
, strlen (modes
), 0);
407 * Call the close operation on a port object.
408 * see also scm_close.
410 SCM_PROC(s_close_port
, "close-port", 1, 0, 0, scm_close_port
);
413 scm_close_port (port
)
419 port
= SCM_COERCE_OUTPORT (port
);
421 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
), port
, SCM_ARG1
,
423 if (SCM_CLOSEDP (port
))
425 i
= SCM_PTOBNUM (port
);
427 if (scm_ptobs
[i
].fclose
)
429 SCM_SYSCALL (rv
= (scm_ptobs
[i
].fclose
) (SCM_STREAM (port
)));
430 /* ports with a closed file descriptor can be reclosed without error. */
431 if (rv
< 0 && errno
!= EBADF
)
432 scm_syserror (s_close_port
);
436 scm_remove_from_port_table (port
);
437 SCM_SETAND_CAR (port
, ~SCM_OPN
);
439 return (rv
< 0) ? SCM_BOOL_F
: SCM_BOOL_T
;
442 SCM_PROC(s_close_all_ports_except
, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except
);
445 scm_close_all_ports_except (ports
)
449 SCM_ASSERT (SCM_NIMP (ports
) && SCM_CONSP (ports
), ports
, SCM_ARG1
, s_close_all_ports_except
);
451 while (i
< scm_port_table_size
)
453 SCM thisport
= scm_port_table
[i
]->port
;
455 SCM ports_ptr
= ports
;
457 while (SCM_NNULLP (ports_ptr
))
459 SCM port
= SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr
));
461 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPPORTP (port
), port
, SCM_ARG1
, s_close_all_ports_except
);
462 if (port
== thisport
)
464 ports_ptr
= SCM_CDR (ports_ptr
);
469 /* i is not to be incremented here. */
470 scm_close_port (thisport
);
473 return SCM_UNSPECIFIED
;
476 SCM_PROC(s_input_port_p
, "input-port?", 1, 0, 0, scm_input_port_p
);
484 return SCM_INPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
487 SCM_PROC(s_output_port_p
, "output-port?", 1, 0, 0, scm_output_port_p
);
490 scm_output_port_p (x
)
495 return SCM_OUTPORTP (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
499 SCM_PROC(s_eof_object_p
, "eof-object?", 1, 0, 0, scm_eof_object_p
);
505 return SCM_EOF_OBJECT_P (x
) ? SCM_BOOL_T
: SCM_BOOL_F
;
508 SCM_PROC(s_force_output
, "force-output", 0, 1, 0, scm_force_output
);
511 scm_force_output (port
)
514 if (SCM_UNBNDP (port
))
518 port
= SCM_COERCE_OUTPORT (port
);
519 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPOUTPORTP (port
), port
, SCM_ARG1
, s_force_output
);
522 scm_sizet i
= SCM_PTOBNUM (port
);
523 SCM_SYSCALL ((scm_ptobs
[i
].fflush
) (SCM_STREAM (port
)));
524 return SCM_UNSPECIFIED
;
528 SCM_PROC (s_flush_all_ports
, "flush-all-ports", 0, 0, 0, scm_flush_all_ports
);
530 scm_flush_all_ports (void)
534 for (i
= 0; i
< scm_port_table_size
; i
++)
536 SCM port
= scm_port_table
[i
]->port
;
537 if (SCM_OPOUTPORTP (port
))
539 scm_sizet ptob
= SCM_PTOBNUM (port
);
540 (scm_ptobs
[ptob
].fflush
) (SCM_STREAM (port
));
543 return SCM_UNSPECIFIED
;
546 SCM_PROC(s_read_char
, "read-char", 0, 1, 0, scm_read_char
);
553 if (SCM_UNBNDP (port
))
556 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_read_char
);
560 return SCM_MAKICHR (c
);
564 SCM_PROC(s_peek_char
, "peek-char", 0, 1, 0, scm_peek_char
);
571 if (SCM_UNBNDP (port
))
574 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG1
, s_peek_char
);
578 scm_ungetc (c
, port
);
579 return SCM_MAKICHR (c
);
583 * A generic fgets method. We supply this method so that ports which
584 * can't use fgets(3) (like string ports or soft ports) can still use
585 * line-based i/o. The generic method calls the port's own fgetc method
586 * for input. It should be possible to write a more efficient
587 * method for any given port representation -- this is supplied just
588 * to ensure that you don't have to.
591 char * scm_generic_fgets
SCM_P ((SCM port
, int *len
));
594 scm_generic_fgets (port
, len
)
598 SCM f
= SCM_STREAM (port
);
599 scm_sizet p
= SCM_PTOBNUM (port
);
602 int limit
= 80; /* current size of buffer */
605 /* FIXME: It would be nice to be able to check for EOF before anything. */
608 buf
= (char *) malloc (limit
* sizeof(char));
610 /* If a char has been pushed onto the port with scm_ungetc,
612 if (SCM_CRDYP (port
))
614 buf
[*len
] = SCM_CGETUN (port
);
616 if (buf
[(*len
)++] == '\n')
626 buf
= (char *) realloc (buf
, sizeof(char) * limit
* 2);
630 c
= (scm_ptobs
[p
].fgetc
) (f
);
634 if (c
== EOF
|| c
== '\n')
647 SCM_PROC (s_unread_char
, "unread-char", 2, 0, 0, scm_unread_char
);
650 scm_unread_char (cobj
, port
)
656 SCM_ASSERT (SCM_ICHRP (cobj
), cobj
, SCM_ARG1
, s_unread_char
);
658 if (SCM_UNBNDP (port
))
661 SCM_ASSERT (SCM_NIMP (port
) && SCM_OPINPORTP (port
), port
, SCM_ARG2
, s_unread_char
);
666 scm_ungetc (c
, port
);
670 SCM_PROC (s_port_line
, "port-line", 1, 0, 0, scm_port_line
);
676 port
= SCM_COERCE_OUTPORT (port
);
677 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
681 return SCM_MAKINUM (SCM_LINUM (port
));
684 SCM_PROC (s_set_port_line_x
, "set-port-line!", 2, 0, 0, scm_set_port_line_x
);
687 scm_set_port_line_x (port
, line
)
691 port
= SCM_COERCE_OUTPORT (port
);
692 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
696 SCM_ASSERT (SCM_INUMP (line
), line
, SCM_ARG2
, s_set_port_line_x
);
697 return SCM_PTAB_ENTRY (port
)->line_number
= SCM_INUM (line
);
700 SCM_PROC (s_port_column
, "port-column", 1, 0, 0, scm_port_column
);
703 scm_port_column (port
)
706 port
= SCM_COERCE_OUTPORT (port
);
707 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
711 return SCM_MAKINUM (SCM_COL (port
));
714 SCM_PROC (s_set_port_column_x
, "set-port-column!", 2, 0, 0, scm_set_port_column_x
);
717 scm_set_port_column_x (port
, column
)
721 port
= SCM_COERCE_OUTPORT (port
);
722 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
725 s_set_port_column_x
);
726 SCM_ASSERT (SCM_INUMP (column
), column
, SCM_ARG2
, s_set_port_column_x
);
727 return SCM_PTAB_ENTRY (port
)->column_number
= SCM_INUM (column
);
730 SCM_PROC (s_port_filename
, "port-filename", 1, 0, 0, scm_port_filename
);
733 scm_port_filename (port
)
736 port
= SCM_COERCE_OUTPORT (port
);
737 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
741 return SCM_PTAB_ENTRY (port
)->file_name
;
744 SCM_PROC (s_set_port_filename_x
, "set-port-filename!", 2, 0, 0, scm_set_port_filename_x
);
747 scm_set_port_filename_x (port
, filename
)
751 port
= SCM_COERCE_OUTPORT (port
);
752 SCM_ASSERT (SCM_NIMP (port
) && SCM_PORTP (port
) && SCM_OPENP (port
),
755 s_set_port_filename_x
);
756 /* We allow the user to set the filename to whatever he likes. */
757 return SCM_PTAB_ENTRY (port
)->file_name
= filename
;
761 extern char * ttyname();
766 scm_prinport (exp
, port
, type
)
771 scm_puts ("#<", port
);
772 if (SCM_CLOSEDP (exp
))
773 scm_puts ("closed: ", port
);
776 if (SCM_RDNG
& SCM_CAR (exp
))
777 scm_puts ("input: ", port
);
778 if (SCM_WRTNG
& SCM_CAR (exp
))
779 scm_puts ("output: ", port
);
781 scm_puts (type
, port
);
782 scm_putc (' ', port
);
788 if (SCM_OPENP (exp
) && scm_tc16_fport
== SCM_TYP16 (exp
) && isatty (fileno ((FILE *)SCM_STREAM (exp
))))
789 scm_puts (ttyname (fileno ((FILE *)SCM_STREAM (exp
))), port
);
796 if (SCM_OPFPORTP (exp
))
797 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp
)), 10, port
);
799 scm_intprint (SCM_CDR (exp
), 16, port
);
800 scm_putc ('>', port
);
805 scm_ports_prehistory ()
808 scm_ptobs
= (scm_ptobfuns
*) malloc (sizeof (scm_ptobfuns
));
810 /* WARNING: These scm_newptob calls must be done in this order.
811 * They must agree with the port declarations in tags.h.
813 /* scm_tc16_fport = */ scm_newptob (&scm_fptob
);
814 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob
);
815 /* scm_tc16_strport = */ scm_newptob (&scm_stptob
);
816 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob
);
824 int scm_tc16_void_port
= 0;
827 print_void_port (SCM exp
, SCM port
, scm_print_state
*pstate
)
829 scm_prinport (exp
, port
, "void");
834 putc_void_port (int c
, SCM strm
)
836 return 0; /* vestigial return value */
840 puts_void_port (char *s
, SCM strm
)
842 return 0; /* vestigial return value */
846 write_void_port (char *ptr
, scm_sizet size
, scm_sizet nitems
, SCM strm
)
855 flush_void_port (SCM strm
)
862 getc_void_port (SCM strm
)
868 fgets_void_port (SCM strm
, int *len
)
874 close_void_port (SCM strm
)
876 return 0; /* this is ignored by scm_close_port. */
888 static struct scm_ptobfuns void_port_ptob
=
907 scm_void_port (mode_str
)
912 struct scm_port_table
* pt
;
914 SCM_NEWCELL (answer
);
916 mode_bits
= scm_mode_bits (mode_str
);
917 pt
= scm_add_to_port_table (answer
);
918 SCM_SETCAR (answer
, scm_tc16_void_port
| mode_bits
);
919 SCM_SETPTAB_ENTRY (answer
, pt
);
920 SCM_SETSTREAM (answer
, SCM_BOOL_F
);
926 SCM_PROC (s_sys_make_void_port
, "%make-void-port", 1, 0, 0, scm_sys_make_void_port
);
929 scm_sys_make_void_port (mode
)
932 SCM_ASSERT (SCM_NIMP (mode
) && SCM_ROSTRINGP (mode
), mode
,
933 SCM_ARG1
, s_sys_make_void_port
);
935 SCM_COERCE_SUBSTR (mode
);
936 return scm_void_port (SCM_ROCHARS (mode
));
947 scm_tc16_void_port
= scm_newptob (&void_port_ptob
);