Docstring of port-for-each updated to new behaviour.
[bpt/guile.git] / libguile / ports.c
1 /* Copyright (C) 1995,1996,1997,1998,1999, 2000 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
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46 /* Headers. */
47
48 #include <stdio.h>
49 #include "libguile/_scm.h"
50 #include "libguile/eval.h"
51 #include "libguile/objects.h"
52 #include "libguile/smob.h"
53 #include "libguile/chars.h"
54
55 #include "libguile/keywords.h"
56 #include "libguile/root.h"
57 #include "libguile/strings.h"
58
59 #include "libguile/validate.h"
60 #include "libguile/ports.h"
61
62 #ifdef HAVE_STRING_H
63 #include <string.h>
64 #endif
65
66 #ifdef HAVE_MALLOC_H
67 #include <malloc.h>
68 #endif
69
70 #ifdef HAVE_UNISTD_H
71 #include <unistd.h>
72 #endif
73
74 #ifdef HAVE_SYS_IOCTL_H
75 #include <sys/ioctl.h>
76 #endif
77
78 \f
79 /* The port kind table --- a dynamically resized array of port types. */
80
81
82 /* scm_ptobs scm_numptob
83 * implement a dynamicly resized array of ptob records.
84 * Indexes into this table are used when generating type
85 * tags for smobjects (if you know a tag you can get an index and conversely).
86 */
87 scm_ptob_descriptor *scm_ptobs;
88 int scm_numptob;
89
90 /* GC marker for a port with stream of SCM type. */
91 SCM
92 scm_markstream (SCM ptr)
93 {
94 int openp;
95 openp = SCM_CELL_WORD_0 (ptr) & SCM_OPN;
96 if (openp)
97 return SCM_PACK (SCM_STREAM (ptr));
98 else
99 return SCM_BOOL_F;
100 }
101
102 /*
103 * We choose to use an interface similar to the smob interface with
104 * fill_input and write as standard fields, passed to the port
105 * type constructor, and optional fields set by setters.
106 */
107
108 static void
109 flush_port_default (SCM port)
110 {
111 }
112
113 static void
114 end_input_default (SCM port, int offset)
115 {
116 }
117
118 scm_bits_t
119 scm_make_port_type (char *name,
120 int (*fill_input) (SCM port),
121 void (*write) (SCM port, const void *data, size_t size))
122 {
123 char *tmp;
124 if (255 <= scm_numptob)
125 goto ptoberr;
126 SCM_DEFER_INTS;
127 SCM_SYSCALL (tmp = (char *) realloc ((char *) scm_ptobs,
128 (1 + scm_numptob)
129 * sizeof (scm_ptob_descriptor)));
130 if (tmp)
131 {
132 scm_ptobs = (scm_ptob_descriptor *) tmp;
133
134 scm_ptobs[scm_numptob].name = name;
135 scm_ptobs[scm_numptob].mark = 0;
136 scm_ptobs[scm_numptob].free = scm_free0;
137 scm_ptobs[scm_numptob].print = scm_port_print;
138 scm_ptobs[scm_numptob].equalp = 0;
139 scm_ptobs[scm_numptob].close = 0;
140
141 scm_ptobs[scm_numptob].write = write;
142 scm_ptobs[scm_numptob].flush = flush_port_default;
143
144 scm_ptobs[scm_numptob].end_input = end_input_default;
145 scm_ptobs[scm_numptob].fill_input = fill_input;
146 scm_ptobs[scm_numptob].input_waiting = 0;
147
148 scm_ptobs[scm_numptob].seek = 0;
149 scm_ptobs[scm_numptob].truncate = 0;
150
151 scm_numptob++;
152 }
153 SCM_ALLOW_INTS;
154 if (!tmp)
155 {
156 ptoberr:
157 scm_memory_error ("scm_make_port_type");
158 }
159 /* Make a class object if Goops is present */
160 if (scm_port_class)
161 scm_make_port_classes (scm_numptob - 1, SCM_PTOBNAME (scm_numptob - 1));
162 return scm_tc7_port + (scm_numptob - 1) * 256;
163 }
164
165 void
166 scm_set_port_mark (long tc, SCM (*mark) (SCM))
167 {
168 scm_ptobs[SCM_TC2PTOBNUM (tc)].mark = mark;
169 }
170
171 void
172 scm_set_port_free (long tc, scm_sizet (*free) (SCM))
173 {
174 scm_ptobs[SCM_TC2PTOBNUM (tc)].free = free;
175 }
176
177 void
178 scm_set_port_print (long tc, int (*print) (SCM exp, SCM port,
179 scm_print_state *pstate))
180 {
181 scm_ptobs[SCM_TC2PTOBNUM (tc)].print = print;
182 }
183
184 void
185 scm_set_port_equalp (long tc, SCM (*equalp) (SCM, SCM))
186 {
187 scm_ptobs[SCM_TC2PTOBNUM (tc)].equalp = equalp;
188 }
189
190 void
191 scm_set_port_flush (long tc, void (*flush) (SCM port))
192 {
193 scm_ptobs[SCM_TC2PTOBNUM (tc)].flush = flush;
194 }
195
196 void
197 scm_set_port_end_input (long tc, void (*end_input) (SCM port, int offset))
198 {
199 scm_ptobs[SCM_TC2PTOBNUM (tc)].end_input = end_input;
200 }
201
202 void
203 scm_set_port_close (long tc, int (*close) (SCM))
204 {
205 scm_ptobs[SCM_TC2PTOBNUM (tc)].close = close;
206 }
207
208 void
209 scm_set_port_seek (long tc, off_t (*seek) (SCM port,
210 off_t OFFSET,
211 int WHENCE))
212 {
213 scm_ptobs[SCM_TC2PTOBNUM (tc)].seek = seek;
214 }
215
216 void
217 scm_set_port_truncate (long tc, void (*truncate) (SCM port, off_t length))
218 {
219 scm_ptobs[SCM_TC2PTOBNUM (tc)].truncate = truncate;
220 }
221
222 void
223 scm_set_port_input_waiting (long tc, int (*input_waiting) (SCM))
224 {
225 scm_ptobs[SCM_TC2PTOBNUM (tc)].input_waiting = input_waiting;
226 }
227
228 \f
229
230 SCM_DEFINE (scm_char_ready_p, "char-ready?", 0, 1, 0,
231 (SCM port),
232 "Returns @code{#t} if a character is ready on input @var{port} and\n"
233 "returns @code{#f} otherwise. If @code{char-ready?} returns @code{#t}\n"
234 "then the next @code{read-char} operation on @var{port} is\n"
235 "guaranteed not to hang. If @var{port} is a file port at end of\n"
236 "file then @code{char-ready?} returns @code{#t}.\n"
237 "@footnote{@code{char-ready?} exists to make it possible for a\n"
238 "program to accept characters from interactive ports without getting\n"
239 "stuck waiting for input. Any input editors associated with such ports\n"
240 "must make sure that characters whose existence has been asserted by\n"
241 "@code{char-ready?} cannot be rubbed out. If @code{char-ready?} were to\n"
242 "return @code{#f} at end of file, a port at end of file would be\n"
243 "indistinguishable from an interactive port that has no ready\n"
244 "characters.}")
245 #define FUNC_NAME s_scm_char_ready_p
246 {
247 scm_port *pt;
248
249 if (SCM_UNBNDP (port))
250 port = scm_cur_inp;
251 else
252 SCM_VALIDATE_OPINPORT (1,port);
253
254 pt = SCM_PTAB_ENTRY (port);
255
256 /* if the current read buffer is filled, or the
257 last pushed-back char has been read and the saved buffer is
258 filled, result is true. */
259 if (pt->read_pos < pt->read_end
260 || (pt->read_buf == pt->putback_buf
261 && pt->saved_read_pos < pt->saved_read_end))
262 return SCM_BOOL_T;
263 else
264 {
265 scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
266
267 if (ptob->input_waiting)
268 return SCM_BOOL(ptob->input_waiting (port));
269 else
270 return SCM_BOOL_T;
271 }
272 }
273 #undef FUNC_NAME
274
275 /* move up to read_len chars from port's putback and/or read buffers
276 into memory starting at dest. returns the number of chars moved. */
277 size_t scm_take_from_input_buffers (SCM port, char *dest, size_t read_len)
278 {
279 scm_port *pt = SCM_PTAB_ENTRY (port);
280 size_t chars_read = 0;
281 size_t from_buf = min (pt->read_end - pt->read_pos, read_len);
282
283 if (from_buf > 0)
284 {
285 memcpy (dest, pt->read_pos, from_buf);
286 pt->read_pos += from_buf;
287 chars_read += from_buf;
288 read_len -= from_buf;
289 dest += from_buf;
290 }
291
292 /* if putback was active, try the real input buffer too. */
293 if (pt->read_buf == pt->putback_buf)
294 {
295 from_buf = min (pt->saved_read_end - pt->saved_read_pos, read_len);
296 if (from_buf > 0)
297 {
298 memcpy (dest, pt->saved_read_pos, from_buf);
299 pt->saved_read_pos += from_buf;
300 chars_read += from_buf;
301 }
302 }
303 return chars_read;
304 }
305
306 /* Clear a port's read buffers, returning the contents. */
307 SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
308 (SCM port),
309 "Drains @var{PORT}'s read buffers (including any pushed-back characters)\n"
310 "and returns the contents as a single string.")
311 #define FUNC_NAME s_scm_drain_input
312 {
313 SCM result;
314 scm_port *pt = SCM_PTAB_ENTRY (port);
315 int count;
316
317 SCM_VALIDATE_OPINPORT (1,port);
318
319 count = pt->read_end - pt->read_pos;
320 if (pt->read_buf == pt->putback_buf)
321 count += pt->saved_read_end - pt->saved_read_pos;
322
323 result = scm_makstr (count, 0);
324 scm_take_from_input_buffers (port, SCM_STRING_CHARS (result), count);
325
326 return result;
327 }
328 #undef FUNC_NAME
329
330 \f
331 /* Standard ports --- current input, output, error, and more(!). */
332
333 SCM_DEFINE (scm_current_input_port, "current-input-port", 0, 0, 0,
334 (),
335 "Returns the current input port. This is the default port used by many\n"
336 "input procedures. Initially, @code{current-input-port} returns the\n"
337 "value of @code{???}.")
338 #define FUNC_NAME s_scm_current_input_port
339 {
340 return scm_cur_inp;
341 }
342 #undef FUNC_NAME
343
344 SCM_DEFINE (scm_current_output_port, "current-output-port", 0, 0, 0,
345 (),
346 "Returns the current output port. This is the default port used by many\n"
347 "output procedures. Initially, @code{current-output-port} returns the\n"
348 "value of @code{???}.")
349 #define FUNC_NAME s_scm_current_output_port
350 {
351 return scm_cur_outp;
352 }
353 #undef FUNC_NAME
354
355 SCM_DEFINE (scm_current_error_port, "current-error-port", 0, 0, 0,
356 (),
357 "Return the port to which errors and warnings should be sent (the\n"
358 "@dfn{standard error} in Unix and C terminology).")
359 #define FUNC_NAME s_scm_current_error_port
360 {
361 return scm_cur_errp;
362 }
363 #undef FUNC_NAME
364
365 SCM_DEFINE (scm_current_load_port, "current-load-port", 0, 0, 0,
366 (),
367 "Return the current-load-port.\n"
368 "The load port is used internally by `primitive-load'.")
369 #define FUNC_NAME s_scm_current_load_port
370 {
371 return scm_cur_loadp;
372 }
373 #undef FUNC_NAME
374
375 SCM_DEFINE (scm_set_current_input_port, "set-current-input-port", 1, 0, 0,
376 (SCM port),
377 "@deffnx primitive set-current-output-port port\n"
378 "@deffnx primitive set-current-error-port port\n"
379 "Change the ports returned by @code{current-input-port},\n"
380 "@code{current-output-port} and @code{current-error-port}, respectively,\n"
381 "so that they use the supplied @var{port} for input or output.")
382 #define FUNC_NAME s_scm_set_current_input_port
383 {
384 SCM oinp = scm_cur_inp;
385 SCM_VALIDATE_OPINPORT (1,port);
386 scm_cur_inp = port;
387 return oinp;
388 }
389 #undef FUNC_NAME
390
391
392 SCM_DEFINE (scm_set_current_output_port, "set-current-output-port", 1, 0, 0,
393 (SCM port),
394 "Set the current default output port to PORT.")
395 #define FUNC_NAME s_scm_set_current_output_port
396 {
397 SCM ooutp = scm_cur_outp;
398 port = SCM_COERCE_OUTPORT (port);
399 SCM_VALIDATE_OPOUTPORT (1,port);
400 scm_cur_outp = port;
401 return ooutp;
402 }
403 #undef FUNC_NAME
404
405
406 SCM_DEFINE (scm_set_current_error_port, "set-current-error-port", 1, 0, 0,
407 (SCM port),
408 "Set the current default error port to PORT.")
409 #define FUNC_NAME s_scm_set_current_error_port
410 {
411 SCM oerrp = scm_cur_errp;
412 port = SCM_COERCE_OUTPORT (port);
413 SCM_VALIDATE_OPOUTPORT (1,port);
414 scm_cur_errp = port;
415 return oerrp;
416 }
417 #undef FUNC_NAME
418
419 \f
420 /* The port table --- an array of pointers to ports. */
421
422 scm_port **scm_port_table;
423
424 int scm_port_table_size = 0; /* Number of ports in scm_port_table. */
425 int scm_port_table_room = 20; /* Size of the array. */
426
427 /* Add a port to the table. */
428
429 scm_port *
430 scm_add_to_port_table (SCM port)
431 {
432 scm_port *entry;
433
434 if (scm_port_table_size == scm_port_table_room)
435 {
436 void *newt = realloc ((char *) scm_port_table,
437 (scm_sizet) (sizeof (scm_port *)
438 * scm_port_table_room * 2));
439 if (newt == NULL)
440 scm_memory_error ("scm_add_to_port_table");
441 scm_port_table = (scm_port **) newt;
442 scm_port_table_room *= 2;
443 }
444 entry = (scm_port *) malloc (sizeof (scm_port));
445 if (entry == NULL)
446 scm_memory_error ("scm_add_to_port_table");
447
448 entry->port = port;
449 entry->entry = scm_port_table_size;
450 entry->revealed = 0;
451 entry->stream = 0;
452 entry->file_name = SCM_BOOL_F;
453 entry->line_number = 0;
454 entry->column_number = 0;
455 entry->putback_buf = 0;
456 entry->putback_buf_size = 0;
457 entry->rw_active = SCM_PORT_NEITHER;
458 entry->rw_random = 0;
459
460 scm_port_table[scm_port_table_size] = entry;
461 scm_port_table_size++;
462
463 return entry;
464 }
465
466 /* Remove a port from the table and destroy it. */
467
468 void
469 scm_remove_from_port_table (SCM port)
470 {
471 scm_port *p = SCM_PTAB_ENTRY (port);
472 int i = p->entry;
473
474 if (i >= scm_port_table_size)
475 scm_wta (port, "Port not in table", "scm_remove_from_port_table");
476 if (p->putback_buf)
477 free (p->putback_buf);
478 free (p);
479 /* Since we have just freed slot i we can shrink the table by moving
480 the last entry to that slot... */
481 if (i < scm_port_table_size - 1)
482 {
483 scm_port_table[i] = scm_port_table[scm_port_table_size - 1];
484 scm_port_table[i]->entry = i;
485 }
486 SCM_SETPTAB_ENTRY (port, 0);
487 scm_port_table_size--;
488 }
489
490 #ifdef GUILE_DEBUG
491 /* Functions for debugging. */
492
493 SCM_DEFINE (scm_pt_size, "pt-size", 0, 0, 0,
494 (),
495 "Returns the number of ports in the port table.\n"
496 "`pt-size' is only included in GUILE_DEBUG builds.")
497 #define FUNC_NAME s_scm_pt_size
498 {
499 return SCM_MAKINUM (scm_port_table_size);
500 }
501 #undef FUNC_NAME
502
503 SCM_DEFINE (scm_pt_member, "pt-member", 1, 0, 0,
504 (SCM index),
505 "Returns the port at INDEX in the port table.\n"
506 "`pt-member' is only included in GUILE_DEBUG builds.")
507 #define FUNC_NAME s_scm_pt_member
508 {
509 int i;
510 SCM_VALIDATE_INUM_COPY (1,index,i);
511 if (i < 0 || i >= scm_port_table_size)
512 return SCM_BOOL_F;
513 else
514 return scm_port_table[i]->port;
515 }
516 #undef FUNC_NAME
517 #endif
518
519 void
520 scm_port_non_buffer (scm_port *pt)
521 {
522 pt->read_pos = pt->read_buf = pt->read_end = &pt->shortbuf;
523 pt->write_buf = pt->write_pos = &pt->shortbuf;
524 pt->read_buf_size = pt->write_buf_size = 1;
525 pt->write_end = pt->write_buf + pt->write_buf_size;
526 }
527
528 \f
529 /* Revealed counts --- an oddity inherited from SCSH. */
530
531 /* Find a port in the table and return its revealed count.
532 Also used by the garbage collector.
533 */
534
535 int
536 scm_revealed_count (SCM port)
537 {
538 return SCM_REVEALED(port);
539 }
540
541
542
543 /* Return the revealed count for a port. */
544
545 SCM_DEFINE (scm_port_revealed, "port-revealed", 1, 0, 0,
546 (SCM port),
547 "Returns the revealed count for @var{port}.")
548 #define FUNC_NAME s_scm_port_revealed
549 {
550 port = SCM_COERCE_OUTPORT (port);
551 SCM_VALIDATE_PORT (1,port);
552 return SCM_MAKINUM (scm_revealed_count (port));
553 }
554 #undef FUNC_NAME
555
556 /* Set the revealed count for a port. */
557 SCM_DEFINE (scm_set_port_revealed_x, "set-port-revealed!", 2, 0, 0,
558 (SCM port, SCM rcount),
559 "Sets the revealed count for a port to a given value.\n"
560 "The return value is unspecified.")
561 #define FUNC_NAME s_scm_set_port_revealed_x
562 {
563 port = SCM_COERCE_OUTPORT (port);
564 SCM_VALIDATE_PORT (1,port);
565 SCM_VALIDATE_INUM (2,rcount);
566 SCM_REVEALED (port) = SCM_INUM (rcount);
567 return SCM_UNSPECIFIED;
568 }
569 #undef FUNC_NAME
570
571
572 \f
573 /* Retrieving a port's mode. */
574
575 /* Return the flags that characterize a port based on the mode
576 * string used to open a file for that port.
577 *
578 * See PORT FLAGS in scm.h
579 */
580
581 long
582 scm_mode_bits (char *modes)
583 {
584 return (SCM_OPN
585 | (strchr (modes, 'r') || strchr (modes, '+') ? SCM_RDNG : 0)
586 | ( strchr (modes, 'w')
587 || strchr (modes, 'a')
588 || strchr (modes, '+') ? SCM_WRTNG : 0)
589 | (strchr (modes, '0') ? SCM_BUF0 : 0)
590 | (strchr (modes, 'l') ? SCM_BUFLINE : 0));
591 }
592
593
594 /* Return the mode flags from an open port.
595 * Some modes such as "append" are only used when opening
596 * a file and are not returned here. */
597
598 SCM_DEFINE (scm_port_mode, "port-mode", 1, 0, 0,
599 (SCM port),
600 "Returns the port modes associated with the open port @var{port}. These\n"
601 "will not necessarily be identical to the modes used when the port was\n"
602 "opened, since modes such as \"append\" which are used only during\n"
603 "port creation are not retained.")
604 #define FUNC_NAME s_scm_port_mode
605 {
606 char modes[3];
607 modes[0] = '\0';
608
609 port = SCM_COERCE_OUTPORT (port);
610 SCM_VALIDATE_OPPORT (1,port);
611 if (SCM_CELL_WORD_0 (port) & SCM_RDNG) {
612 if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
613 strcpy (modes, "r+");
614 else
615 strcpy (modes, "r");
616 }
617 else if (SCM_CELL_WORD_0 (port) & SCM_WRTNG)
618 strcpy (modes, "w");
619 if (SCM_CELL_WORD_0 (port) & SCM_BUF0)
620 strcat (modes, "0");
621 return scm_makfromstr (modes, strlen (modes), 0);
622 }
623 #undef FUNC_NAME
624
625
626 \f
627 /* Closing ports. */
628
629 /* scm_close_port
630 * Call the close operation on a port object.
631 * see also scm_close.
632 */
633 SCM_DEFINE (scm_close_port, "close-port", 1, 0, 0,
634 (SCM port),
635 "Close the specified port object. Returns @code{#t} if it successfully\n"
636 "closes a port or @code{#f} if it was already\n"
637 "closed. An exception may be raised if an error occurs, for example\n"
638 "when flushing buffered output.\n"
639 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
640 "which can close file descriptors.")
641 #define FUNC_NAME s_scm_close_port
642 {
643 scm_sizet i;
644 int rv;
645
646 port = SCM_COERCE_OUTPORT (port);
647
648 SCM_VALIDATE_PORT (1, port);
649 if (SCM_CLOSEDP (port))
650 return SCM_BOOL_F;
651 i = SCM_PTOBNUM (port);
652 if (scm_ptobs[i].close)
653 rv = (scm_ptobs[i].close) (port);
654 else
655 rv = 0;
656 scm_remove_from_port_table (port);
657 SCM_SETAND_CAR (port, ~SCM_OPN);
658 return SCM_NEGATE_BOOL (rv < 0);
659 }
660 #undef FUNC_NAME
661
662 SCM_DEFINE (scm_close_input_port, "close-input-port", 1, 0, 0,
663 (SCM port),
664 "Close the specified input port object. The routine has no effect if\n"
665 "the file has already been closed. An exception may be raised if an\n"
666 "error occurs. The value returned is unspecified.\n\n"
667 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
668 "which can close file descriptors.")
669 #define FUNC_NAME s_scm_close_input_port
670 {
671 SCM_VALIDATE_INPUT_PORT (1, port);
672 scm_close_port (port);
673 return SCM_UNSPECIFIED;
674 }
675 #undef FUNC_NAME
676
677 SCM_DEFINE (scm_close_output_port, "close-output-port", 1, 0, 0,
678 (SCM port),
679 "Close the specified output port object. The routine has no effect if\n"
680 "the file has already been closed. An exception may be raised if an\n"
681 "error occurs. The value returned is unspecified.\n\n"
682 "See also @ref{Ports and File Descriptors, close}, for a procedure\n"
683 "which can close file descriptors.")
684 #define FUNC_NAME s_scm_close_output_port
685 {
686 port = SCM_COERCE_OUTPORT (port);
687 SCM_VALIDATE_OUTPUT_PORT (1, port);
688 scm_close_port (port);
689 return SCM_UNSPECIFIED;
690 }
691 #undef FUNC_NAME
692
693 SCM_DEFINE (scm_port_for_each, "port-for-each", 1, 0, 0,
694 (SCM proc),
695 "Apply @var{proc} to each port in the Guile port table\n"
696 "in turn. The return value is unspecified. More specifically,\n"
697 "@var{proc} is applied exactly once to every port that exists\n"
698 "in the system at the time @var{port-for-each} is invoked.\n"
699 "Changes to the port table while @var{port-for-each} is running\n"
700 "have no effect as far as @var{port-for-each} is concerned.\n")
701 #define FUNC_NAME s_scm_port_for_each
702 {
703 int i;
704 SCM ports;
705
706 SCM_VALIDATE_PROC (1, proc);
707
708 /* when pre-emptive multithreading is supported, access to the port
709 table will need to be controlled by a mutex. */
710
711 /* Even without pre-emptive multithreading, running arbitrary code
712 while scanning the port table is unsafe because the port table
713 can change arbitrarily (from a GC, for example). So we build a
714 list in advance while blocking the GC. -mvo */
715
716 SCM_DEFER_INTS;
717 scm_block_gc++;
718 ports = SCM_EOL;
719 for (i = 0; i < scm_port_table_size; i++)
720 ports = scm_cons (scm_port_table[i]->port, ports);
721 scm_block_gc--;
722 SCM_ALLOW_INTS;
723
724 while (ports != SCM_EOL)
725 {
726 scm_apply (proc, scm_cons (SCM_CAR (ports), SCM_EOL), SCM_EOL);
727 ports = SCM_CDR (ports);
728 }
729
730 return SCM_UNSPECIFIED;
731 }
732 #undef FUNC_NAME
733
734 #if (SCM_DEBUG_DEPRECATED == 0)
735
736 SCM_DEFINE (scm_close_all_ports_except, "close-all-ports-except", 0, 0, 1,
737 (SCM ports),
738 "[DEPRECATED] Close all open file ports used by the interpreter\n"
739 "except for those supplied as arguments. This procedure\n"
740 "was intended to be used before an exec call to close file descriptors\n"
741 "which are not needed in the new process. However it has the\n"
742 "undesirable side-effect of flushing buffes, so it's deprecated.\n"
743 "Use port-for-each instead.")
744 #define FUNC_NAME s_scm_close_all_ports_except
745 {
746 int i = 0;
747 SCM_VALIDATE_REST_ARGUMENT (ports);
748 while (i < scm_port_table_size)
749 {
750 SCM thisport = scm_port_table[i]->port;
751 int found = 0;
752 SCM ports_ptr = ports;
753
754 while (SCM_NNULLP (ports_ptr))
755 {
756 SCM port = SCM_COERCE_OUTPORT (SCM_CAR (ports_ptr));
757 if (i == 0)
758 SCM_VALIDATE_OPPORT (SCM_ARG1,port);
759 if (SCM_EQ_P (port, thisport))
760 found = 1;
761 ports_ptr = SCM_CDR (ports_ptr);
762 }
763 if (found)
764 i++;
765 else
766 /* i is not to be incremented here. */
767 scm_close_port (thisport);
768 }
769 return SCM_UNSPECIFIED;
770 }
771 #undef FUNC_NAME
772
773 #endif
774
775 \f
776 /* Utter miscellany. Gosh, we should clean this up some time. */
777
778 SCM_DEFINE (scm_input_port_p, "input-port?", 1, 0, 0,
779 (SCM x),
780 "Returns @code{#t} if @var{x} is an input port, otherwise returns\n"
781 "@code{#f}. Any object satisfying this predicate also satisfies\n"
782 "@code{port?}.")
783 #define FUNC_NAME s_scm_input_port_p
784 {
785 if (SCM_IMP (x))
786 return SCM_BOOL_F;
787 return SCM_BOOL(SCM_INPUT_PORT_P (x));
788 }
789 #undef FUNC_NAME
790
791 SCM_DEFINE (scm_output_port_p, "output-port?", 1, 0, 0,
792 (SCM x),
793 "Returns @code{#t} if @var{x} is an output port, otherwise returns\n"
794 "@code{#f}. Any object satisfying this predicate also satisfies\n"
795 "@code{port?}.")
796 #define FUNC_NAME s_scm_output_port_p
797 {
798 if (SCM_IMP (x))
799 return SCM_BOOL_F;
800 if (SCM_PORT_WITH_PS_P (x))
801 x = SCM_PORT_WITH_PS_PORT (x);
802 return SCM_BOOL(SCM_OUTPUT_PORT_P (x));
803 }
804 #undef FUNC_NAME
805
806 SCM_DEFINE (scm_port_p, "port?", 1, 0, 0,
807 (SCM x),
808 "Returns a boolean indicating whether @var{x} is a port.\n"
809 "Equivalent to @code{(or (input-port? X) (output-port? X))}.")
810 #define FUNC_NAME s_scm_port_p
811 {
812 return SCM_BOOL (SCM_PORTP (x));
813 }
814 #undef FUNC_NAME
815
816 SCM_DEFINE (scm_port_closed_p, "port-closed?", 1, 0, 0,
817 (SCM port),
818 "Returns @code{#t} if @var{port} is closed or @code{#f} if it is open.")
819 #define FUNC_NAME s_scm_port_closed_p
820 {
821 SCM_VALIDATE_PORT (1,port);
822 return SCM_NEGATE_BOOL(SCM_OPPORTP (port));
823 }
824 #undef FUNC_NAME
825
826 SCM_DEFINE (scm_eof_object_p, "eof-object?", 1, 0, 0,
827 (SCM x),
828 "Returns @code{#t} if @var{x} is an end-of-file object; otherwise\n"
829 "returns @code{#f}.")
830 #define FUNC_NAME s_scm_eof_object_p
831 {
832 return SCM_BOOL(SCM_EOF_OBJECT_P (x));
833 }
834 #undef FUNC_NAME
835
836 SCM_DEFINE (scm_force_output, "force-output", 0, 1, 0,
837 (SCM port),
838 "Flush the specified output port, or the current output port if @var{port}\n"
839 "is omitted. The current output buffer contents are passed to the \n"
840 "underlying port implementation (e.g., in the case of fports, the\n"
841 "data will be written to the file and the output buffer will be cleared.)\n"
842 "It has no effect on an unbuffered port.\n\n"
843 "The return value is unspecified.")
844 #define FUNC_NAME s_scm_force_output
845 {
846 if (SCM_UNBNDP (port))
847 port = scm_cur_outp;
848 else
849 {
850 port = SCM_COERCE_OUTPORT (port);
851 SCM_VALIDATE_OPOUTPORT (1,port);
852 }
853 scm_flush (port);
854 return SCM_UNSPECIFIED;
855 }
856 #undef FUNC_NAME
857
858 SCM_DEFINE (scm_flush_all_ports, "flush-all-ports", 0, 0, 0,
859 (),
860 "Equivalent to calling @code{force-output} on\n"
861 "all open output ports. The return value is unspecified.")
862 #define FUNC_NAME s_scm_flush_all_ports
863 {
864 int i;
865
866 for (i = 0; i < scm_port_table_size; i++)
867 {
868 if (SCM_OPOUTPORTP (scm_port_table[i]->port))
869 scm_flush (scm_port_table[i]->port);
870 }
871 return SCM_UNSPECIFIED;
872 }
873 #undef FUNC_NAME
874
875 SCM_DEFINE (scm_read_char, "read-char", 0, 1, 0,
876 (SCM port),
877 "Returns the next character available from @var{port}, updating\n"
878 "@var{port} to point to the following character. If no more\n"
879 "characters are available, an end-of-file object is returned.")
880 #define FUNC_NAME s_scm_read_char
881 {
882 int c;
883 if (SCM_UNBNDP (port))
884 port = scm_cur_inp;
885 SCM_VALIDATE_OPINPORT (1,port);
886 c = scm_getc (port);
887 if (EOF == c)
888 return SCM_EOF_VAL;
889 return SCM_MAKE_CHAR (c);
890 }
891 #undef FUNC_NAME
892
893 /* this should only be called when the read buffer is empty. it
894 tries to refill the read buffer. it returns the first char from
895 the port, which is either EOF or *(pt->read_pos). */
896 int
897 scm_fill_input (SCM port)
898 {
899 scm_port *pt = SCM_PTAB_ENTRY (port);
900
901 if (pt->read_buf == pt->putback_buf)
902 {
903 /* finished reading put-back chars. */
904 pt->read_buf = pt->saved_read_buf;
905 pt->read_pos = pt->saved_read_pos;
906 pt->read_end = pt->saved_read_end;
907 pt->read_buf_size = pt->saved_read_buf_size;
908 if (pt->read_pos < pt->read_end)
909 return *(pt->read_pos);
910 }
911 return scm_ptobs[SCM_PTOBNUM (port)].fill_input (port);
912 }
913
914 int
915 scm_getc (SCM port)
916 {
917 int c;
918 scm_port *pt = SCM_PTAB_ENTRY (port);
919
920 if (pt->rw_active == SCM_PORT_WRITE)
921 {
922 /* may be marginally faster than calling scm_flush. */
923 scm_ptobs[SCM_PTOBNUM (port)].flush (port);
924 }
925
926 if (pt->rw_random)
927 pt->rw_active = SCM_PORT_READ;
928
929 if (pt->read_pos >= pt->read_end)
930 {
931 if (scm_fill_input (port) == EOF)
932 return EOF;
933 }
934
935 c = *(pt->read_pos++);
936
937 if (c == '\n')
938 {
939 SCM_INCLINE (port);
940 }
941 else if (c == '\t')
942 {
943 SCM_TABCOL (port);
944 }
945 else
946 {
947 SCM_INCCOL (port);
948 }
949
950 return c;
951 }
952
953 void
954 scm_putc (char c, SCM port)
955 {
956 scm_lfwrite (&c, 1, port);
957 }
958
959 void
960 scm_puts (const char *s, SCM port)
961 {
962 scm_lfwrite (s, strlen (s), port);
963 }
964
965 void
966 scm_lfwrite (const char *ptr, scm_sizet size, SCM port)
967 {
968 scm_port *pt = SCM_PTAB_ENTRY (port);
969 scm_ptob_descriptor *ptob = &scm_ptobs[SCM_PTOBNUM (port)];
970
971 if (pt->rw_active == SCM_PORT_READ)
972 scm_end_input (port);
973
974 ptob->write (port, ptr, size);
975
976 if (pt->rw_random)
977 pt->rw_active = SCM_PORT_WRITE;
978 }
979
980
981 void
982 scm_flush (SCM port)
983 {
984 scm_sizet i = SCM_PTOBNUM (port);
985 (scm_ptobs[i].flush) (port);
986 }
987
988 void
989 scm_end_input (SCM port)
990 {
991 int offset;
992 scm_port *pt = SCM_PTAB_ENTRY (port);
993
994 if (pt->read_buf == pt->putback_buf)
995 {
996 offset = pt->read_end - pt->read_pos;
997 pt->read_buf = pt->saved_read_buf;
998 pt->read_pos = pt->saved_read_pos;
999 pt->read_end = pt->saved_read_end;
1000 pt->read_buf_size = pt->saved_read_buf_size;
1001 }
1002 else
1003 offset = 0;
1004
1005 scm_ptobs[SCM_PTOBNUM (port)].end_input (port, offset);
1006 }
1007
1008 \f
1009
1010
1011 void
1012 scm_ungetc (int c, SCM port)
1013 {
1014 scm_port *pt = SCM_PTAB_ENTRY (port);
1015
1016 if (pt->read_buf == pt->putback_buf)
1017 /* already using the put-back buffer. */
1018 {
1019 /* enlarge putback_buf if necessary. */
1020 if (pt->read_end == pt->read_buf + pt->read_buf_size
1021 && pt->read_buf == pt->read_pos)
1022 {
1023 int new_size = pt->read_buf_size * 2;
1024 unsigned char *tmp =
1025 (unsigned char *) realloc (pt->putback_buf, new_size);
1026
1027 if (tmp == NULL)
1028 scm_memory_error ("scm_ungetc");
1029 pt->read_pos = pt->read_buf = pt->putback_buf = tmp;
1030 pt->read_end = pt->read_buf + pt->read_buf_size;
1031 pt->read_buf_size = pt->putback_buf_size = new_size;
1032 }
1033
1034 /* shift any existing bytes to buffer + 1. */
1035 if (pt->read_pos == pt->read_end)
1036 pt->read_end = pt->read_buf + 1;
1037 else if (pt->read_pos != pt->read_buf + 1)
1038 {
1039 int count = pt->read_end - pt->read_pos;
1040
1041 memmove (pt->read_buf + 1, pt->read_pos, count);
1042 pt->read_end = pt->read_buf + 1 + count;
1043 }
1044
1045 pt->read_pos = pt->read_buf;
1046 }
1047 else
1048 /* switch to the put-back buffer. */
1049 {
1050 if (pt->putback_buf == NULL)
1051 {
1052 pt->putback_buf
1053 = (unsigned char *) malloc (SCM_INITIAL_PUTBACK_BUF_SIZE);
1054 if (pt->putback_buf == NULL)
1055 scm_memory_error ("scm_ungetc");
1056 pt->putback_buf_size = SCM_INITIAL_PUTBACK_BUF_SIZE;
1057 }
1058
1059 pt->saved_read_buf = pt->read_buf;
1060 pt->saved_read_pos = pt->read_pos;
1061 pt->saved_read_end = pt->read_end;
1062 pt->saved_read_buf_size = pt->read_buf_size;
1063
1064 pt->read_pos = pt->read_buf = pt->putback_buf;
1065 pt->read_end = pt->read_buf + 1;
1066 pt->read_buf_size = pt->putback_buf_size;
1067 }
1068
1069 *pt->read_buf = c;
1070
1071 if (pt->rw_random)
1072 pt->rw_active = SCM_PORT_READ;
1073
1074 if (c == '\n')
1075 {
1076 /* What should col be in this case?
1077 * We'll leave it at -1.
1078 */
1079 SCM_LINUM (port) -= 1;
1080 }
1081 else
1082 SCM_COL(port) -= 1;
1083 }
1084
1085
1086 void
1087 scm_ungets (const char *s, int n, SCM port)
1088 {
1089 /* This is simple minded and inefficient, but unreading strings is
1090 * probably not a common operation, and remember that line and
1091 * column numbers have to be handled...
1092 *
1093 * Please feel free to write an optimized version!
1094 */
1095 while (n--)
1096 scm_ungetc (s[n], port);
1097 }
1098
1099
1100 SCM_DEFINE (scm_peek_char, "peek-char", 0, 1, 0,
1101 (SCM port),
1102 "Returns the next character available from @var{port},\n"
1103 "@emph{without} updating @var{port} to point to the following\n"
1104 "character. If no more characters are available, an end-of-file object\n"
1105 "is returned.@footnote{The value returned by a call to @code{peek-char}\n"
1106 "is the same as the value that would have been returned by a call to\n"
1107 "@code{read-char} on the same port. The only difference is that the very\n"
1108 "next call to @code{read-char} or @code{peek-char} on that\n"
1109 "@var{port} will return the value returned by the preceding call to\n"
1110 "@code{peek-char}. In particular, a call to @code{peek-char} on an\n"
1111 "interactive port will hang waiting for input whenever a call to\n"
1112 "@code{read-char} would have hung.}")
1113 #define FUNC_NAME s_scm_peek_char
1114 {
1115 int c;
1116 if (SCM_UNBNDP (port))
1117 port = scm_cur_inp;
1118 else
1119 SCM_VALIDATE_OPINPORT (1,port);
1120 c = scm_getc (port);
1121 if (EOF == c)
1122 return SCM_EOF_VAL;
1123 scm_ungetc (c, port);
1124 return SCM_MAKE_CHAR (c);
1125 }
1126 #undef FUNC_NAME
1127
1128 SCM_DEFINE (scm_unread_char, "unread-char", 1, 1, 0,
1129 (SCM cobj, SCM port),
1130 "Place @var{char} in @var{port} so that it will be read by the\n"
1131 "next read operation. If called multiple times, the unread characters\n"
1132 "will be read again in last-in first-out order. If @var{port} is\n"
1133 "not supplied, the current input port is used.")
1134 #define FUNC_NAME s_scm_unread_char
1135 {
1136 int c;
1137
1138 SCM_VALIDATE_CHAR (1,cobj);
1139 if (SCM_UNBNDP (port))
1140 port = scm_cur_inp;
1141 else
1142 SCM_VALIDATE_OPINPORT (2,port);
1143
1144 c = SCM_CHAR (cobj);
1145
1146 scm_ungetc (c, port);
1147 return cobj;
1148 }
1149 #undef FUNC_NAME
1150
1151 SCM_DEFINE (scm_unread_string, "unread-string", 2, 0, 0,
1152 (SCM str, SCM port),
1153 "Place the string @var{str} in @var{port} so that its characters will be\n"
1154 "read in subsequent read operations. If called multiple times, the\n"
1155 "unread characters will be read again in last-in first-out order. If\n"
1156 "@var{port} is not supplied, the current-input-port is used.")
1157 #define FUNC_NAME s_scm_unread_string
1158 {
1159 SCM_VALIDATE_STRING (1,str);
1160 if (SCM_UNBNDP (port))
1161 port = scm_cur_inp;
1162 else
1163 SCM_VALIDATE_OPINPORT (2,port);
1164
1165 scm_ungets (SCM_STRING_CHARS (str), SCM_STRING_LENGTH (str), port);
1166
1167 return str;
1168 }
1169 #undef FUNC_NAME
1170
1171 SCM_DEFINE (scm_seek, "seek", 3, 0, 0,
1172 (SCM object, SCM offset, SCM whence),
1173 "Sets the current position of @var{fd/port} to the integer @var{offset},\n"
1174 "which is interpreted according to the value of @var{whence}.\n\n"
1175 "One of the following variables should be supplied\n"
1176 "for @var{whence}:\n"
1177 "@defvar SEEK_SET\n"
1178 "Seek from the beginning of the file.\n"
1179 "@end defvar\n"
1180 "@defvar SEEK_CUR\n"
1181 "Seek from the current position.\n"
1182 "@end defvar\n"
1183 "@defvar SEEK_END\n"
1184 "Seek from the end of the file.\n"
1185 "@end defvar\n\n"
1186 "If @var{fd/port} is a file descriptor, the underlying system call is\n"
1187 "@code{lseek}. @var{port} may be a string port.\n\n"
1188 "The value returned is the new position in the file. This means that\n"
1189 "the current position of a port can be obtained using:\n"
1190 "@smalllisp\n"
1191 "(seek port 0 SEEK_CUR)\n"
1192 "@end smalllisp")
1193 #define FUNC_NAME s_scm_seek
1194 {
1195 off_t off;
1196 off_t rv;
1197 int how;
1198
1199 object = SCM_COERCE_OUTPORT (object);
1200
1201 off = SCM_NUM2LONG (2,offset);
1202 SCM_VALIDATE_INUM_COPY (3,whence,how);
1203 if (how != SEEK_SET && how != SEEK_CUR && how != SEEK_END)
1204 SCM_OUT_OF_RANGE (3, whence);
1205 if (SCM_OPPORTP (object))
1206 {
1207 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
1208
1209 if (!ptob->seek)
1210 SCM_MISC_ERROR ("port is not seekable",
1211 scm_cons (object, SCM_EOL));
1212 else
1213 rv = ptob->seek (object, off, how);
1214 }
1215 else /* file descriptor?. */
1216 {
1217 SCM_VALIDATE_INUM (1,object);
1218 rv = lseek (SCM_INUM (object), off, how);
1219 if (rv == -1)
1220 SCM_SYSERROR;
1221 }
1222 return scm_long2num (rv);
1223 }
1224 #undef FUNC_NAME
1225
1226 SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
1227 (SCM object, SCM length),
1228 "Truncates the object referred to by @var{obj} to at most @var{size} bytes.\n"
1229 "@var{obj} can be a string containing a file name or an integer file\n"
1230 "descriptor or a port. @var{size} may be omitted if @var{obj} is not\n"
1231 "a file name, in which case the truncation occurs at the current port.\n"
1232 "position.\n\n"
1233 "The return value is unspecified.")
1234 #define FUNC_NAME s_scm_truncate_file
1235 {
1236 int rv;
1237 off_t c_length;
1238
1239 /* object can be a port, fdes or filename. */
1240
1241 if (SCM_UNBNDP (length))
1242 {
1243 /* must supply length if object is a filename. */
1244 if (SCM_STRINGP (object))
1245 SCM_MISC_ERROR("must supply length if OBJECT is a filename",SCM_EOL);
1246
1247 length = scm_seek (object, SCM_INUM0, SCM_MAKINUM (SEEK_CUR));
1248 }
1249 c_length = SCM_NUM2LONG (2,length);
1250 if (c_length < 0)
1251 SCM_MISC_ERROR ("negative offset", SCM_EOL);
1252
1253 object = SCM_COERCE_OUTPORT (object);
1254 if (SCM_INUMP (object))
1255 {
1256 SCM_SYSCALL (rv = ftruncate (SCM_INUM (object), c_length));
1257 }
1258 else if (SCM_OPOUTPORTP (object))
1259 {
1260 scm_port *pt = SCM_PTAB_ENTRY (object);
1261 scm_ptob_descriptor *ptob = scm_ptobs + SCM_PTOBNUM (object);
1262
1263 if (!ptob->truncate)
1264 SCM_MISC_ERROR ("port is not truncatable", SCM_EOL);
1265 if (pt->rw_active == SCM_PORT_READ)
1266 scm_end_input (object);
1267 else if (pt->rw_active == SCM_PORT_WRITE)
1268 ptob->flush (object);
1269
1270 ptob->truncate (object, c_length);
1271 rv = 0;
1272 }
1273 else
1274 {
1275 SCM_VALIDATE_STRING (1, object);
1276 SCM_STRING_COERCE_0TERMINATION_X (object);
1277 SCM_SYSCALL (rv = truncate (SCM_STRING_CHARS (object), c_length));
1278 }
1279 if (rv == -1)
1280 SCM_SYSERROR;
1281 return SCM_UNSPECIFIED;
1282 }
1283 #undef FUNC_NAME
1284
1285 SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
1286 (SCM port),
1287 "Return the current line number for PORT.")
1288 #define FUNC_NAME s_scm_port_line
1289 {
1290 port = SCM_COERCE_OUTPORT (port);
1291 SCM_VALIDATE_OPENPORT (1,port);
1292 return SCM_MAKINUM (SCM_LINUM (port));
1293 }
1294 #undef FUNC_NAME
1295
1296 SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
1297 (SCM port, SCM line),
1298 "Set the current line number for PORT to LINE.")
1299 #define FUNC_NAME s_scm_set_port_line_x
1300 {
1301 port = SCM_COERCE_OUTPORT (port);
1302 SCM_VALIDATE_OPENPORT (1,port);
1303 SCM_VALIDATE_INUM (2,line);
1304 SCM_PTAB_ENTRY (port)->line_number = SCM_INUM (line);
1305 return SCM_UNSPECIFIED;
1306 }
1307 #undef FUNC_NAME
1308
1309 SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
1310 (SCM port),
1311 "@deffnx primitive port-line [input-port]\n"
1312 "Return the current column number or line number of @var{input-port},\n"
1313 "using the current input port if none is specified. If the number is\n"
1314 "unknown, the result is #f. Otherwise, the result is a 0-origin integer\n"
1315 "- i.e. the first character of the first line is line 0, column 0.\n"
1316 "(However, when you display a file position, for example in an error\n"
1317 "message, we recommand you add 1 to get 1-origin integers. This is\n"
1318 "because lines and column numbers traditionally start with 1, and that is\n"
1319 "what non-programmers will find most natural.)")
1320 #define FUNC_NAME s_scm_port_column
1321 {
1322 port = SCM_COERCE_OUTPORT (port);
1323 SCM_VALIDATE_OPENPORT (1,port);
1324 return SCM_MAKINUM (SCM_COL (port));
1325 }
1326 #undef FUNC_NAME
1327
1328 SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
1329 (SCM port, SCM column),
1330 "@deffnx primitive set-port-line! port line\n"
1331 "Set the current column or line number of @var{port}, using the\n"
1332 "current input port if none is specified.")
1333 #define FUNC_NAME s_scm_set_port_column_x
1334 {
1335 port = SCM_COERCE_OUTPORT (port);
1336 SCM_VALIDATE_OPENPORT (1,port);
1337 SCM_VALIDATE_INUM (2,column);
1338 SCM_PTAB_ENTRY (port)->column_number = SCM_INUM (column);
1339 return SCM_UNSPECIFIED;
1340 }
1341 #undef FUNC_NAME
1342
1343 SCM_DEFINE (scm_port_filename, "port-filename", 1, 0, 0,
1344 (SCM port),
1345 "Return the filename associated with @var{port}. This function returns\n"
1346 "the strings \"standard input\", \"standard output\" and \"standard error\"\n"
1347 "when called on the current input, output and error ports respectively.")
1348 #define FUNC_NAME s_scm_port_filename
1349 {
1350 port = SCM_COERCE_OUTPORT (port);
1351 SCM_VALIDATE_OPENPORT (1,port);
1352 return SCM_FILENAME (port);
1353 }
1354 #undef FUNC_NAME
1355
1356 SCM_DEFINE (scm_set_port_filename_x, "set-port-filename!", 2, 0, 0,
1357 (SCM port, SCM filename),
1358 "Change the filename associated with @var{port}, using the current input\n"
1359 "port if none is specified. Note that this does not change the port's\n"
1360 "source of data, but only the value that is returned by\n"
1361 "@code{port-filename} and reported in diagnostic output.")
1362 #define FUNC_NAME s_scm_set_port_filename_x
1363 {
1364 port = SCM_COERCE_OUTPORT (port);
1365 SCM_VALIDATE_OPENPORT (1,port);
1366 /* We allow the user to set the filename to whatever he likes. */
1367 SCM_SET_FILENAME (port, filename);
1368 return SCM_UNSPECIFIED;
1369 }
1370 #undef FUNC_NAME
1371
1372 #ifndef ttyname
1373 extern char * ttyname();
1374 #endif
1375
1376 void
1377 scm_print_port_mode (SCM exp, SCM port)
1378 {
1379 scm_puts (SCM_CLOSEDP (exp)
1380 ? "closed: "
1381 : (SCM_RDNG & SCM_CELL_WORD_0 (exp)
1382 ? (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
1383 ? "input-output: "
1384 : "input: ")
1385 : (SCM_WRTNG & SCM_CELL_WORD_0 (exp)
1386 ? "output: "
1387 : "bogus: ")),
1388 port);
1389 }
1390
1391 int
1392 scm_port_print (SCM exp, SCM port, scm_print_state *pstate)
1393 {
1394 char *type = SCM_PTOBNAME (SCM_PTOBNUM (exp));
1395 if (!type)
1396 type = "port";
1397 scm_puts ("#<", port);
1398 scm_print_port_mode (exp, port);
1399 scm_puts (type, port);
1400 scm_putc (' ', port);
1401 scm_intprint (SCM_CELL_WORD_1 (exp), 16, port);
1402 scm_putc ('>', port);
1403 return 1;
1404 }
1405
1406 void
1407 scm_ports_prehistory ()
1408 {
1409 scm_numptob = 0;
1410 scm_ptobs = (scm_ptob_descriptor *) malloc (sizeof (scm_ptob_descriptor));
1411 }
1412
1413 \f
1414
1415 /* Void ports. */
1416
1417 scm_bits_t scm_tc16_void_port = 0;
1418
1419 static int fill_input_void_port (SCM port)
1420 {
1421 return EOF;
1422 }
1423
1424 static void
1425 write_void_port (SCM port, const void *data, size_t size)
1426 {
1427 }
1428
1429 SCM
1430 scm_void_port (char *mode_str)
1431 {
1432 int mode_bits;
1433 SCM answer;
1434 scm_port * pt;
1435
1436 SCM_NEWCELL (answer);
1437 SCM_DEFER_INTS;
1438 mode_bits = scm_mode_bits (mode_str);
1439 pt = scm_add_to_port_table (answer);
1440 scm_port_non_buffer (pt);
1441 SCM_SETPTAB_ENTRY (answer, pt);
1442 SCM_SETSTREAM (answer, 0);
1443 SCM_SET_CELL_TYPE (answer, scm_tc16_void_port | mode_bits);
1444 SCM_ALLOW_INTS;
1445 return answer;
1446 }
1447
1448 SCM_DEFINE (scm_sys_make_void_port, "%make-void-port", 1, 0, 0,
1449 (SCM mode),
1450 "Create and return a new void port. A void port acts like\n"
1451 "/dev/null. The @var{mode} argument\n"
1452 "specifies the input/output modes for this port: see the\n"
1453 "documentation for @code{open-file} in @ref{File Ports}.")
1454 #define FUNC_NAME s_scm_sys_make_void_port
1455 {
1456 SCM_VALIDATE_STRING (1, mode);
1457 SCM_STRING_COERCE_0TERMINATION_X (mode);
1458 return scm_void_port (SCM_STRING_CHARS (mode));
1459 }
1460 #undef FUNC_NAME
1461
1462 \f
1463 /* Initialization. */
1464
1465 void
1466 scm_init_ports ()
1467 {
1468 /* lseek() symbols. */
1469 scm_sysintern ("SEEK_SET", SCM_MAKINUM (SEEK_SET));
1470 scm_sysintern ("SEEK_CUR", SCM_MAKINUM (SEEK_CUR));
1471 scm_sysintern ("SEEK_END", SCM_MAKINUM (SEEK_END));
1472
1473 scm_tc16_void_port = scm_make_port_type ("void", fill_input_void_port,
1474 write_void_port);
1475 #ifndef SCM_MAGIC_SNARFER
1476 #include "libguile/ports.x"
1477 #endif
1478 }
1479
1480 /*
1481 Local Variables:
1482 c-file-style: "gnu"
1483 End:
1484 */