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