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