* init.c: (scm_boot_guile, scm_boot_guile_1): New, simplified
[bpt/guile.git] / libguile / ports.c
CommitLineData
0f2d19dd
JB
1/* Copyright (C) 1995,1996 Free Software Foundation, Inc.
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
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
19 *
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41\f
42#include <stdio.h>
43#include "_scm.h"
20e6290e
JB
44#include "genio.h"
45#include "chars.h"
0f2d19dd 46
20e6290e
JB
47#include "markers.h"
48#include "fports.h"
49#include "strports.h"
50#include "vports.h"
51
52#include "ports.h"
0f2d19dd
JB
53
54#ifdef HAVE_MALLOC_H
95b88819 55#include <malloc.h>
0f2d19dd
JB
56#endif
57
58#ifdef HAVE_UNISTD_H
59#include <unistd.h>
60#endif
61
95b88819
GH
62#ifdef HAVE_SYS_IOCTL_H
63#include <sys/ioctl.h>
64#endif
0f2d19dd
JB
65\f
66
67
68/* scm_ptobs scm_numptob
69 * implement a dynamicly resized array of ptob records.
70 * Indexes into this table are used when generating type
71 * tags for smobjects (if you know a tag you can get an index and conversely).
72 */
73scm_ptobfuns *scm_ptobs;
74scm_sizet scm_numptob;
75
1cc91f1b 76
0f2d19dd
JB
77SCM
78scm_markstream (ptr)
79 SCM ptr;
0f2d19dd
JB
80{
81 int openp;
82 if (SCM_GC8MARKP (ptr))
83 return SCM_BOOL_F;
84 openp = SCM_CAR (ptr) & SCM_OPN;
85 SCM_SETGC8MARK (ptr);
86 if (openp)
87 return SCM_STREAM (ptr);
88 else
89 return SCM_BOOL_F;
90}
91
92
1cc91f1b 93
0f2d19dd
JB
94long
95scm_newptob (ptob)
96 scm_ptobfuns *ptob;
0f2d19dd
JB
97{
98 char *tmp;
99 if (255 <= scm_numptob)
100 goto ptoberr;
101 SCM_DEFER_INTS;
102 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs, (1 + scm_numptob) * sizeof (scm_ptobfuns)));
103 if (tmp)
104 {
105 scm_ptobs = (scm_ptobfuns *) tmp;
106 scm_ptobs[scm_numptob].mark = ptob->mark;
107 scm_ptobs[scm_numptob].free = ptob->free;
108 scm_ptobs[scm_numptob].print = ptob->print;
109 scm_ptobs[scm_numptob].equalp = ptob->equalp;
110 scm_ptobs[scm_numptob].fputc = ptob->fputc;
111 scm_ptobs[scm_numptob].fputs = ptob->fputs;
112 scm_ptobs[scm_numptob].fwrite = ptob->fwrite;
113 scm_ptobs[scm_numptob].fflush = ptob->fflush;
114 scm_ptobs[scm_numptob].fgetc = ptob->fgetc;
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
137
138#ifdef __IBMC__
139# define MSDOS
140#endif
141#ifdef MSDOS
142# ifndef GO32
143# include <io.h>
144# include <conio.h>
1cc91f1b
JB
145
146static int input_waiting SCM_P ((FILE *f));
147
0f2d19dd
JB
148static int
149input_waiting (f)
150 FILE *f;
0f2d19dd
JB
151{
152 if (feof (f))
153 return 1;
154 if (fileno (f) == fileno (stdin) && (isatty (fileno (stdin))))
155 return kbhit ();
156 return -1;
157}
158# endif
159#else
160# ifdef _DCC
161# include <ioctl.h>
162# else
163# ifndef AMIGA
164# ifndef vms
165# ifdef MWC
166# include <sys/io.h>
167# else
168# ifndef THINK_C
169# ifndef ARM_ULIB
170# include <sys/ioctl.h>
171# endif
172# endif
173# endif
174# endif
175# endif
176# endif
177
178
1cc91f1b
JB
179
180static int input_waiting SCM_P ((FILE *f));
181
0f2d19dd
JB
182static int
183input_waiting(f)
184 FILE *f;
0f2d19dd
JB
185{
186# ifdef FIONREAD
187 long remir;
188 if (feof(f)) return 1;
189 ioctl(fileno(f), FIONREAD, &remir);
190 return remir;
191# else
192 return -1;
193# endif
194}
195#endif
196
197SCM_PROC(s_char_ready_p, "char-ready?", 1, 0, 0, scm_char_ready_p);
1cc91f1b 198
0f2d19dd
JB
199SCM
200scm_char_ready_p (port)
201 SCM port;
0f2d19dd
JB
202{
203 if (SCM_UNBNDP (port))
204 port = scm_cur_inp;
205 else
206 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_char_ready_p);
207 if (SCM_CRDYP (port) || !SCM_FPORTP (port))
208 return SCM_BOOL_T;
209 return input_waiting ((FILE *)SCM_STREAM (port)) ? SCM_BOOL_T : SCM_BOOL_F;
210}
211
212
213\f
214
215SCM_PROC (s_ungetc_char_ready_p, "ungetc-char-ready?", 1, 0, 0, scm_ungetc_char_ready_p);
1cc91f1b 216
0f2d19dd
JB
217SCM
218scm_ungetc_char_ready_p (port)
219 SCM port;
0f2d19dd
JB
220{
221 if (SCM_UNBNDP (port))
222 port = scm_cur_inp;
223 else
224 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_ungetc_char_ready_p);
225 return (SCM_CRDYP (port)
226 ? SCM_BOOL_T
227 : SCM_BOOL_F);
228}
229
230
231\f
232
233
234/* {Standard Ports}
235 */
236SCM_PROC(s_current_input_port, "current-input-port", 0, 0, 0, scm_current_input_port);
1cc91f1b 237
0f2d19dd
JB
238SCM
239scm_current_input_port ()
0f2d19dd
JB
240{
241 return scm_cur_inp;
242}
243
244SCM_PROC(s_current_output_port, "current-output-port", 0, 0, 0, scm_current_output_port);
1cc91f1b 245
0f2d19dd
JB
246SCM
247scm_current_output_port ()
0f2d19dd
JB
248{
249 return scm_cur_outp;
250}
251
252SCM_PROC(s_current_error_port, "current-error-port", 0, 0, 0, scm_current_error_port);
1cc91f1b 253
0f2d19dd
JB
254SCM
255scm_current_error_port ()
0f2d19dd
JB
256{
257 return scm_cur_errp;
258}
259
260SCM_PROC(s_set_current_input_port, "set-current-input-port", 1, 0, 0, scm_set_current_input_port);
1cc91f1b 261
0f2d19dd
JB
262SCM
263scm_set_current_input_port (port)
264 SCM port;
0f2d19dd
JB
265{
266 SCM oinp = scm_cur_inp;
267 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_set_current_input_port);
268 scm_cur_inp = port;
269 return oinp;
270}
271
272
273SCM_PROC(s_set_current_output_port, "set-current-output-port", 1, 0, 0, scm_set_current_output_port);
1cc91f1b 274
0f2d19dd
JB
275SCM
276scm_set_current_output_port (port)
277 SCM port;
0f2d19dd
JB
278{
279 SCM ooutp = scm_cur_outp;
280 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_output_port);
281 scm_cur_outp = port;
282 return ooutp;
283}
284
285
286SCM_PROC(s_set_current_error_port, "set-current-error-port", 1, 0, 0, scm_set_current_error_port);
1cc91f1b 287
0f2d19dd
JB
288SCM
289scm_set_current_error_port (port)
290 SCM port;
0f2d19dd
JB
291{
292 SCM oerrp = scm_cur_errp;
293 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_set_current_error_port);
294 scm_cur_errp = port;
295 return oerrp;
296}
297
298\f
299
300/* {Ports - in general}
301 *
302 */
303
304/* Array of open ports, required for reliable MOVE->FDES etc. */
305struct scm_port_table **scm_port_table;
306
307int scm_port_table_size = 0; /* Number of ports in scm_port_table. */
308int scm_port_table_room = 20; /* Size of the array. */
309
310/* Add a port to the table. Call with SCM_DEFER_INTS active. */
1cc91f1b 311
0f2d19dd
JB
312struct scm_port_table *
313scm_add_to_port_table (port)
314 SCM port;
0f2d19dd
JB
315{
316 if (scm_port_table_size == scm_port_table_room)
317 {
318 scm_port_table = ((struct scm_port_table **)
319 realloc ((char *) scm_port_table,
320 (long) (sizeof (struct scm_port_table)
321 * scm_port_table_room * 2)));
322 /* !!! error checking */
323 scm_port_table_room *= 2;
324 }
325 scm_port_table[scm_port_table_size] = ((struct scm_port_table *)
326 scm_must_malloc (sizeof (struct scm_port_table),
327 "system port table"));
328 scm_port_table[scm_port_table_size]->port = port;
329 scm_port_table[scm_port_table_size]->revealed = 0;
330 scm_port_table[scm_port_table_size]->stream = 0;
ebf7394e 331 scm_port_table[scm_port_table_size]->file_name = SCM_BOOL_F;
0f2d19dd
JB
332 scm_port_table[scm_port_table_size]->line_number = 1;
333 scm_port_table[scm_port_table_size]->column_number = 0;
334 scm_port_table[scm_port_table_size]->representation = scm_regular_port;
335 return scm_port_table[scm_port_table_size++];
336}
337
338/* Remove a port from the table. Call with SCM_DEFER_INTS active. */
1cc91f1b 339
0f2d19dd
JB
340void
341scm_remove_from_port_table (port)
342 SCM port;
0f2d19dd
JB
343{
344 int i = 0;
345 while (scm_port_table[i]->port != port)
346 {
347 i++;
348 /* Error if not found: too violent? May occur in GC. */
349 if (i >= scm_port_table_size)
350 scm_wta (port, "Port not in table", "scm_remove_from_port_table");
351 }
352 scm_must_free ((char *)scm_port_table[i]);
353 scm_mallocated -= sizeof (*scm_port_table[i]);
354 scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
355 SCM_SETPTAB_ENTRY (port, 0);
356 scm_port_table_size--;
357}
358
359#ifdef DEBUG
360/* Undocumented functions for debugging. */
361/* Return the number of ports in the table. */
362static char s_pt_size[] = "pt-size";
1cc91f1b 363
0f2d19dd
JB
364SCM
365scm_pt_size ()
0f2d19dd
JB
366{
367 return SCM_MAKINUM (scm_port_table_size);
368}
369
370/* Return the ith member of the port table. */
371static char s_pt_member[] = "pt-member";
1cc91f1b 372
0f2d19dd
JB
373SCM
374scm_pt_member (member)
375 SCM member;
0f2d19dd
JB
376{
377 int i;
378 SCM_ASSERT (SCM_INUMP (member), member, SCM_ARG1, s_pt_member);
379 i = SCM_INUM (member);
380 if (i < 0 || i >= scm_port_table_size)
381 return SCM_BOOL_F;
382 else
383 return scm_port_table[i]->port;
384}
385#endif
386
387
8b13c6b3
GH
388/* Find a port in the table and return its revealed count.
389 Also used by the garbage collector.
0f2d19dd 390 */
1cc91f1b 391
0f2d19dd
JB
392int
393scm_revealed_count (port)
394 SCM port;
0f2d19dd
JB
395{
396 return SCM_REVEALED(port);
397}
398
399
400
401/* Return the revealed count for a port. */
402
403SCM_PROC(s_port_revealed, "port-revealed", 1, 0, 0, scm_port_revealed);
1cc91f1b 404
0f2d19dd
JB
405SCM
406scm_port_revealed (port)
407 SCM port;
0f2d19dd 408{
0f2d19dd 409 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_port_revealed);
8b13c6b3 410 return SCM_MAKINUM (scm_revealed_count (port));
0f2d19dd
JB
411}
412
413/* Set the revealed count for a port. */
414SCM_PROC(s_set_port_revealed_x, "set-port-revealed!", 2, 0, 0, scm_set_port_revealed_x);
1cc91f1b 415
0f2d19dd
JB
416SCM
417scm_set_port_revealed_x (port, rcount)
418 SCM port;
419 SCM rcount;
0f2d19dd
JB
420{
421 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_set_port_revealed_x);
422 SCM_ASSERT (SCM_INUMP (rcount), rcount, SCM_ARG2, s_set_port_revealed_x);
423 SCM_DEFER_INTS;
424 SCM_REVEALED (port) = SCM_INUM (rcount);
425 SCM_ALLOW_INTS;
8b13c6b3 426 return SCM_UNSPECIFIED;
0f2d19dd
JB
427}
428
429/* scm_close_port
430 * Call the close operation on a port object.
431 */
432SCM_PROC(s_close_port, "close-port", 1, 0, 0, scm_close_port);
1cc91f1b 433
0f2d19dd
JB
434SCM
435scm_close_port (port)
436 SCM port;
0f2d19dd
JB
437{
438 scm_sizet i;
439 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port), port, SCM_ARG1, s_close_port);
440 if (SCM_CLOSEDP (port))
441 return SCM_UNSPECIFIED;
442 i = SCM_PTOBNUM (port);
443 SCM_DEFER_INTS;
444 if (scm_ptobs[i].fclose)
445 SCM_SYSCALL ((scm_ptobs[i].fclose) (SCM_STREAM (port)));
446 scm_remove_from_port_table (port);
898a256f 447 SCM_SETAND_CAR (port, ~SCM_OPN);
0f2d19dd
JB
448 SCM_ALLOW_INTS;
449 return SCM_UNSPECIFIED;
450}
451
452SCM_PROC(s_close_all_ports_except, "close-all-ports-except", 0, 0, 1, scm_close_all_ports_except);
1cc91f1b 453
0f2d19dd
JB
454SCM
455scm_close_all_ports_except (ports)
456 SCM ports;
0f2d19dd
JB
457{
458 int i = 0;
459 SCM_ASSERT (SCM_NIMP (ports) && SCM_CONSP (ports), ports, SCM_ARG1, s_close_all_ports_except);
460 SCM_DEFER_INTS;
461 while (i < scm_port_table_size)
462 {
463 SCM thisport = scm_port_table[i]->port;
464 int found = 0;
465 SCM ports_ptr = ports;
466
467 while (SCM_NNULLP (ports_ptr))
468 {
469 SCM port = SCM_CAR (ports_ptr);
470 if (i == 0)
471 SCM_ASSERT (SCM_NIMP (port) && SCM_OPPORTP (port), port, SCM_ARG1, s_close_all_ports_except);
472 if (port == thisport)
473 found = 1;
474 ports_ptr = SCM_CDR (ports_ptr);
475 }
476 if (found)
477 i++;
478 else
479 /* i is not to be incremented here. */
480 scm_close_port (thisport);
481 }
482 SCM_ALLOW_INTS;
483 return SCM_UNSPECIFIED;
484}
485
486SCM_PROC(s_input_port_p, "input-port?", 1, 0, 0, scm_input_port_p);
1cc91f1b 487
0f2d19dd
JB
488SCM
489scm_input_port_p (x)
490 SCM x;
0f2d19dd
JB
491{
492 if (SCM_IMP (x))
493 return SCM_BOOL_F;
494 return SCM_INPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F;
495}
496
497SCM_PROC(s_output_port_p, "output-port?", 1, 0, 0, scm_output_port_p);
1cc91f1b 498
0f2d19dd
JB
499SCM
500scm_output_port_p (x)
501 SCM x;
0f2d19dd
JB
502{
503 if (SCM_IMP (x))
504 return SCM_BOOL_F;
505 return SCM_OUTPORTP (x) ? SCM_BOOL_T : SCM_BOOL_F;
506}
507
508
509SCM_PROC(s_eof_object_p, "eof-object?", 1, 0, 0, scm_eof_object_p);
1cc91f1b 510
0f2d19dd
JB
511SCM
512scm_eof_object_p (x)
513 SCM x;
0f2d19dd
JB
514{
515 return (SCM_EOF_VAL == x) ? SCM_BOOL_T : SCM_BOOL_F;
516}
517
518SCM_PROC(s_force_output, "force-output", 0, 1, 0, scm_force_output);
1cc91f1b 519
0f2d19dd
JB
520SCM
521scm_force_output (port)
522 SCM port;
0f2d19dd
JB
523{
524 if (SCM_UNBNDP (port))
525 port = scm_cur_outp;
526 else
527 SCM_ASSERT (SCM_NIMP (port) && SCM_OPOUTPORTP (port), port, SCM_ARG1, s_force_output);
528 {
529 scm_sizet i = SCM_PTOBNUM (port);
530 SCM_SYSCALL ((scm_ptobs[i].fflush) (SCM_STREAM (port)));
531 return SCM_UNSPECIFIED;
532 }
533}
534
535
536SCM_PROC(s_read_char, "read-char", 0, 1, 0, scm_read_char);
1cc91f1b 537
0f2d19dd
JB
538SCM
539scm_read_char (port)
540 SCM port;
0f2d19dd
JB
541{
542 int c;
543 if (SCM_UNBNDP (port))
544 port = scm_cur_inp;
545 else
546 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_read_char);
547 c = scm_gen_getc (port);
548 if (EOF == c)
549 return SCM_EOF_VAL;
550 return SCM_MAKICHR (c);
551}
552
553
554SCM_PROC(s_peek_char, "peek-char", 0, 1, 0, scm_peek_char);
1cc91f1b 555
0f2d19dd
JB
556SCM
557scm_peek_char (port)
558 SCM port;
0f2d19dd
JB
559{
560 int c;
561 if (SCM_UNBNDP (port))
562 port = scm_cur_inp;
563 else
564 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG1, s_peek_char);
565 c = scm_gen_getc (port);
566 if (EOF == c)
567 return SCM_EOF_VAL;
568 scm_gen_ungetc (c, port);
569 return SCM_MAKICHR (c);
570}
571
572SCM_PROC (s_unread_char, "unread-char", 2, 0, 0, scm_unread_char);
1cc91f1b 573
0f2d19dd
JB
574SCM
575scm_unread_char (cobj, port)
576 SCM cobj;
577 SCM port;
0f2d19dd
JB
578{
579 int c;
580
581 SCM_ASSERT (SCM_ICHRP (cobj), cobj, SCM_ARG1, s_unread_char);
582
583 if (SCM_UNBNDP (port))
584 port = scm_cur_inp;
585 else
586 SCM_ASSERT (SCM_NIMP (port) && SCM_OPINPORTP (port), port, SCM_ARG2, s_unread_char);
587
588
589 c = SCM_ICHR (cobj);
590
591 scm_gen_ungetc (c, port);
592 return cobj;
593}
594
595
596
d14af9f2 597SCM_PROC (s_port_line, "port-line", 0, 1, 0, scm_port_line);
1cc91f1b 598
0f2d19dd 599SCM
d14af9f2 600scm_port_line (port)
0f2d19dd 601 SCM port;
0f2d19dd
JB
602{
603 SCM p;
604 p = ((port == SCM_UNDEFINED)
605 ? scm_cur_inp
606 : port);
607 if (!(SCM_NIMP (p) && SCM_PORTP (p)))
608 return SCM_BOOL_F;
609 else
610 return SCM_MAKINUM (SCM_LINUM (p));
611}
612
d043d8c2
MD
613SCM_PROC (s_set_port_line_x, "set-port-line!", 1, 1, 0, scm_set_port_line_x);
614
615SCM
616scm_set_port_line_x (port, line)
617 SCM port;
618 SCM line;
619{
620 if (line == SCM_UNDEFINED)
621 {
622 line = port;
623 port = scm_cur_inp;
624 }
625 else
626 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
627 port,
628 SCM_ARG1,
629 s_set_port_line_x);
630 return SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
631}
632
d14af9f2 633SCM_PROC (s_port_column, "port-column", 0, 1, 0, scm_port_column);
1cc91f1b 634
0f2d19dd 635SCM
d14af9f2 636scm_port_column (port)
0f2d19dd 637 SCM port;
0f2d19dd
JB
638{
639 SCM p;
640 p = ((port == SCM_UNDEFINED)
641 ? scm_cur_inp
642 : port);
643 if (!(SCM_NIMP (p) && SCM_PORTP (p)))
644 return SCM_BOOL_F;
645 else
646 return SCM_MAKINUM (SCM_COL (p));
647}
648
d043d8c2
MD
649SCM_PROC (s_set_port_column_x, "set-port-column!", 1, 1, 0, scm_set_port_column_x);
650
651SCM
652scm_set_port_column_x (port, column)
653 SCM port;
654 SCM column;
655{
656 if (column == SCM_UNDEFINED)
657 {
658 column = port;
659 port = scm_cur_inp;
660 }
661 else
662 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
663 port,
664 SCM_ARG1,
665 s_set_port_column_x);
666 return SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
667}
668
d14af9f2 669SCM_PROC (s_port_filename, "port-filename", 0, 1, 0, scm_port_filename);
1cc91f1b 670
0f2d19dd 671SCM
d14af9f2 672scm_port_filename (port)
0f2d19dd 673 SCM port;
0f2d19dd
JB
674{
675 SCM p;
676 p = ((port == SCM_UNDEFINED)
677 ? scm_cur_inp
678 : port);
679 if (!(SCM_NIMP (p) && SCM_PORTP (p)))
680 return SCM_BOOL_F;
681 else
682 return SCM_PTAB_ENTRY (p)->file_name;
683}
684
d14af9f2 685SCM_PROC (s_set_port_filename_x, "set-port-filename!", 1, 1, 0, scm_set_port_filename_x);
1cc91f1b 686
d14af9f2
MD
687SCM
688scm_set_port_filename_x (port, filename)
689 SCM port;
690 SCM filename;
d14af9f2
MD
691{
692 if (filename == SCM_UNDEFINED)
693 {
694 filename = port;
695 port = scm_cur_inp;
696 }
697 else
698 SCM_ASSERT (SCM_NIMP (port) && SCM_PORTP (port) && SCM_OPENP (port),
699 port,
700 SCM_ARG1,
701 s_set_port_filename_x);
702 return SCM_PTAB_ENTRY (port)->file_name = filename;
703}
704
0f2d19dd
JB
705#ifndef ttyname
706extern char * ttyname();
707#endif
708
1cc91f1b 709
0f2d19dd
JB
710void
711scm_prinport (exp, port, type)
712 SCM exp;
713 SCM port;
714 char *type;
0f2d19dd
JB
715{
716 scm_gen_puts (scm_regular_string, "#<", port);
717 if (SCM_CLOSEDP (exp))
718 scm_gen_puts (scm_regular_string, "closed: ", port);
719 else
720 {
721 if (SCM_RDNG & SCM_CAR (exp))
722 scm_gen_puts (scm_regular_string, "input: ", port);
723 if (SCM_WRTNG & SCM_CAR (exp))
724 scm_gen_puts (scm_regular_string, "output: ", port);
725 }
726 scm_gen_puts (scm_regular_string, type, port);
727 scm_gen_putc (' ', port);
728#ifndef MSDOS
729#ifndef __EMX__
730#ifndef _DCC
731#ifndef AMIGA
732#ifndef THINK_C
733 if (SCM_OPENP (exp) && scm_tc16_fport == SCM_TYP16 (exp) && isatty (fileno ((FILE *)SCM_STREAM (exp))))
734 scm_gen_puts (scm_regular_string, ttyname (fileno ((FILE *)SCM_STREAM (exp))), port);
735 else
736#endif
737#endif
738#endif
739#endif
740#endif
741 if (SCM_OPFPORTP (exp))
742 scm_intprint ((long) fileno ((FILE *)SCM_STREAM (exp)), 10, port);
743 else
744 scm_intprint (SCM_CDR (exp), 16, port);
745 scm_gen_putc ('>', port);
746}
747
1cc91f1b 748
0f2d19dd
JB
749void
750scm_ports_prehistory ()
0f2d19dd
JB
751{
752 scm_numptob = 0;
753 scm_ptobs = (scm_ptobfuns *) malloc (sizeof (scm_ptobfuns));
754
755 /* WARNING: These scm_newptob calls must be done in this order.
756 * They must agree with the port declarations in tags.h.
757 */
758 /* scm_tc16_fport = */ scm_newptob (&scm_fptob);
759 /* scm_tc16_pipe = */ scm_newptob (&scm_pipob);
760 /* scm_tc16_strport = */ scm_newptob (&scm_stptob);
761 /* scm_tc16_sfport = */ scm_newptob (&scm_sfptob);
762}
763\f
764
765\f
766/* {Void Ports}
767 */
768
769int scm_tc16_void_port = 0;
770
771static int
772print_void_port (exp, port, writing)
773 SCM exp;
774 SCM port;
775 int writing;
776{
777 scm_prinport (exp, port, "void");
778 return 1;
779}
780
781static int
782putc_void_port (c, strm)
783 int c;
784 SCM strm;
785{
786 return 0; /* vestigial return value */
787}
788
789static int
790puts_void_port (s, strm)
791 char * s;
792 SCM strm;
793{
794 return 0; /* vestigial return value */
795}
796
797static scm_sizet
798write_void_port (ptr, size, nitems, strm)
799 void * ptr;
800 int size;
801 int nitems;
802 SCM strm;
803{
804 int len;
805 len = size * nitems;
806 return len;
807}
808
1cc91f1b
JB
809
810static int flush_void_port SCM_P ((SCM strm));
811
0f2d19dd
JB
812static int
813flush_void_port (strm)
814 SCM strm;
0f2d19dd
JB
815{
816 return 0;
817}
818
1cc91f1b
JB
819
820static int getc_void_port SCM_P ((SCM strm));
821
0f2d19dd
JB
822static int
823getc_void_port (strm)
824 SCM strm;
0f2d19dd
JB
825{
826 return EOF;
827}
828
1cc91f1b
JB
829
830static int close_void_port SCM_P ((SCM strm));
831
0f2d19dd
JB
832static int
833close_void_port (strm)
834 SCM strm;
0f2d19dd
JB
835{
836 return 0; /* this is ignored by scm_close_port. */
837}
838
839
1cc91f1b
JB
840
841static int noop0 SCM_P ((SCM stream));
842
0f2d19dd
JB
843static int
844noop0 (stream)
1cc91f1b 845 SCM stream;
0f2d19dd
JB
846{
847 return 0;
848}
849
850
851static struct scm_ptobfuns void_port_ptob =
852{
853 scm_mark0,
854 noop0,
855 print_void_port,
856 0, /* equal? */
857 putc_void_port,
858 puts_void_port,
859 write_void_port,
860 flush_void_port,
861 getc_void_port,
862 close_void_port,
863};
864
865\f
866
1cc91f1b 867
0f2d19dd
JB
868SCM
869scm_void_port (mode_str)
870 char * mode_str;
0f2d19dd
JB
871{
872 int mode_bits;
873 SCM answer;
874 struct scm_port_table * pt;
875
876 SCM_NEWCELL (answer);
877 SCM_DEFER_INTS;
878 mode_bits = scm_mode_bits (mode_str);
879 pt = scm_add_to_port_table (answer);
898a256f 880 SCM_SETCAR (answer, scm_tc16_void_port | mode_bits);
0f2d19dd
JB
881 SCM_SETPTAB_ENTRY (answer, pt);
882 SCM_SETSTREAM (answer, SCM_BOOL_F);
883 SCM_ALLOW_INTS;
884 return answer;
885}
886
887
888SCM_PROC (s_sys_make_void_port, "%make-void-port", 1, 0, 0, scm_sys_make_void_port);
1cc91f1b 889
0f2d19dd
JB
890SCM
891scm_sys_make_void_port (mode)
892 SCM mode;
0f2d19dd
JB
893{
894 SCM_ASSERT (SCM_NIMP (mode) && SCM_STRINGP (mode), mode,
895 SCM_ARG1, s_sys_make_void_port);
896
897 return scm_void_port (SCM_ROCHARS (mode));
898}
899
900
901
902\f
903
1cc91f1b 904
0f2d19dd
JB
905void
906scm_init_ports ()
0f2d19dd
JB
907{
908 scm_tc16_void_port = scm_newptob (&void_port_ptob);
909#include "ports.x"
910}
911