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