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