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