Simplify smob and port marking; set the mark bit in the generic
[bpt/guile.git] / libguile / ports.c
CommitLineData
1e598865 1/* Copyright (C) 1995,1996,1997 Free Software Foundation, Inc.
0f2d19dd
JB
2 *
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)
6 * any later version.
7 *
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.
12 *
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
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.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
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.
37 *
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
0f2d19dd
JB
41\f
42#include <stdio.h>
43#include "_scm.h"
20e6290e
JB
44#include "genio.h"
45#include "chars.h"
0f2d19dd 46
20e6290e 47#include "markers.h"
5d09ff4e 48#include "filesys.h"
20e6290e
JB
49#include "fports.h"
50#include "strports.h"
51#include "vports.h"
89ea5b7c 52#include "kw.h"
20e6290e
JB
53
54#include "ports.h"
0f2d19dd
JB
55
56#ifdef HAVE_MALLOC_H
95b88819 57#include <malloc.h>
0f2d19dd
JB
58#endif
59
60#ifdef HAVE_UNISTD_H
61#include <unistd.h>
62#endif
63
95b88819
GH
64#ifdef HAVE_SYS_IOCTL_H
65#include <sys/ioctl.h>
66#endif
0f2d19dd
JB
67\f
68
69
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).
74 */
75scm_ptobfuns *scm_ptobs;
76scm_sizet scm_numptob;
77
1cc91f1b 78
0f2d19dd
JB
79SCM
80scm_markstream (ptr)
81 SCM ptr;
0f2d19dd
JB
82{
83 int openp;
0f2d19dd 84 openp = SCM_CAR (ptr) & SCM_OPN;
0f2d19dd
JB
85 if (openp)
86 return SCM_STREAM (ptr);
87 else
88 return SCM_BOOL_F;
89}
90
91
1cc91f1b 92
0f2d19dd
JB
93long
94scm_newptob (ptob)
95 scm_ptobfuns *ptob;
0f2d19dd
JB
96{
97 char *tmp;
98 if (255 <= scm_numptob)
99 goto ptoberr;
100 SCM_DEFER_INTS;
101 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs, (1 + scm_numptob) * sizeof (scm_ptobfuns)));
102 if (tmp)
103 {
104 scm_ptobs = (scm_ptobfuns *) tmp;
105 scm_ptobs[scm_numptob].mark = ptob->mark;
106 scm_ptobs[scm_numptob].free = ptob->free;
107 scm_ptobs[scm_numptob].print = ptob->print;
108 scm_ptobs[scm_numptob].equalp = ptob->equalp;
109 scm_ptobs[scm_numptob].fputc = ptob->fputc;
110 scm_ptobs[scm_numptob].fputs = ptob->fputs;
111 scm_ptobs[scm_numptob].fwrite = ptob->fwrite;
112 scm_ptobs[scm_numptob].fflush = ptob->fflush;
113 scm_ptobs[scm_numptob].fgetc = ptob->fgetc;
3cb988bd 114 scm_ptobs[scm_numptob].fgets = ptob->fgets;
0f2d19dd
JB
115 scm_ptobs[scm_numptob].fclose = ptob->fclose;
116 scm_numptob++;
117 }
118 SCM_ALLOW_INTS;
119 if (!tmp)
120 ptoberr:scm_wta (SCM_MAKINUM ((long) scm_numptob), (char *) SCM_NALLOC, "newptob");
121 return scm_tc7_port + (scm_numptob - 1) * 256;
122}
123
124\f
125/* internal SCM call */
1cc91f1b 126
0f2d19dd
JB
127void
128scm_fflush (port)
129 SCM port;
0f2d19dd
JB
130{
131 scm_sizet i = SCM_PTOBNUM (port);
132 (scm_ptobs[i].fflush) (SCM_STREAM (port));
133}
134
135\f
136
44493941 137SCM_PROC(s_char_ready_p, "char-ready?", 0, 1, 0, scm_char_ready_p);
1cc91f1b 138
0f2d19dd
JB
139SCM
140scm_char_ready_p (port)
141 SCM port;
0f2d19dd
JB
142{
143 if (SCM_UNBNDP (port))
144 port = scm_cur_inp;
145 else
146 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_char_ready_p);
147 if (SCM_CRDYP (port) || !SCM_FPORTP (port))
148 return SCM_BOOL_T;
5d09ff4e
MD
149 return (scm_input_waiting_p ((FILE *) SCM_STREAM (port), s_char_ready_p)
150 ? SCM_BOOL_T
151 : SCM_BOOL_F);
0f2d19dd 152}
0f2d19dd
JB
153
154
155\f
156
157
158/* {Standard Ports}
159 */
160SCM_PROC(s_current_input_port, "current-input-port", 0, 0, 0, scm_current_input_port);
1cc91f1b 161
0f2d19dd
JB
162SCM
163scm_current_input_port ()
0f2d19dd
JB
164{
165 return scm_cur_inp;
166}
167
168SCM_PROC(s_current_output_port, "current-output-port", 0, 0, 0, scm_current_output_port);
1cc91f1b 169
0f2d19dd
JB
170SCM
171scm_current_output_port ()
0f2d19dd
JB
172{
173 return scm_cur_outp;
174}
175
176SCM_PROC(s_current_error_port, "current-error-port", 0, 0, 0, scm_current_error_port);
1cc91f1b 177
0f2d19dd
JB
178SCM
179scm_current_error_port ()
0f2d19dd
JB
180{
181 return scm_cur_errp;
182}
183
31614d8e
MD
184SCM_PROC(s_current_load_port, "current-load-port", 0, 0, 0, scm_current_load_port);
185
186SCM
187scm_current_load_port ()
188{
189 return scm_cur_loadp;
190}
191
0f2d19dd 192SCM_PROC(s_set_current_input_port, "set-current-input-port", 1, 0, 0, scm_set_current_input_port);
1cc91f1b 193
0f2d19dd
JB
194SCM
195scm_set_current_input_port (port)
196 SCM port;
0f2d19dd
JB
197{
198 SCM oinp = scm_cur_inp;
199 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_set_current_input_port);
200 scm_cur_inp = port;
201 return oinp;
202}
203
204
205SCM_PROC(s_set_current_output_port, "set-current-output-port", 1, 0, 0, scm_set_current_output_port);
1cc91f1b 206
0f2d19dd
JB
207SCM
208scm_set_current_output_port (port)
209 SCM port;
0f2d19dd
JB
210{
211 SCM ooutp = scm_cur_outp;
78446828 212 port = SCM_COERCE_OUTPORT (port);
0f2d19dd
JB
213 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_output_port);
214 scm_cur_outp = port;
215 return ooutp;
216}
217
218
219SCM_PROC(s_set_current_error_port, "set-current-error-port", 1, 0, 0, scm_set_current_error_port);
1cc91f1b 220
0f2d19dd
JB
221SCM
222scm_set_current_error_port (port)
223 SCM port;
0f2d19dd
JB
224{
225 SCM oerrp = scm_cur_errp;
78446828 226 port = SCM_COERCE_OUTPORT (port);
0f2d19dd
JB
227 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_error_port);
228 scm_cur_errp = port;
229 return oerrp;
230}
231
232\f
233
234/* {Ports - in general}
235 *
236 */
237
238/* Array of open ports, required for reliable MOVE->FDES etc. */
239struct scm_port_table **scm_port_table;
240
241int scm_port_table_size = 0; /* Number of ports in scm_port_table. */
242int scm_port_table_room = 20; /* Size of the array. */
243
244/* Add a port to the table. Call with SCM_DEFER_INTS active. */
1cc91f1b 245
0f2d19dd
JB
246struct scm_port_table *
247scm_add_to_port_table (port)
248 SCM port;
0f2d19dd
JB
249{
250 if (scm_port_table_size == scm_port_table_room)
251 {
252 scm_port_table = ((struct scm_port_table **)
253 realloc ((char *) scm_port_table,
67fe060e
GH
254 (scm_sizet) (sizeof (struct scm_port_table *)
255 * scm_port_table_room * 2)));
0f2d19dd
JB
256 /* !!! error checking */
257 scm_port_table_room *= 2;
258 }
259 scm_port_table[scm_port_table_size] = ((struct scm_port_table *)
260 scm_must_malloc (sizeof (struct scm_port_table),
261 "system port table"));
262 scm_port_table[scm_port_table_size]->port = port;
263 scm_port_table[scm_port_table_size]->revealed = 0;
264 scm_port_table[scm_port_table_size]->stream = 0;
ebf7394e 265 scm_port_table[scm_port_table_size]->file_name = SCM_BOOL_F;
d0defdf3 266 scm_port_table[scm_port_table_size]->line_number = 0;
0f2d19dd 267 scm_port_table[scm_port_table_size]->column_number = 0;
0f2d19dd
JB
268 return scm_port_table[scm_port_table_size++];
269}
270
271/* Remove a port from the table. Call with SCM_DEFER_INTS active. */
1cc91f1b 272
0f2d19dd
JB
273void
274scm_remove_from_port_table (port)
275 SCM port;
0f2d19dd
JB
276{
277 int i = 0;
278 while (scm_port_table[i]->port != port)
279 {
280 i++;
281 /* Error if not found: too violent? May occur in GC. */
282 if (i >= scm_port_table_size)
283 scm_wta (port, "Port not in table", "scm_remove_from_port_table");
284 }
285 scm_must_free ((char *)scm_port_table[i]);
286 scm_mallocated -= sizeof (*scm_port_table[i]);
287 scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
288 SCM_SETPTAB_ENTRY (port, 0);
289 scm_port_table_size--;
290}
291
fea6b4ea 292#ifdef GUILE_DEBUG
0f2d19dd
JB
293/* Undocumented functions for debugging. */
294/* Return the number of ports in the table. */
1cc91f1b 295
1146b6cd 296SCM_PROC(s_pt_size, "pt-size", 0, 0, 0, scm_pt_size);
0f2d19dd
JB
297SCM
298scm_pt_size ()
0f2d19dd
JB
299{
300 return SCM_MAKINUM (scm_port_table_size);
301}
302
303/* Return the ith member of the port table. */
1146b6cd 304SCM_PROC(s_pt_member, "pt-member", 1, 0, 0, scm_pt_member);
0f2d19dd
JB
305SCM
306scm_pt_member (member)
307 SCM member;
0f2d19dd
JB
308{
309 int i;
310 SCM_ASSERT (SCM_INUMP (member), member, SCM_ARG1, s_pt_member);
311 i = SCM_INUM (member);
312 if (i < 0 || i >= scm_port_table_size)
313 return SCM_BOOL_F;
314 else
315 return scm_port_table[i]->port;
316}
317#endif
318
319
8b13c6b3
GH
320/* Find a port in the table and return its revealed count.
321 Also used by the garbage collector.
0f2d19dd 322 */
1cc91f1b 323
0f2d19dd
JB
324int
325scm_revealed_count (port)
326 SCM port;
0f2d19dd
JB
327{
328 return SCM_REVEALED(port);
329}
330
331
332
333/* Return the revealed count for a port. */
334
335SCM_PROC(s_port_revealed, "port-revealed", 1, 0, 0, scm_port_revealed);
1cc91f1b 336
0f2d19dd
JB
337SCM
338scm_port_revealed (port)
339 SCM port;
0f2d19dd 340{
78446828 341 port = SCM_COERCE_OUTPORT (port);
0f2d19dd 342 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_port_revealed);
8b13c6b3 343 return SCM_MAKINUM (scm_revealed_count (port));
0f2d19dd
JB
344}
345
346/* Set the revealed count for a port. */
347SCM_PROC(s_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x);
1cc91f1b 348
0f2d19dd
JB
349SCM
350scm_set_port_revealed_x (port, rcount)
351 SCM port;
352 SCM rcount;
0f2d19dd 353{
78446828 354 port = SCM_COERCE_OUTPORT (port);
0f2d19dd
JB
355 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_set_port_revealed_x);
356 SCM_ASSERT (SCM_INUMP (rcount), rcount, SCM_ARG2, s_set_port_revealed_x);
357 SCM_DEFER_INTS;
358 SCM_REVEALED (port) = SCM_INUM (rcount);
359 SCM_ALLOW_INTS;
8b13c6b3 360 return SCM_UNSPECIFIED;
0f2d19dd
JB
361}
362
eadd48de
GH
363/* Return the flags that characterize a port based on the mode
364 * string used to open a file for that port.
365 *
366 * See PORT FLAGS in scm.h
367 */
368
369long
370scm_mode_bits (modes)
371 char *modes;
372{
373 return (SCM_OPN
374 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
375 | ( strchr (modes, 'w')
376 || strchr (modes, 'a')
377 || strchr (modes, '+') ? SCM_WRTNG : 0)
378 | (strchr (modes, '0') ? SCM_BUF0 : 0));
379}
380
381
382/* Return the mode flags from an open port.
383 * Some modes such as "append" are only used when opening
384 * a file and are not returned here. */
385
386SCM_PROC(s_port_mode, "port-mode", 1, 0, 0, scm_port_mode);
387
388SCM
389scm_port_mode (port)
390 SCM port;
391{
392 char modes[3];
393 modes[0] = '\0';
78446828
MV
394
395 port = SCM_COERCE_OUTPORT (port);
eadd48de
GH
396 SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_port_mode);
397 if (SCM_CAR (port) & SCM_RDNG) {
398 if (SCM_CAR (port) & SCM_WRTNG)
399 strcpy (modes, "r+");
400 else
401 strcpy (modes, "r");
402 }
403 else if (SCM_CAR (port) & SCM_WRTNG)
404 strcpy (modes, "w");
405 if (SCM_CAR (port) & SCM_BUF0)
406 strcat (modes, "0");
407 return scm_makfromstr (modes, strlen (modes), 0);
408}
409
410
0f2d19dd
JB
411/* scm_close_port
412 * Call the close operation on a port object.
eadd48de 413 * see also scm_close.
0f2d19dd
JB
414 */
415SCM_PROC(s_close_port, "close-port", 1, 0, 0, scm_close_port);
1cc91f1b 416
0f2d19dd
JB
417SCM
418scm_close_port (port)
419 SCM port;
0f2d19dd
JB
420{
421 scm_sizet i;
eadd48de
GH
422 int rv;
423
78446828
MV
424 port = SCM_COERCE_OUTPORT (port);
425
341eaef0 426 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1,
3c1750f3 427 s_close_port);
0f2d19dd 428 if (SCM_CLOSEDP (port))
eadd48de 429 return SCM_BOOL_F;
0f2d19dd
JB
430 i = SCM_PTOBNUM (port);
431 SCM_DEFER_INTS;
432 if (scm_ptobs[i].fclose)
eadd48de
GH
433 {
434 SCM_SYSCALL (rv = (scm_ptobs[i].fclose) (SCM_STREAM (port)));
435 /* ports with a closed file descriptor can be reclosed without error. */
436 if (rv < 0 && errno != EBADF)
437 scm_syserror (s_close_port);
438 }
439 else
440 rv = 0;
0f2d19dd 441 scm_remove_from_port_table (port);
898a256f 442 SCM_SETAND_CAR (port, ~SCM_OPN);
0f2d19dd 443 SCM_ALLOW_INTS;
eadd48de 444 return (rv < 0) ? SCM_BOOL_F : SCM_BOOL_T;
0f2d19dd
JB
445}
446
447SCM_PROC(s_close_all_ports_except, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except);
1cc91f1b 448
0f2d19dd
JB
449SCM
450scm_close_all_ports_except (ports)
451 SCM ports;
0f2d19dd
JB
452{
453 int i = 0;
454 SCM_ASSERT (SCM_NIMP (ports) && SCM_CONSP (ports), ports, SCM_ARG1, s_close_all_ports_except);
455 SCM_DEFER_INTS;
456 while (i < scm_port_table_size)
457 {
458 SCM thisport = scm_port_table[i]->port;
459 int found = 0;
460 SCM ports_ptr = ports;
461
462 while (SCM_NNULLP (ports_ptr))
463 {
78446828 464 SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr));
0f2d19dd
JB
465 if (i == 0)
466 SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_close_all_ports_except);
467 if (port == thisport)
468 found = 1;
469 ports_ptr = SCM_CDR (ports_ptr);
470 }
471 if (found)
472 i++;
473 else
474 /* i is not to be incremented here. */
475 scm_close_port (thisport);
476 }
477 SCM_ALLOW_INTS;
478 return SCM_UNSPECIFIED;
479}
480
481SCM_PROC(s_input_port_p, "input-port?", 1, 0, 0, scm_input_port_p);
1cc91f1b 482
0f2d19dd
JB
483SCM
484scm_input_port_p (x)
485 SCM x;
0f2d19dd
JB
486{
487 if (SCM_IMP (x))
488 return SCM_BOOL_F;
489 return SCM_INPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F;
490}
491
492SCM_PROC(s_output_port_p, "output-port?", 1, 0, 0, scm_output_port_p);
1cc91f1b 493
0f2d19dd
JB
494SCM
495scm_output_port_p (x)
496 SCM x;
0f2d19dd
JB
497{
498 if (SCM_IMP (x))
499 return SCM_BOOL_F;
500 return SCM_OUTPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F;
501}
502
503
504SCM_PROC(s_eof_object_p, "eof-object?", 1, 0, 0, scm_eof_object_p);
1cc91f1b 505
0f2d19dd
JB
506SCM
507scm_eof_object_p (x)
508 SCM x;
0f2d19dd 509{
0c32d76c 510 return SCM_EOF_OBJECT_P (x) ? SCM_BOOL_T : SCM_BOOL_F;
0f2d19dd
JB
511}
512
513SCM_PROC(s_force_output, "force-output", 0, 1, 0, scm_force_output);
1cc91f1b 514
0f2d19dd
JB
515SCM
516scm_force_output (port)
517 SCM port;
0f2d19dd
JB
518{
519 if (SCM_UNBNDP (port))
520 port = scm_cur_outp;
521 else
78446828
MV
522 {
523 port = SCM_COERCE_OUTPORT (port);
524 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_force_output);
525 }
0f2d19dd
JB
526 {
527 scm_sizet i = SCM_PTOBNUM (port);
528 SCM_SYSCALL ((scm_ptobs[i].fflush) (SCM_STREAM (port)));
529 return SCM_UNSPECIFIED;
530 }
531}
532
9c29ac66 533SCM_PROC (s_flush_all_ports, "flush-all-ports", 0, 0, 0, scm_flush_all_ports);
89ea5b7c
GH
534SCM
535scm_flush_all_ports (void)
536{
537 int i;
538
539 for (i = 0; i < scm_port_table_size; i++)
540 {
541 SCM port = scm_port_table[i]->port;
542 if (SCM_OPOUTPORTP (port))
543 {
544 scm_sizet ptob = SCM_PTOBNUM (port);
545 (scm_ptobs[ptob].fflush) (SCM_STREAM (port));
546 }
547 }
548 return SCM_UNSPECIFIED;
549}
0f2d19dd
JB
550
551SCM_PROC(s_read_char, "read-char", 0, 1, 0, scm_read_char);
1cc91f1b 552
0f2d19dd
JB
553SCM
554scm_read_char (port)
555 SCM port;
0f2d19dd
JB
556{
557 int c;
558 if (SCM_UNBNDP (port))
334341aa 559 port = scm_cur_inp;
0f2d19dd
JB
560 else
561 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_read_char);
b7f3516f 562 c = scm_getc (port);
0f2d19dd
JB
563 if (EOF == c)
564 return SCM_EOF_VAL;
565 return SCM_MAKICHR (c);
566}
567
568
569SCM_PROC(s_peek_char, "peek-char", 0, 1, 0, scm_peek_char);
1cc91f1b 570
0f2d19dd
JB
571SCM
572scm_peek_char (port)
573 SCM port;
0f2d19dd
JB
574{
575 int c;
576 if (SCM_UNBNDP (port))
577 port = scm_cur_inp;
578 else
579 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_peek_char);
b7f3516f 580 c = scm_getc (port);
0f2d19dd
JB
581 if (EOF == c)
582 return SCM_EOF_VAL;
b7f3516f 583 scm_ungetc (c, port);
0f2d19dd
JB
584 return SCM_MAKICHR (c);
585}
586
3cb988bd
TP
587/*
588 * A generic fgets method. We supply this method so that ports which
589 * can't use fgets(3) (like string ports or soft ports) can still use
590 * line-based i/o. The generic method calls the port's own fgetc method
591 * for input. It should be possible to write a more efficient
592 * method for any given port representation -- this is supplied just
593 * to ensure that you don't have to.
594 */
595
848f2a01 596char * scm_generic_fgets SCM_P ((SCM port, int *len));
3cb988bd
TP
597
598char *
848f2a01 599scm_generic_fgets (port, len)
3cb988bd 600 SCM port;
848f2a01 601 int *len;
3cb988bd
TP
602{
603 SCM f = SCM_STREAM (port);
604 scm_sizet p = SCM_PTOBNUM (port);
605
848f2a01 606 char *buf;
3cb988bd
TP
607 int limit = 80; /* current size of buffer */
608 int c;
609
848f2a01 610 /* FIXME: It would be nice to be able to check for EOF before anything. */
3cb988bd 611
848f2a01 612 *len = 0;
3e2043c4
TP
613 buf = (char *) malloc (limit * sizeof(char));
614
615 /* If a char has been pushed onto the port with scm_ungetc,
616 read that first. */
617 if (SCM_CRDYP (port))
618 {
848f2a01 619 buf[*len] = SCM_CGETUN (port);
3e2043c4 620 SCM_CLRDY (port);
848f2a01 621 if (buf[(*len)++] == '\n')
3e2043c4 622 {
848f2a01 623 buf[*len] = '\0';
3e2043c4
TP
624 return buf;
625 }
626 }
3cb988bd
TP
627
628 while (1) {
848f2a01 629 if (*len >= limit-1)
3cb988bd 630 {
3e2043c4 631 buf = (char *) realloc (buf, sizeof(char) * limit * 2);
3cb988bd
TP
632 limit *= 2;
633 }
634
635 c = (scm_ptobs[p].fgetc) (f);
636 if (c != EOF)
848f2a01 637 buf[(*len)++] = c;
3cb988bd
TP
638
639 if (c == EOF || c == '\n')
640 {
848f2a01 641 if (*len)
3cb988bd 642 {
848f2a01 643 buf[*len] = '\0';
3cb988bd
TP
644 return buf;
645 }
3e2043c4 646 free (buf);
3cb988bd
TP
647 return NULL;
648 }
649 }
650}
651
0f2d19dd 652SCM_PROC (s_unread_char, "unread-char", 2, 0, 0, scm_unread_char);
1cc91f1b 653
0f2d19dd
JB
654SCM
655scm_unread_char (cobj, port)
656 SCM cobj;
657 SCM port;
0f2d19dd
JB
658{
659 int c;
660
661 SCM_ASSERT (SCM_ICHRP (cobj), cobj, SCM_ARG1, s_unread_char);
662
663 if (SCM_UNBNDP (port))
664 port = scm_cur_inp;
665 else
666 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG2, s_unread_char);
667
668
669 c = SCM_ICHR (cobj);
670
b7f3516f 671 scm_ungetc (c, port);
0f2d19dd
JB
672 return cobj;
673}
674
360fc44c 675SCM_PROC (s_port_line, "port-line", 1, 0, 0, scm_port_line);
1cc91f1b 676
0f2d19dd 677SCM
d14af9f2 678scm_port_line (port)
0f2d19dd 679 SCM port;
0f2d19dd 680{
78446828 681 port = SCM_COERCE_OUTPORT (port);
360fc44c
MD
682 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
683 port,
684 SCM_ARG1,
685 s_port_line);
686 return SCM_MAKINUM (SCM_LINUM (port));
0f2d19dd
JB
687}
688
360fc44c 689SCM_PROC (s_set_port_line_x, "set-port-line!", 2, 0, 0, scm_set_port_line_x);
d043d8c2
MD
690
691SCM
692scm_set_port_line_x (port, line)
693 SCM port;
694 SCM line;
695{
360fc44c
MD
696 port = SCM_COERCE_OUTPORT (port);
697 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
698 port,
699 SCM_ARG1,
700 s_set_port_line_x);
701 SCM_ASSERT (SCM_INUMP (line), line, SCM_ARG2, s_set_port_line_x);
d043d8c2
MD
702 return SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
703}
704
360fc44c 705SCM_PROC (s_port_column, "port-column", 1, 0, 0, scm_port_column);
1cc91f1b 706
0f2d19dd 707SCM
d14af9f2 708scm_port_column (port)
0f2d19dd 709 SCM port;
0f2d19dd 710{
78446828 711 port = SCM_COERCE_OUTPORT (port);
360fc44c
MD
712 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
713 port,
714 SCM_ARG1,
715 s_port_column);
716 return SCM_MAKINUM (SCM_COL (port));
0f2d19dd
JB
717}
718
360fc44c 719SCM_PROC (s_set_port_column_x, "set-port-column!", 2, 0, 0, scm_set_port_column_x);
d043d8c2
MD
720
721SCM
722scm_set_port_column_x (port, column)
723 SCM port;
724 SCM column;
725{
360fc44c
MD
726 port = SCM_COERCE_OUTPORT (port);
727 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
728 port,
729 SCM_ARG1,
730 s_set_port_column_x);
731 SCM_ASSERT (SCM_INUMP (column), column, SCM_ARG2, s_set_port_column_x);
d043d8c2
MD
732 return SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
733}
734
360fc44c 735SCM_PROC (s_port_filename, "port-filename", 1, 0, 0, scm_port_filename);
1cc91f1b 736
0f2d19dd 737SCM
d14af9f2 738scm_port_filename (port)
0f2d19dd 739 SCM port;
0f2d19dd 740{
78446828 741 port = SCM_COERCE_OUTPORT (port);
360fc44c
MD
742 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
743 port,
744 SCM_ARG1,
745 s_port_filename);
746 return SCM_PTAB_ENTRY (port)->file_name;
0f2d19dd
JB
747}
748
360fc44c 749SCM_PROC (s_set_port_filename_x, "set-port-filename!", 2, 0, 0, scm_set_port_filename_x);
1cc91f1b 750
d14af9f2
MD
751SCM
752scm_set_port_filename_x (port, filename)
753 SCM port;
754 SCM filename;
d14af9f2 755{
360fc44c
MD
756 port = SCM_COERCE_OUTPORT (port);
757 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
758 port,
759 SCM_ARG1,
760 s_set_port_filename_x);
761 /* We allow the user to set the filename to whatever he likes. */
d14af9f2
MD
762 return SCM_PTAB_ENTRY (port)->file_name = filename;
763}
764
0f2d19dd
JB
765#ifndef ttyname
766extern char * ttyname();
767#endif
768
1cc91f1b 769
0f2d19dd
JB
770void
771scm_prinport (exp, port, type)
772 SCM exp;
773 SCM port;
774 char *type;
0f2d19dd 775{
b7f3516f 776 scm_puts ("#<", port);
0f2d19dd 777 if (SCM_CLOSEDP (exp))
b7f3516f 778 scm_puts ("closed: ", port);
0f2d19dd
JB
779 else
780 {
781 if (SCM_RDNG & SCM_CAR (exp))
b7f3516f 782 scm_puts ("input: ", port);
0f2d19dd 783 if (SCM_WRTNG & SCM_CAR (exp))
b7f3516f 784 scm_puts ("output: ", port);
0f2d19dd 785 }
b7f3516f
TT
786 scm_puts (type, port);
787 scm_putc (' ', port);
0f2d19dd
JB
788#ifndef MSDOS
789#ifndef __EMX__
790#ifndef _DCC
791#ifndef AMIGA
792#ifndef THINK_C
793 if (SCM_OPENP (exp) && scm_tc16_fport == SCM_TYP16 (exp) && isatty (fileno ((FILE *)SCM_STREAM (exp))))
b7f3516f 794 scm_puts (ttyname (fileno ((FILE *)SCM_STREAM (exp))), port);
0f2d19dd
JB
795 else
796#endif
797#endif
798#endif
799#endif
800#endif
801 if (SCM_OPFPORTP (exp))
802 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp)), 10, port);
803 else
804 scm_intprint (SCM_CDR (exp), 16, port);
b7f3516f 805 scm_putc ('>', port);
0f2d19dd
JB
806}
807
1cc91f1b 808
0f2d19dd
JB
809void
810scm_ports_prehistory ()
0f2d19dd
JB
811{
812 scm_numptob = 0;
813 scm_ptobs = (scm_ptobfuns *) malloc (sizeof (scm_ptobfuns));
814
815 /* WARNING: These scm_newptob calls must be done in this order.
816 * They must agree with the port declarations in tags.h.
817 */
818 /* scm_tc16_fport = */ scm_newptob (&scm_fptob);
819 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob);
820 /* scm_tc16_strport = */ scm_newptob (&scm_stptob);
821 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob);
822}
823\f
824
825\f
826/* {Void Ports}
827 */
828
829int scm_tc16_void_port = 0;
830
831static int
38cb0e9c 832print_void_port (SCM exp, SCM port, scm_print_state *pstate)
0f2d19dd
JB
833{
834 scm_prinport (exp, port, "void");
835 return 1;
836}
837
838static int
38cb0e9c 839putc_void_port (int c, SCM strm)
0f2d19dd
JB
840{
841 return 0; /* vestigial return value */
842}
843
844static int
38cb0e9c 845puts_void_port (char *s, SCM strm)
0f2d19dd
JB
846{
847 return 0; /* vestigial return value */
848}
849
850static scm_sizet
38cb0e9c 851write_void_port (char *ptr, scm_sizet size, scm_sizet nitems, SCM strm)
0f2d19dd
JB
852{
853 int len;
854 len = size * nitems;
855 return len;
856}
857
1cc91f1b 858
0f2d19dd 859static int
38cb0e9c 860flush_void_port (SCM strm)
0f2d19dd
JB
861{
862 return 0;
863}
864
1cc91f1b 865
0f2d19dd 866static int
38cb0e9c 867getc_void_port (SCM strm)
0f2d19dd
JB
868{
869 return EOF;
870}
871
3cb988bd 872static char *
848f2a01 873fgets_void_port (SCM strm, int *len)
3cb988bd
TP
874{
875 return NULL;
876}
1cc91f1b 877
0f2d19dd 878static int
38cb0e9c 879close_void_port (SCM strm)
0f2d19dd
JB
880{
881 return 0; /* this is ignored by scm_close_port. */
882}
883
884
1cc91f1b 885
0f2d19dd 886static int
38cb0e9c 887noop0 (SCM stream)
0f2d19dd
JB
888{
889 return 0;
890}
891
892
893static struct scm_ptobfuns void_port_ptob =
894{
dc53f026 895 0,
0f2d19dd
JB
896 noop0,
897 print_void_port,
898 0, /* equal? */
899 putc_void_port,
900 puts_void_port,
901 write_void_port,
902 flush_void_port,
903 getc_void_port,
3cb988bd 904 fgets_void_port,
0f2d19dd
JB
905 close_void_port,
906};
907
908\f
909
1cc91f1b 910
0f2d19dd
JB
911SCM
912scm_void_port (mode_str)
913 char * mode_str;
0f2d19dd
JB
914{
915 int mode_bits;
916 SCM answer;
917 struct scm_port_table * pt;
918
919 SCM_NEWCELL (answer);
920 SCM_DEFER_INTS;
921 mode_bits = scm_mode_bits (mode_str);
922 pt = scm_add_to_port_table (answer);
898a256f 923 SCM_SETCAR (answer, scm_tc16_void_port | mode_bits);
0f2d19dd
JB
924 SCM_SETPTAB_ENTRY (answer, pt);
925 SCM_SETSTREAM (answer, SCM_BOOL_F);
926 SCM_ALLOW_INTS;
927 return answer;
928}
929
930
931SCM_PROC (s_sys_make_void_port, "%make-void-port", 1, 0, 0, scm_sys_make_void_port);
1cc91f1b 932
0f2d19dd
JB
933SCM
934scm_sys_make_void_port (mode)
935 SCM mode;
0f2d19dd 936{
89958ad0 937 SCM_ASSERT (SCM_NIMP (mode) && SCM_ROSTRINGP (mode), mode,
0f2d19dd
JB
938 SCM_ARG1, s_sys_make_void_port);
939
89958ad0 940 SCM_COERCE_SUBSTR (mode);
0f2d19dd
JB
941 return scm_void_port (SCM_ROCHARS (mode));
942}
943
944
945
946\f
947
1cc91f1b 948
0f2d19dd
JB
949void
950scm_init_ports ()
0f2d19dd
JB
951{
952 scm_tc16_void_port = scm_newptob (&void_port_ptob);
953#include "ports.x"
954}
955